| 1 |
<?php if ( ! defined( 'ABSPATH' ) ) { |
| 2 |
die; } // Cannot access directly. |
| 3 |
/** |
| 4 |
* |
| 5 |
* Field: code_editor |
| 6 |
* |
| 7 |
* @since 1.0.0 |
| 8 |
* @version 1.0.0 |
| 9 |
*/ |
| 10 |
if ( ! class_exists( 'ADMINIFY_Field_code_editor' ) ) { |
| 11 |
class ADMINIFY_Field_code_editor extends ADMINIFY_Fields { |
| 12 |
|
| 13 |
public $version = '5.65.2'; |
| 14 |
public $cdn_url = 'https://cdn.jsdelivr.net/npm/codemirror@'; |
| 15 |
|
| 16 |
public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) { |
| 17 |
parent::__construct( $field, $value, $unique, $where, $parent ); |
| 18 |
} |
| 19 |
|
| 20 |
public function render() { |
| 21 |
$default_settings = [ |
| 22 |
'tabSize' => 2, |
| 23 |
'lineNumbers' => true, |
| 24 |
'theme' => 'default', |
| 25 |
'mode' => 'htmlmixed', |
| 26 |
'cdnURL' => $this->cdn_url . $this->version, |
| 27 |
]; |
| 28 |
|
| 29 |
$settings = ( ! empty( $this->field['settings'] ) ) ? $this->field['settings'] : []; |
| 30 |
$settings = wp_parse_args( $settings, $default_settings ); |
| 31 |
|
| 32 |
echo wp_kses_post( $this->field_before() ); |
| 33 |
echo '<textarea name="' . esc_attr( $this->field_name() ) . '"' . esc_attr( $this->field_attributes() ) . ' data-editor="' . esc_attr( json_encode( $settings ) ) . '">' . wp_kses_post( $this->value ) . '</textarea>'; |
| 34 |
echo wp_kses_post( $this->field_after() ); |
| 35 |
} |
| 36 |
|
| 37 |
public function enqueue() { |
| 38 |
$page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; |
| 39 |
|
| 40 |
// Do not loads CodeMirror in revslider page. |
| 41 |
if ( in_array( $page, [ 'revslider' ] ) ) { |
| 42 |
return; } |
| 43 |
|
| 44 |
if ( ! wp_script_is( 'adminify-codemirror' ) ) { |
| 45 |
wp_enqueue_script( 'adminify-codemirror', esc_url( $this->cdn_url . $this->version . '/lib/codemirror.min.js' ), [ 'adminify' ], $this->version, true ); |
| 46 |
wp_enqueue_script( 'adminify-codemirror-loadmode', esc_url( $this->cdn_url . $this->version . '/addon/mode/loadmode.min.js' ), [ 'adminify-codemirror' ], $this->version, true ); |
| 47 |
} |
| 48 |
|
| 49 |
if ( ! wp_style_is( 'adminify-codemirror' ) ) { |
| 50 |
wp_enqueue_style( 'adminify-codemirror', esc_url( $this->cdn_url . $this->version . '/lib/codemirror.min.css' ), [], $this->version ); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
} |
| 55 |
} |
| 56 |
|