PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.1.1
Kubio AI Page Builder v2.1.1
2.9.1 2.9.0 2.8.6 2.8.5 2.8.4 2.8.3 2.8.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 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 2 years ago menu 3 years ago polyfills 3 years ago preview 2 years ago shapes 2 years ago shortcodes 3 years ago src 2 years ago add-edit-in-kubio.php 2 years 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 4 years ago kubio-block-library.php 2 years ago kubio-editor.php 2 years ago load.php 2 years ago
global-data.php
480 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 add_action(
412 'admin_init',
413 function () {
414
415 $styles = kubio_get_global_data_content();
416 global $_wp_registered_theme_features;
417
418 if ( isset( $_wp_registered_theme_features['editor-color-palette'] ) ) {
419 unset( $_wp_registered_theme_features['editor-color-palette'] );
420 }
421
422 $color_palette = Arr::get( $styles, 'colors', array() );
423
424 foreach ( $color_palette as $index => $value ) {
425 if ( ! is_array( $value['color'] ) ) {
426 continue;
427 }
428 $color_palette[ $index ] = array_merge(
429 $color_palette[ $index ],
430 array(
431 'color' => 'rgb(' . implode( ',', $value['color'] ) . ')',
432 )
433 );
434 }
435 add_theme_support( 'editor-color-palette', $color_palette );
436 },
437 20
438 );
439
440 add_action( 'wp_head', 'kubio_register_global_style', 0 );
441
442
443 add_filter(
444 'rest_prepare_' . kubio_global_data_post_type(),
445 function ( $response, $post ) {
446
447 $parsed = json_decode( $post->post_content, true );
448
449 $parsed['menuLocations'] = array();
450 $current_locations = get_nav_menu_locations();
451 foreach ( get_registered_nav_menus() as $name => $description ) {
452 $location = new stdClass();
453 $location->name = $name;
454 $location->description = $description;
455 $location->menu = ( isset( $current_locations[ $name ] ) ) ? $current_locations[ $name ] : 0;
456
457 $parsed['menuLocations'][] = $location;
458 }
459
460 // remove global style visited attribute
461 Arr::forget( $parsed, 'globalStyle.style.descendants.body.typography.holders.a.states.visited' );
462
463 $response->set_data(
464 array_merge(
465 $response->get_data(),
466 array(
467 'content' => array(
468 'raw' => json_encode( $parsed ),
469 ),
470 'parsed' => $parsed,
471 )
472 )
473 );
474
475 return $response;
476 },
477 10,
478 2
479 );
480