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 | /** |
||
0 ignored issues
–
show
Coding Style
Documentation
introduced
by
Loading history...
|
|||
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 | $modules_css = $this->modules_css; |
||
196 | $styles = $modules_css::loop_controls( $config_id ); |
||
197 | $styles = apply_filters( "kirki_gutenberg_{$config_id}_dynamic_css", $styles ); |
||
198 | |||
199 | if ( empty( $styles ) ) { |
||
200 | continue; |
||
201 | } |
||
202 | } |
||
203 | |||
204 | return $styles; |
||
205 | } |
||
206 | |||
207 | /** |
||
0 ignored issues
–
show
|
|||
208 | * Helper method to check if feature is disabled. |
||
209 | * |
||
210 | * Feature can be disabled by KIRKI_NO_OUTPUT constant, |
||
211 | * gutenbeg_support argument, and disabled output argument. |
||
212 | * |
||
213 | * @access public |
||
214 | * @since 3.0.35 |
||
215 | * |
||
216 | * @return bool $disabled Is gutenberg integration feature disabled? |
||
217 | */ |
||
218 | private function is_disabled( $args = array() ) { |
||
219 | if ( defined( 'KIRKI_NO_OUTPUT' ) && true === KIRKI_NO_OUTPUT ) { |
||
220 | return true; |
||
221 | } |
||
222 | |||
223 | if ( ! empty( $args ) ) { |
||
224 | if ( isset( $args['disable_output'] ) && true === $args['disable_output'] ) { |
||
225 | return true; |
||
226 | } |
||
227 | |||
228 | if ( ! isset( $args['gutenberg_support'] ) || true !== $args['gutenberg_support'] ) { |
||
229 | return true; |
||
230 | } |
||
231 | } |
||
232 | |||
233 | return false; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Load Fonts in Gutenberg Editor. |
||
238 | * |
||
239 | * @access public |
||
240 | * @since 3.0.35 |
||
241 | */ |
||
242 | public function load_fonts() { |
||
243 | |||
244 | $modules_webfonts = $this->modules_webfonts; |
||
245 | $google_fonts = $this->google_fonts; |
||
246 | foreach ( $this->configs as $config_id => $args ) { |
||
247 | |||
248 | if ( $this->is_disabled( $args ) ) { |
||
249 | continue; |
||
250 | } |
||
251 | |||
252 | $this->modules_webfont_loader->set_load( true ); |
||
253 | $this->modules_webfont_loader->enqueue_scripts(); |
||
254 | |||
255 | $async = new Kirki_Modules_Webfonts_Async( |
||
256 | $config_id, |
||
257 | $modules_webfonts::get_instance(), |
||
258 | $google_fonts::get_instance() |
||
259 | ); |
||
260 | |||
261 | $async->webfont_loader(); |
||
262 | $async->webfont_loader_script(); |
||
263 | |||
264 | $local_fonts = new Kirki_Modules_Webfonts_Local( |
||
265 | $modules_webfonts::get_instance(), |
||
266 | $google_fonts::get_instance() |
||
267 | ); |
||
268 | |||
269 | $local_fonts->add_styles(); |
||
270 | |||
271 | return; |
||
272 | } |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Enqueue fontawesome in Gutenberg Editor. |
||
277 | * |
||
278 | * @access public |
||
279 | * @since 3.0.35 |
||
280 | */ |
||
281 | public function enqueue_fontawesome() { |
||
282 | foreach ( $this->configs as $config_id => $args ) { |
||
283 | |||
284 | if ( $this->is_disabled( $args ) ) { |
||
285 | continue; |
||
286 | } |
||
287 | $modules_css = $this->modules_css; |
||
288 | if ( $modules_css::get_enqueue_fa() && apply_filters( 'kirki_load_fontawesome', true ) ) { |
||
289 | wp_enqueue_script( 'kirki-fontawesome-font', 'https://use.fontawesome.com/30858dc40a.js', array(), '4.0.7', true ); |
||
290 | } |
||
291 | |||
292 | return; |
||
293 | } |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Set class property for $configs. |
||
298 | * |
||
299 | * @access private |
||
300 | * @since 3.0.35 |
||
301 | */ |
||
302 | private function set_configs() { |
||
303 | return $this->configs = Kirki::$config; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Set class property for $enabled. |
||
308 | * |
||
309 | * @access private |
||
310 | * @since 3.0.35 |
||
311 | */ |
||
312 | private function set_enabled() { |
||
313 | $this->enabled = ! $this->is_disabled(); |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * Set class property for $modules_css. |
||
318 | * |
||
319 | * @access private |
||
320 | * @since 3.0.35 |
||
321 | */ |
||
322 | private function set_modules_css() { |
||
323 | $this->modules_css = Kirki_Modules_CSS::get_instance(); |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Set class property for $google_fonts. |
||
328 | * |
||
329 | * @access private |
||
330 | * @since 3.0.35 |
||
331 | */ |
||
332 | private function set_google_fonts() { |
||
333 | $this->google_fonts = Kirki_Fonts_Google::get_instance(); |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Set class property for $modules_webfonts. |
||
338 | * |
||
339 | * @access private |
||
340 | * @since 3.0.35 |
||
341 | */ |
||
342 | private function set_modules_webfonts() { |
||
343 | $this->modules_webfonts = Kirki_Modules_Webfonts::get_instance(); |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Set class property for $modules_webfont_loader. |
||
348 | * |
||
349 | * @access private |
||
350 | * @since 3.0.35 |
||
351 | */ |
||
352 | private function set_modules_webfont_loader() { |
||
353 | $this->modules_webfont_loader = Kirki_Modules_Webfont_Loader::get_instance(); |
||
354 | } |
||
355 | } |
||
356 |