5 * Bootstraps the Customize experience on the server-side.
7 * Sets up the theme-switching process if a theme other than the active one is
8 * being previewed and customized.
10 * Serves as a factory for Customize Controls and Settings, and
11 * instantiates default Customize Controls and Settings.
14 * @subpackage Customize
17 final class WP_Customize_Manager {
19 * An instance of the theme being previewed.
26 * The directory name of the previously active theme (within the theme_root).
30 protected $original_stylesheet;
33 * Whether this is a Customizer pageload.
37 protected $previewing = false;
40 * Methods and properties deailing with managing widgets in the customizer.
42 * @var WP_Customize_Widgets
46 protected $settings = array();
47 protected $containers = array();
48 protected $panels = array();
49 protected $sections = array();
50 protected $controls = array();
52 protected $nonce_tick;
54 protected $customized;
57 * $_POST values for Customize Settings.
61 private $_post_values;
68 public function __construct() {
69 require( ABSPATH . WPINC . '/class-wp-customize-setting.php' );
70 require( ABSPATH . WPINC . '/class-wp-customize-panel.php' );
71 require( ABSPATH . WPINC . '/class-wp-customize-section.php' );
72 require( ABSPATH . WPINC . '/class-wp-customize-control.php' );
73 require( ABSPATH . WPINC . '/class-wp-customize-widgets.php' );
75 $this->widgets = new WP_Customize_Widgets( $this );
77 add_filter( 'wp_die_handler', array( $this, 'wp_die_handler' ) );
79 add_action( 'setup_theme', array( $this, 'setup_theme' ) );
80 add_action( 'wp_loaded', array( $this, 'wp_loaded' ) );
82 // Run wp_redirect_status late to make sure we override the status last.
83 add_action( 'wp_redirect_status', array( $this, 'wp_redirect_status' ), 1000 );
85 // Do not spawn cron (especially the alternate cron) while running the customizer.
86 remove_action( 'init', 'wp_cron' );
88 // Do not run update checks when rendering the controls.
89 remove_action( 'admin_init', '_maybe_update_core' );
90 remove_action( 'admin_init', '_maybe_update_plugins' );
91 remove_action( 'admin_init', '_maybe_update_themes' );
93 add_action( 'wp_ajax_customize_save', array( $this, 'save' ) );
95 add_action( 'customize_register', array( $this, 'register_controls' ) );
96 add_action( 'customize_controls_init', array( $this, 'prepare_controls' ) );
97 add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_control_scripts' ) );
101 * Return true if it's an AJAX request.
107 public function doing_ajax() {
108 return isset( $_POST['customized'] ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX );
112 * Custom wp_die wrapper. Returns either the standard message for UI
113 * or the AJAX message.
117 * @param mixed $ajax_message AJAX return
118 * @param mixed $message UI message
120 protected function wp_die( $ajax_message, $message = null ) {
121 if ( $this->doing_ajax() )
122 wp_die( $ajax_message );
125 $message = __( 'Cheatin’ uh?' );
131 * Return the AJAX wp_die() handler if it's a customized request.
137 public function wp_die_handler() {
138 if ( $this->doing_ajax() )
139 return '_ajax_wp_die_handler';
141 return '_default_wp_die_handler';
145 * Start preview and customize theme.
147 * Check if customize query variable exist. Init filters to filter the current theme.
151 public function setup_theme() {
152 send_origin_headers();
154 if ( is_admin() && ! $this->doing_ajax() )
156 elseif ( $this->doing_ajax() && ! is_user_logged_in() )
159 show_admin_bar( false );
161 if ( ! current_user_can( 'customize' ) ) {
165 $this->original_stylesheet = get_stylesheet();
167 $this->theme = wp_get_theme( isset( $_REQUEST['theme'] ) ? $_REQUEST['theme'] : null );
169 if ( $this->is_theme_active() ) {
170 // Once the theme is loaded, we'll validate it.
171 add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ) );
173 // If the requested theme is not the active theme and the user doesn't have the
174 // switch_themes cap, bail.
175 if ( ! current_user_can( 'switch_themes' ) )
178 // If the theme has errors while loading, bail.
179 if ( $this->theme()->errors() )
182 // If the theme isn't allowed per multisite settings, bail.
183 if ( ! $this->theme()->is_allowed() )
187 $this->start_previewing_theme();
191 * Callback to validate a theme once it is loaded
195 public function after_setup_theme() {
196 if ( ! $this->doing_ajax() && ! validate_current_theme() ) {
197 wp_redirect( 'themes.php?broken=true' );
203 * If the theme to be previewed isn't the active theme, add filter callbacks
204 * to swap it out at runtime.
208 public function start_previewing_theme() {
209 // Bail if we're already previewing.
210 if ( $this->is_preview() )
213 $this->previewing = true;
215 if ( ! $this->is_theme_active() ) {
216 add_filter( 'template', array( $this, 'get_template' ) );
217 add_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
218 add_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
220 // @link: http://core.trac.wordpress.org/ticket/20027
221 add_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
222 add_filter( 'pre_option_template', array( $this, 'get_template' ) );
224 // Handle custom theme roots.
225 add_filter( 'pre_option_stylesheet_root', array( $this, 'get_stylesheet_root' ) );
226 add_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
230 * Fires once the Customizer theme preview has started.
234 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
236 do_action( 'start_previewing_theme', $this );
240 * Stop previewing the selected theme.
242 * Removes filters to change the current theme.
246 public function stop_previewing_theme() {
247 if ( ! $this->is_preview() )
250 $this->previewing = false;
252 if ( ! $this->is_theme_active() ) {
253 remove_filter( 'template', array( $this, 'get_template' ) );
254 remove_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
255 remove_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
257 // @link: http://core.trac.wordpress.org/ticket/20027
258 remove_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
259 remove_filter( 'pre_option_template', array( $this, 'get_template' ) );
261 // Handle custom theme roots.
262 remove_filter( 'pre_option_stylesheet_root', array( $this, 'get_stylesheet_root' ) );
263 remove_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
267 * Fires once the Customizer theme preview has stopped.
271 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
273 do_action( 'stop_previewing_theme', $this );
277 * Get the theme being customized.
283 public function theme() {
288 * Get the registered settings.
294 public function settings() {
295 return $this->settings;
299 * Get the registered controls.
305 public function controls() {
306 return $this->controls;
310 * Get the registered containers.
316 public function containers() {
317 return $this->containers;
321 * Get the registered sections.
327 public function sections() {
328 return $this->sections;
332 * Get the registered panels.
337 * @return array Panels.
339 public function panels() {
340 return $this->panels;
344 * Checks if the current theme is active.
350 public function is_theme_active() {
351 return $this->get_stylesheet() == $this->original_stylesheet;
355 * Register styles/scripts and initialize the preview of each setting
359 public function wp_loaded() {
362 * Fires once WordPress has loaded, allowing scripts and styles to be initialized.
366 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
368 do_action( 'customize_register', $this );
370 if ( $this->is_preview() && ! is_admin() )
371 $this->customize_preview_init();
375 * Prevents AJAX requests from following redirects when previewing a theme
376 * by issuing a 200 response instead of a 30x.
378 * Instead, the JS will sniff out the location header.
385 public function wp_redirect_status( $status ) {
386 if ( $this->is_preview() && ! is_admin() )
393 * Decode the $_POST['customized'] values for a specific Customize Setting.
397 * @param mixed $setting A WP_Customize_Setting derived object
398 * @return string $post_value Sanitized value
400 public function post_value( $setting ) {
401 if ( ! isset( $this->_post_values ) ) {
402 if ( isset( $_POST['customized'] ) )
403 $this->_post_values = json_decode( wp_unslash( $_POST['customized'] ), true );
405 $this->_post_values = false;
408 if ( isset( $this->_post_values[ $setting->id ] ) )
409 return $setting->sanitize( $this->_post_values[ $setting->id ] );
413 * Print javascript settings.
417 public function customize_preview_init() {
418 $this->nonce_tick = check_ajax_referer( 'preview-customize_' . $this->get_stylesheet(), 'nonce' );
420 $this->prepare_controls();
422 wp_enqueue_script( 'customize-preview' );
423 add_action( 'wp', array( $this, 'customize_preview_override_404_status' ) );
424 add_action( 'wp_head', array( $this, 'customize_preview_base' ) );
425 add_action( 'wp_head', array( $this, 'customize_preview_html5' ) );
426 add_action( 'wp_footer', array( $this, 'customize_preview_settings' ), 20 );
427 add_action( 'shutdown', array( $this, 'customize_preview_signature' ), 1000 );
428 add_filter( 'wp_die_handler', array( $this, 'remove_preview_signature' ) );
430 foreach ( $this->settings as $setting ) {
435 * Fires once the Customizer preview has initialized and JavaScript
436 * settings have been printed.
440 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
442 do_action( 'customize_preview_init', $this );
446 * Prevent sending a 404 status when returning the response for the customize
447 * preview, since it causes the jQuery AJAX to fail. Send 200 instead.
452 public function customize_preview_override_404_status() {
454 status_header( 200 );
459 * Print base element for preview frame.
463 public function customize_preview_base() {
464 ?><base href="<?php echo home_url( '/' ); ?>" /><?php
468 * Print a workaround to handle HTML5 tags in IE < 9
472 public function customize_preview_html5() { ?>
474 <script type="text/javascript">
475 var e = [ 'abbr', 'article', 'aside', 'audio', 'canvas', 'datalist', 'details',
476 'figure', 'footer', 'header', 'hgroup', 'mark', 'menu', 'meter', 'nav',
477 'output', 'progress', 'section', 'time', 'video' ];
478 for ( var i = 0; i < e.length; i++ ) {
479 document.createElement( e[i] );
486 * Print javascript settings for preview frame.
490 public function customize_preview_settings() {
493 'channel' => wp_unslash( $_POST['customize_messenger_channel'] ),
494 'activeControls' => array(),
497 if ( 2 == $this->nonce_tick ) {
498 $settings['nonce'] = array(
499 'save' => wp_create_nonce( 'save-customize_' . $this->get_stylesheet() ),
500 'preview' => wp_create_nonce( 'preview-customize_' . $this->get_stylesheet() )
504 foreach ( $this->settings as $id => $setting ) {
505 $settings['values'][ $id ] = $setting->js_value();
507 foreach ( $this->controls as $id => $control ) {
508 $settings['activeControls'][ $id ] = $control->active();
512 <script type="text/javascript">
513 var _wpCustomizeSettings = <?php echo json_encode( $settings ); ?>;
519 * Prints a signature so we can ensure the customizer was properly executed.
523 public function customize_preview_signature() {
524 echo 'WP_CUSTOMIZER_SIGNATURE';
528 * Removes the signature in case we experience a case where the customizer was not properly executed.
532 public function remove_preview_signature( $return = null ) {
533 remove_action( 'shutdown', array( $this, 'customize_preview_signature' ), 1000 );
539 * Is it a theme preview?
543 * @return bool True if it's a preview, false if not.
545 public function is_preview() {
546 return (bool) $this->previewing;
550 * Retrieve the template name of the previewed theme.
554 * @return string Template name.
556 public function get_template() {
557 return $this->theme()->get_template();
561 * Retrieve the stylesheet name of the previewed theme.
565 * @return string Stylesheet name.
567 public function get_stylesheet() {
568 return $this->theme()->get_stylesheet();
572 * Retrieve the template root of the previewed theme.
576 * @return string Theme root.
578 public function get_template_root() {
579 return get_raw_theme_root( $this->get_template(), true );
583 * Retrieve the stylesheet root of the previewed theme.
587 * @return string Theme root.
589 public function get_stylesheet_root() {
590 return get_raw_theme_root( $this->get_stylesheet(), true );
594 * Filter the current theme and return the name of the previewed theme.
598 * @param $current_theme {@internal Parameter is not used}
599 * @return string Theme name.
601 public function current_theme( $current_theme ) {
602 return $this->theme()->display('Name');
606 * Switch the theme and trigger the save() method on each setting.
610 public function save() {
611 if ( ! $this->is_preview() )
614 check_ajax_referer( 'save-customize_' . $this->get_stylesheet(), 'nonce' );
616 // Do we have to switch themes?
617 if ( ! $this->is_theme_active() ) {
618 // Temporarily stop previewing the theme to allow switch_themes()
619 // to operate properly.
620 $this->stop_previewing_theme();
621 switch_theme( $this->get_stylesheet() );
622 update_option( 'theme_switched_via_customizer', true );
623 $this->start_previewing_theme();
627 * Fires once the theme has switched in the Customizer, but before settings
632 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
634 do_action( 'customize_save', $this );
636 foreach ( $this->settings as $setting ) {
641 * Fires after Customize settings have been saved.
645 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
647 do_action( 'customize_save_after', $this );
653 * Add a customize setting.
657 * @param WP_Customize_Setting|string $id Customize Setting object, or ID.
658 * @param array $args Setting arguments; passed to WP_Customize_Setting
661 public function add_setting( $id, $args = array() ) {
662 if ( is_a( $id, 'WP_Customize_Setting' ) )
665 $setting = new WP_Customize_Setting( $this, $id, $args );
667 $this->settings[ $setting->id ] = $setting;
671 * Retrieve a customize setting.
675 * @param string $id Customize Setting ID.
676 * @return WP_Customize_Setting
678 public function get_setting( $id ) {
679 if ( isset( $this->settings[ $id ] ) )
680 return $this->settings[ $id ];
684 * Remove a customize setting.
688 * @param string $id Customize Setting ID.
690 public function remove_setting( $id ) {
691 unset( $this->settings[ $id ] );
695 * Add a customize panel.
700 * @param WP_Customize_Panel|string $id Customize Panel object, or Panel ID.
701 * @param array $args Optional. Panel arguments. Default empty array.
703 public function add_panel( $id, $args = array() ) {
704 if ( is_a( $id, 'WP_Customize_Panel' ) ) {
708 $panel = new WP_Customize_Panel( $this, $id, $args );
711 $this->panels[ $panel->id ] = $panel;
715 * Retrieve a customize panel.
720 * @param string $id Panel ID to get.
721 * @return WP_Customize_Panel Requested panel instance.
723 public function get_panel( $id ) {
724 if ( isset( $this->panels[ $id ] ) ) {
725 return $this->panels[ $id ];
730 * Remove a customize panel.
735 * @param string $id Panel ID to remove.
737 public function remove_panel( $id ) {
738 unset( $this->panels[ $id ] );
742 * Add a customize section.
746 * @param WP_Customize_Section|string $id Customize Section object, or Section ID.
747 * @param array $args Section arguments.
749 public function add_section( $id, $args = array() ) {
750 if ( is_a( $id, 'WP_Customize_Section' ) )
753 $section = new WP_Customize_Section( $this, $id, $args );
755 $this->sections[ $section->id ] = $section;
759 * Retrieve a customize section.
763 * @param string $id Section ID.
764 * @return WP_Customize_Section
766 public function get_section( $id ) {
767 if ( isset( $this->sections[ $id ] ) )
768 return $this->sections[ $id ];
772 * Remove a customize section.
776 * @param string $id Section ID.
778 public function remove_section( $id ) {
779 unset( $this->sections[ $id ] );
783 * Add a customize control.
787 * @param WP_Customize_Control|string $id Customize Control object, or ID.
788 * @param array $args Control arguments; passed to WP_Customize_Control
791 public function add_control( $id, $args = array() ) {
792 if ( is_a( $id, 'WP_Customize_Control' ) )
795 $control = new WP_Customize_Control( $this, $id, $args );
797 $this->controls[ $control->id ] = $control;
801 * Retrieve a customize control.
805 * @param string $id ID of the control.
806 * @return WP_Customize_Control $control The control object.
808 public function get_control( $id ) {
809 if ( isset( $this->controls[ $id ] ) )
810 return $this->controls[ $id ];
814 * Remove a customize control.
818 * @param string $id ID of the control.
820 public function remove_control( $id ) {
821 unset( $this->controls[ $id ] );
825 * Helper function to compare two objects by priority.
829 * @param object $a Object A.
830 * @param object $b Object B.
833 protected final function _cmp_priority( $a, $b ) {
839 return ( $ap > $bp ) ? 1 : -1;
843 * Prepare panels, sections, and controls.
845 * For each, check if required related components exist,
846 * whether the user has the necessary capabilities,
847 * and sort by priority.
851 public function prepare_controls() {
853 $this->controls = array_reverse( $this->controls );
856 foreach ( $this->controls as $id => $control ) {
857 if ( ! isset( $this->sections[ $control->section ] ) || ! $control->check_capabilities() ) {
861 $this->sections[ $control->section ]->controls[] = $control;
862 $controls[ $id ] = $control;
864 $this->controls = $controls;
867 // Reversing makes uasort sort by time added when conflicts occur.
868 $this->sections = array_reverse( $this->sections );
869 uasort( $this->sections, array( $this, '_cmp_priority' ) );
872 foreach ( $this->sections as $section ) {
873 if ( ! $section->check_capabilities() || ! $section->controls ) {
877 usort( $section->controls, array( $this, '_cmp_priority' ) );
879 if ( ! $section->panel ) {
880 // Top-level section.
881 $sections[] = $section;
883 // This section belongs to a panel.
884 if ( isset( $this->panels [ $section->panel ] ) ) {
885 $this->panels[ $section->panel ]->sections[] = $section;
889 $this->sections = $sections;
892 // Reversing makes uasort sort by time added when conflicts occur.
893 $this->panels = array_reverse( $this->panels );
894 uasort( $this->panels, array( $this, '_cmp_priority' ) );
897 foreach ( $this->panels as $panel ) {
898 if ( ! $panel->check_capabilities() || ! $panel->sections ) {
902 usort( $panel->sections, array( $this, '_cmp_priority' ) );
905 $this->panels = $panels;
907 // Sort panels and top-level sections together.
908 $this->containers = array_merge( $this->panels, $this->sections );
909 uasort( $this->containers, array( $this, '_cmp_priority' ) );
913 * Enqueue scripts for customize controls.
917 public function enqueue_control_scripts() {
918 foreach ( $this->controls as $control ) {
924 * Register some default controls.
928 public function register_controls() {
930 /* Site Title & Tagline */
932 $this->add_section( 'title_tagline', array(
933 'title' => __( 'Site Title & Tagline' ),
937 $this->add_setting( 'blogname', array(
938 'default' => get_option( 'blogname' ),
940 'capability' => 'manage_options',
943 $this->add_control( 'blogname', array(
944 'label' => __( 'Site Title' ),
945 'section' => 'title_tagline',
948 $this->add_setting( 'blogdescription', array(
949 'default' => get_option( 'blogdescription' ),
951 'capability' => 'manage_options',
954 $this->add_control( 'blogdescription', array(
955 'label' => __( 'Tagline' ),
956 'section' => 'title_tagline',
961 $this->add_section( 'colors', array(
962 'title' => __( 'Colors' ),
966 $this->add_setting( 'header_textcolor', array(
967 'theme_supports' => array( 'custom-header', 'header-text' ),
968 'default' => get_theme_support( 'custom-header', 'default-text-color' ),
970 'sanitize_callback' => array( $this, '_sanitize_header_textcolor' ),
971 'sanitize_js_callback' => 'maybe_hash_hex_color',
974 // Input type: checkbox
976 $this->add_control( 'display_header_text', array(
977 'settings' => 'header_textcolor',
978 'label' => __( 'Display Header Text' ),
979 'section' => 'title_tagline',
980 'type' => 'checkbox',
983 $this->add_control( new WP_Customize_Color_Control( $this, 'header_textcolor', array(
984 'label' => __( 'Header Text Color' ),
985 'section' => 'colors',
989 // With sanitize_callback
990 $this->add_setting( 'background_color', array(
991 'default' => get_theme_support( 'custom-background', 'default-color' ),
992 'theme_supports' => 'custom-background',
994 'sanitize_callback' => 'sanitize_hex_color_no_hash',
995 'sanitize_js_callback' => 'maybe_hash_hex_color',
998 $this->add_control( new WP_Customize_Color_Control( $this, 'background_color', array(
999 'label' => __( 'Background Color' ),
1000 'section' => 'colors',
1006 $this->add_section( 'header_image', array(
1007 'title' => __( 'Header Image' ),
1008 'theme_supports' => 'custom-header',
1012 $this->add_setting( new WP_Customize_Filter_Setting( $this, 'header_image', array(
1013 'default' => get_theme_support( 'custom-header', 'default-image' ),
1014 'theme_supports' => 'custom-header',
1017 $this->add_setting( new WP_Customize_Header_Image_Setting( $this, 'header_image_data', array(
1018 // 'default' => get_theme_support( 'custom-header', 'default-image' ),
1019 'theme_supports' => 'custom-header',
1022 $this->add_control( new WP_Customize_Header_Image_Control( $this ) );
1024 /* Custom Background */
1026 $this->add_section( 'background_image', array(
1027 'title' => __( 'Background Image' ),
1028 'theme_supports' => 'custom-background',
1032 $this->add_setting( 'background_image', array(
1033 'default' => get_theme_support( 'custom-background', 'default-image' ),
1034 'theme_supports' => 'custom-background',
1037 $this->add_setting( new WP_Customize_Background_Image_Setting( $this, 'background_image_thumb', array(
1038 'theme_supports' => 'custom-background',
1041 $this->add_control( new WP_Customize_Background_Image_Control( $this ) );
1043 $this->add_setting( 'background_repeat', array(
1044 'default' => get_theme_support( 'custom-background', 'default-repeat' ),
1045 'theme_supports' => 'custom-background',
1048 $this->add_control( 'background_repeat', array(
1049 'label' => __( 'Background Repeat' ),
1050 'section' => 'background_image',
1053 'no-repeat' => __('No Repeat'),
1054 'repeat' => __('Tile'),
1055 'repeat-x' => __('Tile Horizontally'),
1056 'repeat-y' => __('Tile Vertically'),
1060 $this->add_setting( 'background_position_x', array(
1061 'default' => get_theme_support( 'custom-background', 'default-position-x' ),
1062 'theme_supports' => 'custom-background',
1065 $this->add_control( 'background_position_x', array(
1066 'label' => __( 'Background Position' ),
1067 'section' => 'background_image',
1070 'left' => __('Left'),
1071 'center' => __('Center'),
1072 'right' => __('Right'),
1076 $this->add_setting( 'background_attachment', array(
1077 'default' => get_theme_support( 'custom-background', 'default-attachment' ),
1078 'theme_supports' => 'custom-background',
1081 $this->add_control( 'background_attachment', array(
1082 'label' => __( 'Background Attachment' ),
1083 'section' => 'background_image',
1086 'scroll' => __('Scroll'),
1087 'fixed' => __('Fixed'),
1091 // If the theme is using the default background callback, we can update
1092 // the background CSS using postMessage.
1093 if ( get_theme_support( 'custom-background', 'wp-head-callback' ) === '_custom_background_cb' ) {
1094 foreach ( array( 'color', 'image', 'position_x', 'repeat', 'attachment' ) as $prop ) {
1095 $this->get_setting( 'background_' . $prop )->transport = 'postMessage';
1101 $locations = get_registered_nav_menus();
1102 $menus = wp_get_nav_menus();
1103 $num_locations = count( array_keys( $locations ) );
1105 $this->add_section( 'nav', array(
1106 'title' => __( 'Navigation' ),
1107 'theme_supports' => 'menus',
1109 '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.'),
1113 $choices = array( 0 => __( '— Select —' ) );
1114 foreach ( $menus as $menu ) {
1115 $choices[ $menu->term_id ] = wp_html_excerpt( $menu->name, 40, '…' );
1118 foreach ( $locations as $location => $description ) {
1119 $menu_setting_id = "nav_menu_locations[{$location}]";
1121 $this->add_setting( $menu_setting_id, array(
1122 'sanitize_callback' => 'absint',
1123 'theme_supports' => 'menus',
1126 $this->add_control( $menu_setting_id, array(
1127 'label' => $description,
1130 'choices' => $choices,
1135 /* Static Front Page */
1138 $this->add_section( 'static_front_page', array(
1139 'title' => __( 'Static Front Page' ),
1140 // 'theme_supports' => 'static-front-page',
1142 'description' => __( 'Your theme supports a static front page.' ),
1145 $this->add_setting( 'show_on_front', array(
1146 'default' => get_option( 'show_on_front' ),
1147 'capability' => 'manage_options',
1149 // 'theme_supports' => 'static-front-page',
1152 $this->add_control( 'show_on_front', array(
1153 'label' => __( 'Front page displays' ),
1154 'section' => 'static_front_page',
1157 'posts' => __( 'Your latest posts' ),
1158 'page' => __( 'A static page' ),
1162 $this->add_setting( 'page_on_front', array(
1164 'capability' => 'manage_options',
1165 // 'theme_supports' => 'static-front-page',
1168 $this->add_control( 'page_on_front', array(
1169 'label' => __( 'Front page' ),
1170 'section' => 'static_front_page',
1171 'type' => 'dropdown-pages',
1174 $this->add_setting( 'page_for_posts', array(
1176 'capability' => 'manage_options',
1177 // 'theme_supports' => 'static-front-page',
1180 $this->add_control( 'page_for_posts', array(
1181 'label' => __( 'Posts page' ),
1182 'section' => 'static_front_page',
1183 'type' => 'dropdown-pages',
1188 * Callback for validating the header_textcolor value.
1190 * Accepts 'blank', and otherwise uses sanitize_hex_color_no_hash().
1191 * Returns default text color if hex color is empty.
1195 * @param string $color
1198 public function _sanitize_header_textcolor( $color ) {
1199 if ( 'blank' === $color )
1202 $color = sanitize_hex_color_no_hash( $color );
1203 if ( empty( $color ) )
1204 $color = get_theme_support( 'custom-header', 'default-text-color' );
1211 * Sanitizes a hex color.
1213 * Returns either '', a 3 or 6 digit hex color (with #), or null.
1214 * For sanitizing values without a #, see sanitize_hex_color_no_hash().
1218 * @param string $color
1219 * @return string|null
1221 function sanitize_hex_color( $color ) {
1222 if ( '' === $color )
1225 // 3 or 6 hex digits, or the empty string.
1226 if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) )
1233 * Sanitizes a hex color without a hash. Use sanitize_hex_color() when possible.
1235 * Saving hex colors without a hash puts the burden of adding the hash on the
1236 * UI, which makes it difficult to use or upgrade to other color types such as
1237 * rgba, hsl, rgb, and html color names.
1239 * Returns either '', a 3 or 6 digit hex color (without a #), or null.
1242 * @uses sanitize_hex_color()
1244 * @param string $color
1245 * @return string|null
1247 function sanitize_hex_color_no_hash( $color ) {
1248 $color = ltrim( $color, '#' );
1250 if ( '' === $color )
1253 return sanitize_hex_color( '#' . $color ) ? $color : null;
1257 * Ensures that any hex color is properly hashed.
1258 * Otherwise, returns value untouched.
1260 * This method should only be necessary if using sanitize_hex_color_no_hash().
1264 * @param string $color
1267 function maybe_hash_hex_color( $color ) {
1268 if ( $unhashed = sanitize_hex_color_no_hash( $color ) )
1269 return '#' . $unhashed;