Skip to content

Commit

Permalink
Webfonts: change class properties from static to instance members (#3…
Browse files Browse the repository at this point in the history
…9361)

* Move WP_Webfonts properties to instance instead of static members

* Expose wp_webfonts through a global variable instead of static, just like wp_styles

* Register font on test before generating styles
  • Loading branch information
zaguiini authored Mar 14, 2022
1 parent cd0b8fb commit 4dc5505
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
14 changes: 6 additions & 8 deletions lib/compat/wordpress-6.0/class-wp-webfonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@ class WP_Webfonts {
/**
* An array of registered webfonts.
*
* @static
* @access private
* @var array
*/
private static $webfonts = array();
private $webfonts = array();

/**
* An array of registered providers.
*
* @static
* @access private
* @var array
*/
private static $providers = array();
private $providers = array();

/**
* Stylesheet handle.
Expand Down Expand Up @@ -63,7 +61,7 @@ public function init() {
* @return array
*/
public function get_fonts() {
return self::$webfonts;
return $this->webfonts;
}

/**
Expand All @@ -72,7 +70,7 @@ public function get_fonts() {
* @return array
*/
public function get_providers() {
return self::$providers;
return $this->providers;
}

/**
Expand All @@ -84,7 +82,7 @@ public function register_font( $font ) {
$font = $this->validate_font( $font );
if ( $font ) {
$id = $this->get_font_id( $font );
self::$webfonts[ $id ] = $font;
$this->webfonts[ $id ] = $font;
}
}

Expand Down Expand Up @@ -194,7 +192,7 @@ public function register_provider( $provider, $class ) {
if ( empty( $provider ) || empty( $class ) ) {
return false;
}
self::$providers[ $provider ] = $class;
$this->providers[ $provider ] = $class;
return true;
}

Expand Down
10 changes: 5 additions & 5 deletions lib/compat/wordpress-6.0/webfonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
* @return WP_Webfonts Instance of the controller.
*/
function wp_webfonts() {
static $instance;
global $wp_webfonts;

if ( ! $instance instanceof WP_Webfonts ) {
$instance = new WP_Webfonts();
$instance->init();
if ( ! $wp_webfonts instanceof WP_Webfonts ) {
$wp_webfonts = new WP_Webfonts();
$wp_webfonts->init();
}

return $instance;
return $wp_webfonts;
}

/**
Expand Down
33 changes: 32 additions & 1 deletion phpunit/class-wp-webfonts-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@
* @covers WP_Webfonts_Test
*/
class WP_Webfonts_Test extends WP_UnitTestCase {
/**
* WP_Webfonts instance reference
*
* @var WP_Webfonts
*/
private $old_wp_webfonts;

function setUp() {
global $wp_webfonts;
$this->old_wp_webfonts = $wp_webfonts;

$wp_webfonts = null;
}

function tearDown() {
global $wp_webfonts;

$wp_webfonts = $this->old_wp_webfonts;
}

/**
* @covers wp_register_webfonts
Expand Down Expand Up @@ -106,8 +125,20 @@ public function test_validate_font() {
* @covers WP_Webfonts::generate_styles
*/
public function test_generate_styles() {
$font = 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',
'font-display' => 'fallback',
);

wp_register_webfont( $font );

$this->assertEquals(
'@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\');}',
'@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\');}',
wp_webfonts()->generate_styles()
);
}
Expand Down

0 comments on commit 4dc5505

Please sign in to comment.