Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webfonts: change class properties from static to instance members #39361

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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