| | 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 | * @see register_post_type() |
| | 335 | * |
| | 336 | * @param string $post_type Post type key. |
| | 337 | * @param array|string $args Array or string of arguments for registering a post type. |
| | 338 | */ |
| | 339 | public function __construct( $post_type, $args = array() ) { |
| | 340 | $args = wp_parse_args( $args ); |
| | 341 | |
| | 342 | /** |
| | 343 | * Filter the arguments for registering a post type. |
| | 344 | * |
| | 345 | * @since 4.4.0 |
| | 346 | * |
| | 347 | * @param array $args Array of arguments for registering a post type. |
| | 348 | * @param string $post_type Post type key. |
| | 349 | */ |
| | 350 | $args = apply_filters( 'register_post_type_args', $args, $post_type ); |
| | 351 | |
| | 352 | $has_edit_link = ! empty( $args['_edit_link'] ); |
| | 353 | |
| | 354 | // Args prefixed with an underscore are reserved for internal use. |
| | 355 | $defaults = array( |
| | 356 | 'labels' => array(), |
| | 357 | 'description' => '', |
| | 358 | 'public' => false, |
| | 359 | 'hierarchical' => false, |
| | 360 | 'exclude_from_search' => null, |
| | 361 | 'publicly_queryable' => null, |
| | 362 | 'show_ui' => null, |
| | 363 | 'show_in_menu' => null, |
| | 364 | 'show_in_nav_menus' => null, |
| | 365 | 'show_in_admin_bar' => null, |
| | 366 | 'menu_position' => null, |
| | 367 | 'menu_icon' => null, |
| | 368 | 'capability_type' => 'post', |
| | 369 | 'capabilities' => array(), |
| | 370 | 'map_meta_cap' => null, |
| | 371 | 'supports' => array(), |
| | 372 | 'register_meta_box_cb' => null, |
| | 373 | 'taxonomies' => array(), |
| | 374 | 'has_archive' => false, |
| | 375 | 'rewrite' => true, |
| | 376 | 'query_var' => true, |
| | 377 | 'can_export' => true, |
| | 378 | 'delete_with_user' => null, |
| | 379 | '_builtin' => false, |
| | 380 | '_edit_link' => 'post.php?post=%d', |
| | 381 | ); |
| | 382 | $args = array_merge( $defaults, $args ); |
| | 383 | $args = (object) $args; |
| | 384 | |
| | 385 | $args->name = $post_type; |
| | 386 | |
| | 387 | // If not set, default to the setting for public. |
| | 388 | if ( null === $args->publicly_queryable ) { |
| | 389 | $args->publicly_queryable = $args->public; |
| | 390 | } |
| | 391 | |
| | 392 | // If not set, default to the setting for public. |
| | 393 | if ( null === $args->show_ui ) { |
| | 394 | $args->show_ui = $args->public; |
| | 395 | } |
| | 396 | |
| | 397 | // If not set, default to the setting for show_ui. |
| | 398 | if ( null === $args->show_in_menu || ! $args->show_ui ) { |
| | 399 | $args->show_in_menu = $args->show_ui; |
| | 400 | } |
| | 401 | |
| | 402 | // If not set, default to the whether the full UI is shown. |
| | 403 | if ( null === $args->show_in_admin_bar ) { |
| | 404 | $args->show_in_admin_bar = (bool) $args->show_in_menu; |
| | 405 | } |
| | 406 | |
| | 407 | // If not set, default to the setting for public. |
| | 408 | if ( null === $args->show_in_nav_menus ) { |
| | 409 | $args->show_in_nav_menus = $args->public; |
| | 410 | } |
| | 411 | |
| | 412 | // If not set, default to true if not public, false if public. |
| | 413 | if ( null === $args->exclude_from_search ) { |
| | 414 | $args->exclude_from_search = ! $args->public; |
| | 415 | } |
| | 416 | |
| | 417 | // Back compat with quirky handling in version 3.0. #14122. |
| | 418 | if ( empty( $args->capabilities ) && null === $args->map_meta_cap && in_array( $args->capability_type, array( |
| | 419 | 'post', |
| | 420 | 'page', |
| | 421 | ) ) |
| | 422 | ) { |
| | 423 | $args->map_meta_cap = true; |
| | 424 | } |
| | 425 | |
| | 426 | // If not set, default to false. |
| | 427 | if ( null === $args->map_meta_cap ) { |
| | 428 | $args->map_meta_cap = false; |
| | 429 | } |
| | 430 | |
| | 431 | // If there's no specified edit link and no UI, remove the edit link. |
| | 432 | if ( ! $args->show_ui && ! $has_edit_link ) { |
| | 433 | $args->_edit_link = ''; |
| | 434 | } |
| | 435 | |
| | 436 | $this->cap = get_post_type_capabilities( $args ); |
| | 437 | unset( $args->capabilities ); |
| | 438 | |
| | 439 | if ( is_array( $args->capability_type ) ) { |
| | 440 | $args->capability_type = $args->capability_type[0]; |
| | 441 | } |
| | 442 | |
| | 443 | if ( false !== $args->query_var ) { |
| | 444 | if ( true === $args->query_var ) { |
| | 445 | $args->query_var = $post_type; |
| | 446 | } else { |
| | 447 | $args->query_var = sanitize_title_with_dashes( $args->query_var ); |
| | 448 | } |
| | 449 | } |
| | 450 | |
| | 451 | if ( false !== $args->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) { |
| | 452 | if ( ! is_array( $args->rewrite ) ) { |
| | 453 | $args->rewrite = array(); |
| | 454 | } |
| | 455 | if ( empty( $args->rewrite['slug'] ) ) { |
| | 456 | $args->rewrite['slug'] = $post_type; |
| | 457 | } |
| | 458 | if ( ! isset( $args->rewrite['with_front'] ) ) { |
| | 459 | $args->rewrite['with_front'] = true; |
| | 460 | } |
| | 461 | if ( ! isset( $args->rewrite['pages'] ) ) { |
| | 462 | $args->rewrite['pages'] = true; |
| | 463 | } |
| | 464 | if ( ! isset( $args->rewrite['feeds'] ) || ! $args->has_archive ) { |
| | 465 | $args->rewrite['feeds'] = (bool) $args->has_archive; |
| | 466 | } |
| | 467 | if ( ! isset( $args->rewrite['ep_mask'] ) ) { |
| | 468 | if ( isset( $args->permalink_epmask ) ) { |
| | 469 | $args->rewrite['ep_mask'] = $args->permalink_epmask; |
| | 470 | } else { |
| | 471 | $args->rewrite['ep_mask'] = EP_PERMALINK; |
| | 472 | } |
| | 473 | } |
| | 474 | } |
| | 475 | |
| | 476 | foreach( get_object_vars( $args) as $property_name => $property_value ) { |
| | 477 | $this->$property_name = $property_value; |
| | 478 | } |
| | 479 | |
| | 480 | $this->labels = get_post_type_labels( $this ); |
| | 481 | $this->label = $this->labels->name; |
| | 482 | } |
| | 483 | |
| | 484 | /** |
| | 485 | * Performs the necessary steps to register the post type. |
| | 486 | * |
| | 487 | * @since 4.6.0 |
| | 488 | * @access public |
| | 489 | * |
| | 490 | * @see register_post_type() |
| | 491 | * |
| | 492 | * @global WP_Rewrite $wp_rewrite Used for default feeds. |
| | 493 | * @global WP $wp Used to add query vars. |
| | 494 | */ |
| | 495 | public function _register() { |
| | 496 | global $wp_rewrite, $wp; |
| | 497 | |
| | 498 | if ( ! empty( $this->supports ) ) { |
| | 499 | add_post_type_support( $this->name, $this->supports ); |
| | 500 | unset( $this->supports ); |
| | 501 | } elseif ( false !== $this->supports ) { |
| | 502 | // Add default features |
| | 503 | add_post_type_support( $this->name, array( 'title', 'editor' ) ); |
| | 504 | } |
| | 505 | |
| | 506 | if ( false !== $this->query_var && $wp && is_post_type_viewable( $this ) ) { |
| | 507 | $wp->add_query_var( $this->query_var ); |
| | 508 | } |
| | 509 | |
| | 510 | if ( false !== $this->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) { |
| | 511 | if ( $this->hierarchical ) { |
| | 512 | add_rewrite_tag( "%$this->name%", '(.+?)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&pagename=" ); |
| | 513 | } else { |
| | 514 | add_rewrite_tag( "%$this->name%", '([^/]+)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&name=" ); |
| | 515 | } |
| | 516 | |
| | 517 | if ( $this->has_archive ) { |
| | 518 | $archive_slug = $this->has_archive === true ? $this->rewrite['slug'] : $this->has_archive; |
| | 519 | if ( $this->rewrite['with_front'] ) { |
| | 520 | $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug; |
| | 521 | } else { |
| | 522 | $archive_slug = $wp_rewrite->root . $archive_slug; |
| | 523 | } |
| | 524 | |
| | 525 | add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$this->name", 'top' ); |
| | 526 | if ( $this->rewrite['feeds'] && $wp_rewrite->feeds ) { |
| | 527 | $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')'; |
| | 528 | add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' ); |
| | 529 | add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' ); |
| | 530 | } |
| | 531 | if ( $this->rewrite['pages'] ) { |
| | 532 | add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$this->name" . '&paged=$matches[1]', 'top' ); |
| | 533 | } |
| | 534 | } |
| | 535 | |
| | 536 | $permastruct_args = $this->rewrite; |
| | 537 | $permastruct_args['feed'] = $permastruct_args['feeds']; |
| | 538 | add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $permastruct_args ); |
| | 539 | } |
| | 540 | |
| | 541 | // Register the post type meta box if a custom callback was specified. |
| | 542 | if ( $this->register_meta_box_cb ) { |
| | 543 | add_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10, 1 ); |
| | 544 | } |
| | 545 | |
| | 546 | add_action( 'future_' . $this->name, '_future_post_hook', 5, 2 ); |
| | 547 | |
| | 548 | foreach ( $this->taxonomies as $taxonomy ) { |
| | 549 | register_taxonomy_for_object_type( $taxonomy, $this->name ); |
| | 550 | } |
| | 551 | |
| | 552 | /** |
| | 553 | * Fires after a post type is registered. |
| | 554 | * |
| | 555 | * @since 3.3.0 |
| | 556 | * |
| | 557 | * @param string $post_type Post type. |
| | 558 | * @param object $args Arguments used to register the post type. |
| | 559 | */ |
| | 560 | do_action( 'registered_post_type', $this->name, $this ); |
| | 561 | } |
| | 562 | |
| | 563 | /** |
| | 564 | * Performs the necessary steps to unregister the post type. |
| | 565 | * |
| | 566 | * @since 4.6.0 |
| | 567 | * @access public |
| | 568 | * |
| | 569 | * @see unregister_post_type() |
| | 570 | * |
| | 571 | * @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
| | 572 | * @global WP $wp Current WordPress environment instance. |
| | 573 | * @global array $_wp_post_type_features Used to remove post type features. |
| | 574 | * @global array $post_type_meta_caps Used to remove meta capabilities. |
| | 575 | */ |
| | 576 | public function _unregister() { |
| | 577 | global $wp, $wp_rewrite, $_wp_post_type_features, $post_type_meta_caps; |
| | 578 | |
| | 579 | // Remove query var. |
| | 580 | if ( false !== $this->query_var ) { |
| | 581 | $wp->remove_query_var( $this->query_var ); |
| | 582 | } |
| | 583 | |
| | 584 | // Remove any rewrite rules, permastructs, and rules. |
| | 585 | if ( false !== $this->rewrite ) { |
| | 586 | remove_rewrite_tag( "%$this->name%" ); |
| | 587 | remove_permastruct( $this->name ); |
| | 588 | foreach ( $wp_rewrite->extra_rules_top as $regex => $query ) { |
| | 589 | if ( false !== strpos( $query, "index.php?post_type=$this->name" ) ) { |
| | 590 | unset( $wp_rewrite->extra_rules_top[ $regex ] ); |
| | 591 | } |
| | 592 | } |
| | 593 | } |
| | 594 | |
| | 595 | // Remove registered custom meta capabilities. |
| | 596 | foreach ( $this->cap as $cap ) { |
| | 597 | unset( $post_type_meta_caps[ $cap ] ); |
| | 598 | } |
| | 599 | |
| | 600 | // Remove all post type support. |
| | 601 | unset( $_wp_post_type_features[ $this->name ] ); |
| | 602 | |
| | 603 | // Unregister the post type meta box if a custom callback was specified. |
| | 604 | if ( $this->register_meta_box_cb ) { |
| | 605 | remove_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb ); |
| | 606 | } |
| | 607 | |
| | 608 | // Remove the post type from all taxonomies. |
| | 609 | foreach ( get_object_taxonomies( $this->name ) as $taxonomy ) { |
| | 610 | unregister_taxonomy_for_object_type( $taxonomy, $this->name ); |
| | 611 | } |
| | 612 | |
| | 613 | // Remove the future post hook action. |
| | 614 | remove_action( 'future_' . $this->name, '_future_post_hook', 5 ); |
| | 615 | |
| | 616 | /** |
| | 617 | * Fires after a post type was unregistered. |
| | 618 | * |
| | 619 | * @since 4.5.0 |
| | 620 | * |
| | 621 | * @param string $post_type Post type key. |
| | 622 | */ |
| | 623 | do_action( 'unregistered_post_type', $this->name ); |
| | 624 | } |
| | 625 | |
| | 626 | /** |
| | 627 | * Retrieves a post type object by name. |
| | 628 | * |
| | 629 | * @since 4.6.0 |
| | 630 | * @access public |
| | 631 | * @static |
| | 632 | * |
| | 633 | * @global array $wp_post_types List of post types. |
| | 634 | * |
| | 635 | * @param string $post_type The name of a registered post type. |
| | 636 | * @return WP_Post_Type|null WP_Post_Type object if it exists, null otherwise. |
| | 637 | */ |
| | 638 | public static function get_instance( $post_type ) { |
| | 639 | global $wp_post_types; |
| | 640 | |
| | 641 | if ( ! is_scalar( $post_type ) || empty( $wp_post_types[ $post_type ] ) ) { |
| | 642 | return null; |
| | 643 | } |
| | 644 | |
| | 645 | return $wp_post_types[ $post_type ]; |
| | 646 | } |
| | 647 | } |