Skip to content

Commit

Permalink
refactor: add isV2MigratedForm method
Browse files Browse the repository at this point in the history
  • Loading branch information
glaubersilva committed Dec 11, 2024
1 parent c9aa3d1 commit b40fa77
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/Campaigns/Migrations/MigrateFormsToCampaignForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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;
}
}

0 comments on commit b40fa77

Please sign in to comment.