From 8479b4df7560a87136dbf8257d43653d2ce92b7c Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 12 Dec 2024 13:38:37 +0400 Subject: [PATCH] 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 da0489210e21f..1309472b80024 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 b94e42d5f2ccd..4c68346569407 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 ) {