self::USER_AGENT, ) ); $code = wp_remote_retrieve_response_code( $response ); if ( 200 !== $code ) { return ''; } return wp_remote_retrieve_body( $response ); } /** * Parse the stylesheet and build it into a font object which OMGF can understand. * * @param string $stylesheet A valid CSS stylesheet. * * @return array */ private static function convert_to_fonts_object( $stylesheet ) { preg_match_all( '/font-family:\s\'(.*?)\';/', $stylesheet, $font_families ); if ( empty( $font_families[1] ) ) { return array(); } $font_families = array_unique( $font_families[1] ); $object = array(); foreach ( $font_families as $font_family ) { $slug = sanitize_title( $font_family ); $object[ $slug ] = array( 'slug' => $slug, 'fontFamily' => $font_family, 'variants' => self::parse_variants( $stylesheet, $font_family ), 'subsets' => self::parse_subsets( $stylesheet, $font_family ), ); } return $object; } /** * Parse a stylesheet from Google Fonts' API into a valid Font Object. * * @param string $stylesheet The stylesheet to parse. * @param string $font_family The font family used in the parse. * * @return array */ private static function parse_variants( $stylesheet, $font_family ) { /** * This also captures the commented Subset name. */ preg_match_all( '/\/\*\s.*?}/s', $stylesheet, $font_faces ); if ( empty( $font_faces[0] ) ) { return array(); } $font_object = array(); foreach ( $font_faces[0] as $font_face ) { // Check for exact match of font-family. if ( ! preg_match( '/font-family:[\s\'"]*?' . $font_family . '[\'"]?;/', $font_face ) ) { continue; } preg_match( '/font-style:\s(normal|italic);/', $font_face, $font_style ); preg_match( '/font-weight:\s([0-9]+);/', $font_face, $font_weight ); preg_match( '/src:\surl\((.*?woff2)\)/', $font_face, $font_src ); preg_match( '/\/\*\s([a-z\-0-9\[\]]+?)\s\*\//', $font_face, $subset ); preg_match( '/unicode-range:\s(.*?);/', $font_face, $range ); $subset = ! empty( $subset[1] ) ? trim( $subset[1], '[]' ) : ''; /** * Remove variants that have subset the user doesn't need. */ $allowed_subsets = apply_filters( 'generatepress_google_font_subsets', GeneratePress_Pro_Font_Library::get_settings( 'preferred_subset' ) ); if ( empty( $allowed_subsets ) ) { $allowed_subsets = array( 'latin' ); } if ( ! empty( $subset ) && ! in_array( $subset, $allowed_subsets, true ) && ! is_numeric( $subset ) ) { continue; } /** * If $subset is empty, assume it's a logographic (Chinese, Japanese, etc.) character set. * * @TODO: Apply subset setting here. */ if ( is_numeric( $subset ) ) { $subset = 'logogram-' . $subset; } $key = $subset . '-' . $font_weight[1] . ( 'normal' === $font_style[1] ? '' : '-' . $font_style[1] ); // Setup font object data. $font_object[ $key ] = array( 'fontFamily' => $font_family, 'fontStyle' => $font_style[1], 'fontWeight' => $font_weight[1], 'src' => $font_src[1], ); if ( ! empty( $subset ) ) { $font_object[ $key ]['subset'] = $subset; } if ( ! empty( $range ) && isset( $range[1] ) ) { $font_object[ $key ]['range'] = $range[1]; } } return $font_object; } /** * Parse stylesheets for subsets, which in Google Fonts stylesheets are always * included, commented above each @font-face statements, e.g. /* latin-ext * * @param string $stylesheet The stylesheet to parse. * @param string $font_family The font family used in the parse. * * @return array */ private static function parse_subsets( $stylesheet, $font_family ) { preg_match_all( '/\/\*\s([a-z\-]+?)\s\*\//', $stylesheet, $subsets ); if ( empty( $subsets[1] ) ) { return array(); } $subsets = array_unique( $subsets[1] ); return $subsets; } }