wp/wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    15  * @see WP_REST_Posts_Controller
    15  * @see WP_REST_Posts_Controller
    16  */
    16  */
    17 class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
    17 class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
    18 
    18 
    19 	/**
    19 	/**
    20 	 * Get the nav menu item, if the ID is valid.
    20 	 * Gets the nav menu item, if the ID is valid.
    21 	 *
    21 	 *
    22 	 * @since 5.9.0
    22 	 * @since 5.9.0
    23 	 *
    23 	 *
    24 	 * @param int $id Supplied ID.
    24 	 * @param int $id Supplied ID.
    25 	 * @return object|WP_Error Post object if ID is valid, WP_Error otherwise.
    25 	 * @return object|WP_Error Post object if ID is valid, WP_Error otherwise.
    55 	 * Checks if a given request has access to read a menu item if they have access to edit them.
    55 	 * Checks if a given request has access to read a menu item if they have access to edit them.
    56 	 *
    56 	 *
    57 	 * @since 5.9.0
    57 	 * @since 5.9.0
    58 	 *
    58 	 *
    59 	 * @param WP_REST_Request $request Full details about the request.
    59 	 * @param WP_REST_Request $request Full details about the request.
    60 	 * @return bool|WP_Error True if the request has read access for the item, WP_Error object otherwise.
    60 	 * @return bool|WP_Error True if the request has read access for the item, WP_Error object or false otherwise.
    61 	 */
    61 	 */
    62 	public function get_item_permissions_check( $request ) {
    62 	public function get_item_permissions_check( $request ) {
    63 		$permission_check = parent::get_item_permissions_check( $request );
    63 		$permission_check = parent::get_item_permissions_check( $request );
    64 
    64 
    65 		if ( true !== $permission_check ) {
    65 		if ( true !== $permission_check ) {
    75 	 * This allows for any user that can `edit_theme_options` or edit any REST API available post type.
    75 	 * This allows for any user that can `edit_theme_options` or edit any REST API available post type.
    76 	 *
    76 	 *
    77 	 * @since 5.9.0
    77 	 * @since 5.9.0
    78 	 *
    78 	 *
    79 	 * @param WP_REST_Request $request Full details about the request.
    79 	 * @param WP_REST_Request $request Full details about the request.
    80 	 * @return bool|WP_Error Whether the current user has permission.
    80 	 * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise.
    81 	 */
    81 	 */
    82 	protected function check_has_read_only_access( $request ) {
    82 	protected function check_has_read_only_access( $request ) {
    83 		if ( current_user_can( 'edit_theme_options' ) ) {
    83 		if ( current_user_can( 'edit_theme_options' ) ) {
    84 			return true;
    84 			return true;
    85 		}
    85 		}
   105 	 * Creates a single post.
   105 	 * Creates a single post.
   106 	 *
   106 	 *
   107 	 * @since 5.9.0
   107 	 * @since 5.9.0
   108 	 *
   108 	 *
   109 	 * @param WP_REST_Request $request Full details about the request.
   109 	 * @param WP_REST_Request $request Full details about the request.
   110 	 *
       
   111 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
   110 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
   112 	 */
   111 	 */
   113 	public function create_item( $request ) {
   112 	public function create_item( $request ) {
   114 		if ( ! empty( $request['id'] ) ) {
   113 		if ( ! empty( $request['id'] ) ) {
   115 			return new WP_Error( 'rest_post_exists', __( 'Cannot create existing post.' ), array( 'status' => 400 ) );
   114 			return new WP_Error( 'rest_post_exists', __( 'Cannot create existing post.' ), array( 'status' => 400 ) );
   197 	 * Updates a single nav menu item.
   196 	 * Updates a single nav menu item.
   198 	 *
   197 	 *
   199 	 * @since 5.9.0
   198 	 * @since 5.9.0
   200 	 *
   199 	 *
   201 	 * @param WP_REST_Request $request Full details about the request.
   200 	 * @param WP_REST_Request $request Full details about the request.
   202 	 *
       
   203 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
   201 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
   204 	 */
   202 	 */
   205 	public function update_item( $request ) {
   203 	public function update_item( $request ) {
   206 		$valid_check = $this->get_nav_menu_item( $request['id'] );
   204 		$valid_check = $this->get_nav_menu_item( $request['id'] );
   207 		if ( is_wp_error( $valid_check ) ) {
   205 		if ( is_wp_error( $valid_check ) ) {
   486 	/**
   484 	/**
   487 	 * Prepares a single post output for response.
   485 	 * Prepares a single post output for response.
   488 	 *
   486 	 *
   489 	 * @since 5.9.0
   487 	 * @since 5.9.0
   490 	 *
   488 	 *
   491 	 * @param WP_Post          $item   Post object.
   489 	 * @param WP_Post         $item    Post object.
   492 	 * @param WP_REST_Request $request Request object.
   490 	 * @param WP_REST_Request $request Request object.
   493 	 * @return WP_REST_Response Response object.
   491 	 * @return WP_REST_Response Response object.
   494 	 */
   492 	 */
   495 	public function prepare_item_for_response( $item, $request ) {
   493 	public function prepare_item_for_response( $item, $request ) {
   496 		// Base fields for every post.
   494 		// Base fields for every post.
   610 		$data    = $this->filter_response_by_context( $data, $context );
   608 		$data    = $this->filter_response_by_context( $data, $context );
   611 
   609 
   612 		// Wrap the data in a response object.
   610 		// Wrap the data in a response object.
   613 		$response = rest_ensure_response( $data );
   611 		$response = rest_ensure_response( $data );
   614 
   612 
   615 		$links = $this->prepare_links( $item );
   613 		if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
   616 		$response->add_links( $links );
   614 			$links = $this->prepare_links( $item );
   617 
   615 			$response->add_links( $links );
   618 		if ( ! empty( $links['self']['href'] ) ) {
   616 
   619 			$actions = $this->get_available_actions( $item, $request );
   617 			if ( ! empty( $links['self']['href'] ) ) {
   620 
   618 				$actions = $this->get_available_actions( $item, $request );
   621 			$self = $links['self']['href'];
   619 
   622 
   620 				$self = $links['self']['href'];
   623 			foreach ( $actions as $rel ) {
   621 
   624 				$response->add_link( $rel, $self );
   622 				foreach ( $actions as $rel ) {
       
   623 					$response->add_link( $rel, $self );
       
   624 				}
   625 			}
   625 			}
   626 		}
   626 		}
   627 
   627 
   628 		/**
   628 		/**
   629 		 * Filters the menu item data for a REST API response.
   629 		 * Filters the menu item data for a REST API response.
   674 
   674 
   675 		return $links;
   675 		return $links;
   676 	}
   676 	}
   677 
   677 
   678 	/**
   678 	/**
   679 	 * Retrieve Link Description Objects that should be added to the Schema for the posts collection.
   679 	 * Retrieves Link Description Objects that should be added to the Schema for the posts collection.
   680 	 *
   680 	 *
   681 	 * @since 5.9.0
   681 	 * @since 5.9.0
   682 	 *
   682 	 *
   683 	 * @return array
   683 	 * @return array
   684 	 */
   684 	 */
   708 	 * @since 5.9.0
   708 	 * @since 5.9.0
   709 	 *
   709 	 *
   710 	 * @return array Item schema data.
   710 	 * @return array Item schema data.
   711 	 */
   711 	 */
   712 	public function get_item_schema() {
   712 	public function get_item_schema() {
       
   713 		if ( $this->schema ) {
       
   714 			return $this->add_additional_fields_schema( $this->schema );
       
   715 		}
       
   716 
   713 		$schema = array(
   717 		$schema = array(
   714 			'$schema' => 'http://json-schema.org/draft-04/schema#',
   718 			'$schema' => 'http://json-schema.org/draft-04/schema#',
   715 			'title'   => $this->post_type,
   719 			'title'   => $this->post_type,
   716 			'type'    => 'object',
   720 			'type'    => 'object',
   717 		);
   721 		);
   743 			'context'     => array( 'view', 'edit', 'embed' ),
   747 			'context'     => array( 'view', 'edit', 'embed' ),
   744 			'readonly'    => true,
   748 			'readonly'    => true,
   745 		);
   749 		);
   746 
   750 
   747 		$schema['properties']['type_label'] = array(
   751 		$schema['properties']['type_label'] = array(
   748 			'description' => __( 'Name of type.' ),
   752 			'description' => __( 'The singular label used to describe this type of menu item.' ),
   749 			'type'        => 'string',
   753 			'type'        => 'string',
   750 			'context'     => array( 'view', 'edit', 'embed' ),
   754 			'context'     => array( 'view', 'edit', 'embed' ),
   751 			'readonly'    => true,
   755 			'readonly'    => true,
   752 		);
   756 		);
   753 
   757 
   790 			'items'       => array(
   794 			'items'       => array(
   791 				'type' => 'string',
   795 				'type' => 'string',
   792 			),
   796 			),
   793 			'context'     => array( 'view', 'edit', 'embed' ),
   797 			'context'     => array( 'view', 'edit', 'embed' ),
   794 			'arg_options' => array(
   798 			'arg_options' => array(
   795 				'sanitize_callback' => function ( $value ) {
   799 				'sanitize_callback' => static function ( $value ) {
   796 					return array_map( 'sanitize_html_class', wp_parse_list( $value ) );
   800 					return array_map( 'sanitize_html_class', wp_parse_list( $value ) );
   797 				},
   801 				},
   798 			),
   802 			),
   799 		);
   803 		);
   800 
   804 
   838 			'context'     => array( 'view', 'edit', 'embed' ),
   842 			'context'     => array( 'view', 'edit', 'embed' ),
   839 			'enum'        => array(
   843 			'enum'        => array(
   840 				'_blank',
   844 				'_blank',
   841 				'',
   845 				'',
   842 			),
   846 			),
   843 		);
       
   844 
       
   845 		$schema['properties']['type_label'] = array(
       
   846 			'description' => __( 'The singular label used to describe this type of menu item.' ),
       
   847 			'context'     => array( 'view', 'edit', 'embed' ),
       
   848 			'type'        => 'string',
       
   849 			'readonly'    => true,
       
   850 		);
   847 		);
   851 
   848 
   852 		$schema['properties']['url'] = array(
   849 		$schema['properties']['url'] = array(
   853 			'description' => __( 'The URL to which this menu item points.' ),
   850 			'description' => __( 'The URL to which this menu item points.' ),
   854 			'type'        => 'string',
   851 			'type'        => 'string',
   858 				'validate_callback' => static function ( $url ) {
   855 				'validate_callback' => static function ( $url ) {
   859 					if ( '' === $url ) {
   856 					if ( '' === $url ) {
   860 						return true;
   857 						return true;
   861 					}
   858 					}
   862 
   859 
   863 					if ( esc_url_raw( $url ) ) {
   860 					if ( sanitize_url( $url ) ) {
   864 						return true;
   861 						return true;
   865 					}
   862 					}
   866 
   863 
   867 					return new WP_Error(
   864 					return new WP_Error(
   868 						'rest_invalid_url',
   865 						'rest_invalid_url',
   878 			'items'       => array(
   875 			'items'       => array(
   879 				'type' => 'string',
   876 				'type' => 'string',
   880 			),
   877 			),
   881 			'context'     => array( 'view', 'edit', 'embed' ),
   878 			'context'     => array( 'view', 'edit', 'embed' ),
   882 			'arg_options' => array(
   879 			'arg_options' => array(
   883 				'sanitize_callback' => function ( $value ) {
   880 				'sanitize_callback' => static function ( $value ) {
   884 					return array_map( 'sanitize_html_class', wp_parse_list( $value ) );
   881 					return array_map( 'sanitize_html_class', wp_parse_list( $value ) );
   885 				},
   882 				},
   886 			),
   883 			),
   887 		);
   884 		);
   888 
   885 
   919 
   916 
   920 		if ( $schema_links ) {
   917 		if ( $schema_links ) {
   921 			$schema['links'] = $schema_links;
   918 			$schema['links'] = $schema_links;
   922 		}
   919 		}
   923 
   920 
   924 		return $this->add_additional_fields_schema( $schema );
   921 		$this->schema = $schema;
       
   922 
       
   923 		return $this->add_additional_fields_schema( $this->schema );
   925 	}
   924 	}
   926 
   925 
   927 	/**
   926 	/**
   928 	 * Retrieves the query params for the posts collection.
   927 	 * Retrieves the query params for the posts collection.
   929 	 *
   928 	 *
   996 			if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) {
   995 			if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) {
   997 				$query_args['orderby'] = $orderby_mappings[ $request['orderby'] ];
   996 				$query_args['orderby'] = $orderby_mappings[ $request['orderby'] ];
   998 			}
   997 			}
   999 		}
   998 		}
  1000 
   999 
       
  1000 		$query_args['update_menu_item_cache'] = true;
       
  1001 
  1001 		return $query_args;
  1002 		return $query_args;
  1002 	}
  1003 	}
  1003 
  1004 
  1004 	/**
  1005 	/**
  1005 	 * Gets the id of the menu that the given menu item belongs to.
  1006 	 * Gets the id of the menu that the given menu item belongs to.