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