| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\FormMigration\Controllers; |
| 4 |
|
| 5 |
use Give\Campaigns\Repositories\CampaignRepository; |
| 6 |
use Give\Campaigns\ValueObjects\CampaignType; |
| 7 |
use Give\DonationForms\V2\Models\DonationForm; |
| 8 |
use Give\FormMigration\Concerns\Blocks\BlockDifference; |
| 9 |
use Give\FormMigration\DataTransferObjects\FormMigrationPayload; |
| 10 |
use Give\FormMigration\Pipeline; |
| 11 |
use Give\Framework\Blocks\BlockModel; |
| 12 |
use Give\Framework\Database\DB; |
| 13 |
use Give\Log\Log; |
| 14 |
use WP_REST_Request; |
| 15 |
use WP_REST_Response; |
| 16 |
|
| 17 |
class MigrationController |
| 18 |
{ |
| 19 |
protected $debugContext; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var WP_REST_Request |
| 23 |
*/ |
| 24 |
protected $request; |
| 25 |
|
| 26 |
public function __construct(WP_REST_Request $request) |
| 27 |
{ |
| 28 |
$this->request = $request; |
| 29 |
} |
| 30 |
|
| 31 |
public function __invoke(DonationForm $formV2) |
| 32 |
{ |
| 33 |
$payload = FormMigrationPayload::fromFormV2($formV2); |
| 34 |
|
| 35 |
give(Pipeline::class) |
| 36 |
->afterEach(function($stepClass, $payload, $_payload) { |
| 37 |
(new BlockDifference($_payload->formV3->blocks)) |
| 38 |
->skip('givewp/section') |
| 39 |
->onBlockAdded(function(BlockModel $block) { |
| 40 |
$this->debugContext[] = [ |
| 41 |
'ADDED' => $block->name, |
| 42 |
'ATTRIBUTES' => $block->getAttributes(), |
| 43 |
]; |
| 44 |
}) |
| 45 |
->onBlockDifference(function(BlockModel $block, $differences) { |
| 46 |
$this->debugContext[] = [ |
| 47 |
'UPDATED' => $block->name, |
| 48 |
'ATTRIBUTES' => $differences, |
| 49 |
]; |
| 50 |
}) |
| 51 |
->diff($payload->formV3->blocks); |
| 52 |
}) |
| 53 |
->process($payload) |
| 54 |
->finally(function(FormMigrationPayload $payload) { |
| 55 |
$payload->formV3->save(); |
| 56 |
|
| 57 |
// Associate upgraded form to a campaign |
| 58 |
$campaignRepository = give(CampaignRepository::class); |
| 59 |
if ($campaign = $campaignRepository->getByFormId($payload->formV2->id)) { |
| 60 |
$campaignRepository->addCampaignForm($campaign, $payload->formV3->id); |
| 61 |
} else { |
| 62 |
// Fallback: Check for non-core campaigns (e.g., P2P) linked via give_campaigns.form_id |
| 63 |
$campaignData = DB::table('give_campaigns') |
| 64 |
->where('form_id', $payload->formV2->id) |
| 65 |
->where('campaign_type', CampaignType::CORE, '!=') |
| 66 |
->get(); |
| 67 |
|
| 68 |
if ($campaignData) { |
| 69 |
DB::table('give_campaign_forms') |
| 70 |
->insert([ |
| 71 |
'form_id' => $payload->formV3->id, |
| 72 |
'campaign_id' => $campaignData->id, |
| 73 |
]); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
Log::info(esc_html__('Form migrated from v2 to v3.', 'give'), $this->debugContext); |
| 78 |
}); |
| 79 |
|
| 80 |
$redirectArgs = apply_filters('givewp_form_migration_redirect_args', [ |
| 81 |
'post_type' => 'give_forms', |
| 82 |
'page' => 'givewp-form-builder', |
| 83 |
'donationFormID' => $payload->formV3->id, |
| 84 |
], $payload->formV2->id, $payload->formV3->id); |
| 85 |
|
| 86 |
return new WP_REST_Response([ |
| 87 |
'v2FormId' => $payload->formV2->id, |
| 88 |
'v3FormId' => $payload->formV3->id, |
| 89 |
'redirect' => add_query_arg($redirectArgs, admin_url('edit.php')), |
| 90 |
]); |
| 91 |
} |
| 92 |
} |
| 93 |
|