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';
68 * Capability required to use this control.
70 * Normally this is empty and the capability is derived from the capabilities
71 * of the associated `$settings`.
83 public $priority = 10;
101 public $description = '';
104 * @todo: Remove choices
109 public $choices = array();
115 public $input_attrs = array();
118 * @deprecated It is better to just call the json() method
122 public $json = array();
128 public $type = 'text';
136 * @see WP_Customize_Control::active()
138 * @var callable Callback is called with one argument, the instance of
139 * WP_Customize_Control, and returns bool to indicate whether
140 * the control is active (such as it relates to the URL
141 * currently being previewed).
143 public $active_callback = '';
148 * Supplied `$args` override class property defaults.
150 * If `$args['settings']` is not defined, use the $id as the setting ID.
154 * @param WP_Customize_Manager $manager Customizer bootstrap instance.
155 * @param string $id Control ID.
156 * @param array $args {
157 * Optional. Arguments to override class property defaults.
159 * @type int $instance_number Order in which this instance was created in relation
160 * to other instances.
161 * @type WP_Customize_Manager $manager Customizer bootstrap instance.
162 * @type string $id Control ID.
163 * @type array $settings All settings tied to the control. If undefined, `$id` will
165 * @type string $setting The primary setting for the control (if there is one).
167 * @type int $priority Order priority to load the control. Default 10.
168 * @type string $section Section the control belongs to. Default empty.
169 * @type string $label Label for the control. Default empty.
170 * @type string $description Description for the control. Default empty.
171 * @type array $choices List of choices for 'radio' or 'select' type controls, where
172 * values are the keys, and labels are the values.
173 * Default empty array.
174 * @type array $input_attrs List of custom input attributes for control output, where
175 * attribute names are the keys and values are the values. Not
176 * used for 'checkbox', 'radio', 'select', 'textarea', or
177 * 'dropdown-pages' control types. Default empty array.
178 * @type array $json Deprecated. Use WP_Customize_Control::json() instead.
179 * @type string $type Control type. Core controls include 'text', 'checkbox',
180 * 'textarea', 'radio', 'select', and 'dropdown-pages'. Additional
181 * input types such as 'email', 'url', 'number', 'hidden', and
182 * 'date' are supported implicitly. Default 'text'.
185 public function __construct( $manager, $id, $args = array() ) {
186 $keys = array_keys( get_object_vars( $this ) );
187 foreach ( $keys as $key ) {
188 if ( isset( $args[ $key ] ) ) {
189 $this->$key = $args[ $key ];
193 $this->manager = $manager;
195 if ( empty( $this->active_callback ) ) {
196 $this->active_callback = array( $this, 'active_callback' );
198 self::$instance_count += 1;
199 $this->instance_number = self::$instance_count;
202 if ( ! isset( $this->settings ) ) {
203 $this->settings = $id;
207 if ( is_array( $this->settings ) ) {
208 foreach ( $this->settings as $key => $setting ) {
209 $settings[ $key ] = $this->manager->get_setting( $setting );
211 } else if ( is_string( $this->settings ) ) {
212 $this->setting = $this->manager->get_setting( $this->settings );
213 $settings['default'] = $this->setting;
215 $this->settings = $settings;
219 * Enqueue control related scripts/styles.
223 public function enqueue() {}
226 * Check whether control is active to current Customizer preview.
231 * @return bool Whether the control is active to the current preview.
233 final public function active() {
235 $active = call_user_func( $this->active_callback, $this );
238 * Filters response of WP_Customize_Control::active().
242 * @param bool $active Whether the Customizer control is active.
243 * @param WP_Customize_Control $control WP_Customize_Control instance.
245 $active = apply_filters( 'customize_control_active', $active, $control );
251 * Default callback used when invoking WP_Customize_Control::active().
253 * Subclasses can override this with their specific logic, or they may
254 * provide an 'active_callback' argument to the constructor.
259 * @return true Always true.
261 public function active_callback() {
266 * Fetch a setting's value.
267 * Grabs the main setting by default.
271 * @param string $setting_key
272 * @return mixed The requested setting's value, if the setting exists.
274 final public function value( $setting_key = 'default' ) {
275 if ( isset( $this->settings[ $setting_key ] ) ) {
276 return $this->settings[ $setting_key ]->value();
281 * Refresh the parameters passed to the JavaScript via JSON.
285 public function to_json() {
286 $this->json['settings'] = array();
287 foreach ( $this->settings as $key => $setting ) {
288 $this->json['settings'][ $key ] = $setting->id;
291 $this->json['type'] = $this->type;
292 $this->json['priority'] = $this->priority;
293 $this->json['active'] = $this->active();
294 $this->json['section'] = $this->section;
295 $this->json['content'] = $this->get_content();
296 $this->json['label'] = $this->label;
297 $this->json['description'] = $this->description;
298 $this->json['instanceNumber'] = $this->instance_number;
302 * Get the data to export to the client via JSON.
306 * @return array Array of parameters passed to the JavaScript.
308 public function json() {
314 * Checks if the user can use this control.
316 * Returns false if the user cannot manipulate one of the associated settings,
317 * or if one of the associated settings does not exist. Also returns false if
318 * the associated section does not exist or if its capability check returns
323 * @return bool False if theme doesn't support the control or user doesn't have the required permissions, otherwise true.
325 final public function check_capabilities() {
326 if ( ! empty( $this->capability ) && ! current_user_can( $this->capability ) ) {
330 foreach ( $this->settings as $setting ) {
331 if ( ! $setting || ! $setting->check_capabilities() ) {
336 $section = $this->manager->get_section( $this->section );
337 if ( isset( $section ) && ! $section->check_capabilities() ) {
345 * Get the control's content for insertion into the Customizer pane.
349 * @return string Contents of the control.
351 final public function get_content() {
353 $this->maybe_render();
354 return trim( ob_get_clean() );
358 * Check capabilities and render the control.
361 * @uses WP_Customize_Control::render()
363 final public function maybe_render() {
364 if ( ! $this->check_capabilities() )
368 * Fires just before the current Customizer control is rendered.
372 * @param WP_Customize_Control $this WP_Customize_Control instance.
374 do_action( 'customize_render_control', $this );
377 * Fires just before a specific Customizer control is rendered.
379 * The dynamic portion of the hook name, `$this->id`, refers to
384 * @param WP_Customize_Control $this WP_Customize_Control instance.
386 do_action( 'customize_render_control_' . $this->id, $this );
392 * Renders the control wrapper and calls $this->render_content() for the internals.
396 protected function render() {
397 $id = 'customize-control-' . str_replace( array( '[', ']' ), array( '-', '' ), $this->id );
398 $class = 'customize-control customize-control-' . $this->type;
400 ?><li id="<?php echo esc_attr( $id ); ?>" class="<?php echo esc_attr( $class ); ?>">
401 <?php $this->render_content(); ?>
406 * Get the data link attribute for a setting.
410 * @param string $setting_key
411 * @return string Data link parameter, if $setting_key is a valid setting, empty string otherwise.
413 public function get_link( $setting_key = 'default' ) {
414 if ( ! isset( $this->settings[ $setting_key ] ) )
417 return 'data-customize-setting-link="' . esc_attr( $this->settings[ $setting_key ]->id ) . '"';
421 * Render the data link attribute for the control's input element.
424 * @uses WP_Customize_Control::get_link()
426 * @param string $setting_key
428 public function link( $setting_key = 'default' ) {
429 echo $this->get_link( $setting_key );
433 * Render the custom attributes for the control's input element.
438 public function input_attrs() {
439 foreach ( $this->input_attrs as $attr => $value ) {
440 echo $attr . '="' . esc_attr( $value ) . '" ';
445 * Render the control's content.
447 * Allows the content to be overriden without having to rewrite the wrapper in `$this::render()`.
449 * Supports basic input types `text`, `checkbox`, `textarea`, `radio`, `select` and `dropdown-pages`.
450 * Additional input types such as `email`, `url`, `number`, `hidden` and `date` are supported implicitly.
452 * Control content can alternately be rendered in JS. See WP_Customize_Control::print_template().
456 protected function render_content() {
457 switch( $this->type ) {
461 <input type="checkbox" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); checked( $this->value() ); ?> />
462 <?php echo esc_html( $this->label ); ?>
463 <?php if ( ! empty( $this->description ) ) : ?>
464 <span class="description customize-control-description"><?php echo $this->description; ?></span>
470 if ( empty( $this->choices ) )
473 $name = '_customize-radio-' . $this->id;
475 if ( ! empty( $this->label ) ) : ?>
476 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
478 if ( ! empty( $this->description ) ) : ?>
479 <span class="description customize-control-description"><?php echo $this->description ; ?></span>
482 foreach ( $this->choices as $value => $label ) :
485 <input type="radio" value="<?php echo esc_attr( $value ); ?>" name="<?php echo esc_attr( $name ); ?>" <?php $this->link(); checked( $this->value(), $value ); ?> />
486 <?php echo esc_html( $label ); ?><br/>
492 if ( empty( $this->choices ) )
497 <?php if ( ! empty( $this->label ) ) : ?>
498 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
500 if ( ! empty( $this->description ) ) : ?>
501 <span class="description customize-control-description"><?php echo $this->description; ?></span>
504 <select <?php $this->link(); ?>>
506 foreach ( $this->choices as $value => $label )
507 echo '<option value="' . esc_attr( $value ) . '"' . selected( $this->value(), $value, false ) . '>' . $label . '</option>';
516 <?php if ( ! empty( $this->label ) ) : ?>
517 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
519 if ( ! empty( $this->description ) ) : ?>
520 <span class="description customize-control-description"><?php echo $this->description; ?></span>
522 <textarea rows="5" <?php $this->link(); ?>><?php echo esc_textarea( $this->value() ); ?></textarea>
526 case 'dropdown-pages':
529 <?php if ( ! empty( $this->label ) ) : ?>
530 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
532 if ( ! empty( $this->description ) ) : ?>
533 <span class="description customize-control-description"><?php echo $this->description; ?></span>
536 <?php $dropdown = wp_dropdown_pages(
538 'name' => '_customize-dropdown-pages-' . $this->id,
540 'show_option_none' => __( '— Select —' ),
541 'option_none_value' => '0',
542 'selected' => $this->value(),
546 // Hackily add in the data link parameter.
547 $dropdown = str_replace( '<select', '<select ' . $this->get_link(), $dropdown );
556 <?php if ( ! empty( $this->label ) ) : ?>
557 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
559 if ( ! empty( $this->description ) ) : ?>
560 <span class="description customize-control-description"><?php echo $this->description; ?></span>
562 <input type="<?php echo esc_attr( $this->type ); ?>" <?php $this->input_attrs(); ?> value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); ?> />
570 * Render the control's JS template.
572 * This function is only run for control types that have been registered with
573 * WP_Customize_Manager::register_control_type().
575 * In the future, this will also print the template for the control's container
576 * element and be override-able.
580 final public function print_template() {
582 <script type="text/html" id="tmpl-customize-control-<?php echo $this->type; ?>-content">
583 <?php $this->content_template(); ?>
589 * An Underscore (JS) template for this control's content (but not its container).
591 * Class variables for this control class are available in the `data` JS object;
592 * export custom variables by overriding WP_Customize_Control::to_json().
594 * @see WP_Customize_Control::print_template()
598 protected function content_template() {}
602 /** WP_Customize_Color_Control class */
603 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-color-control.php' );
605 /** WP_Customize_Media_Control class */
606 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-media-control.php' );
608 /** WP_Customize_Upload_Control class */
609 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-upload-control.php' );
611 /** WP_Customize_Image_Control class */
612 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-image-control.php' );
614 /** WP_Customize_Background_Image_Control class */
615 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-background-image-control.php' );
617 /** WP_Customize_Cropped_Image_Control class */
618 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-cropped-image-control.php' );
620 /** WP_Customize_Site_Icon_Control class */
621 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-site-icon-control.php' );
623 /** WP_Customize_Header_Image_Control class */
624 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-header-image-control.php' );
626 /** WP_Customize_Theme_Control class */
627 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-theme-control.php' );
629 /** WP_Widget_Area_Customize_Control class */
630 require_once( ABSPATH . WPINC . '/customize/class-wp-widget-area-customize-control.php' );
632 /** WP_Widget_Form_Customize_Control class */
633 require_once( ABSPATH . WPINC . '/customize/class-wp-widget-form-customize-control.php' );
635 /** WP_Customize_Nav_Menu_Control class */
636 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-control.php' );
638 /** WP_Customize_Nav_Menu_Item_Control class */
639 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-item-control.php' );
641 /** WP_Customize_Nav_Menu_Location_Control class */
642 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-location-control.php' );
644 /** WP_Customize_Nav_Menu_Name_Control class */
645 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-name-control.php' );
647 /** WP_Customize_Nav_Menu_Auto_Add_Control class */
648 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-auto-add-control.php' );
650 /** WP_Customize_New_Menu_Control class */
651 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-new-menu-control.php' );