| 1 |
<?php |
| 2 |
namespace Upress\EzCache\FileOptimizer; |
| 3 |
|
| 4 |
/** |
| 5 |
* Combine Google Fonts |
| 6 |
* |
| 7 |
* @since 3.1 |
| 8 |
* @author Remy Perona |
| 9 |
*/ |
| 10 |
class CombineGoogleFonts extends BaseFileOptimizer { |
| 11 |
protected $fonts; |
| 12 |
protected $subsets; |
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
$this->fonts = []; |
| 16 |
$this->subsets = []; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Combines multiple Google Fonts links into one |
| 21 |
* |
| 22 |
* @param string $html HTML content. |
| 23 |
* |
| 24 |
* @return string |
| 25 |
*/ |
| 26 |
public function optimize( $html ) { |
| 27 |
$fonts = $this->find( '<link(?:\s+(?:(?!href\s*=\s*)[^>])+)?(?:\s+href\s*=\s*([\'"])((?:https?:)?\/\/fonts\.googleapis\.com\/css(?:(?!\1).)+)\1)(?:\s+[^>]*)?>', $html ); |
| 28 |
|
| 29 |
if ( ! $fonts ) { |
| 30 |
return $html; |
| 31 |
} |
| 32 |
|
| 33 |
$this->parse( $fonts ); |
| 34 |
|
| 35 |
if ( empty( $this->fonts ) ) { |
| 36 |
return $html; |
| 37 |
} |
| 38 |
|
| 39 |
$html = str_replace( '</title>', '</title>' . $this->get_combine_tag(), $html ); |
| 40 |
|
| 41 |
foreach ( $fonts as $font ) { |
| 42 |
$html = str_replace( $font[0], '', $html ); |
| 43 |
} |
| 44 |
|
| 45 |
return $html; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Finds links to Google fonts |
| 50 |
* |
| 51 |
* @param string $pattern Pattern to search for. |
| 52 |
* @param string $html HTML content. |
| 53 |
* |
| 54 |
* @return bool|array |
| 55 |
*/ |
| 56 |
protected function find( $pattern, $html ) { |
| 57 |
preg_match_all( '/' . $pattern . '/uUmsi', $html, $matches, PREG_SET_ORDER ); |
| 58 |
|
| 59 |
if ( count( $matches ) <= 1 ) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
return $matches; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Parses found matches to extract fonts and subsets |
| 68 |
* |
| 69 |
* @param array $matches Found matches for the pattern. |
| 70 |
*/ |
| 71 |
protected function parse( $matches ) { |
| 72 |
foreach ( $matches as $match ) { |
| 73 |
$query = $this->get_url_component( $match[2], PHP_URL_QUERY ); |
| 74 |
|
| 75 |
if ( ! isset( $query ) ) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
$query = html_entity_decode( $query ); |
| 80 |
$font = wp_parse_args( $query ); |
| 81 |
|
| 82 |
// Add font to the collection. |
| 83 |
$this->fonts[] = rawurlencode( htmlentities( $font['family'] ) ); |
| 84 |
|
| 85 |
// Add subset to collection. |
| 86 |
$this->subsets[] = isset( $font['subset'] ) ? rawurlencode( htmlentities( $font['subset'] ) ) : ''; |
| 87 |
} |
| 88 |
|
| 89 |
// Concatenate fonts tag. |
| 90 |
$this->subsets = ( $this->subsets ) ? '&subset=' . implode( ',', array_filter( array_unique( $this->subsets ) ) ) : ''; |
| 91 |
$this->fonts = implode( '|', array_filter( array_unique( $this->fonts ) ) ); |
| 92 |
$this->fonts = str_replace( '|', '%7C', $this->fonts ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Returns the combined Google fonts link tag |
| 97 |
* |
| 98 |
* @return string |
| 99 |
*/ |
| 100 |
protected function get_combine_tag() { |
| 101 |
return '<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=' . $this->fonts . $this->subsets . '" />'; |
| 102 |
} |
| 103 |
} |
| 104 |
|