-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create endpoint to fetch menu custom items
- Loading branch information
Showing
11 changed files
with
155 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
/** | ||
* WP_REST_Menu_Custom_Items_Controller class. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Class that returns the menu items added via the | ||
* `customize_nav_menu_available_items` filter. | ||
*/ | ||
class WP_REST_Menu_Custom_Items_Controller extends WP_REST_Controller { | ||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct() { | ||
$this->namespace = '__experimental'; | ||
$this->rest_base = 'menu-custom-items'; | ||
} | ||
|
||
/** | ||
* Registers the necessary REST API routes. | ||
* | ||
* @access public | ||
*/ | ||
public function register_routes() { | ||
register_rest_route( | ||
$this->namespace, | ||
'/' . $this->rest_base, | ||
array( | ||
array( | ||
'methods' => WP_REST_Server::READABLE, | ||
'callback' => array( $this, 'get_menu_custom_items' ), | ||
'permission_callback' => array( $this, 'permissions_check' ), | ||
'args' => $this->get_collection_params(), | ||
), | ||
'schema' => array( $this, 'get_public_item_schema' ), | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Checks if a given request has access to read menu items if they have access to edit them. | ||
* | ||
* @return true|WP_Error True if the request has read access, WP_Error object otherwise. | ||
*/ | ||
public function permissions_check() { | ||
$post_type = get_post_type_object( 'nav_menu_item' ); | ||
if ( ! current_user_can( $post_type->cap->edit_posts ) ) { | ||
return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to view menu items.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) ); | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Returns the menu items added via the | ||
* `customize_nav_menu_available_item_types` filter. | ||
* | ||
* @param WP_REST_Request $request Full details about the request. | ||
* @return WP_REST_Response|WP_Error Menu custom items, or WP_Error if type could not be found. | ||
*/ | ||
public function get_menu_custom_items( $request ) { | ||
$requested_type = $request->get_param( 'type' ); | ||
$item_types = apply_filters( 'customize_nav_menu_available_item_types', array() ); | ||
|
||
if ( is_array( $item_types ) ) { | ||
foreach ( $item_types as $item_type ) { | ||
if ( $item_type['type'] === $requested_type ) { | ||
return rest_ensure_response( | ||
apply_filters( 'customize_nav_menu_available_items', array(), $item_type['type'], $item_type['object'], 0 ) | ||
); | ||
} | ||
} | ||
} | ||
|
||
return new WP_Error( 'rest_invalid_menu_item_type', __( 'This item type could not be found.', 'gutenberg' ), array( 'status' => 404 ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
packages/core-data/src/fetch/__experimental-fetch-menu-custom-items.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import apiFetch from '@wordpress/api-fetch'; | ||
import { addQueryArgs } from '@wordpress/url'; | ||
|
||
const fetchMenuCustomItems = async ( search, { type } ) => { | ||
const path = addQueryArgs( '/__experimental/menu-custom-items', { type } ); | ||
return apiFetch( { | ||
path, | ||
} ).then( ( results ) => { | ||
return results.filter( | ||
( result ) => | ||
search === '' || result.title.match( new RegExp( search, 'i' ) ) | ||
); | ||
} ); | ||
}; | ||
|
||
export default fetchMenuCustomItems; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as __experimentalFetchLinkSuggestions } from './__experimental-fetch-link-suggestions'; | ||
export { default as __experimentalFetchRemoteUrlData } from './__experimental-fetch-remote-url-data'; | ||
export { default as __experimentalFetchMenuCustomItems } from './__experimental-fetch-menu-custom-items'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters