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 * If $args['settings'] is not defined, use the $id as the setting ID.
84 * @param WP_Customize_Manager $manager
88 function __construct( $manager, $id, $args = array() ) {
89 $keys = array_keys( get_object_vars( $this ) );
90 foreach ( $keys as $key ) {
91 if ( isset( $args[ $key ] ) )
92 $this->$key = $args[ $key ];
95 $this->manager = $manager;
100 if ( empty( $this->settings ) )
101 $this->settings = $id;
104 if ( is_array( $this->settings ) ) {
105 foreach ( $this->settings as $key => $setting ) {
106 $settings[ $key ] = $this->manager->get_setting( $setting );
109 $this->setting = $this->manager->get_setting( $this->settings );
110 $settings['default'] = $this->setting;
112 $this->settings = $settings;
116 * Enqueue control related scripts/styles.
120 public function enqueue() {}
124 * Fetch a setting's value.
125 * Grabs the main setting by default.
129 * @param string $setting_key
130 * @return mixed The requested setting's value, if the setting exists.
132 public final function value( $setting_key = 'default' ) {
133 if ( isset( $this->settings[ $setting_key ] ) )
134 return $this->settings[ $setting_key ]->value();
138 * Refresh the parameters passed to the JavaScript via JSON.
142 public function to_json() {
143 $this->json['settings'] = array();
144 foreach ( $this->settings as $key => $setting ) {
145 $this->json['settings'][ $key ] = $setting->id;
148 $this->json['type'] = $this->type;
152 * Check if the theme supports the control and check user capabilities.
156 * @return bool False if theme doesn't support the control or user doesn't have the required permissions, otherwise true.
158 public final function check_capabilities() {
159 foreach ( $this->settings as $setting ) {
160 if ( ! $setting->check_capabilities() )
164 $section = $this->manager->get_section( $this->section );
165 if ( isset( $section ) && ! $section->check_capabilities() )
172 * Check capabilities and render the control.
175 * @uses WP_Customize_Control::render()
177 public final function maybe_render() {
178 if ( ! $this->check_capabilities() )
181 do_action( 'customize_render_control', $this );
182 do_action( 'customize_render_control_' . $this->id, $this );
188 * Render the control. Renders the control wrapper, then calls $this->render_content().
192 protected function render() {
193 $id = 'customize-control-' . str_replace( '[', '-', str_replace( ']', '', $this->id ) );
194 $class = 'customize-control customize-control-' . $this->type;
196 ?><li id="<?php echo esc_attr( $id ); ?>" class="<?php echo esc_attr( $class ); ?>">
197 <?php $this->render_content(); ?>
202 * Get the data link parameter for a setting.
206 * @param string $setting_key
207 * @return string Data link parameter, if $setting_key is a valid setting, empty string otherwise.
209 public function get_link( $setting_key = 'default' ) {
210 if ( ! isset( $this->settings[ $setting_key ] ) )
213 return 'data-customize-setting-link="' . esc_attr( $this->settings[ $setting_key ]->id ) . '"';
217 * Render the data link parameter for a setting
220 * @uses WP_Customize_Control::get_link()
222 * @param string $setting_key
224 public function link( $setting_key = 'default' ) {
225 echo $this->get_link( $setting_key );
229 * Render the control's content.
231 * Allows the content to be overriden without having to rewrite the wrapper.
235 protected function render_content() {
236 switch( $this->type ) {
240 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
241 <input type="text" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); ?> />
248 <input type="checkbox" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); checked( $this->value() ); ?> />
249 <?php echo esc_html( $this->label ); ?>
254 if ( empty( $this->choices ) )
257 $name = '_customize-radio-' . $this->id;
260 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
262 foreach ( $this->choices as $value => $label ) :
265 <input type="radio" value="<?php echo esc_attr( $value ); ?>" name="<?php echo esc_attr( $name ); ?>" <?php $this->link(); checked( $this->value(), $value ); ?> />
266 <?php echo esc_html( $label ); ?><br/>
272 if ( empty( $this->choices ) )
277 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
278 <select <?php $this->link(); ?>>
280 foreach ( $this->choices as $value => $label )
281 echo '<option value="' . esc_attr( $value ) . '"' . selected( $this->value(), $value, false ) . '>' . $label . '</option>';
287 case 'dropdown-pages':
288 $dropdown = wp_dropdown_pages(
290 'name' => '_customize-dropdown-pages-' . $this->id,
292 'show_option_none' => __( '— Select —' ),
293 'option_none_value' => '0',
294 'selected' => $this->value(),
298 // Hackily add in the data link parameter.
299 $dropdown = str_replace( '<select', '<select ' . $this->get_link(), $dropdown );
302 '<label class="customize-control-select"><span class="customize-control-title">%s</span> %s</label>',
312 * Customize Color Control Class
315 * @subpackage Customize
318 class WP_Customize_Color_Control extends WP_Customize_Control {
323 public $type = 'color';
334 * If $args['settings'] is not defined, use the $id as the setting ID.
337 * @uses WP_Customize_Control::__construct()
339 * @param WP_Customize_Manager $manager
343 public function __construct( $manager, $id, $args = array() ) {
344 $this->statuses = array( '' => __('Default') );
345 parent::__construct( $manager, $id, $args );
349 * Enqueue control related scripts/styles.
353 public function enqueue() {
354 wp_enqueue_script( 'wp-color-picker' );
355 wp_enqueue_style( 'wp-color-picker' );
359 * Refresh the parameters passed to the JavaScript via JSON.
362 * @uses WP_Customize_Control::to_json()
364 public function to_json() {
366 $this->json['statuses'] = $this->statuses;
370 * Render the control's content.
374 public function render_content() {
375 $this_default = $this->setting->default;
377 if ( $this_default ) {
378 if ( false === strpos( $this_default, '#' ) )
379 $this_default = '#' . $this_default;
380 $default_attr = ' data-default-color="' . esc_attr( $this_default ) . '"';
382 // The input's value gets set by JS. Don't fill it.
385 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
386 <div class="customize-control-content">
387 <input class="color-picker-hex" type="text" maxlength="7" placeholder="<?php esc_attr_e( 'Hex Value' ); ?>"<?php echo $default_attr; ?> />
395 * Customize Upload Control Class
398 * @subpackage Customize
401 class WP_Customize_Upload_Control extends WP_Customize_Control {
402 public $type = 'upload';
403 public $removed = '';
405 public $extensions = array();
408 * Enqueue control related scripts/styles.
412 public function enqueue() {
413 wp_enqueue_script( 'wp-plupload' );
417 * Refresh the parameters passed to the JavaScript via JSON.
420 * @uses WP_Customize_Control::to_json()
422 public function to_json() {
425 $this->json['removed'] = $this->removed;
427 if ( $this->context )
428 $this->json['context'] = $this->context;
430 if ( $this->extensions )
431 $this->json['extensions'] = implode( ',', $this->extensions );
435 * Render the control's content.
439 public function render_content() {
442 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
444 <a href="#" class="button-secondary upload"><?php _e( 'Upload' ); ?></a>
445 <a href="#" class="remove"><?php _e( 'Remove' ); ?></a>
453 * Customize Image Control Class
456 * @subpackage Customize
459 class WP_Customize_Image_Control extends WP_Customize_Upload_Control {
460 public $type = 'image';
463 public $extensions = array( 'jpg', 'jpeg', 'gif', 'png' );
465 protected $tabs = array();
470 * If $args['settings'] is not defined, use the $id as the setting ID.
473 * @uses WP_Customize_Upload_Control::__construct()
475 * @param WP_Customize_Manager $manager
479 public function __construct( $manager, $id, $args ) {
480 $this->statuses = array( '' => __('No Image') );
482 parent::__construct( $manager, $id, $args );
484 $this->add_tab( 'upload-new', __('Upload New'), array( $this, 'tab_upload_new' ) );
485 $this->add_tab( 'uploaded', __('Uploaded'), array( $this, 'tab_uploaded' ) );
487 // Early priority to occur before $this->manager->prepare_controls();
488 add_action( 'customize_controls_init', array( $this, 'prepare_control' ), 5 );
492 * Prepares the control.
494 * If no tabs exist, removes the control from the manager.
498 public function prepare_control() {
500 $this->manager->remove_control( $this->id );
504 * Refresh the parameters passed to the JavaScript via JSON.
507 * @uses WP_Customize_Upload_Control::to_json()
509 public function to_json() {
511 $this->json['statuses'] = $this->statuses;
515 * Render the control's content.
519 public function render_content() {
520 $src = $this->value();
521 if ( isset( $this->get_url ) )
522 $src = call_user_func( $this->get_url, $src );
525 <div class="customize-image-picker">
526 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
528 <div class="customize-control-content">
529 <div class="dropdown preview-thumbnail" tabindex="0">
530 <div class="dropdown-content">
531 <?php if ( empty( $src ) ): ?>
532 <img style="display:none;" />
534 <img src="<?php echo esc_url( set_url_scheme( $src ) ); ?>" />
536 <div class="dropdown-status"></div>
538 <div class="dropdown-arrow"></div>
542 <div class="library">
544 <?php foreach ( $this->tabs as $id => $tab ): ?>
545 <li data-customize-tab='<?php echo esc_attr( $id ); ?>' tabindex='0'>
546 <?php echo esc_html( $tab['label'] ); ?>
550 <?php foreach ( $this->tabs as $id => $tab ): ?>
551 <div class="library-content" data-customize-tab='<?php echo esc_attr( $id ); ?>'>
552 <?php call_user_func( $tab['callback'] ); ?>
557 <div class="actions">
558 <a href="#" class="remove"><?php _e( 'Remove Image' ); ?></a>
565 * Add a tab to the control.
570 * @param string $label
571 * @param mixed $callback
573 public function add_tab( $id, $label, $callback ) {
574 $this->tabs[ $id ] = array(
576 'callback' => $callback,
581 * Remove a tab from the control.
587 public function remove_tab( $id ) {
588 unset( $this->tabs[ $id ] );
594 public function tab_upload_new() {
595 if ( ! _device_can_upload() ) {
596 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://wordpress.org/mobile/' ) . '</p>';
599 <div class="upload-dropzone">
600 <?php _e('Drop a file here or <a href="#" class="upload">select a file</a>.'); ?>
602 <div class="upload-fallback">
603 <span class="button-secondary"><?php _e('Select File'); ?></span>
612 public function tab_uploaded() {
614 <div class="uploaded-target"></div>
622 * @param string $thumbnail_url
624 public function print_tab_image( $url, $thumbnail_url = null ) {
625 $url = set_url_scheme( $url );
626 $thumbnail_url = ( $thumbnail_url ) ? set_url_scheme( $thumbnail_url ) : $url;
628 <a href="#" class="thumbnail" data-customize-image-value="<?php echo esc_url( $url ); ?>">
629 <img src="<?php echo esc_url( $thumbnail_url ); ?>" />
636 * Customize Background Image Control Class
639 * @subpackage Customize
642 class WP_Customize_Background_Image_Control extends WP_Customize_Image_Control {
648 * @uses WP_Customize_Image_Control::__construct()
650 * @param WP_Customize_Manager $manager
652 public function __construct( $manager ) {
653 parent::__construct( $manager, 'background_image', array(
654 'label' => __( 'Background Image' ),
655 'section' => 'background_image',
656 'context' => 'custom-background',
657 'get_url' => 'get_background_image',
660 if ( $this->setting->default )
661 $this->add_tab( 'default', __('Default'), array( $this, 'tab_default_background' ) );
667 public function tab_uploaded() {
668 $backgrounds = get_posts( array(
669 'post_type' => 'attachment',
670 'meta_key' => '_wp_attachment_is_custom_background',
671 'meta_value' => $this->manager->get_stylesheet(),
676 ?><div class="uploaded-target"></div><?php
678 if ( empty( $backgrounds ) )
681 foreach ( (array) $backgrounds as $background )
682 $this->print_tab_image( esc_url_raw( $background->guid ) );
687 * @uses WP_Customize_Image_Control::print_tab_image()
689 public function tab_default_background() {
690 $this->print_tab_image( $this->setting->default );
695 * Customize Header Image Control Class
698 * @subpackage Customize
701 class WP_Customize_Header_Image_Control extends WP_Customize_Image_Control {
703 * The processed default headers.
707 protected $default_headers;
710 * The uploaded headers.
714 protected $uploaded_headers;
720 * @uses WP_Customize_Image_Control::__construct()
721 * @uses WP_Customize_Image_Control::add_tab()
723 * @param WP_Customize_Manager $manager
725 public function __construct( $manager ) {
726 parent::__construct( $manager, 'header_image', array(
727 'label' => __( 'Header Image' ),
729 'default' => 'header_image',
730 'data' => 'header_image_data',
732 'section' => 'header_image',
733 'context' => 'custom-header',
734 'removed' => 'remove-header',
735 'get_url' => 'get_header_image',
738 'remove-header' => __('No Image'),
739 'random-default-image' => __('Random Default Image'),
740 'random-uploaded-image' => __('Random Uploaded Image'),
744 // Remove the upload tab.
745 $this->remove_tab( 'upload-new' );
749 * Prepares the control.
751 * If no tabs exist, removes the control from the manager.
755 public function prepare_control() {
756 global $custom_image_header;
757 if ( empty( $custom_image_header ) )
758 return parent::prepare_control();
760 // Process default headers and uploaded headers.
761 $custom_image_header->process_default_headers();
762 $this->default_headers = $custom_image_header->default_headers;
763 $this->uploaded_headers = get_uploaded_header_images();
765 if ( $this->default_headers )
766 $this->add_tab( 'default', __('Default'), array( $this, 'tab_default_headers' ) );
768 if ( ! $this->uploaded_headers )
769 $this->remove_tab( 'uploaded' );
771 return parent::prepare_control();
777 * @param mixed $choice Which header image to select. (@see Custom_Image_Header::get_header_image() )
778 * @param array $header
780 public function print_header_image( $choice, $header ) {
781 $header['url'] = set_url_scheme( $header['url'] );
782 $header['thumbnail_url'] = set_url_scheme( $header['thumbnail_url'] );
784 $header_image_data = array( 'choice' => $choice );
785 foreach ( array( 'attachment_id', 'width', 'height', 'url', 'thumbnail_url' ) as $key ) {
786 if ( isset( $header[ $key ] ) )
787 $header_image_data[ $key ] = $header[ $key ];
792 <a href="#" class="thumbnail"
793 data-customize-image-value="<?php echo esc_url( $header['url'] ); ?>"
794 data-customize-header-image-data="<?php echo esc_attr( json_encode( $header_image_data ) ); ?>">
795 <img src="<?php echo esc_url( $header['thumbnail_url'] ); ?>" />
803 public function tab_uploaded() {
804 ?><div class="uploaded-target"></div><?php
806 foreach ( $this->uploaded_headers as $choice => $header )
807 $this->print_header_image( $choice, $header );
813 public function tab_default_headers() {
814 foreach ( $this->default_headers as $choice => $header )
815 $this->print_header_image( $choice, $header );