diff --git a/.github/workflows/brand-plugin-test.yml b/.github/workflows/brand-plugin-test.yml new file mode 100644 index 0000000..2def1f2 --- /dev/null +++ b/.github/workflows/brand-plugin-test.yml @@ -0,0 +1,58 @@ +name: Cypress Tests for Module Updates in Brand Plugin + +on: + pull_request: + types: [opened, reopened, ready_for_review, synchronize] + branches: + - main + - trunk + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }} + cancel-in-progress: true + +jobs: + setup: + name: Setup + runs-on: ubuntu-latest + outputs: + branch: ${{ steps.extract_branch.outputs.branch }} + steps: + - name: Extract branch name + shell: bash + run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT + id: extract_branch + + bluehost: + name: Bluehost Build and Test + needs: setup + uses: newfold-labs/workflows/.github/workflows/module-plugin-test.yml@main + with: + only-module-tests: true + module-repo: ${{ github.repository }} + module-branch: ${{ needs.setup.outputs.branch }} + plugin-repo: 'bluehost/bluehost-wordpress-plugin' + secrets: inherit + + hostgator: + name: HostGator Build and Test + needs: setup + uses: newfold-labs/workflows/.github/workflows/module-plugin-test.yml@main + with: + only-module-tests: true + module-repo: ${{ github.repository }} + module-branch: ${{ needs.setup.outputs.branch }} + plugin-repo: 'newfold-labs/wp-plugin-hostgator' + secrets: inherit + + crazydomains: + name: Crazy Domains Build and Test + needs: setup + uses: newfold-labs/workflows/.github/workflows/module-plugin-test.yml@main + with: + only-module-tests: true + module-repo: ${{ github.repository }} + module-branch: ${{ needs.setup.outputs.branch }} + plugin-repo: 'newfold-labs/wp-plugin-crazy-domains' + secrets: inherit \ No newline at end of file diff --git a/bootstrap.php b/bootstrap.php index 8f725e8..5dadbd3 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -28,6 +28,8 @@ function () { if ( 'atomic' === getContext( 'platform' ) ) { $brand = 'bh-cloud'; } + define( 'NFD_MIGRATION_PLUGIN_URL', $container->plugin()->url ); + defined( 'NFD_PROXY_ACCESS_WORKER' ) || define( 'NFD_PROXY_ACCESS_WORKER', 'https://hiive.cloud/workers/migration-token-proxy' ); defined( 'NFD_MIGRATION_PROXY_WORKER' ) || define( 'NFD_MIGRATION_PROXY_WORKER', 'https://migrate.bluehost.com' ); diff --git a/includes/Migration.php b/includes/Migration.php index 2484617..d8bc56d 100644 --- a/includes/Migration.php +++ b/includes/Migration.php @@ -61,6 +61,10 @@ function ( $listeners ) { add_action( 'rest_api_init', array( $this, 'register_routes' ) ); add_action( 'pre_update_option_nfd_migrate_site', array( $this, 'on_update_nfd_migrate_site' ) ); add_action( 'pre_update_option_instawp_last_migration_details', array( $this, 'on_update_instawp_last_migration_details' ), 10, 1 ); + if ( $container->plugin()->id === 'bluehost' ) { + add_action( 'admin_init', array( $this, 'register_wp_migration_tool' ) ); // Adds WordPress Migration tool to imports list + add_action( 'admin_enqueue_scripts', array( $this, 'set_import_tools' ) ); + } } /** @@ -114,4 +118,44 @@ public function register_settings() { ); } } + + /** + * Register WordPress Migration Tool to imports. + */ + public function register_wp_migration_tool() { + register_importer( + 'site_migration_wordpress_importer', + __( 'WordPress Migration Tool', 'wp-module-migration' ), + __( 'Migrate an existing WordPress site to this WordPress instance. This tool will make a copy of an existing site and automatically import it into this WordPress instance This will overwrite all the content.', 'wp-module-migration' ), + array( $this, 'wordpress_migration_tool' ) + ); + } + + /** + * Initiates the Migration service redirects it the instawp screen + */ + public function wordpress_migration_tool() { + $this->insta_service = new InstaMigrateService(); + $response = $this->insta_service->install_instawp_connect(); + if ( ! is_wp_error( $response ) ) { + wp_redirect( $response['redirect_url'] ); + } else { + wp_safe_redirect( admin_url( 'import.php' ) ); + } + die(); + } + + /** + * Changes the text WordPress to WordPress content in import page + */ + public function set_import_tools() { + \wp_enqueue_script( 'nfd_migration_tool', NFD_MIGRATION_PLUGIN_URL . 'vendor/newfold-labs/wp-module-migration/includes/import-tools-changes.js', array( 'jquery' ), '1.0', true ); + wp_enqueue_style( 'nfd_migration_tool', NFD_MIGRATION_PLUGIN_URL . 'vendor/newfold-labs/wp-module-migration/includes/styles.css', array(), '1.0', 'all' ); + $migration_data = array( + 'migration_title' => __( 'Preparing your site', 'wp-module-migration' ), + 'migration_description' => __( 'Please wait a few seconds while we get your new account ready to import your existing WordPress site.', 'wp-module-migration' ), + 'wordpress_title' => __( 'WordPress Content', 'wp-module-migration' ), + ); + wp_localize_script( 'nfd_migration_tool', 'migration', $migration_data ); + } } diff --git a/includes/Services/InstaMigrateService.php b/includes/Services/InstaMigrateService.php index c0b6690..ac35e93 100644 --- a/includes/Services/InstaMigrateService.php +++ b/includes/Services/InstaMigrateService.php @@ -24,6 +24,13 @@ class InstaMigrateService { * @var $insta_api_key */ private $insta_api_key = ''; + + /** + * Retry count + * + * @var $count + */ + private $count = 0; /** * Set required api keys for insta to initiate the migration */ @@ -57,7 +64,7 @@ public function install_instawp_connect() { } // Connect the website with InstaWP server - if ( empty( Helper::get_api_key() ) ) { + if ( empty( Helper::get_api_key() ) || empty( Helper::get_connect_id() ) ) { $api_key = Helper::get_api_key( false, $this->insta_api_key ); $connect_response = Helper::instawp_generate_api_key( $api_key ); @@ -73,7 +80,13 @@ public function install_instawp_connect() { if ( function_exists( 'instawp' ) ) { // Check if there is a connect ID if ( empty( Helper::get_connect_id() ) ) { - return new \WP_Error( 'Bad request', esc_html__( 'Connect plugin is installed but no connect ID.' ), array( 'status' => 400 ) ); + if ( $count < 2 ) { + $count++; + delete_option( 'instawp_api_options' ); + self::install_instawp_connect(); + } else { + return new \WP_Error( 'Bad request', esc_html__( 'Connect plugin is installed but no connect ID.' ), array( 'status' => 400 ) ); + } } return array( diff --git a/includes/import-tools-changes.js b/includes/import-tools-changes.js new file mode 100644 index 0000000..147e4ae --- /dev/null +++ b/includes/import-tools-changes.js @@ -0,0 +1,70 @@ +//Changes the text of the wordpress to wordpress content in import + const importer_titles = document.getElementsByClassName("importer-title"); + if( importer_titles ){ + [...importer_titles].forEach((val, index) => { + if( val?.outerText === 'WordPress'){ + document.getElementsByClassName("importer-title")[index].innerText = migration.wordpress_title + } + }) + } + + // designs a modal for migration tool + const node = document.createElement("div"); +node.innerHTML = `
+

${migration.migration_title}

+${migration.migration_description} +
`; + +node.style.position = "absolute"; +node.style.top = "0"; +node.style.bottom = "0"; +node.style.right = "0"; +node.style.left = "0"; +node.style.backgroundColor = "#ffffff5e"; +node.style.display = "none"; +node.style.alignItems = "center"; +node.style.justifyContent = "center"; +node.setAttribute("id", "migration-progress-modal") + +document.getElementById("wpbody-content").appendChild(node) + +function startMigration(){ + document.getElementById("migration-progress-modal").style.display = "flex"; + + fetch( + nfdplugin.restApiUrl + "/newfold-migration/v1/migrate/connect&_locale=user", + { + credentials: 'same-origin', + headers: { + 'Content-Type': 'application/json', + 'X-WP-Nonce': nfdplugin.restApiNonce, + }, + } + ) + .then((response) => response.json()) + .then(res => { + document.getElementById("migration-progress-modal").style.display = "none"; + if(res?.success){ + window.open(res?.data?.redirect_url, "_self") + } + // else{ + // document.getElementsByClassName("wrap")[0]; + // // alert("please try again in sometime. Thanks!") + // } + }) +} +// load a pop up when user clicks on run importer for wordpress migration tool + document.querySelector('a[href*="import=site_migration_wordpress_importer"]')?.addEventListener('click', function(e) { + e.preventDefault(); + startMigration(); + + }); + + + // trigger migration when user lands on tools page with migrate param +const params = new URLSearchParams(document.location.search); +const canMigrateSite = params.get("migrate"); + +if(canMigrateSite === "true"){ + startMigration() +} \ No newline at end of file diff --git a/includes/styles.css b/includes/styles.css new file mode 100644 index 0000000..220d897 --- /dev/null +++ b/includes/styles.css @@ -0,0 +1,48 @@ +.migrate-screen{ + box-shadow: 1px 2px 8px; + font-size: 15px; + display: flex; + flex-direction: column; + background-color: #fff; + padding: 24px 32px 32px 24px; + gap: 15px; + border-radius: 4px 0px 0px 0px; + margin-top: 1%; + max-width: max-content; +} + +.nfd-migration-loading{ + display: flex; + gap: 16px; + align-items: center; +} +.nfd-migration-title{ + display: inline-block; + font-family: Open Sans; + font-size: 20px; + font-weight: 600; + line-height: 27.24px; + text-align: left; +} + +.nfd-migration-loader { + display: inline-block; + border: 4px solid #949FB1; + border-radius: 50%; + border-top: 4px solid #0060F0; + width: 29px; + height: 28px; + -webkit-animation: spin 2s linear infinite; /* Safari */ + animation: spin 2s linear infinite; + } + + /* Safari */ + @-webkit-keyframes spin { + 0% { -webkit-transform: rotate(0deg); } + 100% { -webkit-transform: rotate(360deg); } + } + + @keyframes spin { + 0% { transform: rotate(0deg); } + 100% { transform: rotate(360deg); } + } \ No newline at end of file