class-wp-customize-code-editor-control.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Customize API: WP_Customize_Code_Editor_Control class
  4. *
  5. * @package WordPress
  6. * @subpackage Customize
  7. * @since 4.9.0
  8. */
  9. /**
  10. * Customize Code Editor Control class.
  11. *
  12. * @since 4.9.0
  13. *
  14. * @see WP_Customize_Control
  15. */
  16. class WP_Customize_Code_Editor_Control extends WP_Customize_Control {
  17. /**
  18. * Customize control type.
  19. *
  20. * @since 4.9.0
  21. * @var string
  22. */
  23. public $type = 'code_editor';
  24. /**
  25. * Type of code that is being edited.
  26. *
  27. * @since 4.9.0
  28. * @var string
  29. */
  30. public $code_type = '';
  31. /**
  32. * Code editor settings.
  33. *
  34. * @see wp_enqueue_code_editor()
  35. * @since 4.9.0
  36. * @var array|false
  37. */
  38. public $editor_settings = array();
  39. /**
  40. * Enqueue control related scripts/styles.
  41. *
  42. * @since 4.9.0
  43. */
  44. public function enqueue() {
  45. $this->editor_settings = wp_enqueue_code_editor( array_merge(
  46. array(
  47. 'type' => $this->code_type,
  48. 'codemirror' => array(
  49. 'indentUnit' => 2,
  50. 'tabSize' => 2,
  51. ),
  52. ),
  53. $this->editor_settings
  54. ) );
  55. }
  56. /**
  57. * Refresh the parameters passed to the JavaScript via JSON.
  58. *
  59. * @since 4.9.0
  60. * @see WP_Customize_Control::json()
  61. *
  62. * @return array Array of parameters passed to the JavaScript.
  63. */
  64. public function json() {
  65. $json = parent::json();
  66. $json['editor_settings'] = $this->editor_settings;
  67. $json['input_attrs'] = $this->input_attrs;
  68. return $json;
  69. }
  70. /**
  71. * Don't render the control content from PHP, as it's rendered via JS on load.
  72. *
  73. * @since 4.9.0
  74. */
  75. public function render_content() {}
  76. /**
  77. * Render a JS template for control display.
  78. *
  79. * @since 4.9.0
  80. */
  81. public function content_template() {
  82. ?>
  83. <# var elementIdPrefix = 'el' + String( Math.random() ); #>
  84. <# if ( data.label ) { #>
  85. <label for="{{ elementIdPrefix }}_editor" class="customize-control-title">
  86. {{ data.label }}
  87. </label>
  88. <# } #>
  89. <# if ( data.description ) { #>
  90. <span class="description customize-control-description">{{{ data.description }}}</span>
  91. <# } #>
  92. <div class="customize-control-notifications-container"></div>
  93. <textarea id="{{ elementIdPrefix }}_editor"
  94. <# _.each( _.extend( { 'class': 'code' }, data.input_attrs ), function( value, key ) { #>
  95. {{{ key }}}="{{ value }}"
  96. <# }); #>
  97. ></textarea>
  98. <?php
  99. }
  100. }