Skip to content

Commit

Permalink
Improve detection of the Gutenberg editor.
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Feb 12, 2024
1 parent 837a35f commit be2cc76
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 50 deletions.
45 changes: 14 additions & 31 deletions src/php/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
73 changes: 54 additions & 19 deletions tests/unit/MainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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(
Expand All @@ -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',
[
Expand All @@ -1161,7 +1152,6 @@ public function test_sanitize_post_name_without_gutenberg() {
'return' => true,
]
);

WP_Mock::userFunction(
'get_option',
[
Expand All @@ -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 ) );
}

Expand All @@ -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 );

Expand All @@ -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';
Expand All @@ -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 );

Expand All @@ -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';
Expand Down

0 comments on commit be2cc76

Please sign in to comment.