From 91db2c4731870663408c7a98d8bdfda42fdbc969 Mon Sep 17 00:00:00 2001 From: lstoyanoff Date: Fri, 6 Mar 2020 17:40:27 +0200 Subject: [PATCH 1/5] Change rich-text field activation and add method for modifying wp_editor settings --- core/Field/Rich_Text_Field.php | 61 ++++++++++++++++++++----- packages/core/fields/rich-text/index.js | 3 +- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/core/Field/Rich_Text_Field.php b/core/Field/Rich_Text_Field.php index a150cc745..10883d48b 100644 --- a/core/Field/Rich_Text_Field.php +++ b/core/Field/Rich_Text_Field.php @@ -6,12 +6,52 @@ * WYSIWYG rich text area field class. */ class Rich_Text_Field extends Textarea_Field { + /** + * Total number of Rich_Text_Field objects + * + * @var integer + */ + private static $instance_count = 1; + + /** + * Current instance index + * + * @var integer + */ + protected $editor_index; + + /** + * WP Editor settings + * + * @link https://developer.wordpress.org/reference/classes/_wp_editors/parse_settings/ + * @var array + */ + protected $settings = array( + 'tinymce' => array( + 'resize' => true, + 'wp_autoresize_on' => true, + ), + ); + + /** + * Set the editor settings + * + * @param array $settings + * @return self $this + */ + public function set_settings( $settings ) { + $this->settings = array_merge( $this->settings, $settings ); + + return $this; + } /** * {@inheritDoc} */ - public static function field_type_activated() { - add_action( 'admin_footer', array( get_class(), 'editor_init' ) ); + public function activate() { + parent::activate(); + + add_action( 'admin_footer', array( $this, 'editor_init' ) ); } /** @@ -20,20 +60,16 @@ public static function field_type_activated() { * Instead of enqueueing all required scripts and stylesheets and setting up TinyMCE, * wp_editor() automatically enqueues and sets up everything. */ - public static function editor_init() { + public function editor_init() { ?>
array( - 'resize' => true, - 'wp_autoresize_on' => true, - ), - ); - add_filter( 'user_can_richedit', '__return_true' ); - wp_editor( '', 'carbon_settings', $settings ); + wp_editor( '', 'carbon_settings_' . self::$instance_count, $this->settings ); remove_filter( 'user_can_richedit', '__return_true' ); + + $this->editor_index = self::$instance_count; + self::$instance_count++; ?>
user_can_richedit(), 'media_buttons' => $media_buttons, + 'editor_index' => $this->editor_index, ) ); return $field_data; } -} +} \ No newline at end of file diff --git a/packages/core/fields/rich-text/index.js b/packages/core/fields/rich-text/index.js index b51673590..cfeb5362d 100644 --- a/packages/core/fields/rich-text/index.js +++ b/packages/core/fields/rich-text/index.js @@ -141,7 +141,6 @@ class RichTextField extends Component { */ initEditor = () => { const { id, field } = this.props; - if ( field.rich_editing ) { const editorSetup = ( editor ) => { this.editor = editor; @@ -154,7 +153,7 @@ class RichTextField extends Component { }; const editorOptions = { - ...window.tinyMCEPreInit.mceInit.carbon_settings, + ...window.tinyMCEPreInit.mceInit[ `carbon_settings_${ field.editor_index }` ], selector: `#${ id }`, setup: editorSetup }; From 78bafbd4f867be797afd8dd72d88a6f94e96a393 Mon Sep 17 00:00:00 2001 From: lstoyanoff Date: Fri, 6 Mar 2020 17:57:56 +0200 Subject: [PATCH 2/5] Polish rich-text field editor settings method --- core/Field/Rich_Text_Field.php | 37 ++++++++++++------------- packages/core/fields/rich-text/index.js | 2 +- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/core/Field/Rich_Text_Field.php b/core/Field/Rich_Text_Field.php index 10883d48b..bc7e1d513 100644 --- a/core/Field/Rich_Text_Field.php +++ b/core/Field/Rich_Text_Field.php @@ -6,20 +6,6 @@ * WYSIWYG rich text area field class. */ class Rich_Text_Field extends Textarea_Field { - /** - * Total number of Rich_Text_Field objects - * - * @var integer - */ - private static $instance_count = 1; - - /** - * Current instance index - * - * @var integer - */ - protected $editor_index; - /** * WP Editor settings * @@ -33,6 +19,13 @@ class Rich_Text_Field extends Textarea_Field { ), ); + /** + * WP Editor settings reference + * + * @var string + */ + protected $settings_reference; + /** * Set the editor settings * @@ -45,6 +38,15 @@ public function set_settings( $settings ) { return $this; } + /** + * Get the editor settings reference + * + * @return string + */ + protected function get_settings_reference() { + return 'carbon_fields_settings_' . $this->id; + } + /** * {@inheritDoc} */ @@ -65,11 +67,8 @@ public function editor_init() {
settings ); + wp_editor( '', $this->get_settings_reference(), $this->settings ); remove_filter( 'user_can_richedit', '__return_true' ); - - $this->editor_index = self::$instance_count; - self::$instance_count++; ?>
user_can_richedit(), 'media_buttons' => $media_buttons, - 'editor_index' => $this->editor_index, + 'settings_reference' => $this->get_settings_reference(), ) ); return $field_data; diff --git a/packages/core/fields/rich-text/index.js b/packages/core/fields/rich-text/index.js index cfeb5362d..13f80b392 100644 --- a/packages/core/fields/rich-text/index.js +++ b/packages/core/fields/rich-text/index.js @@ -153,7 +153,7 @@ class RichTextField extends Component { }; const editorOptions = { - ...window.tinyMCEPreInit.mceInit[ `carbon_settings_${ field.editor_index }` ], + ...window.tinyMCEPreInit.mceInit[ field.settings_reference ], selector: `#${ id }`, setup: editorSetup }; From 41289ee4542a6009f18dac731bcb8d1f6a02ccae Mon Sep 17 00:00:00 2001 From: lstoyanoff Date: Thu, 12 Mar 2020 16:53:14 +0200 Subject: [PATCH 3/5] Optimize and fix rich-text options --- core/Field/Rich_Text_Field.php | 27 ++++++++++++++++++------- packages/core/fields/rich-text/index.js | 20 ++++++++++-------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/core/Field/Rich_Text_Field.php b/core/Field/Rich_Text_Field.php index bc7e1d513..9a34d52d9 100644 --- a/core/Field/Rich_Text_Field.php +++ b/core/Field/Rich_Text_Field.php @@ -13,12 +13,15 @@ class Rich_Text_Field extends Textarea_Field { * @var array */ protected $settings = array( + 'media_buttons' => true, 'tinymce' => array( 'resize' => true, 'wp_autoresize_on' => true, ), ); + protected static $instances_references = []; + /** * WP Editor settings reference * @@ -44,7 +47,9 @@ public function set_settings( $settings ) { * @return string */ protected function get_settings_reference() { - return 'carbon_fields_settings_' . $this->id; + $reference_id = md5( json_encode( $this->settings ) ); + + return 'carbon_fields_settings_' . $reference_id; } /** @@ -63,6 +68,11 @@ public function activate() { * wp_editor() automatically enqueues and sets up everything. */ public function editor_init() { + if( in_array( $this->get_settings_reference(), self::$instances_references ) ) { + return; + } + + self::$instances_references[] = $this->get_settings_reference(); ?>
settings['media_buttons'] ) { + ob_start(); + remove_action( 'media_buttons', 'media_buttons' ); - $this->upload_image_button_html(); + $this->upload_image_button_html(); - do_action( 'media_buttons' ); + do_action( 'media_buttons' ); - add_action( 'media_buttons', 'media_buttons' ); + add_action( 'media_buttons', 'media_buttons' ); - $media_buttons = apply_filters( 'crb_media_buttons_html', ob_get_clean(), $this->base_name ); + $media_buttons = apply_filters( 'crb_media_buttons_html', ob_get_clean(), $this->base_name ); + } $field_data = array_merge( $field_data, array( 'rich_editing' => user_can_richedit(), diff --git a/packages/core/fields/rich-text/index.js b/packages/core/fields/rich-text/index.js index 13f80b392..ed3fedd2d 100644 --- a/packages/core/fields/rich-text/index.js +++ b/packages/core/fields/rich-text/index.js @@ -95,6 +95,8 @@ class RichTextField extends Component { ? template( field.media_buttons )( { id } ) : null; + const shouldRenderTabs = field.rich_editing && window.tinyMCEPreInit.qtInit[ field.settings_reference ]; + return (
) } - { field.rich_editing && ( + { shouldRenderTabs && (