]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-wp-customize-manager.php
WordPress 4.5.1
[autoinstalls/wordpress.git] / wp-includes / class-wp-customize-manager.php
1 <?php
2 /**
3  * WordPress Customize Manager classes
4  *
5  * @package WordPress
6  * @subpackage Customize
7  * @since 3.4.0
8  */
9
10 /**
11  * Customize Manager class.
12  *
13  * Bootstraps the Customize experience on the server-side.
14  *
15  * Sets up the theme-switching process if a theme other than the active one is
16  * being previewed and customized.
17  *
18  * Serves as a factory for Customize Controls and Settings, and
19  * instantiates default Customize Controls and Settings.
20  *
21  * @since 3.4.0
22  */
23 final class WP_Customize_Manager {
24         /**
25          * An instance of the theme being previewed.
26          *
27          * @since 3.4.0
28          * @access protected
29          * @var WP_Theme
30          */
31         protected $theme;
32
33         /**
34          * The directory name of the previously active theme (within the theme_root).
35          *
36          * @since 3.4.0
37          * @access protected
38          * @var string
39          */
40         protected $original_stylesheet;
41
42         /**
43          * Whether this is a Customizer pageload.
44          *
45          * @since 3.4.0
46          * @access protected
47          * @var bool
48          */
49         protected $previewing = false;
50
51         /**
52          * Methods and properties dealing with managing widgets in the Customizer.
53          *
54          * @since 3.9.0
55          * @access public
56          * @var WP_Customize_Widgets
57          */
58         public $widgets;
59
60         /**
61          * Methods and properties dealing with managing nav menus in the Customizer.
62          *
63          * @since 4.3.0
64          * @access public
65          * @var WP_Customize_Nav_Menus
66          */
67         public $nav_menus;
68
69         /**
70          * Methods and properties dealing with selective refresh in the Customizer preview.
71          *
72          * @since 4.5.0
73          * @access public
74          * @var WP_Customize_Selective_Refresh
75          */
76         public $selective_refresh;
77
78         /**
79          * Registered instances of WP_Customize_Setting.
80          *
81          * @since 3.4.0
82          * @access protected
83          * @var array
84          */
85         protected $settings = array();
86
87         /**
88          * Sorted top-level instances of WP_Customize_Panel and WP_Customize_Section.
89          *
90          * @since 4.0.0
91          * @access protected
92          * @var array
93          */
94         protected $containers = array();
95
96         /**
97          * Registered instances of WP_Customize_Panel.
98          *
99          * @since 4.0.0
100          * @access protected
101          * @var array
102          */
103         protected $panels = array();
104
105         /**
106          * List of core components.
107          *
108          * @since 4.5.0
109          * @access protected
110          * @var array
111          */
112         protected $components = array( 'widgets', 'nav_menus' );
113
114         /**
115          * Registered instances of WP_Customize_Section.
116          *
117          * @since 3.4.0
118          * @access protected
119          * @var array
120          */
121         protected $sections = array();
122
123         /**
124          * Registered instances of WP_Customize_Control.
125          *
126          * @since 3.4.0
127          * @access protected
128          * @var array
129          */
130         protected $controls = array();
131
132         /**
133          * Return value of check_ajax_referer() in customize_preview_init() method.
134          *
135          * @since 3.5.0
136          * @access protected
137          * @var false|int
138          */
139         protected $nonce_tick;
140
141         /**
142          * Panel types that may be rendered from JS templates.
143          *
144          * @since 4.3.0
145          * @access protected
146          * @var array
147          */
148         protected $registered_panel_types = array();
149
150         /**
151          * Section types that may be rendered from JS templates.
152          *
153          * @since 4.3.0
154          * @access protected
155          * @var array
156          */
157         protected $registered_section_types = array();
158
159         /**
160          * Control types that may be rendered from JS templates.
161          *
162          * @since 4.1.0
163          * @access protected
164          * @var array
165          */
166         protected $registered_control_types = array();
167
168         /**
169          * Initial URL being previewed.
170          *
171          * @since 4.4.0
172          * @access protected
173          * @var string
174          */
175         protected $preview_url;
176
177         /**
178          * URL to link the user to when closing the Customizer.
179          *
180          * @since 4.4.0
181          * @access protected
182          * @var string
183          */
184         protected $return_url;
185
186         /**
187          * Mapping of 'panel', 'section', 'control' to the ID which should be autofocused.
188          *
189          * @since 4.4.0
190          * @access protected
191          * @var array
192          */
193         protected $autofocus = array();
194
195         /**
196          * Unsanitized values for Customize Settings parsed from $_POST['customized'].
197          *
198          * @var array
199          */
200         private $_post_values;
201
202         /**
203          * Constructor.
204          *
205          * @since 3.4.0
206          */
207         public function __construct() {
208                 require_once( ABSPATH . WPINC . '/class-wp-customize-setting.php' );
209                 require_once( ABSPATH . WPINC . '/class-wp-customize-panel.php' );
210                 require_once( ABSPATH . WPINC . '/class-wp-customize-section.php' );
211                 require_once( ABSPATH . WPINC . '/class-wp-customize-control.php' );
212
213                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-color-control.php' );
214                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-media-control.php' );
215                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-upload-control.php' );
216                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-image-control.php' );
217                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-background-image-control.php' );
218                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-cropped-image-control.php' );
219                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-site-icon-control.php' );
220                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-header-image-control.php' );
221                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-theme-control.php' );
222                 require_once( ABSPATH . WPINC . '/customize/class-wp-widget-area-customize-control.php' );
223                 require_once( ABSPATH . WPINC . '/customize/class-wp-widget-form-customize-control.php' );
224                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-control.php' );
225                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-item-control.php' );
226                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-location-control.php' );
227                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-name-control.php' );
228                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-auto-add-control.php' );
229                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-new-menu-control.php' );
230
231                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menus-panel.php' );
232
233                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-themes-section.php' );
234                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-sidebar-section.php' );
235                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-section.php' );
236                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-new-menu-section.php' );
237
238                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-filter-setting.php' );
239                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-header-image-setting.php' );
240                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-background-image-setting.php' );
241                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-item-setting.php' );
242                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-setting.php' );
243
244                 /**
245                  * Filter the core Customizer components to load.
246                  *
247                  * This allows Core components to be excluded from being instantiated by
248                  * filtering them out of the array. Note that this filter generally runs
249                  * during the {@see 'plugins_loaded'} action, so it cannot be added
250                  * in a theme.
251                  *
252                  * @since 4.4.0
253                  *
254                  * @see WP_Customize_Manager::__construct()
255                  *
256                  * @param array                $components List of core components to load.
257                  * @param WP_Customize_Manager $this       WP_Customize_Manager instance.
258                  */
259                 $components = apply_filters( 'customize_loaded_components', $this->components, $this );
260
261                 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-selective-refresh.php' );
262                 $this->selective_refresh = new WP_Customize_Selective_Refresh( $this );
263
264                 if ( in_array( 'widgets', $components, true ) ) {
265                         require_once( ABSPATH . WPINC . '/class-wp-customize-widgets.php' );
266                         $this->widgets = new WP_Customize_Widgets( $this );
267                 }
268
269                 if ( in_array( 'nav_menus', $components, true ) ) {
270                         require_once( ABSPATH . WPINC . '/class-wp-customize-nav-menus.php' );
271                         $this->nav_menus = new WP_Customize_Nav_Menus( $this );
272                 }
273
274                 add_filter( 'wp_die_handler', array( $this, 'wp_die_handler' ) );
275
276                 add_action( 'setup_theme', array( $this, 'setup_theme' ) );
277                 add_action( 'wp_loaded',   array( $this, 'wp_loaded' ) );
278
279                 // Run wp_redirect_status late to make sure we override the status last.
280                 add_action( 'wp_redirect_status', array( $this, 'wp_redirect_status' ), 1000 );
281
282                 // Do not spawn cron (especially the alternate cron) while running the Customizer.
283                 remove_action( 'init', 'wp_cron' );
284
285                 // Do not run update checks when rendering the controls.
286                 remove_action( 'admin_init', '_maybe_update_core' );
287                 remove_action( 'admin_init', '_maybe_update_plugins' );
288                 remove_action( 'admin_init', '_maybe_update_themes' );
289
290                 add_action( 'wp_ajax_customize_save',           array( $this, 'save' ) );
291                 add_action( 'wp_ajax_customize_refresh_nonces', array( $this, 'refresh_nonces' ) );
292
293                 add_action( 'customize_register',                 array( $this, 'register_controls' ) );
294                 add_action( 'customize_register',                 array( $this, 'register_dynamic_settings' ), 11 ); // allow code to create settings first
295                 add_action( 'customize_controls_init',            array( $this, 'prepare_controls' ) );
296                 add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_control_scripts' ) );
297
298                 // Render Panel, Section, and Control templates.
299                 add_action( 'customize_controls_print_footer_scripts', array( $this, 'render_panel_templates' ), 1 );
300                 add_action( 'customize_controls_print_footer_scripts', array( $this, 'render_section_templates' ), 1 );
301                 add_action( 'customize_controls_print_footer_scripts', array( $this, 'render_control_templates' ), 1 );
302
303                 // Export the settings to JS via the _wpCustomizeSettings variable.
304                 add_action( 'customize_controls_print_footer_scripts', array( $this, 'customize_pane_settings' ), 1000 );
305         }
306
307         /**
308          * Return true if it's an AJAX request.
309          *
310          * @since 3.4.0
311          * @since 4.2.0 Added `$action` param.
312          * @access public
313          *
314          * @param string|null $action Whether the supplied AJAX action is being run.
315          * @return bool True if it's an AJAX request, false otherwise.
316          */
317         public function doing_ajax( $action = null ) {
318                 $doing_ajax = ( defined( 'DOING_AJAX' ) && DOING_AJAX );
319                 if ( ! $doing_ajax ) {
320                         return false;
321                 }
322
323                 if ( ! $action ) {
324                         return true;
325                 } else {
326                         /*
327                          * Note: we can't just use doing_action( "wp_ajax_{$action}" ) because we need
328                          * to check before admin-ajax.php gets to that point.
329                          */
330                         return isset( $_REQUEST['action'] ) && wp_unslash( $_REQUEST['action'] ) === $action;
331                 }
332         }
333
334         /**
335          * Custom wp_die wrapper. Returns either the standard message for UI
336          * or the AJAX message.
337          *
338          * @since 3.4.0
339          *
340          * @param mixed $ajax_message AJAX return
341          * @param mixed $message UI message
342          */
343         protected function wp_die( $ajax_message, $message = null ) {
344                 if ( $this->doing_ajax() || isset( $_POST['customized'] ) ) {
345                         wp_die( $ajax_message );
346                 }
347
348                 if ( ! $message ) {
349                         $message = __( 'Cheatin&#8217; uh?' );
350                 }
351
352                 wp_die( $message );
353         }
354
355         /**
356          * Return the AJAX wp_die() handler if it's a customized request.
357          *
358          * @since 3.4.0
359          *
360          * @return string
361          */
362         public function wp_die_handler() {
363                 if ( $this->doing_ajax() || isset( $_POST['customized'] ) ) {
364                         return '_ajax_wp_die_handler';
365                 }
366
367                 return '_default_wp_die_handler';
368         }
369
370         /**
371          * Start preview and customize theme.
372          *
373          * Check if customize query variable exist. Init filters to filter the current theme.
374          *
375          * @since 3.4.0
376          */
377         public function setup_theme() {
378                 send_origin_headers();
379
380                 $doing_ajax_or_is_customized = ( $this->doing_ajax() || isset( $_POST['customized'] ) );
381                 if ( is_admin() && ! $doing_ajax_or_is_customized ) {
382                         auth_redirect();
383                 } elseif ( $doing_ajax_or_is_customized && ! is_user_logged_in() ) {
384                         $this->wp_die( 0, __( 'You must be logged in to complete this action.' ) );
385                 }
386
387                 show_admin_bar( false );
388
389                 if ( ! current_user_can( 'customize' ) ) {
390                         $this->wp_die( -1, __( 'You are not allowed to customize the appearance of this site.' ) );
391                 }
392
393                 $this->original_stylesheet = get_stylesheet();
394
395                 $this->theme = wp_get_theme( isset( $_REQUEST['theme'] ) ? $_REQUEST['theme'] : null );
396
397                 if ( $this->is_theme_active() ) {
398                         // Once the theme is loaded, we'll validate it.
399                         add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ) );
400                 } else {
401                         // If the requested theme is not the active theme and the user doesn't have the
402                         // switch_themes cap, bail.
403                         if ( ! current_user_can( 'switch_themes' ) ) {
404                                 $this->wp_die( -1, __( 'You are not allowed to edit theme options on this site.' ) );
405                         }
406
407                         // If the theme has errors while loading, bail.
408                         if ( $this->theme()->errors() ) {
409                                 $this->wp_die( -1, $this->theme()->errors()->get_error_message() );
410                         }
411
412                         // If the theme isn't allowed per multisite settings, bail.
413                         if ( ! $this->theme()->is_allowed() ) {
414                                 $this->wp_die( -1, __( 'The requested theme does not exist.' ) );
415                         }
416                 }
417
418                 $this->start_previewing_theme();
419         }
420
421         /**
422          * Callback to validate a theme once it is loaded
423          *
424          * @since 3.4.0
425          */
426         public function after_setup_theme() {
427                 $doing_ajax_or_is_customized = ( $this->doing_ajax() || isset( $_SERVER['customized'] ) );
428                 if ( ! $doing_ajax_or_is_customized && ! validate_current_theme() ) {
429                         wp_redirect( 'themes.php?broken=true' );
430                         exit;
431                 }
432         }
433
434         /**
435          * If the theme to be previewed isn't the active theme, add filter callbacks
436          * to swap it out at runtime.
437          *
438          * @since 3.4.0
439          */
440         public function start_previewing_theme() {
441                 // Bail if we're already previewing.
442                 if ( $this->is_preview() ) {
443                         return;
444                 }
445
446                 $this->previewing = true;
447
448                 if ( ! $this->is_theme_active() ) {
449                         add_filter( 'template', array( $this, 'get_template' ) );
450                         add_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
451                         add_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
452
453                         // @link: https://core.trac.wordpress.org/ticket/20027
454                         add_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
455                         add_filter( 'pre_option_template', array( $this, 'get_template' ) );
456
457                         // Handle custom theme roots.
458                         add_filter( 'pre_option_stylesheet_root', array( $this, 'get_stylesheet_root' ) );
459                         add_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
460                 }
461
462                 /**
463                  * Fires once the Customizer theme preview has started.
464                  *
465                  * @since 3.4.0
466                  *
467                  * @param WP_Customize_Manager $this WP_Customize_Manager instance.
468                  */
469                 do_action( 'start_previewing_theme', $this );
470         }
471
472         /**
473          * Stop previewing the selected theme.
474          *
475          * Removes filters to change the current theme.
476          *
477          * @since 3.4.0
478          */
479         public function stop_previewing_theme() {
480                 if ( ! $this->is_preview() ) {
481                         return;
482                 }
483
484                 $this->previewing = false;
485
486                 if ( ! $this->is_theme_active() ) {
487                         remove_filter( 'template', array( $this, 'get_template' ) );
488                         remove_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
489                         remove_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
490
491                         // @link: https://core.trac.wordpress.org/ticket/20027
492                         remove_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
493                         remove_filter( 'pre_option_template', array( $this, 'get_template' ) );
494
495                         // Handle custom theme roots.
496                         remove_filter( 'pre_option_stylesheet_root', array( $this, 'get_stylesheet_root' ) );
497                         remove_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
498                 }
499
500                 /**
501                  * Fires once the Customizer theme preview has stopped.
502                  *
503                  * @since 3.4.0
504                  *
505                  * @param WP_Customize_Manager $this WP_Customize_Manager instance.
506                  */
507                 do_action( 'stop_previewing_theme', $this );
508         }
509
510         /**
511          * Get the theme being customized.
512          *
513          * @since 3.4.0
514          *
515          * @return WP_Theme
516          */
517         public function theme() {
518                 if ( ! $this->theme ) {
519                         $this->theme = wp_get_theme();
520                 }
521                 return $this->theme;
522         }
523
524         /**
525          * Get the registered settings.
526          *
527          * @since 3.4.0
528          *
529          * @return array
530          */
531         public function settings() {
532                 return $this->settings;
533         }
534
535         /**
536          * Get the registered controls.
537          *
538          * @since 3.4.0
539          *
540          * @return array
541          */
542         public function controls() {
543                 return $this->controls;
544         }
545
546         /**
547          * Get the registered containers.
548          *
549          * @since 4.0.0
550          *
551          * @return array
552          */
553         public function containers() {
554                 return $this->containers;
555         }
556
557         /**
558          * Get the registered sections.
559          *
560          * @since 3.4.0
561          *
562          * @return array
563          */
564         public function sections() {
565                 return $this->sections;
566         }
567
568         /**
569          * Get the registered panels.
570          *
571          * @since 4.0.0
572          * @access public
573          *
574          * @return array Panels.
575          */
576         public function panels() {
577                 return $this->panels;
578         }
579
580         /**
581          * Checks if the current theme is active.
582          *
583          * @since 3.4.0
584          *
585          * @return bool
586          */
587         public function is_theme_active() {
588                 return $this->get_stylesheet() == $this->original_stylesheet;
589         }
590
591         /**
592          * Register styles/scripts and initialize the preview of each setting
593          *
594          * @since 3.4.0
595          */
596         public function wp_loaded() {
597
598                 /**
599                  * Fires once WordPress has loaded, allowing scripts and styles to be initialized.
600                  *
601                  * @since 3.4.0
602                  *
603                  * @param WP_Customize_Manager $this WP_Customize_Manager instance.
604                  */
605                 do_action( 'customize_register', $this );
606
607                 if ( $this->is_preview() && ! is_admin() )
608                         $this->customize_preview_init();
609         }
610
611         /**
612          * Prevents AJAX requests from following redirects when previewing a theme
613          * by issuing a 200 response instead of a 30x.
614          *
615          * Instead, the JS will sniff out the location header.
616          *
617          * @since 3.4.0
618          *
619          * @param $status
620          * @return int
621          */
622         public function wp_redirect_status( $status ) {
623                 if ( $this->is_preview() && ! is_admin() )
624                         return 200;
625
626                 return $status;
627         }
628
629         /**
630          * Parse the incoming $_POST['customized'] JSON data and store the unsanitized
631          * settings for subsequent post_value() lookups.
632          *
633          * @since 4.1.1
634          *
635          * @return array
636          */
637         public function unsanitized_post_values() {
638                 if ( ! isset( $this->_post_values ) ) {
639                         if ( isset( $_POST['customized'] ) ) {
640                                 $this->_post_values = json_decode( wp_unslash( $_POST['customized'] ), true );
641                         }
642                         if ( empty( $this->_post_values ) ) { // if not isset or if JSON error
643                                 $this->_post_values = array();
644                         }
645                 }
646                 if ( empty( $this->_post_values ) ) {
647                         return array();
648                 } else {
649                         return $this->_post_values;
650                 }
651         }
652
653         /**
654          * Return the sanitized value for a given setting from the request's POST data.
655          *
656          * @since 3.4.0
657          * @since 4.1.1 Introduced 'default' parameter.
658          *
659          * @param WP_Customize_Setting $setting A WP_Customize_Setting derived object
660          * @param mixed $default value returned $setting has no post value (added in 4.2.0).
661          * @return string|mixed $post_value Sanitized value or the $default provided
662          */
663         public function post_value( $setting, $default = null ) {
664                 $post_values = $this->unsanitized_post_values();
665                 if ( array_key_exists( $setting->id, $post_values ) ) {
666                         return $setting->sanitize( $post_values[ $setting->id ] );
667                 } else {
668                         return $default;
669                 }
670         }
671
672         /**
673          * Override a setting's (unsanitized) value as found in any incoming $_POST['customized'].
674          *
675          * @since 4.2.0
676          * @access public
677          *
678          * @param string $setting_id ID for the WP_Customize_Setting instance.
679          * @param mixed  $value      Post value.
680          */
681         public function set_post_value( $setting_id, $value ) {
682                 $this->unsanitized_post_values();
683                 $this->_post_values[ $setting_id ] = $value;
684
685                 /**
686                  * Announce when a specific setting's unsanitized post value has been set.
687                  *
688                  * Fires when the {@see WP_Customize_Manager::set_post_value()} method is called.
689                  *
690                  * The dynamic portion of the hook name, `$setting_id`, refers to the setting ID.
691                  *
692                  * @since 4.4.0
693                  *
694                  * @param mixed                $value Unsanitized setting post value.
695                  * @param WP_Customize_Manager $this  WP_Customize_Manager instance.
696                  */
697                 do_action( "customize_post_value_set_{$setting_id}", $value, $this );
698
699                 /**
700                  * Announce when any setting's unsanitized post value has been set.
701                  *
702                  * Fires when the {@see WP_Customize_Manager::set_post_value()} method is called.
703                  *
704                  * This is useful for `WP_Customize_Setting` instances to watch
705                  * in order to update a cached previewed value.
706                  *
707                  * @since 4.4.0
708                  *
709                  * @param string               $setting_id Setting ID.
710                  * @param mixed                $value      Unsanitized setting post value.
711                  * @param WP_Customize_Manager $this       WP_Customize_Manager instance.
712                  */
713                 do_action( 'customize_post_value_set', $setting_id, $value, $this );
714         }
715
716         /**
717          * Print JavaScript settings.
718          *
719          * @since 3.4.0
720          */
721         public function customize_preview_init() {
722                 $this->nonce_tick = check_ajax_referer( 'preview-customize_' . $this->get_stylesheet(), 'nonce' );
723
724                 $this->prepare_controls();
725
726                 wp_enqueue_script( 'customize-preview' );
727                 add_action( 'wp', array( $this, 'customize_preview_override_404_status' ) );
728                 add_action( 'wp_head', array( $this, 'customize_preview_base' ) );
729                 add_action( 'wp_head', array( $this, 'customize_preview_html5' ) );
730                 add_action( 'wp_head', array( $this, 'customize_preview_loading_style' ) );
731                 add_action( 'wp_footer', array( $this, 'customize_preview_settings' ), 20 );
732                 add_action( 'shutdown', array( $this, 'customize_preview_signature' ), 1000 );
733                 add_filter( 'wp_die_handler', array( $this, 'remove_preview_signature' ) );
734
735                 foreach ( $this->settings as $setting ) {
736                         $setting->preview();
737                 }
738
739                 /**
740                  * Fires once the Customizer preview has initialized and JavaScript
741                  * settings have been printed.
742                  *
743                  * @since 3.4.0
744                  *
745                  * @param WP_Customize_Manager $this WP_Customize_Manager instance.
746                  */
747                 do_action( 'customize_preview_init', $this );
748         }
749
750         /**
751          * Prevent sending a 404 status when returning the response for the customize
752          * preview, since it causes the jQuery AJAX to fail. Send 200 instead.
753          *
754          * @since 4.0.0
755          * @access public
756          */
757         public function customize_preview_override_404_status() {
758                 if ( is_404() ) {
759                         status_header( 200 );
760                 }
761         }
762
763         /**
764          * Print base element for preview frame.
765          *
766          * @since 3.4.0
767          */
768         public function customize_preview_base() {
769                 ?><base href="<?php echo home_url( '/' ); ?>" /><?php
770         }
771
772         /**
773          * Print a workaround to handle HTML5 tags in IE < 9.
774          *
775          * @since 3.4.0
776          */
777         public function customize_preview_html5() { ?>
778                 <!--[if lt IE 9]>
779                 <script type="text/javascript">
780                         var e = [ 'abbr', 'article', 'aside', 'audio', 'canvas', 'datalist', 'details',
781                                 'figure', 'footer', 'header', 'hgroup', 'mark', 'menu', 'meter', 'nav',
782                                 'output', 'progress', 'section', 'time', 'video' ];
783                         for ( var i = 0; i < e.length; i++ ) {
784                                 document.createElement( e[i] );
785                         }
786                 </script>
787                 <![endif]--><?php
788         }
789
790         /**
791          * Print CSS for loading indicators for the Customizer preview.
792          *
793          * @since 4.2.0
794          * @access public
795          */
796         public function customize_preview_loading_style() {
797                 ?><style>
798                         body.wp-customizer-unloading {
799                                 opacity: 0.25;
800                                 cursor: progress !important;
801                                 -webkit-transition: opacity 0.5s;
802                                 transition: opacity 0.5s;
803                         }
804                         body.wp-customizer-unloading * {
805                                 pointer-events: none !important;
806                         }
807                 </style><?php
808         }
809
810         /**
811          * Print JavaScript settings for preview frame.
812          *
813          * @since 3.4.0
814          */
815         public function customize_preview_settings() {
816                 $settings = array(
817                         'theme' => array(
818                                 'stylesheet' => $this->get_stylesheet(),
819                                 'active'     => $this->is_theme_active(),
820                         ),
821                         'url' => array(
822                                 'self' => empty( $_SERVER['REQUEST_URI'] ) ? home_url( '/' ) : esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ),
823                         ),
824                         'channel' => wp_unslash( $_POST['customize_messenger_channel'] ),
825                         'activePanels' => array(),
826                         'activeSections' => array(),
827                         'activeControls' => array(),
828                         'nonce' => $this->get_nonces(),
829                         'l10n' => array(
830                                 'shiftClickToEdit' => __( 'Shift-click to edit this element.' ),
831                         ),
832                         '_dirty' => array_keys( $this->unsanitized_post_values() ),
833                 );
834
835                 foreach ( $this->panels as $panel_id => $panel ) {
836                         if ( $panel->check_capabilities() ) {
837                                 $settings['activePanels'][ $panel_id ] = $panel->active();
838                                 foreach ( $panel->sections as $section_id => $section ) {
839                                         if ( $section->check_capabilities() ) {
840                                                 $settings['activeSections'][ $section_id ] = $section->active();
841                                         }
842                                 }
843                         }
844                 }
845                 foreach ( $this->sections as $id => $section ) {
846                         if ( $section->check_capabilities() ) {
847                                 $settings['activeSections'][ $id ] = $section->active();
848                         }
849                 }
850                 foreach ( $this->controls as $id => $control ) {
851                         if ( $control->check_capabilities() ) {
852                                 $settings['activeControls'][ $id ] = $control->active();
853                         }
854                 }
855
856                 ?>
857                 <script type="text/javascript">
858                         var _wpCustomizeSettings = <?php echo wp_json_encode( $settings ); ?>;
859                         _wpCustomizeSettings.values = {};
860                         (function( v ) {
861                                 <?php
862                                 /*
863                                  * Serialize settings separately from the initial _wpCustomizeSettings
864                                  * serialization in order to avoid a peak memory usage spike.
865                                  * @todo We may not even need to export the values at all since the pane syncs them anyway.
866                                  */
867                                 foreach ( $this->settings as $id => $setting ) {
868                                         if ( $setting->check_capabilities() ) {
869                                                 printf(
870                                                         "v[%s] = %s;\n",
871                                                         wp_json_encode( $id ),
872                                                         wp_json_encode( $setting->js_value() )
873                                                 );
874                                         }
875                                 }
876                                 ?>
877                         })( _wpCustomizeSettings.values );
878                 </script>
879                 <?php
880         }
881
882         /**
883          * Prints a signature so we can ensure the Customizer was properly executed.
884          *
885          * @since 3.4.0
886          */
887         public function customize_preview_signature() {
888                 echo 'WP_CUSTOMIZER_SIGNATURE';
889         }
890
891         /**
892          * Removes the signature in case we experience a case where the Customizer was not properly executed.
893          *
894          * @since 3.4.0
895          *
896          * @param mixed $return Value passed through for wp_die_handler filter.
897          * @return mixed Value passed through for wp_die_handler filter.
898          */
899         public function remove_preview_signature( $return = null ) {
900                 remove_action( 'shutdown', array( $this, 'customize_preview_signature' ), 1000 );
901
902                 return $return;
903         }
904
905         /**
906          * Is it a theme preview?
907          *
908          * @since 3.4.0
909          *
910          * @return bool True if it's a preview, false if not.
911          */
912         public function is_preview() {
913                 return (bool) $this->previewing;
914         }
915
916         /**
917          * Retrieve the template name of the previewed theme.
918          *
919          * @since 3.4.0
920          *
921          * @return string Template name.
922          */
923         public function get_template() {
924                 return $this->theme()->get_template();
925         }
926
927         /**
928          * Retrieve the stylesheet name of the previewed theme.
929          *
930          * @since 3.4.0
931          *
932          * @return string Stylesheet name.
933          */
934         public function get_stylesheet() {
935                 return $this->theme()->get_stylesheet();
936         }
937
938         /**
939          * Retrieve the template root of the previewed theme.
940          *
941          * @since 3.4.0
942          *
943          * @return string Theme root.
944          */
945         public function get_template_root() {
946                 return get_raw_theme_root( $this->get_template(), true );
947         }
948
949         /**
950          * Retrieve the stylesheet root of the previewed theme.
951          *
952          * @since 3.4.0
953          *
954          * @return string Theme root.
955          */
956         public function get_stylesheet_root() {
957                 return get_raw_theme_root( $this->get_stylesheet(), true );
958         }
959
960         /**
961          * Filter the current theme and return the name of the previewed theme.
962          *
963          * @since 3.4.0
964          *
965          * @param $current_theme {@internal Parameter is not used}
966          * @return string Theme name.
967          */
968         public function current_theme( $current_theme ) {
969                 return $this->theme()->display('Name');
970         }
971
972         /**
973          * Switch the theme and trigger the save() method on each setting.
974          *
975          * @since 3.4.0
976          */
977         public function save() {
978                 if ( ! $this->is_preview() ) {
979                         wp_send_json_error( 'not_preview' );
980                 }
981
982                 $action = 'save-customize_' . $this->get_stylesheet();
983                 if ( ! check_ajax_referer( $action, 'nonce', false ) ) {
984                         wp_send_json_error( 'invalid_nonce' );
985                 }
986
987                 // Do we have to switch themes?
988                 if ( ! $this->is_theme_active() ) {
989                         // Temporarily stop previewing the theme to allow switch_themes()
990                         // to operate properly.
991                         $this->stop_previewing_theme();
992                         switch_theme( $this->get_stylesheet() );
993                         update_option( 'theme_switched_via_customizer', true );
994                         $this->start_previewing_theme();
995                 }
996
997                 /**
998                  * Fires once the theme has switched in the Customizer, but before settings
999                  * have been saved.
1000                  *
1001                  * @since 3.4.0
1002                  *
1003                  * @param WP_Customize_Manager $this WP_Customize_Manager instance.
1004                  */
1005                 do_action( 'customize_save', $this );
1006
1007                 foreach ( $this->settings as $setting ) {
1008                         $setting->save();
1009                 }
1010
1011                 /**
1012                  * Fires after Customize settings have been saved.
1013                  *
1014                  * @since 3.6.0
1015                  *
1016                  * @param WP_Customize_Manager $this WP_Customize_Manager instance.
1017                  */
1018                 do_action( 'customize_save_after', $this );
1019
1020                 /**
1021                  * Filter response data for a successful customize_save AJAX request.
1022                  *
1023                  * This filter does not apply if there was a nonce or authentication failure.
1024                  *
1025                  * @since 4.2.0
1026                  *
1027                  * @param array                $data Additional information passed back to the 'saved'
1028                  *                                   event on `wp.customize`.
1029                  * @param WP_Customize_Manager $this WP_Customize_Manager instance.
1030                  */
1031                 $response = apply_filters( 'customize_save_response', array(), $this );
1032                 wp_send_json_success( $response );
1033         }
1034
1035         /**
1036          * Refresh nonces for the current preview.
1037          *
1038          * @since 4.2.0
1039          */
1040         public function refresh_nonces() {
1041                 if ( ! $this->is_preview() ) {
1042                         wp_send_json_error( 'not_preview' );
1043                 }
1044
1045                 wp_send_json_success( $this->get_nonces() );
1046         }
1047
1048         /**
1049          * Add a customize setting.
1050          *
1051          * @since 3.4.0
1052          * @since 4.5.0 Return added WP_Customize_Setting instance.
1053          * @access public
1054          *
1055          * @param WP_Customize_Setting|string $id   Customize Setting object, or ID.
1056          * @param array                       $args Setting arguments; passed to WP_Customize_Setting
1057          *                                          constructor.
1058          * @return WP_Customize_Setting             The instance of the setting that was added.
1059          */
1060         public function add_setting( $id, $args = array() ) {
1061                 if ( $id instanceof WP_Customize_Setting ) {
1062                         $setting = $id;
1063                 } else {
1064                         $class = 'WP_Customize_Setting';
1065
1066                         /** This filter is documented in wp-includes/class-wp-customize-manager.php */
1067                         $args = apply_filters( 'customize_dynamic_setting_args', $args, $id );
1068
1069                         /** This filter is documented in wp-includes/class-wp-customize-manager.php */
1070                         $class = apply_filters( 'customize_dynamic_setting_class', $class, $id, $args );
1071
1072                         $setting = new $class( $this, $id, $args );
1073                 }
1074
1075                 $this->settings[ $setting->id ] = $setting;
1076                 return $setting;
1077         }
1078
1079         /**
1080          * Register any dynamically-created settings, such as those from $_POST['customized']
1081          * that have no corresponding setting created.
1082          *
1083          * This is a mechanism to "wake up" settings that have been dynamically created
1084          * on the front end and have been sent to WordPress in `$_POST['customized']`. When WP
1085          * loads, the dynamically-created settings then will get created and previewed
1086          * even though they are not directly created statically with code.
1087          *
1088          * @since 4.2.0
1089          * @access public
1090          *
1091          * @param array $setting_ids The setting IDs to add.
1092          * @return array The WP_Customize_Setting objects added.
1093          */
1094         public function add_dynamic_settings( $setting_ids ) {
1095                 $new_settings = array();
1096                 foreach ( $setting_ids as $setting_id ) {
1097                         // Skip settings already created
1098                         if ( $this->get_setting( $setting_id ) ) {
1099                                 continue;
1100                         }
1101
1102                         $setting_args = false;
1103                         $setting_class = 'WP_Customize_Setting';
1104
1105                         /**
1106                          * Filter a dynamic setting's constructor args.
1107                          *
1108                          * For a dynamic setting to be registered, this filter must be employed
1109                          * to override the default false value with an array of args to pass to
1110                          * the WP_Customize_Setting constructor.
1111                          *
1112                          * @since 4.2.0
1113                          *
1114                          * @param false|array $setting_args The arguments to the WP_Customize_Setting constructor.
1115                          * @param string      $setting_id   ID for dynamic setting, usually coming from `$_POST['customized']`.
1116                          */
1117                         $setting_args = apply_filters( 'customize_dynamic_setting_args', $setting_args, $setting_id );
1118                         if ( false === $setting_args ) {
1119                                 continue;
1120                         }
1121
1122                         /**
1123                          * Allow non-statically created settings to be constructed with custom WP_Customize_Setting subclass.
1124                          *
1125                          * @since 4.2.0
1126                          *
1127                          * @param string $setting_class WP_Customize_Setting or a subclass.
1128                          * @param string $setting_id    ID for dynamic setting, usually coming from `$_POST['customized']`.
1129                          * @param array  $setting_args  WP_Customize_Setting or a subclass.
1130                          */
1131                         $setting_class = apply_filters( 'customize_dynamic_setting_class', $setting_class, $setting_id, $setting_args );
1132
1133                         $setting = new $setting_class( $this, $setting_id, $setting_args );
1134
1135                         $this->add_setting( $setting );
1136                         $new_settings[] = $setting;
1137                 }
1138                 return $new_settings;
1139         }
1140
1141         /**
1142          * Retrieve a customize setting.
1143          *
1144          * @since 3.4.0
1145          *
1146          * @param string $id Customize Setting ID.
1147          * @return WP_Customize_Setting|void The setting, if set.
1148          */
1149         public function get_setting( $id ) {
1150                 if ( isset( $this->settings[ $id ] ) ) {
1151                         return $this->settings[ $id ];
1152                 }
1153         }
1154
1155         /**
1156          * Remove a customize setting.
1157          *
1158          * @since 3.4.0
1159          *
1160          * @param string $id Customize Setting ID.
1161          */
1162         public function remove_setting( $id ) {
1163                 unset( $this->settings[ $id ] );
1164         }
1165
1166         /**
1167          * Add a customize panel.
1168          *
1169          * @since 4.0.0
1170          * @since 4.5.0 Return added WP_Customize_Panel instance.
1171          * @access public
1172          *
1173          * @param WP_Customize_Panel|string $id   Customize Panel object, or Panel ID.
1174          * @param array                     $args Optional. Panel arguments. Default empty array.
1175          *
1176          * @return WP_Customize_Panel             The instance of the panel that was added.
1177          */
1178         public function add_panel( $id, $args = array() ) {
1179                 if ( $id instanceof WP_Customize_Panel ) {
1180                         $panel = $id;
1181                 } else {
1182                         $panel = new WP_Customize_Panel( $this, $id, $args );
1183                 }
1184
1185                 $this->panels[ $panel->id ] = $panel;
1186                 return $panel;
1187         }
1188
1189         /**
1190          * Retrieve a customize panel.
1191          *
1192          * @since 4.0.0
1193          * @access public
1194          *
1195          * @param string $id Panel ID to get.
1196          * @return WP_Customize_Panel|void Requested panel instance, if set.
1197          */
1198         public function get_panel( $id ) {
1199                 if ( isset( $this->panels[ $id ] ) ) {
1200                         return $this->panels[ $id ];
1201                 }
1202         }
1203
1204         /**
1205          * Remove a customize panel.
1206          *
1207          * @since 4.0.0
1208          * @access public
1209          *
1210          * @param string $id Panel ID to remove.
1211          */
1212         public function remove_panel( $id ) {
1213                 // Removing core components this way is _doing_it_wrong().
1214                 if ( in_array( $id, $this->components, true ) ) {
1215                         /* translators: 1: panel id, 2: link to 'customize_loaded_components' filter reference */
1216                         $message = sprintf( __( 'Removing %1$s manually will cause PHP warnings. Use the %2$s filter instead.' ),
1217                                 $id,
1218                                 '<a href="' . esc_url( 'https://developer.wordpress.org/reference/hooks/customize_loaded_components/' ) . '"><code>customize_loaded_components</code></a>'
1219                         );
1220
1221                         _doing_it_wrong( __METHOD__, $message, '4.5' );
1222                 }
1223                 unset( $this->panels[ $id ] );
1224         }
1225
1226         /**
1227          * Register a customize panel type.
1228          *
1229          * Registered types are eligible to be rendered via JS and created dynamically.
1230          *
1231          * @since 4.3.0
1232          * @access public
1233          *
1234          * @see WP_Customize_Panel
1235          *
1236          * @param string $panel Name of a custom panel which is a subclass of WP_Customize_Panel.
1237          */
1238         public function register_panel_type( $panel ) {
1239                 $this->registered_panel_types[] = $panel;
1240         }
1241
1242         /**
1243          * Render JS templates for all registered panel types.
1244          *
1245          * @since 4.3.0
1246          * @access public
1247          */
1248         public function render_panel_templates() {
1249                 foreach ( $this->registered_panel_types as $panel_type ) {
1250                         $panel = new $panel_type( $this, 'temp', array() );
1251                         $panel->print_template();
1252                 }
1253         }
1254
1255         /**
1256          * Add a customize section.
1257          *
1258          * @since 3.4.0
1259          * @since 4.5.0 Return added WP_Customize_Section instance.
1260          * @access public
1261          *
1262          * @param WP_Customize_Section|string $id   Customize Section object, or Section ID.
1263          * @param array                       $args Section arguments.
1264          *
1265          * @return WP_Customize_Section             The instance of the section that was added.
1266          */
1267         public function add_section( $id, $args = array() ) {
1268                 if ( $id instanceof WP_Customize_Section ) {
1269                         $section = $id;
1270                 } else {
1271                         $section = new WP_Customize_Section( $this, $id, $args );
1272                 }
1273
1274                 $this->sections[ $section->id ] = $section;
1275                 return $section;
1276         }
1277
1278         /**
1279          * Retrieve a customize section.
1280          *
1281          * @since 3.4.0
1282          *
1283          * @param string $id Section ID.
1284          * @return WP_Customize_Section|void The section, if set.
1285          */
1286         public function get_section( $id ) {
1287                 if ( isset( $this->sections[ $id ] ) )
1288                         return $this->sections[ $id ];
1289         }
1290
1291         /**
1292          * Remove a customize section.
1293          *
1294          * @since 3.4.0
1295          *
1296          * @param string $id Section ID.
1297          */
1298         public function remove_section( $id ) {
1299                 unset( $this->sections[ $id ] );
1300         }
1301
1302         /**
1303          * Register a customize section type.
1304          *
1305          * Registered types are eligible to be rendered via JS and created dynamically.
1306          *
1307          * @since 4.3.0
1308          * @access public
1309          *
1310          * @see WP_Customize_Section
1311          *
1312          * @param string $section Name of a custom section which is a subclass of WP_Customize_Section.
1313          */
1314         public function register_section_type( $section ) {
1315                 $this->registered_section_types[] = $section;
1316         }
1317
1318         /**
1319          * Render JS templates for all registered section types.
1320          *
1321          * @since 4.3.0
1322          * @access public
1323          */
1324         public function render_section_templates() {
1325                 foreach ( $this->registered_section_types as $section_type ) {
1326                         $section = new $section_type( $this, 'temp', array() );
1327                         $section->print_template();
1328                 }
1329         }
1330
1331         /**
1332          * Add a customize control.
1333          *
1334          * @since 3.4.0
1335          * @since 4.5.0 Return added WP_Customize_Control instance.
1336          * @access public
1337          *
1338          * @param WP_Customize_Control|string $id   Customize Control object, or ID.
1339          * @param array                       $args Control arguments; passed to WP_Customize_Control
1340          *                                          constructor.
1341          * @return WP_Customize_Control             The instance of the control that was added.
1342          */
1343         public function add_control( $id, $args = array() ) {
1344                 if ( $id instanceof WP_Customize_Control ) {
1345                         $control = $id;
1346                 } else {
1347                         $control = new WP_Customize_Control( $this, $id, $args );
1348                 }
1349
1350                 $this->controls[ $control->id ] = $control;
1351                 return $control;
1352         }
1353
1354         /**
1355          * Retrieve a customize control.
1356          *
1357          * @since 3.4.0
1358          *
1359          * @param string $id ID of the control.
1360          * @return WP_Customize_Control|void The control object, if set.
1361          */
1362         public function get_control( $id ) {
1363                 if ( isset( $this->controls[ $id ] ) )
1364                         return $this->controls[ $id ];
1365         }
1366
1367         /**
1368          * Remove a customize control.
1369          *
1370          * @since 3.4.0
1371          *
1372          * @param string $id ID of the control.
1373          */
1374         public function remove_control( $id ) {
1375                 unset( $this->controls[ $id ] );
1376         }
1377
1378         /**
1379          * Register a customize control type.
1380          *
1381          * Registered types are eligible to be rendered via JS and created dynamically.
1382          *
1383          * @since 4.1.0
1384          * @access public
1385          *
1386          * @param string $control Name of a custom control which is a subclass of
1387          *                        {@see WP_Customize_Control}.
1388          */
1389         public function register_control_type( $control ) {
1390                 $this->registered_control_types[] = $control;
1391         }
1392
1393         /**
1394          * Render JS templates for all registered control types.
1395          *
1396          * @since 4.1.0
1397          * @access public
1398          */
1399         public function render_control_templates() {
1400                 foreach ( $this->registered_control_types as $control_type ) {
1401                         $control = new $control_type( $this, 'temp', array(
1402                                 'settings' => array(),
1403                         ) );
1404                         $control->print_template();
1405                 }
1406         }
1407
1408         /**
1409          * Helper function to compare two objects by priority, ensuring sort stability via instance_number.
1410          *
1411          * @since 3.4.0
1412          *
1413          * @param WP_Customize_Panel|WP_Customize_Section|WP_Customize_Control $a Object A.
1414          * @param WP_Customize_Panel|WP_Customize_Section|WP_Customize_Control $b Object B.
1415          * @return int
1416          */
1417         protected function _cmp_priority( $a, $b ) {
1418                 if ( $a->priority === $b->priority ) {
1419                         return $a->instance_number - $b->instance_number;
1420                 } else {
1421                         return $a->priority - $b->priority;
1422                 }
1423         }
1424
1425         /**
1426          * Prepare panels, sections, and controls.
1427          *
1428          * For each, check if required related components exist,
1429          * whether the user has the necessary capabilities,
1430          * and sort by priority.
1431          *
1432          * @since 3.4.0
1433          */
1434         public function prepare_controls() {
1435
1436                 $controls = array();
1437                 uasort( $this->controls, array( $this, '_cmp_priority' ) );
1438
1439                 foreach ( $this->controls as $id => $control ) {
1440                         if ( ! isset( $this->sections[ $control->section ] ) || ! $control->check_capabilities() ) {
1441                                 continue;
1442                         }
1443
1444                         $this->sections[ $control->section ]->controls[] = $control;
1445                         $controls[ $id ] = $control;
1446                 }
1447                 $this->controls = $controls;
1448
1449                 // Prepare sections.
1450                 uasort( $this->sections, array( $this, '_cmp_priority' ) );
1451                 $sections = array();
1452
1453                 foreach ( $this->sections as $section ) {
1454                         if ( ! $section->check_capabilities() ) {
1455                                 continue;
1456                         }
1457
1458                         usort( $section->controls, array( $this, '_cmp_priority' ) );
1459
1460                         if ( ! $section->panel ) {
1461                                 // Top-level section.
1462                                 $sections[ $section->id ] = $section;
1463                         } else {
1464                                 // This section belongs to a panel.
1465                                 if ( isset( $this->panels [ $section->panel ] ) ) {
1466                                         $this->panels[ $section->panel ]->sections[ $section->id ] = $section;
1467                                 }
1468                         }
1469                 }
1470                 $this->sections = $sections;
1471
1472                 // Prepare panels.
1473                 uasort( $this->panels, array( $this, '_cmp_priority' ) );
1474                 $panels = array();
1475
1476                 foreach ( $this->panels as $panel ) {
1477                         if ( ! $panel->check_capabilities() ) {
1478                                 continue;
1479                         }
1480
1481                         uasort( $panel->sections, array( $this, '_cmp_priority' ) );
1482                         $panels[ $panel->id ] = $panel;
1483                 }
1484                 $this->panels = $panels;
1485
1486                 // Sort panels and top-level sections together.
1487                 $this->containers = array_merge( $this->panels, $this->sections );
1488                 uasort( $this->containers, array( $this, '_cmp_priority' ) );
1489         }
1490
1491         /**
1492          * Enqueue scripts for customize controls.
1493          *
1494          * @since 3.4.0
1495          */
1496         public function enqueue_control_scripts() {
1497                 foreach ( $this->controls as $control ) {
1498                         $control->enqueue();
1499                 }
1500         }
1501
1502         /**
1503          * Determine whether the user agent is iOS.
1504          *
1505          * @since 4.4.0
1506          * @access public
1507          *
1508          * @return bool Whether the user agent is iOS.
1509          */
1510         public function is_ios() {
1511                 return wp_is_mobile() && preg_match( '/iPad|iPod|iPhone/', $_SERVER['HTTP_USER_AGENT'] );
1512         }
1513
1514         /**
1515          * Get the template string for the Customizer pane document title.
1516          *
1517          * @since 4.4.0
1518          * @access public
1519          *
1520          * @return string The template string for the document title.
1521          */
1522         public function get_document_title_template() {
1523                 if ( $this->is_theme_active() ) {
1524                         /* translators: %s: document title from the preview */
1525                         $document_title_tmpl = __( 'Customize: %s' );
1526                 } else {
1527                         /* translators: %s: document title from the preview */
1528                         $document_title_tmpl = __( 'Live Preview: %s' );
1529                 }
1530                 $document_title_tmpl = html_entity_decode( $document_title_tmpl, ENT_QUOTES, 'UTF-8' ); // Because exported to JS and assigned to document.title.
1531                 return $document_title_tmpl;
1532         }
1533
1534         /**
1535          * Set the initial URL to be previewed.
1536          *
1537          * URL is validated.
1538          *
1539          * @since 4.4.0
1540          * @access public
1541          *
1542          * @param string $preview_url URL to be previewed.
1543          */
1544         public function set_preview_url( $preview_url ) {
1545                 $this->preview_url = wp_validate_redirect( $preview_url, home_url( '/' ) );
1546         }
1547
1548         /**
1549          * Get the initial URL to be previewed.
1550          *
1551          * @since 4.4.0
1552          * @access public
1553          *
1554          * @return string URL being previewed.
1555          */
1556         public function get_preview_url() {
1557                 if ( empty( $this->preview_url ) ) {
1558                         $preview_url = home_url( '/' );
1559                 } else {
1560                         $preview_url = $this->preview_url;
1561                 }
1562                 return $preview_url;
1563         }
1564
1565         /**
1566          * Set URL to link the user to when closing the Customizer.
1567          *
1568          * URL is validated.
1569          *
1570          * @since 4.4.0
1571          * @access public
1572          *
1573          * @param string $return_url URL for return link.
1574          */
1575         public function set_return_url( $return_url ) {
1576                 $return_url = remove_query_arg( wp_removable_query_args(), $return_url );
1577                 $return_url = wp_validate_redirect( $return_url );
1578                 $this->return_url = $return_url;
1579         }
1580
1581         /**
1582          * Get URL to link the user to when closing the Customizer.
1583          *
1584          * @since 4.4.0
1585          * @access public
1586          *
1587          * @return string URL for link to close Customizer.
1588          */
1589         public function get_return_url() {
1590                 $referer = wp_get_referer();
1591                 $excluded_referer_basenames = array( 'customize.php', 'wp-login.php' );
1592
1593                 if ( $this->return_url ) {
1594                         $return_url = $this->return_url;
1595                 } else if ( $referer && ! in_array( basename( parse_url( $referer, PHP_URL_PATH ) ), $excluded_referer_basenames, true ) ) {
1596                         $return_url = $referer;
1597                 } else if ( $this->preview_url ) {
1598                         $return_url = $this->preview_url;
1599                 } else {
1600                         $return_url = home_url( '/' );
1601                 }
1602                 return $return_url;
1603         }
1604
1605         /**
1606          * Set the autofocused constructs.
1607          *
1608          * @since 4.4.0
1609          * @access public
1610          *
1611          * @param array $autofocus {
1612          *     Mapping of 'panel', 'section', 'control' to the ID which should be autofocused.
1613          *
1614          *     @type string [$control]  ID for control to be autofocused.
1615          *     @type string [$section]  ID for section to be autofocused.
1616          *     @type string [$panel]    ID for panel to be autofocused.
1617          * }
1618          */
1619         public function set_autofocus( $autofocus ) {
1620                 $this->autofocus = array_filter( wp_array_slice_assoc( $autofocus, array( 'panel', 'section', 'control' ) ), 'is_string' );
1621         }
1622
1623         /**
1624          * Get the autofocused constructs.
1625          *
1626          * @since 4.4.0
1627          * @access public
1628          *
1629          * @return array {
1630          *     Mapping of 'panel', 'section', 'control' to the ID which should be autofocused.
1631          *
1632          *     @type string [$control]  ID for control to be autofocused.
1633          *     @type string [$section]  ID for section to be autofocused.
1634          *     @type string [$panel]    ID for panel to be autofocused.
1635          * }
1636          */
1637         public function get_autofocus() {
1638                 return $this->autofocus;
1639         }
1640
1641         /**
1642          * Get nonces for the Customizer.
1643          *
1644          * @since 4.5.0
1645          * @return array Nonces.
1646          */
1647         public function get_nonces() {
1648                 $nonces = array(
1649                         'save' => wp_create_nonce( 'save-customize_' . $this->get_stylesheet() ),
1650                         'preview' => wp_create_nonce( 'preview-customize_' . $this->get_stylesheet() ),
1651                 );
1652
1653                 /**
1654                  * Filter nonces for Customizer.
1655                  *
1656                  * @since 4.2.0
1657                  *
1658                  * @param array                $nonces Array of refreshed nonces for save and
1659                  *                                     preview actions.
1660                  * @param WP_Customize_Manager $this   WP_Customize_Manager instance.
1661                  */
1662                 $nonces = apply_filters( 'customize_refresh_nonces', $nonces, $this );
1663
1664                 return $nonces;
1665         }
1666
1667         /**
1668          * Print JavaScript settings for parent window.
1669          *
1670          * @since 4.4.0
1671          */
1672         public function customize_pane_settings() {
1673                 /*
1674                  * If the front end and the admin are served from the same domain, load the
1675                  * preview over ssl if the Customizer is being loaded over ssl. This avoids
1676                  * insecure content warnings. This is not attempted if the admin and front end
1677                  * are on different domains to avoid the case where the front end doesn't have
1678                  * ssl certs. Domain mapping plugins can allow other urls in these conditions
1679                  * using the customize_allowed_urls filter.
1680                  */
1681
1682                 $allowed_urls = array( home_url( '/' ) );
1683                 $admin_origin = parse_url( admin_url() );
1684                 $home_origin  = parse_url( home_url() );
1685                 $cross_domain = ( strtolower( $admin_origin['host'] ) !== strtolower( $home_origin['host'] ) );
1686
1687                 if ( is_ssl() && ! $cross_domain ) {
1688                         $allowed_urls[] = home_url( '/', 'https' );
1689                 }
1690
1691                 /**
1692                  * Filter the list of URLs allowed to be clicked and followed in the Customizer preview.
1693                  *
1694                  * @since 3.4.0
1695                  *
1696                  * @param array $allowed_urls An array of allowed URLs.
1697                  */
1698                 $allowed_urls = array_unique( apply_filters( 'customize_allowed_urls', $allowed_urls ) );
1699
1700                 $login_url = add_query_arg( array(
1701                         'interim-login' => 1,
1702                         'customize-login' => 1,
1703                 ), wp_login_url() );
1704
1705                 // Prepare Customizer settings to pass to JavaScript.
1706                 $settings = array(
1707                         'theme'    => array(
1708                                 'stylesheet' => $this->get_stylesheet(),
1709                                 'active'     => $this->is_theme_active(),
1710                         ),
1711                         'url'      => array(
1712                                 'preview'       => esc_url_raw( $this->get_preview_url() ),
1713                                 'parent'        => esc_url_raw( admin_url() ),
1714                                 'activated'     => esc_url_raw( home_url( '/' ) ),
1715                                 'ajax'          => esc_url_raw( admin_url( 'admin-ajax.php', 'relative' ) ),
1716                                 'allowed'       => array_map( 'esc_url_raw', $allowed_urls ),
1717                                 'isCrossDomain' => $cross_domain,
1718                                 'home'          => esc_url_raw( home_url( '/' ) ),
1719                                 'login'         => esc_url_raw( $login_url ),
1720                         ),
1721                         'browser'  => array(
1722                                 'mobile' => wp_is_mobile(),
1723                                 'ios'    => $this->is_ios(),
1724                         ),
1725                         'panels'   => array(),
1726                         'sections' => array(),
1727                         'nonce'    => $this->get_nonces(),
1728                         'autofocus' => $this->get_autofocus(),
1729                         'documentTitleTmpl' => $this->get_document_title_template(),
1730                         'previewableDevices' => $this->get_previewable_devices(),
1731                 );
1732
1733                 // Prepare Customize Section objects to pass to JavaScript.
1734                 foreach ( $this->sections() as $id => $section ) {
1735                         if ( $section->check_capabilities() ) {
1736                                 $settings['sections'][ $id ] = $section->json();
1737                         }
1738                 }
1739
1740                 // Prepare Customize Panel objects to pass to JavaScript.
1741                 foreach ( $this->panels() as $panel_id => $panel ) {
1742                         if ( $panel->check_capabilities() ) {
1743                                 $settings['panels'][ $panel_id ] = $panel->json();
1744                                 foreach ( $panel->sections as $section_id => $section ) {
1745                                         if ( $section->check_capabilities() ) {
1746                                                 $settings['sections'][ $section_id ] = $section->json();
1747                                         }
1748                                 }
1749                         }
1750                 }
1751
1752                 ?>
1753                 <script type="text/javascript">
1754                         var _wpCustomizeSettings = <?php echo wp_json_encode( $settings ); ?>;
1755                         _wpCustomizeSettings.controls = {};
1756                         _wpCustomizeSettings.settings = {};
1757                         <?php
1758
1759                         // Serialize settings one by one to improve memory usage.
1760                         echo "(function ( s ){\n";
1761                         foreach ( $this->settings() as $setting ) {
1762                                 if ( $setting->check_capabilities() ) {
1763                                         printf(
1764                                                 "s[%s] = %s;\n",
1765                                                 wp_json_encode( $setting->id ),
1766                                                 wp_json_encode( array(
1767                                                         'value'     => $setting->js_value(),
1768                                                         'transport' => $setting->transport,
1769                                                         'dirty'     => $setting->dirty,
1770                                                 ) )
1771                                         );
1772                                 }
1773                         }
1774                         echo "})( _wpCustomizeSettings.settings );\n";
1775
1776                         // Serialize controls one by one to improve memory usage.
1777                         echo "(function ( c ){\n";
1778                         foreach ( $this->controls() as $control ) {
1779                                 if ( $control->check_capabilities() ) {
1780                                         printf(
1781                                                 "c[%s] = %s;\n",
1782                                                 wp_json_encode( $control->id ),
1783                                                 wp_json_encode( $control->json() )
1784                                         );
1785                                 }
1786                         }
1787                         echo "})( _wpCustomizeSettings.controls );\n";
1788                 ?>
1789                 </script>
1790                 <?php
1791         }
1792
1793         /**
1794          * Returns a list of devices to allow previewing.
1795          *
1796          * @access public
1797          * @since 4.5.0
1798          *
1799          * @return array List of devices with labels and default setting.
1800          */
1801         public function get_previewable_devices() {
1802                 $devices = array(
1803                         'desktop' => array(
1804                                 'label' => __( 'Enter desktop preview mode' ),
1805                                 'default' => true,
1806                         ),
1807                         'tablet' => array(
1808                                 'label' => __( 'Enter tablet preview mode' ),
1809                         ),
1810                         'mobile' => array(
1811                                 'label' => __( 'Enter mobile preview mode' ),
1812                         ),
1813                 );
1814
1815                 /**
1816                  * Filter the available devices to allow previewing in the Customizer.
1817                  *
1818                  * @since 4.5.0
1819                  *
1820                  * @see WP_Customize_Manager::get_previewable_devices()
1821                  *
1822                  * @param array $devices List of devices with labels and default setting.
1823                  */
1824                 $devices = apply_filters( 'customize_previewable_devices', $devices );
1825
1826                 return $devices;
1827         }
1828
1829         /**
1830          * Register some default controls.
1831          *
1832          * @since 3.4.0
1833          */
1834         public function register_controls() {
1835
1836                 /* Panel, Section, and Control Types */
1837                 $this->register_panel_type( 'WP_Customize_Panel' );
1838                 $this->register_section_type( 'WP_Customize_Section' );
1839                 $this->register_section_type( 'WP_Customize_Sidebar_Section' );
1840                 $this->register_control_type( 'WP_Customize_Color_Control' );
1841                 $this->register_control_type( 'WP_Customize_Media_Control' );
1842                 $this->register_control_type( 'WP_Customize_Upload_Control' );
1843                 $this->register_control_type( 'WP_Customize_Image_Control' );
1844                 $this->register_control_type( 'WP_Customize_Background_Image_Control' );
1845                 $this->register_control_type( 'WP_Customize_Cropped_Image_Control' );
1846                 $this->register_control_type( 'WP_Customize_Site_Icon_Control' );
1847                 $this->register_control_type( 'WP_Customize_Theme_Control' );
1848
1849                 /* Themes */
1850
1851                 $this->add_section( new WP_Customize_Themes_Section( $this, 'themes', array(
1852                         'title'      => $this->theme()->display( 'Name' ),
1853                         'capability' => 'switch_themes',
1854                         'priority'   => 0,
1855                 ) ) );
1856
1857                 // Themes Setting (unused - the theme is considerably more fundamental to the Customizer experience).
1858                 $this->add_setting( new WP_Customize_Filter_Setting( $this, 'active_theme', array(
1859                         'capability' => 'switch_themes',
1860                 ) ) );
1861
1862                 require_once( ABSPATH . 'wp-admin/includes/theme.php' );
1863
1864                 // Theme Controls.
1865
1866                 // Add a control for the active/original theme.
1867                 if ( ! $this->is_theme_active() ) {
1868                         $themes = wp_prepare_themes_for_js( array( wp_get_theme( $this->original_stylesheet ) ) );
1869                         $active_theme = current( $themes );
1870                         $active_theme['isActiveTheme'] = true;
1871                         $this->add_control( new WP_Customize_Theme_Control( $this, $active_theme['id'], array(
1872                                 'theme'    => $active_theme,
1873                                 'section'  => 'themes',
1874                                 'settings' => 'active_theme',
1875                         ) ) );
1876                 }
1877
1878                 $themes = wp_prepare_themes_for_js();
1879                 foreach ( $themes as $theme ) {
1880                         if ( $theme['active'] || $theme['id'] === $this->original_stylesheet ) {
1881                                 continue;
1882                         }
1883
1884                         $theme_id = 'theme_' . $theme['id'];
1885                         $theme['isActiveTheme'] = false;
1886                         $this->add_control( new WP_Customize_Theme_Control( $this, $theme_id, array(
1887                                 'theme'    => $theme,
1888                                 'section'  => 'themes',
1889                                 'settings' => 'active_theme',
1890                         ) ) );
1891                 }
1892
1893                 /* Site Identity */
1894
1895                 $this->add_section( 'title_tagline', array(
1896                         'title'    => __( 'Site Identity' ),
1897                         'priority' => 20,
1898                 ) );
1899
1900                 $this->add_setting( 'blogname', array(
1901                         'default'    => get_option( 'blogname' ),
1902                         'type'       => 'option',
1903                         'capability' => 'manage_options',
1904                 ) );
1905
1906                 $this->add_control( 'blogname', array(
1907                         'label'      => __( 'Site Title' ),
1908                         'section'    => 'title_tagline',
1909                 ) );
1910
1911                 $this->add_setting( 'blogdescription', array(
1912                         'default'    => get_option( 'blogdescription' ),
1913                         'type'       => 'option',
1914                         'capability' => 'manage_options',
1915                 ) );
1916
1917                 $this->add_control( 'blogdescription', array(
1918                         'label'      => __( 'Tagline' ),
1919                         'section'    => 'title_tagline',
1920                 ) );
1921
1922                 // Add a setting to hide header text if the theme doesn't support custom headers.
1923                 if ( ! current_theme_supports( 'custom-header', 'header-text' ) ) {
1924                         $this->add_setting( 'header_text', array(
1925                                 'theme_supports'    => array( 'custom-logo', 'header-text' ),
1926                                 'default'           => 1,
1927                                 'sanitize_callback' => 'absint',
1928                         ) );
1929
1930                         $this->add_control( 'header_text', array(
1931                                 'label'    => __( 'Display Site Title and Tagline' ),
1932                                 'section'  => 'title_tagline',
1933                                 'settings' => 'header_text',
1934                                 'type'     => 'checkbox',
1935                         ) );
1936                 }
1937
1938                 $this->add_setting( 'site_icon', array(
1939                         'type'       => 'option',
1940                         'capability' => 'manage_options',
1941                         'transport'  => 'postMessage', // Previewed with JS in the Customizer controls window.
1942                 ) );
1943
1944                 $this->add_control( new WP_Customize_Site_Icon_Control( $this, 'site_icon', array(
1945                         'label'       => __( 'Site Icon' ),
1946                         'description' => sprintf(
1947                                 /* translators: %s: site icon size in pixels */
1948                                 __( 'The Site Icon is used as a browser and app icon for your site. Icons must be square, and at least %s pixels wide and tall.' ),
1949                                 '<strong>512</strong>'
1950                         ),
1951                         'section'     => 'title_tagline',
1952                         'priority'    => 60,
1953                         'height'      => 512,
1954                         'width'       => 512,
1955                 ) ) );
1956
1957                 $this->add_setting( 'custom_logo', array(
1958                         'theme_supports' => array( 'custom-logo' ),
1959                         'transport'      => 'postMessage',
1960                 ) );
1961
1962                 $custom_logo_args = get_theme_support( 'custom-logo' );
1963                 $this->add_control( new WP_Customize_Cropped_Image_Control( $this, 'custom_logo', array(
1964                         'label'         => __( 'Logo' ),
1965                         'section'       => 'title_tagline',
1966                         'priority'      => 8,
1967                         'height'        => $custom_logo_args[0]['height'],
1968                         'width'         => $custom_logo_args[0]['width'],
1969                         'flex_height'   => $custom_logo_args[0]['flex-height'],
1970                         'flex_width'    => $custom_logo_args[0]['flex-width'],
1971                         'button_labels' => array(
1972                                 'select'       => __( 'Select logo' ),
1973                                 'change'       => __( 'Change logo' ),
1974                                 'remove'       => __( 'Remove' ),
1975                                 'default'      => __( 'Default' ),
1976                                 'placeholder'  => __( 'No logo selected' ),
1977                                 'frame_title'  => __( 'Select logo' ),
1978                                 'frame_button' => __( 'Choose logo' ),
1979                         ),
1980                 ) ) );
1981
1982                 $this->selective_refresh->add_partial( 'custom_logo', array(
1983                         'settings'            => array( 'custom_logo' ),
1984                         'selector'            => '.custom-logo-link',
1985                         'render_callback'     => array( $this, '_render_custom_logo_partial' ),
1986                         'container_inclusive' => true,
1987                 ) );
1988
1989                 /* Colors */
1990
1991                 $this->add_section( 'colors', array(
1992                         'title'          => __( 'Colors' ),
1993                         'priority'       => 40,
1994                 ) );
1995
1996                 $this->add_setting( 'header_textcolor', array(
1997                         'theme_supports' => array( 'custom-header', 'header-text' ),
1998                         'default'        => get_theme_support( 'custom-header', 'default-text-color' ),
1999
2000                         'sanitize_callback'    => array( $this, '_sanitize_header_textcolor' ),
2001                         'sanitize_js_callback' => 'maybe_hash_hex_color',
2002                 ) );
2003
2004                 // Input type: checkbox
2005                 // With custom value
2006                 $this->add_control( 'display_header_text', array(
2007                         'settings' => 'header_textcolor',
2008                         'label'    => __( 'Display Site Title and Tagline' ),
2009                         'section'  => 'title_tagline',
2010                         'type'     => 'checkbox',
2011                         'priority' => 40,
2012                 ) );
2013
2014                 $this->add_control( new WP_Customize_Color_Control( $this, 'header_textcolor', array(
2015                         'label'   => __( 'Header Text Color' ),
2016                         'section' => 'colors',
2017                 ) ) );
2018
2019                 // Input type: Color
2020                 // With sanitize_callback
2021                 $this->add_setting( 'background_color', array(
2022                         'default'        => get_theme_support( 'custom-background', 'default-color' ),
2023                         'theme_supports' => 'custom-background',
2024
2025                         'sanitize_callback'    => 'sanitize_hex_color_no_hash',
2026                         'sanitize_js_callback' => 'maybe_hash_hex_color',
2027                 ) );
2028
2029                 $this->add_control( new WP_Customize_Color_Control( $this, 'background_color', array(
2030                         'label'   => __( 'Background Color' ),
2031                         'section' => 'colors',
2032                 ) ) );
2033
2034
2035                 /* Custom Header */
2036
2037                 $this->add_section( 'header_image', array(
2038                         'title'          => __( 'Header Image' ),
2039                         'theme_supports' => 'custom-header',
2040                         'priority'       => 60,
2041                 ) );
2042
2043                 $this->add_setting( new WP_Customize_Filter_Setting( $this, 'header_image', array(
2044                         'default'        => get_theme_support( 'custom-header', 'default-image' ),
2045                         'theme_supports' => 'custom-header',
2046                 ) ) );
2047
2048                 $this->add_setting( new WP_Customize_Header_Image_Setting( $this, 'header_image_data', array(
2049                         // 'default'        => get_theme_support( 'custom-header', 'default-image' ),
2050                         'theme_supports' => 'custom-header',
2051                 ) ) );
2052
2053                 $this->add_control( new WP_Customize_Header_Image_Control( $this ) );
2054
2055                 /* Custom Background */
2056
2057                 $this->add_section( 'background_image', array(
2058                         'title'          => __( 'Background Image' ),
2059                         'theme_supports' => 'custom-background',
2060                         'priority'       => 80,
2061                 ) );
2062
2063                 $this->add_setting( 'background_image', array(
2064                         'default'        => get_theme_support( 'custom-background', 'default-image' ),
2065                         'theme_supports' => 'custom-background',
2066                 ) );
2067
2068                 $this->add_setting( new WP_Customize_Background_Image_Setting( $this, 'background_image_thumb', array(
2069                         'theme_supports' => 'custom-background',
2070                 ) ) );
2071
2072                 $this->add_control( new WP_Customize_Background_Image_Control( $this ) );
2073
2074                 $this->add_setting( 'background_repeat', array(
2075                         'default'        => get_theme_support( 'custom-background', 'default-repeat' ),
2076                         'theme_supports' => 'custom-background',
2077                 ) );
2078
2079                 $this->add_control( 'background_repeat', array(
2080                         'label'      => __( 'Background Repeat' ),
2081                         'section'    => 'background_image',
2082                         'type'       => 'radio',
2083                         'choices'    => array(
2084                                 'no-repeat'  => __('No Repeat'),
2085                                 'repeat'     => __('Tile'),
2086                                 'repeat-x'   => __('Tile Horizontally'),
2087                                 'repeat-y'   => __('Tile Vertically'),
2088                         ),
2089                 ) );
2090
2091                 $this->add_setting( 'background_position_x', array(
2092                         'default'        => get_theme_support( 'custom-background', 'default-position-x' ),
2093                         'theme_supports' => 'custom-background',
2094                 ) );
2095
2096                 $this->add_control( 'background_position_x', array(
2097                         'label'      => __( 'Background Position' ),
2098                         'section'    => 'background_image',
2099                         'type'       => 'radio',
2100                         'choices'    => array(
2101                                 'left'       => __('Left'),
2102                                 'center'     => __('Center'),
2103                                 'right'      => __('Right'),
2104                         ),
2105                 ) );
2106
2107                 $this->add_setting( 'background_attachment', array(
2108                         'default'        => get_theme_support( 'custom-background', 'default-attachment' ),
2109                         'theme_supports' => 'custom-background',
2110                 ) );
2111
2112                 $this->add_control( 'background_attachment', array(
2113                         'label'      => __( 'Background Attachment' ),
2114                         'section'    => 'background_image',
2115                         'type'       => 'radio',
2116                         'choices'    => array(
2117                                 'scroll'     => __('Scroll'),
2118                                 'fixed'      => __('Fixed'),
2119                         ),
2120                 ) );
2121
2122                 // If the theme is using the default background callback, we can update
2123                 // the background CSS using postMessage.
2124                 if ( get_theme_support( 'custom-background', 'wp-head-callback' ) === '_custom_background_cb' ) {
2125                         foreach ( array( 'color', 'image', 'position_x', 'repeat', 'attachment' ) as $prop ) {
2126                                 $this->get_setting( 'background_' . $prop )->transport = 'postMessage';
2127                         }
2128                 }
2129
2130                 /* Static Front Page */
2131                 // #WP19627
2132
2133                 // Replicate behavior from options-reading.php and hide front page options if there are no pages
2134                 if ( get_pages() ) {
2135                         $this->add_section( 'static_front_page', array(
2136                                 'title'          => __( 'Static Front Page' ),
2137                         //      'theme_supports' => 'static-front-page',
2138                                 'priority'       => 120,
2139                                 'description'    => __( 'Your theme supports a static front page.' ),
2140                         ) );
2141
2142                         $this->add_setting( 'show_on_front', array(
2143                                 'default'        => get_option( 'show_on_front' ),
2144                                 'capability'     => 'manage_options',
2145                                 'type'           => 'option',
2146                         //      'theme_supports' => 'static-front-page',
2147                         ) );
2148
2149                         $this->add_control( 'show_on_front', array(
2150                                 'label'   => __( 'Front page displays' ),
2151                                 'section' => 'static_front_page',
2152                                 'type'    => 'radio',
2153                                 'choices' => array(
2154                                         'posts' => __( 'Your latest posts' ),
2155                                         'page'  => __( 'A static page' ),
2156                                 ),
2157                         ) );
2158
2159                         $this->add_setting( 'page_on_front', array(
2160                                 'type'       => 'option',
2161                                 'capability' => 'manage_options',
2162                         //      'theme_supports' => 'static-front-page',
2163                         ) );
2164
2165                         $this->add_control( 'page_on_front', array(
2166                                 'label'      => __( 'Front page' ),
2167                                 'section'    => 'static_front_page',
2168                                 'type'       => 'dropdown-pages',
2169                         ) );
2170
2171                         $this->add_setting( 'page_for_posts', array(
2172                                 'type'           => 'option',
2173                                 'capability'     => 'manage_options',
2174                         //      'theme_supports' => 'static-front-page',
2175                         ) );
2176
2177                         $this->add_control( 'page_for_posts', array(
2178                                 'label'      => __( 'Posts page' ),
2179                                 'section'    => 'static_front_page',
2180                                 'type'       => 'dropdown-pages',
2181                         ) );
2182                 }
2183         }
2184
2185         /**
2186          * Add settings from the POST data that were not added with code, e.g. dynamically-created settings for Widgets
2187          *
2188          * @since 4.2.0
2189          * @access public
2190          *
2191          * @see add_dynamic_settings()
2192          */
2193         public function register_dynamic_settings() {
2194                 $this->add_dynamic_settings( array_keys( $this->unsanitized_post_values() ) );
2195         }
2196
2197         /**
2198          * Callback for validating the header_textcolor value.
2199          *
2200          * Accepts 'blank', and otherwise uses sanitize_hex_color_no_hash().
2201          * Returns default text color if hex color is empty.
2202          *
2203          * @since 3.4.0
2204          *
2205          * @param string $color
2206          * @return mixed
2207          */
2208         public function _sanitize_header_textcolor( $color ) {
2209                 if ( 'blank' === $color )
2210                         return 'blank';
2211
2212                 $color = sanitize_hex_color_no_hash( $color );
2213                 if ( empty( $color ) )
2214                         $color = get_theme_support( 'custom-header', 'default-text-color' );
2215
2216                 return $color;
2217         }
2218
2219         /**
2220          * Callback for rendering the custom logo, used in the custom_logo partial.
2221          *
2222          * This method exists because the partial object and context data are passed
2223          * into a partial's render_callback so we cannot use get_custom_logo() as
2224          * the render_callback directly since it expects a blog ID as the first
2225          * argument. When WP no longer supports PHP 5.3, this method can be removed
2226          * in favor of an anonymous function.
2227          *
2228          * @see WP_Customize_Manager::register_controls()
2229          *
2230          * @since 4.5.0
2231          * @access private
2232          *
2233          * @return string Custom logo.
2234          */
2235         public function _render_custom_logo_partial() {
2236                 return get_custom_logo();
2237         }
2238 }
2239
2240 /**
2241  * Sanitizes a hex color.
2242  *
2243  * Returns either '', a 3 or 6 digit hex color (with #), or nothing.
2244  * For sanitizing values without a #, see sanitize_hex_color_no_hash().
2245  *
2246  * @since 3.4.0
2247  *
2248  * @param string $color
2249  * @return string|void
2250  */
2251 function sanitize_hex_color( $color ) {
2252         if ( '' === $color )
2253                 return '';
2254
2255         // 3 or 6 hex digits, or the empty string.
2256         if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) )
2257                 return $color;
2258 }
2259
2260 /**
2261  * Sanitizes a hex color without a hash. Use sanitize_hex_color() when possible.
2262  *
2263  * Saving hex colors without a hash puts the burden of adding the hash on the
2264  * UI, which makes it difficult to use or upgrade to other color types such as
2265  * rgba, hsl, rgb, and html color names.
2266  *
2267  * Returns either '', a 3 or 6 digit hex color (without a #), or null.
2268  *
2269  * @since 3.4.0
2270  *
2271  * @param string $color
2272  * @return string|null
2273  */
2274 function sanitize_hex_color_no_hash( $color ) {
2275         $color = ltrim( $color, '#' );
2276
2277         if ( '' === $color )
2278                 return '';
2279
2280         return sanitize_hex_color( '#' . $color ) ? $color : null;
2281 }
2282
2283 /**
2284  * Ensures that any hex color is properly hashed.
2285  * Otherwise, returns value untouched.
2286  *
2287  * This method should only be necessary if using sanitize_hex_color_no_hash().
2288  *
2289  * @since 3.4.0
2290  *
2291  * @param string $color
2292  * @return string
2293  */
2294 function maybe_hash_hex_color( $color ) {
2295         if ( $unhashed = sanitize_hex_color_no_hash( $color ) )
2296                 return '#' . $unhashed;
2297
2298         return $color;
2299 }