Loads the font collection data from a JSON file URL.
Parameters
$url
stringrequired- URL to a JSON file containing the font collection data.
Source
private function load_from_url( $url ) {
// Limit key to 167 characters to avoid failure in the case of a long URL.
$transient_key = substr( 'wp_font_collection_url_' . $url, 0, 167 );
$data = get_site_transient( $transient_key );
if ( false === $data ) {
$response = wp_safe_remote_get( $url );
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
return new WP_Error(
'font_collection_request_error',
sprintf(
// translators: %s: Font collection URL.
__( 'Error fetching the font collection data from "%s".' ),
$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.' ) );
}
// Make sure the data is valid before storing it in a transient.
$data = $this->sanitize_and_validate_data( $data, array( 'font_families' ) );
if ( is_wp_error( $data ) ) {
return $data;
}
set_site_transient( $transient_key, $data, DAY_IN_SECONDS );
}
return $data;
}
Changelog
Version | Description |
---|---|
6.5.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.