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