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