TransferController.php
51 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\FormMigration\Controllers; |
| 4 | |
| 5 | use Give\DonationForms\V2\Models\DonationForm; |
| 6 | use Give\DonationForms\ValueObjects\DonationFormStatus; |
| 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 Give\Framework\QueryBuilder\QueryBuilder; |
| 13 | use WP_REST_Request; |
| 14 | use WP_REST_Response; |
| 15 | |
| 16 | class TransferController |
| 17 | { |
| 18 | protected $debugContext; |
| 19 | |
| 20 | /** |
| 21 | * @var WP_REST_Request |
| 22 | */ |
| 23 | protected $request; |
| 24 | |
| 25 | public function __construct(WP_REST_Request $request) |
| 26 | { |
| 27 | $this->request = $request; |
| 28 | } |
| 29 | |
| 30 | public function __invoke(DonationForm $formV2, TransferOptions $options) |
| 31 | { |
| 32 | DB::transaction(function() use ($formV2, $options) { |
| 33 | |
| 34 | $v3FormId = (new GetMigratedFormId)($formV2->id); |
| 35 | TransferFormUrl::from($formV2->id)->to($v3FormId); |
| 36 | TransferDonations::from($formV2->id)->to($v3FormId); |
| 37 | |
| 38 | if($options->shouldDelete()) { |
| 39 | wp_trash_post($formV2->id); |
| 40 | } |
| 41 | |
| 42 | wp_update_post(['ID' => $v3FormId, 'post_status' => $formV2->status->getValue()]); |
| 43 | give_update_meta($v3FormId, 'transferredFormId', true); |
| 44 | }); |
| 45 | |
| 46 | return new WP_REST_Response(array('errors' => [], 'successes' => [ |
| 47 | $formV2->id |
| 48 | ])); |
| 49 | } |
| 50 | } |
| 51 |