CMB2 Documentation
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  • Hooks
  • Download Docs
  • Github

Packages

  • CMB2
  • Demo
    • CMB2
  • None
  • Tests
    • CMB2

Classes

  • CMB2
  • CMB2_Ajax
  • CMB2_Base
  • CMB2_Bootstrap_2101
  • CMB2_Boxes
  • CMB2_Display_Checkbox
  • CMB2_Display_Colorpicker
  • CMB2_Display_File
  • CMB2_Display_File_List
  • CMB2_Display_Multicheck
  • CMB2_Display_oEmbed
  • CMB2_Display_Select
  • CMB2_Display_Taxonomy_Multicheck
  • CMB2_Display_Taxonomy_Radio
  • CMB2_Display_Text_Date
  • CMB2_Display_Text_Date_Timezone
  • CMB2_Display_Text_Money
  • CMB2_Display_Text_Time
  • CMB2_Display_Text_Url
  • CMB2_Display_Textarea
  • CMB2_Display_Textarea_Code
  • CMB2_Field
  • CMB2_Field_Display
  • CMB2_Hookup
  • CMB2_Hookup_Base
  • CMB2_Integration_Box
  • CMB2_JS
  • CMB2_Option
  • CMB2_Options
  • CMB2_Options_Hookup
  • CMB2_REST
  • CMB2_REST_Controller
  • CMB2_REST_Controller_Boxes
  • CMB2_REST_Controller_Fields
  • CMB2_Sanitize
  • CMB2_Show_Filters
  • CMB2_Type_Base
  • CMB2_Type_Checkbox
  • CMB2_Type_Colorpicker
  • CMB2_Type_Counter_Base
  • CMB2_Type_File
  • CMB2_Type_File_Base
  • CMB2_Type_File_List
  • CMB2_Type_Multi_Base
  • CMB2_Type_Multicheck
  • CMB2_Type_Oembed
  • CMB2_Type_Picker_Base
  • CMB2_Type_Radio
  • CMB2_Type_Select
  • CMB2_Type_Select_Timezone
  • CMB2_Type_Taxonomy_Base
  • CMB2_Type_Taxonomy_Multicheck
  • CMB2_Type_Taxonomy_Multicheck_Hierarchical
  • CMB2_Type_Taxonomy_Radio
  • CMB2_Type_Taxonomy_Radio_Hierarchical
  • CMB2_Type_Taxonomy_Select
  • CMB2_Type_Taxonomy_Select_Hierarchical
  • CMB2_Type_Text
  • CMB2_Type_Text_Date
  • CMB2_Type_Text_Datetime_Timestamp
  • CMB2_Type_Text_Datetime_Timestamp_Timezone
  • CMB2_Type_Text_Time
  • CMB2_Type_Textarea
  • CMB2_Type_Textarea_Code
  • CMB2_Type_Title
  • CMB2_Type_Wysiwyg
  • CMB2_Types
  • CMB2_Utils

Hooks

  • Hook Reference
  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
/**
 * CMB wysiwyg field type
 *
 * @since  2.2.2
 *
 * @category  WordPress_Plugin
 * @package   CMB2
 * @author    CMB2 team
 * @license   GPL-2.0+
 * @link      https://cmb2.io
 *
 * @method string _id()
 * @method string _desc()
 */
class CMB2_Type_Wysiwyg extends CMB2_Type_Textarea {

    /**
     * Handles outputting a 'wysiwyg' element
     * @since  1.1.0
     * @return string Form wysiwyg element
     */
    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'] );
        }

        // Character counter not currently working for grouped WYSIWYG
        $this->field->args['char_counter'] = false;

        // wysiwyg fields in a group need some special handling.
        $field->add_js_dependencies( array( 'wp-util', 'cmb2-wysiwyg' ) );

        // Hook in our template-output to the footer.
        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;

        // Initate the editor with special id/value/name so we can retrieve the options in JS.
        $editor = $this->get_wp_editor( array(
            'value'   => 'cmb2_v_' . $group_id . $field_id,
            'id'      => 'cmb2_i_' . $group_id . $field_id,
            'options' => $options,
        ) );

        // Then replace the special id/value/name with underscore placeholders.
        $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 );

        // And put the editor instance in a JS template wrapper.
        echo '<script type="text/template" id="tmpl-cmb2-wysiwyg-' . $group_id . '-' . $field_id . '">';
        // Need to wrap the template in a wrapper div w/ specific data attributes which will be used when adding/removing rows.
        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>';
    }

}
CMB2 Documentation API documentation generated by ApiGen