CMB2_Type_Wysiwyg

CMB wysiwyg field type.

Defined (1)

The class is defined in the following location(s).

/includes/types/CMB2_Type_Wysiwyg.php  
  1. class CMB2_Type_Wysiwyg extends CMB2_Type_Textarea { 
  2.  
  3. /** 
  4. * Handles outputting a 'wysiwyg' element 
  5. * @since 1.1.0 
  6. * @param array $args Override arguments 
  7. * @return string Form wysiwyg element 
  8. */ 
  9. public function render() { 
  10. $field = $this->field; 
  11. $a = $this->parse_args( 'wysiwyg', array( 
  12. 'id' => $this->_id(),  
  13. 'value' => $field->escaped_value( 'stripslashes' ),  
  14. 'desc' => $this->_desc( true ),  
  15. 'options' => $field->options(),  
  16. ) ); 
  17.  
  18. if ( ! $field->group ) { 
  19. return $this->rendered( $this->get_wp_editor( $a ) . $a['desc'] ); 
  20.  
  21. // wysiwyg fields in a group need some special handling. 
  22.  
  23. $field->add_js_dependencies( 'wp-util' ); 
  24. $field->add_js_dependencies( 'cmb2-wysiwyg' ); 
  25.  
  26. // Hook in our template-output to the footer. 
  27. add_action( is_admin() ? 'admin_footer' : 'wp_footer', array( $this, 'add_wysiwyg_template_for_group' ) ); 
  28.  
  29. return $this->rendered( 
  30. sprintf( '<div class="cmb2-wysiwyg-wrap">%s', parent::render( array( 
  31. 'class' => 'cmb2_textarea cmb2-wysiwyg-placeholder',  
  32. 'data-groupid' => $field->group->id(),  
  33. 'data-iterator' => $field->group->index,  
  34. 'data-fieldid' => $field->id( true ),  
  35. 'desc' => '</div>' . $this->_desc( true ),  
  36. ) ) ) 
  37. ); 
  38.  
  39. protected function get_wp_editor( $args ) { 
  40. ob_start(); 
  41. wp_editor( $args['value'], $args['id'], $args['options'] ); 
  42. return ob_get_clean(); 
  43.  
  44. public function add_wysiwyg_template_for_group() { 
  45. $group_id = $this->field->group->id(); 
  46. $field_id = $this->field->id( true ); 
  47. $options = $this->field->options(); 
  48. $options['textarea_name'] = 'cmb2_n_' . $group_id . $field_id; 
  49.  
  50. // Initate the editor with special id/value/name so we can retrieve the options in JS. 
  51. $editor = $this->get_wp_editor( array( 
  52. 'value' => 'cmb2_v_' . $group_id . $field_id,  
  53. 'id' => 'cmb2_i_' . $group_id . $field_id,  
  54. 'options' => $options,  
  55. ) ); 
  56.  
  57. // Then replace the special id/value/name with underscore placeholders. 
  58. $editor = str_replace( array( 
  59. 'cmb2_n_' . $group_id . $field_id,  
  60. 'cmb2_v_' . $group_id . $field_id,  
  61. 'cmb2_i_' . $group_id . $field_id,  
  62. ), array( 
  63. '{{ data.name }}',  
  64. '{{{ data.value }}}',  
  65. '{{ data.id }}',  
  66. ), $editor ); 
  67.  
  68. // And put the editor instance in a JS template wrapper. 
  69. echo '<script type="text/template" id="tmpl-cmb2-wysiwyg-' . $group_id . '-' . $field_id . '">'; 
  70. // Need to wrap the template in a wrapper div w/ specific data attributes which will be used when adding/removing rows. 
  71. echo '<div class="cmb2-wysiwyg-inner-wrap" data-iterator="{{ data.iterator }}" data-groupid="'. $group_id .'" data-id="'. $field_id .'">'. $editor .'</div>'; 
  72. echo '</script>'; 
  73.