class-wp-post.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. */
  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. * @static
  201. *
  202. * @global wpdb $wpdb WordPress database abstraction object.
  203. *
  204. * @param int $post_id Post ID.
  205. * @return WP_Post|false Post object, false otherwise.
  206. */
  207. public static function get_instance( $post_id ) {
  208. global $wpdb;
  209. $post_id = (int) $post_id;
  210. if ( ! $post_id ) {
  211. return false;
  212. }
  213. $_post = wp_cache_get( $post_id, 'posts' );
  214. if ( ! $_post ) {
  215. $_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id ) );
  216. if ( ! $_post )
  217. return false;
  218. $_post = sanitize_post( $_post, 'raw' );
  219. wp_cache_add( $_post->ID, $_post, 'posts' );
  220. } elseif ( empty( $_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. * 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. if ( 'page_template' == $key )
  248. return true;
  249. if ( 'post_category' == $key )
  250. return true;
  251. if ( 'tags_input' == $key )
  252. return true;
  253. return metadata_exists( 'post', $this->ID, $key );
  254. }
  255. /**
  256. * Getter.
  257. *
  258. * @since 3.5.0
  259. *
  260. * @param string $key Key to get.
  261. * @return mixed
  262. */
  263. public function __get( $key ) {
  264. if ( 'page_template' == $key && $this->__isset( $key ) ) {
  265. return get_post_meta( $this->ID, '_wp_page_template', true );
  266. }
  267. if ( 'post_category' == $key ) {
  268. if ( is_object_in_taxonomy( $this->post_type, 'category' ) )
  269. $terms = get_the_terms( $this, 'category' );
  270. if ( empty( $terms ) )
  271. return array();
  272. return wp_list_pluck( $terms, 'term_id' );
  273. }
  274. if ( 'tags_input' == $key ) {
  275. if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) )
  276. $terms = get_the_terms( $this, 'post_tag' );
  277. if ( empty( $terms ) )
  278. return array();
  279. return wp_list_pluck( $terms, 'name' );
  280. }
  281. // Rest of the values need filtering.
  282. if ( 'ancestors' == $key )
  283. $value = get_post_ancestors( $this );
  284. else
  285. $value = get_post_meta( $this->ID, $key, true );
  286. if ( $this->filter )
  287. $value = sanitize_post_field( $key, $value, $this->ID, $this->filter );
  288. return $value;
  289. }
  290. /**
  291. * {@Missing Summary}
  292. *
  293. * @since 3.5.0
  294. *
  295. * @param string $filter Filter.
  296. * @return self|array|bool|object|WP_Post
  297. */
  298. public function filter( $filter ) {
  299. if ( $this->filter == $filter )
  300. return $this;
  301. if ( $filter == 'raw' )
  302. return self::get_instance( $this->ID );
  303. return sanitize_post( $this, $filter );
  304. }
  305. /**
  306. * Convert object to array.
  307. *
  308. * @since 3.5.0
  309. *
  310. * @return array Object as array.
  311. */
  312. public function to_array() {
  313. $post = get_object_vars( $this );
  314. foreach ( array( 'ancestors', 'page_template', 'post_category', 'tags_input' ) as $key ) {
  315. if ( $this->__isset( $key ) )
  316. $post[ $key ] = $this->__get( $key );
  317. }
  318. return $post;
  319. }
  320. }