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