1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
<?php
class CMB2_Type_Wysiwyg extends CMB2_Type_Textarea {
public function render( $args = array() ) {
$field = $this->field;
$a = $this->parse_args( 'wysiwyg', array(
'id' => $this->_id( '', false ),
'value' => $field->escaped_value( 'stripslashes' ),
'desc' => $this->_desc( true ),
'options' => $field->options(),
) );
if ( ! $field->group ) {
$a = $this->maybe_update_attributes_for_char_counter( $a );
if ( $this->has_counter ) {
$a['options']['editor_class'] = ! empty( $a['options']['editor_class'] )
? $a['options']['editor_class'] . ' cmb2-count-chars'
: 'cmb2-count-chars';
}
return $this->rendered( $this->get_wp_editor( $a ) . $a['desc'] );
}
$this->field->args['char_counter'] = false;
$field->add_js_dependencies( array( 'wp-util', 'cmb2-wysiwyg' ) );
add_action( is_admin() ? 'admin_footer' : 'wp_footer', array( $this, 'add_wysiwyg_template_for_group' ) );
return $this->rendered(
sprintf( '<div class="cmb2-wysiwyg-wrap">%s', parent::render( array(
'class' => 'cmb2_textarea cmb2-wysiwyg-placeholder',
'data-groupid' => $field->group->id(),
'data-iterator' => $field->group->index,
'data-fieldid' => $field->id( true ),
'desc' => '</div>' . $this->_desc( true ),
) ) )
);
}
protected function get_wp_editor( $args ) {
ob_start();
wp_editor( $args['value'], $args['id'], $args['options'] );
return ob_get_clean();
}
public function add_wysiwyg_template_for_group() {
$group_id = $this->field->group->id();
$field_id = $this->field->id( true );
$hash = $this->field->hash_id();
$options = $this->field->options();
$options['textarea_name'] = 'cmb2_n_' . $group_id . $field_id;
$editor = $this->get_wp_editor( array(
'value' => 'cmb2_v_' . $group_id . $field_id,
'id' => 'cmb2_i_' . $group_id . $field_id,
'options' => $options,
) );
$editor = str_replace( array(
'cmb2_n_' . $group_id . $field_id,
'cmb2_v_' . $group_id . $field_id,
'cmb2_i_' . $group_id . $field_id,
), array(
'{{ data.name }}',
'{{{ data.value }}}',
'{{ data.id }}',
), $editor );
echo '<script type="text/template" id="tmpl-cmb2-wysiwyg-' . $group_id . '-' . $field_id . '">';
echo '<div class="cmb2-wysiwyg-inner-wrap" data-iterator="{{ data.iterator }}" data-groupid="' . $group_id . '" data-id="' . $field_id . '" data-hash="' . $hash . '">' . $editor . '</div>';
echo '</script>';
}
}