From dd7f2fb9fb2d91d81fe67fc5874f1e226b767cba Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Mon, 6 Dec 2021 11:50:51 +0200 Subject: [PATCH] Remove providers & add filters --- lib/class-wp-webfonts-provider-local.php | 263 -------------- lib/class-wp-webfonts-provider.php | 68 ---- lib/class-wp-webfonts.php | 340 ++++++++++++------ lib/load.php | 2 - lib/webfonts.php | 98 +---- .../class-wp-webfonts-local-provider-test.php | 149 -------- phpunit/class-wp-webfonts-test.php | 34 +- 7 files changed, 250 insertions(+), 704 deletions(-) delete mode 100644 lib/class-wp-webfonts-provider-local.php delete mode 100644 lib/class-wp-webfonts-provider.php delete mode 100644 phpunit/class-wp-webfonts-local-provider-test.php diff --git a/lib/class-wp-webfonts-provider-local.php b/lib/class-wp-webfonts-provider-local.php deleted file mode 100644 index b2cc2524c27e74..00000000000000 --- a/lib/class-wp-webfonts-provider-local.php +++ /dev/null @@ -1,263 +0,0 @@ - - * array( - * 'source-serif-pro.normal.200 900' => array( - * 'provider' => 'local', - * 'font_family' => 'Source Serif Pro', - * 'font_weight' => '200 900', - * 'font_style' => 'normal', - * 'src' => 'https://example.com/wp-content/themes/twentytwentytwo/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2' ), - * ), - * 'source-serif-pro.italic.400 900' => array( - * 'provider' => 'local', - * 'font_family' => 'Source Serif Pro', - * 'font_weight' => '200 900', - * 'font_style' => 'italic', - * 'src' => 'https://example.com/wp-content/themes/twentytwentytwo/assets/fonts/source-serif-pro/SourceSerif4Variable-Italic.ttf.woff2' ), - * ), - * ) - * - * - * the following `@font-face` styles are generated and returned: - * - * - * @font-face{ - * font-family:"Source Serif Pro"; - * font-style:normal; - * font-weight:200 900; - * font-stretch:normal; - * src:local("Source Serif Pro"), url('/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2') format('woff2'); - * } - * @font-face{ - * font-family:"Source Serif Pro"; - * font-style:italic; - * font-weight:200 900; - * font-stretch:normal; - * src:local("Source Serif Pro"), url('/assets/fonts/source-serif-pro/SourceSerif4Variable-Italic.ttf.woff2') format('woff2'); - * } - * - * - * @since 6.0.0 - * - * @return string The `@font-face` CSS. - */ - public function get_css() { - $css = ''; - - foreach ( $this->webfonts as $webfont ) { - // Order the webfont's `src` items to optimize for browser support. - $webfont = $this->order_src( $webfont ); - - // Build the @font-face CSS for this webfont. - $css .= '@font-face{' . $this->build_font_face_css( $webfont ) . '}'; - } - - return $css; - } - - /** - * Order `src` items to optimize for browser support. - * - * @since 6.0.0 - * - * @param array $webfont Webfont to process. - * @return array - */ - private function order_src( array $webfont ) { - if ( ! is_array( $webfont['src'] ) ) { - $webfont['src'] = (array) $webfont['src']; - } - - $src = array(); - $src_ordered = array(); - - foreach ( $webfont['src'] as $url ) { - // Add data URIs first. - if ( 0 === strpos( 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', - ); - } - $webfont['src'] = $src_ordered; - - return $webfont; - } - - /** - * Builds the font-family's CSS. - * - * @since 6.0.0 - * - * @param array $webfont Webfont to process. - * @return string This font-family's CSS. - */ - private function build_font_face_css( array $webfont ) { - $css = ''; - - // Wrap font-family in quotes if it contains spaces. - if ( - false !== strpos( $webfont['font-family'], ' ' ) && - false === strpos( $webfont['font-family'], '"' ) && - false === strpos( $webfont['font-family'], "'" ) - ) { - $webfont['font-family'] = '"' . $webfont['font-family'] . '"'; - } - - foreach ( $webfont as $key => $value ) { - - // Skip "provider". - if ( 'provider' === $key ) { - continue; - } - - // Compile the "src" parameter. - if ( 'src' === $key ) { - $value = $this->compile_src( $webfont['font-family'], $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.0.0 - * - * @param string $font_family Font family. - * @param array $value Value to process. - * @return string The CSS. - */ - private function compile_src( $font_family, array $value ) { - $src = "local($font_family)"; - - foreach ( $value as $item ) { - - if ( 0 === strpos( $item['url'], get_site_url() ) ) { - $item['url'] = wp_make_link_relative( $item['url'] ); - } - - $src .= ( 'data' === $item['format'] ) - ? ", url({$item['url']})" - : ", url('{$item['url']}') format('{$item['format']}')"; - } - return $src; - } - - /** - * Compiles the font variation settings. - * - * @since 6.0.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; - } -} diff --git a/lib/class-wp-webfonts-provider.php b/lib/class-wp-webfonts-provider.php deleted file mode 100644 index 96b12547986425..00000000000000 --- a/lib/class-wp-webfonts-provider.php +++ /dev/null @@ -1,68 +0,0 @@ -webfonts = $webfonts; - } - - /** - * Gets the `@font-face` CSS for the provider's webfonts. - * - * This method is where the provider does it processing to build the - * needed `@font-face` CSS for all of its webfonts. Specifics of how - * this processing is done is contained in each provider. - * - * @since 6.0.0 - * - * @return string The `@font-face` CSS. - */ - abstract public function get_css(); -} diff --git a/lib/class-wp-webfonts.php b/lib/class-wp-webfonts.php index 903032760de7f4..221afe1402e8e6 100644 --- a/lib/class-wp-webfonts.php +++ b/lib/class-wp-webfonts.php @@ -19,15 +19,6 @@ class WP_Webfonts { */ private static $webfonts = array(); - /** - * An array of registered providers. - * - * @static - * @access private - * @var array - */ - private static $providers = array(); - /** * Stylesheet handle. * @@ -40,9 +31,6 @@ class WP_Webfonts { */ public function init() { - // Register default providers. - $this->register_provider( 'local', 'WP_Webfonts_Provider_Local' ); - // Register callback to generate and enqueue styles. if ( did_action( 'wp_enqueue_scripts' ) ) { $this->stylesheet_handle = 'webfonts-footer'; @@ -66,15 +54,6 @@ public function get_fonts() { return self::$webfonts; } - /** - * Get the list of providers. - * - * @return array - */ - public function get_providers() { - return self::$providers; - } - /** * Register a webfont. * @@ -95,7 +74,7 @@ public function register_font( $font ) { * @return string */ public function get_font_id( $font ) { - return sanitize_title( "{$font['font-family']}-{$font['font-weight']}-{$font['font-style']}-{$font['provider']}" ); + return sanitize_title( "{$font['font-family']}-{$font['font-weight']}-{$font['font-style']}" ); } /** @@ -109,7 +88,6 @@ public function validate_font( $font ) { $font = wp_parse_args( $font, array( - 'provider' => 'local', 'font-family' => '', 'font-style' => 'normal', 'font-weight' => '400', @@ -123,34 +101,29 @@ public function validate_font( $font ) { return false; } - // Local fonts need a "src". - if ( 'local' === $font['provider'] ) { - // Make sure that local fonts have 'src' defined. - if ( empty( $font['src'] ) || ( ! is_string( $font['src'] ) && ! is_array( $font['src'] ) ) ) { - trigger_error( __( 'Webfont src must be a non-empty string or an array of strings.', 'gutenberg' ) ); - return false; - } + // Fonts need a "src". + if ( empty( $font['src'] ) || ( ! is_string( $font['src'] ) && ! is_array( $font['src'] ) ) ) { + trigger_error( __( 'Webfont src must be a non-empty string or an array of strings.', 'gutenberg' ) ); + return false; } // Validate the 'src' property. - if ( ! empty( $font['src'] ) ) { - foreach ( (array) $font['src'] as $src ) { - if ( empty( $src ) || ! is_string( $src ) ) { - trigger_error( __( 'Each webfont src must be a non-empty string.', 'gutenberg' ) ); - return false; - } - - if ( - // Validate data URLs. - ! preg_match( '/^data:.+;base64/', $src ) && - // Validate URLs. - ! filter_var( $src, FILTER_VALIDATE_URL ) && - // Check if it's a URL starting with "//" (omitted protocol). - 0 !== strpos( $src, '//' ) - ) { - trigger_error( __( 'Webfont src must be a valid URL or a data URI.', 'gutenberg' ) ); - return false; - } + foreach ( (array) $font['src'] as $src ) { + if ( empty( $src ) || ! is_string( $src ) ) { + trigger_error( __( 'Each webfont src must be a non-empty string.', 'gutenberg' ) ); + return false; + } + + if ( + // Validate data URLs. + ! preg_match( '/^data:.+;base64/', $src ) && + // Validate URLs. + ! filter_var( $src, FILTER_VALIDATE_URL ) && + // Check if it's a URL starting with "//" (omitted protocol). + 0 !== strpos( $src, '//' ) + ) { + trigger_error( __( 'Webfont src must be a valid URL or a data URI.', 'gutenberg' ) ); + return false; } } @@ -196,9 +169,6 @@ public function validate_font( $font ) { 'size-adjust', 'src', 'unicode-range', - - // Exceptions. - 'provider', ); foreach ( $font as $prop => $value ) { @@ -210,28 +180,12 @@ public function validate_font( $font ) { return $font; } - /** - * Register a provider. - * - * @param string $provider The provider name. - * @param string $class The provider class name. - * - * @return bool Whether the provider was registered successfully. - */ - public function register_provider( $provider, $class ) { - if ( empty( $provider ) || empty( $class ) ) { - return false; - } - self::$providers[ $provider ] = $class; - return true; - } - /** * Generate and enqueue webfonts styles. */ public function generate_and_enqueue_styles() { // Generate the styles. - $styles = $this->generate_styles(); + $styles = $this->get_css(); // Bail out if there are no styles to enqueue. if ( '' === $styles ) { @@ -251,7 +205,7 @@ public function generate_and_enqueue_styles() { */ public function generate_and_enqueue_editor_styles() { // Generate the styles. - $styles = $this->generate_styles(); + $styles = $this->get_css(); // Bail out if there are no styles to enqueue. if ( '' === $styles ) { @@ -262,62 +216,230 @@ public function generate_and_enqueue_editor_styles() { } /** - * Generate styles for webfonts. + * Gets the `@font-face` CSS styles for locally-hosted font files. * - * By default (due to privacy concerns), this API will not do remote requests to - * external webfont services nor generate `@font-face` styles for these remote - * providers. The filter `'has_remote_webfonts_request_permission'` is provided - * to grant permission to do the remote request. + * 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 webfonts. + * + * For example, when given these webfonts: + * + * array( + * 'source-serif-pro-200-900-normal' => array( + * 'font_family' => 'Source Serif Pro', + * 'font_weight' => '200 900', + * 'font_style' => 'normal', + * 'src' => 'https://example.com/wp-content/themes/twentytwentytwo/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2' ), + * ), + * 'source-serif-pro-400-900-italic' => array( + * 'font_family' => 'Source Serif Pro', + * 'font_weight' => '200 900', + * 'font_style' => 'italic', + * 'src' => 'https://example.com/wp-content/themes/twentytwentytwo/assets/fonts/source-serif-pro/SourceSerif4Variable-Italic.ttf.woff2' ), + * ), + * ) + * + * + * the following `@font-face` styles are generated and returned: + * + * + * @font-face{ + * font-family:"Source Serif Pro"; + * font-style:normal; + * font-weight:200 900; + * font-stretch:normal; + * src:local("Source Serif Pro"), url('/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2') format('woff2'); + * } + * @font-face{ + * font-family:"Source Serif Pro"; + * font-style:italic; + * font-weight:200 900; + * font-stretch:normal; + * src:local("Source Serif Pro"), url('/assets/fonts/source-serif-pro/SourceSerif4Variable-Italic.ttf.woff2') format('woff2'); + * } + * * * @since 6.0.0 * - * @return string $styles Generated styles. + * @return string The `@font-face` CSS. */ - public function generate_styles() { - $styles = ''; - $providers = $this->get_providers(); - - // Group webfonts by provider. - $webfonts_by_provider = array(); - $registered_webfonts = $this->get_fonts(); - foreach ( $registered_webfonts as $id => $webfont ) { - $provider = $webfont['provider']; - if ( ! isset( $providers[ $provider ] ) ) { + public function get_css() { + $css = ''; + $fonts = $this->get_fonts(); + + foreach ( $fonts as $font ) { + // Order the font's `src` items to optimize for browser support. + $font = $this->order_src( $font ); + + // Build the @font-face CSS for this webfont. + $css .= '@font-face{' . $this->build_font_face_css( $font ) . '}'; + } + + return $css; + } + + /** + * Order `src` items to optimize for browser support. + * + * @since 6.0.0 + * + * @param array $webfont Webfont to process. + * @return array + */ + private function order_src( $webfont ) { + if ( ! is_array( $webfont['src'] ) ) { + $webfont['src'] = (array) $webfont['src']; + } + + $src = array(); + $src_ordered = array(); + + foreach ( $webfont['src'] as $url ) { + // Add data URIs first. + if ( 0 === strpos( trim( $url ), 'data:' ) ) { + $src_ordered[] = array( + 'url' => $url, + 'format' => 'data', + ); continue; } - $webfonts_by_provider[ $provider ] = isset( $webfonts_by_provider[ $provider ] ) ? $webfonts_by_provider[ $provider ] : array(); - $webfonts_by_provider[ $provider ][ $id ] = $webfont; + $format = pathinfo( $url, PATHINFO_EXTENSION ); + $src[ $format ] = $url; } - /* - * Loop through each of the providers to get the CSS for their respective webfonts - * to incrementally generate the collective styles for all of them. - */ - foreach ( $providers as $provider_id => $provider_class ) { + // Add woff2. + if ( ! empty( $src['woff2'] ) ) { + $src_ordered[] = array( + 'url' => $src['woff2'], + 'format' => 'woff2', + ); + } - // Bail out if the provider class does not exist. - if ( ! class_exists( $provider_class ) ) { - continue; + // 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', + ); + } + $webfont['src'] = $src_ordered; + + return $webfont; + } + + /** + * Builds the font-family's CSS. + * + * @since 6.0.0 + * + * @param array $webfont Webfont to process. + * @return string This font-family's CSS. + */ + private function build_font_face_css( $webfont ) { + $css = ''; + + // Wrap font-family in quotes if it contains spaces. + if ( + false !== strpos( $webfont['font-family'], ' ' ) && + false === strpos( $webfont['font-family'], '"' ) && + false === strpos( $webfont['font-family'], "'" ) + ) { + $webfont['font-family'] = '"' . $webfont['font-family'] . '"'; + } + + foreach ( $webfont as $key => $value ) { + // Compile the "src" parameter. + if ( 'src' === $key ) { + $value = $this->compile_src( $webfont['font-family'], $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 ); } - $provider_webfonts = isset( $webfonts_by_provider[ $provider_id ] ) - ? $webfonts_by_provider[ $provider_id ] - : array(); + if ( ! empty( $value ) ) { + $css .= "$key:$value;"; + } + } - // If there are no registered webfonts for this provider, skip it. - if ( empty( $provider_webfonts ) ) { - continue; + /** + * Filters the font-family's CSS. + * + * @since 6.0.0 + * + * @param string $css The font-family's CSS. + * @param array $webfont The font-family's data. + * + * @return string The font-family's CSS. + */ + return apply_filters( 'font_face_css', $css, $webfont ); + } + + /** + * Compiles the `src` into valid CSS. + * + * @since 6.0.0 + * + * @param string $font_family Font family. + * @param array $value Value to process. + * @return string The CSS. + */ + private function compile_src( $font_family, $value ) { + $src = "local($font_family)"; + + foreach ( $value as $item ) { + + if ( 0 === strpos( $item['url'], get_site_url() ) ) { + $item['url'] = wp_make_link_relative( $item['url'] ); } - /* - * Process the webfonts by first passing them to the provider via `set_webfonts()` - * and then getting the CSS from the provider. - */ - $provider = new $provider_class(); - $provider->set_webfonts( $provider_webfonts ); - $styles .= $provider->get_css(); + $src .= ( 'data' === $item['format'] ) + ? ", url({$item['url']})" + : ", url('{$item['url']}') format('{$item['format']}')"; + } + return $src; + } + + /** + * Compiles the font variation settings. + * + * @since 6.0.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 $styles; + return $variations; } } diff --git a/lib/load.php b/lib/load.php index 99a9d99efe7bba..da7d71433117fb 100644 --- a/lib/load.php +++ b/lib/load.php @@ -151,8 +151,6 @@ function gutenberg_is_experiment_enabled( $name ) { /** WordPress Webfonts Classes & Functions */ require_once __DIR__ . '/class-wp-webfonts.php'; - require_once __DIR__ . '/class-wp-webfonts-provider.php'; - require_once __DIR__ . '/class-wp-webfonts-provider-local.php'; require_once __DIR__ . '/webfonts.php'; /** diff --git a/lib/webfonts.php b/lib/webfonts.php index c722b9521548d3..c533512ba4eb1a 100644 --- a/lib/webfonts.php +++ b/lib/webfonts.php @@ -37,14 +37,12 @@ function wp_webfonts() { * wp_register_webfonts( * array( * array( - * 'provider' => 'local', * 'font_family' => 'Source Serif Pro', * 'font_weight' => '200 900', * 'font_style' => 'normal', * 'src' => get_theme_file_uri( 'assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2' ), * ), * array( - * 'provider' => 'local', * 'font_family' => 'Source Serif Pro', * 'font_weight' => '200 900', * 'font_style' => 'italic', @@ -54,26 +52,6 @@ function wp_webfonts() { * ); * * - * When requesting from the remote Google Fonts API service provider: - * - * wp_register_webfonts( - * array( - * array( - * 'provider' => 'google', - * 'font_family' => 'Source Serif Pro', - * 'font_weight' => '200 900', - * 'font_style' => 'normal', - * ), - * array( - * 'provider' => 'google', - * 'font_family' => 'Source Serif Pro', - * 'font_weight' => '200 900', - * 'font_style' => 'italic', - * ), - * ) - * ); - * - * * @since 6.0.0 * * @param array $webfonts Webfonts to be registered. @@ -97,7 +75,6 @@ function wp_register_webfonts( array $webfonts = array() ) { * ``` * wp_register_webfont( * array( - * 'provider' => 'local', * 'font_family' => 'Source Serif Pro', * 'font_weight' => '200 900', * 'font_style' => 'normal', @@ -106,75 +83,24 @@ function wp_register_webfonts( array $webfonts = array() ) { * ); * ``` * - * When requesting from the remote Google Fonts API service provider: - * ``` - * wp_register_webfonts( - * array( - * 'provider' => 'google', - * 'font_family' => 'Source Serif Pro', - * 'font_weight' => '200 900', - * 'font_style' => 'normal', - * ) - * ); - * ``` - * * @since 6.0.0 * * @param array $webfont Webfont to be registered. * See {@see WP_Webfonts_Registry::register()} for a list of supported arguments. */ function wp_register_webfont( array $webfont ) { - wp_webfonts()->register_font( $webfont ); -} + /** + * Filters the webfonts to be registered. + * + * @since 6.0.0 + * + * @param array $webfont Webfont to be registered. + * See {@see WP_Webfonts_Registry::register()} for a list of supported arguments. + * + * @return array $webfont Webfont to be registered. + */ + $webfont = apply_filters( 'wp_register_webfont', $webfont ); -/** - * Registers a custom font service provider. - * - * A webfont provider contains the business logic for how to - * interact with a remote font service and how to generate - * the `@font-face` styles for that remote service. - * - * See the `WP_Webfonts_Google_Provider` for inspiration. - * - * How to register a custom font service provider: - * 1. Load its class file into memory before registration. - * 2. Pass the class' name to this function. - * - * For example, for a class named `My_Custom_Font_Service_Provider`: - * ``` - * wp_register_webfont_provider( My_Custom_Font_Service_Provider::class ); - * ``` - * - * @since 6.0.0 - * - * @param string $name The provider's name. - * @param string $classname The provider's class name. - * The class should be a child of `WP_Webfonts_Provider`. - * See {@see WP_Webfonts_Provider}. - * - * @return bool True when registered. False when provider does not exist. - */ -function wp_register_webfont_provider( $name, $classname ) { - return wp_webfonts()->register_provider( $name, $classname ); + wp_webfonts()->register_font( $webfont ); } -/** - * Gets all registered providers. - * - * Return an array of providers, each keyed by their unique - * ID (i.e. the `$id` property in the provider's object) with - * an instance of the provider (object): - * ID => provider instance - * - * Each provider contains the business logic for how to - * process its specific font service (i.e. local or remote) - * and how to generate the `@font-face` styles for its service. - * - * @since 6.0.0 - * - * @return WP_Webfonts_Provider[] All registered providers, - * each keyed by their unique ID. - */ -function wp_get_webfont_providers() { - return wp_webfonts()->get_providers(); -} diff --git a/phpunit/class-wp-webfonts-local-provider-test.php b/phpunit/class-wp-webfonts-local-provider-test.php deleted file mode 100644 index 69892d277bc227..00000000000000 --- a/phpunit/class-wp-webfonts-local-provider-test.php +++ /dev/null @@ -1,149 +0,0 @@ -provider = new WP_Webfonts_Provider_Local(); - - $this->set_up_theme(); - } - - /** - * Local `src` paths to need to be relative to the theme. This method sets up the - * `wp-content/themes/` directory to ensure consistency when running tests. - */ - private function set_up_theme() { - $this->theme_root = realpath( DIR_TESTDATA . '/themedir1' ); - $this->orig_theme_dir = $GLOBALS['wp_theme_directories']; - $GLOBALS['wp_theme_directories'] = array( $this->theme_root ); - - $theme_root_callback = function () { - return $this->theme_root; - }; - add_filter( 'theme_root', $theme_root_callback ); - add_filter( 'stylesheet_root', $theme_root_callback ); - add_filter( 'template_root', $theme_root_callback ); - - // Clear caches. - wp_clean_themes_cache(); - unset( $GLOBALS['wp_themes'] ); - } - - function tear_down() { - // Restore the original theme directory setup. - $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir; - wp_clean_themes_cache(); - unset( $GLOBALS['wp_themes'] ); - - parent::tear_down(); - } - - /** - * @covers WP_Webfonts_Provider_Local::set_webfonts - */ - public function test_set_webfonts() { - $webfonts = array( - 'source-serif-pro-200-900-normal-local' => array( - 'provider' => 'local', - 'font-family' => 'Source Serif Pro', - 'font-style' => 'normal', - 'font-weight' => '200 900', - 'font-stretch' => 'normal', - 'src' => 'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2', - ), - 'source-serif-pro-200-900-italic-local' => array( - 'provider' => 'local', - 'font-family' => 'Source Serif Pro', - 'font-style' => 'italic', - 'font-weight' => '200 900', - 'font-stretch' => 'normal', - 'src' => 'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Italic.ttf.woff2', - ), - ); - - $this->provider->set_webfonts( $webfonts ); - - $property = $this->get_webfonts_property(); - $this->assertSame( $webfonts, $property->getValue( $this->provider ) ); - } - - /** - * @covers WP_Webfonts_Provider_Local::get_css - * - * @dataProvider data_get_css - * - * @param array $webfonts Prepared webfonts (to store in WP_Webfonts_Provider_Local::$webfonts property). - * @param string $expected Expected CSS. - */ - public function test_get_css( array $webfonts, $expected ) { - $property = $this->get_webfonts_property(); - $property->setValue( $this->provider, $webfonts ); - - $this->assertSame( $expected, $this->provider->get_css() ); - } - - /** - * Data provider. - * - * @return array - */ - public function data_get_css() { - return array( - 'truetype format' => array( - 'webfonts' => array( - 'open-sans-bold-italic-local' => array( - 'provider' => 'local', - 'font-family' => 'Open Sans', - 'font-style' => 'italic', - 'font-weight' => 'bold', - 'src' => 'http://example.org/assets/fonts/OpenSans-Italic-VariableFont_wdth,wght.ttf', - ), - ), - 'expected' => << array( - 'webfonts' => array( - 'source-serif-pro-200-900-normal-local' => array( - 'provider' => 'local', - 'font-family' => 'Source Serif Pro', - 'font-style' => 'normal', - 'font-weight' => '200 900', - 'font-stretch' => 'normal', - 'src' => 'http://example.org/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2', - ), - 'source-serif-pro-400-900-italic-local' => array( - 'provider' => 'local', - 'font-family' => 'Source Serif Pro', - 'font-style' => 'italic', - 'font-weight' => '200 900', - 'font-stretch' => 'normal', - 'src' => 'http://example.org/assets/fonts/source-serif-pro/SourceSerif4Variable-Italic.ttf.woff2', - ), - ), - 'expected' => <<provider, 'webfonts' ); - $property->setAccessible( true ); - - return $property; - } -} diff --git a/phpunit/class-wp-webfonts-test.php b/phpunit/class-wp-webfonts-test.php index 5b7edc6b2bceee..b876c2f585c65a 100644 --- a/phpunit/class-wp-webfonts-test.php +++ b/phpunit/class-wp-webfonts-test.php @@ -15,7 +15,6 @@ class WP_Webfonts_Test extends WP_UnitTestCase { public function test_get_fonts() { $fonts = array( array( - 'provider' => 'local', 'font-family' => 'Source Serif Pro', 'font-style' => 'normal', 'font-weight' => '200 900', @@ -24,7 +23,6 @@ public function test_get_fonts() { 'font-display' => 'fallback', ), array( - 'provider' => 'local', 'font-family' => 'Source Serif Pro', 'font-style' => 'italic', 'font-weight' => '200 900', @@ -35,30 +33,14 @@ public function test_get_fonts() { ); $expected = array( - 'source-serif-pro-200-900-normal-local' => $fonts[0], - 'source-serif-pro-200-900-italic-local' => $fonts[1], + 'source-serif-pro-200-900-normal' => $fonts[0], + 'source-serif-pro-200-900-italic' => $fonts[1], ); wp_register_webfonts( $fonts ); $this->assertEquals( $expected, wp_webfonts()->get_fonts() ); } - /** - * @covers wp_register_webfont - * @covers WP_Webfonts::register_provider - * @covers WP_Webfonts::get_providers - */ - public function test_get_providers() { - wp_register_webfont_provider( 'test-provider', 'Test_Provider' ); - $this->assertEquals( - array( - 'local' => 'WP_Webfonts_Provider_Local', - 'test-provider' => 'Test_Provider', - ), - wp_get_webfont_providers() - ); - } - /** * @covers WP_Webfonts::validate_font */ @@ -71,9 +53,6 @@ public function test_validate_font() { 'src' => 'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2', ); - // Test missing provider fallback to local. - $this->assertEquals( 'local', wp_webfonts()->validate_font( $font )['provider'] ); - // Test missing font-weight fallback to 400. $this->assertEquals( '400', wp_webfonts()->validate_font( $font )['font-weight'] ); @@ -83,7 +62,7 @@ public function test_validate_font() { // Test missing font-display fallback to fallback. $this->assertEquals( 'fallback', wp_webfonts()->validate_font( $font )['font-display'] ); - // Test local font with missing "src". + // Test font with missing "src". $this->assertFalse( wp_webfonts()->validate_font( array( 'font-family' => 'Test Font 2' ) ) ); // Test malformatted src. @@ -132,11 +111,12 @@ public function test_validate_font() { } /** - * @covers WP_Webfonts::generate_styles + * @covers WP_Webfonts::get_css + * @covers WP_Webfonts::build_font_face_css */ - public function test_generate_styles() { + public function test_get_css() { $this->assertEquals( - wp_webfonts()->generate_styles(), + wp_webfonts()->get_css(), '@font-face{font-family:"Source Serif Pro";font-style:normal;font-weight:200 900;font-display:fallback;font-stretch:normal;src:local("Source Serif Pro"), url(\'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2\') format(\'woff2\');}@font-face{font-family:"Source Serif Pro";font-style:italic;font-weight:200 900;font-display:fallback;font-stretch:normal;src:local("Source Serif Pro"), url(\'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Italic.ttf.woff2\') format(\'woff2\');}' ); }