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


Generated : Sun Nov 1 08:20:35 2026 Cross-referenced by PHPXref