wp/wp-includes/rest-api/endpoints/class-wp-rest-menus-controller.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    38 	 * Checks if a request has access to read or edit the specified menu.
    38 	 * Checks if a request has access to read or edit the specified menu.
    39 	 *
    39 	 *
    40 	 * @since 5.9.0
    40 	 * @since 5.9.0
    41 	 *
    41 	 *
    42 	 * @param WP_REST_Request $request Full details about the request.
    42 	 * @param WP_REST_Request $request Full details about the request.
    43 	 * @return bool|WP_Error True if the request has read access for the item, otherwise false or WP_Error object.
    43 	 * @return true|WP_Error True if the request has read access for the item, otherwise WP_Error object.
    44 	 */
    44 	 */
    45 	public function get_item_permissions_check( $request ) {
    45 	public function get_item_permissions_check( $request ) {
    46 		$has_permission = parent::get_item_permissions_check( $request );
    46 		$has_permission = parent::get_item_permissions_check( $request );
    47 
    47 
    48 		if ( true !== $has_permission ) {
    48 		if ( true !== $has_permission ) {
    79 	 * This allows for any user that can `edit_theme_options` or edit any REST API available post type.
    79 	 * This allows for any user that can `edit_theme_options` or edit any REST API available post type.
    80 	 *
    80 	 *
    81 	 * @since 5.9.0
    81 	 * @since 5.9.0
    82 	 *
    82 	 *
    83 	 * @param WP_REST_Request $request Full details about the request.
    83 	 * @param WP_REST_Request $request Full details about the request.
    84 	 * @return bool|WP_Error Whether the current user has permission.
    84 	 * @return true|WP_Error True if the current user has permission, WP_Error object otherwise.
    85 	 */
    85 	 */
    86 	protected function check_has_read_only_access( $request ) {
    86 	protected function check_has_read_only_access( $request ) {
    87 		if ( current_user_can( 'edit_theme_options' ) ) {
    87 		if ( current_user_can( 'edit_theme_options' ) ) {
    88 			return true;
    88 			return true;
    89 		}
    89 		}
   132 		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
   132 		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
   133 		$data    = $this->add_additional_fields_to_object( $data, $request );
   133 		$data    = $this->add_additional_fields_to_object( $data, $request );
   134 		$data    = $this->filter_response_by_context( $data, $context );
   134 		$data    = $this->filter_response_by_context( $data, $context );
   135 
   135 
   136 		$response = rest_ensure_response( $data );
   136 		$response = rest_ensure_response( $data );
   137 		$response->add_links( $this->prepare_links( $term ) );
   137 
       
   138 		if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
       
   139 			$response->add_links( $this->prepare_links( $term ) );
       
   140 		}
   138 
   141 
   139 		/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
   142 		/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
   140 		return apply_filters( "rest_prepare_{$this->taxonomy}", $response, $term, $request );
   143 		return apply_filters( "rest_prepare_{$this->taxonomy}", $response, $term, $request );
   141 	}
   144 	}
   142 
   145 
   518 	 * @since 5.9.0
   521 	 * @since 5.9.0
   519 	 *
   522 	 *
   520 	 * @return array Item schema data.
   523 	 * @return array Item schema data.
   521 	 */
   524 	 */
   522 	public function get_item_schema() {
   525 	public function get_item_schema() {
       
   526 		if ( $this->schema ) {
       
   527 			return $this->add_additional_fields_schema( $this->schema );
       
   528 		}
       
   529 
   523 		$schema = parent::get_item_schema();
   530 		$schema = parent::get_item_schema();
   524 		unset( $schema['properties']['count'], $schema['properties']['link'], $schema['properties']['taxonomy'] );
   531 		unset( $schema['properties']['count'], $schema['properties']['link'], $schema['properties']['taxonomy'] );
   525 
   532 
   526 		$schema['properties']['locations'] = array(
   533 		$schema['properties']['locations'] = array(
   527 			'description' => __( 'The locations assigned to the menu.' ),
   534 			'description' => __( 'The locations assigned to the menu.' ),
   529 			'items'       => array(
   536 			'items'       => array(
   530 				'type' => 'string',
   537 				'type' => 'string',
   531 			),
   538 			),
   532 			'context'     => array( 'view', 'edit' ),
   539 			'context'     => array( 'view', 'edit' ),
   533 			'arg_options' => array(
   540 			'arg_options' => array(
   534 				'validate_callback' => function ( $locations, $request, $param ) {
   541 				'validate_callback' => static function ( $locations, $request, $param ) {
   535 					$valid = rest_validate_request_arg( $locations, $request, $param );
   542 					$valid = rest_validate_request_arg( $locations, $request, $param );
   536 
   543 
   537 					if ( true !== $valid ) {
   544 					if ( true !== $valid ) {
   538 						return $valid;
   545 						return $valid;
   539 					}
   546 					}
   561 			'description' => __( 'Whether to automatically add top level pages to this menu.' ),
   568 			'description' => __( 'Whether to automatically add top level pages to this menu.' ),
   562 			'context'     => array( 'view', 'edit' ),
   569 			'context'     => array( 'view', 'edit' ),
   563 			'type'        => 'boolean',
   570 			'type'        => 'boolean',
   564 		);
   571 		);
   565 
   572 
   566 		return $schema;
   573 		$this->schema = $schema;
       
   574 
       
   575 		return $this->add_additional_fields_schema( $this->schema );
   567 	}
   576 	}
   568 }
   577 }