Advertisement
_Jackky

mb-jackky-tinymce-field.php

Apr 22nd, 2021
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.75 KB | None | 0 0
  1. <?php
  2.  
  3. if ( class_exists( 'RWMB_Field' ) ) {
  4.     class RWMB_Jackky_Tinymce_Field extends RWMB_Field {
  5.  
  6.         /**
  7.          * Get field HTML.
  8.          *
  9.          * @param mixed $meta  Meta value.
  10.          * @param array $field Field parameters.
  11.          * @return string
  12.          */
  13.         public static function html( $meta, $field ) {
  14.             // Using output buffering because wp_editor() echos directly.
  15.             ob_start();
  16.    
  17.             $attributes = self::get_attributes( $field );
  18.    
  19.             $options = [
  20.                 'editor_class'  => 'jackky-tinymce rwmb-jackky-tinymce', // rwmb-jackky-tinymce class is for clone fix
  21.                 'dfw'           => true,
  22.             ];
  23.  
  24.             $options['textarea_name'] = $field['field_name'];
  25.             if ( ! empty( $attributes['required'] ) )
  26.             {
  27.                 $options['editor_class'] .= ' jackky-tinymce-required';
  28.             }
  29.  
  30.             $settings = json_encode( $field['settings'] );
  31.  
  32.             if ( isset($field['settings']['tinymce']) )
  33.             {
  34.                 $options = array_merge( $options, $field['settings']['tinymce'] ); // a hotfix to load wp_editor more properly
  35.             }
  36.    
  37.             wp_editor( $meta, $attributes['id'], $options );
  38.             echo '<script class="jackky-tinymce-id" type="text/html" data-id="' . esc_attr( $attributes['id'] ) . '" data-settings=\'' . $settings . '\'></script>';
  39.    
  40.             return ob_get_clean();
  41.         }
  42.  
  43.         /**
  44.          * Escape meta for field output.
  45.          *
  46.          * @param mixed $meta Meta value.
  47.          * @return mixed
  48.          */
  49.         public static function esc_meta( $meta ) {
  50.             return $meta;
  51.         }
  52.    
  53.         /**
  54.          * Normalize parameters for field.
  55.          *
  56.          * @param array $field Field parameters.
  57.          * @return array
  58.          */
  59.         public static function normalize( $field ) {
  60.             $field = parent::normalize( $field );
  61.             $field = wp_parse_args(
  62.                 $field,
  63.                 array(
  64.                     'raw'           => false,
  65.                     'settings'      => [],
  66.                 )
  67.             );
  68.    
  69.             return $field;
  70.         }
  71.  
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement