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( wp_unslash( $_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 ) {
515 do_action( 'customize_save_after', $this );
521 * Add a customize setting.
525 * @param string $id A specific ID of the setting. Can be a
526 * theme mod or option name.
527 * @param array $args Setting arguments.
529 public function add_setting( $id, $args = array() ) {
530 if ( is_a( $id, 'WP_Customize_Setting' ) )
533 $setting = new WP_Customize_Setting( $this, $id, $args );
535 $this->settings[ $setting->id ] = $setting;
539 * Retrieve a customize setting.
543 * @param string $id A specific ID of the setting.
544 * @return object The settings object.
546 public function get_setting( $id ) {
547 if ( isset( $this->settings[ $id ] ) )
548 return $this->settings[ $id ];
552 * Remove a customize setting.
556 * @param string $id A specific ID of the setting.
558 public function remove_setting( $id ) {
559 unset( $this->settings[ $id ] );
563 * Add a customize section.
567 * @param string $id A specific ID of the section.
568 * @param array $args Section arguments.
570 public function add_section( $id, $args = array() ) {
571 if ( is_a( $id, 'WP_Customize_Section' ) )
574 $section = new WP_Customize_Section( $this, $id, $args );
576 $this->sections[ $section->id ] = $section;
580 * Retrieve a customize section.
584 * @param string $id A specific ID of the section.
585 * @return object The section object.
587 public function get_section( $id ) {
588 if ( isset( $this->sections[ $id ] ) )
589 return $this->sections[ $id ];
593 * Remove a customize section.
597 * @param string $id A specific ID of the section.
599 public function remove_section( $id ) {
600 unset( $this->sections[ $id ] );
604 * Add a customize control.
608 * @param string $id A specific ID of the control.
609 * @param array $args Setting arguments.
611 public function add_control( $id, $args = array() ) {
612 if ( is_a( $id, 'WP_Customize_Control' ) )
615 $control = new WP_Customize_Control( $this, $id, $args );
617 $this->controls[ $control->id ] = $control;
621 * Retrieve a customize control.
625 * @param string $id A specific ID of the control.
626 * @return object The settings object.
628 public function get_control( $id ) {
629 if ( isset( $this->controls[ $id ] ) )
630 return $this->controls[ $id ];
634 * Remove a customize setting.
638 * @param string $id A specific ID of the control.
640 public function remove_control( $id ) {
641 unset( $this->controls[ $id ] );
645 * Helper function to compare two objects by priority.
649 * @param object $a Object A.
650 * @param object $b Object B.
653 protected final function _cmp_priority( $a, $b ) {
659 return ( $ap > $bp ) ? 1 : -1;
663 * Prepare settings and sections.
667 public function prepare_controls() {
669 // Reversing makes uasort sort by time added when conflicts occur.
671 $this->controls = array_reverse( $this->controls );
674 foreach ( $this->controls as $id => $control ) {
675 if ( ! isset( $this->sections[ $control->section ] ) || ! $control->check_capabilities() )
678 $this->sections[ $control->section ]->controls[] = $control;
679 $controls[ $id ] = $control;
681 $this->controls = $controls;
684 $this->sections = array_reverse( $this->sections );
685 uasort( $this->sections, array( $this, '_cmp_priority' ) );
688 foreach ( $this->sections as $section ) {
689 if ( ! $section->check_capabilities() || ! $section->controls )
692 usort( $section->controls, array( $this, '_cmp_priority' ) );
693 $sections[] = $section;
695 $this->sections = $sections;
699 * Enqueue scripts for customize controls.
703 public function enqueue_control_scripts() {
704 foreach ( $this->controls as $control ) {
710 * Register some default controls.
714 public function register_controls() {
716 /* Site Title & Tagline */
718 $this->add_section( 'title_tagline', array(
719 'title' => __( 'Site Title & Tagline' ),
723 $this->add_setting( 'blogname', array(
724 'default' => get_option( 'blogname' ),
726 'capability' => 'manage_options',
729 $this->add_control( 'blogname', array(
730 'label' => __( 'Site Title' ),
731 'section' => 'title_tagline',
734 $this->add_setting( 'blogdescription', array(
735 'default' => get_option( 'blogdescription' ),
737 'capability' => 'manage_options',
740 $this->add_control( 'blogdescription', array(
741 'label' => __( 'Tagline' ),
742 'section' => 'title_tagline',
747 $this->add_section( 'colors', array(
748 'title' => __( 'Colors' ),
752 $this->add_setting( 'header_textcolor', array(
753 'theme_supports' => array( 'custom-header', 'header-text' ),
754 'default' => get_theme_support( 'custom-header', 'default-text-color' ),
756 'sanitize_callback' => array( $this, '_sanitize_header_textcolor' ),
757 'sanitize_js_callback' => 'maybe_hash_hex_color',
760 // Input type: checkbox
762 $this->add_control( 'display_header_text', array(
763 'settings' => 'header_textcolor',
764 'label' => __( 'Display Header Text' ),
765 'section' => 'title_tagline',
766 'type' => 'checkbox',
769 $this->add_control( new WP_Customize_Color_Control( $this, 'header_textcolor', array(
770 'label' => __( 'Header Text Color' ),
771 'section' => 'colors',
775 // With sanitize_callback
776 $this->add_setting( 'background_color', array(
777 'default' => get_theme_support( 'custom-background', 'default-color' ),
778 'theme_supports' => 'custom-background',
780 'sanitize_callback' => 'sanitize_hex_color_no_hash',
781 'sanitize_js_callback' => 'maybe_hash_hex_color',
784 $this->add_control( new WP_Customize_Color_Control( $this, 'background_color', array(
785 'label' => __( 'Background Color' ),
786 'section' => 'colors',
792 $this->add_section( 'header_image', array(
793 'title' => __( 'Header Image' ),
794 'theme_supports' => 'custom-header',
798 $this->add_setting( new WP_Customize_Filter_Setting( $this, 'header_image', array(
799 'default' => get_theme_support( 'custom-header', 'default-image' ),
800 'theme_supports' => 'custom-header',
803 $this->add_setting( new WP_Customize_Header_Image_Setting( $this, 'header_image_data', array(
804 // 'default' => get_theme_support( 'custom-header', 'default-image' ),
805 'theme_supports' => 'custom-header',
808 $this->add_control( new WP_Customize_Header_Image_Control( $this ) );
810 /* Custom Background */
812 $this->add_section( 'background_image', array(
813 'title' => __( 'Background Image' ),
814 'theme_supports' => 'custom-background',
818 $this->add_setting( 'background_image', array(
819 'default' => get_theme_support( 'custom-background', 'default-image' ),
820 'theme_supports' => 'custom-background',
823 $this->add_setting( new WP_Customize_Background_Image_Setting( $this, 'background_image_thumb', array(
824 'theme_supports' => 'custom-background',
827 $this->add_control( new WP_Customize_Background_Image_Control( $this ) );
829 $this->add_setting( 'background_repeat', array(
830 'default' => 'repeat',
831 'theme_supports' => 'custom-background',
834 $this->add_control( 'background_repeat', array(
835 'label' => __( 'Background Repeat' ),
836 'section' => 'background_image',
839 'no-repeat' => __('No Repeat'),
840 'repeat' => __('Tile'),
841 'repeat-x' => __('Tile Horizontally'),
842 'repeat-y' => __('Tile Vertically'),
846 $this->add_setting( 'background_position_x', array(
848 'theme_supports' => 'custom-background',
851 $this->add_control( 'background_position_x', array(
852 'label' => __( 'Background Position' ),
853 'section' => 'background_image',
856 'left' => __('Left'),
857 'center' => __('Center'),
858 'right' => __('Right'),
862 $this->add_setting( 'background_attachment', array(
863 'default' => 'fixed',
864 'theme_supports' => 'custom-background',
867 $this->add_control( 'background_attachment', array(
868 'label' => __( 'Background Attachment' ),
869 'section' => 'background_image',
872 'fixed' => __('Fixed'),
873 'scroll' => __('Scroll'),
877 // If the theme is using the default background callback, we can update
878 // the background CSS using postMessage.
879 if ( get_theme_support( 'custom-background', 'wp-head-callback' ) === '_custom_background_cb' ) {
880 foreach ( array( 'color', 'image', 'position_x', 'repeat', 'attachment' ) as $prop ) {
881 $this->get_setting( 'background_' . $prop )->transport = 'postMessage';
887 $locations = get_registered_nav_menus();
888 $menus = wp_get_nav_menus();
889 $menu_locations = get_nav_menu_locations();
890 $num_locations = count( array_keys( $locations ) );
892 $this->add_section( 'nav', array(
893 'title' => __( 'Navigation' ),
894 'theme_supports' => 'menus',
896 '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.'),
900 $choices = array( 0 => __( '— Select —' ) );
901 foreach ( $menus as $menu ) {
902 $choices[ $menu->term_id ] = wp_html_excerpt( $menu->name, 40, '…' );
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().
978 * Returns default text color if hex color is empty.
982 * @param string $color
985 public function _sanitize_header_textcolor( $color ) {
986 if ( 'blank' === $color )
989 $color = sanitize_hex_color_no_hash( $color );
990 if ( empty( $color ) )
991 $color = get_theme_support( 'custom-header', 'default-text-color' );
998 * Validates a hex color.
1000 * Returns either '', a 3 or 6 digit hex color (with #), or null.
1001 * For validating values without a #, see sanitize_hex_color_no_hash().
1005 * @param string $color
1006 * @return string|null
1008 function sanitize_hex_color( $color ) {
1009 if ( '' === $color )
1012 // 3 or 6 hex digits, or the empty string.
1013 if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) )
1020 * Sanitizes a hex color without a hash. Use sanitize_hex_color() when possible.
1022 * Saving hex colors without a hash puts the burden of adding the hash on the
1023 * UI, which makes it difficult to use or upgrade to other color types such as
1024 * rgba, hsl, rgb, and html color names.
1026 * Returns either '', a 3 or 6 digit hex color (without a #), or null.
1029 * @uses sanitize_hex_color()
1031 * @param string $color
1032 * @return string|null
1034 function sanitize_hex_color_no_hash( $color ) {
1035 $color = ltrim( $color, '#' );
1037 if ( '' === $color )
1040 return sanitize_hex_color( '#' . $color ) ? $color : null;
1044 * Ensures that any hex color is properly hashed.
1045 * Otherwise, returns value untouched.
1047 * This method should only be necessary if using sanitize_hex_color_no_hash().
1051 * @param string $color
1054 function maybe_hash_hex_color( $color ) {
1055 if ( $unhashed = sanitize_hex_color_no_hash( $color ) )
1056 return '#' . $unhashed;