| 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(wp_json_encode($formContents))); |
| 16 |
$newFormContents[] = $migrationHandler->migrate(); |
| 17 |
} |
| 18 |
self::migratePaypal(); |
| 19 |
$config = (object) []; |
| 20 |
$config->delete_table = true; |
| 21 |
update_option('bitform_app_config', $config); |
| 22 |
set_transient('bitforms_v1_form_contents', $newFormContents); |
| 23 |
return $newFormContents; |
| 24 |
} |
| 25 |
|
| 26 |
private static function migratePaypal() |
| 27 |
{ |
| 28 |
$integrationHandler = new IntegrationHandler(0); |
| 29 |
$formIntegrations = $integrationHandler->getAllIntegration('app', 'payments'); |
| 30 |
if (!is_wp_error($formIntegrations)) { |
| 31 |
return false; |
| 32 |
} |
| 33 |
$integrationModel = new IntegrationModel(); |
| 34 |
|
| 35 |
foreach ($formIntegrations as $payment) { |
| 36 |
$integrationDetails = json_decode($payment->integration_details); |
| 37 |
if ('PayPal' === $integrationDetails->type) { |
| 38 |
$integrationDetails->mode = 'live'; |
| 39 |
$integrationDetails->clientSecret = ''; |
| 40 |
$integrationModel->update([ |
| 41 |
'integration_details' => wp_json_encode($integrationDetails), |
| 42 |
], [ |
| 43 |
'id' => $payment->id, |
| 44 |
]); |
| 45 |
} |
| 46 |
} |
| 47 |
return true; |
| 48 |
} |
| 49 |
|
| 50 |
private function dropTableAndRename($deletedTableName, $renamedTableName) |
| 51 |
{ |
| 52 |
global $wpdb; |
| 53 |
$deleteTable = "{$wpdb->prefix}bitforms_{$deletedTableName}"; |
| 54 |
$renameTable = "{$wpdb->prefix}bitforms_{$renamedTableName}"; |
| 55 |
// $wpdb->query("DROP TABLE IF EXISTS $deleteTable"); |
| 56 |
// $wpdb->query("RENAME TABLE $renameTable TO $deleteTable"); |
| 57 |
$wpdb->query( |
| 58 |
$wpdb->prepare( |
| 59 |
"DROP TABLE IF EXISTS $deleteTable" |
| 60 |
) |
| 61 |
); |
| 62 |
$wpdb->query( |
| 63 |
$wpdb->prepare( |
| 64 |
"RENAME TABLE $renameTable TO $deleteTable" |
| 65 |
) |
| 66 |
); |
| 67 |
} |
| 68 |
} |
| 69 |
|