PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.4.3
Kubio AI Page Builder v2.4.3
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 / kubio-editor.php
kubio / lib Last commit date
AI 1 year ago admin-pages 2 years ago api 1 year ago blog 3 years ago customizer 2 years ago filters 1 year ago full-site-editing 2 years ago importer 4 years ago integrations 1 year ago menu 3 years ago polyfills 3 years ago preview 2 years ago shapes 2 years ago shortcodes 2 years ago src 1 year ago add-edit-in-kubio.php 1 year ago editor-assets.php 1 year ago env.php 1 year ago filters.php 1 year ago frontend.php 4 years ago global-data.php 1 year ago init.php 2 years ago kubio-block-library.php 2 years ago kubio-editor.php 1 year ago load.php 1 year ago
kubio-editor.php
1324 lines
1 <?php
2
3 use IlluminateAgnostic\Arr\Support\Arr;
4 use Kubio\AssetsDependencyInjector;
5 use Kubio\Core\LodashBasic;
6 use Kubio\Core\Utils;
7 use Kubio\Flags;
8
9 function kubio_is_kubio_editor_page() {
10 global $pagenow;
11
12 $is = false;
13 if ( substr( $pagenow, 0, -4 ) === 'admin' ) {
14 $page = Arr::get( $_REQUEST, 'page', false );
15
16 if ( $page === 'kubio' ) {
17 $is = true;
18 }
19 }
20
21 return apply_filters( 'kubio/is_kubio_editor_page', $is );
22 }
23
24 function kubio_is_kubio_editor_page_frontend_request() {
25 $kubio_flags = kubio_get_editor_requests_flags();
26 $is_frontend_request = false;
27 foreach ( $kubio_flags as $flag ) {
28 if ( isset( $_REQUEST[ $flag ] ) ) {
29 $is_frontend_request = true;
30 }
31 }
32 return $is_frontend_request;
33 }
34 function kubio_edit_site_get_first_post_id( $loaded_id ) {
35 if ( ! $loaded_id ) {
36 $ids = get_posts(
37 array(
38 'post_type' => 'page',
39 'posts_per_page' => 1,
40 'fields' => 'ids',
41 'orderby' => 'date',
42 'order' => 'ASC',
43 )
44 );
45
46 if ( ! empty( $ids ) ) {
47 $loaded_id = $ids[0];
48 }
49 }
50
51 if ( ! $loaded_id ) {
52 $ids = get_posts(
53 array(
54 'post_type' => 'post',
55 'posts_per_page' => 1,
56 'fields' => 'ids',
57 )
58 );
59
60 if ( ! empty( $ids ) ) {
61 $loaded_id = $ids[0];
62 }
63 }
64 return $loaded_id;
65 }
66 //this should also treat the template parts case. But at the moment you can't preview template parts but if will in the future
67 //the logic should work the same
68 function kubio_edit_site_get_template_id( $loaded_id, $postType ) {
69
70 $parts = explode( '//', $loaded_id );
71 if ( count( $parts ) !== 2 ) {
72 return null;
73 }
74 $templateName = $parts[1];
75 $query = array(
76 'name' => $templateName,
77 'post_type' => $postType,
78 'fields' => 'ids',
79 'post_status' => 'publish',
80 'posts_per_page' => 1,
81 );
82 $ids = get_posts( $query );
83 $resultPostId = null;
84 if ( ! empty( $ids ) ) {
85 $resultPostId = $ids[0];
86 }
87 return $resultPostId;
88 }
89 function kubio_edit_site_get_edited_entity() {
90 $pag_on_front = intval( get_option( 'page_on_front' ) );
91 $loaded_id = isset( $_GET['postId'] ) ? intval( $_GET['postId'] ) : $pag_on_front;
92 $post_type = isset( $_GET['postType'] ) ? sanitize_text_field( $_GET['postType'] ) : null;
93
94 if ( ! post_type_exists( $post_type ) || ! is_numeric( $loaded_id ) ) {
95 return array();
96 }
97
98 //when you have latest post set as your home page and you enter the builder without the post id the loaded_id will be 0
99 //in this case we'll load the index template
100 if ( $loaded_id === 0 ) {
101 $themeName = get_stylesheet();
102 $indexTemplateId = strtr( ':theme//index', array( ':theme' => $themeName ) );
103 $loaded_id = $indexTemplateId;
104 $post_type = 'wp_template';
105 }
106
107 $templatePostId = null;
108 if ( ! ! $loaded_id && ! is_numeric( $loaded_id ) ) {
109 $templatePostId = $loaded_id;
110 $loaded_id = kubio_edit_site_get_template_id( $loaded_id, $post_type );
111 }
112 if ( ! $loaded_id || ! is_numeric( $loaded_id ) ) {
113 $loaded_id = kubio_edit_site_get_first_post_id( $loaded_id );
114 }
115
116 if ( $loaded_id ) {
117 $entity = get_post( $loaded_id );
118
119 return array(
120 'path' => get_permalink( $loaded_id ),
121 'context' => array(
122 'postType' => $entity ? $entity->post_type : '',
123 'postId' => $templatePostId ? $templatePostId : $loaded_id,
124 'query' => '',
125 'postTitle' => $entity ? $entity->post_title : '',
126 'title' => $entity ? $entity->post_title : '',
127 ),
128 'slug' => $entity ? $entity->post_name : '',
129 'label' => $entity ? $entity->post_title : '',
130 );
131 }
132
133 return array();
134 }
135
136
137 function kubio_get_patterns_categories() {
138 return array(
139 array(
140 'label' => __( 'Hero accent', 'kubio' ),
141 'name' => 'kubio-content/hero accent',
142 ),
143
144 array(
145 'label' => __( 'About', 'kubio' ),
146 'name' => 'kubio-content/about',
147 ),
148
149 array(
150 'label' => __( 'Features', 'kubio' ),
151 'name' => 'kubio-content/features',
152 ),
153
154 array(
155 'label' => __( 'Content', 'kubio' ),
156 'name' => 'kubio-content/content',
157 ),
158
159 array(
160 'label' => __( 'Call to action', 'kubio' ),
161 'name' => 'kubio-content/cta',
162 ),
163 array(
164 'label' => __( 'Blog', 'kubio' ),
165 'name' => 'kubio-content/blog',
166 ),
167
168 array(
169 'label' => __( 'Counters', 'kubio' ),
170 'name' => 'kubio-content/counters',
171 ),
172
173 array(
174 'label' => __( 'Portfolio', 'kubio' ),
175 'name' => 'kubio-content/portfolio',
176 ),
177
178 array(
179 'label' => __( 'Photo gallery', 'kubio' ),
180 'name' => 'kubio-content/photo gallery',
181 ),
182
183 array(
184 'label' => __( 'Testimonials', 'kubio' ),
185 'name' => 'kubio-content/testimonials',
186 ),
187
188 array(
189 'label' => __( 'Clients', 'kubio' ),
190 'name' => 'kubio-content/clients',
191 ),
192
193 array(
194 'label' => __( 'Team', 'kubio' ),
195 'name' => 'kubio-content/team',
196 ),
197
198 array(
199 'label' => __( 'Contact', 'kubio' ),
200 'name' => 'kubio-content/contact',
201 ),
202
203 array(
204 'label' => __( 'F.A.Q.', 'kubio' ),
205 'name' => 'kubio-content/f.a.q.',
206 ),
207
208 array(
209 'label' => __( 'Pricing', 'kubio' ),
210 'name' => 'kubio-content/pricing',
211 ),
212 array(
213 'label' => __( 'Inner Headers', 'kubio' ),
214 'name' => 'kubio-header/inner headers',
215 ),
216
217 array(
218 'label' => __( 'Headers', 'kubio' ),
219 'name' => 'kubio-header/headers',
220 ),
221
222 array(
223 'label' => __( 'Footers', 'kubio' ),
224 'name' => 'kubio-footer/footers',
225 ),
226 );
227 }
228
229 function kubio_get_editor_style( $get_css = false, $skiped_handlers = array() ) {
230 $style_handles = array(
231 'wp-edit-blocks',
232 'wp-block-editor',
233 'wp-block-library',
234 'wp-block-library-theme',
235 'kubio-editor',
236 'kubio-controls',
237 'kubio-utils',
238 'kubio-format-library',
239 'kubio-pro',
240 'kubio-block-library-editor',
241 'kubio-third-party-blocks',
242 'kubio-icons',
243 );
244
245 if ( kubio_wpml_is_active() ) {
246 $styles_to_copy = array(
247 'legacy-dropdown',
248 'legacy-dropdown-click',
249 'legacy-list-horizontal',
250 'legacy-list-vertical',
251 'legacy-post-translations',
252 'menu-item',
253 );
254
255 foreach ( $styles_to_copy as $style ) {
256 $style_handles[] = "kubio-copy-of-wpml-{$style}";
257 }
258 }
259
260 $style_handles = apply_filters( 'kubio/kubio_get_editor_style/style_handles', $style_handles );
261
262 $wp_styles = wp_styles();
263 foreach ( array_keys( $wp_styles->registered ) as $handle ) {
264 if ( strpos( $handle, AssetsDependencyInjector::KUBIO_DEPENENCY_PREFIX ) === 0 ) {
265 $style_handles[] = $handle;
266 }
267 }
268
269 foreach ( $skiped_handlers as $index => $handler ) {
270 $handler = preg_replace( '#(.*?)-inline-css$#', '$1', $handler );
271 $handler = preg_replace( '#(.*?)-css$#', '$1', $handler );
272 $skiped_handlers[ $index ] = $handler;
273 }
274
275 $skiped_handlers = array_unique( $skiped_handlers );
276 $style_handles = array_diff( $style_handles, $skiped_handlers );
277
278 $block_registry = WP_Block_Type_Registry::get_instance();
279
280 foreach ( $block_registry->get_all_registered() as $block_type ) {
281 if ( ! empty( $block_type->style ) ) {
282 $style_handles[] = $block_type->style;
283 }
284
285 if ( ! empty( $block_type->editor_style ) ) {
286 $style_handles[] = $block_type->editor_style;
287 }
288 }
289
290 $style_handles = Arr::flatten( $style_handles );
291 $style_handles = array_values( array_unique( $style_handles ) );
292 $done = wp_styles()->done;
293 ob_start();
294
295 ?>
296 <style><?php echo wp_get_global_stylesheet(); ?></style>
297 <?php
298 wp_styles()->done = $skiped_handlers;
299 wp_styles()->do_items( $style_handles );
300 wp_styles()->done = $done;
301
302 $style = ob_get_clean();
303
304 if ( $get_css ) {
305 $style = preg_replace( "#<link(.*)href='(.*?)'(.*)/>#", '@import url($2);', $style );
306 $style = preg_replace( '#<style(.*)>#', '', $style );
307 $style = preg_replace( '#</style>#', '', $style );
308 }
309
310 return $style;
311 }
312
313 function kubio_get_editor_scripts( $skiped_handlers = array() ) {
314 $script_handles = kubio_get_frontend_scripts();
315
316 $wp_scripts = wp_scripts();
317 foreach ( array_keys( $wp_scripts->registered ) as $handle ) {
318 if ( strpos( $handle, AssetsDependencyInjector::KUBIO_DEPENENCY_PREFIX ) === 0 ) {
319 array_unshift( $script_handles, $handle );
320 }
321 }
322
323 foreach ( $skiped_handlers as $index => $handler ) {
324 $handler = preg_replace( '#(.*?)-js-after#', '$1', $handler );
325 $handler = preg_replace( '#(.*?)-js-before#', '$1', $handler );
326 $handler = preg_replace( '#(.*?)-js-translations#', '$1', $handler );
327 $handler = preg_replace( '#(.*?)-js-extra#', '$1', $handler );
328 $skiped_handlers[ $index ] = $handler;
329 }
330
331 $script = '';
332 $done = wp_scripts()->done;
333 ob_start();
334 wp_scripts()->done = $skiped_handlers;
335 wp_scripts()->do_items( $script_handles );
336 wp_scripts()->done = $done;
337 $script = ob_get_clean();
338
339 return $script;
340 }
341
342 function kubio_enqueue_editor_page_assets() {
343 // Editor default styles
344 wp_enqueue_media();
345 wp_enqueue_style( 'wp-format-library' );
346 wp_enqueue_style( 'kubio-format-library' );
347 wp_enqueue_script( 'kubio-editor' );
348 wp_enqueue_script( 'kubio-block-library' );
349 wp_enqueue_style( 'kubio-admin-panel' );
350 wp_enqueue_style( 'kubio-ai' );
351
352 wp_enqueue_style( 'kubio-editor' );
353 wp_enqueue_style( 'kubio-icons' );
354 wp_enqueue_style( 'kubio-advanced-panel' );
355 wp_enqueue_style( 'kubio-block-library-editor' );
356 wp_enqueue_style( 'kubio-controls' );
357 wp_enqueue_style( 'kubio-utils' );
358 wp_enqueue_style( 'kubio-scripts' );
359 wp_enqueue_style( 'kubio-wp-global-styles' );
360
361 }
362
363 function kubio_extend_block_editor_styles_html() {
364 $script = kubio_get_editor_scripts();
365 $style = kubio_get_editor_style();
366 $script = "<template id='kubio-scripts-template'>{$script}</template>";
367
368 printf(
369 '<script>window.__kubioEditorStyles = %s</script>',
370 wp_json_encode( array( 'html' => $style . $script ) )
371 );
372
373 if ( function_exists( 'wp_add_iframed_editor_assets_html' ) ) {
374 wp_add_iframed_editor_assets_html();
375 }
376
377 }
378
379 function kubio_edit_site_get_settings() {
380 $max_upload_size = wp_max_upload_size();
381 if ( ! $max_upload_size ) {
382 $max_upload_size = 0;
383 }
384
385 // This filter is documented in wp-admin/includes/media.php.
386 // This filter is documented in wp-admin/includes/media.php.
387 $image_size_names = apply_filters(
388 'image_size_names_choose',
389 array(
390 'thumbnail' => __( 'Thumbnail', 'kubio' ),
391 'medium' => __( 'Medium', 'kubio' ),
392 'large' => __( 'Large', 'kubio' ),
393 'full' => __( 'Original Size', 'kubio' ),
394 )
395 );
396 $available_image_sizes = array();
397 foreach ( $image_size_names as $image_size_slug => $image_size_name ) {
398 $available_image_sizes[] = array(
399 'slug' => $image_size_slug,
400 'name' => $image_size_name,
401 );
402 }
403
404 global $current_screen;
405 if ( function_exists( '_register_core_block_patterns_and_categories' ) ) {
406 _register_core_block_patterns_and_categories();
407 }
408
409 if ( function_exists( '_load_remote_block_patterns' ) ) {
410 _load_remote_block_patterns();
411 }
412
413 if ( function_exists( '_load_remote_featured_patterns' ) ) {
414 _load_remote_featured_patterns( $current_screen );
415 }
416
417 global $wp_version;
418 $block_editor_context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) );
419 $settings = array_merge(
420 get_default_block_editor_settings(),
421 array(
422 'alignWide' => true, //get_theme_support( 'align-wide' ),
423 'siteUrl' => site_url( '/' ),
424 'postsPerPage' => get_option( 'posts_per_page' ),
425 'defaultTemplateTypes' => kubio_get_indexed_default_template_types(),
426 '__experimentalBlockPatterns' => array(),
427 '__experimentalBlockPatternCategories' => array_merge(
428 WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(),
429 kubio_get_patterns_categories()
430 ),
431 'imageSizes' => $available_image_sizes,
432 'isRTL' => is_rtl(),
433 'maxUploadFileSize' => $max_upload_size,
434
435 'wpVersion' => preg_replace( '/([0-9]+).([0-9]+).*/', '$1.$2', $wp_version ),
436 )
437 );
438
439 if ( function_exists( 'get_block_editor_settings' ) && class_exists( '\WP_Block_Editor_Context' ) ) {
440 $settings = get_block_editor_settings( $settings, $block_editor_context );
441 }
442
443 if ( function_exists( 'gutenberg_experimental_global_styles_settings' ) ) {
444 $settings = gutenberg_experimental_global_styles_settings( $settings );
445 }
446
447 $settings['state'] = array(
448 'entity' => kubio_edit_site_get_edited_entity(),
449 );
450
451 $settings['kubioPrimaryMenuLocation'] = apply_filters(
452 'kubio/primary_menu_location',
453 'header-menu'
454 );
455
456 $settings['isKubioTheme'] = kubio_theme_has_kubio_block_support();
457
458 $settings['supportsTemplateMode'] = kubio_theme_has_block_templates_support();
459
460 global $post;
461 $settings = apply_filters( 'block_editor_settings_all', $settings, $post );
462 $settings = apply_filters( 'kubio/block_editor_settings', $settings, $post );
463 $settings['isWooCommerce'] = function_exists( 'WC' );
464
465 $settings['defaultTemplatePartAreas'] = kubio_get_allowed_template_part_areas();
466 $settings['classicThemeTemplates'] = kubio_get_classic_theme_templates();
467 $settings['kubioThemeAssetsUrlBase'] = untrailingslashit( apply_filters( 'kubio/importer/kubio-url-placeholder-replacement', '' ) );
468
469 // adding it here since `select('core').getCurrentTheme().theme_uri` returns an empty string.
470 $settings['themeUri'] = get_template_directory_uri();
471 $settings['classicHasFrontPageTemplate'] = kubio_third_party_theme_has_front_page_template();
472 $settings['classicThemePrimaryTemplates'] = array_keys( kubio_get_classic_theme_primary_templates() );
473
474 $scrollable_image_path = wp_get_theme()->get_file_path( 'resources/images/front-page-preview.jpg' );
475 // Handle the Front page suggestion preview image
476 if ( file_exists( $scrollable_image_path ) ) {
477 $settings['kubioHasFpsScrollPreview'] = true; // 'resources/images/front-page-preview.jpg';
478 } else {
479 $settings['kubioHasFpsScrollPreview'] = false; //'screenshot.jpg';
480 }
481
482 $settings['supportsLayout'] = isset( $settings['supportsLayout'] ) ? $settings['supportsLayout'] : false;
483 $layout = array_merge( array( 'contentSize' => '1172px' ), LodashBasic::get( $settings, '__experimentalFeatures.layout', array() ) );
484
485 LodashBasic::set( $settings, '__experimentalFeatures.layout', $layout );
486 LodashBasic::set( $settings, '__experimentalFeatures.blocks.core/post-content.layout', $layout );
487
488 // use array_values to ensure that json_encode sees it as array not object
489 $styles = array_values(
490 array_merge(
491 function_exists( 'get_block_editor_theme_styles' ) ? get_block_editor_theme_styles() : array(),
492 array(
493 array(
494 'css' => kubio_get_editor_style( true ),
495 '__unstableType' => 'theme',
496 ),
497 )
498 )
499 );
500
501 if ( function_exists( 'wp_get_global_stylesheet' ) ) {
502 $block_classes = array(
503 'css' => 'base-layout-styles',
504 '__unstableType' => 'base-layout',
505 'isGlobalStyles' => true,
506 );
507 $actual_css = wp_get_global_stylesheet( array( $block_classes['css'] ) );
508
509 if ( $actual_css ) {
510 $block_classes['css'] = $actual_css;
511 $styles[] = $block_classes;
512 }
513 }
514
515 $settings['styles'] = array_merge( $settings['styles'], $styles );
516
517 return $settings;
518 }
519
520 function kubio_block_editor_general_settings( $settings ) {
521 $settings['__unstableEnableFullSiteEditingBlocks'] = true;
522 $settings['enableFSEBlocks'] = true;
523 $settings['kubioGlobalSettings'] = (object) Flags::getSettings();
524
525 // settings for outside Kubio editor
526 // __unstableResolvedAssets was added in WP 6.0
527 if ( isset( $settings['__unstableResolvedAssets'] ) && ! kubio_is_kubio_editor_page() ) {
528 $current_style = Arr::get( $settings, '__unstableResolvedAssets.styles', '' );
529 $current_scripts = Arr::get( $settings, '__unstableResolvedAssets.scripts', '' );
530
531 preg_match_all( "#id='(.*?)'#m", $current_style, $matches );
532 $style_ids = Arr::get( $matches, 1, array() );
533
534 preg_match_all( "#id='(.*?)'#m", $current_scripts, $matches );
535 $script_ids = Arr::get( $matches, 1, array() );
536
537 $style = array(
538 sprintf( '<style>%s</style>', kubio_render_global_colors() ),
539 kubio_get_editor_style( false, $style_ids ),
540 );
541
542 $script = kubio_get_editor_scripts( $script_ids );
543
544 Arr::set(
545 $settings,
546 '__unstableResolvedAssets.styles',
547 $current_style . "\n\n" . implode( "\n", $style )
548 );
549
550 Arr::set(
551 $settings,
552 '__unstableResolvedAssets.scripts',
553 $current_scripts . "\n\n$script"
554 );
555
556 // kubio_enqueue_frontend_scripts();
557
558 }
559
560 $settings['kubioLoaded'] = true;
561
562 $settings['kubioThemesBaseURL'] = get_theme_root_uri();
563 return $settings;
564 }
565
566 add_filter( 'block_editor_settings_all', 'kubio_block_editor_general_settings', 50 );
567
568 function kubio_load_gutenberg_assets() {
569
570 kubio_fix_template_and_parts_default_editor();
571
572 AssetsDependencyInjector::injectKubioScriptDependencies( 'swiper' );
573 AssetsDependencyInjector::injectKubioFrontendStyleDependencies( 'swiper' );
574
575 AssetsDependencyInjector::injectKubioScriptDependencies( 'fancybox' );
576 AssetsDependencyInjector::injectKubioFrontendStyleDependencies( 'fancybox' );
577
578 AssetsDependencyInjector::injectKubioScriptDependencies( 'typed' );
579 AssetsDependencyInjector::injectKubioScriptDependencies( 'jquery-masonry', false );
580
581 wp_enqueue_style( 'kubio-pro' );
582
583 wp_enqueue_script( 'kubio-block-patterns' );
584 wp_enqueue_script( 'kubio-third-party-blocks' );
585 wp_localize_script(
586 'kubio-block-patterns',
587 'kubioBlockPatterns',
588 array(
589 'inGutenbergEditor' => ! kubio_is_kubio_editor_page(),
590 )
591 );
592
593 wp_add_inline_style( 'kubio-block-library', kubio_get_shapes_css() );
594 }
595
596 //https://mantis.iconvert.pro/view.php?id=54771
597 function kubio_fix_template_and_parts_default_editor() {
598
599 global $post;
600
601 if ( ! $post ) {
602 return;
603 }
604 if ( ! in_array( $post->post_type, array( 'wp_template', 'wp_template_part' ) ) ) {
605 return;
606 }
607 $preload_paths = array(
608
609 '/wp/v2/themes?context=edit&status=active',
610
611 );
612
613 $preload_data = array_reduce(
614 $preload_paths,
615 'rest_preload_api_request',
616 array()
617 );
618 $theme_data = Arr::get( $preload_data, $preload_paths[0] . '.body.0' );
619 if ( ! $theme_data ) {
620 return;
621 }
622 //Very dirty method of fixing the bug(https://mantis.iconvert.pro/view.php?id=54771). But could not find any other method. The normal preload did not seem to fix the issue.
623 wp_add_inline_script(
624 'wp-core-data',
625 sprintf( 'wp.data.select("core").getCurrentTheme = () => { return %s }', wp_json_encode( $theme_data ) ),
626 'after'
627 );
628
629 }
630
631 function kubio_get_post_types() {
632 $extra_types = get_post_types(
633 array(
634 '_builtin' => false,
635 'public' => true,
636 )
637 );
638
639 $types = array();
640 global $wp_post_types;
641
642 foreach ( $extra_types as $type ) {
643 $types[] = array(
644 'entity' => $type,
645 'kind' => 'postType',
646 'title' => $wp_post_types[ $type ]->label,
647 );
648 }
649
650 return $types;
651 }
652
653 add_action( 'enqueue_block_editor_assets', 'kubio_load_gutenberg_assets' );
654
655
656 function kubio_add_endpoint_details(
657 $url,
658 $results_per_page = null
659 ) {
660 $vars = array( 'context' => 'edit' );
661 if ( $results_per_page !== null ) {
662 $vars['per_page'] = $results_per_page;
663 }
664 return $url . '?' . build_query( $vars );
665 }
666
667 function kubio_edit_site_init( $hook ) {
668 if ( ! kubio_is_kubio_editor_page() ) {
669 return;
670 }
671 add_filter( 'use_block_editor_for_post_type', '__return_true' );
672
673 global $current_screen, $post;
674 $current_screen->is_block_editor( true );
675 $active_theme = wp_get_theme()->get_stylesheet();
676
677 // Inline the Editor Settings
678 $settings = kubio_edit_site_get_settings();
679 // Preload block editor paths.
680 // most of these are copied from edit-forms-blocks.php.
681 $preload_paths = array(
682 array( '/wp/v2/media', 'OPTIONS' ),
683 '/wp/v2/types?context=view',
684 '/wp/v2/types/wp_template?context=edit',
685 '/wp/v2/types/wp_template-part?context=edit',
686 '/wp/v2/templates?context=edit&per_page=-1',
687 '/wp/v2/template-parts?context=edit&per_page=-1',
688 '/wp/v2/themes?context=edit&status=active',
689 '/wp/v2/users/me',
690
691 // contact and subscribe
692 '/kubio/v1/contact-form/forms_by_type',
693 '/kubio/v1/subscribe-form/forms_by_type',
694
695 // menu locations and menus
696 '/__experimental/menu-locations?context=edit',
697 '/wp/v2/menu-locations?context=edit',
698 '/wp/v2/users/me?context=edit',
699
700 // taxonimies
701 '/wp/v2/taxonomies?context=view',
702
703 // global data
704 '/wp/v2/kubio/global-data/' . kubio_global_data_post_id(),
705 array( '/wp/v2/kubio/global-data/' . kubio_global_data_post_id(), 'OPTIONS' ),
706 '/wp/v2/kubio/global-data/' . kubio_global_data_post_id() . '?context=edit',
707
708 // general
709 '/wp/v2/settings',
710 array( '/wp/v2/settings', 'OPTIONS' ),
711
712 kubio_add_endpoint_details( "/wp/v2/template-parts/{$active_theme}//footer" ),
713 kubio_add_endpoint_details( "/wp/v2/template-parts/{$active_theme}//front-header" ),
714 kubio_add_endpoint_details( "/wp/v2/templates/{$active_theme}//front-page" ),
715 kubio_add_endpoint_details( "/wp/v2/templates/{$active_theme}//full-width" ),
716 kubio_add_endpoint_details( "/wp/v2/templates/{$active_theme}//front-page" ),
717
718 );
719
720 // try to preload current page
721
722 $wp_global_style = 0;
723 if ( class_exists( '\WP_Theme_JSON_Resolver' ) && method_exists( WP_Theme_JSON_Resolver::class, 'get_user_global_styles_post_id' ) ) {
724 $wp_global_style = WP_Theme_JSON_Resolver::get_user_global_styles_post_id();
725 }
726 if ( $wp_global_style ) {
727 $preload_paths = array_merge(
728 $preload_paths,
729 array(
730 '/wp/v2/global-styles/' . $wp_global_style . '?context=edit',
731 '/wp/v2/global-styles/' . $wp_global_style,
732 '/wp/v2/global-styles/themes/' . $active_theme,
733 )
734 );
735 }
736
737 // menus
738 $menus = wp_get_nav_menus();
739 foreach ( $menus as $menu_term ) {
740 $preload_paths[] = "/kubio/v1/menu/{$menu_term->term_id}";
741 $preload_paths[] = "/kubio/v1/menu/{$menu_term->term_id}?context=edit";
742 }
743
744 $preload_data = array_reduce(
745 $preload_paths,
746 'rest_preload_api_request',
747 array()
748 );
749
750 // prepare editor context
751 $editor_context_props = array( 'name' => 'core/edit-site' );
752 $query_post_id = Arr::get( $_REQUEST, 'postId', null );
753 $query_post_type = Arr::get( $_REQUEST, 'postType', null );
754 $page_on_front = get_option( 'page_on_front', null );
755 $post_to_load = null;
756
757 if ( ! $query_post_id && ! $query_post_type ) {
758 if ( $page_on_front ) {
759 $post_to_load = get_post( $page_on_front );
760 if ( is_wp_error( $post_to_load ) ) {
761 $post_to_load = null;
762 }
763 }
764 }
765
766 if ( $query_post_type && $query_post_id ) {
767 if ( is_numeric( $query_post_id ) ) {
768 $post_to_load = get_post( $query_post_id );
769 if ( is_wp_error( $post_to_load ) ) {
770 $post_to_load = null;
771 }
772 } else {
773 $post_to_load = kubio_get_block_template( $query_post_id, $query_post_type );
774
775 if ( ! $post_to_load || is_wp_error( $post_to_load ) ) {
776 $post_to_load = null;
777 }
778 }
779 }
780
781 if ( $post_to_load ) {
782 $post_to_load_type = property_exists( $post_to_load, 'type' ) ? $post_to_load->type : $post_to_load->post_type;
783 $post_to_load_id = property_exists( $post_to_load, 'wp_id' ) ? $post_to_load->wp_id : $post_to_load->ID;
784
785 $editor_context_props['post'] = get_post( $post_to_load_id );
786 $rest_path = rest_get_route_for_post( $post_to_load_id );
787 $preload_paths[] = $rest_path;
788 $preload_paths[] = add_query_arg( 'context', 'edit', $rest_path );
789 $preload_paths[] = sprintf( '/wp/v2/types/%s?context=edit', $post_to_load_type );
790 }
791
792 $block_editor_context = new \WP_Block_Editor_Context( $editor_context_props );
793
794 if ( function_exists( 'block_editor_rest_api_preload' ) ) {
795 block_editor_rest_api_preload( $preload_paths, $block_editor_context );
796 } else {
797 wp_add_inline_script(
798 'wp-api-fetch',
799 sprintf(
800 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
801 wp_json_encode( $preload_data )
802 ),
803 'after'
804 );
805 }
806
807 $server_side_settings = get_block_editor_server_block_settings();
808 foreach ( array_keys( $server_side_settings ) as $server_side_block_name ) {
809 if ( strpos( $server_side_block_name, 'kubio/' ) === 0 ) {
810 unset( $server_side_settings[ $server_side_block_name ] );
811 }
812 }
813 wp_add_inline_script(
814 'wp-blocks',
815 sprintf(
816 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions( %s );',
817 wp_json_encode( $server_side_settings )
818 ),
819 'after'
820 );
821
822 wp_add_inline_script(
823 'wp-blocks',
824 sprintf(
825 'wp.blocks.setCategories( %s );',
826 wp_json_encode( get_block_categories( $post ) )
827 ),
828 'after'
829 );
830
831 kubio_enqueue_editor_page_assets();
832 $settings['postTypes'] = kubio_get_post_types();
833
834 $ai_hash = Flags::get( 'start_with_ai_hash', null );
835 $ai_hash_param = Arr::get( $_REQUEST, 'ai', null );
836 $settings['startWithAIFrontPage'] = ( $ai_hash && $ai_hash_param && $ai_hash_param === $ai_hash );
837
838 $black_wizard_onboarding_hash = Flags::get( 'black_wizard_onboarding_hash', null );
839 $black_wizard_onboarding_param = Arr::get( $_REQUEST, 'black-wizard-onboarding', null );
840 $settings['startWithBlackWizardOnboarding'] = ( kubio_is_black_wizard_onboarding_enabled() &&
841 $black_wizard_onboarding_hash &&
842 $black_wizard_onboarding_param &&
843 $black_wizard_onboarding_hash === $black_wizard_onboarding_param );
844
845 if($settings['startWithBlackWizardOnboarding']) {
846 add_filter('admin_body_class', function ($classes) {
847 $classes .= ' kubio-will-have-black-wizard-onboarding';
848 return $classes;
849 });
850 }
851 if ( isset( $_REQUEST['generate-ai-frontpage'] ) && ! Flags::get( 'generated_from_getting_started', false ) ) {
852 $settings['startGeneratingAIFrontPage'] = true;
853 Flags::set( 'generated_from_getting_started', true );
854 }
855
856 wp_add_inline_script(
857 'kubio-editor',
858 sprintf(
859 "window.kubioEditSiteSettings = %s;\n" .
860 'wp.domReady( function() { kubio.editor.initialize( "kubio-editor", window.kubioEditSiteSettings ); } );',
861 wp_json_encode( $settings )
862 ),
863 'after'
864 );
865
866 wp_add_inline_style( 'kubio-editor', kubio_get_shapes_css() );
867 wp_add_inline_style( 'kubio-editor', kubio_render_global_colors() );
868
869 add_action( 'admin_footer', 'kubio_extend_block_editor_styles_html' );
870 do_action( 'enqueue_block_editor_assets' );
871 do_action( 'kubio/editor/enqueue_assets' );
872
873 $editor_styles = function_exists( 'get_editor_stylesheets' ) ? get_editor_stylesheets() : null;
874 if (
875 current_theme_supports( 'wp-block-styles' ) ||
876 ( ! is_array( $editor_styles ) || count( $editor_styles ) === 0 )
877 ) {
878 wp_enqueue_style( 'wp-block-library-theme' );
879 }
880
881 }
882
883 add_action( 'admin_enqueue_scripts', 'kubio_edit_site_init' );
884
885 // Add tinymce scripts within the Kubio Editor and WordPress Editor since we need them in blocks like Subscribe form.
886 // we use the `enqueue_block_editor_assets` action since it is called in both editors.
887 add_action(
888 'enqueue_block_editor_assets',
889 function () {
890 wp_enqueue_script( 'wp-tinymce' );
891 wp_enqueue_editor();
892 wp_enqueue_code_editor( array() );
893 wp_tinymce_inline_scripts();
894 }
895 );
896
897 /**
898 * Renders the Menu Page
899 *
900 * @return void
901 */
902 function kubio_edit_site_render_block_editor() {
903 ?>
904 <style>
905 div#wpcontent,
906 #wpbody-content {
907 position: fixed;
908 z-index: 1000000;
909 width: 100vw;
910 height: 100vh;
911 background: #fff;
912 top: 0%;
913 left: 0%;
914 right: 0;
915 border: 0;
916 display: block;
917 box-sizing: border-box;
918 }
919
920 #wp-link-backdrop {
921 z-index: 1000050;
922 }
923
924 #wp-link-wrap {
925 z-index: 1000100;
926 }
927
928 #kubio-editor {
929 position: absolute;
930 top: 0;
931 bottom: 0;
932 left: 0;
933 right: 0;
934 z-index: 1000;
935 width: 100%;
936 height: 100%;
937 }
938
939 .kubio-loading-editor {
940 position: absolute;
941 top: 50%;
942 left: 50%;
943 display: block;
944 font-size: 1.5em;
945 transform: translate(-50%, -50%);
946 }
947
948 .kubio-loading-editor iframe {
949 margin: 0px auto 0px auto;
950 }
951 </style>
952 <div id="kubio-editor" class="kubio">
953 <div class="kubio-loading-editor">
954 <?php echo kubio_get_iframe_loader( array( 'size' => '120px' ) ); ?>
955 </div>
956 </div>
957 <?php
958 }
959
960 /**
961 * Registers the new WP Admin Menu
962 *
963 * @return void
964 */
965 function kubio_edit_site_add_menu_page() {
966
967 if ( ! current_user_can( 'edit_theme_options' ) ) {
968 return;
969 }
970
971 add_menu_page(
972 __( 'Kubio', 'kubio' ),
973 sprintf(
974 '<span class="kubio-menu-item"><span class="kubio-menu-item--icon">%s</span><span>%s</span></span>',
975 wp_kses_post( KUBIO_LOGO_SVG ),
976 __( 'Kubio', 'kubio' )
977 ),
978 'edit_posts',
979 'kubio',
980 'kubio_edit_site_render_block_editor',
981 'data:image/svg+xml;base64,' . base64_encode( str_replace( '<svg', '<svg fill="#5246F1" ', KUBIO_LOGO_SVG ) ),
982 20
983 );
984
985 add_submenu_page(
986 'themes.php',
987 __( 'Kubio', 'kubio' ),
988 __( 'Edit with Kubio', 'kubio' ),
989 'edit_posts',
990 'kubio_appearance_edit',
991 'kubio_edit_site_render_block_editor',
992 1
993 );
994
995 global $submenu;
996
997 if ( isset( $submenu['themes.php'] ) ) {
998 foreach ( $submenu['themes.php'] as $index => $submenu_item ) {
999 if ( $submenu_item[2] === 'kubio_appearance_edit' ) {
1000 $submenu['themes.php'][ $index ][2] = Utils::kubioGetEditorURL();
1001 }
1002 }
1003 }
1004 }
1005
1006 function kubio_admin_page_submenu_hook() {
1007 global $submenu;
1008
1009 $kubio_submenu = Arr::get( $submenu, 'kubio', null );
1010
1011 if ( is_array( $kubio_submenu ) ) {
1012 foreach ( $kubio_submenu as $index => $submenu_data ) {
1013 $menu_slug = Arr::get( $submenu_data, '2', null );
1014 if ( $menu_slug === 'kubio' ) {
1015 $submenu['kubio'][ $index ][0] = __( 'Open Kubio Editor', 'kubio' );
1016 }
1017 }
1018 }
1019 }
1020
1021 add_action( 'admin_menu', 'kubio_admin_page_submenu_hook', 200 );
1022
1023 add_action( 'admin_menu', 'kubio_edit_site_add_menu_page' );
1024
1025 function kubio_admin_menu_style() {
1026 ?>
1027 <style>
1028 #adminmenu .toplevel_page_kubio .wp-menu-image {
1029 display: none;
1030 }
1031
1032 .wp-admin.folded #adminmenu .toplevel_page_kubio .wp-menu-image {
1033 display: block;
1034 background-size: 16px auto;
1035 }
1036
1037 #adminmenu .kubio-menu-item {
1038 display: flex;
1039 align-items: center;
1040 }
1041
1042 #adminmenu .kubio-menu-item>span {
1043 display: block;
1044 line-height: 1;
1045 }
1046
1047 #adminmenu .kubio-menu-item--icon {
1048 color: rgba(240, 246, 252, .6);
1049 }
1050
1051 #adminmenu .wp-menu-name:hover .kubio-menu-item--icon {
1052 color: inherit;
1053 }
1054
1055 #adminmenu .kubio-menu-item--icon svg {
1056 fill: currentColor;
1057 height: 15px;
1058 margin-right: 11px;
1059 }
1060
1061 #adminmenu .wp-menu-open .kubio-menu-item--icon svg {
1062 fill: #fff;
1063 }
1064
1065 #adminmenu .toplevel_page_kubio div.wp-menu-name {
1066 padding: 8px 8px 8px 10px;
1067 }
1068
1069 #adminmenu .wp-submenu .kubio-menu-item--icon {
1070 display: none;
1071 }
1072 </style>
1073 <?php
1074 }
1075
1076 add_action( 'admin_head', 'kubio_admin_menu_style' );
1077
1078 /**
1079 * Register a core site setting for front page information.
1080 */
1081 function kubio_register_site_editor_homepage_settings() {
1082 global $wp_registered_settings;
1083
1084 if ( ! isset( $wp_registered_settings['show_on_front'] ) ) {
1085 register_setting(
1086 'reading',
1087 'show_on_front',
1088 array(
1089 'show_in_rest' => true,
1090 'type' => 'string',
1091 'description' => __( 'What to show on the front page', 'kubio' ),
1092 )
1093 );
1094 }
1095
1096 if ( ! isset( $wp_registered_settings['page_on_front'] ) ) {
1097 register_setting(
1098 'reading',
1099 'page_on_front',
1100 array(
1101 'show_in_rest' => true,
1102 'type' => 'number',
1103 'description' => __(
1104 'The ID of the page that should be displayed on the front page',
1105 'kubio'
1106 ),
1107 )
1108 );
1109 }
1110 if ( ! isset( $wp_registered_settings['page_for_posts'] ) ) {
1111 register_setting(
1112 'reading',
1113 'page_for_posts',
1114 array(
1115 'show_in_rest' => true,
1116 'type' => 'number',
1117 'description' => __(
1118 'The ID of the page that displays the posts',
1119 'kubio'
1120 ),
1121 )
1122 );
1123 }
1124
1125 if ( ! isset( $wp_registered_settings['site_icon'] ) ) {
1126 register_setting(
1127 'general',
1128 'site_icon',
1129 array(
1130 'show_in_rest' => array(
1131 'name' => 'site_icon',
1132 ),
1133 'type' => 'integer',
1134 'description' => __( 'Site icon.', 'kubio' ),
1135 )
1136 );
1137 }
1138
1139 if ( class_exists( 'Jetpack_Notifications' ) ) {
1140 remove_action( 'wp_head', array( Jetpack_Notifications::init(), 'styles_and_scripts' ), 120 );
1141 remove_action( 'admin_head', array( Jetpack_Notifications::init(), 'styles_and_scripts' ) );
1142 }
1143 }
1144
1145 add_action( 'init', 'kubio_register_site_editor_homepage_settings', 100 );
1146
1147 function kubio_editor_add_default_template_types( $template_types ) {
1148 if ( kubio_is_kubio_editor_page() && is_user_logged_in() ) {
1149 $kubio_templates = array(
1150 'full-width' => array(
1151 'title' => _x( 'Full Width', 'Template name', 'kubio' ),
1152 'description' => __(
1153 'Recommended Kubio template to display individual pages. This template works best with the Kubio provided blocks and predesigned sections.',
1154 'kubio'
1155 ),
1156 ),
1157 'kubio-full-width' => array(
1158 'title' => _x( 'Kubio Full Width', 'Template name', 'kubio' ),
1159 'description' => __(
1160 'Recommended Kubio template to display individual pages. This template works best with the Kubio provided blocks and predesigned sections.',
1161 'kubio'
1162 ),
1163 ),
1164 );
1165
1166 foreach ( $kubio_templates as $slug => $settings ) {
1167 $template_types[ $slug ] = $settings;
1168 }
1169 }
1170
1171 return $template_types;
1172 }
1173
1174 add_filter( 'default_template_types', 'kubio_editor_add_default_template_types' );
1175
1176 add_action(
1177 'init',
1178 function () {
1179 if ( kubio_is_kubio_editor_page() && is_user_logged_in() ) {
1180 $has_valid_req = Utils::validateRequirements();
1181
1182 if ( is_wp_error( $has_valid_req ) ) {
1183 ob_start();
1184 ?>
1185 <style>
1186 body#error-page {
1187 margin: 0;
1188 position: absolute;
1189 top: 50%;
1190 left: 50%;
1191 transform: translate(-50%, -50%);
1192 border-left: 4px solid #fb3e41;
1193 padding-top: 0;
1194 padding-bottom: 0;
1195 }
1196
1197 body#error-page .kubio-mrn {
1198 flex-direction: column;
1199 align-items: flex-start;
1200 }
1201
1202 body#error-page .kubio-mrn-icon-holder {
1203 padding-bottom: 12px;
1204 }
1205
1206 body#error-page br {
1207 display: none;
1208 }
1209
1210 p {
1211 margin: 12px 0 0 0;
1212 }
1213
1214 body#error-page a {
1215 margin-top: 16px;
1216 display: block;
1217 }
1218
1219 #error-page p.kubio-mrn-rollback-message>a {
1220 display: inline;
1221 }
1222 </style>
1223 <?php
1224 _kubio_requirements_not_met_notice();
1225 $content = ob_get_clean();
1226 wp_die( $content );
1227 }
1228
1229 if ( ! current_user_can( 'edit_theme_options' ) ) {
1230 wp_die(
1231 '<h1>' . __( 'You need a higher level of permission.', 'kubio' ) . '</h1>' .
1232 '<p>' . __( 'Sorry, you are not allowed to customize this site.', 'kubio' ) . '</p>',
1233 403
1234 );
1235 }
1236 }
1237 },
1238 5
1239 );
1240
1241 /**
1242 * Registers block editor 'kubio_section' post type.
1243 */
1244 function kubio_register_kubio_section_post_type() {
1245 if ( post_type_exists( 'kubio_section' ) ) {
1246 return;
1247 }
1248
1249 $args = array(
1250 'label' => __( 'Kubio Section', 'kubio' ),
1251 'public' => false,
1252 'show_ui' => false,
1253 'show_in_rest' => true,
1254 'show_in_menu' => false,
1255 'show_in_nav_menus' => false,
1256 'rewrite' => false,
1257 'exclude_from_search' => true,
1258 'publicly_queryable' => false,
1259 'rest_base' => 'kubio_section',
1260 'capabilities' => array(
1261 'read' => 'edit_theme_options',
1262 'create_posts' => 'edit_theme_options',
1263 'edit_posts' => 'edit_theme_options',
1264 'edit_published_posts' => 'edit_theme_options',
1265 'delete_published_posts' => 'edit_theme_options',
1266 'edit_others_posts' => 'edit_theme_options',
1267 'delete_others_posts' => 'edit_theme_options',
1268 ),
1269 'map_meta_cap' => true,
1270 'supports' => array(
1271 'title',
1272 'editor',
1273 'revisions',
1274 ),
1275 );
1276
1277 register_post_type( 'kubio_section', $args );
1278 }
1279
1280 add_action( 'init', 'kubio_register_kubio_section_post_type', 9 );
1281
1282
1283 /**
1284 * Registers block editor 'kubio_section' post type.
1285 */
1286 function kubio_register_kubio_favorites_post_type() {
1287 if ( post_type_exists( 'kubio_favorites' ) ) {
1288 return;
1289 }
1290
1291 $args = array(
1292 'label' => __( 'Kubio Favorites', 'kubio' ),
1293 'public' => false,
1294 'show_ui' => false,
1295 'show_in_rest' => true,
1296 'show_in_menu' => false,
1297 'show_in_nav_menus' => false,
1298 'rewrite' => false,
1299 'exclude_from_search' => true,
1300 'publicly_queryable' => false,
1301 'rest_base' => 'kubio_favorites',
1302 'capabilities' => array(
1303 'read' => 'edit_theme_options',
1304 'create_posts' => 'edit_theme_options',
1305 'edit_posts' => 'edit_theme_options',
1306 'edit_published_posts' => 'edit_theme_options',
1307 'delete_published_posts' => 'edit_theme_options',
1308 'edit_others_posts' => 'edit_theme_options',
1309 'delete_others_posts' => 'edit_theme_options',
1310 ),
1311 'map_meta_cap' => true,
1312 'supports' => array(
1313 'title',
1314 'editor',
1315 'revisions',
1316 ),
1317 'can_export' => false,
1318 );
1319
1320 register_post_type( 'kubio_favorites', $args );
1321 }
1322
1323 add_action( 'init', 'kubio_register_kubio_favorites_post_type', 9 );
1324