code_editor.php 2.0 KB

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