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.
32 * The directory name of the previously active theme (within the theme_root).
36 protected $original_stylesheet;
39 * Whether this is a Customizer pageload.
43 protected $previewing = false;
46 * Methods and properties deailing with managing widgets in the Customizer.
48 * @var WP_Customize_Widgets
53 * Methods and properties deailing with managing nav menus in the Customizer.
55 * @var WP_Customize_Nav_Menus
59 protected $settings = array();
60 protected $containers = array();
61 protected $panels = array();
62 protected $sections = array();
63 protected $controls = array();
65 protected $nonce_tick;
67 protected $customized;
70 * Panel types that may be rendered from JS templates.
76 protected $registered_panel_types = array();
79 * Section types that may be rendered from JS templates.
85 protected $registered_section_types = array();
88 * Control types that may be rendered from JS templates.
94 protected $registered_control_types = array();
97 * Unsanitized values for Customize Settings parsed from $_POST['customized'].
101 private $_post_values;
108 public function __construct() {
109 require_once( ABSPATH . WPINC . '/class-wp-customize-setting.php' );
110 require_once( ABSPATH . WPINC . '/class-wp-customize-panel.php' );
111 require_once( ABSPATH . WPINC . '/class-wp-customize-section.php' );
112 require_once( ABSPATH . WPINC . '/class-wp-customize-control.php' );
113 require_once( ABSPATH . WPINC . '/class-wp-customize-widgets.php' );
114 require_once( ABSPATH . WPINC . '/class-wp-customize-nav-menus.php' );
116 $this->widgets = new WP_Customize_Widgets( $this );
117 $this->nav_menus = new WP_Customize_Nav_Menus( $this );
119 add_filter( 'wp_die_handler', array( $this, 'wp_die_handler' ) );
121 add_action( 'setup_theme', array( $this, 'setup_theme' ) );
122 add_action( 'wp_loaded', array( $this, 'wp_loaded' ) );
124 // Run wp_redirect_status late to make sure we override the status last.
125 add_action( 'wp_redirect_status', array( $this, 'wp_redirect_status' ), 1000 );
127 // Do not spawn cron (especially the alternate cron) while running the Customizer.
128 remove_action( 'init', 'wp_cron' );
130 // Do not run update checks when rendering the controls.
131 remove_action( 'admin_init', '_maybe_update_core' );
132 remove_action( 'admin_init', '_maybe_update_plugins' );
133 remove_action( 'admin_init', '_maybe_update_themes' );
135 add_action( 'wp_ajax_customize_save', array( $this, 'save' ) );
136 add_action( 'wp_ajax_customize_refresh_nonces', array( $this, 'refresh_nonces' ) );
138 add_action( 'customize_register', array( $this, 'register_controls' ) );
139 add_action( 'customize_register', array( $this, 'register_dynamic_settings' ), 11 ); // allow code to create settings first
140 add_action( 'customize_controls_init', array( $this, 'prepare_controls' ) );
141 add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_control_scripts' ) );
145 * Return true if it's an AJAX request.
148 * @since 4.2.0 Added `$action` param.
151 * @param string|null $action Whether the supplied AJAX action is being run.
152 * @return bool True if it's an AJAX request, false otherwise.
154 public function doing_ajax( $action = null ) {
155 $doing_ajax = ( defined( 'DOING_AJAX' ) && DOING_AJAX );
156 if ( ! $doing_ajax ) {
164 * Note: we can't just use doing_action( "wp_ajax_{$action}" ) because we need
165 * to check before admin-ajax.php gets to that point.
167 return isset( $_REQUEST['action'] ) && wp_unslash( $_REQUEST['action'] ) === $action;
172 * Custom wp_die wrapper. Returns either the standard message for UI
173 * or the AJAX message.
177 * @param mixed $ajax_message AJAX return
178 * @param mixed $message UI message
180 protected function wp_die( $ajax_message, $message = null ) {
181 if ( $this->doing_ajax() || isset( $_POST['customized'] ) ) {
182 wp_die( $ajax_message );
186 $message = __( 'Cheatin’ uh?' );
193 * Return the AJAX wp_die() handler if it's a customized request.
199 public function wp_die_handler() {
200 if ( $this->doing_ajax() || isset( $_POST['customized'] ) ) {
201 return '_ajax_wp_die_handler';
204 return '_default_wp_die_handler';
208 * Start preview and customize theme.
210 * Check if customize query variable exist. Init filters to filter the current theme.
214 public function setup_theme() {
215 send_origin_headers();
217 $doing_ajax_or_is_customized = ( $this->doing_ajax() || isset( $_POST['customized'] ) );
218 if ( is_admin() && ! $doing_ajax_or_is_customized ) {
220 } elseif ( $doing_ajax_or_is_customized && ! is_user_logged_in() ) {
224 show_admin_bar( false );
226 if ( ! current_user_can( 'customize' ) ) {
230 $this->original_stylesheet = get_stylesheet();
232 $this->theme = wp_get_theme( isset( $_REQUEST['theme'] ) ? $_REQUEST['theme'] : null );
234 if ( $this->is_theme_active() ) {
235 // Once the theme is loaded, we'll validate it.
236 add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ) );
238 // If the requested theme is not the active theme and the user doesn't have the
239 // switch_themes cap, bail.
240 if ( ! current_user_can( 'switch_themes' ) ) {
244 // If the theme has errors while loading, bail.
245 if ( $this->theme()->errors() ) {
249 // If the theme isn't allowed per multisite settings, bail.
250 if ( ! $this->theme()->is_allowed() ) {
255 $this->start_previewing_theme();
259 * Callback to validate a theme once it is loaded
263 public function after_setup_theme() {
264 $doing_ajax_or_is_customized = ( $this->doing_ajax() || isset( $_SERVER['customized'] ) );
265 if ( ! $doing_ajax_or_is_customized && ! validate_current_theme() ) {
266 wp_redirect( 'themes.php?broken=true' );
272 * If the theme to be previewed isn't the active theme, add filter callbacks
273 * to swap it out at runtime.
277 public function start_previewing_theme() {
278 // Bail if we're already previewing.
279 if ( $this->is_preview() ) {
283 $this->previewing = true;
285 if ( ! $this->is_theme_active() ) {
286 add_filter( 'template', array( $this, 'get_template' ) );
287 add_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
288 add_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
290 // @link: https://core.trac.wordpress.org/ticket/20027
291 add_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
292 add_filter( 'pre_option_template', array( $this, 'get_template' ) );
294 // Handle custom theme roots.
295 add_filter( 'pre_option_stylesheet_root', array( $this, 'get_stylesheet_root' ) );
296 add_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
300 * Fires once the Customizer theme preview has started.
304 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
306 do_action( 'start_previewing_theme', $this );
310 * Stop previewing the selected theme.
312 * Removes filters to change the current theme.
316 public function stop_previewing_theme() {
317 if ( ! $this->is_preview() ) {
321 $this->previewing = false;
323 if ( ! $this->is_theme_active() ) {
324 remove_filter( 'template', array( $this, 'get_template' ) );
325 remove_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
326 remove_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
328 // @link: https://core.trac.wordpress.org/ticket/20027
329 remove_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
330 remove_filter( 'pre_option_template', array( $this, 'get_template' ) );
332 // Handle custom theme roots.
333 remove_filter( 'pre_option_stylesheet_root', array( $this, 'get_stylesheet_root' ) );
334 remove_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
338 * Fires once the Customizer theme preview has stopped.
342 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
344 do_action( 'stop_previewing_theme', $this );
348 * Get the theme being customized.
354 public function theme() {
355 if ( ! $this->theme ) {
356 $this->theme = wp_get_theme();
362 * Get the registered settings.
368 public function settings() {
369 return $this->settings;
373 * Get the registered controls.
379 public function controls() {
380 return $this->controls;
384 * Get the registered containers.
390 public function containers() {
391 return $this->containers;
395 * Get the registered sections.
401 public function sections() {
402 return $this->sections;
406 * Get the registered panels.
411 * @return array Panels.
413 public function panels() {
414 return $this->panels;
418 * Checks if the current theme is active.
424 public function is_theme_active() {
425 return $this->get_stylesheet() == $this->original_stylesheet;
429 * Register styles/scripts and initialize the preview of each setting
433 public function wp_loaded() {
436 * Fires once WordPress has loaded, allowing scripts and styles to be initialized.
440 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
442 do_action( 'customize_register', $this );
444 if ( $this->is_preview() && ! is_admin() )
445 $this->customize_preview_init();
449 * Prevents AJAX requests from following redirects when previewing a theme
450 * by issuing a 200 response instead of a 30x.
452 * Instead, the JS will sniff out the location header.
459 public function wp_redirect_status( $status ) {
460 if ( $this->is_preview() && ! is_admin() )
467 * Parse the incoming $_POST['customized'] JSON data and store the unsanitized
468 * settings for subsequent post_value() lookups.
474 public function unsanitized_post_values() {
475 if ( ! isset( $this->_post_values ) ) {
476 if ( isset( $_POST['customized'] ) ) {
477 $this->_post_values = json_decode( wp_unslash( $_POST['customized'] ), true );
479 if ( empty( $this->_post_values ) ) { // if not isset or if JSON error
480 $this->_post_values = array();
483 if ( empty( $this->_post_values ) ) {
486 return $this->_post_values;
491 * Return the sanitized value for a given setting from the request's POST data.
494 * @since 4.1.1 Introduced 'default' parameter.
496 * @param WP_Customize_Setting $setting A WP_Customize_Setting derived object
497 * @param mixed $default value returned $setting has no post value (added in 4.2.0).
498 * @return string|mixed $post_value Sanitized value or the $default provided
500 public function post_value( $setting, $default = null ) {
501 $post_values = $this->unsanitized_post_values();
502 if ( array_key_exists( $setting->id, $post_values ) ) {
503 return $setting->sanitize( $post_values[ $setting->id ] );
510 * Override a setting's (unsanitized) value as found in any incoming $_POST['customized'].
515 * @param string $setting_id ID for the WP_Customize_Setting instance.
516 * @param mixed $value Post value.
518 public function set_post_value( $setting_id, $value ) {
519 $this->unsanitized_post_values();
520 $this->_post_values[ $setting_id ] = $value;
524 * Print JavaScript settings.
528 public function customize_preview_init() {
529 $this->nonce_tick = check_ajax_referer( 'preview-customize_' . $this->get_stylesheet(), 'nonce' );
531 $this->prepare_controls();
533 wp_enqueue_script( 'customize-preview' );
534 add_action( 'wp', array( $this, 'customize_preview_override_404_status' ) );
535 add_action( 'wp_head', array( $this, 'customize_preview_base' ) );
536 add_action( 'wp_head', array( $this, 'customize_preview_html5' ) );
537 add_action( 'wp_head', array( $this, 'customize_preview_loading_style' ) );
538 add_action( 'wp_footer', array( $this, 'customize_preview_settings' ), 20 );
539 add_action( 'shutdown', array( $this, 'customize_preview_signature' ), 1000 );
540 add_filter( 'wp_die_handler', array( $this, 'remove_preview_signature' ) );
542 foreach ( $this->settings as $setting ) {
547 * Fires once the Customizer preview has initialized and JavaScript
548 * settings have been printed.
552 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
554 do_action( 'customize_preview_init', $this );
558 * Prevent sending a 404 status when returning the response for the customize
559 * preview, since it causes the jQuery AJAX to fail. Send 200 instead.
564 public function customize_preview_override_404_status() {
566 status_header( 200 );
571 * Print base element for preview frame.
575 public function customize_preview_base() {
576 ?><base href="<?php echo home_url( '/' ); ?>" /><?php
580 * Print a workaround to handle HTML5 tags in IE < 9.
584 public function customize_preview_html5() { ?>
586 <script type="text/javascript">
587 var e = [ 'abbr', 'article', 'aside', 'audio', 'canvas', 'datalist', 'details',
588 'figure', 'footer', 'header', 'hgroup', 'mark', 'menu', 'meter', 'nav',
589 'output', 'progress', 'section', 'time', 'video' ];
590 for ( var i = 0; i < e.length; i++ ) {
591 document.createElement( e[i] );
598 * Print CSS for loading indicators for the Customizer preview.
603 public function customize_preview_loading_style() {
605 body.wp-customizer-unloading {
607 cursor: progress !important;
608 -webkit-transition: opacity 0.5s;
609 transition: opacity 0.5s;
611 body.wp-customizer-unloading * {
612 pointer-events: none !important;
618 * Print JavaScript settings for preview frame.
622 public function customize_preview_settings() {
625 'channel' => wp_unslash( $_POST['customize_messenger_channel'] ),
626 'activePanels' => array(),
627 'activeSections' => array(),
628 'activeControls' => array(),
630 'loading' => __( 'Loading ...' ),
634 if ( 2 == $this->nonce_tick ) {
635 $settings['nonce'] = array(
636 'save' => wp_create_nonce( 'save-customize_' . $this->get_stylesheet() ),
637 'preview' => wp_create_nonce( 'preview-customize_' . $this->get_stylesheet() )
641 foreach ( $this->settings as $id => $setting ) {
642 if ( $setting->check_capabilities() ) {
643 $settings['values'][ $id ] = $setting->js_value();
646 foreach ( $this->panels as $panel_id => $panel ) {
647 if ( $panel->check_capabilities() ) {
648 $settings['activePanels'][ $panel_id ] = $panel->active();
649 foreach ( $panel->sections as $section_id => $section ) {
650 if ( $section->check_capabilities() ) {
651 $settings['activeSections'][ $section_id ] = $section->active();
656 foreach ( $this->sections as $id => $section ) {
657 if ( $section->check_capabilities() ) {
658 $settings['activeSections'][ $id ] = $section->active();
661 foreach ( $this->controls as $id => $control ) {
662 if ( $control->check_capabilities() ) {
663 $settings['activeControls'][ $id ] = $control->active();
668 <script type="text/javascript">
669 var _wpCustomizeSettings = <?php echo wp_json_encode( $settings ); ?>;
675 * Prints a signature so we can ensure the Customizer was properly executed.
679 public function customize_preview_signature() {
680 echo 'WP_CUSTOMIZER_SIGNATURE';
684 * Removes the signature in case we experience a case where the Customizer was not properly executed.
690 public function remove_preview_signature( $return = null ) {
691 remove_action( 'shutdown', array( $this, 'customize_preview_signature' ), 1000 );
697 * Is it a theme preview?
701 * @return bool True if it's a preview, false if not.
703 public function is_preview() {
704 return (bool) $this->previewing;
708 * Retrieve the template name of the previewed theme.
712 * @return string Template name.
714 public function get_template() {
715 return $this->theme()->get_template();
719 * Retrieve the stylesheet name of the previewed theme.
723 * @return string Stylesheet name.
725 public function get_stylesheet() {
726 return $this->theme()->get_stylesheet();
730 * Retrieve the template root of the previewed theme.
734 * @return string Theme root.
736 public function get_template_root() {
737 return get_raw_theme_root( $this->get_template(), true );
741 * Retrieve the stylesheet root of the previewed theme.
745 * @return string Theme root.
747 public function get_stylesheet_root() {
748 return get_raw_theme_root( $this->get_stylesheet(), true );
752 * Filter the current theme and return the name of the previewed theme.
756 * @param $current_theme {@internal Parameter is not used}
757 * @return string Theme name.
759 public function current_theme( $current_theme ) {
760 return $this->theme()->display('Name');
764 * Switch the theme and trigger the save() method on each setting.
768 public function save() {
769 if ( ! $this->is_preview() ) {
770 wp_send_json_error( 'not_preview' );
773 $action = 'save-customize_' . $this->get_stylesheet();
774 if ( ! check_ajax_referer( $action, 'nonce', false ) ) {
775 wp_send_json_error( 'invalid_nonce' );
778 // Do we have to switch themes?
779 if ( ! $this->is_theme_active() ) {
780 // Temporarily stop previewing the theme to allow switch_themes()
781 // to operate properly.
782 $this->stop_previewing_theme();
783 switch_theme( $this->get_stylesheet() );
784 update_option( 'theme_switched_via_customizer', true );
785 $this->start_previewing_theme();
789 * Fires once the theme has switched in the Customizer, but before settings
794 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
796 do_action( 'customize_save', $this );
798 foreach ( $this->settings as $setting ) {
803 * Fires after Customize settings have been saved.
807 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
809 do_action( 'customize_save_after', $this );
812 * Filter response data for a successful customize_save AJAX request.
814 * This filter does not apply if there was a nonce or authentication failure.
818 * @param array $data Additional information passed back to the 'saved'
819 * event on `wp.customize`.
820 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
822 $response = apply_filters( 'customize_save_response', array(), $this );
823 wp_send_json_success( $response );
827 * Refresh nonces for the current preview.
831 public function refresh_nonces() {
832 if ( ! $this->is_preview() ) {
833 wp_send_json_error( 'not_preview' );
837 'save' => wp_create_nonce( 'save-customize_' . $this->get_stylesheet() ),
838 'preview' => wp_create_nonce( 'preview-customize_' . $this->get_stylesheet() ),
842 * Filter nonces for a customize_refresh_nonces AJAX request.
846 * @param array $nonces Array of refreshed nonces for save and
848 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
850 $nonces = apply_filters( 'customize_refresh_nonces', $nonces, $this );
851 wp_send_json_success( $nonces );
855 * Add a customize setting.
859 * @param WP_Customize_Setting|string $id Customize Setting object, or ID.
860 * @param array $args Setting arguments; passed to WP_Customize_Setting
863 public function add_setting( $id, $args = array() ) {
864 if ( $id instanceof WP_Customize_Setting ) {
867 $setting = new WP_Customize_Setting( $this, $id, $args );
869 $this->settings[ $setting->id ] = $setting;
873 * Register any dynamically-created settings, such as those from $_POST['customized']
874 * that have no corresponding setting created.
876 * This is a mechanism to "wake up" settings that have been dynamically created
877 * on the frontend and have been sent to WordPress in `$_POST['customized']`. When WP
878 * loads, the dynamically-created settings then will get created and previewed
879 * even though they are not directly created statically with code.
883 * @param array $setting_ids The setting IDs to add.
884 * @return WP_Customize_Setting The settings added.
886 public function add_dynamic_settings( $setting_ids ) {
887 $new_settings = array();
888 foreach ( $setting_ids as $setting_id ) {
889 // Skip settings already created
890 if ( $this->get_setting( $setting_id ) ) {
894 $setting_args = false;
895 $setting_class = 'WP_Customize_Setting';
898 * Filter a dynamic setting's constructor args.
900 * For a dynamic setting to be registered, this filter must be employed
901 * to override the default false value with an array of args to pass to
902 * the WP_Customize_Setting constructor.
906 * @param false|array $setting_args The arguments to the WP_Customize_Setting constructor.
907 * @param string $setting_id ID for dynamic setting, usually coming from `$_POST['customized']`.
909 $setting_args = apply_filters( 'customize_dynamic_setting_args', $setting_args, $setting_id );
910 if ( false === $setting_args ) {
915 * Allow non-statically created settings to be constructed with custom WP_Customize_Setting subclass.
919 * @param string $setting_class WP_Customize_Setting or a subclass.
920 * @param string $setting_id ID for dynamic setting, usually coming from `$_POST['customized']`.
921 * @param array $setting_args WP_Customize_Setting or a subclass.
923 $setting_class = apply_filters( 'customize_dynamic_setting_class', $setting_class, $setting_id, $setting_args );
925 $setting = new $setting_class( $this, $setting_id, $setting_args );
927 $this->add_setting( $setting );
928 $new_settings[] = $setting;
930 return $new_settings;
934 * Retrieve a customize setting.
938 * @param string $id Customize Setting ID.
939 * @return WP_Customize_Setting|void The setting, if set.
941 public function get_setting( $id ) {
942 if ( isset( $this->settings[ $id ] ) ) {
943 return $this->settings[ $id ];
948 * Remove a customize setting.
952 * @param string $id Customize Setting ID.
954 public function remove_setting( $id ) {
955 unset( $this->settings[ $id ] );
959 * Add a customize panel.
964 * @param WP_Customize_Panel|string $id Customize Panel object, or Panel ID.
965 * @param array $args Optional. Panel arguments. Default empty array.
967 public function add_panel( $id, $args = array() ) {
968 if ( $id instanceof WP_Customize_Panel ) {
971 $panel = new WP_Customize_Panel( $this, $id, $args );
974 $this->panels[ $panel->id ] = $panel;
978 * Retrieve a customize panel.
983 * @param string $id Panel ID to get.
984 * @return WP_Customize_Panel|void Requested panel instance, if set.
986 public function get_panel( $id ) {
987 if ( isset( $this->panels[ $id ] ) ) {
988 return $this->panels[ $id ];
993 * Remove a customize panel.
998 * @param string $id Panel ID to remove.
1000 public function remove_panel( $id ) {
1001 unset( $this->panels[ $id ] );
1005 * Register a customize panel type.
1007 * Registered types are eligible to be rendered via JS and created dynamically.
1012 * @see WP_Customize_Panel
1014 * @param string $panel Name of a custom panel which is a subclass of WP_Customize_Panel.
1016 public function register_panel_type( $panel ) {
1017 $this->registered_panel_types[] = $panel;
1021 * Render JS templates for all registered panel types.
1026 public function render_panel_templates() {
1027 foreach ( $this->registered_panel_types as $panel_type ) {
1028 $panel = new $panel_type( $this, 'temp', array() );
1029 $panel->print_template();
1034 * Add a customize section.
1038 * @param WP_Customize_Section|string $id Customize Section object, or Section ID.
1039 * @param array $args Section arguments.
1041 public function add_section( $id, $args = array() ) {
1042 if ( $id instanceof WP_Customize_Section ) {
1045 $section = new WP_Customize_Section( $this, $id, $args );
1047 $this->sections[ $section->id ] = $section;
1051 * Retrieve a customize section.
1055 * @param string $id Section ID.
1056 * @return WP_Customize_Section|void The section, if set.
1058 public function get_section( $id ) {
1059 if ( isset( $this->sections[ $id ] ) )
1060 return $this->sections[ $id ];
1064 * Remove a customize section.
1068 * @param string $id Section ID.
1070 public function remove_section( $id ) {
1071 unset( $this->sections[ $id ] );
1075 * Register a customize section type.
1077 * Registered types are eligible to be rendered via JS and created dynamically.
1082 * @see WP_Customize_Section
1084 * @param string $section Name of a custom section which is a subclass of WP_Customize_Section.
1086 public function register_section_type( $section ) {
1087 $this->registered_section_types[] = $section;
1091 * Render JS templates for all registered section types.
1096 public function render_section_templates() {
1097 foreach ( $this->registered_section_types as $section_type ) {
1098 $section = new $section_type( $this, 'temp', array() );
1099 $section->print_template();
1104 * Add a customize control.
1108 * @param WP_Customize_Control|string $id Customize Control object, or ID.
1109 * @param array $args Control arguments; passed to WP_Customize_Control
1112 public function add_control( $id, $args = array() ) {
1113 if ( $id instanceof WP_Customize_Control ) {
1116 $control = new WP_Customize_Control( $this, $id, $args );
1118 $this->controls[ $control->id ] = $control;
1122 * Retrieve a customize control.
1126 * @param string $id ID of the control.
1127 * @return WP_Customize_Control|void The control object, if set.
1129 public function get_control( $id ) {
1130 if ( isset( $this->controls[ $id ] ) )
1131 return $this->controls[ $id ];
1135 * Remove a customize control.
1139 * @param string $id ID of the control.
1141 public function remove_control( $id ) {
1142 unset( $this->controls[ $id ] );
1146 * Register a customize control type.
1148 * Registered types are eligible to be rendered via JS and created dynamically.
1153 * @param string $control Name of a custom control which is a subclass of
1154 * {@see WP_Customize_Control}.
1156 public function register_control_type( $control ) {
1157 $this->registered_control_types[] = $control;
1161 * Render JS templates for all registered control types.
1166 public function render_control_templates() {
1167 foreach ( $this->registered_control_types as $control_type ) {
1168 $control = new $control_type( $this, 'temp', array() );
1169 $control->print_template();
1174 * Helper function to compare two objects by priority, ensuring sort stability via instance_number.
1178 * @param WP_Customize_Panel|WP_Customize_Section|WP_Customize_Control $a Object A.
1179 * @param WP_Customize_Panel|WP_Customize_Section|WP_Customize_Control $b Object B.
1182 protected function _cmp_priority( $a, $b ) {
1183 if ( $a->priority === $b->priority ) {
1184 return $a->instance_number - $a->instance_number;
1186 return $a->priority - $b->priority;
1191 * Prepare panels, sections, and controls.
1193 * For each, check if required related components exist,
1194 * whether the user has the necessary capabilities,
1195 * and sort by priority.
1199 public function prepare_controls() {
1201 $controls = array();
1202 uasort( $this->controls, array( $this, '_cmp_priority' ) );
1204 foreach ( $this->controls as $id => $control ) {
1205 if ( ! isset( $this->sections[ $control->section ] ) || ! $control->check_capabilities() ) {
1209 $this->sections[ $control->section ]->controls[] = $control;
1210 $controls[ $id ] = $control;
1212 $this->controls = $controls;
1214 // Prepare sections.
1215 uasort( $this->sections, array( $this, '_cmp_priority' ) );
1216 $sections = array();
1218 foreach ( $this->sections as $section ) {
1219 if ( ! $section->check_capabilities() ) {
1223 usort( $section->controls, array( $this, '_cmp_priority' ) );
1225 if ( ! $section->panel ) {
1226 // Top-level section.
1227 $sections[ $section->id ] = $section;
1229 // This section belongs to a panel.
1230 if ( isset( $this->panels [ $section->panel ] ) ) {
1231 $this->panels[ $section->panel ]->sections[ $section->id ] = $section;
1235 $this->sections = $sections;
1238 uasort( $this->panels, array( $this, '_cmp_priority' ) );
1241 foreach ( $this->panels as $panel ) {
1242 if ( ! $panel->check_capabilities() ) {
1246 uasort( $panel->sections, array( $this, '_cmp_priority' ) );
1247 $panels[ $panel->id ] = $panel;
1249 $this->panels = $panels;
1251 // Sort panels and top-level sections together.
1252 $this->containers = array_merge( $this->panels, $this->sections );
1253 uasort( $this->containers, array( $this, '_cmp_priority' ) );
1257 * Enqueue scripts for customize controls.
1261 public function enqueue_control_scripts() {
1262 foreach ( $this->controls as $control ) {
1263 $control->enqueue();
1268 * Register some default controls.
1272 public function register_controls() {
1274 /* Panel, Section, and Control Types */
1275 $this->register_panel_type( 'WP_Customize_Panel' );
1276 $this->register_section_type( 'WP_Customize_Section' );
1277 $this->register_section_type( 'WP_Customize_Sidebar_Section' );
1278 $this->register_control_type( 'WP_Customize_Color_Control' );
1279 $this->register_control_type( 'WP_Customize_Media_Control' );
1280 $this->register_control_type( 'WP_Customize_Upload_Control' );
1281 $this->register_control_type( 'WP_Customize_Image_Control' );
1282 $this->register_control_type( 'WP_Customize_Background_Image_Control' );
1283 $this->register_control_type( 'WP_Customize_Cropped_Image_Control' );
1284 $this->register_control_type( 'WP_Customize_Site_Icon_Control' );
1285 $this->register_control_type( 'WP_Customize_Theme_Control' );
1289 $this->add_section( new WP_Customize_Themes_Section( $this, 'themes', array(
1290 'title' => $this->theme()->display( 'Name' ),
1291 'capability' => 'switch_themes',
1295 // Themes Setting (unused - the theme is considerably more fundamental to the Customizer experience).
1296 $this->add_setting( new WP_Customize_Filter_Setting( $this, 'active_theme', array(
1297 'capability' => 'switch_themes',
1300 require_once( ABSPATH . 'wp-admin/includes/theme.php' );
1304 // Add a control for the active/original theme.
1305 if ( ! $this->is_theme_active() ) {
1306 $themes = wp_prepare_themes_for_js( array( wp_get_theme( $this->original_stylesheet ) ) );
1307 $active_theme = current( $themes );
1308 $active_theme['isActiveTheme'] = true;
1309 $this->add_control( new WP_Customize_Theme_Control( $this, $active_theme['id'], array(
1310 'theme' => $active_theme,
1311 'section' => 'themes',
1312 'settings' => 'active_theme',
1316 $themes = wp_prepare_themes_for_js();
1317 foreach ( $themes as $theme ) {
1318 if ( $theme['active'] || $theme['id'] === $this->original_stylesheet ) {
1322 $theme_id = 'theme_' . $theme['id'];
1323 $theme['isActiveTheme'] = false;
1324 $this->add_control( new WP_Customize_Theme_Control( $this, $theme_id, array(
1326 'section' => 'themes',
1327 'settings' => 'active_theme',
1333 $this->add_section( 'title_tagline', array(
1334 'title' => __( 'Site Identity' ),
1338 $this->add_setting( 'blogname', array(
1339 'default' => get_option( 'blogname' ),
1341 'capability' => 'manage_options',
1344 $this->add_control( 'blogname', array(
1345 'label' => __( 'Site Title' ),
1346 'section' => 'title_tagline',
1349 $this->add_setting( 'blogdescription', array(
1350 'default' => get_option( 'blogdescription' ),
1352 'capability' => 'manage_options',
1355 $this->add_control( 'blogdescription', array(
1356 'label' => __( 'Tagline' ),
1357 'section' => 'title_tagline',
1360 $this->add_setting( 'site_icon', array(
1362 'capability' => 'manage_options',
1363 'transport' => 'postMessage', // Previewed with JS in the Customizer controls window.
1366 $this->add_control( new WP_Customize_Site_Icon_Control( $this, 'site_icon', array(
1367 'label' => __( 'Site Icon' ),
1368 'description' => __( 'The Site Icon is used as a browser and app icon for your site. Icons must be square, and at least 512px wide and tall.' ),
1369 'section' => 'title_tagline',
1377 $this->add_section( 'colors', array(
1378 'title' => __( 'Colors' ),
1382 $this->add_setting( 'header_textcolor', array(
1383 'theme_supports' => array( 'custom-header', 'header-text' ),
1384 'default' => get_theme_support( 'custom-header', 'default-text-color' ),
1386 'sanitize_callback' => array( $this, '_sanitize_header_textcolor' ),
1387 'sanitize_js_callback' => 'maybe_hash_hex_color',
1390 // Input type: checkbox
1391 // With custom value
1392 $this->add_control( 'display_header_text', array(
1393 'settings' => 'header_textcolor',
1394 'label' => __( 'Display Header Text' ),
1395 'section' => 'title_tagline',
1396 'type' => 'checkbox',
1400 $this->add_control( new WP_Customize_Color_Control( $this, 'header_textcolor', array(
1401 'label' => __( 'Header Text Color' ),
1402 'section' => 'colors',
1405 // Input type: Color
1406 // With sanitize_callback
1407 $this->add_setting( 'background_color', array(
1408 'default' => get_theme_support( 'custom-background', 'default-color' ),
1409 'theme_supports' => 'custom-background',
1411 'sanitize_callback' => 'sanitize_hex_color_no_hash',
1412 'sanitize_js_callback' => 'maybe_hash_hex_color',
1415 $this->add_control( new WP_Customize_Color_Control( $this, 'background_color', array(
1416 'label' => __( 'Background Color' ),
1417 'section' => 'colors',
1423 $this->add_section( 'header_image', array(
1424 'title' => __( 'Header Image' ),
1425 'theme_supports' => 'custom-header',
1429 $this->add_setting( new WP_Customize_Filter_Setting( $this, 'header_image', array(
1430 'default' => get_theme_support( 'custom-header', 'default-image' ),
1431 'theme_supports' => 'custom-header',
1434 $this->add_setting( new WP_Customize_Header_Image_Setting( $this, 'header_image_data', array(
1435 // 'default' => get_theme_support( 'custom-header', 'default-image' ),
1436 'theme_supports' => 'custom-header',
1439 $this->add_control( new WP_Customize_Header_Image_Control( $this ) );
1441 /* Custom Background */
1443 $this->add_section( 'background_image', array(
1444 'title' => __( 'Background Image' ),
1445 'theme_supports' => 'custom-background',
1449 $this->add_setting( 'background_image', array(
1450 'default' => get_theme_support( 'custom-background', 'default-image' ),
1451 'theme_supports' => 'custom-background',
1454 $this->add_setting( new WP_Customize_Background_Image_Setting( $this, 'background_image_thumb', array(
1455 'theme_supports' => 'custom-background',
1458 $this->add_control( new WP_Customize_Background_Image_Control( $this ) );
1460 $this->add_setting( 'background_repeat', array(
1461 'default' => get_theme_support( 'custom-background', 'default-repeat' ),
1462 'theme_supports' => 'custom-background',
1465 $this->add_control( 'background_repeat', array(
1466 'label' => __( 'Background Repeat' ),
1467 'section' => 'background_image',
1470 'no-repeat' => __('No Repeat'),
1471 'repeat' => __('Tile'),
1472 'repeat-x' => __('Tile Horizontally'),
1473 'repeat-y' => __('Tile Vertically'),
1477 $this->add_setting( 'background_position_x', array(
1478 'default' => get_theme_support( 'custom-background', 'default-position-x' ),
1479 'theme_supports' => 'custom-background',
1482 $this->add_control( 'background_position_x', array(
1483 'label' => __( 'Background Position' ),
1484 'section' => 'background_image',
1487 'left' => __('Left'),
1488 'center' => __('Center'),
1489 'right' => __('Right'),
1493 $this->add_setting( 'background_attachment', array(
1494 'default' => get_theme_support( 'custom-background', 'default-attachment' ),
1495 'theme_supports' => 'custom-background',
1498 $this->add_control( 'background_attachment', array(
1499 'label' => __( 'Background Attachment' ),
1500 'section' => 'background_image',
1503 'scroll' => __('Scroll'),
1504 'fixed' => __('Fixed'),
1508 // If the theme is using the default background callback, we can update
1509 // the background CSS using postMessage.
1510 if ( get_theme_support( 'custom-background', 'wp-head-callback' ) === '_custom_background_cb' ) {
1511 foreach ( array( 'color', 'image', 'position_x', 'repeat', 'attachment' ) as $prop ) {
1512 $this->get_setting( 'background_' . $prop )->transport = 'postMessage';
1516 /* Static Front Page */
1519 // Replicate behavior from options-reading.php and hide front page options if there are no pages
1520 if ( get_pages() ) {
1521 $this->add_section( 'static_front_page', array(
1522 'title' => __( 'Static Front Page' ),
1523 // 'theme_supports' => 'static-front-page',
1525 'description' => __( 'Your theme supports a static front page.' ),
1528 $this->add_setting( 'show_on_front', array(
1529 'default' => get_option( 'show_on_front' ),
1530 'capability' => 'manage_options',
1532 // 'theme_supports' => 'static-front-page',
1535 $this->add_control( 'show_on_front', array(
1536 'label' => __( 'Front page displays' ),
1537 'section' => 'static_front_page',
1540 'posts' => __( 'Your latest posts' ),
1541 'page' => __( 'A static page' ),
1545 $this->add_setting( 'page_on_front', array(
1547 'capability' => 'manage_options',
1548 // 'theme_supports' => 'static-front-page',
1551 $this->add_control( 'page_on_front', array(
1552 'label' => __( 'Front page' ),
1553 'section' => 'static_front_page',
1554 'type' => 'dropdown-pages',
1557 $this->add_setting( 'page_for_posts', array(
1559 'capability' => 'manage_options',
1560 // 'theme_supports' => 'static-front-page',
1563 $this->add_control( 'page_for_posts', array(
1564 'label' => __( 'Posts page' ),
1565 'section' => 'static_front_page',
1566 'type' => 'dropdown-pages',
1572 * Add settings from the POST data that were not added with code, e.g. dynamically-created settings for Widgets
1577 * @see add_dynamic_settings()
1579 public function register_dynamic_settings() {
1580 $this->add_dynamic_settings( array_keys( $this->unsanitized_post_values() ) );
1584 * Callback for validating the header_textcolor value.
1586 * Accepts 'blank', and otherwise uses sanitize_hex_color_no_hash().
1587 * Returns default text color if hex color is empty.
1591 * @param string $color
1594 public function _sanitize_header_textcolor( $color ) {
1595 if ( 'blank' === $color )
1598 $color = sanitize_hex_color_no_hash( $color );
1599 if ( empty( $color ) )
1600 $color = get_theme_support( 'custom-header', 'default-text-color' );
1607 * Sanitizes a hex color.
1609 * Returns either '', a 3 or 6 digit hex color (with #), or nothing.
1610 * For sanitizing values without a #, see sanitize_hex_color_no_hash().
1614 * @param string $color
1615 * @return string|void
1617 function sanitize_hex_color( $color ) {
1618 if ( '' === $color )
1621 // 3 or 6 hex digits, or the empty string.
1622 if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) )
1627 * Sanitizes a hex color without a hash. Use sanitize_hex_color() when possible.
1629 * Saving hex colors without a hash puts the burden of adding the hash on the
1630 * UI, which makes it difficult to use or upgrade to other color types such as
1631 * rgba, hsl, rgb, and html color names.
1633 * Returns either '', a 3 or 6 digit hex color (without a #), or null.
1637 * @param string $color
1638 * @return string|null
1640 function sanitize_hex_color_no_hash( $color ) {
1641 $color = ltrim( $color, '#' );
1643 if ( '' === $color )
1646 return sanitize_hex_color( '#' . $color ) ? $color : null;
1650 * Ensures that any hex color is properly hashed.
1651 * Otherwise, returns value untouched.
1653 * This method should only be necessary if using sanitize_hex_color_no_hash().
1657 * @param string $color
1660 function maybe_hash_hex_color( $color ) {
1661 if ( $unhashed = sanitize_hex_color_no_hash( $color ) )
1662 return '#' . $unhashed;