2
2
/**
3
3
* Font Collection class.
4
4
*
5
- * This file contains the Font Collection class definition .
5
+ * This class is used to manage a collection of fonts .
6
6
*
7
7
* @package WordPress
8
8
* @subpackage Font Library
12
12
if ( ! class_exists ( 'WP_Font_Collection ' ) ) {
13
13
14
14
/**
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.
16
18
*
17
19
* @since 6.5.0
18
20
*/
19
- final class WP_Font_Collection {
21
+ class WP_Font_Collection {
20
22
/**
21
23
* The unique slug for the font collection.
22
24
*
23
25
* @since 6.5.0
24
- *
25
26
* @var string
26
27
*/
27
28
public $ slug ;
28
29
29
30
/**
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.
31
40
*
32
41
* @since 6.5.0
42
+ * @var string
43
+ */
44
+ public $ description ;
45
+
46
+ /**
47
+ * Array of font families in the collection.
33
48
*
49
+ * @since 6.5.0
34
50
* @var array
35
51
*/
36
- private $ data ;
52
+ public $ font_families ;
37
53
38
54
/**
39
- * Font collection JSON file path or url .
55
+ * Categories associated with the font collection .
40
56
*
41
57
* @since 6.5.0
58
+ * @var array
59
+ */
60
+ public $ categories ;
61
+
62
+ /**
63
+ * Font collection JSON cache.
42
64
*
43
- * @var string
65
+ * Caches the font collection data loaded from JSON to optimize performance.
66
+ *
67
+ * @since 6.5.0
68
+ * @var array
44
69
*/
45
- private $ src ;
70
+ private static $ collection_json_cache = array () ;
46
71
47
72
/**
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.
49
76
*
50
77
* @since 6.5.0
51
78
*
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.
55
82
*
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.
60
87
* }
61
88
*/
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 ();
71
95
72
96
if ( $ this ->slug !== $ slug ) {
73
97
_doing_it_wrong (
@@ -77,95 +101,83 @@ public function __construct( $slug, $data_or_file ) {
77
101
'6.5.0 '
78
102
);
79
103
}
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
- }
99
104
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
+ );
104
112
}
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 ;
116
113
}
117
114
118
115
/**
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.
120
117
*
121
118
* @since 6.5.0
122
119
*
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.
124
121
* @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.
126
123
*/
127
- private function load_from_json ( $ file_or_url ) {
124
+ public static function load_from_json ( $ file_or_url ) {
128
125
$ url = wp_http_validate_url ( $ file_or_url );
129
126
$ file = file_exists ( $ file_or_url ) ? wp_normalize_path ( realpath ( $ file_or_url ) ) : false ;
130
127
131
128
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 );
134
131
_doing_it_wrong ( __METHOD__ , $ message , '6.5.0 ' );
135
132
return new WP_Error ( 'font_collection_json_missing ' , $ message );
136
133
}
137
134
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 );
139
136
}
140
137
141
138
/**
142
- * Loads the font collection data from a JSON file path.
139
+ * Loads font collection data from a JSON file path.
143
140
*
144
141
* @since 6.5.0
145
142
*
146
143
* @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.
149
145
*/
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
+
151
151
$ data = wp_json_file_decode ( $ file , array ( 'associative ' => true ) );
152
152
if ( empty ( $ data ) ) {
153
153
return new WP_Error ( 'font_collection_decode_error ' , __ ( 'Error decoding the font collection JSON file contents. ' , 'gutenberg ' ) );
154
154
}
155
155
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
+
156
165
return $ data ;
157
166
}
158
167
159
168
/**
160
- * Loads the font collection data from a JSON file url .
169
+ * Loads font collection data from a JSON file URL .
161
170
*
162
171
* @since 6.5.0
163
172
*
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.
167
175
*/
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
+
169
181
// Limit key to 167 characters to avoid failure in the case of a long url.
170
182
$ transient_key = substr ( 'wp_font_collection_url_ ' . $ url , 0 , 167 );
171
183
$ data = get_site_transient ( $ transient_key );
@@ -182,40 +194,18 @@ private function load_from_url( $url ) {
182
194
return new WP_Error ( 'font_collection_decode_error ' , __ ( 'Error decoding the font collection data from the REST response JSON. ' , 'gutenberg ' ) );
183
195
}
184
196
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 );
189
202
}
190
203
191
204
set_site_transient ( $ transient_key , $ data , DAY_IN_SECONDS );
192
205
}
193
206
194
- return $ data ;
195
- }
207
+ static ::$ collection_json_cache [ $ url ] = $ data ;
196
208
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
- }
219
209
return $ data ;
220
210
}
221
211
}
0 commit comments