2015-08-26 14:40:21 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2015-09-22 15:10:24 +02:00
|
|
|
* Post API: WP_Post class
|
2015-08-26 14:40:21 +02:00
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage Post
|
2015-09-22 15:10:24 +02:00
|
|
|
* @since 4.4.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Core class used to implement the WP_Post object.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
*
|
|
|
|
* @property string $page_template
|
|
|
|
*
|
2022-05-21 14:07:13 +02:00
|
|
|
* @property-read int[] $ancestors
|
|
|
|
* @property-read int[] $post_category
|
|
|
|
* @property-read string[] $tags_input
|
2015-08-26 14:40:21 +02:00
|
|
|
*/
|
|
|
|
final class WP_Post {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Post ID.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $ID;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ID of post author.
|
|
|
|
*
|
|
|
|
* A numeric string, for compatibility reasons.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $post_author = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The post's local publication time.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $post_date = '0000-00-00 00:00:00';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The post's GMT publication time.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $post_date_gmt = '0000-00-00 00:00:00';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The post's content.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $post_content = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The post's title.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $post_title = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The post's excerpt.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $post_excerpt = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The post's status.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $post_status = 'publish';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether comments are allowed.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $comment_status = 'open';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether pings are allowed.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $ping_status = 'open';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The post's password in plain text.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $post_password = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The post's slug.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $post_name = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* URLs queued to be pinged.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $to_ping = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* URLs that have been pinged.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $pinged = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The post's local modified time.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $post_modified = '0000-00-00 00:00:00';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The post's GMT modified time.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $post_modified_gmt = '0000-00-00 00:00:00';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A utility DB field for post content.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $post_content_filtered = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ID of a post's parent post.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $post_parent = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The unique identifier for a post, not necessarily a URL, used as the feed GUID.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $guid = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A field used for ordering posts.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $menu_order = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The post's type, like post or page.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $post_type = 'post';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An attachment's mime type.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $post_mime_type = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cached comment count.
|
|
|
|
*
|
|
|
|
* A numeric string, for compatibility reasons.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $comment_count = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores the post object's sanitization level.
|
|
|
|
*
|
|
|
|
* Does not correspond to a DB field.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $filter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve WP_Post instance.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
2015-08-26 14:40:21 +02:00
|
|
|
*
|
2015-10-15 01:44:25 +02:00
|
|
|
* @global wpdb $wpdb WordPress database abstraction object.
|
2015-08-26 14:40:21 +02:00
|
|
|
*
|
|
|
|
* @param int $post_id Post ID.
|
|
|
|
* @return WP_Post|false Post object, false otherwise.
|
|
|
|
*/
|
|
|
|
public static function get_instance( $post_id ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
2017-01-26 17:53:41 +01:00
|
|
|
$post_id = (int) $post_id;
|
|
|
|
if ( ! $post_id ) {
|
2015-08-26 14:40:21 +02:00
|
|
|
return false;
|
2016-08-26 21:09:27 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 14:40:21 +02:00
|
|
|
$_post = wp_cache_get( $post_id, 'posts' );
|
|
|
|
|
|
|
|
if ( ! $_post ) {
|
|
|
|
$_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id ) );
|
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! $_post ) {
|
2015-08-26 14:40:21 +02:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 14:40:21 +02:00
|
|
|
|
|
|
|
$_post = sanitize_post( $_post, 'raw' );
|
|
|
|
wp_cache_add( $_post->ID, $_post, 'posts' );
|
2022-04-01 05:18:05 +02:00
|
|
|
} elseif ( empty( $_post->filter ) || 'raw' !== $_post->filter ) {
|
2015-08-26 14:40:21 +02:00
|
|
|
$_post = sanitize_post( $_post, 'raw' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return new WP_Post( $_post );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2015-08-26 14:40:21 +02:00
|
|
|
* @param WP_Post|object $post Post object.
|
|
|
|
*/
|
|
|
|
public function __construct( $post ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
foreach ( get_object_vars( $post ) as $key => $value ) {
|
2015-08-26 14:40:21 +02:00
|
|
|
$this->$key = $value;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 14:40:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Isset-er.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2015-08-26 14:40:21 +02:00
|
|
|
* @param string $key Property to check if set.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function __isset( $key ) {
|
2019-03-15 22:55:50 +01:00
|
|
|
if ( 'ancestors' === $key ) {
|
2015-08-26 14:40:21 +02:00
|
|
|
return true;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 14:40:21 +02:00
|
|
|
|
2019-03-15 22:55:50 +01:00
|
|
|
if ( 'page_template' === $key ) {
|
Posts, Post Types: Add support for post type templates.
WordPress has supported custom page templates for over 12 years, allowing developers to create various layouts for specific pages.
While this feature is very helpful, it has always been limited to the 'page' post type and not was not available to other post types.
By opening up the page template functionality to all post types, we continue to improve the template hierarchy's flexibility.
In addition to the `Template Name` file header, the post types supported by a template can be specified using `Template Post Type: post, foo, bar`.
When at least one template exists for a post type, the 'Post Attributes' meta box will be displayed in the back end, without the need to add post type support for `'page-attributes'`. 'Post Attributes' can be customized per post type using the `'attributes'` label when registering a post type.
Props johnbillion, Mte90, dipesh.kakadiya, swissspidy.
Fixes #18375.
Built from https://develop.svn.wordpress.org/trunk@38951
git-svn-id: http://core.svn.wordpress.org/trunk@38894 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-10-26 10:07:30 +02:00
|
|
|
return true;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 14:40:21 +02:00
|
|
|
|
2019-03-15 22:55:50 +01:00
|
|
|
if ( 'post_category' === $key ) {
|
2017-11-27 00:57:55 +01:00
|
|
|
return true;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 14:40:21 +02:00
|
|
|
|
2019-03-15 22:55:50 +01:00
|
|
|
if ( 'tags_input' === $key ) {
|
2017-11-27 00:57:55 +01:00
|
|
|
return true;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 14:40:21 +02:00
|
|
|
|
|
|
|
return metadata_exists( 'post', $this->ID, $key );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2015-08-26 14:40:21 +02:00
|
|
|
* @param string $key Key to get.
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __get( $key ) {
|
2019-03-15 22:55:50 +01:00
|
|
|
if ( 'page_template' === $key && $this->__isset( $key ) ) {
|
2015-08-26 14:40:21 +02:00
|
|
|
return get_post_meta( $this->ID, '_wp_page_template', true );
|
|
|
|
}
|
|
|
|
|
2019-03-15 22:55:50 +01:00
|
|
|
if ( 'post_category' === $key ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( is_object_in_taxonomy( $this->post_type, 'category' ) ) {
|
2015-08-26 14:40:21 +02:00
|
|
|
$terms = get_the_terms( $this, 'category' );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 14:40:21 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( empty( $terms ) ) {
|
2015-08-26 14:40:21 +02:00
|
|
|
return array();
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 14:40:21 +02:00
|
|
|
|
|
|
|
return wp_list_pluck( $terms, 'term_id' );
|
|
|
|
}
|
|
|
|
|
2019-03-15 22:55:50 +01:00
|
|
|
if ( 'tags_input' === $key ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) ) {
|
2015-08-26 14:40:21 +02:00
|
|
|
$terms = get_the_terms( $this, 'post_tag' );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 14:40:21 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( empty( $terms ) ) {
|
2015-08-26 14:40:21 +02:00
|
|
|
return array();
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 14:40:21 +02:00
|
|
|
|
|
|
|
return wp_list_pluck( $terms, 'name' );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rest of the values need filtering.
|
2019-03-15 22:55:50 +01:00
|
|
|
if ( 'ancestors' === $key ) {
|
2015-08-26 14:40:21 +02:00
|
|
|
$value = get_post_ancestors( $this );
|
2017-12-01 00:11:00 +01:00
|
|
|
} else {
|
2015-08-26 14:40:21 +02:00
|
|
|
$value = get_post_meta( $this->ID, $key, true );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 14:40:21 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( $this->filter ) {
|
2015-08-26 14:40:21 +02:00
|
|
|
$value = sanitize_post_field( $key, $value, $this->ID, $this->filter );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 14:40:21 +02:00
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@Missing Summary}
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2015-08-26 14:40:21 +02:00
|
|
|
* @param string $filter Filter.
|
2020-11-09 16:15:08 +01:00
|
|
|
* @return WP_Post
|
2015-08-26 14:40:21 +02:00
|
|
|
*/
|
|
|
|
public function filter( $filter ) {
|
2019-03-15 22:55:50 +01:00
|
|
|
if ( $this->filter === $filter ) {
|
2015-08-26 14:40:21 +02:00
|
|
|
return $this;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 14:40:21 +02:00
|
|
|
|
2019-03-15 22:55:50 +01:00
|
|
|
if ( 'raw' === $filter ) {
|
2015-08-26 14:40:21 +02:00
|
|
|
return self::get_instance( $this->ID );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 14:40:21 +02:00
|
|
|
|
|
|
|
return sanitize_post( $this, $filter );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert object to array.
|
|
|
|
*
|
2017-07-13 14:08:41 +02:00
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2015-08-26 14:40:21 +02:00
|
|
|
* @return array Object as array.
|
|
|
|
*/
|
|
|
|
public function to_array() {
|
|
|
|
$post = get_object_vars( $this );
|
|
|
|
|
|
|
|
foreach ( array( 'ancestors', 'page_template', 'post_category', 'tags_input' ) as $key ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( $this->__isset( $key ) ) {
|
2015-08-26 14:40:21 +02:00
|
|
|
$post[ $key ] = $this->__get( $key );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 14:40:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $post;
|
|
|
|
}
|
|
|
|
}
|