wp/wp-includes/class-wp-post-type.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    12  *
    12  *
    13  * @since 4.6.0
    13  * @since 4.6.0
    14  *
    14  *
    15  * @see register_post_type()
    15  * @see register_post_type()
    16  */
    16  */
       
    17 #[AllowDynamicProperties]
    17 final class WP_Post_Type {
    18 final class WP_Post_Type {
    18 	/**
    19 	/**
    19 	 * Post type key.
    20 	 * Post type key.
    20 	 *
    21 	 *
    21 	 * @since 4.6.0
    22 	 * @since 4.6.0
   219 	 * Taxonomies can be registered later with `register_taxonomy()` or `register_taxonomy_for_object_type()`.
   220 	 * Taxonomies can be registered later with `register_taxonomy()` or `register_taxonomy_for_object_type()`.
   220 	 *
   221 	 *
   221 	 * Default empty array.
   222 	 * Default empty array.
   222 	 *
   223 	 *
   223 	 * @since 4.6.0
   224 	 * @since 4.6.0
   224 	 * @var array $taxonomies
   225 	 * @var string[] $taxonomies
   225 	 */
   226 	 */
   226 	public $taxonomies = array();
   227 	public $taxonomies = array();
   227 
   228 
   228 	/**
   229 	/**
   229 	 * Whether there should be post type archives, or if a string, the archive slug to use.
   230 	 * Whether there should be post type archives, or if a string, the archive slug to use.
   279 	 * Default empty array.
   280 	 * Default empty array.
   280 	 *
   281 	 *
   281 	 * @link https://developer.wordpress.org/block-editor/developers/block-api/block-templates/
   282 	 * @link https://developer.wordpress.org/block-editor/developers/block-api/block-templates/
   282 	 *
   283 	 *
   283 	 * @since 5.0.0
   284 	 * @since 5.0.0
   284 	 * @var array $template
   285 	 * @var array[] $template
   285 	 */
   286 	 */
   286 	public $template = array();
   287 	public $template = array();
   287 
   288 
   288 	/**
   289 	/**
   289 	 * Whether the block template should be locked if $template is set.
   290 	 * Whether the block template should be locked if $template is set.
   394 	 * @var WP_REST_Controller $rest_controller
   395 	 * @var WP_REST_Controller $rest_controller
   395 	 */
   396 	 */
   396 	public $rest_controller;
   397 	public $rest_controller;
   397 
   398 
   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 	/**
   399 	 * Constructor.
   448 	 * Constructor.
   400 	 *
   449 	 *
   401 	 * See the register_post_type() function for accepted arguments for `$args`.
   450 	 * See the register_post_type() function for accepted arguments for `$args`.
   402 	 *
   451 	 *
   403 	 * Will populate object properties from the provided arguments and assign other
   452 	 * Will populate object properties from the provided arguments and assign other
   407 	 *
   456 	 *
   408 	 * @see register_post_type()
   457 	 * @see register_post_type()
   409 	 *
   458 	 *
   410 	 * @param string       $post_type Post type key.
   459 	 * @param string       $post_type Post type key.
   411 	 * @param array|string $args      Optional. Array or string of arguments for registering a post type.
   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.
   412 	 *                                Default empty array.
   462 	 *                                Default empty array.
   413 	 */
   463 	 */
   414 	public function __construct( $post_type, $args = array() ) {
   464 	public function __construct( $post_type, $args = array() ) {
   415 		$this->name = $post_type;
   465 		$this->name = $post_type;
   416 
   466 
   451 		 *
   501 		 *
   452 		 *  - `register_post_post_type_args`
   502 		 *  - `register_post_post_type_args`
   453 		 *  - `register_page_post_type_args`
   503 		 *  - `register_page_post_type_args`
   454 		 *
   504 		 *
   455 		 * @since 6.0.0
   505 		 * @since 6.0.0
       
   506 		 * @since 6.4.0 Added `late_route_registration`, `autosave_rest_controller_class` and `revisions_rest_controller_class` arguments.
   456 		 *
   507 		 *
   457 		 * @param array  $args      Array of arguments for registering a post type.
   508 		 * @param array  $args      Array of arguments for registering a post type.
   458 		 *                          See the register_post_type() function for accepted arguments.
   509 		 *                          See the register_post_type() function for accepted arguments.
   459 		 * @param string $post_type Post type key.
   510 		 * @param string $post_type Post type key.
   460 		 */
   511 		 */
   462 
   513 
   463 		$has_edit_link = ! empty( $args['_edit_link'] );
   514 		$has_edit_link = ! empty( $args['_edit_link'] );
   464 
   515 
   465 		// Args prefixed with an underscore are reserved for internal use.
   516 		// Args prefixed with an underscore are reserved for internal use.
   466 		$defaults = array(
   517 		$defaults = array(
   467 			'labels'                => array(),
   518 			'labels'                          => array(),
   468 			'description'           => '',
   519 			'description'                     => '',
   469 			'public'                => false,
   520 			'public'                          => false,
   470 			'hierarchical'          => false,
   521 			'hierarchical'                    => false,
   471 			'exclude_from_search'   => null,
   522 			'exclude_from_search'             => null,
   472 			'publicly_queryable'    => null,
   523 			'publicly_queryable'              => null,
   473 			'show_ui'               => null,
   524 			'show_ui'                         => null,
   474 			'show_in_menu'          => null,
   525 			'show_in_menu'                    => null,
   475 			'show_in_nav_menus'     => null,
   526 			'show_in_nav_menus'               => null,
   476 			'show_in_admin_bar'     => null,
   527 			'show_in_admin_bar'               => null,
   477 			'menu_position'         => null,
   528 			'menu_position'                   => null,
   478 			'menu_icon'             => null,
   529 			'menu_icon'                       => null,
   479 			'capability_type'       => 'post',
   530 			'capability_type'                 => 'post',
   480 			'capabilities'          => array(),
   531 			'capabilities'                    => array(),
   481 			'map_meta_cap'          => null,
   532 			'map_meta_cap'                    => null,
   482 			'supports'              => array(),
   533 			'supports'                        => array(),
   483 			'register_meta_box_cb'  => null,
   534 			'register_meta_box_cb'            => null,
   484 			'taxonomies'            => array(),
   535 			'taxonomies'                      => array(),
   485 			'has_archive'           => false,
   536 			'has_archive'                     => false,
   486 			'rewrite'               => true,
   537 			'rewrite'                         => true,
   487 			'query_var'             => true,
   538 			'query_var'                       => true,
   488 			'can_export'            => true,
   539 			'can_export'                      => true,
   489 			'delete_with_user'      => null,
   540 			'delete_with_user'                => null,
   490 			'show_in_rest'          => false,
   541 			'show_in_rest'                    => false,
   491 			'rest_base'             => false,
   542 			'rest_base'                       => false,
   492 			'rest_namespace'        => false,
   543 			'rest_namespace'                  => false,
   493 			'rest_controller_class' => false,
   544 			'rest_controller_class'           => false,
   494 			'template'              => array(),
   545 			'autosave_rest_controller_class'  => false,
   495 			'template_lock'         => false,
   546 			'revisions_rest_controller_class' => false,
   496 			'_builtin'              => false,
   547 			'late_route_registration'         => false,
   497 			'_edit_link'            => 'post.php?post=%d',
   548 			'template'                        => array(),
       
   549 			'template_lock'                   => false,
       
   550 			'_builtin'                        => false,
       
   551 			'_edit_link'                      => 'post.php?post=%d',
   498 		);
   552 		);
   499 
   553 
   500 		$args = array_merge( $defaults, $args );
   554 		$args = array_merge( $defaults, $args );
   501 
   555 
   502 		$args['name'] = $this->name;
   556 		$args['name'] = $this->name;
   614 				} else {
   668 				} else {
   615 					add_post_type_support( $this->name, $args );
   669 					add_post_type_support( $this->name, $args );
   616 				}
   670 				}
   617 			}
   671 			}
   618 			unset( $this->supports );
   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 			}
   619 		} elseif ( false !== $this->supports ) {
   684 		} elseif ( false !== $this->supports ) {
   620 			// Add default features.
   685 			// Add default features.
   621 			add_post_type_support( $this->name, array( 'title', 'editor' ) );
   686 			add_post_type_support( $this->name, array( 'title', 'editor', 'autosave' ) );
   622 		}
   687 		}
   623 	}
   688 	}
   624 
   689 
   625 	/**
   690 	/**
   626 	 * Adds the necessary rewrite rules for the post type.
   691 	 * Adds the necessary rewrite rules for the post type.
   733 		// Remove any rewrite rules, permastructs, and rules.
   798 		// Remove any rewrite rules, permastructs, and rules.
   734 		if ( false !== $this->rewrite ) {
   799 		if ( false !== $this->rewrite ) {
   735 			remove_rewrite_tag( "%$this->name%" );
   800 			remove_rewrite_tag( "%$this->name%" );
   736 			remove_permastruct( $this->name );
   801 			remove_permastruct( $this->name );
   737 			foreach ( $wp_rewrite->extra_rules_top as $regex => $query ) {
   802 			foreach ( $wp_rewrite->extra_rules_top as $regex => $query ) {
   738 				if ( false !== strpos( $query, "index.php?post_type=$this->name" ) ) {
   803 				if ( str_contains( $query, "index.php?post_type=$this->name" ) ) {
   739 					unset( $wp_rewrite->extra_rules_top[ $regex ] );
   804 					unset( $wp_rewrite->extra_rules_top[ $regex ] );
   740 				}
   805 				}
   741 			}
   806 			}
   742 		}
   807 		}
   743 
   808 
   813 
   878 
   814 		return $this->rest_controller;
   879 		return $this->rest_controller;
   815 	}
   880 	}
   816 
   881 
   817 	/**
   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 	/**
   818 	 * Returns the default labels for post types.
   962 	 * Returns the default labels for post types.
   819 	 *
   963 	 *
   820 	 * @since 6.0.0
   964 	 * @since 6.0.0
   821 	 *
   965 	 *
   822 	 * @return (string|null)[][] The default labels for post types.
   966 	 * @return (string|null)[][] The default labels for post types.
   827 		}
   971 		}
   828 
   972 
   829 		self::$default_labels = array(
   973 		self::$default_labels = array(
   830 			'name'                     => array( _x( 'Posts', 'post type general name' ), _x( 'Pages', 'post type general name' ) ),
   974 			'name'                     => array( _x( 'Posts', 'post type general name' ), _x( 'Pages', 'post type general name' ) ),
   831 			'singular_name'            => array( _x( 'Post', 'post type singular name' ), _x( 'Page', 'post type singular name' ) ),
   975 			'singular_name'            => array( _x( 'Post', 'post type singular name' ), _x( 'Page', 'post type singular name' ) ),
   832 			'add_new'                  => array( _x( 'Add New', 'post' ), _x( 'Add New', 'page' ) ),
   976 			'add_new'                  => array( __( 'Add New Post' ), __( 'Add New Page' ) ),
   833 			'add_new_item'             => array( __( 'Add New Post' ), __( 'Add New Page' ) ),
   977 			'add_new_item'             => array( __( 'Add New Post' ), __( 'Add New Page' ) ),
   834 			'edit_item'                => array( __( 'Edit Post' ), __( 'Edit Page' ) ),
   978 			'edit_item'                => array( __( 'Edit Post' ), __( 'Edit Page' ) ),
   835 			'new_item'                 => array( __( 'New Post' ), __( 'New Page' ) ),
   979 			'new_item'                 => array( __( 'New Post' ), __( 'New Page' ) ),
   836 			'view_item'                => array( __( 'View Post' ), __( 'View Page' ) ),
   980 			'view_item'                => array( __( 'View Post' ), __( 'View Page' ) ),
   837 			'view_items'               => array( __( 'View Posts' ), __( 'View Pages' ) ),
   981 			'view_items'               => array( __( 'View Posts' ), __( 'View Pages' ) ),
   853 			'items_list_navigation'    => array( __( 'Posts list navigation' ), __( 'Pages list navigation' ) ),
   997 			'items_list_navigation'    => array( __( 'Posts list navigation' ), __( 'Pages list navigation' ) ),
   854 			'items_list'               => array( __( 'Posts list' ), __( 'Pages list' ) ),
   998 			'items_list'               => array( __( 'Posts list' ), __( 'Pages list' ) ),
   855 			'item_published'           => array( __( 'Post published.' ), __( 'Page published.' ) ),
   999 			'item_published'           => array( __( 'Post published.' ), __( 'Page published.' ) ),
   856 			'item_published_privately' => array( __( 'Post published privately.' ), __( 'Page published privately.' ) ),
  1000 			'item_published_privately' => array( __( 'Post published privately.' ), __( 'Page published privately.' ) ),
   857 			'item_reverted_to_draft'   => array( __( 'Post reverted to draft.' ), __( 'Page reverted to draft.' ) ),
  1001 			'item_reverted_to_draft'   => array( __( 'Post reverted to draft.' ), __( 'Page reverted to draft.' ) ),
       
  1002 			'item_trashed'             => array( __( 'Post trashed.' ), __( 'Page trashed.' ) ),
   858 			'item_scheduled'           => array( __( 'Post scheduled.' ), __( 'Page scheduled.' ) ),
  1003 			'item_scheduled'           => array( __( 'Post scheduled.' ), __( 'Page scheduled.' ) ),
   859 			'item_updated'             => array( __( 'Post updated.' ), __( 'Page updated.' ) ),
  1004 			'item_updated'             => array( __( 'Post updated.' ), __( 'Page updated.' ) ),
   860 			'item_link'                => array(
  1005 			'item_link'                => array(
   861 				_x( 'Post Link', 'navigation link block title' ),
  1006 				_x( 'Post Link', 'navigation link block title' ),
   862 				_x( 'Page Link', 'navigation link block title' ),
  1007 				_x( 'Page Link', 'navigation link block title' ),