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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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(
  46. array_merge(
  47. array(
  48. 'type' => $this->code_type,
  49. 'codemirror' => array(
  50. 'indentUnit' => 2,
  51. 'tabSize' => 2,
  52. ),
  53. ),
  54. $this->editor_settings
  55. )
  56. );
  57. }
  58. /**
  59. * Refresh the parameters passed to the JavaScript via JSON.
  60. *
  61. * @since 4.9.0
  62. *
  63. * @see WP_Customize_Control::json()
  64. *
  65. * @return array Array of parameters passed to the JavaScript.
  66. */
  67. public function json() {
  68. $json = parent::json();
  69. $json['editor_settings'] = $this->editor_settings;
  70. $json['input_attrs'] = $this->input_attrs;
  71. return $json;
  72. }
  73. /**
  74. * Don't render the control content from PHP, as it's rendered via JS on load.
  75. *
  76. * @since 4.9.0
  77. */
  78. public function render_content() {}
  79. /**
  80. * Render a JS template for control display.
  81. *
  82. * @since 4.9.0
  83. */
  84. public function content_template() {
  85. ?>
  86. <# var elementIdPrefix = 'el' + String( Math.random() ); #>
  87. <# if ( data.label ) { #>
  88. <label for="{{ elementIdPrefix }}_editor" class="customize-control-title">
  89. {{ data.label }}
  90. </label>
  91. <# } #>
  92. <# if ( data.description ) { #>
  93. <span class="description customize-control-description">{{{ data.description }}}</span>
  94. <# } #>
  95. <div class="customize-control-notifications-container"></div>
  96. <textarea id="{{ elementIdPrefix }}_editor"
  97. <# _.each( _.extend( { 'class': 'code' }, data.input_attrs ), function( value, key ) { #>
  98. {{{ key }}}="{{ value }}"
  99. <# }); #>
  100. ></textarea>
  101. <?php
  102. }
  103. }