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 * Filter 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, __( 'You are not allowed to customize the appearance of 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, __( '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( $_SERVER['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 * Return the sanitized value for a given setting from the request's POST data.
657 * @since 4.1.1 Introduced 'default' parameter.
659 * @param WP_Customize_Setting $setting A WP_Customize_Setting derived object
660 * @param mixed $default value returned $setting has no post value (added in 4.2.0).
661 * @return string|mixed $post_value Sanitized value or the $default provided
663 public function post_value( $setting, $default = null ) {
664 $post_values = $this->unsanitized_post_values();
665 if ( array_key_exists( $setting->id, $post_values ) ) {
666 return $setting->sanitize( $post_values[ $setting->id ] );
673 * Override a setting's (unsanitized) value as found in any incoming $_POST['customized'].
678 * @param string $setting_id ID for the WP_Customize_Setting instance.
679 * @param mixed $value Post value.
681 public function set_post_value( $setting_id, $value ) {
682 $this->unsanitized_post_values();
683 $this->_post_values[ $setting_id ] = $value;
686 * Announce when a specific setting's unsanitized post value has been set.
688 * Fires when the {@see WP_Customize_Manager::set_post_value()} method is called.
690 * The dynamic portion of the hook name, `$setting_id`, refers to the setting ID.
694 * @param mixed $value Unsanitized setting post value.
695 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
697 do_action( "customize_post_value_set_{$setting_id}", $value, $this );
700 * Announce when any setting's unsanitized post value has been set.
702 * Fires when the {@see WP_Customize_Manager::set_post_value()} method is called.
704 * This is useful for `WP_Customize_Setting` instances to watch
705 * in order to update a cached previewed value.
709 * @param string $setting_id Setting ID.
710 * @param mixed $value Unsanitized setting post value.
711 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
713 do_action( 'customize_post_value_set', $setting_id, $value, $this );
717 * Print JavaScript settings.
721 public function customize_preview_init() {
722 $this->nonce_tick = check_ajax_referer( 'preview-customize_' . $this->get_stylesheet(), 'nonce' );
724 $this->prepare_controls();
726 wp_enqueue_script( 'customize-preview' );
727 add_action( 'wp', array( $this, 'customize_preview_override_404_status' ) );
728 add_action( 'wp_head', array( $this, 'customize_preview_base' ) );
729 add_action( 'wp_head', array( $this, 'customize_preview_html5' ) );
730 add_action( 'wp_head', array( $this, 'customize_preview_loading_style' ) );
731 add_action( 'wp_footer', array( $this, 'customize_preview_settings' ), 20 );
732 add_action( 'shutdown', array( $this, 'customize_preview_signature' ), 1000 );
733 add_filter( 'wp_die_handler', array( $this, 'remove_preview_signature' ) );
735 foreach ( $this->settings as $setting ) {
740 * Fires once the Customizer preview has initialized and JavaScript
741 * settings have been printed.
745 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
747 do_action( 'customize_preview_init', $this );
751 * Prevent sending a 404 status when returning the response for the customize
752 * preview, since it causes the jQuery AJAX to fail. Send 200 instead.
757 public function customize_preview_override_404_status() {
759 status_header( 200 );
764 * Print base element for preview frame.
768 public function customize_preview_base() {
769 ?><base href="<?php echo home_url( '/' ); ?>" /><?php
773 * Print a workaround to handle HTML5 tags in IE < 9.
777 public function customize_preview_html5() { ?>
779 <script type="text/javascript">
780 var e = [ 'abbr', 'article', 'aside', 'audio', 'canvas', 'datalist', 'details',
781 'figure', 'footer', 'header', 'hgroup', 'mark', 'menu', 'meter', 'nav',
782 'output', 'progress', 'section', 'time', 'video' ];
783 for ( var i = 0; i < e.length; i++ ) {
784 document.createElement( e[i] );
791 * Print CSS for loading indicators for the Customizer preview.
796 public function customize_preview_loading_style() {
798 body.wp-customizer-unloading {
800 cursor: progress !important;
801 -webkit-transition: opacity 0.5s;
802 transition: opacity 0.5s;
804 body.wp-customizer-unloading * {
805 pointer-events: none !important;
811 * Print JavaScript settings for preview frame.
815 public function customize_preview_settings() {
818 'stylesheet' => $this->get_stylesheet(),
819 'active' => $this->is_theme_active(),
822 'self' => empty( $_SERVER['REQUEST_URI'] ) ? home_url( '/' ) : esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ),
824 'channel' => wp_unslash( $_POST['customize_messenger_channel'] ),
825 'activePanels' => array(),
826 'activeSections' => array(),
827 'activeControls' => array(),
828 'nonce' => $this->get_nonces(),
830 'shiftClickToEdit' => __( 'Shift-click to edit this element.' ),
832 '_dirty' => array_keys( $this->unsanitized_post_values() ),
835 foreach ( $this->panels as $panel_id => $panel ) {
836 if ( $panel->check_capabilities() ) {
837 $settings['activePanels'][ $panel_id ] = $panel->active();
838 foreach ( $panel->sections as $section_id => $section ) {
839 if ( $section->check_capabilities() ) {
840 $settings['activeSections'][ $section_id ] = $section->active();
845 foreach ( $this->sections as $id => $section ) {
846 if ( $section->check_capabilities() ) {
847 $settings['activeSections'][ $id ] = $section->active();
850 foreach ( $this->controls as $id => $control ) {
851 if ( $control->check_capabilities() ) {
852 $settings['activeControls'][ $id ] = $control->active();
857 <script type="text/javascript">
858 var _wpCustomizeSettings = <?php echo wp_json_encode( $settings ); ?>;
859 _wpCustomizeSettings.values = {};
863 * Serialize settings separately from the initial _wpCustomizeSettings
864 * serialization in order to avoid a peak memory usage spike.
865 * @todo We may not even need to export the values at all since the pane syncs them anyway.
867 foreach ( $this->settings as $id => $setting ) {
868 if ( $setting->check_capabilities() ) {
871 wp_json_encode( $id ),
872 wp_json_encode( $setting->js_value() )
877 })( _wpCustomizeSettings.values );
883 * Prints a signature so we can ensure the Customizer was properly executed.
887 public function customize_preview_signature() {
888 echo 'WP_CUSTOMIZER_SIGNATURE';
892 * Removes the signature in case we experience a case where the Customizer was not properly executed.
896 * @param mixed $return Value passed through for wp_die_handler filter.
897 * @return mixed Value passed through for wp_die_handler filter.
899 public function remove_preview_signature( $return = null ) {
900 remove_action( 'shutdown', array( $this, 'customize_preview_signature' ), 1000 );
906 * Is it a theme preview?
910 * @return bool True if it's a preview, false if not.
912 public function is_preview() {
913 return (bool) $this->previewing;
917 * Retrieve the template name of the previewed theme.
921 * @return string Template name.
923 public function get_template() {
924 return $this->theme()->get_template();
928 * Retrieve the stylesheet name of the previewed theme.
932 * @return string Stylesheet name.
934 public function get_stylesheet() {
935 return $this->theme()->get_stylesheet();
939 * Retrieve the template root of the previewed theme.
943 * @return string Theme root.
945 public function get_template_root() {
946 return get_raw_theme_root( $this->get_template(), true );
950 * Retrieve the stylesheet root of the previewed theme.
954 * @return string Theme root.
956 public function get_stylesheet_root() {
957 return get_raw_theme_root( $this->get_stylesheet(), true );
961 * Filter the current theme and return the name of the previewed theme.
965 * @param $current_theme {@internal Parameter is not used}
966 * @return string Theme name.
968 public function current_theme( $current_theme ) {
969 return $this->theme()->display('Name');
973 * Switch the theme and trigger the save() method on each setting.
977 public function save() {
978 if ( ! $this->is_preview() ) {
979 wp_send_json_error( 'not_preview' );
982 $action = 'save-customize_' . $this->get_stylesheet();
983 if ( ! check_ajax_referer( $action, 'nonce', false ) ) {
984 wp_send_json_error( 'invalid_nonce' );
987 // Do we have to switch themes?
988 if ( ! $this->is_theme_active() ) {
989 // Temporarily stop previewing the theme to allow switch_themes()
990 // to operate properly.
991 $this->stop_previewing_theme();
992 switch_theme( $this->get_stylesheet() );
993 update_option( 'theme_switched_via_customizer', true );
994 $this->start_previewing_theme();
998 * Fires once the theme has switched in the Customizer, but before settings
1003 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
1005 do_action( 'customize_save', $this );
1007 foreach ( $this->settings as $setting ) {
1012 * Fires after Customize settings have been saved.
1016 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
1018 do_action( 'customize_save_after', $this );
1021 * Filter response data for a successful customize_save AJAX request.
1023 * This filter does not apply if there was a nonce or authentication failure.
1027 * @param array $data Additional information passed back to the 'saved'
1028 * event on `wp.customize`.
1029 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
1031 $response = apply_filters( 'customize_save_response', array(), $this );
1032 wp_send_json_success( $response );
1036 * Refresh nonces for the current preview.
1040 public function refresh_nonces() {
1041 if ( ! $this->is_preview() ) {
1042 wp_send_json_error( 'not_preview' );
1045 wp_send_json_success( $this->get_nonces() );
1049 * Add a customize setting.
1052 * @since 4.5.0 Return added WP_Customize_Setting instance.
1055 * @param WP_Customize_Setting|string $id Customize Setting object, or ID.
1056 * @param array $args Setting arguments; passed to WP_Customize_Setting
1058 * @return WP_Customize_Setting The instance of the setting that was added.
1060 public function add_setting( $id, $args = array() ) {
1061 if ( $id instanceof WP_Customize_Setting ) {
1064 $class = 'WP_Customize_Setting';
1066 /** This filter is documented in wp-includes/class-wp-customize-manager.php */
1067 $args = apply_filters( 'customize_dynamic_setting_args', $args, $id );
1069 /** This filter is documented in wp-includes/class-wp-customize-manager.php */
1070 $class = apply_filters( 'customize_dynamic_setting_class', $class, $id, $args );
1072 $setting = new $class( $this, $id, $args );
1075 $this->settings[ $setting->id ] = $setting;
1080 * Register any dynamically-created settings, such as those from $_POST['customized']
1081 * that have no corresponding setting created.
1083 * This is a mechanism to "wake up" settings that have been dynamically created
1084 * on the front end and have been sent to WordPress in `$_POST['customized']`. When WP
1085 * loads, the dynamically-created settings then will get created and previewed
1086 * even though they are not directly created statically with code.
1091 * @param array $setting_ids The setting IDs to add.
1092 * @return array The WP_Customize_Setting objects added.
1094 public function add_dynamic_settings( $setting_ids ) {
1095 $new_settings = array();
1096 foreach ( $setting_ids as $setting_id ) {
1097 // Skip settings already created
1098 if ( $this->get_setting( $setting_id ) ) {
1102 $setting_args = false;
1103 $setting_class = 'WP_Customize_Setting';
1106 * Filter a dynamic setting's constructor args.
1108 * For a dynamic setting to be registered, this filter must be employed
1109 * to override the default false value with an array of args to pass to
1110 * the WP_Customize_Setting constructor.
1114 * @param false|array $setting_args The arguments to the WP_Customize_Setting constructor.
1115 * @param string $setting_id ID for dynamic setting, usually coming from `$_POST['customized']`.
1117 $setting_args = apply_filters( 'customize_dynamic_setting_args', $setting_args, $setting_id );
1118 if ( false === $setting_args ) {
1123 * Allow non-statically created settings to be constructed with custom WP_Customize_Setting subclass.
1127 * @param string $setting_class WP_Customize_Setting or a subclass.
1128 * @param string $setting_id ID for dynamic setting, usually coming from `$_POST['customized']`.
1129 * @param array $setting_args WP_Customize_Setting or a subclass.
1131 $setting_class = apply_filters( 'customize_dynamic_setting_class', $setting_class, $setting_id, $setting_args );
1133 $setting = new $setting_class( $this, $setting_id, $setting_args );
1135 $this->add_setting( $setting );
1136 $new_settings[] = $setting;
1138 return $new_settings;
1142 * Retrieve a customize setting.
1146 * @param string $id Customize Setting ID.
1147 * @return WP_Customize_Setting|void The setting, if set.
1149 public function get_setting( $id ) {
1150 if ( isset( $this->settings[ $id ] ) ) {
1151 return $this->settings[ $id ];
1156 * Remove a customize setting.
1160 * @param string $id Customize Setting ID.
1162 public function remove_setting( $id ) {
1163 unset( $this->settings[ $id ] );
1167 * Add a customize panel.
1170 * @since 4.5.0 Return added WP_Customize_Panel instance.
1173 * @param WP_Customize_Panel|string $id Customize Panel object, or Panel ID.
1174 * @param array $args Optional. Panel arguments. Default empty array.
1176 * @return WP_Customize_Panel The instance of the panel that was added.
1178 public function add_panel( $id, $args = array() ) {
1179 if ( $id instanceof WP_Customize_Panel ) {
1182 $panel = new WP_Customize_Panel( $this, $id, $args );
1185 $this->panels[ $panel->id ] = $panel;
1190 * Retrieve a customize panel.
1195 * @param string $id Panel ID to get.
1196 * @return WP_Customize_Panel|void Requested panel instance, if set.
1198 public function get_panel( $id ) {
1199 if ( isset( $this->panels[ $id ] ) ) {
1200 return $this->panels[ $id ];
1205 * Remove a customize panel.
1210 * @param string $id Panel ID to remove.
1212 public function remove_panel( $id ) {
1213 // Removing core components this way is _doing_it_wrong().
1214 if ( in_array( $id, $this->components, true ) ) {
1215 /* translators: 1: panel id, 2: link to 'customize_loaded_components' filter reference */
1216 $message = sprintf( __( 'Removing %1$s manually will cause PHP warnings. Use the %2$s filter instead.' ),
1218 '<a href="' . esc_url( 'https://developer.wordpress.org/reference/hooks/customize_loaded_components/' ) . '"><code>customize_loaded_components</code></a>'
1221 _doing_it_wrong( __METHOD__, $message, '4.5' );
1223 unset( $this->panels[ $id ] );
1227 * Register a customize panel type.
1229 * Registered types are eligible to be rendered via JS and created dynamically.
1234 * @see WP_Customize_Panel
1236 * @param string $panel Name of a custom panel which is a subclass of WP_Customize_Panel.
1238 public function register_panel_type( $panel ) {
1239 $this->registered_panel_types[] = $panel;
1243 * Render JS templates for all registered panel types.
1248 public function render_panel_templates() {
1249 foreach ( $this->registered_panel_types as $panel_type ) {
1250 $panel = new $panel_type( $this, 'temp', array() );
1251 $panel->print_template();
1256 * Add a customize section.
1259 * @since 4.5.0 Return added WP_Customize_Section instance.
1262 * @param WP_Customize_Section|string $id Customize Section object, or Section ID.
1263 * @param array $args Section arguments.
1265 * @return WP_Customize_Section The instance of the section that was added.
1267 public function add_section( $id, $args = array() ) {
1268 if ( $id instanceof WP_Customize_Section ) {
1271 $section = new WP_Customize_Section( $this, $id, $args );
1274 $this->sections[ $section->id ] = $section;
1279 * Retrieve a customize section.
1283 * @param string $id Section ID.
1284 * @return WP_Customize_Section|void The section, if set.
1286 public function get_section( $id ) {
1287 if ( isset( $this->sections[ $id ] ) )
1288 return $this->sections[ $id ];
1292 * Remove a customize section.
1296 * @param string $id Section ID.
1298 public function remove_section( $id ) {
1299 unset( $this->sections[ $id ] );
1303 * Register a customize section type.
1305 * Registered types are eligible to be rendered via JS and created dynamically.
1310 * @see WP_Customize_Section
1312 * @param string $section Name of a custom section which is a subclass of WP_Customize_Section.
1314 public function register_section_type( $section ) {
1315 $this->registered_section_types[] = $section;
1319 * Render JS templates for all registered section types.
1324 public function render_section_templates() {
1325 foreach ( $this->registered_section_types as $section_type ) {
1326 $section = new $section_type( $this, 'temp', array() );
1327 $section->print_template();
1332 * Add a customize control.
1335 * @since 4.5.0 Return added WP_Customize_Control instance.
1338 * @param WP_Customize_Control|string $id Customize Control object, or ID.
1339 * @param array $args Control arguments; passed to WP_Customize_Control
1341 * @return WP_Customize_Control The instance of the control that was added.
1343 public function add_control( $id, $args = array() ) {
1344 if ( $id instanceof WP_Customize_Control ) {
1347 $control = new WP_Customize_Control( $this, $id, $args );
1350 $this->controls[ $control->id ] = $control;
1355 * Retrieve a customize control.
1359 * @param string $id ID of the control.
1360 * @return WP_Customize_Control|void The control object, if set.
1362 public function get_control( $id ) {
1363 if ( isset( $this->controls[ $id ] ) )
1364 return $this->controls[ $id ];
1368 * Remove a customize control.
1372 * @param string $id ID of the control.
1374 public function remove_control( $id ) {
1375 unset( $this->controls[ $id ] );
1379 * Register a customize control type.
1381 * Registered types are eligible to be rendered via JS and created dynamically.
1386 * @param string $control Name of a custom control which is a subclass of
1387 * {@see WP_Customize_Control}.
1389 public function register_control_type( $control ) {
1390 $this->registered_control_types[] = $control;
1394 * Render JS templates for all registered control types.
1399 public function render_control_templates() {
1400 foreach ( $this->registered_control_types as $control_type ) {
1401 $control = new $control_type( $this, 'temp', array(
1402 'settings' => array(),
1404 $control->print_template();
1409 * Helper function to compare two objects by priority, ensuring sort stability via instance_number.
1413 * @param WP_Customize_Panel|WP_Customize_Section|WP_Customize_Control $a Object A.
1414 * @param WP_Customize_Panel|WP_Customize_Section|WP_Customize_Control $b Object B.
1417 protected function _cmp_priority( $a, $b ) {
1418 if ( $a->priority === $b->priority ) {
1419 return $a->instance_number - $b->instance_number;
1421 return $a->priority - $b->priority;
1426 * Prepare panels, sections, and controls.
1428 * For each, check if required related components exist,
1429 * whether the user has the necessary capabilities,
1430 * and sort by priority.
1434 public function prepare_controls() {
1436 $controls = array();
1437 uasort( $this->controls, array( $this, '_cmp_priority' ) );
1439 foreach ( $this->controls as $id => $control ) {
1440 if ( ! isset( $this->sections[ $control->section ] ) || ! $control->check_capabilities() ) {
1444 $this->sections[ $control->section ]->controls[] = $control;
1445 $controls[ $id ] = $control;
1447 $this->controls = $controls;
1449 // Prepare sections.
1450 uasort( $this->sections, array( $this, '_cmp_priority' ) );
1451 $sections = array();
1453 foreach ( $this->sections as $section ) {
1454 if ( ! $section->check_capabilities() ) {
1458 usort( $section->controls, array( $this, '_cmp_priority' ) );
1460 if ( ! $section->panel ) {
1461 // Top-level section.
1462 $sections[ $section->id ] = $section;
1464 // This section belongs to a panel.
1465 if ( isset( $this->panels [ $section->panel ] ) ) {
1466 $this->panels[ $section->panel ]->sections[ $section->id ] = $section;
1470 $this->sections = $sections;
1473 uasort( $this->panels, array( $this, '_cmp_priority' ) );
1476 foreach ( $this->panels as $panel ) {
1477 if ( ! $panel->check_capabilities() ) {
1481 uasort( $panel->sections, array( $this, '_cmp_priority' ) );
1482 $panels[ $panel->id ] = $panel;
1484 $this->panels = $panels;
1486 // Sort panels and top-level sections together.
1487 $this->containers = array_merge( $this->panels, $this->sections );
1488 uasort( $this->containers, array( $this, '_cmp_priority' ) );
1492 * Enqueue scripts for customize controls.
1496 public function enqueue_control_scripts() {
1497 foreach ( $this->controls as $control ) {
1498 $control->enqueue();
1503 * Determine whether the user agent is iOS.
1508 * @return bool Whether the user agent is iOS.
1510 public function is_ios() {
1511 return wp_is_mobile() && preg_match( '/iPad|iPod|iPhone/', $_SERVER['HTTP_USER_AGENT'] );
1515 * Get the template string for the Customizer pane document title.
1520 * @return string The template string for the document title.
1522 public function get_document_title_template() {
1523 if ( $this->is_theme_active() ) {
1524 /* translators: %s: document title from the preview */
1525 $document_title_tmpl = __( 'Customize: %s' );
1527 /* translators: %s: document title from the preview */
1528 $document_title_tmpl = __( 'Live Preview: %s' );
1530 $document_title_tmpl = html_entity_decode( $document_title_tmpl, ENT_QUOTES, 'UTF-8' ); // Because exported to JS and assigned to document.title.
1531 return $document_title_tmpl;
1535 * Set the initial URL to be previewed.
1542 * @param string $preview_url URL to be previewed.
1544 public function set_preview_url( $preview_url ) {
1545 $this->preview_url = wp_validate_redirect( $preview_url, home_url( '/' ) );
1549 * Get the initial URL to be previewed.
1554 * @return string URL being previewed.
1556 public function get_preview_url() {
1557 if ( empty( $this->preview_url ) ) {
1558 $preview_url = home_url( '/' );
1560 $preview_url = $this->preview_url;
1562 return $preview_url;
1566 * Set URL to link the user to when closing the Customizer.
1573 * @param string $return_url URL for return link.
1575 public function set_return_url( $return_url ) {
1576 $return_url = remove_query_arg( wp_removable_query_args(), $return_url );
1577 $return_url = wp_validate_redirect( $return_url );
1578 $this->return_url = $return_url;
1582 * Get URL to link the user to when closing the Customizer.
1587 * @return string URL for link to close Customizer.
1589 public function get_return_url() {
1590 $referer = wp_get_referer();
1591 $excluded_referer_basenames = array( 'customize.php', 'wp-login.php' );
1593 if ( $this->return_url ) {
1594 $return_url = $this->return_url;
1595 } else if ( $referer && ! in_array( basename( parse_url( $referer, PHP_URL_PATH ) ), $excluded_referer_basenames, true ) ) {
1596 $return_url = $referer;
1597 } else if ( $this->preview_url ) {
1598 $return_url = $this->preview_url;
1600 $return_url = home_url( '/' );
1606 * Set the autofocused constructs.
1611 * @param array $autofocus {
1612 * Mapping of 'panel', 'section', 'control' to the ID which should be autofocused.
1614 * @type string [$control] ID for control to be autofocused.
1615 * @type string [$section] ID for section to be autofocused.
1616 * @type string [$panel] ID for panel to be autofocused.
1619 public function set_autofocus( $autofocus ) {
1620 $this->autofocus = array_filter( wp_array_slice_assoc( $autofocus, array( 'panel', 'section', 'control' ) ), 'is_string' );
1624 * Get the autofocused constructs.
1630 * Mapping of 'panel', 'section', 'control' to the ID which should be autofocused.
1632 * @type string [$control] ID for control to be autofocused.
1633 * @type string [$section] ID for section to be autofocused.
1634 * @type string [$panel] ID for panel to be autofocused.
1637 public function get_autofocus() {
1638 return $this->autofocus;
1642 * Get nonces for the Customizer.
1645 * @return array Nonces.
1647 public function get_nonces() {
1649 'save' => wp_create_nonce( 'save-customize_' . $this->get_stylesheet() ),
1650 'preview' => wp_create_nonce( 'preview-customize_' . $this->get_stylesheet() ),
1654 * Filter nonces for Customizer.
1658 * @param array $nonces Array of refreshed nonces for save and
1660 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
1662 $nonces = apply_filters( 'customize_refresh_nonces', $nonces, $this );
1668 * Print JavaScript settings for parent window.
1672 public function customize_pane_settings() {
1674 * If the front end and the admin are served from the same domain, load the
1675 * preview over ssl if the Customizer is being loaded over ssl. This avoids
1676 * insecure content warnings. This is not attempted if the admin and front end
1677 * are on different domains to avoid the case where the front end doesn't have
1678 * ssl certs. Domain mapping plugins can allow other urls in these conditions
1679 * using the customize_allowed_urls filter.
1682 $allowed_urls = array( home_url( '/' ) );
1683 $admin_origin = parse_url( admin_url() );
1684 $home_origin = parse_url( home_url() );
1685 $cross_domain = ( strtolower( $admin_origin['host'] ) !== strtolower( $home_origin['host'] ) );
1687 if ( is_ssl() && ! $cross_domain ) {
1688 $allowed_urls[] = home_url( '/', 'https' );
1692 * Filter the list of URLs allowed to be clicked and followed in the Customizer preview.
1696 * @param array $allowed_urls An array of allowed URLs.
1698 $allowed_urls = array_unique( apply_filters( 'customize_allowed_urls', $allowed_urls ) );
1700 $login_url = add_query_arg( array(
1701 'interim-login' => 1,
1702 'customize-login' => 1,
1703 ), wp_login_url() );
1705 // Prepare Customizer settings to pass to JavaScript.
1708 'stylesheet' => $this->get_stylesheet(),
1709 'active' => $this->is_theme_active(),
1712 'preview' => esc_url_raw( $this->get_preview_url() ),
1713 'parent' => esc_url_raw( admin_url() ),
1714 'activated' => esc_url_raw( home_url( '/' ) ),
1715 'ajax' => esc_url_raw( admin_url( 'admin-ajax.php', 'relative' ) ),
1716 'allowed' => array_map( 'esc_url_raw', $allowed_urls ),
1717 'isCrossDomain' => $cross_domain,
1718 'home' => esc_url_raw( home_url( '/' ) ),
1719 'login' => esc_url_raw( $login_url ),
1722 'mobile' => wp_is_mobile(),
1723 'ios' => $this->is_ios(),
1725 'panels' => array(),
1726 'sections' => array(),
1727 'nonce' => $this->get_nonces(),
1728 'autofocus' => $this->get_autofocus(),
1729 'documentTitleTmpl' => $this->get_document_title_template(),
1730 'previewableDevices' => $this->get_previewable_devices(),
1733 // Prepare Customize Section objects to pass to JavaScript.
1734 foreach ( $this->sections() as $id => $section ) {
1735 if ( $section->check_capabilities() ) {
1736 $settings['sections'][ $id ] = $section->json();
1740 // Prepare Customize Panel objects to pass to JavaScript.
1741 foreach ( $this->panels() as $panel_id => $panel ) {
1742 if ( $panel->check_capabilities() ) {
1743 $settings['panels'][ $panel_id ] = $panel->json();
1744 foreach ( $panel->sections as $section_id => $section ) {
1745 if ( $section->check_capabilities() ) {
1746 $settings['sections'][ $section_id ] = $section->json();
1753 <script type="text/javascript">
1754 var _wpCustomizeSettings = <?php echo wp_json_encode( $settings ); ?>;
1755 _wpCustomizeSettings.controls = {};
1756 _wpCustomizeSettings.settings = {};
1759 // Serialize settings one by one to improve memory usage.
1760 echo "(function ( s ){\n";
1761 foreach ( $this->settings() as $setting ) {
1762 if ( $setting->check_capabilities() ) {
1765 wp_json_encode( $setting->id ),
1766 wp_json_encode( array(
1767 'value' => $setting->js_value(),
1768 'transport' => $setting->transport,
1769 'dirty' => $setting->dirty,
1774 echo "})( _wpCustomizeSettings.settings );\n";
1776 // Serialize controls one by one to improve memory usage.
1777 echo "(function ( c ){\n";
1778 foreach ( $this->controls() as $control ) {
1779 if ( $control->check_capabilities() ) {
1782 wp_json_encode( $control->id ),
1783 wp_json_encode( $control->json() )
1787 echo "})( _wpCustomizeSettings.controls );\n";
1794 * Returns a list of devices to allow previewing.
1799 * @return array List of devices with labels and default setting.
1801 public function get_previewable_devices() {
1804 'label' => __( 'Enter desktop preview mode' ),
1808 'label' => __( 'Enter tablet preview mode' ),
1811 'label' => __( 'Enter mobile preview mode' ),
1816 * Filter the available devices to allow previewing in the Customizer.
1820 * @see WP_Customize_Manager::get_previewable_devices()
1822 * @param array $devices List of devices with labels and default setting.
1824 $devices = apply_filters( 'customize_previewable_devices', $devices );
1830 * Register some default controls.
1834 public function register_controls() {
1836 /* Panel, Section, and Control Types */
1837 $this->register_panel_type( 'WP_Customize_Panel' );
1838 $this->register_section_type( 'WP_Customize_Section' );
1839 $this->register_section_type( 'WP_Customize_Sidebar_Section' );
1840 $this->register_control_type( 'WP_Customize_Color_Control' );
1841 $this->register_control_type( 'WP_Customize_Media_Control' );
1842 $this->register_control_type( 'WP_Customize_Upload_Control' );
1843 $this->register_control_type( 'WP_Customize_Image_Control' );
1844 $this->register_control_type( 'WP_Customize_Background_Image_Control' );
1845 $this->register_control_type( 'WP_Customize_Cropped_Image_Control' );
1846 $this->register_control_type( 'WP_Customize_Site_Icon_Control' );
1847 $this->register_control_type( 'WP_Customize_Theme_Control' );
1851 $this->add_section( new WP_Customize_Themes_Section( $this, 'themes', array(
1852 'title' => $this->theme()->display( 'Name' ),
1853 'capability' => 'switch_themes',
1857 // Themes Setting (unused - the theme is considerably more fundamental to the Customizer experience).
1858 $this->add_setting( new WP_Customize_Filter_Setting( $this, 'active_theme', array(
1859 'capability' => 'switch_themes',
1862 require_once( ABSPATH . 'wp-admin/includes/theme.php' );
1866 // Add a control for the active/original theme.
1867 if ( ! $this->is_theme_active() ) {
1868 $themes = wp_prepare_themes_for_js( array( wp_get_theme( $this->original_stylesheet ) ) );
1869 $active_theme = current( $themes );
1870 $active_theme['isActiveTheme'] = true;
1871 $this->add_control( new WP_Customize_Theme_Control( $this, $active_theme['id'], array(
1872 'theme' => $active_theme,
1873 'section' => 'themes',
1874 'settings' => 'active_theme',
1878 $themes = wp_prepare_themes_for_js();
1879 foreach ( $themes as $theme ) {
1880 if ( $theme['active'] || $theme['id'] === $this->original_stylesheet ) {
1884 $theme_id = 'theme_' . $theme['id'];
1885 $theme['isActiveTheme'] = false;
1886 $this->add_control( new WP_Customize_Theme_Control( $this, $theme_id, array(
1888 'section' => 'themes',
1889 'settings' => 'active_theme',
1895 $this->add_section( 'title_tagline', array(
1896 'title' => __( 'Site Identity' ),
1900 $this->add_setting( 'blogname', array(
1901 'default' => get_option( 'blogname' ),
1903 'capability' => 'manage_options',
1906 $this->add_control( 'blogname', array(
1907 'label' => __( 'Site Title' ),
1908 'section' => 'title_tagline',
1911 $this->add_setting( 'blogdescription', array(
1912 'default' => get_option( 'blogdescription' ),
1914 'capability' => 'manage_options',
1917 $this->add_control( 'blogdescription', array(
1918 'label' => __( 'Tagline' ),
1919 'section' => 'title_tagline',
1922 // Add a setting to hide header text if the theme doesn't support custom headers.
1923 if ( ! current_theme_supports( 'custom-header', 'header-text' ) ) {
1924 $this->add_setting( 'header_text', array(
1925 'theme_supports' => array( 'custom-logo', 'header-text' ),
1927 'sanitize_callback' => 'absint',
1930 $this->add_control( 'header_text', array(
1931 'label' => __( 'Display Site Title and Tagline' ),
1932 'section' => 'title_tagline',
1933 'settings' => 'header_text',
1934 'type' => 'checkbox',
1938 $this->add_setting( 'site_icon', array(
1940 'capability' => 'manage_options',
1941 'transport' => 'postMessage', // Previewed with JS in the Customizer controls window.
1944 $this->add_control( new WP_Customize_Site_Icon_Control( $this, 'site_icon', array(
1945 'label' => __( 'Site Icon' ),
1946 'description' => sprintf(
1947 /* translators: %s: site icon size in pixels */
1948 __( 'The Site Icon is used as a browser and app icon for your site. Icons must be square, and at least %s pixels wide and tall.' ),
1949 '<strong>512</strong>'
1951 'section' => 'title_tagline',
1957 $this->add_setting( 'custom_logo', array(
1958 'theme_supports' => array( 'custom-logo' ),
1959 'transport' => 'postMessage',
1962 $custom_logo_args = get_theme_support( 'custom-logo' );
1963 $this->add_control( new WP_Customize_Cropped_Image_Control( $this, 'custom_logo', array(
1964 'label' => __( 'Logo' ),
1965 'section' => 'title_tagline',
1967 'height' => $custom_logo_args[0]['height'],
1968 'width' => $custom_logo_args[0]['width'],
1969 'flex_height' => $custom_logo_args[0]['flex-height'],
1970 'flex_width' => $custom_logo_args[0]['flex-width'],
1971 'button_labels' => array(
1972 'select' => __( 'Select logo' ),
1973 'change' => __( 'Change logo' ),
1974 'remove' => __( 'Remove' ),
1975 'default' => __( 'Default' ),
1976 'placeholder' => __( 'No logo selected' ),
1977 'frame_title' => __( 'Select logo' ),
1978 'frame_button' => __( 'Choose logo' ),
1982 $this->selective_refresh->add_partial( 'custom_logo', array(
1983 'settings' => array( 'custom_logo' ),
1984 'selector' => '.custom-logo-link',
1985 'render_callback' => array( $this, '_render_custom_logo_partial' ),
1986 'container_inclusive' => true,
1991 $this->add_section( 'colors', array(
1992 'title' => __( 'Colors' ),
1996 $this->add_setting( 'header_textcolor', array(
1997 'theme_supports' => array( 'custom-header', 'header-text' ),
1998 'default' => get_theme_support( 'custom-header', 'default-text-color' ),
2000 'sanitize_callback' => array( $this, '_sanitize_header_textcolor' ),
2001 'sanitize_js_callback' => 'maybe_hash_hex_color',
2004 // Input type: checkbox
2005 // With custom value
2006 $this->add_control( 'display_header_text', array(
2007 'settings' => 'header_textcolor',
2008 'label' => __( 'Display Site Title and Tagline' ),
2009 'section' => 'title_tagline',
2010 'type' => 'checkbox',
2014 $this->add_control( new WP_Customize_Color_Control( $this, 'header_textcolor', array(
2015 'label' => __( 'Header Text Color' ),
2016 'section' => 'colors',
2019 // Input type: Color
2020 // With sanitize_callback
2021 $this->add_setting( 'background_color', array(
2022 'default' => get_theme_support( 'custom-background', 'default-color' ),
2023 'theme_supports' => 'custom-background',
2025 'sanitize_callback' => 'sanitize_hex_color_no_hash',
2026 'sanitize_js_callback' => 'maybe_hash_hex_color',
2029 $this->add_control( new WP_Customize_Color_Control( $this, 'background_color', array(
2030 'label' => __( 'Background Color' ),
2031 'section' => 'colors',
2037 $this->add_section( 'header_image', array(
2038 'title' => __( 'Header Image' ),
2039 'theme_supports' => 'custom-header',
2043 $this->add_setting( new WP_Customize_Filter_Setting( $this, 'header_image', array(
2044 'default' => get_theme_support( 'custom-header', 'default-image' ),
2045 'theme_supports' => 'custom-header',
2048 $this->add_setting( new WP_Customize_Header_Image_Setting( $this, 'header_image_data', array(
2049 // 'default' => get_theme_support( 'custom-header', 'default-image' ),
2050 'theme_supports' => 'custom-header',
2053 $this->add_control( new WP_Customize_Header_Image_Control( $this ) );
2055 /* Custom Background */
2057 $this->add_section( 'background_image', array(
2058 'title' => __( 'Background Image' ),
2059 'theme_supports' => 'custom-background',
2063 $this->add_setting( 'background_image', array(
2064 'default' => get_theme_support( 'custom-background', 'default-image' ),
2065 'theme_supports' => 'custom-background',
2068 $this->add_setting( new WP_Customize_Background_Image_Setting( $this, 'background_image_thumb', array(
2069 'theme_supports' => 'custom-background',
2072 $this->add_control( new WP_Customize_Background_Image_Control( $this ) );
2074 $this->add_setting( 'background_repeat', array(
2075 'default' => get_theme_support( 'custom-background', 'default-repeat' ),
2076 'theme_supports' => 'custom-background',
2079 $this->add_control( 'background_repeat', array(
2080 'label' => __( 'Background Repeat' ),
2081 'section' => 'background_image',
2084 'no-repeat' => __('No Repeat'),
2085 'repeat' => __('Tile'),
2086 'repeat-x' => __('Tile Horizontally'),
2087 'repeat-y' => __('Tile Vertically'),
2091 $this->add_setting( 'background_position_x', array(
2092 'default' => get_theme_support( 'custom-background', 'default-position-x' ),
2093 'theme_supports' => 'custom-background',
2096 $this->add_control( 'background_position_x', array(
2097 'label' => __( 'Background Position' ),
2098 'section' => 'background_image',
2101 'left' => __('Left'),
2102 'center' => __('Center'),
2103 'right' => __('Right'),
2107 $this->add_setting( 'background_attachment', array(
2108 'default' => get_theme_support( 'custom-background', 'default-attachment' ),
2109 'theme_supports' => 'custom-background',
2112 $this->add_control( 'background_attachment', array(
2113 'label' => __( 'Background Attachment' ),
2114 'section' => 'background_image',
2117 'scroll' => __('Scroll'),
2118 'fixed' => __('Fixed'),
2122 // If the theme is using the default background callback, we can update
2123 // the background CSS using postMessage.
2124 if ( get_theme_support( 'custom-background', 'wp-head-callback' ) === '_custom_background_cb' ) {
2125 foreach ( array( 'color', 'image', 'position_x', 'repeat', 'attachment' ) as $prop ) {
2126 $this->get_setting( 'background_' . $prop )->transport = 'postMessage';
2130 /* Static Front Page */
2133 // Replicate behavior from options-reading.php and hide front page options if there are no pages
2134 if ( get_pages() ) {
2135 $this->add_section( 'static_front_page', array(
2136 'title' => __( 'Static Front Page' ),
2137 // 'theme_supports' => 'static-front-page',
2139 'description' => __( 'Your theme supports a static front page.' ),
2142 $this->add_setting( 'show_on_front', array(
2143 'default' => get_option( 'show_on_front' ),
2144 'capability' => 'manage_options',
2146 // 'theme_supports' => 'static-front-page',
2149 $this->add_control( 'show_on_front', array(
2150 'label' => __( 'Front page displays' ),
2151 'section' => 'static_front_page',
2154 'posts' => __( 'Your latest posts' ),
2155 'page' => __( 'A static page' ),
2159 $this->add_setting( 'page_on_front', array(
2161 'capability' => 'manage_options',
2162 // 'theme_supports' => 'static-front-page',
2165 $this->add_control( 'page_on_front', array(
2166 'label' => __( 'Front page' ),
2167 'section' => 'static_front_page',
2168 'type' => 'dropdown-pages',
2171 $this->add_setting( 'page_for_posts', array(
2173 'capability' => 'manage_options',
2174 // 'theme_supports' => 'static-front-page',
2177 $this->add_control( 'page_for_posts', array(
2178 'label' => __( 'Posts page' ),
2179 'section' => 'static_front_page',
2180 'type' => 'dropdown-pages',
2186 * Add settings from the POST data that were not added with code, e.g. dynamically-created settings for Widgets
2191 * @see add_dynamic_settings()
2193 public function register_dynamic_settings() {
2194 $this->add_dynamic_settings( array_keys( $this->unsanitized_post_values() ) );
2198 * Callback for validating the header_textcolor value.
2200 * Accepts 'blank', and otherwise uses sanitize_hex_color_no_hash().
2201 * Returns default text color if hex color is empty.
2205 * @param string $color
2208 public function _sanitize_header_textcolor( $color ) {
2209 if ( 'blank' === $color )
2212 $color = sanitize_hex_color_no_hash( $color );
2213 if ( empty( $color ) )
2214 $color = get_theme_support( 'custom-header', 'default-text-color' );
2220 * Callback for rendering the custom logo, used in the custom_logo partial.
2222 * This method exists because the partial object and context data are passed
2223 * into a partial's render_callback so we cannot use get_custom_logo() as
2224 * the render_callback directly since it expects a blog ID as the first
2225 * argument. When WP no longer supports PHP 5.3, this method can be removed
2226 * in favor of an anonymous function.
2228 * @see WP_Customize_Manager::register_controls()
2233 * @return string Custom logo.
2235 public function _render_custom_logo_partial() {
2236 return get_custom_logo();
2241 * Sanitizes a hex color.
2243 * Returns either '', a 3 or 6 digit hex color (with #), or nothing.
2244 * For sanitizing values without a #, see sanitize_hex_color_no_hash().
2248 * @param string $color
2249 * @return string|void
2251 function sanitize_hex_color( $color ) {
2252 if ( '' === $color )
2255 // 3 or 6 hex digits, or the empty string.
2256 if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) )
2261 * Sanitizes a hex color without a hash. Use sanitize_hex_color() when possible.
2263 * Saving hex colors without a hash puts the burden of adding the hash on the
2264 * UI, which makes it difficult to use or upgrade to other color types such as
2265 * rgba, hsl, rgb, and html color names.
2267 * Returns either '', a 3 or 6 digit hex color (without a #), or null.
2271 * @param string $color
2272 * @return string|null
2274 function sanitize_hex_color_no_hash( $color ) {
2275 $color = ltrim( $color, '#' );
2277 if ( '' === $color )
2280 return sanitize_hex_color( '#' . $color ) ? $color : null;
2284 * Ensures that any hex color is properly hashed.
2285 * Otherwise, returns value untouched.
2287 * This method should only be necessary if using sanitize_hex_color_no_hash().
2291 * @param string $color
2294 function maybe_hash_hex_color( $color ) {
2295 if ( $unhashed = sanitize_hex_color_no_hash( $color ) )
2296 return '#' . $unhashed;