| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\FormMigration\Controllers; |
| 4 |
|
| 5 |
use Give\Campaigns\Repositories\CampaignRepository; |
| 6 |
use Give\DonationForms\V2\Models\DonationForm; |
| 7 |
use Give\FormMigration\Actions\GetMigratedFormId; |
| 8 |
use Give\FormMigration\Actions\TransferDonations; |
| 9 |
use Give\FormMigration\Actions\TransferFormUrl; |
| 10 |
use Give\FormMigration\DataTransferObjects\TransferOptions; |
| 11 |
use Give\Framework\Database\DB; |
| 12 |
use WP_REST_Request; |
| 13 |
use WP_REST_Response; |
| 14 |
|
| 15 |
class TransferController |
| 16 |
{ |
| 17 |
protected $debugContext; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var WP_REST_Request |
| 21 |
*/ |
| 22 |
protected $request; |
| 23 |
|
| 24 |
public function __construct(WP_REST_Request $request) |
| 25 |
{ |
| 26 |
$this->request = $request; |
| 27 |
} |
| 28 |
|
| 29 |
public function __invoke(DonationForm $formV2, TransferOptions $options) |
| 30 |
{ |
| 31 |
DB::transaction(function() use ($formV2, $options) { |
| 32 |
|
| 33 |
$v3FormId = (new GetMigratedFormId)($formV2->id); |
| 34 |
TransferFormUrl::from($formV2->id)->to($v3FormId); |
| 35 |
TransferDonations::from($formV2->id)->to($v3FormId); |
| 36 |
|
| 37 |
// Promote upgraded form to default form |
| 38 |
$campaignRepository = give(CampaignRepository::class); |
| 39 |
if ($campaign = $campaignRepository->getByFormId($formV2->id)) { |
| 40 |
$defaultForm = $campaign->defaultForm(); |
| 41 |
|
| 42 |
if ($defaultForm->id === $formV2->id) { |
| 43 |
$campaignRepository->updateDefaultCampaignForm($campaign, $v3FormId); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
if($options->shouldDelete()) { |
| 48 |
wp_trash_post($formV2->id); |
| 49 |
} |
| 50 |
|
| 51 |
wp_update_post(['ID' => $v3FormId, 'post_status' => $formV2->status->getValue()]); |
| 52 |
give_update_meta($v3FormId, 'transferredFormId', true); |
| 53 |
}); |
| 54 |
|
| 55 |
return new WP_REST_Response(array('errors' => [], 'successes' => [ |
| 56 |
$formV2->id |
| 57 |
])); |
| 58 |
} |
| 59 |
} |
| 60 |
|