1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Embeds webfonts in styles. |
4
|
|
|
* |
5
|
|
|
* @package Kirki |
6
|
|
|
* @category Core |
7
|
|
|
* @author Aristeides Stathopoulos |
8
|
|
|
* @copyright Copyright (c) 2017, Aristeides Stathopoulos |
9
|
|
|
* @license http://opensource.org/licenses/https://opensource.org/licenses/MIT |
10
|
|
|
* @since 3.0 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Manages the way Google Fonts are enqueued. |
15
|
|
|
*/ |
16
|
|
|
final class Kirki_Modules_Webfonts_Embed { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The config ID. |
20
|
|
|
* |
21
|
|
|
* @access protected |
22
|
|
|
* @since 3.0.0 |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $config_id; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The Kirki_Modules_Webfonts object. |
29
|
|
|
* |
30
|
|
|
* @access protected |
31
|
|
|
* @since 3.0.0 |
32
|
|
|
* @var object |
33
|
|
|
*/ |
34
|
|
|
protected $webfonts; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The Kirki_Fonts_Google object. |
38
|
|
|
* |
39
|
|
|
* @access protected |
40
|
|
|
* @since 3.0.0 |
41
|
|
|
* @var object |
42
|
|
|
*/ |
43
|
|
|
protected $googlefonts; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The google link |
47
|
|
|
* |
48
|
|
|
* @access public |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
public $link = ''; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Constructor. |
55
|
|
|
* |
56
|
|
|
* @access public |
57
|
|
|
* @since 3.0 |
58
|
|
|
*/ |
59
|
|
|
public function __construct( $config_id, $webfonts, $googlefonts, $args = array() ) { |
|
|
|
|
60
|
|
|
|
61
|
|
|
$this->config_id = $config_id; |
62
|
|
|
$this->webfonts = $webfonts; |
63
|
|
|
$this->googlefonts = $googlefonts; |
64
|
|
|
|
65
|
|
|
add_filter( "kirki/{$config_id}/dynamic_css", array( $this, 'embed_css' ) ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Embeds the CSS from googlefonts API inside the Kirki output CSS. |
70
|
|
|
* |
71
|
|
|
* @access public |
72
|
|
|
* @since 3.0.0 |
73
|
|
|
* @param string $css The original CSS. |
74
|
|
|
* @return string The modified CSS. |
75
|
|
|
*/ |
76
|
|
|
public function embed_css( $css ) { |
77
|
|
|
|
78
|
|
|
// Go through our fields and populate $this->fonts. |
79
|
|
|
$this->webfonts->loop_fields(); |
80
|
|
|
|
81
|
|
|
$this->googlefonts->fonts = apply_filters( 'kirki/enqueue_google_fonts', $this->googlefonts->fonts ); |
82
|
|
|
|
83
|
|
|
// Goes through $this->fonts and adds or removes things as needed. |
84
|
|
|
$this->googlefonts->process_fonts(); |
85
|
|
|
|
86
|
|
|
// Go through $this->fonts and populate $this->link. |
87
|
|
|
$link_obj = new Kirki_Modules_Webfonts_Link( $this->config_id, $this->webfonts, $this->googlefonts ); |
88
|
|
|
$link_obj->create_link(); |
89
|
|
|
$this->link = $link_obj->link; |
90
|
|
|
|
91
|
|
|
// If $this->link is not empty then enqueue it. |
92
|
|
|
if ( '' !== $this->link ) { |
93
|
|
|
return $this->get_url_contents( $this->link ) . "\n" . $css; |
94
|
|
|
} |
95
|
|
|
return $css; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the contents of a remote google-fonts link. |
100
|
|
|
* Responses get cached for 1 day. |
101
|
|
|
* |
102
|
|
|
* @access protected |
103
|
|
|
* @since 3.0.0 |
104
|
|
|
* @param string $url The link we want to get. |
105
|
|
|
* @return string|false Returns false if there's an error. |
106
|
|
|
*/ |
107
|
|
|
protected function get_url_contents( $url = '' ) { |
108
|
|
|
|
109
|
|
|
// If $url is not set, use $this->link. |
110
|
|
|
$url = ( '' === $url ) ? $this->link : $url; |
111
|
|
|
|
112
|
|
|
// Sanitize the URL. |
113
|
|
|
$url = esc_url_raw( $url ); |
114
|
|
|
|
115
|
|
|
// The transient name. |
116
|
|
|
$transient_name = 'kirki_googlefonts_contents_' . md5( $url ); |
117
|
|
|
|
118
|
|
|
// Get the transient value. |
119
|
|
|
$html = get_transient( $transient_name ); |
120
|
|
|
|
121
|
|
|
// Check for transient, if none, grab remote HTML file. |
122
|
|
|
if ( false === $html ) { |
123
|
|
|
|
124
|
|
|
// Get remote HTML file. |
125
|
|
|
$response = wp_remote_get( $url ); |
|
|
|
|
126
|
|
|
|
127
|
|
|
// Check for error. |
128
|
|
|
if ( is_wp_error( $response ) ) { |
129
|
|
|
set_transient( 'kirki_googlefonts_fallback_to_link', 'yes', HOUR_IN_SECONDS ); |
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// Parse remote HTML file. |
134
|
|
|
$data = wp_remote_retrieve_body( $response ); |
135
|
|
|
|
136
|
|
|
// Check for error. |
137
|
|
|
if ( is_wp_error( $data ) ) { |
138
|
|
|
set_transient( 'kirki_googlefonts_fallback_to_link', 'yes', HOUR_IN_SECONDS ); |
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// If empty, return false. |
143
|
|
|
if ( ! $data ) { |
144
|
|
|
set_transient( 'kirki_googlefonts_fallback_to_link', 'yes', HOUR_IN_SECONDS ); |
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// Store remote HTML file in transient, expire after 24 hours. |
149
|
|
|
set_transient( $transient_name, $data, DAY_IN_SECONDS ); |
150
|
|
|
set_transient( 'kirki_googlefonts_fallback_to_link', 'no', DAY_IN_SECONDS ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $html; |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.