3 * WordPress Customize Control classes
6 * @subpackage Customize
11 * Customize Control class.
15 class WP_Customize_Control {
18 * Incremented with each new class instantiation, then stored in $instance_number.
20 * Used when sorting two instances whose priorities are equal.
28 protected static $instance_count = 0;
31 * Order in which this instance was created in relation to other instances.
37 public $instance_number;
41 * @var WP_Customize_Manager
52 * All settings tied to the control.
60 * The primary setting for the control (if there is one).
65 public $setting = 'default';
71 public $priority = 10;
89 public $description = '';
92 * @todo: Remove choices
97 public $choices = array();
103 public $input_attrs = array();
106 * @deprecated It is better to just call the json() method
110 public $json = array();
116 public $type = 'text';
124 * @see WP_Customize_Control::active()
126 * @var callable Callback is called with one argument, the instance of
127 * WP_Customize_Control, and returns bool to indicate whether
128 * the control is active (such as it relates to the URL
129 * currently being previewed).
131 public $active_callback = '';
136 * Supplied $args override class property defaults.
138 * If $args['settings'] is not defined, use the $id as the setting ID.
142 * @param WP_Customize_Manager $manager Customizer bootstrap instance.
143 * @param string $id Control ID.
144 * @param array $args Optional. Arguments to override class property defaults.
146 public function __construct( $manager, $id, $args = array() ) {
147 $keys = array_keys( get_object_vars( $this ) );
148 foreach ( $keys as $key ) {
149 if ( isset( $args[ $key ] ) ) {
150 $this->$key = $args[ $key ];
154 $this->manager = $manager;
156 if ( empty( $this->active_callback ) ) {
157 $this->active_callback = array( $this, 'active_callback' );
159 self::$instance_count += 1;
160 $this->instance_number = self::$instance_count;
163 if ( empty( $this->settings ) ) {
164 $this->settings = $id;
168 if ( is_array( $this->settings ) ) {
169 foreach ( $this->settings as $key => $setting ) {
170 $settings[ $key ] = $this->manager->get_setting( $setting );
173 $this->setting = $this->manager->get_setting( $this->settings );
174 $settings['default'] = $this->setting;
176 $this->settings = $settings;
180 * Enqueue control related scripts/styles.
184 public function enqueue() {}
187 * Check whether control is active to current Customizer preview.
192 * @return bool Whether the control is active to the current preview.
194 final public function active() {
196 $active = call_user_func( $this->active_callback, $this );
199 * Filter response of WP_Customize_Control::active().
203 * @param bool $active Whether the Customizer control is active.
204 * @param WP_Customize_Control $control WP_Customize_Control instance.
206 $active = apply_filters( 'customize_control_active', $active, $control );
212 * Default callback used when invoking WP_Customize_Control::active().
214 * Subclasses can override this with their specific logic, or they may
215 * provide an 'active_callback' argument to the constructor.
220 * @return true Always true.
222 public function active_callback() {
227 * Fetch a setting's value.
228 * Grabs the main setting by default.
232 * @param string $setting_key
233 * @return mixed The requested setting's value, if the setting exists.
235 final public function value( $setting_key = 'default' ) {
236 if ( isset( $this->settings[ $setting_key ] ) ) {
237 return $this->settings[ $setting_key ]->value();
242 * Refresh the parameters passed to the JavaScript via JSON.
246 public function to_json() {
247 $this->json['settings'] = array();
248 foreach ( $this->settings as $key => $setting ) {
249 $this->json['settings'][ $key ] = $setting->id;
252 $this->json['type'] = $this->type;
253 $this->json['priority'] = $this->priority;
254 $this->json['active'] = $this->active();
255 $this->json['section'] = $this->section;
256 $this->json['content'] = $this->get_content();
257 $this->json['label'] = $this->label;
258 $this->json['description'] = $this->description;
259 $this->json['instanceNumber'] = $this->instance_number;
263 * Get the data to export to the client via JSON.
267 * @return array Array of parameters passed to the JavaScript.
269 public function json() {
275 * Check if the theme supports the control and check user capabilities.
279 * @return bool False if theme doesn't support the control or user doesn't have the required permissions, otherwise true.
281 final public function check_capabilities() {
282 foreach ( $this->settings as $setting ) {
283 if ( ! $setting->check_capabilities() )
287 $section = $this->manager->get_section( $this->section );
288 if ( isset( $section ) && ! $section->check_capabilities() )
295 * Get the control's content for insertion into the Customizer pane.
299 * @return string Contents of the control.
301 final public function get_content() {
303 $this->maybe_render();
304 return trim( ob_get_clean() );
308 * Check capabilities and render the control.
311 * @uses WP_Customize_Control::render()
313 final public function maybe_render() {
314 if ( ! $this->check_capabilities() )
318 * Fires just before the current Customizer control is rendered.
322 * @param WP_Customize_Control $this WP_Customize_Control instance.
324 do_action( 'customize_render_control', $this );
327 * Fires just before a specific Customizer control is rendered.
329 * The dynamic portion of the hook name, `$this->id`, refers to
334 * @param WP_Customize_Control $this {@see WP_Customize_Control} instance.
336 do_action( 'customize_render_control_' . $this->id, $this );
342 * Renders the control wrapper and calls $this->render_content() for the internals.
346 protected function render() {
347 $id = 'customize-control-' . str_replace( array( '[', ']' ), array( '-', '' ), $this->id );
348 $class = 'customize-control customize-control-' . $this->type;
350 ?><li id="<?php echo esc_attr( $id ); ?>" class="<?php echo esc_attr( $class ); ?>">
351 <?php $this->render_content(); ?>
356 * Get the data link attribute for a setting.
360 * @param string $setting_key
361 * @return string Data link parameter, if $setting_key is a valid setting, empty string otherwise.
363 public function get_link( $setting_key = 'default' ) {
364 if ( ! isset( $this->settings[ $setting_key ] ) )
367 return 'data-customize-setting-link="' . esc_attr( $this->settings[ $setting_key ]->id ) . '"';
371 * Render the data link attribute for the control's input element.
374 * @uses WP_Customize_Control::get_link()
376 * @param string $setting_key
378 public function link( $setting_key = 'default' ) {
379 echo $this->get_link( $setting_key );
383 * Render the custom attributes for the control's input element.
388 public function input_attrs() {
389 foreach ( $this->input_attrs as $attr => $value ) {
390 echo $attr . '="' . esc_attr( $value ) . '" ';
395 * Render the control's content.
397 * Allows the content to be overriden without having to rewrite the wrapper in $this->render().
399 * Supports basic input types `text`, `checkbox`, `textarea`, `radio`, `select` and `dropdown-pages`.
400 * Additional input types such as `email`, `url`, `number`, `hidden` and `date` are supported implicitly.
402 * Control content can alternately be rendered in JS. See {@see WP_Customize_Control::print_template()}.
406 protected function render_content() {
407 switch( $this->type ) {
411 <input type="checkbox" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); checked( $this->value() ); ?> />
412 <?php echo esc_html( $this->label ); ?>
413 <?php if ( ! empty( $this->description ) ) : ?>
414 <span class="description customize-control-description"><?php echo $this->description; ?></span>
420 if ( empty( $this->choices ) )
423 $name = '_customize-radio-' . $this->id;
425 if ( ! empty( $this->label ) ) : ?>
426 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
428 if ( ! empty( $this->description ) ) : ?>
429 <span class="description customize-control-description"><?php echo $this->description ; ?></span>
432 foreach ( $this->choices as $value => $label ) :
435 <input type="radio" value="<?php echo esc_attr( $value ); ?>" name="<?php echo esc_attr( $name ); ?>" <?php $this->link(); checked( $this->value(), $value ); ?> />
436 <?php echo esc_html( $label ); ?><br/>
442 if ( empty( $this->choices ) )
447 <?php if ( ! empty( $this->label ) ) : ?>
448 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
450 if ( ! empty( $this->description ) ) : ?>
451 <span class="description customize-control-description"><?php echo $this->description; ?></span>
454 <select <?php $this->link(); ?>>
456 foreach ( $this->choices as $value => $label )
457 echo '<option value="' . esc_attr( $value ) . '"' . selected( $this->value(), $value, false ) . '>' . $label . '</option>';
466 <?php if ( ! empty( $this->label ) ) : ?>
467 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
469 if ( ! empty( $this->description ) ) : ?>
470 <span class="description customize-control-description"><?php echo $this->description; ?></span>
472 <textarea rows="5" <?php $this->link(); ?>><?php echo esc_textarea( $this->value() ); ?></textarea>
476 case 'dropdown-pages':
479 <?php if ( ! empty( $this->label ) ) : ?>
480 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
482 if ( ! empty( $this->description ) ) : ?>
483 <span class="description customize-control-description"><?php echo $this->description; ?></span>
486 <?php $dropdown = wp_dropdown_pages(
488 'name' => '_customize-dropdown-pages-' . $this->id,
490 'show_option_none' => __( '— Select —' ),
491 'option_none_value' => '0',
492 'selected' => $this->value(),
496 // Hackily add in the data link parameter.
497 $dropdown = str_replace( '<select', '<select ' . $this->get_link(), $dropdown );
506 <?php if ( ! empty( $this->label ) ) : ?>
507 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
509 if ( ! empty( $this->description ) ) : ?>
510 <span class="description customize-control-description"><?php echo $this->description; ?></span>
512 <input type="<?php echo esc_attr( $this->type ); ?>" <?php $this->input_attrs(); ?> value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); ?> />
520 * Render the control's JS template.
522 * This function is only run for control types that have been registered with
523 * {@see WP_Customize_Manager::register_control_type()}.
525 * In the future, this will also print the template for the control's container
526 * element and be override-able.
530 final public function print_template() {
532 <script type="text/html" id="tmpl-customize-control-<?php echo $this->type; ?>-content">
533 <?php $this->content_template(); ?>
539 * An Underscore (JS) template for this control's content (but not its container).
541 * Class variables for this control class are available in the `data` JS object;
542 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
544 * @see WP_Customize_Control::print_template()
548 protected function content_template() {}
552 /** WP_Customize_Color_Control class */
553 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-color-control.php' );
555 /** WP_Customize_Media_Control class */
556 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-media-control.php' );
558 /** WP_Customize_Upload_Control class */
559 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-upload-control.php' );
561 /** WP_Customize_Image_Control class */
562 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-image-control.php' );
564 /** WP_Customize_Background_Image_Control class */
565 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-background-image-control.php' );
567 /** WP_Customize_Cropped_Image_Control class */
568 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-cropped-image-control.php' );
570 /** WP_Customize_Site_Icon_Control class */
571 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-site-icon-control.php' );
573 /** WP_Customize_Header_Image_Control class */
574 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-header-image-control.php' );
576 /** WP_Customize_Theme_Control class */
577 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-theme-control.php' );
579 /** WP_Widget_Area_Customize_Control class */
580 require_once( ABSPATH . WPINC . '/customize/class-wp-widget-area-customize-control.php' );
582 /** WP_Widget_Form_Customize_Control class */
583 require_once( ABSPATH . WPINC . '/customize/class-wp-widget-form-customize-control.php' );
585 /** WP_Customize_Nav_Menu_Control class */
586 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-control.php' );
588 /** WP_Customize_Nav_Menu_Item_Control class */
589 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-item-control.php' );
591 /** WP_Customize_Nav_Menu_Location_Control class */
592 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-location-control.php' );
594 /** WP_Customize_Nav_Menu_Name_Control class */
595 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-name-control.php' );
597 /** WP_Customize_Nav_Menu_Auto_Add_Control class */
598 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-auto-add-control.php' );
600 /** WP_Customize_New_Menu_Control class */
601 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-new-menu-control.php' );