Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PRESS0-1877 | Added Migration flow to Tools page #63

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
58 changes: 58 additions & 0 deletions .github/workflows/brand-plugin-test.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

Expand Down
44 changes: 44 additions & 0 deletions includes/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}
}

/**
Expand Down Expand Up @@ -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 <strong>This will overwrite all the content.</strong>', '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 );
}
}
17 changes: 15 additions & 2 deletions includes/Services/InstaMigrateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 );

Expand All @@ -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(
Expand Down
70 changes: 70 additions & 0 deletions includes/import-tools-changes.js
Original file line number Diff line number Diff line change
@@ -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 = `<div class='migrate-screen'>
<div class='nfd-migration-loading'><span class='nfd-migration-loader'></span><h2 class='nfd-migration-title'>${migration.migration_title}</h2></div>
${migration.migration_description}
</div>`;

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()
}
48 changes: 48 additions & 0 deletions includes/styles.css
Original file line number Diff line number Diff line change
@@ -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); }
}
Loading