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