PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.8.6
Kubio AI Page Builder v2.8.6
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 / KubioFrontPageRevertNotice.php
kubio / lib / src / Core Last commit date
Background 1 year ago Blocks 1 year ago GlobalElements 1 year ago Layout 1 year ago Separators 1 year ago StyleManager 2 months ago Styles 1 year ago Activation.php 1 year ago Backup.php 1 year ago CustomizerImporter.php 1 year ago Deactivation.php 1 year ago EditInKubioCustomizerPanel.php 1 year ago Element.php 1 year ago ElementBase.php 4 years ago Importer.php 2 months ago InnerBlocks.php 1 year ago KubioFrontPageRevertNotice.php 6 days ago LodashBasic.php 1 year ago Registry.php 1 year ago ThirdPartyPluginAssetLoaderInEditor.php 4 months ago Utils.php 6 days ago
KubioFrontPageRevertNotice.php
996 lines
1 <?php
2
3 namespace Kubio\Core;
4
5 use Kubio\Flags;
6
7
8 class KubioFrontPageRevertNotice
9 {
10 protected static $instance = null;
11 public static $nonceKey = 'kubioFrontPageRevertNoticeNonce';
12 public static $showNoticeFlagKey = 'showKubioFrontPageRevertNotice';
13 public static $frontPageRevertedKey = 'kubioFrontPageReverted';
14 public static $frontPageBackupKey = 'KubioFrontPageRevertNoticeFrontPageBackup';
15 public static $menuBackupKey = 'kubioFrontPageRevertNoticeMenuBackup';
16 public static $templatePartsBackupKey = 'kubioFrontPageRevertNoticeTemplatePartsBackup';
17 public static $frontpageIsFromStarterContent = 'kubioFrontPageRevertFrontPageIsFromStarterContent';
18 public static $globalDataBackupKey = 'kubioFrontPageRevertedGlobalDataBackup';
19
20 protected function __construct()
21 {
22 add_action('wp_enqueue_scripts', array($this, 'onPrintNoticeAssetsWithCheck'));
23 add_action('wp_footer', array($this, 'onPrintNoticeWithCheck'));
24 add_action('wp_ajax_kubio_front_page_revert_action', array($this, 'onKeepKubioFrontPage'));
25 add_action('wp_ajax_kubio_restore_front_page', array($this, 'onRestoreUserFrontPage'));
26 add_action('rest_api_init', array($this, 'initRestApi'));
27 add_filter('kubio/kubio-utils-data/extras', array($this, 'addKubioUtilsData'));
28 }
29
30
31 public function addKubioUtilsData($data) {
32 if(!$this->getCurrentUserIsAdmin()) {
33 return $data;
34 }
35 $data['frontPageRevertNoticeNonce'] =wp_create_nonce( KubioFrontPageRevertNotice::$nonceKey );
36 return $data;
37 }
38
39 public function getFeatureIsEnabled() {
40 return apply_filters( 'kubio/front_page_revert_notice_is_enabled', false );
41 }
42
43 public function getThemeHasStarterContentKeySet() {
44 $stylesheet = get_stylesheet();
45 $value = Flags::get( "with_starter_content_$stylesheet", false );
46 return $value;
47 }
48
49 public function getIsFreshSite() {
50 $is_fresh_site = get_option( 'fresh_site' );
51 $is_starter_content = $this->getThemeHasStarterContentKeySet();
52 return $is_fresh_site || $is_starter_content;
53 }
54 public function initRestApi() {
55 $namespace = 'kubio/v1';
56
57 register_rest_route(
58 $namespace,
59 '/get-kubio-front-page-revert-notice-editor-html',
60 array(
61 'methods' => 'GET',
62 'callback' => array($this, 'getEditorNoticeHtml'),
63 'permission_callback' => function () {
64 return current_user_can( 'edit_theme_options' );
65 },
66 )
67 );
68 }
69
70 public function getEditorNoticeHtml() {
71 ob_start();
72 echo wp_kses_post($this->getRestoreNoticeHTML());
73
74 //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
75 echo $this->getRestoreNoticeStyle();
76 $content = ob_get_clean();
77 return new \WP_REST_Response($content, 200);
78 }
79
80 public static function getShowNoticeInEditor() {
81 $instance = static::getInstance();
82 return $instance->getCurrentUserIsAdmin() && $instance->shouldShowRestoreNotice() && $instance->getHasFrontPageBackupData();
83 }
84 public function getFrontPageBackupData() {
85 $encodedData = Flags::get($this->getFrontPageBackupKey());
86 if (empty($encodedData)) {
87 return null;
88 }
89
90 $data = json_decode($encodedData, true);
91 return $data;
92 }
93 //check if there is some data to restore
94 public function getHasFrontPageBackupData() {
95 $frontPageEncodedData = Flags::get($this->getFrontPageBackupKey());
96 return !empty($frontPageEncodedData);
97 }
98
99
100 public function getCurrentUserIsAdmin()
101 {
102 return Utils::getCurrentUserIsAdmin();
103 }
104
105 public function getShowNoticeKey()
106 {
107 $template = get_template();
108 return $template . '.' . static::$showNoticeFlagKey;
109 }
110
111 public function getGlobalDataKey() {
112 $template = get_template();
113 return $template . '.' . static::$globalDataBackupKey;
114 }
115 public function getFrontPageFromtStarterContentKey()
116 {
117 $template = get_template();
118 return $template . '.' . static::$frontpageIsFromStarterContent;
119 }
120 public function getFrontPageBackupKey()
121 {
122 $template = get_template();
123 return $template . '.' . static::$frontPageBackupKey;
124 }
125
126 public function getFrontPageRevertedKey() {
127 $template = get_template();
128 return $template . '.' . static::$frontPageRevertedKey;
129 }
130
131 public function getMenuBackupKey()
132 {
133 $template = get_template();
134 return $template . '.' . static::$menuBackupKey;
135 }
136 public function getTemplatePartsBackupKey() {
137 $template = get_template();
138 return $template . '.' . static::$templatePartsBackupKey;
139 }
140 public function updateShowNoticeFlag($newValue)
141 {
142 Flags::set($this->getShowNoticeKey(), $newValue);
143 }
144 public function shouldShowRestoreNotice()
145 {
146 return Flags::get($this->getShowNoticeKey());
147 }
148
149
150
151 public function onKeepKubioFrontPage()
152 {
153 if ( ! $this->getCurrentUserIsAdmin() ) {
154 return;
155 }
156 check_ajax_referer(static::$nonceKey);
157 $this->cleanUpFlags();
158 wp_send_json_success();
159 }
160
161
162 public function onRestoreUserFrontPage()
163 {
164 if ( ! $this->getCurrentUserIsAdmin() ) {
165 return;
166 }
167 check_ajax_referer(static::$nonceKey);
168 $this->updateShowNoticeFlag(false);
169 $this->restoreWebsiteFrontPage();
170 $this->restoreWebsiteMenu();
171 $this->restoreTemplateParts();
172 $this->restoreGlobalData();
173 $this->updateStartSourceToKnowItReverted();
174
175 if(get_option('show_on_front') === 'posts') {
176 $this->onUpdateBlogTemplateToHaveFrontHeader();
177 }
178 //mark the front page as reverted. Maybe we'll need it later to revert revert it back :D
179 Flags::set($this->getFrontPageRevertedKey(), true);
180 $this->cleanUpFlags();
181 wp_send_json_success();
182 }
183
184 public function updateStartSourceToKnowItReverted() {
185 $startSource = Flags::get( 'start_source', 'other' );
186 $newStartSource = $startSource . "_r";
187 Flags::set( 'start_source', $newStartSource);
188 }
189 public function onUpdateBlogTemplateToHaveFrontHeader() {
190 $stylesheet = get_stylesheet();
191 $query = new \WP_Query(
192 array(
193 'post_type' => 'wp_template',
194 'post_status' => array( 'publish' ),
195 'post_name__in' => array( 'index' ),
196 'posts_per_page' => 10,
197 'no_found_rows' => true,
198 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
199 'tax_query' => array(
200 array(
201 'taxonomy' => 'wp_theme',
202 'field' => 'name',
203 'terms' => array( $stylesheet ),
204 ),
205 ),
206 )
207 );
208 if(!$query->have_posts()) {
209 return;
210 }
211 $posts = $query->posts;
212 if(count($posts) === 0) {
213 return;
214 }
215 $blogPost = null;
216 // foreach($posts as $post) {
217 // if($post->post_name === 'home') {
218 // $blogPost = $post;
219 // }
220 // }
221 if(empty($blogPost)) {
222 $blogPost = $posts[0];
223 }
224
225
226 //update the template part from header to front header to have the front page look the same as before the plugin
227 $content = $blogPost->post_content;
228 $content = str_replace('"slug":"header"', '"slug":"front-header"', $content);
229 wp_update_post(
230 array(
231 'ID' => $blogPost->ID,
232 'post_content' => $content
233 )
234 );
235 }
236
237 public function restoreGlobalData() {
238 $globalDataBackup = Flags::get($this->getGlobalDataKey());
239
240 if (empty($globalDataBackup)) {
241 return;
242 }
243
244 $id = kubio_global_data_post_id();
245 $post = get_post( $id );
246 if(!$post) {
247 return;
248 }
249 wp_update_post(
250 array(
251 'ID' => intval( $id ),
252 'post_content' => $globalDataBackup,
253 )
254 );
255
256
257 }
258
259 public function cleanUpFlags() {
260 Flags::delete($this->getShowNoticeKey());
261 Flags::delete($this->getMenuBackupKey());
262 Flags::delete($this->getFrontPageFromtStarterContentKey());
263 Flags::delete($this->getTemplatePartsBackupKey());
264 Flags::delete($this->getFrontPageBackupKey());
265 Flags::delete($this->getGlobalDataKey());
266 $this->removeBlackWizardFlags();
267
268 //this flag should remain to know if the site was reverted or not
269 //Flags::delete($this->getFrontPageRevertedKey());
270 }
271 public function removeBlackWizardFlags() {
272 if ( Flags::get( 'black_wizard_onboarding_hash' ) ) {
273 Flags::delete( 'black_wizard_onboarding_hash' );
274 }
275 if ( Flags::get( 'auto_start_black_wizard_onboarding' ) ) {
276 Flags::delete( 'auto_start_black_wizard_onboarding' );
277 }
278 }
279
280 //if this is true then a theme like vertice with starter content was used. This kind of themes only show starter content
281 //if no changes had be made on the site, so to revert it we must restore blog
282 public function getHasStarterContentBackup() {
283 return Flags::get($this->getFrontPageFromtStarterContentKey());
284 }
285 public function restoreWebsiteFrontPage()
286 {
287
288 //if the frontpage comes from starter content do a different logic
289 if($this->getHasStarterContentBackup()) {
290 $this->restoreFrontPageForStarterContent();
291 return;
292 }
293 $data = $this->getFrontPageBackupData();
294 if (empty($data)) {
295 return false;
296 }
297 $showOnFront = $data['showOnFront'];
298 //if you want the front to show blog the front page template should be deleted.
299
300
301 $frontPageId = $data['frontPageId'];
302 $blogPageId = $data['blogPageId'];
303
304
305 update_option('show_on_front', $showOnFront);
306 update_option('page_on_front', $frontPageId);
307 update_option('page_for_posts', $blogPageId);
308 }
309 //The starter content flow only shows up if you did not make any changes so it means your frontpage was blog before
310 //So we restore the blog as homepage
311 public function restoreFrontPageForStarterContent() {
312 update_option('show_on_front', 'posts' );
313 update_option('page_on_front', '0');
314 update_option('page_for_posts', '0');
315
316 }
317
318
319
320 public function getCurrentMenuId()
321 {
322 $currentSetLocations = get_nav_menu_locations();
323 $menuId = null;
324 $commonHeaderLocations = array(
325 'header-menu',
326 'header',
327 'primary',
328 'main',
329 'menu-1',
330 );
331
332 foreach ($currentSetLocations as $locationName => $locationId) {
333 if ($menuId) {
334 break;
335 }
336 if (in_array($locationName, $commonHeaderLocations)) {
337 $menuId = $locationId;
338 break;
339 }
340 }
341 return $menuId;
342 }
343
344 public function getShouldBackupData() {
345 if(!$this->getFeatureIsEnabled()) {
346 return false;
347 }
348
349 //for fresh sites do not run the restore notice
350 if($this->getIsFreshSite()) {
351 return false;
352 }
353
354
355 //if the activation is not with frontpage do not backup data and mark that the notice should be displayed
356 $activateWithFrontpage = Activation::load()->activeWithFrontpage();
357 if(!$activateWithFrontpage) {
358 return false;
359 }
360 return true;
361 }
362 public function backupUserData()
363 {
364 if(!$this->getShouldBackupData()) {
365 return;
366 }
367 $this->backupWebsiteFrontPage();
368 $this->backupWebsiteMenu();
369 $this->updateShowNoticeFlag(true);
370 }
371
372 public function backupUserUsedStarterContentFrontpage() {
373 if(!$this->getShouldBackupData()) {
374 return;
375 }
376
377 if(!$this->getThemeHasStarterContentKeySet()) {
378 return;
379 }
380
381 Flags::set($this->getFrontPageFromtStarterContentKey(), true);
382 }
383
384 //the template parts are created later that is why we need a different function
385 public function backupTemplateParts() {
386 if(!$this->getShouldBackupData()) {
387 return;
388 }
389 $entities = array_keys( Importer::getAvailableTemplateParts() );
390
391 //we only backup the header slugs.
392 $header_slugs = ['header', 'front-header'];
393 $saved_template_parts = [];
394 foreach ( $entities as $slug ) {
395 $is_current_kubio_template = apply_filters( 'kubio/template/is_importing_kubio_template', kubio_theme_has_kubio_block_support(), $slug );
396
397 if(!$is_current_kubio_template || !in_array($slug, $header_slugs)) {
398 continue;
399 }
400
401
402 $content = $this->getTemplatePartContentBySlug($slug);
403 if(empty($content)) {
404 continue;
405 }
406 $saved_template_parts[$slug] = $content;
407 }
408
409 if(empty($saved_template_parts)) {
410 return;
411 }
412 $json_content = json_encode($saved_template_parts);
413 Flags::set( static::getTemplatePartsBackupKey(), $json_content );
414 }
415
416
417
418
419 public function restoreTemplateParts() {
420 $templatePartsString = Flags::get( static::getTemplatePartsBackupKey(), array() );
421 if(empty($templatePartsString)) {
422 return;
423 }
424 $templatePartsData = json_decode($templatePartsString, true);
425 foreach ( $templatePartsData as $slug => $templatePartContent) {
426
427 $is_current_kubio_template = true;
428 Importer::createTemplatePart( $slug, $templatePartContent, true, $is_current_kubio_template ? 'kubio' : 'theme' );
429 }
430
431 }
432 public function restoreWebsiteMenu()
433 {
434 $frontPageEncodedData = Flags::get($this->getFrontPageBackupKey());
435 $menuEncodedData = Flags::get($this->getMenuBackupKey());
436
437 $menuId = $this->getCurrentMenuId();
438 if (!$menuId) {
439 return;
440 }
441
442
443 if (empty($frontPageEncodedData)) {
444 return false;
445 }
446
447 $frontPageData = json_decode($frontPageEncodedData, true);
448 $menuData = json_decode($menuEncodedData, true);
449 $showOnFront = $frontPageData['showOnFront'];
450
451 $frontPageId = $frontPageData['frontPageId'];
452 $blogPageId = $frontPageData['blogPageId'];
453
454 //if the user comes from starter content it means the theme made some changes and we can assume the site had no
455 //changes so we restore to the blog
456 if($this->getHasStarterContentBackup()) {
457 $showOnFront = 'posts';
458 $frontPageId = '0';
459 $blogPageId = '0';
460 $menuEncodedData = null;
461 }
462
463 $pageOnFront = $showOnFront === 'page';
464 $blogOnFront = !$pageOnFront;
465
466
467 //if on plugin activation there was no menu then create a fallback menu
468 if (empty($menuEncodedData)) {
469 $this->createFallbackMenu($menuId, $blogOnFront, $frontPageId, $blogPageId);
470 return;
471 }
472
473
474 $menuItems = wp_get_nav_menu_items($menuId);
475 if (!$menuItems) {
476 return;
477 }
478 $this->emptyMenuItems($menuId);
479 $newMenuIdByOldMenuId = [];
480 foreach ($menuData as $item) {
481 $isHome = intval($item['object_id']) === intval($frontPageId);
482 $isBlog = intval($item['object_id']) === intval($blogPageId);
483 $parentId = $item['parent'];
484
485 //update the parent id with the new ids
486 if(!empty($parentId)) {
487 $parentId = LodashBasic::get($newMenuIdByOldMenuId, $parentId, '0' );
488 }
489 if ($blogOnFront && $isBlog) {
490 continue;
491 }
492 if ($blogOnFront) {
493 if ($isBlog) {
494 continue;
495 }
496 if ($isHome) {
497 $newId = wp_update_nav_menu_item($menuId, 0, [
498 'menu-item-title' => __('Home', 'kubio'),
499 'menu-item-object' => 'custom',
500 'menu-item-url' => home_url(),
501 'menu-item-status' => 'publish',
502 'menu-item-parent-id' => $parentId,
503 'menu-item-position' => $item['menu_order'],
504 ]);
505 $newMenuIdByOldMenuId[$item['id']] = $newId;
506 continue;
507 }
508 }
509
510 $newId = wp_update_nav_menu_item($menuId, 0, [
511 'menu-item-title' => $item['title'],
512 'menu-item-url' => $item['url'],
513 'menu-item-status' => 'publish',
514 'menu-item-parent-id' => $parentId,
515 'menu-item-position' => $item['menu_order'],
516 'menu-item-type' => $item['type'],
517 'menu-item-object-id' => $item['object_id'],
518 'menu-item-object' => $item['object'],
519 ]);
520
521 $newMenuIdByOldMenuId[$item['id']] = $newId;
522 }
523 }
524
525
526 public function emptyMenuItems($menuId)
527 {
528 if (!$menuId) {
529 return;
530 }
531 $menuItems = wp_get_nav_menu_items($menuId);
532 if (!$menuItems) {
533 return;
534 }
535 foreach ($menuItems as $item) {
536 wp_delete_post($item->ID, true);
537 }
538 }
539
540 //if there was no menu before
541 public function createFallbackMenu($menuId, $blogOnFront, $frontPageId, $blogPageId)
542 {
543
544 $this->emptyMenuItems($menuId);
545 if ($blogOnFront) {
546 wp_update_nav_menu_item($menuId, 0, [
547 'menu-item-title' => __('Home', 'kubio'),
548 'menu-item-object' => 'custom',
549 'menu-item-url' => home_url(),
550 'menu-item-status' => 'publish',
551 ]);
552 return;
553 }
554
555 if ($frontPageId) {
556 wp_update_nav_menu_item(
557 $menuId,
558 0,
559 array(
560 'menu-item-title' => __('Home', 'kubio'),
561 'menu-item-object' => 'page',
562 'menu-item-object-id' => $frontPageId,
563 'menu-item-type' => 'post_type',
564 'menu-item-status' => 'publish',
565 )
566 );
567 }
568 if ($blogPageId) {
569 wp_update_nav_menu_item(
570 $menuId,
571 0,
572 array(
573 'menu-item-title' => __('Blog', 'kubio'),
574 'menu-item-object' => 'page',
575 'menu-item-object-id' => $blogPageId,
576 'menu-item-type' => 'post_type',
577 'menu-item-status' => 'publish',
578 )
579 );
580 }
581 }
582
583
584
585
586
587 public function backupWebsiteFrontPage()
588 {
589
590 $showOnFront = get_option('show_on_front');
591 $frontPageId = get_option('page_on_front');
592 $blogPageId = get_option('page_for_posts');
593 $backupData = json_encode([
594 'showOnFront' => $showOnFront,
595 'frontPageId' => $frontPageId,
596 'blogPageId' => $blogPageId
597 ]);
598
599
600 Flags::set($this->getFrontPageBackupKey(), $backupData);
601 }
602
603
604 public function backupGlobalData() {
605 if(!$this->getShouldBackupData()) {
606 return;
607 }
608 $id = kubio_global_data_post_id();
609 $post = get_post( $id );
610 if(!$post) {
611 return;
612 }
613
614 $content = $post->post_content;
615 Flags::set($this->getGlobalDataKey(), $content);
616 }
617
618
619
620 public function getTemplatePartContentBySlug($slug) {
621 $theme = get_stylesheet();
622 $source = 'kubio';
623 $query_args = array(
624 'post_status' => 'publish',
625 'post_type' => 'wp_template_part',
626 'name' => $slug,
627 'posts_per_page' => 1,
628 'tax_query' => array(
629 array(
630 'taxonomy' => 'wp_theme',
631 'field' => 'slug',
632 'terms' => $theme,
633 ),
634 ),
635 'meta_query' => array(
636 array(
637 'key' => '_kubio_template_source',
638 'value' => $source,
639 'compare' => '=',
640 ),
641 ),
642 );
643 $query = new \WP_Query($query_args);
644 if (!$query->have_posts()) {
645 return null;
646 }
647 if(count($query->posts) < 1) {
648 return null;
649 }
650
651 $templatePartPost = $query->posts[0];
652 return $templatePartPost->post_content;
653 }
654
655 public function backupWebsiteMenu()
656 {
657 $menuId = $this->getCurrentMenuId();
658 if (empty($menuId)) {
659 return;
660 }
661 $menu_items = wp_get_nav_menu_items($menuId);
662 if (empty($menu_items)) {
663 return;
664 }
665 if ($menu_items) {
666 $backup_data = [];
667
668 foreach ($menu_items as $item) {
669 $backup_data[] = [
670 'id' => $item->ID,
671 'title' => $item->title,
672 'url' => $item->url,
673 'menu_order' => $item->menu_order,
674 'parent' => $item->menu_item_parent,
675 'type' => $item->type,
676 'object_id' => $item->object_id,
677 'object' => $item->object,
678 ];
679 }
680 }
681 $jsoBackupData = json_encode($backup_data);
682 Flags::set($this->getMenuBackupKey(), $jsoBackupData);
683 }
684
685 public static function getInstance()
686 {
687 if (! static::$instance) {
688 static::$instance = new static();
689 }
690
691 return static::$instance;
692 }
693
694 public static function load()
695 {
696 return static::getInstance();
697 }
698
699 public function getShouldPrintNotice() {
700 if (!is_front_page() || !$this->getCurrentUserIsAdmin()) {
701 return false;
702 }
703 if (!$this->shouldShowRestoreNotice() || !$this->getHasFrontPageBackupData()) {
704 return false;
705 }
706 return true;
707 }
708 public function onPrintNoticeWithCheck()
709 {
710
711 if(!$this->getShouldPrintNotice()) {
712 return;
713
714 }
715 echo wp_kses_post($this->getRestoreNoticeHTML());
716 }
717
718 public function onPrintNoticeAssetsWithCheck() {
719 if(!$this->getShouldPrintNotice()) {
720 return;
721 }
722
723 //phpcs:ignore WordPress.WP.AlternativeFunctions.strip_tags_strip_tags
724 $style = strip_tags($this->getRestoreNoticeStyle());
725 wp_add_inline_style( 'kubio-block-library', $style);
726
727 //phpcs:ignore WordPress.WP.AlternativeFunctions.strip_tags_strip_tags
728 $script = strip_tags($this->getRestoreNoticeScript());
729 wp_add_inline_script( 'jquery', $script, 'after' );
730 }
731
732
733 public function getRestoreNoticeHTML()
734 {
735 ob_start();
736
737 ?>
738 <div class="kubio-front-page-revert-popup__parent">
739 <div class="kubio-front-page-revert-notice">
740 <div class="kubio-front-page-revert-notice__header">
741 <?php echo esc_html__('Keep the new home page?', 'kubio'); ?>
742 <span class="kubio-front-page-revert-notice__header__close">×</span>
743 </div>
744 <div class="kubio-front-page-revert-notice__content">
745 <?php echo esc_html__(" We created a new home page for you. If you don't like it you can restore your previous home page.", 'kubio'); ?>
746 </div>
747 <div class="kubio-front-page-revert-notice__footer">
748 <button class="kubio-front-page-revert-notice__button kubio-front-page-revert-notice__footer__keep">
749 <?php echo esc_html__("Yes, keep my new home page", 'kubio'); ?>
750 </button>
751 <button class="kubio-front-page-revert-notice__button kubio-front-page-revert-notice__footer__restore">
752 <?php echo esc_html__("Restore my previous home page", 'kubio'); ?>
753 </button>
754 </div>
755 </div>
756 </div>
757
758 <?php
759
760 $content = ob_get_clean();
761
762 return $content;
763 }
764
765 public function getRestoreNoticeScript()
766 {
767 ob_start();
768
769 $encodedData = Flags::get($this->getFrontPageBackupKey());
770 if (empty($encodedData)) {
771 return false;
772 }
773
774 $data = json_decode($encodedData, true);
775 $showOnFront = $data['showOnFront'];
776 $blogOnFront = $showOnFront === 'posts';
777 $confirmMessage;
778 if($blogOnFront) {
779 $confirmMessage = esc_html__('Your previous home page was showing the most recent blog posts. Are you sure you want to restore your previous home page?', 'kubio');
780 } else {
781 $confirmMessage = esc_html__('Are you sure you want to restore your previous home page?', 'kubio');
782 }
783 $fetchUrl = add_query_arg(
784 array(
785 // 'action' => 'kubio-remote-notifications-retrieve',
786 '_wpnonce' => wp_create_nonce(static::$nonceKey),
787
788 ),
789 admin_url('admin-ajax.php')
790 );
791
792 ?>
793
794 <script>
795 (function($) {
796
797 $(document).ready(function() {
798
799 const baseUrl = "<?php echo esc_url($fetchUrl); ?>";
800
801 const getUrl = function(action) {
802 try {
803 const urlObject = new URL(baseUrl);
804
805 const searchQuery = urlObject.searchParams;
806 searchQuery.append('action', action)
807 const url = urlObject.toString();
808 return url;
809 } catch (e) {
810 console.error(e);
811 }
812 return null;
813 }
814 const removeNotice = () => {
815 let container = document.querySelector('.kubio-front-page-revert-notice');
816 if (!container) {
817 return
818 }
819 container.parentNode.removeChild(container);
820 }
821 let pending = false;
822 const onKeepFrontPage = function() {
823 if (pending) {
824 return
825 }
826 pending = true;
827 removeNotice();
828 const url = getUrl('kubio_front_page_revert_action');
829 window.fetch(url)
830
831
832 }
833 const onRestoreFrontPage = function() {
834 if (!confirm("<?php echo esc_html($confirmMessage); ?>")) {
835 return
836 }
837 if (pending) {
838 return
839 }
840 pending = true;
841
842 removeNotice();
843 const url = getUrl('kubio_restore_front_page');
844 window.fetch(url)
845 .finally((result) => {
846 window.location.href = "<?php echo esc_url(home_url()); ?>"
847 })
848
849 }
850
851 let keepButtons = document.querySelectorAll('.kubio-front-page-revert-notice__footer__keep, .kubio-front-page-revert-notice__header__close');
852 if (keepButtons.length > 0) {
853 [...keepButtons].forEach(button => {
854 button.addEventListener('click', onKeepFrontPage)
855 })
856 }
857 let restoreButton = document.querySelector('.kubio-front-page-revert-notice__footer__restore');
858 if (restoreButton) {
859 restoreButton.addEventListener('click', onRestoreFrontPage)
860 }
861
862 })
863 })(jQuery)
864 </script>
865
866 <?php
867
868 $content = ob_get_clean();
869
870 return $content;
871 }
872 public function getRestoreNoticeStyle()
873 {
874 ob_start();
875 ?>
876 <style>
877 .kubio-front-page-revert-notice {
878 position: fixed;
879 bottom: 1px;
880 left: 150px;
881 width: 375px;
882 padding-top: 20px;
883 padding-bottom: 20px;
884 padding-left: 25px;
885 padding-right: 25px;
886 box-shadow: 0px 4px 15px 0px #00000040;
887 z-index: 99999;
888 background: #F4F4F7;
889 display: flex;
890 flex-direction: column;
891 gap: 20px;
892 text-align: center;
893 font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif;
894 box-sizing: border-box;
895 opacity: 0;
896 animation: kubioFrontRevertPopupSlideUp 0.5s ease-out forwards;
897 animation-delay: 1s;
898 }
899
900 .kubio-front-page-revert-notice__header {
901 background: #007CBA;
902 margin-left: -25px;
903 margin-right: -25px;
904 margin-top: -20px;
905 padding-left: 25px;
906 padding-right: 25px;
907 padding-top: 20px;
908 padding-bottom: 20px;
909 color: white;
910 font-size: 20px;
911 font-weight: 700;
912 text-align: center;
913 line-height: 1.3;
914 position: relative;
915
916
917 }
918 .kubio-front-page-revert-notice__header__close {
919 position: absolute;
920 top: 0px;
921 right: 4px;
922 font-size: 24px;
923 padding: 5px;
924 cursor: pointer;
925 line-height: 1;
926 }
927
928 @keyframes kubioFrontRevertPopupSlideUp {
929 from {
930 transform: translateY(100%);
931 opacity: 0;
932 }
933 to {
934 transform: translateY(0);
935 opacity: 1;
936 }
937 }
938
939 .kubio-front-page-revert-notice__content {
940 font-size: 14px;
941 }
942
943 .kubio-front-page-revert-notice__button {
944 align-items: center;
945 background: none;
946 border: 0;
947 border-radius: 2px;
948 box-sizing: border-box;
949 cursor: pointer;
950 display: inline-flex;
951 justify-content: center;
952 font-family: inherit;
953 font-size: 13px;
954 font-weight: 400;
955 height: 36px;
956 margin: 0;
957 padding: 6px 12px;
958 text-decoration: none;
959 transition: box-shadow .1s linear;
960 background: #007CBA;
961 outline: 1px solid #0000;
962 text-decoration: none;
963 text-shadow: none;
964 white-space: nowrap;
965 transition: 0.3s ease-in-out;
966 transition-property: color, background-color;
967
968 }
969 .kubio-front-page-revert-notice__footer__keep {
970 background: #007CBA;
971 color: #fff;
972 &:hover {
973 background-color: #006ba1;
974 color: #fff;
975 }
976 }
977 .kubio-front-page-revert-notice__footer__restore {
978 background: #cc1818;
979 color: #fff;
980 &:hover {
981 background-color: #9e1313;
982 }
983 }
984
985
986 .kubio-front-page-revert-notice__footer {
987 display: flex;
988 flex-direction: column;
989 gap: 10px;
990 }
991 </style>
992 <?php
993 return ob_get_clean();
994 }
995 }
996