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 $nonce_tick;
22 protected $customized;
24 private $_post_values;
31 public function __construct() {
32 require( ABSPATH . WPINC . '/class-wp-customize-setting.php' );
33 require( ABSPATH . WPINC . '/class-wp-customize-section.php' );
34 require( ABSPATH . WPINC . '/class-wp-customize-control.php' );
36 add_filter( 'wp_die_handler', array( $this, 'wp_die_handler' ) );
38 add_action( 'setup_theme', array( $this, 'setup_theme' ) );
39 add_action( 'wp_loaded', array( $this, 'wp_loaded' ) );
41 // Run wp_redirect_status late to make sure we override the status last.
42 add_action( 'wp_redirect_status', array( $this, 'wp_redirect_status' ), 1000 );
44 // Do not spawn cron (especially the alternate cron) while running the customizer.
45 remove_action( 'init', 'wp_cron' );
47 // Do not run update checks when rendering the controls.
48 remove_action( 'admin_init', '_maybe_update_core' );
49 remove_action( 'admin_init', '_maybe_update_plugins' );
50 remove_action( 'admin_init', '_maybe_update_themes' );
52 add_action( 'wp_ajax_customize_save', array( $this, 'save' ) );
54 add_action( 'customize_register', array( $this, 'register_controls' ) );
55 add_action( 'customize_controls_init', array( $this, 'prepare_controls' ) );
56 add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_control_scripts' ) );
60 * Return true if it's an AJAX request.
64 public function doing_ajax() {
65 return isset( $_POST['customized'] ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX );
69 * Custom wp_die wrapper. Returns either the standard message for UI
70 * or the AJAX message.
72 * @param mixed $ajax_message AJAX return
73 * @param mixed $message UI message
77 protected function wp_die( $ajax_message, $message = null ) {
78 if ( $this->doing_ajax() )
79 wp_die( $ajax_message );
82 $message = __( 'Cheatin’ uh?' );
88 * Return the AJAX wp_die() handler if it's a customized request.
92 public function wp_die_handler() {
93 if ( $this->doing_ajax() )
94 return '_ajax_wp_die_handler';
96 return '_default_wp_die_handler';
99 * Start preview and customize theme.
101 * Check if customize query variable exist. Init filters to filter the current theme.
105 public function setup_theme() {
106 send_origin_headers();
108 if ( is_admin() && ! $this->doing_ajax() )
110 elseif ( $this->doing_ajax() && ! is_user_logged_in() )
113 show_admin_bar( false );
115 if ( ! current_user_can( 'edit_theme_options' ) )
118 $this->original_stylesheet = get_stylesheet();
120 $this->theme = wp_get_theme( isset( $_REQUEST['theme'] ) ? $_REQUEST['theme'] : null );
122 if ( $this->is_theme_active() ) {
123 // Once the theme is loaded, we'll validate it.
124 add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ) );
126 if ( ! current_user_can( 'switch_themes' ) )
129 // If the theme isn't active, you can't preview it if it is not allowed or has errors.
130 if ( $this->theme()->errors() )
133 if ( ! $this->theme()->is_allowed() )
137 $this->start_previewing_theme();
140 function after_setup_theme() {
141 if ( ! $this->doing_ajax() && ! validate_current_theme() ) {
142 wp_redirect( 'themes.php?broken=true' );
148 * Start previewing the selected theme.
150 * Adds filters to change the current theme.
154 public function start_previewing_theme() {
155 // Bail if we're already previewing.
156 if ( $this->is_preview() )
159 $this->previewing = true;
161 if ( ! $this->is_theme_active() ) {
162 add_filter( 'template', array( $this, 'get_template' ) );
163 add_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
164 add_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
166 // @link: http://core.trac.wordpress.org/ticket/20027
167 add_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
168 add_filter( 'pre_option_template', array( $this, 'get_template' ) );
170 // Handle custom theme roots.
171 add_filter( 'pre_option_stylesheet_root', array( $this, 'get_stylesheet_root' ) );
172 add_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
175 do_action( 'start_previewing_theme', $this );
179 * Stop previewing the selected theme.
181 * Removes filters to change the current theme.
185 public function stop_previewing_theme() {
186 if ( ! $this->is_preview() )
189 $this->previewing = false;
191 if ( ! $this->is_theme_active() ) {
192 remove_filter( 'template', array( $this, 'get_template' ) );
193 remove_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
194 remove_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
196 // @link: http://core.trac.wordpress.org/ticket/20027
197 remove_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
198 remove_filter( 'pre_option_template', array( $this, 'get_template' ) );
200 // Handle custom theme roots.
201 remove_filter( 'pre_option_stylesheet_root', array( $this, 'get_stylesheet_root' ) );
202 remove_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
205 do_action( 'stop_previewing_theme', $this );
209 * Get the theme being customized.
215 public function theme() {
220 * Get the registered settings.
226 public function settings() {
227 return $this->settings;
231 * Get the registered controls.
237 public function controls() {
238 return $this->controls;
242 * Get the registered sections.
248 public function sections() {
249 return $this->sections;
253 * Checks if the current theme is active.
259 public function is_theme_active() {
260 return $this->get_stylesheet() == $this->original_stylesheet;
264 * Register styles/scripts and initialize the preview of each setting
268 public function wp_loaded() {
269 do_action( 'customize_register', $this );
271 if ( $this->is_preview() && ! is_admin() )
272 $this->customize_preview_init();
276 * Prevents AJAX requests from following redirects when previewing a theme
277 * by issuing a 200 response instead of a 30x.
279 * Instead, the JS will sniff out the location header.
283 public function wp_redirect_status( $status ) {
284 if ( $this->is_preview() && ! is_admin() )
291 * Decode the $_POST attribute used to override the WP_Customize_Setting values.
295 public function post_value( $setting ) {
296 if ( ! isset( $this->_post_values ) ) {
297 if ( isset( $_POST['customized'] ) )
298 $this->_post_values = json_decode( stripslashes( $_POST['customized'] ), true );
300 $this->_post_values = false;
303 if ( isset( $this->_post_values[ $setting->id ] ) )
304 return $setting->sanitize( $this->_post_values[ $setting->id ] );
308 * Print javascript settings.
312 public function customize_preview_init() {
313 $this->nonce_tick = check_ajax_referer( 'preview-customize_' . $this->get_stylesheet(), 'nonce' );
315 $this->prepare_controls();
317 wp_enqueue_script( 'customize-preview' );
318 add_action( 'wp_head', array( $this, 'customize_preview_base' ) );
319 add_action( 'wp_head', array( $this, 'customize_preview_html5' ) );
320 add_action( 'wp_footer', array( $this, 'customize_preview_settings' ), 20 );
321 add_action( 'shutdown', array( $this, 'customize_preview_signature' ), 1000 );
322 add_filter( 'wp_die_handler', array( $this, 'remove_preview_signature' ) );
324 foreach ( $this->settings as $setting ) {
328 do_action( 'customize_preview_init', $this );
332 * Print base element for preview frame.
336 public function customize_preview_base() {
337 ?><base href="<?php echo home_url( '/' ); ?>" /><?php
341 * Print a workaround to handle HTML5 tags in IE < 9
345 public function customize_preview_html5() { ?>
347 <script type="text/javascript">
348 var e = [ 'abbr', 'article', 'aside', 'audio', 'canvas', 'datalist', 'details',
349 'figure', 'footer', 'header', 'hgroup', 'mark', 'menu', 'meter', 'nav',
350 'output', 'progress', 'section', 'time', 'video' ];
351 for ( var i = 0; i < e.length; i++ ) {
352 document.createElement( e[i] );
359 * Print javascript settings for preview frame.
363 public function customize_preview_settings() {
366 'channel' => esc_js( $_POST['customize_messenger_channel'] ),
369 if ( 2 == $this->nonce_tick ) {
370 $settings['nonce'] = array(
371 'save' => wp_create_nonce( 'save-customize_' . $this->get_stylesheet() ),
372 'preview' => wp_create_nonce( 'preview-customize_' . $this->get_stylesheet() )
376 foreach ( $this->settings as $id => $setting ) {
377 $settings['values'][ $id ] = $setting->js_value();
381 <script type="text/javascript">
382 var _wpCustomizeSettings = <?php echo json_encode( $settings ); ?>;
388 * Prints a signature so we can ensure the customizer was properly executed.
392 public function customize_preview_signature() {
393 echo 'WP_CUSTOMIZER_SIGNATURE';
397 * Removes the signature in case we experience a case where the customizer was not properly executed.
401 public function remove_preview_signature( $return = null ) {
402 remove_action( 'shutdown', array( $this, 'customize_preview_signature' ), 1000 );
408 * Is it a theme preview?
412 * @return bool True if it's a preview, false if not.
414 public function is_preview() {
415 return (bool) $this->previewing;
419 * Retrieve the template name of the previewed theme.
423 * @return string Template name.
425 public function get_template() {
426 return $this->theme()->get_template();
430 * Retrieve the stylesheet name of the previewed theme.
434 * @return string Stylesheet name.
436 public function get_stylesheet() {
437 return $this->theme()->get_stylesheet();
441 * Retrieve the template root of the previewed theme.
445 * @return string Theme root.
447 public function get_template_root() {
448 return get_raw_theme_root( $this->get_template(), true );
452 * Retrieve the stylesheet root of the previewed theme.
456 * @return string Theme root.
458 public function get_stylesheet_root() {
459 return get_raw_theme_root( $this->get_stylesheet(), true );
463 * Filter the current theme and return the name of the previewed theme.
467 * @return string Theme name.
469 public function current_theme( $current_theme ) {
470 return $this->theme()->display('Name');
474 * Switch the theme and trigger the save action of each setting.
478 public function save() {
479 if ( ! $this->is_preview() )
482 check_ajax_referer( 'save-customize_' . $this->get_stylesheet(), 'nonce' );
484 // Do we have to switch themes?
485 if ( ! $this->is_theme_active() ) {
486 // Temporarily stop previewing the theme to allow switch_themes()
487 // to operate properly.
488 $this->stop_previewing_theme();
489 switch_theme( $this->get_template(), $this->get_stylesheet() );
490 $this->start_previewing_theme();
493 do_action( 'customize_save', $this );
495 foreach ( $this->settings as $setting ) {
503 * Add a customize setting.
507 * @param string $id A specific ID of the setting. Can be a
508 * theme mod or option name.
509 * @param array $args Setting arguments.
511 public function add_setting( $id, $args = array() ) {
512 if ( is_a( $id, 'WP_Customize_Setting' ) )
515 $setting = new WP_Customize_Setting( $this, $id, $args );
517 $this->settings[ $setting->id ] = $setting;
521 * Retrieve a customize setting.
525 * @param string $id A specific ID of the setting.
526 * @return object The settings object.
528 public function get_setting( $id ) {
529 if ( isset( $this->settings[ $id ] ) )
530 return $this->settings[ $id ];
534 * Remove a customize setting.
538 * @param string $id A specific ID of the setting.
540 public function remove_setting( $id ) {
541 unset( $this->settings[ $id ] );
545 * Add a customize section.
549 * @param string $id A specific ID of the section.
550 * @param array $args Section arguments.
552 public function add_section( $id, $args = array() ) {
553 if ( is_a( $id, 'WP_Customize_Section' ) )
556 $section = new WP_Customize_Section( $this, $id, $args );
558 $this->sections[ $section->id ] = $section;
562 * Retrieve a customize section.
566 * @param string $id A specific ID of the section.
567 * @return object The section object.
569 public function get_section( $id ) {
570 if ( isset( $this->sections[ $id ] ) )
571 return $this->sections[ $id ];
575 * Remove a customize section.
579 * @param string $id A specific ID of the section.
581 public function remove_section( $id ) {
582 unset( $this->sections[ $id ] );
586 * Add a customize control.
590 * @param string $id A specific ID of the control.
591 * @param array $args Setting arguments.
593 public function add_control( $id, $args = array() ) {
594 if ( is_a( $id, 'WP_Customize_Control' ) )
597 $control = new WP_Customize_Control( $this, $id, $args );
599 $this->controls[ $control->id ] = $control;
603 * Retrieve a customize control.
607 * @param string $id A specific ID of the control.
608 * @return object The settings object.
610 public function get_control( $id ) {
611 if ( isset( $this->controls[ $id ] ) )
612 return $this->controls[ $id ];
616 * Remove a customize setting.
620 * @param string $id A specific ID of the control.
622 public function remove_control( $id ) {
623 unset( $this->controls[ $id ] );
627 * Helper function to compare two objects by priority.
631 * @param object $a Object A.
632 * @param object $b Object B.
634 protected final function _cmp_priority( $a, $b ) {
640 return ( $ap > $bp ) ? 1 : -1;
644 * Prepare settings and sections.
648 public function prepare_controls() {
650 // Reversing makes uasort sort by time added when conflicts occur.
652 $this->controls = array_reverse( $this->controls );
655 foreach ( $this->controls as $id => $control ) {
656 if ( ! isset( $this->sections[ $control->section ] ) || ! $control->check_capabilities() )
659 $this->sections[ $control->section ]->controls[] = $control;
660 $controls[ $id ] = $control;
662 $this->controls = $controls;
665 $this->sections = array_reverse( $this->sections );
666 uasort( $this->sections, array( $this, '_cmp_priority' ) );
669 foreach ( $this->sections as $section ) {
670 if ( ! $section->check_capabilities() || ! $section->controls )
673 usort( $section->controls, array( $this, '_cmp_priority' ) );
674 $sections[] = $section;
676 $this->sections = $sections;
680 * Enqueue scripts for customize controls.
684 public function enqueue_control_scripts() {
685 foreach ( $this->controls as $control ) {
691 * Register some default controls.
695 public function register_controls() {
697 /* Site Title & Tagline */
699 $this->add_section( 'title_tagline', array(
700 'title' => __( 'Site Title & Tagline' ),
704 $this->add_setting( 'blogname', array(
705 'default' => get_option( 'blogname' ),
707 'capability' => 'manage_options',
710 $this->add_control( 'blogname', array(
711 'label' => __( 'Site Title' ),
712 'section' => 'title_tagline',
715 $this->add_setting( 'blogdescription', array(
716 'default' => get_option( 'blogdescription' ),
718 'capability' => 'manage_options',
721 $this->add_control( 'blogdescription', array(
722 'label' => __( 'Tagline' ),
723 'section' => 'title_tagline',
728 $this->add_section( 'colors', array(
729 'title' => __( 'Colors' ),
733 $this->add_setting( 'header_textcolor', array(
734 'theme_supports' => array( 'custom-header', 'header-text' ),
735 'default' => get_theme_support( 'custom-header', 'default-text-color' ),
737 'sanitize_callback' => array( $this, '_sanitize_header_textcolor' ),
738 'sanitize_js_callback' => 'maybe_hash_hex_color',
741 // Input type: checkbox
743 $this->add_control( 'display_header_text', array(
744 'settings' => 'header_textcolor',
745 'label' => __( 'Display Header Text' ),
746 'section' => 'title_tagline',
747 'type' => 'checkbox',
750 $this->add_control( new WP_Customize_Color_Control( $this, 'header_textcolor', array(
751 'label' => __( 'Header Text Color' ),
752 'section' => 'colors',
756 // With sanitize_callback
757 $this->add_setting( 'background_color', array(
758 'default' => get_theme_support( 'custom-background', 'default-color' ),
759 'theme_supports' => 'custom-background',
761 'sanitize_callback' => 'sanitize_hex_color_no_hash',
762 'sanitize_js_callback' => 'maybe_hash_hex_color',
765 $this->add_control( new WP_Customize_Color_Control( $this, 'background_color', array(
766 'label' => __( 'Background Color' ),
767 'section' => 'colors',
773 $this->add_section( 'header_image', array(
774 'title' => __( 'Header Image' ),
775 'theme_supports' => 'custom-header',
779 $this->add_setting( new WP_Customize_Filter_Setting( $this, 'header_image', array(
780 'default' => get_theme_support( 'custom-header', 'default-image' ),
781 'theme_supports' => 'custom-header',
784 $this->add_setting( new WP_Customize_Header_Image_Setting( $this, 'header_image_data', array(
785 // 'default' => get_theme_support( 'custom-header', 'default-image' ),
786 'theme_supports' => 'custom-header',
789 $this->add_control( new WP_Customize_Header_Image_Control( $this ) );
791 /* Custom Background */
793 $this->add_section( 'background_image', array(
794 'title' => __( 'Background Image' ),
795 'theme_supports' => 'custom-background',
799 $this->add_setting( 'background_image', array(
800 'default' => get_theme_support( 'custom-background', 'default-image' ),
801 'theme_supports' => 'custom-background',
804 $this->add_setting( new WP_Customize_Background_Image_Setting( $this, 'background_image_thumb', array(
805 'theme_supports' => 'custom-background',
808 $this->add_control( new WP_Customize_Background_Image_Control( $this ) );
810 $this->add_setting( 'background_repeat', array(
811 'default' => 'repeat',
812 'theme_supports' => 'custom-background',
815 $this->add_control( 'background_repeat', array(
816 'label' => __( 'Background Repeat' ),
817 'section' => 'background_image',
820 'no-repeat' => __('No Repeat'),
821 'repeat' => __('Tile'),
822 'repeat-x' => __('Tile Horizontally'),
823 'repeat-y' => __('Tile Vertically'),
827 $this->add_setting( 'background_position_x', array(
829 'theme_supports' => 'custom-background',
832 $this->add_control( 'background_position_x', array(
833 'label' => __( 'Background Position' ),
834 'section' => 'background_image',
837 'left' => __('Left'),
838 'center' => __('Center'),
839 'right' => __('Right'),
843 $this->add_setting( 'background_attachment', array(
844 'default' => 'fixed',
845 'theme_supports' => 'custom-background',
848 $this->add_control( 'background_attachment', array(
849 'label' => __( 'Background Attachment' ),
850 'section' => 'background_image',
853 'fixed' => __('Fixed'),
854 'scroll' => __('Scroll'),
858 // If the theme is using the default background callback, we can update
859 // the background CSS using postMessage.
860 if ( get_theme_support( 'custom-background', 'wp-head-callback' ) === '_custom_background_cb' ) {
861 foreach ( array( 'color', 'image', 'position_x', 'repeat', 'attachment' ) as $prop ) {
862 $this->get_setting( 'background_' . $prop )->transport = 'postMessage';
868 $locations = get_registered_nav_menus();
869 $menus = wp_get_nav_menus();
870 $menu_locations = get_nav_menu_locations();
871 $num_locations = count( array_keys( $locations ) );
873 $this->add_section( 'nav', array(
874 'title' => __( 'Navigation' ),
875 'theme_supports' => 'menus',
877 '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.'),
881 $choices = array( 0 => __( '— Select —' ) );
882 foreach ( $menus as $menu ) {
883 $truncated_name = wp_html_excerpt( $menu->name, 40 );
884 $truncated_name = ( $truncated_name == $menu->name ) ? $menu->name : trim( $truncated_name ) . '…';
885 $choices[ $menu->term_id ] = $truncated_name;
888 foreach ( $locations as $location => $description ) {
889 $menu_setting_id = "nav_menu_locations[{$location}]";
891 $this->add_setting( $menu_setting_id, array(
892 'sanitize_callback' => 'absint',
893 'theme_supports' => 'menus',
896 $this->add_control( $menu_setting_id, array(
897 'label' => $description,
900 'choices' => $choices,
905 /* Static Front Page */
908 $this->add_section( 'static_front_page', array(
909 'title' => __( 'Static Front Page' ),
910 // 'theme_supports' => 'static-front-page',
912 'description' => __( 'Your theme supports a static front page.' ),
915 $this->add_setting( 'show_on_front', array(
916 'default' => get_option( 'show_on_front' ),
917 'capability' => 'manage_options',
919 // 'theme_supports' => 'static-front-page',
922 $this->add_control( 'show_on_front', array(
923 'label' => __( 'Front page displays' ),
924 'section' => 'static_front_page',
927 'posts' => __( 'Your latest posts' ),
928 'page' => __( 'A static page' ),
932 $this->add_setting( 'page_on_front', array(
934 'capability' => 'manage_options',
935 // 'theme_supports' => 'static-front-page',
938 $this->add_control( 'page_on_front', array(
939 'label' => __( 'Front page' ),
940 'section' => 'static_front_page',
941 'type' => 'dropdown-pages',
944 $this->add_setting( 'page_for_posts', array(
946 'capability' => 'manage_options',
947 // 'theme_supports' => 'static-front-page',
950 $this->add_control( 'page_for_posts', array(
951 'label' => __( 'Posts page' ),
952 'section' => 'static_front_page',
953 'type' => 'dropdown-pages',
958 * Callback for validating the header_textcolor value.
960 * Accepts 'blank', and otherwise uses sanitize_hex_color_no_hash().
964 public function _sanitize_header_textcolor( $color ) {
965 return ( 'blank' === $color ) ? 'blank' : sanitize_hex_color_no_hash( $color );
970 * Validates a hex color.
972 * Returns either '', a 3 or 6 digit hex color (with #), or null.
973 * For validating values without a #, see sanitize_hex_color_no_hash().
977 function sanitize_hex_color( $color ) {
981 // 3 or 6 hex digits, or the empty string.
982 if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) )
989 * Sanitizes a hex color without a hash. Use sanitize_hex_color() when possible.
991 * Saving hex colors without a hash puts the burden of adding the hash on the
992 * UI, which makes it difficult to use or upgrade to other color types such as
993 * rgba, hsl, rgb, and html color names.
995 * Returns either '', a 3 or 6 digit hex color (without a #), or null.
999 function sanitize_hex_color_no_hash( $color ) {
1000 $color = ltrim( $color, '#' );
1002 if ( '' === $color )
1005 return sanitize_hex_color( '#' . $color ) ? $color : null;
1009 * Ensures that any hex color is properly hashed.
1010 * Otherwise, returns value untouched.
1012 * This method should only be necessary if using sanitize_hex_color_no_hash().
1016 function maybe_hash_hex_color( $color ) {
1017 if ( $unhashed = sanitize_hex_color_no_hash( $color ) )
1018 return '#' . $unhashed;