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


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref