3 * Customize Control Class
6 * @subpackage Customize
9 class WP_Customize_Control {
12 * @var WP_Customize_Manager
23 * All settings tied to the control.
31 * The primary setting for the control (if there is one).
36 public $setting = 'default';
42 public $priority = 10;
57 * @todo: Remove choices
62 public $choices = array();
68 public $json = array();
74 public $type = 'text';
80 * Supplied $args override class property defaults.
82 * If $args['settings'] is not defined, use the $id as the setting ID.
86 * @param WP_Customize_Manager $manager
90 function __construct( $manager, $id, $args = array() ) {
91 $keys = array_keys( get_object_vars( $this ) );
92 foreach ( $keys as $key ) {
93 if ( isset( $args[ $key ] ) )
94 $this->$key = $args[ $key ];
97 $this->manager = $manager;
101 if ( empty( $this->settings ) )
102 $this->settings = $id;
105 if ( is_array( $this->settings ) ) {
106 foreach ( $this->settings as $key => $setting ) {
107 $settings[ $key ] = $this->manager->get_setting( $setting );
110 $this->setting = $this->manager->get_setting( $this->settings );
111 $settings['default'] = $this->setting;
113 $this->settings = $settings;
117 * Enqueue control related scripts/styles.
121 public function enqueue() {}
125 * Fetch a setting's value.
126 * Grabs the main setting by default.
130 * @param string $setting_key
131 * @return mixed The requested setting's value, if the setting exists.
133 public final function value( $setting_key = 'default' ) {
134 if ( isset( $this->settings[ $setting_key ] ) )
135 return $this->settings[ $setting_key ]->value();
139 * Refresh the parameters passed to the JavaScript via JSON.
143 public function to_json() {
144 $this->json['settings'] = array();
145 foreach ( $this->settings as $key => $setting ) {
146 $this->json['settings'][ $key ] = $setting->id;
149 $this->json['type'] = $this->type;
153 * Check if the theme supports the control and check user capabilities.
157 * @return bool False if theme doesn't support the control or user doesn't have the required permissions, otherwise true.
159 public final function check_capabilities() {
160 foreach ( $this->settings as $setting ) {
161 if ( ! $setting->check_capabilities() )
165 $section = $this->manager->get_section( $this->section );
166 if ( isset( $section ) && ! $section->check_capabilities() )
173 * Check capabilities and render the control.
176 * @uses WP_Customize_Control::render()
178 public final function maybe_render() {
179 if ( ! $this->check_capabilities() )
183 * Fires just before the current Customizer control is rendered.
187 * @param WP_Customize_Control $this WP_Customize_Control instance.
189 do_action( 'customize_render_control', $this );
192 * Fires just before a specific Customizer control is rendered.
194 * The dynamic portion of the hook name, $this->id, refers to
199 * @param WP_Customize_Control $this WP_Customize_Control instance.
201 do_action( 'customize_render_control_' . $this->id, $this );
207 * Renders the control wrapper and calls $this->render_content() for the internals.
211 protected function render() {
212 $id = 'customize-control-' . str_replace( '[', '-', str_replace( ']', '', $this->id ) );
213 $class = 'customize-control customize-control-' . $this->type;
215 ?><li id="<?php echo esc_attr( $id ); ?>" class="<?php echo esc_attr( $class ); ?>">
216 <?php $this->render_content(); ?>
221 * Get the data link attribute for a setting.
225 * @param string $setting_key
226 * @return string Data link parameter, if $setting_key is a valid setting, empty string otherwise.
228 public function get_link( $setting_key = 'default' ) {
229 if ( ! isset( $this->settings[ $setting_key ] ) )
232 return 'data-customize-setting-link="' . esc_attr( $this->settings[ $setting_key ]->id ) . '"';
236 * Render the data link attribute for the control's input element.
239 * @uses WP_Customize_Control::get_link()
241 * @param string $setting_key
243 public function link( $setting_key = 'default' ) {
244 echo $this->get_link( $setting_key );
248 * Render the control's content.
250 * Allows the content to be overriden without having to rewrite the wrapper in $this->render().
252 * Supports basic input types `text`, `checkbox`, `radio`, `select` and `dropdown-pages`.
256 protected function render_content() {
257 switch( $this->type ) {
261 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
262 <input type="text" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); ?> />
269 <input type="checkbox" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); checked( $this->value() ); ?> />
270 <?php echo esc_html( $this->label ); ?>
275 if ( empty( $this->choices ) )
278 $name = '_customize-radio-' . $this->id;
281 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
283 foreach ( $this->choices as $value => $label ) :
286 <input type="radio" value="<?php echo esc_attr( $value ); ?>" name="<?php echo esc_attr( $name ); ?>" <?php $this->link(); checked( $this->value(), $value ); ?> />
287 <?php echo esc_html( $label ); ?><br/>
293 if ( empty( $this->choices ) )
298 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
299 <select <?php $this->link(); ?>>
301 foreach ( $this->choices as $value => $label )
302 echo '<option value="' . esc_attr( $value ) . '"' . selected( $this->value(), $value, false ) . '>' . $label . '</option>';
308 case 'dropdown-pages':
309 $dropdown = wp_dropdown_pages(
311 'name' => '_customize-dropdown-pages-' . $this->id,
313 'show_option_none' => __( '— Select —' ),
314 'option_none_value' => '0',
315 'selected' => $this->value(),
319 // Hackily add in the data link parameter.
320 $dropdown = str_replace( '<select', '<select ' . $this->get_link(), $dropdown );
323 '<label class="customize-control-select"><span class="customize-control-title">%s</span> %s</label>',
333 * Customize Color Control Class
336 * @subpackage Customize
339 class WP_Customize_Color_Control extends WP_Customize_Control {
344 public $type = 'color';
356 * @uses WP_Customize_Control::__construct()
358 * @param WP_Customize_Manager $manager
362 public function __construct( $manager, $id, $args = array() ) {
363 $this->statuses = array( '' => __('Default') );
364 parent::__construct( $manager, $id, $args );
368 * Enqueue scripts/styles for the color picker.
372 public function enqueue() {
373 wp_enqueue_script( 'wp-color-picker' );
374 wp_enqueue_style( 'wp-color-picker' );
378 * Refresh the parameters passed to the JavaScript via JSON.
381 * @uses WP_Customize_Control::to_json()
383 public function to_json() {
385 $this->json['statuses'] = $this->statuses;
389 * Render the control's content.
393 public function render_content() {
394 $this_default = $this->setting->default;
396 if ( $this_default ) {
397 if ( false === strpos( $this_default, '#' ) )
398 $this_default = '#' . $this_default;
399 $default_attr = ' data-default-color="' . esc_attr( $this_default ) . '"';
401 // The input's value gets set by JS. Don't fill it.
404 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
405 <div class="customize-control-content">
406 <input class="color-picker-hex" type="text" maxlength="7" placeholder="<?php esc_attr_e( 'Hex Value' ); ?>"<?php echo $default_attr; ?> />
414 * Customize Upload Control Class
417 * @subpackage Customize
420 class WP_Customize_Upload_Control extends WP_Customize_Control {
421 public $type = 'upload';
422 public $removed = '';
424 public $extensions = array();
427 * Enqueue control related scripts/styles.
431 public function enqueue() {
432 wp_enqueue_script( 'wp-plupload' );
436 * Refresh the parameters passed to the JavaScript via JSON.
439 * @uses WP_Customize_Control::to_json()
441 public function to_json() {
444 $this->json['removed'] = $this->removed;
446 if ( $this->context )
447 $this->json['context'] = $this->context;
449 if ( $this->extensions )
450 $this->json['extensions'] = implode( ',', $this->extensions );
454 * Render the control's content.
458 public function render_content() {
461 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
463 <a href="#" class="button-secondary upload"><?php _e( 'Upload' ); ?></a>
464 <a href="#" class="remove"><?php _e( 'Remove' ); ?></a>
472 * Customize Image Control Class
475 * @subpackage Customize
478 class WP_Customize_Image_Control extends WP_Customize_Upload_Control {
479 public $type = 'image';
482 public $extensions = array( 'jpg', 'jpeg', 'gif', 'png' );
484 protected $tabs = array();
490 * @uses WP_Customize_Upload_Control::__construct()
492 * @param WP_Customize_Manager $manager
496 public function __construct( $manager, $id, $args ) {
497 $this->statuses = array( '' => __('No Image') );
499 parent::__construct( $manager, $id, $args );
501 $this->add_tab( 'upload-new', __('Upload New'), array( $this, 'tab_upload_new' ) );
502 $this->add_tab( 'uploaded', __('Uploaded'), array( $this, 'tab_uploaded' ) );
504 // Early priority to occur before $this->manager->prepare_controls();
505 add_action( 'customize_controls_init', array( $this, 'prepare_control' ), 5 );
509 * Prepares the control.
511 * If no tabs exist, removes the control from the manager.
515 public function prepare_control() {
517 $this->manager->remove_control( $this->id );
521 * Refresh the parameters passed to the JavaScript via JSON.
524 * @uses WP_Customize_Upload_Control::to_json()
526 public function to_json() {
528 $this->json['statuses'] = $this->statuses;
532 * Render the control's content.
536 public function render_content() {
537 $src = $this->value();
538 if ( isset( $this->get_url ) )
539 $src = call_user_func( $this->get_url, $src );
542 <div class="customize-image-picker">
543 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
545 <div class="customize-control-content">
546 <div class="dropdown preview-thumbnail" tabindex="0">
547 <div class="dropdown-content">
548 <?php if ( empty( $src ) ): ?>
549 <img style="display:none;" />
551 <img src="<?php echo esc_url( set_url_scheme( $src ) ); ?>" />
553 <div class="dropdown-status"></div>
555 <div class="dropdown-arrow"></div>
559 <div class="library">
561 <?php foreach ( $this->tabs as $id => $tab ): ?>
562 <li data-customize-tab='<?php echo esc_attr( $id ); ?>' tabindex='0'>
563 <?php echo esc_html( $tab['label'] ); ?>
567 <?php foreach ( $this->tabs as $id => $tab ): ?>
568 <div class="library-content" data-customize-tab='<?php echo esc_attr( $id ); ?>'>
569 <?php call_user_func( $tab['callback'] ); ?>
574 <div class="actions">
575 <a href="#" class="remove"><?php _e( 'Remove Image' ); ?></a>
582 * Add a tab to the control.
587 * @param string $label
588 * @param mixed $callback
590 public function add_tab( $id, $label, $callback ) {
591 $this->tabs[ $id ] = array(
593 'callback' => $callback,
598 * Remove a tab from the control.
604 public function remove_tab( $id ) {
605 unset( $this->tabs[ $id ] );
611 public function tab_upload_new() {
612 if ( ! _device_can_upload() ) {
613 echo '<p>' . sprintf( __('The web browser on your device cannot be used to upload files. You may be able to use the <a href="%s">native app for your device</a> instead.'), 'https://wordpress.org/mobile/' ) . '</p>';
616 <div class="upload-dropzone">
617 <?php _e('Drop a file here or <a href="#" class="upload">select a file</a>.'); ?>
619 <div class="upload-fallback">
620 <span class="button-secondary"><?php _e('Select File'); ?></span>
629 public function tab_uploaded() {
631 <div class="uploaded-target"></div>
639 * @param string $thumbnail_url
641 public function print_tab_image( $url, $thumbnail_url = null ) {
642 $url = set_url_scheme( $url );
643 $thumbnail_url = ( $thumbnail_url ) ? set_url_scheme( $thumbnail_url ) : $url;
645 <a href="#" class="thumbnail" data-customize-image-value="<?php echo esc_url( $url ); ?>">
646 <img src="<?php echo esc_url( $thumbnail_url ); ?>" />
653 * Customize Background Image Control Class
656 * @subpackage Customize
659 class WP_Customize_Background_Image_Control extends WP_Customize_Image_Control {
665 * @uses WP_Customize_Image_Control::__construct()
667 * @param WP_Customize_Manager $manager
669 public function __construct( $manager ) {
670 parent::__construct( $manager, 'background_image', array(
671 'label' => __( 'Background Image' ),
672 'section' => 'background_image',
673 'context' => 'custom-background',
674 'get_url' => 'get_background_image',
677 if ( $this->setting->default )
678 $this->add_tab( 'default', __('Default'), array( $this, 'tab_default_background' ) );
684 public function tab_uploaded() {
685 $backgrounds = get_posts( array(
686 'post_type' => 'attachment',
687 'meta_key' => '_wp_attachment_is_custom_background',
688 'meta_value' => $this->manager->get_stylesheet(),
693 ?><div class="uploaded-target"></div><?php
695 if ( empty( $backgrounds ) )
698 foreach ( (array) $backgrounds as $background )
699 $this->print_tab_image( esc_url_raw( $background->guid ) );
704 * @uses WP_Customize_Image_Control::print_tab_image()
706 public function tab_default_background() {
707 $this->print_tab_image( $this->setting->default );
711 class WP_Customize_Header_Image_Control extends WP_Customize_Image_Control {
712 public $type = 'header';
714 public function __construct( $manager ) {
715 parent::__construct( $manager, 'header_image', array(
716 'label' => __( 'Header Image' ),
718 'default' => 'header_image',
719 'data' => 'header_image_data',
721 'section' => 'header_image',
722 'context' => 'custom-header',
723 'removed' => 'remove-header',
724 'get_url' => 'get_header_image',
729 public function to_json() {
733 public function enqueue() {
735 wp_enqueue_script( 'customize-views' );
737 $this->prepare_control();
739 wp_localize_script( 'customize-views', '_wpCustomizeHeader', array(
741 'width' => absint( get_theme_support( 'custom-header', 'width' ) ),
742 'height' => absint( get_theme_support( 'custom-header', 'height' ) ),
743 'flex-width' => absint( get_theme_support( 'custom-header', 'flex-width' ) ),
744 'flex-height' => absint( get_theme_support( 'custom-header', 'flex-height' ) ),
745 'currentImgSrc' => $this->get_current_image_src(),
748 'add' => wp_create_nonce( 'header-add' ),
749 'remove' => wp_create_nonce( 'header-remove' ),
751 'uploads' => $this->uploaded_headers,
752 'defaults' => $this->default_headers
758 public function prepare_control() {
759 global $custom_image_header;
760 if ( empty( $custom_image_header ) ) {
764 // Process default headers and uploaded headers.
765 $custom_image_header->process_default_headers();
766 $this->default_headers = $custom_image_header->get_default_header_images();
767 $this->uploaded_headers = $custom_image_header->get_uploaded_header_images();
770 function print_header_image_template() {
772 <script type="text/template" id="tmpl-header-choice">
773 <# if (data.random) { #>
774 <button type="button" class="button display-options random">
775 <span class="dashicons dashicons-randomize dice"></span>
776 <# if ( data.type === 'uploaded' ) { #>
777 <?php _e( 'Randomize uploaded headers' ); ?>
778 <# } else if ( data.type === 'default' ) { #>
779 <?php _e( 'Randomize suggested headers' ); ?>
785 <# if (data.type === 'uploaded') { #>
786 <div class="dashicons dashicons-no close"></div>
789 <button type="button" class="choice thumbnail"
790 data-customize-image-value="{{{data.header.url}}}"
791 data-customize-header-image-data="{{JSON.stringify(data.header)}}">
792 <span class="screen-reader-text"><?php _e( 'Set image' ); ?></span>
793 <img src="{{{data.header.thumbnail_url}}}" alt="{{{data.header.alt_text || data.header.description}}}">
799 <script type="text/template" id="tmpl-header-current">
800 <# if (data.choice) { #>
801 <# if (data.random) { #>
803 <div class="placeholder">
805 <span><span class="dashicons dashicons-randomize dice"></span>
806 <# if ( data.type === 'uploaded' ) { #>
807 <?php _e( 'Randomizing uploaded headers' ); ?>
808 <# } else if ( data.type === 'default' ) { #>
809 <?php _e( 'Randomizing suggested headers' ); ?>
817 <img src="{{{data.header.thumbnail_url}}}" alt="{{{data.header.alt_text || data.header.description}}}" tabindex="0"/>
822 <div class="placeholder">
825 <?php _e( 'No image set' ); ?>
835 public function get_current_image_src() {
836 $src = $this->value();
837 if ( isset( $this->get_url ) ) {
838 $src = call_user_func( $this->get_url, $src );
844 public function render_content() {
845 $this->print_header_image_template();
846 $visibility = $this->get_current_image_src() ? '' : ' style="display:none" ';
847 $width = absint( get_theme_support( 'custom-header', 'width' ) );
848 $height = absint( get_theme_support( 'custom-header', 'height' ) );
852 <div class="customize-control-content">
853 <p class="customizer-section-intro">
855 if ( $width && $height ) {
856 printf( __( 'While you can crop images to your liking after clicking <strong>Add new</strong>, your theme recommends a header size of <strong>%s × %s</strong> pixels.' ), $width, $height );
857 } elseif ( $width ) {
858 printf( __( 'While you can crop images to your liking after clicking <strong>Add new</strong>, your theme recommends a header width of <strong>%s</strong> pixels.' ), $width );
860 printf( __( 'While you can crop images to your liking after clicking <strong>Add new</strong>, your theme recommends a header height of <strong>%s</strong> pixels.' ), $height );
864 <div class="current">
865 <span class="customize-control-title">
866 <?php _e( 'Current header' ); ?>
868 <div class="container">
871 <div class="actions">
872 <?php /* translators: Hide as in hide header image via the Customizer */ ?>
873 <button type="button"<?php echo $visibility ?> class="button remove"><?php _ex( 'Hide image', 'custom header' ); ?></button>
874 <?php /* translators: New as in add new header image via the Customizer */ ?>
875 <button type="button" class="button new"><?php _ex( 'Add new image', 'header image' ); ?></button>
876 <div style="clear:both"></div>
878 <div class="choices">
879 <span class="customize-control-title header-previously-uploaded">
880 <?php _ex( 'Previously uploaded', 'custom headers' ); ?>
882 <div class="uploaded">
886 <span class="customize-control-title header-default">
887 <?php _ex( 'Suggested', 'custom headers' ); ?>
889 <div class="default">
900 * Widget Area Customize Control Class
903 class WP_Widget_Area_Customize_Control extends WP_Customize_Control {
904 public $type = 'sidebar_widgets';
907 public function to_json() {
909 $exported_properties = array( 'sidebar_id' );
910 foreach ( $exported_properties as $key ) {
911 $this->json[ $key ] = $this->$key;
915 public function render_content() {
917 <span class="button-secondary add-new-widget" tabindex="0">
918 <?php _e( 'Add a Widget' ); ?>
921 <span class="reorder-toggle" tabindex="0">
922 <span class="reorder"><?php _ex( 'Reorder', 'Reorder widgets in Customizer' ); ?></span>
923 <span class="reorder-done"><?php _ex( 'Done', 'Cancel reordering widgets in Customizer' ); ?></span>
930 * Widget Form Customize Control Class
932 class WP_Widget_Form_Customize_Control extends WP_Customize_Control {
933 public $type = 'widget_form';
935 public $widget_id_base;
937 public $is_new = false;
940 public $is_wide = false;
942 public function to_json() {
944 $exported_properties = array( 'widget_id', 'widget_id_base', 'sidebar_id', 'width', 'height', 'is_wide' );
945 foreach ( $exported_properties as $key ) {
946 $this->json[ $key ] = $this->$key;
950 public function render_content() {
951 global $wp_registered_widgets;
952 require_once ABSPATH . '/wp-admin/includes/widgets.php';
954 $widget = $wp_registered_widgets[ $this->widget_id ];
955 if ( ! isset( $widget['params'][0] ) ) {
956 $widget['params'][0] = array();
960 'widget_id' => $widget['id'],
961 'widget_name' => $widget['name'],
964 $args = wp_list_widget_controls_dynamic_sidebar( array( 0 => $args, 1 => $widget['params'][0] ) );
965 echo $this->manager->widgets->get_widget_control( $args );