| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\FormMigration\Commands; |
| 4 |
|
| 5 |
use Give\DonationForms\Models\DonationForm as DonationFormV3; |
| 6 |
use Give\DonationForms\V2\Models\DonationForm as DonationFormV2; |
| 7 |
use Give\FormMigration\Actions\TransferDonations; |
| 8 |
use Give\FormMigration\Actions\TransferFormUrl; |
| 9 |
use Give\FormMigration\Concerns\Blocks\BlockDifference; |
| 10 |
use Give\FormMigration\DataTransferObjects\FormMigrationPayload; |
| 11 |
use Give\FormMigration\DataTransferObjects\TransferOptions; |
| 12 |
use Give\FormMigration\Pipeline; |
| 13 |
use Give\Framework\Blocks\BlockModel; |
| 14 |
use Give\Framework\Database\DB; |
| 15 |
use WP_CLI; |
| 16 |
|
| 17 |
use function WP_CLI\Utils\get_flag_value; |
| 18 |
|
| 19 |
class TransferCommand |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Prints a greeting. |
| 23 |
* |
| 24 |
* ## OPTIONS |
| 25 |
* |
| 26 |
* <id> |
| 27 |
* : A form ID to transfer donations |
| 28 |
* |
| 29 |
* [--dry-run] |
| 30 |
* : Whether to dry run |
| 31 |
* |
| 32 |
* [--changeUrl] |
| 33 |
* : Whether to change the URL |
| 34 |
* |
| 35 |
* [--delete] |
| 36 |
* : Whether to delete the old form |
| 37 |
* |
| 38 |
* [--redirect] |
| 39 |
* : Whether to redirect the old form in shortcodes and blocks |
| 40 |
*/ |
| 41 |
public function __invoke( $args, $assoc_args ) |
| 42 |
{ |
| 43 |
[$formIdV3] = $args; |
| 44 |
|
| 45 |
$sourceId = give_get_meta($formIdV3, 'migratedFormId', true); |
| 46 |
|
| 47 |
|
| 48 |
$options = TransferOptions::fromArray([ |
| 49 |
'delete' => (bool) get_flag_value($assoc_args, 'delete'), |
| 50 |
]); |
| 51 |
|
| 52 |
$isDryRun = get_flag_value($assoc_args, 'dry-run'); |
| 53 |
|
| 54 |
$count = DB::table('give_revenue') |
| 55 |
->where('form_id', $sourceId) |
| 56 |
->count(); |
| 57 |
|
| 58 |
try { |
| 59 |
DB::transaction(function() use ($formIdV3, $sourceId, $options, $isDryRun) { |
| 60 |
TransferFormUrl::from($sourceId)->to($formIdV3); |
| 61 |
TransferDonations::from($sourceId)->to($formIdV3); |
| 62 |
|
| 63 |
if($options->shouldDelete()) { |
| 64 |
wp_trash_post($sourceId); |
| 65 |
} |
| 66 |
|
| 67 |
give_update_meta($formIdV3, 'transferredFormId', true); |
| 68 |
|
| 69 |
if($isDryRun) { |
| 70 |
DB::rollback(); |
| 71 |
} |
| 72 |
}); |
| 73 |
WP_CLI::success(sprintf('Transferred %s donation(s).', $count)); |
| 74 |
} catch( \Exception $e ) { |
| 75 |
WP_CLI::error('Failed to transfer donations.'); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|