Skip to content

Commit e1e460a

Browse files
authored
Disable Classic block: Control inserter support via filter (WordPress#77845)
* Disable Classic block: Control inserter support via filter * Fix PHPCS short ternary error * Always declare editor dependency for wp-block-library
1 parent e4fcfec commit e1e460a

2 files changed

Lines changed: 22 additions & 48 deletions

File tree

lib/client-assets.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,15 @@ function gutenberg_override_translation_file( $file, $handle ) {
8787
add_filter( 'load_script_translation_file', 'gutenberg_override_translation_file', 10, 2 );
8888

8989
/**
90-
* Handle special case dependencies for wp-block-library that depend on runtime conditions.
91-
*
92-
* This adds the 'editor' dependency conditionally based on experiments and classic block requirements.
93-
* All other script registrations are handled by the auto-generated build/scripts.php file.
90+
* Adds the 'editor' dependency to wp-block-library, required by the Classic block.
9491
*
9592
* @param WP_Scripts $scripts WP_Scripts instance.
9693
*/
9794
function gutenberg_register_block_library_script_special_case( $scripts ) {
9895
$handle = 'wp-block-library';
9996
$script = $scripts->query( $handle, 'registered' );
100-
if (
101-
! gutenberg_is_experiment_enabled( 'gutenberg-no-tinymce' ) ||
102-
gutenberg_post_being_edited_requires_classic_block()
103-
) {
104-
if ( ! in_array( 'editor', $script->deps, true ) ) {
105-
$script->deps[] = 'editor';
106-
}
97+
if ( ! in_array( 'editor', $script->deps, true ) ) {
98+
$script->deps[] = 'editor';
10799
}
108100
}
109101
add_action( 'wp_default_scripts', 'gutenberg_register_block_library_script_special_case', 11 );

lib/experimental/disable-tinymce.php

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,36 @@
99
* Render a variable that we'll use to declare that the editor will need the classic block.
1010
*/
1111
function gutenberg_declare_classic_block_necessary() {
12-
if ( ! gutenberg_post_being_edited_requires_classic_block() ) {
12+
if ( ! gutenberg_classic_block_supports_inserter() ) {
1313
return;
1414
}
1515
echo '<script type="text/javascript">window.wp.needsClassicBlock = true;</script>';
1616
}
1717
add_action( 'admin_print_footer_scripts', 'gutenberg_declare_classic_block_necessary', 20 );
1818

1919
/**
20-
* Whether the current editor contains a classic block instance.
20+
* Whether the Classic block should be available in the inserter.
2121
*
22-
* @return bool True if the editor contains a classic block, false otherwise.
22+
* @return bool True if the Classic block should be in the inserter.
2323
*/
24-
function gutenberg_post_being_edited_requires_classic_block() {
25-
if ( ! is_admin() ) {
26-
return false;
27-
}
28-
29-
// Continue only if we're in the post editor.
30-
if ( empty( $_GET['post'] ) || empty( $_GET['action'] ) || 'edit' !== $_GET['action'] ) {
31-
return false;
32-
}
33-
34-
// Bail if for some reason the post isn't found.
35-
$current_post = get_post( absint( $_GET['post'] ) );
36-
if ( ! $current_post ) {
37-
return false;
38-
}
39-
40-
// Check if block editor is disabled by "Classic Editor" or another plugin.
24+
function gutenberg_classic_block_supports_inserter() {
25+
$post = null;
4126
if (
42-
function_exists( 'use_block_editor_for_post_type' ) &&
43-
! use_block_editor_for_post_type( $current_post->post_type )
27+
is_admin() &&
28+
! empty( $_GET['post'] ) &&
29+
! empty( $_GET['action'] ) &&
30+
'edit' === $_GET['action']
4431
) {
45-
return true;
46-
}
47-
48-
$content = $current_post->post_content;
49-
if ( empty( $content ) ) {
50-
return false;
51-
}
52-
53-
$parsed_blocks = parse_blocks( $content );
54-
foreach ( $parsed_blocks as $block ) {
55-
$is_freeform_block = empty( $block['blockName'] ) || 'core/freeform' === $block['blockName'];
56-
if ( $is_freeform_block && strlen( trim( $block['innerHTML'] ) ) > 0 ) {
57-
return true;
58-
}
32+
$post = get_post( absint( $_GET['post'] ) );
5933
}
6034

61-
return false;
35+
/**
36+
* Filters whether the Classic block should be available in the inserter.
37+
*
38+
* Defaults to false. Use this filter to opt in (globally or per post).
39+
*
40+
* @param bool $supports_inserter Whether the Classic block is available in the inserter.
41+
* @param WP_Post|null $post The post being edited, or null if not in the post editor.
42+
*/
43+
return (bool) apply_filters( 'gutenberg_classic_block_supports_inserter', false, $post );
6244
}

0 commit comments

Comments
 (0)