- Timestamp:
- 02/21/2024 07:24:14 PM (13 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/fonts/class-wp-font-collection.php
r57657 r57686 47 47 * @since 6.5.0 48 48 * 49 * @param string $slug Font collection slug. 50 * @param array|string $data_or_file Font collection data array or a path/URL to a JSON file 51 * containing the font collection. 52 * See {@see wp_register_font_collection()} for the supported fields. 53 */ 54 public function __construct( $slug, $data_or_file ) { 49 * @param string $slug Font collection slug. May only contain alphanumeric characters, dashes, 50 * and underscores. See sanitize_title(). 51 * @param array $args Font collection data. See wp_register_font_collection() for information on accepted arguments. 52 */ 53 public function __construct( string $slug, array $args ) { 55 54 $this->slug = sanitize_title( $slug ); 56 55 if ( $this->slug !== $slug ) { … … 63 62 } 64 63 65 if ( is_array( $data_or_file ) ) {66 $this->data = $this->sanitize_and_validate_data( $data_or_file ); 67 } else{64 $required_properties = array( 'name', 'font_families' ); 65 66 if ( isset( $args['font_families'] ) && is_string( $args['font_families'] ) ) { 68 67 // JSON data is lazy loaded by ::get_data(). 69 $this->src = $data_or_file; 70 } 68 $this->src = $args['font_families']; 69 unset( $args['font_families'] ); 70 71 $required_properties = array( 'name' ); 72 } 73 74 $this->data = $this->sanitize_and_validate_data( $args, $required_properties ); 71 75 } 72 76 … … 79 83 */ 80 84 public function get_data() { 85 if ( is_wp_error( $this->data ) ) { 86 return $this->data; 87 } 88 81 89 // If the collection uses JSON data, load it and cache the data/error. 82 if ( $this->src && empty( $this->data) ) {90 if ( isset( $this->src ) ) { 83 91 $this->data = $this->load_from_json( $this->src ); 84 92 } … … 117 125 } 118 126 119 return $url ? $this->load_from_url( $url ) : $this->load_from_file( $file ); 127 $data = $url ? $this->load_from_url( $url ) : $this->load_from_file( $file ); 128 129 if ( is_wp_error( $data ) ) { 130 return $data; 131 } 132 133 $data = array( 134 'name' => $this->data['name'], 135 'font_families' => $data['font_families'], 136 ); 137 138 if ( isset( $this->data['description'] ) ) { 139 $data['description'] = $this->data['description']; 140 } 141 142 if ( isset( $this->data['categories'] ) ) { 143 $data['categories'] = $this->data['categories']; 144 } 145 146 return $data; 120 147 } 121 148 … … 135 162 } 136 163 137 return $this->sanitize_and_validate_data( $data );164 return $this->sanitize_and_validate_data( $data, array( 'font_families' ) ); 138 165 } 139 166 … … 155 182 $response = wp_safe_remote_get( $url ); 156 183 if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { 157 // translators: %s: Font collection URL. 158 return new WP_Error( 'font_collection_request_error', sprintf( __( 'Error fetching the font collection data from "%s".' ), $url ) ); 184 return new WP_Error( 185 'font_collection_request_error', 186 sprintf( 187 // translators: %s: Font collection URL. 188 __( 'Error fetching the font collection data from "%s".' ), 189 $url 190 ) 191 ); 159 192 } 160 193 … … 165 198 166 199 // Make sure the data is valid before storing it in a transient. 167 $data = $this->sanitize_and_validate_data( $data );200 $data = $this->sanitize_and_validate_data( $data, array( 'font_families' ) ); 168 201 if ( is_wp_error( $data ) ) { 169 202 return $data; … … 181 214 * @since 6.5.0 182 215 * 183 * @param array $data Font collection data to sanitize and validate. 216 * @param array $data Font collection data to sanitize and validate. 217 * @param array $required_properties Required properties that must exist in the passed data. 184 218 * @return array|WP_Error Sanitized data if valid, otherwise a WP_Error instance. 185 219 */ 186 private function sanitize_and_validate_data( $data ) {220 private function sanitize_and_validate_data( $data, $required_properties = array() ) { 187 221 $schema = self::get_sanitization_schema(); 188 222 $data = WP_Font_Utils::sanitize_from_schema( $data, $schema ); 189 223 190 $required_properties = array( 'name', 'font_families' );191 224 foreach ( $required_properties as $property ) { 192 225 if ( empty( $data[ $property ] ) ) {
Note: See TracChangeset
for help on using the changeset viewer.