Skip to content

Instantly share code, notes, and snippets.

@austinginder
Last active January 29, 2020 19:23
Show Gist options
  • Save austinginder/54c55adc2834be9c940597e3ebccd8e6 to your computer and use it in GitHub Desktop.
Save austinginder/54c55adc2834be9c940597e3ebccd8e6 to your computer and use it in GitHub Desktop.
Manually migrate data between alpha versions of CaptainCore. (in progress, not yet completed).
<?php
$users = array_column( get_users ( [ 'fields' => [ 'ID' ] ] ), "ID" );
$ids_accounts_json = "ids-accounts.json";
$ids_sites_json = "ids-sites.json";
$ids_domains_json = "ids-domains.json";
$ids_processes_json = "ids-processes.json";
$ids_processlogs_json = "ids-processlogs.json";
// Generate empty .json if needed
if ( ! file_exists( $ids_accounts_json ) ) {
file_put_contents( $ids_accounts_json, '[]' );
}
if ( ! file_exists( $ids_sites_json ) ) {
file_put_contents( $ids_sites_json, '[]' );
}
if ( ! file_exists( $ids_domains_json ) ) {
file_put_contents( $ids_domains_json, '[]' );
}
if ( ! file_exists( $ids_processes_json ) ) {
file_put_contents( $ids_processes_json, '[]' );
}
if ( ! file_exists( $ids_processlogs_json ) ) {
file_put_contents( $ids_processlogs_json, '[]' );
}
// Track legacy IDs in JSON files
$account_ids = json_decode( file_get_contents( $ids_accounts_json ) );
$account_legacy_ids = array_column( $account_ids, 'legacy_id' );
$site_ids = json_decode( file_get_contents( $ids_sites_json ) );
$site_legacy_ids = array_column( $site_ids, 'legacy_id' );
$domain_ids = json_decode( file_get_contents( $ids_domains_json ) );
$domain_legacy_ids = array_column( $domain_ids, 'legacy_id' );
$process_ids = json_decode( file_get_contents( $ids_processes_json ) );
$process_legacy_ids = array_column( $process_ids, 'legacy_id' );
$process_log_ids = json_decode( file_get_contents( $ids_processlogs_json ) );
$process_log_legacy_ids = array_column( $process_log_ids, 'legacy_id' );
$process = new CaptainCore\Processes();
// Fetch legacy CPT items
$processes = get_posts( [
'order' => 'asc',
'orderby' => 'title',
'posts_per_page' => '-1',
'post_type' => 'captcore_process',
'fields' => 'ids',
] );
foreach( $processes as $process_id ) {
$process_legacy = ( new CaptainCore\Process( $process_id ) )->get_legacy();
if ( in_array( $process_id, $process_legacy_ids ) ) {
$key = array_search( $process_id, array_column( $process_ids, 'legacy_id' ) );
$process_id_new = $process_ids[$key]->new_id;
// Updating existing site within custom table
echo "Updating process #{$process_id_new}\n";
$process_legacy->updated_at = date("Y-m-d H:i:s");
$process->update( (array) $process_legacy, array( "process_id" => $process_id_new ) );
continue;
}
// Creating new account in custom table
echo "Creating process for legacy #{$process_id}\n";
$time_now = date("Y-m-d H:i:s");
$process_legacy->updated_at = $time_now;
$process_id_new = $process->insert( (array) $process_legacy );
// Track both legacy and new IDs
$process_ids[] = [
"legacy_id" => $process_id,
"new_id" => $process_id_new
];
}
$process_log = new CaptainCore\ProcessLogs();
// Fetch legacy CPT items
$process_logs = get_posts( [
'order' => 'asc',
'orderby' => 'title',
'posts_per_page' => '-1',
'post_type' => 'captcore_processlog',
'fields' => 'ids',
] );
foreach( $process_logs as $process_log_id ) {
$process_log_legacy = ( new CaptainCore\ProcessLog( $process_log_id ) )->get_legacy();
if ( in_array( $process_log_id, $process_log_legacy_ids ) ) {
$key = array_search( $process_log_id, $process_log_legacy_ids );
$process_log_id_new = $process_log_ids[$key]->new_id;
// Update process_id to new process_id
$key = array_search( $process_log_legacy->process_id[0], $process_legacy_ids );
$process_log_legacy->process_id = $process_ids[$key]->new_id;
// Updating existing site within custom table
echo "Updating process log #{$process_log_id_new}\n";
$process_log_legacy->updated_at = date("Y-m-d H:i:s");
$process_log->update( (array) $process_log_legacy, [ "process_log_id" => $process_log_id_new ] );
// Prepare permissions
$legacy_permission_ids = get_post_meta( $process_log_id, 'website', true );
$new_permission_ids = [];
if ( is_array( $legacy_permission_ids ) ) {
foreach( $legacy_permission_ids as $legacy_permission_id ) {
$key = array_search( $legacy_permission_id, $site_legacy_ids );
$new_permission_ids[] = $site_ids[$key]->new_id;
}
}
// Migrate permissions
(new CaptainCore\ProcessLog( $process_log_id_new ) )->insert_sites( $new_permission_ids );
continue;
}
// Creating new account in custom table
echo "Creating process log for legacy #{$process_log_id}\n";
$time_now = date("Y-m-d H:i:s");
$process_log_legacy->updated_at = $time_now;
$process_log_id_new = $process_log->insert( (array) $process_log_legacy );
// Track both legacy and new IDs
$process_log_ids[] = [
"legacy_id" => $process_log_id,
"new_id" => $process_log_id_new
];
}
// Migrate $hosting_plans to from ACF options to wp_option.captaincore_hosting_plans
$hosting_plans = get_field( 'hosting_plans', 'option' );
foreach ($hosting_plans as $key => $hosting_plan) {
// update for new format
$hosting_plans[$key] = [
"name" => $hosting_plan["name"],
"price" => $hosting_plan["price"],
"limits" => [
"visits" => $hosting_plan["visits_limit"],
"storage" => $hosting_plan["storage_limit"],
"sites" => $hosting_plan["sites_limit"],
]
];
}
update_option( 'captaincore_hosting_plans', json_encode( $hosting_plans ) );
// Migrate process roles from taxonomnies to wp_option.captaincore_process_roles
$process_roles = ( new CaptainCore\Process )->get_legacy_roles();
$process_repeat_field = get_field_object( 'field_57f791d6363f4' );
$process_repeat = $process_repeat_field['choices'];
update_option( 'captaincore_process_roles', json_encode( $process_roles ) );
update_option( 'captaincore_process_repeat', json_encode( $process_repeat ) );
$account = new CaptainCore\Accounts();
// Fetch legacy CPT items
$accounts = get_posts( [
'order' => 'asc',
'orderby' => 'title',
'posts_per_page' => '-1',
'post_type' => 'captcore_customer',
'fields' => 'ids',
] );
foreach( $accounts as $account_id ) {
$account_legacy = ( new CaptainCore\AccountCPT( $account_id, true ) )->get_raw();
if ( in_array( $account_id, $account_legacy_ids ) ) {
$key = array_search( $account_id, array_column( $account_ids, 'legacy_id' ) );
$account_id_new = $account_ids[$key]->new_id;
$processed_invites = $account_ids[$key]->processed_invites;
if ( $processed_invites != true ) {
$invites_db = new CaptainCore\Invites();
$invites = $invites_db->where( [ "account_id" => $account_id ] );
foreach ($invites as $invite) {
$invites_db->update( [ "account_id" => $account_id_new ], [ "invite_id" => $invite->invite_id ] );
}
$account_ids[$key]->processed_invites = true;
}
// Updating existing site within custom table
echo "Updating account #{$account_id_new} {$account_legacy->name}\n";
$account_legacy->updated_at = date("Y-m-d H:i:s");
$account->update( (array) $account_legacy, array( "account_id" => $account_id_new ) );
continue;
}
// Creating new account in custom table
echo "Creating account {$account_legacy->name}\n";
$time_now = date("Y-m-d H:i:s");
$account_legacy->updated_at = $time_now;
$account_id_new = $account->insert( (array) $account_legacy );
// Track both legacy and new IDs
$account_ids[] = [
"legacy_id" => $account_id,
"new_id" => $account_id_new
];
}
$upload_dir = wp_upload_dir();
$upload_dir_base = $upload_dir["basedir"];
$site = new CaptainCore\Sites();
$sites = get_posts( [
'order' => 'asc',
'orderby' => 'title',
'posts_per_page' => '-1',
'post_type' => 'captcore_website',
'fields' => 'ids',
] );
foreach( $sites as $site_id ) {
$site_legacy = ( new CaptainCore\SiteCPT( $site_id ) )->get_raw();
$key = array_search( $site_id, array_column( $site_ids, 'legacy_id' ) );
$processed_environments = $site_ids[$key]->processed_environments;
// Fetch relating environments
$db_environments = new CaptainCore\Environments();
$environments = $db_environments->fetch_environments( $site_id );
// If environments processed then pull details from new SiteID
if ( $processed_environments == true ) {
$site_legacy = ( new CaptainCore\SiteCPT( $site_id ) )->get_raw( $site_ids[$key]->new_id );
$environments = $db_environments->fetch_environments( $site_ids[$key]->new_id );
}
// Prepare permissions
$legacy_permission_ids = get_post_meta( $site_id, 'partner', true );
$new_permission_ids = [];
if ( is_array( $legacy_permission_ids ) ) {
foreach( $legacy_permission_ids as $legacy_permission_id ) {
$key = array_search( $legacy_permission_id, array_column( $account_ids, 'legacy_id' ) );
$new_permission_ids[] = $account_ids[$key]->new_id;
}
}
// Patch in screenshot
$screenshots = (object) [];
if ( isset($environments) && intval( $environments[0]->screenshot ) ) {
$site_legacy->screenshot = true;
} else {
$site_legacy->screenshot = false;
}
if ( in_array( $site_id, $site_legacy_ids ) ) {
// Move screenshots if needed
$key = array_search( $site_id, array_column( $site_ids, 'legacy_id' ) );
$site_id_new = $site_ids[$key]->new_id;
$processed_screenshots = $site_ids[$key]->processed_screenshots;
if ( $processed_screenshots != true ) {
$old_directory = "{$upload_dir_base}/screenshots/{$site_legacy->site}_{$site_id}/";
$new_directory = "{$upload_dir_base}/screenshots/{$site_legacy->site}_{$site_id_new}/";
if ( file_exists( $old_directory ) ) {
rename( $old_directory, $new_directory );
$site_ids[$key]->processed_screenshots = true;
}
}
if ( $processed_environments != true ) {
foreach( array_column( $environments, "environment_id" ) as $environment_id ) {
$db_environments->update( [ "site_id" => $site_id_new ], [ "environment_id" => $environment_id ] );
}
$site_ids[$key]->processed_environments = true;
}
$key = array_search( $site_legacy->account_id, array_column( $account_ids, 'legacy_id' ) );
if ( $key ) {
$site_legacy->account_id = $account_ids[$key]->new_id;
} else {
$site_legacy->account_id = "";
}
// Updating existing site within custom table
echo "Updating site #{$site_id_new} {$site_legacy->name}\n";
$site_legacy->updated_at = date("Y-m-d H:i:s");
$site->update( (array) $site_legacy, array( "site_id" => $site_id_new ) );
// Migrate permissions
(new CaptainCore\Site( $site_id_new ) )->assign_accounts( $new_permission_ids );
continue;
}
// Creating new site in custom table
echo "Creating site {$site_legacy->name}\n";
$time_now = date("Y-m-d H:i:s");
$site_legacy->updated_at = $time_now;
$site_id_new = $site->insert( (array) $site_legacy );
// Migrate permissions
// ( new CaptainCore\Site( $site_id_new ) )->insert_accounts( $new_permission_ids );
// Track both legacy and new IDs
$site_ids[] = [
"legacy_id" => $site_id,
"new_id" => $site_id_new
];
}
// Migrate domains
// Fetch legacy CPT items
$domains = get_posts( [
'order' => 'asc',
'orderby' => 'title',
'posts_per_page' => '-1',
'post_type' => 'captcore_domain',
'fields' => 'ids',
] );
$domain = new CaptainCore\Domains;
foreach( $domains as $domain_id ) {
$domain_legacy = ( new CaptainCore\Domain( $domain_id ) )->get_legacy();
// Prepare permissions
$legacy_permission_ids = $domain_legacy->permissions;
unset( $domain_legacy->permissions );
$new_permission_ids = [];
if ( is_array( $legacy_permission_ids ) ) {
foreach( $legacy_permission_ids as $legacy_permission_id ) {
$key = array_search( $legacy_permission_id, array_column( $account_ids, 'legacy_id' ) );
$new_permission_ids[] = $account_ids[$key]->new_id;
}
}
if ( in_array( $domain_id, $domain_legacy_ids ) ) {
$key = array_search( $domain_id, array_column( $domain_ids, 'legacy_id' ) );
$domain_id_new = $domain_ids[$key]->new_id;
// Updating existing site within custom table
echo "Updating domain #{$domain_id_new} {$domain_legacy->name}\n";
$domain_legacy->updated_at = date("Y-m-d H:i:s");
$domain->update( (array) $domain_legacy, array( "domain_id" => $domain_id_new ) );
// Migrate permissions
(new CaptainCore\Domain( $domain_id_new ) )->assign_accounts( $new_permission_ids );
continue;
}
// Creating new account in custom table
echo "Creating domain {$domain_legacy->name}\n";
$time_now = date("Y-m-d H:i:s");
$domain_legacy->updated_at = $time_now;
$domain_id_new = $domain->insert( (array) $domain_legacy );
// Migrate permissions
//( new CaptainCore\Domain( $domain_id_new ) )->insert_accounts( $new_permission_ids );
// Track both legacy and new IDs
$domain_ids[] = [
"legacy_id" => $domain_id,
"new_id" => $domain_id_new
];
}
// Migrate permissions
foreach( $users as $user_id ) {
echo "Migrating permissions user #{$user_id}\n";
$new_permissions = [];
$current_permissions = get_user_meta( $user_id, "partner", true );
if ( ! is_array( $current_permissions ) ) {
continue;
}
foreach ( $current_permissions as $account_id ) {
$key = array_search( $account_id, array_column( $account_ids, 'legacy_id' ) );
$new_permissions[] = $account_ids[$key]->new_id;
}
$user = new CaptainCore\User( $user_id, true );
$user->assign_accounts( $new_permissions );
//update_user_meta( $user_id, "accounts", $new_permissions );
}
// Really time consuming task of switchover all relating data over to new site ids based on environments IDs
$captures = ( new CaptainCore\Captures() )->all();
foreach ($captures as $capture) {
$site_id = ( new CaptainCore\Environments )->get( $capture->environment_id )->site_id;
if ( intval( $site_id ) > 0 ) {
( new CaptainCore\Captures() )->update( [ "site_id" => $site_id ], [ "capture_id" => $capture->capture_id ] );
}
}
$quicksaves = ( new CaptainCore\Quicksaves() )->all();
foreach ($quicksaves as $quicksave) {
$site_id = ( new CaptainCore\Environments )->get( $quicksave->environment_id )->site_id;
if ( intval( $site_id ) > 0 ) {
( new CaptainCore\Quicksaves() )->update( [ "site_id" => $site_id ], [ "quicksave_id" => $quicksave->quicksave_id ] );
}
}
$snapshots = ( new CaptainCore\Snapshots() )->all();
foreach ($snapshots as $snapshot) {
$site_id = ( new CaptainCore\Environments )->get( $snapshot->environment_id )->site_id;
if ( intval( $site_id ) > 0 ) {
( new CaptainCore\Snapshots() )->update( [ "site_id" => $site_id ], [ "snapshot_id" => $snapshot->snapshot_id ] );
}
}
$update_logs = ( new CaptainCore\UpdateLogs() )->all();
foreach ($update_logs as $update_log) {
$site_id = ( new CaptainCore\Environments )->get( $update_log->environment_id )->site_id;
if ( intval( $site_id ) > 0 ) {
( new CaptainCore\UpdateLogs() )->update( [ "site_id" => $site_id ], [ "log_id" => $update_log->log_id ] );
}
}
// Update .json file
file_put_contents( $ids_accounts_json, json_encode( $account_ids, JSON_PRETTY_PRINT ) );
file_put_contents( $ids_sites_json, json_encode( $site_ids, JSON_PRETTY_PRINT ) );
file_put_contents( $ids_domains_json, json_encode( $domain_ids, JSON_PRETTY_PRINT ) );
file_put_contents( $ids_processes_json, json_encode( $process_ids, JSON_PRETTY_PRINT ) );
file_put_contents( $ids_processlogs_json, json_encode( $process_log_ids, JSON_PRETTY_PRINT ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment