W3cubDocs

/WordPress

WP_Font_Collection::load_from_url( string $url ): array|WP_Error

Loads the font collection data from a JSON file URL.

Parameters

$urlstringrequired
URL to a JSON file containing the font collection data.

Return

array|WP_Error An array containing the font collection data on success, else an instance of WP_Error on failure.

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.

© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_font_collection/load_from_url