Editor: add navigation fallback.

Creates a fallback menu for the Navigation block including an API endpoint to retrieve it.

Props get_dave, spacedmonkey, kebbet, flixos90, mikeschroder, ramonopoly, audrasjb.
Fixes 58557.

Built from https://develop.svn.wordpress.org/trunk@56052


git-svn-id: http://core.svn.wordpress.org/trunk@55564 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
isabel_brison 2023-06-27 05:54:19 +00:00
parent 54c97ab015
commit 161b471f0b
7 changed files with 619 additions and 1 deletions

View File

@ -0,0 +1,130 @@
<?php
/**
* WP_Classic_To_Block_Menu_Converter class
*
* @package WordPress
* @since 6.3.0
*/
/**
* Converts a Classic Menu to Block Menu blocks.
*
* @since 6.3.0.
* @access public
*/
class WP_Classic_To_Block_Menu_Converter {
/**
* Converts a Classic Menu to blocks.
*
* @since 6.3.0.
*
* @param WP_Term $menu The Menu term object of the menu to convert.
* @return string the serialized and normalized parsed blocks.
*/
public static function convert( $menu ) {
if ( ! is_nav_menu( $menu ) ) {
return new WP_Error(
'invalid_menu',
__( 'The menu provided is not a valid menu.' )
);
}
$menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
if ( empty( $menu_items ) ) {
return array();
}
// Set up the $menu_item variables.
// Adds the class property classes for the current context, if applicable.
_wp_menu_item_classes_by_context( $menu_items );
$menu_items_by_parent_id = static::group_by_parent_id( $menu_items );
$first_menu_item = isset( $menu_items_by_parent_id[0] )
? $menu_items_by_parent_id[0]
: array();
$inner_blocks = static::to_blocks(
$first_menu_item,
$menu_items_by_parent_id
);
return serialize_blocks( $inner_blocks );
}
/**
* Returns an array of menu items grouped by the id of the parent menu item.
*
* @since 6.3.0.
*
* @param array $menu_items An array of menu items.
* @return array
*/
private static function group_by_parent_id( $menu_items ) {
$menu_items_by_parent_id = array();
foreach ( $menu_items as $menu_item ) {
$menu_items_by_parent_id[ $menu_item->menu_item_parent ][] = $menu_item;
}
return $menu_items_by_parent_id;
}
/**
* Turns menu item data into a nested array of parsed blocks
*
* @since 6.3.0.
*
* @param array $menu_items An array of menu items that represent
* an individual level of a menu.
* @param array $menu_items_by_parent_id An array keyed by the id of the
* parent menu where each element is an
* array of menu items that belong to
* that parent.
* @return array An array of parsed block data.
*/
private static function to_blocks( $menu_items, $menu_items_by_parent_id ) {
if ( empty( $menu_items ) ) {
return array();
}
$blocks = array();
foreach ( $menu_items as $menu_item ) {
$class_name = ! empty( $menu_item->classes ) ? implode( ' ', (array) $menu_item->classes ) : null;
$id = ( null !== $menu_item->object_id && 'custom' !== $menu_item->object ) ? $menu_item->object_id : null;
$opens_in_new_tab = null !== $menu_item->target && '_blank' === $menu_item->target;
$rel = ( null !== $menu_item->xfn && '' !== $menu_item->xfn ) ? $menu_item->xfn : null;
$kind = null !== $menu_item->type ? str_replace( '_', '-', $menu_item->type ) : 'custom';
$block = array(
'blockName' => isset( $menu_items_by_parent_id[ $menu_item->ID ] ) ? 'core/navigation-submenu' : 'core/navigation-link',
'attrs' => array(
'className' => $class_name,
'description' => $menu_item->description,
'id' => $id,
'kind' => $kind,
'label' => $menu_item->title,
'opensInNewTab' => $opens_in_new_tab,
'rel' => $rel,
'title' => $menu_item->attr_title,
'type' => $menu_item->object,
'url' => $menu_item->url,
),
);
$block['innerBlocks'] = isset( $menu_items_by_parent_id[ $menu_item->ID ] )
? static::to_blocks( $menu_items_by_parent_id[ $menu_item->ID ], $menu_items_by_parent_id )
: array();
$block['innerContent'] = array_map( 'serialize_block', $block['innerBlocks'] );
$blocks[] = $block;
}
return $blocks;
}
}

