PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.2.0
Kubio AI Page Builder v2.2.0
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 / src / Core / Activation.php
kubio / lib / src / Core Last commit date
Background 2 years ago Blocks 3 years ago GlobalElements 3 years ago Layout 4 years ago License 2 years ago Separators 4 years ago StyleManager 2 years ago Styles 4 years ago Activation.php 2 years ago Backup.php 4 years ago CustomizerImporter.php 3 years ago Deactivation.php 4 years ago EditInKubioCustomizerPanel.php 4 years ago Element.php 2 years ago ElementBase.php 4 years ago Importer.php 2 years ago InnerBlocks.php 4 years ago LodashBasic.php 2 years ago Registry.php 3 years ago Utils.php 2 years ago
Activation.php
720 lines
1 <?php
2
3 namespace Kubio\Core;
4
5 use IlluminateAgnostic\Arr\Support\Arr;
6 use Kubio\Flags;
7
8 class Activation {
9
10
11 private static $instance = null;
12
13 private $remote_content = array();
14
15
16 public function __construct() {
17 add_action(
18 'activated_plugin',
19 function ( $plugin ) {
20
21 if ( $plugin === plugin_basename( KUBIO_ENTRY_FILE ) ) {
22
23 $hash = uniqid( 'activate-' );
24 Flags::set( 'activation-hash', $hash );
25
26 $url = add_query_arg(
27 array(
28 'page' => 'kubio-get-started',
29 'kubio-activation-hash' => $hash,
30 ),
31 admin_url( 'admin.php' )
32 );
33
34 if ( ! $this->isCLI() && ! Arr::has( $_REQUEST, 'tgmpa-activate' ) ) {
35 wp_redirect( $url );
36 exit();
37 } else {
38
39 if ( Arr::has( $_REQUEST, 'tgmpa-activate' ) ) {
40 Flags::set( 'activated_from_tgmpa', true );
41 }
42
43 Flags::set( 'import_design', false );
44 Flags::set( 'start_with_ai', false );
45
46 if ( $this->isCLI() ) {
47 add_filter( 'user_has_cap', array( Importer::class, 'allowImportCaps' ), 10, 2 );
48 $this->activate();
49 }
50 }
51 }
52 }
53 );
54
55 $self = $this;
56
57 // handle direct tgmpa activation
58 add_action(
59 'init',
60 function () {
61
62 if ( ! is_admin() ) {
63 return;
64 }
65
66 if ( Flags::get( 'activated_from_tgmpa' ) ) {
67 Flags::delete( 'activated_from_tgmpa' );
68
69 $hash = uniqid( 'activate-' );
70 Flags::set( 'activation-hash', $hash );
71 $url = add_query_arg(
72 array(
73 'page' => 'kubio-get-started',
74 'kubio-activation-hash' => $hash,
75 ),
76 admin_url( 'admin.php' )
77 );
78
79 wp_redirect( $url );
80 exit();
81 }
82 },
83 5
84 );
85
86 add_action(
87 'init',
88 function () use ( $self ) {
89
90 if ( ! is_admin() ) {
91 return;
92 }
93
94 $hash = sanitize_text_field( Arr::get( $_REQUEST, 'kubio-activation-hash', null ) );
95 $saved_hash = Flags::get( 'activation-hash', false );
96 if ( $saved_hash === $hash ) {
97 Flags::delete( 'activation-hash' );
98 $self->activate();
99 }
100 },
101 500
102 );
103
104 add_action( 'after_switch_theme', array( $this, 'afterSwitchTheme' ) );
105 }
106
107 public function isCLI() {
108 return defined( 'WP_CLI' ) && WP_CLI;
109 }
110
111 public function startWithAI() {
112 return Flags::get( 'start_with_ai', false ) !== false;
113 }
114
115 public function activeWithFrontpage() {
116
117 if ( $this->startWithAI() ) {
118 return true;
119 }
120
121 return apply_filters( 'kubio/activation/activate_with_frontpage', Flags::get( 'import_design', false ) !== false );
122 }
123
124 public function importUnmodifiedTemplates() {
125 return ! CustomizerImporter::themeHasModifiedOptions();
126 }
127
128 public function importCustomizedTemplates() {
129 return CustomizerImporter::themeHasModifiedOptions();
130 }
131
132
133 private function shouldRestoreDeactivationBackup( Backup $backup ) {
134 $template = get_stylesheet();
135 $identifier = Flags::getSetting( "deactivation_backup_key.{$template}", null );
136 return $identifier && $backup->hasBackup( $identifier );
137 }
138
139 private function restoreDeactivationBackup( Backup $backup ) {
140 $template = get_stylesheet();
141 $identifier = Flags::getSetting( "deactivation_backup_key.{$template}", null );
142 $status = $backup->restoreBackup( $identifier );
143
144 if ( ! is_wp_error( $status ) ) {
145 $backup->deleteBackup( $identifier );
146 }
147 Flags::delete( $identifier );
148 }
149
150
151 public function activate() {
152 $backup = new Backup();
153
154 if ( $this->shouldRestoreDeactivationBackup( $backup ) ) {
155 $this->restoreDeactivationBackup( $backup );
156 return;
157 }
158
159 // activate pro
160 if ( kubio_is_pro() && ! Flags::get( 'kubio_pro_activation_time', false ) ) {
161 Flags::set( 'kubio_pro_activation_time', time() );
162 }
163
164 // if free previously activated return
165 if ( Flags::get( 'kubio_activation_time', false ) ) {
166 $stylesheet = Flags::get( 'stylesheet', null );
167 if ( $stylesheet === get_stylesheet() ) {
168 return;
169 }
170 }
171
172 Flags::set( 'kubio_activation_time', time() );
173 Flags::set( 'stylesheet', get_stylesheet() );
174
175 $this->addCommonFilters();
176 $this->prepareRemoteData();
177
178 add_filter( 'kubio/importer/page_path', array( $this, 'getDesignPagePath' ), 10, 2 );
179
180 if ( $this->importCustomizedTemplates() ) {
181 add_filter( 'kubio/importer/content', array( $this, 'importCustomizerOptions' ), 20, 3 );
182 }
183
184 wp_cache_flush();
185 $this->importDesign();
186 $this->importTemplates();
187 $this->importTemplateParts();
188 wp_cache_flush();
189 do_action( 'kubio/after_activation' );
190
191 if ( ! $this->isCLI() ) {
192
193 // make an educated guess about the start source if not set
194 if ( ! Flags::get( 'start_source', false ) ) {
195 $start_source = 'other';
196 if ( $this->startWithAI() ) {
197 $start_source = 'notice-ai';
198 } elseif ( $this->activeWithFrontpage() ) {
199 $start_source = 'notice-homepage';
200 }
201 Flags::set( 'start_source', $start_source );
202 }
203
204 if ( $this->startWithAI() ) {
205 Flags::set( 'start_with_ai', false );
206 $ai_hash = md5( uniqid( 'start-with-ai' ) );
207 Flags::set( 'start_with_ai_hash', $ai_hash );
208 wp_redirect(
209 Utils::kubioGetEditorURL(
210 array(
211 'ai' => $ai_hash,
212 )
213 )
214 );
215 exit();
216 }
217
218 $is_unmodified_supported_theme = kubio_theme_has_kubio_block_support() && ! CustomizerImporter::themeHasModifiedOptions();
219 if ( get_option( 'fresh_site' ) || $is_unmodified_supported_theme ) {
220 if ( $this->activeWithFrontpage() ) {
221 wp_redirect(
222 Utils::kubioGetEditorURL()
223 );
224 exit();
225 }
226
227 $url = add_query_arg(
228 array(
229 'page' => 'kubio-get-started',
230 'tab' => 'website-starter',
231 ),
232 admin_url( 'admin.php' )
233 );
234
235 wp_redirect( $url );
236 exit();
237 }
238 }
239
240 }
241
242 public function addCommonFilters() {
243 add_filter( 'kubio/importer/skip-remote-file-import', '__return_true' );
244 add_filter( 'kubio/importer/content', array( $this, 'getFileContent' ), 1, 3 );
245 add_filter( 'kubio/importer/content', array( $this, 'templateMapPartsTheme' ), 10, 2 );
246 add_filter( 'kubio/importer/content', array( $this, 'updateBlocks' ), 10, 3 );
247 remove_filter( 'theme_mod_nav_menu_locations', 'kubio_nav_menu_locations_from_global_data' );
248 remove_filter( 'wp_insert_post_data', 'kubio_on_post_update', 10, 3 );
249 remove_action( 'wp_insert_post', 'kubio_update_meta', 10, 3 );
250
251 add_filter( 'kubio/importer/available_templates', array( $this, 'getAvailableTemplates' ), 10 );
252 add_filter( 'kubio/importer/available_template_parts', array( $this, 'getAvailableTemplateParts' ), 10 );
253 }
254
255 public function prepareRemoteData() {
256 if ( ! \kubio_theme_has_kubio_block_support() ) {
257 return;
258 }
259
260 $with_front_page = apply_filters( 'kubio/importer/with_front_page', $this->importUnmodifiedTemplates() );
261
262 $base_url = 'https://themes.kubiobuilder.com';
263 $file_name = get_stylesheet() . '__' . get_template() . '__' . ( $with_front_page ? 'with-front' : 'default' ) . '.data';
264
265 $url = apply_filters( 'kubio/remote_data_url', "{$base_url}/{$file_name}" );
266 $response = wp_safe_remote_get( $url );
267
268 if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200 /* && Utils::isProduction() */ ) {
269 $content = wp_remote_retrieve_body( $response );
270 $data = unserialize( $content );
271
272 if ( ! is_array( $data ) || Arr::get( $data, 'error' ) ) {
273 return;
274 }
275
276 $this->remote_content = $data;
277 } else {
278 $content = file_get_contents( KUBIO_ROOT_DIR . '/defaults/default-site.dat' );
279 $this->remote_content = unserialize( $content );
280 }
281
282 $global_data = Arr::get( $this->remote_content, 'global-data' );
283 if ( $global_data ) {
284 $global_data = json_decode( $global_data, true );
285
286 if ( json_last_error() === JSON_ERROR_NONE ) {
287 Arr::forget( $global_data, 'menuLocations' );
288 kubio_replace_global_data_content( $global_data );
289 }
290 }
291
292 $theme = Arr::get( $this->remote_content, 'theme' );
293
294 // if the child theme does not exists use the theme name for assets
295 if ( $theme && $theme !== get_stylesheet() ) {
296 add_filter(
297 'kubio/importer/kubio-url-placeholder-replacement',
298 function () use ( $theme ) {
299
300 return "https://static-assets.kubiobuilder.com/themes/{$theme}/assets/";
301 },
302 10
303 );
304 }
305 }
306
307 public function importDesign() {
308 if ( $this->isCLI() ) {
309 return;
310 }
311
312 $result = $this->setPages();
313
314 // try to set the blog page and menu
315 if ( ! is_wp_error( $result ) ) {
316 static::preparePrimaryMenu();
317 } else {
318 // only set menu location
319 static::preparePrimaryMenu( false );
320 }
321 }
322
323 private function setPages( $data = array() ) {
324
325 if ( ! kubio_theme_has_kubio_block_support() ) {
326 return new \WP_Error( 'not_supported_themes' );
327 }
328
329 $data = array_merge(
330 array(
331 'front_content' => null,
332 'with_blog_page' => true,
333 ),
334 $data
335 );
336
337 $front_page_id = $this->importFrontPage();
338
339 if ( is_wp_error( $front_page_id ) ) {
340 return $front_page_id;
341 }
342
343 update_option( 'show_on_front', 'page' );
344 update_option( 'page_on_front', $front_page_id );
345
346 $posts_page_id = intval( get_option( 'page_for_posts' ) );
347
348 if ( ! $posts_page_id && $data['with_blog_page'] ) {
349 $posts_page_id = wp_insert_post(
350 array(
351 'comment_status' => 'closed',
352 'ping_status' => 'closed',
353 'post_name' => 'blog',
354 'post_title' => __( 'Blog', 'kubio' ),
355 'post_status' => 'publish',
356 'post_type' => 'page',
357 'page_template' => apply_filters(
358 'kubio/front_page_template',
359 'page-templates/homepage.php'
360 ),
361 'post_content' => '',
362 'meta_input' => array(
363 '_kubio_created_at_activation' => 1,
364 ),
365 )
366 );
367
368 if ( ! is_wp_error( $posts_page_id ) ) {
369 update_option( 'page_for_posts', $posts_page_id );
370 }
371 }
372
373 return $posts_page_id;
374 }
375
376 /**
377 *
378 * @return int|WP_Error
379 */
380 private function importFrontPage() {
381 $page_on_front = get_option( 'page_on_front' );
382 $query = new \WP_Query(
383 array(
384 'post__in' => array( $page_on_front ),
385 'post_status' => array( 'publish' ),
386 'fields' => 'ids',
387 'post_type' => 'page',
388 )
389 );
390
391 $content = '';
392
393 if ( $this->activeWithFrontpage() ) {
394 $content = Importer::getTemplateContent( 'page', 'front-page' );
395 }
396
397 if ( $query->have_posts() ) {
398 if ( apply_filters( 'kubio/activation/override_front_page_content', false ) ) {
399 wp_update_post(
400 array(
401 'ID' => intval( $page_on_front ),
402 'post_content' => wp_slash( kubio_serialize_blocks( parse_blocks( $content ) ) ),
403 )
404 );
405 }
406
407 return intval( $page_on_front );
408 }
409
410 if ( ! is_string( $content ) ) {
411 $content = '';
412 }
413
414 return wp_insert_post(
415 array(
416 'comment_status' => 'closed',
417 'ping_status' => 'closed',
418 'post_name' => 'front_page',
419 'post_title' => __( 'Home', 'kubio' ),
420 'post_status' => 'publish',
421 'post_type' => 'page',
422 'page_template' => apply_filters(
423 'kubio/front_page_template',
424 'kubio-full-width'
425 ),
426 'post_content' => wp_slash( kubio_serialize_blocks( parse_blocks( $content ) ) ),
427 'meta_input' => array(
428 '_kubio_created_at_activation' => 1,
429 ),
430 )
431 );
432 }
433
434 public static function preparePrimaryMenu( $create_menu_items = true ) {
435 $theme_menu_locations = array_keys( get_registered_nav_menus() );
436 $common_header_locations = array(
437 'header-menu',
438 'header',
439 'primary',
440 'main',
441 'menu-1',
442 );
443
444 $selected_location = null;
445 /**
446 * Try to make an educated guess and primary menu location
447 */
448 foreach ( $theme_menu_locations as $location ) {
449 foreach ( $common_header_locations as $common_header_location ) {
450 if ( stripos( $location, $common_header_location ) !== false ) {
451 $selected_location = $location;
452 break;
453 }
454 }
455
456 if ( $selected_location ) {
457 break;
458 }
459 }
460
461 $selected_location = apply_filters( 'kubio/primary_menu_location', $selected_location );
462
463 if ( $selected_location ) {
464
465 $current_set_locations = get_nav_menu_locations();
466
467 $primary_menu_id = Arr::get( $current_set_locations, $selected_location, null );
468
469 if ( $create_menu_items ) {
470
471 if ( ! $primary_menu_id ) {
472 $primary_menu_id = wp_create_nav_menu( __( 'Primary menu', 'kubio' ) );
473 }
474
475 if ( is_wp_error( $primary_menu_id ) ) {
476 return;
477 }
478
479 $menu_items = wp_get_nav_menu_items( $primary_menu_id );
480 $has_front_page = false;
481 $has_blog_page = false;
482
483 foreach ( $menu_items as $menu_item ) {
484
485 if ( ! $has_front_page ) {
486 $menu_item_object_is_front_page = $menu_item->type === 'post_type' && $menu_item->object === 'page' && intval( $menu_item->object_id ) === intval( get_option( 'page_on_front' ) );
487 $custom_url = $menu_item->type === 'custom' ? $menu_item->url : null;
488 $menu_item_link_is_front_page = false;
489 $parsed_url = parse_url( $custom_url );
490
491 if ( $parsed_url && $custom_url ) {
492 $site_url = site_url();
493
494 $parsed_url = array_merge(
495 array(
496 'scheme' => '',
497 'host' => '',
498 'path' => '',
499 ),
500 $parsed_url
501 );
502
503 $menu_item_url = "{$parsed_url['scheme']}://{$parsed_url['host']}{$parsed_url['path']}";
504 $menu_item_link_is_front_page = untrailingslashit( $menu_item_url ) === untrailingslashit( $site_url );
505 }
506
507 if ( $menu_item_object_is_front_page || $menu_item_link_is_front_page ) {
508 $has_front_page = true;
509 }
510 }
511
512 if ( $menu_item->type === 'post_type' && $menu_item->object === 'page' && intval( $menu_item->object_id ) === intval( get_option( 'page_for_posts' ) ) ) {
513 $has_blog_page = true;
514 }
515 }
516
517 if ( ! $has_front_page ) {
518 wp_update_nav_menu_item(
519 $primary_menu_id,
520 0,
521 array(
522 'menu-item-title' => __( 'Home', 'kubio' ),
523 'menu-item-object' => 'page',
524 'menu-item-object-id' => get_option( 'page_on_front' ),
525 'menu-item-type' => 'post_type',
526 'menu-item-status' => 'publish',
527 )
528 );
529 }
530
531 if ( ! $has_blog_page && get_option( 'page_for_posts', 0 ) ) {
532 wp_update_nav_menu_item(
533 $primary_menu_id,
534 0,
535 array(
536 'menu-item-title' => __( 'Blog', 'kubio' ),
537 'menu-item-object' => 'page',
538 'menu-item-object-id' => get_option( 'page_for_posts' ),
539 'menu-item-type' => 'post_type',
540 'menu-item-status' => 'publish',
541 )
542 );
543 }
544 }
545
546 if ( ! $primary_menu_id ) {
547 return;
548 }
549
550 $next_nav_menu_locations = array_merge(
551 $current_set_locations,
552 array(
553 $selected_location => $primary_menu_id,
554 )
555 );
556
557 if ( ! isset( $next_nav_menu_locations['header-menu'] ) ) {
558 $next_nav_menu_locations['header-menu'] = $primary_menu_id;
559 }
560
561 set_theme_mod( 'nav_menu_locations', $next_nav_menu_locations );
562 }
563 }
564
565 public function importTemplates() {
566 $entities = array_keys( Importer::getAvailableTemplates() );
567
568 foreach ( $entities as $slug ) {
569 $is_current_kubio_template = apply_filters( 'kubio/template/is_importing_kubio_template', kubio_theme_has_kubio_block_support(), $slug );
570 Importer::createTemplate( $slug, Importer::getTemplateContent( 'wp_template', $slug ), false, $is_current_kubio_template ? 'kubio' : 'theme' );
571 }
572
573 Flags::set( 'kubio_templates_imported', time() );
574 }
575
576 public function importTemplateParts() {
577 $entities = array_keys( Importer::getAvailableTemplateParts() );
578
579 foreach ( $entities as $slug ) {
580 $is_current_kubio_template = apply_filters( 'kubio/template/is_importing_kubio_template', kubio_theme_has_kubio_block_support(), $slug );
581 Importer::createTemplatePart( $slug, Importer::getTemplateContent( 'wp_template_part', $slug ), false, $is_current_kubio_template ? 'kubio' : 'theme' );
582 }
583
584 Flags::set( 'kubio_template_parts_imported', time() );
585 }
586
587 public static function load() {
588 if ( ! self::$instance ) {
589 self::$instance = new self();
590 }
591
592 return self::$instance;
593 }
594
595 public static function skipAfterSwitchTheme() {
596 set_transient( 'kubio_skip_after_theme_switch', true );
597 }
598
599 public function afterSwitchTheme() {
600 $skip = get_transient( 'kubio_skip_after_theme_switch' );
601
602 if ( $skip ) {
603 delete_transient( 'kubio_skip_after_theme_switch' );
604 return;
605 }
606
607 $this->addCommonFilters();
608
609 add_filter( 'kubio/importer/page_path', array( $this, 'getDesignPagePath' ), 10, 2 );
610 $this->prepareRemoteData( true );
611
612 $this->importDesign();
613
614 $this->importTemplates();
615 $this->importTemplateParts();
616
617 do_action( 'kubio/after_switch_theme' );
618 }
619
620 public function getAvailableTemplates( $current_templates = array() ) {
621
622 $templates = $current_templates;
623
624 if ( kubio_theme_has_kubio_block_support() ) {
625 $templates = Arr::get( $this->remote_content, 'block-templates', array() );
626
627 foreach ( array_keys( $templates ) as $template ) {
628 $templates[ $template ] = null;
629 }
630
631 $templates = array_replace( $templates, $templates );
632 }
633
634 return $templates;
635 }
636
637 public function getAvailableTemplateParts( $current_parts = array() ) {
638
639 $templates = $current_parts;
640
641 if ( kubio_theme_has_kubio_block_support() ) {
642 $templates = Arr::get( $this->remote_content, 'block-template-parts', array() );
643
644 foreach ( array_keys( $templates ) as $template ) {
645 $templates[ $template ] = null;
646 }
647
648 $templates = array_replace( $templates, $templates );
649 }
650
651 return $templates;
652 }
653
654 public function getDesignPagePath( $path, $slug ) {
655 if ( $slug === 'front-page' ) {
656 return null;
657 }
658
659 return $path;
660 }
661
662 public function updateBlocks( $content, $type, $slug ) {
663 $blocks = parse_blocks( $content );
664 $blocks = Importer::maybeImportBlockAssets( $blocks );
665
666 if ( $type === 'wp_template_part' && strpos( $slug, 'footer' ) !== false ) {
667 $blocks = Importer::setBlocksLocks( $blocks, null );
668 }
669
670 return kubio_serialize_blocks( $blocks );
671 }
672
673
674
675 public function getFileContent( $content, $type, $slug ) {
676
677 if ( $content !== null ) {
678 return $content;
679 }
680
681 $category = '';
682 switch ( $type ) {
683 case 'wp_template':
684 $category = 'block-templates';
685 break;
686 case 'wp_template_part':
687 $category = 'block-template-parts';
688 break;
689 case 'page':
690 $category = 'pages';
691 break;
692 }
693
694 return Arr::get( $this->remote_content, "{$category}.{$slug}", '' );
695 }
696
697 public function templateMapPartsTheme( $content, $type ) {
698
699 if ( $type === 'wp_template' || $type === 'wp_template_part' ) {
700 $blocks = parse_blocks( $content );
701 $updated_blocks = kubio_blocks_update_template_parts_theme( $blocks, get_stylesheet() );
702
703 return kubio_serialize_blocks( $updated_blocks );
704 }
705
706 return $content;
707 }
708
709 public function importCustomizerOptions( $content, $type, $slug ) {
710
711 if ( ! kubio_theme_has_kubio_block_support() ) {
712 return $content;
713 }
714
715 $customizer_importer = new CustomizerImporter( $content, $type, $slug );
716
717 return $customizer_importer->process();
718 }
719 }
720