WordPress 3.4
[autoinstalls/wordpress.git] / wp-includes / class-wp-customize-control.php
1 <?php
2 /**
3  * Customize Control Class
4  *
5  * @package WordPress
6  * @subpackage Customize
7  * @since 3.4.0
8  */
9
10 class WP_Customize_Control {
11         public $manager;
12         public $id;
13
14         // All settings tied to the control.
15         public $settings;
16
17         // The primary setting for the control (if there is one).
18         public $setting = 'default';
19
20         public $priority          = 10;
21         public $section           = '';
22         public $label             = '';
23         // @todo: remove choices
24         public $choices           = array();
25
26         public $json = array();
27
28         public $type = 'text';
29
30
31         /**
32          * Constructor.
33          *
34          * If $args['settings'] is not defined, use the $id as the setting ID.
35          *
36          * @since 3.4.0
37          */
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 ];
43                 }
44
45                 $this->manager = $manager;
46                 $this->id = $id;
47
48
49                 // Process settings.
50                 if ( empty( $this->settings ) )
51                         $this->settings = $id;
52
53                 $settings = array();
54                 if ( is_array( $this->settings ) ) {
55                         foreach ( $this->settings as $key => $setting ) {
56                                 $settings[ $key ] = $this->manager->get_setting( $setting );
57                         }
58                 } else {
59                         $this->setting = $this->manager->get_setting( $this->settings );
60                         $settings['default'] = $this->setting;
61                 }
62                 $this->settings = $settings;
63         }
64
65         /**
66          * Enqueue control related scripts/styles.
67          *
68          * @since 3.4.0
69          */
70         public function enqueue() {}
71
72
73         /**
74          * Fetch a setting's value.
75          * Grabs the main setting by default.
76          *
77          * @since 3.4.0
78          */
79         public final function value( $setting_key = 'default' ) {
80                 if ( isset( $this->settings[ $setting_key ] ) )
81                         return $this->settings[ $setting_key ]->value();
82         }
83
84         /**
85          * Refresh the parameters passed to the JavaScript via JSON.
86          *
87          * @since 3.4.0
88          */
89         public function to_json() {
90                 $this->json['settings'] = array();
91                 foreach ( $this->settings as $key => $setting ) {
92                         $this->json['settings'][ $key ] = $setting->id;
93                 }
94
95                 $this->json['type'] = $this->type;
96         }
97
98         /**
99          * Check if the theme supports the control and check user capabilities.
100          *
101          * @since 3.4.0
102          *
103          * @return bool False if theme doesn't support the control or user doesn't have the required permissions, otherwise true.
104          */
105         public final function check_capabilities() {
106                 foreach ( $this->settings as $setting ) {
107                         if ( ! $setting->check_capabilities() )
108                                 return false;
109                 }
110
111                 $section = $this->manager->get_section( $this->section );
112                 if ( isset( $section ) && ! $section->check_capabilities() )
113                         return false;
114
115                 return true;
116         }
117
118         /**
119          * Check capabilities and render the control.
120          *
121          * @since 3.4.0
122          */
123         public final function maybe_render() {
124                 if ( ! $this->check_capabilities() )
125                         return;
126
127                 do_action( 'customize_render_control', $this );
128                 do_action( 'customize_render_control_' . $this->id, $this );
129
130                 $this->render();
131         }
132
133         /**
134          * Render the control. Renders the control wrapper, then calls $this->render_content().
135          *
136          * @since 3.4.0
137          */
138         protected function render() {
139                 $id    = 'customize-control-' . str_replace( '[', '-', str_replace( ']', '', $this->id ) );
140                 $class = 'customize-control customize-control-' . $this->type;
141
142                 ?><li id="<?php echo esc_attr( $id ); ?>" class="<?php echo esc_attr( $class ); ?>">
143                         <?php $this->render_content(); ?>
144                 </li><?php
145         }
146
147         public function get_link( $setting_key = 'default' ) {
148                 if ( ! isset( $this->settings[ $setting_key ] ) )
149                         return '';
150
151                 return 'data-customize-setting-link="' . esc_attr( $this->settings[ $setting_key ]->id ) . '"';
152         }
153
154         public function link( $setting_key = 'default' ) {
155                 echo $this->get_link( $setting_key );
156         }
157
158         /**
159          * Render the control's content.
160          *
161          * Allows the content to be overriden without having to rewrite the wrapper.
162          *
163          * @since 3.4.0
164          */
165         protected function render_content() {
166                 switch( $this->type ) {
167                         case 'text':
168                                 ?>
169                                 <label>
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(); ?> />
172                                 </label>
173                                 <?php
174                                 break;
175                         case 'checkbox':
176                                 ?>
177                                 <label>
178                                         <input type="checkbox" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); checked( $this->value() ); ?> />
179                                         <?php echo esc_html( $this->label ); ?>
180                                 </label>
181                                 <?php
182                                 break;
183                         case 'radio':
184                                 if ( empty( $this->choices ) )
185                                         return;
186
187                                 $name = '_customize-radio-' . $this->id;
188
189                                 ?>
190                                 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
191                                 <?php
192                                 foreach ( $this->choices as $value => $label ) :
193                                         ?>
194                                         <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/>
197                                         </label>
198                                         <?php
199                                 endforeach;
200                                 break;
201                         case 'select':
202                                 if ( empty( $this->choices ) )
203                                         return;
204
205                                 ?>
206                                 <label>
207                                         <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
208                                         <select <?php $this->link(); ?>>
209                                                 <?php
210                                                 foreach ( $this->choices as $value => $label )
211                                                         echo '<option value="' . esc_attr( $value ) . '"' . selected( $this->value(), $value, false ) . '>' . $label . '</option>';
212                                                 ?>
213                                         </select>
214                                 </label>
215                                 <?php
216                                 break;
217                         case 'dropdown-pages':
218                                 $dropdown = wp_dropdown_pages(
219                                         array(
220                                                 'name'              => '_customize-dropdown-pages-' . $this->id,
221                                                 'echo'              => 0,
222                                                 'show_option_none'  => __( '&mdash; Select &mdash;' ),
223                                                 'option_none_value' => '0',
224                                                 'selected'          => $this->value(),
225                                         )
226                                 );
227
228                                 // Hackily add in the data link parameter.
229                                 $dropdown = str_replace( '<select', '<select ' . $this->get_link(), $dropdown );
230
231                                 printf(
232                                         '<label class="customize-control-select"><span class="customize-control-title">%s</span> %s</label>',
233                                         $this->label,
234                                         $dropdown
235                                 );
236                                 break;
237                 }
238         }
239 }
240
241 class WP_Customize_Color_Control extends WP_Customize_Control {
242         public $type = 'color';
243         public $statuses;
244
245         public function __construct( $manager, $id, $args = array() ) {
246                 $this->statuses = array( '' => __('Default') );
247                 parent::__construct( $manager, $id, $args );
248         }
249
250         public function enqueue() {
251                 wp_enqueue_script( 'farbtastic' );
252                 wp_enqueue_style( 'farbtastic' );
253         }
254
255         public function to_json() {
256                 parent::to_json();
257                 $this->json['statuses'] = $this->statuses;
258         }
259
260         public function render_content() {
261                 ?>
262                 <label>
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>
268                                         </div>
269                                         <div class="dropdown-arrow"></div>
270                                 </div>
271                                 <input class="color-picker-hex" type="text" maxlength="7" placeholder="<?php esc_attr_e('Hex Value'); ?>" />
272                         </div>
273                         <div class="farbtastic-placeholder"></div>
274                 </label>
275                 <?php
276         }
277 }
278
279 class WP_Customize_Upload_Control extends WP_Customize_Control {
280         public $type    = 'upload';
281         public $removed = '';
282         public $context;
283
284         public function enqueue() {
285                 wp_enqueue_script( 'wp-plupload' );
286         }
287
288         public function to_json() {
289                 parent::to_json();
290
291                 $this->json['removed'] = $this->removed;
292
293                 if ( $this->context )
294                         $this->json['context'] = $this->context;
295         }
296
297         public function render_content() {
298                 ?>
299                 <label>
300                         <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
301                         <div>
302                                 <a href="#" class="button-secondary upload"><?php _e( 'Upload' ); ?></a>
303                                 <a href="#" class="remove"><?php _e( 'Remove' ); ?></a>
304                         </div>
305                 </label>
306                 <?php
307         }
308 }
309
310 class WP_Customize_Image_Control extends WP_Customize_Upload_Control {
311         public $type = 'image';
312         public $get_url;
313         public $statuses;
314
315         protected $tabs = array();
316
317         public function __construct( $manager, $id, $args ) {
318                 $this->statuses = array( '' => __('No Image') );
319
320                 parent::__construct( $manager, $id, $args );
321
322                 $this->add_tab( 'upload-new', __('Upload New'), array( $this, 'tab_upload_new' ) );
323                 $this->add_tab( 'uploaded',   __('Uploaded'),   array( $this, 'tab_uploaded' ) );
324         }
325
326         public function to_json() {
327                 parent::to_json();
328                 $this->json['statuses'] = $this->statuses;
329         }
330
331         public function render_content() {
332                 $src = $this->value();
333                 if ( isset( $this->get_url ) )
334                         $src = call_user_func( $this->get_url, $src );
335
336                 ?>
337                 <div class="customize-image-picker">
338                         <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
339
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;" />
345                                                 <?php else: ?>
346                                                         <img src="<?php echo esc_url( set_url_scheme( $src ) ); ?>" />
347                                                 <?php endif; ?>
348                                                 <div class="dropdown-status"></div>
349                                         </div>
350                                         <div class="dropdown-arrow"></div>
351                                 </div>
352                         </div>
353
354                         <div class="library">
355                                 <ul>
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'] ); ?>
359                                                 </li>
360                                         <?php endforeach; ?>
361                                 </ul>
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'] ); ?>
365                                         </div>
366                                 <?php endforeach; ?>
367                         </div>
368
369                         <div class="actions">
370                                 <a href="#" class="remove"><?php _e( 'Remove Image' ); ?></a>
371                         </div>
372                 </div>
373                 <?php
374         }
375
376         public function add_tab( $id, $label, $callback ) {
377                 $this->tabs[ $id ] = array(
378                         'label'    => $label,
379                         'callback' => $callback,
380                 );
381         }
382
383         public function remove_tab( $id ) {
384                 unset( $this->tabs[ $id ] );
385         }
386
387         public function tab_upload_new() {
388                 if ( ! _device_can_upload() ) {
389                         ?>
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>
391                         <?php
392                 } else {
393                         ?>
394                         <div class="upload-dropzone">
395                                 <?php _e('Drop a file here or <a href="#" class="upload">select a file</a>.'); ?>
396                         </div>
397                         <div class="upload-fallback">
398                                 <span class="button-secondary"><?php _e('Select File'); ?></span>
399                         </div>
400                         <?php
401                 }
402         }
403
404         public function tab_uploaded() {
405                 ?>
406                 <div class="uploaded-target"></div>
407                 <?php
408         }
409
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;
413                 ?>
414                 <a href="#" class="thumbnail" data-customize-image-value="<?php echo esc_url( $url ); ?>">
415                         <img src="<?php echo esc_url( $thumbnail_url ); ?>" />
416                 </a>
417                 <?php
418         }
419 }
420
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',
428                 ) );
429
430                 if ( $this->setting->default )
431                         $this->add_tab( 'default',  __('Default'),  array( $this, 'tab_default_background' ) );
432         }
433
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(),
439                         'orderby'    => 'none',
440                         'nopaging'   => true,
441                 ) );
442
443                 ?><div class="uploaded-target"></div><?php
444
445                 if ( empty( $backgrounds ) )
446                         return;
447
448                 foreach ( (array) $backgrounds as $background )
449                         $this->print_tab_image( esc_url_raw( $background->guid ) );
450         }
451
452         public function tab_default_background() {
453                 $this->print_tab_image( $this->setting->default );
454         }
455 }
456
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' ),
461                         'settings' => array(
462                                 'default' => 'header_image',
463                                 'data'    => 'header_image_data',
464                         ),
465                         'section'  => 'header_image',
466                         'context'  => 'custom-header',
467                         'removed'  => 'remove-header',
468                         'get_url'  => 'get_header_image',
469                         'statuses' => array(
470                                 ''                      => __('Default'),
471                                 'remove-header'         => __('No Image'),
472                                 'random-default-image'  => __('Random Default Image'),
473                                 'random-uploaded-image' => __('Random Uploaded Image'),
474                         )
475                 ) );
476
477                 $this->add_tab( 'default',  __('Default'),  array( $this, 'tab_default_headers' ) );
478         }
479
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'] );
483
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 ];
488                 }
489
490
491                 ?>
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'] ); ?>" />
496                 </a>
497                 <?php
498         }
499
500         public function tab_uploaded() {
501                 $headers = get_uploaded_header_images();
502
503                 ?><div class="uploaded-target"></div><?php
504
505                 foreach ( $headers as $choice => $header )
506                         $this->print_header_image( $choice, $header );
507         }
508
509         public function tab_default_headers() {
510                 global $custom_image_header;
511                 $custom_image_header->process_default_headers();
512
513                 foreach ( $custom_image_header->default_headers as $choice => $header )
514                         $this->print_header_image( $choice, $header );
515         }
516 }