wp/wp-includes/class-wp-post-type.php
changeset 16 a86126ab1dd4
parent 7 cf61fcea0001
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
   198 	 * Provide a callback function that sets up the meta boxes for the edit form.
   198 	 * Provide a callback function that sets up the meta boxes for the edit form.
   199 	 *
   199 	 *
   200 	 * Do `remove_meta_box()` and `add_meta_box()` calls in the callback. Default null.
   200 	 * Do `remove_meta_box()` and `add_meta_box()` calls in the callback. Default null.
   201 	 *
   201 	 *
   202 	 * @since 4.6.0
   202 	 * @since 4.6.0
   203 	 * @var string $register_meta_box_cb
   203 	 * @var callable $register_meta_box_cb
   204 	 */
   204 	 */
   205 	public $register_meta_box_cb = null;
   205 	public $register_meta_box_cb = null;
   206 
   206 
   207 	/**
   207 	/**
   208 	 * An array of taxonomy identifiers that will be registered for the post type.
   208 	 * An array of taxonomy identifiers that will be registered for the post type.
   248 	public $can_export = true;
   248 	public $can_export = true;
   249 
   249 
   250 	/**
   250 	/**
   251 	 * Whether to delete posts of this type when deleting a user.
   251 	 * Whether to delete posts of this type when deleting a user.
   252 	 *
   252 	 *
   253 	 * If true, posts of this type belonging to the user will be moved to trash when then user is deleted.
   253 	 * If true, posts of this type belonging to the user will be moved to Trash when then user is deleted.
   254 	 * If false, posts of this type belonging to the user will *not* be trashed or deleted.
   254 	 * If false, posts of this type belonging to the user will *not* be trashed or deleted.
   255 	 * If not set (the default), posts are trashed if post_type_supports( 'author' ).
   255 	 * If not set (the default), posts are trashed if post_type_supports( 'author' ).
   256 	 * Otherwise posts are not trashed or deleted. Default null.
   256 	 * Otherwise posts are not trashed or deleted. Default null.
   257 	 *
   257 	 *
   258 	 * @since 4.6.0
   258 	 * @since 4.6.0
   332 	 *
   332 	 *
   333 	 * @since 4.7.4
   333 	 * @since 4.7.4
   334 	 * @var string|bool $rest_controller_class
   334 	 * @var string|bool $rest_controller_class
   335 	 */
   335 	 */
   336 	public $rest_controller_class;
   336 	public $rest_controller_class;
       
   337 
       
   338 	/**
       
   339 	 * The controller instance for this post type's REST API endpoints.
       
   340 	 *
       
   341 	 * Lazily computed. Should be accessed using {@see WP_Post_Type::get_rest_controller()}.
       
   342 	 *
       
   343 	 * @since 5.3.0
       
   344 	 * @var WP_REST_Controller $rest_controller
       
   345 	 */
       
   346 	public $rest_controller;
   337 
   347 
   338 	/**
   348 	/**
   339 	 * Constructor.
   349 	 * Constructor.
   340 	 *
   350 	 *
   341 	 * Will populate object properties from the provided arguments and assign other
   351 	 * Will populate object properties from the provided arguments and assign other
   411 
   421 
   412 		$args = array_merge( $defaults, $args );
   422 		$args = array_merge( $defaults, $args );
   413 
   423 
   414 		$args['name'] = $this->name;
   424 		$args['name'] = $this->name;
   415 
   425 
   416 		// If not set, default to the setting for public.
   426 		// If not set, default to the setting for 'public'.
   417 		if ( null === $args['publicly_queryable'] ) {
   427 		if ( null === $args['publicly_queryable'] ) {
   418 			$args['publicly_queryable'] = $args['public'];
   428 			$args['publicly_queryable'] = $args['public'];
   419 		}
   429 		}
   420 
   430 
   421 		// If not set, default to the setting for public.
   431 		// If not set, default to the setting for 'public'.
   422 		if ( null === $args['show_ui'] ) {
   432 		if ( null === $args['show_ui'] ) {
   423 			$args['show_ui'] = $args['public'];
   433 			$args['show_ui'] = $args['public'];
   424 		}
   434 		}
   425 
   435 
   426 		// If not set, default to the setting for show_ui.
   436 		// If not set, default to the setting for 'show_ui'.
   427 		if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) {
   437 		if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) {
   428 			$args['show_in_menu'] = $args['show_ui'];
   438 			$args['show_in_menu'] = $args['show_ui'];
   429 		}
   439 		}
   430 
   440 
   431 		// If not set, default to the whether the full UI is shown.
   441 		// If not set, default to the setting for 'show_in_menu'.
   432 		if ( null === $args['show_in_admin_bar'] ) {
   442 		if ( null === $args['show_in_admin_bar'] ) {
   433 			$args['show_in_admin_bar'] = (bool) $args['show_in_menu'];
   443 			$args['show_in_admin_bar'] = (bool) $args['show_in_menu'];
   434 		}
   444 		}
   435 
   445 
   436 		// If not set, default to the setting for public.
   446 		// If not set, default to the setting for 'public'.
   437 		if ( null === $args['show_in_nav_menus'] ) {
   447 		if ( null === $args['show_in_nav_menus'] ) {
   438 			$args['show_in_nav_menus'] = $args['public'];
   448 			$args['show_in_nav_menus'] = $args['public'];
   439 		}
   449 		}
   440 
   450 
   441 		// If not set, default to true if not public, false if public.
   451 		// If not set, default to true if not public, false if public.
   442 		if ( null === $args['exclude_from_search'] ) {
   452 		if ( null === $args['exclude_from_search'] ) {
   443 			$args['exclude_from_search'] = ! $args['public'];
   453 			$args['exclude_from_search'] = ! $args['public'];
   444 		}
   454 		}
   445 
   455 
   446 		// Back compat with quirky handling in version 3.0. #14122.
   456 		// Back compat with quirky handling in version 3.0. #14122.
   447 		if ( empty( $args['capabilities'] ) && null === $args['map_meta_cap'] && in_array( $args['capability_type'], array( 'post', 'page' ) ) ) {
   457 		if ( empty( $args['capabilities'] )
       
   458 			&& null === $args['map_meta_cap'] && in_array( $args['capability_type'], array( 'post', 'page' ), true )
       
   459 		) {
   448 			$args['map_meta_cap'] = true;
   460 			$args['map_meta_cap'] = true;
   449 		}
   461 		}
   450 
   462 
   451 		// If not set, default to false.
   463 		// If not set, default to false.
   452 		if ( null === $args['map_meta_cap'] ) {
   464 		if ( null === $args['map_meta_cap'] ) {
   471 			} else {
   483 			} else {
   472 				$args['query_var'] = sanitize_title_with_dashes( $args['query_var'] );
   484 				$args['query_var'] = sanitize_title_with_dashes( $args['query_var'] );
   473 			}
   485 			}
   474 		}
   486 		}
   475 
   487 
   476 		if ( false !== $args['rewrite'] && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
   488 		if ( false !== $args['rewrite'] && ( is_admin() || get_option( 'permalink_structure' ) ) ) {
   477 			if ( ! is_array( $args['rewrite'] ) ) {
   489 			if ( ! is_array( $args['rewrite'] ) ) {
   478 				$args['rewrite'] = array();
   490 				$args['rewrite'] = array();
   479 			}
   491 			}
   480 			if ( empty( $args['rewrite']['slug'] ) ) {
   492 			if ( empty( $args['rewrite']['slug'] ) ) {
   481 				$args['rewrite']['slug'] = $this->name;
   493 				$args['rewrite']['slug'] = $this->name;
   511 	 *
   523 	 *
   512 	 * @since 4.6.0
   524 	 * @since 4.6.0
   513 	 */
   525 	 */
   514 	public function add_supports() {
   526 	public function add_supports() {
   515 		if ( ! empty( $this->supports ) ) {
   527 		if ( ! empty( $this->supports ) ) {
   516 			add_post_type_support( $this->name, $this->supports );
   528 			foreach ( $this->supports as $feature => $args ) {
       
   529 				if ( is_array( $args ) ) {
       
   530 					add_post_type_support( $this->name, $feature, $args );
       
   531 				} else {
       
   532 					add_post_type_support( $this->name, $args );
       
   533 				}
       
   534 			}
   517 			unset( $this->supports );
   535 			unset( $this->supports );
   518 		} elseif ( false !== $this->supports ) {
   536 		} elseif ( false !== $this->supports ) {
   519 			// Add default features.
   537 			// Add default features.
   520 			add_post_type_support( $this->name, array( 'title', 'editor' ) );
   538 			add_post_type_support( $this->name, array( 'title', 'editor' ) );
   521 		}
   539 		}
   524 	/**
   542 	/**
   525 	 * Adds the necessary rewrite rules for the post type.
   543 	 * Adds the necessary rewrite rules for the post type.
   526 	 *
   544 	 *
   527 	 * @since 4.6.0
   545 	 * @since 4.6.0
   528 	 *
   546 	 *
   529 	 * @global WP_Rewrite $wp_rewrite WordPress Rewrite Component.
   547 	 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
   530 	 * @global WP         $wp         Current WordPress environment instance.
   548 	 * @global WP         $wp         Current WordPress environment instance.
   531 	 */
   549 	 */
   532 	public function add_rewrite_rules() {
   550 	public function add_rewrite_rules() {
   533 		global $wp_rewrite, $wp;
   551 		global $wp_rewrite, $wp;
   534 
   552 
   535 		if ( false !== $this->query_var && $wp && is_post_type_viewable( $this ) ) {
   553 		if ( false !== $this->query_var && $wp && is_post_type_viewable( $this ) ) {
   536 			$wp->add_query_var( $this->query_var );
   554 			$wp->add_query_var( $this->query_var );
   537 		}
   555 		}
   538 
   556 
   539 		if ( false !== $this->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
   557 		if ( false !== $this->rewrite && ( is_admin() || get_option( 'permalink_structure' ) ) ) {
   540 			if ( $this->hierarchical ) {
   558 			if ( $this->hierarchical ) {
   541 				add_rewrite_tag( "%$this->name%", '(.+?)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&pagename=" );
   559 				add_rewrite_tag( "%$this->name%", '(.+?)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&pagename=" );
   542 			} else {
   560 			} else {
   543 				add_rewrite_tag( "%$this->name%", '([^/]+)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&name=" );
   561 				add_rewrite_tag( "%$this->name%", '([^/]+)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&name=" );
   544 			}
   562 			}
   674 	 * @since 4.6.0
   692 	 * @since 4.6.0
   675 	 */
   693 	 */
   676 	public function remove_hooks() {
   694 	public function remove_hooks() {
   677 		remove_action( 'future_' . $this->name, '_future_post_hook', 5 );
   695 		remove_action( 'future_' . $this->name, '_future_post_hook', 5 );
   678 	}
   696 	}
       
   697 
       
   698 	/**
       
   699 	 * Gets the REST API controller for this post type.
       
   700 	 *
       
   701 	 * Will only instantiate the controller class once per request.
       
   702 	 *
       
   703 	 * @since 5.3.0
       
   704 	 *
       
   705 	 * @return WP_REST_Controller|null The controller instance, or null if the post type
       
   706 	 *                                 is set not to show in rest.
       
   707 	 */
       
   708 	public function get_rest_controller() {
       
   709 		if ( ! $this->show_in_rest ) {
       
   710 			return null;
       
   711 		}
       
   712 
       
   713 		$class = $this->rest_controller_class ? $this->rest_controller_class : WP_REST_Posts_Controller::class;
       
   714 
       
   715 		if ( ! class_exists( $class ) ) {
       
   716 			return null;
       
   717 		}
       
   718 
       
   719 		if ( ! is_subclass_of( $class, WP_REST_Controller::class ) ) {
       
   720 			return null;
       
   721 		}
       
   722 
       
   723 		if ( ! $this->rest_controller ) {
       
   724 			$this->rest_controller = new $class( $this->name );
       
   725 		}
       
   726 
       
   727 		if ( ! ( $this->rest_controller instanceof $class ) ) {
       
   728 			return null;
       
   729 		}
       
   730 
       
   731 		return $this->rest_controller;
       
   732 	}
   679 }
   733 }