Skip to content

Commit

Permalink
Rename font to webfont where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
jeyip committed Mar 23, 2022
1 parent 8dc711e commit b92fdda
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
52 changes: 26 additions & 26 deletions lib/compat/wordpress-6.0/class-wp-webfonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function init() {
* @return array[]
*/
public function get_registered_webfonts() {
return $this->registered_webfonts;
return $this->registered_webfonts;
}

/**
Expand Down Expand Up @@ -99,24 +99,24 @@ public function get_providers() {
/**
* Register a webfont.
*
* @param array $font The font arguments.
* @param array $webfont The font argument.
*/
public function register_font( $font ) {
$font = $this->validate_font( $font );
public function register_webfont( $webfont ) {
$webfont = $this->validate_webfont( $webfont );

// If not valid, bail out.
if ( ! $font ) {
if ( ! $webfont ) {
return false;
}

$slug = $this->get_font_slug( $font );
$slug = $this->get_font_slug( $webfont );

// Initialize a new font-family collection.
if ( ! isset( $this->registered_webfonts[ $slug ] ) ) {
$this->registered_webfonts[ $slug ] = array();
}

$this->registered_webfonts[ $slug ][] = $font;
$this->registered_webfonts[ $slug ][] = $webfont;
}

/**
Expand All @@ -127,7 +127,7 @@ public function register_font( $font ) {
*
* @param string|array $webfont The webfont to be enqueued.
*/
public function enqueue_font( $webfont ) {
public function enqueue_webfont( $webfont ) {
$slug = $this->get_font_slug( $webfont );

if ( isset( $this->enqueued_webfonts[ $slug ] ) ) {
Expand All @@ -148,7 +148,7 @@ public function enqueue_font( $webfont ) {
return false;
}

$this->register_font( $webfont );
$this->register_webfont( $webfont );
}

$this->enqueued_webfonts[ $slug ] = $this->registered_webfonts[ $slug ];
Expand Down Expand Up @@ -176,15 +176,15 @@ public static function get_font_slug( $to_convert ) {
}

/**
* Validate a font.
* Validate a webfont.
*
* @param array $font The font arguments.
* @param array $webfont The webfont arguments.
*
* @return array|false The validated font arguments, or false if the font is invalid.
* @return array|false The validated webfont arguments, or false if the webfont is invalid.
*/
public function validate_font( $font ) {
$font = wp_parse_args(
$font,
public function validate_webfont( $webfont ) {
$webfont = wp_parse_args(
$webfont,
array(
'provider' => 'local',
'font-family' => '',
Expand All @@ -195,23 +195,23 @@ public function validate_font( $font ) {
);

// Check the font-family.
if ( empty( $font['font-family'] ) || ! is_string( $font['font-family'] ) ) {
if ( empty( $webfont['font-family'] ) || ! is_string( $webfont['font-family'] ) ) {
trigger_error( __( 'Webfont font family must be a non-empty string.', 'gutenberg' ) );
return false;
}

// Local fonts need a "src".
if ( 'local' === $font['provider'] ) {
if ( 'local' === $webfont['provider'] ) {
// Make sure that local fonts have 'src' defined.
if ( empty( $font['src'] ) || ( ! is_string( $font['src'] ) && ! is_array( $font['src'] ) ) ) {
if ( empty( $webfont['src'] ) || ( ! is_string( $webfont['src'] ) && ! is_array( $webfont['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( $webfont['src'] ) ) {
foreach ( (array) $webfont['src'] as $src ) {
if ( empty( $src ) || ! is_string( $src ) ) {
trigger_error( __( 'Each webfont src must be a non-empty string.', 'gutenberg' ) );
return false;
Expand All @@ -220,14 +220,14 @@ public function validate_font( $font ) {
}

// Check the font-weight.
if ( ! is_string( $font['font-weight'] ) && ! is_int( $font['font-weight'] ) ) {
if ( ! is_string( $webfont['font-weight'] ) && ! is_int( $webfont['font-weight'] ) ) {
trigger_error( __( 'Webfont font weight must be a properly formatted string or integer.', 'gutenberg' ) );
return false;
}

// Check the font-display.
if ( ! in_array( $font['font-display'], array( 'auto', 'block', 'fallback', 'swap' ), true ) ) {
$font['font-display'] = 'fallback';
if ( ! in_array( $webfont['font-display'], array( 'auto', 'block', 'fallback', 'swap' ), true ) ) {
$webfont['font-display'] = 'fallback';
}

$valid_props = array(
Expand All @@ -250,13 +250,13 @@ public function validate_font( $font ) {
'provider',
);

foreach ( $font as $prop => $value ) {
foreach ( $webfont as $prop => $value ) {
if ( ! in_array( $prop, $valid_props, true ) ) {
unset( $font[ $prop ] );
unset( $webfont[ $prop ] );
}
}

return $font;
return $webfont;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function gutenberg_register_webfonts_from_theme_json() {
}
}
foreach ( $webfonts as $webfont ) {
wp_webfonts()->register_font( $webfont );
wp_webfonts()->register_webfont( $webfont );
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/compat/wordpress-6.0/webfonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function wp_register_webfonts( array $webfonts = array() ) {
* @param array $webfont Webfont to be registered.
*/
function wp_register_webfont( array $webfont ) {
wp_webfonts()->register_font( $webfont );
wp_webfonts()->register_webfont( $webfont );
}

/**
Expand Down Expand Up @@ -158,7 +158,7 @@ function wp_enqueue_webfonts( $webfonts ) {
* If array, register and enqueues a new webfont.
*/
function wp_enqueue_webfont( $webfont ) {
wp_webfonts()->enqueue_font( $webfont );
wp_webfonts()->enqueue_webfont( $webfont );
}

/**
Expand Down

0 comments on commit b92fdda

Please sign in to comment.