3 * WordPress Customize Manager classes
6 * @subpackage Customize
11 * Customize Manager class.
13 * Bootstraps the Customize experience on the server-side.
15 * Sets up the theme-switching process if a theme other than the active one is
16 * being previewed and customized.
18 * Serves as a factory for Customize Controls and Settings, and
19 * instantiates default Customize Controls and Settings.
23 final class WP_Customize_Manager {
25 * An instance of the theme being previewed.
34 * The directory name of the previously active theme (within the theme_root).
40 protected $original_stylesheet;
43 * Whether this is a Customizer pageload.
49 protected $previewing = false;
52 * Methods and properties dealing with managing widgets in the Customizer.
56 * @var WP_Customize_Widgets
61 * Methods and properties dealing with managing nav menus in the Customizer.
65 * @var WP_Customize_Nav_Menus
70 * Methods and properties dealing with selective refresh in the Customizer preview.
74 * @var WP_Customize_Selective_Refresh
76 public $selective_refresh;
79 * Registered instances of WP_Customize_Setting.
85 protected $settings = array();
88 * Sorted top-level instances of WP_Customize_Panel and WP_Customize_Section.
94 protected $containers = array();
97 * Registered instances of WP_Customize_Panel.
103 protected $panels = array();
106 * List of core components.
112 protected $components = array( 'widgets', 'nav_menus' );
115 * Registered instances of WP_Customize_Section.
121 protected $sections = array();
124 * Registered instances of WP_Customize_Control.
130 protected $controls = array();
133 * Return value of check_ajax_referer() in customize_preview_init() method.
139 protected $nonce_tick;
142 * Panel types that may be rendered from JS templates.
148 protected $registered_panel_types = array();
151 * Section types that may be rendered from JS templates.
157 protected $registered_section_types = array();
160 * Control types that may be rendered from JS templates.
166 protected $registered_control_types = array();
169 * Initial URL being previewed.
175 protected $preview_url;
178 * URL to link the user to when closing the Customizer.
184 protected $return_url;
187 * Mapping of 'panel', 'section', 'control' to the ID which should be autofocused.
193 protected $autofocus = array();
196 * Unsanitized values for Customize Settings parsed from $_POST['customized'].
200 private $_post_values;
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' );
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' );
231 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menus-panel.php' );
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' );
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' );
245 * Filters the core Customizer components to load.
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
254 * @see WP_Customize_Manager::__construct()
256 * @param array $components List of core components to load.
257 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
259 $components = apply_filters( 'customize_loaded_components', $this->components, $this );
261 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-selective-refresh.php' );
262 $this->selective_refresh = new WP_Customize_Selective_Refresh( $this );
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 );
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 );
274 add_filter( 'wp_die_handler', array( $this, 'wp_die_handler' ) );
276 add_action( 'setup_theme', array( $this, 'setup_theme' ) );
277 add_action( 'wp_loaded', array( $this, 'wp_loaded' ) );
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 );
282 // Do not spawn cron (especially the alternate cron) while running the Customizer.
283 remove_action( 'init', 'wp_cron' );
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' );
290 add_action( 'wp_ajax_customize_save', array( $this, 'save' ) );
291 add_action( 'wp_ajax_customize_refresh_nonces', array( $this, 'refresh_nonces' ) );
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' ) );
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 );
303 // Export the settings to JS via the _wpCustomizeSettings variable.
304 add_action( 'customize_controls_print_footer_scripts', array( $this, 'customize_pane_settings' ), 1000 );
308 * Return true if it's an Ajax request.
311 * @since 4.2.0 Added `$action` param.
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.
317 public function doing_ajax( $action = null ) {
318 $doing_ajax = ( defined( 'DOING_AJAX' ) && DOING_AJAX );
319 if ( ! $doing_ajax ) {
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.
330 return isset( $_REQUEST['action'] ) && wp_unslash( $_REQUEST['action'] ) === $action;
335 * Custom wp_die wrapper. Returns either the standard message for UI
336 * or the Ajax message.
340 * @param mixed $ajax_message Ajax return
341 * @param mixed $message UI message
343 protected function wp_die( $ajax_message, $message = null ) {
344 if ( $this->doing_ajax() || isset( $_POST['customized'] ) ) {
345 wp_die( $ajax_message );
349 $message = __( 'Cheatin’ uh?' );
356 * Return the Ajax wp_die() handler if it's a customized request.
362 public function wp_die_handler() {
363 if ( $this->doing_ajax() || isset( $_POST['customized'] ) ) {
364 return '_ajax_wp_die_handler';
367 return '_default_wp_die_handler';
371 * Start preview and customize theme.
373 * Check if customize query variable exist. Init filters to filter the current theme.
377 public function setup_theme() {
378 send_origin_headers();
380 $doing_ajax_or_is_customized = ( $this->doing_ajax() || isset( $_POST['customized'] ) );
381 if ( is_admin() && ! $doing_ajax_or_is_customized ) {
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.' ) );
387 show_admin_bar( false );
389 if ( ! current_user_can( 'customize' ) ) {
390 $this->wp_die( -1, __( 'Sorry, you are not allowed to customize this site.' ) );
393 $this->original_stylesheet = get_stylesheet();
395 $this->theme = wp_get_theme( isset( $_REQUEST['theme'] ) ? $_REQUEST['theme'] : null );
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' ) );
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.' ) );
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() );
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.' ) );
418 $this->start_previewing_theme();
422 * Callback to validate a theme once it is loaded
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' );
435 * If the theme to be previewed isn't the active theme, add filter callbacks
436 * to swap it out at runtime.
440 public function start_previewing_theme() {
441 // Bail if we're already previewing.
442 if ( $this->is_preview() ) {
446 $this->previewing = true;
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' ) );
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' ) );
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' ) );
463 * Fires once the Customizer theme preview has started.
467 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
469 do_action( 'start_previewing_theme', $this );
473 * Stop previewing the selected theme.
475 * Removes filters to change the current theme.
479 public function stop_previewing_theme() {
480 if ( ! $this->is_preview() ) {
484 $this->previewing = false;
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' ) );
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' ) );
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' ) );
501 * Fires once the Customizer theme preview has stopped.
505 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
507 do_action( 'stop_previewing_theme', $this );
511 * Get the theme being customized.
517 public function theme() {
518 if ( ! $this->theme ) {
519 $this->theme = wp_get_theme();
525 * Get the registered settings.
531 public function settings() {
532 return $this->settings;
536 * Get the registered controls.
542 public function controls() {
543 return $this->controls;
547 * Get the registered containers.
553 public function containers() {
554 return $this->containers;
558 * Get the registered sections.
564 public function sections() {
565 return $this->sections;
569 * Get the registered panels.
574 * @return array Panels.
576 public function panels() {
577 return $this->panels;
581 * Checks if the current theme is active.
587 public function is_theme_active() {
588 return $this->get_stylesheet() == $this->original_stylesheet;
592 * Register styles/scripts and initialize the preview of each setting
596 public function wp_loaded() {
599 * Fires once WordPress has loaded, allowing scripts and styles to be initialized.
603 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
605 do_action( 'customize_register', $this );
607 if ( $this->is_preview() && ! is_admin() )
608 $this->customize_preview_init();
612 * Prevents Ajax requests from following redirects when previewing a theme
613 * by issuing a 200 response instead of a 30x.
615 * Instead, the JS will sniff out the location header.
622 public function wp_redirect_status( $status ) {
623 if ( $this->is_preview() && ! is_admin() )
630 * Parse the incoming $_POST['customized'] JSON data and store the unsanitized
631 * settings for subsequent post_value() lookups.
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 );
642 if ( empty( $this->_post_values ) ) { // if not isset or if JSON error
643 $this->_post_values = array();
646 if ( empty( $this->_post_values ) ) {
649 return $this->_post_values;
654 * Returns the sanitized value for a given setting from the request's POST data.
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.
661 * @see WP_REST_Server::dispatch()
662 * @see WP_Rest_Request::sanitize_params()
663 * @see WP_Rest_Request::has_valid_params()
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.
670 public function post_value( $setting, $default = null ) {
671 $post_values = $this->unsanitized_post_values();
672 if ( ! array_key_exists( $setting->id, $post_values ) ) {
675 $value = $post_values[ $setting->id ];
676 $valid = $setting->validate( $value );
677 if ( is_wp_error( $valid ) ) {
680 $value = $setting->sanitize( $value );
681 if ( is_null( $value ) || is_wp_error( $value ) ) {
688 * Override a setting's (unsanitized) value as found in any incoming $_POST['customized'].
693 * @param string $setting_id ID for the WP_Customize_Setting instance.
694 * @param mixed $value Post value.
696 public function set_post_value( $setting_id, $value ) {
697 $this->unsanitized_post_values();
698 $this->_post_values[ $setting_id ] = $value;
701 * Announce when a specific setting's unsanitized post value has been set.
703 * Fires when the WP_Customize_Manager::set_post_value() method is called.
705 * The dynamic portion of the hook name, `$setting_id`, refers to the setting ID.
709 * @param mixed $value Unsanitized setting post value.
710 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
712 do_action( "customize_post_value_set_{$setting_id}", $value, $this );
715 * Announce when any setting's unsanitized post value has been set.
717 * Fires when the WP_Customize_Manager::set_post_value() method is called.
719 * This is useful for `WP_Customize_Setting` instances to watch
720 * in order to update a cached previewed value.
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.
728 do_action( 'customize_post_value_set', $setting_id, $value, $this );
732 * Print JavaScript settings.
736 public function customize_preview_init() {
737 $this->nonce_tick = check_ajax_referer( 'preview-customize_' . $this->get_stylesheet(), 'nonce' );
739 $this->prepare_controls();
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' ) );
750 foreach ( $this->settings as $setting ) {
755 * Fires once the Customizer preview has initialized and JavaScript
756 * settings have been printed.
760 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
762 do_action( 'customize_preview_init', $this );
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.
772 public function customize_preview_override_404_status() {
774 status_header( 200 );
779 * Print base element for preview frame.
783 public function customize_preview_base() {
784 ?><base href="<?php echo home_url( '/' ); ?>" /><?php
788 * Print a workaround to handle HTML5 tags in IE < 9.
792 public function customize_preview_html5() { ?>
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] );
806 * Print CSS for loading indicators for the Customizer preview.
811 public function customize_preview_loading_style() {
813 body.wp-customizer-unloading {
815 cursor: progress !important;
816 -webkit-transition: opacity 0.5s;
817 transition: opacity 0.5s;
819 body.wp-customizer-unloading * {
820 pointer-events: none !important;
826 * Print JavaScript settings for preview frame.
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 );
836 'stylesheet' => $this->get_stylesheet(),
837 'active' => $this->is_theme_active(),
840 'self' => empty( $_SERVER['REQUEST_URI'] ) ? home_url( '/' ) : esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ),
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(),
849 'shiftClickToEdit' => __( 'Shift-click to edit this element.' ),
851 '_dirty' => array_keys( $this->unsanitized_post_values() ),
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();
864 foreach ( $this->sections as $id => $section ) {
865 if ( $section->check_capabilities() ) {
866 $settings['activeSections'][ $id ] = $section->active();
869 foreach ( $this->controls as $id => $control ) {
870 if ( $control->check_capabilities() ) {
871 $settings['activeControls'][ $id ] = $control->active();
876 <script type="text/javascript">
877 var _wpCustomizeSettings = <?php echo wp_json_encode( $settings ); ?>;
878 _wpCustomizeSettings.values = {};
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.
886 foreach ( $this->settings as $id => $setting ) {
887 if ( $setting->check_capabilities() ) {
890 wp_json_encode( $id ),
891 wp_json_encode( $setting->js_value() )
896 })( _wpCustomizeSettings.values );
902 * Prints a signature so we can ensure the Customizer was properly executed.
906 public function customize_preview_signature() {
907 echo 'WP_CUSTOMIZER_SIGNATURE';
911 * Removes the signature in case we experience a case where the Customizer was not properly executed.
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.
918 public function remove_preview_signature( $return = null ) {
919 remove_action( 'shutdown', array( $this, 'customize_preview_signature' ), 1000 );
925 * Is it a theme preview?
929 * @return bool True if it's a preview, false if not.
931 public function is_preview() {
932 return (bool) $this->previewing;
936 * Retrieve the template name of the previewed theme.
940 * @return string Template name.
942 public function get_template() {
943 return $this->theme()->get_template();
947 * Retrieve the stylesheet name of the previewed theme.
951 * @return string Stylesheet name.
953 public function get_stylesheet() {
954 return $this->theme()->get_stylesheet();
958 * Retrieve the template root of the previewed theme.
962 * @return string Theme root.
964 public function get_template_root() {
965 return get_raw_theme_root( $this->get_template(), true );
969 * Retrieve the stylesheet root of the previewed theme.
973 * @return string Theme root.
975 public function get_stylesheet_root() {
976 return get_raw_theme_root( $this->get_stylesheet(), true );
980 * Filters the current theme and return the name of the previewed theme.
984 * @param $current_theme {@internal Parameter is not used}
985 * @return string Theme name.
987 public function current_theme( $current_theme ) {
988 return $this->theme()->display('Name');
992 * Validates setting values.
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.
1001 * @see WP_REST_Request::has_valid_params()
1002 * @see WP_Customize_Setting::validate()
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`.
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 ) ) {
1014 $validity = $setting->validate( $unsanitized_value );
1015 if ( ! is_wp_error( $validity ) ) {
1016 $value = $setting->sanitize( $unsanitized_value );
1017 if ( is_null( $value ) ) {
1019 } elseif ( is_wp_error( $value ) ) {
1023 if ( false === $validity ) {
1024 $validity = new WP_Error( 'invalid_value', __( 'Invalid value.' ) );
1026 $validities[ $setting_id ] = $validity;
1032 * Prepares setting validity for exporting to the client (JS).
1034 * Converts `WP_Error` instance into array suitable for passing into the
1035 * `wp.customize.Notification` JS model.
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.
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();
1053 $error_data = array_merge(
1055 array( 'from_server' => true )
1057 $notification[ $error_code ] = array(
1058 'message' => join( ' ', $error_messages ),
1059 'data' => $error_data,
1062 return $notification;
1069 * Switch the theme and trigger the save() method on each setting.
1073 public function save() {
1074 if ( ! $this->is_preview() ) {
1075 wp_send_json_error( 'not_preview' );
1078 $action = 'save-customize_' . $this->get_stylesheet();
1079 if ( ! check_ajax_referer( $action, 'nonce', false ) ) {
1080 wp_send_json_error( 'invalid_nonce' );
1084 * Fires before save validation happens.
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.
1092 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
1094 do_action( 'customize_save_validation_before', $this );
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 ) {
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 ) ),
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 );
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();
1122 * Fires once the theme has switched in the Customizer, but before settings
1127 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
1129 do_action( 'customize_save', $this );
1131 foreach ( $this->settings as $setting ) {
1136 * Fires after Customize settings have been saved.
1140 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
1142 do_action( 'customize_save_after', $this );
1145 'setting_validities' => $exported_setting_validities,
1149 * Filters response data for a successful customize_save Ajax request.
1151 * This filter does not apply if there was a nonce or authentication failure.
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.
1159 $response = apply_filters( 'customize_save_response', $data, $this );
1160 wp_send_json_success( $response );
1164 * Refresh nonces for the current preview.
1168 public function refresh_nonces() {
1169 if ( ! $this->is_preview() ) {
1170 wp_send_json_error( 'not_preview' );
1173 wp_send_json_success( $this->get_nonces() );
1177 * Add a customize setting.
1180 * @since 4.5.0 Return added WP_Customize_Setting instance.
1183 * @param WP_Customize_Setting|string $id Customize Setting object, or ID.
1184 * @param array $args Setting arguments; passed to WP_Customize_Setting
1186 * @return WP_Customize_Setting The instance of the setting that was added.
1188 public function add_setting( $id, $args = array() ) {
1189 if ( $id instanceof WP_Customize_Setting ) {
1192 $class = 'WP_Customize_Setting';
1194 /** This filter is documented in wp-includes/class-wp-customize-manager.php */
1195 $args = apply_filters( 'customize_dynamic_setting_args', $args, $id );
1197 /** This filter is documented in wp-includes/class-wp-customize-manager.php */
1198 $class = apply_filters( 'customize_dynamic_setting_class', $class, $id, $args );
1200 $setting = new $class( $this, $id, $args );
1203 $this->settings[ $setting->id ] = $setting;
1208 * Register any dynamically-created settings, such as those from $_POST['customized']
1209 * that have no corresponding setting created.
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.
1219 * @param array $setting_ids The setting IDs to add.
1220 * @return array The WP_Customize_Setting objects added.
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 ) ) {
1230 $setting_args = false;
1231 $setting_class = 'WP_Customize_Setting';
1234 * Filters a dynamic setting's constructor args.
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.
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']`.
1245 $setting_args = apply_filters( 'customize_dynamic_setting_args', $setting_args, $setting_id );
1246 if ( false === $setting_args ) {
1251 * Allow non-statically created settings to be constructed with custom WP_Customize_Setting subclass.
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.
1259 $setting_class = apply_filters( 'customize_dynamic_setting_class', $setting_class, $setting_id, $setting_args );
1261 $setting = new $setting_class( $this, $setting_id, $setting_args );
1263 $this->add_setting( $setting );
1264 $new_settings[] = $setting;
1266 return $new_settings;
1270 * Retrieve a customize setting.
1274 * @param string $id Customize Setting ID.
1275 * @return WP_Customize_Setting|void The setting, if set.
1277 public function get_setting( $id ) {
1278 if ( isset( $this->settings[ $id ] ) ) {
1279 return $this->settings[ $id ];
1284 * Remove a customize setting.
1288 * @param string $id Customize Setting ID.
1290 public function remove_setting( $id ) {
1291 unset( $this->settings[ $id ] );
1295 * Add a customize panel.
1298 * @since 4.5.0 Return added WP_Customize_Panel instance.
1301 * @param WP_Customize_Panel|string $id Customize Panel object, or Panel ID.
1302 * @param array $args Optional. Panel arguments. Default empty array.
1304 * @return WP_Customize_Panel The instance of the panel that was added.
1306 public function add_panel( $id, $args = array() ) {
1307 if ( $id instanceof WP_Customize_Panel ) {
1310 $panel = new WP_Customize_Panel( $this, $id, $args );
1313 $this->panels[ $panel->id ] = $panel;
1318 * Retrieve a customize panel.
1323 * @param string $id Panel ID to get.
1324 * @return WP_Customize_Panel|void Requested panel instance, if set.
1326 public function get_panel( $id ) {
1327 if ( isset( $this->panels[ $id ] ) ) {
1328 return $this->panels[ $id ];
1333 * Remove a customize panel.
1338 * @param string $id Panel ID to remove.
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.' ),
1346 '<a href="' . esc_url( 'https://developer.wordpress.org/reference/hooks/customize_loaded_components/' ) . '"><code>customize_loaded_components</code></a>'
1349 _doing_it_wrong( __METHOD__, $message, '4.5.0' );
1351 unset( $this->panels[ $id ] );
1355 * Register a customize panel type.
1357 * Registered types are eligible to be rendered via JS and created dynamically.
1362 * @see WP_Customize_Panel
1364 * @param string $panel Name of a custom panel which is a subclass of WP_Customize_Panel.
1366 public function register_panel_type( $panel ) {
1367 $this->registered_panel_types[] = $panel;
1371 * Render JS templates for all registered panel types.
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();
1384 * Add a customize section.
1387 * @since 4.5.0 Return added WP_Customize_Section instance.
1390 * @param WP_Customize_Section|string $id Customize Section object, or Section ID.
1391 * @param array $args Section arguments.
1393 * @return WP_Customize_Section The instance of the section that was added.
1395 public function add_section( $id, $args = array() ) {
1396 if ( $id instanceof WP_Customize_Section ) {
1399 $section = new WP_Customize_Section( $this, $id, $args );
1402 $this->sections[ $section->id ] = $section;
1407 * Retrieve a customize section.
1411 * @param string $id Section ID.
1412 * @return WP_Customize_Section|void The section, if set.
1414 public function get_section( $id ) {
1415 if ( isset( $this->sections[ $id ] ) )
1416 return $this->sections[ $id ];
1420 * Remove a customize section.
1424 * @param string $id Section ID.
1426 public function remove_section( $id ) {
1427 unset( $this->sections[ $id ] );
1431 * Register a customize section type.
1433 * Registered types are eligible to be rendered via JS and created dynamically.
1438 * @see WP_Customize_Section
1440 * @param string $section Name of a custom section which is a subclass of WP_Customize_Section.
1442 public function register_section_type( $section ) {
1443 $this->registered_section_types[] = $section;
1447 * Render JS templates for all registered section types.
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();
1460 * Add a customize control.
1463 * @since 4.5.0 Return added WP_Customize_Control instance.
1466 * @param WP_Customize_Control|string $id Customize Control object, or ID.
1467 * @param array $args Control arguments; passed to WP_Customize_Control
1469 * @return WP_Customize_Control The instance of the control that was added.
1471 public function add_control( $id, $args = array() ) {
1472 if ( $id instanceof WP_Customize_Control ) {
1475 $control = new WP_Customize_Control( $this, $id, $args );
1478 $this->controls[ $control->id ] = $control;
1483 * Retrieve a customize control.
1487 * @param string $id ID of the control.
1488 * @return WP_Customize_Control|void The control object, if set.
1490 public function get_control( $id ) {
1491 if ( isset( $this->controls[ $id ] ) )
1492 return $this->controls[ $id ];
1496 * Remove a customize control.
1500 * @param string $id ID of the control.
1502 public function remove_control( $id ) {
1503 unset( $this->controls[ $id ] );
1507 * Register a customize control type.
1509 * Registered types are eligible to be rendered via JS and created dynamically.
1514 * @param string $control Name of a custom control which is a subclass of
1515 * WP_Customize_Control.
1517 public function register_control_type( $control ) {
1518 $this->registered_control_types[] = $control;
1522 * Render JS templates for all registered control types.
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(),
1532 $control->print_template();
1535 <script type="text/html" id="tmpl-customize-control-notifications">
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>
1546 * Helper function to compare two objects by priority, ensuring sort stability via instance_number.
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.
1554 protected function _cmp_priority( $a, $b ) {
1555 if ( $a->priority === $b->priority ) {
1556 return $a->instance_number - $b->instance_number;
1558 return $a->priority - $b->priority;
1563 * Prepare panels, sections, and controls.
1565 * For each, check if required related components exist,
1566 * whether the user has the necessary capabilities,
1567 * and sort by priority.
1571 public function prepare_controls() {
1573 $controls = array();
1574 uasort( $this->controls, array( $this, '_cmp_priority' ) );
1576 foreach ( $this->controls as $id => $control ) {
1577 if ( ! isset( $this->sections[ $control->section ] ) || ! $control->check_capabilities() ) {
1581 $this->sections[ $control->section ]->controls[] = $control;
1582 $controls[ $id ] = $control;
1584 $this->controls = $controls;
1586 // Prepare sections.
1587 uasort( $this->sections, array( $this, '_cmp_priority' ) );
1588 $sections = array();
1590 foreach ( $this->sections as $section ) {
1591 if ( ! $section->check_capabilities() ) {
1595 usort( $section->controls, array( $this, '_cmp_priority' ) );
1597 if ( ! $section->panel ) {
1598 // Top-level section.
1599 $sections[ $section->id ] = $section;
1601 // This section belongs to a panel.
1602 if ( isset( $this->panels [ $section->panel ] ) ) {
1603 $this->panels[ $section->panel ]->sections[ $section->id ] = $section;
1607 $this->sections = $sections;
1610 uasort( $this->panels, array( $this, '_cmp_priority' ) );
1613 foreach ( $this->panels as $panel ) {
1614 if ( ! $panel->check_capabilities() ) {
1618 uasort( $panel->sections, array( $this, '_cmp_priority' ) );
1619 $panels[ $panel->id ] = $panel;
1621 $this->panels = $panels;
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' ) );
1629 * Enqueue scripts for customize controls.
1633 public function enqueue_control_scripts() {
1634 foreach ( $this->controls as $control ) {
1635 $control->enqueue();
1640 * Determine whether the user agent is iOS.
1645 * @return bool Whether the user agent is iOS.
1647 public function is_ios() {
1648 return wp_is_mobile() && preg_match( '/iPad|iPod|iPhone/', $_SERVER['HTTP_USER_AGENT'] );
1652 * Get the template string for the Customizer pane document title.
1657 * @return string The template string for the document title.
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' );
1664 /* translators: %s: document title from the preview */
1665 $document_title_tmpl = __( 'Live Preview: %s' );
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;
1672 * Set the initial URL to be previewed.
1679 * @param string $preview_url URL to be previewed.
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( '/' ) );
1687 * Get the initial URL to be previewed.
1692 * @return string URL being previewed.
1694 public function get_preview_url() {
1695 if ( empty( $this->preview_url ) ) {
1696 $preview_url = home_url( '/' );
1698 $preview_url = $this->preview_url;
1700 return $preview_url;
1704 * Set URL to link the user to when closing the Customizer.
1711 * @param string $return_url URL for return link.
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;
1721 * Get URL to link the user to when closing the Customizer.
1726 * @return string URL for link to close Customizer.
1728 public function get_return_url() {
1729 $referer = wp_get_referer();
1730 $excluded_referer_basenames = array( 'customize.php', 'wp-login.php' );
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;
1739 $return_url = home_url( '/' );
1745 * Set the autofocused constructs.
1750 * @param array $autofocus {
1751 * Mapping of 'panel', 'section', 'control' to the ID which should be autofocused.
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.
1758 public function set_autofocus( $autofocus ) {
1759 $this->autofocus = array_filter( wp_array_slice_assoc( $autofocus, array( 'panel', 'section', 'control' ) ), 'is_string' );
1763 * Get the autofocused constructs.
1769 * Mapping of 'panel', 'section', 'control' to the ID which should be autofocused.
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.
1776 public function get_autofocus() {
1777 return $this->autofocus;
1781 * Get nonces for the Customizer.
1784 * @return array Nonces.
1786 public function get_nonces() {
1788 'save' => wp_create_nonce( 'save-customize_' . $this->get_stylesheet() ),
1789 'preview' => wp_create_nonce( 'preview-customize_' . $this->get_stylesheet() ),
1793 * Filters nonces for Customizer.
1797 * @param array $nonces Array of refreshed nonces for save and
1799 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
1801 $nonces = apply_filters( 'customize_refresh_nonces', $nonces, $this );
1807 * Print JavaScript settings for parent window.
1811 public function customize_pane_settings() {
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.
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'] ) );
1826 if ( is_ssl() && ! $cross_domain ) {
1827 $allowed_urls[] = home_url( '/', 'https' );
1831 * Filters the list of URLs allowed to be clicked and followed in the Customizer preview.
1835 * @param array $allowed_urls An array of allowed URLs.
1837 $allowed_urls = array_unique( apply_filters( 'customize_allowed_urls', $allowed_urls ) );
1839 $login_url = add_query_arg( array(
1840 'interim-login' => 1,
1841 'customize-login' => 1,
1842 ), wp_login_url() );
1844 // Prepare Customizer settings to pass to JavaScript.
1847 'stylesheet' => $this->get_stylesheet(),
1848 'active' => $this->is_theme_active(),
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 ),
1861 'mobile' => wp_is_mobile(),
1862 'ios' => $this->is_ios(),
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(),
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();
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();
1892 <script type="text/javascript">
1893 var _wpCustomizeSettings = <?php echo wp_json_encode( $settings ); ?>;
1894 _wpCustomizeSettings.controls = {};
1895 _wpCustomizeSettings.settings = {};
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() ) {
1904 wp_json_encode( $setting->id ),
1905 wp_json_encode( $setting->json() )
1909 echo "})( _wpCustomizeSettings.settings );\n";
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() ) {
1917 wp_json_encode( $control->id ),
1918 wp_json_encode( $control->json() )
1922 echo "})( _wpCustomizeSettings.controls );\n";
1929 * Returns a list of devices to allow previewing.
1934 * @return array List of devices with labels and default setting.
1936 public function get_previewable_devices() {
1939 'label' => __( 'Enter desktop preview mode' ),
1943 'label' => __( 'Enter tablet preview mode' ),
1946 'label' => __( 'Enter mobile preview mode' ),
1951 * Filters the available devices to allow previewing in the Customizer.
1955 * @see WP_Customize_Manager::get_previewable_devices()
1957 * @param array $devices List of devices with labels and default setting.
1959 $devices = apply_filters( 'customize_previewable_devices', $devices );
1965 * Register some default controls.
1969 public function register_controls() {
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' );
1986 $this->add_section( new WP_Customize_Themes_Section( $this, 'themes', array(
1987 'title' => $this->theme()->display( 'Name' ),
1988 'capability' => 'switch_themes',
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',
1997 require_once( ABSPATH . 'wp-admin/includes/theme.php' );
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',
2013 $themes = wp_prepare_themes_for_js();
2014 foreach ( $themes as $theme ) {
2015 if ( $theme['active'] || $theme['id'] === $this->original_stylesheet ) {
2019 $theme_id = 'theme_' . $theme['id'];
2020 $theme['isActiveTheme'] = false;
2021 $this->add_control( new WP_Customize_Theme_Control( $this, $theme_id, array(
2023 'section' => 'themes',
2024 'settings' => 'active_theme',
2030 $this->add_section( 'title_tagline', array(
2031 'title' => __( 'Site Identity' ),
2035 $this->add_setting( 'blogname', array(
2036 'default' => get_option( 'blogname' ),
2038 'capability' => 'manage_options',
2041 $this->add_control( 'blogname', array(
2042 'label' => __( 'Site Title' ),
2043 'section' => 'title_tagline',
2046 $this->add_setting( 'blogdescription', array(
2047 'default' => get_option( 'blogdescription' ),
2049 'capability' => 'manage_options',
2052 $this->add_control( 'blogdescription', array(
2053 'label' => __( 'Tagline' ),
2054 'section' => 'title_tagline',
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' ),
2062 'sanitize_callback' => 'absint',
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',
2073 $this->add_setting( 'site_icon', array(
2075 'capability' => 'manage_options',
2076 'transport' => 'postMessage', // Previewed with JS in the Customizer controls window.
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>'
2086 'section' => 'title_tagline',
2092 $this->add_setting( 'custom_logo', array(
2093 'theme_supports' => array( 'custom-logo' ),
2094 'transport' => 'postMessage',
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',
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' ),
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,
2126 $this->add_section( 'colors', array(
2127 'title' => __( 'Colors' ),
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' ),
2135 'sanitize_callback' => array( $this, '_sanitize_header_textcolor' ),
2136 'sanitize_js_callback' => 'maybe_hash_hex_color',
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',
2149 $this->add_control( new WP_Customize_Color_Control( $this, 'header_textcolor', array(
2150 'label' => __( 'Header Text Color' ),
2151 'section' => 'colors',
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',
2160 'sanitize_callback' => 'sanitize_hex_color_no_hash',
2161 'sanitize_js_callback' => 'maybe_hash_hex_color',
2164 $this->add_control( new WP_Customize_Color_Control( $this, 'background_color', array(
2165 'label' => __( 'Background Color' ),
2166 'section' => 'colors',
2172 $this->add_section( 'header_image', array(
2173 'title' => __( 'Header Image' ),
2174 'theme_supports' => 'custom-header',
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',
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',
2188 $this->add_control( new WP_Customize_Header_Image_Control( $this ) );
2190 /* Custom Background */
2192 $this->add_section( 'background_image', array(
2193 'title' => __( 'Background Image' ),
2194 'theme_supports' => 'custom-background',
2198 $this->add_setting( 'background_image', array(
2199 'default' => get_theme_support( 'custom-background', 'default-image' ),
2200 'theme_supports' => 'custom-background',
2203 $this->add_setting( new WP_Customize_Background_Image_Setting( $this, 'background_image_thumb', array(
2204 'theme_supports' => 'custom-background',
2207 $this->add_control( new WP_Customize_Background_Image_Control( $this ) );
2209 $this->add_setting( 'background_repeat', array(
2210 'default' => get_theme_support( 'custom-background', 'default-repeat' ),
2211 'theme_supports' => 'custom-background',
2214 $this->add_control( 'background_repeat', array(
2215 'label' => __( 'Background Repeat' ),
2216 'section' => 'background_image',
2219 'no-repeat' => __('No Repeat'),
2220 'repeat' => __('Tile'),
2221 'repeat-x' => __('Tile Horizontally'),
2222 'repeat-y' => __('Tile Vertically'),
2226 $this->add_setting( 'background_position_x', array(
2227 'default' => get_theme_support( 'custom-background', 'default-position-x' ),
2228 'theme_supports' => 'custom-background',
2231 $this->add_control( 'background_position_x', array(
2232 'label' => __( 'Background Position' ),
2233 'section' => 'background_image',
2236 'left' => __('Left'),
2237 'center' => __('Center'),
2238 'right' => __('Right'),
2242 $this->add_setting( 'background_attachment', array(
2243 'default' => get_theme_support( 'custom-background', 'default-attachment' ),
2244 'theme_supports' => 'custom-background',
2247 $this->add_control( 'background_attachment', array(
2248 'label' => __( 'Background Attachment' ),
2249 'section' => 'background_image',
2252 'scroll' => __('Scroll'),
2253 'fixed' => __('Fixed'),
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';
2265 /* Static Front Page */
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',
2274 'description' => __( 'Your theme supports a static front page.' ),
2277 $this->add_setting( 'show_on_front', array(
2278 'default' => get_option( 'show_on_front' ),
2279 'capability' => 'manage_options',
2281 // 'theme_supports' => 'static-front-page',
2284 $this->add_control( 'show_on_front', array(
2285 'label' => __( 'Front page displays' ),
2286 'section' => 'static_front_page',
2289 'posts' => __( 'Your latest posts' ),
2290 'page' => __( 'A static page' ),
2294 $this->add_setting( 'page_on_front', array(
2296 'capability' => 'manage_options',
2297 // 'theme_supports' => 'static-front-page',
2300 $this->add_control( 'page_on_front', array(
2301 'label' => __( 'Front page' ),
2302 'section' => 'static_front_page',
2303 'type' => 'dropdown-pages',
2306 $this->add_setting( 'page_for_posts', array(
2308 'capability' => 'manage_options',
2309 // 'theme_supports' => 'static-front-page',
2312 $this->add_control( 'page_for_posts', array(
2313 'label' => __( 'Posts page' ),
2314 'section' => 'static_front_page',
2315 'type' => 'dropdown-pages',
2321 * Add settings from the POST data that were not added with code, e.g. dynamically-created settings for Widgets
2326 * @see add_dynamic_settings()
2328 public function register_dynamic_settings() {
2329 $this->add_dynamic_settings( array_keys( $this->unsanitized_post_values() ) );
2333 * Callback for validating the header_textcolor value.
2335 * Accepts 'blank', and otherwise uses sanitize_hex_color_no_hash().
2336 * Returns default text color if hex color is empty.
2340 * @param string $color
2343 public function _sanitize_header_textcolor( $color ) {
2344 if ( 'blank' === $color )
2347 $color = sanitize_hex_color_no_hash( $color );
2348 if ( empty( $color ) )
2349 $color = get_theme_support( 'custom-header', 'default-text-color' );
2355 * Callback for rendering the custom logo, used in the custom_logo partial.
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.
2363 * @see WP_Customize_Manager::register_controls()
2368 * @return string Custom logo.
2370 public function _render_custom_logo_partial() {
2371 return get_custom_logo();