[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> class-wp-post.php (source)

   1  <?php
   2  /**
   3   * Post API: WP_Post class
   4   *
   5   * @package WordPress
   6   * @subpackage Post
   7   * @since 4.4.0
   8   */
   9  
  10  /**
  11   * Core class used to implement the WP_Post object.
  12   *
  13   * @since 3.5.0
  14   *
  15   * @property string $page_template
  16   *
  17   * @property-read int[]    $ancestors
  18   * @property-read int[]    $post_category
  19   * @property-read string[] $tags_input
  20   */
  21  #[AllowDynamicProperties]
  22  final class WP_Post {
  23  
  24      /**
  25       * Post ID.
  26       *
  27       * @since 3.5.0
  28       * @var int
  29       */
  30      public $ID;
  31  
  32      /**
  33       * ID of post author.
  34       *
  35       * A numeric string, for compatibility reasons.
  36       *
  37       * @since 3.5.0
  38       * @var string
  39       */
  40      public $post_author = 0;
  41  
  42      /**
  43       * The post's local publication time.
  44       *
  45       * @since 3.5.0
  46       * @var string
  47       */
  48      public $post_date = '0000-00-00 00:00:00';
  49  
  50      /**
  51       * The post's GMT publication time.
  52       *
  53       * @since 3.5.0
  54       * @var string
  55       */
  56      public $post_date_gmt = '0000-00-00 00:00:00';
  57  
  58      /**
  59       * The post's content.
  60       *
  61       * @since 3.5.0
  62       * @var string
  63       */
  64      public $post_content = '';
  65  
  66      /**
  67       * The post's title.
  68       *
  69       * @since 3.5.0
  70       * @var string
  71       */
  72      public $post_title = '';
  73  
  74      /**
  75       * The post's excerpt.
  76       *
  77       * @since 3.5.0
  78       * @var string
  79       */
  80      public $post_excerpt = '';
  81  
  82      /**
  83       * The post's status.
  84       *
  85       * @since 3.5.0
  86       * @var string
  87       */
  88      public $post_status = 'publish';
  89  
  90      /**
  91       * Whether comments are allowed.
  92       *
  93       * @since 3.5.0
  94       * @var string
  95       */
  96      public $comment_status = 'open';
  97  
  98      /**
  99       * Whether pings are allowed.
 100       *
 101       * @since 3.5.0
 102       * @var string
 103       */
 104      public $ping_status = 'open';
 105  
 106      /**
 107       * The post's password in plain text.
 108       *
 109       * @since 3.5.0
 110       * @var string
 111       */
 112      public $post_password = '';
 113  
 114      /**
 115       * The post's slug.
 116       *
 117       * @since 3.5.0
 118       * @var string
 119       */
 120      public $post_name = '';
 121  
 122      /**
 123       * URLs queued to be pinged.
 124       *
 125       * @since 3.5.0
 126       * @var string
 127       */
 128      public $to_ping = '';
 129  
 130      /**
 131       * URLs that have been pinged.
 132       *
 133       * @since 3.5.0
 134       * @var string
 135       */
 136      public $pinged = '';
 137  
 138      /**
 139       * The post's local modified time.
 140       *
 141       * @since 3.5.0
 142       * @var string
 143       */
 144      public $post_modified = '0000-00-00 00:00:00';
 145  
 146      /**
 147       * The post's GMT modified time.
 148       *
 149       * @since 3.5.0
 150       * @var string
 151       */
 152      public $post_modified_gmt = '0000-00-00 00:00:00';
 153  
 154      /**
 155       * A utility DB field for post content.
 156       *
 157       * @since 3.5.0
 158       * @var string
 159       */
 160      public $post_content_filtered = '';
 161  
 162      /**
 163       * ID of a post's parent post.
 164       *
 165       * @since 3.5.0
 166       * @var int
 167       */
 168      public $post_parent = 0;
 169  
 170      /**
 171       * The unique identifier for a post, not necessarily a URL, used as the feed GUID.
 172       *
 173       * @since 3.5.0
 174       * @var string
 175       */
 176      public $guid = '';
 177  
 178      /**
 179       * A field used for ordering posts.
 180       *
 181       * @since 3.5.0
 182       * @var int
 183       */
 184      public $menu_order = 0;
 185  
 186      /**
 187       * The post's type, like post or page.
 188       *
 189       * @since 3.5.0
 190       * @var string
 191       */
 192      public $post_type = 'post';
 193  
 194      /**
 195       * An attachment's mime type.
 196       *
 197       * @since 3.5.0
 198       * @var string
 199       */
 200      public $post_mime_type = '';
 201  
 202      /**
 203       * Cached comment count.
 204       *
 205       * A numeric string, for compatibility reasons.
 206       *
 207       * @since 3.5.0
 208       * @var string
 209       */
 210      public $comment_count = 0;
 211  
 212      /**
 213       * Stores the post object's sanitization level.
 214       *
 215       * Does not correspond to a DB field.
 216       *
 217       * @since 3.5.0
 218       * @var string
 219       */
 220      public $filter;
 221  
 222      /**
 223       * Retrieve WP_Post instance.
 224       *
 225       * @since 3.5.0
 226       *
 227       * @global wpdb $wpdb WordPress database abstraction object.
 228       *
 229       * @param int $post_id Post ID.
 230       * @return WP_Post|false Post object, false otherwise.
 231       */
 232  	public static function get_instance( $post_id ) {
 233          global $wpdb;
 234  
 235          $post_id = (int) $post_id;
 236          if ( ! $post_id ) {
 237              return false;
 238          }
 239  
 240          $_post = wp_cache_get( $post_id, 'posts' );
 241  
 242          if ( ! $_post ) {
 243              $_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id ) );
 244  
 245              if ( ! $_post ) {
 246                  return false;
 247              }
 248  
 249              $_post = sanitize_post( $_post, 'raw' );
 250              wp_cache_add( $_post->ID, $_post, 'posts' );
 251          } elseif ( empty( $_post->filter ) || 'raw' !== $_post->filter ) {
 252              $_post = sanitize_post( $_post, 'raw' );
 253          }
 254  
 255          return new WP_Post( $_post );
 256      }
 257  
 258      /**
 259       * Constructor.
 260       *
 261       * @since 3.5.0
 262       *
 263       * @param WP_Post|object $post Post object.
 264       */
 265  	public function __construct( $post ) {
 266          foreach ( get_object_vars( $post ) as $key => $value ) {
 267              $this->$key = $value;
 268          }
 269      }
 270  
 271      /**
 272       * Isset-er.
 273       *
 274       * @since 3.5.0
 275       *
 276       * @param string $key Property to check if set.
 277       * @return bool
 278       */
 279  	public function __isset( $key ) {
 280          if ( 'ancestors' === $key ) {
 281              return true;
 282          }
 283  
 284          if ( 'page_template' === $key ) {
 285              return true;
 286          }
 287  
 288          if ( 'post_category' === $key ) {
 289              return true;
 290          }
 291  
 292          if ( 'tags_input' === $key ) {
 293              return true;
 294          }
 295  
 296          return metadata_exists( 'post', $this->ID, $key );
 297      }
 298  
 299      /**
 300       * Getter.
 301       *
 302       * @since 3.5.0
 303       *
 304       * @param string $key Key to get.
 305       * @return mixed
 306       */
 307  	public function __get( $key ) {
 308          if ( 'page_template' === $key && $this->__isset( $key ) ) {
 309              return get_post_meta( $this->ID, '_wp_page_template', true );
 310          }
 311  
 312          if ( 'post_category' === $key ) {
 313              if ( is_object_in_taxonomy( $this->post_type, 'category' ) ) {
 314                  $terms = get_the_terms( $this, 'category' );
 315              }
 316  
 317              if ( empty( $terms ) ) {
 318                  return array();
 319              }
 320  
 321              return wp_list_pluck( $terms, 'term_id' );
 322          }
 323  
 324          if ( 'tags_input' === $key ) {
 325              if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) ) {
 326                  $terms = get_the_terms( $this, 'post_tag' );
 327              }
 328  
 329              if ( empty( $terms ) ) {
 330                  return array();
 331              }
 332  
 333              return wp_list_pluck( $terms, 'name' );
 334          }
 335  
 336          // Rest of the values need filtering.
 337          if ( 'ancestors' === $key ) {
 338              $value = get_post_ancestors( $this );
 339          } else {
 340              $value = get_post_meta( $this->ID, $key, true );
 341          }
 342  
 343          if ( $this->filter ) {
 344              $value = sanitize_post_field( $key, $value, $this->ID, $this->filter );
 345          }
 346  
 347          return $value;
 348      }
 349  
 350      /**
 351       * {@Missing Summary}
 352       *
 353       * @since 3.5.0
 354       *
 355       * @param string $filter Filter.
 356       * @return WP_Post
 357       */
 358  	public function filter( $filter ) {
 359          if ( $this->filter === $filter ) {
 360              return $this;
 361          }
 362  
 363          if ( 'raw' === $filter ) {
 364              return self::get_instance( $this->ID );
 365          }
 366  
 367          return sanitize_post( $this, $filter );
 368      }
 369  
 370      /**
 371       * Convert object to array.
 372       *
 373       * @since 3.5.0
 374       *
 375       * @return array Object as array.
 376       */
 377  	public function to_array() {
 378          $post = get_object_vars( $this );
 379  
 380          foreach ( array( 'ancestors', 'page_template', 'post_category', 'tags_input' ) as $key ) {
 381              if ( $this->__isset( $key ) ) {
 382                  $post[ $key ] = $this->__get( $key );
 383              }
 384          }
 385  
 386          return $post;
 387      }
 388  }


Generated : Sat Dec 21 08:20:01 2024 Cross-referenced by PHPXref