Make WordPress Core

Changeset 37283

Timestamp:
04/21/2016 07:21:40 PM (8 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.

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r37197 r37283  
    22372237    }
    22382238}
    2239 
    2240 /**
    2241  * Sanitizes a hex color.
    2242  *
    2243  * Returns either '', a 3 or 6 digit hex color (with #), or nothing.
    2244  * For sanitizing values without a #, see sanitize_hex_color_no_hash().
    2245  *
    2246  * @since 3.4.0
    2247  *
    2248  * @param string $color
    2249  * @return string|void
    2250  */
    2251 function sanitize_hex_color( $color ) {
    2252     if ( '' === $color )
    2253         return '';
    2254 
    2255     // 3 or 6 hex digits, or the empty string.
    2256     if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) )
    2257         return $color;
    2258 }
    2259 
    2260 /**
    2261  * Sanitizes a hex color without a hash. Use sanitize_hex_color() when possible.
    2262  *
    2263  * Saving hex colors without a hash puts the burden of adding the hash on the
    2264  * UI, which makes it difficult to use or upgrade to other color types such as
    2265  * rgba, hsl, rgb, and html color names.
    2266  *
    2267  * Returns either '', a 3 or 6 digit hex color (without a #), or null.
    2268  *
    2269  * @since 3.4.0
    2270  *
    2271  * @param string $color
    2272  * @return string|null
    2273  */
    2274 function sanitize_hex_color_no_hash( $color ) {
    2275     $color = ltrim( $color, '#' );
    2276 
    2277     if ( '' === $color )
    2278         return '';
    2279 
    2280     return sanitize_hex_color( '#' . $color ) ? $color : null;
    2281 }
    2282 
    2283 /**
    2284  * Ensures that any hex color is properly hashed.
    2285  * Otherwise, returns value untouched.
    2286  *
    2287  * This method should only be necessary if using sanitize_hex_color_no_hash().
    2288  *
    2289  * @since 3.4.0
    2290  *
    2291  * @param string $color
    2292  * @return string
    2293  */
    2294 function maybe_hash_hex_color( $color ) {
    2295     if ( $unhashed = sanitize_hex_color_no_hash( $color ) )
    2296         return '#' . $unhashed;
    2297 
    2298     return $color;
    2299 }
  • trunk/src/wp-includes/formatting.php

    r36970 r37283  
    48044804    return $short_url;
    48054805}
     4806
     4807
     4808
     4809
     4810
     4811
     4812
     4813
     4814
     4815
     4816
     4817
     4818
     4819
     4820
     4821
     4822
     4823
     4824
     4825
     4826
     4827
     4828
     4829
     4830
     4831
     4832
     4833
     4834
     4835
     4836
     4837
     4838
     4839
     4840
     4841
     4842
     4843
     4844
     4845
     4846
     4847
     4848
     4849
     4850
     4851
     4852
     4853
     4854
     4855
     4856
     4857
     4858
     4859
     4860
     4861
     4862
     4863
     4864
     4865
     4866
     4867
     4868
     4869
     4870
Note: See TracChangeset for help on using the changeset viewer.