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