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