]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-wp-customize-manager.php
WordPress 4.6.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                  * Filters 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, __( 'Sorry, you are not allowed to customize 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, __( 'Sorry, 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( $_POST['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          * Returns 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 the `$default` parameter.
658          * @since 4.6.0 `$default` is now returned early when the setting post value is invalid.
659          * @access public
660          *
661          * @see WP_REST_Server::dispatch()
662          * @see WP_Rest_Request::sanitize_params()
663          * @see WP_Rest_Request::has_valid_params()
664          *
665          * @param WP_Customize_Setting $setting A WP_Customize_Setting derived object.
666          * @param mixed                $default Value returned $setting has no post value (added in 4.2.0)
667          *                                      or the post value is invalid (added in 4.6.0).
668          * @return string|mixed $post_value Sanitized value or the $default provided.
669          */
670         public function post_value( $setting, $default = null ) {
671                 $post_values = $this->unsanitized_post_values();
672                 if ( ! array_key_exists( $setting->id, $post_values ) ) {
673                         return $default;
674                 }
675                 $value = $post_values[ $setting->id ];
676                 $valid = $setting->validate( $value );
677                 if ( is_wp_error( $valid ) ) {
678                         return $default;
679                 }
680                 $value = $setting->sanitize( $value );
681                 if ( is_null( $value ) || is_wp_error( $value ) ) {
682                         return $default;
683                 }
684                 return $value;
685         }
686
687         /**
688          * Override a setting's (unsanitized) value as found in any incoming $_POST['customized'].
689          *
690          * @since 4.2.0
691          * @access public
692          *
693          * @param string $setting_id ID for the WP_Customize_Setting instance.
694          * @param mixed  $value      Post value.
695          */
696         public function set_post_value( $setting_id, $value ) {
697                 $this->unsanitized_post_values();
698                 $this->_post_values[ $setting_id ] = $value;
699
700                 /**
701                  * Announce when a specific setting's unsanitized post value has been set.
702                  *
703                  * Fires when the WP_Customize_Manager::set_post_value() method is called.
704                  *
705                  * The dynamic portion of the hook name, `$setting_id`, refers to the setting ID.
706                  *
707                  * @since 4.4.0
708                  *
709                  * @param mixed                $value Unsanitized setting post value.
710                  * @param WP_Customize_Manager $this  WP_Customize_Manager instance.
711                  */
712                 do_action( "customize_post_value_set_{$setting_id}", $value, $this );
713
714                 /**
715                  * Announce when any setting's unsanitized post value has been set.
716                  *
717                  * Fires when the WP_Customize_Manager::set_post_value() method is called.
718                  *
719                  * This is useful for `WP_Customize_Setting` instances to watch
720                  * in order to update a cached previewed value.
721                  *
722                  * @since 4.4.0
723                  *
724                  * @param string               $setting_id Setting ID.
725                  * @param mixed                $value      Unsanitized setting post value.
726                  * @param WP_Customize_Manager $this       WP_Customize_Manager instance.
727                  */
728                 do_action( 'customize_post_value_set', $setting_id, $value, $this );
729         }
730
731         /**
732          * Print JavaScript settings.
733          *
734          * @since 3.4.0
735          */
736         public function customize_preview_init() {
737                 $this->nonce_tick = check_ajax_referer( 'preview-customize_' . $this->get_stylesheet(), 'nonce' );
738
739                 $this->prepare_controls();
740
741                 wp_enqueue_script( 'customize-preview' );
742                 add_action( 'wp', array( $this, 'customize_preview_override_404_status' ) );
743                 add_action( 'wp_head', array( $this, 'customize_preview_base' ) );
744                 add_action( 'wp_head', array( $this, 'customize_preview_html5' ) );
745                 add_action( 'wp_head', array( $this, 'customize_preview_loading_style' ) );
746                 add_action( 'wp_footer', array( $this, 'customize_preview_settings' ), 20 );
747                 add_action( 'shutdown', array( $this, 'customize_preview_signature' ), 1000 );
748                 add_filter( 'wp_die_handler', array( $this, 'remove_preview_signature' ) );
749
750                 foreach ( $this->settings as $setting ) {
751                         $setting->preview();
752                 }
753
754                 /**
755                  * Fires once the Customizer preview has initialized and JavaScript
756                  * settings have been printed.
757                  *
758                  * @since 3.4.0
759                  *
760                  * @param WP_Customize_Manager $this WP_Customize_Manager instance.
761                  */
762                 do_action( 'customize_preview_init', $this );
763         }
764
765         /**
766          * Prevent sending a 404 status when returning the response for the customize
767          * preview, since it causes the jQuery Ajax to fail. Send 200 instead.
768          *
769          * @since 4.0.0
770          * @access public
771          */
772         public function customize_preview_override_404_status() {
773                 if ( is_404() ) {
774                         status_header( 200 );
775                 }
776         }
777
778         /**
779          * Print base element for preview frame.
780          *
781          * @since 3.4.0
782          */
783         public function customize_preview_base() {
784                 ?><base href="<?php echo home_url( '/' ); ?>" /><?php
785         }
786
787         /**
788          * Print a workaround to handle HTML5 tags in IE < 9.
789          *
790          * @since 3.4.0
791          */
792         public function customize_preview_html5() { ?>
793                 <!--[if lt IE 9]>
794                 <script type="text/javascript">
795                         var e = [ 'abbr', 'article', 'aside', 'audio', 'canvas', 'datalist', 'details',
796                                 'figure', 'footer', 'header', 'hgroup', 'mark', 'menu', 'meter', 'nav',
797                                 'output', 'progress', 'section', 'time', 'video' ];
798                         for ( var i = 0; i < e.length; i++ ) {
799                                 document.createElement( e[i] );
800                         }
801                 </script>
802                 <![endif]--><?php
803         }
804
805         /**
806          * Print CSS for loading indicators for the Customizer preview.
807          *
808          * @since 4.2.0
809          * @access public
810          */
811         public function customize_preview_loading_style() {
812                 ?><style>
813                         body.wp-customizer-unloading {
814                                 opacity: 0.25;
815                                 cursor: progress !important;
816                                 -webkit-transition: opacity 0.5s;
817                                 transition: opacity 0.5s;
818                         }
819                         body.wp-customizer-unloading * {
820                                 pointer-events: none !important;
821                         }
822                 </style><?php
823         }
824
825         /**
826          * Print JavaScript settings for preview frame.
827          *
828          * @since 3.4.0
829          */
830         public function customize_preview_settings() {
831                 $setting_validities = $this->validate_setting_values( $this->unsanitized_post_values() );
832                 $exported_setting_validities = array_map( array( $this, 'prepare_setting_validity_for_js' ), $setting_validities );
833
834                 $settings = array(
835                         'theme' => array(
836                                 'stylesheet' => $this->get_stylesheet(),
837                                 'active'     => $this->is_theme_active(),
838                         ),
839                         'url' => array(
840                                 'self' => empty( $_SERVER['REQUEST_URI'] ) ? home_url( '/' ) : esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ),
841                         ),
842                         'channel' => wp_unslash( $_POST['customize_messenger_channel'] ),
843                         'activePanels' => array(),
844                         'activeSections' => array(),
845                         'activeControls' => array(),
846                         'settingValidities' => $exported_setting_validities,
847                         'nonce' => $this->get_nonces(),
848                         'l10n' => array(
849                                 'shiftClickToEdit' => __( 'Shift-click to edit this element.' ),
850                         ),
851                         '_dirty' => array_keys( $this->unsanitized_post_values() ),
852                 );
853
854                 foreach ( $this->panels as $panel_id => $panel ) {
855                         if ( $panel->check_capabilities() ) {
856                                 $settings['activePanels'][ $panel_id ] = $panel->active();
857                                 foreach ( $panel->sections as $section_id => $section ) {
858                                         if ( $section->check_capabilities() ) {
859                                                 $settings['activeSections'][ $section_id ] = $section->active();
860                                         }
861                                 }
862                         }
863                 }
864                 foreach ( $this->sections as $id => $section ) {
865                         if ( $section->check_capabilities() ) {
866                                 $settings['activeSections'][ $id ] = $section->active();
867                         }
868                 }
869                 foreach ( $this->controls as $id => $control ) {
870                         if ( $control->check_capabilities() ) {
871                                 $settings['activeControls'][ $id ] = $control->active();
872                         }
873                 }
874
875                 ?>
876                 <script type="text/javascript">
877                         var _wpCustomizeSettings = <?php echo wp_json_encode( $settings ); ?>;
878                         _wpCustomizeSettings.values = {};
879                         (function( v ) {
880                                 <?php
881                                 /*
882                                  * Serialize settings separately from the initial _wpCustomizeSettings
883                                  * serialization in order to avoid a peak memory usage spike.
884                                  * @todo We may not even need to export the values at all since the pane syncs them anyway.
885                                  */
886                                 foreach ( $this->settings as $id => $setting ) {
887                                         if ( $setting->check_capabilities() ) {
888                                                 printf(
889                                                         "v[%s] = %s;\n",
890                                                         wp_json_encode( $id ),
891                                                         wp_json_encode( $setting->js_value() )
892                                                 );
893                                         }
894                                 }
895                                 ?>
896                         })( _wpCustomizeSettings.values );
897                 </script>
898                 <?php
899         }
900
901         /**
902          * Prints a signature so we can ensure the Customizer was properly executed.
903          *
904          * @since 3.4.0
905          */
906         public function customize_preview_signature() {
907                 echo 'WP_CUSTOMIZER_SIGNATURE';
908         }
909
910         /**
911          * Removes the signature in case we experience a case where the Customizer was not properly executed.
912          *
913          * @since 3.4.0
914          *
915          * @param mixed $return Value passed through for {@see 'wp_die_handler'} filter.
916          * @return mixed Value passed through for {@see 'wp_die_handler'} filter.
917          */
918         public function remove_preview_signature( $return = null ) {
919                 remove_action( 'shutdown', array( $this, 'customize_preview_signature' ), 1000 );
920
921                 return $return;
922         }
923
924         /**
925          * Is it a theme preview?
926          *
927          * @since 3.4.0
928          *
929          * @return bool True if it's a preview, false if not.
930          */
931         public function is_preview() {
932                 return (bool) $this->previewing;
933         }
934
935         /**
936          * Retrieve the template name of the previewed theme.
937          *
938          * @since 3.4.0
939          *
940          * @return string Template name.
941          */
942         public function get_template() {
943                 return $this->theme()->get_template();
944         }
945
946         /**
947          * Retrieve the stylesheet name of the previewed theme.
948          *
949          * @since 3.4.0
950          *
951          * @return string Stylesheet name.
952          */
953         public function get_stylesheet() {
954                 return $this->theme()->get_stylesheet();
955         }
956
957         /**
958          * Retrieve the template root of the previewed theme.
959          *
960          * @since 3.4.0
961          *
962          * @return string Theme root.
963          */
964         public function get_template_root() {
965                 return get_raw_theme_root( $this->get_template(), true );
966         }
967
968         /**
969          * Retrieve the stylesheet root of the previewed theme.
970          *
971          * @since 3.4.0
972          *
973          * @return string Theme root.
974          */
975         public function get_stylesheet_root() {
976                 return get_raw_theme_root( $this->get_stylesheet(), true );
977         }
978
979         /**
980          * Filters the current theme and return the name of the previewed theme.
981          *
982          * @since 3.4.0
983          *
984          * @param $current_theme {@internal Parameter is not used}
985          * @return string Theme name.
986          */
987         public function current_theme( $current_theme ) {
988                 return $this->theme()->display('Name');
989         }
990
991         /**
992          * Validates setting values.
993          *
994          * Sanitization is applied to the values before being passed for validation.
995          * Validation is skipped for unregistered settings or for values that are
996          * already null since they will be skipped anyway.
997          *
998          * @since 4.6.0
999          * @access public
1000          *
1001          * @see WP_REST_Request::has_valid_params()
1002          * @see WP_Customize_Setting::validate()
1003          *
1004          * @param array $setting_values Mapping of setting IDs to values to sanitize and validate.
1005          * @return array Mapping of setting IDs to return value of validate method calls, either `true` or `WP_Error`.
1006          */
1007         public function validate_setting_values( $setting_values ) {
1008                 $validities = array();
1009                 foreach ( $setting_values as $setting_id => $unsanitized_value ) {
1010                         $setting = $this->get_setting( $setting_id );
1011                         if ( ! $setting || is_null( $unsanitized_value ) ) {
1012                                 continue;
1013                         }
1014                         $validity = $setting->validate( $unsanitized_value );
1015                         if ( ! is_wp_error( $validity ) ) {
1016                                 $value = $setting->sanitize( $unsanitized_value );
1017                                 if ( is_null( $value ) ) {
1018                                         $validity = false;
1019                                 } elseif ( is_wp_error( $value ) ) {
1020                                         $validity = $value;
1021                                 }
1022                         }
1023                         if ( false === $validity ) {
1024                                 $validity = new WP_Error( 'invalid_value', __( 'Invalid value.' ) );
1025                         }
1026                         $validities[ $setting_id ] = $validity;
1027                 }
1028                 return $validities;
1029         }
1030
1031         /**
1032          * Prepares setting validity for exporting to the client (JS).
1033          *
1034          * Converts `WP_Error` instance into array suitable for passing into the
1035          * `wp.customize.Notification` JS model.
1036          *
1037          * @since 4.6.0
1038          * @access public
1039          *
1040          * @param true|WP_Error $validity Setting validity.
1041          * @return true|array If `$validity` was a WP_Error, the error codes will be array-mapped
1042          *                    to their respective `message` and `data` to pass into the
1043          *                    `wp.customize.Notification` JS model.
1044          */
1045         public function prepare_setting_validity_for_js( $validity ) {
1046                 if ( is_wp_error( $validity ) ) {
1047                         $notification = array();
1048                         foreach ( $validity->errors as $error_code => $error_messages ) {
1049                                 $error_data = $validity->get_error_data( $error_code );
1050                                 if ( is_null( $error_data ) ) {
1051                                         $error_data = array();
1052                                 }
1053                                 $error_data = array_merge(
1054                                         $error_data,
1055                                         array( 'from_server' => true )
1056                                 );
1057                                 $notification[ $error_code ] = array(
1058                                         'message' => join( ' ', $error_messages ),
1059                                         'data' => $error_data,
1060                                 );
1061                         }
1062                         return $notification;
1063                 } else {
1064                         return true;
1065                 }
1066         }
1067
1068         /**
1069          * Switch the theme and trigger the save() method on each setting.
1070          *
1071          * @since 3.4.0
1072          */
1073         public function save() {
1074                 if ( ! $this->is_preview() ) {
1075                         wp_send_json_error( 'not_preview' );
1076                 }
1077
1078                 $action = 'save-customize_' . $this->get_stylesheet();
1079                 if ( ! check_ajax_referer( $action, 'nonce', false ) ) {
1080                         wp_send_json_error( 'invalid_nonce' );
1081                 }
1082
1083                 /**
1084                  * Fires before save validation happens.
1085                  *
1086                  * Plugins can add just-in-time {@see 'customize_validate_{$this->ID}'} filters
1087                  * at this point to catch any settings registered after `customize_register`.
1088                  * The dynamic portion of the hook name, `$this->ID` refers to the setting ID.
1089                  *
1090                  * @since 4.6.0
1091                  *
1092                  * @param WP_Customize_Manager $this WP_Customize_Manager instance.
1093                  */
1094                 do_action( 'customize_save_validation_before', $this );
1095
1096                 // Validate settings.
1097                 $setting_validities = $this->validate_setting_values( $this->unsanitized_post_values() );
1098                 $invalid_setting_count = count( array_filter( $setting_validities, 'is_wp_error' ) );
1099                 $exported_setting_validities = array_map( array( $this, 'prepare_setting_validity_for_js' ), $setting_validities );
1100                 if ( $invalid_setting_count > 0 ) {
1101                         $response = array(
1102                                 'setting_validities' => $exported_setting_validities,
1103                                 'message' => sprintf( _n( 'There is %s invalid setting.', 'There are %s invalid settings.', $invalid_setting_count ), number_format_i18n( $invalid_setting_count ) ),
1104                         );
1105
1106                         /** This filter is documented in wp-includes/class-wp-customize-manager.php */
1107                         $response = apply_filters( 'customize_save_response', $response, $this );
1108                         wp_send_json_error( $response );
1109                 }
1110
1111                 // Do we have to switch themes?
1112                 if ( ! $this->is_theme_active() ) {
1113                         // Temporarily stop previewing the theme to allow switch_themes()
1114                         // to operate properly.
1115                         $this->stop_previewing_theme();
1116                         switch_theme( $this->get_stylesheet() );
1117                         update_option( 'theme_switched_via_customizer', true );
1118                         $this->start_previewing_theme();
1119                 }
1120
1121                 /**
1122                  * Fires once the theme has switched in the Customizer, but before settings
1123                  * have been saved.
1124                  *
1125                  * @since 3.4.0
1126                  *
1127                  * @param WP_Customize_Manager $this WP_Customize_Manager instance.
1128                  */
1129                 do_action( 'customize_save', $this );
1130
1131                 foreach ( $this->settings as $setting ) {
1132                         $setting->save();
1133                 }
1134
1135                 /**
1136                  * Fires after Customize settings have been saved.
1137                  *
1138                  * @since 3.6.0
1139                  *
1140                  * @param WP_Customize_Manager $this WP_Customize_Manager instance.
1141                  */
1142                 do_action( 'customize_save_after', $this );
1143
1144                 $data = array(
1145                         'setting_validities' => $exported_setting_validities,
1146                 );
1147
1148                 /**
1149                  * Filters response data for a successful customize_save Ajax request.
1150                  *
1151                  * This filter does not apply if there was a nonce or authentication failure.
1152                  *
1153                  * @since 4.2.0
1154                  *
1155                  * @param array                $data Additional information passed back to the 'saved'
1156                  *                                   event on `wp.customize`.
1157                  * @param WP_Customize_Manager $this WP_Customize_Manager instance.
1158                  */
1159                 $response = apply_filters( 'customize_save_response', $data, $this );
1160                 wp_send_json_success( $response );
1161         }
1162
1163         /**
1164          * Refresh nonces for the current preview.
1165          *
1166          * @since 4.2.0
1167          */
1168         public function refresh_nonces() {
1169                 if ( ! $this->is_preview() ) {
1170                         wp_send_json_error( 'not_preview' );
1171                 }
1172
1173                 wp_send_json_success( $this->get_nonces() );
1174         }
1175
1176         /**
1177          * Add a customize setting.
1178          *
1179          * @since 3.4.0
1180          * @since 4.5.0 Return added WP_Customize_Setting instance.
1181          * @access public
1182          *
1183          * @param WP_Customize_Setting|string $id   Customize Setting object, or ID.
1184          * @param array                       $args Setting arguments; passed to WP_Customize_Setting
1185          *                                          constructor.
1186          * @return WP_Customize_Setting             The instance of the setting that was added.
1187          */
1188         public function add_setting( $id, $args = array() ) {
1189                 if ( $id instanceof WP_Customize_Setting ) {
1190                         $setting = $id;
1191                 } else {
1192                         $class = 'WP_Customize_Setting';
1193
1194                         /** This filter is documented in wp-includes/class-wp-customize-manager.php */
1195                         $args = apply_filters( 'customize_dynamic_setting_args', $args, $id );
1196
1197                         /** This filter is documented in wp-includes/class-wp-customize-manager.php */
1198                         $class = apply_filters( 'customize_dynamic_setting_class', $class, $id, $args );
1199
1200                         $setting = new $class( $this, $id, $args );
1201                 }
1202
1203                 $this->settings[ $setting->id ] = $setting;
1204                 return $setting;
1205         }
1206
1207         /**
1208          * Register any dynamically-created settings, such as those from $_POST['customized']
1209          * that have no corresponding setting created.
1210          *
1211          * This is a mechanism to "wake up" settings that have been dynamically created
1212          * on the front end and have been sent to WordPress in `$_POST['customized']`. When WP
1213          * loads, the dynamically-created settings then will get created and previewed
1214          * even though they are not directly created statically with code.
1215          *
1216          * @since 4.2.0
1217          * @access public
1218          *
1219          * @param array $setting_ids The setting IDs to add.
1220          * @return array The WP_Customize_Setting objects added.
1221          */
1222         public function add_dynamic_settings( $setting_ids ) {
1223                 $new_settings = array();
1224                 foreach ( $setting_ids as $setting_id ) {
1225                         // Skip settings already created
1226                         if ( $this->get_setting( $setting_id ) ) {
1227                                 continue;
1228                         }
1229
1230                         $setting_args = false;
1231                         $setting_class = 'WP_Customize_Setting';
1232
1233                         /**
1234                          * Filters a dynamic setting's constructor args.
1235                          *
1236                          * For a dynamic setting to be registered, this filter must be employed
1237                          * to override the default false value with an array of args to pass to
1238                          * the WP_Customize_Setting constructor.
1239                          *
1240                          * @since 4.2.0
1241                          *
1242                          * @param false|array $setting_args The arguments to the WP_Customize_Setting constructor.
1243                          * @param string      $setting_id   ID for dynamic setting, usually coming from `$_POST['customized']`.
1244                          */
1245                         $setting_args = apply_filters( 'customize_dynamic_setting_args', $setting_args, $setting_id );
1246                         if ( false === $setting_args ) {
1247                                 continue;
1248                         }
1249
1250                         /**
1251                          * Allow non-statically created settings to be constructed with custom WP_Customize_Setting subclass.
1252                          *
1253                          * @since 4.2.0
1254                          *
1255                          * @param string $setting_class WP_Customize_Setting or a subclass.
1256                          * @param string $setting_id    ID for dynamic setting, usually coming from `$_POST['customized']`.
1257                          * @param array  $setting_args  WP_Customize_Setting or a subclass.
1258                          */
1259                         $setting_class = apply_filters( 'customize_dynamic_setting_class', $setting_class, $setting_id, $setting_args );
1260
1261                         $setting = new $setting_class( $this, $setting_id, $setting_args );
1262
1263                         $this->add_setting( $setting );
1264                         $new_settings[] = $setting;
1265                 }
1266                 return $new_settings;
1267         }
1268
1269         /**
1270          * Retrieve a customize setting.
1271          *
1272          * @since 3.4.0
1273          *
1274          * @param string $id Customize Setting ID.
1275          * @return WP_Customize_Setting|void The setting, if set.
1276          */
1277         public function get_setting( $id ) {
1278                 if ( isset( $this->settings[ $id ] ) ) {
1279                         return $this->settings[ $id ];
1280                 }
1281         }
1282
1283         /**
1284          * Remove a customize setting.
1285          *
1286          * @since 3.4.0
1287          *
1288          * @param string $id Customize Setting ID.
1289          */
1290         public function remove_setting( $id ) {
1291                 unset( $this->settings[ $id ] );
1292         }
1293
1294         /**
1295          * Add a customize panel.
1296          *
1297          * @since 4.0.0
1298          * @since 4.5.0 Return added WP_Customize_Panel instance.
1299          * @access public
1300          *
1301          * @param WP_Customize_Panel|string $id   Customize Panel object, or Panel ID.
1302          * @param array                     $args Optional. Panel arguments. Default empty array.
1303          *
1304          * @return WP_Customize_Panel             The instance of the panel that was added.
1305          */
1306         public function add_panel( $id, $args = array() ) {
1307                 if ( $id instanceof WP_Customize_Panel ) {
1308                         $panel = $id;
1309                 } else {
1310                         $panel = new WP_Customize_Panel( $this, $id, $args );
1311                 }
1312
1313                 $this->panels[ $panel->id ] = $panel;
1314                 return $panel;
1315         }
1316
1317         /**
1318          * Retrieve a customize panel.
1319          *
1320          * @since 4.0.0
1321          * @access public
1322          *
1323          * @param string $id Panel ID to get.
1324          * @return WP_Customize_Panel|void Requested panel instance, if set.
1325          */
1326         public function get_panel( $id ) {
1327                 if ( isset( $this->panels[ $id ] ) ) {
1328                         return $this->panels[ $id ];
1329                 }
1330         }
1331
1332         /**
1333          * Remove a customize panel.
1334          *
1335          * @since 4.0.0
1336          * @access public
1337          *
1338          * @param string $id Panel ID to remove.
1339          */
1340         public function remove_panel( $id ) {
1341                 // Removing core components this way is _doing_it_wrong().
1342                 if ( in_array( $id, $this->components, true ) ) {
1343                         /* translators: 1: panel id, 2: link to 'customize_loaded_components' filter reference */
1344                         $message = sprintf( __( 'Removing %1$s manually will cause PHP warnings. Use the %2$s filter instead.' ),
1345                                 $id,
1346                                 '<a href="' . esc_url( 'https://developer.wordpress.org/reference/hooks/customize_loaded_components/' ) . '"><code>customize_loaded_components</code></a>'
1347                         );
1348
1349                         _doing_it_wrong( __METHOD__, $message, '4.5.0' );
1350                 }
1351                 unset( $this->panels[ $id ] );
1352         }
1353
1354         /**
1355          * Register a customize panel type.
1356          *
1357          * Registered types are eligible to be rendered via JS and created dynamically.
1358          *
1359          * @since 4.3.0
1360          * @access public
1361          *
1362          * @see WP_Customize_Panel
1363          *
1364          * @param string $panel Name of a custom panel which is a subclass of WP_Customize_Panel.
1365          */
1366         public function register_panel_type( $panel ) {
1367                 $this->registered_panel_types[] = $panel;
1368         }
1369
1370         /**
1371          * Render JS templates for all registered panel types.
1372          *
1373          * @since 4.3.0
1374          * @access public
1375          */
1376         public function render_panel_templates() {
1377                 foreach ( $this->registered_panel_types as $panel_type ) {
1378                         $panel = new $panel_type( $this, 'temp', array() );
1379                         $panel->print_template();
1380                 }
1381         }
1382
1383         /**
1384          * Add a customize section.
1385          *
1386          * @since 3.4.0
1387          * @since 4.5.0 Return added WP_Customize_Section instance.
1388          * @access public
1389          *
1390          * @param WP_Customize_Section|string $id   Customize Section object, or Section ID.
1391          * @param array                       $args Section arguments.
1392          *
1393          * @return WP_Customize_Section             The instance of the section that was added.
1394          */
1395         public function add_section( $id, $args = array() ) {
1396                 if ( $id instanceof WP_Customize_Section ) {
1397                         $section = $id;
1398                 } else {
1399                         $section = new WP_Customize_Section( $this, $id, $args );
1400                 }
1401
1402                 $this->sections[ $section->id ] = $section;
1403                 return $section;
1404         }
1405
1406         /**
1407          * Retrieve a customize section.
1408          *
1409          * @since 3.4.0
1410          *
1411          * @param string $id Section ID.
1412          * @return WP_Customize_Section|void The section, if set.
1413          */
1414         public function get_section( $id ) {
1415                 if ( isset( $this->sections[ $id ] ) )
1416                         return $this->sections[ $id ];
1417         }
1418
1419         /**
1420          * Remove a customize section.
1421          *
1422          * @since 3.4.0
1423          *
1424          * @param string $id Section ID.
1425          */
1426         public function remove_section( $id ) {
1427                 unset( $this->sections[ $id ] );
1428         }
1429
1430         /**
1431          * Register a customize section type.
1432          *
1433          * Registered types are eligible to be rendered via JS and created dynamically.
1434          *
1435          * @since 4.3.0
1436          * @access public
1437          *
1438          * @see WP_Customize_Section
1439          *
1440          * @param string $section Name of a custom section which is a subclass of WP_Customize_Section.
1441          */
1442         public function register_section_type( $section ) {
1443                 $this->registered_section_types[] = $section;
1444         }
1445
1446         /**
1447          * Render JS templates for all registered section types.
1448          *
1449          * @since 4.3.0
1450          * @access public
1451          */
1452         public function render_section_templates() {
1453                 foreach ( $this->registered_section_types as $section_type ) {
1454                         $section = new $section_type( $this, 'temp', array() );
1455                         $section->print_template();
1456                 }
1457         }
1458
1459         /**
1460          * Add a customize control.
1461          *
1462          * @since 3.4.0
1463          * @since 4.5.0 Return added WP_Customize_Control instance.
1464          * @access public
1465          *
1466          * @param WP_Customize_Control|string $id   Customize Control object, or ID.
1467          * @param array                       $args Control arguments; passed to WP_Customize_Control
1468          *                                          constructor.
1469          * @return WP_Customize_Control             The instance of the control that was added.
1470          */
1471         public function add_control( $id, $args = array() ) {
1472                 if ( $id instanceof WP_Customize_Control ) {
1473                         $control = $id;
1474                 } else {
1475                         $control = new WP_Customize_Control( $this, $id, $args );
1476                 }
1477
1478                 $this->controls[ $control->id ] = $control;
1479                 return $control;
1480         }
1481
1482         /**
1483          * Retrieve a customize control.
1484          *
1485          * @since 3.4.0
1486          *
1487          * @param string $id ID of the control.
1488          * @return WP_Customize_Control|void The control object, if set.
1489          */
1490         public function get_control( $id ) {
1491                 if ( isset( $this->controls[ $id ] ) )
1492                         return $this->controls[ $id ];
1493         }
1494
1495         /**
1496          * Remove a customize control.
1497          *
1498          * @since 3.4.0
1499          *
1500          * @param string $id ID of the control.
1501          */
1502         public function remove_control( $id ) {
1503                 unset( $this->controls[ $id ] );
1504         }
1505
1506         /**
1507          * Register a customize control type.
1508          *
1509          * Registered types are eligible to be rendered via JS and created dynamically.
1510          *
1511          * @since 4.1.0
1512          * @access public
1513          *
1514          * @param string $control Name of a custom control which is a subclass of
1515          *                        WP_Customize_Control.
1516          */
1517         public function register_control_type( $control ) {
1518                 $this->registered_control_types[] = $control;
1519         }
1520
1521         /**
1522          * Render JS templates for all registered control types.
1523          *
1524          * @since 4.1.0
1525          * @access public
1526          */
1527         public function render_control_templates() {
1528                 foreach ( $this->registered_control_types as $control_type ) {
1529                         $control = new $control_type( $this, 'temp', array(
1530                                 'settings' => array(),
1531                         ) );
1532                         $control->print_template();
1533                 }
1534                 ?>
1535                 <script type="text/html" id="tmpl-customize-control-notifications">
1536                         <ul>
1537                                 <# _.each( data.notifications, function( notification ) { #>
1538                                         <li class="notice notice-{{ notification.type || 'info' }} {{ data.altNotice ? 'notice-alt' : '' }}" data-code="{{ notification.code }}" data-type="{{ notification.type }}">{{ notification.message || notification.code }}</li>
1539                                 <# } ); #>
1540                         </ul>
1541                 </script>
1542                 <?php
1543         }
1544
1545         /**
1546          * Helper function to compare two objects by priority, ensuring sort stability via instance_number.
1547          *
1548          * @since 3.4.0
1549          *
1550          * @param WP_Customize_Panel|WP_Customize_Section|WP_Customize_Control $a Object A.
1551          * @param WP_Customize_Panel|WP_Customize_Section|WP_Customize_Control $b Object B.
1552          * @return int
1553          */
1554         protected function _cmp_priority( $a, $b ) {
1555                 if ( $a->priority === $b->priority ) {
1556                         return $a->instance_number - $b->instance_number;
1557                 } else {
1558                         return $a->priority - $b->priority;
1559                 }
1560         }
1561
1562         /**
1563          * Prepare panels, sections, and controls.
1564          *
1565          * For each, check if required related components exist,
1566          * whether the user has the necessary capabilities,
1567          * and sort by priority.
1568          *
1569          * @since 3.4.0
1570          */
1571         public function prepare_controls() {
1572
1573                 $controls = array();
1574                 uasort( $this->controls, array( $this, '_cmp_priority' ) );
1575
1576                 foreach ( $this->controls as $id => $control ) {
1577                         if ( ! isset( $this->sections[ $control->section ] ) || ! $control->check_capabilities() ) {
1578                                 continue;
1579                         }
1580
1581                         $this->sections[ $control->section ]->controls[] = $control;
1582                         $controls[ $id ] = $control;
1583                 }
1584                 $this->controls = $controls;
1585
1586                 // Prepare sections.
1587                 uasort( $this->sections, array( $this, '_cmp_priority' ) );
1588                 $sections = array();
1589
1590                 foreach ( $this->sections as $section ) {
1591                         if ( ! $section->check_capabilities() ) {
1592                                 continue;
1593                         }
1594
1595                         usort( $section->controls, array( $this, '_cmp_priority' ) );
1596
1597                         if ( ! $section->panel ) {
1598                                 // Top-level section.
1599                                 $sections[ $section->id ] = $section;
1600                         } else {
1601                                 // This section belongs to a panel.
1602                                 if ( isset( $this->panels [ $section->panel ] ) ) {
1603                                         $this->panels[ $section->panel ]->sections[ $section->id ] = $section;
1604                                 }
1605                         }
1606                 }
1607                 $this->sections = $sections;
1608
1609                 // Prepare panels.
1610                 uasort( $this->panels, array( $this, '_cmp_priority' ) );
1611                 $panels = array();
1612
1613                 foreach ( $this->panels as $panel ) {
1614                         if ( ! $panel->check_capabilities() ) {
1615                                 continue;
1616                         }
1617
1618                         uasort( $panel->sections, array( $this, '_cmp_priority' ) );
1619                         $panels[ $panel->id ] = $panel;
1620                 }
1621                 $this->panels = $panels;
1622
1623                 // Sort panels and top-level sections together.
1624                 $this->containers = array_merge( $this->panels, $this->sections );
1625                 uasort( $this->containers, array( $this, '_cmp_priority' ) );
1626         }
1627
1628         /**
1629          * Enqueue scripts for customize controls.
1630          *
1631          * @since 3.4.0
1632          */
1633         public function enqueue_control_scripts() {
1634                 foreach ( $this->controls as $control ) {
1635                         $control->enqueue();
1636                 }
1637         }
1638
1639         /**
1640          * Determine whether the user agent is iOS.
1641          *
1642          * @since 4.4.0
1643          * @access public
1644          *
1645          * @return bool Whether the user agent is iOS.
1646          */
1647         public function is_ios() {
1648                 return wp_is_mobile() && preg_match( '/iPad|iPod|iPhone/', $_SERVER['HTTP_USER_AGENT'] );
1649         }
1650
1651         /**
1652          * Get the template string for the Customizer pane document title.
1653          *
1654          * @since 4.4.0
1655          * @access public
1656          *
1657          * @return string The template string for the document title.
1658          */
1659         public function get_document_title_template() {
1660                 if ( $this->is_theme_active() ) {
1661                         /* translators: %s: document title from the preview */
1662                         $document_title_tmpl = __( 'Customize: %s' );
1663                 } else {
1664                         /* translators: %s: document title from the preview */
1665                         $document_title_tmpl = __( 'Live Preview: %s' );
1666                 }
1667                 $document_title_tmpl = html_entity_decode( $document_title_tmpl, ENT_QUOTES, 'UTF-8' ); // Because exported to JS and assigned to document.title.
1668                 return $document_title_tmpl;
1669         }
1670
1671         /**
1672          * Set the initial URL to be previewed.
1673          *
1674          * URL is validated.
1675          *
1676          * @since 4.4.0
1677          * @access public
1678          *
1679          * @param string $preview_url URL to be previewed.
1680          */
1681         public function set_preview_url( $preview_url ) {
1682                 $preview_url = esc_url_raw( $preview_url );
1683                 $this->preview_url = wp_validate_redirect( $preview_url, home_url( '/' ) );
1684         }
1685
1686         /**
1687          * Get the initial URL to be previewed.
1688          *
1689          * @since 4.4.0
1690          * @access public
1691          *
1692          * @return string URL being previewed.
1693          */
1694         public function get_preview_url() {
1695                 if ( empty( $this->preview_url ) ) {
1696                         $preview_url = home_url( '/' );
1697                 } else {
1698                         $preview_url = $this->preview_url;
1699                 }
1700                 return $preview_url;
1701         }
1702
1703         /**
1704          * Set URL to link the user to when closing the Customizer.
1705          *
1706          * URL is validated.
1707          *
1708          * @since 4.4.0
1709          * @access public
1710          *
1711          * @param string $return_url URL for return link.
1712          */
1713         public function set_return_url( $return_url ) {
1714                 $return_url = esc_url_raw( $return_url );
1715                 $return_url = remove_query_arg( wp_removable_query_args(), $return_url );
1716                 $return_url = wp_validate_redirect( $return_url );
1717                 $this->return_url = $return_url;
1718         }
1719
1720         /**
1721          * Get URL to link the user to when closing the Customizer.
1722          *
1723          * @since 4.4.0
1724          * @access public
1725          *
1726          * @return string URL for link to close Customizer.
1727          */
1728         public function get_return_url() {
1729                 $referer = wp_get_referer();
1730                 $excluded_referer_basenames = array( 'customize.php', 'wp-login.php' );
1731
1732                 if ( $this->return_url ) {
1733                         $return_url = $this->return_url;
1734                 } else if ( $referer && ! in_array( basename( parse_url( $referer, PHP_URL_PATH ) ), $excluded_referer_basenames, true ) ) {
1735                         $return_url = $referer;
1736                 } else if ( $this->preview_url ) {
1737                         $return_url = $this->preview_url;
1738                 } else {
1739                         $return_url = home_url( '/' );
1740                 }
1741                 return $return_url;
1742         }
1743
1744         /**
1745          * Set the autofocused constructs.
1746          *
1747          * @since 4.4.0
1748          * @access public
1749          *
1750          * @param array $autofocus {
1751          *     Mapping of 'panel', 'section', 'control' to the ID which should be autofocused.
1752          *
1753          *     @type string [$control]  ID for control to be autofocused.
1754          *     @type string [$section]  ID for section to be autofocused.
1755          *     @type string [$panel]    ID for panel to be autofocused.
1756          * }
1757          */
1758         public function set_autofocus( $autofocus ) {
1759                 $this->autofocus = array_filter( wp_array_slice_assoc( $autofocus, array( 'panel', 'section', 'control' ) ), 'is_string' );
1760         }
1761
1762         /**
1763          * Get the autofocused constructs.
1764          *
1765          * @since 4.4.0
1766          * @access public
1767          *
1768          * @return array {
1769          *     Mapping of 'panel', 'section', 'control' to the ID which should be autofocused.
1770          *
1771          *     @type string [$control]  ID for control to be autofocused.
1772          *     @type string [$section]  ID for section to be autofocused.
1773          *     @type string [$panel]    ID for panel to be autofocused.
1774          * }
1775          */
1776         public function get_autofocus() {
1777                 return $this->autofocus;
1778         }
1779
1780         /**
1781          * Get nonces for the Customizer.
1782          *
1783          * @since 4.5.0
1784          * @return array Nonces.
1785          */
1786         public function get_nonces() {
1787                 $nonces = array(
1788                         'save' => wp_create_nonce( 'save-customize_' . $this->get_stylesheet() ),
1789                         'preview' => wp_create_nonce( 'preview-customize_' . $this->get_stylesheet() ),
1790                 );
1791
1792                 /**
1793                  * Filters nonces for Customizer.
1794                  *
1795                  * @since 4.2.0
1796                  *
1797                  * @param array                $nonces Array of refreshed nonces for save and
1798                  *                                     preview actions.
1799                  * @param WP_Customize_Manager $this   WP_Customize_Manager instance.
1800                  */
1801                 $nonces = apply_filters( 'customize_refresh_nonces', $nonces, $this );
1802
1803                 return $nonces;
1804         }
1805
1806         /**
1807          * Print JavaScript settings for parent window.
1808          *
1809          * @since 4.4.0
1810          */
1811         public function customize_pane_settings() {
1812                 /*
1813                  * If the front end and the admin are served from the same domain, load the
1814                  * preview over ssl if the Customizer is being loaded over ssl. This avoids
1815                  * insecure content warnings. This is not attempted if the admin and front end
1816                  * are on different domains to avoid the case where the front end doesn't have
1817                  * ssl certs. Domain mapping plugins can allow other urls in these conditions
1818                  * using the customize_allowed_urls filter.
1819                  */
1820
1821                 $allowed_urls = array( home_url( '/' ) );
1822                 $admin_origin = parse_url( admin_url() );
1823                 $home_origin  = parse_url( home_url() );
1824                 $cross_domain = ( strtolower( $admin_origin['host'] ) !== strtolower( $home_origin['host'] ) );
1825
1826                 if ( is_ssl() && ! $cross_domain ) {
1827                         $allowed_urls[] = home_url( '/', 'https' );
1828                 }
1829
1830                 /**
1831                  * Filters the list of URLs allowed to be clicked and followed in the Customizer preview.
1832                  *
1833                  * @since 3.4.0
1834                  *
1835                  * @param array $allowed_urls An array of allowed URLs.
1836                  */
1837                 $allowed_urls = array_unique( apply_filters( 'customize_allowed_urls', $allowed_urls ) );
1838
1839                 $login_url = add_query_arg( array(
1840                         'interim-login' => 1,
1841                         'customize-login' => 1,
1842                 ), wp_login_url() );
1843
1844                 // Prepare Customizer settings to pass to JavaScript.
1845                 $settings = array(
1846                         'theme'    => array(
1847                                 'stylesheet' => $this->get_stylesheet(),
1848                                 'active'     => $this->is_theme_active(),
1849                         ),
1850                         'url'      => array(
1851                                 'preview'       => esc_url_raw( $this->get_preview_url() ),
1852                                 'parent'        => esc_url_raw( admin_url() ),
1853                                 'activated'     => esc_url_raw( home_url( '/' ) ),
1854                                 'ajax'          => esc_url_raw( admin_url( 'admin-ajax.php', 'relative' ) ),
1855                                 'allowed'       => array_map( 'esc_url_raw', $allowed_urls ),
1856                                 'isCrossDomain' => $cross_domain,
1857                                 'home'          => esc_url_raw( home_url( '/' ) ),
1858                                 'login'         => esc_url_raw( $login_url ),
1859                         ),
1860                         'browser'  => array(
1861                                 'mobile' => wp_is_mobile(),
1862                                 'ios'    => $this->is_ios(),
1863                         ),
1864                         'panels'   => array(),
1865                         'sections' => array(),
1866                         'nonce'    => $this->get_nonces(),
1867                         'autofocus' => $this->get_autofocus(),
1868                         'documentTitleTmpl' => $this->get_document_title_template(),
1869                         'previewableDevices' => $this->get_previewable_devices(),
1870                 );
1871
1872                 // Prepare Customize Section objects to pass to JavaScript.
1873                 foreach ( $this->sections() as $id => $section ) {
1874                         if ( $section->check_capabilities() ) {
1875                                 $settings['sections'][ $id ] = $section->json();
1876                         }
1877                 }
1878
1879                 // Prepare Customize Panel objects to pass to JavaScript.
1880                 foreach ( $this->panels() as $panel_id => $panel ) {
1881                         if ( $panel->check_capabilities() ) {
1882                                 $settings['panels'][ $panel_id ] = $panel->json();
1883                                 foreach ( $panel->sections as $section_id => $section ) {
1884                                         if ( $section->check_capabilities() ) {
1885                                                 $settings['sections'][ $section_id ] = $section->json();
1886                                         }
1887                                 }
1888                         }
1889                 }
1890
1891                 ?>
1892                 <script type="text/javascript">
1893                         var _wpCustomizeSettings = <?php echo wp_json_encode( $settings ); ?>;
1894                         _wpCustomizeSettings.controls = {};
1895                         _wpCustomizeSettings.settings = {};
1896                         <?php
1897
1898                         // Serialize settings one by one to improve memory usage.
1899                         echo "(function ( s ){\n";
1900                         foreach ( $this->settings() as $setting ) {
1901                                 if ( $setting->check_capabilities() ) {
1902                                         printf(
1903                                                 "s[%s] = %s;\n",
1904                                                 wp_json_encode( $setting->id ),
1905                                                 wp_json_encode( $setting->json() )
1906                                         );
1907                                 }
1908                         }
1909                         echo "})( _wpCustomizeSettings.settings );\n";
1910
1911                         // Serialize controls one by one to improve memory usage.
1912                         echo "(function ( c ){\n";
1913                         foreach ( $this->controls() as $control ) {
1914                                 if ( $control->check_capabilities() ) {
1915                                         printf(
1916                                                 "c[%s] = %s;\n",
1917                                                 wp_json_encode( $control->id ),
1918                                                 wp_json_encode( $control->json() )
1919                                         );
1920                                 }
1921                         }
1922                         echo "})( _wpCustomizeSettings.controls );\n";
1923                 ?>
1924                 </script>
1925                 <?php
1926         }
1927
1928         /**
1929          * Returns a list of devices to allow previewing.
1930          *
1931          * @access public
1932          * @since 4.5.0
1933          *
1934          * @return array List of devices with labels and default setting.
1935          */
1936         public function get_previewable_devices() {
1937                 $devices = array(
1938                         'desktop' => array(
1939                                 'label' => __( 'Enter desktop preview mode' ),
1940                                 'default' => true,
1941                         ),
1942                         'tablet' => array(
1943                                 'label' => __( 'Enter tablet preview mode' ),
1944                         ),
1945                         'mobile' => array(
1946                                 'label' => __( 'Enter mobile preview mode' ),
1947                         ),
1948                 );
1949
1950                 /**
1951                  * Filters the available devices to allow previewing in the Customizer.
1952                  *
1953                  * @since 4.5.0
1954                  *
1955                  * @see WP_Customize_Manager::get_previewable_devices()
1956                  *
1957                  * @param array $devices List of devices with labels and default setting.
1958                  */
1959                 $devices = apply_filters( 'customize_previewable_devices', $devices );
1960
1961                 return $devices;
1962         }
1963
1964         /**
1965          * Register some default controls.
1966          *
1967          * @since 3.4.0
1968          */
1969         public function register_controls() {
1970
1971                 /* Panel, Section, and Control Types */
1972                 $this->register_panel_type( 'WP_Customize_Panel' );
1973                 $this->register_section_type( 'WP_Customize_Section' );
1974                 $this->register_section_type( 'WP_Customize_Sidebar_Section' );
1975                 $this->register_control_type( 'WP_Customize_Color_Control' );
1976                 $this->register_control_type( 'WP_Customize_Media_Control' );
1977                 $this->register_control_type( 'WP_Customize_Upload_Control' );
1978                 $this->register_control_type( 'WP_Customize_Image_Control' );
1979                 $this->register_control_type( 'WP_Customize_Background_Image_Control' );
1980                 $this->register_control_type( 'WP_Customize_Cropped_Image_Control' );
1981                 $this->register_control_type( 'WP_Customize_Site_Icon_Control' );
1982                 $this->register_control_type( 'WP_Customize_Theme_Control' );
1983
1984                 /* Themes */
1985
1986                 $this->add_section( new WP_Customize_Themes_Section( $this, 'themes', array(
1987                         'title'      => $this->theme()->display( 'Name' ),
1988                         'capability' => 'switch_themes',
1989                         'priority'   => 0,
1990                 ) ) );
1991
1992                 // Themes Setting (unused - the theme is considerably more fundamental to the Customizer experience).
1993                 $this->add_setting( new WP_Customize_Filter_Setting( $this, 'active_theme', array(
1994                         'capability' => 'switch_themes',
1995                 ) ) );
1996
1997                 require_once( ABSPATH . 'wp-admin/includes/theme.php' );
1998
1999                 // Theme Controls.
2000
2001                 // Add a control for the active/original theme.
2002                 if ( ! $this->is_theme_active() ) {
2003                         $themes = wp_prepare_themes_for_js( array( wp_get_theme( $this->original_stylesheet ) ) );
2004                         $active_theme = current( $themes );
2005                         $active_theme['isActiveTheme'] = true;
2006                         $this->add_control( new WP_Customize_Theme_Control( $this, $active_theme['id'], array(
2007                                 'theme'    => $active_theme,
2008                                 'section'  => 'themes',
2009                                 'settings' => 'active_theme',
2010                         ) ) );
2011                 }
2012
2013                 $themes = wp_prepare_themes_for_js();
2014                 foreach ( $themes as $theme ) {
2015                         if ( $theme['active'] || $theme['id'] === $this->original_stylesheet ) {
2016                                 continue;
2017                         }
2018
2019                         $theme_id = 'theme_' . $theme['id'];
2020                         $theme['isActiveTheme'] = false;
2021                         $this->add_control( new WP_Customize_Theme_Control( $this, $theme_id, array(
2022                                 'theme'    => $theme,
2023                                 'section'  => 'themes',
2024                                 'settings' => 'active_theme',
2025                         ) ) );
2026                 }
2027
2028                 /* Site Identity */
2029
2030                 $this->add_section( 'title_tagline', array(
2031                         'title'    => __( 'Site Identity' ),
2032                         'priority' => 20,
2033                 ) );
2034
2035                 $this->add_setting( 'blogname', array(
2036                         'default'    => get_option( 'blogname' ),
2037                         'type'       => 'option',
2038                         'capability' => 'manage_options',
2039                 ) );
2040
2041                 $this->add_control( 'blogname', array(
2042                         'label'      => __( 'Site Title' ),
2043                         'section'    => 'title_tagline',
2044                 ) );
2045
2046                 $this->add_setting( 'blogdescription', array(
2047                         'default'    => get_option( 'blogdescription' ),
2048                         'type'       => 'option',
2049                         'capability' => 'manage_options',
2050                 ) );
2051
2052                 $this->add_control( 'blogdescription', array(
2053                         'label'      => __( 'Tagline' ),
2054                         'section'    => 'title_tagline',
2055                 ) );
2056
2057                 // Add a setting to hide header text if the theme doesn't support custom headers.
2058                 if ( ! current_theme_supports( 'custom-header', 'header-text' ) ) {
2059                         $this->add_setting( 'header_text', array(
2060                                 'theme_supports'    => array( 'custom-logo', 'header-text' ),
2061                                 'default'           => 1,
2062                                 'sanitize_callback' => 'absint',
2063                         ) );
2064
2065                         $this->add_control( 'header_text', array(
2066                                 'label'    => __( 'Display Site Title and Tagline' ),
2067                                 'section'  => 'title_tagline',
2068                                 'settings' => 'header_text',
2069                                 'type'     => 'checkbox',
2070                         ) );
2071                 }
2072
2073                 $this->add_setting( 'site_icon', array(
2074                         'type'       => 'option',
2075                         'capability' => 'manage_options',
2076                         'transport'  => 'postMessage', // Previewed with JS in the Customizer controls window.
2077                 ) );
2078
2079                 $this->add_control( new WP_Customize_Site_Icon_Control( $this, 'site_icon', array(
2080                         'label'       => __( 'Site Icon' ),
2081                         'description' => sprintf(
2082                                 /* translators: %s: site icon size in pixels */
2083                                 __( '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.' ),
2084                                 '<strong>512</strong>'
2085                         ),
2086                         'section'     => 'title_tagline',
2087                         'priority'    => 60,
2088                         'height'      => 512,
2089                         'width'       => 512,
2090                 ) ) );
2091
2092                 $this->add_setting( 'custom_logo', array(
2093                         'theme_supports' => array( 'custom-logo' ),
2094                         'transport'      => 'postMessage',
2095                 ) );
2096
2097                 $custom_logo_args = get_theme_support( 'custom-logo' );
2098                 $this->add_control( new WP_Customize_Cropped_Image_Control( $this, 'custom_logo', array(
2099                         'label'         => __( 'Logo' ),
2100                         'section'       => 'title_tagline',
2101                         'priority'      => 8,
2102                         'height'        => $custom_logo_args[0]['height'],
2103                         'width'         => $custom_logo_args[0]['width'],
2104                         'flex_height'   => $custom_logo_args[0]['flex-height'],
2105                         'flex_width'    => $custom_logo_args[0]['flex-width'],
2106                         'button_labels' => array(
2107                                 'select'       => __( 'Select logo' ),
2108                                 'change'       => __( 'Change logo' ),
2109                                 'remove'       => __( 'Remove' ),
2110                                 'default'      => __( 'Default' ),
2111                                 'placeholder'  => __( 'No logo selected' ),
2112                                 'frame_title'  => __( 'Select logo' ),
2113                                 'frame_button' => __( 'Choose logo' ),
2114                         ),
2115                 ) ) );
2116
2117                 $this->selective_refresh->add_partial( 'custom_logo', array(
2118                         'settings'            => array( 'custom_logo' ),
2119                         'selector'            => '.custom-logo-link',
2120                         'render_callback'     => array( $this, '_render_custom_logo_partial' ),
2121                         'container_inclusive' => true,
2122                 ) );
2123
2124                 /* Colors */
2125
2126                 $this->add_section( 'colors', array(
2127                         'title'          => __( 'Colors' ),
2128                         'priority'       => 40,
2129                 ) );
2130
2131                 $this->add_setting( 'header_textcolor', array(
2132                         'theme_supports' => array( 'custom-header', 'header-text' ),
2133                         'default'        => get_theme_support( 'custom-header', 'default-text-color' ),
2134
2135                         'sanitize_callback'    => array( $this, '_sanitize_header_textcolor' ),
2136                         'sanitize_js_callback' => 'maybe_hash_hex_color',
2137                 ) );
2138
2139                 // Input type: checkbox
2140                 // With custom value
2141                 $this->add_control( 'display_header_text', array(
2142                         'settings' => 'header_textcolor',
2143                         'label'    => __( 'Display Site Title and Tagline' ),
2144                         'section'  => 'title_tagline',
2145                         'type'     => 'checkbox',
2146                         'priority' => 40,
2147                 ) );
2148
2149                 $this->add_control( new WP_Customize_Color_Control( $this, 'header_textcolor', array(
2150                         'label'   => __( 'Header Text Color' ),
2151                         'section' => 'colors',
2152                 ) ) );
2153
2154                 // Input type: Color
2155                 // With sanitize_callback
2156                 $this->add_setting( 'background_color', array(
2157                         'default'        => get_theme_support( 'custom-background', 'default-color' ),
2158                         'theme_supports' => 'custom-background',
2159
2160                         'sanitize_callback'    => 'sanitize_hex_color_no_hash',
2161                         'sanitize_js_callback' => 'maybe_hash_hex_color',
2162                 ) );
2163
2164                 $this->add_control( new WP_Customize_Color_Control( $this, 'background_color', array(
2165                         'label'   => __( 'Background Color' ),
2166                         'section' => 'colors',
2167                 ) ) );
2168
2169
2170                 /* Custom Header */
2171
2172                 $this->add_section( 'header_image', array(
2173                         'title'          => __( 'Header Image' ),
2174                         'theme_supports' => 'custom-header',
2175                         'priority'       => 60,
2176                 ) );
2177
2178                 $this->add_setting( new WP_Customize_Filter_Setting( $this, 'header_image', array(
2179                         'default'        => get_theme_support( 'custom-header', 'default-image' ),
2180                         'theme_supports' => 'custom-header',
2181                 ) ) );
2182
2183                 $this->add_setting( new WP_Customize_Header_Image_Setting( $this, 'header_image_data', array(
2184                         // 'default'        => get_theme_support( 'custom-header', 'default-image' ),
2185                         'theme_supports' => 'custom-header',
2186                 ) ) );
2187
2188                 $this->add_control( new WP_Customize_Header_Image_Control( $this ) );
2189
2190                 /* Custom Background */
2191
2192                 $this->add_section( 'background_image', array(
2193                         'title'          => __( 'Background Image' ),
2194                         'theme_supports' => 'custom-background',
2195                         'priority'       => 80,
2196                 ) );
2197
2198                 $this->add_setting( 'background_image', array(
2199                         'default'        => get_theme_support( 'custom-background', 'default-image' ),
2200                         'theme_supports' => 'custom-background',
2201                 ) );
2202
2203                 $this->add_setting( new WP_Customize_Background_Image_Setting( $this, 'background_image_thumb', array(
2204                         'theme_supports' => 'custom-background',
2205                 ) ) );
2206
2207                 $this->add_control( new WP_Customize_Background_Image_Control( $this ) );
2208
2209                 $this->add_setting( 'background_repeat', array(
2210                         'default'        => get_theme_support( 'custom-background', 'default-repeat' ),
2211                         'theme_supports' => 'custom-background',
2212                 ) );
2213
2214                 $this->add_control( 'background_repeat', array(
2215                         'label'      => __( 'Background Repeat' ),
2216                         'section'    => 'background_image',
2217                         'type'       => 'radio',
2218                         'choices'    => array(
2219                                 'no-repeat'  => __('No Repeat'),
2220                                 'repeat'     => __('Tile'),
2221                                 'repeat-x'   => __('Tile Horizontally'),
2222                                 'repeat-y'   => __('Tile Vertically'),
2223                         ),
2224                 ) );
2225
2226                 $this->add_setting( 'background_position_x', array(
2227                         'default'        => get_theme_support( 'custom-background', 'default-position-x' ),
2228                         'theme_supports' => 'custom-background',
2229                 ) );
2230
2231                 $this->add_control( 'background_position_x', array(
2232                         'label'      => __( 'Background Position' ),
2233                         'section'    => 'background_image',
2234                         'type'       => 'radio',
2235                         'choices'    => array(
2236                                 'left'       => __('Left'),
2237                                 'center'     => __('Center'),
2238                                 'right'      => __('Right'),
2239                         ),
2240                 ) );
2241
2242                 $this->add_setting( 'background_attachment', array(
2243                         'default'        => get_theme_support( 'custom-background', 'default-attachment' ),
2244                         'theme_supports' => 'custom-background',
2245                 ) );
2246
2247                 $this->add_control( 'background_attachment', array(
2248                         'label'      => __( 'Background Attachment' ),
2249                         'section'    => 'background_image',
2250                         'type'       => 'radio',
2251                         'choices'    => array(
2252                                 'scroll'     => __('Scroll'),
2253                                 'fixed'      => __('Fixed'),
2254                         ),
2255                 ) );
2256
2257                 // If the theme is using the default background callback, we can update
2258                 // the background CSS using postMessage.
2259                 if ( get_theme_support( 'custom-background', 'wp-head-callback' ) === '_custom_background_cb' ) {
2260                         foreach ( array( 'color', 'image', 'position_x', 'repeat', 'attachment' ) as $prop ) {
2261                                 $this->get_setting( 'background_' . $prop )->transport = 'postMessage';
2262                         }
2263                 }
2264
2265                 /* Static Front Page */
2266                 // #WP19627
2267
2268                 // Replicate behavior from options-reading.php and hide front page options if there are no pages
2269                 if ( get_pages() ) {
2270                         $this->add_section( 'static_front_page', array(
2271                                 'title'          => __( 'Static Front Page' ),
2272                         //      'theme_supports' => 'static-front-page',
2273                                 'priority'       => 120,
2274                                 'description'    => __( 'Your theme supports a static front page.' ),
2275                         ) );
2276
2277                         $this->add_setting( 'show_on_front', array(
2278                                 'default'        => get_option( 'show_on_front' ),
2279                                 'capability'     => 'manage_options',
2280                                 'type'           => 'option',
2281                         //      'theme_supports' => 'static-front-page',
2282                         ) );
2283
2284                         $this->add_control( 'show_on_front', array(
2285                                 'label'   => __( 'Front page displays' ),
2286                                 'section' => 'static_front_page',
2287                                 'type'    => 'radio',
2288                                 'choices' => array(
2289                                         'posts' => __( 'Your latest posts' ),
2290                                         'page'  => __( 'A static page' ),
2291                                 ),
2292                         ) );
2293
2294                         $this->add_setting( 'page_on_front', array(
2295                                 'type'       => 'option',
2296                                 'capability' => 'manage_options',
2297                         //      'theme_supports' => 'static-front-page',
2298                         ) );
2299
2300                         $this->add_control( 'page_on_front', array(
2301                                 'label'      => __( 'Front page' ),
2302                                 'section'    => 'static_front_page',
2303                                 'type'       => 'dropdown-pages',
2304                         ) );
2305
2306                         $this->add_setting( 'page_for_posts', array(
2307                                 'type'           => 'option',
2308                                 'capability'     => 'manage_options',
2309                         //      'theme_supports' => 'static-front-page',
2310                         ) );
2311
2312                         $this->add_control( 'page_for_posts', array(
2313                                 'label'      => __( 'Posts page' ),
2314                                 'section'    => 'static_front_page',
2315                                 'type'       => 'dropdown-pages',
2316                         ) );
2317                 }
2318         }
2319
2320         /**
2321          * Add settings from the POST data that were not added with code, e.g. dynamically-created settings for Widgets
2322          *
2323          * @since 4.2.0
2324          * @access public
2325          *
2326          * @see add_dynamic_settings()
2327          */
2328         public function register_dynamic_settings() {
2329                 $this->add_dynamic_settings( array_keys( $this->unsanitized_post_values() ) );
2330         }
2331
2332         /**
2333          * Callback for validating the header_textcolor value.
2334          *
2335          * Accepts 'blank', and otherwise uses sanitize_hex_color_no_hash().
2336          * Returns default text color if hex color is empty.
2337          *
2338          * @since 3.4.0
2339          *
2340          * @param string $color
2341          * @return mixed
2342          */
2343         public function _sanitize_header_textcolor( $color ) {
2344                 if ( 'blank' === $color )
2345                         return 'blank';
2346
2347                 $color = sanitize_hex_color_no_hash( $color );
2348                 if ( empty( $color ) )
2349                         $color = get_theme_support( 'custom-header', 'default-text-color' );
2350
2351                 return $color;
2352         }
2353
2354         /**
2355          * Callback for rendering the custom logo, used in the custom_logo partial.
2356          *
2357          * This method exists because the partial object and context data are passed
2358          * into a partial's render_callback so we cannot use get_custom_logo() as
2359          * the render_callback directly since it expects a blog ID as the first
2360          * argument. When WP no longer supports PHP 5.3, this method can be removed
2361          * in favor of an anonymous function.
2362          *
2363          * @see WP_Customize_Manager::register_controls()
2364          *
2365          * @since 4.5.0
2366          * @access private
2367          *
2368          * @return string Custom logo.
2369          */
2370         public function _render_custom_logo_partial() {
2371                 return get_custom_logo();
2372         }
2373 }