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