Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
lstoyanoff committed Mar 18, 2020
2 parents ca5d111 + 6deb9dc commit f9ad3b6
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 31 deletions.
2 changes: 1 addition & 1 deletion config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Define version constant
if ( ! defined( __NAMESPACE__ . '\VERSION' ) ) {
define( __NAMESPACE__ . '\VERSION', '3.1.14' );
define( __NAMESPACE__ . '\VERSION', '3.1.15' );
}

# Define root directory
Expand Down
111 changes: 92 additions & 19 deletions core/Field/Rich_Text_Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,83 @@
* WYSIWYG rich text area field class.
*/
class Rich_Text_Field extends Textarea_Field {
/**
* All Rich Text Fields settings references.
* Used to prevent duplicated wp_editor initialization with the same settings.
*
* @var array
*/
protected static $settings_references = [];

/**
* WP Editor settings
*
* @link https://developer.wordpress.org/reference/classes/_wp_editors/parse_settings/
* @var array
*/
protected $settings = array(
'media_buttons' => true,
'tinymce' => array(
'resize' => true,
'wp_autoresize_on' => true,
),
);

/**
* MD5 Hash of the instance settings
*
* @var string
*/
protected $settings_hash = null;

/**
* WP Editor settings reference
*
* @var string
*/
protected $settings_reference;

/**
* Set the editor settings
*
* @param array $settings
* @return self $this
*/
public function set_settings( $settings ) {
$this->settings = array_merge( $this->settings, $settings );

return $this;
}

/**
* Calc the settings hash
*
* @return string
*/
protected function calc_settings_hash() {
return md5( json_encode( $this->settings ) );
}

/**
* Get the editor settings reference
*
* @return string
*/
protected function get_settings_reference() {
if ( is_null( $this->settings_hash ) ) {
$this->settings_hash = $this->calc_settings_hash();
}

return 'carbon_fields_settings_' . $this->settings_hash;
}

/**
* {@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' ) );
}

/**
Expand All @@ -20,19 +91,17 @@ 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() {
if( in_array( $this->get_settings_reference(), self::$settings_references ) ) {
return;
}

self::$settings_references[] = $this->get_settings_reference();
?>
<div style="display:none;">
<?php
$settings = array(
'tinymce' => array(
'resize' => true,
'wp_autoresize_on' => true,
),
);

add_filter( 'user_can_richedit', '__return_true' );
wp_editor( '', 'carbon_settings', $settings );
wp_editor( '', $this->get_settings_reference(), $this->settings );
remove_filter( 'user_can_richedit', '__return_true' );
?>
</div>
Expand All @@ -59,22 +128,26 @@ public function upload_image_button_html() {
public function to_json( $load ) {
$field_data = parent::to_json( $load );

ob_start();
remove_action( 'media_buttons', 'media_buttons' );
$media_buttons = '';
if ( $this->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(),
'rich_editing' => user_can_richedit() && ! empty( $this->settings['tinymce'] ),
'media_buttons' => $media_buttons,
'settings_reference' => $this->get_settings_reference(),
) );

return $field_data;
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "carbon-fields",
"version": "3.1.14",
"version": "3.1.15",
"description": "WordPress developer-friendly custom fields for post types, taxonomy terms, users, comments, widgets, options, navigation menus and more.",
"directories": {
"test": "tests"
Expand Down
23 changes: 13 additions & 10 deletions packages/core/fields/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div
id={ `wp-${ id }-wrap` }
Expand All @@ -107,7 +109,7 @@ class RichTextField extends Component {
</div>
) }

{ field.rich_editing && (
{ shouldRenderTabs && (
<div className="wp-editor-tabs">
<button type="button" id={ `${ id }-tmce` } className="wp-switch-editor switch-tmce" data-wp-editor-id={ id }>
{ __( 'Visual', 'carbon-fields-ui' ) }
Expand Down Expand Up @@ -141,7 +143,6 @@ class RichTextField extends Component {
*/
initEditor = () => {
const { id, field } = this.props;

if ( field.rich_editing ) {
const editorSetup = ( editor ) => {
this.editor = editor;
Expand All @@ -154,23 +155,25 @@ class RichTextField extends Component {
};

const editorOptions = {
...window.tinyMCEPreInit.mceInit.carbon_settings,
...window.tinyMCEPreInit.mceInit[ field.settings_reference ],
selector: `#${ id }`,
setup: editorSetup
};

window.tinymce.init( editorOptions );
}

const quickTagsOptions = {
...window.tinyMCEPreInit,
id
};
const quickTagsOptions = { ...window.tinyMCEPreInit.qtInit[ field.settings_reference ] };

window.quicktags( quickTagsOptions );
if ( quickTagsOptions ) {
const qtagInstance = window.quicktags( {
...quickTagsOptions,
id
} );

// Force the initialization of the quick tags.
window.QTags._buttonsInit();
// Force the initialization of the quick tags.
window.QTags._buttonsInit( qtagInstance.id );
}
}

/**
Expand Down

0 comments on commit f9ad3b6

Please sign in to comment.