From b40fa7704bc85954b6e4905e53b4968ed4f27479 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Wed, 11 Dec 2024 15:17:20 -0300 Subject: [PATCH] refactor: add isV2MigratedForm method --- .../MigrateFormsToCampaignForms.php | 40 ++++++++++++++----- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php index fa45fecd65..ed0761c38f 100644 --- a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php +++ b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php @@ -114,21 +114,18 @@ protected function getUpgradedFormData(): array */ public function createCampaignForForm($formData): void { - $formId = $formData->id; - $formStatus = $formData->status; - $formTitle = $formData->title; - $formCreatedAt = $formData->createdAt; - - $isV3Form = Utils::isV3Form($formId); - /** - * Exclude upgraded V2 forms as their corresponding V3 version will be used to create the campaign - later the V2 form will be added to the proper campaign as a non-default form trough the addUpgradedFormToCampaign() method. + * Skip upgraded V2 forms as their corresponding V3 version will be used to create the campaign - later the V2 form will be added to the proper campaign as a non-default form through the addUpgradedFormToCampaign() method. */ - if ( ! $isV3Form && _give_is_form_migrated($formId)) { + if ($this->isV2MigratedForm($formData->id)) { return; } - $formSettings = $isV3Form ? json_decode($formData->settings) : $this->getV2FormSettings($formId); + $formId = $formData->id; + $formStatus = $formData->status; + $formTitle = $formData->title; + $formCreatedAt = $formData->createdAt; + $formSettings = Utils::isV3Form($formId) ? json_decode($formData->settings) : $this->getV2FormSettings($formId); DB::table('give_campaigns') ->insert([ @@ -307,4 +304,27 @@ protected function getV2FormGoalType(int $formId): string return $onlyRecurringEnabled ? 'amountFromSubscriptions' : 'amount'; } } + + /** + * @unreleased + */ + private function isV2MigratedForm(int $formId): bool + { + global $wpdb; + + $isMigratedForm = (bool)DB::get_var( + DB::prepare( + " + SELECT `form_id` + FROM `{$wpdb->prefix}give_formmeta` + JOIN `{$wpdb->posts}` + ON `{$wpdb->posts}`.`ID` = `{$wpdb->prefix}give_formmeta`.`form_id` + WHERE `meta_key` = 'migratedFormId' + AND `meta_value` = %d", + $formId + ) + ); + + return ! Utils::isV3Form($formId) && $isMigratedForm; + } }