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


Generated : Tue Apr 21 08:20:12 2026 Cross-referenced by PHPXref