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