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