Skip to content
This repository has been archived by the owner on Dec 16, 2023. It is now read-only.

Commit

Permalink
Fixes for ACF4 WYSIWYG editors
Browse files Browse the repository at this point in the history
  • Loading branch information
funkjedi committed Nov 18, 2016
1 parent cac259b commit feffec1
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 36 deletions.
55 changes: 45 additions & 10 deletions assets/acf_4/qtranslatex.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,20 @@ jQuery(window).load(function() {
qTranslateConfig.qtx.removeContentHook(this);
});

var post_type = jQuery('#post_type').val();

// Whitelist fields for translation
function isTranslatableField(field){
if (post_type === 'acf-field-group') {
if (field.id.match(/acf-field-field_[a-z0-9]+_label/)) return true;
if (field.id.match(/acf-field-field_[a-z0-9]+_instructions/)) return true;
if (field.id.match(/acf-field-field_[a-z0-9]+_default_value/)) return true;
return false;
}
return true;
}

// Watch and add content hooks when new fields are added
var timeoutContentHooksTinyMCE;
jQuery(document).on('acf/setup_fields', function(e, new_field) {
new_field = jQuery(new_field);
new_field.find(field_types).not('.qtranxs-translatable').each(function() {
Expand All @@ -40,6 +52,8 @@ jQuery(window).load(function() {
return;
}

if (!isTranslatableField(field)) return;

qTranslateConfig.qtx.addContentHookC(this, field.closest('form').get(0));

// Since ACFv4 doesn't update tinyMCEPreInit.mceInit so we
Expand All @@ -55,17 +69,11 @@ jQuery(window).load(function() {

// Run in a setTimeout block to give the tinyMCE instance
// enough time to initialize before setting the editor hooks
clearTimeout(timeoutContentHooksTinyMCE);
timeoutContentHooksTinyMCE = setTimeout(function(){
qTranslateConfig.qtx.addContentHooksTinyMCE();
setTimeout(function(){
jQuery.each(tinyMCE.editors, function(i, ed){
var mceInit = tinyMCEPreInit.mceInit[ed.id];
console.log('initEditors:',mceInit);
if (mceInit && mceInit.init_instance_callback) {
mceInit.init_instance_callback(ed);
}
setEditorHooks(ed);
});
}, 50);
},50);
});

// Watch and remove content hooks when fields are removed
Expand All @@ -76,4 +84,31 @@ jQuery(window).load(function() {
});
});


// Extracted from qTranslate-X
// admin/js/common.js#L840
function setEditorHooks(ed) {
var id = ed.id;
if (!id) return;
var h=qTranslateConfig.qtx.hasContentHook(id);
if(!h || h.mce) return;
h.mce=ed;
ed.getContainer().className += ' qtranxs-translatable';
ed.getElement().className += ' qtranxs-translatable';
var updateTinyMCEonInit = h.updateTinyMCEonInit;
if (updateTinyMCEonInit == null) {
var text_e = ed.getContent({format: 'html'}).replace(/\s+/g,'');
var text_h = h.contentField.value.replace(/\s+/g,'');
updateTinyMCEonInit = text_e != text_h;
}
if (updateTinyMCEonInit) {
text = h.contentField.value;
if (h.wpautop && window.switchEditors) {
text = window.switchEditors.wpautop(text);
}
h.mce.setContent(text,{format: 'html'});
}
return h;
}

});
73 changes: 47 additions & 26 deletions src/acf_4/fields/wysiwyg.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,28 @@ function __construct($plugin) {
'default_value' => '',
);

// Create an acf version of the_content filter (acf_the_content)
if( isset($GLOBALS['wp_embed']) ) {

add_filter( 'acf_the_content', array( $GLOBALS['wp_embed'], 'run_shortcode' ), 8 );
add_filter( 'acf_the_content', array( $GLOBALS['wp_embed'], 'autoembed' ), 8 );

}

add_filter( 'acf_the_content', 'capital_P_dangit', 11 );
add_filter( 'acf_the_content', 'wptexturize' );
add_filter( 'acf_the_content', 'convert_smilies' );
add_filter( 'acf_the_content', 'convert_chars' );
add_filter( 'acf_the_content', 'wpautop' );
add_filter( 'acf_the_content', 'shortcode_unautop' );
//add_filter( 'acf_the_content', 'prepend_attachment' ); *should only be for the_content (causes double image on attachment page)
add_filter( 'acf_the_content', 'do_shortcode', 11);

acf_field::__construct();

// filters
add_filter( 'acf/fields/wysiwyg/toolbars', array( $this, 'toolbars'), 1, 1 );
add_filter( 'mce_external_plugins', array( $this, 'mce_external_plugins'), 20, 1 );
}

