W3cubDocs

/WordPress

WP_Font_Face::order_src( array $font_face ): array

Orders src items to optimize for browser support.

Parameters

$font_facearrayrequired
Font face to process.

Return

array Font-face with ordered src items.

Source

private function order_src( array $font_face ) {
	if ( ! is_array( $font_face['src'] ) ) {
		$font_face['src'] = (array) $font_face['src'];
	}

	$src         = array();
	$src_ordered = array();

	foreach ( $font_face['src'] as $url ) {
		// Add data URIs first.
		if ( str_starts_with( trim( $url ), 'data:' ) ) {
			$src_ordered[] = array(
				'url'    => $url,
				'format' => 'data',
			);
			continue;
		}
		$format         = pathinfo( $url, PATHINFO_EXTENSION );
		$src[ $format ] = $url;
	}

	// Add woff2.
	if ( ! empty( $src['woff2'] ) ) {
		$src_ordered[] = array(
			'url'    => $src['woff2'],
			'format' => 'woff2',
		);
	}

	// Add woff.
	if ( ! empty( $src['woff'] ) ) {
		$src_ordered[] = array(
			'url'    => $src['woff'],
			'format' => 'woff',
		);
	}

	// Add ttf.
	if ( ! empty( $src['ttf'] ) ) {
		$src_ordered[] = array(
			'url'    => $src['ttf'],
			'format' => 'truetype',
		);
	}

	// Add eot.
	if ( ! empty( $src['eot'] ) ) {
		$src_ordered[] = array(
			'url'    => $src['eot'],
			'format' => 'embedded-opentype',
		);
	}

	// Add otf.
	if ( ! empty( $src['otf'] ) ) {
		$src_ordered[] = array(
			'url'    => $src['otf'],
			'format' => 'opentype',
		);
	}
	$font_face['src'] = $src_ordered;

	return $font_face;
}

Changelog

Version Description
6.4.0 Introduced.

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