Skip to content

Commit d16d226

Browse files
anton-vlasenkoAnton Vlasenko
authored and
Anton Vlasenko
committed
Apply fixes.
1 parent 51f59e6 commit d16d226

File tree

5 files changed

+297
-340
lines changed

5 files changed

+297
-340
lines changed

lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php

+94-104
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Font Collection class.
44
*
5-
* This file contains the Font Collection class definition.
5+
* This class is used to manage a collection of fonts.
66
*
77
* @package WordPress
88
* @subpackage Font Library
@@ -12,62 +12,86 @@
1212
if ( ! class_exists( 'WP_Font_Collection' ) ) {
1313

1414
/**
15-
* Font Collection class.
15+
* Manages a collection of fonts.
16+
*
17+
* This class provides functionality to manage a group of font families, including loading font data from JSON files.
1618
*
1719
* @since 6.5.0
1820
*/
19-
final class WP_Font_Collection {
21+
class WP_Font_Collection {
2022
/**
2123
* The unique slug for the font collection.
2224
*
2325
* @since 6.5.0
24-
*
2526
* @var string
2627
*/
2728
public $slug;
2829

2930
/**
30-
* Font collection data.
31+
* The name of the font collection.
32+
*
33+
* @since 6.5.0
34+
* @var string
35+
*/
36+
public $name;
37+
38+
/**
39+
* Description of the font collection.
3140
*
3241
* @since 6.5.0
42+
* @var string
43+
*/
44+
public $description;
45+
46+
/**
47+
* Array of font families in the collection.
3348
*
49+
* @since 6.5.0
3450
* @var array
3551
*/
36-
private $data;
52+
public $font_families;
3753

3854
/**
39-
* Font collection JSON file path or url.
55+
* Categories associated with the font collection.
4056
*
4157
* @since 6.5.0
58+
* @var array
59+
*/
60+
public $categories;
61+
62+
/**
63+
* Font collection JSON cache.
4264
*
43-
* @var string
65+
* Caches the font collection data loaded from JSON to optimize performance.
66+
*
67+
* @since 6.5.0
68+
* @var array
4469
*/
45-
private $src;
70+
private static $collection_json_cache = array();
4671

4772
/**
48-
* WP_Font_Collection constructor.
73+
* Constructor for the WP_Font_Collection class.
74+
*
75+
* Initializes a new instance of the WP_Font_Collection class with the specified slug and configuration options.
4976
*
5077
* @since 6.5.0
5178
*
52-
* @param string $slug Font collection slug.
53-
* @param array|string $data_or_file {
54-
* Font collection data array or a file path or url to a JSON file containing the font collection.
79+
* @param string $slug Font collection slug.
80+
* @param array $args {
81+
* Optional. Array of configuration options for the font collection.
5582
*
56-
* @type string $name Name of the font collection.
57-
* @type string $description Description of the font collection.
58-
* @type array $font_families Array of font family definitions that are in the collection.
59-
* @type array $categories Array of categories for the fonts that are in the collection.
83+
* @type string $name Name of the font collection.
84+
* @type string $description Description of the font collection.
85+
* @type array $font_families Array of font family definitions included in the collection.
86+
* @type array $categories Array of categories associated with the fonts in the collection.
6087
* }
6188
*/
62-
public function __construct( $slug, $data_or_file ) {
63-
$this->slug = sanitize_title( $slug );
64-
65-
// Data or json are lazy loaded and validated in get_data().
66-
if ( is_array( $data_or_file ) ) {
67-
$this->data = $data_or_file;
68-
} else {
69-
$this->src = $data_or_file;
70-
}
89+
public function __construct( $slug, $args = array() ) {
90+
$this->slug = sanitize_title( $slug );
91+
$this->name = isset( $args['name'] ) ? $args['name'] : __( 'Unnamed Font Collection', 'gutenberg' );
92+
$this->description = isset( $args['description'] ) ? $args['description'] : '';
93+
$this->font_families = isset( $args['font_families'] ) ? $args['font_families'] : array();
94+
$this->categories = isset( $args['categories'] ) ? $args['categories'] : array();
7195

7296
if ( $this->slug !== $slug ) {
7397
_doing_it_wrong(
@@ -77,95 +101,83 @@ public function __construct( $slug, $data_or_file ) {
77101
'6.5.0'
78102
);
79103
}
80-
}
81-
82-
/**
83-
* Retrieves the font collection data.
84-
*
85-
* @since 6.5.0
86-
*
87-
* @return array|WP_Error An array containing the font collection data, or a WP_Error on failure.
88-
*/
89-
public function get_data() {
90-
// If we have a JSON config, load it and cache the data if it's valid.
91-
if ( $this->src && empty( $this->data ) ) {
92-
$data = $this->load_from_json( $this->src );
93-
if ( is_wp_error( $data ) ) {
94-
return $data;
95-
}
96-
97-
$this->data = $data;
98-
}
99104

100-
// Validate required properties are not empty.
101-
$data = $this->validate_data( $this->data );
102-
if ( is_wp_error( $data ) ) {
103-
return $data;
105+
if ( empty( $args['font_families'] ) ) {
106+
_doing_it_wrong(
107+
__METHOD__,
108+
/* translators: %s: Font collection slug. */
109+
sprintf( __( 'Font collection "%s" does not contain any font families.', 'gutenberg' ), $slug ),
110+
'6.5.0'
111+
);
104112
}
105-
106-
// Set defaults for optional properties.
107-
$data = wp_parse_args(
108-
$data,
109-
array(
110-
'description' => '',
111-
'categories' => array(),
112-
)
113-
);
114-
115-
return $data;
116113
}
117114

118115
/**
119-
* Loads the font collection data from a JSON file path or url.
116+
* Loads the font collection data from a json file path or url.
120117
*
121118
* @since 6.5.0
122119
*
123-
* @param string $file_or_url File path or url to a JSON file containing the font collection data.
120+
* @param string $file_or_url File path or url to a json file containing the font collection data.
124121
* @return array|WP_Error An array containing the font collection data on success,
125-
* else an instance of WP_Error on failure.
122+
* or WP_Error object on failure.
126123
*/
127-
private function load_from_json( $file_or_url ) {
124+
public static function load_from_json( $file_or_url ) {
128125
$url = wp_http_validate_url( $file_or_url );
129126
$file = file_exists( $file_or_url ) ? wp_normalize_path( realpath( $file_or_url ) ) : false;
130127

131128
if ( ! $url && ! $file ) {
132-
// translators: %s: File path or url to font collection JSON file.
133-
$message = __( 'Font collection JSON file is invalid or does not exist.', 'gutenberg' );
129+
// translators: %s: File path or url to font collection json file.
130+
$message = sprintf( __( 'Font collection JSON file "%s" is invalid or does not exist.', 'gutenberg' ), $file_or_url );
134131
_doing_it_wrong( __METHOD__, $message, '6.5.0' );
135132
return new WP_Error( 'font_collection_json_missing', $message );
136133
}
137134

138-
return $url ? $this->load_from_url( $url ) : $this->load_from_file( $file );
135+
return $url ? self::load_from_url( $url ) : self::load_from_file( $file );
139136
}
140137

141138
/**
142-
* Loads the font collection data from a JSON file path.
139+
* Loads font collection data from a JSON file path.
143140
*
144141
* @since 6.5.0
145142
*
146143
* @param string $file File path to a JSON file containing the font collection data.
147-
* @return array|WP_Error An array containing the font collection data on success,
148-
* else an instance of WP_Error on failure.
144+
* @return array|WP_Error Array containing the font collection data on success, or WP_Error object on failure.
149145
*/
150-
private function load_from_file( $file ) {
146+
private static function load_from_file( $file ) {
147+
if ( array_key_exists( $file, static::$collection_json_cache ) ) {
148+
return static::$collection_json_cache[ $file ];
149+
}
150+
151151
$data = wp_json_file_decode( $file, array( 'associative' => true ) );
152152
if ( empty( $data ) ) {
153153
return new WP_Error( 'font_collection_decode_error', __( 'Error decoding the font collection JSON file contents.', 'gutenberg' ) );
154154
}
155155

156+
if ( empty( $data['slug'] ) ) {
157+
// translators: %s: Font collection JSON URL.
158+
$message = sprintf( __( 'Font collection JSON file "%s" requires a slug.', 'gutenberg' ), $file );
159+
_doing_it_wrong( __METHOD__, $message, '6.5.0' );
160+
return new WP_Error( 'font_collection_invalid_json', $message );
161+
}
162+
163+
static::$collection_json_cache[ $file ] = $data;
164+
156165
return $data;
157166
}
158167

159168
/**
160-
* Loads the font collection data from a JSON file url.
169+
* Loads font collection data from a JSON file URL.
161170
*
162171
* @since 6.5.0
163172
*
164-
* @param string $url Url to a JSON file containing the font collection data.
165-
* @return array|WP_Error An array containing the font collection data on success,
166-
* else an instance of WP_Error on failure.
173+
* @param string $url URL to a JSON file containing the font collection data.
174+
* @return array|WP_Error Array containing the font collection data on success, or WP_Error object on failure.
167175
*/
168-
private function load_from_url( $url ) {
176+
private static function load_from_url( $url ) {
177+
if ( array_key_exists( $url, static::$collection_json_cache ) ) {
178+
return static::$collection_json_cache[ $url ];
179+
}
180+
169181
// Limit key to 167 characters to avoid failure in the case of a long url.
170182
$transient_key = substr( 'wp_font_collection_url_' . $url, 0, 167 );
171183
$data = get_site_transient( $transient_key );
@@ -182,40 +194,18 @@ private function load_from_url( $url ) {
182194
return new WP_Error( 'font_collection_decode_error', __( 'Error decoding the font collection data from the REST response JSON.', 'gutenberg' ) );
183195
}
184196

185-
// Make sure the data is valid before caching it.
186-
$data = $this->validate_data( $data );
187-
if ( is_wp_error( $data ) ) {
188-
return $data;
197+
if ( empty( $data['slug'] ) ) {
198+
// translators: %s: Font collection JSON URL.
199+
$message = sprintf( __( 'Font collection JSON file "%s" requires a slug.', 'gutenberg' ), $url );
200+
_doing_it_wrong( __METHOD__, $message, '6.5.0' );
201+
return new WP_Error( 'font_collection_invalid_json', $message );
189202
}
190203

191204
set_site_transient( $transient_key, $data, DAY_IN_SECONDS );
192205
}
193206

194-
return $data;
195-
}
207+
static::$collection_json_cache[ $url ] = $data;
196208

197-
/**
198-
* Validates the font collection configuration.
199-
*
200-
* @since 6.5.0
201-
*
202-
* @param array $data Font collection configuration.
203-
* @return array|WP_Error Array of data if valid, otherwise a WP_Error instance.
204-
*/
205-
private function validate_data( $data ) {
206-
$required_properties = array( 'name', 'font_families' );
207-
foreach ( $required_properties as $property ) {
208-
if ( empty( $data[ $property ] ) ) {
209-
$message = sprintf(
210-
// translators: 1: Font collection slug, 2: Missing property name.
211-
__( 'Font collection "%1$s" has missing or empty property: "%2$s."', 'gutenberg' ),
212-
$this->slug,
213-
$property
214-
);
215-
_doing_it_wrong( __METHOD__, $message, '6.5.0' );
216-
return new WP_Error( 'font_collection_missing_property', $message );
217-
}
218-
}
219209
return $data;
220210
}
221211
}

0 commit comments

Comments
 (0)