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;
60 public $description = '';
63 * @todo: Remove choices
68 public $choices = array();
74 public $input_attrs = array();
80 public $json = array();
86 public $type = 'text';
94 * @see WP_Customize_Control::active()
96 * @var callable Callback is called with one argument, the instance of
97 * WP_Customize_Control, and returns bool to indicate whether
98 * the control is active (such as it relates to the URL
99 * currently being previewed).
101 public $active_callback = '';
106 * Supplied $args override class property defaults.
108 * If $args['settings'] is not defined, use the $id as the setting ID.
112 * @param WP_Customize_Manager $manager
116 public function __construct( $manager, $id, $args = array() ) {
117 $keys = array_keys( get_object_vars( $this ) );
118 foreach ( $keys as $key ) {
119 if ( isset( $args[ $key ] ) ) {
120 $this->$key = $args[ $key ];
124 $this->manager = $manager;
126 if ( empty( $this->active_callback ) ) {
127 $this->active_callback = array( $this, 'active_callback' );
131 if ( empty( $this->settings ) ) {
132 $this->settings = $id;
136 if ( is_array( $this->settings ) ) {
137 foreach ( $this->settings as $key => $setting ) {
138 $settings[ $key ] = $this->manager->get_setting( $setting );
141 $this->setting = $this->manager->get_setting( $this->settings );
142 $settings['default'] = $this->setting;
144 $this->settings = $settings;
148 * Enqueue control related scripts/styles.
152 public function enqueue() {}
155 * Check whether control is active to current customizer preview.
160 * @return bool Whether the control is active to the current preview.
162 public final function active() {
164 $active = call_user_func( $this->active_callback, $this );
167 * Filter response of WP_Customize_Control::active().
171 * @param bool $active Whether the Customizer control is active.
172 * @param WP_Customize_Control $control WP_Customize_Control instance.
174 $active = apply_filters( 'customize_control_active', $active, $control );
180 * Default callback used when invoking WP_Customize_Control::active().
182 * Subclasses can override this with their specific logic, or they may
183 * provide an 'active_callback' argument to the constructor.
188 * @return bool Always true.
190 public function active_callback() {
195 * Fetch a setting's value.
196 * Grabs the main setting by default.
200 * @param string $setting_key
201 * @return mixed The requested setting's value, if the setting exists.
203 public final function value( $setting_key = 'default' ) {
204 if ( isset( $this->settings[ $setting_key ] ) ) {
205 return $this->settings[ $setting_key ]->value();
210 * Refresh the parameters passed to the JavaScript via JSON.
214 public function to_json() {
215 $this->json['settings'] = array();
216 foreach ( $this->settings as $key => $setting ) {
217 $this->json['settings'][ $key ] = $setting->id;
220 $this->json['type'] = $this->type;
221 $this->json['active'] = $this->active();
225 * Check if the theme supports the control and check user capabilities.
229 * @return bool False if theme doesn't support the control or user doesn't have the required permissions, otherwise true.
231 public final function check_capabilities() {
232 foreach ( $this->settings as $setting ) {
233 if ( ! $setting->check_capabilities() )
237 $section = $this->manager->get_section( $this->section );
238 if ( isset( $section ) && ! $section->check_capabilities() )
245 * Check capabilities and render the control.
248 * @uses WP_Customize_Control::render()
250 public final function maybe_render() {
251 if ( ! $this->check_capabilities() )
255 * Fires just before the current Customizer control is rendered.
259 * @param WP_Customize_Control $this WP_Customize_Control instance.
261 do_action( 'customize_render_control', $this );
264 * Fires just before a specific Customizer control is rendered.
266 * The dynamic portion of the hook name, $this->id, refers to
271 * @param WP_Customize_Control $this WP_Customize_Control instance.
273 do_action( 'customize_render_control_' . $this->id, $this );
279 * Renders the control wrapper and calls $this->render_content() for the internals.
283 protected function render() {
284 $id = 'customize-control-' . str_replace( '[', '-', str_replace( ']', '', $this->id ) );
285 $class = 'customize-control customize-control-' . $this->type;
287 ?><li id="<?php echo esc_attr( $id ); ?>" class="<?php echo esc_attr( $class ); ?>">
288 <?php $this->render_content(); ?>
293 * Get the data link attribute for a setting.
297 * @param string $setting_key
298 * @return string Data link parameter, if $setting_key is a valid setting, empty string otherwise.
300 public function get_link( $setting_key = 'default' ) {
301 if ( ! isset( $this->settings[ $setting_key ] ) )
304 return 'data-customize-setting-link="' . esc_attr( $this->settings[ $setting_key ]->id ) . '"';
308 * Render the data link attribute for the control's input element.
311 * @uses WP_Customize_Control::get_link()
313 * @param string $setting_key
315 public function link( $setting_key = 'default' ) {
316 echo $this->get_link( $setting_key );
320 * Render the custom attributes for the control's input element.
325 public function input_attrs() {
326 foreach( $this->input_attrs as $attr => $value ) {
327 echo $attr . '="' . esc_attr( $value ) . '" ';
332 * Render the control's content.
334 * Allows the content to be overriden without having to rewrite the wrapper in $this->render().
336 * Supports basic input types `text`, `checkbox`, `textarea`, `radio`, `select` and `dropdown-pages`.
337 * Additional input types such as `email`, `url`, `number`, `hidden` and `date` are supported implicitly.
341 protected function render_content() {
342 switch( $this->type ) {
346 <input type="checkbox" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); checked( $this->value() ); ?> />
347 <?php echo esc_html( $this->label ); ?>
348 <?php if ( ! empty( $this->description ) ) : ?>
349 <span class="description customize-control-description"><?php echo $this->description; ?></span>
355 if ( empty( $this->choices ) )
358 $name = '_customize-radio-' . $this->id;
360 if ( ! empty( $this->label ) ) : ?>
361 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
363 if ( ! empty( $this->description ) ) : ?>
364 <span class="description customize-control-description"><?php echo $this->description ; ?></span>
367 foreach ( $this->choices as $value => $label ) :
370 <input type="radio" value="<?php echo esc_attr( $value ); ?>" name="<?php echo esc_attr( $name ); ?>" <?php $this->link(); checked( $this->value(), $value ); ?> />
371 <?php echo esc_html( $label ); ?><br/>
377 if ( empty( $this->choices ) )
382 <?php if ( ! empty( $this->label ) ) : ?>
383 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
385 if ( ! empty( $this->description ) ) : ?>
386 <span class="description customize-control-description"><?php echo $this->description; ?></span>
389 <select <?php $this->link(); ?>>
391 foreach ( $this->choices as $value => $label )
392 echo '<option value="' . esc_attr( $value ) . '"' . selected( $this->value(), $value, false ) . '>' . $label . '</option>';
401 <?php if ( ! empty( $this->label ) ) : ?>
402 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
404 if ( ! empty( $this->description ) ) : ?>
405 <span class="description customize-control-description"><?php echo $this->description; ?></span>
407 <textarea rows="5" <?php $this->link(); ?>><?php echo esc_textarea( $this->value() ); ?></textarea>
411 case 'dropdown-pages':
412 $dropdown = wp_dropdown_pages(
414 'name' => '_customize-dropdown-pages-' . $this->id,
416 'show_option_none' => __( '— Select —' ),
417 'option_none_value' => '0',
418 'selected' => $this->value(),
422 // Hackily add in the data link parameter.
423 $dropdown = str_replace( '<select', '<select ' . $this->get_link(), $dropdown );
426 '<label class="customize-control-select"><span class="customize-control-title">%s</span> %s</label>',
434 <?php if ( ! empty( $this->label ) ) : ?>
435 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
437 if ( ! empty( $this->description ) ) : ?>
438 <span class="description customize-control-description"><?php echo $this->description; ?></span>
440 <input type="<?php echo esc_attr( $this->type ); ?>" <?php $this->input_attrs(); ?> value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); ?> />
449 * Customize Color Control Class
452 * @subpackage Customize
455 class WP_Customize_Color_Control extends WP_Customize_Control {
460 public $type = 'color';
472 * @uses WP_Customize_Control::__construct()
474 * @param WP_Customize_Manager $manager
478 public function __construct( $manager, $id, $args = array() ) {
479 $this->statuses = array( '' => __('Default') );
480 parent::__construct( $manager, $id, $args );
484 * Enqueue scripts/styles for the color picker.
488 public function enqueue() {
489 wp_enqueue_script( 'wp-color-picker' );
490 wp_enqueue_style( 'wp-color-picker' );
494 * Refresh the parameters passed to the JavaScript via JSON.
497 * @uses WP_Customize_Control::to_json()
499 public function to_json() {
501 $this->json['statuses'] = $this->statuses;
505 * Render the control's content.
509 public function render_content() {
510 $this_default = $this->setting->default;
512 if ( $this_default ) {
513 if ( false === strpos( $this_default, '#' ) )
514 $this_default = '#' . $this_default;
515 $default_attr = ' data-default-color="' . esc_attr( $this_default ) . '"';
517 // The input's value gets set by JS. Don't fill it.
520 <?php if ( ! empty( $this->label ) ) : ?>
521 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
523 if ( ! empty( $this->description ) ) : ?>
524 <span class="description customize-control-description"><?php echo $this->description; ?></span>
527 <div class="customize-control-content">
528 <input class="color-picker-hex" type="text" maxlength="7" placeholder="<?php esc_attr_e( 'Hex Value' ); ?>"<?php echo $default_attr; ?> />
536 * Customize Upload Control Class
539 * @subpackage Customize
542 class WP_Customize_Upload_Control extends WP_Customize_Control {
543 public $type = 'upload';
544 public $removed = '';
546 public $extensions = array();
549 * Enqueue control related scripts/styles.
553 public function enqueue() {
554 wp_enqueue_script( 'wp-plupload' );
558 * Refresh the parameters passed to the JavaScript via JSON.
561 * @uses WP_Customize_Control::to_json()
563 public function to_json() {
566 $this->json['removed'] = $this->removed;
568 if ( $this->context )
569 $this->json['context'] = $this->context;
571 if ( $this->extensions )
572 $this->json['extensions'] = implode( ',', $this->extensions );
576 * Render the control's content.
580 public function render_content() {
583 <?php if ( ! empty( $this->label ) ) : ?>
584 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
586 if ( ! empty( $this->description ) ) : ?>
587 <span class="description customize-control-description"><?php echo $this->description; ?></span>
590 <a href="#" class="button-secondary upload"><?php _e( 'Upload' ); ?></a>
591 <a href="#" class="remove"><?php _e( 'Remove' ); ?></a>
599 * Customize Image Control Class
602 * @subpackage Customize
605 class WP_Customize_Image_Control extends WP_Customize_Upload_Control {
606 public $type = 'image';
609 public $extensions = array( 'jpg', 'jpeg', 'gif', 'png' );
611 protected $tabs = array();
617 * @uses WP_Customize_Upload_Control::__construct()
619 * @param WP_Customize_Manager $manager
623 public function __construct( $manager, $id, $args ) {
624 $this->statuses = array( '' => __('No Image') );
626 parent::__construct( $manager, $id, $args );
628 $this->add_tab( 'upload-new', __('Upload New'), array( $this, 'tab_upload_new' ) );
629 $this->add_tab( 'uploaded', __('Uploaded'), array( $this, 'tab_uploaded' ) );
631 // Early priority to occur before $this->manager->prepare_controls();
632 add_action( 'customize_controls_init', array( $this, 'prepare_control' ), 5 );
636 * Prepares the control.
638 * If no tabs exist, removes the control from the manager.
642 public function prepare_control() {
644 $this->manager->remove_control( $this->id );
648 * Refresh the parameters passed to the JavaScript via JSON.
651 * @uses WP_Customize_Upload_Control::to_json()
653 public function to_json() {
655 $this->json['statuses'] = $this->statuses;
659 * Render the control's content.
663 public function render_content() {
664 $src = $this->value();
665 if ( isset( $this->get_url ) )
666 $src = call_user_func( $this->get_url, $src );
669 <div class="customize-image-picker">
670 <?php if ( ! empty( $this->label ) ) : ?>
671 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
673 if ( ! empty( $this->description ) ) : ?>
674 <span class="description customize-control-description"><?php echo $this->description; ?></span>
677 <div class="customize-control-content">
678 <div class="dropdown preview-thumbnail" tabindex="0">
679 <div class="dropdown-content">
680 <?php if ( empty( $src ) ): ?>
681 <img style="display:none;" />
683 <img src="<?php echo esc_url( set_url_scheme( $src ) ); ?>" />
685 <div class="dropdown-status"></div>
687 <div class="dropdown-arrow"></div>
691 <div class="library">
693 <?php foreach ( $this->tabs as $id => $tab ): ?>
694 <li data-customize-tab='<?php echo esc_attr( $id ); ?>' tabindex='0'>
695 <?php echo esc_html( $tab['label'] ); ?>
699 <?php foreach ( $this->tabs as $id => $tab ): ?>
700 <div class="library-content" data-customize-tab='<?php echo esc_attr( $id ); ?>'>
701 <?php call_user_func( $tab['callback'] ); ?>
706 <div class="actions">
707 <a href="#" class="remove"><?php _e( 'Remove Image' ); ?></a>
714 * Add a tab to the control.
719 * @param string $label
720 * @param mixed $callback
722 public function add_tab( $id, $label, $callback ) {
723 $this->tabs[ $id ] = array(
725 'callback' => $callback,
730 * Remove a tab from the control.
736 public function remove_tab( $id ) {
737 unset( $this->tabs[ $id ] );
743 public function tab_upload_new() {
744 if ( ! _device_can_upload() ) {
745 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.'), 'http://apps.wordpress.org/' ) . '</p>';
748 <div class="upload-dropzone">
749 <?php _e('Drop a file here or <a href="#" class="upload">select a file</a>.'); ?>
751 <div class="upload-fallback">
752 <span class="button-secondary"><?php _e('Select File'); ?></span>
761 public function tab_uploaded() {
763 <div class="uploaded-target"></div>
771 * @param string $thumbnail_url
773 public function print_tab_image( $url, $thumbnail_url = null ) {
774 $url = set_url_scheme( $url );
775 $thumbnail_url = ( $thumbnail_url ) ? set_url_scheme( $thumbnail_url ) : $url;
777 <a href="#" class="thumbnail" data-customize-image-value="<?php echo esc_url( $url ); ?>">
778 <img src="<?php echo esc_url( $thumbnail_url ); ?>" />
785 * Customize Background Image Control Class
788 * @subpackage Customize
791 class WP_Customize_Background_Image_Control extends WP_Customize_Image_Control {
797 * @uses WP_Customize_Image_Control::__construct()
799 * @param WP_Customize_Manager $manager
801 public function __construct( $manager ) {
802 parent::__construct( $manager, 'background_image', array(
803 'label' => __( 'Background Image' ),
804 'section' => 'background_image',
805 'context' => 'custom-background',
806 'get_url' => 'get_background_image',
809 if ( $this->setting->default )
810 $this->add_tab( 'default', __('Default'), array( $this, 'tab_default_background' ) );
816 public function tab_uploaded() {
817 $backgrounds = get_posts( array(
818 'post_type' => 'attachment',
819 'meta_key' => '_wp_attachment_is_custom_background',
820 'meta_value' => $this->manager->get_stylesheet(),
825 ?><div class="uploaded-target"></div><?php
827 if ( empty( $backgrounds ) )
830 foreach ( (array) $backgrounds as $background )
831 $this->print_tab_image( esc_url_raw( $background->guid ) );
836 * @uses WP_Customize_Image_Control::print_tab_image()
838 public function tab_default_background() {
839 $this->print_tab_image( $this->setting->default );
843 class WP_Customize_Header_Image_Control extends WP_Customize_Image_Control {
844 public $type = 'header';
846 public function __construct( $manager ) {
847 parent::__construct( $manager, 'header_image', array(
848 'label' => __( 'Header Image' ),
850 'default' => 'header_image',
851 'data' => 'header_image_data',
853 'section' => 'header_image',
854 'context' => 'custom-header',
855 'removed' => 'remove-header',
856 'get_url' => 'get_header_image',
861 public function to_json() {
865 public function enqueue() {
867 wp_enqueue_script( 'customize-views' );
869 $this->prepare_control();
871 wp_localize_script( 'customize-views', '_wpCustomizeHeader', array(
873 'width' => absint( get_theme_support( 'custom-header', 'width' ) ),
874 'height' => absint( get_theme_support( 'custom-header', 'height' ) ),
875 'flex-width' => absint( get_theme_support( 'custom-header', 'flex-width' ) ),
876 'flex-height' => absint( get_theme_support( 'custom-header', 'flex-height' ) ),
877 'currentImgSrc' => $this->get_current_image_src(),
880 'add' => wp_create_nonce( 'header-add' ),
881 'remove' => wp_create_nonce( 'header-remove' ),
883 'uploads' => $this->uploaded_headers,
884 'defaults' => $this->default_headers
890 public function prepare_control() {
891 global $custom_image_header;
892 if ( empty( $custom_image_header ) ) {
896 // Process default headers and uploaded headers.
897 $custom_image_header->process_default_headers();
898 $this->default_headers = $custom_image_header->get_default_header_images();
899 $this->uploaded_headers = $custom_image_header->get_uploaded_header_images();
902 function print_header_image_template() {
904 <script type="text/template" id="tmpl-header-choice">
905 <# if (data.random) { #>
906 <button type="button" class="button display-options random">
907 <span class="dashicons dashicons-randomize dice"></span>
908 <# if ( data.type === 'uploaded' ) { #>
909 <?php _e( 'Randomize uploaded headers' ); ?>
910 <# } else if ( data.type === 'default' ) { #>
911 <?php _e( 'Randomize suggested headers' ); ?>
917 <# if (data.type === 'uploaded') { #>
918 <div class="dashicons dashicons-no close"></div>
921 <button type="button" class="choice thumbnail"
922 data-customize-image-value="{{{data.header.url}}}"
923 data-customize-header-image-data="{{JSON.stringify(data.header)}}">
924 <span class="screen-reader-text"><?php _e( 'Set image' ); ?></span>
925 <img src="{{{data.header.thumbnail_url}}}" alt="{{{data.header.alt_text || data.header.description}}}">
931 <script type="text/template" id="tmpl-header-current">
932 <# if (data.choice) { #>
933 <# if (data.random) { #>
935 <div class="placeholder">
937 <span><span class="dashicons dashicons-randomize dice"></span>
938 <# if ( data.type === 'uploaded' ) { #>
939 <?php _e( 'Randomizing uploaded headers' ); ?>
940 <# } else if ( data.type === 'default' ) { #>
941 <?php _e( 'Randomizing suggested headers' ); ?>
949 <img src="{{{data.header.thumbnail_url}}}" alt="{{{data.header.alt_text || data.header.description}}}" tabindex="0"/>
954 <div class="placeholder">
957 <?php _e( 'No image set' ); ?>
967 public function get_current_image_src() {
968 $src = $this->value();
969 if ( isset( $this->get_url ) ) {
970 $src = call_user_func( $this->get_url, $src );
976 public function render_content() {
977 $this->print_header_image_template();
978 $visibility = $this->get_current_image_src() ? '' : ' style="display:none" ';
979 $width = absint( get_theme_support( 'custom-header', 'width' ) );
980 $height = absint( get_theme_support( 'custom-header', 'height' ) );
984 <div class="customize-control-content">
985 <p class="customizer-section-intro">
987 if ( $width && $height ) {
988 printf( __( 'While you can crop images to your liking after clicking <strong>Add new image</strong>, your theme recommends a header size of <strong>%s × %s</strong> pixels.' ), $width, $height );
989 } elseif ( $width ) {
990 printf( __( 'While you can crop images to your liking after clicking <strong>Add new image</strong>, your theme recommends a header width of <strong>%s</strong> pixels.' ), $width );
992 printf( __( 'While you can crop images to your liking after clicking <strong>Add new image</strong>, your theme recommends a header height of <strong>%s</strong> pixels.' ), $height );
996 <div class="current">
997 <span class="customize-control-title">
998 <?php _e( 'Current header' ); ?>
1000 <div class="container">
1003 <div class="actions">
1004 <?php /* translators: Hide as in hide header image via the Customizer */ ?>
1005 <button type="button"<?php echo $visibility ?> class="button remove"><?php _ex( 'Hide image', 'custom header' ); ?></button>
1006 <?php /* translators: New as in add new header image via the Customizer */ ?>
1007 <button type="button" class="button new"><?php _ex( 'Add new image', 'header image' ); ?></button>
1008 <div style="clear:both"></div>
1010 <div class="choices">
1011 <span class="customize-control-title header-previously-uploaded">
1012 <?php _ex( 'Previously uploaded', 'custom headers' ); ?>
1014 <div class="uploaded">
1018 <span class="customize-control-title header-default">
1019 <?php _ex( 'Suggested', 'custom headers' ); ?>
1021 <div class="default">
1032 * Widget Area Customize Control Class
1035 class WP_Widget_Area_Customize_Control extends WP_Customize_Control {
1036 public $type = 'sidebar_widgets';
1039 public function to_json() {
1041 $exported_properties = array( 'sidebar_id' );
1042 foreach ( $exported_properties as $key ) {
1043 $this->json[ $key ] = $this->$key;
1047 public function render_content() {
1049 <span class="button-secondary add-new-widget" tabindex="0">
1050 <?php _e( 'Add a Widget' ); ?>
1053 <span class="reorder-toggle" tabindex="0">
1054 <span class="reorder"><?php _ex( 'Reorder', 'Reorder widgets in Customizer' ); ?></span>
1055 <span class="reorder-done"><?php _ex( 'Done', 'Cancel reordering widgets in Customizer' ); ?></span>
1061 * Whether the current sidebar is rendered on the page.
1066 * @return bool Whether sidebar is rendered.
1068 public function active_callback() {
1069 return $this->manager->widgets->is_sidebar_rendered( $this->sidebar_id );
1074 * Widget Form Customize Control Class
1076 class WP_Widget_Form_Customize_Control extends WP_Customize_Control {
1077 public $type = 'widget_form';
1079 public $widget_id_base;
1081 public $is_new = false;
1084 public $is_wide = false;
1086 public function to_json() {
1088 $exported_properties = array( 'widget_id', 'widget_id_base', 'sidebar_id', 'width', 'height', 'is_wide' );
1089 foreach ( $exported_properties as $key ) {
1090 $this->json[ $key ] = $this->$key;
1094 public function render_content() {
1095 global $wp_registered_widgets;
1096 require_once ABSPATH . '/wp-admin/includes/widgets.php';
1098 $widget = $wp_registered_widgets[ $this->widget_id ];
1099 if ( ! isset( $widget['params'][0] ) ) {
1100 $widget['params'][0] = array();
1104 'widget_id' => $widget['id'],
1105 'widget_name' => $widget['name'],
1108 $args = wp_list_widget_controls_dynamic_sidebar( array( 0 => $args, 1 => $widget['params'][0] ) );
1109 echo $this->manager->widgets->get_widget_control( $args );
1113 * Whether the current widget is rendered on the page.
1118 * @return bool Whether the widget is rendered.
1120 function active_callback() {
1121 return $this->manager->widgets->is_widget_rendered( $this->widget_id );