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