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
This commit is contained in:
Sergey Biryukov 2023-10-06 14:06:22 +00:00
parent 67c2237b52
commit 5c4f902c20
6 changed files with 50 additions and 59 deletions

View File

@ -867,6 +867,8 @@ $_old_files = array(
'wp-includes/images/wlw', 'wp-includes/images/wlw',
'wp-includes/wlwmanifest.xml', 'wp-includes/wlwmanifest.xml',
'wp-includes/random_compat', 'wp-includes/random_compat',
// 6.4
'wp-includes/navigation-fallback.php',
); );
/** /**

View File

@ -17,6 +17,50 @@
*/ */
class WP_Navigation_Fallback { 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. * 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. * @return WP_Post|null the fallback Navigation Post or null.
*/ */
public static function get_fallback() { public static function get_fallback() {
/** /**
* Filters whether or not a fallback should be created. * Filters whether or not a fallback should be created.
* *

View File

@ -711,6 +711,9 @@ add_action( 'wp_footer', 'the_block_template_skip_link' );
add_action( 'setup_theme', 'wp_enable_block_templates' ); add_action( 'setup_theme', 'wp_enable_block_templates' );
add_action( 'wp_loaded', '_add_template_loader_filters' ); 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. // Fluid typography.
add_filter( 'render_block', 'wp_render_typography_support', 10, 2 ); add_filter( 'render_block', 'wp_render_typography_support', 10, 2 );

View File

@ -1,56 +0,0 @@
<?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 expose a limited subset of fields but the editor requires
* additional fields to be available in order to utilize 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_embedded_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' ) );
/*
* 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;
}
add_filter(
'rest_wp_navigation_item_schema',
'wp_add_fields_to_navigation_fallback_embedded_links'
);

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.4-beta2-56792'; $wp_version = '6.4-beta2-56793';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

View File

@ -354,7 +354,6 @@ require ABSPATH . WPINC . '/block-supports/position.php';
require ABSPATH . WPINC . '/block-supports/spacing.php'; require ABSPATH . WPINC . '/block-supports/spacing.php';
require ABSPATH . WPINC . '/block-supports/typography.php'; require ABSPATH . WPINC . '/block-supports/typography.php';
require ABSPATH . WPINC . '/block-supports/settings.php'; require ABSPATH . WPINC . '/block-supports/settings.php';
require ABSPATH . WPINC . '/navigation-fallback.php';
require ABSPATH . WPINC . '/style-engine.php'; require ABSPATH . WPINC . '/style-engine.php';
require ABSPATH . WPINC . '/style-engine/class-wp-style-engine.php'; require ABSPATH . WPINC . '/style-engine/class-wp-style-engine.php';
require ABSPATH . WPINC . '/style-engine/class-wp-style-engine-css-declarations.php'; require ABSPATH . WPINC . '/style-engine/class-wp-style-engine-css-declarations.php';