6 * @subpackage Customize
10 final class WP_Customize_Manager {
12 protected $original_stylesheet;
14 protected $previewing = false;
16 protected $settings = array();
17 protected $sections = array();
18 protected $controls = array();
20 protected $customized;
22 private $_post_values;
29 public function __construct() {
30 require( ABSPATH . WPINC . '/class-wp-customize-setting.php' );
31 require( ABSPATH . WPINC . '/class-wp-customize-section.php' );
32 require( ABSPATH . WPINC . '/class-wp-customize-control.php' );
34 add_filter( 'wp_die_handler', array( $this, 'wp_die_handler' ) );
36 add_action( 'setup_theme', array( $this, 'setup_theme' ) );
37 add_action( 'wp_loaded', array( $this, 'wp_loaded' ) );
39 // Run wp_redirect_status late to make sure we override the status last.
40 add_action( 'wp_redirect_status', array( $this, 'wp_redirect_status' ), 1000 );
42 // Do not spawn cron (especially the alternate cron) while running the customizer.
43 remove_action( 'init', 'wp_cron' );
45 // Do not run update checks when rendering the controls.
46 remove_action( 'admin_init', '_maybe_update_core' );
47 remove_action( 'admin_init', '_maybe_update_plugins' );
48 remove_action( 'admin_init', '_maybe_update_themes' );
50 add_action( 'wp_ajax_customize_save', array( $this, 'save' ) );
52 add_action( 'customize_register', array( $this, 'register_controls' ) );
53 add_action( 'customize_controls_init', array( $this, 'prepare_controls' ) );
54 add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_control_scripts' ) );
58 * Return true if it's an AJAX request.
62 public function doing_ajax() {
63 return isset( $_POST['customized'] ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX );
67 * Custom wp_die wrapper. Returns either the standard message for UI
68 * or the AJAX message.
70 * @param mixed $ajax_message AJAX return
71 * @param mixed $message UI message
75 protected function wp_die( $ajax_message, $message = null ) {
76 if ( $this->doing_ajax() )
77 wp_die( $ajax_message );
80 $message = __( 'Cheatin’ uh?' );
86 * Return the AJAX wp_die() handler if it's a customized request.
90 public function wp_die_handler() {
91 if ( $this->doing_ajax() )
92 return '_ajax_wp_die_handler';
94 return '_default_wp_die_handler';
97 * Start preview and customize theme.
99 * Check if customize query variable exist. Init filters to filter the current theme.
103 public function setup_theme() {
104 send_origin_headers();
106 if ( is_admin() && ! $this->doing_ajax() )
108 elseif ( $this->doing_ajax() && ! is_user_logged_in() )
111 show_admin_bar( false );
113 if ( ! current_user_can( 'edit_theme_options' ) )
116 $this->original_stylesheet = get_stylesheet();
118 $this->theme = wp_get_theme( isset( $_REQUEST['theme'] ) ? $_REQUEST['theme'] : null );
120 if ( $this->is_theme_active() ) {
121 // Once the theme is loaded, we'll validate it.
122 add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ) );
124 if ( ! current_user_can( 'switch_themes' ) )
127 // If the theme isn't active, you can't preview it if it is not allowed or has errors.
128 if ( $this->theme()->errors() )
131 if ( ! $this->theme()->is_allowed() )
135 $this->start_previewing_theme();
138 function after_setup_theme() {
139 if ( ! $this->doing_ajax() && ! validate_current_theme() ) {
140 wp_redirect( 'themes.php?broken=true' );
146 * Start previewing the selected theme.
148 * Adds filters to change the current theme.
152 public function start_previewing_theme() {
153 // Bail if we're already previewing.
154 if ( $this->is_preview() )
157 $this->previewing = true;
159 if ( ! $this->is_theme_active() ) {
160 add_filter( 'template', array( $this, 'get_template' ) );
161 add_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
162 add_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
164 // @link: http://core.trac.wordpress.org/ticket/20027
165 add_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
166 add_filter( 'pre_option_template', array( $this, 'get_template' ) );
168 // Handle custom theme roots.
169 add_filter( 'pre_option_stylesheet_root', array( $this, 'get_stylesheet_root' ) );
170 add_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
173 do_action( 'start_previewing_theme', $this );
177 * Stop previewing the selected theme.
179 * Removes filters to change the current theme.
183 public function stop_previewing_theme() {
184 if ( ! $this->is_preview() )
187 $this->previewing = false;
189 if ( ! $this->is_theme_active() ) {
190 remove_filter( 'template', array( $this, 'get_template' ) );
191 remove_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
192 remove_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
194 // @link: http://core.trac.wordpress.org/ticket/20027
195 remove_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
196 remove_filter( 'pre_option_template', array( $this, 'get_template' ) );
198 // Handle custom theme roots.
199 remove_filter( 'pre_option_stylesheet_root', array( $this, 'get_stylesheet_root' ) );
200 remove_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
203 do_action( 'stop_previewing_theme', $this );
207 * Get the theme being customized.
213 public function theme() {
218 * Get the registered settings.
224 public function settings() {
225 return $this->settings;
229 * Get the registered controls.
235 public function controls() {
236 return $this->controls;
240 * Get the registered sections.
246 public function sections() {
247 return $this->sections;
251 * Checks if the current theme is active.
257 public function is_theme_active() {
258 return $this->get_stylesheet() == $this->original_stylesheet;
262 * Register styles/scripts and initialize the preview of each setting
266 public function wp_loaded() {
267 do_action( 'customize_register', $this );
269 if ( $this->is_preview() && ! is_admin() )
270 $this->customize_preview_init();
274 * Prevents AJAX requests from following redirects when previewing a theme
275 * by issuing a 200 response instead of a 30x.
277 * Instead, the JS will sniff out the location header.
281 public function wp_redirect_status( $status ) {
282 if ( $this->is_preview() && ! is_admin() )
289 * Decode the $_POST attribute used to override the WP_Customize_Setting values.
293 public function post_value( $setting ) {
294 if ( ! isset( $this->_post_values ) ) {
295 if ( isset( $_POST['customized'] ) )
296 $this->_post_values = json_decode( stripslashes( $_POST['customized'] ), true );
298 $this->_post_values = false;
301 if ( isset( $this->_post_values[ $setting->id ] ) )
302 return $setting->sanitize( $this->_post_values[ $setting->id ] );
306 * Print javascript settings.
310 public function customize_preview_init() {
311 $this->prepare_controls();
313 wp_enqueue_script( 'customize-preview' );
314 add_action( 'wp_head', array( $this, 'customize_preview_base' ) );
315 add_action( 'wp_head', array( $this, 'customize_preview_html5' ) );
316 add_action( 'wp_footer', array( $this, 'customize_preview_settings' ), 20 );
317 add_action( 'shutdown', array( $this, 'customize_preview_signature' ), 1000 );
318 add_filter( 'wp_die_handler', array( $this, 'remove_preview_signature' ) );
320 foreach ( $this->settings as $setting ) {
324 do_action( 'customize_preview_init', $this );
328 * Print base element for preview frame.
332 public function customize_preview_base() {
333 ?><base href="<?php echo home_url( '/' ); ?>" /><?php
337 * Print a workaround to handle HTML5 tags in IE < 9
341 public function customize_preview_html5() { ?>
343 <script type="text/javascript">
344 var e = [ 'abbr', 'article', 'aside', 'audio', 'canvas', 'datalist', 'details',
345 'figure', 'footer', 'header', 'hgroup', 'mark', 'menu', 'meter', 'nav',
346 'output', 'progress', 'section', 'time', 'video' ];
347 for ( var i = 0; i < e.length; i++ ) {
348 document.createElement( e[i] );
355 * Print javascript settings for preview frame.
359 public function customize_preview_settings() {
362 'channel' => esc_js( $_POST['customize_messenger_channel'] ),
365 foreach ( $this->settings as $id => $setting ) {
366 $settings['values'][ $id ] = $setting->js_value();
370 <script type="text/javascript">
371 var _wpCustomizeSettings = <?php echo json_encode( $settings ); ?>;
377 * Prints a signature so we can ensure the customizer was properly executed.
381 public function customize_preview_signature() {
382 echo 'WP_CUSTOMIZER_SIGNATURE';
386 * Removes the signature in case we experience a case where the customizer was not properly executed.
390 public function remove_preview_signature( $return = null ) {
391 remove_action( 'shutdown', array( $this, 'customize_preview_signature' ), 1000 );
397 * Is it a theme preview?
401 * @return bool True if it's a preview, false if not.
403 public function is_preview() {
404 return (bool) $this->previewing;
408 * Retrieve the template name of the previewed theme.
412 * @return string Template name.
414 public function get_template() {
415 return $this->theme()->get_template();
419 * Retrieve the stylesheet name of the previewed theme.
423 * @return string Stylesheet name.
425 public function get_stylesheet() {
426 return $this->theme()->get_stylesheet();
430 * Retrieve the template root of the previewed theme.
434 * @return string Theme root.
436 public function get_template_root() {
437 return get_raw_theme_root( $this->get_template(), true );
441 * Retrieve the stylesheet root of the previewed theme.
445 * @return string Theme root.
447 public function get_stylesheet_root() {
448 return get_raw_theme_root( $this->get_stylesheet(), true );
452 * Filter the current theme and return the name of the previewed theme.
456 * @return string Theme name.
458 public function current_theme( $current_theme ) {
459 return $this->theme()->display('Name');
463 * Switch the theme and trigger the save action of each setting.
467 public function save() {
468 if ( ! $this->is_preview() )
471 check_ajax_referer( 'customize_controls-' . $this->get_stylesheet(), 'nonce' );
473 // Do we have to switch themes?
474 if ( ! $this->is_theme_active() ) {
475 // Temporarily stop previewing the theme to allow switch_themes()
476 // to operate properly.
477 $this->stop_previewing_theme();
478 switch_theme( $this->get_template(), $this->get_stylesheet() );
479 $this->start_previewing_theme();
482 do_action( 'customize_save', $this );
484 foreach ( $this->settings as $setting ) {
492 * Add a customize setting.
496 * @param string $id A specific ID of the setting. Can be a
497 * theme mod or option name.
498 * @param array $args Setting arguments.
500 public function add_setting( $id, $args = array() ) {
501 if ( is_a( $id, 'WP_Customize_Setting' ) )
504 $setting = new WP_Customize_Setting( $this, $id, $args );
506 $this->settings[ $setting->id ] = $setting;
510 * Retrieve a customize setting.
514 * @param string $id A specific ID of the setting.
515 * @return object The settings object.
517 public function get_setting( $id ) {
518 if ( isset( $this->settings[ $id ] ) )
519 return $this->settings[ $id ];
523 * Remove a customize setting.
527 * @param string $id A specific ID of the setting.
529 public function remove_setting( $id ) {
530 unset( $this->settings[ $id ] );
534 * Add a customize section.
538 * @param string $id A specific ID of the section.
539 * @param array $args Section arguments.
541 public function add_section( $id, $args = array() ) {
542 if ( is_a( $id, 'WP_Customize_Section' ) )
545 $section = new WP_Customize_Section( $this, $id, $args );
547 $this->sections[ $section->id ] = $section;
551 * Retrieve a customize section.
555 * @param string $id A specific ID of the section.
556 * @return object The section object.
558 public function get_section( $id ) {
559 if ( isset( $this->sections[ $id ] ) )
560 return $this->sections[ $id ];
564 * Remove a customize section.
568 * @param string $id A specific ID of the section.
570 public function remove_section( $id ) {
571 unset( $this->sections[ $id ] );
575 * Add a customize control.
579 * @param string $id A specific ID of the control.
580 * @param array $args Setting arguments.
582 public function add_control( $id, $args = array() ) {
583 if ( is_a( $id, 'WP_Customize_Control' ) )
586 $control = new WP_Customize_Control( $this, $id, $args );
588 $this->controls[ $control->id ] = $control;
592 * Retrieve a customize control.
596 * @param string $id A specific ID of the control.
597 * @return object The settings object.
599 public function get_control( $id ) {
600 if ( isset( $this->controls[ $id ] ) )
601 return $this->controls[ $id ];
605 * Remove a customize setting.
609 * @param string $id A specific ID of the control.
611 public function remove_control( $id ) {
612 unset( $this->controls[ $id ] );
616 * Helper function to compare two objects by priority.
620 * @param object $a Object A.
621 * @param object $b Object B.
623 protected final function _cmp_priority( $a, $b ) {
629 return ( $ap > $bp ) ? 1 : -1;
633 * Prepare settings and sections.
637 public function prepare_controls() {
639 // Reversing makes uasort sort by time added when conflicts occur.
641 $this->controls = array_reverse( $this->controls );
644 foreach ( $this->controls as $id => $control ) {
645 if ( ! isset( $this->sections[ $control->section ] ) || ! $control->check_capabilities() )
648 $this->sections[ $control->section ]->controls[] = $control;
649 $controls[ $id ] = $control;
651 $this->controls = $controls;
654 $this->sections = array_reverse( $this->sections );
655 uasort( $this->sections, array( $this, '_cmp_priority' ) );
658 foreach ( $this->sections as $section ) {
659 if ( ! $section->check_capabilities() || ! $section->controls )
662 usort( $section->controls, array( $this, '_cmp_priority' ) );
663 $sections[] = $section;
665 $this->sections = $sections;
669 * Enqueue scripts for customize controls.
673 public function enqueue_control_scripts() {
674 foreach ( $this->controls as $control ) {
680 * Register some default controls.
684 public function register_controls() {
686 /* Site Title & Tagline */
688 $this->add_section( 'title_tagline', array(
689 'title' => __( 'Site Title & Tagline' ),
693 $this->add_setting( 'blogname', array(
694 'default' => get_option( 'blogname' ),
696 'capability' => 'manage_options',
699 $this->add_control( 'blogname', array(
700 'label' => __( 'Site Title' ),
701 'section' => 'title_tagline',
704 $this->add_setting( 'blogdescription', array(
705 'default' => get_option( 'blogdescription' ),
707 'capability' => 'manage_options',
710 $this->add_control( 'blogdescription', array(
711 'label' => __( 'Tagline' ),
712 'section' => 'title_tagline',
717 $this->add_section( 'colors', array(
718 'title' => __( 'Colors' ),
722 $this->add_setting( 'header_textcolor', array(
723 'theme_supports' => array( 'custom-header', 'header-text' ),
724 'default' => get_theme_support( 'custom-header', 'default-text-color' ),
726 'sanitize_callback' => array( $this, '_sanitize_header_textcolor' ),
727 'sanitize_js_callback' => 'maybe_hash_hex_color',
730 // Input type: checkbox
732 $this->add_control( 'display_header_text', array(
733 'settings' => 'header_textcolor',
734 'label' => __( 'Display Header Text' ),
735 'section' => 'title_tagline',
736 'type' => 'checkbox',
739 $this->add_control( new WP_Customize_Color_Control( $this, 'header_textcolor', array(
740 'label' => __( 'Header Text Color' ),
741 'section' => 'colors',
745 // With sanitize_callback
746 $this->add_setting( 'background_color', array(
747 'default' => get_theme_support( 'custom-background', 'default-color' ),
748 'theme_supports' => 'custom-background',
750 'sanitize_callback' => 'sanitize_hex_color_no_hash',
751 'sanitize_js_callback' => 'maybe_hash_hex_color',
754 $this->add_control( new WP_Customize_Color_Control( $this, 'background_color', array(
755 'label' => __( 'Background Color' ),
756 'section' => 'colors',
762 $this->add_section( 'header_image', array(
763 'title' => __( 'Header Image' ),
764 'theme_supports' => 'custom-header',
768 $this->add_setting( new WP_Customize_Filter_Setting( $this, 'header_image', array(
769 'default' => get_theme_support( 'custom-header', 'default-image' ),
770 'theme_supports' => 'custom-header',
773 $this->add_setting( new WP_Customize_Header_Image_Setting( $this, 'header_image_data', array(
774 // 'default' => get_theme_support( 'custom-header', 'default-image' ),
775 'theme_supports' => 'custom-header',
778 $this->add_control( new WP_Customize_Header_Image_Control( $this ) );
780 /* Custom Background */
782 $this->add_section( 'background_image', array(
783 'title' => __( 'Background Image' ),
784 'theme_supports' => 'custom-background',
788 $this->add_setting( 'background_image', array(
789 'default' => get_theme_support( 'custom-background', 'default-image' ),
790 'theme_supports' => 'custom-background',
793 $this->add_setting( new WP_Customize_Background_Image_Setting( $this, 'background_image_thumb', array(
794 'theme_supports' => 'custom-background',
797 $this->add_control( new WP_Customize_Background_Image_Control( $this ) );
799 $this->add_setting( 'background_repeat', array(
800 'default' => 'repeat',
801 'theme_supports' => 'custom-background',
804 $this->add_control( 'background_repeat', array(
805 'label' => __( 'Background Repeat' ),
806 'section' => 'background_image',
809 'no-repeat' => __('No Repeat'),
810 'repeat' => __('Tile'),
811 'repeat-x' => __('Tile Horizontally'),
812 'repeat-y' => __('Tile Vertically'),
816 $this->add_setting( 'background_position_x', array(
818 'theme_supports' => 'custom-background',
821 $this->add_control( 'background_position_x', array(
822 'label' => __( 'Background Position' ),
823 'section' => 'background_image',
826 'left' => __('Left'),
827 'center' => __('Center'),
828 'right' => __('Right'),
832 $this->add_setting( 'background_attachment', array(
833 'default' => 'fixed',
834 'theme_supports' => 'custom-background',
837 $this->add_control( 'background_attachment', array(
838 'label' => __( 'Background Attachment' ),
839 'section' => 'background_image',
842 'fixed' => __('Fixed'),
843 'scroll' => __('Scroll'),
847 // If the theme is using the default background callback, we can update
848 // the background CSS using postMessage.
849 if ( get_theme_support( 'custom-background', 'wp-head-callback' ) === '_custom_background_cb' ) {
850 foreach ( array( 'color', 'image', 'position_x', 'repeat', 'attachment' ) as $prop ) {
851 $this->get_setting( 'background_' . $prop )->transport = 'postMessage';
857 $locations = get_registered_nav_menus();
858 $menus = wp_get_nav_menus();
859 $menu_locations = get_nav_menu_locations();
860 $num_locations = count( array_keys( $locations ) );
862 $this->add_section( 'nav', array(
863 'title' => __( 'Navigation' ),
864 'theme_supports' => 'menus',
866 'description' => sprintf( _n('Your theme supports %s menu. Select which menu you would like to use.', 'Your theme supports %s menus. Select which menu appears in each location.', $num_locations ), number_format_i18n( $num_locations ) ) . "\n\n" . __('You can edit your menu content on the Menus screen in the Appearance section.'),
870 $choices = array( 0 => __( '— Select —' ) );
871 foreach ( $menus as $menu ) {
872 $truncated_name = wp_html_excerpt( $menu->name, 40 );
873 $truncated_name = ( $truncated_name == $menu->name ) ? $menu->name : trim( $truncated_name ) . '…';
874 $choices[ $menu->term_id ] = $truncated_name;
877 foreach ( $locations as $location => $description ) {
878 $menu_setting_id = "nav_menu_locations[{$location}]";
880 $this->add_setting( $menu_setting_id, array(
881 'sanitize_callback' => 'absint',
882 'theme_supports' => 'menus',
885 $this->add_control( $menu_setting_id, array(
886 'label' => $description,
889 'choices' => $choices,
894 /* Static Front Page */
897 $this->add_section( 'static_front_page', array(
898 'title' => __( 'Static Front Page' ),
899 // 'theme_supports' => 'static-front-page',
901 'description' => __( 'Your theme supports a static front page.' ),
904 $this->add_setting( 'show_on_front', array(
905 'default' => get_option( 'show_on_front' ),
906 'capability' => 'manage_options',
908 // 'theme_supports' => 'static-front-page',
911 $this->add_control( 'show_on_front', array(
912 'label' => __( 'Front page displays' ),
913 'section' => 'static_front_page',
916 'posts' => __( 'Your latest posts' ),
917 'page' => __( 'A static page' ),
921 $this->add_setting( 'page_on_front', array(
923 'capability' => 'manage_options',
924 // 'theme_supports' => 'static-front-page',
927 $this->add_control( 'page_on_front', array(
928 'label' => __( 'Front page' ),
929 'section' => 'static_front_page',
930 'type' => 'dropdown-pages',
933 $this->add_setting( 'page_for_posts', array(
935 'capability' => 'manage_options',
936 // 'theme_supports' => 'static-front-page',
939 $this->add_control( 'page_for_posts', array(
940 'label' => __( 'Posts page' ),
941 'section' => 'static_front_page',
942 'type' => 'dropdown-pages',
947 * Callback for validating the header_textcolor value.
949 * Accepts 'blank', and otherwise uses sanitize_hex_color_no_hash().
953 public function _sanitize_header_textcolor( $color ) {
954 return ( 'blank' === $color ) ? 'blank' : sanitize_hex_color_no_hash( $color );
959 * Validates a hex color.
961 * Returns either '', a 3 or 6 digit hex color (with #), or null.
962 * For validating values without a #, see sanitize_hex_color_no_hash().
966 function sanitize_hex_color( $color ) {
970 // 3 or 6 hex digits, or the empty string.
971 if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) )
978 * Sanitizes a hex color without a hash. Use sanitize_hex_color() when possible.
980 * Saving hex colors without a hash puts the burden of adding the hash on the
981 * UI, which makes it difficult to use or upgrade to other color types such as
982 * rgba, hsl, rgb, and html color names.
984 * Returns either '', a 3 or 6 digit hex color (without a #), or null.
988 function sanitize_hex_color_no_hash( $color ) {
989 $color = ltrim( $color, '#' );
994 return sanitize_hex_color( '#' . $color ) ? $color : null;
998 * Ensures that any hex color is properly hashed.
999 * Otherwise, returns value untouched.
1001 * This method should only be necessary if using sanitize_hex_color_no_hash().
1005 function maybe_hash_hex_color( $color ) {
1006 if ( $unhashed = sanitize_hex_color_no_hash( $color ) )
1007 return '#' . $unhashed;