| 1 | <?php |
| 2 | /** |
| 3 | * Post API: WP_Post_Type class |
| 4 | * |
| 5 | * @package WordPress |
| 6 | * @subpackage Post |
| 7 | * @since 4.6.0 |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Core class used for interacting with post types. |
| 12 | * |
| 13 | * @since 4.6.0 |
| 14 | * |
| 15 | */ |
| 16 | final class WP_Post_Type { |
| 17 | /** |
| 18 | * Post type key. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | public $name; |
| 23 | |
| 24 | /** |
| 25 | * Name of the post type shown in the menu. Usually plural. |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | public $label; |
| 30 | |
| 31 | /** |
| 32 | * An array of labels for this post type. |
| 33 | * |
| 34 | * If not set, post labels are inherited for non-hierarchical types |
| 35 | * and page labels for hierarchical ones. |
| 36 | * |
| 37 | * @see get_post_type_labels() |
| 38 | * |
| 39 | * @var array |
| 40 | */ |
| 41 | public $labels; |
| 42 | |
| 43 | /** |
| 44 | * A short descriptive summary of what the post type is. |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | public $description; |
| 49 | |
| 50 | /** |
| 51 | * Whether a post type is intended for use publicly either via the admin interface or by front-end users. |
| 52 | * |
| 53 | * While the default settings of $exclude_from_search, $publicly_queryable, $show_ui, |
| 54 | * and $show_in_nav_menus are inherited from public, each does not |
| 55 | * rely on this relationship and controls a very specific intention. |
| 56 | * |
| 57 | * @var bool |
| 58 | */ |
| 59 | public $public = false; |
| 60 | |
| 61 | /** |
| 62 | * Whether the post type is hierarchical (e.g. page). |
| 63 | * |
| 64 | * @var bool |
| 65 | */ |
| 66 | public $hierarchical = false; |
| 67 | |
| 68 | /** |
| 69 | * Whether to exclude posts with this post type from front end search |
| 70 | * results. |
| 71 | * |
| 72 | * Default is the opposite value of $public. |
| 73 | * |
| 74 | * @var bool |
| 75 | */ |
| 76 | public $exclude_from_search = null; |
| 77 | |
| 78 | /** |
| 79 | * Whether queries can be performed on the front end for the post type as part of {@see parse_request()}. |
| 80 | * |
| 81 | * @var bool |
| 82 | */ |
| 83 | public $publicly_queryable = null; |
| 84 | |
| 85 | /** |
| 86 | * Whether to generate and allow a UI for managing this post type in the admin. |
| 87 | * |
| 88 | * Default is value of $public. |
| 89 | * |
| 90 | * @var bool |
| 91 | */ |
| 92 | public $show_ui = null; |
| 93 | |
| 94 | /** |
| 95 | * Where to show the post type in the admin menu. |
| 96 | * |
| 97 | * To work, $show_ui must be true. If true, the post type is shown in its own top level menu. If false, no menu is shown. |
| 98 | * If a string of an existing top level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post |
| 99 | * type will be placed as a sub-menu of that. |
| 100 | * |
| 101 | * Default is value of $show_ui. |
| 102 | * |
| 103 | * @var bool |
| 104 | */ |
| 105 | public $show_in_menu = null; |
| 106 | |
| 107 | /** |
| 108 | * Makes this post type available for selection in navigation menus. |
| 109 | * |
| 110 | * Default is value $public. |
| 111 | * |
| 112 | * @var bool |
| 113 | */ |
| 114 | public $show_in_nav_menus = null; |
| 115 | |
| 116 | /** |
| 117 | * Makes this post type available via the admin bar. |
| 118 | * |
| 119 | * Default is value of $show_in_menu. |
| 120 | * |
| 121 | * @var bool |
| 122 | */ |
| 123 | public $show_in_admin_bar = null; |
| 124 | |
| 125 | /** |
| 126 | * The position in the menu order the post type should appear. |
| 127 | * |
| 128 | * To work, $show_in_menu must be true. Default null (at the bottom). |
| 129 | * |
| 130 | * @var int |
| 131 | */ |
| 132 | public $menu_position = null; |
| 133 | |
| 134 | /** |
| 135 | * The url to the icon to be used for this menu. |
| 136 | * |
| 137 | * Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme. |
| 138 | * This should begin with 'data:image/svg+xml;base64,'. Pass the name of a Dashicons helper class to use a font icon, e.g. |
| 139 | * 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS. |
| 140 | * |
| 141 | * Defaults to use the posts icon. |
| 142 | * |
| 143 | * @var string |
| 144 | */ |
| 145 | public $menu_icon = null; |
| 146 | |
| 147 | /** |
| 148 | * The string to use to build the read, edit, and delete capabilities. |
| 149 | * |
| 150 | * May be passed as an array to allow for alternative plurals when using |
| 151 | * this argument as a base to construct the capabilities, e.g. |
| 152 | * array('story', 'stories'). Default 'post'. |
| 153 | * |
| 154 | * @var string |
| 155 | */ |
| 156 | public $capability_type = 'post'; |
| 157 | |
| 158 | /** |
| 159 | * Whether to use the internal default meta capability handling. |
| 160 | * |
| 161 | * Default false. |
| 162 | * |
| 163 | * @var bool |
| 164 | */ |
| 165 | public $map_meta_cap = false; |
| 166 | |
| 167 | /** |
| 168 | * Provide a callback function that sets up the meta boxes for the edit form. |
| 169 | * |
| 170 | * Do remove_meta_box() and add_meta_box() calls in the callback. Default null. |
| 171 | * |
| 172 | * @var string |
| 173 | */ |
| 174 | public $register_meta_box_cb = null; |
| 175 | |
| 176 | /** |
| 177 | * An array of taxonomy identifiers that will be registered for the post type. |
| 178 | * |
| 179 | * Taxonomies can be registered later with {@see register_taxonomy()} or {@see register_taxonomy_for_object_type()}. |
| 180 | * |
| 181 | * Default empty array. |
| 182 | * |
| 183 | * @var array |
| 184 | */ |
| 185 | public $taxonomies = array(); |
| 186 | |
| 187 | /** |
| 188 | * Whether there should be post type archives, or if a string, the archive slug to use. |
| 189 | * |
| 190 | * Will generate the proper rewrite rules if $rewrite is enabled. Default false. |
| 191 | * |
| 192 | * @var bool|string |
| 193 | */ |
| 194 | public $has_archive = false; |
| 195 | |
| 196 | /** |
| 197 | * Sets the query_var key for this post type. |
| 198 | * |
| 199 | * Defaults to $post_type key. If false, a post type cannot be loaded at ?{query_var}={post_slug}. |
| 200 | * If specified as a string, the query ?{query_var_string}={post_slug} will be valid. |
| 201 | * |
| 202 | * @var string|bool |
| 203 | */ |
| 204 | public $query_var; |
| 205 | |
| 206 | /** |
| 207 | * Whether to allow this post type to be exported. |
| 208 | * |
| 209 | * Default true. |
| 210 | * |
| 211 | * @var bool |
| 212 | */ |
| 213 | public $can_export = true; |
| 214 | |
| 215 | /** |
| 216 | * Whether to delete posts of this type when deleting a user. |
| 217 | * |
| 218 | * If true, posts of this type belonging to the user will be moved to trash when then user is deleted. |
| 219 | * If false, posts of this type belonging to the user will *not* be trashed or deleted. |
| 220 | * If not set (the default), posts are trashed if post_type_supports('author'). |
| 221 | * Otherwise posts are not trashed or deleted. Default null. |
| 222 | * |
| 223 | * @var bool |
| 224 | */ |
| 225 | public $delete_with_user = null; |
| 226 | |
| 227 | /** |
| 228 | * Whether this post type is a native or "built-in" post_type. |
| 229 | * |
| 230 | * Default false. |
| 231 | * |
| 232 | * @var bool |
| 233 | */ |
| 234 | public $_builtin = false; |
| 235 | |
| 236 | /** |
| 237 | * URL segment to use for edit link of this post type. |
| 238 | * |
| 239 | * Default 'post.php?post=%d' |
| 240 | * |
| 241 | * @var string |
| 242 | */ |
| 243 | public $_edit_link = 'post.php?post=%d'; |
| 244 | |
| 245 | |
| 246 | /** |
| 247 | * Post type capabilities. |
| 248 | * |
| 249 | * @var array |
| 250 | */ |
| 251 | public $cap; |
| 252 | |
| 253 | /** |
| 254 | * @var array|false |
| 255 | */ |
| 256 | public $rewrite; |
| 257 | |
| 258 | /** |
| 259 | * Creates a new WP_Post_Type object. |
| 260 | * |
| 261 | * Will populate object properties from the provided arguments and assign other |
| 262 | * default properties based on that information. |
| 263 | * |
| 264 | * @since 4.6.0 |
| 265 | * @access public |
| 266 | * |
| 267 | * @global WP_Rewrite $wp_rewrite Used for default feeds. |
| 268 | * @global WP $wp Used to add query vars. |
| 269 | * |
| 270 | * @see register_post_type() |
| 271 | * |
| 272 | * @param string $post_type Post type key. |
| 273 | * @param array|string $args Array or string of arguments for registering a post type. |
| 274 | */ |
| 275 | public function __construct( $post_type, $args = array() ) { |
| 276 | global $wp, $wp_rewrite; |
| 277 | |
| 278 | $args = wp_parse_args( $args ); |
| 279 | |
| 280 | /** |
| 281 | * Filter the arguments for registering a post type. |
| 282 | * |
| 283 | * @since 4.4.0 |
| 284 | * |
| 285 | * @param array $args Array of arguments for registering a post type. |
| 286 | * @param string $post_type Post type key. |
| 287 | */ |
| 288 | $args = apply_filters( 'register_post_type_args', $args, $post_type ); |
| 289 | |
| 290 | $has_edit_link = ! empty( $args['_edit_link'] ); |
| 291 | |
| 292 | // Args prefixed with an underscore are reserved for internal use. |
| 293 | $defaults = array( |
| 294 | 'labels' => array(), |
| 295 | 'description' => '', |
| 296 | 'public' => false, |
| 297 | 'hierarchical' => false, |
| 298 | 'exclude_from_search' => null, |
| 299 | 'publicly_queryable' => null, |
| 300 | 'show_ui' => null, |
| 301 | 'show_in_menu' => null, |
| 302 | 'show_in_nav_menus' => null, |
| 303 | 'show_in_admin_bar' => null, |
| 304 | 'menu_position' => null, |
| 305 | 'menu_icon' => null, |
| 306 | 'capability_type' => 'post', |
| 307 | 'capabilities' => array(), |
| 308 | 'map_meta_cap' => null, |
| 309 | 'supports' => array(), |
| 310 | 'register_meta_box_cb' => null, |
| 311 | 'taxonomies' => array(), |
| 312 | 'has_archive' => false, |
| 313 | 'rewrite' => true, |
| 314 | 'query_var' => true, |
| 315 | 'can_export' => true, |
| 316 | 'delete_with_user' => null, |
| 317 | '_builtin' => false, |
| 318 | '_edit_link' => 'post.php?post=%d', |
| 319 | ); |
| 320 | $args = array_merge( $defaults, $args ); |
| 321 | $args = (object) $args; |
| 322 | |
| 323 | $args->name = $post_type; |
| 324 | |
| 325 | // If not set, default to the setting for public. |
| 326 | if ( null === $args->publicly_queryable ) { |
| 327 | $args->publicly_queryable = $args->public; |
| 328 | } |
| 329 | |
| 330 | // If not set, default to the setting for public. |
| 331 | if ( null === $args->show_ui ) { |
| 332 | $args->show_ui = $args->public; |
| 333 | } |
| 334 | |
| 335 | // If not set, default to the setting for show_ui. |
| 336 | if ( null === $args->show_in_menu || ! $args->show_ui ) { |
| 337 | $args->show_in_menu = $args->show_ui; |
| 338 | } |
| 339 | |
| 340 | // If not set, default to the whether the full UI is shown. |
| 341 | if ( null === $args->show_in_admin_bar ) { |
| 342 | $args->show_in_admin_bar = (bool) $args->show_in_menu; |
| 343 | } |
| 344 | |
| 345 | // If not set, default to the setting for public. |
| 346 | if ( null === $args->show_in_nav_menus ) { |
| 347 | $args->show_in_nav_menus = $args->public; |
| 348 | } |
| 349 | |
| 350 | // If not set, default to true if not public, false if public. |
| 351 | if ( null === $args->exclude_from_search ) { |
| 352 | $args->exclude_from_search = ! $args->public; |
| 353 | } |
| 354 | |
| 355 | // Back compat with quirky handling in version 3.0. #14122. |
| 356 | if ( empty( $args->capabilities ) && null === $args->map_meta_cap && in_array( $args->capability_type, array( |
| 357 | 'post', |
| 358 | 'page', |
| 359 | ) ) |
| 360 | ) { |
| 361 | $args->map_meta_cap = true; |
| 362 | } |
| 363 | |
| 364 | // If not set, default to false. |
| 365 | if ( null === $args->map_meta_cap ) { |
| 366 | $args->map_meta_cap = false; |
| 367 | } |
| 368 | |
| 369 | // If there's no specified edit link and no UI, remove the edit link. |
| 370 | if ( ! $args->show_ui && ! $has_edit_link ) { |
| 371 | $args->_edit_link = ''; |
| 372 | } |
| 373 | |
| 374 | $this->cap = get_post_type_capabilities( $args ); |
| 375 | unset( $args->capabilities ); |
| 376 | |
| 377 | if ( is_array( $args->capability_type ) ) { |
| 378 | $args->capability_type = $args->capability_type[0]; |
| 379 | } |
| 380 | |
| 381 | if ( ! empty( $args->supports ) ) { |
| 382 | add_post_type_support( $post_type, $args->supports ); |
| 383 | } elseif ( false !== $args->supports ) { |
| 384 | // Add default features |
| 385 | add_post_type_support( $post_type, array( 'title', 'editor' ) ); |
| 386 | } |
| 387 | |
| 388 | if ( false !== $args->query_var ) { |
| 389 | if ( true === $args->query_var ) { |
| 390 | $args->query_var = $post_type; |
| 391 | } else { |
| 392 | $args->query_var = sanitize_title_with_dashes( $args->query_var ); |
| 393 | } |
| 394 | |
| 395 | if ( $wp && is_post_type_viewable( $args ) ) { |
| 396 | $wp->add_query_var( $args->query_var ); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | if ( false !== $args->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) { |
| 401 | if ( ! is_array( $args->rewrite ) ) { |
| 402 | $args->rewrite = array(); |
| 403 | } |
| 404 | if ( empty( $args->rewrite['slug'] ) ) { |
| 405 | $args->rewrite['slug'] = $post_type; |
| 406 | } |
| 407 | if ( ! isset( $args->rewrite['with_front'] ) ) { |
| 408 | $args->rewrite['with_front'] = true; |
| 409 | } |
| 410 | if ( ! isset( $args->rewrite['pages'] ) ) { |
| 411 | $args->rewrite['pages'] = true; |
| 412 | } |
| 413 | if ( ! isset( $args->rewrite['feeds'] ) || ! $args->has_archive ) { |
| 414 | $args->rewrite['feeds'] = (bool) $args->has_archive; |
| 415 | } |
| 416 | if ( ! isset( $args->rewrite['ep_mask'] ) ) { |
| 417 | if ( isset( $args->permalink_epmask ) ) { |
| 418 | $args->rewrite['ep_mask'] = $args->permalink_epmask; |
| 419 | } else { |
| 420 | $args->rewrite['ep_mask'] = EP_PERMALINK; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | if ( $args->hierarchical ) { |
| 425 | add_rewrite_tag( "%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&pagename=" ); |
| 426 | } else { |
| 427 | add_rewrite_tag( "%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=" ); |
| 428 | } |
| 429 | |
| 430 | if ( $args->has_archive ) { |
| 431 | $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive; |
| 432 | if ( $args->rewrite['with_front'] ) { |
| 433 | $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug; |
| 434 | } else { |
| 435 | $archive_slug = $wp_rewrite->root . $archive_slug; |
| 436 | } |
| 437 | |
| 438 | add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$post_type", 'top' ); |
| 439 | if ( $args->rewrite['feeds'] && $wp_rewrite->feeds ) { |
| 440 | $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')'; |
| 441 | add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' ); |
| 442 | add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' ); |
| 443 | } |
| 444 | if ( $args->rewrite['pages'] ) { |
| 445 | add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&paged=$matches[1]', 'top' ); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | $permastruct_args = $args->rewrite; |
| 450 | $permastruct_args['feed'] = $permastruct_args['feeds']; |
| 451 | add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $permastruct_args ); |
| 452 | } |
| 453 | |
| 454 | // Register the post type meta box if a custom callback was specified. |
| 455 | if ( $args->register_meta_box_cb ) { |
| 456 | add_action( 'add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1 ); |
| 457 | } |
| 458 | |
| 459 | foreach( get_object_vars( $args) as $property_name => $property_value ) { |
| 460 | $this->$property_name = $property_value; |
| 461 | } |
| 462 | |
| 463 | $this->labels = get_post_type_labels( $args ); |
| 464 | $this->label = $this->labels->name; |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Retrieves a post type object by name. |
| 469 | * |
| 470 | * @since 4.6.0 |
| 471 | * |
| 472 | * @global array $wp_post_types List of post types. |
| 473 | * |
| 474 | * @param string $post_type The name of a registered post type. |
| 475 | * @return WP_Post_Type|null WP_Post_Type object if it exists, null otherwise. |
| 476 | */ |
| 477 | public static function get_instance( $post_type ) { |
| 478 | global $wp_post_types; |
| 479 | |
| 480 | if ( ! is_scalar( $post_type ) || empty( $wp_post_types[ $post_type ] ) ) { |
| 481 | return null; |
| 482 | } |
| 483 | |
| 484 | return $wp_post_types[ $post_type ]; |
| 485 | } |
| 486 | } |