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
|
|
|
* @param string $config_id The config-ID. |
59
|
|
|
* @param object $webfonts The Kirki_Modules_Webfonts object. |
60
|
|
|
* @param object $googlefonts The Kirki_Fonts_Google object. |
61
|
|
|
* @param array $args Extra args we want to pass. |
62
|
|
|
*/ |
63
|
|
|
public function __construct( $config_id, $webfonts, $googlefonts, $args = array() ) { |
|
|
|
|
64
|
|
|
|
65
|
|
|
$this->config_id = $config_id; |
66
|
|
|
$this->webfonts = $webfonts; |
67
|
|
|
$this->googlefonts = $googlefonts; |
68
|
|
|
|
69
|
|
|
add_filter( "kirki/{$config_id}/dynamic_css", array( $this, 'embed_css' ) ); |
70
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'inline_css' ), 999 ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Adds inline css. |
75
|
|
|
* |
76
|
|
|
* @access public |
77
|
|
|
* @since 3.0.0 |
78
|
|
|
*/ |
79
|
|
|
public function inline_css() { |
80
|
|
|
wp_add_inline_style( 'kirki-styles', $this->embed_css() ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Embeds the CSS from googlefonts API inside the Kirki output CSS. |
85
|
|
|
* |
86
|
|
|
* @access public |
87
|
|
|
* @since 3.0.0 |
88
|
|
|
* @param string $css The original CSS. |
89
|
|
|
* @return string The modified CSS. |
90
|
|
|
*/ |
91
|
|
|
public function embed_css( $css = '' ) { |
92
|
|
|
|
93
|
|
|
// Go through our fields and populate $this->fonts. |
94
|
|
|
$this->webfonts->loop_fields( $this->config_id ); |
95
|
|
|
|
96
|
|
|
$this->googlefonts->fonts = apply_filters( 'kirki/enqueue_google_fonts', $this->googlefonts->fonts ); |
97
|
|
|
|
98
|
|
|
// Goes through $this->fonts and adds or removes things as needed. |
99
|
|
|
$this->googlefonts->process_fonts(); |
100
|
|
|
|
101
|
|
|
// Go through $this->fonts and populate $this->link. |
102
|
|
|
$link_obj = new Kirki_Modules_Webfonts_Link( $this->config_id, $this->webfonts, $this->googlefonts ); |
103
|
|
|
$link_obj->create_link(); |
104
|
|
|
$this->link = $link_obj->link; |
105
|
|
|
|
106
|
|
|
// If $this->link is not empty then enqueue it. |
107
|
|
|
if ( '' !== $this->link ) { |
108
|
|
|
return $this->get_url_contents( $this->link ) . "\n" . $css; |
109
|
|
|
} |
110
|
|
|
return $css; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the contents of a remote google-fonts link. |
115
|
|
|
* Responses get cached for 1 day. |
116
|
|
|
* |
117
|
|
|
* @access protected |
118
|
|
|
* @since 3.0.0 |
119
|
|
|
* @param string $url The link we want to get. |
120
|
|
|
* @return string|false Returns false if there's an error. |
121
|
|
|
*/ |
122
|
|
|
protected function get_url_contents( $url = '' ) { |
123
|
|
|
|
124
|
|
|
// If $url is not set, use $this->link. |
125
|
|
|
$url = ( '' === $url ) ? $this->link : $url; |
126
|
|
|
|
127
|
|
|
// Sanitize the URL. |
128
|
|
|
$url = esc_url_raw( $url ); |
129
|
|
|
|
130
|
|
|
// The transient name. |
131
|
|
|
$transient_name = 'kirki_googlefonts_contents_' . md5( $url ); |
132
|
|
|
|
133
|
|
|
// Get the transient value. |
134
|
|
|
$data = get_transient( $transient_name ); |
135
|
|
|
|
136
|
|
|
// Check for transient, if none, grab remote HTML file. |
137
|
|
|
if ( false === $data ) { |
138
|
|
|
|
139
|
|
|
// Get remote HTML file. |
140
|
|
|
$response = wp_remote_get( $url ); |
|
|
|
|
141
|
|
|
|
142
|
|
|
// Check for error. |
143
|
|
|
if ( is_wp_error( $response ) ) { |
144
|
|
|
set_transient( 'kirki_googlefonts_fallback_to_link', 'yes', HOUR_IN_SECONDS ); |
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if ( ! isset( $response['response'] ) || ! is_array( $response['response'] ) || ! isset( $response['response']['code'] ) || 200 !== $response['response']['code'] ) { |
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// Parse remote HTML file. |
153
|
|
|
$data = wp_remote_retrieve_body( $response ); |
154
|
|
|
|
155
|
|
|
// Check for error. |
156
|
|
|
if ( is_wp_error( $data ) ) { |
157
|
|
|
set_transient( 'kirki_googlefonts_fallback_to_link', 'yes', HOUR_IN_SECONDS ); |
158
|
|
|
return false; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// Return false if the data is empty. |
162
|
|
|
if ( ! $data ) { |
163
|
|
|
set_transient( 'kirki_googlefonts_fallback_to_link', 'yes', HOUR_IN_SECONDS ); |
164
|
|
|
return false; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// Store remote HTML file in transient, expire after 24 hours. |
168
|
|
|
set_transient( $transient_name, $data, DAY_IN_SECONDS ); |
169
|
|
|
set_transient( 'kirki_googlefonts_fallback_to_link', 'no', DAY_IN_SECONDS ); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $data; |
173
|
|
|
|
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.