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 / editor-assets.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
editor-assets.php
481 lines
1 <?php
2
3 use IlluminateAgnostic\Arr\Support\Arr;
4 use Kubio\Core\Utils;
5 use Kubio\DemoSites\DemoPartsRepository;
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 $script_handles = array();
59
60 foreach ( $scripts as $script ) {
61 $handle = "kubio-scripts-dep-{$script['handle']}";
62 $script_handles[ $handle ] = array(
63 $handle,
64 kubio_url( "/static/{$script['src']}" ),
65 $script['deps'],
66 $version,
67 true,
68 );
69 }
70
71 return $script_handles;
72 }
73
74 function kubio_register_frontend_script( $handle ) {
75 add_filter(
76 'kubio/frontend/scripts',
77 function( $scripts ) use ( $handle ) {
78
79 if ( ! in_array( $handle, $scripts ) ) {
80 $scripts[] = $handle;
81 }
82
83 return $scripts;
84 }
85 );
86 }
87
88 function kubio_get_frontend_scripts() {
89 return apply_filters( 'kubio/frontend/scripts', array() );
90 }
91
92 function kubio_enqueue_frontend_scripts() {
93 $scripts = apply_filters( 'kubio/frontend/scripts', array() );
94 foreach ( $scripts as $handle ) {
95 wp_enqueue_script( $handle );
96 }
97 }
98
99 function kubio_register_packages_scripts() {
100
101 $registered = array();
102
103 $paths = glob( KUBIO_ROOT_DIR . 'build/*/index.js' );
104 foreach ( $paths as $path ) {
105 $handle = 'kubio-' . basename( dirname( $path ) );
106 $asset_file = substr( $path, 0, - 3 ) . '.asset.php';
107 $asset = file_exists( $asset_file )
108 ? require( $asset_file )
109 : null;
110 $dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : array();
111
112 if ( Utils::isDebug() ) {
113 $version = uniqid( time() . '-' );
114 } else {
115 $version = isset( $asset['version'] ) ? $asset['version'] : filemtime( $path );
116 }
117
118 switch ( $handle ) {
119 case 'kubio-editor':
120 array_push( $dependencies, 'wp-dom-ready', 'editor' );
121 break;
122
123 case 'kubio-format-library':
124 array_push( $dependencies, 'wp-format-library' );
125 break;
126
127 case 'kubio-scripts':
128 $extra_scripts = kubio_register_kubio_scripts_scripts_dependencies( $version );
129 $registered = array_merge( $registered, $extra_scripts );
130 $extra_deps = array_keys( $extra_scripts );
131 $dependencies = array_merge( $dependencies, $extra_deps, array( 'jquery', 'jquery-masonry' ) );
132 $dependencies = array_diff( $dependencies, array( 'wp-polyfill' ) );
133 break;
134
135 case 'kubio-frontend':
136 $dependencies = array( 'kubio-scripts' );
137 kubio_register_frontend_script( 'kubio-frontend' );
138 break;
139
140 case 'kubio-block-library':
141 array_push( $dependencies, 'kubio-format-library' );
142 break;
143
144 case 'kubio-block-editor':
145 array_push( $dependencies, 'wp-block-editor', 'wp-block-directory' );
146 break;
147
148 }
149
150 $kubio_path = substr( $path, strlen( KUBIO_ROOT_DIR ) );
151
152 $registered[] = array(
153 $handle,
154 kubio_url( $kubio_path ),
155 $dependencies,
156 $version,
157 true,
158 );
159 }
160
161 foreach ( $registered as $script ) {
162
163 if ( is_array( $script ) && count( $script ) >= 2 ) {
164 $handle = $script[0];
165 $deps = $script[2];
166 if ( in_array( 'wp-i18n', $deps, true ) ) {
167 wp_set_script_translations( $handle, 'kubio' );
168 }
169
170 call_user_func_array( 'wp_register_script', $script );
171 do_action( 'kubio_registered_script', $script[0], $script[3] );
172 }
173 }
174
175 do_action( 'kubio_scripts_registered', $registered );
176 }
177
178
179 function kubio_replace_default_scripts( $scripts ) {
180
181 if ( ! kubio_is_kubio_editor_page() ) {
182 return;
183 }
184
185 $to_replace = array(
186 'wp-block-editor' => 'block-editor',
187 );
188
189 foreach ( $to_replace as $old => $new ) {
190 $script_path = KUBIO_ROOT_DIR . "/build/{$new}/index.js";
191 $asset_file = KUBIO_ROOT_DIR . "/build/{$new}/index.asset.php";
192
193 $asset = file_exists( $asset_file )
194 ? require( $asset_file )
195 : null;
196 $dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : array();
197 $version = isset( $asset['version'] ) ? $asset['version'] : filemtime( $script_path );
198
199 kubio_override_script(
200 $scripts,
201 $old,
202 kubio_url( "/build/{$new}/index.js" ),
203 $dependencies,
204 $version,
205 true
206 );
207 }
208
209 }
210
211
212 function kubio_register_kubio_block_library__style_dependencies( $version ) {
213 $styles = array(
214 array(
215 'handle' => 'fancybox',
216 'src' => 'fancybox/jquery.fancybox.min.css',
217 ),
218 array(
219 'handle' => 'swiper',
220 'src' => 'swiper/css/swiper.min.css',
221 ),
222 );
223
224 $handles = array();
225
226 foreach ( $styles as $style ) {
227 $handle = "kubio-block-library-dep-{$style['handle']}";
228 $handles[ $handle ] = array(
229 $handle,
230 kubio_url( "/static/{$style['src']}" ),
231 isset( $style['deps'] ) ? $style['deps'] : array(),
232 $version,
233 );
234 }
235
236 return $handles;
237 }
238
239
240 function kubio_register_packages_styles() {
241
242 $registered = array();
243
244 foreach ( glob( KUBIO_ROOT_DIR . 'build/*/style.css' ) as $path ) {
245 $handle = 'kubio-' . basename( dirname( $path ) );
246 $kubio_path = substr( $path, strlen( KUBIO_ROOT_DIR ) );
247 $version = filemtime( $path );
248 $dependencies = array();
249
250 switch ( $handle ) {
251 case 'kubio-editor':
252 $dependencies = array( 'wp-edit-blocks' );
253 break;
254
255 case 'kubio-format-library':
256 array_push( $dependencies, 'wp-format-library' );
257 break;
258
259 case 'kubio-admin-panel':
260 array_push( $dependencies, 'kubio-utils' );
261 break;
262
263 case 'kubio-block-library':
264 $extra_styles = kubio_register_kubio_block_library__style_dependencies( $version );
265 $registered = array_merge( $registered, $extra_styles );
266 $extra_deps = array_keys( $extra_styles );
267 $dependencies = array_merge( $dependencies, $extra_deps, array( 'wp-block-library' ) );
268 break;
269 }
270
271 $registered[] = array(
272 $handle,
273 kubio_url( $kubio_path ),
274 $dependencies,
275 $version,
276 );
277 }
278
279 foreach ( glob( KUBIO_ROOT_DIR . 'build/*/editor.css' ) as $path ) {
280 $handle = 'kubio-' . basename( dirname( $path ) );
281 $kubio_path = substr( $path, strlen( KUBIO_ROOT_DIR ) );
282 $version = filemtime( $path );
283 $dependencies = array();
284
285 switch ( $handle ) {
286 case 'kubio-editor':
287 $dependencies = array( 'wp-edit-blocks' );
288 break;
289
290 case 'kubio-block-library':
291 $dependencies = array( /* 'wp-block-library' */ );
292 break;
293 }
294
295 $registered[] = array(
296 "{$handle}-editor",
297 kubio_url( $kubio_path ),
298 $dependencies,
299 $version,
300 );
301 }
302
303 foreach ( $registered as $style ) {
304
305 if ( is_array( $style ) && count( $style ) >= 2 ) {
306
307 call_user_func_array( 'wp_register_style', $style );
308
309 }
310 }
311 }
312
313
314 function kubio_replace_default_styles( $styles ) {
315
316 if ( ! kubio_is_kubio_editor_page() ) {
317 return;
318 }
319
320 // Editor Styles .
321 kubio_override_style(
322 $styles,
323 'wp-block-editor',
324 kubio_url( 'build/block-editor/style.css' ),
325 array( 'wp-components', 'wp-editor-font' ),
326 filemtime( KUBIO_ROOT_DIR . 'build/editor/style.css' )
327 );
328 $styles->add_data( 'wp-block-editor', 'rtl', 'replace' );
329
330 }
331
332 add_action( 'init', 'kubio_register_packages_scripts' );
333 add_action( 'init', 'kubio_register_packages_styles' );
334
335 add_action( 'wp_default_styles', 'kubio_replace_default_styles' );
336 add_action( 'wp_default_scripts', 'kubio_replace_default_scripts' );
337
338
339 add_action(
340 'kubio_registered_script',
341 function ( $handle, $version ) {
342 if ( $handle === 'kubio-utils' || $handle === 'kubio-admin-panel' ) {
343 $include_test_templates = defined( 'KUBIO_INCLUDE_TEST_TEMPLATES' ) && KUBIO_INCLUDE_TEST_TEMPLATES === true;
344 $data = 'window.kubioUtilsData=' . wp_json_encode(
345 array_merge(
346 kubio_get_site_urls(),
347 array(
348
349 'defaultAssetsUrl' => kubio_url( 'static/default-assets' ),
350 'patternsAssetsUrl' => kubio_url( 'static/patterns' ),
351 'kubioRemoteContentFile' => 'https://static-assets.kubiobuilder.com/content-2022-05-17.json',
352 'kubioRemoteContent' => Utils::getCloudUrl( '/api/snippets/globals' ),
353 'kubioLocalContentFile' => kubio_url( 'static/patterns/content-converted.json' ),
354 'kubioEditorURL' => add_query_arg( 'page', 'kubio', admin_url( 'admin.php' ) ),
355 'patternsOnTheFly' =>
356 ( defined( 'KUBIO_PATTERNS_ON_THE_FLY' ) && KUBIO_PATTERNS_ON_THE_FLY )
357 ? KUBIO_PATTERNS_ON_THE_FLY
358 : '',
359 'base_url' => site_url(),
360 'admin_url' => admin_url(),
361 'admin_plugins_url' => admin_url( 'plugins.php' ),
362 'demo_sites' => DemoSitesRepository::getInstance()->getDemoSitesList(),
363 'demo_parts_by_slug' => DemoPartsRepository::getInstance()->getDemoParts(),
364 'demo_sites_url' => Utils::getCloudUrl( 'api/project/demo-sites' ),
365 'demo_parts_url' => Utils::getCloudUrl( 'api/demo-sites/get-demo-content' ),
366 'plugins_states' => DemoSitesRepository::getInstance()->getPluginsStates(),
367 'include_test_templates' => $include_test_templates,
368 'last_imported_starter' => Flags::get( 'last_imported_starter' ),
369
370 )
371 )
372 );
373
374 wp_add_inline_script( $handle, $data, 'before' );
375 }
376
377 if ( $handle === 'kubio-style-manager' ) {
378
379 $url = add_query_arg(
380 array(
381 'action' => 'kubio_style_manager_web_worker',
382 'v' => Utils::isDebug() ? time() : KUBIO_VERSION,
383 ),
384 admin_url( 'admin-ajax.php' )
385 );
386
387 wp_add_inline_script(
388 $handle,
389 'var _kubioStyleManagerWorkerURL=' . wp_json_encode( $url ),
390 'before'
391 );
392 }
393 },
394 10,
395 2
396 );
397
398 function kubio_print_style_manager_web_worker() {
399 header( 'content-type: application/javascript' );
400
401 $script = '';
402 $done = wp_scripts()->done;
403 ob_start();
404 wp_scripts()->done = array();
405 wp_scripts()->do_items( 'kubio-style-manager' );
406 wp_scripts()->done = $done;
407 $script = ob_get_clean();
408
409 $script = preg_replace_callback(
410 '#<script(.*?)>(.*?)</script>#s',
411 function( $matches ) {
412 $script_attrs = Arr::get( $matches, 1, '' );
413 preg_match( "#src='(.*?)'#", $script_attrs, $attrs_match );
414 $url = Arr::get( $attrs_match, 1, '' );
415 $content = trim( Arr::get( $matches, 2, '' ) );
416
417 $result = array();
418
419 if ( ! empty( $url ) ) {
420 $result[] = sprintf( "importScripts('%s');", $url );
421 }
422
423 if ( ! empty( $content ) ) {
424 $result[] = $content;
425 }
426
427 return trim( implode( "\n", $result ) ) . "\n\n";
428 },
429 $script
430 );
431
432 $content = file_get_contents( KUBIO_ROOT_DIR . '/defaults/style-manager-web-worker-template.js' );
433 $content = str_replace( '// {{{importScriptsPlaceholder}}}', $script, $content );
434
435 if ( ! Utils::isDebug() ) {
436 header( 'Cache-control: public' );
437 header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', time() ) . ' GMT' );
438 header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + YEAR_IN_SECONDS ) . ' GMT' );
439 header( 'Etag: ' . md5( $content ) );
440 }
441
442 die( $content );
443 }
444
445 add_action( 'wp_ajax_kubio_style_manager_web_worker', 'kubio_print_style_manager_web_worker' );
446
447 // quick test for safari
448 add_action(
449 'admin_init',
450 function () {
451 ob_start();
452 ?>
453 <script>
454 window.requestIdleCallback =
455 window.requestIdleCallback ||
456 function (cb) {
457 var start = Date.now();
458 return setTimeout(function () {
459 cb({
460 didTimeout: false,
461 timeRemaining: function () {
462 return Math.max(0, 50 - (Date.now() - start));
463 },
464 });
465 }, 1);
466 };
467
468 window.cancelIdleCallback =
469 window.cancelIdleCallback ||
470 function (id) {
471 clearTimeout(id);
472 };
473 </script>
474 <?php
475
476 $content = strip_tags( ob_get_clean() );
477
478 wp_add_inline_script( 'wp-polyfill', $content, 'after' );
479 }
480 );
481