class-wp-post.php 6.3 KB

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