From be2cc761b3a3d4e93dccc8f74a5ea62a1e99dfdc Mon Sep 17 00:00:00 2001 From: kagg-design Date: Mon, 12 Feb 2024 14:47:21 +0300 Subject: [PATCH] Improve detection of the Gutenberg editor. --- src/php/Main.php | 45 ++++++++----------------- tests/unit/MainTest.php | 73 ++++++++++++++++++++++++++++++----------- 2 files changed, 68 insertions(+), 50 deletions(-) diff --git a/src/php/Main.php b/src/php/Main.php index 9a06e84..62b7a53 100644 --- a/src/php/Main.php +++ b/src/php/Main.php @@ -536,48 +536,31 @@ public function transliterate( string $str ): string { } /** - * Check if Classic Editor plugin is active. - * - * @link https://kagg.eu/how-to-catch-gutenberg/ + * Check if the Block Editor is active. + * Must only be used after plugins_loaded action is fired. * * @return bool + * @noinspection PhpUndefinedFunctionInspection */ - private function is_classic_editor_plugin_active(): bool { + private function is_gutenberg_editor_active(): bool { + // Gutenberg plugin is installed and activated. + // This filter was removed in WP 5.5. + if ( has_filter( 'replace_editor', 'gutenberg_init' ) ) { + return true; + } + // @codeCoverageIgnoreStart if ( ! function_exists( 'is_plugin_active' ) ) { include_once ABSPATH . 'wp-admin/includes/plugin.php'; } - // @codeCoverageIgnoreEnd - return is_plugin_active( 'classic-editor/classic-editor.php' ); - } - - /** - * Check if Block Editor is active. - * Must only be used after plugins_loaded action is fired. - * - * @link https://kagg.eu/how-to-catch-gutenberg/ - * - * @return bool - */ - private function is_gutenberg_editor_active(): bool { - - // Gutenberg plugin is installed and activated. - $gutenberg = ! ( false === has_filter( 'replace_editor', 'gutenberg_init' ) ); - - // Block editor since 5.0. - $block_editor = version_compare( $GLOBALS['wp_version'], '5.0-beta', '>' ); - - if ( ! $gutenberg && ! $block_editor ) { - return false; + if ( is_plugin_active( 'classic-editor/classic-editor.php' ) ) { + return in_array( get_option( 'classic-editor-replace' ), [ 'no-replace', 'block' ], true ); } - if ( $this->is_classic_editor_plugin_active() ) { - $editor_option = get_option( 'classic-editor-replace' ); - $block_editor_active = [ 'no-replace', 'block' ]; - - return in_array( $editor_option, $block_editor_active, true ); + if ( is_plugin_active( 'disable-gutenberg/disable-gutenberg.php' ) ) { + return ! disable_gutenberg(); } return true; diff --git a/tests/unit/MainTest.php b/tests/unit/MainTest.php index 21d17bd..ff753f4 100644 --- a/tests/unit/MainTest.php +++ b/tests/unit/MainTest.php @@ -53,7 +53,7 @@ class MainTest extends CyrToLatTestCase { public function tearDown(): void { // phpcs:disable WordPress.Security.NonceVerification.Missing // phpcs:disable WordPress.Security.NonceVerification.Recommended - unset( $GLOBALS['wp_version'], $GLOBALS['wpdb'], $GLOBALS['current_screen'], $GLOBALS['product'], $_POST, $_GET ); + unset( $GLOBALS['wpdb'], $GLOBALS['current_screen'], $GLOBALS['product'], $_POST, $_GET ); // phpcs:enable WordPress.Security.NonceVerification.Recommended // phpcs:enable WordPress.Security.NonceVerification.Missing } @@ -1135,8 +1135,6 @@ public static function dp_test_min_suffix(): array { * Test that sanitize_post_name() does nothing if no Block/Gutenberg editor is active */ public function test_sanitize_post_name_without_gutenberg() { - $subject = Mockery::mock( Main::class )->makePartial()->shouldAllowMockingProtectedMethods(); - $data = [ 'something' ]; WP_Mock::userFunction( @@ -1146,13 +1144,6 @@ public function test_sanitize_post_name_without_gutenberg() { 'return' => false, ] ); - - // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited - $GLOBALS['wp_version'] = '4.9'; - self::assertSame( $data, $subject->sanitize_post_name( $data ) ); - - FunctionMocker::replace( 'function_exists', true ); - WP_Mock::userFunction( 'is_plugin_active', [ @@ -1161,7 +1152,6 @@ public function test_sanitize_post_name_without_gutenberg() { 'return' => true, ] ); - WP_Mock::userFunction( 'get_option', [ @@ -1171,8 +1161,45 @@ public function test_sanitize_post_name_without_gutenberg() { ] ); - // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited - $GLOBALS['wp_version'] = '5.0'; + $subject = Mockery::mock( Main::class )->makePartial()->shouldAllowMockingProtectedMethods(); + + self::assertSame( $data, $subject->sanitize_post_name( $data ) ); + } + + /** + * Test that sanitize_post_name() does nothing if Disable Gutenberg plugin is active + */ + public function test_sanitize_post_name_with_disable_gutenberg_plugin() { + $data = [ 'something' ]; + + WP_Mock::userFunction( + 'has_filter', + [ + 'args' => [ 'replace_editor', 'gutenberg_init' ], + 'return' => false, + ] + ); + WP_Mock::userFunction( + 'is_plugin_active', + [ + 'times' => 1, + 'args' => [ 'classic-editor/classic-editor.php' ], + 'return' => false, + ] + ); + WP_Mock::userFunction( + 'is_plugin_active', + [ + 'times' => 1, + 'args' => [ 'disable-gutenberg/disable-gutenberg.php' ], + 'return' => true, + ] + ); + + $subject = Mockery::mock( Main::class )->makePartial()->shouldAllowMockingProtectedMethods(); + + FunctionMocker::replace( 'disable_gutenberg', true ); + self::assertSame( $data, $subject->sanitize_post_name( $data ) ); } @@ -1190,9 +1217,6 @@ public function test_sanitize_post_name_not_post_edit_screen() { ] ); - // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited - $GLOBALS['wp_version'] = '5.0'; - $subject = Mockery::mock( Main::class )->makePartial()->shouldAllowMockingProtectedMethods(); FunctionMocker::replace( 'function_exists', true ); @@ -1203,6 +1227,13 @@ public function test_sanitize_post_name_not_post_edit_screen() { 'return' => false, ] ); + WP_Mock::userFunction( + 'is_plugin_active', + [ + 'args' => [ 'disable-gutenberg/disable-gutenberg.php' ], + 'return' => false, + ] + ); $current_screen = Mockery::mock( WP_Screen::class ); $current_screen->base = 'not post'; @@ -1226,9 +1257,6 @@ public function test_sanitize_post_name_not_post_edit_screen() { */ public function test_sanitize_post_name( array $data, array $expected ) { - // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited - $GLOBALS['wp_version'] = '5.0'; - $subject = Mockery::mock( Main::class )->makePartial()->shouldAllowMockingProtectedMethods(); FunctionMocker::replace( 'function_exists', true ); @@ -1239,6 +1267,13 @@ public function test_sanitize_post_name( array $data, array $expected ) { 'return' => false, ] ); + WP_Mock::userFunction( + 'is_plugin_active', + [ + 'args' => [ 'disable-gutenberg/disable-gutenberg.php' ], + 'return' => false, + ] + ); $current_screen = Mockery::mock( WP_Screen::class ); $current_screen->base = 'post';