PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 1.4.3
Kubio AI Page Builder v1.4.3
2.9.1 2.9.0 2.8.6 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
607 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_stylesheet();
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_stylesheet();
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 $url = apply_filters( 'kubio/remote_data_url', "{$base_url}/{$file_name }" );
184 $response = wp_safe_remote_get( $url );
185
186 if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200 /* && Utils::isProduction() */ ) {
187 $content = wp_remote_retrieve_body( $response );
188 $data = unserialize( $content );
189
190 if ( ! is_array( $data ) || Arr::get( $data, 'error' ) ) {
191 return;
192 }
193
194 $this->remote_content = $data;
195
196 } else {
197 $content = file_get_contents( KUBIO_ROOT_DIR . '/defaults/default-site.dat' );
198 $this->remote_content = unserialize( $content );
199 }
200
201 if ( $global_data = Arr::get( $this->remote_content, 'global-data' ) ) {
202 $global_data = json_decode( $global_data, true );
203
204 if ( json_last_error() === JSON_ERROR_NONE ) {
205 Arr::forget( $global_data, 'menuLocations' );
206 kubio_replace_global_data_content( $global_data );
207 }
208 }
209
210 $theme = Arr::get( $this->remote_content, 'theme' );
211
212 // if the child theme does not exists use the theme name for assets
213 if ( $theme && $theme !== get_stylesheet() ) {
214 add_filter(
215 'kubio/importer/kubio-url-placeholder-replacement',
216 function () use ( $theme ) {
217
218 return "https://static-assets.kubiobuilder.com/themes/{$theme}/assets/";
219 },
220 10
221 );
222 }
223
224 }
225
226 public function importDesign() {
227 $result = $this->setPages();
228
229 // try to set the blog page and menu
230 if ( ! is_wp_error( $result ) ) {
231 $this->preparePrimaryMenu();
232 }
233
234 }
235
236 private function setPages( $data = array() ) {
237
238 if ( ! kubio_theme_has_kubio_block_support() ) {
239 return new \WP_Error( 'not_supporterd_themes' );
240 }
241
242 $data = array_merge(
243 array(
244 'front_content' => null,
245 'with_blog_page' => true,
246 ),
247 $data
248 );
249
250 $front_page_id = $this->importFrontPage();
251
252 if ( is_wp_error( $front_page_id ) ) {
253 return $front_page_id;
254 }
255
256 update_option( 'show_on_front', 'page' );
257 update_option( 'page_on_front', $front_page_id );
258
259 $posts_page_id = intval( get_option( 'page_for_posts' ) );
260
261 if ( ! $posts_page_id && $data['with_blog_page'] ) {
262 $posts_page_id = wp_insert_post(
263 array(
264 'comment_status' => 'closed',
265 'ping_status' => 'closed',
266 'post_name' => 'blog',
267 'post_title' => __( 'Blog', 'kubio' ),
268 'post_status' => 'publish',
269 'post_type' => 'page',
270 'page_template' => apply_filters(
271 'kubio/front_page_template',
272 'page-templates/homepage.php'
273 ),
274 'post_content' => '',
275 )
276 );
277
278 if ( ! is_wp_error( $posts_page_id ) ) {
279 update_option( 'page_for_posts', $posts_page_id );
280 }
281 }
282
283 return $posts_page_id;
284 }
285
286 /**
287 *
288 * @return int|WP_Error
289 */
290 private function importFrontPage() {
291
292 $page_on_front = get_option( 'page_on_front' );
293 $query = new \WP_Query(
294 array(
295 'post__in' => array( $page_on_front ),
296 'post_status' => array( 'publish' ),
297 'fields' => 'ids',
298 'post_type' => 'page',
299 )
300 );
301
302 if ( $query->have_posts() ) {
303 return intval( $page_on_front );
304 }
305
306 $content = '';
307
308 if ( $this->activeWithFrontpage() ) {
309 $content = Importer::getTemplateContent( 'page', 'front-page' );
310 }
311
312 if ( ! is_string( $content ) ) {
313 $content = '';
314 }
315
316 return wp_insert_post(
317 array(
318 'comment_status' => 'closed',
319 'ping_status' => 'closed',
320 'post_name' => 'front_page',
321 'post_title' => __( 'Front Page', 'kubio' ),
322 'post_status' => 'publish',
323 'post_type' => 'page',
324 'page_template' => apply_filters(
325 'kubio/front_page_template',
326 'kubio-full-width'
327 ),
328 'post_content' => wp_slash( kubio_serialize_blocks( parse_blocks( $content ) ) ),
329 )
330 );
331
332 }
333
334 private function preparePrimaryMenu() {
335 $theme_menu_locations = array_keys( get_registered_nav_menus() );
336 $common_header_locations = array(
337 'header-menu',
338 'header',
339 'primary',
340 'main',
341 'menu-1',
342 );
343
344 $selected_location = null;
345 /**
346 * Try to make an educated guess and primary menu location
347 */
348 foreach ( $theme_menu_locations as $location ) {
349 foreach ( $common_header_locations as $common_header_location ) {
350 if ( stripos( $location, $common_header_location ) !== false ) {
351 $selected_location = $location;
352 break;
353 }
354 }
355
356 if ( $selected_location ) {
357 break;
358 }
359 }
360
361 $selected_location = apply_filters( 'kubio/primary_menu_location', $selected_location );
362
363 if ( $selected_location ) {
364
365 $current_set_locations = get_nav_menu_locations();
366
367 $primary_menu_id = Arr::get( $current_set_locations, $selected_location, null );
368
369 if ( ! $primary_menu_id ) {
370 $primary_menu_id = wp_create_nav_menu( __( 'Primary menu', 'kubio' ) );
371 }
372
373 if ( is_wp_error( $primary_menu_id ) ) {
374 return;
375 }
376
377 $menu_items = wp_get_nav_menu_items( $primary_menu_id );
378 $has_front_page = false;
379 $has_blog_page = false;
380
381 foreach ( $menu_items as $menu_item ) {
382
383 if ( ! $has_front_page ) {
384 $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' ) );
385 $custom_url = $menu_item->type === 'custom' ? $menu_item->url : null;
386 $menu_item_link_is_front_page = false;
387 $parsed_url = parse_url( $custom_url );
388
389 if ( $parsed_url && $custom_url ) {
390 $site_url = site_url();
391
392 $parsed_url = array_merge(
393 array(
394 'scheme' => '',
395 'host' => '',
396 'path' => '',
397 ),
398 $parsed_url
399 );
400
401 $menu_item_url = "{$parsed_url['scheme']}://{$parsed_url['host']}{$parsed_url['path']}";
402 $menu_item_link_is_front_page = untrailingslashit( $menu_item_url ) === untrailingslashit( $site_url );
403 }
404
405 if ( $menu_item_object_is_front_page || $menu_item_link_is_front_page ) {
406 $has_front_page = true;
407 }
408 }
409
410 if ( $menu_item->type === 'post_type' && $menu_item->object === 'page' && intval( $menu_item->object_id ) === intval( get_option( 'page_for_posts' ) ) ) {
411 $has_blog_page = true;
412 }
413 }
414
415 if ( ! $has_front_page ) {
416 wp_update_nav_menu_item(
417 $primary_menu_id,
418 0,
419 array(
420 'menu-item-title' => __( 'Front Page', 'kubio' ),
421 'menu-item-object' => 'page',
422 'menu-item-object-id' => get_option( 'page_on_front' ),
423 'menu-item-type' => 'post_type',
424 'menu-item-status' => 'publish',
425 )
426 );
427 }
428
429 if ( ! $has_blog_page && get_option( 'page_for_posts', 0 ) ) {
430 wp_update_nav_menu_item(
431 $primary_menu_id,
432 0,
433 array(
434 'menu-item-title' => __( 'Blog', 'kubio' ),
435 'menu-item-object' => 'page',
436 'menu-item-object-id' => get_option( 'page_for_posts' ),
437 'menu-item-type' => 'post_type',
438 'menu-item-status' => 'publish',
439 )
440 );
441 }
442
443 set_theme_mod(
444 'nav_menu_locations',
445 array_merge(
446 $current_set_locations,
447 array(
448 $selected_location => $primary_menu_id,
449 )
450 )
451 );
452 }
453 }
454
455 private function importTemplates() {
456 $entities = array_keys( Importer::getAvailableTemplates() );
457
458 foreach ( $entities as $slug ) {
459 $is_current_kubio_template = apply_filters( 'kubio/template/is_importing_kubio_template', kubio_theme_has_kubio_block_support(), $slug );
460 Importer::createTemplate( $slug, Importer::getTemplateContent( 'wp_template', $slug ), false, $is_current_kubio_template ? 'kubio' : 'theme' );
461 }
462
463 Flags::set( 'kubio_templates_imported', time() );
464 }
465
466 private function importTemplateParts() {
467 $entities = array_keys( Importer::getAvailableTemplateParts() );
468
469 foreach ( $entities as $slug ) {
470 $is_current_kubio_template = apply_filters( 'kubio/template/is_importing_kubio_template', kubio_theme_has_kubio_block_support(), $slug );
471 Importer::createTemplatePart( $slug, Importer::getTemplateContent( 'wp_template_part', $slug ), false, $is_current_kubio_template ? 'kubio' : 'theme' );
472 }
473
474 Flags::set( 'kubio_template_parts_imported', time() );
475 }
476
477 public static function load() {
478 if ( ! self::$instance ) {
479 self::$instance = new self();
480 }
481
482 return self::$instance;
483 }
484
485 public static function skipAfterSwitchTheme() {
486 set_transient( 'kubio_skip_after_theme_switch', true );
487 }
488
489 public function afterSwitchTheme() {
490
491 $skip = get_transient( 'kubio_skip_after_theme_switch' );
492
493 if ( $skip ) {
494 delete_transient( 'kubio_skip_after_theme_switch' );
495 return;
496 }
497
498 $this->addCommonFilters();
499
500 add_filter( 'kubio/importer/page_path', array( $this, 'getDesignPagePath' ), 10, 2 );
501 $this->prepareRemoteData( true );
502
503 $this->importDesign();
504
505 $this->importTemplates();
506 $this->importTemplateParts();
507 do_action( 'kubio/after_switch_theme' );
508 }
509
510 public function getAvailableTemplates( $current_templates = array() ) {
511
512 $templates = $current_templates;
513
514 if ( kubio_theme_has_kubio_block_support() ) {
515 $templates = Arr::get( $this->remote_content, 'block-templates', array() );
516
517 foreach ( array_keys( $templates ) as $template ) {
518 $templates[ $template ] = null;
519 }
520
521 $templates = array_replace( $templates, $templates );
522 }
523
524 return $templates;
525 }
526
527 public function getAvailableTemplateParts( $current_parts = array() ) {
528
529 $templates = $current_parts;
530
531 if ( kubio_theme_has_kubio_block_support() ) {
532 $templates = Arr::get( $this->remote_content, 'block-template-parts', array() );
533
534 foreach ( array_keys( $templates ) as $template ) {
535 $templates[ $template ] = null;
536 }
537
538 $templates = array_replace( $templates, $templates );
539
540 }
541
542 return $templates;
543 }
544
545 public function getDesignPagePath( $path, $slug ) {
546 if ( $slug === 'front-page' ) {
547 return null;
548 }
549
550 return $path;
551 }
552
553 public function importAssets( $content ) {
554 $blocks = parse_blocks( $content );
555 $blocks = Importer::maybeImportBlockAssets( $blocks );
556
557 return kubio_serialize_blocks( $blocks );
558 }
559
560 public function getFileContent( $content, $type, $slug ) {
561
562 if ( $content !== null ) {
563 return $content;
564 }
565
566 $category = '';
567 switch ( $type ) {
568 case 'wp_template':
569 $category = 'block-templates';
570 break;
571 case 'wp_template_part':
572 $category = 'block-template-parts';
573 break;
574 case 'page':
575 $category = 'pages';
576 break;
577 }
578
579 return Arr::get( $this->remote_content, "{$category}.{$slug}", '' );
580 }
581
582 public function templateMapPartsTheme( $content, $type ) {
583
584 if ( $type === 'wp_template' || $type === 'wp_template_part' ) {
585 $blocks = parse_blocks( $content );
586 $updated_blocks = kubio_blocks_update_template_parts_theme( $blocks, get_stylesheet() );
587
588 return kubio_serialize_blocks( $updated_blocks );
589 }
590
591 return $content;
592 }
593
594 public function importCustomizerOptions( $content, $type, $slug ) {
595
596 if ( ! kubio_theme_has_kubio_block_support() ) {
597 return $content;
598 }
599
600 $customizer_importer = new CustomizerImporter( $content, $type, $slug );
601
602 return $customizer_importer->process();
603 }
604
605
606 }
607