PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.2.0
Kubio AI Page Builder v2.2.0
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 / editor-assets.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 2 years ago kubio-block-library.php 2 years ago kubio-editor.php 2 years ago load.php 2 years ago
editor-assets.php
551 lines
1 <?php
2
3 use IlluminateAgnostic\Arr\Support\Arr;
4 use Kubio\AssetsDependencyInjector;
5 use Kubio\Core\Utils;
6 use Kubio\DemoSites\DemoSitesRepository;
7 use Kubio\Flags;
8
9 function kubio_override_script( $scripts, $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
10 $script = $scripts->query( $handle, 'registered' );
11 if ( $script ) {
12
13 $script->src = $src;
14 $script->deps = $deps;
15 $script->ver = $ver;
16 $script->args = $in_footer;
17
18 unset( $script->extra['group'] );
19 if ( $in_footer ) {
20 $script->add_data( 'group', 1 );
21 }
22 } else {
23 $scripts->add( $handle, $src, $deps, $ver, $in_footer );
24 }
25
26 if ( in_array( 'wp-i18n', $deps, true ) ) {
27 $scripts->set_translations( $handle, 'kubio' );
28 }
29 }
30
31 function kubio_override_style( $styles, $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
32 $style = $styles->query( $handle, 'registered' );
33 if ( $style ) {
34 $styles->remove( $handle );
35 }
36 $styles->add( $handle, $src, $deps, $ver, $media );
37 }
38
39 function kubio_register_kubio_scripts_scripts_dependencies( $version ) {
40 $scripts = array(
41 array(
42 'handle' => 'typed',
43 'deps' => array( 'jquery' ),
44 'src' => 'typed.js',
45 ),
46 array(
47 'handle' => 'fancybox',
48 'deps' => array( 'jquery' ),
49 'src' => 'fancybox/jquery.fancybox.min.js',
50 ),
51 array(
52 'handle' => 'swiper',
53 'deps' => array( 'jquery' ),
54 'src' => 'swiper/js/swiper.js',
55 ),
56 );
57
58 foreach ( $scripts as $script ) {
59 AssetsDependencyInjector::registerKubioScriptsDependency(
60 $script['handle'],
61 kubio_url( "/static/{$script['src']}" ),
62 $script['deps'],
63 $version
64 );
65 }
66
67 }
68
69 function kubio_register_frontend_script( $handle ) {
70 add_filter(
71 'kubio/frontend/scripts',
72 function( $scripts ) use ( $handle ) {
73
74 if ( ! in_array( $handle, $scripts ) ) {
75 $scripts[] = $handle;
76 }
77
78 return $scripts;
79 }
80 );
81 }
82
83 function kubio_get_frontend_scripts() {
84 return apply_filters( 'kubio/frontend/scripts', array() );
85 }
86
87 function kubio_enqueue_frontend_scripts() {
88 $scripts = apply_filters( 'kubio/frontend/scripts', array() );
89 foreach ( $scripts as $handle ) {
90 wp_enqueue_script( $handle );
91 }
92 }
93
94 function kubio_register_packages_scripts() {
95
96 $registered = array();
97
98 $paths = glob( KUBIO_ROOT_DIR . 'build/*/index.js' );
99 foreach ( $paths as $path ) {
100 $handle = 'kubio-' . basename( dirname( $path ) );
101 $asset_file = substr( $path, 0, - 3 ) . '.asset.php';
102 $asset = file_exists( $asset_file )
103 ? require( $asset_file )
104 : null;
105 $dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : array();
106
107 if ( Utils::isDebug() ) {
108 $version = uniqid( time() . '-' );
109 } else {
110 $version = isset( $asset['version'] ) ? $asset['version'] : filemtime( $path );
111 }
112
113 switch ( $handle ) {
114 case 'kubio-editor':
115 array_push( $dependencies, 'wp-dom-ready', 'editor', 'wp-editor' );
116
117 if ( kubio_is_kubio_editor_page() ) {
118 array_push( $dependencies, 'kubio-interface-store' );
119 }
120
121 break;
122
123 case 'kubio-format-library':
124 array_push( $dependencies, 'wp-format-library' );
125 break;
126
127 case 'kubio-scripts':
128 kubio_register_kubio_scripts_scripts_dependencies( $version );
129 $dependencies = array_merge( $dependencies, array( 'jquery' ) );
130 $dependencies = array_diff( $dependencies, array( 'wp-polyfill' ) );
131 break;
132
133 case 'kubio-frontend':
134 $dependencies = array( 'kubio-scripts' );
135 kubio_register_frontend_script( 'kubio-frontend' );
136 break;
137
138 case 'kubio-block-library':
139 array_push( $dependencies, 'kubio-format-library' );
140 break;
141
142 case 'kubio-block-editor':
143 if ( wp_script_is( 'wp-private-apis', 'registered' ) ) {
144 $dependencies[] = 'wp-private-apis';
145 }
146
147 if ( wp_script_is( 'wp-experiments', 'registered' ) ) {
148 $dependencies[] = 'wp-experiments';
149 }
150
151 //For backward compatability to 6.1
152 if ( Utils::wpVersionCompare( '6.4', '<' ) ) {
153 if ( ( $key = array_search( 'wp-commands', $dependencies ) ) !== false ) {
154 unset( $dependencies[ $key ] );
155 }
156 }
157 array_push( $dependencies, 'wp-block-directory' );
158 break;
159
160 }
161
162 $kubio_path = substr( $path, strlen( KUBIO_ROOT_DIR ) );
163
164 $registered[] = array(
165 $handle,
166 kubio_url( $kubio_path ),
167 $dependencies,
168 $version,
169 true,
170 );
171 }
172
173 foreach ( $registered as $script ) {
174
175 if ( is_array( $script ) && count( $script ) >= 2 ) {
176 $handle = $script[0];
177 $deps = $script[2];
178 if ( in_array( 'wp-i18n', $deps, true ) ) {
179 wp_set_script_translations( $handle, 'kubio' );
180 }
181
182 call_user_func_array( 'wp_register_script', $script );
183 do_action( 'kubio_registered_script', $script[0], $script[3] );
184 }
185 }
186
187 do_action( 'kubio_scripts_registered', $registered );
188 }
189
190
191 function kubio_replace_default_scripts( $scripts ) {
192
193 if ( ! kubio_is_kubio_editor_page() ) {
194 return;
195 }
196
197 $to_replace = array(
198 'wp-block-editor' => 'block-editor',
199 );
200
201 foreach ( $to_replace as $old => $new ) {
202 $script_path = KUBIO_ROOT_DIR . "/build/{$new}/index.js";
203 $asset_file = KUBIO_ROOT_DIR . "/build/{$new}/index.asset.php";
204
205 $asset = file_exists( $asset_file )
206 ? require( $asset_file )
207 : null;
208 $dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : array();
209 $version = isset( $asset['version'] ) ? $asset['version'] : filemtime( $script_path );
210
211 //For backward compatability to 6.1
212 if ( Utils::wpVersionCompare( '6.4', '<' ) ) {
213 if ( ( $key = array_search( 'wp-commands', $dependencies ) ) !== false ) {
214 unset( $dependencies[ $key ] );
215 }
216 }
217 kubio_override_script(
218 $scripts,
219 $old,
220 kubio_url( "/build/{$new}/index.js" ),
221 $dependencies,
222 $version,
223 true
224 );
225 }
226
227 }
228
229
230 function kubio_register_kubio_block_library_style_dependencies( $version ) {
231 $styles = array(
232 array(
233 'handle' => 'fancybox',
234 'src' => 'fancybox/jquery.fancybox.min.css',
235 ),
236 array(
237 'handle' => 'swiper',
238 'src' => 'swiper/css/swiper.min.css',
239 ),
240 );
241
242 foreach ( $styles as $style ) {
243 AssetsDependencyInjector::registerKubioFrontendStyleDependency(
244 $style['handle'],
245 kubio_url( "/static/{$style['src']}" ),
246 isset( $style['deps'] ) ? $style['deps'] : array(),
247 $version
248 );
249 }
250
251 }
252
253
254 function kubio_register_packages_styles() {
255
256 $registered = array();
257
258 foreach ( glob( KUBIO_ROOT_DIR . 'build/*/style.css' ) as $path ) {
259 $handle = 'kubio-' . basename( dirname( $path ) );
260 $kubio_path = substr( $path, strlen( KUBIO_ROOT_DIR ) );
261 $version = filemtime( $path );
262 $dependencies = array();
263
264 switch ( $handle ) {
265 case 'kubio-editor':
266 $dependencies = array( 'wp-edit-blocks' );
267 break;
268
269 case 'kubio-format-library':
270 array_push( $dependencies, 'wp-format-library' );
271 break;
272
273 case 'kubio-admin-panel':
274 array_push( $dependencies, 'kubio-utils' );
275 break;
276
277 case 'kubio-ai':
278 array_push( $dependencies, 'wp-components' );
279 break;
280
281 case 'kubio-block-library':
282 kubio_register_kubio_block_library_style_dependencies( $version );
283 break;
284 }
285
286 $registered[] = array(
287 $handle,
288 kubio_url( $kubio_path ),
289 $dependencies,
290 $version,
291 );
292 }
293
294 foreach ( glob( KUBIO_ROOT_DIR . 'build/*/editor.css' ) as $path ) {
295 $handle = 'kubio-' . basename( dirname( $path ) );
296 $kubio_path = substr( $path, strlen( KUBIO_ROOT_DIR ) );
297 $version = filemtime( $path );
298 $dependencies = array();
299
300 switch ( $handle ) {
301 case 'kubio-editor':
302 $dependencies = array( 'wp-edit-blocks' );
303 break;
304
305 case 'kubio-block-library':
306 $dependencies = array( /* 'wp-block-library' */ );
307 break;
308 }
309
310 $registered[] = array(
311 "{$handle}-editor",
312 kubio_url( $kubio_path ),
313 $dependencies,
314 $version,
315 );
316 }
317
318 foreach ( $registered as $style ) {
319
320 if ( is_array( $style ) && count( $style ) >= 2 ) {
321
322 call_user_func_array( 'wp_register_style', $style );
323
324 }
325 }
326 }
327
328
329 function kubio_replace_default_styles( $styles ) {
330
331 if ( ! kubio_is_kubio_editor_page() ) {
332 return;
333 }
334
335 // Editor Styles .
336 kubio_override_style(
337 $styles,
338 'wp-block-editor',
339 kubio_url( 'build/block-editor/style.css' ),
340 array( 'wp-components', 'wp-editor-font' ),
341 filemtime( KUBIO_ROOT_DIR . 'build/editor/style.css' )
342 );
343 $styles->add_data( 'wp-block-editor', 'rtl', 'replace' );
344
345 }
346
347 add_action( 'init', 'kubio_register_packages_scripts' );
348 add_action( 'init', 'kubio_register_packages_styles' );
349
350 add_action( 'wp_default_styles', 'kubio_replace_default_styles' );
351 add_action( 'wp_default_scripts', 'kubio_replace_default_scripts' );
352
353
354 add_action(
355 'kubio_registered_script',
356 function ( $handle, $version ) {
357 global $wp_version;
358 if ( $handle === 'kubio-utils' || $handle === 'kubio-admin-panel' ) {
359 $include_test_templates = defined( 'KUBIO_INCLUDE_TEST_TEMPLATES' ) && KUBIO_INCLUDE_TEST_TEMPLATES === true;
360 $data = 'window.kubioUtilsData=' . wp_json_encode(
361 array_merge(
362 kubio_get_site_urls(),
363 array(
364 'defaultAssetsURL' => kubio_url( 'static/default-assets' ),
365 'staticAssetsURL' => kubio_url( 'static' ),
366 'patternsAssetsUrl' => kubio_url( 'static/patterns' ),
367 'kubioRemoteContentFile' => 'https://static-assets.kubiobuilder.com/content-2022-05-17.json',
368 'kubioCloudPresetsUrl' => Utils::getGlobalSnippetsURL(),
369 'kubioCloudPresetCategoriesUrl' => Utils::getGlobalSnippetsCategoriesURL(),
370 'kubioCloudPresetTagsUrl' => Utils::getGlobalSnippetsTagsURL(),
371 'kubioCloudUrl' => Utils::getCloudURL(),
372 'kubioRemoteContent' => Utils::getSnippetsURL( '/globals' ),
373 'kubioLocalContentFile' => kubio_url( 'static/patterns/content-converted.json' ),
374 'kubioEditorURL' => add_query_arg( 'page', 'kubio', admin_url( 'admin.php' ) ),
375 'patternsOnTheFly' => ( defined( 'KUBIO_PATTERNS_ON_THE_FLY' ) && KUBIO_PATTERNS_ON_THE_FLY ) ? KUBIO_PATTERNS_ON_THE_FLY : '',
376 'base_url' => site_url(),
377 'admin_url' => admin_url(),
378 'admin_plugins_url' => admin_url( 'plugins.php' ),
379 'demo_sites_url' => Utils::getStarterSitesURL(),
380 'demo_parts_url' => Utils::getStarterPartsURL(),
381 'plugins_states' => DemoSitesRepository::getInstance()->getPluginsStates(),
382 'last_imported_starter' => Flags::get( 'last_imported_starter' ),
383 'demo_site_ajax_nonce' => wp_create_nonce( 'kubio-ajax-demo-site-verification' ),
384 'ajax_url' => admin_url( 'admin-ajax.php' ),
385 'kubio_ajax_nonce' => wp_create_nonce( 'kubio_ajax_nonce' ),
386 'enable_starter_sites' => apply_filters( 'kubio/starter-sites/enabled', true ),
387 'wpVersion' => preg_replace( '/([0-9]+).([0-9]+).*/', '$1.$2', $wp_version ),
388 'enable_try_online' => Utils::isTryOnlineEnabled(),
389 'supplementary_upgrade_to_pro' => apply_filters( 'kubio/show-supplementary-upgrade-to-pro', false ),
390 'kubioAIPricingURL' => Utils::getCloudURL( '/ui-route/my-plans?purchase_ai=1' ),
391 'kubioAIParallelCalls' => apply_filters( 'kubio/ai/parallel-calls', 5 ),
392 'showInternalFeatures' => defined( '\KUBIO_INTERNAL' ) && \KUBIO_INTERNAL,
393 'sectionStylesTags' => array( 'shadow', 'flat', 'outlined', 'rounded', 'minimal' ),
394 'activatedOnStage2' => Flags::getSetting( 'activatedOnStage2', false ),
395 'aiStage2' => Flags::getSetting( 'aiStage2', false ),
396 'wpAdminUpgradePage' => add_query_arg(
397 array(
398 'tab' => 'pro-upgrade',
399 'page' => 'kubio-get-started',
400 ),
401 admin_url( 'admin.php' )
402 ),
403 'allow3rdPartyBlogOverride' => apply_filters( 'kubio/allow_3rd_party_blog_override', true ),
404 ),
405 apply_filters( 'kubio/kubio-utils-data/extras', array() )
406 )
407 );
408
409 wp_add_inline_script( $handle, $data, 'before' );
410 }
411
412 if ( $handle === 'kubio-style-manager' ) {
413
414 $url = add_query_arg(
415 array(
416 'action' => 'kubio_style_manager_web_worker',
417 '_wpnonce' => wp_create_nonce( 'kubio_style_manager_web_worker_nonce' ),
418 'v' => filemtime( KUBIO_ROOT_DIR . '/defaults/style-manager-web-worker-template.js' ) . '-' . ( Utils::isDebug() ? time() : KUBIO_VERSION . '-' . $wp_version ),
419 ),
420 admin_url( 'admin-ajax.php' )
421 );
422
423 wp_add_inline_script(
424 $handle,
425 'var _kubioStyleManagerWorkerURL=' . wp_json_encode( $url ),
426 'before'
427 );
428 }
429 },
430 10,
431 2
432 );
433
434 function kubio_print_style_manager_web_worker() {
435 check_ajax_referer('kubio_style_manager_web_worker_nonce');
436 header( 'content-type: application/javascript' );
437
438 $script = '';
439 $done = wp_scripts()->done;
440 ob_start();
441 wp_scripts()->done = array( 'wp-inert-polyfill', 'wp-polyfill' );
442 wp_scripts()->do_items( 'kubio-style-manager' );
443 wp_scripts()->done = $done;
444 $script = ob_get_clean();
445
446 $script = preg_replace_callback(
447 '#<script(.*?)>(.*?)</script>#s',
448 function( $matches ) {
449 $script_attrs = Arr::get( $matches, 1, '' );
450 preg_match( "#src=(\"|')(.*?)(\"|')#", $script_attrs, $attrs_match );
451 $url = Arr::get( $attrs_match, 2, '' );
452 $content = trim( Arr::get( $matches, 2, '' ) );
453
454 $result = array();
455
456 if ( ! empty( $url ) ) {
457 $result[] = sprintf( "importScripts('%s');", $url );
458 }
459
460 if ( ! empty( $content ) ) {
461 $result[] = $content;
462 }
463
464 return trim( implode( "\n", $result ) ) . "\n\n";
465 },
466 $script
467 );
468
469 $content = file_get_contents( KUBIO_ROOT_DIR . '/defaults/style-manager-web-worker-template.js' );
470 $content = str_replace( '// {{{importScriptsPlaceholder}}}', $script, $content );
471
472 if ( ! Utils::isDebug() ) {
473 header( 'Cache-control: public' );
474 header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', time() ) . ' GMT' );
475 header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + YEAR_IN_SECONDS ) . ' GMT' );
476 header( 'Etag: ' . md5( $content ) );
477 }
478
479 die( $content );
480 }
481
482 add_action( 'wp_ajax_kubio_style_manager_web_worker', 'kubio_print_style_manager_web_worker' );
483
484 // quick test for safari
485 add_action(
486 'admin_init',
487 function () {
488 ob_start();
489 ?>
490 <script>
491 window.requestIdleCallback =
492 window.requestIdleCallback ||
493 function (cb) {
494 var start = Date.now();
495 return setTimeout(function () {
496 cb({
497 didTimeout: false,
498 timeRemaining: function () {
499 return Math.max(0, 50 - (Date.now() - start));
500 },
501 });
502 }, 1);
503 };
504
505 window.cancelIdleCallback =
506 window.cancelIdleCallback ||
507 function (id) {
508 clearTimeout(id);
509 };
510 </script>
511 <?php
512
513 $content = strip_tags( ob_get_clean() );
514
515 wp_add_inline_script( 'wp-polyfill', $content, 'after' );
516 }
517 );
518
519 function kubio_defer_kubio_scripts( $tag, $handle, $src ) {
520
521 if ( is_admin() ) {
522 return $tag;
523 }
524
525 if ( strpos( $src, kubio_url() ) === 0 ) {
526 $tag = str_replace( 'src=', 'defer src=', $tag );
527 }
528
529 return $tag;
530 }
531
532 add_filter( 'script_loader_tag', 'kubio_defer_kubio_scripts', 10, 3 );
533
534 function kubio_defer_kubio_styles( $tag, $handle, $href, $media ) {
535
536 if ( is_admin() ) {
537 return $tag;
538 }
539
540 $defferable_handles = array( 'kubio-google-fonts', 'kubio-third-party-blocks' );
541
542 if ( in_array( $handle, $defferable_handles ) ) {
543 $tag = preg_replace( "#rel='(.*?)'#", 'rel="preload" as="style" onload="this.onload=null;this.rel=\'$1\'"', $tag );
544 $tag .= "<noscript><link rel='stylesheet' href='{$href}' media='{$media}'></noscript>";
545 }
546
547 return $tag;
548 }
549
550 add_filter( 'style_loader_tag', 'kubio_defer_kubio_styles', 10, 4 );
551