| 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 $name |
| 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 $label |
| 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 $labels |
| 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 $description |
| 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 $public |
| 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 $hierarchical |
| 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 $exclude_from_search |
| 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 $publicly_queryable |
| 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 $show_ui |
| 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 |
| 127 | * shown. If a string of an existing top level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post type |
| 128 | * 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 $show_in_menu |
| 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 $show_in_nav_menus |
| 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 $show_in_admin_bar |
| 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 $menu_position |
| 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 |
| 176 | * icon, e.g. |
| 177 | * 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS. |
| 178 | * |
| 179 | * Defaults to use the posts icon. |
| 180 | * |
| 181 | * @since 4.6.0 |
| 182 | * @access public |
| 183 | * @var string $menu_icon |
| 184 | */ |
| 185 | public $menu_icon = null; |
| 186 | |
| 187 | /** |
| 188 | * The string to use to build the read, edit, and delete capabilities. |
| 189 | * |
| 190 | * May be passed as an array to allow for alternative plurals when using |
| 191 | * this argument as a base to construct the capabilities, e.g. |
| 192 | * array('story', 'stories'). Default 'post'. |
| 193 | * |
| 194 | * @since 4.6.0 |
| 195 | * @access public |
| 196 | * @var string $capability_type |
| 197 | */ |
| 198 | public $capability_type = 'post'; |
| 199 | |
| 200 | /** |
| 201 | * Whether to use the internal default meta capability handling. |
| 202 | * |
| 203 | * Default false. |
| 204 | * |
| 205 | * @since 4.6.0 |
| 206 | * @access public |
| 207 | * @var bool $map_meta_cap |
| 208 | */ |
| 209 | public $map_meta_cap = false; |
| 210 | |
| 211 | /** |
| 212 | * Provide a callback function that sets up the meta boxes for the edit form. |
| 213 | * |
| 214 | * Do remove_meta_box() and add_meta_box() calls in the callback. Default null. |
| 215 | * |
| 216 | * @since 4.6.0 |
| 217 | * @access public |
| 218 | * @var string $register_meta_box_cb |
| 219 | */ |
| 220 | public $register_meta_box_cb = null; |
| 221 | |
| 222 | /** |
| 223 | * An array of taxonomy identifiers that will be registered for the post type. |
| 224 | * |
| 225 | * Taxonomies can be registered later with {@see register_taxonomy()} or {@see register_taxonomy_for_object_type()}. |
| 226 | * |
| 227 | * Default empty array. |
| 228 | * |
| 229 | * @since 4.6.0 |
| 230 | * @access public |
| 231 | * @var array $taxonomies |
| 232 | */ |
| 233 | public $taxonomies = array(); |
| 234 | |
| 235 | /** |
| 236 | * Whether there should be post type archives, or if a string, the archive slug to use. |
| 237 | * |
| 238 | * Will generate the proper rewrite rules if $rewrite is enabled. Default false. |
| 239 | * |
| 240 | * @since 4.6.0 |
| 241 | * @access public |
| 242 | * @var bool|string $has_archive |
| 243 | */ |
| 244 | public $has_archive = false; |
| 245 | |
| 246 | /** |
| 247 | * Sets the query_var key for this post type. |
| 248 | * |
| 249 | * Defaults to $post_type key. If false, a post type cannot be loaded at ?{query_var}={post_slug}. |
| 250 | * If specified as a string, the query ?{query_var_string}={post_slug} will be valid. |
| 251 | * |
| 252 | * @since 4.6.0 |
| 253 | * @access public |
| 254 | * @var string|bool $query_var |
| 255 | */ |
| 256 | public $query_var; |
| 257 | |
| 258 | /** |
| 259 | * Whether to allow this post type to be exported. |
| 260 | * |
| 261 | * Default true. |
| 262 | * |
| 263 | * @since 4.6.0 |
| 264 | * @access public |
| 265 | * @var bool $can_export |
| 266 | */ |
| 267 | public $can_export = true; |
| 268 | |
| 269 | /** |
| 270 | * Whether to delete posts of this type when deleting a user. |
| 271 | * |
| 272 | * If true, posts of this type belonging to the user will be moved to trash when then user is deleted. |
| 273 | * If false, posts of this type belonging to the user will *not* be trashed or deleted. |
| 274 | * If not set (the default), posts are trashed if post_type_supports('author'). |
| 275 | * Otherwise posts are not trashed or deleted. Default null. |
| 276 | * |
| 277 | * @since 4.6.0 |
| 278 | * @access public |
| 279 | * @var bool $delete_with_user |
| 280 | */ |
| 281 | public $delete_with_user = null; |
| 282 | |
| 283 | /** |
| 284 | * Whether this post type is a native or "built-in" post_type. |
| 285 | * |
| 286 | * Default false. |
| 287 | * |
| 288 | * @since 4.6.0 |
| 289 | * @access public |
| 290 | * @var bool $_builtin |
| 291 | */ |
| 292 | public $_builtin = false; |
| 293 | |
| 294 | /** |
| 295 | * URL segment to use for edit link of this post type. |
| 296 | * |
| 297 | * Default 'post.php?post=%d' |
| 298 | * |
| 299 | * @since 4.6.0 |
| 300 | * @access public |
| 301 | * @var string $_edit_link |
| 302 | */ |
| 303 | public $_edit_link = 'post.php?post=%d'; |
| 304 | |
| 305 | |
| 306 | /** |
| 307 | * Post type capabilities. |
| 308 | * |
| 309 | * @since 4.6.0 |
| 310 | * @access public |
| 311 | * @var array $cap |
| 312 | */ |
| 313 | public $cap; |
| 314 | |
| 315 | /** |
| 316 | * Triggers the handling of rewrites for this post type. |
| 317 | * |
| 318 | * Defaults to true, using $post_type as slug |
| 319 | * |
| 320 | * @since 4.6.0 |
| 321 | * @access public |
| 322 | * @var array|false $rewrite |
| 323 | */ |
| 324 | public $rewrite; |
| 325 | |
| 326 | /** |
| 327 | * The features supported by the post type. |
| 328 | * |
| 329 | * @since 4.6.0 |
| 330 | * @access public |
| 331 | * @var array|bool $supports |
| 332 | */ |
| 333 | public $supports; |
| 334 | |
| 335 | /** |
| 336 | * Constructor. |
| 337 | * |
| 338 | * Will populate object properties from the provided arguments and assign other |
| 339 | * default properties based on that information. |
| 340 | * |
| 341 | * @since 4.6.0 |
| 342 | * @access public |
| 343 | * |
| 344 | * @see register_post_type() |
| 345 | * |
| 346 | * @param string $post_type Post type key. |
| 347 | * @param array|string $args Optional. Array or string of arguments for registering a post type. |
| 348 | * Default empty array. |
| 349 | */ |
| 350 | public function __construct( $post_type, $args = array() ) { |
| 351 | $this->name = $post_type; |
| 352 | |
| 353 | $this->set_props( $args ); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Sets post type properties. |
| 358 | * |
| 359 | * @since 4.6.0 |
| 360 | * @access public |
| 361 | * |
| 362 | * @param array|string $args Array or string of arguments for registering a post type. |
| 363 | */ |
| 364 | public function set_props( $args ) { |
| 365 | $args = wp_parse_args( $args ); |
| 366 | |
| 367 | /** |
| 368 | * Filter the arguments for registering a post type. |
| 369 | * |
| 370 | * @since 4.4.0 |
| 371 | * |
| 372 | * @param array $args Array of arguments for registering a post type. |
| 373 | * @param string $post_type Post type key. |
| 374 | */ |
| 375 | $args = apply_filters( 'register_post_type_args', $args, $this->name ); |
| 376 | |
| 377 | $has_edit_link = ! empty( $args['_edit_link'] ); |
| 378 | |
| 379 | // Args prefixed with an underscore are reserved for internal use. |
| 380 | $defaults = array( |
| 381 | 'labels' => array(), |
| 382 | 'description' => '', |
| 383 | 'public' => false, |
| 384 | 'hierarchical' => false, |
| 385 | 'exclude_from_search' => null, |
| 386 | 'publicly_queryable' => null, |
| 387 | 'show_ui' => null, |
| 388 | 'show_in_menu' => null, |
| 389 | 'show_in_nav_menus' => null, |
| 390 | 'show_in_admin_bar' => null, |
| 391 | 'menu_position' => null, |
| 392 | 'menu_icon' => null, |
| 393 | 'capability_type' => 'post', |
| 394 | 'capabilities' => array(), |
| 395 | 'map_meta_cap' => null, |
| 396 | 'supports' => array(), |
| 397 | 'register_meta_box_cb' => null, |
| 398 | 'taxonomies' => array(), |
| 399 | 'has_archive' => false, |
| 400 | 'rewrite' => true, |
| 401 | 'query_var' => true, |
| 402 | 'can_export' => true, |
| 403 | 'delete_with_user' => null, |
| 404 | '_builtin' => false, |
| 405 | '_edit_link' => 'post.php?post=%d', |
| 406 | ); |
| 407 | |
| 408 | $args = array_merge( $defaults, $args ); |
| 409 | |
| 410 | $args['name'] = $this->name; |
| 411 | |
| 412 | // If not set, default to the setting for public. |
| 413 | if ( null === $args['publicly_queryable'] ) { |
| 414 | $args['publicly_queryable'] = $args['public']; |
| 415 | } |
| 416 | |
| 417 | // If not set, default to the setting for public. |
| 418 | if ( null === $args['show_ui'] ) { |
| 419 | $args['show_ui'] = $args['public']; |
| 420 | } |
| 421 | |
| 422 | // If not set, default to the setting for show_ui. |
| 423 | if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) { |
| 424 | $args['show_in_menu'] = $args['show_ui']; |
| 425 | } |
| 426 | |
| 427 | // If not set, default to the whether the full UI is shown. |
| 428 | if ( null === $args['show_in_admin_bar'] ) { |
| 429 | $args['show_in_admin_bar'] = (bool) $args['show_in_menu']; |
| 430 | } |
| 431 | |
| 432 | // If not set, default to the setting for public. |
| 433 | if ( null === $args['show_in_nav_menus'] ) { |
| 434 | $args['show_in_nav_menus'] = $args['public']; |
| 435 | } |
| 436 | |
| 437 | // If not set, default to true if not public, false if public. |
| 438 | if ( null === $args['exclude_from_search'] ) { |
| 439 | $args['exclude_from_search'] = ! $args['public']; |
| 440 | } |
| 441 | |
| 442 | // Back compat with quirky handling in version 3.0. #14122. |
| 443 | if ( empty( $args['capabilities'] ) && null === $args['map_meta_cap'] && in_array( $args['capability_type'], array( |
| 444 | 'post', |
| 445 | 'page', |
| 446 | ) ) |
| 447 | ) { |
| 448 | $args['map_meta_cap'] = true; |
| 449 | } |
| 450 | |
| 451 | // If not set, default to false. |
| 452 | if ( null === $args['map_meta_cap'] ) { |
| 453 | $args['map_meta_cap'] = false; |
| 454 | } |
| 455 | |
| 456 | // If there's no specified edit link and no UI, remove the edit link. |
| 457 | if ( ! $args['show_ui'] && ! $has_edit_link ) { |
| 458 | $args['_edit_link'] = ''; |
| 459 | } |
| 460 | |
| 461 | $this->cap = get_post_type_capabilities( (object) $args ); |
| 462 | unset( $args['capabilities'] ); |
| 463 | |
| 464 | if ( is_array( $args['capability_type'] ) ) { |
| 465 | $args['capability_type'] = $args['capability_type'][0]; |
| 466 | } |
| 467 | |
| 468 | if ( false !== $args['query_var'] ) { |
| 469 | if ( true === $args['query_var'] ) { |
| 470 | $args['query_var'] = $this->name; |
| 471 | } else { |
| 472 | $args['query_var'] = sanitize_title_with_dashes( $args['query_var'] ); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | if ( false !== $args['rewrite'] && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) { |
| 477 | if ( ! is_array( $args['rewrite'] ) ) { |
| 478 | $args['rewrite'] = array(); |
| 479 | } |
| 480 | if ( empty( $args['rewrite']['slug'] ) ) { |
| 481 | $args['rewrite']['slug'] = $this->name; |
| 482 | } |
| 483 | if ( ! isset( $args['rewrite']['with_front'] ) ) { |
| 484 | $args['rewrite']['with_front'] = true; |
| 485 | } |
| 486 | if ( ! isset( $args['rewrite']['pages'] ) ) { |
| 487 | $args['rewrite']['pages'] = true; |
| 488 | } |
| 489 | if ( ! isset( $args['rewrite']['feeds'] ) || ! $args['has_archive'] ) { |
| 490 | $args['rewrite']['feeds'] = (bool) $args['has_archive']; |
| 491 | } |
| 492 | if ( ! isset( $args['rewrite']['ep_mask'] ) ) { |
| 493 | if ( isset( $args['permalink_epmask'] ) ) { |
| 494 | $args['rewrite']['ep_mask'] = $args['permalink_epmask']; |
| 495 | } else { |
| 496 | $args['rewrite']['ep_mask'] = EP_PERMALINK; |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | foreach ( $args as $property_name => $property_value ) { |
| 502 | $this->$property_name = $property_value; |
| 503 | } |
| 504 | |
| 505 | $this->labels = get_post_type_labels( $this ); |
| 506 | $this->label = $this->labels->name; |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * Sets the features support for the post type. |
| 511 | * |
| 512 | * @since 4.6.0 |
| 513 | * @access public |
| 514 | */ |
| 515 | public function add_supports() { |
| 516 | if ( ! empty( $this->supports ) ) { |
| 517 | add_post_type_support( $this->name, $this->supports ); |
| 518 | unset( $this->supports ); |
| 519 | } elseif ( false !== $this->supports ) { |
| 520 | // Add default features |
| 521 | add_post_type_support( $this->name, array( 'title', 'editor' ) ); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * Adds the necessary rewrite rules for the post type. |
| 527 | * |
| 528 | * @since 4.6.0 |
| 529 | * @access public |
| 530 | * |
| 531 | * @global WP_Rewrite $wp_rewrite WordPress Rewrite Component. |
| 532 | * @global WP $wp Current WordPress environment instance. |
| 533 | */ |
| 534 | public function add_rewrite_rules() { |
| 535 | global $wp_rewrite, $wp; |
| 536 | |
| 537 | if ( false !== $this->query_var && $wp && is_post_type_viewable( $this ) ) { |
| 538 | $wp->add_query_var( $this->query_var ); |
| 539 | } |
| 540 | |
| 541 | if ( false !== $this->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) { |
| 542 | if ( $this->hierarchical ) { |
| 543 | add_rewrite_tag( "%$this->name%", '(.+?)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&pagename=" ); |
| 544 | } else { |
| 545 | add_rewrite_tag( "%$this->name%", '([^/]+)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&name=" ); |
| 546 | } |
| 547 | |
| 548 | if ( $this->has_archive ) { |
| 549 | $archive_slug = $this->has_archive === true ? $this->rewrite['slug'] : $this->has_archive; |
| 550 | if ( $this->rewrite['with_front'] ) { |
| 551 | $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug; |
| 552 | } else { |
| 553 | $archive_slug = $wp_rewrite->root . $archive_slug; |
| 554 | } |
| 555 | |
| 556 | add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$this->name", 'top' ); |
| 557 | if ( $this->rewrite['feeds'] && $wp_rewrite->feeds ) { |
| 558 | $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')'; |
| 559 | add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' ); |
| 560 | add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' ); |
| 561 | } |
| 562 | if ( $this->rewrite['pages'] ) { |
| 563 | add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$this->name" . '&paged=$matches[1]', 'top' ); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | $permastruct_args = $this->rewrite; |
| 568 | $permastruct_args['feed'] = $permastruct_args['feeds']; |
| 569 | add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $permastruct_args ); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * Registers the post type meta box if a custom callback was specified. |
| 575 | * |
| 576 | * @since 4.6.0 |
| 577 | * @access public |
| 578 | */ |
| 579 | public function register_meta_boxes() { |
| 580 | if ( $this->register_meta_box_cb ) { |
| 581 | add_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10, 1 ); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * Adds the future post hook action for the post type. |
| 587 | * |
| 588 | * @since 4.6.0 |
| 589 | * @access public |
| 590 | */ |
| 591 | public function add_hooks() { |
| 592 | add_action( 'future_' . $this->name, '_future_post_hook', 5, 2 ); |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * Registers the taxonomies for the post type. |
| 597 | * |
| 598 | * @since 4.6.0 |
| 599 | * @access public |
| 600 | */ |
| 601 | public function register_taxonomies() { |
| 602 | foreach ( $this->taxonomies as $taxonomy ) { |
| 603 | register_taxonomy_for_object_type( $taxonomy, $this->name ); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * Removes the features support for the post type. |
| 609 | * |
| 610 | * @since 4.6.0 |
| 611 | * @access public |
| 612 | * |
| 613 | * @global array $_wp_post_type_features Post type features. |
| 614 | */ |
| 615 | public function remove_supports() { |
| 616 | global $_wp_post_type_features; |
| 617 | |
| 618 | unset( $_wp_post_type_features[ $this->name ] ); |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * Removes any rewrite rules, permastructs, and rules for the post type. |
| 623 | * |
| 624 | * @since 4.6.0 |
| 625 | * @access public |
| 626 | * |
| 627 | * @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
| 628 | * @global WP $wp Current WordPress environment instance. |
| 629 | * @global array $post_type_meta_caps Used to remove meta capabilities. |
| 630 | */ |
| 631 | public function remove_rewrite_rules() { |
| 632 | global $wp, $wp_rewrite, $post_type_meta_caps; |
| 633 | |
| 634 | // Remove query var. |
| 635 | if ( false !== $this->query_var ) { |
| 636 | $wp->remove_query_var( $this->query_var ); |
| 637 | } |
| 638 | |
| 639 | // Remove any rewrite rules, permastructs, and rules. |
| 640 | if ( false !== $this->rewrite ) { |
| 641 | remove_rewrite_tag( "%$this->name%" ); |
| 642 | remove_permastruct( $this->name ); |
| 643 | foreach ( $wp_rewrite->extra_rules_top as $regex => $query ) { |
| 644 | if ( false !== strpos( $query, "index.php?post_type=$this->name" ) ) { |
| 645 | unset( $wp_rewrite->extra_rules_top[ $regex ] ); |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | // Remove registered custom meta capabilities. |
| 651 | foreach ( $this->cap as $cap ) { |
| 652 | unset( $post_type_meta_caps[ $cap ] ); |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * Unregisters the post type meta box if a custom callback was specified. |
| 658 | * |
| 659 | * @since 4.6.0 |
| 660 | * @access public |
| 661 | */ |
| 662 | public function unregister_meta_boxes() { |
| 663 | if ( $this->register_meta_box_cb ) { |
| 664 | remove_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10 ); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | /** |
| 669 | * Removes the post type from all taxonomies. |
| 670 | * |
| 671 | * @since 4.6.0 |
| 672 | * @access public |
| 673 | */ |
| 674 | public function unregister_taxonomies() { |
| 675 | foreach ( get_object_taxonomies( $this->name ) as $taxonomy ) { |
| 676 | unregister_taxonomy_for_object_type( $taxonomy, $this->name ); |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * Removes the future post hook action for the post type. |
| 682 | * |
| 683 | * @since 4.6.0 |
| 684 | * @access public |
| 685 | */ |
| 686 | public function remove_hooks() { |
| 687 | remove_action( 'future_' . $this->name, '_future_post_hook', 5 ); |
| 688 | } |
| 689 | } |