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

Font Library: address feedback from wordpress-develop#6027 #58691

Merged
merged 9 commits into from
Feb 5, 2024
18 changes: 9 additions & 9 deletions lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,6 @@ final class WP_Font_Collection {
*/
public function __construct( $slug, $data_or_file ) {
$this->slug = sanitize_title( $slug );

if ( is_array( $data_or_file ) ) {
$this->data = $this->sanitize_and_validate_data( $data_or_file );
} else {
// JSON data is lazy loaded by ::get_data().
$this->src = $data_or_file;
}

if ( $this->slug !== $slug ) {
_doing_it_wrong(
__METHOD__,
Expand All @@ -71,6 +63,13 @@ public function __construct( $slug, $data_or_file ) {
'6.5.0'
);
}

if ( is_array( $data_or_file ) ) {
$this->data = $this->sanitize_and_validate_data( $data_or_file );
} else {
// JSON data is lazy loaded by ::get_data().
$this->src = $data_or_file;
}
}

/**
Expand All @@ -95,6 +94,7 @@ public function get_data() {
'description' => '',
'categories' => array(),
);

return wp_parse_args( $this->data, $defaults );
}

Expand Down Expand Up @@ -229,7 +229,7 @@ private static function get_sanitization_schema() {
'fontFamily' => 'sanitize_text_field',
'fontStyle' => 'sanitize_text_field',
'fontWeight' => 'sanitize_text_field',
'src' => function ( $value ) {
'src' => static function ( $value ) {
return is_array( $value )
? array_map( 'sanitize_text_field', $value )
: sanitize_text_field( $value );
Expand Down
7 changes: 3 additions & 4 deletions lib/compat/wordpress-6.5/fonts/class-wp-font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class WP_Font_Library {
* Font collections.
*
* @since 6.5.0
*
* @var array
*/
private $collections = array();
Expand Down Expand Up @@ -52,8 +51,8 @@ public function register_font_collection( $slug, $data_or_file ) {

if ( $this->is_collection_registered( $new_collection->slug ) ) {
$error_message = sprintf(
/* translators: %s: Font collection slug. */
__( 'Font collection with slug: "%s" is already registered.', 'gutenberg' ),
/* translators: %s: Font collection slug. */
__( 'Font collection with slug "%s" is already registered.', 'gutenberg' ),
$new_collection->slug
);
_doing_it_wrong(
Expand Down Expand Up @@ -122,7 +121,7 @@ public function get_font_collections() {
* or WP_Error object if the font collection doesn't exist.
*/
public function get_font_collection( $slug ) {
if ( array_key_exists( $slug, $this->collections ) ) {
if ( $this->is_collection_registered( $slug ) ) {
return $this->collections[ $slug ];
}
return new WP_Error( 'font_collection_not_found', 'Font collection not found.' );
Expand Down
4 changes: 2 additions & 2 deletions lib/compat/wordpress-6.5/fonts/fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ function gutenberg_register_font_collections() {
* }
*/
function wp_get_font_dir( $defaults = array() ) {
// Multi site path
$site_path = '';
if ( is_multisite() && ! ( is_main_network() && is_main_site() ) ) {
$site_path = '/sites/' . get_current_blog_id();
Expand Down Expand Up @@ -258,9 +257,10 @@ function _wp_before_delete_font_face( $post_id, $post ) {
}

$font_files = get_post_meta( $post_id, '_wp_font_face_file', false );
$font_dir = wp_get_font_dir()['path'];

foreach ( $font_files as $font_file ) {
wp_delete_file( wp_get_font_dir()['path'] . '/' . $font_file );
wp_delete_file( $font_dir . '/' . $font_file );
}
}
add_action( 'before_delete_post', '_wp_before_delete_font_face', 10, 2 );
Expand Down
Loading