Changeset 57657
- Timestamp:
- 02/20/2024 07:09:43 AM (10 months ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/fonts/class-wp-font-collection.php
r57539 r57657 220 220 'font_family_settings' => array( 221 221 'name' => 'sanitize_text_field', 222 'slug' => 'sanitize_title', 223 'fontFamily' => 'sanitize_text_field', 222 'slug' => static function ( $value ) { 223 return _wp_to_kebab_case( sanitize_title( $value ) ); 224 }, 225 'fontFamily' => 'WP_Font_Utils::sanitize_font_family', 224 226 'preview' => 'sanitize_url', 225 227 'fontFace' => array( -
trunk/src/wp-includes/fonts/class-wp-font-utils.php
r57632 r57657 20 20 class WP_Font_Utils { 21 21 /** 22 * Adds surrounding quotes to font family names that contain special characters. 23 * 24 * It follows the recommendations from the CSS Fonts Module Level 4. 25 * @link https://www.w3.org/TR/css-fonts-4/#font-family-prop 26 * 27 * @since 6.5.0 28 * 29 * @param string $item A font family name. 30 * @return string The font family name with surrounding quotes, if necessary. 31 */ 32 private static function maybe_add_quotes( $item ) { 33 // Matches strings that are not exclusively alphabetic characters or hyphens, and do not exactly follow the pattern generic(alphabetic characters or hyphens). 34 $regex = '/^(?!generic\([a-zA-Z\-]+\)$)(?!^[a-zA-Z\-]+$).+/'; 35 $item = trim( $item ); 36 if ( preg_match( $regex, $item ) ) { 37 $item = trim( $item, "\"'" ); 38 return '"' . $item . '"'; 39 } 40 return $item; 41 } 42 43 /** 22 44 * Sanitizes and formats font family names. 23 45 * 24 * - Applies `sanitize_text_field` 25 * - Adds surrounding quotes to names that contain spaces and are not already quoted 46 * - Applies `sanitize_text_field`. 47 * - Adds surrounding quotes to names containing any characters that are not alphabetic or dashes. 48 * 49 * It follows the recommendations from the CSS Fonts Module Level 4. 50 * @link https://www.w3.org/TR/css-fonts-4/#font-family-prop 26 51 * 27 52 * @since 6.5.0 … … 38 63 } 39 64 40 $font_family = sanitize_text_field( $font_family ); 41 $font_families = explode( ',', $font_family ); 42 $wrapped_font_families = array_map( 43 function ( $family ) { 44 $trimmed = trim( $family ); 45 if ( ! empty( $trimmed ) && str_contains( $trimmed, ' ' ) && ! str_contains( $trimmed, "'" ) && ! str_contains( $trimmed, '"' ) ) { 46 return '"' . $trimmed . '"'; 65 $output = sanitize_text_field( $font_family ); 66 $formatted_items = array(); 67 if ( str_contains( $output, ',' ) ) { 68 $items = explode( ',', $output ); 69 foreach ( $items as $item ) { 70 $formatted_item = self::maybe_add_quotes( $item ); 71 if ( ! empty( $formatted_item ) ) { 72 $formatted_items[] = $formatted_item; 47 73 } 48 return $trimmed; 49 }, 50 $font_families 51 ); 52 53 if ( count( $wrapped_font_families ) === 1 ) { 54 $font_family = $wrapped_font_families[0]; 55 } else { 56 $font_family = implode( ', ', $wrapped_font_families ); 57 } 58 59 return $font_family; 74 } 75 return implode( ', ', $formatted_items ); 76 } 77 return self::maybe_add_quotes( $output ); 60 78 } 61 79 -
trunk/tests/phpunit/tests/fonts/font-library/wpFontCollection/getData.php
r57539 r57657 160 160 array( 161 161 'font_family_settings' => array( 162 'fontFamily' => ' Open Sans, sans-serif',162 'fontFamily' => '"Open Sans", sans-serif', 163 163 'slug' => 'open-sans', 164 164 'name' => 'Open Sans', -
trunk/tests/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFontFamily.php
r57539 r57657 36 36 return array( 37 37 'data_families_with_spaces_and_numbers' => array( 38 'font_family' => ' Rock 3D , Open Sans,serif',39 'expected' => ' "Rock 3D", "Open Sans", serif',38 'font_family' => 'Arial, Rock 3D , Open Sans,serif', 39 'expected' => 'Arial, "Rock 3D", "Open Sans", serif', 40 40 ), 41 41 'data_single_font_family' => array( 42 42 'font_family' => 'Rock 3D', 43 43 'expected' => '"Rock 3D"', 44 ),45 'data_no_spaces' => array(46 'font_family' => 'Rock3D',47 'expected' => 'Rock3D',48 44 ), 49 45 'data_many_spaces_and_existing_quotes' => array( … … 59 55 'expected' => '"Rock 3D"', 60 56 ), 57 'data_font_family_with_generic_names' => array( 58 'font_family' => 'generic(kai), generic(font[name]), generic(fangsong), Rock 3D', 59 'expected' => 'generic(kai), "generic(font[name])", generic(fangsong), "Rock 3D"', 60 ), 61 61 ); 62 62 } -
trunk/tests/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php
r57548 r57657 682 682 $settings = array( 683 683 'name' => 'Open Sans', 684 'fontFamily' => ' "Open Sans, "Noto Sans", sans-serif',684 'fontFamily' => 'Open Sans, "Noto Sans", sans-serif', 685 685 'preview' => 'https://s.w.org/images/fonts/16.9/previews/open-sans/open-sans-400-normal.svg', 686 686 ); … … 701 701 'name' => $settings['name'], 702 702 'slug' => 'open-sans-2', 703 'fontFamily' => $settings['fontFamily'],703 'fontFamily' => '"Open Sans", "Noto Sans", sans-serif', 704 704 'preview' => $settings['preview'], 705 705 );
Note: See TracChangeset
for help on using the changeset viewer.