PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.3.0
Kubio AI Page Builder v2.3.0
2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / lib / global-data.php
kubio / lib Last commit date
AI 2 years ago admin-pages 2 years ago api 2 years ago blog 3 years ago customizer 2 years ago filters 2 years ago full-site-editing 2 years ago importer 4 years ago integrations 1 year ago menu 3 years ago polyfills 3 years ago preview 2 years ago shapes 2 years ago shortcodes 1 year ago src 1 year ago add-edit-in-kubio.php 1 year ago editor-assets.php 2 years ago env.php 2 years ago filters.php 2 years ago frontend.php 4 years ago global-data.php 2 years ago init.php 2 years ago kubio-block-library.php 2 years ago kubio-editor.php 1 year ago load.php 2 years ago
global-data.php
502 lines
1 <?php
2
3
4 use IlluminateAgnostic\Arr\Support\Arr;
5 use Kubio\Core\StyleManager\GlobalStyleRender;
6 use Kubio\Core\StyleManager\StyleManager;
7 use Kubio\Core\Utils;
8 use Kubio\Flags;
9 use Kubio\GoogleFontsLocalLoader;
10
11 function kubio_global_data_post_type() {
12 return 'kubio-globals';
13 }
14
15 /**
16 * Registers a Custom Post Type to store the user's origin config.
17 */
18 function kubio_register_global_data_post_type() {
19 $args = array(
20 'label' => __( 'Kubio Globals', 'kubio' ),
21 'public' => false,
22 'show_ui' => false,
23 'show_in_rest' => true,
24 'rewrite' => false,
25 'rest_base' => 'kubio/global-data',
26 'capabilities' => array(
27 'read' => 'edit_theme_options',
28 'create_posts' => 'edit_theme_options',
29 'edit_posts' => 'edit_theme_options',
30 'edit_published_posts' => 'edit_theme_options',
31 'delete_published_posts' => 'edit_theme_options',
32 'edit_others_posts' => 'edit_theme_options',
33 'delete_others_posts' => 'edit_theme_options',
34 ),
35 'map_meta_cap' => true,
36 'supports' => array(
37 'title',
38 'editor',
39 'revisions',
40 ),
41 'can_export' => true,
42 );
43 register_post_type( kubio_global_data_post_type(), $args );
44 register_post_meta(
45 kubio_global_data_post_type(),
46 'compiled_css',
47 array(
48 'show_in_rest' => true,
49 'single' => true,
50 'type' => 'string',
51 'auth_callback' => function () {
52 return current_user_can( 'edit_theme_options' );
53 },
54 )
55 );
56 }
57
58
59 add_action( 'init', 'kubio_register_global_data_post_type', 8 );
60
61 function kubio_add_global_data_edit_capability() {
62 $role = get_role( 'editor' );
63 if ( $role && ! $role->has_cap( 'edit_' . kubio_global_data_post_type() ) ) {
64 $role->add_cap( 'read_' . kubio_global_data_post_type() );
65 }
66 }
67
68 add_action( 'admin_init', 'kubio_add_global_data_edit_capability' );
69
70
71 function kubio_global_data_post_id( $create_new = true, $skip_cache = false, $theme = null ) {
72
73 if ( ! $skip_cache && $cached = wp_cache_get( 'id', 'kubio/global_data' ) ) {
74 return $cached;
75 }
76
77 $post_type = kubio_global_data_post_type();
78 $stylesheet = get_stylesheet();
79
80 $query = new WP_Query(
81 array(
82 'post_type' => $post_type,
83 'post_status' => array( 'draft', 'publish' ),
84 'no_found_rows' => true,
85 'post_per_page' => 1,
86 'tax_query' => array(
87 array(
88 'taxonomy' => 'wp_theme',
89 'field' => 'name',
90 'terms' => $theme ? array( $theme ) : array( $stylesheet ),
91 ),
92 ),
93 )
94 );
95
96 // fallback for current instances - get the post without theme term and set it later
97 $set_term = false;
98 if ( ! $query->have_posts() && ! $theme ) {
99 $set_term = true;
100 $query = new WP_Query(
101 array(
102 'post_type' => $post_type,
103 'post_status' => array( 'draft', 'publish' ),
104 'no_found_rows' => true,
105 'post_per_page' => 1,
106 )
107 );
108 }
109
110 if ( $query->have_posts() ) {
111 $post = $query->next_post();
112 $kubio_global_post_content = json_decode( $post->post_content, true );
113 wp_cache_set( 'data', $kubio_global_post_content, 'kubio/global_data' );
114 $id = $post->ID;
115 if ( $set_term ) {
116 wp_set_post_terms( $id, $stylesheet, 'wp_theme' );
117 }
118 } else {
119
120 if ( $create_new ) {
121 $content = file_get_contents( __DIR__ . '/../defaults/global-data.json' );
122 $id = wp_insert_post(
123 array(
124 'post_content' => json_encode( json_decode( $content, true ) ), // remove the pretty prints
125 'post_status' => 'publish',
126 'post_type' => $post_type,
127 'post_name' => $post_type,
128 'post_title' => __( 'Kubio Globals', 'kubio' ),
129 'tax_input' => array(
130 'wp_theme' => array( $stylesheet ),
131 ),
132 ),
133 true
134 );
135 } else {
136 return null;
137 }
138 }
139
140 if ( kubio_is_page_preview() ) {
141 $autosaved_posts = kubio_get_current_changeset_data( 'autosaves', array() );
142
143 foreach ( $autosaved_posts as $autosaved_post ) {
144 $autosaved_parent = intval( Arr::get( $autosaved_post, 'parent', 0 ) );
145 if ( $autosaved_parent === intval( $id ) ) {
146 return $autosaved_post['id'];
147 }
148 }
149 }
150
151 if ( ! $skip_cache ) {
152 wp_cache_set( 'id', $id, 'kubio/global_data' );
153 }
154 return $id;
155 }
156
157 function kubio_get_global_data_content( $redo_cache = false ) {
158
159 $id = kubio_global_data_post_id();
160
161 if ( ! $redo_cache && $cached = wp_cache_get( 'data', "kubio/global_data/{$id}" ) ) {
162 return $cached;
163 }
164
165 $post = get_post( $id );
166 $kubio_global_post_content = json_decode( $post->post_content, true );
167 wp_cache_set( 'data', $kubio_global_post_content, "kubio/global_data/{$id}" );
168
169 return $kubio_global_post_content;
170 }
171
172 function kubio_get_theme_global_data_content( $theme ) {
173 $id = kubio_global_data_post_id( false, true, $theme );
174 $post = get_post( $id );
175
176 if ( is_wp_error( $post ) ) {
177 return null;
178 }
179
180 return json_decode( $post->post_content, true );
181 }
182
183 function kubio_has_global_data( $theme = null ) {
184 $id = kubio_global_data_post_id( false, true, $theme );
185
186 return ! ! $id;
187 }
188
189 function kubio_get_global_data( $path, $fallback = null ) {
190 $data = kubio_get_global_data_content();
191
192 return Arr::get( $data, $path, $fallback );
193 }
194
195 function kubio_replace_global_data_content( $data, $theme = null ) {
196
197 if ( ! is_string( $data ) ) {
198 $data = wp_slash( json_encode( $data ) );
199 }
200
201 $id = kubio_global_data_post_id( true, true, $theme );
202 return wp_update_post(
203 array(
204 'ID' => $id,
205 'post_content' => $data,
206 )
207 );
208 }
209
210 function kubio_set_global_data( $path, $value ) {
211 $data = kubio_get_global_data_content();
212 Arr::set( $data, $path, $value );
213
214 wp_cache_set( 'data', $data, 'kubio/global_data' );
215 kubio_replace_global_data_content( $data );
216 }
217
218 function kubio_get_initial_global_data_content() {
219 $content = file_get_contents( __DIR__ . '/../defaults/global-data.json' );
220 return json_decode( $content, true );
221 }
222
223 function kubio_edit_global_styles_editor_settings( $settings ) {
224
225 $settings['kubioGlobalStyleEntityType'] = kubio_global_data_post_type();
226 $settings['kubioGlobalStyleEntityId'] = kubio_global_data_post_id();
227 $settings['kubioGlobalStyleDefaults'] = kubio_get_global_data_content();
228 $settings['kubioInitialGlobalStyleDefaults'] = kubio_get_initial_global_data_content();
229
230 return $settings;
231 }
232
233 function kubio_on_global_data_post_update( $data ) {
234
235 if ( $data['post_type'] !== kubio_global_data_post_type() ) {
236 return $data;
237 }
238
239 if ( $data['post_status'] !== 'publish' ) {
240 return $data;
241 }
242
243 $content = json_decode( wp_unslash( $data['post_content'] ), true );
244 $locations = Arr::get( $content, 'menuLocations', array() );
245 $current_locations = get_theme_mod( 'nav_menu_locations', array() );
246
247 $should_update_locations = false;
248 foreach ( $locations as $location ) {
249 $location_name = Arr::get( $location, 'name' );
250 $location_menu = Arr::get( $location, 'menu' );
251
252 if ( $location_menu && intval( $current_locations[ $location_name ] ) !== intval( $location_menu ) ) {
253 $should_update_locations = true;
254 $current_locations[ $location_name ] = $location_menu;
255 }
256 }
257
258 if ( $should_update_locations ) {
259 set_theme_mod( 'nav_menu_locations', $current_locations );
260 }
261
262 Arr::forget( $content, 'menuLocations' );
263 $data['post_content'] = wp_slash( json_encode( $content ) );
264
265 $settings = Arr::get( $content, '_settings', array() );
266
267 Flags::setSettings(
268 array_replace_recursive(
269 Flags::getSettings( true ),
270 $settings
271 )
272 );
273
274 Arr::forget( $content, '_settings' );
275
276 return $data;
277 }
278
279 add_filter(
280 'wp_insert_post_data',
281 'kubio_on_global_data_post_update',
282 10,
283 1
284 );
285
286 add_filter( 'block_editor_settings_all', 'kubio_edit_global_styles_editor_settings' );
287
288 function kubio_register_global_style() {
289 $styles = kubio_get_global_data_content();
290 $styles = Arr::get( $styles, 'globalStyle', array() );
291
292 $styleRenderer = new GlobalStyleRender( $styles );
293 $globalStyle = $styleRenderer->export();
294
295 StyleManager::getInstance()->registerBlockStyle( $globalStyle );
296 }
297
298 function kubio_render_global_colors() {
299 $styles = kubio_get_global_data_content();
300 list( $color_palette, ) = (array) get_theme_support( 'editor-color-palette' );
301 $color_palette = Arr::get( $styles, 'colors', $color_palette );
302
303 $vars = array();
304 $color_palette = is_array( $color_palette ) ? $color_palette : array();
305 foreach ( $color_palette as $index => $value ) {
306 $vars[] = '--' . $value['slug'] . ':' . implode( ',', $value['color'] );
307 }
308
309 $color_palette_variants = Arr::get( $styles, 'colorVariants', $color_palette );
310 foreach ( $color_palette_variants as $index => $value ) {
311 $vars[] = '--' . $value['slug'] . ':' . implode( ',', $value['color'] );
312 }
313
314 $new_line = Utils::isDebug() ? "\n" : '';
315 $css = array( ':root {' );
316 $css[] = implode( ";{$new_line}", $vars );
317 $css[] = '}';
318
319 $prefixes = array( '.has-', '[data-kubio] .has-' );
320 $suffixes = array(
321 '-color' => 'color',
322 '-background-color' => 'background-color',
323 );
324
325 foreach ( $color_palette as $value ) {
326 foreach ( $prefixes as $prefix ) {
327 foreach ( $suffixes as $suffix => $property ) {
328 $css[] = "{$prefix}{$value['slug']}{$suffix}{{$property}:rgb(var(--{$value['slug']}))}";
329 }
330 }
331
332 if ( Utils::isDebug() ) {
333 $css[] = "\n";
334 }
335 }
336
337 return implode( Utils::isDebug() ? $new_line : ' ', $css );
338 }
339
340
341 function kubio_enqueue_google_fonts() {
342
343 $fonts_query = GoogleFontsLocalLoader::getInstance()->getFontsQuery();
344
345 if ( ! $fonts_query ) {
346 return;
347 }
348
349 $query_args = array(
350 'family' => urlencode( $fonts_query ),
351 'display' => 'swap',
352 );
353
354 // in preview load remote google fonts to save disk space ( as the content is not yet saved )
355 // also load remote fonts if the user chosed not to use local google fonts
356 if ( kubio_is_page_preview() || ! Flags::getSetting( 'googleFonts.serveLocally', false ) ) {
357 $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
358 wp_enqueue_style( 'kubio-google-fonts', $fonts_url, array(), null );
359 } else {
360 GoogleFontsLocalLoader::enqueuLocalGoogleFonts( $fonts_query );
361 }
362
363 }
364
365 add_action( 'wp_enqueue_scripts', 'kubio_enqueue_google_fonts' );
366
367 function kubio_enqueue_typekit_fonts() {
368 $globalData = kubio_get_global_data_content();
369 $typeKitProject = Arr::get( $globalData, 'fonts.typekit.project', '' );
370
371 if ( ! empty( $typeKitProject ) ) {
372 ?>
373 <script>
374 (function (d) {
375 var config = {
376 kitId: '<?php echo esc_js( $typeKitProject ); ?>',
377 scriptTimeout: 3000,
378 async: true
379 },
380 h = d.documentElement,
381 t = setTimeout(function () {
382 h.className = h.className.replace(/\bwf-loading\b/g, "") + " wf-inactive";
383 }, config.scriptTimeout),
384 tk = d.createElement("script"),
385 f = false,
386 s = d.getElementsByTagName("script")[0],
387 a;
388 h.className += " wf-loading";
389 tk.src = 'https://use.typekit.net/' + config.kitId + '.js';
390 tk.async = true;
391 tk.onload = tk.onreadystatechange = function () {
392 a = this.readyState;
393 if (f || a && a != "complete" && a != "loaded") return;
394 f = true;
395 clearTimeout(t);
396 try {
397 Typekit.load(config)
398 } catch (e) {
399 }
400 };
401 s.parentNode.insertBefore(tk, s)
402 })(document);
403 </script>
404 <?php
405 }
406 }
407
408 add_action( 'wp_head', 'kubio_enqueue_typekit_fonts' );
409
410
411 function kubio_get_editor_colors( $as_var = false ) {
412 $styles = kubio_get_global_data_content();
413 $color_palette = Arr::get( $styles, 'colors', array() );
414
415 $colors = array();
416
417 foreach ( $color_palette as $index => $value ) {
418 if ( ! is_array( $value['color'] ) ) {
419 continue;
420 }
421 $colors[] = array(
422 // translators: %s is the number of the color e.g. Kubio color 1
423 'name' => sprintf( __( 'Kubio color %s', 'kubio' ), $index + 1 ),
424 'slug' => $value['slug'],
425 'color' => $as_var ? sprintf( 'rgba(var(--%s), 1)', $value['slug'] ) : 'rgb(' . implode( ',', $value['color'] ) . ')',
426 );
427 }
428
429 return $colors;
430 }
431
432 add_action( 'wp_head', 'kubio_register_global_style', 0 );
433
434 add_filter(
435 'wp_theme_json_data_default',
436 function( $wp_theme_json_data ) {
437 $config = $wp_theme_json_data->get_data();
438
439 $colors = kubio_get_editor_colors();
440 $current_colors = Arr::get( $config, 'settings.color.palette.default', array() );
441 Arr::set( $config, 'settings.color.palette.default', array_merge( $current_colors, $colors ) );
442
443 return new WP_Theme_JSON_Data( $config, 'default' );
444 }
445 );
446
447 add_action(
448 'after_setup_theme',
449 function() {
450
451 $current_supported_colors = get_theme_support( 'editor-color-palette' );
452 $colors = kubio_get_editor_colors( true );
453
454 if ( ! $current_supported_colors ) {
455 add_theme_support( 'editor-color-palette', $colors );
456 } else {
457 $colors = array_merge( $current_supported_colors[0], $colors );
458 add_theme_support( 'editor-color-palette', $colors );
459 }
460 },
461 100
462 );
463
464
465 add_filter(
466 'rest_prepare_' . kubio_global_data_post_type(),
467 function ( $response, $post ) {
468
469 $parsed = json_decode( $post->post_content, true );
470
471 $parsed['menuLocations'] = array();
472 $current_locations = get_nav_menu_locations();
473 foreach ( get_registered_nav_menus() as $name => $description ) {
474 $location = new stdClass();
475 $location->name = $name;
476 $location->description = $description;
477 $location->menu = ( isset( $current_locations[ $name ] ) ) ? $current_locations[ $name ] : 0;
478
479 $parsed['menuLocations'][] = $location;
480 }
481
482 // remove global style visited attribute
483 Arr::forget( $parsed, 'globalStyle.style.descendants.body.typography.holders.a.states.visited' );
484
485 $response->set_data(
486 array_merge(
487 $response->get_data(),
488 array(
489 'content' => array(
490 'raw' => json_encode( $parsed ),
491 ),
492 'parsed' => $parsed,
493 )
494 )
495 );
496
497 return $response;
498 },
499 10,
500 2
501 );
502