wp/wp-includes/class-wp-post.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 18 be944660c56a
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    15  * @property string $page_template
    15  * @property string $page_template
    16  *
    16  *
    17  * @property-read array  $ancestors
    17  * @property-read array  $ancestors
    18  * @property-read int    $post_category
    18  * @property-read int    $post_category
    19  * @property-read string $tag_input
    19  * @property-read string $tag_input
    20  *
       
    21  */
    20  */
    22 final class WP_Post {
    21 final class WP_Post {
    23 
    22 
    24 	/**
    23 	/**
    25 	 * Post ID.
    24 	 * Post ID.
   221 
   220 
   222 	/**
   221 	/**
   223 	 * Retrieve WP_Post instance.
   222 	 * Retrieve WP_Post instance.
   224 	 *
   223 	 *
   225 	 * @since 3.5.0
   224 	 * @since 3.5.0
   226 	 * @static
       
   227 	 *
   225 	 *
   228 	 * @global wpdb $wpdb WordPress database abstraction object.
   226 	 * @global wpdb $wpdb WordPress database abstraction object.
   229 	 *
   227 	 *
   230 	 * @param int $post_id Post ID.
   228 	 * @param int $post_id Post ID.
   231 	 * @return WP_Post|false Post object, false otherwise.
   229 	 * @return WP_Post|false Post object, false otherwise.
   241 		$_post = wp_cache_get( $post_id, 'posts' );
   239 		$_post = wp_cache_get( $post_id, 'posts' );
   242 
   240 
   243 		if ( ! $_post ) {
   241 		if ( ! $_post ) {
   244 			$_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id ) );
   242 			$_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id ) );
   245 
   243 
   246 			if ( ! $_post )
   244 			if ( ! $_post ) {
   247 				return false;
   245 				return false;
       
   246 			}
   248 
   247 
   249 			$_post = sanitize_post( $_post, 'raw' );
   248 			$_post = sanitize_post( $_post, 'raw' );
   250 			wp_cache_add( $_post->ID, $_post, 'posts' );
   249 			wp_cache_add( $_post->ID, $_post, 'posts' );
   251 		} elseif ( empty( $_post->filter ) ) {
   250 		} elseif ( empty( $_post->filter ) ) {
   252 			$_post = sanitize_post( $_post, 'raw' );
   251 			$_post = sanitize_post( $_post, 'raw' );
   261 	 * @since 3.5.0
   260 	 * @since 3.5.0
   262 	 *
   261 	 *
   263 	 * @param WP_Post|object $post Post object.
   262 	 * @param WP_Post|object $post Post object.
   264 	 */
   263 	 */
   265 	public function __construct( $post ) {
   264 	public function __construct( $post ) {
   266 		foreach ( get_object_vars( $post ) as $key => $value )
   265 		foreach ( get_object_vars( $post ) as $key => $value ) {
   267 			$this->$key = $value;
   266 			$this->$key = $value;
       
   267 		}
   268 	}
   268 	}
   269 
   269 
   270 	/**
   270 	/**
   271 	 * Isset-er.
   271 	 * Isset-er.
   272 	 *
   272 	 *
   274 	 *
   274 	 *
   275 	 * @param string $key Property to check if set.
   275 	 * @param string $key Property to check if set.
   276 	 * @return bool
   276 	 * @return bool
   277 	 */
   277 	 */
   278 	public function __isset( $key ) {
   278 	public function __isset( $key ) {
   279 		if ( 'ancestors' == $key )
   279 		if ( 'ancestors' === $key ) {
   280 			return true;
   280 			return true;
   281 
   281 		}
   282 		if ( 'page_template' == $key )
   282 
       
   283 		if ( 'page_template' === $key ) {
   283 			return true;
   284 			return true;
   284 
   285 		}
   285 		if ( 'post_category' == $key )
   286 
   286 		   return true;
   287 		if ( 'post_category' === $key ) {
   287 
   288 			return true;
   288 		if ( 'tags_input' == $key )
   289 		}
   289 		   return true;
   290 
       
   291 		if ( 'tags_input' === $key ) {
       
   292 			return true;
       
   293 		}
   290 
   294 
   291 		return metadata_exists( 'post', $this->ID, $key );
   295 		return metadata_exists( 'post', $this->ID, $key );
   292 	}
   296 	}
   293 
   297 
   294 	/**
   298 	/**
   298 	 *
   302 	 *
   299 	 * @param string $key Key to get.
   303 	 * @param string $key Key to get.
   300 	 * @return mixed
   304 	 * @return mixed
   301 	 */
   305 	 */
   302 	public function __get( $key ) {
   306 	public function __get( $key ) {
   303 		if ( 'page_template' == $key && $this->__isset( $key ) ) {
   307 		if ( 'page_template' === $key && $this->__isset( $key ) ) {
   304 			return get_post_meta( $this->ID, '_wp_page_template', true );
   308 			return get_post_meta( $this->ID, '_wp_page_template', true );
   305 		}
   309 		}
   306 
   310 
   307 		if ( 'post_category' == $key ) {
   311 		if ( 'post_category' === $key ) {
   308 			if ( is_object_in_taxonomy( $this->post_type, 'category' ) )
   312 			if ( is_object_in_taxonomy( $this->post_type, 'category' ) ) {
   309 				$terms = get_the_terms( $this, 'category' );
   313 				$terms = get_the_terms( $this, 'category' );
   310 
   314 			}
   311 			if ( empty( $terms ) )
   315 
       
   316 			if ( empty( $terms ) ) {
   312 				return array();
   317 				return array();
       
   318 			}
   313 
   319 
   314 			return wp_list_pluck( $terms, 'term_id' );
   320 			return wp_list_pluck( $terms, 'term_id' );
   315 		}
   321 		}
   316 
   322 
   317 		if ( 'tags_input' == $key ) {
   323 		if ( 'tags_input' === $key ) {
   318 			if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) )
   324 			if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) ) {
   319 				$terms = get_the_terms( $this, 'post_tag' );
   325 				$terms = get_the_terms( $this, 'post_tag' );
   320 
   326 			}
   321 			if ( empty( $terms ) )
   327 
       
   328 			if ( empty( $terms ) ) {
   322 				return array();
   329 				return array();
       
   330 			}
   323 
   331 
   324 			return wp_list_pluck( $terms, 'name' );
   332 			return wp_list_pluck( $terms, 'name' );
   325 		}
   333 		}
   326 
   334 
   327 		// Rest of the values need filtering.
   335 		// Rest of the values need filtering.
   328 		if ( 'ancestors' == $key )
   336 		if ( 'ancestors' === $key ) {
   329 			$value = get_post_ancestors( $this );
   337 			$value = get_post_ancestors( $this );
   330 		else
   338 		} else {
   331 			$value = get_post_meta( $this->ID, $key, true );
   339 			$value = get_post_meta( $this->ID, $key, true );
   332 
   340 		}
   333 		if ( $this->filter )
   341 
       
   342 		if ( $this->filter ) {
   334 			$value = sanitize_post_field( $key, $value, $this->ID, $this->filter );
   343 			$value = sanitize_post_field( $key, $value, $this->ID, $this->filter );
       
   344 		}
   335 
   345 
   336 		return $value;
   346 		return $value;
   337 	}
   347 	}
   338 
   348 
   339 	/**
   349 	/**
   340 	 * {@Missing Summary}
   350 	 * {@Missing Summary}
   341 	 *
   351 	 *
   342 	 * @since 3.5.0
   352 	 * @since 3.5.0
   343 	 *
   353 	 *
   344 	 * @param string $filter Filter.
   354 	 * @param string $filter Filter.
   345 	 * @return self|array|bool|object|WP_Post
   355 	 * @return array|bool|object|WP_Post
   346 	 */
   356 	 */
   347 	public function filter( $filter ) {
   357 	public function filter( $filter ) {
   348 		if ( $this->filter == $filter )
   358 		if ( $this->filter === $filter ) {
   349 			return $this;
   359 			return $this;
   350 
   360 		}
   351 		if ( $filter == 'raw' )
   361 
       
   362 		if ( 'raw' === $filter ) {
   352 			return self::get_instance( $this->ID );
   363 			return self::get_instance( $this->ID );
       
   364 		}
   353 
   365 
   354 		return sanitize_post( $this, $filter );
   366 		return sanitize_post( $this, $filter );
   355 	}
   367 	}
   356 
   368 
   357 	/**
   369 	/**
   363 	 */
   375 	 */
   364 	public function to_array() {
   376 	public function to_array() {
   365 		$post = get_object_vars( $this );
   377 		$post = get_object_vars( $this );
   366 
   378 
   367 		foreach ( array( 'ancestors', 'page_template', 'post_category', 'tags_input' ) as $key ) {
   379 		foreach ( array( 'ancestors', 'page_template', 'post_category', 'tags_input' ) as $key ) {
   368 			if ( $this->__isset( $key ) )
   380 			if ( $this->__isset( $key ) ) {
   369 				$post[ $key ] = $this->__get( $key );
   381 				$post[ $key ] = $this->__get( $key );
       
   382 			}
   370 		}
   383 		}
   371 
   384 
   372 		return $post;
   385 		return $post;
   373 	}
   386 	}
   374 }
   387 }