class-wp-customize-color-control.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Customize API: WP_Customize_Color_Control class
  4. *
  5. * @package WordPress
  6. * @subpackage Customize
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Customize Color Control class.
  11. *
  12. * @since 3.4.0
  13. *
  14. * @see WP_Customize_Control
  15. */
  16. class WP_Customize_Color_Control extends WP_Customize_Control {
  17. /**
  18. * Type.
  19. *
  20. * @var string
  21. */
  22. public $type = 'color';
  23. /**
  24. * Statuses.
  25. *
  26. * @var array
  27. */
  28. public $statuses;
  29. /**
  30. * Mode.
  31. *
  32. * @since 4.7.0
  33. * @var string
  34. */
  35. public $mode = 'full';
  36. /**
  37. * Constructor.
  38. *
  39. * @since 3.4.0
  40. * @uses WP_Customize_Control::__construct()
  41. *
  42. * @param WP_Customize_Manager $manager Customizer bootstrap instance.
  43. * @param string $id Control ID.
  44. * @param array $args Optional. Arguments to override class property defaults.
  45. */
  46. public function __construct( $manager, $id, $args = array() ) {
  47. $this->statuses = array( '' => __( 'Default' ) );
  48. parent::__construct( $manager, $id, $args );
  49. }
  50. /**
  51. * Enqueue scripts/styles for the color picker.
  52. *
  53. * @since 3.4.0
  54. */
  55. public function enqueue() {
  56. wp_enqueue_script( 'wp-color-picker' );
  57. wp_enqueue_style( 'wp-color-picker' );
  58. }
  59. /**
  60. * Refresh the parameters passed to the JavaScript via JSON.
  61. *
  62. * @since 3.4.0
  63. * @uses WP_Customize_Control::to_json()
  64. */
  65. public function to_json() {
  66. parent::to_json();
  67. $this->json['statuses'] = $this->statuses;
  68. $this->json['defaultValue'] = $this->setting->default;
  69. $this->json['mode'] = $this->mode;
  70. }
  71. /**
  72. * Don't render the control content from PHP, as it's rendered via JS on load.
  73. *
  74. * @since 3.4.0
  75. */
  76. public function render_content() {}
  77. /**
  78. * Render a JS template for the content of the color picker control.
  79. *
  80. * @since 4.1.0
  81. */
  82. public function content_template() {
  83. ?>
  84. <# var defaultValue = '#RRGGBB', defaultValueAttr = '',
  85. isHueSlider = data.mode === 'hue';
  86. if ( data.defaultValue && _.isString( data.defaultValue ) && ! isHueSlider ) {
  87. if ( '#' !== data.defaultValue.substring( 0, 1 ) ) {
  88. defaultValue = '#' + data.defaultValue;
  89. } else {
  90. defaultValue = data.defaultValue;
  91. }
  92. defaultValueAttr = ' data-default-color=' + defaultValue; // Quotes added automatically.
  93. } #>
  94. <# if ( data.label ) { #>
  95. <span class="customize-control-title">{{{ data.label }}}</span>
  96. <# } #>
  97. <# if ( data.description ) { #>
  98. <span class="description customize-control-description">{{{ data.description }}}</span>
  99. <# } #>
  100. <div class="customize-control-content">
  101. <label><span class="screen-reader-text">{{{ data.label }}}</span>
  102. <# if ( isHueSlider ) { #>
  103. <input class="color-picker-hue" type="text" data-type="hue" />
  104. <# } else { #>
  105. <input class="color-picker-hex" type="text" maxlength="7" placeholder="{{ defaultValue }}" {{ defaultValueAttr }} />
  106. <# } #>
  107. </label>
  108. </div>
  109. <?php
  110. }
  111. }