View File

@ -0,0 +1,247 @@
<?php
/**
* WP_Navigation_Fallback class
*
* Manages fallback behavior for Navigation menus.
*
* @package WordPress
* @subpackage Navigation
* @since 6.3.0
*/
/**
* Manages fallback behavior for Navigation menus.
*
* @access public
* @since 6.3.0.
*/
class WP_Navigation_Fallback {
/**
* Gets (and/or creates) an appropriate fallback Navigation Menu.
*
* @since 6.3.0.
*
* @return WP_Post|null the fallback Navigation Post or null.
*/
public static function get_fallback() {
$fallback = static::get_most_recently_published_navigation();
if ( $fallback ) {
return $fallback;
}
$fallback = static::create_classic_menu_fallback();
if ( $fallback && ! is_wp_error( $fallback ) ) {
// Return the newly created fallback post object which will now be the most recently created navigation menu.
return $fallback instanceof WP_Post ? $fallback : static::get_most_recently_published_navigation();
}
$fallback = static::create_default_fallback();
if ( $fallback && ! is_wp_error( $fallback ) ) {
// Return the newly created fallback post object which will now be the most recently created navigation menu.
return $fallback instanceof WP_Post ? $fallback : static::get_most_recently_published_navigation();
}
return null;
}
/**
* Finds the most recently published `wp_navigation` post type.
*
* @since 6.3.0.
*
* @return WP_Post|null the first non-empty Navigation or null.
*/
private static function get_most_recently_published_navigation() {
$parsed_args = array(
'post_type' => 'wp_navigation',
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'order' => 'DESC',
'orderby' => 'date',
'post_status' => 'publish',
'posts_per_page' => 1,
);
$navigation_post = new WP_Query( $parsed_args );
if ( count( $navigation_post->posts ) > 0 ) {
return $navigation_post->posts[0];
}
return null;
}
/**
* Creates a Navigation Menu post from a Classic Menu.
*
* @since 6.3.0.
*
* @return int|WP_Error The post ID of the default fallback menu or a WP_Error object.
*/
private static function create_classic_menu_fallback() {
// See if we have a classic menu.
$classic_nav_menu = static::get_fallback_classic_menu();
if ( ! $classic_nav_menu ) {
return new WP_Error( 'no_classic_menus', __( 'No Classic Menus found.' ) );
}
// If there is a classic menu then convert it to blocks.
$classic_nav_menu_blocks = WP_Classic_To_Block_Menu_Converter::convert( $classic_nav_menu );
if ( empty( $classic_nav_menu_blocks ) ) {
return new WP_Error( 'cannot_convert_classic_menu', __( 'Unable to convert Classic Menu to blocks.' ) );
}
// Create a new navigation menu from the classic menu.
$classic_menu_fallback = wp_insert_post(
array(
'post_content' => $classic_nav_menu_blocks,
'post_title' => $classic_nav_menu->name,
'post_name' => $classic_nav_menu->slug,
'post_status' => 'publish',
'post_type' => 'wp_navigation',
),
true // So that we can check whether the result is an error.
);
return $classic_menu_fallback;
}
/**
* Determine the most appropriate classic navigation menu to use as a fallback.
*
* @since 6.3.0.
*
* @return WP_Term|null The most appropriate classic navigation menu to use as a fallback.
*/
private static function get_fallback_classic_menu() {
$classic_nav_menus = wp_get_nav_menus();
if ( ! $classic_nav_menus || is_wp_error( $classic_nav_menus ) ) {
return null;
}
$nav_menu = static::get_nav_menu_at_primary_location();
if ( $nav_menu ) {
return $nav_menu;
}
$nav_menu = static::get_nav_menu_with_primary_slug( $classic_nav_menus );
if ( $nav_menu ) {
return $nav_menu;
}
return static::get_most_recently_created_nav_menu( $classic_nav_menus );
}
/**
* Sorts the classic menus and returns the most recently created one.
*
* @since 6.3.0.
*
* @param WP_Term[] $classic_nav_menus Array of classic nav menu term objects.
* @return WP_Term The most recently created classic nav menu.
*/
private static function get_most_recently_created_nav_menu( $classic_nav_menus ) {
usort(
$classic_nav_menus,
static function( $a, $b ) {
return $b->term_id - $a->term_id;
}
);
return $classic_nav_menus[0];
}
/**
* Returns the classic menu with the slug `primary` if it exists.
*
* @since 6.3.0.
*
* @param WP_Term[] $classic_nav_menus Array of classic nav menu term objects.
* @return WP_Term|null The classic nav menu with the slug `primary` or null.
*/
private static function get_nav_menu_with_primary_slug( $classic_nav_menus ) {
foreach ( $classic_nav_menus as $classic_nav_menu ) {
if ( 'primary' === $classic_nav_menu->slug ) {
return $classic_nav_menu;
}
}
return null;
}
/**
* Gets the classic menu assigned to the `primary` navigation menu location
* if it exists.
*
* @since 6.3.0.
*
* @return WP_Term|null The classic nav menu assigned to the `primary` location or null.
*/
private static function get_nav_menu_at_primary_location() {
$locations = get_nav_menu_locations();
if ( isset( $locations['primary'] ) ) {
$primary_menu = wp_get_nav_menu_object( $locations['primary'] );
if ( $primary_menu ) {
return $primary_menu;
}
}
return null;
}
/**
* Creates a default Navigation Block Menu fallback.
*
* @since 6.3.0.
*
* @return int|WP_Error The post ID of the default fallback menu or a WP_Error object.
*/
private static function create_default_fallback() {
$default_blocks = static::get_default_fallback_blocks();
// Create a new navigation menu from the fallback blocks.
$default_fallback = wp_insert_post(
array(
'post_content' => $default_blocks,
'post_title' => _x( 'Navigation', 'Title of a Navigation menu' ),
'post_name' => 'navigation',
'post_status' => 'publish',
'post_type' => 'wp_navigation',
),
true // So that we can check whether the result is an error.
);
return $default_fallback;
}
/**
* Gets the rendered markup for the default fallback blocks.
*
* @since 6.3.0.
*
* @return string default blocks markup to use a the fallback.
*/
private static function get_default_fallback_blocks() {
$registry = WP_Block_Type_Registry::get_instance();
// If `core/page-list` is not registered then use empty blocks.
return $registry->is_registered( 'core/page-list' ) ? '<!-- wp:page-list /-->' : '';
}
}

