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