From b74ae218010ed2046ddf0ec3d773435c825a3066 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 23 Aug 2024 14:27:30 +0200 Subject: [PATCH] Fix hacky block editor settings filter --- .../block-editor-settings-mobile.php | 25 ++++++------------- ...-rest-block-editor-settings-controller.php | 5 ++++ .../block-editor-settings-mobile-test.php | 8 +----- 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/lib/experimental/block-editor-settings-mobile.php b/lib/experimental/block-editor-settings-mobile.php index 91e3694c199f83..62c03db222afce 100644 --- a/lib/experimental/block-editor-settings-mobile.php +++ b/lib/experimental/block-editor-settings-mobile.php @@ -16,24 +16,15 @@ * @return array New block editor settings. */ function gutenberg_get_block_editor_settings_mobile( $settings ) { - if ( - defined( 'REST_REQUEST' ) && - REST_REQUEST && - isset( $_GET['context'] ) && - 'mobile' === $_GET['context'] - ) { - if ( wp_theme_has_theme_json() ) { - $settings['__experimentalStyles'] = gutenberg_get_global_styles(); - } - - // To tell mobile that the site uses quote v2 (inner blocks). - // See https://github.com/WordPress/gutenberg/pull/25892. - $settings['__experimentalEnableQuoteBlockV2'] = true; - // To tell mobile that the site uses list v2 (inner blocks). - $settings['__experimentalEnableListBlockV2'] = true; + if ( wp_theme_has_theme_json() ) { + $settings['__experimentalStyles'] = gutenberg_get_global_styles(); } + // To tell mobile that the site uses quote v2 (inner blocks). + // See https://github.com/WordPress/gutenberg/pull/25892. + $settings['__experimentalEnableQuoteBlockV2'] = true; + // To tell mobile that the site uses list v2 (inner blocks). + $settings['__experimentalEnableListBlockV2'] = true; + return $settings; } - -add_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings_mobile', PHP_INT_MAX ); diff --git a/lib/experimental/class-wp-rest-block-editor-settings-controller.php b/lib/experimental/class-wp-rest-block-editor-settings-controller.php index 2c4bf29bc21a73..e15eb0a92e9164 100644 --- a/lib/experimental/class-wp-rest-block-editor-settings-controller.php +++ b/lib/experimental/class-wp-rest-block-editor-settings-controller.php @@ -93,9 +93,14 @@ public function get_items( $request ) { // phpcs:ignore VariableAnalysis.CodeAna break; } + + add_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings_mobile', PHP_INT_MAX ); + $editor_context = new WP_Block_Editor_Context( array( 'name' => $editor_context_name ) ); $settings = get_block_editor_settings( array(), $editor_context ); + remove_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings_mobile', PHP_INT_MAX ); + return rest_ensure_response( $settings ); } diff --git a/phpunit/experimental/block-editor-settings-mobile-test.php b/phpunit/experimental/block-editor-settings-mobile-test.php index 907b750d8c8638..4257543fbbcc90 100644 --- a/phpunit/experimental/block-editor-settings-mobile-test.php +++ b/phpunit/experimental/block-editor-settings-mobile-test.php @@ -10,11 +10,6 @@ * * @covers WP_REST_Block_Editor_Settings_Controller */ - -if ( ! defined( 'REST_REQUEST' ) ) { - define( 'REST_REQUEST', true ); -} - class Gutenberg_REST_Block_Editor_Settings_Controller_Test extends WP_Test_REST_Controller_Testcase { /** * @var int @@ -56,8 +51,7 @@ public function test_register_routes() { public function test_get_items() { wp_set_current_user( self::$admin_id ); $request = new WP_REST_Request( 'GET', '/wp-block-editor/v1/settings' ); - // Set context for mobile settings. - $_GET['context'] = 'mobile'; + $request->set_param( 'context', 'mobile' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data();