Skip to content

Commit

Permalink
Separate style element from get_css()
Browse files Browse the repository at this point in the history
Moves the <style> element from the provider's get_css() method to restore only returning the `@font-face` CSS.

Adds a WP_Webfonts_Provider::get_style_element() method, providing a default `<style>` element markup, while giving each provider the ability to customize via overloading.
  • Loading branch information
hellofromtonya committed Dec 1, 2022
1 parent e3f4d4e commit dec6a3e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 42 deletions.
20 changes: 1 addition & 19 deletions lib/experimental/class-wp-webfonts-provider-local.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,6 @@ class WP_Webfonts_Provider_Local extends WP_Webfonts_Provider {
*/
protected $id = 'local';

/**
* Holds a string which contains the type attribute for style tag.
*
* If the active theme does not declare HTML5 support for 'style',
* then it initializes as `type='text/css'`.
*
* @since 6.1.0
*
* @var string
*/
private $type_attr = '';

/**
* Constructor.
*
Expand All @@ -58,7 +46,7 @@ function_exists( 'is_admin' ) && ! is_admin()
&&
function_exists( 'current_theme_supports' ) && ! current_theme_supports( 'html5', 'style' )
) {
$this->type_attr = " type='text/css'";
$this->style_tag_atts = " type='text/css'";
}
}

Expand Down Expand Up @@ -123,12 +111,6 @@ public function get_css() {
$css .= '@font-face{' . $this->build_font_face_css( $webfont ) . '}';
}

$css = sprintf(
"<style id='wp-webfonts-local'%s>\n%s\n</style>\n",
$this->type_attr,
$css
);

return $css;
}

Expand Down
38 changes: 35 additions & 3 deletions lib/experimental/class-wp-webfonts-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
*/
abstract class WP_Webfonts_Provider {

/**
* The provider's unique ID.
*
* @since X.X.X
*
* @var string
*/
protected $id = '';

/**
* Webfonts to be processed.
*
Expand All @@ -43,6 +52,15 @@ abstract class WP_Webfonts_Provider {
*/
protected $webfonts = array();

/**
* Font-face style tag's attribute(s).
*
* @since X.X.X
*
* @var string
*/
protected $style_tag_atts = '';

/**
* Sets this provider's webfonts property.
*
Expand All @@ -57,6 +75,18 @@ public function set_webfonts( array $webfonts ) {
$this->webfonts = $webfonts;
}

/**
* Prints the generated styles.
*
* @since X.X.X
*/
public function print_styles() {
printf(
$this->get_style_element(),
$this->get_css()
);
}

/**
* Gets the `@font-face` CSS for the provider's webfonts.
*
Expand All @@ -71,11 +101,13 @@ public function set_webfonts( array $webfonts ) {
abstract public function get_css();

/**
* Prints the generated styles.
* Gets the `<style>` element for wrapping the `@font-face` CSS.
*
* @since X.X.X
*
* @return string The style element.
*/
public function print_styles() {
echo $this->get_css();
protected function get_style_element() {
return "<style id='wp-webfonts-{$this->id}'{$this->style_tag_atts}>\n%s\n</style>\n";
}
}
19 changes: 12 additions & 7 deletions phpunit/fixtures/mock-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ class Mock_Provider extends WP_Webfonts_Provider {
*/
protected $id = 'mock';

/**
* CSS to print.
*
* @var string
*/
public $css = '<mock id="wp-webfonts-mock" attr="some-attr">%s</mock>\n';

public function get_css() {
$handles = array_keys( $this->webfonts );
return implode( '; ', $handles );
return sprintf( $this->css, implode( '; ', $handles ) );
}

/**
* Gets the `<style>` element for wrapping the `@font-face` CSS.
*
* @since X.X.X
*
* @return string The style element.
*/
protected function get_style_element() {
return '<mock id="wp-webfonts-mock" attr="some-attr">%s</mock>\n';
}
}
44 changes: 31 additions & 13 deletions phpunit/webfonts/wpWebfontsProviderLocal-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function test_set_webfonts() {
/**
* @covers WP_Webfonts_Provider_Local::get_css
*
* @dataProvider data_get_css
* @dataProvider data_get_css_print_styles
*
* @param array $webfonts Prepared webfonts (to store in WP_Webfonts_Provider_Local::$webfonts property).
* @param string $expected Expected CSS.
Expand All @@ -72,15 +72,32 @@ 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() );
$this->assertSame( $expected['font-face-css'], $this->provider->get_css() );
}

/**
* @covers WP_Webfonts_Provider_Local::print_styles
*
* @dataProvider data_get_css_print_styles
*
* @param array $webfonts Prepared webfonts (to store in WP_Webfonts_Provider_Local::$webfonts property).
* @param string $expected Expected CSS.
*/
public function test_print_styles( array $webfonts, $expected ) {
$property = $this->get_webfonts_property();
$property->setValue( $this->provider, $webfonts );

$expected_output = sprintf( $expected['style-element'], $expected['font-face-css'] );
$this->expectOutputString( $expected_output );
$this->provider->print_styles();
}

/**
* Data provider.
*
* @return array
*/
public function data_get_css() {
public function data_get_css_print_styles() {
return array(
'truetype format' => array(
'webfonts' => array(
Expand All @@ -92,13 +109,13 @@ public function data_get_css() {
'src' => 'http://example.org/assets/fonts/OpenSans-Italic-VariableFont_wdth,wght.ttf',
),
),
'expected' => <<<CSS
<style id='wp-webfonts-local' type='text/css'>
'expected' => array(
'style-element' => "<style id='wp-webfonts-local' type='text/css'>\n%s\n</style>\n",
'font-face-css' => <<<CSS
@font-face{font-family:"Open Sans";font-style:italic;font-weight:bold;src:local("Open Sans"), url('http://example.org/assets/fonts/OpenSans-Italic-VariableFont_wdth,wght.ttf') format('truetype');}
</style>
CSS
,
,
),
),
'woff2 format' => array(
'webfonts' => array(
Expand All @@ -119,13 +136,14 @@ public function data_get_css() {
'src' => 'http://example.org/assets/fonts/source-serif-pro/SourceSerif4Variable-Italic.ttf.woff2',
),
),
'expected' => <<<CSS
<style id='wp-webfonts-local' type='text/css'>
'expected' => array(
'style-element' => "<style id='wp-webfonts-local' type='text/css'>\n%s\n</style>\n",
'font-face-css' => <<<CSS
@font-face{font-family:"Source Serif Pro";font-style:normal;font-weight:200 900;font-stretch:normal;src:local("Source Serif Pro"), url('http://example.org/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('http://example.org/assets/fonts/source-serif-pro/SourceSerif4Variable-Italic.ttf.woff2') format('woff2');}
</style>
CSS
,

,
),
),
);
}
Expand Down

0 comments on commit dec6a3e

Please sign in to comment.