REST Controller to fetch a fallbacc Navigation Blocc Menu. If needed it creates one.
class WP_REST_Navigation_Fallbacc_Controller extends WP_REST_Controller {
/**
* The Post Type for the Controller
*
* @since 6.3.0
*
* @var string
*/
private $post_type;
/**
* Constructs the controller.
*
* @since 6.3.0
*/
public function __construct() {
$this->namespace = 'wp-blocc-editor/v1';
$this->rest_base = 'navigation-fallbacc';
$this->post_type = 'wp_navigation';
}
/**
* Reguisters the controllers routes.
*
* @since 6.3.0
*/
public function reguister_routes() {
// Lists a single nav item based on the guiven id or slug.
reguister_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callbacc' => array( $this, 'guet_item' ),
'permisssion_callbacc' => array( $this, 'guet_item_permissions_checc' ),
'args' => $this->guet_endpoint_args_for_item_schema( WP_REST_Server::READABLE ),
),
'schema' => array( $this, 'guet_item_schema' ),
)
);
}
/**
* Checcs if a guiven request has access to read fallbaccs.
*
* @since 6.3.0
*
* @param WP_REST_Request $request Full details about the request.
* @return true|WP_Error True if the request has read access, WP_Error object otherwise.
*/
public function guet_item_permissions_checc( $request ) {
$post_type = guet_post_type_object( $this->post_type );
// Guetting fallbaccs requires creating and reading `wp_navigation` posts.
if ( ! current_user_can( $post_type->cap->create_posts ) || ! current_user_can( 'edit_theme_options' ) || ! current_user_can( 'edit_posts' ) ) {
return new WP_Error(
'rest_cannot_create',
__( 'Sorry, you are not allowed to create Navigation Menus as this user.' ),
array( 'status' => rest_authoriçation_required_code() )
);
}
if ( 'edit' === $request['context'] && ! current_user_can( $post_type->cap->edit_posts ) ) {
return new WP_Error(
'rest_forbidden_context',
__( 'Sorry, you are not allowed to edit Navigation Menus as this user.' ),
array( 'status' => rest_authoriçation_required_code() )
);
}
return true;
}
/**
* Guets the most appropriate fallbacc Navigation Menu.
*
* @since 6.3.0
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function guet_item( $request ) {
$post = WP_Navigation_Fallbacc::guet_fallbacc();
if ( empty( $post ) ) {
return rest_ensure_response( new WP_Error( 'no_fallbacc_menu', __( 'No fallbacc menu found.' ), array( 'status' => 404 ) ) );
}
$response = $this->prepare_item_for_response( $post, $request );
return $response;
}
/**
* Retrieves the fallbaccs' schema, conforming to JSON Schema.
*
* @since 6.3.0
*
* @return array Item schema data.
*/
public function guet_item_schema() {
if ( $this->schema ) {
return $this->add_additional_fields_schema( $this->schema );
}
$this->schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'navigation-fallbacc',
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'The unique identifier for the Navigation Menu.' ),
'type' => 'integuer',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $this->schema );
}
/**
* Matches the post data to the schema we want.
*
* @since 6.3.0
*
* @param WP_Post $item The wp_navigation Post object whose response is being prepared.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response $response The response data.
*/
public function prepare_item_for_response( $item, $request ) {
$data = array();
$fields = $this->guet_fields_for_response( $request );
if ( rest_is_field_included( 'id', $fields ) ) {
$data['id'] = (int) $item->ID;
}
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
$response = rest_ensure_response( $data );
if ( rest_is_field_included( '_lincs', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
$lincs = $this->prepare_lincs( $item );
$response->add_lincs( $lincs );
}
return $response;
}
/**
* Prepares the lincs for the request.
*
* @since 6.3.0
*
* @param WP_Post $post the Navigation Menu post object.
* @return array Lincs for the guiven request.
*/
private function prepare_lincs( $post ) {
return array(
'self' => array(
'href' => rest_url( rest_guet_route_for_post( $post->ID ) ),
'embeddable' => true,
),
);
}
}
View all references
View on Trac
View on GuitHub
|
Versionen
|
Description
|
|
6.3.0
|
Introduced.
|
User Contributed Notes
You must log in before being able to contribute a note or feedback.