6 * @subpackage Customize
9 final class WP_Customize_Manager {
11 protected $original_stylesheet;
13 protected $previewing = false;
15 protected $settings = array();
16 protected $sections = array();
17 protected $controls = array();
19 protected $nonce_tick;
21 protected $customized;
23 private $_post_values;
30 public function __construct() {
31 require( ABSPATH . WPINC . '/class-wp-customize-setting.php' );
32 require( ABSPATH . WPINC . '/class-wp-customize-section.php' );
33 require( ABSPATH . WPINC . '/class-wp-customize-control.php' );
35 add_filter( 'wp_die_handler', array( $this, 'wp_die_handler' ) );
37 add_action( 'setup_theme', array( $this, 'setup_theme' ) );
38 add_action( 'wp_loaded', array( $this, 'wp_loaded' ) );
40 // Run wp_redirect_status late to make sure we override the status last.
41 add_action( 'wp_redirect_status', array( $this, 'wp_redirect_status' ), 1000 );
43 // Do not spawn cron (especially the alternate cron) while running the customizer.
44 remove_action( 'init', 'wp_cron' );
46 // Do not run update checks when rendering the controls.
47 remove_action( 'admin_init', '_maybe_update_core' );
48 remove_action( 'admin_init', '_maybe_update_plugins' );
49 remove_action( 'admin_init', '_maybe_update_themes' );
51 add_action( 'wp_ajax_customize_save', array( $this, 'save' ) );
53 add_action( 'customize_register', array( $this, 'register_controls' ) );
54 add_action( 'customize_controls_init', array( $this, 'prepare_controls' ) );
55 add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_control_scripts' ) );
59 * Return true if it's an AJAX request.
65 public function doing_ajax() {
66 return isset( $_POST['customized'] ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX );
70 * Custom wp_die wrapper. Returns either the standard message for UI
71 * or the AJAX message.
75 * @param mixed $ajax_message AJAX return
76 * @param mixed $message UI message
78 protected function wp_die( $ajax_message, $message = null ) {
79 if ( $this->doing_ajax() )
80 wp_die( $ajax_message );
83 $message = __( 'Cheatin’ uh?' );
89 * Return the AJAX wp_die() handler if it's a customized request.
95 public function wp_die_handler() {
96 if ( $this->doing_ajax() )
97 return '_ajax_wp_die_handler';
99 return '_default_wp_die_handler';
103 * Start preview and customize theme.
105 * Check if customize query variable exist. Init filters to filter the current theme.
109 public function setup_theme() {
110 send_origin_headers();
112 if ( is_admin() && ! $this->doing_ajax() )
114 elseif ( $this->doing_ajax() && ! is_user_logged_in() )
117 show_admin_bar( false );
119 if ( ! current_user_can( 'edit_theme_options' ) )
122 $this->original_stylesheet = get_stylesheet();
124 $this->theme = wp_get_theme( isset( $_REQUEST['theme'] ) ? $_REQUEST['theme'] : null );
126 if ( $this->is_theme_active() ) {
127 // Once the theme is loaded, we'll validate it.
128 add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ) );
130 if ( ! current_user_can( 'switch_themes' ) )
133 // If the theme isn't active, you can't preview it if it is not allowed or has errors.
134 if ( $this->theme()->errors() )
137 if ( ! $this->theme()->is_allowed() )
141 $this->start_previewing_theme();
145 * Callback to validate a theme once it is loaded
149 function after_setup_theme() {
150 if ( ! $this->doing_ajax() && ! validate_current_theme() ) {
151 wp_redirect( 'themes.php?broken=true' );
157 * Start previewing the selected theme.
159 * Adds filters to change the current theme.
163 public function start_previewing_theme() {
164 // Bail if we're already previewing.
165 if ( $this->is_preview() )
168 $this->previewing = true;
170 if ( ! $this->is_theme_active() ) {
171 add_filter( 'template', array( $this, 'get_template' ) );
172 add_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
173 add_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
175 // @link: http://core.trac.wordpress.org/ticket/20027
176 add_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
177 add_filter( 'pre_option_template', array( $this, 'get_template' ) );
179 // Handle custom theme roots.
180 add_filter( 'pre_option_stylesheet_root', array( $this, 'get_stylesheet_root' ) );
181 add_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
184 do_action( 'start_previewing_theme', $this );
188 * Stop previewing the selected theme.
190 * Removes filters to change the current theme.
194 public function stop_previewing_theme() {
195 if ( ! $this->is_preview() )
198 $this->previewing = false;
200 if ( ! $this->is_theme_active() ) {
201 remove_filter( 'template', array( $this, 'get_template' ) );
202 remove_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
203 remove_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
205 // @link: http://core.trac.wordpress.org/ticket/20027
206 remove_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
207 remove_filter( 'pre_option_template', array( $this, 'get_template' ) );
209 // Handle custom theme roots.
210 remove_filter( 'pre_option_stylesheet_root', array( $this, 'get_stylesheet_root' ) );
211 remove_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
214 do_action( 'stop_previewing_theme', $this );
218 * Get the theme being customized.
224 public function theme() {
229 * Get the registered settings.
235 public function settings() {
236 return $this->settings;
240 * Get the registered controls.
246 public function controls() {
247 return $this->controls;
251 * Get the registered sections.
257 public function sections() {
258 return $this->sections;
262 * Checks if the current theme is active.
268 public function is_theme_active() {
269 return $this->get_stylesheet() == $this->original_stylesheet;
273 * Register styles/scripts and initialize the preview of each setting
277 public function wp_loaded() {
278 do_action( 'customize_register', $this );
280 if ( $this->is_preview() && ! is_admin() )
281 $this->customize_preview_init();
285 * Prevents AJAX requests from following redirects when previewing a theme
286 * by issuing a 200 response instead of a 30x.
288 * Instead, the JS will sniff out the location header.
295 public function wp_redirect_status( $status ) {
296 if ( $this->is_preview() && ! is_admin() )
303 * Decode the $_POST attribute used to override the WP_Customize_Setting values.
307 * @param mixed $setting A WP_Customize_Setting derived object
308 * @return string Sanitized attribute
310 public function post_value( $setting ) {
311 if ( ! isset( $this->_post_values ) ) {
312 if ( isset( $_POST['customized'] ) )
313 $this->_post_values = json_decode( stripslashes( $_POST['customized'] ), true );
315 $this->_post_values = false;
318 if ( isset( $this->_post_values[ $setting->id ] ) )
319 return $setting->sanitize( $this->_post_values[ $setting->id ] );
323 * Print javascript settings.
327 public function customize_preview_init() {
328 $this->nonce_tick = check_ajax_referer( 'preview-customize_' . $this->get_stylesheet(), 'nonce' );
330 $this->prepare_controls();
332 wp_enqueue_script( 'customize-preview' );
333 add_action( 'wp_head', array( $this, 'customize_preview_base' ) );
334 add_action( 'wp_head', array( $this, 'customize_preview_html5' ) );
335 add_action( 'wp_footer', array( $this, 'customize_preview_settings' ), 20 );
336 add_action( 'shutdown', array( $this, 'customize_preview_signature' ), 1000 );
337 add_filter( 'wp_die_handler', array( $this, 'remove_preview_signature' ) );
339 foreach ( $this->settings as $setting ) {
343 do_action( 'customize_preview_init', $this );
347 * Print base element for preview frame.
351 public function customize_preview_base() {
352 ?><base href="<?php echo home_url( '/' ); ?>" /><?php
356 * Print a workaround to handle HTML5 tags in IE < 9
360 public function customize_preview_html5() { ?>
362 <script type="text/javascript">
363 var e = [ 'abbr', 'article', 'aside', 'audio', 'canvas', 'datalist', 'details',
364 'figure', 'footer', 'header', 'hgroup', 'mark', 'menu', 'meter', 'nav',
365 'output', 'progress', 'section', 'time', 'video' ];
366 for ( var i = 0; i < e.length; i++ ) {
367 document.createElement( e[i] );
374 * Print javascript settings for preview frame.
378 public function customize_preview_settings() {
381 'channel' => esc_js( $_POST['customize_messenger_channel'] ),
384 if ( 2 == $this->nonce_tick ) {
385 $settings['nonce'] = array(
386 'save' => wp_create_nonce( 'save-customize_' . $this->get_stylesheet() ),
387 'preview' => wp_create_nonce( 'preview-customize_' . $this->get_stylesheet() )
391 foreach ( $this->settings as $id => $setting ) {
392 $settings['values'][ $id ] = $setting->js_value();
396 <script type="text/javascript">
397 var _wpCustomizeSettings = <?php echo json_encode( $settings ); ?>;
403 * Prints a signature so we can ensure the customizer was properly executed.
407 public function customize_preview_signature() {
408 echo 'WP_CUSTOMIZER_SIGNATURE';
412 * Removes the signature in case we experience a case where the customizer was not properly executed.
416 public function remove_preview_signature( $return = null ) {
417 remove_action( 'shutdown', array( $this, 'customize_preview_signature' ), 1000 );
423 * Is it a theme preview?
427 * @return bool True if it's a preview, false if not.
429 public function is_preview() {
430 return (bool) $this->previewing;
434 * Retrieve the template name of the previewed theme.
438 * @return string Template name.
440 public function get_template() {
441 return $this->theme()->get_template();
445 * Retrieve the stylesheet name of the previewed theme.
449 * @return string Stylesheet name.
451 public function get_stylesheet() {
452 return $this->theme()->get_stylesheet();
456 * Retrieve the template root of the previewed theme.
460 * @return string Theme root.
462 public function get_template_root() {
463 return get_raw_theme_root( $this->get_template(), true );
467 * Retrieve the stylesheet root of the previewed theme.
471 * @return string Theme root.
473 public function get_stylesheet_root() {
474 return get_raw_theme_root( $this->get_stylesheet(), true );
478 * Filter the current theme and return the name of the previewed theme.
482 * @param $current_theme {@internal Parameter is not used}
483 * @return string Theme name.
485 public function current_theme( $current_theme ) {
486 return $this->theme()->display('Name');
490 * Switch the theme and trigger the save action of each setting.
494 public function save() {
495 if ( ! $this->is_preview() )
498 check_ajax_referer( 'save-customize_' . $this->get_stylesheet(), 'nonce' );
500 // Do we have to switch themes?
501 if ( ! $this->is_theme_active() ) {
502 // Temporarily stop previewing the theme to allow switch_themes()
503 // to operate properly.
504 $this->stop_previewing_theme();
505 switch_theme( $this->get_stylesheet() );
506 $this->start_previewing_theme();
509 do_action( 'customize_save', $this );
511 foreach ( $this->settings as $setting ) {
519 * Add a customize setting.
523 * @param string $id A specific ID of the setting. Can be a
524 * theme mod or option name.
525 * @param array $args Setting arguments.
527 public function add_setting( $id, $args = array() ) {
528 if ( is_a( $id, 'WP_Customize_Setting' ) )
531 $setting = new WP_Customize_Setting( $this, $id, $args );
533 $this->settings[ $setting->id ] = $setting;
537 * Retrieve a customize setting.
541 * @param string $id A specific ID of the setting.
542 * @return object The settings object.
544 public function get_setting( $id ) {
545 if ( isset( $this->settings[ $id ] ) )
546 return $this->settings[ $id ];
550 * Remove a customize setting.
554 * @param string $id A specific ID of the setting.
556 public function remove_setting( $id ) {
557 unset( $this->settings[ $id ] );
561 * Add a customize section.
565 * @param string $id A specific ID of the section.
566 * @param array $args Section arguments.
568 public function add_section( $id, $args = array() ) {
569 if ( is_a( $id, 'WP_Customize_Section' ) )
572 $section = new WP_Customize_Section( $this, $id, $args );
574 $this->sections[ $section->id ] = $section;
578 * Retrieve a customize section.
582 * @param string $id A specific ID of the section.
583 * @return object The section object.
585 public function get_section( $id ) {
586 if ( isset( $this->sections[ $id ] ) )
587 return $this->sections[ $id ];
591 * Remove a customize section.
595 * @param string $id A specific ID of the section.
597 public function remove_section( $id ) {
598 unset( $this->sections[ $id ] );
602 * Add a customize control.
606 * @param string $id A specific ID of the control.
607 * @param array $args Setting arguments.
609 public function add_control( $id, $args = array() ) {
610 if ( is_a( $id, 'WP_Customize_Control' ) )
613 $control = new WP_Customize_Control( $this, $id, $args );
615 $this->controls[ $control->id ] = $control;
619 * Retrieve a customize control.
623 * @param string $id A specific ID of the control.
624 * @return object The settings object.
626 public function get_control( $id ) {
627 if ( isset( $this->controls[ $id ] ) )
628 return $this->controls[ $id ];
632 * Remove a customize setting.
636 * @param string $id A specific ID of the control.
638 public function remove_control( $id ) {
639 unset( $this->controls[ $id ] );
643 * Helper function to compare two objects by priority.
647 * @param object $a Object A.
648 * @param object $b Object B.
651 protected final function _cmp_priority( $a, $b ) {
657 return ( $ap > $bp ) ? 1 : -1;
661 * Prepare settings and sections.
665 public function prepare_controls() {
667 // Reversing makes uasort sort by time added when conflicts occur.
669 $this->controls = array_reverse( $this->controls );
672 foreach ( $this->controls as $id => $control ) {
673 if ( ! isset( $this->sections[ $control->section ] ) || ! $control->check_capabilities() )
676 $this->sections[ $control->section ]->controls[] = $control;
677 $controls[ $id ] = $control;
679 $this->controls = $controls;
682 $this->sections = array_reverse( $this->sections );
683 uasort( $this->sections, array( $this, '_cmp_priority' ) );
686 foreach ( $this->sections as $section ) {
687 if ( ! $section->check_capabilities() || ! $section->controls )
690 usort( $section->controls, array( $this, '_cmp_priority' ) );
691 $sections[] = $section;
693 $this->sections = $sections;
697 * Enqueue scripts for customize controls.
701 public function enqueue_control_scripts() {
702 foreach ( $this->controls as $control ) {
708 * Register some default controls.
712 public function register_controls() {
714 /* Site Title & Tagline */
716 $this->add_section( 'title_tagline', array(
717 'title' => __( 'Site Title & Tagline' ),
721 $this->add_setting( 'blogname', array(
722 'default' => get_option( 'blogname' ),
724 'capability' => 'manage_options',
727 $this->add_control( 'blogname', array(
728 'label' => __( 'Site Title' ),
729 'section' => 'title_tagline',
732 $this->add_setting( 'blogdescription', array(
733 'default' => get_option( 'blogdescription' ),
735 'capability' => 'manage_options',
738 $this->add_control( 'blogdescription', array(
739 'label' => __( 'Tagline' ),
740 'section' => 'title_tagline',
745 $this->add_section( 'colors', array(
746 'title' => __( 'Colors' ),
750 $this->add_setting( 'header_textcolor', array(
751 'theme_supports' => array( 'custom-header', 'header-text' ),
752 'default' => get_theme_support( 'custom-header', 'default-text-color' ),
754 'sanitize_callback' => array( $this, '_sanitize_header_textcolor' ),
755 'sanitize_js_callback' => 'maybe_hash_hex_color',
758 // Input type: checkbox
760 $this->add_control( 'display_header_text', array(
761 'settings' => 'header_textcolor',
762 'label' => __( 'Display Header Text' ),
763 'section' => 'title_tagline',
764 'type' => 'checkbox',
767 $this->add_control( new WP_Customize_Color_Control( $this, 'header_textcolor', array(
768 'label' => __( 'Header Text Color' ),
769 'section' => 'colors',
773 // With sanitize_callback
774 $this->add_setting( 'background_color', array(
775 'default' => get_theme_support( 'custom-background', 'default-color' ),
776 'theme_supports' => 'custom-background',
778 'sanitize_callback' => 'sanitize_hex_color_no_hash',
779 'sanitize_js_callback' => 'maybe_hash_hex_color',
782 $this->add_control( new WP_Customize_Color_Control( $this, 'background_color', array(
783 'label' => __( 'Background Color' ),
784 'section' => 'colors',
790 $this->add_section( 'header_image', array(
791 'title' => __( 'Header Image' ),
792 'theme_supports' => 'custom-header',
796 $this->add_setting( new WP_Customize_Filter_Setting( $this, 'header_image', array(
797 'default' => get_theme_support( 'custom-header', 'default-image' ),
798 'theme_supports' => 'custom-header',
801 $this->add_setting( new WP_Customize_Header_Image_Setting( $this, 'header_image_data', array(
802 // 'default' => get_theme_support( 'custom-header', 'default-image' ),
803 'theme_supports' => 'custom-header',
806 $this->add_control( new WP_Customize_Header_Image_Control( $this ) );
808 /* Custom Background */
810 $this->add_section( 'background_image', array(
811 'title' => __( 'Background Image' ),
812 'theme_supports' => 'custom-background',
816 $this->add_setting( 'background_image', array(
817 'default' => get_theme_support( 'custom-background', 'default-image' ),
818 'theme_supports' => 'custom-background',
821 $this->add_setting( new WP_Customize_Background_Image_Setting( $this, 'background_image_thumb', array(
822 'theme_supports' => 'custom-background',
825 $this->add_control( new WP_Customize_Background_Image_Control( $this ) );
827 $this->add_setting( 'background_repeat', array(
828 'default' => 'repeat',
829 'theme_supports' => 'custom-background',
832 $this->add_control( 'background_repeat', array(
833 'label' => __( 'Background Repeat' ),
834 'section' => 'background_image',
837 'no-repeat' => __('No Repeat'),
838 'repeat' => __('Tile'),
839 'repeat-x' => __('Tile Horizontally'),
840 'repeat-y' => __('Tile Vertically'),
844 $this->add_setting( 'background_position_x', array(
846 'theme_supports' => 'custom-background',
849 $this->add_control( 'background_position_x', array(
850 'label' => __( 'Background Position' ),
851 'section' => 'background_image',
854 'left' => __('Left'),
855 'center' => __('Center'),
856 'right' => __('Right'),
860 $this->add_setting( 'background_attachment', array(
861 'default' => 'fixed',
862 'theme_supports' => 'custom-background',
865 $this->add_control( 'background_attachment', array(
866 'label' => __( 'Background Attachment' ),
867 'section' => 'background_image',
870 'fixed' => __('Fixed'),
871 'scroll' => __('Scroll'),
875 // If the theme is using the default background callback, we can update
876 // the background CSS using postMessage.
877 if ( get_theme_support( 'custom-background', 'wp-head-callback' ) === '_custom_background_cb' ) {
878 foreach ( array( 'color', 'image', 'position_x', 'repeat', 'attachment' ) as $prop ) {
879 $this->get_setting( 'background_' . $prop )->transport = 'postMessage';
885 $locations = get_registered_nav_menus();
886 $menus = wp_get_nav_menus();
887 $menu_locations = get_nav_menu_locations();
888 $num_locations = count( array_keys( $locations ) );
890 $this->add_section( 'nav', array(
891 'title' => __( 'Navigation' ),
892 'theme_supports' => 'menus',
894 '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.'),
898 $choices = array( 0 => __( '— Select —' ) );
899 foreach ( $menus as $menu ) {
900 $truncated_name = wp_html_excerpt( $menu->name, 40 );
901 $truncated_name = ( $truncated_name == $menu->name ) ? $menu->name : trim( $truncated_name ) . '…';
902 $choices[ $menu->term_id ] = $truncated_name;
905 foreach ( $locations as $location => $description ) {
906 $menu_setting_id = "nav_menu_locations[{$location}]";
908 $this->add_setting( $menu_setting_id, array(
909 'sanitize_callback' => 'absint',
910 'theme_supports' => 'menus',
913 $this->add_control( $menu_setting_id, array(
914 'label' => $description,
917 'choices' => $choices,
922 /* Static Front Page */
925 $this->add_section( 'static_front_page', array(
926 'title' => __( 'Static Front Page' ),
927 // 'theme_supports' => 'static-front-page',
929 'description' => __( 'Your theme supports a static front page.' ),
932 $this->add_setting( 'show_on_front', array(
933 'default' => get_option( 'show_on_front' ),
934 'capability' => 'manage_options',
936 // 'theme_supports' => 'static-front-page',
939 $this->add_control( 'show_on_front', array(
940 'label' => __( 'Front page displays' ),
941 'section' => 'static_front_page',
944 'posts' => __( 'Your latest posts' ),
945 'page' => __( 'A static page' ),
949 $this->add_setting( 'page_on_front', array(
951 'capability' => 'manage_options',
952 // 'theme_supports' => 'static-front-page',
955 $this->add_control( 'page_on_front', array(
956 'label' => __( 'Front page' ),
957 'section' => 'static_front_page',
958 'type' => 'dropdown-pages',
961 $this->add_setting( 'page_for_posts', array(
963 'capability' => 'manage_options',
964 // 'theme_supports' => 'static-front-page',
967 $this->add_control( 'page_for_posts', array(
968 'label' => __( 'Posts page' ),
969 'section' => 'static_front_page',
970 'type' => 'dropdown-pages',
975 * Callback for validating the header_textcolor value.
977 * Accepts 'blank', and otherwise uses sanitize_hex_color_no_hash().
981 * @param string $color
984 public function _sanitize_header_textcolor( $color ) {
985 return ( 'blank' === $color ) ? 'blank' : sanitize_hex_color_no_hash( $color );
990 * Validates a hex color.
992 * Returns either '', a 3 or 6 digit hex color (with #), or null.
993 * For validating values without a #, see sanitize_hex_color_no_hash().
997 * @param string $color
998 * @return string|null
1000 function sanitize_hex_color( $color ) {
1001 if ( '' === $color )
1004 // 3 or 6 hex digits, or the empty string.
1005 if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) )
1012 * Sanitizes a hex color without a hash. Use sanitize_hex_color() when possible.
1014 * Saving hex colors without a hash puts the burden of adding the hash on the
1015 * UI, which makes it difficult to use or upgrade to other color types such as
1016 * rgba, hsl, rgb, and html color names.
1018 * Returns either '', a 3 or 6 digit hex color (without a #), or null.
1021 * @uses sanitize_hex_color()
1023 * @param string $color
1024 * @return string|null
1026 function sanitize_hex_color_no_hash( $color ) {
1027 $color = ltrim( $color, '#' );
1029 if ( '' === $color )
1032 return sanitize_hex_color( '#' . $color ) ? $color : null;
1036 * Ensures that any hex color is properly hashed.
1037 * Otherwise, returns value untouched.
1039 * This method should only be necessary if using sanitize_hex_color_no_hash().
1043 * @param string $color
1046 function maybe_hash_hex_color( $color ) {
1047 if ( $unhashed = sanitize_hex_color_no_hash( $color ) )
1048 return '#' . $unhashed;