|
9 | 9 | * Render a variable that we'll use to declare that the editor will need the classic block. |
10 | 10 | */ |
11 | 11 | function gutenberg_declare_classic_block_necessary() { |
12 | | - if ( ! gutenberg_post_being_edited_requires_classic_block() ) { |
| 12 | + if ( ! gutenberg_classic_block_supports_inserter() ) { |
13 | 13 | return; |
14 | 14 | } |
15 | 15 | echo '<script type="text/javascript">window.wp.needsClassicBlock = true;</script>'; |
16 | 16 | } |
17 | 17 | add_action( 'admin_print_footer_scripts', 'gutenberg_declare_classic_block_necessary', 20 ); |
18 | 18 |
|
19 | 19 | /** |
20 | | - * Whether the current editor contains a classic block instance. |
| 20 | + * Whether the Classic block should be available in the inserter. |
21 | 21 | * |
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. |
23 | 23 | */ |
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; |
41 | 26 | 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'] |
44 | 31 | ) { |
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'] ) ); |
59 | 33 | } |
60 | 34 |
|
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 ); |
62 | 44 | } |
0 commit comments