From 3007f1fd1c66ba36f5e0086796ef2da4b9354be7 Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Tue, 23 Oct 2018 21:39:23 +0300 Subject: [PATCH 1/3] Fix the TinyMCE init array - Added "manual" convert from php to js, same as in class-wp-editor.php. - Reformated the code so it's more readable. --- lib/client-assets.php | 200 ++++++++++++++++++++++-------------------- 1 file changed, 103 insertions(+), 97 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index b9b2f10037262..1648d2e0331a4 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -536,104 +536,110 @@ function gutenberg_register_scripts_and_styles() { 'window.wp.oldEditor = window.wp.editor;', 'after' ); - $tinymce_settings = apply_filters( - 'tiny_mce_before_init', - array( - 'plugins' => implode( - ',', - array_unique( - apply_filters( - 'tiny_mce_plugins', - array( - 'charmap', - 'colorpicker', - 'hr', - 'lists', - 'media', - 'paste', - 'tabfocus', - 'textcolor', - 'fullscreen', - 'wordpress', - 'wpautoresize', - 'wpeditimage', - 'wpemoji', - 'wpgallery', - 'wplink', - 'wpdialogs', - 'wptextpattern', - 'wpview', - ) - ) - ) - ), - 'toolbar1' => implode( - ',', - array_merge( - apply_filters( - 'mce_buttons', - array( - 'formatselect', - 'bold', - 'italic', - 'bullist', - 'numlist', - 'blockquote', - 'alignleft', - 'aligncenter', - 'alignright', - 'link', - 'unlink', - 'wp_more', - 'spellchecker', - 'wp_add_media', - ), - 'editor' - ), - array( 'kitchensink' ) - ) - ), - 'toolbar2' => implode( - ',', - apply_filters( - 'mce_buttons_2', - array( - 'strikethrough', - 'hr', - 'forecolor', - 'pastetext', - 'removeformat', - 'charmap', - 'outdent', - 'indent', - 'undo', - 'redo', - 'wp_help', - ), - 'editor' - ) - ), - 'toolbar3' => implode( ',', apply_filters( 'mce_buttons_3', array(), 'editor' ) ), - 'toolbar4' => implode( ',', apply_filters( 'mce_buttons_4', array(), 'editor' ) ), - 'external_plugins' => apply_filters( 'mce_external_plugins', array() ), - ), - 'editor' - ); - if ( isset( $tinymce_settings['style_formats'] ) && is_string( $tinymce_settings['style_formats'] ) ) { - // Decode the options as we used to recommende json_encoding the TinyMCE settings. - $tinymce_settings['style_formats'] = json_decode( $tinymce_settings['style_formats'] ); + + $tinymce_plugins = array( + 'charmap', + 'colorpicker', + 'hr', + 'lists', + 'media', + 'paste', + 'tabfocus', + 'textcolor', + 'fullscreen', + 'wordpress', + 'wpautoresize', + 'wpeditimage', + 'wpemoji', + 'wpgallery', + 'wplink', + 'wpdialogs', + 'wptextpattern', + 'wpview', + ); + $tinymce_plugins = apply_filters( 'tiny_mce_plugins', $tinymce_plugins, 'classic-block' ); + $tinymce_plugins = array_unique( $tinymce_plugins ); + + $toolbar1 = array( + 'formatselect', + 'bold', + 'italic', + 'bullist', + 'numlist', + 'blockquote', + 'alignleft', + 'aligncenter', + 'alignright', + 'link', + 'unlink', + 'wp_more', + 'spellchecker', + 'wp_add_media', + 'kitchensink', + ); + $toolbar1 = apply_filters( 'mce_buttons', $toolbar1, 'classic-block' ); + + $toolbar2 = array( + 'strikethrough', + 'hr', + 'forecolor', + 'pastetext', + 'removeformat', + 'charmap', + 'outdent', + 'indent', + 'undo', + 'redo', + 'wp_help', + ); + $toolbar2 = apply_filters( 'mce_buttons_2', $toolbar2, 'classic-block' ); + + $toolbar3 = apply_filters( 'mce_buttons_3', array(), 'classic-block' ); + $toolbar4 = apply_filters( 'mce_buttons_4', array(), 'classic-block' ); + + $external_plugins = apply_filters( 'mce_external_plugins', array(), 'classic-block' ); + + $tinymce_settings = array( + 'plugins'=> implode( ',', $tinymce_plugins ), + 'toolbar1' => implode( ',', $toolbar1 ), + 'toolbar2' => implode( ',', $toolbar2 ), + 'toolbar3' => implode( ',', $toolbar3 ), + 'toolbar4' => implode( ',', $toolbar4 ), + 'external_plugins' => wp_json_encode( $external_plugins ), + 'block_editor' => true, + ); + $tinymce_settings = apply_filters( 'tiny_mce_before_init', $tinymce_settings, 'classic-block' ); + + // Do "by hand" translation from PHP array to js object. + // Prevents breakage in some custom settings. + $init_obj = ''; + foreach ( $tinymce_settings as $key => $value ) { + if ( is_bool( $value ) ) { + $val = $value ? 'true' : 'false'; + $init_obj .= $key . ':' . $val . ','; + continue; + } elseif ( ! empty( $value ) && is_string( $value ) && ( + ( '{' == $value{0} && '}' == $value{strlen( $value ) - 1} ) || + ( '[' == $value{0} && ']' == $value{strlen( $value ) - 1} ) || + preg_match( '/^\(?function ?\(/', $value ) ) ) { + + $init_obj .= $key . ':' . $value . ','; + continue; + } + $init_obj .= $key . ':"' . $value . '",'; } - wp_localize_script( - 'wp-block-library', - 'wpEditorL10n', - array( - 'tinymce' => array( - 'baseURL' => includes_url( 'js/tinymce' ), - 'suffix' => SCRIPT_DEBUG ? '' : '.min', - 'settings' => $tinymce_settings, - ), - ) - ); + + $init_obj = '{' . trim( $init_obj, ' ,' ) . '}'; + + $script = 'window.wpEditorL10n = { + tinymce: { + baseURL: ' . wp_json_encode( includes_url( 'js/tinymce' ) ) . ', + suffix: ' . ( SCRIPT_DEBUG ? '""' : '".min"' ) . ', + settings: ' . $init_obj . ', + } + }'; + + wp_add_inline_script( 'wp-block-library', $script, 'before' ); gutenberg_override_script( 'wp-editor', From f8ad2995a7729adcf24a1b08f252123ac5173fee Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Wed, 24 Oct 2018 12:13:51 +0300 Subject: [PATCH 2/3] Fix missing space to fix failing tests... --- lib/client-assets.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 1648d2e0331a4..77df616d6c5c8 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -600,13 +600,13 @@ function gutenberg_register_scripts_and_styles() { $external_plugins = apply_filters( 'mce_external_plugins', array(), 'classic-block' ); $tinymce_settings = array( - 'plugins'=> implode( ',', $tinymce_plugins ), + 'plugins' => implode( ',', $tinymce_plugins ), 'toolbar1' => implode( ',', $toolbar1 ), 'toolbar2' => implode( ',', $toolbar2 ), 'toolbar3' => implode( ',', $toolbar3 ), 'toolbar4' => implode( ',', $toolbar4 ), 'external_plugins' => wp_json_encode( $external_plugins ), - 'block_editor' => true, + 'classic_block_editor' => true, ); $tinymce_settings = apply_filters( 'tiny_mce_before_init', $tinymce_settings, 'classic-block' ); From c6962657db5c68dcad6851518539f5ecb0174fb7 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Tue, 20 Nov 2018 17:46:07 +0100 Subject: [PATCH 3/3] Fix phpcs --- lib/client-assets.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 08037d1c2a15c..fd1553c3cfd87 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -347,12 +347,12 @@ function gutenberg_register_scripts_and_styles() { $external_plugins = apply_filters( 'mce_external_plugins', array(), 'classic-block' ); $tinymce_settings = array( - 'plugins' => implode( ',', $tinymce_plugins ), - 'toolbar1' => implode( ',', $toolbar1 ), - 'toolbar2' => implode( ',', $toolbar2 ), - 'toolbar3' => implode( ',', $toolbar3 ), - 'toolbar4' => implode( ',', $toolbar4 ), - 'external_plugins' => wp_json_encode( $external_plugins ), + 'plugins' => implode( ',', $tinymce_plugins ), + 'toolbar1' => implode( ',', $toolbar1 ), + 'toolbar2' => implode( ',', $toolbar2 ), + 'toolbar3' => implode( ',', $toolbar3 ), + 'toolbar4' => implode( ',', $toolbar4 ), + 'external_plugins' => wp_json_encode( $external_plugins ), 'classic_block_editor' => true, ); $tinymce_settings = apply_filters( 'tiny_mce_before_init', $tinymce_settings, 'classic-block' ); @@ -362,7 +362,7 @@ function gutenberg_register_scripts_and_styles() { $init_obj = ''; foreach ( $tinymce_settings as $key => $value ) { if ( is_bool( $value ) ) { - $val = $value ? 'true' : 'false'; + $val = $value ? 'true' : 'false'; $init_obj .= $key . ':' . $val . ','; continue; } elseif ( ! empty( $value ) && is_string( $value ) && (