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