View File

@ -0,0 +1,41 @@
<?php
/**
* Navigation Fallback
*
* Functions required for managing Navigation fallbacks behavior.
*
* @package WordPress
* @since 6.3.0
*/
/**
* Expose additional fields in the embeddable links of the
* Navigation Fallback REST endpoint.
*
* The endpoint may embed the full Navigation Menu object into the
* response as the `self` link. By default the Posts Controller
* will only exposes a limited subset of fields but the editor requires
* additional fields to be available in order to utilise the menu.
*
* @since 6.3.0
*
* @param array $schema the schema for the `wp_navigation` post.
* @return array the modified schema.
*/
function wp_add_fields_to_navigation_fallback_embeded_links( $schema ) {
// Expose top level fields.
$schema['properties']['status']['context'] = array_merge( $schema['properties']['status']['context'], array( 'embed' ) );
$schema['properties']['content']['context'] = array_merge( $schema['properties']['content']['context'], array( 'embed' ) );
// Expose sub properties of content field.
$schema['properties']['content']['properties']['raw']['context'] = array_merge( $schema['properties']['content']['properties']['raw']['context'], array( 'embed' ) );
$schema['properties']['content']['properties']['rendered']['context'] = array_merge( $schema['properties']['content']['properties']['rendered']['context'], array( 'embed' ) );
$schema['properties']['content']['properties']['block_version']['context'] = array_merge( $schema['properties']['content']['properties']['block_version']['context'], array( 'embed' ) );
return $schema;
}
add_filter(
'rest_wp_navigation_item_schema',
'wp_add_fields_to_navigation_fallback_embeded_links'
);

