1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Gutenberg integration for Kirki. |
4
|
|
|
* |
5
|
|
|
* This class contains methods for integrating Kirki with |
6
|
|
|
* the new WordPress core editor, Gutenberg. It provides |
7
|
|
|
* fonts and styles to be output by the theme. |
8
|
|
|
* |
9
|
|
|
* @package Kirki |
10
|
|
|
* @category Core |
11
|
|
|
* @author Tim Elsass |
12
|
|
|
* @copyright Copyright (c) 2017, Aristeides Stathopoulos |
13
|
|
|
* @license https://opensource.org/licenses/MIT |
14
|
|
|
* @since 3.0.35 |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Wrapper class for static methods. |
19
|
|
|
* |
20
|
|
|
* @since 3.0.35 |
21
|
|
|
*/ |
22
|
|
|
class Kirki_Modules_Gutenberg { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The object instance. |
26
|
|
|
* |
27
|
|
|
* @static |
28
|
|
|
* @access private |
29
|
|
|
* @since 3.0.35 |
30
|
|
|
* @var object |
31
|
|
|
*/ |
32
|
|
|
private static $instance; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Configuration reference. |
36
|
|
|
* |
37
|
|
|
* @access public |
38
|
|
|
* @since 3.0.35 |
39
|
|
|
* @var object $configs |
40
|
|
|
*/ |
41
|
|
|
private $configs; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Whether feature is enabled. |
45
|
|
|
* |
46
|
|
|
* @access public |
47
|
|
|
* @since 3.0.35 |
48
|
|
|
* @var bool $enabled |
49
|
|
|
*/ |
50
|
|
|
public $enabled; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* CSS Module reference. |
54
|
|
|
* |
55
|
|
|
* @access public |
56
|
|
|
* @since 3.0.35 |
57
|
|
|
* @var object $modules_css |
58
|
|
|
*/ |
59
|
|
|
private $modules_css; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Webfonts Module reference. |
63
|
|
|
* |
64
|
|
|
* @access public |
65
|
|
|
* @since 3.0.35 |
66
|
|
|
* @var object $modules_webfonts |
67
|
|
|
*/ |
68
|
|
|
private $modules_webfonts; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Google Fonts reference. |
72
|
|
|
* |
73
|
|
|
* @access public |
74
|
|
|
* @since 3.0.35 |
75
|
|
|
* @var object $google_fonts |
76
|
|
|
*/ |
77
|
|
|
private $google_fonts; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Webfonts Loader Module reference. |
81
|
|
|
* |
82
|
|
|
* @access public |
83
|
|
|
* @since 3.0.35 |
84
|
|
|
* @var object $modules_webfonts |
85
|
|
|
*/ |
86
|
|
|
private $modules_webfont_loader; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Constructor. |
90
|
|
|
* |
91
|
|
|
* @access protected |
92
|
|
|
* @since 3.0.0 |
93
|
|
|
*/ |
94
|
|
|
protected function __construct() { |
95
|
|
|
add_action( 'admin_init', array( $this, 'init' ) ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Initialize Module. |
100
|
|
|
* |
101
|
|
|
* Sets class properties and add necessary hooks. |
102
|
|
|
* |
103
|
|
|
* @since 3.0.35 |
104
|
|
|
*/ |
105
|
|
|
public function init() { |
106
|
|
|
$this->set_configs(); |
107
|
|
|
$this->set_enabled(); |
108
|
|
|
$this->set_modules_css(); |
109
|
|
|
$this->set_google_fonts(); |
110
|
|
|
$this->set_modules_webfonts(); |
111
|
|
|
$this->set_modules_webfont_loader(); |
112
|
|
|
$this->add_hooks(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Gets an instance of this object. |
117
|
|
|
* Prevents duplicate instances which avoid artefacts and improves performance. |
118
|
|
|
* |
119
|
|
|
* @static |
120
|
|
|
* @access public |
121
|
|
|
* @since 3.0.0 |
122
|
|
|
* @return object |
123
|
|
|
*/ |
124
|
|
|
public static function get_instance() { |
125
|
|
|
if ( ! self::$instance ) { |
126
|
|
|
self::$instance = new self(); |
127
|
|
|
} |
128
|
|
|
return self::$instance; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Add hooks for Gutenberg editor integration. |
133
|
|
|
* |
134
|
|
|
* @access protected |
135
|
|
|
* @since 3.0.35 |
136
|
|
|
*/ |
137
|
|
|
protected function add_hooks() { |
138
|
|
|
if ( ! $this->is_disabled() ) { |
139
|
|
|
add_action( 'after_setup_theme', array( $this, 'add_theme_support' ), 999 ); |
140
|
|
|
add_action( 'enqueue_block_editor_assets', array( $this, 'load_fonts' ) ); |
141
|
|
|
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_fontawesome' ) ); |
142
|
|
|
add_filter( 'block_editor_settings', array( $this, 'enqueue' ) ); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Add theme support for editor styles. |
148
|
|
|
* |
149
|
|
|
* This checks if theme has declared editor-styles support |
150
|
|
|
* already, and if not present, declares it. Hooked late. |
151
|
|
|
* |
152
|
|
|
* @access public |
153
|
|
|
* @since 3.0.35 |
154
|
|
|
*/ |
155
|
|
|
public function add_theme_support() { |
156
|
|
|
if ( true !== get_theme_support( 'editor-styles' ) ) { |
157
|
|
|
add_theme_support( 'editor-styles' ); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
|
|
|
|
162
|
|
|
* Enqueue styles to Gutenberg Editor. |
163
|
|
|
* |
164
|
|
|
* @access public |
165
|
|
|
* @since 3.0.35 |
166
|
|
|
*/ |
167
|
|
|
public function enqueue( $settings ) { |
168
|
|
|
$styles = $this->get_styles(); |
169
|
|
|
|
170
|
|
|
if ( ! empty( $styles ) ) { |
171
|
|
|
$settings['styles'][] = array( 'css' => $styles ); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $settings; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Gets the styles to add to Gutenberg Editor. |
179
|
|
|
* |
180
|
|
|
* @access public |
181
|
|
|
* @since 3.0.35 |
182
|
|
|
* |
183
|
|
|
* @return string $styles String containing inline styles to add to Gutenberg. |
184
|
|
|
*/ |
185
|
|
|
public function get_styles() { |
186
|
|
|
|
187
|
|
|
$styles = null; |
188
|
|
|
|
189
|
|
|
foreach ( $this->configs as $config_id => $args ) { |
190
|
|
|
|
191
|
|
|
if ( true === $this->is_disabled( $args ) ) { |
192
|
|
|
continue; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$styles = $this->modules_css::loop_controls( $config_id ); |
196
|
|
|
$styles = apply_filters( "kirki_gutenberg_{$config_id}_dynamic_css", $styles ); |
197
|
|
|
|
198
|
|
|
if ( empty( $styles ) ) { |
199
|
|
|
continue; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $styles; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
|
|
|
|
207
|
|
|
* Helper method to check if feature is disabled. |
208
|
|
|
* |
209
|
|
|
* Feature can be disabled by KIRKI_NO_OUTPUT constant, |
210
|
|
|
* gutenbeg_support argument, and disabled output argument. |
211
|
|
|
* |
212
|
|
|
* @access public |
213
|
|
|
* @since 3.0.35 |
214
|
|
|
* |
215
|
|
|
* @return bool $disabled Is gutenberg integration feature disabled? |
216
|
|
|
*/ |
217
|
|
|
private function is_disabled( $args = array() ) { |
218
|
|
|
if ( defined( 'KIRKI_NO_OUTPUT' ) && true === KIRKI_NO_OUTPUT ) { |
219
|
|
|
return true; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
if ( ! empty( $args ) ) { |
223
|
|
|
if ( isset( $args['disable_output'] ) && true === $args['disable_output'] ) { |
224
|
|
|
return true; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
if ( ! isset( $args['gutenberg_support'] ) || true !== $args['gutenberg_support'] ) { |
228
|
|
|
return true; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return false; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Load Fonts in Gutenberg Editor. |
237
|
|
|
* |
238
|
|
|
* @access public |
239
|
|
|
* @since 3.0.35 |
240
|
|
|
*/ |
241
|
|
|
public function load_fonts() { |
242
|
|
|
foreach ( $this->configs as $config_id => $args ) { |
243
|
|
|
|
244
|
|
|
if ( $this->is_disabled( $args ) ) { |
245
|
|
|
continue; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
$this->modules_webfont_loader::$load = true; |
249
|
|
|
$this->modules_webfont_loader->enqueue_scripts(); |
250
|
|
|
|
251
|
|
|
$async = new Kirki_Modules_Webfonts_Async( |
252
|
|
|
$config_id, |
253
|
|
|
$this->modules_webfonts::get_instance(), |
254
|
|
|
$this->google_fonts::get_instance() |
255
|
|
|
); |
256
|
|
|
|
257
|
|
|
$async->webfont_loader(); |
258
|
|
|
$async->webfont_loader_script(); |
259
|
|
|
|
260
|
|
|
$local_fonts = new Kirki_Modules_Webfonts_Local( |
261
|
|
|
$this->modules_webfonts::get_instance(), |
262
|
|
|
$this->google_fonts::get_instance() |
263
|
|
|
); |
264
|
|
|
|
265
|
|
|
$local_fonts->add_styles(); |
266
|
|
|
|
267
|
|
|
return; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Enqueue fontawesome in Gutenberg Editor. |
273
|
|
|
* |
274
|
|
|
* @access public |
275
|
|
|
* @since 3.0.35 |
276
|
|
|
*/ |
277
|
|
|
public function enqueue_fontawesome() { |
278
|
|
|
foreach ( $this->configs as $config_id => $args ) { |
279
|
|
|
|
280
|
|
|
if ( $this->is_disabled( $args ) ) { |
281
|
|
|
continue; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
if ( $this->modules_css::get_enqueue_fa() && apply_filters( 'kirki_load_fontawesome', true ) ) { |
285
|
|
|
wp_enqueue_script( 'kirki-fontawesome-font', 'https://use.fontawesome.com/30858dc40a.js', array(), '4.0.7', true ); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
return; |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Set class property for $configs. |
294
|
|
|
* |
295
|
|
|
* @access private |
296
|
|
|
* @since 3.0.35 |
297
|
|
|
*/ |
298
|
|
|
private function set_configs() { |
299
|
|
|
return $this->configs = Kirki::$config; |
|
|
|
|
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Set class property for $enabled. |
304
|
|
|
* |
305
|
|
|
* @access private |
306
|
|
|
* @since 3.0.35 |
307
|
|
|
*/ |
308
|
|
|
private function set_enabled() { |
309
|
|
|
$this->enabled = ! $this->is_disabled(); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Set class property for $modules_css. |
314
|
|
|
* |
315
|
|
|
* @access private |
316
|
|
|
* @since 3.0.35 |
317
|
|
|
*/ |
318
|
|
|
private function set_modules_css() { |
319
|
|
|
$this->modules_css = Kirki_Modules_CSS::get_instance(); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Set class property for $google_fonts. |
324
|
|
|
* |
325
|
|
|
* @access private |
326
|
|
|
* @since 3.0.35 |
327
|
|
|
*/ |
328
|
|
|
private function set_google_fonts() { |
329
|
|
|
$this->google_fonts = Kirki_Fonts_Google::get_instance(); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Set class property for $modules_webfonts. |
334
|
|
|
* |
335
|
|
|
* @access private |
336
|
|
|
* @since 3.0.35 |
337
|
|
|
*/ |
338
|
|
|
private function set_modules_webfonts() { |
339
|
|
|
$this->modules_webfonts = Kirki_Modules_Webfonts::get_instance(); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Set class property for $modules_webfont_loader. |
344
|
|
|
* |
345
|
|
|
* @access private |
346
|
|
|
* @since 3.0.35 |
347
|
|
|
*/ |
348
|
|
|
private function set_modules_webfont_loader() { |
349
|
|
|
$this->modules_webfont_loader = Kirki_Modules_Webfont_Loader::get_instance(); |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
|