wp/wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
    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 true|WP_Error True if the request has read access for the item, WP_Error object otherwise.
    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 		/**
       
    84 		 * Filters whether the current user has read access to menu items via the REST API.
       
    85 		 *
       
    86 		 * @since 6.8.0
       
    87 		 *
       
    88 		 * @param bool               $read_only_access Whether the current user has read access to menu items
       
    89 		 *                                             via the REST API.
       
    90 		 * @param WP_REST_Request    $request          Full details about the request.
       
    91 		 * @param WP_REST_Controller $this             The current instance of the controller.
       
    92 		 */
       
    93 		$read_only_access = apply_filters( 'rest_menu_read_access', false, $request, $this );
       
    94 		if ( $read_only_access ) {
       
    95 			return true;
       
    96 		}
       
    97 
    83 		if ( current_user_can( 'edit_theme_options' ) ) {
    98 		if ( current_user_can( 'edit_theme_options' ) ) {
    84 			return true;
    99 			return true;
    85 		}
   100 		}
    86 
   101 
    87 		if ( current_user_can( 'edit_posts' ) ) {
   102 		if ( current_user_can( 'edit_posts' ) ) {
   100 			array( 'status' => rest_authorization_required_code() )
   115 			array( 'status' => rest_authorization_required_code() )
   101 		);
   116 		);
   102 	}
   117 	}
   103 
   118 
   104 	/**
   119 	/**
   105 	 * Creates a single post.
   120 	 * Creates a single nav menu item.
   106 	 *
   121 	 *
   107 	 * @since 5.9.0
   122 	 * @since 5.9.0
   108 	 *
   123 	 *
   109 	 * @param WP_REST_Request $request Full details about the request.
   124 	 * @param WP_REST_Request $request Full details about the request.
   110 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
   125 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
   265 
   280 
   266 		return rest_ensure_response( $response );
   281 		return rest_ensure_response( $response );
   267 	}
   282 	}
   268 
   283 
   269 	/**
   284 	/**
   270 	 * Deletes a single menu item.
   285 	 * Deletes a single nav menu item.
   271 	 *
   286 	 *
   272 	 * @since 5.9.0
   287 	 * @since 5.9.0
   273 	 *
   288 	 *
   274 	 * @param WP_REST_Request $request Full details about the request.
   289 	 * @param WP_REST_Request $request Full details about the request.
   275 	 * @return WP_REST_Response|WP_Error True on success, or WP_Error object on failure.
   290 	 * @return WP_REST_Response|WP_Error True on success, or WP_Error object on failure.
   315 
   330 
   316 		return $response;
   331 		return $response;
   317 	}
   332 	}
   318 
   333 
   319 	/**
   334 	/**
   320 	 * Prepares a single post for create or update.
   335 	 * Prepares a single nav menu item for create or update.
   321 	 *
   336 	 *
   322 	 * @since 5.9.0
   337 	 * @since 5.9.0
   323 	 *
   338 	 *
   324 	 * @param WP_REST_Request $request Request object.
   339 	 * @param WP_REST_Request $request Request object.
   325 	 *
   340 	 *
   480 		 */
   495 		 */
   481 		return apply_filters( 'rest_pre_insert_nav_menu_item', $prepared_nav_item, $request );
   496 		return apply_filters( 'rest_pre_insert_nav_menu_item', $prepared_nav_item, $request );
   482 	}
   497 	}
   483 
   498 
   484 	/**
   499 	/**
   485 	 * Prepares a single post output for response.
   500 	 * Prepares a single nav menu item output for response.
   486 	 *
   501 	 *
   487 	 * @since 5.9.0
   502 	 * @since 5.9.0
   488 	 *
   503 	 *
   489 	 * @param WP_Post         $item    Post object.
   504 	 * @param WP_Post         $item    Post object.
   490 	 * @param WP_REST_Request $request Request object.
   505 	 * @param WP_REST_Request $request Request object.
   508 			$data['title']['raw'] = $menu_item->title;
   523 			$data['title']['raw'] = $menu_item->title;
   509 		}
   524 		}
   510 
   525 
   511 		if ( rest_is_field_included( 'title.rendered', $fields ) ) {
   526 		if ( rest_is_field_included( 'title.rendered', $fields ) ) {
   512 			add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
   527 			add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
       
   528 			add_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
   513 
   529 
   514 			/** This filter is documented in wp-includes/post-template.php */
   530 			/** This filter is documented in wp-includes/post-template.php */
   515 			$title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID );
   531 			$title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID );
   516 
   532 
   517 			$data['title']['rendered'] = $title;
   533 			$data['title']['rendered'] = $title;
   518 
   534 
   519 			remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
   535 			remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
       
   536 			remove_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
   520 		}
   537 		}
   521 
   538 
   522 		if ( rest_is_field_included( 'status', $fields ) ) {
   539 		if ( rest_is_field_included( 'status', $fields ) ) {
   523 			$data['status'] = $menu_item->post_status;
   540 			$data['status'] = $menu_item->post_status;
   524 		}
   541 		}
   674 
   691 
   675 		return $links;
   692 		return $links;
   676 	}
   693 	}
   677 
   694 
   678 	/**
   695 	/**
   679 	 * Retrieves Link Description Objects that should be added to the Schema for the posts collection.
   696 	 * Retrieves Link Description Objects that should be added to the Schema for the nav menu items collection.
   680 	 *
   697 	 *
   681 	 * @since 5.9.0
   698 	 * @since 5.9.0
   682 	 *
   699 	 *
   683 	 * @return array
   700 	 * @return array
   684 	 */
   701 	 */
   701 
   718 
   702 		return $links;
   719 		return $links;
   703 	}
   720 	}
   704 
   721 
   705 	/**
   722 	/**
   706 	 * Retrieves the term's schema, conforming to JSON Schema.
   723 	 * Retrieves the nav menu item's schema, conforming to JSON Schema.
   707 	 *
   724 	 *
   708 	 * @since 5.9.0
   725 	 * @since 5.9.0
   709 	 *
   726 	 *
   710 	 * @return array Item schema data.
   727 	 * @return array Item schema data.
   711 	 */
   728 	 */
   922 
   939 
   923 		return $this->add_additional_fields_schema( $this->schema );
   940 		return $this->add_additional_fields_schema( $this->schema );
   924 	}
   941 	}
   925 
   942 
   926 	/**
   943 	/**
   927 	 * Retrieves the query params for the posts collection.
   944 	 * Retrieves the query params for the nav menu items collection.
   928 	 *
   945 	 *
   929 	 * @since 5.9.0
   946 	 * @since 5.9.0
   930 	 *
   947 	 *
   931 	 * @return array Collection parameters.
   948 	 * @return array Collection parameters.
   932 	 */
   949 	 */