From 67badf0fc09490a099223feecde670c9b3d66055 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 12 Dec 2024 13:38:37 +0400 Subject: [PATCH 1/4] REST API: Simplify 'default_rendering_mode' field registration for post types --- ...tenberg-rest-post-types-controller-6-8.php | 70 +++++++------------ lib/compat/wordpress-6.8/rest-api.php | 11 --- 2 files changed, 25 insertions(+), 56 deletions(-) diff --git a/lib/compat/wordpress-6.8/class-gutenberg-rest-post-types-controller-6-8.php b/lib/compat/wordpress-6.8/class-gutenberg-rest-post-types-controller-6-8.php index da0489210e21f1..1309472b800246 100644 --- a/lib/compat/wordpress-6.8/class-gutenberg-rest-post-types-controller-6-8.php +++ b/lib/compat/wordpress-6.8/class-gutenberg-rest-post-types-controller-6-8.php @@ -6,56 +6,36 @@ */ /** - * Gutenberg_REST_Post_Types_Controller_6_8 class + * Register a REST field for the default rendering mode of a post type. * - * Add Block Editor default rendering mode to the post type response - * to allow enabling/disabling at the post type level. + * Note: Logic will become part of the `prepare_item_for_response` method when backporting to the core. + * + * @return void */ -class Gutenberg_REST_Post_Types_Controller_6_8 extends WP_REST_Post_Types_Controller { - /** - * Add Block Editor default rendering mode setting to the response. - * - * @param WP_Post_Type $item Post type object. - * @param WP_REST_Request $request Request object. - * @return WP_REST_Response Response object. - */ - public function prepare_item_for_response( $item, $request ) { - $response = parent::prepare_item_for_response( $item, $request ); - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - - // Property will only exist if the post type supports the block editor. - if ( 'edit' === $context && property_exists( $item, 'default_rendering_mode' ) ) { - /** - * Filters the block editor rendering mode for a post type. - * - * @since 6.8.0 - * @param string $default_rendering_mode Default rendering mode for the post type. - * @param WP_Post_Type $post_type Post type name. - * @return string Default rendering mode for the post type. - */ - $rendering_mode = apply_filters( 'post_type_default_rendering_mode', $item->default_rendering_mode, $item ); +function gutenberg_editor_rendering_mode_field() { + register_rest_field( 'type', 'default_rendering_mode', array( + 'get_callback' => static function( $object ) { + $post_type_object = get_post_type_object( $object['slug'] ); - /** - * Filters the block editor rendering mode for a specific post type. - * Applied after the generic `post_type_default_rendering_mode` filter. - * - * The dynamic portion of the hook name, `$item->name`, refers to the post type slug. - * - * @since 6.8.0 - * @param string $default_rendering_mode Default rendering mode for the post type. - * @param WP_Post_Type $post_type Post type object. - * @return string Default rendering mode for the post type. - */ - $rendering_mode = apply_filters( "post_type_{$item->name}_default_rendering_mode", $rendering_mode, $item ); + // Property will only exist if the post type supports the block editor. + if ( ! $post_type_object || ! isset( $post_type_object->default_rendering_mode ) ) { + return ''; + } // Validate the filtered rendering mode. - if ( ! in_array( $rendering_mode, gutenberg_post_type_rendering_modes(), true ) ) { - $rendering_mode = 'post-only'; + if ( ! in_array( $post_type_object->default_rendering_mode, gutenberg_post_type_rendering_modes(), true ) ) { + return 'post-only'; } - $response->data['default_rendering_mode'] = $rendering_mode; - } - - return rest_ensure_response( $response ); - } + return $post_type_object->default_rendering_mode; + }, + 'schema' => array( + 'description' => __( 'The rendering mode for the editor.', 'gutenberg' ), + 'type' => 'string', + 'enum' => array( 'post-only', 'template-locked' ), + 'context' => array( 'edit' ), + 'readonly' => true, + ), + ) ); } +add_action( 'rest_api_init', 'gutenberg_editor_rendering_mode_field' ); diff --git a/lib/compat/wordpress-6.8/rest-api.php b/lib/compat/wordpress-6.8/rest-api.php index b94e42d5f2ccd0..4c683465694071 100644 --- a/lib/compat/wordpress-6.8/rest-api.php +++ b/lib/compat/wordpress-6.8/rest-api.php @@ -10,17 +10,6 @@ die( 'Silence is golden.' ); } -if ( ! function_exists( 'gutenberg_add_post_type_rendering_mode' ) ) { - /** - * Add Block Editor default rendering mode to the post type response. - */ - function gutenberg_add_post_type_rendering_mode() { - $controller = new Gutenberg_REST_Post_Types_Controller_6_8(); - $controller->register_routes(); - } -} -add_action( 'rest_api_init', 'gutenberg_add_post_type_rendering_mode' ); - // When querying terms for a given taxonomy in the REST API, respect the default // query arguments set for that taxonomy upon registration. function gutenberg_respect_taxonomy_default_args_in_rest_api( $args ) { From b7eca3ec5ddfe09e1f3b6dfe7a6831f5fa3ce737 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 12 Dec 2024 13:59:39 +0400 Subject: [PATCH 2/4] Update backport changelog file --- backport-changelog/6.8/7129.md | 1 + 1 file changed, 1 insertion(+) diff --git a/backport-changelog/6.8/7129.md b/backport-changelog/6.8/7129.md index 301f1abc45d0d7..142a4321359200 100644 --- a/backport-changelog/6.8/7129.md +++ b/backport-changelog/6.8/7129.md @@ -2,3 +2,4 @@ https://github.com/WordPress/wordpress-develop/pull/7129 * https://github.com/WordPress/gutenberg/pull/62304 * https://github.com/WordPress/gutenberg/pull/67879 +* https://github.com/WordPress/gutenberg/pull/67867 From f67a33c0f77a75fa2096571279b7e5cfddd57da5 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 12 Dec 2024 15:43:32 +0400 Subject: [PATCH 3/4] WPCS and fallback fixes --- ...tenberg-rest-post-types-controller-6-8.php | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/lib/compat/wordpress-6.8/class-gutenberg-rest-post-types-controller-6-8.php b/lib/compat/wordpress-6.8/class-gutenberg-rest-post-types-controller-6-8.php index 1309472b800246..003bbe0ef9c48d 100644 --- a/lib/compat/wordpress-6.8/class-gutenberg-rest-post-types-controller-6-8.php +++ b/lib/compat/wordpress-6.8/class-gutenberg-rest-post-types-controller-6-8.php @@ -13,29 +13,33 @@ * @return void */ function gutenberg_editor_rendering_mode_field() { - register_rest_field( 'type', 'default_rendering_mode', array( - 'get_callback' => static function( $object ) { - $post_type_object = get_post_type_object( $object['slug'] ); + register_rest_field( + 'type', + 'default_rendering_mode', + array( + 'get_callback' => static function ( $object ) { + $post_type_object = get_post_type_object( $object['slug'] ); - // Property will only exist if the post type supports the block editor. - if ( ! $post_type_object || ! isset( $post_type_object->default_rendering_mode ) ) { - return ''; - } + // Property will only exist if the post type supports the block editor. + if ( ! $post_type_object || ! isset( $post_type_object->default_rendering_mode ) ) { + return 'post-only'; + } - // Validate the filtered rendering mode. - if ( ! in_array( $post_type_object->default_rendering_mode, gutenberg_post_type_rendering_modes(), true ) ) { - return 'post-only'; - } + // Validate the filtered rendering mode. + if ( ! in_array( $post_type_object->default_rendering_mode, gutenberg_post_type_rendering_modes(), true ) ) { + return 'post-only'; + } - return $post_type_object->default_rendering_mode; - }, - 'schema' => array( - 'description' => __( 'The rendering mode for the editor.', 'gutenberg' ), - 'type' => 'string', - 'enum' => array( 'post-only', 'template-locked' ), - 'context' => array( 'edit' ), - 'readonly' => true, - ), - ) ); + return $post_type_object->default_rendering_mode; + }, + 'schema' => array( + 'description' => __( 'The rendering mode for the editor.', 'gutenberg' ), + 'type' => 'string', + 'enum' => array( 'post-only', 'template-locked' ), + 'context' => array( 'edit' ), + 'readonly' => true, + ), + ) + ); } add_action( 'rest_api_init', 'gutenberg_editor_rendering_mode_field' ); From 547a7cc0442a0418ca19651fc8a85352b84be6f5 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 12 Dec 2024 15:48:14 +0400 Subject: [PATCH 4/4] Fallback on falsy values --- .../editor/src/components/provider/index.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/editor/src/components/provider/index.js b/packages/editor/src/components/provider/index.js index 4b8a3b6b23c251..d28e0a3e2e7016 100644 --- a/packages/editor/src/components/provider/index.js +++ b/packages/editor/src/components/provider/index.js @@ -179,23 +179,25 @@ export const ExperimentalEditorProvider = withRegistryProvider( getRenderingMode, __unstableIsEditorReady, } = select( editorStore ); - const { getEntitiesConfig } = select( coreStore ); + const { + getEntitiesConfig, + getPostType, + hasFinishedResolution, + } = select( coreStore ); - const postTypeObject = select( coreStore ).getPostType( - post.type + const postTypeObject = getPostType( post.type ); + const _hasLoadedPostObject = hasFinishedResolution( + 'getPostType', + [ post.type ] ); - const _hasLoadedPostObject = select( - coreStore - ).hasFinishedResolution( 'getPostType', [ post.type ] ); - return { hasLoadedPostObject: _hasLoadedPostObject, editorSettings: getEditorSettings(), isReady: __unstableIsEditorReady(), mode: getRenderingMode(), defaultMode: - postTypeObject?.default_rendering_mode ?? 'post-only', + postTypeObject?.default_rendering_mode || 'post-only', selection: getEditorSelection(), postTypeEntities: post.type === 'wp_template'