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