WP_Font_Collection::load_from_jsonprivateWP 6.5.0

Loads font collection data from a JSON file or URL.

Method of the class: WP_Font_Collection{}

No Hooks.

Returns

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

Usage

// private - for code of main (parent) class only
$result = $this->load_from_json( $file_or_url );
$file_or_url(string) (required)
File path or URL to a JSON file containing the font collection data.

Changelog

Since 6.5.0 Introduced.

WP_Font_Collection::load_from_json() code WP 7.0

private function load_from_json( $file_or_url ) {
	$url  = wp_http_validate_url( $file_or_url );
	$file = file_exists( $file_or_url ) ? wp_normalize_path( realpath( $file_or_url ) ) : false;

	if ( ! $url && ! $file ) {
		// translators: %s: File path or URL to font collection JSON file.
		$message = __( 'Font collection JSON file is invalid or does not exist.' );
		_doing_it_wrong( __METHOD__, $message, '6.5.0' );
		return new WP_Error( 'font_collection_json_missing', $message );
	}

	$data = $url ? $this->load_from_url( $url ) : $this->load_from_file( $file );

	if ( is_wp_error( $data ) ) {
		return $data;
	}

	$data = array(
		'name'          => $this->data['name'],
		'font_families' => $data['font_families'],
	);

	if ( isset( $this->data['description'] ) ) {
		$data['description'] = $this->data['description'];
	}

	if ( isset( $this->data['categories'] ) ) {
		$data['categories'] = $this->data['categories'];
	}

	return $data;
}