class-wp-post.php 6.3 KB

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