diff --git a/wp-includes/class-wp-classic-to-block-menu-converter.php b/wp-includes/class-wp-classic-to-block-menu-converter.php new file mode 100644 index 0000000000..0f72b93514 --- /dev/null +++ b/wp-includes/class-wp-classic-to-block-menu-converter.php @@ -0,0 +1,130 @@ +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; + } +} diff --git a/wp-includes/class-wp-navigation-fallback.php b/wp-includes/class-wp-navigation-fallback.php new file mode 100644 index 0000000000..6df4993a0f --- /dev/null +++ b/wp-includes/class-wp-navigation-fallback.php @@ -0,0 +1,247 @@ + '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' ) ? '' : ''; + } +} diff --git a/wp-includes/navigation-fallback.php b/wp-includes/navigation-fallback.php new file mode 100644 index 0000000000..e5bcc16801 --- /dev/null +++ b/wp-includes/navigation-fallback.php @@ -0,0 +1,41 @@ +register_routes(); + + // Navigation Fallback. + $controller = new WP_REST_Navigation_Fallback_Controller(); + $controller->register_routes(); } /** diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-navigation-fallback-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-navigation-fallback-controller.php new file mode 100644 index 0000000000..eafec4a91f --- /dev/null +++ b/wp-includes/rest-api/endpoints/class-wp-rest-navigation-fallback-controller.php @@ -0,0 +1,193 @@ +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, + ), + ); + } +} diff --git a/wp-includes/version.php b/wp-includes/version.php index 285dfd9d31..ac27792db9 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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. diff --git a/wp-settings.php b/wp-settings.php index 3b59004ada..26d2662f6b 100644 --- a/wp-settings.php +++ b/wp-settings.php @@ -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';