Make WordPress Core

Ticket #27583: 27583.2.diff

File 27583.2.diff, 6.7 KB (added by tollmanz, 11 years ago)

Wrap sanitize_hex_color with is_hex_color

  • src/wp-includes/class-wp-customize-manager.php

     
    11821182}
    11831183
    11841184/**
    1185  * Sanitizes a hex color.
    1186  *
    1187  * Returns either '', a 3 or 6 digit hex color (with #), or null.
    1188  * For sanitizing values without a #, see sanitize_hex_color_no_hash().
    1189  *
    1190  * @since 3.4.0
    1191  *
    1192  * @param string $color
    1193  * @return string|null
    1194  */
    1195 function sanitize_hex_color( $color ) {
    1196         if ( '' === $color )
    1197                 return '';
    1198 
    1199         // 3 or 6 hex digits, or the empty string.
    1200         if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) )
    1201                 return $color;
    1202 
    1203         return null;
    1204 }
    1205 
    1206 /**
    12071185 * Sanitizes a hex color without a hash. Use sanitize_hex_color() when possible.
    12081186 *
    12091187 * Saving hex colors without a hash puts the burden of adding the hash on the
     
    12151193 * @since 3.4.0
    12161194 * @uses sanitize_hex_color()
    12171195 *
    1218  * @param string $color
    1219  * @return string|null
     1196 * @param string $color The hex color to remove the hash from.
     1197 * @return string|null The color with the removed hash.
    12201198 */
    12211199function sanitize_hex_color_no_hash( $color ) {
    12221200        $color = ltrim( $color, '#' );
    12231201
    1224         if ( '' === $color )
     1202        if ( '' === $color ) {
    12251203                return '';
     1204        }
    12261205
    1227         return sanitize_hex_color( '#' . $color ) ? $color : null;
     1206        if ( is_hex_color( '#' . $color ) ) {
     1207                return $color;
     1208        } else {
     1209                return null;
     1210        }
    12281211}
    12291212
    12301213/**
     
    12351218 *
    12361219 * @since 3.4.0
    12371220 *
    1238  * @param string $color
    1239  * @return string
     1221 * @param string $color The color to "sanitize" to prepend a hash to.
     1222 * @return string The color with the prepended hash.
    12401223 */
    12411224function maybe_hash_hex_color( $color ) {
    1242         if ( $unhashed = sanitize_hex_color_no_hash( $color ) )
     1225        if ( $unhashed = sanitize_hex_color_no_hash( $color ) ) {
    12431226                return '#' . $unhashed;
     1227        }
    12441228
    12451229        return $color;
    12461230}
  • src/wp-includes/formatting.php

     
    39723972
    39733973        return $spaces;
    39743974}
     3975
     3976/**
     3977 * Validates a hex color.
     3978 *
     3979 * @since 4.0.0
     3980 *
     3981 * @param string $color The color to validate.
     3982 * @return bool True if the value is a valid hex color; false if it is not.
     3983 */
     3984function is_hex_color( $color ) {
     3985        $clean_color = sanitize_hex_color( $color );
     3986        return ! empty( $clean_color );
     3987}
     3988
     3989/**
     3990 * Sanitizes a hex color.
     3991 *
     3992 * Returns either '', a 3 or 6 digit hex color (with #), or null.
     3993 *
     3994 * @since 4.0.0
     3995 *
     3996 * @param string $color The color to "sanitize".
     3997 * @return string|null The color value if value; empty string if empty; null if not valid.
     3998 */
     3999function sanitize_hex_color( $color ) {
     4000        if ( '' === $color ) {
     4001                return '';
     4002        }
     4003
     4004        // 3 or 6 hex digits, or the empty string.
     4005        if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) ) {
     4006                return $color;
     4007        }
     4008
     4009        return null;
     4010}
     4011 No newline at end of file
  • tests/phpunit/tests/formatting/color.php

     
     1<?php
     2/**
     3 * @group formatting
     4 * @group color
     5 */
     6class Tests_Formatting_Color extends WP_UnitTestCase {
     7        /**
     8         * @ticket 27583
     9         */
     10        function test_valid_colors_pass_validation() {
     11                // Numeric 3 digit
     12                $color = '#333';
     13                $this->assertTrue( is_hex_color( $color ) );
     14
     15                // Alphanumeric 3 character
     16                $color = '#f4A';
     17                $this->assertTrue( is_hex_color( $color ) );
     18
     19                // Alpha 3 character
     20                $color = '#fff';
     21                $this->assertTrue( is_hex_color( $color ) );
     22
     23                // Numeric 6 digit
     24                $color = '#333333';
     25                $this->assertTrue( is_hex_color( $color ) );
     26
     27                // Alphanumeric 6 character
     28                $color = '#ff44AA';
     29                $this->assertTrue( is_hex_color( $color ) );
     30
     31                // Alpha 6 character
     32                $color = '#ffffff';
     33                $this->assertTrue( is_hex_color( $color ) );
     34        }
     35
     36        /**
     37         * @ticket 27583
     38         */
     39        function test_invalid_colors_do_not_pass() {
     40                // Too many characters
     41                $color = '#1234567';
     42                $this->assertFalse( is_hex_color( $color ) );
     43
     44                // Too few characters
     45                $color = '#12';
     46                $this->assertFalse( is_hex_color( $color ) );
     47
     48                // Missing #
     49                $color = 'ffffff';
     50                $this->assertFalse( is_hex_color( $color ) );
     51
     52                // Invalid character
     53                $color = '#f9a%ad';
     54                $this->assertFalse( is_hex_color( $color ) );
     55
     56                // Empty string
     57                $color = '';
     58                $this->assertFalse( is_hex_color( $color ) );
     59
     60                // Whitespace
     61                $color = '#ff fff';
     62                $this->assertFalse( is_hex_color( $color ) );
     63        }
     64
     65        function test_removal_of_hash_from_colors() {
     66                require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
     67
     68                // Numeric 3 digit
     69                $color = '#333';
     70                $color_no_hash = '333';
     71                $this->assertEquals( $color_no_hash, sanitize_hex_color_no_hash( $color ) );
     72
     73                // Alphanumeric 3 character
     74                $color = '#f4A';
     75                $color_no_hash = 'f4A';
     76                $this->assertEquals( $color_no_hash, sanitize_hex_color_no_hash( $color ) );
     77
     78                // Alpha 3 character
     79                $color = '#fff';
     80                $color_no_hash = 'fff';
     81                $this->assertEquals( $color_no_hash, sanitize_hex_color_no_hash( $color ) );
     82
     83                // Numeric 6 digit
     84                $color = '#333333';
     85                $color_no_hash = '333333';
     86                $this->assertEquals( $color_no_hash, sanitize_hex_color_no_hash( $color ) );
     87
     88                // Alphanumeric 6 character
     89                $color = '#ff44AA';
     90                $color_no_hash = 'ff44AA';
     91                $this->assertEquals( $color_no_hash, sanitize_hex_color_no_hash( $color ) );
     92
     93                // Alpha 6 character
     94                $color = '#ffffff';
     95                $color_no_hash = 'ffffff';
     96                $this->assertEquals( $color_no_hash, sanitize_hex_color_no_hash( $color ) );
     97        }
     98
     99        function test_prepend_hash_on_color_without_hash() {
     100                require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
     101
     102                // Numeric 3 digit
     103                $color = '#333';
     104                $color_no_hash = '333';
     105                $this->assertEquals( $color, maybe_hash_hex_color( $color_no_hash ) );
     106
     107                // Alphanumeric 3 character
     108                $color = '#f4A';
     109                $color_no_hash = 'f4A';
     110                $this->assertEquals( $color, maybe_hash_hex_color( $color_no_hash ) );
     111
     112                // Alpha 3 character
     113                $color = '#fff';
     114                $color_no_hash = 'fff';
     115                $this->assertEquals( $color, maybe_hash_hex_color( $color_no_hash ) );
     116
     117                // Numeric 6 digit
     118                $color = '#333333';
     119                $color_no_hash = '333333';
     120                $this->assertEquals( $color, maybe_hash_hex_color( $color_no_hash ) );
     121
     122                // Alphanumeric 6 character
     123                $color = '#ff44AA';
     124                $color_no_hash = 'ff44AA';
     125                $this->assertEquals( $color, maybe_hash_hex_color( $color_no_hash ) );
     126
     127                // Alpha 6 character
     128                $color = '#ffffff';
     129                $color_no_hash = 'ffffff';
     130                $this->assertEquals( $color, maybe_hash_hex_color( $color_no_hash ) );
     131        }
     132}
     133 No newline at end of file