| @@ -9,9 +9,9 @@ | ||
| 9 | 9 | |
| 10 | 10 | /** |
| 11 | 11 | * Get a list of Google fonts |
| 12 | 12 | * |
| 13 | - * @return Array | |
| 13 | + * @return array | |
| 14 | 14 | */ |
| 15 | 15 | public static function get_google_fonts() { |
| 16 | 16 | // https://www.googleapis.com/webfonts/v1/webfonts?sort=alpha |
| 17 | 17 | $google_fonts_file = WPO_PLUGIN_MAIN_PATH.'google-fonts.json'; |
| @@ -21,9 +21,9 @@ | ||
| 21 | 21 | return array(); |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | /** |
| 25 | - * Check if the google font exist or not | |
| 25 | + * Check if the Google font exist or not | |
| 26 | 26 | * |
| 27 | 27 | * @param string $font |
| 28 | 28 | * @return boolean |
| 29 | 29 | */ |
| @@ -50,9 +50,9 @@ | ||
| 50 | 50 | self::convert_v1_font_specs_to_v2(); |
| 51 | 51 | $merge = self::build(); |
| 52 | 52 | $config = wp_optimize_minify_config(); |
| 53 | 53 | /** |
| 54 | - * Filters wether to add display=swap to Google fonts urls | |
| 54 | + * Filters whether to add display=swap to Google fonts urls | |
| 55 | 55 | * |
| 56 | 56 | * @param boolean $display - Default to true |
| 57 | 57 | */ |
| 58 | 58 | if (apply_filters('wpo_minify_gfont_display_swap', $config->get('enable_display_swap'))) { |
| @@ -70,8 +70,10 @@ | ||
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | /** |
| 73 | 73 | * Parses font url based on whether it is API version 1 or 2 |
| 74 | + * | |
| 75 | + * @param string $font | |
| 74 | 76 | */ |
| 75 | 77 | private static function parse_font_url($font) { |
| 76 | 78 | if (false !== strpos($font, 'css?')) { |
| 77 | 79 | self::parse_font_api1_url($font); |
| @@ -81,11 +83,13 @@ | ||
| 81 | 83 | } |
| 82 | 84 | |
| 83 | 85 | /** |
| 84 | 86 | * Parses google font api version 1 url |
| 87 | + * | |
| 88 | + * @param string $font | |
| 85 | 89 | */ |
| 86 | 90 | private static function parse_font_api1_url($font) { |
| 87 | - parse_str(parse_url(rtrim($font, '|'), PHP_URL_QUERY), $font_elements); | |
| 91 | + parse_str(wp_parse_url(rtrim($font, '|'), PHP_URL_QUERY), $font_elements); | |
| 88 | 92 | // Process each font family |
| 89 | 93 | foreach (explode('|', $font_elements['family']) as $font_family) { |
| 90 | 94 | // Separate font and sizes |
| 91 | 95 | $font_family = explode(':', $font_family); |
| @@ -94,9 +98,9 @@ | ||
| 94 | 98 | self::$fonts[$font_family[0]]['specs'] = isset($font_family[1]) ? explode(',', rtrim($font_family[1], ',')) : array(); |
| 95 | 99 | } else { |
| 96 | 100 | // if the family was already added, and this new one has weights, merge with previous |
| 97 | 101 | if (isset($font_family[1])) { |
| 98 | - if (isset(self::$fonts[$font_family[0]]['version']) && 'V2' == self::$fonts[$font_family[0]]['version']) { | |
| 102 | + if (isset(self::$fonts[$font_family[0]]['version']) && 'V2' === self::$fonts[$font_family[0]]['version']) { | |
| 99 | 103 | self::$fonts[$font_family[0]]['specs'] = explode(',', rtrim($font_family[1], ',')); |
| 100 | 104 | } else { |
| 101 | 105 | self::$fonts[$font_family[0]]['specs'] = array_merge(self::$fonts[$font_family[0]]['specs'], explode(',', rtrim($font_family[1], ','))); |
| 102 | 106 | } |
| @@ -112,11 +116,13 @@ | ||
| 112 | 116 | } |
| 113 | 117 | |
| 114 | 118 | /** |
| 115 | 119 | * Parses google font api version 2 url |
| 120 | + * | |
| 121 | + * @param string $font | |
| 116 | 122 | */ |
| 117 | 123 | private static function parse_font_api2_url($font) { |
| 118 | - $parsed_url = parse_url($font, PHP_URL_QUERY); | |
| 124 | + $parsed_url = wp_parse_url($font, PHP_URL_QUERY); | |
| 119 | 125 | $query_elements = explode('&', $parsed_url); |
| 120 | 126 | foreach ($query_elements as $element) { |
| 121 | 127 | $family_str = str_replace('family=', '', $element); |
| 122 | 128 | $family = explode(':', $family_str); |
| @@ -176,10 +182,10 @@ | ||
| 176 | 182 | * Converts google font api version 1 font specification into API V2 |
| 177 | 183 | */ |
| 178 | 184 | private static function convert_v1_font_specs_to_v2() { |
| 179 | 185 | foreach (self::$fonts as $font_name => $font_details) { |
| 180 | - if ('V2' == $font_details['version']) continue; | |
| 181 | - if (0 == count($font_details['specs'])) { | |
| 186 | + if ('V2' === $font_details['version']) continue; | |
| 187 | + if (0 === count($font_details['specs'])) { | |
| 182 | 188 | self::$fonts[$font_name]['specs'] = array( |
| 183 | 189 | 'wght' => array(), |
| 184 | 190 | 'ital' => array(), |
| 185 | 191 | 'ital,wght' => array(), |
| @@ -204,14 +210,21 @@ | ||
| 204 | 210 | unset(self::$fonts[$font_name]['specs'][$key]); |
| 205 | 211 | if (!isset(self::$fonts[$font_name]['specs']['ital,wght'])) { |
| 206 | 212 | self::$fonts[$font_name]['specs']['ital,wght'] = array(); |
| 207 | 213 | } |
| 208 | - if (false !== strpos($detail, 'i')) { | |
| 214 | + if ('inherit' === $detail) { | |
| 215 | + array_push(self::$fonts[$font_name]['specs']['ital,wght'], '0,400'); | |
| 216 | + array_push(self::$fonts[$font_name]['specs']['ital,wght'], '1,400'); | |
| 217 | + } elseif ('regular' === $detail) { | |
| 218 | + $detail = 'regular' === $detail ? 400 : $detail; | |
| 219 | + array_push(self::$fonts[$font_name]['specs']['ital,wght'], '0,' . $detail); | |
| 220 | + } elseif (false !== strpos($detail, 'i')) { | |
| 209 | 221 | $detail = str_replace(array('italic', 'i'), '', $detail); |
| 210 | 222 | $detail = '' === $detail ? 400 : $detail; |
| 211 | 223 | array_push(self::$fonts[$font_name]['specs']['ital,wght'], '1,' . $detail); |
| 212 | 224 | } else { |
| 213 | - $detail = 'regular' === $detail ? 400 : $detail; | |
| 225 | + $detail = str_replace(array('normal'), '', $detail); | |
| 226 | + $detail = '' === $detail ? 400 : $detail; | |
| 214 | 227 | array_push(self::$fonts[$font_name]['specs']['ital,wght'], '0,' . $detail); |
| 215 | 228 | } |
| 216 | 229 | break; |
| 217 | 230 | } |
| @@ -227,10 +240,10 @@ | ||
| 227 | 240 | */ |
| 228 | 241 | private static function build() { |
| 229 | 242 | $result = ''; |
| 230 | 243 | foreach (self::$fonts as $font_name => $font_details) { |
| 231 | - if ('display=swap' == $font_name) continue; | |
| 232 | - if ('' != $result) { | |
| 244 | + if ('display=swap' === $font_name) continue; | |
| 245 | + if ('' !== $result) { | |
| 233 | 246 | $result .= '&'; |
| 234 | 247 | } |
| 235 | 248 | $result .= 'family=' . str_replace(' ', '+', $font_name); |
| 236 | 249 | $result .= self::specs_to_string($font_details['specs']); |
| @@ -238,9 +251,9 @@ | ||
| 238 | 251 | return $result; |
| 239 | 252 | } |
| 240 | 253 | |
| 241 | 254 | /** |
| 242 | - * Converts font specifications into a valid google font api2 url string | |
| 255 | + * Converts font specifications into a valid Google font api2 url string | |
| 243 | 256 | * |
| 244 | 257 | * @param array $font_specs Font style and weight specifications |
| 245 | 258 | * |
| 246 | 259 | * @return string |
| @@ -257,11 +270,11 @@ | ||
| 257 | 270 | } |
| 258 | 271 | |
| 259 | 272 | // Italic only |
| 260 | 273 | if ($italic_weights && !$weights && !$all_weights) { |
| 261 | - if ('1' == $font_specs['ital'][0]) { | |
| 274 | + if ('1' === $font_specs['ital'][0]) { | |
| 262 | 275 | return ':ital@1'; |
| 263 | - } elseif ('0;1' == $font_specs['ital'][0]) { | |
| 276 | + } elseif ('0;1' === $font_specs['ital'][0]) { | |
| 264 | 277 | return ':ital@0;1'; |
| 265 | 278 | } |
| 266 | 279 | } |
| 267 | 280 | |
| @@ -280,9 +293,9 @@ | ||
| 280 | 293 | } |
| 281 | 294 | break; |
| 282 | 295 | case 'ital': |
| 283 | 296 | foreach ($units as $unit) { |
| 284 | - array_push($result, 1 == $unit ? '1,400' : $unit); | |
| 297 | + array_push($result, 1 === (int) $unit ? '1,400' : $unit); | |
| 285 | 298 | } |
| 286 | 299 | break; |
| 287 | 300 | case 'ital,wght': |
| 288 | 301 | foreach ($units as $unit) { |