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

In this article

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

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

VersionDescription
6.5.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.