[ 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       * @since 4.6.0
 341       * @var stdClass $cap
 342       */
 343      public $cap;
 344  
 345      /**
 346       * Triggers the handling of rewrites for this post type.
 347       *
 348       * Defaults to true, using $post_type as slug.
 349       *
 350       * @since 4.6.0
 351       * @var array|false $rewrite
 352       */
 353      public $rewrite;
 354  
 355      /**
 356       * The features supported by the post type.
 357       *
 358       * @since 4.6.0
 359       * @var array|bool $supports
 360       */
 361      public $supports;
 362  
 363      /**
 364       * Whether this post type should appear in the REST API.
 365       *
 366       * Default false. If true, standard endpoints will be registered with
 367       * respect to $rest_base and $rest_controller_class.
 368       *
 369       * @since 4.7.4
 370       * @var bool $show_in_rest
 371       */
 372      public $show_in_rest;
 373  
 374      /**
 375       * The base path for this post type's REST API endpoints.
 376       *
 377       * @since 4.7.4
 378       * @var string|bool $rest_base
 379       */
 380      public $rest_base;
 381  
 382      /**
 383       * The namespace for this post type's REST API endpoints.
 384       *
 385       * @since 5.9.0
 386       * @var string|bool $rest_namespace
 387       */
 388      public $rest_namespace;
 389  
 390      /**
 391       * The controller for this post type's REST API endpoints.
 392       *
 393       * Custom controllers must extend WP_REST_Controller.
 394       *
 395       * @since 4.7.4
 396       * @var string|bool $rest_controller_class
 397       */
 398      public $rest_controller_class;
 399  
 400      /**
 401       * The controller instance for this post type's REST API endpoints.
 402       *
 403       * Lazily computed. Should be accessed using {@see WP_Post_Type::get_rest_controller()}.
 404       *
 405       * @since 5.3.0
 406       * @var WP_REST_Controller $rest_controller
 407       */
 408      public $rest_controller;
 409  
 410      /**
 411       * The controller for this post type's revisions REST API endpoints.
 412       *
 413       * Custom controllers must extend WP_REST_Controller.
 414       *
 415       * @since 6.4.0
 416       * @var string|bool $revisions_rest_controller_class
 417       */
 418      public $revisions_rest_controller_class;
 419  
 420      /**
 421       * The controller instance for this post type's revisions REST API endpoints.
 422       *
 423       * Lazily computed. Should be accessed using {@see WP_Post_Type::get_revisions_rest_controller()}.
 424       *
 425       * @since 6.4.0
 426       * @var WP_REST_Controller $revisions_rest_controller
 427       */
 428      public $revisions_rest_controller;
 429  
 430      /**
 431       * The controller for this post type's autosave REST API endpoints.
 432       *
 433       * Custom controllers must extend WP_REST_Controller.
 434       *
 435       * @since 6.4.0
 436       * @var string|bool $autosave_rest_controller_class
 437       */
 438      public $autosave_rest_controller_class;
 439  
 440      /**
 441       * The controller instance for this post type's autosave REST API endpoints.
 442       *
 443       * Lazily computed. Should be accessed using {@see WP_Post_Type::get_autosave_rest_controller()}.
 444       *
 445       * @since 6.4.0
 446       * @var WP_REST_Controller $autosave_rest_controller
 447       */
 448      public $autosave_rest_controller;
 449  
 450      /**
 451       * 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.
 452       *
 453       * @since 6.4.0
 454       * @var bool $late_route_registration
 455       */
 456      public $late_route_registration;
 457  
 458      /**
 459       * Constructor.
 460       *
 461       * See the register_post_type() function for accepted arguments for `$args`.
 462       *
 463       * Will populate object properties from the provided arguments and assign other
 464       * default properties based on that information.
 465       *
 466       * @since 4.6.0
 467       *
 468       * @see register_post_type()
 469       *
 470       * @param string       $post_type Post type key.
 471       * @param array|string $args      Optional. Array or string of arguments for registering a post type.
 472       *                                See register_post_type() for information on accepted arguments.
 473       *                                Default empty array.
 474       */
 475  	public function __construct( $post_type, $args = array() ) {
 476          $this->name = $post_type;
 477  
 478          $this->set_props( $args );
 479      }
 480  
 481      /**
 482       * Sets post type properties.
 483       *
 484       * See the register_post_type() function for accepted arguments for `$args`.
 485       *
 486       * @since 4.6.0
 487       *
 488       * @param array|string $args Array or string of arguments for registering a post type.
 489       */
 490  	public function set_props( $args ) {
 491          $args = wp_parse_args( $args );
 492  
 493          /**
 494           * Filters the arguments for registering a post type.
 495           *
 496           * @since 4.4.0
 497           *
 498           * @param array  $args      Array of arguments for registering a post type.
 499           *                          See the register_post_type() function for accepted arguments.
 500           * @param string $post_type Post type key.
 501           */
 502          $args = apply_filters( 'register_post_type_args', $args, $this->name );
 503  
 504          $post_type = $this->name;
 505  
 506          /**
 507           * Filters the arguments for registering a specific post type.
 508           *
 509           * The dynamic portion of the filter name, `$post_type`, refers to the post type key.
 510           *
 511           * Possible hook names include:
 512           *
 513           *  - `register_post_post_type_args`
 514           *  - `register_page_post_type_args`
 515           *
 516           * @since 6.0.0
 517           * @since 6.4.0 Added `late_route_registration`, `autosave_rest_controller_class` and `revisions_rest_controller_class` arguments.
 518           *
 519           * @param array  $args      Array of arguments for registering a post type.
 520           *                          See the register_post_type() function for accepted arguments.
 521           * @param string $post_type Post type key.
 522           */
 523          $args = apply_filters( "register_{$post_type}_post_type_args", $args, $this->name );
 524  
 525          $has_edit_link = ! empty( $args['_edit_link'] );
 526  
 527          // Args prefixed with an underscore are reserved for internal use.
 528          $defaults = array(
 529              'labels'                          => array(),
 530              'description'                     => '',
 531              'public'                          => false,
 532              'hierarchical'                    => false,
 533              'exclude_from_search'             => null,
 534              'publicly_queryable'              => null,
 535              'embeddable'                      => null,
 536              'show_ui'                         => null,
 537              'show_in_menu'                    => null,
 538              'show_in_nav_menus'               => null,
 539              'show_in_admin_bar'               => null,
 540              'menu_position'                   => null,
 541              'menu_icon'                       => null,
 542              'capability_type'                 => 'post',
 543              'capabilities'                    => array(),
 544              'map_meta_cap'                    => null,
 545              'supports'                        => array(),
 546              'register_meta_box_cb'            => null,
 547              'taxonomies'                      => array(),
 548              'has_archive'                     => false,
 549              'rewrite'                         => true,
 550              'query_var'                       => true,
 551              'can_export'                      => true,
 552              'delete_with_user'                => null,
 553              'show_in_rest'                    => false,
 554              'rest_base'                       => false,
 555              'rest_namespace'                  => false,
 556              'rest_controller_class'           => false,
 557              'autosave_rest_controller_class'  => false,
 558              'revisions_rest_controller_class' => false,
 559              'late_route_registration'         => false,
 560              'template'                        => array(),
 561              'template_lock'                   => false,
 562              '_builtin'                        => false,
 563              '_edit_link'                      => 'post.php?post=%d',
 564          );
 565  
 566          $args = array_merge( $defaults, $args );
 567  
 568          $args['name'] = $this->name;
 569  
 570          // If not set, default to the setting for 'public'.
 571          if ( null === $args['publicly_queryable'] ) {
 572              $args['publicly_queryable'] = $args['public'];
 573          }
 574  
 575          // If not set, default to the setting for 'public'.
 576          if ( null === $args['show_ui'] ) {
 577              $args['show_ui'] = $args['public'];
 578          }
 579  
 580          // If not set, default to the setting for 'public'.
 581          if ( null === $args['embeddable'] ) {
 582              $args['embeddable'] = $args['public'];
 583          }
 584  
 585          // If not set, default rest_namespace to wp/v2 if show_in_rest is true.
 586          if ( false === $args['rest_namespace'] && ! empty( $args['show_in_rest'] ) ) {
 587              $args['rest_namespace'] = 'wp/v2';
 588          }
 589  
 590          // If not set, default to the setting for 'show_ui'.
 591          if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) {
 592              $args['show_in_menu'] = $args['show_ui'];
 593          }
 594  
 595          // If not set, default to the setting for 'show_in_menu'.
 596          if ( null === $args['show_in_admin_bar'] ) {
 597              $args['show_in_admin_bar'] = (bool) $args['show_in_menu'];
 598          }
 599  
 600          // If not set, default to the setting for 'public'.
 601          if ( null === $args['show_in_nav_menus'] ) {
 602              $args['show_in_nav_menus'] = $args['public'];
 603          }
 604  
 605          // If not set, default to true if not public, false if public.
 606          if ( null === $args['exclude_from_search'] ) {
 607              $args['exclude_from_search'] = ! $args['public'];
 608          }
 609  
 610          // Back compat with quirky handling in version 3.0. #14122.
 611          if ( empty( $args['capabilities'] )
 612              && null === $args['map_meta_cap'] && in_array( $args['capability_type'], array( 'post', 'page' ), true )
 613          ) {
 614              $args['map_meta_cap'] = true;
 615          }
 616  
 617          // If not set, default to false.
 618          if ( null === $args['map_meta_cap'] ) {
 619              $args['map_meta_cap'] = false;
 620          }
 621  
 622          // If there's no specified edit link and no UI, remove the edit link.
 623          if ( ! $args['show_ui'] && ! $has_edit_link ) {
 624              $args['_edit_link'] = '';
 625          }
 626  
 627          $this->cap = get_post_type_capabilities( (object) $args );
 628          unset( $args['capabilities'] );
 629  
 630          if ( is_array( $args['capability_type'] ) ) {
 631              $args['capability_type'] = $args['capability_type'][0];
 632          }
 633  
 634          if ( false !== $args['query_var'] ) {
 635              if ( true === $args['query_var'] ) {
 636                  $args['query_var'] = $this->name;
 637              } else {
 638                  $args['query_var'] = sanitize_title_with_dashes( $args['query_var'] );
 639              }
 640          }
 641  
 642          if ( false !== $args['rewrite'] && ( is_admin() || get_option( 'permalink_structure' ) ) ) {
 643              if ( ! is_array( $args['rewrite'] ) ) {
 644                  $args['rewrite'] = array();
 645              }
 646              if ( empty( $args['rewrite']['slug'] ) ) {
 647                  $args['rewrite']['slug'] = $this->name;
 648              }
 649              if ( ! isset( $args['rewrite']['with_front'] ) ) {
 650                  $args['rewrite']['with_front'] = true;
 651              }
 652              if ( ! isset( $args['rewrite']['pages'] ) ) {
 653                  $args['rewrite']['pages'] = true;
 654              }
 655              if ( ! isset( $args['rewrite']['feeds'] ) || ! $args['has_archive'] ) {
 656                  $args['rewrite']['feeds'] = (bool) $args['has_archive'];
 657              }
 658              if ( ! isset( $args['rewrite']['ep_mask'] ) ) {
 659                  $args['rewrite']['ep_mask'] = $args['permalink_epmask'] ?? EP_PERMALINK;
 660              }
 661          }
 662  
 663          foreach ( $args as $property_name => $property_value ) {
 664              $this->$property_name = $property_value;
 665          }
 666  
 667          $this->labels = get_post_type_labels( $this );
 668          $this->label  = $this->labels->name;
 669      }
 670  
 671      /**
 672       * Sets the features support for the post type.
 673       *
 674       * @since 4.6.0
 675       */
 676  	public function add_supports() {
 677          if ( ! empty( $this->supports ) ) {
 678              foreach ( $this->supports as $feature => $args ) {
 679                  if ( is_array( $args ) ) {
 680                      add_post_type_support( $this->name, $feature, $args );
 681                  } else {
 682                      add_post_type_support( $this->name, $args );
 683                  }
 684              }
 685              unset( $this->supports );
 686  
 687              /*
 688               * 'editor' support implies 'autosave' support for backward compatibility.
 689               * 'autosave' support needs to be explicitly removed if not desired.
 690               */
 691              if (
 692                  post_type_supports( $this->name, 'editor' ) &&
 693                  ! post_type_supports( $this->name, 'autosave' )
 694              ) {
 695                  add_post_type_support( $this->name, 'autosave' );
 696              }
 697          } elseif ( false !== $this->supports ) {
 698              // Add default features.
 699              add_post_type_support( $this->name, array( 'title', 'editor', 'autosave' ) );
 700          }
 701      }
 702  
 703      /**
 704       * Adds the necessary rewrite rules for the post type.
 705       *
 706       * @since 4.6.0
 707       *
 708       * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 709       * @global WP         $wp         Current WordPress environment instance.
 710       */
 711  	public function add_rewrite_rules() {
 712          global $wp_rewrite, $wp;
 713  
 714          if ( false !== $this->query_var && $wp && is_post_type_viewable( $this ) ) {
 715              $wp->add_query_var( $this->query_var );
 716          }
 717  
 718          if ( false !== $this->rewrite && ( is_admin() || get_option( 'permalink_structure' ) ) ) {
 719              if ( $this->hierarchical ) {
 720                  add_rewrite_tag( "%$this->name%", '(.+?)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&pagename=" );
 721              } else {
 722                  add_rewrite_tag( "%$this->name%", '([^/]+)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&name=" );
 723              }
 724  
 725              if ( $this->has_archive ) {
 726                  $archive_slug = true === $this->has_archive ? $this->rewrite['slug'] : $this->has_archive;
 727                  if ( $this->rewrite['with_front'] ) {
 728                      $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
 729                  } else {
 730                      $archive_slug = $wp_rewrite->root . $archive_slug;
 731                  }
 732  
 733                  add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$this->name", 'top' );
 734                  if ( $this->rewrite['feeds'] && $wp_rewrite->feeds ) {
 735                      $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
 736                      add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
 737                      add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
 738                  }
 739                  if ( $this->rewrite['pages'] ) {
 740                      add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$this->name" . '&paged=$matches[1]', 'top' );
 741                  }
 742              }
 743  
 744              $permastruct_args         = $this->rewrite;
 745              $permastruct_args['feed'] = $permastruct_args['feeds'];
 746              add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $permastruct_args );
 747          }
 748      }
 749  
 750      /**
 751       * Registers the post type meta box if a custom callback was specified.
 752       *
 753       * @since 4.6.0
 754       */
 755  	public function register_meta_boxes() {
 756          if ( $this->register_meta_box_cb ) {
 757              add_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10, 1 );
 758          }
 759      }
 760  
 761      /**
 762       * Adds the future post hook action for the post type.
 763       *
 764       * @since 4.6.0
 765       */
 766  	public function add_hooks() {
 767          add_action( 'future_' . $this->name, '_future_post_hook', 5, 2 );
 768      }
 769  
 770      /**
 771       * Registers the taxonomies for the post type.
 772       *
 773       * @since 4.6.0
 774       */
 775  	public function register_taxonomies() {
 776          foreach ( $this->taxonomies as $taxonomy ) {
 777              register_taxonomy_for_object_type( $taxonomy, $this->name );
 778          }
 779      }
 780  
 781      /**
 782       * Removes the features support for the post type.
 783       *
 784       * @since 4.6.0
 785       *
 786       * @global array $_wp_post_type_features Post type features.
 787       */
 788  	public function remove_supports() {
 789          global $_wp_post_type_features;
 790  
 791          unset( $_wp_post_type_features[ $this->name ] );
 792      }
 793  
 794      /**
 795       * Removes any rewrite rules, permastructs, and rules for the post type.
 796       *
 797       * @since 4.6.0
 798       *
 799       * @global WP_Rewrite $wp_rewrite          WordPress rewrite component.
 800       * @global WP         $wp                  Current WordPress environment instance.
 801       * @global array      $post_type_meta_caps Used to remove meta capabilities.
 802       */
 803  	public function remove_rewrite_rules() {
 804          global $wp, $wp_rewrite, $post_type_meta_caps;
 805  
 806          // Remove query var.
 807          if ( false !== $this->query_var ) {
 808              $wp->remove_query_var( $this->query_var );
 809          }
 810  
 811          // Remove any rewrite rules, permastructs, and rules.
 812          if ( false !== $this->rewrite ) {
 813              remove_rewrite_tag( "%$this->name%" );
 814              remove_permastruct( $this->name );
 815              foreach ( $wp_rewrite->extra_rules_top as $regex => $query ) {
 816                  if ( str_contains( $query, "index.php?post_type=$this->name" ) ) {
 817                      unset( $wp_rewrite->extra_rules_top[ $regex ] );
 818                  }
 819              }
 820          }
 821  
 822          // Remove registered custom meta capabilities.
 823          foreach ( $this->cap as $cap ) {
 824              unset( $post_type_meta_caps[ $cap ] );
 825          }
 826      }
 827  
 828      /**
 829       * Unregisters the post type meta box if a custom callback was specified.
 830       *
 831       * @since 4.6.0
 832       */
 833  	public function unregister_meta_boxes() {
 834          if ( $this->register_meta_box_cb ) {
 835              remove_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10 );
 836          }
 837      }
 838  
 839      /**
 840       * Removes the post type from all taxonomies.
 841       *
 842       * @since 4.6.0
 843       */
 844  	public function unregister_taxonomies() {
 845          foreach ( get_object_taxonomies( $this->name ) as $taxonomy ) {
 846              unregister_taxonomy_for_object_type( $taxonomy, $this->name );
 847          }
 848      }
 849  
 850      /**
 851       * Removes the future post hook action for the post type.
 852       *
 853       * @since 4.6.0
 854       */
 855  	public function remove_hooks() {
 856          remove_action( 'future_' . $this->name, '_future_post_hook', 5 );
 857      }
 858  
 859      /**
 860       * Gets the REST API controller for this post type.
 861       *
 862       * Will only instantiate the controller class once per request.
 863       *
 864       * @since 5.3.0
 865       *
 866       * @return WP_REST_Controller|null The controller instance, or null if the post type
 867       *                                 is set not to show in rest.
 868       */
 869  	public function get_rest_controller() {
 870          if ( ! $this->show_in_rest ) {
 871              return null;
 872          }
 873  
 874          $class = $this->rest_controller_class ? $this->rest_controller_class : WP_REST_Posts_Controller::class;
 875  
 876          if ( ! class_exists( $class ) ) {
 877              return null;
 878          }
 879  
 880          if ( ! is_subclass_of( $class, WP_REST_Controller::class ) ) {
 881              return null;
 882          }
 883  
 884          if ( ! $this->rest_controller ) {
 885              $this->rest_controller = new $class( $this->name );
 886          }
 887  
 888          if ( ! ( $this->rest_controller instanceof $class ) ) {
 889              return null;
 890          }
 891  
 892          return $this->rest_controller;
 893      }
 894  
 895      /**
 896       * Gets the REST API revisions controller for this post type.
 897       *
 898       * Will only instantiate the controller class once per request.
 899       *
 900       * @since 6.4.0
 901       *
 902       * @return WP_REST_Controller|null The controller instance, or null if the post type
 903       *                                 is set not to show in rest.
 904       */
 905  	public function get_revisions_rest_controller() {
 906          if ( ! $this->show_in_rest ) {
 907              return null;
 908          }
 909  
 910          if ( ! post_type_supports( $this->name, 'revisions' ) ) {
 911              return null;
 912          }
 913  
 914          $class = $this->revisions_rest_controller_class ? $this->revisions_rest_controller_class : WP_REST_Revisions_Controller::class;
 915          if ( ! class_exists( $class ) ) {
 916              return null;
 917          }
 918  
 919          if ( ! is_subclass_of( $class, WP_REST_Controller::class ) ) {
 920              return null;
 921          }
 922  
 923          if ( ! $this->revisions_rest_controller ) {
 924              $this->revisions_rest_controller = new $class( $this->name );
 925          }
 926  
 927          if ( ! ( $this->revisions_rest_controller instanceof $class ) ) {
 928              return null;
 929          }
 930  
 931          return $this->revisions_rest_controller;
 932      }
 933  
 934      /**
 935       * Gets the REST API autosave controller for this post type.
 936       *
 937       * Will only instantiate the controller class once per request.
 938       *
 939       * @since 6.4.0
 940       *
 941       * @return WP_REST_Controller|null The controller instance, or null if the post type
 942       *                                 is set not to show in rest.
 943       */
 944  	public function get_autosave_rest_controller() {
 945          if ( ! $this->show_in_rest ) {
 946              return null;
 947          }
 948  
 949          if ( ! post_type_supports( $this->name, 'autosave' ) ) {
 950              return null;
 951          }
 952  
 953          $class = $this->autosave_rest_controller_class ? $this->autosave_rest_controller_class : WP_REST_Autosaves_Controller::class;
 954  
 955          if ( ! class_exists( $class ) ) {
 956              return null;
 957          }
 958  
 959          if ( ! is_subclass_of( $class, WP_REST_Controller::class ) ) {
 960              return null;
 961          }
 962  
 963          if ( ! $this->autosave_rest_controller ) {
 964              $this->autosave_rest_controller = new $class( $this->name );
 965          }
 966  
 967          if ( ! ( $this->autosave_rest_controller instanceof $class ) ) {
 968              return null;
 969          }
 970  
 971          return $this->autosave_rest_controller;
 972      }
 973  
 974      /**
 975       * Returns the default labels for post types.
 976       *
 977       * @since 6.0.0
 978       *
 979       * @return (string|null)[][] The default labels for post types.
 980       */
 981  	public static function get_default_labels() {
 982          if ( ! empty( self::$default_labels ) ) {
 983              return self::$default_labels;
 984          }
 985  
 986          self::$default_labels = array(
 987              'name'                     => array( _x( 'Posts', 'post type general name' ), _x( 'Pages', 'post type general name' ) ),
 988              'singular_name'            => array( _x( 'Post', 'post type singular name' ), _x( 'Page', 'post type singular name' ) ),
 989              'add_new'                  => array( __( 'Add' ), __( 'Add' ) ),
 990              'add_new_item'             => array( __( 'Add Post' ), __( 'Add Page' ) ),
 991              'edit_item'                => array( __( 'Edit Post' ), __( 'Edit Page' ) ),
 992              'new_item'                 => array( __( 'New Post' ), __( 'New Page' ) ),
 993              'view_item'                => array( __( 'View Post' ), __( 'View Page' ) ),
 994              'view_items'               => array( __( 'View Posts' ), __( 'View Pages' ) ),
 995              'search_items'             => array( __( 'Search Posts' ), __( 'Search Pages' ) ),
 996              'not_found'                => array( __( 'No posts found.' ), __( 'No pages found.' ) ),
 997              'not_found_in_trash'       => array( __( 'No posts found in Trash.' ), __( 'No pages found in Trash.' ) ),
 998              'parent_item_colon'        => array( null, __( 'Parent Page:' ) ),
 999              'all_items'                => array( __( 'All Posts' ), __( 'All Pages' ) ),
1000              'archives'                 => array( __( 'Post Archives' ), __( 'Page Archives' ) ),
1001              'attributes'               => array( __( 'Post Attributes' ), __( 'Page Attributes' ) ),
1002              'insert_into_item'         => array( __( 'Insert into post' ), __( 'Insert into page' ) ),
1003              'uploaded_to_this_item'    => array( __( 'Uploaded to this post' ), __( 'Uploaded to this page' ) ),
1004              'featured_image'           => array( _x( 'Featured image', 'post' ), _x( 'Featured image', 'page' ) ),
1005              'set_featured_image'       => array( _x( 'Set featured image', 'post' ), _x( 'Set featured image', 'page' ) ),
1006              'remove_featured_image'    => array( _x( 'Remove featured image', 'post' ), _x( 'Remove featured image', 'page' ) ),
1007              'use_featured_image'       => array( _x( 'Use as featured image', 'post' ), _x( 'Use as featured image', 'page' ) ),
1008              'filter_items_list'        => array( __( 'Filter posts list' ), __( 'Filter pages list' ) ),
1009              'filter_by_date'           => array( __( 'Filter by date' ), __( 'Filter by date' ) ),
1010              'items_list_navigation'    => array( __( 'Posts list navigation' ), __( 'Pages list navigation' ) ),
1011              'items_list'               => array( __( 'Posts list' ), __( 'Pages list' ) ),
1012              'item_published'           => array( __( 'Post published.' ), __( 'Page published.' ) ),
1013              'item_published_privately' => array( __( 'Post published privately.' ), __( 'Page published privately.' ) ),
1014              'item_reverted_to_draft'   => array( __( 'Post reverted to draft.' ), __( 'Page reverted to draft.' ) ),
1015              'item_trashed'             => array( __( 'Post trashed.' ), __( 'Page trashed.' ) ),
1016              'item_scheduled'           => array( __( 'Post scheduled.' ), __( 'Page scheduled.' ) ),
1017              'item_updated'             => array( __( 'Post updated.' ), __( 'Page updated.' ) ),
1018              'item_link'                => array(
1019                  _x( 'Post Link', 'navigation link block title' ),
1020                  _x( 'Page Link', 'navigation link block title' ),
1021              ),
1022              'item_link_description'    => array(
1023                  _x( 'A link to a post.', 'navigation link block description' ),
1024                  _x( 'A link to a page.', 'navigation link block description' ),
1025              ),
1026          );
1027  
1028          return self::$default_labels;
1029      }
1030  
1031      /**
1032       * Resets the cache for the default labels.
1033       *
1034       * @since 6.0.0
1035       */
1036  	public static function reset_default_labels() {
1037          self::$default_labels = array();
1038      }
1039  }


Generated : Sun Jun 21 08:20:10 2026 Cross-referenced by PHPXref