Astra_WebFont_Loader::get_remote_files_from_css( string $remote_styles = '' )

Get font files from the CSS.


Description


Parameters

$remote_styles

(string) (Optional) Remote stylesheet data.

Default value: ''


Return

(array) Returns an array of font-families and the font-files used.


Source

File: inc/lib/webfont/class-astra-webfont-loader.php

	public function get_remote_files_from_css( $remote_styles = '' ) {

		if ( '' === $remote_styles ) {
			$remote_styles = $this->remote_styles;
		}

		$font_faces = explode( '@font-face', $remote_styles );

		$result = array();

		// Loop all our font-face declarations.
		foreach ( $font_faces as $font_face ) {

			// Make sure we only process styles inside this declaration.
			$style = explode( '}', $font_face );
			$style = isset( $style[0] ) ? $style[0] : '';

			// Sanity check.
			if ( false === strpos( $style, 'font-family' ) ) {
				continue;
			}

			// Get an array of our font-families.
			preg_match_all( '/font-family.*?\;/', $style, $matched_font_families );

			// Get an array of our font-files.
			preg_match_all( '/url\(.*?\)/i', $style, $matched_font_files );

			// Get the font-family name.
			$font_family = 'unknown';
			if ( isset( $matched_font_families[0] ) && isset( $matched_font_families[0][0] ) ) {
				$font_family = rtrim( ltrim( $matched_font_families[0][0], 'font-family:' ), ';' );
				$font_family = trim( str_replace( array( "'", ';' ), '', $font_family ) );
				$font_family = sanitize_key( strtolower( str_replace( ' ', '-', $font_family ) ) );
			}

			// Make sure the font-family is set in our array.
			if ( ! isset( $result[ $font_family ] ) ) {
				$result[ $font_family ] = array();
			}

			// Get files for this font-family and add them to the array.
			foreach ( $matched_font_files as $match ) {

				// Sanity check.
				if ( ! isset( $match[0] ) ) {
					continue;
				}

				// Add the file URL.
				$result[ $font_family ][] = rtrim( ltrim( $match[0], 'url(' ), ')' );
			}

			// Make sure we have unique items.
			// We're using array_flip here instead of array_unique for improved performance.
			$result[ $font_family ] = array_flip( array_flip( $result[ $font_family ] ) );
		}

		return $result;
	}

Changelog

Changelog
Version Description
3.6.0 Introduced.


User Contributed Notes

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