[ 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                  if ( isset( $args['permalink_epmask'] ) ) {
 659                      $args['rewrite']['ep_mask'] = $args['permalink_epmask'];
 660                  } else {
 661                      $args['rewrite']['ep_mask'] = EP_PERMALINK;
 662                  }
 663              }
 664          }
 665  
 666          foreach ( $args as $property_name => $property_value ) {
 667              $this->$property_name = $property_value;
 668          }
 669  
 670          $this->labels = get_post_type_labels( $this );
 671          $this->label  = $this->labels->name;
 672      }
 673  
 674      /**
 675       * Sets the features support for the post type.
 676       *
 677       * @since 4.6.0
 678       */
 679  	public function add_supports() {
 680          if ( ! empty( $this->supports ) ) {
 681              foreach ( $this->supports as $feature => $args ) {
 682                  if ( is_array( $args ) ) {
 683                      add_post_type_support( $this->name, $feature, $args );
 684                  } else {
 685                      add_post_type_support( $this->name, $args );
 686                  }
 687              }
 688              unset( $this->supports );
 689  
 690              /*
 691               * 'editor' support implies 'autosave' support for backward compatibility.
 692               * 'autosave' support needs to be explicitly removed if not desired.
 693               */
 694              if (
 695                  post_type_supports( $this->name, 'editor' ) &&
 696                  ! post_type_supports( $this->name, 'autosave' )
 697              ) {
 698                  add_post_type_support( $this->name, 'autosave' );
 699              }
 700          } elseif ( false !== $this->supports ) {
 701              // Add default features.
 702              add_post_type_support( $this->name, array( 'title', 'editor', 'autosave' ) );
 703          }
 704      }
 705  
 706      /**
 707       * Adds the necessary rewrite rules for the post type.
 708       *
 709       * @since 4.6.0
 710       *
 711       * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 712       * @global WP         $wp         Current WordPress environment instance.
 713       */
 714  	public function add_rewrite_rules() {
 715          global $wp_rewrite, $wp;
 716  
 717          if ( false !== $this->query_var && $wp && is_post_type_viewable( $this ) ) {
 718              $wp->add_query_var( $this->query_var );
 719          }
 720  
 721          if ( false !== $this->rewrite && ( is_admin() || get_option( 'permalink_structure' ) ) ) {
 722              if ( $this->hierarchical ) {
 723                  add_rewrite_tag( "%$this->name%", '(.+?)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&pagename=" );
 724              } else {
 725                  add_rewrite_tag( "%$this->name%", '([^/]+)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&name=" );
 726              }
 727  
 728              if ( $this->has_archive ) {
 729                  $archive_slug = true === $this->has_archive ? $this->rewrite['slug'] : $this->has_archive;
 730                  if ( $this->rewrite['with_front'] ) {
 731                      $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
 732                  } else {
 733                      $archive_slug = $wp_rewrite->root . $archive_slug;
 734                  }
 735  
 736                  add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$this->name", 'top' );
 737                  if ( $this->rewrite['feeds'] && $wp_rewrite->feeds ) {
 738                      $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
 739                      add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
 740                      add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
 741                  }
 742                  if ( $this->rewrite['pages'] ) {
 743                      add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$this->name" . '&paged=$matches[1]', 'top' );
 744                  }
 745              }
 746  
 747              $permastruct_args         = $this->rewrite;
 748              $permastruct_args['feed'] = $permastruct_args['feeds'];
 749              add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $permastruct_args );
 750          }
 751      }
 752  
 753      /**
 754       * Registers the post type meta box if a custom callback was specified.
 755       *
 756       * @since 4.6.0
 757       */
 758  	public function register_meta_boxes() {
 759          if ( $this->register_meta_box_cb ) {
 760              add_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10, 1 );
 761          }
 762      }
 763  
 764      /**
 765       * Adds the future post hook action for the post type.
 766       *
 767       * @since 4.6.0
 768       */
 769  	public function add_hooks() {
 770          add_action( 'future_' . $this->name, '_future_post_hook', 5, 2 );
 771      }
 772  
 773      /**
 774       * Registers the taxonomies for the post type.
 775       *
 776       * @since 4.6.0
 777       */
 778  	public function register_taxonomies() {
 779          foreach ( $this->taxonomies as $taxonomy ) {
 780              register_taxonomy_for_object_type( $taxonomy, $this->name );
 781          }
 782      }
 783  
 784      /**
 785       * Removes the features support for the post type.
 786       *
 787       * @since 4.6.0
 788       *
 789       * @global array $_wp_post_type_features Post type features.
 790       */
 791  	public function remove_supports() {
 792          global $_wp_post_type_features;
 793  
 794          unset( $_wp_post_type_features[ $this->name ] );
 795      }
 796  
 797      /**
 798       * Removes any rewrite rules, permastructs, and rules for the post type.
 799       *
 800       * @since 4.6.0
 801       *
 802       * @global WP_Rewrite $wp_rewrite          WordPress rewrite component.
 803       * @global WP         $wp                  Current WordPress environment instance.
 804       * @global array      $post_type_meta_caps Used to remove meta capabilities.
 805       */
 806  	public function remove_rewrite_rules() {
 807          global $wp, $wp_rewrite, $post_type_meta_caps;
 808  
 809          // Remove query var.
 810          if ( false !== $this->query_var ) {
 811              $wp->remove_query_var( $this->query_var );
 812          }
 813  
 814          // Remove any rewrite rules, permastructs, and rules.
 815          if ( false !== $this->rewrite ) {
 816              remove_rewrite_tag( "%$this->name%" );
 817              remove_permastruct( $this->name );
 818              foreach ( $wp_rewrite->extra_rules_top as $regex => $query ) {
 819                  if ( str_contains( $query, "index.php?post_type=$this->name" ) ) {
 820                      unset( $wp_rewrite->extra_rules_top[ $regex ] );
 821                  }
 822              }
 823          }
 824  
 825          // Remove registered custom meta capabilities.
 826          foreach ( $this->cap as $cap ) {
 827              unset( $post_type_meta_caps[ $cap ] );
 828          }
 829      }
 830  
 831      /**
 832       * Unregisters the post type meta box if a custom callback was specified.
 833       *
 834       * @since 4.6.0
 835       */
 836  	public function unregister_meta_boxes() {
 837          if ( $this->register_meta_box_cb ) {
 838              remove_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10 );
 839          }
 840      }
 841  
 842      /**
 843       * Removes the post type from all taxonomies.
 844       *
 845       * @since 4.6.0
 846       */
 847  	public function unregister_taxonomies() {
 848          foreach ( get_object_taxonomies( $this->name ) as $taxonomy ) {
 849              unregister_taxonomy_for_object_type( $taxonomy, $this->name );
 850          }
 851      }
 852  
 853      /**
 854       * Removes the future post hook action for the post type.
 855       *
 856       * @since 4.6.0
 857       */
 858  	public function remove_hooks() {
 859          remove_action( 'future_' . $this->name, '_future_post_hook', 5 );
 860      }
 861  
 862      /**
 863       * Gets the REST API controller for this post type.
 864       *
 865       * Will only instantiate the controller class once per request.
 866       *
 867       * @since 5.3.0
 868       *
 869       * @return WP_REST_Controller|null The controller instance, or null if the post type
 870       *                                 is set not to show in rest.
 871       */
 872  	public function get_rest_controller() {
 873          if ( ! $this->show_in_rest ) {
 874              return null;
 875          }
 876  
 877          $class = $this->rest_controller_class ? $this->rest_controller_class : WP_REST_Posts_Controller::class;
 878  
 879          if ( ! class_exists( $class ) ) {
 880              return null;
 881          }
 882  
 883          if ( ! is_subclass_of( $class, WP_REST_Controller::class ) ) {
 884              return null;
 885          }
 886  
 887          if ( ! $this->rest_controller ) {
 888              $this->rest_controller = new $class( $this->name );
 889          }
 890  
 891          if ( ! ( $this->rest_controller instanceof $class ) ) {
 892              return null;
 893          }
 894  
 895          return $this->rest_controller;
 896      }
 897  
 898      /**
 899       * Gets the REST API revisions controller for this post type.
 900       *
 901       * Will only instantiate the controller class once per request.
 902       *
 903       * @since 6.4.0
 904       *
 905       * @return WP_REST_Controller|null The controller instance, or null if the post type
 906       *                                 is set not to show in rest.
 907       */
 908  	public function get_revisions_rest_controller() {
 909          if ( ! $this->show_in_rest ) {
 910              return null;
 911          }
 912  
 913          if ( ! post_type_supports( $this->name, 'revisions' ) ) {
 914              return null;
 915          }
 916  
 917          $class = $this->revisions_rest_controller_class ? $this->revisions_rest_controller_class : WP_REST_Revisions_Controller::class;
 918          if ( ! class_exists( $class ) ) {
 919              return null;
 920          }
 921  
 922          if ( ! is_subclass_of( $class, WP_REST_Controller::class ) ) {
 923              return null;
 924          }
 925  
 926          if ( ! $this->revisions_rest_controller ) {
 927              $this->revisions_rest_controller = new $class( $this->name );
 928          }
 929  
 930          if ( ! ( $this->revisions_rest_controller instanceof $class ) ) {
 931              return null;
 932          }
 933  
 934          return $this->revisions_rest_controller;
 935      }
 936  
 937      /**
 938       * Gets the REST API autosave controller for this post type.
 939       *
 940       * Will only instantiate the controller class once per request.
 941       *
 942       * @since 6.4.0
 943       *
 944       * @return WP_REST_Controller|null The controller instance, or null if the post type
 945       *                                 is set not to show in rest.
 946       */
 947  	public function get_autosave_rest_controller() {
 948          if ( ! $this->show_in_rest ) {
 949              return null;
 950          }
 951  
 952          if ( ! post_type_supports( $this->name, 'autosave' ) ) {
 953              return null;
 954          }
 955  
 956          $class = $this->autosave_rest_controller_class ? $this->autosave_rest_controller_class : WP_REST_Autosaves_Controller::class;
 957  
 958          if ( ! class_exists( $class ) ) {
 959              return null;
 960          }
 961  
 962          if ( ! is_subclass_of( $class, WP_REST_Controller::class ) ) {
 963              return null;
 964          }
 965  
 966          if ( ! $this->autosave_rest_controller ) {
 967              $this->autosave_rest_controller = new $class( $this->name );
 968          }
 969  
 970          if ( ! ( $this->autosave_rest_controller instanceof $class ) ) {
 971              return null;
 972          }
 973  
 974          return $this->autosave_rest_controller;
 975      }
 976  
 977      /**
 978       * Returns the default labels for post types.
 979       *
 980       * @since 6.0.0
 981       *
 982       * @return (string|null)[][] The default labels for post types.
 983       */
 984  	public static function get_default_labels() {
 985          if ( ! empty( self::$default_labels ) ) {
 986              return self::$default_labels;
 987          }
 988  
 989          self::$default_labels = array(
 990              'name'                     => array( _x( 'Posts', 'post type general name' ), _x( 'Pages', 'post type general name' ) ),
 991              'singular_name'            => array( _x( 'Post', 'post type singular name' ), _x( 'Page', 'post type singular name' ) ),
 992              'add_new'                  => array( __( 'Add New' ), __( 'Add New' ) ),
 993              'add_new_item'             => array( __( 'Add New Post' ), __( 'Add New Page' ) ),
 994              'edit_item'                => array( __( 'Edit Post' ), __( 'Edit Page' ) ),
 995              'new_item'                 => array( __( 'New Post' ), __( 'New Page' ) ),
 996              'view_item'                => array( __( 'View Post' ), __( 'View Page' ) ),
 997              'view_items'               => array( __( 'View Posts' ), __( 'View Pages' ) ),
 998              'search_items'             => array( __( 'Search Posts' ), __( 'Search Pages' ) ),
 999              'not_found'                => array( __( 'No posts found.' ), __( 'No pages found.' ) ),
1000              'not_found_in_trash'       => array( __( 'No posts found in Trash.' ), __( 'No pages found in Trash.' ) ),
1001              'parent_item_colon'        => array( null, __( 'Parent Page:' ) ),
1002              'all_items'                => array( __( 'All Posts' ), __( 'All Pages' ) ),
1003              'archives'                 => array( __( 'Post Archives' ), __( 'Page Archives' ) ),
1004              'attributes'               => array( __( 'Post Attributes' ), __( 'Page Attributes' ) ),
1005              'insert_into_item'         => array( __( 'Insert into post' ), __( 'Insert into page' ) ),
1006              'uploaded_to_this_item'    => array( __( 'Uploaded to this post' ), __( 'Uploaded to this page' ) ),
1007              'featured_image'           => array( _x( 'Featured image', 'post' ), _x( 'Featured image', 'page' ) ),
1008              'set_featured_image'       => array( _x( 'Set featured image', 'post' ), _x( 'Set featured image', 'page' ) ),
1009              'remove_featured_image'    => array( _x( 'Remove featured image', 'post' ), _x( 'Remove featured image', 'page' ) ),
1010              'use_featured_image'       => array( _x( 'Use as featured image', 'post' ), _x( 'Use as featured image', 'page' ) ),
1011              'filter_items_list'        => array( __( 'Filter posts list' ), __( 'Filter pages list' ) ),
1012              'filter_by_date'           => array( __( 'Filter by date' ), __( 'Filter by date' ) ),
1013              'items_list_navigation'    => array( __( 'Posts list navigation' ), __( 'Pages list navigation' ) ),
1014              'items_list'               => array( __( 'Posts list' ), __( 'Pages list' ) ),
1015              'item_published'           => array( __( 'Post published.' ), __( 'Page published.' ) ),
1016              'item_published_privately' => array( __( 'Post published privately.' ), __( 'Page published privately.' ) ),
1017              'item_reverted_to_draft'   => array( __( 'Post reverted to draft.' ), __( 'Page reverted to draft.' ) ),
1018              'item_trashed'             => array( __( 'Post trashed.' ), __( 'Page trashed.' ) ),
1019              'item_scheduled'           => array( __( 'Post scheduled.' ), __( 'Page scheduled.' ) ),
1020              'item_updated'             => array( __( 'Post updated.' ), __( 'Page updated.' ) ),
1021              'item_link'                => array(
1022                  _x( 'Post Link', 'navigation link block title' ),
1023                  _x( 'Page Link', 'navigation link block title' ),
1024              ),
1025              'item_link_description'    => array(
1026                  _x( 'A link to a post.', 'navigation link block description' ),
1027                  _x( 'A link to a page.', 'navigation link block description' ),
1028              ),
1029          );
1030  
1031          return self::$default_labels;
1032      }
1033  
1034      /**
1035       * Resets the cache for the default labels.
1036       *
1037       * @since 6.0.0
1038       */
1039  	public static function reset_default_labels() {
1040          self::$default_labels = array();
1041      }
1042  }


Generated : Sat Jan 25 08:20:01 2025 Cross-referenced by PHPXref