| @@ -17,12 +17,8 @@ | ||
| 17 | 17 | public function process_font_queue( $fonts ) { |
| 18 | 18 | if ( ! is_array( $fonts ) || empty( $fonts ) ) { |
| 19 | 19 | return; |
| 20 | 20 | } |
| 21 | - if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { | |
| 22 | - // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log, WordPress.PHP.DevelopmentFunctions.error_log_print_r | |
| 23 | - error_log( print_r( $fonts, true ) ); | |
| 24 | - } | |
| 25 | 21 | foreach ( $fonts as $family => $weights ) { |
| 26 | 22 | if ( ! is_array( $weights ) ) { |
| 27 | 23 | continue; |
| 28 | 24 | } |
| @@ -264,13 +260,69 @@ | ||
| 264 | 260 | } |
| 265 | 261 | |
| 266 | 262 | |
| 267 | 263 | /** |
| 264 | + * Return locally-available @font-face descriptors for a family. | |
| 265 | + * | |
| 266 | + * @param string $family | |
| 267 | + * @param array $weights Requested weights. | |
| 268 | + * @return array [ [ 'weight' => '400', 'src' => 'https://.../400-xxx.woff2' ], ... ] | |
| 269 | + */ | |
| 270 | + public function get_local_font_faces( $family, $weights = [] ) { | |
| 271 | + $upload_dir = wp_upload_dir(); | |
| 272 | + $dir = trailingslashit( $upload_dir['basedir'] ) . self::FONTS_FOLDER . '/' . $this->sanitize_family( $family ); | |
| 273 | + $url_base = trailingslashit( $upload_dir['baseurl'] ) . self::FONTS_FOLDER . '/' . $this->sanitize_family( $family ); | |
| 274 | + | |
| 275 | + if ( ! is_dir( $dir ) ) { | |
| 276 | + return []; | |
| 277 | + } | |
| 278 | + | |
| 279 | + $faces = []; | |
| 280 | + foreach ( (array) glob( $dir . '/*.woff2' ) as $file_path ) { | |
| 281 | + $file_name = basename( $file_path ); | |
| 282 | + $font_weight = preg_match( '/^(\d+)-/', $file_name, $m ) ? $m[1] : '400'; | |
| 283 | + | |
| 284 | + // If specific weights were requested, only include those. | |
| 285 | + if ( ! empty( $weights ) && ! in_array( (string) $font_weight, array_map( 'strval', $weights ), true ) ) { | |
| 286 | + continue; | |
| 287 | + } | |
| 288 | + | |
| 289 | + $faces[] = [ | |
| 290 | + 'weight' => (string) $font_weight, | |
| 291 | + 'src' => $url_base . '/' . $file_name, | |
| 292 | + ]; | |
| 293 | + } | |
| 294 | + | |
| 295 | + return $faces; | |
| 296 | + } | |
| 297 | + | |
| 298 | + /** | |
| 299 | + * From a [family => weights] map, return the subset not available locally. | |
| 300 | + * | |
| 301 | + * @param array $fonts | |
| 302 | + * @return array [family => [missing weights]] | |
| 303 | + */ | |
| 304 | + public function get_missing( $fonts ) { | |
| 305 | + $missing = []; | |
| 306 | + if ( ! is_array( $fonts ) ) { | |
| 307 | + return $missing; | |
| 308 | + } | |
| 309 | + foreach ( $fonts as $family => $weights ) { | |
| 310 | + foreach ( (array) $weights as $weight ) { | |
| 311 | + if ( ! $this->is_font_weight_available( $family, $weight ) ) { | |
| 312 | + $missing[ $family ][] = (string) $weight; | |
| 313 | + } | |
| 314 | + } | |
| 315 | + } | |
| 316 | + return $missing; | |
| 317 | + } | |
| 318 | + | |
| 319 | + /** | |
| 268 | 320 | * Sanitize font family for folder names |
| 269 | 321 | * |
| 270 | 322 | * @param string $family |
| 271 | 323 | * @return string |
| 272 | 324 | */ |
| 273 | - private function sanitize_family( $family ) { | |
| 325 | + public function sanitize_family( $family ) { | |
| 274 | 326 | return str_replace( ' ', '-', strtolower( $family ) ); |
| 275 | 327 | } |
| 276 | 328 | } |