[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> class-wp-post-type.php (source)

   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   * @see register_post_type()
  16   */
  17  #[AllowDynamicProperties]
  18  final class WP_Post_Type {
  19      /**
  20       * Post type key.
  21       *
  22       * @since 4.6.0
  23       * @var string $name
  24       */
  25      public $name;
  26  
  27      /**
  28       * Name of the post type shown in the menu. Usually plural.
  29       *
  30       * @since 4.6.0
  31       * @var string $label
  32       */
  33      public $label;
  34  
  35      /**
  36       * Labels object for this post type.
  37       *
  38       * If not set, post labels are inherited for non-hierarchical types
  39       * and page labels for hierarchical ones.
  40       *
  41       * @see get_post_type_labels()
  42       *
  43       * @since 4.6.0
  44       * @var stdClass $labels
  45       */
  46      public $labels;
  47  
  48      /**
  49       * Default labels.
  50       *
  51       * @since 6.0.0
  52       * @var (string|null)[][] $default_labels
  53       */
  54      protected static $default_labels = array();
  55  
  56      /**
  57       * A short descriptive summary of what the post type is.
  58       *
  59       * Default empty.
  60       *
  61       * @since 4.6.0
  62       * @var string $description
  63       */
  64      public $description = '';
  65  
  66      /**
  67       * Whether a post type is intended for use publicly either via the admin interface or by front-end users.
  68       *
  69       * While the default settings of $exclude_from_search, $publicly_queryable, $show_ui, and $show_in_nav_menus
  70       * are inherited from public, each does not rely on this relationship and controls a very specific intention.
  71       *
  72       * Default false.
  73       *
  74       * @since 4.6.0
  75       * @var bool $public
  76       */
  77      public $public = false;
  78  
  79      /**
  80       * Whether the post type is hierarchical (e.g. page).
  81       *
  82       * Default false.
  83       *
  84       * @since 4.6.0
  85       * @var bool $hierarchical
  86       */
  87      public $hierarchical = false;
  88  
  89      /**
  90       * Whether to exclude posts with this post type from front end search
  91       * results.
  92       *
  93       * Default is the opposite value of $public.
  94       *
  95       * @since 4.6.0
  96       * @var bool $exclude_from_search
  97       */
  98      public $exclude_from_search = null;
  99  
 100      /**
 101       * Whether queries can be performed on the front end for the post type as part of `parse_request()`.
 102       *
 103       * Endpoints would include:
 104       *
 105       * - `?post_type={post_type_key}`
 106       * - `?{post_type_key}={single_post_slug}`
 107       * - `?{post_type_query_var}={single_post_slug}`
 108       *
 109       * Default is the value of $public.
 110       *
 111       * @since 4.6.0
 112       * @var bool $publicly_queryable
 113       */
 114      public $publicly_queryable = null;
 115  
 116      /**
 117       * Whether this post type is embeddable.
 118       *
 119       * Default is the value of $public.
 120       *
 121       * @since 6.8.0
 122       * @var bool $embeddable
 123       */
 124      public $embeddable = null;
 125  
 126      /**
 127       * Whether to generate and allow a UI for managing this post type in the admin.
 128       *
 129       * Default is the value of $public.
 130       *
 131       * @since 4.6.0
 132       * @var bool $show_ui
 133       */
 134      public $show_ui = null;
 135  
 136      /**
 137       * Where to show the post type in the admin menu.
 138       *
 139       * 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
 140       * shown. If a string of an existing top level menu ('tools.php' or 'edit.php?post_type=page', for example), the
 141       * post type will be placed as a sub-menu of that.
 142       *
 143       * Default is the value of $show_ui.
 144       *
 145       * @since 4.6.0
 146       * @var bool|string $show_in_menu
 147       */
 148      public $show_in_menu = null;
 149  
 150      /**
 151       * Makes this post type available for selection in navigation menus.
 152       *
 153       * Default is the value $public.
 154       *
 155       * @since 4.6.0
 156       * @var bool $show_in_nav_menus
 157       */
 158      public $show_in_nav_menus = null;
 159  
 160      /**
 161       * Makes this post type available via the admin bar.
 162       *
 163       * Default is the value of $show_in_menu.
 164       *
 165       * @since 4.6.0
 166       * @var bool $show_in_admin_bar
 167       */
 168      public $show_in_admin_bar = null;
 169  
 170      /**
 171       * The position in the menu order the post type should appear.
 172       *
 173       * To work, $show_in_menu must be true. Default null (at the bottom).
 174       *
 175       * @since 4.6.0
 176       * @var int $menu_position
 177       */
 178      public $menu_position = null;
 179  
 180      /**
 181       * The URL or reference to the icon to be used for this menu.
 182       *
 183       * Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme.
 184       * This should begin with 'data:image/svg+xml;base64,'. Pass the name of a Dashicons helper class
 185       * to use a font icon, e.g. 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty
 186       * so an icon can be added via CSS.
 187       *
 188       * Defaults to use the posts icon.
 189       *
 190       * @since 4.6.0
 191       * @var string $menu_icon
 192       */
 193      public $menu_icon = null;
 194  
 195      /**
 196       * The string to use to build the read, edit, and delete capabilities.
 197       *
 198       * May be passed as an array to allow for alternative plurals when using
 199       * this argument as a base to construct the capabilities, e.g.
 200       * array( 'story', 'stories' ). Default 'post'.
 201       *
 202       * @since 4.6.0
 203       * @var string $capability_type
 204       */
 205      public $capability_type = 'post';
 206  
 207      /**
 208       * Whether to use the internal default meta capability handling.
 209       *
 210       * Default false.
 211       *
 212       * @since 4.6.0
 213       * @var bool $map_meta_cap
 214       */
 215      public $map_meta_cap = false;
 216  
 217      /**
 218       * Provide a callback function that sets up the meta boxes for the edit form.
 219       *
 220       * Do `remove_meta_box()` and `add_meta_box()` calls in the callback. Default null.
 221       *
 222       * @since 4.6.0
 223       * @var callable $register_meta_box_cb
 224       */
 225      public $register_meta_box_cb = null;
 226  
 227      /**
 228       * An array of taxonomy identifiers that will be registered for the post type.
 229       *
 230       * Taxonomies can be registered later with `register_taxonomy()` or `register_taxonomy_for_object_type()`.
 231       *
 232       * Default empty array.
 233       *
 234       * @since 4.6.0
 235       * @var string[] $taxonomies
 236       */
 237      public $taxonomies = array();
 238  
 239      /**
 240       * Whether there should be post type archives, or if a string, the archive slug to use.
 241       *
 242       * Will generate the proper rewrite rules if $rewrite is enabled. Default false.
 243       *
 244       * @since 4.6.0
 245       * @var bool|string $has_archive
 246       */
 247      public $has_archive = false;
 248  
 249      /**
 250       * Sets the query_var key for this post type.
 251       *
 252       * Defaults to $post_type key. If false, a post type cannot be loaded at `?{query_var}={post_slug}`.
 253       * If specified as a string, the query `?{query_var_string}={post_slug}` will be valid.
 254       *
 255       * @since 4.6.0
 256       * @var string|bool $query_var
 257       */
 258      public $query_var;
 259  
 260      /**
 261       * Whether to allow this post type to be exported.
 262       *
 263       * Default true.
 264       *
 265       * @since 4.6.0
 266       * @var bool $can_export
 267       */
 268      public $can_export = true;
 269  
 270      /**
 271       * Whether to delete posts of this type when deleting a user.
 272       *
 273       * - If true, posts of this type belonging to the user will be moved to Trash when the user is deleted.
 274       * - If false, posts of this type belonging to the user will *not* be trashed or deleted.
 275       * - If not set (the default), posts are trashed if post type supports the 'author' feature.
 276       *   Otherwise posts are not trashed or deleted.
 277       *
 278       * Default null.
 279       *
 280       * @since 4.6.0
 281       * @var bool $delete_with_user
 282       */
 283      public $delete_with_user = null;
 284  
 285      /**
 286       * Array of blocks to use as the default initial state for an editor session.
 287       *
 288       * Each item should be an array containing block name and optional attributes.
 289       *
 290       * Default empty array.
 291       *
 292       * @link https://developer.wordpress.org/block-editor/developers/block-api/block-templates/
 293       *
 294       * @since 5.0.0
 295       * @var array[] $template
 296       */
 297      public $template = array();
 298  
 299      /**
 300       * Whether the block template should be locked if $template is set.
 301       *
 302       * - If set to 'all', the user is unable to insert new blocks, move existing blocks
 303       *   and delete blocks.
 304       * - If set to 'insert', the user is able to move existing blocks but is unable to insert
 305       *   new blocks and delete blocks.
 306       * - If set to 'contentOnly', the user is only able to edit the content of existing blocks.
 307       *
 308       * Default false.
 309       *
 310       * @link https://developer.wordpress.org/block-editor/developers/block-api/block-templates/
 311       *
 312       * @since 5.0.0
 313       * @var string|false $template_lock
 314       */
 315      public $template_lock = false;
 316  
 317      /**
 318       * Whether this post type is a native or "built-in" post_type.
 319       *
 320       * Default false.
 321       *
 322       * @since 4.6.0
 323       * @var bool $_builtin
 324       */
 325      public $_builtin = false;
 326  
 327      /**
 328       * URL segment to use for edit link of this post type.
 329       *
 330       * Default 'post.php?post=%d'.
 331       *
 332       * @since 4.6.0
 333       * @var string $_edit_link
 334       */
 335      public $_edit_link = 'post.php?post=%d';
 336  
 337      /**
 338       * Post type capabilities.
 339       *
 340       * @see get_post_type_capabilities()
 341       *
 342       * @since 4.6.0
 343       * @var stdClass $cap
 344       */
 345      public $cap;
 346  
 347      /**
 348       * Triggers the handling of rewrites for this post type.
 349       *
 350       * Defaults to true, using $post_type as slug.
 351       *
 352       * @since 4.6.0
 353       * @var array|false $rewrite
 354       */
 355      public $rewrite;
 356  
 357      /**
 358       * The features supported by the post type.
 359       *
 360       * @since 4.6.0
 361       * @var array|bool $supports
 362       */
 363      public $supports;
 364  
 365      /**
 366       * Whether this post type should appear in the REST API.
 367       *
 368       * Default false. If true, standard endpoints will be registered with
 369       * respect to $rest_base and $rest_controller_class.
 370       *
 371       * @since 4.7.4
 372       * @var bool $show_in_rest
 373       */
 374      public $show_in_rest;
 375  
 376      /**
 377       * The base path for this post type's REST API endpoints.
 378       *
 379       * @since 4.7.4
 380       * @var string|bool $rest_base
 381       */
 382      public $rest_base;
 383  
 384      /**
 385       * The namespace for this post type's REST API endpoints.
 386       *
 387       * @since 5.9.0
 388       * @var string|bool $rest_namespace
 389       */
 390      public $rest_namespace;
 391  
 392      /**
 393       * The controller for this post type's REST API endpoints.
 394       *
 395       * Custom controllers must extend WP_REST_Controller.
 396       *
 397       * @since 4.7.4
 398       * @var string|bool $rest_controller_class
 399       */
 400      public $rest_controller_class;
 401  
 402      /**
 403       * The controller instance for this post type's REST API endpoints.
 404       *
 405       * Lazily computed. Should be accessed using {@see WP_Post_Type::get_rest_controller()}.
 406       *
 407       * @since 5.3.0
 408       * @var WP_REST_Controller $rest_controller
 409       */
 410      public $rest_controller;
 411  
 412      /**
 413       * The controller for this post type's revisions REST API endpoints.
 414       *
 415       * Custom controllers must extend WP_REST_Controller.
 416       *
 417       * @since 6.4.0
 418       * @var string|bool $revisions_rest_controller_class
 419       */
 420      public $revisions_rest_controller_class;
 421  
 422      /**
 423       * The controller instance for this post type's revisions REST API endpoints.
 424       *
 425       * Lazily computed. Should be accessed using {@see WP_Post_Type::get_revisions_rest_controller()}.
 426       *
 427       * @since 6.4.0
 428       * @var WP_REST_Controller $revisions_rest_controller
 429       */
 430      public $revisions_rest_controller;
 431  
 432      /**
 433       * The controller for this post type's autosave REST API endpoints.
 434       *
 435       * Custom controllers must extend WP_REST_Controller.
 436       *
 437       * @since 6.4.0
 438       * @var string|bool $autosave_rest_controller_class
 439       */
 440      public $autosave_rest_controller_class;
 441  
 442      /**
 443       * The controller instance for this post type's autosave REST API endpoints.
 444       *
 445       * Lazily computed. Should be accessed using {@see WP_Post_Type::get_autosave_rest_controller()}.
 446       *
 447       * @since 6.4.0
 448       * @var WP_REST_Controller $autosave_rest_controller
 449       */
 450      public $autosave_rest_controller;
 451  
 452      /**
 453       * A flag to register the post type REST API controller after its associated autosave / revisions controllers, instead of before. Registration order affects route matching priority.
 454       *
 455       * @since 6.4.0
 456       * @var bool $late_route_registration
 457       */
 458      public $late_route_registration;
 459  
 460      /**
 461       * Constructor.
 462       *
 463       * See the register_post_type() function for accepted arguments for `$args`.
 464       *
 465       * Will populate object properties from the provided arguments and assign other
 466       * default properties based on that information.
 467       *
 468       * @since 4.6.0
 469       *
 470       * @see register_post_type()
 471       *
 472       * @param string       $post_type Post type key.
 473       * @param array|string $args      Optional. Array or string of arguments for registering a post type.
 474       *                                See register_post_type() for information on accepted arguments.
 475       *                                Default empty array.
 476       */
 477  	public function __construct( $post_type, $args = array() ) {
 478          $this->name = $post_type;
 479  
 480          $this->set_props( $args );
 481      }
 482  
 483      /**
 484       * Sets post type properties.
 485       *
 486       * See the register_post_type() function for accepted arguments for `$args`.
 487       *
 488       * @since 4.6.0
 489       *
 490       * @param array|string $args Array or string of arguments for registering a post type.
 491       */
 492  	public function set_props( $args ) {
 493          $args = wp_parse_args( $args );
 494  
 495          /**
 496           * Filters the arguments for registering a post type.
 497           *
 498           * @since 4.4.0
 499           *
 500           * @param array  $args      Array of arguments for registering a post type.
 501           *                          See the register_post_type() function for accepted arguments.
 502           * @param string $post_type Post type key.
 503           */
 504          $args = apply_filters( 'register_post_type_args', $args, $this->name );
 505  
 506          $post_type = $this->name;
 507  
 508          /**
 509           * Filters the arguments for registering a specific post type.
 510           *
 511           * The dynamic portion of the filter name, `$post_type`, refers to the post type key.
 512           *
 513           * Possible hook names include:
 514           *
 515           *  - `register_post_post_type_args`
 516           *  - `register_page_post_type_args`
 517           *
 518           * @since 6.0.0
 519           * @since 6.4.0 Added `late_route_registration`, `autosave_rest_controller_class` and `revisions_rest_controller_class` arguments.
 520           *
 521           * @param array  $args      Array of arguments for registering a post type.
 522           *                          See the register_post_type() function for accepted arguments.
 523           * @param string $post_type Post type key.
 524           */
 525          $args = apply_filters( "register_{$post_type}_post_type_args", $args, $this->name );
 526  
 527          $has_edit_link = ! empty( $args['_edit_link'] );
 528  
 529          // Args prefixed with an underscore are reserved for internal use.
 530          $defaults = array(
 531              'labels'                          => array(),
 532              'description'                     => '',
 533              'public'                          => false,
 534              'hierarchical'                    => false,
 535              'exclude_from_search'             => null,
 536              'publicly_queryable'              => null,
 537              'embeddable'                      => null,
 538              'show_ui'                         => null,
 539              'show_in_menu'                    => null,
 540              'show_in_nav_menus'               => null,
 541              'show_in_admin_bar'               => null,
 542              'menu_position'                   => null,
 543              'menu_icon'                       => null,
 544              'capability_type'                 => 'post',
 545              'capabilities'                    => array(),
 546              'map_meta_cap'                    => null,
 547              'supports'                        => array(),
 548              'register_meta_box_cb'            => null,
 549              'taxonomies'                      => array(),
 550              'has_archive'                     => false,
 551              'rewrite'                         => true,
 552              'query_var'                       => true,
 553              'can_export'                      => true,
 554              'delete_with_user'                => null,
 555              'show_in_rest'                    => false,
 556              'rest_base'                       => false,
 557              'rest_namespace'                  => false,
 558              'rest_controller_class'           => false,
 559              'autosave_rest_controller_class'  => false,
 560              'revisions_rest_controller_class' => false,
 561              'late_route_registration'         => false,
 562              'template'                        => array(),
 563              'template_lock'                   => false,
 564              '_builtin'                        => false,
 565              '_edit_link'                      => 'post.php?post=%d',
 566          );
 567  
 568          $args = array_merge( $defaults, $args );
 569  
 570          $args['name'] = $this->name;
 571  
 572          // If not set, default to the setting for 'public'.
 573          $args['publicly_queryable'] ??= $args['public'];
 574  
 575          // If not set, default to the setting for 'public'.
 576          $args['show_ui'] ??= $args['public'];
 577  
 578          // If not set, default to the setting for 'public'.
 579          $args['embeddable'] ??= $args['public'];
 580  
 581          // If not set, default rest_namespace to wp/v2 if show_in_rest is true.
 582          if ( false === $args['rest_namespace'] && ! empty( $args['show_in_rest'] ) ) {
 583              $args['rest_namespace'] = 'wp/v2';
 584          }
 585  
 586          // If not set, default to the setting for 'show_ui'.
 587          if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) {
 588              $args['show_in_menu'] = $args['show_ui'];
 589          }
 590  
 591          // If not set, default to the setting for 'show_in_menu'.
 592          $args['show_in_admin_bar'] ??= (bool) $args['show_in_menu'];
 593  
 594          // If not set, default to the setting for 'public'.
 595          $args['show_in_nav_menus'] ??= $args['public'];
 596  
 597          // If not set, default to true if not public, false if public.
 598          $args['exclude_from_search'] ??= ! $args['public'];
 599  
 600          // Back compat with quirky handling in version 3.0. #14122.
 601          if ( empty( $args['capabilities'] )
 602              && null === $args['map_meta_cap'] && in_array( $args['capability_type'], array( 'post', 'page' ), true )
 603          ) {
 604              $args['map_meta_cap'] = true;
 605          }
 606  
 607          // If not set, default to false.
 608          $args['map_meta_cap'] ??= false;
 609  
 610          // If there's no specified edit link and no UI, remove the edit link.
 611          if ( ! $args['show_ui'] && ! $has_edit_link ) {
 612              $args['_edit_link'] = '';
 613          }
 614  
 615          $this->cap = get_post_type_capabilities( (object) $args );
 616          unset( $args['capabilities'] );
 617  
 618          if ( is_array( $args['capability_type'] ) ) {
 619              $args['capability_type'] = $args['capability_type'][0];
 620          }
 621  
 622          if ( false !== $args['query_var'] ) {
 623              if ( true === $args['query_var'] ) {
 624                  $args['query_var'] = $this->name;
 625              } else {
 626                  $args['query_var'] = sanitize_title_with_dashes( $args['query_var'] );
 627              }
 628          }
 629  
 630          if ( false !== $args['rewrite'] && ( is_admin() || get_option( 'permalink_structure' ) ) ) {
 631              if ( ! is_array( $args['rewrite'] ) ) {
 632                  $args['rewrite'] = array();
 633              }
 634  
 635              if ( empty( $args['rewrite']['slug'] ) ) {
 636                  $args['rewrite']['slug'] = $this->name;
 637              }
 638  
 639              $args['rewrite']['with_front'] ??= true;
 640              $args['rewrite']['pages']      ??= true;
 641  
 642              if ( ! isset( $args['rewrite']['feeds'] ) || ! $args['has_archive'] ) {
 643                  $args['rewrite']['feeds'] = (bool) $args['has_archive'];
 644              }
 645  
 646              $args['rewrite']['ep_mask'] ??= $args['permalink_epmask'] ?? EP_PERMALINK;
 647          }
 648  
 649          foreach ( $args as $property_name => $property_value ) {
 650              $this->$property_name = $property_value;
 651          }
 652  
 653          $this->labels = get_post_type_labels( $this );
 654          $this->label  = $this->labels->name;
 655      }
 656  
 657      /**
 658       * Sets the features support for the post type.
 659       *
 660       * @since 4.6.0
 661       */
 662  	public function add_supports() {
 663          if ( ! empty( $this->supports ) ) {
 664              foreach ( $this->supports as $feature => $args ) {
 665                  if ( is_array( $args ) ) {
 666                      add_post_type_support( $this->name, $feature, $args );
 667                  } else {
 668                      add_post_type_support( $this->name, $args );
 669                  }
 670              }
 671              unset( $this->supports );
 672  
 673              /*
 674               * 'editor' support implies 'autosave' support for backward compatibility.
 675               * 'autosave' support needs to be explicitly removed if not desired.
 676               */
 677              if (
 678                  post_type_supports( $this->name, 'editor' ) &&
 679                  ! post_type_supports( $this->name, 'autosave' )
 680              ) {
 681                  add_post_type_support( $this->name, 'autosave' );
 682              }
 683          } elseif ( false !== $this->supports ) {
 684              // Add default features.
 685              add_post_type_support( $this->name, array( 'title', 'editor', 'autosave' ) );
 686          }
 687      }
 688  
 689      /**
 690       * Adds the necessary rewrite rules for the post type.
 691       *
 692       * @since 4.6.0
 693       *
 694       * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 695       * @global WP         $wp         Current WordPress environment instance.
 696       */
 697  	public function add_rewrite_rules() {
 698          global $wp_rewrite, $wp;
 699  
 700          if ( false !== $this->query_var && $wp && is_post_type_viewable( $this ) ) {
 701              $wp->add_query_var( $this->query_var );
 702          }
 703  
 704          if ( false !== $this->rewrite && ( is_admin() || get_option( 'permalink_structure' ) ) ) {
 705              if ( $this->hierarchical ) {
 706                  add_rewrite_tag( "%$this->name%", '(.+?)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&pagename=" );
 707              } else {
 708                  add_rewrite_tag( "%$this->name%", '([^/]+)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&name=" );
 709              }
 710  
 711              if ( $this->has_archive ) {
 712                  $archive_slug = true === $this->has_archive ? $this->rewrite['slug'] : $this->has_archive;
 713                  if ( $this->rewrite['with_front'] ) {
 714                      $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
 715                  } else {
 716                      $archive_slug = $wp_rewrite->root . $archive_slug;
 717                  }
 718  
 719                  add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$this->name", 'top' );
 720                  if ( $this->rewrite['feeds'] && $wp_rewrite->feeds ) {
 721                      $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
 722                      add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
 723                      add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
 724                  }
 725                  if ( $this->rewrite['pages'] ) {
 726                      add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$this->name" . '&paged=$matches[1]', 'top' );
 727                  }
 728              }
 729  
 730              $permastruct_args         = $this->rewrite;
 731              $permastruct_args['feed'] = $permastruct_args['feeds'];
 732              add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $permastruct_args );
 733          }
 734      }
 735  
 736      /**
 737       * Registers the post type meta box if a custom callback was specified.
 738       *
 739       * @since 4.6.0
 740       */
 741  	public function register_meta_boxes() {
 742          if ( $this->register_meta_box_cb ) {
 743              add_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10, 1 );
 744          }
 745      }
 746  
 747      /**
 748       * Adds the future post hook action for the post type.
 749       *
 750       * @since 4.6.0
 751       */
 752  	public function add_hooks() {
 753          add_action( 'future_' . $this->name, '_future_post_hook', 5, 2 );
 754      }
 755  
 756      /**
 757       * Registers the taxonomies for the post type.
 758       *
 759       * @since 4.6.0
 760       */
 761  	public function register_taxonomies() {
 762          foreach ( $this->taxonomies as $taxonomy ) {
 763              register_taxonomy_for_object_type( $taxonomy, $this->name );
 764          }
 765      }
 766  
 767      /**
 768       * Removes the features support for the post type.
 769       *
 770       * @since 4.6.0
 771       *
 772       * @global array $_wp_post_type_features Post type features.
 773       */
 774  	public function remove_supports() {
 775          global $_wp_post_type_features;
 776  
 777          unset( $_wp_post_type_features[ $this->name ] );
 778      }
 779  
 780      /**
 781       * Removes any rewrite rules, permastructs, and rules for the post type.
 782       *
 783       * @since 4.6.0
 784       * @since 7.2.0 Registered meta capabilities are no longer removed here. They are rebuilt
 785       *              from the post types that remain by {@see unregister_post_type()}.
 786       *
 787       * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 788       * @global WP         $wp         Current WordPress environment instance.
 789       */
 790  	public function remove_rewrite_rules() {
 791          global $wp, $wp_rewrite;
 792  
 793          // Remove query var.
 794          if ( false !== $this->query_var ) {
 795              $wp->remove_query_var( $this->query_var );
 796          }
 797  
 798          // Remove any rewrite rules, permastructs, and rules.
 799          if ( false !== $this->rewrite ) {
 800              remove_rewrite_tag( "%$this->name%" );
 801              remove_permastruct( $this->name );
 802              foreach ( $wp_rewrite->extra_rules_top as $regex => $query ) {
 803                  if ( str_contains( $query, "index.php?post_type=$this->name" ) ) {
 804                      unset( $wp_rewrite->extra_rules_top[ $regex ] );
 805                  }
 806              }
 807          }
 808      }
 809  
 810      /**
 811       * Unregisters the post type meta box if a custom callback was specified.
 812       *
 813       * @since 4.6.0
 814       */
 815  	public function unregister_meta_boxes() {
 816          if ( $this->register_meta_box_cb ) {
 817              remove_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10 );
 818          }
 819      }
 820  
 821      /**
 822       * Removes the post type from all taxonomies.
 823       *
 824       * @since 4.6.0
 825       */
 826  	public function unregister_taxonomies() {
 827          foreach ( get_object_taxonomies( $this->name ) as $taxonomy ) {
 828              unregister_taxonomy_for_object_type( $taxonomy, $this->name );
 829          }
 830      }
 831  
 832      /**
 833       * Removes the future post hook action for the post type.
 834       *
 835       * @since 4.6.0
 836       */
 837  	public function remove_hooks() {
 838          remove_action( 'future_' . $this->name, '_future_post_hook', 5 );
 839      }
 840  
 841      /**
 842       * Gets the REST API controller for this post type.
 843       *
 844       * Will only instantiate the controller class once per request.
 845       *
 846       * @since 5.3.0
 847       *
 848       * @return WP_REST_Controller|null The controller instance, or null if the post type
 849       *                                 is set not to show in rest.
 850       */
 851  	public function get_rest_controller() {
 852          if ( ! $this->show_in_rest ) {
 853              return null;
 854          }
 855  
 856          $class = $this->rest_controller_class ? $this->rest_controller_class : WP_REST_Posts_Controller::class;
 857  
 858          if ( ! class_exists( $class ) ) {
 859              return null;
 860          }
 861  
 862          if ( ! is_subclass_of( $class, WP_REST_Controller::class ) ) {
 863              return null;
 864          }
 865  
 866          if ( ! $this->rest_controller ) {
 867              $this->rest_controller = new $class( $this->name );
 868          }
 869  
 870          if ( ! ( $this->rest_controller instanceof $class ) ) {
 871              return null;
 872          }
 873  
 874          return $this->rest_controller;
 875      }
 876  
 877      /**
 878       * Gets the REST API revisions controller for this post type.
 879       *
 880       * Will only instantiate the controller class once per request.
 881       *
 882       * @since 6.4.0
 883       *
 884       * @return WP_REST_Controller|null The controller instance, or null if the post type
 885       *                                 is set not to show in rest.
 886       */
 887  	public function get_revisions_rest_controller() {
 888          if ( ! $this->show_in_rest ) {
 889              return null;
 890          }
 891  
 892          if ( ! post_type_supports( $this->name, 'revisions' ) ) {
 893              return null;
 894          }
 895  
 896          $class = $this->revisions_rest_controller_class ? $this->revisions_rest_controller_class : WP_REST_Revisions_Controller::class;
 897          if ( ! class_exists( $class ) ) {
 898              return null;
 899          }
 900  
 901          if ( ! is_subclass_of( $class, WP_REST_Controller::class ) ) {
 902              return null;
 903          }
 904  
 905          if ( ! $this->revisions_rest_controller ) {
 906              $this->revisions_rest_controller = new $class( $this->name );
 907          }
 908  
 909          if ( ! ( $this->revisions_rest_controller instanceof $class ) ) {
 910              return null;
 911          }
 912  
 913          return $this->revisions_rest_controller;
 914      }
 915  
 916      /**
 917       * Gets the REST API autosave controller for this post type.
 918       *
 919       * Will only instantiate the controller class once per request.
 920       *
 921       * @since 6.4.0
 922       *
 923       * @return WP_REST_Controller|null The controller instance, or null if the post type
 924       *                                 is set not to show in rest.
 925       */
 926  	public function get_autosave_rest_controller() {
 927          if ( ! $this->show_in_rest ) {
 928              return null;
 929          }
 930  
 931          if ( ! post_type_supports( $this->name, 'autosave' ) ) {
 932              return null;
 933          }
 934  
 935          $class = $this->autosave_rest_controller_class ? $this->autosave_rest_controller_class : WP_REST_Autosaves_Controller::class;
 936  
 937          if ( ! class_exists( $class ) ) {
 938              return null;
 939          }
 940  
 941          if ( ! is_subclass_of( $class, WP_REST_Controller::class ) ) {
 942              return null;
 943          }
 944  
 945          if ( ! $this->autosave_rest_controller ) {
 946              $this->autosave_rest_controller = new $class( $this->name );
 947          }
 948  
 949          if ( ! ( $this->autosave_rest_controller instanceof $class ) ) {
 950              return null;
 951          }
 952  
 953          return $this->autosave_rest_controller;
 954      }
 955  
 956      /**
 957       * Returns the default labels for post types.
 958       *
 959       * @since 6.0.0
 960       *
 961       * @return (string|null)[][] The default labels for post types.
 962       */
 963  	public static function get_default_labels() {
 964          if ( ! empty( self::$default_labels ) ) {
 965              return self::$default_labels;
 966          }
 967  
 968          self::$default_labels = array(
 969              'name'                     => array( _x( 'Posts', 'post type general name' ), _x( 'Pages', 'post type general name' ) ),
 970              'singular_name'            => array( _x( 'Post', 'post type singular name' ), _x( 'Page', 'post type singular name' ) ),
 971              'add_new'                  => array( __( 'Add' ), __( 'Add' ) ),
 972              'add_new_item'             => array( __( 'Add Post' ), __( 'Add Page' ) ),
 973              'edit_item'                => array( __( 'Edit Post' ), __( 'Edit Page' ) ),
 974              'new_item'                 => array( __( 'New Post' ), __( 'New Page' ) ),
 975              'view_item'                => array( __( 'View Post' ), __( 'View Page' ) ),
 976              'view_items'               => array( __( 'View Posts' ), __( 'View Pages' ) ),
 977              'search_items'             => array( __( 'Search Posts' ), __( 'Search Pages' ) ),
 978              'not_found'                => array( __( 'No posts found.' ), __( 'No pages found.' ) ),
 979              'not_found_in_trash'       => array( __( 'No posts found in Trash.' ), __( 'No pages found in Trash.' ) ),
 980              'parent_item_colon'        => array( null, __( 'Parent Page:' ) ),
 981              'all_items'                => array( __( 'All Posts' ), __( 'All Pages' ) ),
 982              'archives'                 => array( __( 'Post Archives' ), __( 'Page Archives' ) ),
 983              'attributes'               => array( __( 'Post Attributes' ), __( 'Page Attributes' ) ),
 984              'insert_into_item'         => array( __( 'Insert into post' ), __( 'Insert into page' ) ),
 985              'uploaded_to_this_item'    => array( __( 'Uploaded to this post' ), __( 'Uploaded to this page' ) ),
 986              'featured_image'           => array( _x( 'Featured image', 'post' ), _x( 'Featured image', 'page' ) ),
 987              'set_featured_image'       => array( _x( 'Set featured image', 'post' ), _x( 'Set featured image', 'page' ) ),
 988              'remove_featured_image'    => array( _x( 'Remove featured image', 'post' ), _x( 'Remove featured image', 'page' ) ),
 989              'use_featured_image'       => array( _x( 'Use as featured image', 'post' ), _x( 'Use as featured image', 'page' ) ),
 990              'filter_items_list'        => array( __( 'Filter posts list' ), __( 'Filter pages list' ) ),
 991              'filter_by_date'           => array( __( 'Filter by date' ), __( 'Filter by date' ) ),
 992              'items_list_navigation'    => array( __( 'Posts list navigation' ), __( 'Pages list navigation' ) ),
 993              'items_list'               => array( __( 'Posts list' ), __( 'Pages list' ) ),
 994              'item_published'           => array( __( 'Post published.' ), __( 'Page published.' ) ),
 995              'item_published_privately' => array( __( 'Post published privately.' ), __( 'Page published privately.' ) ),
 996              'item_reverted_to_draft'   => array( __( 'Post reverted to draft.' ), __( 'Page reverted to draft.' ) ),
 997              'item_trashed'             => array( __( 'Post trashed.' ), __( 'Page trashed.' ) ),
 998              'item_scheduled'           => array( __( 'Post scheduled.' ), __( 'Page scheduled.' ) ),
 999              'item_updated'             => array( __( 'Post updated.' ), __( 'Page updated.' ) ),
1000              'item_link'                => array(
1001                  _x( 'Post Link', 'navigation link block title' ),
1002                  _x( 'Page Link', 'navigation link block title' ),
1003              ),
1004              'item_link_description'    => array(
1005                  _x( 'A link to a post.', 'navigation link block description' ),
1006                  _x( 'A link to a page.', 'navigation link block description' ),
1007              ),
1008          );
1009  
1010          return self::$default_labels;
1011      }
1012  
1013      /**
1014       * Resets the cache for the default labels.
1015       *
1016       * @since 6.0.0
1017       */
1018  	public static function reset_default_labels() {
1019          self::$default_labels = array();
1020      }
1021  }


Generated : Sun Sep 13 08:20:28 2026 Cross-referenced by PHPXref