3 * Customize Control Class
6 * @subpackage Customize
10 class WP_Customize_Control {
14 // All settings tied to the control.
17 // The primary setting for the control (if there is one).
18 public $setting = 'default';
20 public $priority = 10;
23 // @todo: remove choices
24 public $choices = array();
26 public $json = array();
28 public $type = 'text';
34 * If $args['settings'] is not defined, use the $id as the setting ID.
38 function __construct( $manager, $id, $args = array() ) {
39 $keys = array_keys( get_object_vars( $this ) );
40 foreach ( $keys as $key ) {
41 if ( isset( $args[ $key ] ) )
42 $this->$key = $args[ $key ];
45 $this->manager = $manager;
50 if ( empty( $this->settings ) )
51 $this->settings = $id;
54 if ( is_array( $this->settings ) ) {
55 foreach ( $this->settings as $key => $setting ) {
56 $settings[ $key ] = $this->manager->get_setting( $setting );
59 $this->setting = $this->manager->get_setting( $this->settings );
60 $settings['default'] = $this->setting;
62 $this->settings = $settings;
66 * Enqueue control related scripts/styles.
70 public function enqueue() {}
74 * Fetch a setting's value.
75 * Grabs the main setting by default.
79 public final function value( $setting_key = 'default' ) {
80 if ( isset( $this->settings[ $setting_key ] ) )
81 return $this->settings[ $setting_key ]->value();
85 * Refresh the parameters passed to the JavaScript via JSON.
89 public function to_json() {
90 $this->json['settings'] = array();
91 foreach ( $this->settings as $key => $setting ) {
92 $this->json['settings'][ $key ] = $setting->id;
95 $this->json['type'] = $this->type;
99 * Check if the theme supports the control and check user capabilities.
103 * @return bool False if theme doesn't support the control or user doesn't have the required permissions, otherwise true.
105 public final function check_capabilities() {
106 foreach ( $this->settings as $setting ) {
107 if ( ! $setting->check_capabilities() )
111 $section = $this->manager->get_section( $this->section );
112 if ( isset( $section ) && ! $section->check_capabilities() )
119 * Check capabilities and render the control.
123 public final function maybe_render() {
124 if ( ! $this->check_capabilities() )
127 do_action( 'customize_render_control', $this );
128 do_action( 'customize_render_control_' . $this->id, $this );
134 * Render the control. Renders the control wrapper, then calls $this->render_content().
138 protected function render() {
139 $id = 'customize-control-' . str_replace( '[', '-', str_replace( ']', '', $this->id ) );
140 $class = 'customize-control customize-control-' . $this->type;
142 ?><li id="<?php echo esc_attr( $id ); ?>" class="<?php echo esc_attr( $class ); ?>">
143 <?php $this->render_content(); ?>
147 public function get_link( $setting_key = 'default' ) {
148 if ( ! isset( $this->settings[ $setting_key ] ) )
151 return 'data-customize-setting-link="' . esc_attr( $this->settings[ $setting_key ]->id ) . '"';
154 public function link( $setting_key = 'default' ) {
155 echo $this->get_link( $setting_key );
159 * Render the control's content.
161 * Allows the content to be overriden without having to rewrite the wrapper.
165 protected function render_content() {
166 switch( $this->type ) {
170 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
171 <input type="text" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); ?> />
178 <input type="checkbox" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); checked( $this->value() ); ?> />
179 <?php echo esc_html( $this->label ); ?>
184 if ( empty( $this->choices ) )
187 $name = '_customize-radio-' . $this->id;
190 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
192 foreach ( $this->choices as $value => $label ) :
195 <input type="radio" value="<?php echo esc_attr( $value ); ?>" name="<?php echo esc_attr( $name ); ?>" <?php $this->link(); checked( $this->value(), $value ); ?> />
196 <?php echo esc_html( $label ); ?><br/>
202 if ( empty( $this->choices ) )
207 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
208 <select <?php $this->link(); ?>>
210 foreach ( $this->choices as $value => $label )
211 echo '<option value="' . esc_attr( $value ) . '"' . selected( $this->value(), $value, false ) . '>' . $label . '</option>';
217 case 'dropdown-pages':
218 $dropdown = wp_dropdown_pages(
220 'name' => '_customize-dropdown-pages-' . $this->id,
222 'show_option_none' => __( '— Select —' ),
223 'option_none_value' => '0',
224 'selected' => $this->value(),
228 // Hackily add in the data link parameter.
229 $dropdown = str_replace( '<select', '<select ' . $this->get_link(), $dropdown );
232 '<label class="customize-control-select"><span class="customize-control-title">%s</span> %s</label>',
241 class WP_Customize_Color_Control extends WP_Customize_Control {
242 public $type = 'color';
245 public function __construct( $manager, $id, $args = array() ) {
246 $this->statuses = array( '' => __('Default') );
247 parent::__construct( $manager, $id, $args );
250 public function enqueue() {
251 wp_enqueue_script( 'farbtastic' );
252 wp_enqueue_style( 'farbtastic' );
255 public function to_json() {
257 $this->json['statuses'] = $this->statuses;
260 public function render_content() {
263 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
264 <div class="customize-control-content">
265 <div class="dropdown">
266 <div class="dropdown-content">
267 <div class="dropdown-status"></div>
269 <div class="dropdown-arrow"></div>
271 <input class="color-picker-hex" type="text" maxlength="7" placeholder="<?php esc_attr_e('Hex Value'); ?>" />
273 <div class="farbtastic-placeholder"></div>
279 class WP_Customize_Upload_Control extends WP_Customize_Control {
280 public $type = 'upload';
281 public $removed = '';
284 public function enqueue() {
285 wp_enqueue_script( 'wp-plupload' );
288 public function to_json() {
291 $this->json['removed'] = $this->removed;
293 if ( $this->context )
294 $this->json['context'] = $this->context;
297 public function render_content() {
300 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
302 <a href="#" class="button-secondary upload"><?php _e( 'Upload' ); ?></a>
303 <a href="#" class="remove"><?php _e( 'Remove' ); ?></a>
310 class WP_Customize_Image_Control extends WP_Customize_Upload_Control {
311 public $type = 'image';
315 protected $tabs = array();
317 public function __construct( $manager, $id, $args ) {
318 $this->statuses = array( '' => __('No Image') );
320 parent::__construct( $manager, $id, $args );
322 $this->add_tab( 'upload-new', __('Upload New'), array( $this, 'tab_upload_new' ) );
323 $this->add_tab( 'uploaded', __('Uploaded'), array( $this, 'tab_uploaded' ) );
326 public function to_json() {
328 $this->json['statuses'] = $this->statuses;
331 public function render_content() {
332 $src = $this->value();
333 if ( isset( $this->get_url ) )
334 $src = call_user_func( $this->get_url, $src );
337 <div class="customize-image-picker">
338 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
340 <div class="customize-control-content">
341 <div class="dropdown preview-thumbnail">
342 <div class="dropdown-content">
343 <?php if ( empty( $src ) ): ?>
344 <img style="display:none;" />
346 <img src="<?php echo esc_url( set_url_scheme( $src ) ); ?>" />
348 <div class="dropdown-status"></div>
350 <div class="dropdown-arrow"></div>
354 <div class="library">
356 <?php foreach ( $this->tabs as $id => $tab ): ?>
357 <li data-customize-tab='<?php echo esc_attr( $id ); ?>'>
358 <?php echo esc_html( $tab['label'] ); ?>
362 <?php foreach ( $this->tabs as $id => $tab ): ?>
363 <div class="library-content" data-customize-tab='<?php echo esc_attr( $id ); ?>'>
364 <?php call_user_func( $tab['callback'] ); ?>
369 <div class="actions">
370 <a href="#" class="remove"><?php _e( 'Remove Image' ); ?></a>
376 public function add_tab( $id, $label, $callback ) {
377 $this->tabs[ $id ] = array(
379 'callback' => $callback,
383 public function remove_tab( $id ) {
384 unset( $this->tabs[ $id ] );
387 public function tab_upload_new() {
388 if ( ! _device_can_upload() ) {
390 <p><?php _e('The web browser on your device cannot be used to upload files. You may be able to use the <a href="http://wordpress.org/extend/mobile/">native app for your device</a> instead.'); ?></p>
394 <div class="upload-dropzone">
395 <?php _e('Drop a file here or <a href="#" class="upload">select a file</a>.'); ?>
397 <div class="upload-fallback">
398 <span class="button-secondary"><?php _e('Select File'); ?></span>
404 public function tab_uploaded() {
406 <div class="uploaded-target"></div>
410 public function print_tab_image( $url, $thumbnail_url = null ) {
411 $url = set_url_scheme( $url );
412 $thumbnail_url = ( $thumbnail_url ) ? set_url_scheme( $thumbnail_url ) : $url;
414 <a href="#" class="thumbnail" data-customize-image-value="<?php echo esc_url( $url ); ?>">
415 <img src="<?php echo esc_url( $thumbnail_url ); ?>" />
421 class WP_Customize_Background_Image_Control extends WP_Customize_Image_Control {
422 public function __construct( $manager ) {
423 parent::__construct( $manager, 'background_image', array(
424 'label' => __( 'Background Image' ),
425 'section' => 'background_image',
426 'context' => 'custom-background',
427 'get_url' => 'get_background_image',
430 if ( $this->setting->default )
431 $this->add_tab( 'default', __('Default'), array( $this, 'tab_default_background' ) );
434 public function tab_uploaded() {
435 $backgrounds = get_posts( array(
436 'post_type' => 'attachment',
437 'meta_key' => '_wp_attachment_is_custom_background',
438 'meta_value' => $this->manager->get_stylesheet(),
443 ?><div class="uploaded-target"></div><?php
445 if ( empty( $backgrounds ) )
448 foreach ( (array) $backgrounds as $background )
449 $this->print_tab_image( esc_url_raw( $background->guid ) );
452 public function tab_default_background() {
453 $this->print_tab_image( $this->setting->default );
457 class WP_Customize_Header_Image_Control extends WP_Customize_Image_Control {
458 public function __construct( $manager ) {
459 parent::__construct( $manager, 'header_image', array(
460 'label' => __( 'Header Image' ),
462 'default' => 'header_image',
463 'data' => 'header_image_data',
465 'section' => 'header_image',
466 'context' => 'custom-header',
467 'removed' => 'remove-header',
468 'get_url' => 'get_header_image',
471 'remove-header' => __('No Image'),
472 'random-default-image' => __('Random Default Image'),
473 'random-uploaded-image' => __('Random Uploaded Image'),
477 $this->add_tab( 'default', __('Default'), array( $this, 'tab_default_headers' ) );
480 public function print_header_image( $choice, $header ) {
481 $header['url'] = set_url_scheme( $header['url'] );
482 $header['thumbnail_url'] = set_url_scheme( $header['thumbnail_url'] );
484 $header_image_data = array( 'choice' => $choice );
485 foreach ( array( 'attachment_id', 'width', 'height', 'url', 'thumbnail_url' ) as $key ) {
486 if ( isset( $header[ $key ] ) )
487 $header_image_data[ $key ] = $header[ $key ];
492 <a href="#" class="thumbnail"
493 data-customize-image-value="<?php echo esc_url( $header['url'] ); ?>"
494 data-customize-header-image-data="<?php echo esc_attr( json_encode( $header_image_data ) ); ?>">
495 <img src="<?php echo esc_url( $header['thumbnail_url'] ); ?>" />
500 public function tab_uploaded() {
501 $headers = get_uploaded_header_images();
503 ?><div class="uploaded-target"></div><?php
505 foreach ( $headers as $choice => $header )
506 $this->print_header_image( $choice, $header );
509 public function tab_default_headers() {
510 global $custom_image_header;
511 $custom_image_header->process_default_headers();
513 foreach ( $custom_image_header->default_headers as $choice => $header )
514 $this->print_header_image( $choice, $header );