From 20cbc75aac972b9497ece7b816396ec868f947f0 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 5 Feb 2024 14:48:49 +0000 Subject: [PATCH 1/8] Translate error string Addresses https://github.com/WordPress/wordpress-develop/pull/5285/files#r1345355956 --- lib/compat/wordpress-6.5/fonts/class-wp-font-library.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-font-library.php b/lib/compat/wordpress-6.5/fonts/class-wp-font-library.php index 9bf90ca1d9dd8..ce0841015b39c 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-font-library.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-font-library.php @@ -125,7 +125,7 @@ public function get_font_collection( $slug ) { if ( array_key_exists( $slug, $this->collections ) ) { return $this->collections[ $slug ]; } - return new WP_Error( 'font_collection_not_found', 'Font collection not found.' ); + return new WP_Error( 'font_collection_not_found', __( 'Font collection not found.', 'gutenberg' ) ); } /** From 4f846fa530a6712d949240caf8ffe5626eb57b05 Mon Sep 17 00:00:00 2001 From: Grant Kinney Date: Mon, 5 Feb 2024 09:33:02 -0600 Subject: [PATCH 2/8] Improve get_allowed_font_mime_types comment --- lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php b/lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php index b75ce0ec41cf6..deef23b90a65e 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php @@ -215,13 +215,17 @@ private static function apply_sanitizer( $value, $sanitizer ) { } /** - * Provide the expected mime-type value for font files per-PHP release. Due to differences in the values returned these values differ between PHP versions. + * Returns the expected mime-type values for font files, depending on PHP version. * - * This is necessary until a collection of valid mime-types per-file extension can be provided to 'upload_mimes' filter. + * This is needed because font mime types vary by PHP version, so checking the PHP version + * is necessary until a list of valid mime-types for each file extension can be provided to + * the 'upload_mimes' filter. * * @since 6.5.0 * - * @return Array A collection of mime types keyed by file extension. + * @access private + * + * @return array A collection of mime types keyed by file extension. */ public static function get_allowed_font_mime_types() { $php_7_ttf_mime_type = PHP_VERSION_ID >= 70300 ? 'application/font-sfnt' : 'application/x-font-ttf'; From 82c04d51f0b535e1a0ed17eaa967031b34dc486a Mon Sep 17 00:00:00 2001 From: Grant Kinney Date: Mon, 5 Feb 2024 09:41:14 -0600 Subject: [PATCH 3/8] Use str_contains rather than strpos --- lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php b/lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php index deef23b90a65e..23c95d633fb47 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php @@ -44,7 +44,7 @@ public static function sanitize_font_family( $font_family ) { $wrapped_font_families = array_map( function ( $family ) { $trimmed = trim( $family ); - if ( ! empty( $trimmed ) && false !== strpos( $trimmed, ' ' ) && false === strpos( $trimmed, "'" ) && false === strpos( $trimmed, '"' ) ) { + if ( ! empty( $trimmed ) && str_contains( $trimmed, ' ' ) && ! str_contains( $trimmed, "'" ) && ! str_contains( $trimmed, '"' ) ) { return '"' . $trimmed . '"'; } return $trimmed; From e187b03e4b6c0ee72704736ec048ef16c5d9f8c3 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 5 Feb 2024 16:10:53 +0000 Subject: [PATCH 4/8] Move period outside double quotes --- .../wordpress-6.5/fonts/class-wp-font-collection.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php b/lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php index 019cf0f3f0b29..65655000c8fdf 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php @@ -48,10 +48,10 @@ final class WP_Font_Collection { * * @since 6.5.0 * - * @param string $slug Font collection slug. - * @param array|string $data_or_file Font collection data array or a path/URL to a JSON file - * containing the font collection. - * See {@see wp_register_font_collection()} for the supported fields. + * @param string $slug Font collection slug. + * @param array|string $data_or_file Font collection data array or a path/URL to a JSON file + * containing the font collection. + * See {@see wp_register_font_collection()} for the supported fields. */ public function __construct( $slug, $data_or_file ) { $this->slug = sanitize_title( $slug ); @@ -194,7 +194,7 @@ private function sanitize_and_validate_data( $data ) { if ( empty( $data[ $property ] ) ) { $message = sprintf( // translators: 1: Font collection slug, 2: Missing property name. - __( 'Font collection "%1$s" has missing or empty property: "%2$s."', 'gutenberg' ), + __( 'Font collection "%1$s" has missing or empty property: "%2$s".', 'gutenberg' ), $this->slug, $property ); From ab62473df15d54f8ccfe96e1770294581593cd07 Mon Sep 17 00:00:00 2001 From: Grant Kinney Date: Mon, 5 Feb 2024 10:39:29 -0600 Subject: [PATCH 5/8] Correct error message --- lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php b/lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php index 65655000c8fdf..f63313167a537 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php @@ -162,7 +162,7 @@ private function load_from_url( $url ) { $data = json_decode( wp_remote_retrieve_body( $response ), true ); if ( empty( $data ) ) { - return new WP_Error( 'font_collection_decode_error', __( 'Error decoding the font collection data from the REST response JSON.', 'gutenberg' ) ); + return new WP_Error( 'font_collection_decode_error', __( 'Error decoding the font collection data from the http response JSON.', 'gutenberg' ) ); } // Make sure the data is valid before storing it in a transient. From f6f513f5c868a2a35ebda20c1e4fbcfb45173415 Mon Sep 17 00:00:00 2001 From: Grant Kinney Date: Mon, 5 Feb 2024 11:09:22 -0600 Subject: [PATCH 6/8] Use sprintf for parameter names in translated strings --- .../fonts/class-wp-font-collection.php | 2 +- ...ss-wp-rest-font-collections-controller.php | 2 +- .../class-wp-rest-font-faces-controller.php | 19 ++++++++++--------- ...class-wp-rest-font-families-controller.php | 10 ++++++---- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php b/lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php index f63313167a537..d0c1b584ae511 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php @@ -193,7 +193,7 @@ private function sanitize_and_validate_data( $data ) { foreach ( $required_properties as $property ) { if ( empty( $data[ $property ] ) ) { $message = sprintf( - // translators: 1: Font collection slug, 2: Missing property name. + // translators: 1: Font collection slug, 2: Missing property name, e.g. "font_families". __( 'Font collection "%1$s" has missing or empty property: "%2$s".', 'gutenberg' ), $this->slug, $property diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-collections-controller.php b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-collections-controller.php index 06a073d426abc..90ee80649bb48 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-collections-controller.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-collections-controller.php @@ -319,7 +319,7 @@ public function get_items_permissions_check( $request ) { // phpcs:ignore Variab return new WP_Error( 'rest_cannot_read', - __( 'Sorry, you are not allowed to use the Font Library on this site.', 'gutenberg' ), + __( 'Sorry, you are not allowed to access font collections.', 'gutenberg' ), array( 'status' => rest_authorization_required_code(), ) diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php index 60f04e9ff23df..c2b9e785aaf74 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php @@ -180,8 +180,8 @@ public function validate_create_font_face_settings( $value, $request ) { if ( isset( $settings[ $key ] ) && ! $settings[ $key ] ) { return new WP_Error( 'rest_invalid_param', - /* translators: %s: Font family setting key. */ - sprintf( __( 'font_face_setting[%s] cannot be empty.', 'gutenberg' ), $key ), + /* translators: %s: Name of the missing font face setting parameter, e.g. "font_face_settings[src]". */ + sprintf( __( '%s cannot be empty.', 'gutenberg' ), "font_face_setting[ $key ]" ), array( 'status' => 400 ) ); } @@ -196,7 +196,8 @@ public function validate_create_font_face_settings( $value, $request ) { if ( empty( $src ) ) { return new WP_Error( 'rest_invalid_param', - __( 'font_face_settings[src] values must be non-empty strings.', 'gutenberg' ), + /* translators: %s: Font face source parameter name: "font_face_settings[src]". */ + sprintf( __( '%s values must be non-empty strings.', 'gutenberg' ), 'font_face_settings[src]' ), array( 'status' => 400 ) ); } @@ -205,8 +206,8 @@ public function validate_create_font_face_settings( $value, $request ) { if ( false === wp_http_validate_url( $src ) && ! isset( $files[ $src ] ) ) { return new WP_Error( 'rest_invalid_param', - /* translators: %s: src value in the font face settings. */ - sprintf( __( 'font_face_settings[src] value "%s" must be a valid URL or file reference.', 'gutenberg' ), $src ), + /* translators: 1: Font face source parameter name: "font_face_settings[src]", 2: The invalid src value. */ + sprintf( __( '%1$s value "%2$s" must be a valid URL or file reference.', 'gutenberg' ), 'font_face_settings[src]', $src ), array( 'status' => 400 ) ); } @@ -217,8 +218,8 @@ public function validate_create_font_face_settings( $value, $request ) { if ( ! in_array( $file, $srcs, true ) ) { return new WP_Error( 'rest_invalid_param', - // translators: %s: File key (e.g. `file-0`) in the request data. - sprintf( __( 'File %1$s must be used in font_face_settings[src].', 'gutenberg' ), $file ), + /* translators: 1: File key (e.g. "file-0") in the request data, 2: Font face source parameter name: "font_face_settings[src]". */ + sprintf( __( 'File %1$s must be used in %2$s.', 'gutenberg' ), $file, 'font_face_settings[src]' ), array( 'status' => 400 ) ); } @@ -291,7 +292,7 @@ public function get_item( $request ) { return new WP_Error( 'rest_font_face_parent_id_mismatch', /* translators: %d: A post id. */ - sprintf( __( 'The font face does not belong to the specified font family with id of "%d"', 'gutenberg' ), $font_family->ID ), + sprintf( __( 'The font face does not belong to the specified font family with id of "%d".', 'gutenberg' ), $font_family->ID ), array( 'status' => 404 ) ); } @@ -405,7 +406,7 @@ public function delete_item( $request ) { return new WP_Error( 'rest_font_face_parent_id_mismatch', /* translators: %d: A post id. */ - sprintf( __( 'The font face does not belong to the specified font family with id of "%d"', 'gutenberg' ), $font_family->ID ), + sprintf( __( 'The font face does not belong to the specified font family with id of "%d".', 'gutenberg' ), $font_family->ID ), array( 'status' => 404 ) ); } diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-families-controller.php b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-families-controller.php index e4a2b2f8e9781..dbd59abd6085a 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-families-controller.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-families-controller.php @@ -86,7 +86,8 @@ public function validate_font_family_settings( $value, $request ) { if ( null === $settings ) { return new WP_Error( 'rest_invalid_param', - __( 'font_family_settings parameter must be a valid JSON string.', 'gutenberg' ), + /* translators: %s: Parameter name: "font_family_settings". */ + sprintf( __( '%s parameter must be a valid JSON string.', 'gutenberg' ), 'font_family_settings' ), array( 'status' => 400 ) ); } @@ -102,7 +103,8 @@ public function validate_font_family_settings( $value, $request ) { if ( isset( $settings['slug'] ) ) { return new WP_Error( 'rest_invalid_param', - __( 'font_family_settings[slug] cannot be updated.', 'gutenberg' ), + /* translators: %s: Name of parameter being updated: font_family_settings[slug]". */ + sprintf( __( '%s cannot be updated.', 'gutenberg' ), 'font_family_settings[slug]' ), array( 'status' => 400 ) ); } @@ -121,8 +123,8 @@ public function validate_font_family_settings( $value, $request ) { if ( isset( $settings[ $key ] ) && ! $settings[ $key ] ) { return new WP_Error( 'rest_invalid_param', - /* translators: %s: Font family setting key. */ - sprintf( __( 'font_family_settings[%s] cannot be empty.', 'gutenberg' ), $key ), + /* translators: %s: Name of the empty font family setting parameter, e.g. "font_family_settings[slug]". */ + sprintf( __( '%s cannot be empty.', 'gutenberg' ), "font_family_settings[ $key ]" ), array( 'status' => 400 ) ); } From 59e6fdee0a5ce8009913cc54a5f7c22703a3ef63 Mon Sep 17 00:00:00 2001 From: Grant Kinney Date: Mon, 5 Feb 2024 11:14:10 -0600 Subject: [PATCH 7/8] Capitalize HTTP --- lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php | 2 +- .../wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php b/lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php index d0c1b584ae511..31bb410eb4d86 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php @@ -162,7 +162,7 @@ private function load_from_url( $url ) { $data = json_decode( wp_remote_retrieve_body( $response ), true ); if ( empty( $data ) ) { - return new WP_Error( 'font_collection_decode_error', __( 'Error decoding the font collection data from the http response JSON.', 'gutenberg' ) ); + return new WP_Error( 'font_collection_decode_error', __( 'Error decoding the font collection data from the HTTP response JSON.', 'gutenberg' ) ); } // Make sure the data is valid before storing it in a transient. diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php index c2b9e785aaf74..0ecd069fc230c 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php @@ -180,7 +180,7 @@ public function validate_create_font_face_settings( $value, $request ) { if ( isset( $settings[ $key ] ) && ! $settings[ $key ] ) { return new WP_Error( 'rest_invalid_param', - /* translators: %s: Name of the missing font face setting parameter, e.g. "font_face_settings[src]". */ + /* translators: %s: Name of the missing font face settings parameter, e.g. "font_face_settings[src]". */ sprintf( __( '%s cannot be empty.', 'gutenberg' ), "font_face_setting[ $key ]" ), array( 'status' => 400 ) ); From 28bbc36994e0aa65d34983cb35f9e3bc1271f87c Mon Sep 17 00:00:00 2001 From: Grant Kinney Date: Mon, 5 Feb 2024 11:16:34 -0600 Subject: [PATCH 8/8] Updates tests --- .../tests/fonts/font-library/wpRestFontFacesController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpunit/tests/fonts/font-library/wpRestFontFacesController.php b/phpunit/tests/fonts/font-library/wpRestFontFacesController.php index 273862e3047e8..042928c73da3f 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontFacesController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontFacesController.php @@ -350,7 +350,7 @@ public function test_get_item_invalid_parent_id() { $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_font_face_parent_id_mismatch', $response, 404 ); - $expected_message = 'The font face does not belong to the specified font family with id of "' . self::$other_font_family_id . '"'; + $expected_message = 'The font face does not belong to the specified font family with id of "' . self::$other_font_family_id . '".'; $this->assertSame( $expected_message, $response->as_error()->get_error_messages()[0], 'The message must contain the correct parent ID.' ); } @@ -928,7 +928,7 @@ public function test_delete_item_invalid_parent_id() { $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_font_face_parent_id_mismatch', $response, 404, 'The response should return an error for "rest_font_face_parent_id_mismatch" with 404 status.' ); - $expected_message = 'The font face does not belong to the specified font family with id of "' . self::$other_font_family_id . '"'; + $expected_message = 'The font face does not belong to the specified font family with id of "' . self::$other_font_family_id . '".'; $this->assertSame( $expected_message, $response->as_error()->get_error_messages()[0], 'The message must contain the correct parent ID.' ); }