WordPress.org

Make WordPress Core


Ignore:
Timestamp:
04/21/2016 07:21:40 PM (6 years ago)
Author:
westonruter
Message:

Customize/Formatting: Move sanitize_hex_color(), sanitize_hex_color_no_hash(), and maybe_hash_hex_color() from class-wp-customize-manager.php into formatting.php.

Adds missing braces.

See #33413.
Props downstairsdev, tollmanz.
Fixes #27583.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/formatting.php

    r36970 r37283  
    48044804    return $short_url;
    48054805}
     4806
     4807/**
     4808 * Sanitizes a hex color.
     4809 *
     4810 * Returns either '', a 3 or 6 digit hex color (with #), or nothing.
     4811 * For sanitizing values without a #, see sanitize_hex_color_no_hash().
     4812 *
     4813 * @since 3.4.0
     4814 *
     4815 * @param string $color
     4816 * @return string|void
     4817 */
     4818function sanitize_hex_color( $color ) {
     4819    if ( '' === $color ) {
     4820        return '';
     4821    }
     4822
     4823    // 3 or 6 hex digits, or the empty string.
     4824    if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) ) {
     4825        return $color;
     4826    }
     4827}
     4828
     4829/**
     4830 * Sanitizes a hex color without a hash. Use sanitize_hex_color() when possible.
     4831 *
     4832 * Saving hex colors without a hash puts the burden of adding the hash on the
     4833 * UI, which makes it difficult to use or upgrade to other color types such as
     4834 * rgba, hsl, rgb, and html color names.
     4835 *
     4836 * Returns either '', a 3 or 6 digit hex color (without a #), or null.
     4837 *
     4838 * @since 3.4.0
     4839 *
     4840 * @param string $color
     4841 * @return string|null
     4842 */
     4843function sanitize_hex_color_no_hash( $color ) {
     4844    $color = ltrim( $color, '#' );
     4845
     4846    if ( '' === $color ) {
     4847        return '';
     4848    }
     4849
     4850    return sanitize_hex_color( '#' . $color ) ? $color : null;
     4851}
     4852
     4853/**
     4854 * Ensures that any hex color is properly hashed.
     4855 * Otherwise, returns value untouched.
     4856 *
     4857 * This method should only be necessary if using sanitize_hex_color_no_hash().
     4858 *
     4859 * @since 3.4.0
     4860 *
     4861 * @param string $color
     4862 * @return string
     4863 */
     4864function maybe_hash_hex_color( $color ) {
     4865    if ( $unhashed = sanitize_hex_color_no_hash( $color ) ) {
     4866        return '#' . $unhashed;
     4867    }
     4868
     4869    return $color;
     4870}
Note: See TracChangeset for help on using the changeset viewer.