From 12f86473a58de4e1bdc9a8ed339a37b53dcae5ef Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Wed, 3 May 2023 17:49:31 +0300 Subject: [PATCH 01/31] Add experiment setting field --- lib/experiments-page.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/experiments-page.php b/lib/experiments-page.php index a2df41908aa257..de1d3dbce71a14 100644 --- a/lib/experiments-page.php +++ b/lib/experiments-page.php @@ -89,6 +89,18 @@ function gutenberg_initialize_experiments_settings() { ) ); + add_settings_field( + 'gutenberg-no-tinymce', + __( 'Disable TinyMCE and Classic block', 'gutenberg' ), + 'gutenberg_display_experiment_field', + 'gutenberg-experiments', + 'gutenberg_experiments_section', + array( + 'label' => __( 'Disable TinyMCE and Classic block', 'gutenberg' ), + 'id' => 'gutenberg-no-tinymce', + ) + ); + register_setting( 'gutenberg-experiments', 'gutenberg-experiments' From fc802238902000e4c5e21d93c26fa2186e715c3d Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Wed, 3 May 2023 17:49:52 +0300 Subject: [PATCH 02/31] Set a global when experiment enabled --- lib/experimental/editor-settings.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/experimental/editor-settings.php b/lib/experimental/editor-settings.php index eaa314946ab9a3..27c55498d37c82 100644 --- a/lib/experimental/editor-settings.php +++ b/lib/experimental/editor-settings.php @@ -90,6 +90,9 @@ function gutenberg_enable_experiments() { wp_add_inline_script( 'wp-block-editor', 'window.__experimentalInteractivityAPI = true', 'before' ); } + if ( $gutenberg_experiments && array_key_exists( 'gutenberg-no-tinymce', $gutenberg_experiments ) ) { + wp_add_inline_script( 'wp-block-library', 'window.__experimentalDisableTinymce = true', 'before' ); + } } add_action( 'admin_init', 'gutenberg_enable_experiments' ); From 6183e670e5f46b68c8263dd34bde82cde524969b Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Wed, 3 May 2023 17:50:12 +0300 Subject: [PATCH 03/31] Load Classic block only when experiment is not enabled --- packages/block-library/src/index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index 2b4a0f2fb95f2a..5e65ad578f6f3d 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -142,7 +142,6 @@ const getAllBlocks = () => { buttons, calendar, categories, - ...( window.wp && window.wp.oldEditor ? [ classic ] : [] ), // Only add the classic block in WP Context. code, column, columns, @@ -229,6 +228,12 @@ const getAllBlocks = () => { queryTitle, postAuthorBiography, ]; + + // Only add the classic block in WP Context + if ( window?.wp?.oldEditor && ! window?.__experimentalDisableTinymce ) { + blocks.push( classic ); + } + return blocks.filter( Boolean ); }; From 4ed42c59b51b89a8308449933a6508b09400921f Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Fri, 5 May 2023 17:40:26 +0300 Subject: [PATCH 04/31] Do not enqueue any TinyMCE assets if experiment is enabled --- lib/experimental/disable-tinymce.php | 8 ++++++++ lib/experimental/wp-editors-class-placeholder.php | 4 ++++ lib/load.php | 2 ++ 3 files changed, 14 insertions(+) create mode 100644 lib/experimental/disable-tinymce.php create mode 100644 lib/experimental/wp-editors-class-placeholder.php diff --git a/lib/experimental/disable-tinymce.php b/lib/experimental/disable-tinymce.php new file mode 100644 index 00000000000000..dbda3fc7a38def --- /dev/null +++ b/lib/experimental/disable-tinymce.php @@ -0,0 +1,8 @@ + Date: Mon, 8 May 2023 13:29:20 +0300 Subject: [PATCH 05/31] Do not enqueue editor if experiment enabled --- lib/client-assets.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 9757e4b7ff24a8..d2b563522f5b2d 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -203,7 +203,13 @@ function gutenberg_register_packages_scripts( $scripts ) { // Add dependencies that cannot be detected and generated by build tools. switch ( $handle ) { case 'wp-block-library': - array_push( $dependencies, 'editor' ); + $gutenberg_experiments = get_option( 'gutenberg-experiments' ); + if ( + ! $gutenberg_experiments || + ! array_key_exists( 'gutenberg-no-tinymce', $gutenberg_experiments ) + ) { + array_push( $dependencies, 'editor' ); + } break; case 'wp-edit-post': From b2f58f773bdd0e459dfd535ff632cd0486167f89 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Mon, 8 May 2023 13:49:41 +0300 Subject: [PATCH 06/31] Set and utilize a cookie when tinymce is requested --- lib/client-assets.php | 3 ++- lib/experimental/assets/tinymce-proxy.js | 14 ++++++++++++++ lib/experimental/disable-tinymce.php | 12 +++++++++++- lib/experimental/wp-editors-class-placeholder.php | 2 +- packages/block-library/src/index.js | 10 +++++++++- 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 lib/experimental/assets/tinymce-proxy.js diff --git a/lib/client-assets.php b/lib/client-assets.php index d2b563522f5b2d..35658d282cd856 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -206,7 +206,8 @@ function gutenberg_register_packages_scripts( $scripts ) { $gutenberg_experiments = get_option( 'gutenberg-experiments' ); if ( ! $gutenberg_experiments || - ! array_key_exists( 'gutenberg-no-tinymce', $gutenberg_experiments ) + ! array_key_exists( 'gutenberg-no-tinymce', $gutenberg_experiments ) || + $_COOKIE[ 'requiresTinymce' ] ) { array_push( $dependencies, 'editor' ); } diff --git a/lib/experimental/assets/tinymce-proxy.js b/lib/experimental/assets/tinymce-proxy.js new file mode 100644 index 00000000000000..e8044b8abafd6c --- /dev/null +++ b/lib/experimental/assets/tinymce-proxy.js @@ -0,0 +1,14 @@ +async function markAsRequiringTinymce() { + const expiration = new Date( 'Dec 31, 2099 23:59:59' ); + document.cookie = `requiresTinymce=1; expires=${ expiration }; path=/`; + window.location.reload(); +} + +window.tinymce = new Proxy( + {}, + { + get: markAsRequiringTinymce, + set: markAsRequiringTinymce, + apply: markAsRequiringTinymce, + } +); diff --git a/lib/experimental/disable-tinymce.php b/lib/experimental/disable-tinymce.php index dbda3fc7a38def..74e676f6acdf65 100644 --- a/lib/experimental/disable-tinymce.php +++ b/lib/experimental/disable-tinymce.php @@ -1,8 +1,18 @@ { ]; // Only add the classic block in WP Context - if ( window?.wp?.oldEditor && ! window?.__experimentalDisableTinymce ) { + // and when TinyMCE experiment is disabled or cookie to require TinyMCE is set + if ( + window?.wp?.oldEditor && + ( ! window?.__experimentalDisableTinymce || + ( '; ' + document.cookie ) + .split( '; requiresTinymce=' ) + .pop() + .split( ';' )[ 0 ] ) + ) { blocks.push( classic ); } From fa0cea3ba7b3c1093bc3c554cc18034b5a84ece2 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Mon, 8 May 2023 13:51:25 +0300 Subject: [PATCH 07/31] Safeer cookie access on the server side --- lib/client-assets.php | 2 +- lib/experimental/disable-tinymce.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 35658d282cd856..935b79684ec6da 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -207,7 +207,7 @@ function gutenberg_register_packages_scripts( $scripts ) { if ( ! $gutenberg_experiments || ! array_key_exists( 'gutenberg-no-tinymce', $gutenberg_experiments ) || - $_COOKIE[ 'requiresTinymce' ] + isset( $_COOKIE[ 'requiresTinymce' ] ) ) { array_push( $dependencies, 'editor' ); } diff --git a/lib/experimental/disable-tinymce.php b/lib/experimental/disable-tinymce.php index 74e676f6acdf65..9cb2fce41ffd19 100644 --- a/lib/experimental/disable-tinymce.php +++ b/lib/experimental/disable-tinymce.php @@ -1,6 +1,6 @@ Date: Mon, 8 May 2023 14:00:46 +0300 Subject: [PATCH 08/31] Add a script to force TinyMCE usage for testing --- lib/experimental/disable-tinymce.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/experimental/disable-tinymce.php b/lib/experimental/disable-tinymce.php index 9cb2fce41ffd19..6e08e58f9e8d23 100644 --- a/lib/experimental/disable-tinymce.php +++ b/lib/experimental/disable-tinymce.php @@ -16,3 +16,9 @@ function gutenberg_disable_tinymce() { function gutenberg_enqueue_tinymce_proxy() { wp_enqueue_script( 'gutenberg-tinymce-proxy', plugins_url( 'assets/tinymce-proxy.js' , __FILE__ ) ); } + +// Uncomment the following line to force an external TinyMCE usage +//add_action( 'admin_footer', 'gutenberg_test_tinymce_access' ); +function gutenberg_test_tinymce_access() { + echo ''; +} From f52d567a18cd51c386bee6a17b11b5d06175e8fa Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Mon, 8 May 2023 14:16:17 +0300 Subject: [PATCH 09/31] Move experiment toggle to the higheest priority --- lib/experimental/disable-tinymce.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/experimental/disable-tinymce.php b/lib/experimental/disable-tinymce.php index 6e08e58f9e8d23..b6219a969561ba 100644 --- a/lib/experimental/disable-tinymce.php +++ b/lib/experimental/disable-tinymce.php @@ -1,15 +1,17 @@ Date: Mon, 8 May 2023 14:22:56 +0300 Subject: [PATCH 10/31] Add PHP comments and fix coding standards --- lib/experimental/disable-tinymce.php | 32 ++++++++++++++++--- .../wp-editors-class-placeholder.php | 7 ++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/experimental/disable-tinymce.php b/lib/experimental/disable-tinymce.php index b6219a969561ba..9891cd8aca56d5 100644 --- a/lib/experimental/disable-tinymce.php +++ b/lib/experimental/disable-tinymce.php @@ -1,26 +1,48 @@ const a = window.tinymce.$;'; } + +// Uncomment the following line to force an external TinyMCE usage. +// add_action( 'admin_footer', 'gutenberg_test_tinymce_access' ); \ No newline at end of file diff --git a/lib/experimental/wp-editors-class-placeholder.php b/lib/experimental/wp-editors-class-placeholder.php index b4d1b06e8af122..0f9e5244039ac7 100644 --- a/lib/experimental/wp-editors-class-placeholder.php +++ b/lib/experimental/wp-editors-class-placeholder.php @@ -1,4 +1,11 @@ Date: Mon, 8 May 2023 14:23:51 +0300 Subject: [PATCH 11/31] Rename class file --- .../{wp-editors-class-placeholder.php => class--wp-editors.php} | 0 lib/experimental/disable-tinymce.php | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename lib/experimental/{wp-editors-class-placeholder.php => class--wp-editors.php} (100%) diff --git a/lib/experimental/wp-editors-class-placeholder.php b/lib/experimental/class--wp-editors.php similarity index 100% rename from lib/experimental/wp-editors-class-placeholder.php rename to lib/experimental/class--wp-editors.php diff --git a/lib/experimental/disable-tinymce.php b/lib/experimental/disable-tinymce.php index 9891cd8aca56d5..3ed9e24104c3f4 100644 --- a/lib/experimental/disable-tinymce.php +++ b/lib/experimental/disable-tinymce.php @@ -21,7 +21,7 @@ * Disable TinyMCE by introducing a placeholder `_WP_Editors` class. */ function gutenberg_disable_tinymce() { - require __DIR__ . '/wp-editors-class-placeholder.php'; + require __DIR__ . '/class--wp-editors.php'; } add_action( 'admin_init', 'gutenberg_disable_tinymce' ); From 1ee79d8da6cea5ef0ce2366bd5d1e307ca5d2f54 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Mon, 8 May 2023 14:30:58 +0300 Subject: [PATCH 12/31] More CS --- lib/client-assets.php | 2 +- lib/experimental/class--wp-editors.php | 15 +++++++++++++-- lib/experimental/disable-tinymce.php | 9 ++++----- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 935b79684ec6da..1488e8942abe55 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -207,7 +207,7 @@ function gutenberg_register_packages_scripts( $scripts ) { if ( ! $gutenberg_experiments || ! array_key_exists( 'gutenberg-no-tinymce', $gutenberg_experiments ) || - isset( $_COOKIE[ 'requiresTinymce' ] ) + isset( $_COOKIE['requiresTinymce'] ) ) { array_push( $dependencies, 'editor' ); } diff --git a/lib/experimental/class--wp-editors.php b/lib/experimental/class--wp-editors.php index 0f9e5244039ac7..d0a792f9795fd3 100644 --- a/lib/experimental/class--wp-editors.php +++ b/lib/experimental/class--wp-editors.php @@ -1,11 +1,22 @@ const a = window.tinymce.$;'; } -// Uncomment the following line to force an external TinyMCE usage. -// add_action( 'admin_footer', 'gutenberg_test_tinymce_access' ); \ No newline at end of file +// add_action( 'admin_footer', 'gutenberg_test_tinymce_access' ); // Uncomment the following line to force an external TinyMCE usage. From ddd9dfe3692f218b775ce1595fc1ee99032ea690 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 25 May 2023 15:11:52 +0300 Subject: [PATCH 13/31] Use gutenberg_is_experiment_enabled() --- lib/client-assets.php | 4 +--- lib/experimental/disable-tinymce.php | 6 ------ lib/experimental/editor-settings.php | 2 +- lib/load.php | 5 ++++- 4 files changed, 6 insertions(+), 11 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 1488e8942abe55..2b1a04455c4b0b 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -203,10 +203,8 @@ function gutenberg_register_packages_scripts( $scripts ) { // Add dependencies that cannot be detected and generated by build tools. switch ( $handle ) { case 'wp-block-library': - $gutenberg_experiments = get_option( 'gutenberg-experiments' ); if ( - ! $gutenberg_experiments || - ! array_key_exists( 'gutenberg-no-tinymce', $gutenberg_experiments ) || + ! gutenberg_is_experiment_enabled( 'gutenberg-no-tinymce' ) || isset( $_COOKIE['requiresTinymce'] ) ) { array_push( $dependencies, 'editor' ); diff --git a/lib/experimental/disable-tinymce.php b/lib/experimental/disable-tinymce.php index f162dadcb52327..2095f230010fba 100644 --- a/lib/experimental/disable-tinymce.php +++ b/lib/experimental/disable-tinymce.php @@ -5,12 +5,6 @@ * @package gutenberg */ -// Power on/off button controlled by a Gutenberg experiment checkbox. -$gutenberg_experiments = get_option( 'gutenberg-experiments' ); -if ( ! $gutenberg_experiments || ! array_key_exists( 'gutenberg-no-tinymce', $gutenberg_experiments ) ) { - return; -} - // If user has already requested TinyMCE, we're ending the experiment. if ( isset( $_COOKIE['requiresTinymce'] ) ) { return; diff --git a/lib/experimental/editor-settings.php b/lib/experimental/editor-settings.php index 27c55498d37c82..15d964e2deec72 100644 --- a/lib/experimental/editor-settings.php +++ b/lib/experimental/editor-settings.php @@ -90,7 +90,7 @@ function gutenberg_enable_experiments() { wp_add_inline_script( 'wp-block-editor', 'window.__experimentalInteractivityAPI = true', 'before' ); } - if ( $gutenberg_experiments && array_key_exists( 'gutenberg-no-tinymce', $gutenberg_experiments ) ) { + if ( gutenberg_is_experiment_enabled( 'gutenberg-no-tinymce' ) ) { wp_add_inline_script( 'wp-block-library', 'window.__experimentalDisableTinymce = true', 'before' ); } } diff --git a/lib/load.php b/lib/load.php index b27ae1681c70c5..1dc42937bbfb93 100644 --- a/lib/load.php +++ b/lib/load.php @@ -108,7 +108,10 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/experimental/navigation-theme-opt-in.php'; require __DIR__ . '/experimental/kses.php'; require __DIR__ . '/experimental/l10n.php'; -require __DIR__ . '/experimental/disable-tinymce.php'; + +if ( gutenberg_is_experiment_enabled( 'gutenberg-no-tinymce' ) ) { + require __DIR__ . '/experimental/disable-tinymce.php'; +} if ( gutenberg_is_experiment_enabled( 'gutenberg-interactivity-api-core-blocks' ) ) { require __DIR__ . '/experimental/interactivity-api/blocks.php'; From cf08033d13d612306211b392b3d81a247d103b2c Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 25 May 2023 15:16:48 +0300 Subject: [PATCH 14/31] Cleanup lines from rebase --- lib/load.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/load.php b/lib/load.php index 1dc42937bbfb93..6d3638bf969134 100644 --- a/lib/load.php +++ b/lib/load.php @@ -128,7 +128,6 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/experimental/interactivity-api/directives/wp-style.php'; require __DIR__ . '/experimental/interactivity-api/directives/wp-text.php'; - // Fonts API. if ( ! class_exists( 'WP_Fonts' ) ) { // Fonts API files. From e054060b24c79b987b682872df9fdd3492e87207 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 25 May 2023 15:48:16 +0300 Subject: [PATCH 15/31] Backend detection of classic block instances --- lib/client-assets.php | 3 ++- lib/experimental/disable-tinymce.php | 36 ++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 2b1a04455c4b0b..380d116f64805c 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -205,7 +205,8 @@ function gutenberg_register_packages_scripts( $scripts ) { case 'wp-block-library': if ( ! gutenberg_is_experiment_enabled( 'gutenberg-no-tinymce' ) || - isset( $_COOKIE['requiresTinymce'] ) + isset( $_COOKIE['requiresTinymce'] ) || + gutenberg_current_content_contains_classic_block() ) { array_push( $dependencies, 'editor' ); } diff --git a/lib/experimental/disable-tinymce.php b/lib/experimental/disable-tinymce.php index 2095f230010fba..e214f684a88cd5 100644 --- a/lib/experimental/disable-tinymce.php +++ b/lib/experimental/disable-tinymce.php @@ -6,7 +6,7 @@ */ // If user has already requested TinyMCE, we're ending the experiment. -if ( isset( $_COOKIE['requiresTinymce'] ) ) { +if ( isset( $_COOKIE['requiresTinymce'] ) || gutenberg_current_content_contains_classic_block() ) { return; } @@ -38,4 +38,36 @@ function gutenberg_test_tinymce_access() { echo ''; } -// add_action( 'admin_footer', 'gutenberg_test_tinymce_access' ); // Uncomment the following line to force an external TinyMCE usage. +/** + * Whether the current editor contains a classic block instance. + * + * @return bool True if the editor contains a classic block, false otherwse. + */ +function gutenberg_current_content_contains_classic_block() { + if ( ! is_admin() ) { + return false; + } + + // Post editor + if ( ! empty( $_GET['post'] ) && ! empty( $_GET['action'] ) && $_GET['action'] === 'edit' ) { + $content = get_post( intval( $_GET['post'] ) )->post_content; + } + + // @TODO the rest of the editors + + if ( empty( $content ) ) { + return false; + } + + $parsed_blocks = parse_blocks( $content ); + foreach ( $parsed_blocks as $block ) { + if ( empty( $block['blockName'] ) && ! empty( trim( $block['innerHTML'] ) ) ) { + return true; + } + } + + return false; +} + + +add_action( 'admin_footer', 'gutenberg_test_tinymce_access' ); // Uncomment the following line to force an external TinyMCE usage. From 1799b8a5cd4f302dafab90318bcd006713ea5f3e Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 25 May 2023 15:48:45 +0300 Subject: [PATCH 16/31] Comment the cookie testing code again --- lib/experimental/disable-tinymce.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/experimental/disable-tinymce.php b/lib/experimental/disable-tinymce.php index e214f684a88cd5..e0943c09113ded 100644 --- a/lib/experimental/disable-tinymce.php +++ b/lib/experimental/disable-tinymce.php @@ -69,5 +69,4 @@ function gutenberg_current_content_contains_classic_block() { return false; } - -add_action( 'admin_footer', 'gutenberg_test_tinymce_access' ); // Uncomment the following line to force an external TinyMCE usage. +// add_action( 'admin_footer', 'gutenberg_test_tinymce_access' ); // Uncomment the following line to force an external TinyMCE usage. From f06c648d82ba1b18782ff6c885ba9a2f6e90c930 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 25 May 2023 16:09:37 +0300 Subject: [PATCH 17/31] Fix code style --- lib/experimental/disable-tinymce.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/experimental/disable-tinymce.php b/lib/experimental/disable-tinymce.php index e0943c09113ded..8254ac02418131 100644 --- a/lib/experimental/disable-tinymce.php +++ b/lib/experimental/disable-tinymce.php @@ -48,12 +48,12 @@ function gutenberg_current_content_contains_classic_block() { return false; } - // Post editor - if ( ! empty( $_GET['post'] ) && ! empty( $_GET['action'] ) && $_GET['action'] === 'edit' ) { + // Handle the post editor. + if ( ! empty( $_GET['post'] ) && ! empty( $_GET['action'] ) && 'edit' === $_GET['action'] ) { $content = get_post( intval( $_GET['post'] ) )->post_content; } - // @TODO the rest of the editors + // @TODO the rest of the editors. if ( empty( $content ) ) { return false; From b782ce2159951a5240f7c883004e70c0ea83040b Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 25 May 2023 17:03:21 +0300 Subject: [PATCH 18/31] Consider the freeform block, or any raw HTML to be Unknown block --- packages/block-library/src/missing/edit.js | 33 ++++++++++++++++------ 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/missing/edit.js b/packages/block-library/src/missing/edit.js index 1ef143a639ed06..e1b0d3d53bb0d1 100644 --- a/packages/block-library/src/missing/edit.js +++ b/packages/block-library/src/missing/edit.js @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import { __, sprintf } from '@wordpress/i18n'; +import { __, _x, sprintf } from '@wordpress/i18n'; import { RawHTML } from '@wordpress/element'; import { Button } from '@wordpress/components'; import { createBlock } from '@wordpress/blocks'; @@ -16,28 +16,45 @@ import { safeHTML } from '@wordpress/dom'; function MissingBlockWarning( { attributes, convertToHTML, clientId } ) { const { originalName, originalUndelimitedContent } = attributes; const hasContent = !! originalUndelimitedContent; - const hasHTMLBlock = useSelect( + const { hasFreeformBlock, hasHTMLBlock } = useSelect( ( select ) => { const { canInsertBlockType, getBlockRootClientId } = select( blockEditorStore ); - return canInsertBlockType( - 'core/html', - getBlockRootClientId( clientId ) - ); + return { + hasFreeformBlock: canInsertBlockType( + 'core/freeform', + getBlockRootClientId( clientId ) + ), + hasHTMLBlock: canInsertBlockType( + 'core/html', + getBlockRootClientId( clientId ) + ), + }; }, [ clientId ] ); const actions = []; let messageHTML; + + let blockName = originalName; + + // Defaulting to "Unknown" if the Classic block isn't registered. + if ( + ! hasFreeformBlock && + ( ! blockName || blockName === 'core/freeform' ) + ) { + blockName = _x( 'Unknown', 'The name we use for an unknown block.' ); + } + if ( hasContent && hasHTMLBlock ) { messageHTML = sprintf( /* translators: %s: block name */ __( 'Your site doesn’t include support for the "%s" block. You can leave this block intact, convert its content to a Custom HTML block, or remove it entirely.' ), - originalName + blockName ); actions.push( + ); - if ( hasContent && hasHTMLBlock ) { + if ( hasContent && ! hasFreeformBlock && ! originalName ) { + if ( hasHTMLBlock ) { + messageHTML = __( + 'It appears you are trying to use the deprecated Classic block. You can leave this block intact, convert its content to a Custom HTML block, or remove it entirely. Alternatively, you can refresh the page to use the Classic block.' + ); + actions.push( convertToHtmlButton ); + } else { + messageHTML = __( + 'It appears you are trying to use the deprecated Classic block. You can leave this block intact, or remove it entirely. Alternatively, you can refresh the page to use the Classic block.' + ); + } + } else if ( hasContent && hasHTMLBlock ) { messageHTML = sprintf( /* translators: %s: block name */ __( 'Your site doesn’t include support for the "%s" block. You can leave this block intact, convert its content to a Custom HTML block, or remove it entirely.' ), - blockName - ); - actions.push( - + originalName ); + actions.push( convertToHtmlButton ); } else { messageHTML = sprintf( /* translators: %s: block name */ __( 'Your site doesn’t include support for the "%s" block. You can leave this block intact or remove it entirely.' ), - blockName + originalName ); } From 1c9fb5257223350497ccbfc482462cc564dc990d Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 25 May 2023 17:46:21 +0300 Subject: [PATCH 22/31] Improve content length check `empty()` will return `true` if we pass the '0' string. --- lib/experimental/disable-tinymce.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/disable-tinymce.php b/lib/experimental/disable-tinymce.php index 0c4e6ba39a93e4..839d463e187703 100644 --- a/lib/experimental/disable-tinymce.php +++ b/lib/experimental/disable-tinymce.php @@ -75,7 +75,7 @@ function gutenberg_current_content_contains_classic_block() { $parsed_blocks = parse_blocks( $content ); foreach ( $parsed_blocks as $block ) { - if ( empty( $block['blockName'] ) && ! empty( trim( $block['innerHTML'] ) ) ) { + if ( empty( $block['blockName'] ) && strlen( trim( $block['innerHTML'] ) ) > 0 ) { return true; } } From 72cf3df47185312ea62c0331079d5a681a594f55 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 25 May 2023 17:50:13 +0300 Subject: [PATCH 23/31] Expand comment on classic block registration --- packages/block-library/src/index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index 2e6756bc5ffd5c..865208a559e06f 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -230,7 +230,10 @@ const getAllBlocks = () => { ]; // Only add the classic block in WP Context - // and when TinyMCE experiment is disabled or cookie to require TinyMCE is set + // and when either of the following is true: + // - TinyMCE experiment is disabled; + // - there is a classic block in the current post's content; + // - the cookie to require TinyMCE is set. if ( window?.wp?.oldEditor && ( window?.wp?.needsClassicBlock || From 57ff6914f63d36320359aaecfa4d2264a7629bb7 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Tue, 30 May 2023 16:28:19 +0300 Subject: [PATCH 24/31] Move example TinyMCE usage code up --- lib/experimental/disable-tinymce.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/experimental/disable-tinymce.php b/lib/experimental/disable-tinymce.php index 839d463e187703..45f684fdca71d7 100644 --- a/lib/experimental/disable-tinymce.php +++ b/lib/experimental/disable-tinymce.php @@ -5,6 +5,8 @@ * @package gutenberg */ +// add_action( 'admin_footer', 'gutenberg_test_tinymce_access' ); // Uncomment the following line to force an external TinyMCE usage. + /** * Render a variable that we'll use to declare that the editor will need the classic block. */ @@ -82,5 +84,3 @@ function gutenberg_current_content_contains_classic_block() { return false; } - -// add_action( 'admin_footer', 'gutenberg_test_tinymce_access' ); // Uncomment the following line to force an external TinyMCE usage. From d4d636647bac379cae5b2a682326c0b2c1cf3394 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Tue, 13 Jun 2023 14:12:06 +0300 Subject: [PATCH 25/31] Use a query argument instead of a cookie --- lib/client-assets.php | 2 +- lib/experimental/assets/tinymce-proxy.js | 6 +++--- lib/experimental/disable-tinymce.php | 4 ++-- packages/block-library/src/index.js | 9 ++++----- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 380d116f64805c..b9e385be5eb876 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -205,7 +205,7 @@ function gutenberg_register_packages_scripts( $scripts ) { case 'wp-block-library': if ( ! gutenberg_is_experiment_enabled( 'gutenberg-no-tinymce' ) || - isset( $_COOKIE['requiresTinymce'] ) || + ! empty( $_GET['requiresTinymce'] ) || gutenberg_current_content_contains_classic_block() ) { array_push( $dependencies, 'editor' ); diff --git a/lib/experimental/assets/tinymce-proxy.js b/lib/experimental/assets/tinymce-proxy.js index e8044b8abafd6c..f66ab6b435c931 100644 --- a/lib/experimental/assets/tinymce-proxy.js +++ b/lib/experimental/assets/tinymce-proxy.js @@ -1,7 +1,7 @@ async function markAsRequiringTinymce() { - const expiration = new Date( 'Dec 31, 2099 23:59:59' ); - document.cookie = `requiresTinymce=1; expires=${ expiration }; path=/`; - window.location.reload(); + const currentUrl = new URL( window.location.href ); + currentUrl.searchParams.set( 'requiresTinymce', '1' ); + window.location.href = currentUrl; } window.tinymce = new Proxy( diff --git a/lib/experimental/disable-tinymce.php b/lib/experimental/disable-tinymce.php index 45f684fdca71d7..428d97371e505f 100644 --- a/lib/experimental/disable-tinymce.php +++ b/lib/experimental/disable-tinymce.php @@ -19,7 +19,7 @@ function gutenberg_declare_classic_block_necessary() { add_action( 'admin_footer', 'gutenberg_declare_classic_block_necessary' ); // If user has already requested TinyMCE, we're ending the experiment. -if ( isset( $_COOKIE['requiresTinymce'] ) || gutenberg_current_content_contains_classic_block() ) { +if ( ! empty( $_GET['requiresTinymce'] ) || gutenberg_current_content_contains_classic_block() ) { return; } @@ -35,7 +35,7 @@ function gutenberg_disable_tinymce() { /** * Enqueue TinyMCE proxy script. - * Detects TinyMCE usage and sets the `requiresTinymce` cookie to stop disabling TinyMCE loading. + * Detects TinyMCE usage and sets the `requiresTinymce` query argument to stop disabling TinyMCE loading. */ function gutenberg_enqueue_tinymce_proxy() { wp_enqueue_script( 'gutenberg-tinymce-proxy', plugins_url( 'assets/tinymce-proxy.js', __FILE__ ) ); diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index 865208a559e06f..2a20be10c1bbf0 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -233,15 +233,14 @@ const getAllBlocks = () => { // and when either of the following is true: // - TinyMCE experiment is disabled; // - there is a classic block in the current post's content; - // - the cookie to require TinyMCE is set. + // - the query argument to require TinyMCE is set. if ( window?.wp?.oldEditor && ( window?.wp?.needsClassicBlock || ! window?.__experimentalDisableTinymce || - ( '; ' + document.cookie ) - .split( '; requiresTinymce=' ) - .pop() - .split( ';' )[ 0 ] ) + !! new URLSearchParams( window?.location?.search ).get( + 'requiresTinymce' + ) ) ) { blocks.push( classic ); } From 7fc9a82b1abab9599be91f8482f603cddbaea31d Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 29 Jun 2023 16:20:49 +0300 Subject: [PATCH 26/31] Rename proxy trap --- lib/experimental/assets/tinymce-proxy.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/experimental/assets/tinymce-proxy.js b/lib/experimental/assets/tinymce-proxy.js index f66ab6b435c931..91c9e74bb74ed8 100644 --- a/lib/experimental/assets/tinymce-proxy.js +++ b/lib/experimental/assets/tinymce-proxy.js @@ -1,4 +1,4 @@ -async function markAsRequiringTinymce() { +async function reloadWithTinymce() { const currentUrl = new URL( window.location.href ); currentUrl.searchParams.set( 'requiresTinymce', '1' ); window.location.href = currentUrl; @@ -7,8 +7,8 @@ async function markAsRequiringTinymce() { window.tinymce = new Proxy( {}, { - get: markAsRequiringTinymce, - set: markAsRequiringTinymce, - apply: markAsRequiringTinymce, + get: reloadWithTinymce, + set: reloadWithTinymce, + apply: reloadWithTinymce, } ); From 797093ad42e8d6d7a7ac2f58e0bd6bd084b46fa0 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 29 Jun 2023 16:22:14 +0300 Subject: [PATCH 27/31] Fix the comment --- lib/experimental/disable-tinymce.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/disable-tinymce.php b/lib/experimental/disable-tinymce.php index 428d97371e505f..7a171a6b17dca4 100644 --- a/lib/experimental/disable-tinymce.php +++ b/lib/experimental/disable-tinymce.php @@ -45,7 +45,7 @@ function gutenberg_enqueue_tinymce_proxy() { /** * Example TinyMCE usage used for testing. - * Uncomment the last line of this file to enable. + * Uncomment line 8 in this file to enable. */ function gutenberg_test_tinymce_access() { echo ''; From ddfec4107895a2c18dbea4ab32bf63d81c04a035 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 29 Jun 2023 16:24:22 +0300 Subject: [PATCH 28/31] Rename another function --- lib/client-assets.php | 2 +- lib/experimental/disable-tinymce.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index b9e385be5eb876..99aa7f147ecbfc 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -206,7 +206,7 @@ function gutenberg_register_packages_scripts( $scripts ) { if ( ! gutenberg_is_experiment_enabled( 'gutenberg-no-tinymce' ) || ! empty( $_GET['requiresTinymce'] ) || - gutenberg_current_content_contains_classic_block() + gutenberg_post_being_edited_requires_classic_block() ) { array_push( $dependencies, 'editor' ); } diff --git a/lib/experimental/disable-tinymce.php b/lib/experimental/disable-tinymce.php index 7a171a6b17dca4..f6bdf052fb05e6 100644 --- a/lib/experimental/disable-tinymce.php +++ b/lib/experimental/disable-tinymce.php @@ -11,7 +11,7 @@ * Render a variable that we'll use to declare that the editor will need the classic block. */ function gutenberg_declare_classic_block_necessary() { - if ( ! gutenberg_current_content_contains_classic_block() ) { + if ( ! gutenberg_post_being_edited_requires_classic_block() ) { return; } echo ''; @@ -19,7 +19,7 @@ function gutenberg_declare_classic_block_necessary() { add_action( 'admin_footer', 'gutenberg_declare_classic_block_necessary' ); // If user has already requested TinyMCE, we're ending the experiment. -if ( ! empty( $_GET['requiresTinymce'] ) || gutenberg_current_content_contains_classic_block() ) { +if ( ! empty( $_GET['requiresTinymce'] ) || gutenberg_post_being_edited_requires_classic_block() ) { return; } @@ -56,7 +56,7 @@ function gutenberg_test_tinymce_access() { * * @return bool True if the editor contains a classic block, false otherwse. */ -function gutenberg_current_content_contains_classic_block() { +function gutenberg_post_being_edited_requires_classic_block() { if ( ! is_admin() ) { return false; } From 66ed611cccdb048ae9e58d5763b3e1f95c92451b Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 29 Jun 2023 16:27:18 +0300 Subject: [PATCH 29/31] Improve the classic block load conditions comment --- packages/block-library/src/index.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index 2a20be10c1bbf0..2ae5b94393904e 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -229,11 +229,12 @@ const getAllBlocks = () => { postAuthorBiography, ]; - // Only add the classic block in WP Context - // and when either of the following is true: - // - TinyMCE experiment is disabled; - // - there is a classic block in the current post's content; - // - the query argument to require TinyMCE is set. + // When in a WordPress context, conditionally + // add the classic block and TinyMCE editor + // under any of the following conditions: + // - the current post contains a classic block + // - TinyMCE experiment hasn't been intentionally disabled + // - a query argument specifies that TinyMCE should be loaded if ( window?.wp?.oldEditor && ( window?.wp?.needsClassicBlock || From d448cac348f09c2e42869b9b4c243334f9a50522 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Fri, 30 Jun 2023 14:08:53 +0300 Subject: [PATCH 30/31] Further polish the conditions comment --- packages/block-library/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index 2ae5b94393904e..911bd0c37451ee 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -233,7 +233,7 @@ const getAllBlocks = () => { // add the classic block and TinyMCE editor // under any of the following conditions: // - the current post contains a classic block - // - TinyMCE experiment hasn't been intentionally disabled + // - the experiment to disable TinyMCE isn't active. // - a query argument specifies that TinyMCE should be loaded if ( window?.wp?.oldEditor && From e5ce4e9b0ee5048ee0c772ff4309d2ef607867eb Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Mon, 3 Jul 2023 11:52:43 +0300 Subject: [PATCH 31/31] Fix a fatal in the wp-admin dashboard --- lib/experimental/class--wp-editors.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/experimental/class--wp-editors.php b/lib/experimental/class--wp-editors.php index d0a792f9795fd3..bc49ea80a99421 100644 --- a/lib/experimental/class--wp-editors.php +++ b/lib/experimental/class--wp-editors.php @@ -16,7 +16,12 @@ */ final class _WP_Editors { /** - * Necessary to ensure no additional TinyMcE assets are enqueued. + * Necessary to ensure no additional TinyMCE assets are enqueued. */ public static function enqueue_default_editor() {} + + /** + * Necessary for wp admin dashboard. + */ + public static function editor() {} }