PluginProbe
Gutenberg / 22.3.0
Gutenberg v22.3.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / experimental / disable-tinymce.php

disable-tinymce.php in Gutenberg 22.3.0, at lib/experimental/disable-tinymce.php

107 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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