From 5c4f902c20d66cca22d488d2b3ca2cf3451a9932 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 6 Oct 2023 14:06:22 +0000 Subject: [PATCH] Editor: Move `wp_navigation` schema updating to `WP_Navigation_Fallback` class. This aims to better align the navigation fallback implementation with core architecture and best practices. The function that updates the `wp_navigation` post response schema is now a public method of the `WP_Navigation_Fallback` class, so an extra file previously used for that specific function is no longer necessary. Follow-up to [56052]. Props ramonopoly, scruffian, isabel_brison, mukesh27, swissspidy, rajinsharwar, afercia, audrasjb, mikeschroder, JeffPaul, johnjamesjacoby, TimothyBlynJacobs, oglekler, SergeyBiryukov. Fixes #58910. Built from https://develop.svn.wordpress.org/trunk@56793 git-svn-id: http://core.svn.wordpress.org/trunk@56305 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/update-core.php | 2 + wp-includes/class-wp-navigation-fallback.php | 45 +++++++++++++++- wp-includes/default-filters.php | 3 ++ wp-includes/navigation-fallback.php | 56 -------------------- wp-includes/version.php | 2 +- wp-settings.php | 1 - 6 files changed, 50 insertions(+), 59 deletions(-) delete mode 100644 wp-includes/navigation-fallback.php diff --git a/wp-admin/includes/update-core.php b/wp-admin/includes/update-core.php index 5f5c7a9ab6..51d786fa6d 100644 --- a/wp-admin/includes/update-core.php +++ b/wp-admin/includes/update-core.php @@ -867,6 +867,8 @@ $_old_files = array( 'wp-includes/images/wlw', 'wp-includes/wlwmanifest.xml', 'wp-includes/random_compat', + // 6.4 + 'wp-includes/navigation-fallback.php', ); /** diff --git a/wp-includes/class-wp-navigation-fallback.php b/wp-includes/class-wp-navigation-fallback.php index bd9583d1fa..59fe023d80 100644 --- a/wp-includes/class-wp-navigation-fallback.php +++ b/wp-includes/class-wp-navigation-fallback.php @@ -17,6 +17,50 @@ */ class WP_Navigation_Fallback { + /** + * Updates the wp_navigation custom post type schema, in order to expose + * additional fields in the embeddable links of WP_REST_Navigation_Fallback_Controller. + * + * The Navigation Fallback endpoint may embed the full Navigation Menu object + * into the response as the `self` link. By default, the Posts Controller + * will only expose a limited subset of fields but the editor requires + * additional fields to be available in order to utilize the menu. + * + * Used with the `rest_wp_navigation_item_schema` hook. + * + * @since 6.4.0 + * + * @param array $schema The schema for the `wp_navigation` post. + * @return array The modified schema. + */ + public static function update_wp_navigation_post_schema( $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' ) ); + + /* + * Exposes sub properties of content field. + * These sub properties aren't exposed by the posts controller by default, + * for requests where context is `embed`. + * + * @see WP_REST_Posts_Controller::get_item_schema() + */ + $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' ) ); + + /* + * Exposes sub properties of title field. + * These sub properties aren't exposed by the posts controller by default, + * for requests where context is `embed`. + * + * @see WP_REST_Posts_Controller::get_item_schema() + */ + $schema['properties']['title']['properties']['raw']['context'] = array_merge( $schema['properties']['title']['properties']['raw']['context'], array( 'embed' ) ); + + return $schema; + } + /** * Gets (and/or creates) an appropriate fallback Navigation Menu. * @@ -25,7 +69,6 @@ class WP_Navigation_Fallback { * @return WP_Post|null the fallback Navigation Post or null. */ public static function get_fallback() { - /** * Filters whether or not a fallback should be created. * diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php index 99116470d5..4a7787761b 100644 --- a/wp-includes/default-filters.php +++ b/wp-includes/default-filters.php @@ -711,6 +711,9 @@ add_action( 'wp_footer', 'the_block_template_skip_link' ); add_action( 'setup_theme', 'wp_enable_block_templates' ); add_action( 'wp_loaded', '_add_template_loader_filters' ); +// wp_navigation post type. +add_filter( 'rest_wp_navigation_item_schema', array( 'WP_Navigation_Fallback', 'update_wp_navigation_post_schema' ) ); + // Fluid typography. add_filter( 'render_block', 'wp_render_typography_support', 10, 2 ); diff --git a/wp-includes/navigation-fallback.php b/wp-includes/navigation-fallback.php deleted file mode 100644 index cfd92f07e8..0000000000 --- a/wp-includes/navigation-fallback.php +++ /dev/null @@ -1,56 +0,0 @@ -