wp_editor.php 2.8 KB

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