ETHOD__, __( 'Each font src must be a non-empty string.' ), '6.4.0' ); return false; } } // Check the font-weight. if ( ! is_string( $font_face['font-weight'] ) && ! is_int( $font_face['font-weight'] ) ) { // @todo replace with `wp_trigger_error()`. _doing_it_wrong( __METHOD__, __( 'Font font-weight must be a properly formatted string or integer.' ), '6.4.0' ); return false; } // Check the font-display. if ( ! in_array( $font_face['font-display'], $this->valid_font_display, true ) ) { $font_face['font-display'] = $this->font_face_property_defaults['font-display']; } // Remove invalid properties. foreach ( $font_face as $property => $value ) { if ( ! in_array( $property, $this->valid_font_face_properties, true ) ) { unset( $font_face[ $property ] ); } } return $font_face; } /** * Gets the `@font-face` CSS styles for locally-hosted font files. * * This method does the following processing tasks: * 1. Orchestrates an optimized `src` (with format) for browser support. * 2. Generates the `@font-face` for all its fonts. * * @since 6.4.0 * * @param array[] $font_faces The font-faces to generate @font-face CSS styles. * @return string The `@font-face` CSS styles. */ private function get_css( $font_faces ) { $css = ''; foreach ( $font_faces as $font_face ) { // Order the font's `src` items to optimize for browser support. $font_face = $this->order_src( $font_face ); // Build the @font-face CSS for this font. $css .= '@font-face{' . $this->build_font_face_css( $font_face ) . '}' . "\n"; } // Don't print the last newline character. return rtrim( $css, "\n" ); } /** * Orders `src` items to optimize for browser support. * * @since 6.4.0 * * @param array $font_face Font face to process. * @return array Font-face with ordered src items. */ private function order_src( array $font_face ) { if ( ! is_array( $font_face['src'] ) ) { $font_face['src'] = (array) $font_face['src']; } $src = array(); $src_ordered = array(); foreach ( $font_face['src'] as $url ) { // Add data URIs first. if ( str_starts_with( trim( $url ), 'data:' ) ) { $src_ordered[] = array( 'url' => $url, 'format' => 'data', ); continue; } $format = pathinfo( $url, PATHINFO_EXTENSION ); $src[ $format ] = $url; } // Add woff2. if ( ! empty( $src['woff2'] ) ) { $src_ordered[] = array( 'url' => $src['woff2'], 'format' => 'woff2', ); } // Add woff. if ( ! empty( $src['woff'] ) ) { $src_ordered[] = array( 'url' => $src['woff'], 'format' => 'woff', ); } // Add ttf. if ( ! empty( $src['ttf'] ) ) { $src_ordered[] = array( 'url' => $src['ttf'], 'format' => 'truetype', ); } // Add eot. if ( ! empty( $src['eot'] ) ) { $src_ordered[] = array( 'url' => $src['eot'], 'format' => 'embedded-opentype', ); } // Add otf. if ( ! empty( $src['otf'] ) ) { $src_ordered[] = array( 'url' => $src['otf'], 'format' => 'opentype', ); } $font_face['src'] = $src_ordered; return $font_face; } /** * Builds the font-family's CSS. * * @since 6.4.0 * * @param array $font_face Font face to process. * @return string This font-family's CSS. */ private function build_font_face_css( array $font_face ) { $css = ''; /* * Wrap font-family in quotes if it contains spaces * and is not already wrapped in quotes. */ if ( str_contains( $font_face['font-family'], ' ' ) && ! str_contains( $font_face['font-family'], '"' ) && ! str_contains( $font_face['font-family'], "'" ) ) { $font_face['font-family'] = '"' . $font_face['font-family'] . '"'; } foreach ( $font_face as $key => $value ) { // Compile the "src" parameter. if ( 'src' === $key ) { $value = $this->compile_src( $value ); } // If font-variation-settings is an array, convert it to a string. if ( 'font-variation-settings' === $key && is_array( $value ) ) { $value = $this->compile_variations( $value ); } if ( ! empty( $value ) ) { $css .= "$key:$value;"; } } return $css; } /** * Compiles the `src` into valid CSS. * * @since 6.4.0 * * @param array $value Value to process. * @return string The CSS. */ private function compile_src( array $value ) { $src = ''; foreach ( $value as $item ) { $src .= ( 'data' === $item['format'] ) ? ", url({$item['url']})" : ", url('{$item['url']}') format('{$item['format']}')"; } $src = ltrim( $src, ', ' ); return $src; } /** * Compiles the font variation settings. * * @since 6.4.0 * * @param array $font_variation_settings Array of font variation settings. * @return string The CSS. */ private function compile_variations( array $font_variation_settings ) { $variations = ''; foreach ( $font_variation_settings as $key => $value ) { $variations .= "$key $value"; } return $variations; } }