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 that is being customized.
26 * The directory name of the previously active theme (within the theme_root).
30 protected $original_stylesheet;
33 * Whether filters have been set to change the active theme to the theme being
38 protected $previewing = false;
41 * Methods and properties deailing with managing widgets in the customizer.
43 * @var WP_Customize_Widgets
47 protected $settings = array();
48 protected $sections = array();
49 protected $controls = array();
51 protected $nonce_tick;
53 protected $customized;
56 * $_POST values for Customize Settings.
60 private $_post_values;
67 public function __construct() {
68 require( ABSPATH . WPINC . '/class-wp-customize-setting.php' );
69 require( ABSPATH . WPINC . '/class-wp-customize-section.php' );
70 require( ABSPATH . WPINC . '/class-wp-customize-control.php' );
71 require( ABSPATH . WPINC . '/class-wp-customize-widgets.php' );
73 $this->widgets = new WP_Customize_Widgets( $this );
75 add_filter( 'wp_die_handler', array( $this, 'wp_die_handler' ) );
77 add_action( 'setup_theme', array( $this, 'setup_theme' ) );
78 add_action( 'wp_loaded', array( $this, 'wp_loaded' ) );
80 // Run wp_redirect_status late to make sure we override the status last.
81 add_action( 'wp_redirect_status', array( $this, 'wp_redirect_status' ), 1000 );
83 // Do not spawn cron (especially the alternate cron) while running the customizer.
84 remove_action( 'init', 'wp_cron' );
86 // Do not run update checks when rendering the controls.
87 remove_action( 'admin_init', '_maybe_update_core' );
88 remove_action( 'admin_init', '_maybe_update_plugins' );
89 remove_action( 'admin_init', '_maybe_update_themes' );
91 add_action( 'wp_ajax_customize_save', array( $this, 'save' ) );
93 add_action( 'customize_register', array( $this, 'register_controls' ) );
94 add_action( 'customize_controls_init', array( $this, 'prepare_controls' ) );
95 add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_control_scripts' ) );
99 * Return true if it's an AJAX request.
105 public function doing_ajax() {
106 return isset( $_POST['customized'] ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX );
110 * Custom wp_die wrapper. Returns either the standard message for UI
111 * or the AJAX message.
115 * @param mixed $ajax_message AJAX return
116 * @param mixed $message UI message
118 protected function wp_die( $ajax_message, $message = null ) {
119 if ( $this->doing_ajax() )
120 wp_die( $ajax_message );
123 $message = __( 'Cheatin’ uh?' );
129 * Return the AJAX wp_die() handler if it's a customized request.
135 public function wp_die_handler() {
136 if ( $this->doing_ajax() )
137 return '_ajax_wp_die_handler';
139 return '_default_wp_die_handler';
143 * Start preview and customize theme.
145 * Check if customize query variable exist. Init filters to filter the current theme.
149 public function setup_theme() {
150 send_origin_headers();
152 if ( is_admin() && ! $this->doing_ajax() )
154 elseif ( $this->doing_ajax() && ! is_user_logged_in() )
157 show_admin_bar( false );
159 if ( ! current_user_can( 'edit_theme_options' ) )
162 $this->original_stylesheet = get_stylesheet();
164 $this->theme = wp_get_theme( isset( $_REQUEST['theme'] ) ? $_REQUEST['theme'] : null );
166 if ( $this->is_theme_active() ) {
167 // Once the theme is loaded, we'll validate it.
168 add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ) );
170 // If the requested theme is not the active theme and the user doesn't have the
171 // switch_themes cap, bail.
172 if ( ! current_user_can( 'switch_themes' ) )
175 // If the theme has errors while loading, bail.
176 if ( $this->theme()->errors() )
179 // If the theme isn't allowed per multisite settings, bail.
180 if ( ! $this->theme()->is_allowed() )
184 // All good, let's do some internal business to preview the theme.
185 $this->start_previewing_theme();
189 * Callback to validate a theme once it is loaded
193 function after_setup_theme() {
194 if ( ! $this->doing_ajax() && ! validate_current_theme() ) {
195 wp_redirect( 'themes.php?broken=true' );
201 * Start previewing the selected theme by adding filters to change the current theme.
205 public function start_previewing_theme() {
206 // Bail if we're already previewing.
207 if ( $this->is_preview() )
210 $this->previewing = true;
212 if ( ! $this->is_theme_active() ) {
213 add_filter( 'template', array( $this, 'get_template' ) );
214 add_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
215 add_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
217 // @link: http://core.trac.wordpress.org/ticket/20027
218 add_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
219 add_filter( 'pre_option_template', array( $this, 'get_template' ) );
221 // Handle custom theme roots.
222 add_filter( 'pre_option_stylesheet_root', array( $this, 'get_stylesheet_root' ) );
223 add_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
227 * Fires once the Customizer theme preview has started.
231 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
233 do_action( 'start_previewing_theme', $this );
237 * Stop previewing the selected theme.
239 * Removes filters to change the current theme.
243 public function stop_previewing_theme() {
244 if ( ! $this->is_preview() )
247 $this->previewing = false;
249 if ( ! $this->is_theme_active() ) {
250 remove_filter( 'template', array( $this, 'get_template' ) );
251 remove_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
252 remove_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
254 // @link: http://core.trac.wordpress.org/ticket/20027
255 remove_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
256 remove_filter( 'pre_option_template', array( $this, 'get_template' ) );
258 // Handle custom theme roots.
259 remove_filter( 'pre_option_stylesheet_root', array( $this, 'get_stylesheet_root' ) );
260 remove_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
264 * Fires once the Customizer theme preview has stopped.
268 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
270 do_action( 'stop_previewing_theme', $this );
274 * Get the theme being customized.
280 public function theme() {
285 * Get the registered settings.
291 public function settings() {
292 return $this->settings;
296 * Get the registered controls.
302 public function controls() {
303 return $this->controls;
307 * Get the registered sections.
313 public function sections() {
314 return $this->sections;
318 * Checks if the current theme is active.
324 public function is_theme_active() {
325 return $this->get_stylesheet() == $this->original_stylesheet;
329 * Register styles/scripts and initialize the preview of each setting
333 public function wp_loaded() {
336 * Fires once WordPress has loaded, allowing scripts and styles to be initialized.
340 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
342 do_action( 'customize_register', $this );
344 if ( $this->is_preview() && ! is_admin() )
345 $this->customize_preview_init();
349 * Prevents AJAX requests from following redirects when previewing a theme
350 * by issuing a 200 response instead of a 30x.
352 * Instead, the JS will sniff out the location header.
359 public function wp_redirect_status( $status ) {
360 if ( $this->is_preview() && ! is_admin() )
367 * Decode the $_POST['customized'] values for a specific Customize Setting.
371 * @param mixed $setting A WP_Customize_Setting derived object
372 * @return string $post_value Sanitized value
374 public function post_value( $setting ) {
375 if ( ! isset( $this->_post_values ) ) {
376 if ( isset( $_POST['customized'] ) )
377 $this->_post_values = json_decode( wp_unslash( $_POST['customized'] ), true );
379 $this->_post_values = false;
382 if ( isset( $this->_post_values[ $setting->id ] ) )
383 return $setting->sanitize( $this->_post_values[ $setting->id ] );
387 * Print javascript settings.
391 public function customize_preview_init() {
392 $this->nonce_tick = check_ajax_referer( 'preview-customize_' . $this->get_stylesheet(), 'nonce' );
394 $this->prepare_controls();
396 wp_enqueue_script( 'customize-preview' );
397 add_action( 'wp_head', array( $this, 'customize_preview_base' ) );
398 add_action( 'wp_head', array( $this, 'customize_preview_html5' ) );
399 add_action( 'wp_footer', array( $this, 'customize_preview_settings' ), 20 );
400 add_action( 'shutdown', array( $this, 'customize_preview_signature' ), 1000 );
401 add_filter( 'wp_die_handler', array( $this, 'remove_preview_signature' ) );
403 foreach ( $this->settings as $setting ) {
408 * Fires once the Customizer preview has initialized and JavaScript
409 * settings have been printed.
413 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
415 do_action( 'customize_preview_init', $this );
419 * Print base element for preview frame.
423 public function customize_preview_base() {
424 ?><base href="<?php echo home_url( '/' ); ?>" /><?php
428 * Print a workaround to handle HTML5 tags in IE < 9
432 public function customize_preview_html5() { ?>
434 <script type="text/javascript">
435 var e = [ 'abbr', 'article', 'aside', 'audio', 'canvas', 'datalist', 'details',
436 'figure', 'footer', 'header', 'hgroup', 'mark', 'menu', 'meter', 'nav',
437 'output', 'progress', 'section', 'time', 'video' ];
438 for ( var i = 0; i < e.length; i++ ) {
439 document.createElement( e[i] );
446 * Print javascript settings for preview frame.
450 public function customize_preview_settings() {
453 'channel' => esc_js( $_POST['customize_messenger_channel'] ),
456 if ( 2 == $this->nonce_tick ) {
457 $settings['nonce'] = array(
458 'save' => wp_create_nonce( 'save-customize_' . $this->get_stylesheet() ),
459 'preview' => wp_create_nonce( 'preview-customize_' . $this->get_stylesheet() )
463 foreach ( $this->settings as $id => $setting ) {
464 $settings['values'][ $id ] = $setting->js_value();
468 <script type="text/javascript">
469 var _wpCustomizeSettings = <?php echo json_encode( $settings ); ?>;
475 * Prints a signature so we can ensure the customizer was properly executed.
479 public function customize_preview_signature() {
480 echo 'WP_CUSTOMIZER_SIGNATURE';
484 * Removes the signature in case we experience a case where the customizer was not properly executed.
488 public function remove_preview_signature( $return = null ) {
489 remove_action( 'shutdown', array( $this, 'customize_preview_signature' ), 1000 );
495 * Is it a theme preview?
499 * @return bool True if it's a preview, false if not.
501 public function is_preview() {
502 return (bool) $this->previewing;
506 * Retrieve the template name of the previewed theme.
510 * @return string Template name.
512 public function get_template() {
513 return $this->theme()->get_template();
517 * Retrieve the stylesheet name of the previewed theme.
521 * @return string Stylesheet name.
523 public function get_stylesheet() {
524 return $this->theme()->get_stylesheet();
528 * Retrieve the template root of the previewed theme.
532 * @return string Theme root.
534 public function get_template_root() {
535 return get_raw_theme_root( $this->get_template(), true );
539 * Retrieve the stylesheet root of the previewed theme.
543 * @return string Theme root.
545 public function get_stylesheet_root() {
546 return get_raw_theme_root( $this->get_stylesheet(), true );
550 * Filter the current theme and return the name of the previewed theme.
554 * @param $current_theme {@internal Parameter is not used}
555 * @return string Theme name.
557 public function current_theme( $current_theme ) {
558 return $this->theme()->display('Name');
562 * Switch the theme and trigger the save() method on each setting.
566 public function save() {
567 if ( ! $this->is_preview() )
570 check_ajax_referer( 'save-customize_' . $this->get_stylesheet(), 'nonce' );
572 // Do we have to switch themes?
573 if ( ! $this->is_theme_active() ) {
574 // Temporarily stop previewing the theme to allow switch_themes()
575 // to operate properly.
576 $this->stop_previewing_theme();
577 switch_theme( $this->get_stylesheet() );
578 update_option( 'theme_switched_via_customizer', true );
579 $this->start_previewing_theme();
583 * Fires once the theme has switched in the Customizer, but before settings
588 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
590 do_action( 'customize_save', $this );
592 foreach ( $this->settings as $setting ) {
597 * Fires after Customize settings have been saved.
601 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
603 do_action( 'customize_save_after', $this );
609 * Add a customize setting.
613 * @param WP_Customize_Setting|string $id Customize Setting object, or ID.
614 * @param array $args Setting arguments; passed to WP_Customize_Setting
617 public function add_setting( $id, $args = array() ) {
618 if ( is_a( $id, 'WP_Customize_Setting' ) )
621 $setting = new WP_Customize_Setting( $this, $id, $args );
623 $this->settings[ $setting->id ] = $setting;
627 * Retrieve a customize setting.
631 * @param string $id Customize Setting ID.
632 * @return WP_Customize_Setting
634 public function get_setting( $id ) {
635 if ( isset( $this->settings[ $id ] ) )
636 return $this->settings[ $id ];
640 * Remove a customize setting.
644 * @param string $id Customize Setting ID.
646 public function remove_setting( $id ) {
647 unset( $this->settings[ $id ] );
651 * Add a customize section.
655 * @param WP_Customize_Section|string $id Customize Section object, or Section ID.
656 * @param array $args Section arguments.
658 public function add_section( $id, $args = array() ) {
659 if ( is_a( $id, 'WP_Customize_Section' ) )
662 $section = new WP_Customize_Section( $this, $id, $args );
664 $this->sections[ $section->id ] = $section;
668 * Retrieve a customize section.
672 * @param string $id Section ID.
673 * @return WP_Customize_Section
675 public function get_section( $id ) {
676 if ( isset( $this->sections[ $id ] ) )
677 return $this->sections[ $id ];
681 * Remove a customize section.
685 * @param string $id Section ID.
687 public function remove_section( $id ) {
688 unset( $this->sections[ $id ] );
692 * Add a customize control.
696 * @param WP_Customize_Control|string $id Customize Control object, or ID.
697 * @param array $args Control arguments; passed to WP_Customize_Control
700 public function add_control( $id, $args = array() ) {
701 if ( is_a( $id, 'WP_Customize_Control' ) )
704 $control = new WP_Customize_Control( $this, $id, $args );
706 $this->controls[ $control->id ] = $control;
710 * Retrieve a customize control.
714 * @param string $id ID of the control.
715 * @return WP_Customize_Control $control The control object.
717 public function get_control( $id ) {
718 if ( isset( $this->controls[ $id ] ) )
719 return $this->controls[ $id ];
723 * Remove a customize control.
727 * @param string $id ID of the control.
729 public function remove_control( $id ) {
730 unset( $this->controls[ $id ] );
734 * Helper function to compare two objects by priority.
738 * @param object $a Object A.
739 * @param object $b Object B.
742 protected final function _cmp_priority( $a, $b ) {
748 return ( $ap > $bp ) ? 1 : -1;
752 * Prepare settings and sections.
754 * For each, check if required related components exist,
755 * whether the user has the necessary capabilities,
756 * and sort by priority.
760 public function prepare_controls() {
762 $this->controls = array_reverse( $this->controls );
765 foreach ( $this->controls as $id => $control ) {
766 if ( ! isset( $this->sections[ $control->section ] ) || ! $control->check_capabilities() )
769 $this->sections[ $control->section ]->controls[] = $control;
770 $controls[ $id ] = $control;
772 $this->controls = $controls;
775 // Reversing makes uasort sort by time added when conflicts occur.
776 $this->sections = array_reverse( $this->sections );
777 uasort( $this->sections, array( $this, '_cmp_priority' ) );
780 foreach ( $this->sections as $section ) {
781 if ( ! $section->check_capabilities() || ! $section->controls )
784 usort( $section->controls, array( $this, '_cmp_priority' ) );
785 $sections[] = $section;
787 $this->sections = $sections;
791 * Enqueue scripts for customize controls.
795 public function enqueue_control_scripts() {
796 foreach ( $this->controls as $control ) {
802 * Register some default controls.
806 public function register_controls() {
808 /* Site Title & Tagline */
810 $this->add_section( 'title_tagline', array(
811 'title' => __( 'Site Title & Tagline' ),
815 $this->add_setting( 'blogname', array(
816 'default' => get_option( 'blogname' ),
818 'capability' => 'manage_options',
821 $this->add_control( 'blogname', array(
822 'label' => __( 'Site Title' ),
823 'section' => 'title_tagline',
826 $this->add_setting( 'blogdescription', array(
827 'default' => get_option( 'blogdescription' ),
829 'capability' => 'manage_options',
832 $this->add_control( 'blogdescription', array(
833 'label' => __( 'Tagline' ),
834 'section' => 'title_tagline',
839 $this->add_section( 'colors', array(
840 'title' => __( 'Colors' ),
844 $this->add_setting( 'header_textcolor', array(
845 'theme_supports' => array( 'custom-header', 'header-text' ),
846 'default' => get_theme_support( 'custom-header', 'default-text-color' ),
848 'sanitize_callback' => array( $this, '_sanitize_header_textcolor' ),
849 'sanitize_js_callback' => 'maybe_hash_hex_color',
852 // Input type: checkbox
854 $this->add_control( 'display_header_text', array(
855 'settings' => 'header_textcolor',
856 'label' => __( 'Display Header Text' ),
857 'section' => 'title_tagline',
858 'type' => 'checkbox',
861 $this->add_control( new WP_Customize_Color_Control( $this, 'header_textcolor', array(
862 'label' => __( 'Header Text Color' ),
863 'section' => 'colors',
867 // With sanitize_callback
868 $this->add_setting( 'background_color', array(
869 'default' => get_theme_support( 'custom-background', 'default-color' ),
870 'theme_supports' => 'custom-background',
872 'sanitize_callback' => 'sanitize_hex_color_no_hash',
873 'sanitize_js_callback' => 'maybe_hash_hex_color',
876 $this->add_control( new WP_Customize_Color_Control( $this, 'background_color', array(
877 'label' => __( 'Background Color' ),
878 'section' => 'colors',
884 $this->add_section( 'header_image', array(
885 'title' => __( 'Header Image' ),
886 'theme_supports' => 'custom-header',
890 $this->add_setting( new WP_Customize_Filter_Setting( $this, 'header_image', array(
891 'default' => get_theme_support( 'custom-header', 'default-image' ),
892 'theme_supports' => 'custom-header',
895 $this->add_setting( new WP_Customize_Header_Image_Setting( $this, 'header_image_data', array(
896 // 'default' => get_theme_support( 'custom-header', 'default-image' ),
897 'theme_supports' => 'custom-header',
900 $this->add_control( new WP_Customize_Header_Image_Control( $this ) );
902 /* Custom Background */
904 $this->add_section( 'background_image', array(
905 'title' => __( 'Background Image' ),
906 'theme_supports' => 'custom-background',
910 $this->add_setting( 'background_image', array(
911 'default' => get_theme_support( 'custom-background', 'default-image' ),
912 'theme_supports' => 'custom-background',
915 $this->add_setting( new WP_Customize_Background_Image_Setting( $this, 'background_image_thumb', array(
916 'theme_supports' => 'custom-background',
919 $this->add_control( new WP_Customize_Background_Image_Control( $this ) );
921 $this->add_setting( 'background_repeat', array(
922 'default' => 'repeat',
923 'theme_supports' => 'custom-background',
926 $this->add_control( 'background_repeat', array(
927 'label' => __( 'Background Repeat' ),
928 'section' => 'background_image',
931 'no-repeat' => __('No Repeat'),
932 'repeat' => __('Tile'),
933 'repeat-x' => __('Tile Horizontally'),
934 'repeat-y' => __('Tile Vertically'),
938 $this->add_setting( 'background_position_x', array(
940 'theme_supports' => 'custom-background',
943 $this->add_control( 'background_position_x', array(
944 'label' => __( 'Background Position' ),
945 'section' => 'background_image',
948 'left' => __('Left'),
949 'center' => __('Center'),
950 'right' => __('Right'),
954 $this->add_setting( 'background_attachment', array(
955 'default' => 'fixed',
956 'theme_supports' => 'custom-background',
959 $this->add_control( 'background_attachment', array(
960 'label' => __( 'Background Attachment' ),
961 'section' => 'background_image',
964 'fixed' => __('Fixed'),
965 'scroll' => __('Scroll'),
969 // If the theme is using the default background callback, we can update
970 // the background CSS using postMessage.
971 if ( get_theme_support( 'custom-background', 'wp-head-callback' ) === '_custom_background_cb' ) {
972 foreach ( array( 'color', 'image', 'position_x', 'repeat', 'attachment' ) as $prop ) {
973 $this->get_setting( 'background_' . $prop )->transport = 'postMessage';
979 $locations = get_registered_nav_menus();
980 $menus = wp_get_nav_menus();
981 $menu_locations = get_nav_menu_locations();
982 $num_locations = count( array_keys( $locations ) );
984 $this->add_section( 'nav', array(
985 'title' => __( 'Navigation' ),
986 'theme_supports' => 'menus',
988 '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.'),
992 $choices = array( 0 => __( '— Select —' ) );
993 foreach ( $menus as $menu ) {
994 $choices[ $menu->term_id ] = wp_html_excerpt( $menu->name, 40, '…' );
997 foreach ( $locations as $location => $description ) {
998 $menu_setting_id = "nav_menu_locations[{$location}]";
1000 $this->add_setting( $menu_setting_id, array(
1001 'sanitize_callback' => 'absint',
1002 'theme_supports' => 'menus',
1005 $this->add_control( $menu_setting_id, array(
1006 'label' => $description,
1009 'choices' => $choices,
1014 /* Static Front Page */
1017 $this->add_section( 'static_front_page', array(
1018 'title' => __( 'Static Front Page' ),
1019 // 'theme_supports' => 'static-front-page',
1021 'description' => __( 'Your theme supports a static front page.' ),
1024 $this->add_setting( 'show_on_front', array(
1025 'default' => get_option( 'show_on_front' ),
1026 'capability' => 'manage_options',
1028 // 'theme_supports' => 'static-front-page',
1031 $this->add_control( 'show_on_front', array(
1032 'label' => __( 'Front page displays' ),
1033 'section' => 'static_front_page',
1036 'posts' => __( 'Your latest posts' ),
1037 'page' => __( 'A static page' ),
1041 $this->add_setting( 'page_on_front', array(
1043 'capability' => 'manage_options',
1044 // 'theme_supports' => 'static-front-page',
1047 $this->add_control( 'page_on_front', array(
1048 'label' => __( 'Front page' ),
1049 'section' => 'static_front_page',
1050 'type' => 'dropdown-pages',
1053 $this->add_setting( 'page_for_posts', array(
1055 'capability' => 'manage_options',
1056 // 'theme_supports' => 'static-front-page',
1059 $this->add_control( 'page_for_posts', array(
1060 'label' => __( 'Posts page' ),
1061 'section' => 'static_front_page',
1062 'type' => 'dropdown-pages',
1067 * Callback for validating the header_textcolor value.
1069 * Accepts 'blank', and otherwise uses sanitize_hex_color_no_hash().
1070 * Returns default text color if hex color is empty.
1074 * @param string $color
1077 public function _sanitize_header_textcolor( $color ) {
1078 if ( 'blank' === $color )
1081 $color = sanitize_hex_color_no_hash( $color );
1082 if ( empty( $color ) )
1083 $color = get_theme_support( 'custom-header', 'default-text-color' );
1090 * Sanitizes a hex color.
1092 * Returns either '', a 3 or 6 digit hex color (with #), or null.
1093 * For sanitizing values without a #, see sanitize_hex_color_no_hash().
1097 * @param string $color
1098 * @return string|null
1100 function sanitize_hex_color( $color ) {
1101 if ( '' === $color )
1104 // 3 or 6 hex digits, or the empty string.
1105 if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) )
1112 * Sanitizes a hex color without a hash. Use sanitize_hex_color() when possible.
1114 * Saving hex colors without a hash puts the burden of adding the hash on the
1115 * UI, which makes it difficult to use or upgrade to other color types such as
1116 * rgba, hsl, rgb, and html color names.
1118 * Returns either '', a 3 or 6 digit hex color (without a #), or null.
1121 * @uses sanitize_hex_color()
1123 * @param string $color
1124 * @return string|null
1126 function sanitize_hex_color_no_hash( $color ) {
1127 $color = ltrim( $color, '#' );
1129 if ( '' === $color )
1132 return sanitize_hex_color( '#' . $color ) ? $color : null;
1136 * Ensures that any hex color is properly hashed.
1137 * Otherwise, returns value untouched.
1139 * This method should only be necessary if using sanitize_hex_color_no_hash().
1143 * @param string $color
1146 function maybe_hash_hex_color( $color ) {
1147 if ( $unhashed = sanitize_hex_color_no_hash( $color ) )
1148 return '#' . $unhashed;