| 1 |
<?php if ( ! defined( 'ABSPATH' ) ) { |
| 2 |
die; } // Cannot access directly. |
| 3 |
/** |
| 4 |
* |
| 5 |
* Field: wp_editor |
| 6 |
* |
| 7 |
* @since 1.0.0 |
| 8 |
* @version 1.0.0 |
| 9 |
*/ |
| 10 |
if ( ! class_exists( 'ADMINIFY_Field_wp_editor' ) ) { |
| 11 |
class ADMINIFY_Field_wp_editor extends ADMINIFY_Fields { |
| 12 |
|
| 13 |
public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) { |
| 14 |
parent::__construct( $field, $value, $unique, $where, $parent ); |
| 15 |
} |
| 16 |
|
| 17 |
public function render() { |
| 18 |
$args = wp_parse_args( |
| 19 |
$this->field, |
| 20 |
[ |
| 21 |
'tinymce' => true, |
| 22 |
'quicktags' => true, |
| 23 |
'media_buttons' => true, |
| 24 |
'wpautop' => false, |
| 25 |
'height' => '', |
| 26 |
] |
| 27 |
); |
| 28 |
|
| 29 |
$attributes = [ |
| 30 |
'rows' => 10, |
| 31 |
'class' => 'wp-editor-area', |
| 32 |
'autocomplete' => 'off', |
| 33 |
]; |
| 34 |
|
| 35 |
$editor_height = ( ! empty( $args['height'] ) ) ? ' style="height:' . esc_attr( $args['height'] ) . ';"' : ''; |
| 36 |
|
| 37 |
$editor_settings = [ |
| 38 |
'tinymce' => $args['tinymce'], |
| 39 |
'quicktags' => $args['quicktags'], |
| 40 |
'media_buttons' => $args['media_buttons'], |
| 41 |
'wpautop' => $args['wpautop'], |
| 42 |
]; |
| 43 |
|
| 44 |
echo wp_kses_post( $this->field_before() ); |
| 45 |
|
| 46 |
echo ( adminify_wp_editor_api() ) ? '<div class="adminify-wp-editor" data-editor-settings="' . esc_attr( json_encode( $editor_settings ) ) . '">' : ''; |
| 47 |
|
| 48 |
echo '<textarea name="' . esc_attr( $this->field_name() ) . '"' . wp_kses_post( $this->field_attributes( $attributes )) . esc_attr( $editor_height ) . '>' . wp_kses_post( $this->value ) . '</textarea>'; |
| 49 |
|
| 50 |
echo ( adminify_wp_editor_api() ) ? '</div>' : ''; |
| 51 |
|
| 52 |
echo wp_kses_post( $this->field_after() ); |
| 53 |
} |
| 54 |
|
| 55 |
public function enqueue() { |
| 56 |
if ( adminify_wp_editor_api() && function_exists( 'wp_enqueue_editor' ) ) { |
| 57 |
wp_enqueue_editor(); |
| 58 |
|
| 59 |
$this->setup_wp_editor_settings(); |
| 60 |
|
| 61 |
add_action( 'print_default_editor_scripts', [ $this, 'setup_wp_editor_media_buttons' ] ); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
// Setup wp editor media buttons |
| 66 |
public function setup_wp_editor_media_buttons() { |
| 67 |
if ( ! function_exists( 'media_buttons' ) ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
ob_start(); |
| 72 |
echo '<div class="wp-media-buttons">'; |
| 73 |
do_action( 'media_buttons' ); |
| 74 |
echo '</div>'; |
| 75 |
$media_buttons = ob_get_clean(); |
| 76 |
|
| 77 |
echo '<script type="text/javascript">'; |
| 78 |
echo 'var adminify_media_buttons = ' . json_encode( $media_buttons ) . ';'; |
| 79 |
echo '</script>'; |
| 80 |
} |
| 81 |
|
| 82 |
// Setup wp editor settings |
| 83 |
public function setup_wp_editor_settings() { |
| 84 |
if ( adminify_wp_editor_api() && class_exists( '_WP_Editors' ) ) { |
| 85 |
$defaults = apply_filters( |
| 86 |
'adminify_wp_editor', |
| 87 |
[ |
| 88 |
'tinymce' => [ |
| 89 |
'wp_skip_init' => true, |
| 90 |
], |
| 91 |
] |
| 92 |
); |
| 93 |
|
| 94 |
$setup = _WP_Editors::parse_settings( 'adminify_wp_editor', $defaults ); |
| 95 |
|
| 96 |
_WP_Editors::editor_settings( 'adminify_wp_editor', $setup ); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
} |
| 101 |
} |
| 102 |
|