| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Migration; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Database\IntegrationModel; |
| 6 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 7 |
|
| 8 |
final class MigrateForms |
| 9 |
{ |
| 10 |
public function migrateToV2() |
| 11 |
{ |
| 12 |
$v1FormContents = get_transient('bitforms_v1_form_contents'); |
| 13 |
$newFormContents = []; |
| 14 |
foreach ($v1FormContents as $formContents) { |
| 15 |
$migrationHandler = new Migration(json_decode(json_encode($formContents))); |
| 16 |
$newFormContents[] = $migrationHandler->migrate(); |
| 17 |
} |
| 18 |
self::migratePaypal(); |
| 19 |
$config = (object) []; |
| 20 |
$config->delete_table = true; |
| 21 |
$this->duplicateV1StyleFiles(); |
| 22 |
update_option('bitform_app_config', $config); |
| 23 |
return $newFormContents; |
| 24 |
} |
| 25 |
|
| 26 |
public function rollbackToV1() |
| 27 |
{ |
| 28 |
$this->dropTableAndRename('form', 'form_v1'); |
| 29 |
$this->dropTableAndRename('workflows', 'workflows_v1'); |
| 30 |
$this->dropTableAndRename('success_messages', 'success_messages_v1'); |
| 31 |
$source = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles'; |
| 32 |
MigrationHelper::recursiveDeleteFiles($source); |
| 33 |
$destination = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles-v1'; |
| 34 |
MigrationHelper::recursiveCopyFiles($destination, $source); |
| 35 |
MigrationHelper::recursiveDeleteFiles($destination); |
| 36 |
if (file_exists($destination)) { |
| 37 |
rmdir($destination); |
| 38 |
} |
| 39 |
delete_option('bitforms_migrating_to_v2'); |
| 40 |
delete_option('bitforms_migrated_to_v2'); |
| 41 |
} |
| 42 |
|
| 43 |
private function duplicateV1StyleFiles() |
| 44 |
{ |
| 45 |
$source = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles'; |
| 46 |
$destination = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles-v1'; |
| 47 |
if (!file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles-v1')) { |
| 48 |
wp_mkdir_p(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles-v1'); |
| 49 |
} |
| 50 |
MigrationHelper::recursiveCopyFiles($source, $destination); |
| 51 |
MigrationHelper::recursiveDeleteFiles($source); |
| 52 |
} |
| 53 |
|
| 54 |
private static function migratePaypal() |
| 55 |
{ |
| 56 |
$integrationHandler = new IntegrationHandler(0); |
| 57 |
$formIntegrations = $integrationHandler->getAllIntegration('app', 'payments'); |
| 58 |
if (!is_wp_error($formIntegrations)) { |
| 59 |
return false; |
| 60 |
} |
| 61 |
$integrationModel = new IntegrationModel(); |
| 62 |
|
| 63 |
foreach ($formIntegrations as $payment) { |
| 64 |
$integrationDetails = json_decode($payment->integration_details); |
| 65 |
if ('PayPal' === $integrationDetails->type) { |
| 66 |
$integrationDetails->mode = 'live'; |
| 67 |
$integrationDetails->clientSecret = ''; |
| 68 |
$integrationModel->update([ |
| 69 |
'integration_details' => json_encode($integrationDetails), |
| 70 |
], [ |
| 71 |
'id' => $payment->id, |
| 72 |
]); |
| 73 |
} |
| 74 |
} |
| 75 |
return true; |
| 76 |
} |
| 77 |
|
| 78 |
private function dropTableAndRename($deletedTableName, $renamedTableName) |
| 79 |
{ |
| 80 |
global $wpdb; |
| 81 |
$deleteTable = "{$wpdb->prefix}bitforms_{$deletedTableName}"; |
| 82 |
$renameTable = "{$wpdb->prefix}bitforms_{$renamedTableName}"; |
| 83 |
$wpdb->query("DROP TABLE IF EXISTS $deleteTable"); |
| 84 |
$wpdb->query("RENAME TABLE $renameTable TO $deleteTable"); |
| 85 |
} |
| 86 |
} |
| 87 |
|