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


Generated : Thu Mar 28 08:20:01 2024 Cross-referenced by PHPXref