WordPress 3.9
[autoinstalls/wordpress.git] / wp-includes / class-wp-customize-manager.php
1 <?php
2 /**
3  * Customize Manager.
4  *
5  * Bootstraps the Customize experience on the server-side.
6  *
7  * Sets up the theme-switching process if a theme other than the active one is
8  * being previewed and customized.
9  *
10  * Serves as a factory for Customize Controls and Settings, and
11  * instantiates default Customize Controls and Settings.
12  *
13  * @package WordPress
14  * @subpackage Customize
15  * @since 3.4.0
16  */
17 final class WP_Customize_Manager {
18         /**
19          * An instance of the theme that is being customized.
20          *
21          * @var WP_Theme
22          */
23         protected $theme;
24
25         /**
26          * The directory name of the previously active theme (within the theme_root).
27          *
28          * @var string
29          */
30         protected $original_stylesheet;
31
32         /**
33          * Whether filters have been set to change the active theme to the theme being
34          * customized.
35          *
36          * @var boolean
37          */
38         protected $previewing = false;
39
40         /**
41          * Methods and properties deailing with managing widgets in the customizer.
42          *
43          * @var WP_Customize_Widgets
44          */
45         public $widgets;
46
47         protected $settings = array();
48         protected $sections = array();
49         protected $controls = array();
50
51         protected $nonce_tick;
52
53         protected $customized;
54
55         /**
56          * $_POST values for Customize Settings.
57          *
58          * @var array
59          */
60         private $_post_values;
61
62         /**
63          * Constructor.
64          *
65          * @since 3.4.0
66          */
67         public function __construct() {
68                 require( ABSPATH . WPINC . '/class-wp-customize-setting.php' );
69                 require( ABSPATH . WPINC . '/class-wp-customize-section.php' );
70                 require( ABSPATH . WPINC . '/class-wp-customize-control.php' );
71                 require( ABSPATH . WPINC . '/class-wp-customize-widgets.php' );
72
73                 $this->widgets = new WP_Customize_Widgets( $this );
74
75                 add_filter( 'wp_die_handler', array( $this, 'wp_die_handler' ) );
76
77                 add_action( 'setup_theme',  array( $this, 'setup_theme' ) );
78                 add_action( 'wp_loaded',    array( $this, 'wp_loaded' ) );
79
80                 // Run wp_redirect_status late to make sure we override the status last.
81                 add_action( 'wp_redirect_status', array( $this, 'wp_redirect_status' ), 1000 );
82
83                 // Do not spawn cron (especially the alternate cron) while running the customizer.
84                 remove_action( 'init', 'wp_cron' );
85
86                 // Do not run update checks when rendering the controls.
87                 remove_action( 'admin_init', '_maybe_update_core' );
88                 remove_action( 'admin_init', '_maybe_update_plugins' );
89                 remove_action( 'admin_init', '_maybe_update_themes' );
90
91                 add_action( 'wp_ajax_customize_save', array( $this, 'save' ) );
92
93                 add_action( 'customize_register',                 array( $this, 'register_controls' ) );
94                 add_action( 'customize_controls_init',            array( $this, 'prepare_controls' ) );
95                 add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_control_scripts' ) );
96         }
97
98         /**
99          * Return true if it's an AJAX request.
100          *
101          * @since 3.4.0
102          *
103          * @return bool
104          */
105         public function doing_ajax() {
106                 return isset( $_POST['customized'] ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX );
107         }
108
109         /**
110          * Custom wp_die wrapper. Returns either the standard message for UI
111          * or the AJAX message.
112          *
113          * @since 3.4.0
114          *
115          * @param mixed $ajax_message AJAX return
116          * @param mixed $message UI message
117          */
118         protected function wp_die( $ajax_message, $message = null ) {
119                 if ( $this->doing_ajax() )
120                         wp_die( $ajax_message );
121
122                 if ( ! $message )
123                         $message = __( 'Cheatin&#8217; uh?' );
124
125                 wp_die( $message );
126         }
127
128         /**
129          * Return the AJAX wp_die() handler if it's a customized request.
130          *
131          * @since 3.4.0
132          *
133          * @return string
134          */
135         public function wp_die_handler() {
136                 if ( $this->doing_ajax() )
137                         return '_ajax_wp_die_handler';
138
139                 return '_default_wp_die_handler';
140         }
141
142         /**
143          * Start preview and customize theme.
144          *
145          * Check if customize query variable exist. Init filters to filter the current theme.
146          *
147          * @since 3.4.0
148          */
149         public function setup_theme() {
150                 send_origin_headers();
151
152                 if ( is_admin() && ! $this->doing_ajax() )
153                     auth_redirect();
154                 elseif ( $this->doing_ajax() && ! is_user_logged_in() )
155                     $this->wp_die( 0 );
156
157                 show_admin_bar( false );
158
159                 if ( ! current_user_can( 'edit_theme_options' ) )
160                         $this->wp_die( -1 );
161
162                 $this->original_stylesheet = get_stylesheet();
163
164                 $this->theme = wp_get_theme( isset( $_REQUEST['theme'] ) ? $_REQUEST['theme'] : null );
165
166                 if ( $this->is_theme_active() ) {
167                         // Once the theme is loaded, we'll validate it.
168                         add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ) );
169                 } else {
170                         // If the requested theme is not the active theme and the user doesn't have the
171                         // switch_themes cap, bail.
172                         if ( ! current_user_can( 'switch_themes' ) )
173                                 $this->wp_die( -1 );
174
175                         // If the theme has errors while loading, bail.
176                         if ( $this->theme()->errors() )
177                                 $this->wp_die( -1 );
178
179                         // If the theme isn't allowed per multisite settings, bail.
180                         if ( ! $this->theme()->is_allowed() )
181                                 $this->wp_die( -1 );
182                 }
183
184                 // All good, let's do some internal business to preview the theme.
185                 $this->start_previewing_theme();
186         }
187
188         /**
189          * Callback to validate a theme once it is loaded
190          *
191          * @since 3.4.0
192          */
193         function after_setup_theme() {
194                 if ( ! $this->doing_ajax() && ! validate_current_theme() ) {
195                         wp_redirect( 'themes.php?broken=true' );
196                         exit;
197                 }
198         }
199
200         /**
201          * Start previewing the selected theme by adding filters to change the current theme.
202          *
203          * @since 3.4.0
204          */
205         public function start_previewing_theme() {
206                 // Bail if we're already previewing.
207                 if ( $this->is_preview() )
208                         return;
209
210                 $this->previewing = true;
211
212                 if ( ! $this->is_theme_active() ) {
213                         add_filter( 'template', array( $this, 'get_template' ) );
214                         add_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
215                         add_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
216
217                         // @link: http://core.trac.wordpress.org/ticket/20027
218                         add_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
219                         add_filter( 'pre_option_template', array( $this, 'get_template' ) );
220
221                         // Handle custom theme roots.
222                         add_filter( 'pre_option_stylesheet_root', array( $this, 'get_stylesheet_root' ) );
223                         add_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
224                 }
225
226                 /**
227                  * Fires once the Customizer theme preview has started.
228                  *
229                  * @since 3.4.0
230                  *
231                  * @param WP_Customize_Manager $this WP_Customize_Manager instance.
232                  */
233                 do_action( 'start_previewing_theme', $this );
234         }
235
236         /**
237          * Stop previewing the selected theme.
238          *
239          * Removes filters to change the current theme.
240          *
241          * @since 3.4.0
242          */
243         public function stop_previewing_theme() {
244                 if ( ! $this->is_preview() )
245                         return;
246
247                 $this->previewing = false;
248
249                 if ( ! $this->is_theme_active() ) {
250                         remove_filter( 'template', array( $this, 'get_template' ) );
251                         remove_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
252                         remove_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
253
254                         // @link: http://core.trac.wordpress.org/ticket/20027
255                         remove_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
256                         remove_filter( 'pre_option_template', array( $this, 'get_template' ) );
257
258                         // Handle custom theme roots.
259                         remove_filter( 'pre_option_stylesheet_root', array( $this, 'get_stylesheet_root' ) );
260                         remove_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
261                 }
262
263                 /**
264                  * Fires once the Customizer theme preview has stopped.
265                  *
266                  * @since 3.4.0
267                  *
268                  * @param WP_Customize_Manager $this WP_Customize_Manager instance.
269                  */
270                 do_action( 'stop_previewing_theme', $this );
271         }
272
273         /**
274          * Get the theme being customized.
275          *
276          * @since 3.4.0
277          *
278          * @return WP_Theme
279          */
280         public function theme() {
281                 return $this->theme;
282         }
283
284         /**
285          * Get the registered settings.
286          *
287          * @since 3.4.0
288          *
289          * @return array
290          */
291         public function settings() {
292                 return $this->settings;
293         }
294
295         /**
296          * Get the registered controls.
297          *
298          * @since 3.4.0
299          *
300          * @return array
301          */
302         public function controls() {
303                 return $this->controls;
304         }
305
306         /**
307          * Get the registered sections.
308          *
309          * @since 3.4.0
310          *
311          * @return array
312          */
313         public function sections() {
314                 return $this->sections;
315         }
316
317         /**
318          * Checks if the current theme is active.
319          *
320          * @since 3.4.0
321          *
322          * @return bool
323          */
324         public function is_theme_active() {
325                 return $this->get_stylesheet() == $this->original_stylesheet;
326         }
327
328         /**
329          * Register styles/scripts and initialize the preview of each setting
330          *
331          * @since 3.4.0
332          */
333         public function wp_loaded() {
334
335                 /**
336                  * Fires once WordPress has loaded, allowing scripts and styles to be initialized.
337                  *
338                  * @since 3.4.0
339                  *
340                  * @param WP_Customize_Manager $this WP_Customize_Manager instance.
341                  */
342                 do_action( 'customize_register', $this );
343
344                 if ( $this->is_preview() && ! is_admin() )
345                         $this->customize_preview_init();
346         }
347
348         /**
349          * Prevents AJAX requests from following redirects when previewing a theme
350          * by issuing a 200 response instead of a 30x.
351          *
352          * Instead, the JS will sniff out the location header.
353          *
354          * @since 3.4.0
355          *
356          * @param $status
357          * @return int
358          */
359         public function wp_redirect_status( $status ) {
360                 if ( $this->is_preview() && ! is_admin() )
361                         return 200;
362
363                 return $status;
364         }
365
366         /**
367          * Decode the $_POST['customized'] values for a specific Customize Setting.
368          *
369          * @since 3.4.0
370          *
371          * @param mixed $setting A WP_Customize_Setting derived object
372          * @return string $post_value Sanitized value
373          */
374         public function post_value( $setting ) {
375                 if ( ! isset( $this->_post_values ) ) {
376                         if ( isset( $_POST['customized'] ) )
377                                 $this->_post_values = json_decode( wp_unslash( $_POST['customized'] ), true );
378                         else
379                                 $this->_post_values = false;
380                 }
381
382                 if ( isset( $this->_post_values[ $setting->id ] ) )
383                         return $setting->sanitize( $this->_post_values[ $setting->id ] );
384         }
385
386         /**
387          * Print javascript settings.
388          *
389          * @since 3.4.0
390          */
391         public function customize_preview_init() {
392                 $this->nonce_tick = check_ajax_referer( 'preview-customize_' . $this->get_stylesheet(), 'nonce' );
393
394                 $this->prepare_controls();
395
396                 wp_enqueue_script( 'customize-preview' );
397                 add_action( 'wp_head', array( $this, 'customize_preview_base' ) );
398                 add_action( 'wp_head', array( $this, 'customize_preview_html5' ) );
399                 add_action( 'wp_footer', array( $this, 'customize_preview_settings' ), 20 );
400                 add_action( 'shutdown', array( $this, 'customize_preview_signature' ), 1000 );
401                 add_filter( 'wp_die_handler', array( $this, 'remove_preview_signature' ) );
402
403                 foreach ( $this->settings as $setting ) {
404                         $setting->preview();
405                 }
406
407                 /**
408                  * Fires once the Customizer preview has initialized and JavaScript
409                  * settings have been printed.
410                  *
411                  * @since 3.4.0
412                  *
413                  * @param WP_Customize_Manager $this WP_Customize_Manager instance.
414                  */
415                 do_action( 'customize_preview_init', $this );
416         }
417
418         /**
419          * Print base element for preview frame.
420          *
421          * @since 3.4.0
422          */
423         public function customize_preview_base() {
424                 ?><base href="<?php echo home_url( '/' ); ?>" /><?php
425         }
426
427         /**
428          * Print a workaround to handle HTML5 tags in IE < 9
429          *
430          * @since 3.4.0
431          */
432         public function customize_preview_html5() { ?>
433                 <!--[if lt IE 9]>
434                 <script type="text/javascript">
435                         var e = [ 'abbr', 'article', 'aside', 'audio', 'canvas', 'datalist', 'details',
436                                 'figure', 'footer', 'header', 'hgroup', 'mark', 'menu', 'meter', 'nav',
437                                 'output', 'progress', 'section', 'time', 'video' ];
438                         for ( var i = 0; i < e.length; i++ ) {
439                                 document.createElement( e[i] );
440                         }
441                 </script>
442                 <![endif]--><?php
443         }
444
445         /**
446          * Print javascript settings for preview frame.
447          *
448          * @since 3.4.0
449          */
450         public function customize_preview_settings() {
451                 $settings = array(
452                         'values'  => array(),
453                         'channel' => esc_js( $_POST['customize_messenger_channel'] ),
454                 );
455
456                 if ( 2 == $this->nonce_tick ) {
457                         $settings['nonce'] = array(
458                                 'save' => wp_create_nonce( 'save-customize_' . $this->get_stylesheet() ),
459                                 'preview' => wp_create_nonce( 'preview-customize_' . $this->get_stylesheet() )
460                         );
461                 }
462
463                 foreach ( $this->settings as $id => $setting ) {
464                         $settings['values'][ $id ] = $setting->js_value();
465                 }
466
467                 ?>
468                 <script type="text/javascript">
469                         var _wpCustomizeSettings = <?php echo json_encode( $settings ); ?>;
470                 </script>
471                 <?php
472         }
473
474         /**
475          * Prints a signature so we can ensure the customizer was properly executed.
476          *
477          * @since 3.4.0
478          */
479         public function customize_preview_signature() {
480                 echo 'WP_CUSTOMIZER_SIGNATURE';
481         }
482
483         /**
484          * Removes the signature in case we experience a case where the customizer was not properly executed.
485          *
486          * @since 3.4.0
487          */
488         public function remove_preview_signature( $return = null ) {
489                 remove_action( 'shutdown', array( $this, 'customize_preview_signature' ), 1000 );
490
491                 return $return;
492         }
493
494         /**
495          * Is it a theme preview?
496          *
497          * @since 3.4.0
498          *
499          * @return bool True if it's a preview, false if not.
500          */
501         public function is_preview() {
502                 return (bool) $this->previewing;
503         }
504
505         /**
506          * Retrieve the template name of the previewed theme.
507          *
508          * @since 3.4.0
509          *
510          * @return string Template name.
511          */
512         public function get_template() {
513                 return $this->theme()->get_template();
514         }
515
516         /**
517          * Retrieve the stylesheet name of the previewed theme.
518          *
519          * @since 3.4.0
520          *
521          * @return string Stylesheet name.
522          */
523         public function get_stylesheet() {
524                 return $this->theme()->get_stylesheet();
525         }
526
527         /**
528          * Retrieve the template root of the previewed theme.
529          *
530          * @since 3.4.0
531          *
532          * @return string Theme root.
533          */
534         public function get_template_root() {
535                 return get_raw_theme_root( $this->get_template(), true );
536         }
537
538         /**
539          * Retrieve the stylesheet root of the previewed theme.
540          *
541          * @since 3.4.0
542          *
543          * @return string Theme root.
544          */
545         public function get_stylesheet_root() {
546                 return get_raw_theme_root( $this->get_stylesheet(), true );
547         }
548
549         /**
550          * Filter the current theme and return the name of the previewed theme.
551          *
552          * @since 3.4.0
553          *
554          * @param $current_theme {@internal Parameter is not used}
555          * @return string Theme name.
556          */
557         public function current_theme( $current_theme ) {
558                 return $this->theme()->display('Name');
559         }
560
561         /**
562          * Switch the theme and trigger the save() method on each setting.
563          *
564          * @since 3.4.0
565          */
566         public function save() {
567                 if ( ! $this->is_preview() )
568                         die;
569
570                 check_ajax_referer( 'save-customize_' . $this->get_stylesheet(), 'nonce' );
571
572                 // Do we have to switch themes?
573                 if ( ! $this->is_theme_active() ) {
574                         // Temporarily stop previewing the theme to allow switch_themes()
575                         // to operate properly.
576                         $this->stop_previewing_theme();
577                         switch_theme( $this->get_stylesheet() );
578                         update_option( 'theme_switched_via_customizer', true );
579                         $this->start_previewing_theme();
580                 }
581
582                 /**
583                  * Fires once the theme has switched in the Customizer, but before settings
584                  * have been saved.
585                  *
586                  * @since 3.4.0
587                  *
588                  * @param WP_Customize_Manager $this WP_Customize_Manager instance.
589                  */
590                 do_action( 'customize_save', $this );
591
592                 foreach ( $this->settings as $setting ) {
593                         $setting->save();
594                 }
595
596                 /**
597                  * Fires after Customize settings have been saved.
598                  *
599                  * @since 3.6.0
600                  *
601                  * @param WP_Customize_Manager $this WP_Customize_Manager instance.
602                  */
603                 do_action( 'customize_save_after', $this );
604
605                 die;
606         }
607
608         /**
609          * Add a customize setting.
610          *
611          * @since 3.4.0
612          *
613          * @param WP_Customize_Setting|string $id Customize Setting object, or ID.
614          * @param array $args                     Setting arguments; passed to WP_Customize_Setting
615          *                                        constructor.
616          */
617         public function add_setting( $id, $args = array() ) {
618                 if ( is_a( $id, 'WP_Customize_Setting' ) )
619                         $setting = $id;
620                 else
621                         $setting = new WP_Customize_Setting( $this, $id, $args );
622
623                 $this->settings[ $setting->id ] = $setting;
624         }
625
626         /**
627          * Retrieve a customize setting.
628          *
629          * @since 3.4.0
630          *
631          * @param string $id Customize Setting ID.
632          * @return WP_Customize_Setting
633          */
634         public function get_setting( $id ) {
635                 if ( isset( $this->settings[ $id ] ) )
636                         return $this->settings[ $id ];
637         }
638
639         /**
640          * Remove a customize setting.
641          *
642          * @since 3.4.0
643          *
644          * @param string $id Customize Setting ID.
645          */
646         public function remove_setting( $id ) {
647                 unset( $this->settings[ $id ] );
648         }
649
650         /**
651          * Add a customize section.
652          *
653          * @since 3.4.0
654          *
655          * @param WP_Customize_Section|string $id   Customize Section object, or Section ID.
656          * @param array                       $args Section arguments.
657          */
658         public function add_section( $id, $args = array() ) {
659                 if ( is_a( $id, 'WP_Customize_Section' ) )
660                         $section = $id;
661                 else
662                         $section = new WP_Customize_Section( $this, $id, $args );
663
664                 $this->sections[ $section->id ] = $section;
665         }
666
667         /**
668          * Retrieve a customize section.
669          *
670          * @since 3.4.0
671          *
672          * @param string $id Section ID.
673          * @return WP_Customize_Section
674          */
675         public function get_section( $id ) {
676                 if ( isset( $this->sections[ $id ] ) )
677                         return $this->sections[ $id ];
678         }
679
680         /**
681          * Remove a customize section.
682          *
683          * @since 3.4.0
684          *
685          * @param string $id Section ID.
686          */
687         public function remove_section( $id ) {
688                 unset( $this->sections[ $id ] );
689         }
690
691         /**
692          * Add a customize control.
693          *
694          * @since 3.4.0
695          *
696          * @param WP_Customize_Control|string $id   Customize Control object, or ID.
697          * @param array                       $args Control arguments; passed to WP_Customize_Control
698          *                                          constructor.
699          */
700         public function add_control( $id, $args = array() ) {
701                 if ( is_a( $id, 'WP_Customize_Control' ) )
702                         $control = $id;
703                 else
704                         $control = new WP_Customize_Control( $this, $id, $args );
705
706                 $this->controls[ $control->id ] = $control;
707         }
708
709         /**
710          * Retrieve a customize control.
711          *
712          * @since 3.4.0
713          *
714          * @param string $id ID of the control.
715          * @return WP_Customize_Control $control The control object.
716          */
717         public function get_control( $id ) {
718                 if ( isset( $this->controls[ $id ] ) )
719                         return $this->controls[ $id ];
720         }
721
722         /**
723          * Remove a customize control.
724          *
725          * @since 3.4.0
726          *
727          * @param string $id ID of the control.
728          */
729         public function remove_control( $id ) {
730                 unset( $this->controls[ $id ] );
731         }
732
733         /**
734          * Helper function to compare two objects by priority.
735          *
736          * @since 3.4.0
737          *
738          * @param object $a Object A.
739          * @param object $b Object B.
740          * @return int
741          */
742         protected final function _cmp_priority( $a, $b ) {
743                 $ap = $a->priority;
744                 $bp = $b->priority;
745
746                 if ( $ap == $bp )
747                         return 0;
748                 return ( $ap > $bp ) ? 1 : -1;
749         }
750
751         /**
752          * Prepare settings and sections.
753          *
754          * For each, check if required related components exist,
755          * whether the user has the necessary capabilities,
756          * and sort by priority.
757          *
758          * @since 3.4.0
759          */
760         public function prepare_controls() {
761
762                 $this->controls = array_reverse( $this->controls );
763                 $controls = array();
764
765                 foreach ( $this->controls as $id => $control ) {
766                         if ( ! isset( $this->sections[ $control->section ] ) || ! $control->check_capabilities() )
767                                 continue;
768
769                         $this->sections[ $control->section ]->controls[] = $control;
770                         $controls[ $id ] = $control;
771                 }
772                 $this->controls = $controls;
773
774                 // Prepare sections.
775                 // Reversing makes uasort sort by time added when conflicts occur.
776                 $this->sections = array_reverse( $this->sections );
777                 uasort( $this->sections, array( $this, '_cmp_priority' ) );
778                 $sections = array();
779
780                 foreach ( $this->sections as $section ) {
781                         if ( ! $section->check_capabilities() || ! $section->controls )
782                                 continue;
783
784                         usort( $section->controls, array( $this, '_cmp_priority' ) );
785                         $sections[] = $section;
786                 }
787                 $this->sections = $sections;
788         }
789
790         /**
791          * Enqueue scripts for customize controls.
792          *
793          * @since 3.4.0
794          */
795         public function enqueue_control_scripts() {
796                 foreach ( $this->controls as $control ) {
797                         $control->enqueue();
798                 }
799         }
800
801         /**
802          * Register some default controls.
803          *
804          * @since 3.4.0
805          */
806         public function register_controls() {
807
808                 /* Site Title & Tagline */
809
810                 $this->add_section( 'title_tagline', array(
811                         'title'    => __( 'Site Title & Tagline' ),
812                         'priority' => 20,
813                 ) );
814
815                 $this->add_setting( 'blogname', array(
816                         'default'    => get_option( 'blogname' ),
817                         'type'       => 'option',
818                         'capability' => 'manage_options',
819                 ) );
820
821                 $this->add_control( 'blogname', array(
822                         'label'      => __( 'Site Title' ),
823                         'section'    => 'title_tagline',
824                 ) );
825
826                 $this->add_setting( 'blogdescription', array(
827                         'default'    => get_option( 'blogdescription' ),
828                         'type'       => 'option',
829                         'capability' => 'manage_options',
830                 ) );
831
832                 $this->add_control( 'blogdescription', array(
833                         'label'      => __( 'Tagline' ),
834                         'section'    => 'title_tagline',
835                 ) );
836
837                 /* Colors */
838
839                 $this->add_section( 'colors', array(
840                         'title'          => __( 'Colors' ),
841                         'priority'       => 40,
842                 ) );
843
844                 $this->add_setting( 'header_textcolor', array(
845                         'theme_supports' => array( 'custom-header', 'header-text' ),
846                         'default'        => get_theme_support( 'custom-header', 'default-text-color' ),
847
848                         'sanitize_callback'    => array( $this, '_sanitize_header_textcolor' ),
849                         'sanitize_js_callback' => 'maybe_hash_hex_color',
850                 ) );
851
852                 // Input type: checkbox
853                 // With custom value
854                 $this->add_control( 'display_header_text', array(
855                         'settings' => 'header_textcolor',
856                         'label'    => __( 'Display Header Text' ),
857                         'section'  => 'title_tagline',
858                         'type'     => 'checkbox',
859                 ) );
860
861                 $this->add_control( new WP_Customize_Color_Control( $this, 'header_textcolor', array(
862                         'label'   => __( 'Header Text Color' ),
863                         'section' => 'colors',
864                 ) ) );
865
866                 // Input type: Color
867                 // With sanitize_callback
868                 $this->add_setting( 'background_color', array(
869                         'default'        => get_theme_support( 'custom-background', 'default-color' ),
870                         'theme_supports' => 'custom-background',
871
872                         'sanitize_callback'    => 'sanitize_hex_color_no_hash',
873                         'sanitize_js_callback' => 'maybe_hash_hex_color',
874                 ) );
875
876                 $this->add_control( new WP_Customize_Color_Control( $this, 'background_color', array(
877                         'label'   => __( 'Background Color' ),
878                         'section' => 'colors',
879                 ) ) );
880
881
882                 /* Custom Header */
883
884                 $this->add_section( 'header_image', array(
885                         'title'          => __( 'Header Image' ),
886                         'theme_supports' => 'custom-header',
887                         'priority'       => 60,
888                 ) );
889
890                 $this->add_setting( new WP_Customize_Filter_Setting( $this, 'header_image', array(
891                         'default'        => get_theme_support( 'custom-header', 'default-image' ),
892                         'theme_supports' => 'custom-header',
893                 ) ) );
894
895                 $this->add_setting( new WP_Customize_Header_Image_Setting( $this, 'header_image_data', array(
896                         // 'default'        => get_theme_support( 'custom-header', 'default-image' ),
897                         'theme_supports' => 'custom-header',
898                 ) ) );
899
900                 $this->add_control( new WP_Customize_Header_Image_Control( $this ) );
901
902                 /* Custom Background */
903
904                 $this->add_section( 'background_image', array(
905                         'title'          => __( 'Background Image' ),
906                         'theme_supports' => 'custom-background',
907                         'priority'       => 80,
908                 ) );
909
910                 $this->add_setting( 'background_image', array(
911                         'default'        => get_theme_support( 'custom-background', 'default-image' ),
912                         'theme_supports' => 'custom-background',
913                 ) );
914
915                 $this->add_setting( new WP_Customize_Background_Image_Setting( $this, 'background_image_thumb', array(
916                         'theme_supports' => 'custom-background',
917                 ) ) );
918
919                 $this->add_control( new WP_Customize_Background_Image_Control( $this ) );
920
921                 $this->add_setting( 'background_repeat', array(
922                         'default'        => 'repeat',
923                         'theme_supports' => 'custom-background',
924                 ) );
925
926                 $this->add_control( 'background_repeat', array(
927                         'label'      => __( 'Background Repeat' ),
928                         'section'    => 'background_image',
929                         'type'       => 'radio',
930                         'choices'    => array(
931                                 'no-repeat'  => __('No Repeat'),
932                                 'repeat'     => __('Tile'),
933                                 'repeat-x'   => __('Tile Horizontally'),
934                                 'repeat-y'   => __('Tile Vertically'),
935                         ),
936                 ) );
937
938                 $this->add_setting( 'background_position_x', array(
939                         'default'        => 'left',
940                         'theme_supports' => 'custom-background',
941                 ) );
942
943                 $this->add_control( 'background_position_x', array(
944                         'label'      => __( 'Background Position' ),
945                         'section'    => 'background_image',
946                         'type'       => 'radio',
947                         'choices'    => array(
948                                 'left'       => __('Left'),
949                                 'center'     => __('Center'),
950                                 'right'      => __('Right'),
951                         ),
952                 ) );
953
954                 $this->add_setting( 'background_attachment', array(
955                         'default'        => 'fixed',
956                         'theme_supports' => 'custom-background',
957                 ) );
958
959                 $this->add_control( 'background_attachment', array(
960                         'label'      => __( 'Background Attachment' ),
961                         'section'    => 'background_image',
962                         'type'       => 'radio',
963                         'choices'    => array(
964                                 'fixed'      => __('Fixed'),
965                                 'scroll'     => __('Scroll'),
966                         ),
967                 ) );
968
969                 // If the theme is using the default background callback, we can update
970                 // the background CSS using postMessage.
971                 if ( get_theme_support( 'custom-background', 'wp-head-callback' ) === '_custom_background_cb' ) {
972                         foreach ( array( 'color', 'image', 'position_x', 'repeat', 'attachment' ) as $prop ) {
973                                 $this->get_setting( 'background_' . $prop )->transport = 'postMessage';
974                         }
975                 }
976
977                 /* Nav Menus */
978
979                 $locations      = get_registered_nav_menus();
980                 $menus          = wp_get_nav_menus();
981                 $menu_locations = get_nav_menu_locations();
982                 $num_locations  = count( array_keys( $locations ) );
983
984                 $this->add_section( 'nav', array(
985                         'title'          => __( 'Navigation' ),
986                         'theme_supports' => 'menus',
987                         'priority'       => 100,
988                         'description'    => sprintf( _n('Your theme supports %s menu. Select which menu you would like to use.', 'Your theme supports %s menus. Select which menu appears in each location.', $num_locations ), number_format_i18n( $num_locations ) ) . "\n\n" . __('You can edit your menu content on the Menus screen in the Appearance section.'),
989                 ) );
990
991                 if ( $menus ) {
992                         $choices = array( 0 => __( '&mdash; Select &mdash;' ) );
993                         foreach ( $menus as $menu ) {
994                                 $choices[ $menu->term_id ] = wp_html_excerpt( $menu->name, 40, '&hellip;' );
995                         }
996
997                         foreach ( $locations as $location => $description ) {
998                                 $menu_setting_id = "nav_menu_locations[{$location}]";
999
1000                                 $this->add_setting( $menu_setting_id, array(
1001                                         'sanitize_callback' => 'absint',
1002                                         'theme_supports'    => 'menus',
1003                                 ) );
1004
1005                                 $this->add_control( $menu_setting_id, array(
1006                                         'label'   => $description,
1007                                         'section' => 'nav',
1008                                         'type'    => 'select',
1009                                         'choices' => $choices,
1010                                 ) );
1011                         }
1012                 }
1013
1014                 /* Static Front Page */
1015                 // #WP19627
1016
1017                 $this->add_section( 'static_front_page', array(
1018                         'title'          => __( 'Static Front Page' ),
1019                 //      'theme_supports' => 'static-front-page',
1020                         'priority'       => 120,
1021                         'description'    => __( 'Your theme supports a static front page.' ),
1022                 ) );
1023
1024                 $this->add_setting( 'show_on_front', array(
1025                         'default'        => get_option( 'show_on_front' ),
1026                         'capability'     => 'manage_options',
1027                         'type'           => 'option',
1028                 //      'theme_supports' => 'static-front-page',
1029                 ) );
1030
1031                 $this->add_control( 'show_on_front', array(
1032                         'label'   => __( 'Front page displays' ),
1033                         'section' => 'static_front_page',
1034                         'type'    => 'radio',
1035                         'choices' => array(
1036                                 'posts' => __( 'Your latest posts' ),
1037                                 'page'  => __( 'A static page' ),
1038                         ),
1039                 ) );
1040
1041                 $this->add_setting( 'page_on_front', array(
1042                         'type'       => 'option',
1043                         'capability' => 'manage_options',
1044                 //      'theme_supports' => 'static-front-page',
1045                 ) );
1046
1047                 $this->add_control( 'page_on_front', array(
1048                         'label'      => __( 'Front page' ),
1049                         'section'    => 'static_front_page',
1050                         'type'       => 'dropdown-pages',
1051                 ) );
1052
1053                 $this->add_setting( 'page_for_posts', array(
1054                         'type'           => 'option',
1055                         'capability'     => 'manage_options',
1056                 //      'theme_supports' => 'static-front-page',
1057                 ) );
1058
1059                 $this->add_control( 'page_for_posts', array(
1060                         'label'      => __( 'Posts page' ),
1061                         'section'    => 'static_front_page',
1062                         'type'       => 'dropdown-pages',
1063                 ) );
1064         }
1065
1066         /**
1067          * Callback for validating the header_textcolor value.
1068          *
1069          * Accepts 'blank', and otherwise uses sanitize_hex_color_no_hash().
1070          * Returns default text color if hex color is empty.
1071          *
1072          * @since 3.4.0
1073          *
1074          * @param string $color
1075          * @return string
1076          */
1077         public function _sanitize_header_textcolor( $color ) {
1078                 if ( 'blank' === $color )
1079                         return 'blank';
1080
1081                 $color = sanitize_hex_color_no_hash( $color );
1082                 if ( empty( $color ) )
1083                         $color = get_theme_support( 'custom-header', 'default-text-color' );
1084
1085                 return $color;
1086         }
1087 };
1088
1089 /**
1090  * Sanitizes a hex color.
1091  *
1092  * Returns either '', a 3 or 6 digit hex color (with #), or null.
1093  * For sanitizing values without a #, see sanitize_hex_color_no_hash().
1094  *
1095  * @since 3.4.0
1096  *
1097  * @param string $color
1098  * @return string|null
1099  */
1100 function sanitize_hex_color( $color ) {
1101         if ( '' === $color )
1102                 return '';
1103
1104         // 3 or 6 hex digits, or the empty string.
1105         if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) )
1106                 return $color;
1107
1108         return null;
1109 }
1110
1111 /**
1112  * Sanitizes a hex color without a hash. Use sanitize_hex_color() when possible.
1113  *
1114  * Saving hex colors without a hash puts the burden of adding the hash on the
1115  * UI, which makes it difficult to use or upgrade to other color types such as
1116  * rgba, hsl, rgb, and html color names.
1117  *
1118  * Returns either '', a 3 or 6 digit hex color (without a #), or null.
1119  *
1120  * @since 3.4.0
1121  * @uses sanitize_hex_color()
1122  *
1123  * @param string $color
1124  * @return string|null
1125  */
1126 function sanitize_hex_color_no_hash( $color ) {
1127         $color = ltrim( $color, '#' );
1128
1129         if ( '' === $color )
1130                 return '';
1131
1132         return sanitize_hex_color( '#' . $color ) ? $color : null;
1133 }
1134
1135 /**
1136  * Ensures that any hex color is properly hashed.
1137  * Otherwise, returns value untouched.
1138  *
1139  * This method should only be necessary if using sanitize_hex_color_no_hash().
1140  *
1141  * @since 3.4.0
1142  *
1143  * @param string $color
1144  * @return string
1145  */
1146 function maybe_hash_hex_color( $color ) {
1147         if ( $unhashed = sanitize_hex_color_no_hash( $color ) )
1148                 return '#' . $unhashed;
1149
1150         return $color;
1151 }