wp/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    38 	 * @var int[]
    38 	 * @var int[]
    39 	 */
    39 	 */
    40 	protected $password_check_passed = array();
    40 	protected $password_check_passed = array();
    41 
    41 
    42 	/**
    42 	/**
       
    43 	 * Whether the controller supports batching.
       
    44 	 *
       
    45 	 * @since 5.9.0
       
    46 	 * @var array
       
    47 	 */
       
    48 	protected $allow_batch = array( 'v1' => true );
       
    49 
       
    50 	/**
    43 	 * Constructor.
    51 	 * Constructor.
    44 	 *
    52 	 *
    45 	 * @since 4.7.0
    53 	 * @since 4.7.0
    46 	 *
    54 	 *
    47 	 * @param string $post_type Post type.
    55 	 * @param string $post_type Post type.
    48 	 */
    56 	 */
    49 	public function __construct( $post_type ) {
    57 	public function __construct( $post_type ) {
    50 		$this->post_type = $post_type;
    58 		$this->post_type = $post_type;
    51 		$this->namespace = 'wp/v2';
       
    52 		$obj             = get_post_type_object( $post_type );
    59 		$obj             = get_post_type_object( $post_type );
    53 		$this->rest_base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name;
    60 		$this->rest_base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name;
       
    61 		$this->namespace = ! empty( $obj->rest_namespace ) ? $obj->rest_namespace : 'wp/v2';
    54 
    62 
    55 		$this->meta = new WP_REST_Post_Meta_Fields( $this->post_type );
    63 		$this->meta = new WP_REST_Post_Meta_Fields( $this->post_type );
    56 	}
    64 	}
    57 
    65 
    58 	/**
    66 	/**
    78 					'methods'             => WP_REST_Server::CREATABLE,
    86 					'methods'             => WP_REST_Server::CREATABLE,
    79 					'callback'            => array( $this, 'create_item' ),
    87 					'callback'            => array( $this, 'create_item' ),
    80 					'permission_callback' => array( $this, 'create_item_permissions_check' ),
    88 					'permission_callback' => array( $this, 'create_item_permissions_check' ),
    81 					'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
    89 					'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
    82 				),
    90 				),
    83 				'schema' => array( $this, 'get_public_item_schema' ),
    91 				'allow_batch' => $this->allow_batch,
       
    92 				'schema'      => array( $this, 'get_public_item_schema' ),
    84 			)
    93 			)
    85 		);
    94 		);
    86 
    95 
    87 		$schema        = $this->get_item_schema();
    96 		$schema        = $this->get_item_schema();
    88 		$get_item_args = array(
    97 		$get_item_args = array(
    96 		}
   105 		}
    97 		register_rest_route(
   106 		register_rest_route(
    98 			$this->namespace,
   107 			$this->namespace,
    99 			'/' . $this->rest_base . '/(?P<id>[\d]+)',
   108 			'/' . $this->rest_base . '/(?P<id>[\d]+)',
   100 			array(
   109 			array(
   101 				'args'   => array(
   110 				'args'        => array(
   102 					'id' => array(
   111 					'id' => array(
   103 						'description' => __( 'Unique identifier for the post.' ),
   112 						'description' => __( 'Unique identifier for the post.' ),
   104 						'type'        => 'integer',
   113 						'type'        => 'integer',
   105 					),
   114 					),
   106 				),
   115 				),
   126 							'default'     => false,
   135 							'default'     => false,
   127 							'description' => __( 'Whether to bypass Trash and force deletion.' ),
   136 							'description' => __( 'Whether to bypass Trash and force deletion.' ),
   128 						),
   137 						),
   129 					),
   138 					),
   130 				),
   139 				),
   131 				'schema' => array( $this, 'get_public_item_schema' ),
   140 				'allow_batch' => $this->allow_batch,
       
   141 				'schema'      => array( $this, 'get_public_item_schema' ),
   132 			)
   142 			)
   133 		);
   143 		);
   134 	}
   144 	}
   135 
   145 
   136 	/**
   146 	/**
  1021 
  1031 
  1022 		/**
  1032 		/**
  1023 		 * Fires immediately after a single post is deleted or trashed via the REST API.
  1033 		 * Fires immediately after a single post is deleted or trashed via the REST API.
  1024 		 *
  1034 		 *
  1025 		 * They dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
  1035 		 * They dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
       
  1036 		 *
       
  1037 		 * Possible hook names include:
       
  1038 		 *
       
  1039 		 *  - `rest_delete_post`
       
  1040 		 *  - `rest_delete_page`
       
  1041 		 *  - `rest_delete_attachment`
  1026 		 *
  1042 		 *
  1027 		 * @since 4.7.0
  1043 		 * @since 4.7.0
  1028 		 *
  1044 		 *
  1029 		 * @param WP_Post          $post     The deleted or trashed post.
  1045 		 * @param WP_Post          $post     The deleted or trashed post.
  1030 		 * @param WP_REST_Response $response The response data.
  1046 		 * @param WP_REST_Response $response The response data.
  1533 
  1549 
  1534 			if ( ! isset( $request[ $base ] ) ) {
  1550 			if ( ! isset( $request[ $base ] ) ) {
  1535 				continue;
  1551 				continue;
  1536 			}
  1552 			}
  1537 
  1553 
  1538 			foreach ( $request[ $base ] as $term_id ) {
  1554 			foreach ( (array) $request[ $base ] as $term_id ) {
  1539 				// Invalid terms will be rejected later.
  1555 				// Invalid terms will be rejected later.
  1540 				if ( ! get_term( $term_id, $taxonomy->name ) ) {
  1556 				if ( ! get_term( $term_id, $taxonomy->name ) ) {
  1541 					continue;
  1557 					continue;
  1542 				}
  1558 				}
  1543 
  1559 
  1671 
  1687 
  1672 	/**
  1688 	/**
  1673 	 * Prepares a single post output for response.
  1689 	 * Prepares a single post output for response.
  1674 	 *
  1690 	 *
  1675 	 * @since 4.7.0
  1691 	 * @since 4.7.0
  1676 	 *
  1692 	 * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support.
  1677 	 * @param WP_Post         $post    Post object.
  1693 	 *
       
  1694 	 * @param WP_Post         $item    Post object.
  1678 	 * @param WP_REST_Request $request Request object.
  1695 	 * @param WP_REST_Request $request Request object.
  1679 	 * @return WP_REST_Response Response object.
  1696 	 * @return WP_REST_Response Response object.
  1680 	 */
  1697 	 */
  1681 	public function prepare_item_for_response( $post, $request ) {
  1698 	public function prepare_item_for_response( $item, $request ) {
       
  1699 		// Restores the more descriptive, specific name for use within this method.
       
  1700 		$post            = $item;
  1682 		$GLOBALS['post'] = $post;
  1701 		$GLOBALS['post'] = $post;
  1683 
  1702 
  1684 		setup_postdata( $post );
  1703 		setup_postdata( $post );
  1685 
  1704 
  1686 		$fields = $this->get_fields_for_response( $request );
  1705 		$fields = $this->get_fields_for_response( $request );
  2018 
  2037 
  2019 		$post_type_obj = get_post_type_object( $post->post_type );
  2038 		$post_type_obj = get_post_type_object( $post->post_type );
  2020 
  2039 
  2021 		if ( $post_type_obj->hierarchical && ! empty( $post->post_parent ) ) {
  2040 		if ( $post_type_obj->hierarchical && ! empty( $post->post_parent ) ) {
  2022 			$links['up'] = array(
  2041 			$links['up'] = array(
  2023 				'href'       => rest_url( trailingslashit( $base ) . (int) $post->post_parent ),
  2042 				'href'       => rest_url( rest_get_route_for_post( $post->post_parent ) ),
  2024 				'embeddable' => true,
  2043 				'embeddable' => true,
  2025 			);
  2044 			);
  2026 		}
  2045 		}
  2027 
  2046 
  2028 		// If we have a featured media, add that.
  2047 		// If we have a featured media, add that.
  2029 		$featured_media = get_post_thumbnail_id( $post->ID );
  2048 		$featured_media = get_post_thumbnail_id( $post->ID );
  2030 		if ( $featured_media ) {
  2049 		if ( $featured_media ) {
  2031 			$image_url = rest_url( 'wp/v2/media/' . $featured_media );
  2050 			$image_url = rest_url( rest_get_route_for_post( $featured_media ) );
  2032 
  2051 
  2033 			$links['https://api.w.org/featuredmedia'] = array(
  2052 			$links['https://api.w.org/featuredmedia'] = array(
  2034 				'href'       => $image_url,
  2053 				'href'       => $image_url,
  2035 				'embeddable' => true,
  2054 				'embeddable' => true,
  2036 			);
  2055 			);
  2037 		}
  2056 		}
  2038 
  2057 
  2039 		if ( ! in_array( $post->post_type, array( 'attachment', 'nav_menu_item', 'revision' ), true ) ) {
  2058 		if ( ! in_array( $post->post_type, array( 'attachment', 'nav_menu_item', 'revision' ), true ) ) {
  2040 			$attachments_url = rest_url( 'wp/v2/media' );
  2059 			$attachments_url = rest_url( rest_get_route_for_post_type_items( 'attachment' ) );
  2041 			$attachments_url = add_query_arg( 'parent', $post->ID, $attachments_url );
  2060 			$attachments_url = add_query_arg( 'parent', $post->ID, $attachments_url );
  2042 
  2061 
  2043 			$links['https://api.w.org/attachment'] = array(
  2062 			$links['https://api.w.org/attachment'] = array(
  2044 				'href' => $attachments_url,
  2063 				'href' => $attachments_url,
  2045 			);
  2064 			);
  2049 
  2068 
  2050 		if ( ! empty( $taxonomies ) ) {
  2069 		if ( ! empty( $taxonomies ) ) {
  2051 			$links['https://api.w.org/term'] = array();
  2070 			$links['https://api.w.org/term'] = array();
  2052 
  2071 
  2053 			foreach ( $taxonomies as $tax ) {
  2072 			foreach ( $taxonomies as $tax ) {
  2054 				$taxonomy_obj = get_taxonomy( $tax );
  2073 				$taxonomy_route = rest_get_route_for_taxonomy_items( $tax );
  2055 
  2074 
  2056 				// Skip taxonomies that are not public.
  2075 				// Skip taxonomies that are not public.
  2057 				if ( empty( $taxonomy_obj->show_in_rest ) ) {
  2076 				if ( empty( $taxonomy_route ) ) {
  2058 					continue;
  2077 					continue;
  2059 				}
  2078 				}
  2060 
       
  2061 				$tax_base = ! empty( $taxonomy_obj->rest_base ) ? $taxonomy_obj->rest_base : $tax;
       
  2062 
       
  2063 				$terms_url = add_query_arg(
  2079 				$terms_url = add_query_arg(
  2064 					'post',
  2080 					'post',
  2065 					$post->ID,
  2081 					$post->ID,
  2066 					rest_url( 'wp/v2/' . $tax_base )
  2082 					rest_url( $taxonomy_route )
  2067 				);
  2083 				);
  2068 
  2084 
  2069 				$links['https://api.w.org/term'][] = array(
  2085 				$links['https://api.w.org/term'][] = array(
  2070 					'href'       => $terms_url,
  2086 					'href'       => $terms_url,
  2071 					'taxonomy'   => $tax,
  2087 					'taxonomy'   => $tax,
  2532 		 * Filters the post's schema.
  2548 		 * Filters the post's schema.
  2533 		 *
  2549 		 *
  2534 		 * The dynamic portion of the filter, `$this->post_type`, refers to the
  2550 		 * The dynamic portion of the filter, `$this->post_type`, refers to the
  2535 		 * post type slug for the controller.
  2551 		 * post type slug for the controller.
  2536 		 *
  2552 		 *
       
  2553 		 * Possible hook names include:
       
  2554 		 *
       
  2555 		 *  - `rest_post_item_schema`
       
  2556 		 *  - `rest_page_item_schema`
       
  2557 		 *  - `rest_attachment_item_schema`
       
  2558 		 *
  2537 		 * @since 5.4.0
  2559 		 * @since 5.4.0
  2538 		 *
  2560 		 *
  2539 		 * @param array $schema Item schema data.
  2561 		 * @param array $schema Item schema data.
  2540 		 */
  2562 		 */
  2541 		$schema = apply_filters( "rest_{$this->post_type}_item_schema", $schema );
  2563 		$schema = apply_filters( "rest_{$this->post_type}_item_schema", $schema );