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
11 months 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
16 hours ago
Activation.php
795 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Core; |
| 4 | |
| 5 | use IlluminateAgnostic\Arr\Support\Arr; |
| 6 | use Kubio\Flags; |
| 7 | |
| 8 | class Activation { |
| 9 | |
| 10 | |
| 11 | private static $instance = null; |
| 12 | |
| 13 | private $remote_content = array(); |
| 14 | |
| 15 | |
| 16 | public function __construct() { |
| 17 | |
| 18 | |
| 19 | add_action( |
| 20 | 'activated_plugin', |
| 21 | function ( $plugin ) { |
| 22 | |
| 23 | if ( $plugin === plugin_basename( KUBIO_ENTRY_FILE ) ) { |
| 24 | |
| 25 | $hash = uniqid( 'activate-' ); |
| 26 | Flags::set( 'activation-hash', $hash ); |
| 27 | |
| 28 | $url = add_query_arg( |
| 29 | array( |
| 30 | 'page' => 'kubio-get-started', |
| 31 | 'kubio-activation-hash' => $hash, |
| 32 | ), |
| 33 | admin_url( 'admin.php' ) |
| 34 | ); |
| 35 | |
| 36 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 37 | if ( ! $this->isCLI() && ! Arr::has( $_REQUEST, 'tgmpa-activate' ) && ! $this->isAJAX() ) { |
| 38 | wp_redirect( $url ); |
| 39 | exit(); |
| 40 | } else { |
| 41 | |
| 42 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 43 | if ( Arr::has( $_REQUEST, 'tgmpa-activate' ) || $this->isAJAX() ) { |
| 44 | Flags::set( 'activated_from_tgmpa_or_ajax', true ); |
| 45 | } |
| 46 | |
| 47 | Flags::set( 'import_design', false ); |
| 48 | Flags::set( 'start_with_ai', false ); |
| 49 | |
| 50 | if ( $this->isCLI() ) { |
| 51 | add_filter( 'user_has_cap', array( Importer::class, 'allowImportCaps' ), 10, 2 ); |
| 52 | $this->activate(); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | ); |
| 58 | |
| 59 | $self = $this; |
| 60 | |
| 61 | // handle direct tgmpa activation |
| 62 | add_action( |
| 63 | 'init', |
| 64 | function () { |
| 65 | |
| 66 | if ( ! is_admin() ) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | if ( Flags::get( 'activated_from_tgmpa_or_ajax' ) ) { |
| 71 | Flags::delete( 'activated_from_tgmpa_or_ajax' ); |
| 72 | |
| 73 | $hash = uniqid( 'activate-' ); |
| 74 | Flags::set( 'activation-hash', $hash ); |
| 75 | $url = add_query_arg( |
| 76 | array( |
| 77 | 'page' => 'kubio-get-started', |
| 78 | 'kubio-activation-hash' => $hash, |
| 79 | ), |
| 80 | admin_url( 'admin.php' ) |
| 81 | ); |
| 82 | |
| 83 | wp_redirect( $url ); |
| 84 | exit(); |
| 85 | } |
| 86 | }, |
| 87 | 5 |
| 88 | ); |
| 89 | |
| 90 | add_action( |
| 91 | 'init', |
| 92 | function () use ( $self ) { |
| 93 | |
| 94 | if ( ! is_admin() ) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 99 | $hash = sanitize_text_field( Arr::get( $_REQUEST, 'kubio-activation-hash', null ) ); |
| 100 | $saved_hash = Flags::get( 'activation-hash', false ); |
| 101 | if ( $saved_hash === $hash ) { |
| 102 | Flags::delete( 'activation-hash' ); |
| 103 | $self->activate(); |
| 104 | } |
| 105 | }, |
| 106 | 500 |
| 107 | ); |
| 108 | |
| 109 | add_action( 'after_switch_theme', array( $this, 'afterSwitchTheme' ) ); |
| 110 | } |
| 111 | |
| 112 | public function isCLI() { |
| 113 | return defined( 'WP_CLI' ) && WP_CLI; |
| 114 | } |
| 115 | |
| 116 | public function isAJAX() { |
| 117 | return defined( 'DOING_AJAX' ) && DOING_AJAX; |
| 118 | } |
| 119 | |
| 120 | public function startWithAI() { |
| 121 | return Flags::get( 'start_with_ai', false ) !== false; |
| 122 | } |
| 123 | |
| 124 | public function activatedFromCustomizerOnboardingWizard() { |
| 125 | return Flags::get( 'auto_start_black_wizard_onboarding', false ); |
| 126 | } |
| 127 | |
| 128 | public function startWithBlackWizardOnboarding() { |
| 129 | //return false; |
| 130 | $onboarding_is_supported = kubio_is_black_wizard_onboarding_enabled() && Flags::get( 'import_design_ai_structure', false ); |
| 131 | if(!$onboarding_is_supported) { |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | $activated_from_notice = $this->activeWithFrontpage() && Flags::get( 'start_source', false ) == 'notice-homepage'; |
| 136 | $activated_from_theme_customizer = $this->activatedFromCustomizerOnboardingWizard(); |
| 137 | return $activated_from_notice || $activated_from_theme_customizer; |
| 138 | } |
| 139 | |
| 140 | public function activeWithFrontpage() { |
| 141 | |
| 142 | if ( $this->startWithAI() ) { |
| 143 | return true; |
| 144 | } |
| 145 | $import_design = Flags::get( 'import_design', false ); |
| 146 | return apply_filters( 'kubio/activation/activate_with_frontpage', $import_design !== false ); |
| 147 | } |
| 148 | |
| 149 | public function importUnmodifiedTemplates() { |
| 150 | return ! CustomizerImporter::themeHasModifiedOptions(); |
| 151 | } |
| 152 | |
| 153 | public function importCustomizedTemplates() { |
| 154 | return CustomizerImporter::themeHasModifiedOptions(); |
| 155 | } |
| 156 | |
| 157 | public function getDeactivationBackupKey() { |
| 158 | $template = get_stylesheet(); |
| 159 | $identifier = Flags::getSetting( "deactivation_backup_key.{$template}", null ); |
| 160 | return $identifier; |
| 161 | } |
| 162 | |
| 163 | private function shouldRestoreDeactivationBackup( Backup $backup ) { |
| 164 | $identifier = $this->getDeactivationBackupKey(); |
| 165 | return $identifier && $backup->hasBackup( $identifier ); |
| 166 | } |
| 167 | |
| 168 | private function restoreDeactivationBackup( Backup $backup ) { |
| 169 | $identifier = $this->getDeactivationBackupKey(); |
| 170 | $status = $backup->restoreBackup( $identifier ); |
| 171 | |
| 172 | if ( ! is_wp_error( $status ) ) { |
| 173 | $backup->deleteBackup( $identifier ); |
| 174 | } |
| 175 | Flags::delete( $identifier ); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | public function activate() { |
| 180 | $backup = new Backup(); |
| 181 | |
| 182 | if ( $this->shouldRestoreDeactivationBackup( $backup ) ) { |
| 183 | $this->restoreDeactivationBackup( $backup ); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | do_action( 'kubio/before_activation' ); |
| 188 | |
| 189 | // if free previously activated return |
| 190 | if ( Flags::get( 'kubio_activation_time', false ) ) { |
| 191 | $stylesheet = Flags::get( 'stylesheet', null ); |
| 192 | if ( $stylesheet === get_stylesheet() ) { |
| 193 | return; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | Flags::set( 'kubio_f', get_option( 'fresh_site' ) ); |
| 198 | Flags::set( 'kubio_activation_time', time() ); |
| 199 | Flags::set( 'stylesheet', get_stylesheet() ); |
| 200 | |
| 201 | $this->addCommonFilters(); |
| 202 | $this->prepareRemoteData(); |
| 203 | |
| 204 | //set site uuid on activation |
| 205 | Flags::getSiteUUID(); |
| 206 | |
| 207 | add_filter( 'kubio/importer/page_path', array( $this, 'getDesignPagePath' ), 10, 2 ); |
| 208 | |
| 209 | if ( $this->importCustomizedTemplates() ) { |
| 210 | add_filter( 'kubio/importer/content', array( $this, 'importCustomizerOptions' ), 20, 3 ); |
| 211 | } |
| 212 | |
| 213 | if ( $this->activeWithFrontpage() ) { |
| 214 | add_filter( 'kubio/activation/force_front_page_creation', '__return_true' ); |
| 215 | } |
| 216 | |
| 217 | wp_cache_flush(); |
| 218 | |
| 219 | |
| 220 | //store current data to restore if user wants it |
| 221 | KubioFrontPageRevertNotice::getInstance()->backupUserData(); |
| 222 | |
| 223 | |
| 224 | $this->importDesign(); |
| 225 | $this->importTemplates(); |
| 226 | $this->importTemplateParts(); |
| 227 | |
| 228 | wp_cache_flush(); |
| 229 | do_action( 'kubio/after_activation' ); |
| 230 | //we wait for the template parts to import so we can backup them |
| 231 | KubioFrontPageRevertNotice::getInstance()->backupTemplateParts(); |
| 232 | KubioFrontPageRevertNotice::getInstance()->backupGlobalData(); |
| 233 | if ( ! $this->isCLI() ) { |
| 234 | |
| 235 | // make an educated guess about the start source if not set |
| 236 | if ( ! Flags::get( 'start_source', false ) ) { |
| 237 | $start_source = 'other'; |
| 238 | if ( $this->startWithAI() ) { |
| 239 | $start_source = 'notice-ai'; |
| 240 | } elseif ( $this->activeWithFrontpage() ) { |
| 241 | $start_source = 'notice-homepage'; |
| 242 | } |
| 243 | Flags::set( 'start_source', $start_source ); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | if ( $this->startWithAI() ) { |
| 248 | Flags::set( 'start_with_ai', false ); |
| 249 | $ai_hash = md5( uniqid( 'start-with-ai' ) ); |
| 250 | Flags::set( 'start_with_ai_hash', $ai_hash ); |
| 251 | wp_redirect( |
| 252 | Utils::kubioGetEditorURL( |
| 253 | array( |
| 254 | 'ai' => $ai_hash, |
| 255 | ) |
| 256 | ) |
| 257 | ); |
| 258 | exit(); |
| 259 | } |
| 260 | if( $this->startWithBlackWizardOnboarding()) { |
| 261 | $black_wizard_onboarding_hash = md5( uniqid( 'black-wizard-onboarding' ) ); |
| 262 | Flags::set( 'black_wizard_onboarding_hash', $black_wizard_onboarding_hash ); |
| 263 | $url = Utils::kubioGetEditorURL( |
| 264 | array( |
| 265 | 'black-wizard-onboarding' => $black_wizard_onboarding_hash, |
| 266 | ) |
| 267 | ); |
| 268 | wp_redirect( $url ); |
| 269 | exit(); |
| 270 | } |
| 271 | |
| 272 | if ( $this->activeWithFrontpage() ) { |
| 273 | if ( Flags::get( 'start_source', false ) == 'notice-homepage' ) { |
| 274 | |
| 275 | $url = add_query_arg( |
| 276 | array( |
| 277 | 'page' => 'kubio-get-started', |
| 278 | 'kubio-designed-imported' => intval( ! ! Flags::get( 'import_design', false ) ), |
| 279 | ), |
| 280 | admin_url( 'admin.php' ) |
| 281 | ); |
| 282 | |
| 283 | wp_redirect( $url ); |
| 284 | |
| 285 | } else { |
| 286 | wp_redirect( |
| 287 | Utils::kubioGetEditorURL() |
| 288 | ); |
| 289 | } |
| 290 | exit(); |
| 291 | } |
| 292 | |
| 293 | $is_unmodified_supported_theme = kubio_theme_has_kubio_block_support() && ! CustomizerImporter::themeHasModifiedOptions(); |
| 294 | if ( get_option( 'fresh_site' ) || $is_unmodified_supported_theme ) { |
| 295 | $url = add_query_arg( |
| 296 | array( |
| 297 | 'page' => 'kubio-get-started', |
| 298 | 'tab' => 'website-starter', |
| 299 | ), |
| 300 | admin_url( 'admin.php' ) |
| 301 | ); |
| 302 | wp_redirect( $url ); |
| 303 | exit(); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | public function addCommonFilters() { |
| 309 | add_filter( 'kubio/importer/skip-remote-file-import', '__return_true' ); |
| 310 | add_filter( 'kubio/importer/content', array( $this, 'getFileContent' ), 1, 3 ); |
| 311 | add_filter( 'kubio/importer/content', array( $this, 'templateMapPartsTheme' ), 10, 2 ); |
| 312 | add_filter( 'kubio/importer/content', array( $this, 'updateBlocks' ), 10, 3 ); |
| 313 | remove_filter( 'theme_mod_nav_menu_locations', 'kubio_nav_menu_locations_from_global_data' ); |
| 314 | remove_filter( 'wp_insert_post_data', 'kubio_on_post_update', 10, 3 ); |
| 315 | remove_action( 'wp_insert_post', 'kubio_update_meta', 10, 3 ); |
| 316 | |
| 317 | add_filter( 'kubio/importer/available_templates', array( $this, 'getAvailableTemplates' ), 10 ); |
| 318 | add_filter( 'kubio/importer/available_template_parts', array( $this, 'getAvailableTemplateParts' ), 10 ); |
| 319 | } |
| 320 | |
| 321 | public function prepareRemoteData() { |
| 322 | if ( ! \kubio_theme_has_kubio_block_support() ) { |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | $with_front_page = apply_filters( 'kubio/importer/with_front_page', $this->importUnmodifiedTemplates() ); |
| 327 | |
| 328 | $base_url = 'https://themes.kubiobuilder.com'; |
| 329 | $file_name = get_stylesheet() . '__' . get_template() . '__' . ( $with_front_page ? 'with-front' : 'default' ) . '.data'; |
| 330 | |
| 331 | $url = apply_filters( 'kubio/remote_data_url', "{$base_url}/{$file_name}" ); |
| 332 | $response = wp_safe_remote_get( $url ); |
| 333 | |
| 334 | if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200 /* && Utils::isProduction() */ ) { |
| 335 | $content = wp_remote_retrieve_body( $response ); |
| 336 | $data = unserialize( $content ); |
| 337 | |
| 338 | if ( ! is_array( $data ) || Arr::get( $data, 'error' ) ) { |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | $ai_structure = isset( $data['ai-structure'] ) ? $data['ai-structure'] : null; |
| 343 | if ( $ai_structure ) { |
| 344 | Flags::set( 'import_design_ai_structure', $ai_structure ); |
| 345 | } |
| 346 | $this->remote_content = $data; |
| 347 | } else { |
| 348 | $content = file_get_contents( KUBIO_ROOT_DIR . '/defaults/default-site.dat' ); |
| 349 | $this->remote_content = unserialize( $content ); |
| 350 | } |
| 351 | |
| 352 | $global_data = Arr::get( $this->remote_content, 'global-data' ); |
| 353 | if ( $global_data ) { |
| 354 | $global_data = json_decode( $global_data, true ); |
| 355 | |
| 356 | if ( json_last_error() === JSON_ERROR_NONE ) { |
| 357 | Arr::forget( $global_data, 'menuLocations' ); |
| 358 | kubio_replace_global_data_content( $global_data ); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | $theme = Arr::get( $this->remote_content, 'theme' ); |
| 363 | |
| 364 | // if the child theme does not exists use the theme name for assets |
| 365 | if ( $theme && $theme !== get_stylesheet() ) { |
| 366 | add_filter( |
| 367 | 'kubio/importer/kubio-url-placeholder-replacement', |
| 368 | function () use ( $theme ) { |
| 369 | |
| 370 | return "https://static-assets.kubiobuilder.com/themes/{$theme}/assets/"; |
| 371 | }, |
| 372 | 10 |
| 373 | ); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | public function importDesign() { |
| 378 | if ( $this->isCLI() ) { |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | |
| 383 | $result = $this->setPages(); |
| 384 | |
| 385 | // try to set the blog page and menu |
| 386 | if ( ! is_wp_error( $result ) ) { |
| 387 | static::preparePrimaryMenu(); |
| 388 | } else { |
| 389 | // only set menu location |
| 390 | static::preparePrimaryMenu( false ); |
| 391 | } |
| 392 | |
| 393 | } |
| 394 | |
| 395 | private function setPages( $data = array() ) { |
| 396 | |
| 397 | if ( ! kubio_theme_has_kubio_block_support() ) { |
| 398 | return new \WP_Error( 'not_supported_themes' ); |
| 399 | } |
| 400 | |
| 401 | |
| 402 | $data = array_merge( |
| 403 | array( |
| 404 | 'front_content' => null, |
| 405 | 'with_blog_page' => true, |
| 406 | ), |
| 407 | $data |
| 408 | ); |
| 409 | |
| 410 | $front_page_id = $this->importFrontPage(); |
| 411 | |
| 412 | if ( is_wp_error( $front_page_id ) ) { |
| 413 | return $front_page_id; |
| 414 | } |
| 415 | |
| 416 | update_option( 'show_on_front', 'page' ); |
| 417 | update_option( 'page_on_front', $front_page_id ); |
| 418 | |
| 419 | $posts_page_id = intval( get_option( 'page_for_posts' ) ); |
| 420 | |
| 421 | if ( ! $posts_page_id && $data['with_blog_page'] ) { |
| 422 | $posts_page_id = wp_insert_post( |
| 423 | array( |
| 424 | 'comment_status' => 'closed', |
| 425 | 'ping_status' => 'closed', |
| 426 | 'post_name' => 'blog', |
| 427 | 'post_title' => __( 'Blog', 'kubio' ), |
| 428 | 'post_status' => 'publish', |
| 429 | 'post_type' => 'page', |
| 430 | 'page_template' => apply_filters( |
| 431 | 'kubio/front_page_template', |
| 432 | 'page-templates/homepage.php' |
| 433 | ), |
| 434 | 'post_content' => '', |
| 435 | 'meta_input' => array( |
| 436 | '_kubio_created_at_activation' => 1, |
| 437 | ), |
| 438 | ) |
| 439 | ); |
| 440 | |
| 441 | if ( ! is_wp_error( $posts_page_id ) ) { |
| 442 | update_option( 'page_for_posts', $posts_page_id ); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | return $posts_page_id; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * |
| 451 | * @return int|WP_Error |
| 452 | */ |
| 453 | private function importFrontPage() { |
| 454 | $page_on_front = get_option( 'page_on_front' ); |
| 455 | $query = new \WP_Query( |
| 456 | array( |
| 457 | 'post__in' => array( $page_on_front ), |
| 458 | 'post_status' => array( 'publish' ), |
| 459 | 'fields' => 'ids', |
| 460 | 'post_type' => 'page', |
| 461 | ) |
| 462 | ); |
| 463 | |
| 464 | $content = ''; |
| 465 | |
| 466 | if ( $this->activeWithFrontpage()) { |
| 467 | $content = Importer::getTemplateContent( 'page', 'front-page' ); |
| 468 | } |
| 469 | |
| 470 | if ( $query->have_posts() && ! apply_filters( 'kubio/activation/force_front_page_creation', false ) ) { |
| 471 | if ( apply_filters( 'kubio/activation/override_front_page_content', false ) ) { |
| 472 | KubioFrontPageRevertNotice::getInstance()->backupUserUsedStarterContentFrontpage($page_on_front); |
| 473 | wp_update_post( |
| 474 | array( |
| 475 | 'ID' => intval( $page_on_front ), |
| 476 | 'post_content' => wp_slash( kubio_serialize_blocks( parse_blocks( $content ) ) ), |
| 477 | ) |
| 478 | ); |
| 479 | update_post_meta($page_on_front, '_wp_page_template', ''); |
| 480 | } |
| 481 | |
| 482 | return intval( $page_on_front ); |
| 483 | } |
| 484 | |
| 485 | if ( ! is_string( $content ) ) { |
| 486 | $content = ''; |
| 487 | } |
| 488 | |
| 489 | return wp_insert_post( |
| 490 | array( |
| 491 | 'comment_status' => 'closed', |
| 492 | 'ping_status' => 'closed', |
| 493 | 'post_name' => 'front_page', |
| 494 | 'post_title' => __( 'Home', 'kubio' ), |
| 495 | 'post_status' => 'publish', |
| 496 | 'post_type' => 'page', |
| 497 | 'page_template' => apply_filters( |
| 498 | 'kubio/front_page_template', |
| 499 | 'kubio-full-width' |
| 500 | ), |
| 501 | 'post_content' => wp_slash( kubio_serialize_blocks( parse_blocks( $content ) ) ), |
| 502 | 'meta_input' => array( |
| 503 | '_kubio_created_at_activation' => 1, |
| 504 | ), |
| 505 | ) |
| 506 | ); |
| 507 | } |
| 508 | |
| 509 | public static function preparePrimaryMenu( $create_menu_items = true ) { |
| 510 | $theme_menu_locations = array_keys( get_registered_nav_menus() ); |
| 511 | $common_header_locations = array( |
| 512 | 'header-menu', |
| 513 | 'header', |
| 514 | 'primary', |
| 515 | 'main', |
| 516 | 'menu-1', |
| 517 | ); |
| 518 | |
| 519 | $selected_location = null; |
| 520 | /** |
| 521 | * Try to make an educated guess and primary menu location |
| 522 | */ |
| 523 | foreach ( $theme_menu_locations as $location ) { |
| 524 | foreach ( $common_header_locations as $common_header_location ) { |
| 525 | if ( stripos( $location, $common_header_location ) !== false ) { |
| 526 | $selected_location = $location; |
| 527 | break; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | if ( $selected_location ) { |
| 532 | break; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | $selected_location = apply_filters( 'kubio/primary_menu_location', $selected_location ); |
| 537 | |
| 538 | if ( $selected_location ) { |
| 539 | |
| 540 | $current_set_locations = get_nav_menu_locations(); |
| 541 | |
| 542 | $primary_menu_id = Arr::get( $current_set_locations, $selected_location, null ); |
| 543 | |
| 544 | if ( $create_menu_items ) { |
| 545 | |
| 546 | if ( ! $primary_menu_id ) { |
| 547 | $primary_menu_id = wp_create_nav_menu( __( 'Primary menu', 'kubio' ) ); |
| 548 | } |
| 549 | |
| 550 | if ( is_wp_error( $primary_menu_id ) ) { |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | $menu_items = wp_get_nav_menu_items( $primary_menu_id ); |
| 555 | $has_front_page = false; |
| 556 | $has_blog_page = false; |
| 557 | |
| 558 | foreach ( $menu_items as $menu_item ) { |
| 559 | |
| 560 | if ( ! $has_front_page ) { |
| 561 | $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' ) ); |
| 562 | $custom_url = $menu_item->type === 'custom' ? $menu_item->url : ''; |
| 563 | $menu_item_link_is_front_page = false; |
| 564 | $parsed_url = wp_parse_url( $custom_url ); |
| 565 | |
| 566 | if ( $parsed_url && $custom_url ) { |
| 567 | $site_url = site_url(); |
| 568 | |
| 569 | $parsed_url = array_merge( |
| 570 | array( |
| 571 | 'scheme' => '', |
| 572 | 'host' => '', |
| 573 | 'path' => '', |
| 574 | ), |
| 575 | $parsed_url |
| 576 | ); |
| 577 | |
| 578 | $menu_item_url = "{$parsed_url['scheme']}://{$parsed_url['host']}{$parsed_url['path']}"; |
| 579 | $menu_item_link_is_front_page = untrailingslashit( $menu_item_url ) === untrailingslashit( $site_url ); |
| 580 | } |
| 581 | |
| 582 | if ( $menu_item_object_is_front_page || $menu_item_link_is_front_page ) { |
| 583 | $has_front_page = true; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | if ( $menu_item->type === 'post_type' && $menu_item->object === 'page' && intval( $menu_item->object_id ) === intval( get_option( 'page_for_posts' ) ) ) { |
| 588 | $has_blog_page = true; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | if ( ! $has_front_page ) { |
| 593 | wp_update_nav_menu_item( |
| 594 | $primary_menu_id, |
| 595 | 0, |
| 596 | array( |
| 597 | 'menu-item-title' => __( 'Home', 'kubio' ), |
| 598 | 'menu-item-object' => 'page', |
| 599 | 'menu-item-object-id' => get_option( 'page_on_front' ), |
| 600 | 'menu-item-type' => 'post_type', |
| 601 | 'menu-item-status' => 'publish', |
| 602 | ) |
| 603 | ); |
| 604 | } |
| 605 | |
| 606 | if ( ! $has_blog_page && get_option( 'page_for_posts', 0 ) ) { |
| 607 | wp_update_nav_menu_item( |
| 608 | $primary_menu_id, |
| 609 | 0, |
| 610 | array( |
| 611 | 'menu-item-title' => __( 'Blog', 'kubio' ), |
| 612 | 'menu-item-object' => 'page', |
| 613 | 'menu-item-object-id' => get_option( 'page_for_posts' ), |
| 614 | 'menu-item-type' => 'post_type', |
| 615 | 'menu-item-status' => 'publish', |
| 616 | ) |
| 617 | ); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | if ( ! $primary_menu_id ) { |
| 622 | return; |
| 623 | } |
| 624 | |
| 625 | $next_nav_menu_locations = array_merge( |
| 626 | $current_set_locations, |
| 627 | array( |
| 628 | $selected_location => $primary_menu_id, |
| 629 | ) |
| 630 | ); |
| 631 | |
| 632 | if ( ! isset( $next_nav_menu_locations['header-menu'] ) ) { |
| 633 | $next_nav_menu_locations['header-menu'] = $primary_menu_id; |
| 634 | } |
| 635 | |
| 636 | set_theme_mod( 'nav_menu_locations', $next_nav_menu_locations ); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | public function importTemplates() { |
| 641 | $entities = array_keys( Importer::getAvailableTemplates() ); |
| 642 | |
| 643 | foreach ( $entities as $slug ) { |
| 644 | $is_current_kubio_template = apply_filters( 'kubio/template/is_importing_kubio_template', kubio_theme_has_kubio_block_support(), $slug ); |
| 645 | Importer::createTemplate( $slug, Importer::getTemplateContent( 'wp_template', $slug ), false, $is_current_kubio_template ? 'kubio' : 'theme' ); |
| 646 | } |
| 647 | |
| 648 | Flags::set( 'kubio_templates_imported', time() ); |
| 649 | } |
| 650 | |
| 651 | public function importTemplateParts() { |
| 652 | $entities = array_keys( Importer::getAvailableTemplateParts() ); |
| 653 | |
| 654 | foreach ( $entities as $slug ) { |
| 655 | $is_current_kubio_template = apply_filters( 'kubio/template/is_importing_kubio_template', kubio_theme_has_kubio_block_support(), $slug ); |
| 656 | Importer::createTemplatePart( $slug, Importer::getTemplateContent( 'wp_template_part', $slug ), false, $is_current_kubio_template ? 'kubio' : 'theme' ); |
| 657 | } |
| 658 | |
| 659 | Flags::set( 'kubio_template_parts_imported', time() ); |
| 660 | } |
| 661 | |
| 662 | public static function load() { |
| 663 | if ( ! self::$instance ) { |
| 664 | self::$instance = new self(); |
| 665 | } |
| 666 | |
| 667 | return self::$instance; |
| 668 | } |
| 669 | |
| 670 | public static function skipAfterSwitchTheme() { |
| 671 | set_transient( 'kubio_skip_after_theme_switch', true ); |
| 672 | } |
| 673 | |
| 674 | public function afterSwitchTheme() { |
| 675 | $skip = get_transient( 'kubio_skip_after_theme_switch' ); |
| 676 | |
| 677 | if ( $skip ) { |
| 678 | delete_transient( 'kubio_skip_after_theme_switch' ); |
| 679 | return; |
| 680 | } |
| 681 | |
| 682 | $this->addCommonFilters(); |
| 683 | |
| 684 | add_filter( 'kubio/importer/page_path', array( $this, 'getDesignPagePath' ), 10, 2 ); |
| 685 | $this->prepareRemoteData( true ); |
| 686 | |
| 687 | $this->importDesign(); |
| 688 | |
| 689 | $this->importTemplates(); |
| 690 | $this->importTemplateParts(); |
| 691 | |
| 692 | do_action( 'kubio/after_switch_theme' ); |
| 693 | } |
| 694 | |
| 695 | public function getAvailableTemplates( $current_templates = array() ) { |
| 696 | |
| 697 | $templates = $current_templates; |
| 698 | |
| 699 | if ( kubio_theme_has_kubio_block_support() ) { |
| 700 | $templates = Arr::get( $this->remote_content, 'block-templates', array() ); |
| 701 | |
| 702 | foreach ( array_keys( $templates ) as $template ) { |
| 703 | $templates[ $template ] = null; |
| 704 | } |
| 705 | |
| 706 | $templates = array_replace( $templates, $templates ); |
| 707 | } |
| 708 | |
| 709 | return $templates; |
| 710 | } |
| 711 | |
| 712 | public function getAvailableTemplateParts( $current_parts = array() ) { |
| 713 | |
| 714 | $templates = $current_parts; |
| 715 | |
| 716 | if ( kubio_theme_has_kubio_block_support() ) { |
| 717 | $templates = Arr::get( $this->remote_content, 'block-template-parts', array() ); |
| 718 | |
| 719 | foreach ( array_keys( $templates ) as $template ) { |
| 720 | $templates[ $template ] = null; |
| 721 | } |
| 722 | |
| 723 | $templates = array_replace( $templates, $templates ); |
| 724 | } |
| 725 | |
| 726 | return $templates; |
| 727 | } |
| 728 | |
| 729 | public function getDesignPagePath( $path, $slug ) { |
| 730 | if ( $slug === 'front-page' ) { |
| 731 | return null; |
| 732 | } |
| 733 | |
| 734 | return $path; |
| 735 | } |
| 736 | |
| 737 | public function updateBlocks( $content, $type, $slug ) { |
| 738 | $blocks = parse_blocks( $content ); |
| 739 | $blocks = Importer::maybeImportBlockAssets( $blocks ); |
| 740 | |
| 741 | if ( $type === 'wp_template_part' && strpos( $slug, 'footer' ) !== false ) { |
| 742 | $blocks = Importer::setBlocksLocks( $blocks, null ); |
| 743 | } |
| 744 | |
| 745 | return kubio_serialize_blocks( $blocks ); |
| 746 | } |
| 747 | |
| 748 | |
| 749 | |
| 750 | public function getFileContent( $content, $type, $slug ) { |
| 751 | |
| 752 | if ( $content !== null ) { |
| 753 | return $content; |
| 754 | } |
| 755 | |
| 756 | $category = ''; |
| 757 | switch ( $type ) { |
| 758 | case 'wp_template': |
| 759 | $category = 'block-templates'; |
| 760 | break; |
| 761 | case 'wp_template_part': |
| 762 | $category = 'block-template-parts'; |
| 763 | break; |
| 764 | case 'page': |
| 765 | $category = 'pages'; |
| 766 | break; |
| 767 | } |
| 768 | |
| 769 | return Arr::get( $this->remote_content, "{$category}.{$slug}", '' ); |
| 770 | } |
| 771 | |
| 772 | public function templateMapPartsTheme( $content, $type ) { |
| 773 | |
| 774 | if ( $type === 'wp_template' || $type === 'wp_template_part' ) { |
| 775 | $blocks = parse_blocks( $content ); |
| 776 | $updated_blocks = kubio_blocks_update_template_parts_theme( $blocks, get_stylesheet() ); |
| 777 | |
| 778 | return kubio_serialize_blocks( $updated_blocks ); |
| 779 | } |
| 780 | |
| 781 | return $content; |
| 782 | } |
| 783 | |
| 784 | public function importCustomizerOptions( $content, $type, $slug ) { |
| 785 | |
| 786 | if ( ! kubio_theme_has_kubio_block_support() ) { |
| 787 | return $content; |
| 788 | } |
| 789 | |
| 790 | $customizer_importer = new CustomizerImporter( $content, $type, $slug ); |
| 791 | |
| 792 | return $customizer_importer->process(); |
| 793 | } |
| 794 | } |
| 795 |