| 1 |
<?php |
| 2 |
/** |
| 3 |
* Experiment to disable TinyMCE and the Classic block. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
// add_action( 'admin_footer', 'gutenberg_test_tinymce_access' ); // Uncomment the following line to force an external TinyMCE usage. |
| 9 |
|
| 10 |
/** |
| 11 |
* Render a variable that we'll use to declare that the editor will need the classic block. |
| 12 |
*/ |
| 13 |
function gutenberg_declare_classic_block_necessary() { |
| 14 |
if ( ! gutenberg_post_being_edited_requires_classic_block() ) { |
| 15 |
return; |
| 16 |
} |
| 17 |
echo '<script type="text/javascript">window.wp.needsClassicBlock = true;</script>'; |
| 18 |
} |
| 19 |
add_action( 'admin_print_footer_scripts', 'gutenberg_declare_classic_block_necessary', 20 ); |
| 20 |
|
| 21 |
// If user has already requested TinyMCE, we're ending the experiment. |
| 22 |
if ( ! empty( $_GET['requiresTinymce'] ) || gutenberg_post_being_edited_requires_classic_block() ) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
/** |
| 28 |
* Disable TinyMCE by introducing a placeholder `_WP_Editors` class. |
| 29 |
*/ |
| 30 |
function gutenberg_disable_tinymce() { |
| 31 |
require __DIR__ . '/class--wp-editors.php'; |
| 32 |
} |
| 33 |
|
| 34 |
add_action( 'admin_init', 'gutenberg_disable_tinymce' ); |
| 35 |
|
| 36 |
/** |
| 37 |
* Enqueue TinyMCE proxy script. |
| 38 |
* Detects TinyMCE usage and sets the `requiresTinymce` query argument to stop disabling TinyMCE loading. |
| 39 |
*/ |
| 40 |
function gutenberg_enqueue_tinymce_proxy() { |
| 41 |
wp_enqueue_script( 'gutenberg-tinymce-proxy', plugins_url( 'assets/tinymce-proxy.js', __FILE__ ) ); |
| 42 |
} |
| 43 |
|
| 44 |
add_action( 'admin_enqueue_scripts', 'gutenberg_enqueue_tinymce_proxy' ); |
| 45 |
|
| 46 |
/** |
| 47 |
* Dequeue the `mce-view` script as it was only necessary for the Classic block. |
| 48 |
*/ |
| 49 |
function gutenberg_wp_enqueue_media() { |
| 50 |
wp_dequeue_script( 'mce-view' ); |
| 51 |
} |
| 52 |
|
| 53 |
add_action( 'wp_enqueue_media', 'gutenberg_wp_enqueue_media' ); |
| 54 |
|
| 55 |
/** |
| 56 |
* Example TinyMCE usage used for testing. |
| 57 |
* Uncomment line 8 in this file to enable. |
| 58 |
*/ |
| 59 |
function gutenberg_test_tinymce_access() { |
| 60 |
echo '<script type="text/javascript">const a = window.tinymce.$;</script>'; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Whether the current editor contains a classic block instance. |
| 65 |
* |
| 66 |
* @return bool True if the editor contains a classic block, false otherwise. |
| 67 |
*/ |
| 68 |
function gutenberg_post_being_edited_requires_classic_block() { |
| 69 |
if ( ! is_admin() ) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
// Continue only if we're in the post editor. |
| 74 |
if ( empty( $_GET['post'] ) || empty( $_GET['action'] ) || 'edit' !== $_GET['action'] ) { |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
// Bail if for some reason the post isn't found. |
| 79 |
$current_post = get_post( absint( $_GET['post'] ) ); |
| 80 |
if ( ! $current_post ) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
// Check if block editor is disabled by "Classic Editor" or another plugin. |
| 85 |
if ( |
| 86 |
function_exists( 'use_block_editor_for_post_type' ) && |
| 87 |
! use_block_editor_for_post_type( $current_post->post_type ) |
| 88 |
) { |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
$content = $current_post->post_content; |
| 93 |
if ( empty( $content ) ) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
$parsed_blocks = parse_blocks( $content ); |
| 98 |
foreach ( $parsed_blocks as $block ) { |
| 99 |
$is_freeform_block = empty( $block['blockName'] ) || 'core/freeform' === $block['blockName']; |
| 100 |
if ( $is_freeform_block && strlen( trim( $block['innerHTML'] ) ) > 0 ) { |
| 101 |
return true; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
return false; |
| 106 |
} |
| 107 |
|