/*
Expand Down Expand Up @@ -61,17 +82,24 @@ function create_field($field) {
$languages = qtrans_getSortedLanguages(true);
$values = qtrans_split($field['value'], $quicktags = true);
$currentLanguage = $this->plugin->get_active_language();


// vars
//$id = uniqid('acf-editor-');
$id = 'wysiwyg-' . $field['id'] . '-' . uniqid();
$default_editor = 'tinymce';

// filter value for editor
remove_all_filters('acf_the_editor_content');

remove_filter( 'acf_the_editor_content', 'format_for_editor', 10, 2 );
remove_filter( 'acf_the_editor_content', 'wp_htmledit_pre', 10, 1 );
remove_filter( 'acf_the_editor_content', 'wp_richedit_pre', 10, 1 );

// WP 4.3
if( version_compare($wp_version, '4.3', '>=' ) ) {
add_filter( 'acf_the_editor_content', 'format_for_editor' );
if( version_compare($wp_version, '4.3', '>=' ) ) {
add_filter( 'acf_the_editor_content', 'format_for_editor', 10, 2 );
// WP < 4.3
} else {
$function = user_can_richedit() ? 'wp_richedit_pre' : 'wp_htmledit_pre';
add_filter('acf_the_editor_content', $function);
} else {
$function = user_can_richedit() ? 'wp_richedit_pre' : 'wp_htmledit_pre';
add_filter('acf_the_editor_content', $function, 10, 1);
}

echo '<div class="multi-language-field multi-language-field-wysiwyg">';
Expand All @@ -82,32 +110,25 @@ function create_field($field) {
}

foreach ($languages as $language):
$value = $values[$language];
$id = 'wysiwyg-' . $field['id'] . '-' . uniqid();
$name = $field['name'] . "[$language]";
$class = ($language === $currentLanguage) ? 'acf_wysiwyg wp-editor-wrap current-language' : 'acf_wysiwyg wp-editor-wrap';
$class = ($language === $currentLanguage) ? 'current-language' : '';
$value = apply_filters('acf_the_editor_content', $values[$language], 'tinymce');

?>
<div id="wp-<?php echo $id; ?>-wrap" class="<?php echo $class; ?>" data-toolbar="<?php echo $field['toolbar']; ?>" data-upload="<?php echo $field['media_upload']; ?>" data-language="<?php echo $language; ?>">
<?php if( user_can_richedit() && $field['media_upload'] == 'yes' ): ?>
<?php if( version_compare($wp_version, '3.3', '<') ): ?>
<div id="editor-toolbar">
<div id="media-buttons" class="hide-if-no-js">
<?php do_action( 'media_buttons' ); ?>
</div>
</div>
<?php else: ?>
<div id="wp-<?php echo $id; ?>-editor-tools" class="wp-editor-tools">
<div id="wp-<?php echo $id; ?>-media-buttons" class="hide-if-no-js wp-media-buttons">
<?php do_action( 'media_buttons' ); ?>
</div>
</div>
<div id="wp-<?php echo $id; ?>-wrap" class="acf_wysiwyg wp-core-ui wp-editor-wrap tmce-active <?php echo $class; ?>" data-toolbar="<?php echo $field['toolbar']; ?>" data-upload="<?php echo $field['media_upload']; ?>" data-language="<?php echo $language; ?>">
<div id="wp-<?php echo $id; ?>-editor-tools" class="wp-editor-tools hide-if-no-js">
<?php if( user_can_richedit() && $field['media_upload'] == 'yes' ): ?>
<div id="wp-<?php echo $id; ?>-media-buttons" class="wp-media-buttons">
<?php do_action( 'media_buttons', $id ); ?>
</div>
<?php endif; ?>
<?php endif; ?>
</div>
<div id="wp-<?php echo $id; ?>-editor-container" class="wp-editor-container">
<textarea id="<?php echo $id; ?>" class="qtx-wp-editor-area" name="<?php echo $name; ?>" ><?php echo apply_filters( 'acf_the_editor_content', $value, 'tinymce' ); ?></textarea>
<textarea id="<?php echo $id; ?>" class="qtx-wp-editor-area" name="<?php echo $name; ?>"><?php echo $value; ?></textarea>
</div>
</div>

<?php endforeach;

echo '</div>';
Expand Down

0 comments on commit feffec1

Please sign in to comment.