View File

@ -377,6 +377,10 @@ function create_initial_rest_routes() {
// Site Editor Export.
$controller = new WP_REST_Edit_Site_Export_Controller();
$controller->register_routes();
// Navigation Fallback.
$controller = new WP_REST_Navigation_Fallback_Controller();
$controller->register_routes();
}
/**

View File

@ -0,0 +1,193 @@
<?php
/**
* WP_REST_Navigation_Fallback_Controller class
*
* REST Controller to create/fetch a fallback Navigation Menu.
*
* @package WordPress
* @subpackage REST_API
* @since 6.3.0
*/
/**
* REST Controller to fetch a fallback Navigation Block Menu. If needed it creates one.
*
* @since 6.3.0.
*/
class WP_REST_Navigation_Fallback_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-block-editor/v1';
$this->rest_base = 'navigation-fallback';
$this->post_type = 'wp_navigation';
}
/**
* Registers the controllers routes.
*
* @since 6.3.0.
*
* @return void
*/
public function register_routes() {
// Lists a single nav item based on the given id or slug.
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::READABLE ),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
}
/**
* Checks if a given request has access to read fallbacks.
*
* @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 get_item_permissions_check( $request ) {
$post_type = get_post_type_object( $this->post_type );
// Getting fallbacks 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_authorization_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_authorization_required_code() )
);
}
return true;
}
/**
* Gets the most appropriate fallback 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 get_item( $request ) {
$post = WP_Navigation_Fallback::get_fallback();
if ( empty( $post ) ) {
return rest_ensure_response( new WP_Error( 'no_fallback_menu', __( 'No fallback menu found.' ), array( 'status' => 404 ) ) );
}
$response = $this->prepare_item_for_response( $post, $request );
return $response;
}
/**
* Retrieves the fallbacks' schema, conforming to JSON Schema.
*
* @since 6.3.0.
*
* @return array Item schema data.
*/
public function get_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-fallback',
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'The unique identifier for the Navigation Menu.' ),
'type' => 'integer',
'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->get_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( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
$links = $this->prepare_links( $item );
$response->add_links( $links );
}
return $response;
}
/**
* Prepares the links for the request.
*
* @since 6.3.0.
*
* @param WP_Post $post the Navigation Menu post object.
* @return array Links for the given request.
*/
private function prepare_links( $post ) {
return array(
'self' => array(
'href' => rest_url( rest_get_route_for_post( $post->ID ) ),
'embeddable' => true,
),
);
}
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.3-alpha-56051';
$wp_version = '6.3-alpha-56052';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

View File

@ -291,6 +291,7 @@ require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-widget-types-contro
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-widgets-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-templates-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-url-details-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-navigation-fallback-controller.php';
require ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-meta-fields.php';
require ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-comment-meta-fields.php';
require ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-post-meta-fields.php';
@ -321,6 +322,8 @@ require ABSPATH . WPINC . '/class-wp-block-list.php';
require ABSPATH . WPINC . '/class-wp-block-parser-block.php';
require ABSPATH . WPINC . '/class-wp-block-parser-frame.php';
require ABSPATH . WPINC . '/class-wp-block-parser.php';
require ABSPATH . WPINC . '/class-wp-classic-to-block-menu-converter.php';
require ABSPATH . WPINC . '/class-wp-navigation-fallback.php';
require ABSPATH . WPINC . '/blocks.php';
require ABSPATH . WPINC . '/blocks/index.php';
require ABSPATH . WPINC . '/block-editor.php';