Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

342 řádky
5.7 KiB

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