Skip to content

Commit

Permalink
Font Library: address feedback from wordpress-develop#6027 (#58691)
Browse files Browse the repository at this point in the history
* Removes unneeded comment in wp_get_font_dir
* Move font collection slug check to immediately after slug is assigned
* Fix translators comment indentation
* Remove unneeded colon from error message
* Use is_collection_registered in get_font_collection
* Removes extra line in WP_Font_Library::$collections docblock
* Use variable for font_dir so function isn't called repeatedly within loop
* Adds empty line before the return of WP_Font_Collection::get_data
* Uses static closure in font collection sanitization schema

Co-authored-by: creativecoder <[email protected]>
Co-authored-by: hellofromtonya <[email protected]>
  • Loading branch information
3 people authored Feb 5, 2024
1 parent 4cb9fd7 commit fd98f90
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
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.', 'gutenberg' ) );
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

0 comments on commit fd98f90

Please sign in to comment.