From 639b1aa645ddf20cd06fba5f2f659b2d47367fcb Mon Sep 17 00:00:00 2001 From: alaca Date: Fri, 8 Nov 2024 17:04:11 +0100 Subject: [PATCH 01/20] Initial commit --- .../Actions/ConvertQueryDataToCampaign.php | 1 + .../Actions/LoadCampaignDetailsAssets.php | 2 +- .../MigrateFormsToCampaignForms.php | 10 +++--- .../Tables/CreateCampaignFormsTable.php | 3 +- src/Campaigns/Models/Campaign.php | 15 ++++---- .../Repositories/CampaignRepository.php | 36 ++++++++----------- 6 files changed, 32 insertions(+), 35 deletions(-) diff --git a/src/Campaigns/Actions/ConvertQueryDataToCampaign.php b/src/Campaigns/Actions/ConvertQueryDataToCampaign.php index 5f3e809ce3..209a8677c6 100644 --- a/src/Campaigns/Actions/ConvertQueryDataToCampaign.php +++ b/src/Campaigns/Actions/ConvertQueryDataToCampaign.php @@ -20,6 +20,7 @@ public function __invoke(object $queryObject): Campaign { return new Campaign([ 'id' => (int)$queryObject->id, + 'defaultFormId' => (int)$queryObject->defaultFormId, 'type' => new CampaignType($queryObject->type), 'title' => $queryObject->title, 'shortDescription' => $queryObject->shortDescription, diff --git a/src/Campaigns/Actions/LoadCampaignDetailsAssets.php b/src/Campaigns/Actions/LoadCampaignDetailsAssets.php index 79df96bdc5..50e7c7e044 100644 --- a/src/Campaigns/Actions/LoadCampaignDetailsAssets.php +++ b/src/Campaigns/Actions/LoadCampaignDetailsAssets.php @@ -38,7 +38,7 @@ public function __invoke() 'adminUrl' => admin_url(), 'currency' => give_get_currency(), 'isRecurringEnabled' => defined('GIVE_RECURRING_VERSION') ? GIVE_RECURRING_VERSION : null, - 'defaultForm' => $defaultForm ? $defaultForm->settings->formTitle : null, + 'defaultForm' => $defaultForm ? $defaultForm->title : null, ] ); diff --git a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php index ce4e42f149..07ce2f3b1b 100644 --- a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php +++ b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php @@ -116,6 +116,7 @@ public function createCampaignForForm($formData): void DB::table('give_campaigns') ->insert([ + 'form_id' => $formId, 'campaign_type' => 'core', 'campaign_title' => $formTitle, 'status' => $this->mapFormToCampaignStatus($formStatus), @@ -134,7 +135,7 @@ public function createCampaignForForm($formData): void $campaignId = DB::last_insert_id(); - $this->addCampaignFormRelationship($formId, $campaignId, true); + $this->addCampaignFormRelationship($formId, $campaignId); } /** @@ -142,19 +143,18 @@ public function createCampaignForForm($formData): void */ protected function addUpgradedFormToCampaign($data): void { - $this->addCampaignFormRelationship($data->migratedFormId, $data->campaignId, false); + $this->addCampaignFormRelationship($data->migratedFormId, $data->campaignId); } /** * @unreleased */ - protected function addCampaignFormRelationship($formId, $campaignId, $isDefault) + protected function addCampaignFormRelationship($formId, $campaignId) { DB::table('give_campaign_forms') ->insert([ 'form_id' => $formId, - 'campaign_id' => $campaignId, - 'is_default' => $isDefault, + 'campaign_id' => $campaignId ]); } diff --git a/src/Campaigns/Migrations/Tables/CreateCampaignFormsTable.php b/src/Campaigns/Migrations/Tables/CreateCampaignFormsTable.php index 92e58b6c57..eae5d0b5b8 100644 --- a/src/Campaigns/Migrations/Tables/CreateCampaignFormsTable.php +++ b/src/Campaigns/Migrations/Tables/CreateCampaignFormsTable.php @@ -50,8 +50,7 @@ public function run(): void $sql = "CREATE TABLE $table ( campaign_id INT UNSIGNED NOT NULL, - form_id INT UNSIGNED NOT NULL, - is_default BOOLEAN NOT NULL DEFAULT 0, + form_id INT UNSIGNED NOT NULL KEY form_id (form_id), KEY campaign_id (campaign_id), PRIMARY KEY (campaign_id, form_id) diff --git a/src/Campaigns/Models/Campaign.php b/src/Campaigns/Models/Campaign.php index d504d95930..bf97bed4e5 100644 --- a/src/Campaigns/Models/Campaign.php +++ b/src/Campaigns/Models/Campaign.php @@ -11,7 +11,8 @@ use Give\Campaigns\ValueObjects\CampaignGoalType; use Give\Campaigns\ValueObjects\CampaignStatus; use Give\Campaigns\ValueObjects\CampaignType; -use Give\DonationForms\Models\DonationForm; +use Give\DonationForms\V2\Models\DonationForm; +use Give\DonationForms\V2\Repositories\DonationFormsRepository; use Give\Framework\Exceptions\Primitives\InvalidArgumentException; use Give\Framework\Models\Contracts\ModelCrud; use Give\Framework\Models\Contracts\ModelHasFactory; @@ -23,6 +24,7 @@ * @unreleased * * @property int $id + * @property int $defaultFormId * @property CampaignType $type * @property string $title * @property string $url @@ -46,6 +48,7 @@ class Campaign extends Model implements ModelCrud, ModelHasFactory */ protected $properties = [ 'id' => 'int', + 'defaultFormId' => 'int', 'type' => CampaignType::class, 'title' => 'string', 'shortDescription' => 'string', @@ -67,9 +70,7 @@ class Campaign extends Model implements ModelCrud, ModelHasFactory */ public function defaultForm(): ?DonationForm { - return $this->forms() - ->where('campaign_forms.is_default', true) - ->get(); + return give(DonationFormsRepository::class)->getById($this->defaultFormId); } /** @@ -79,9 +80,11 @@ public function forms(): ModelQueryBuilder { return DonationForm::query() ->join(function (JoinQueryBuilder $builder) { - $builder->leftJoin('give_campaign_forms', 'campaign_forms') + $builder + ->leftJoin('give_campaign_forms', 'campaign_forms') ->on('campaign_forms.form_id', 'forms.id'); - })->where('campaign_forms.campaign_id', $this->id); + }) + ->where('campaign_forms.campaign_id', $this->id); } /** diff --git a/src/Campaigns/Repositories/CampaignRepository.php b/src/Campaigns/Repositories/CampaignRepository.php index 96db261118..355ca4d29d 100644 --- a/src/Campaigns/Repositories/CampaignRepository.php +++ b/src/Campaigns/Repositories/CampaignRepository.php @@ -168,24 +168,19 @@ public function addCampaignForm(Campaign $campaign, int $donationFormId, bool $i DB::query('START TRANSACTION'); try { - // Make sure we'll have only one default form if ($isDefault) { - DB::table('give_campaign_forms') - ->where('campaign_id', $campaign->id) + DB::table('give_campaigns') + ->where('id', $campaign->id) ->update([ - 'is_default' => false, + 'form_id' => $donationFormId, ]); } - $table = DB::prefix('give_campaign_forms'); - DB::query( - DB::prepare("INSERT INTO {$table} (form_id, campaign_id, is_default ) VALUES (%d, %d, %d)", - [ - $donationFormId, - $campaign->id, - $isDefault, - ]) - ); + DB::table('give_campaign_forms') + ->insert([ + 'form_id' => $donationFormId, + 'campaign_id' => $campaign->id, + ]); } catch (Exception $exception) { DB::query('ROLLBACK'); @@ -211,13 +206,11 @@ public function updateDefaultCampaignForm(Campaign $campaign, int $donationFormI DB::query('START TRANSACTION'); try { - DB::query( - DB::prepare('UPDATE ' . DB::prefix('give_campaign_forms') . ' SET is_default = IF(form_id = %d, 1, 0) WHERE campaign_id = %d', - [ - $donationFormId, - $campaign->id, - ]) - ); + DB::table('give_campaign_forms') + ->where('id', $campaign->id) + ->update([ + 'form_id' => $donationFormId + ]); } catch (Exception $exception) { DB::query('ROLLBACK'); @@ -293,7 +286,7 @@ public function mergeCampaigns(Campaign $destinationCampaign, Campaign ...$campa // Migrate forms from campaigns to merge to the destination campaign DB::query( - DB::prepare("UPDATE " . DB::prefix('give_campaign_forms') . " SET is_default = 0, campaign_id = %d WHERE campaign_id IN ($campaignsToMergeIdsString)", + DB::prepare("UPDATE " . DB::prefix('give_campaign_forms') . " campaign_id = %d WHERE campaign_id IN ($campaignsToMergeIdsString)", [ $destinationCampaign->id, ]) @@ -343,6 +336,7 @@ public function prepareQuery(): ModelQueryBuilder return $builder->from('give_campaigns', 'campaigns') ->select( 'id', + ['form_id', 'defaultFormId'], ['campaign_type', 'type'], ['campaign_title', 'title'], ['short_desc', 'shortDescription'], From f292027cc51eba11713b4ca7cb4969c11b595b85 Mon Sep 17 00:00:00 2001 From: alaca Date: Fri, 8 Nov 2024 17:07:07 +0100 Subject: [PATCH 02/20] refactor: form title --- src/Campaigns/Actions/LoadCampaignDetailsAssets.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Campaigns/Actions/LoadCampaignDetailsAssets.php b/src/Campaigns/Actions/LoadCampaignDetailsAssets.php index 50e7c7e044..87349f37eb 100644 --- a/src/Campaigns/Actions/LoadCampaignDetailsAssets.php +++ b/src/Campaigns/Actions/LoadCampaignDetailsAssets.php @@ -38,7 +38,7 @@ public function __invoke() 'adminUrl' => admin_url(), 'currency' => give_get_currency(), 'isRecurringEnabled' => defined('GIVE_RECURRING_VERSION') ? GIVE_RECURRING_VERSION : null, - 'defaultForm' => $defaultForm ? $defaultForm->title : null, + 'defaultForm' => $defaultForm->title, ] ); From bd7263607b120b3f786ffeac03db55ecb2997855 Mon Sep 17 00:00:00 2001 From: alaca Date: Fri, 8 Nov 2024 17:16:02 +0100 Subject: [PATCH 03/20] refactor: move merge campaigns logic to RegisterCampaignRoutes.php --- .../Controllers/CampaignRequestController.php | 15 +++++ src/Campaigns/Routes/MergeCampaigns.php | 65 ------------------- .../Routes/RegisterCampaignRoutes.php | 39 +++++++++++ 3 files changed, 54 insertions(+), 65 deletions(-) delete mode 100644 src/Campaigns/Routes/MergeCampaigns.php diff --git a/src/Campaigns/Controllers/CampaignRequestController.php b/src/Campaigns/Controllers/CampaignRequestController.php index 787a657f3b..6e55e32b4e 100644 --- a/src/Campaigns/Controllers/CampaignRequestController.php +++ b/src/Campaigns/Controllers/CampaignRequestController.php @@ -142,6 +142,21 @@ public function updateCampaign(WP_REST_Request $request) return new WP_REST_Response($campaign->toArray()); } + /** + * @unreleased + * + * @throws Exception + */ + public function mergeCampaigns(WP_REST_Request $request): WP_REST_Response + { + $destinationCampaign = Campaign::find($request->get_param('id')); + $campaignsToMerge = Campaign::query()->whereIn('id', $request->get_param('campaignsToMergeIds'))->getAll(); + + $campaignsMerged = $destinationCampaign->merge(...$campaignsToMerge); + + return new WP_REST_Response($campaignsMerged); + } + /** * @unreleased diff --git a/src/Campaigns/Routes/MergeCampaigns.php b/src/Campaigns/Routes/MergeCampaigns.php deleted file mode 100644 index 1999fa3638..0000000000 --- a/src/Campaigns/Routes/MergeCampaigns.php +++ /dev/null @@ -1,65 +0,0 @@ - WP_REST_Server::EDITABLE, - 'callback' => [$this, 'handleRequest'], - 'permission_callback' => function () { - return current_user_can('manage_options'); - }, - ], - 'args' => [ - 'id' => [ - 'type' => 'integer', - 'required' => true, - ], - 'campaignsToMergeIds' => [ - 'type' => 'array', - 'required' => true, - 'items' => [ - 'type' => 'integer', - ], - ], - ], - ] - ); - } - - /** - * @unreleased - * - * @throws Exception - */ - public function handleRequest(WP_REST_Request $request): WP_REST_Response - { - $destinationCampaign = Campaign::find($request->get_param('id')); - $campaignsToMerge = Campaign::query()->whereIn('id', $request->get_param('campaignsToMergeIds'))->getAll(); - - $campaignsMerged = $destinationCampaign->merge(...$campaignsToMerge); - - return new WP_REST_Response($campaignsMerged); - } -} diff --git a/src/Campaigns/Routes/RegisterCampaignRoutes.php b/src/Campaigns/Routes/RegisterCampaignRoutes.php index 7c56edeaf9..49cc697f9f 100644 --- a/src/Campaigns/Routes/RegisterCampaignRoutes.php +++ b/src/Campaigns/Routes/RegisterCampaignRoutes.php @@ -35,6 +35,7 @@ public function __invoke() $this->registerGetCampaign(); $this->registerUpdateCampaign(); $this->registerGetCampaigns(); + $this->registerMergeCampaigns(); $this->registerCreateCampaign(); } @@ -130,6 +131,44 @@ public function registerUpdateCampaign() } + /** + * Update Campaign route + * + * @unreleased + */ + public function registerMergeCampaigns() + { + register_rest_route( + CampaignRoute::NAMESPACE, + CampaignRoute::CAMPAIGN . '/merge', + [ + [ + 'methods' => WP_REST_Server::EDITABLE, + 'callback' => function (WP_REST_Request $request) { + return $this->campaignRequestController->mergeCampaigns($request); + }, + 'permission_callback' => function () { + return current_user_can('manage_options'); + }, + ], + 'args' => [ + 'id' => [ + 'type' => 'integer', + 'required' => true, + ], + 'campaignsToMergeIds' => [ + 'type' => 'array', + 'required' => true, + 'items' => [ + 'type' => 'integer', + ], + ], + ], + ] + ); + } + + /** * Create Campaign route * From 9a8c9f29ced7b3db56988cb50b09267de90f51ff Mon Sep 17 00:00:00 2001 From: alaca Date: Fri, 8 Nov 2024 17:29:15 +0100 Subject: [PATCH 04/20] refactor: remove unused code --- src/Campaigns/ServiceProvider.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Campaigns/ServiceProvider.php b/src/Campaigns/ServiceProvider.php index bcbc832dd1..5879dde2e5 100644 --- a/src/Campaigns/ServiceProvider.php +++ b/src/Campaigns/ServiceProvider.php @@ -56,7 +56,6 @@ private function registerRoutes() Hooks::addAction('rest_api_init', Routes\GetCampaignsListTable::class, 'registerRoute'); Hooks::addAction('rest_api_init', Routes\DeleteCampaignListTable::class, 'registerRoute'); Hooks::addAction('rest_api_init', Routes\GetCampaignStatistics::class, 'registerRoute'); - Hooks::addAction('rest_api_init', Routes\MergeCampaigns::class, 'registerRoute'); } /** From 27d83386626d218f457e906bacca24338e623268 Mon Sep 17 00:00:00 2001 From: alaca Date: Fri, 8 Nov 2024 18:27:19 +0100 Subject: [PATCH 05/20] fix: table name --- src/Campaigns/Controllers/CampaignRequestController.php | 3 +-- src/Campaigns/Repositories/CampaignRepository.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Campaigns/Controllers/CampaignRequestController.php b/src/Campaigns/Controllers/CampaignRequestController.php index 6e55e32b4e..cb11442eba 100644 --- a/src/Campaigns/Controllers/CampaignRequestController.php +++ b/src/Campaigns/Controllers/CampaignRequestController.php @@ -125,8 +125,7 @@ public function updateCampaign(WP_REST_Request $request) $campaign->goalType = new CampaignGoalType($value); break; case 'defaultFormId': - give(CampaignRepository::class)->updateDefaultCampaignForm($campaign, - $request->get_param('defaultFormId')); + give(CampaignRepository::class)->updateDefaultCampaignForm($campaign, $request->get_param('defaultFormId')); break; default: if ($campaign->hasProperty($key)) { diff --git a/src/Campaigns/Repositories/CampaignRepository.php b/src/Campaigns/Repositories/CampaignRepository.php index 355ca4d29d..a50656c456 100644 --- a/src/Campaigns/Repositories/CampaignRepository.php +++ b/src/Campaigns/Repositories/CampaignRepository.php @@ -206,7 +206,7 @@ public function updateDefaultCampaignForm(Campaign $campaign, int $donationFormI DB::query('START TRANSACTION'); try { - DB::table('give_campaign_forms') + DB::table('give_campaigns') ->where('id', $campaign->id) ->update([ 'form_id' => $donationFormId From 505302a2b4e4ef48585a471fdd1151420729e779 Mon Sep 17 00:00:00 2001 From: "Kyle B. Johnson" Date: Mon, 11 Nov 2024 15:07:08 -0500 Subject: [PATCH 06/20] Update src/Campaigns/Migrations/Tables/CreateCampaignFormsTable.php --- src/Campaigns/Migrations/Tables/CreateCampaignFormsTable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Campaigns/Migrations/Tables/CreateCampaignFormsTable.php b/src/Campaigns/Migrations/Tables/CreateCampaignFormsTable.php index eae5d0b5b8..49c32034c2 100644 --- a/src/Campaigns/Migrations/Tables/CreateCampaignFormsTable.php +++ b/src/Campaigns/Migrations/Tables/CreateCampaignFormsTable.php @@ -50,7 +50,7 @@ public function run(): void $sql = "CREATE TABLE $table ( campaign_id INT UNSIGNED NOT NULL, - form_id INT UNSIGNED NOT NULL + form_id INT UNSIGNED NOT NULL, KEY form_id (form_id), KEY campaign_id (campaign_id), PRIMARY KEY (campaign_id, form_id) From 6cca08ffc417b7a58860109d1c312c1cae387cc3 Mon Sep 17 00:00:00 2001 From: alaca Date: Tue, 19 Nov 2024 18:38:05 +0100 Subject: [PATCH 07/20] refactor: update migration test --- .../Migrations/MigrateFormsToCampaignFormsTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php b/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php index b763b7653b..47471297a4 100644 --- a/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php +++ b/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php @@ -80,9 +80,9 @@ public function testMigratedFormsAreDefault() $migration = new MigrateFormsToCampaignForms(); $migration->run(); - $relationship = DB::table('give_campaign_forms')->where('form_id', $form->id)->get(); + $campaign = Campaign::findByFormId($form->id); - $this->assertEquals(1, $relationship->is_default); + $this->assertEquals($form->id, $campaign->defaultFormId); } /** @@ -98,9 +98,9 @@ public function testUpgradedFormsAreNotDefault() $migration = new MigrateFormsToCampaignForms(); $migration->run(); + + $campaign = Campaign::findByFormId($form2->id); - $relationship = DB::table('give_campaign_forms')->where('form_id', $form1->id)->get(); - - $this->assertEquals(0, $relationship->is_default); + $this->assertNotEquals($form2->id, $campaign->defaultFormId); } } From f86b591f5f41387900ae8f89d9e61ddf572d1cee Mon Sep 17 00:00:00 2001 From: alaca Date: Wed, 20 Nov 2024 16:17:34 +0100 Subject: [PATCH 08/20] refactor: add form_id --- src/Campaigns/Factories/CampaignFactory.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Campaigns/Factories/CampaignFactory.php b/src/Campaigns/Factories/CampaignFactory.php index 5f467c0485..8b71596268 100644 --- a/src/Campaigns/Factories/CampaignFactory.php +++ b/src/Campaigns/Factories/CampaignFactory.php @@ -23,6 +23,7 @@ public function definition(): array return [ 'type' => CampaignType::CORE(), 'enableCampaignPage' => true, + 'form_id' => 1, 'title' => __('GiveWP Campaign', 'give'), 'shortDescription' => __('Campaign short description', 'give'), 'longDescription' => __('Campaign long description', 'give'), From 3af63d54e12f07a18355d211fd428ab7aa7375db Mon Sep 17 00:00:00 2001 From: alaca Date: Wed, 20 Nov 2024 16:19:58 +0100 Subject: [PATCH 09/20] refactor: prop name --- src/Campaigns/Factories/CampaignFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Campaigns/Factories/CampaignFactory.php b/src/Campaigns/Factories/CampaignFactory.php index 8b71596268..1cc64f9510 100644 --- a/src/Campaigns/Factories/CampaignFactory.php +++ b/src/Campaigns/Factories/CampaignFactory.php @@ -23,7 +23,7 @@ public function definition(): array return [ 'type' => CampaignType::CORE(), 'enableCampaignPage' => true, - 'form_id' => 1, + 'defaultFormId' => 1, 'title' => __('GiveWP Campaign', 'give'), 'shortDescription' => __('Campaign short description', 'give'), 'longDescription' => __('Campaign long description', 'give'), From 86fba72ae94c53d9333479b31153ee1fc6dfdbb3 Mon Sep 17 00:00:00 2001 From: alaca Date: Wed, 20 Nov 2024 16:28:20 +0100 Subject: [PATCH 10/20] refactor: table column --- src/Campaigns/Repositories/CampaignRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Campaigns/Repositories/CampaignRepository.php b/src/Campaigns/Repositories/CampaignRepository.php index a4f455063e..7962dd6777 100644 --- a/src/Campaigns/Repositories/CampaignRepository.php +++ b/src/Campaigns/Repositories/CampaignRepository.php @@ -50,7 +50,7 @@ public function getByFormId(int $formId) { return $this->prepareQuery() ->leftJoin('give_campaign_forms', 'campaigns.id', 'forms.campaign_id', 'forms') - ->where('forms.form_id', $formId) + ->where('campaigns.form_id', $formId) ->get(); } From 4541dc06626755258ed9bcf4b2616b3719421803 Mon Sep 17 00:00:00 2001 From: "Kyle B. Johnson" Date: Mon, 25 Nov 2024 12:44:36 -0500 Subject: [PATCH 11/20] chore: Manually add missing MergeCampaigns route --- src/Campaigns/Routes/MergeCampaigns.php | 65 +++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/Campaigns/Routes/MergeCampaigns.php diff --git a/src/Campaigns/Routes/MergeCampaigns.php b/src/Campaigns/Routes/MergeCampaigns.php new file mode 100644 index 0000000000..1999fa3638 --- /dev/null +++ b/src/Campaigns/Routes/MergeCampaigns.php @@ -0,0 +1,65 @@ + WP_REST_Server::EDITABLE, + 'callback' => [$this, 'handleRequest'], + 'permission_callback' => function () { + return current_user_can('manage_options'); + }, + ], + 'args' => [ + 'id' => [ + 'type' => 'integer', + 'required' => true, + ], + 'campaignsToMergeIds' => [ + 'type' => 'array', + 'required' => true, + 'items' => [ + 'type' => 'integer', + ], + ], + ], + ] + ); + } + + /** + * @unreleased + * + * @throws Exception + */ + public function handleRequest(WP_REST_Request $request): WP_REST_Response + { + $destinationCampaign = Campaign::find($request->get_param('id')); + $campaignsToMerge = Campaign::query()->whereIn('id', $request->get_param('campaignsToMergeIds'))->getAll(); + + $campaignsMerged = $destinationCampaign->merge(...$campaignsToMerge); + + return new WP_REST_Response($campaignsMerged); + } +} From cc759b08d7021df467099faaa9b551d6a5e2e668 Mon Sep 17 00:00:00 2001 From: "Kyle B. Johnson" Date: Mon, 25 Nov 2024 13:25:56 -0500 Subject: [PATCH 12/20] fix: Update column name ambiguity --- src/Campaigns/Repositories/CampaignRepository.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Campaigns/Repositories/CampaignRepository.php b/src/Campaigns/Repositories/CampaignRepository.php index 7962dd6777..882e8306a6 100644 --- a/src/Campaigns/Repositories/CampaignRepository.php +++ b/src/Campaigns/Repositories/CampaignRepository.php @@ -44,13 +44,13 @@ public function getById(int $id) /** * @unreleased * - * Get Campaign by Form ID + * Get Campaign by Form ID using a lookup table */ public function getByFormId(int $formId) { return $this->prepareQuery() - ->leftJoin('give_campaign_forms', 'campaigns.id', 'forms.campaign_id', 'forms') - ->where('campaigns.form_id', $formId) + ->leftJoin('give_campaign_forms', 'campaigns.id', 'campaignForms.campaign_id', 'campaignForms') + ->where('campaignForms.form_id', $formId) ->get(); } @@ -338,7 +338,7 @@ public function prepareQuery(): ModelQueryBuilder return $builder->from('give_campaigns', 'campaigns') ->select( 'id', - ['form_id', 'defaultFormId'], + ['campaigns.form_id', 'defaultFormId'], // Prefix the `form_id` column to avoid conflicts with the `give_campaign_forms` table. ['campaign_type', 'type'], ['enable_campaign_page', 'enableCampaignPage'], ['campaign_title', 'title'], From 1f3ce745d0b14c38a9c772f5b23d5c9ff99905ef Mon Sep 17 00:00:00 2001 From: "Kyle B. Johnson" Date: Mon, 25 Nov 2024 14:19:08 -0500 Subject: [PATCH 13/20] fix: Remove alias from campaigns forms join --- src/Campaigns/Models/Campaign.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Campaigns/Models/Campaign.php b/src/Campaigns/Models/Campaign.php index ed17c12787..23d086d901 100644 --- a/src/Campaigns/Models/Campaign.php +++ b/src/Campaigns/Models/Campaign.php @@ -85,7 +85,7 @@ public function forms(): ModelQueryBuilder ->join(function (JoinQueryBuilder $builder) { $builder ->leftJoin('give_campaign_forms', 'campaign_forms') - ->on('campaign_forms.form_id', 'forms.id'); + ->on('campaign_forms.form_id', 'id'); }) ->where('campaign_forms.campaign_id', $this->id); } From 3047919bad4d8ee0bbb16101f401f711d5804243 Mon Sep 17 00:00:00 2001 From: alaca Date: Wed, 27 Nov 2024 12:30:01 +0100 Subject: [PATCH 14/20] refactor: use maybe_add_column function --- .../Migrations/RevenueTable/AddCampaignID.php | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/Campaigns/Migrations/RevenueTable/AddCampaignID.php b/src/Campaigns/Migrations/RevenueTable/AddCampaignID.php index 92101e7c42..a53d7c5586 100644 --- a/src/Campaigns/Migrations/RevenueTable/AddCampaignID.php +++ b/src/Campaigns/Migrations/RevenueTable/AddCampaignID.php @@ -2,7 +2,7 @@ namespace Give\Campaigns\Migrations\RevenueTable; -use Give\Framework\Database\Exceptions\DatabaseQueryException; +use Give\Framework\Database\DB; use Give\Framework\Migrations\Contracts\Migration; use Give\Framework\Migrations\Exceptions\DatabaseMigrationException; @@ -41,19 +41,11 @@ public static function timestamp(): string */ public function run() { - global $wpdb; + $table = DB::prefix('give_revenue'); + $columnAdded = maybe_add_column($table, 'campaign_id', "ALTER TABLE $table ADD COLUMN campaign_id INT UNSIGNED NULL"); - $table = $wpdb->give_revenue; - - $sql = " - ALTER TABLE $table - ADD COLUMN campaign_id INT UNSIGNED NOT NULL DEFAULT '0' - "; - - try { - $wpdb->query($sql); - } catch (DatabaseQueryException $exception) { - throw new DatabaseMigrationException("An error occurred while updating the $table table", 0, $exception); + if ( ! $columnAdded) { + throw new DatabaseMigrationException("An error occurred while updating the $table table"); } } } From 688a69de3d01b3611253b3a997aa573195647ce5 Mon Sep 17 00:00:00 2001 From: alaca Date: Wed, 27 Nov 2024 12:41:19 +0100 Subject: [PATCH 15/20] fix: query --- src/Campaigns/Repositories/CampaignRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Campaigns/Repositories/CampaignRepository.php b/src/Campaigns/Repositories/CampaignRepository.php index 882e8306a6..b5c56c0187 100644 --- a/src/Campaigns/Repositories/CampaignRepository.php +++ b/src/Campaigns/Repositories/CampaignRepository.php @@ -288,7 +288,7 @@ public function mergeCampaigns(Campaign $destinationCampaign, Campaign ...$campa // Migrate forms from campaigns to merge to the destination campaign DB::query( - DB::prepare("UPDATE " . DB::prefix('give_campaign_forms') . " campaign_id = %d WHERE campaign_id IN ($campaignsToMergeIdsString)", + DB::prepare("UPDATE " . DB::prefix('give_campaign_forms') . " SET campaign_id = %d WHERE campaign_id IN ($campaignsToMergeIdsString)", [ $destinationCampaign->id, ]) From 4909b2bec57e2ee01cf374a42515b1bdc953406a Mon Sep 17 00:00:00 2001 From: alaca Date: Wed, 27 Nov 2024 12:48:52 +0100 Subject: [PATCH 16/20] refactor: remove count test --- tests/Unit/Campaigns/Repositories/CampaignRepositoryTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/Unit/Campaigns/Repositories/CampaignRepositoryTest.php b/tests/Unit/Campaigns/Repositories/CampaignRepositoryTest.php index ea8c39207a..19f9bcb649 100644 --- a/tests/Unit/Campaigns/Repositories/CampaignRepositoryTest.php +++ b/tests/Unit/Campaigns/Repositories/CampaignRepositoryTest.php @@ -428,10 +428,6 @@ public function testMergeCampaignsShouldKeepDefaultFormFromDestinationCampaign() //Re-fetch $destinationCampaign = Campaign::find($destinationCampaign->id); - $countDefaultForm = $destinationCampaign->forms() - ->where('campaign_forms.is_default', true)->count(); - - $this->assertEquals(1, $countDefaultForm); $this->assertEquals($defaultFormBeforeMerge->id, $destinationCampaign->defaultForm()->id); } } From 2a94de7b15c356893ed12098a636b63bd4a43e78 Mon Sep 17 00:00:00 2001 From: alaca Date: Wed, 27 Nov 2024 12:59:07 +0100 Subject: [PATCH 17/20] refactor: form id --- .../Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php b/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php index 47471297a4..c74e5e9857 100644 --- a/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php +++ b/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php @@ -98,9 +98,9 @@ public function testUpgradedFormsAreNotDefault() $migration = new MigrateFormsToCampaignForms(); $migration->run(); - + $campaign = Campaign::findByFormId($form2->id); - $this->assertNotEquals($form2->id, $campaign->defaultFormId); + $this->assertNotEquals($form1->id, $campaign->defaultFormId); } } From 37c12fe9da16586b0ff7d6865ef50955d2b009c0 Mon Sep 17 00:00:00 2001 From: alaca Date: Wed, 27 Nov 2024 13:07:24 +0100 Subject: [PATCH 18/20] fix: test --- src/Campaigns/Repositories/CampaignRepository.php | 2 ++ tests/Unit/Campaigns/Models/CampaignModelTest.php | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Campaigns/Repositories/CampaignRepository.php b/src/Campaigns/Repositories/CampaignRepository.php index b5c56c0187..6f2c0213ed 100644 --- a/src/Campaigns/Repositories/CampaignRepository.php +++ b/src/Campaigns/Repositories/CampaignRepository.php @@ -176,6 +176,8 @@ public function addCampaignForm(Campaign $campaign, int $donationFormId, bool $i ->update([ 'form_id' => $donationFormId, ]); + + $campaign->defaultFormId = $donationFormId; } DB::table('give_campaign_forms') diff --git a/tests/Unit/Campaigns/Models/CampaignModelTest.php b/tests/Unit/Campaigns/Models/CampaignModelTest.php index afd868895f..239612b666 100644 --- a/tests/Unit/Campaigns/Models/CampaignModelTest.php +++ b/tests/Unit/Campaigns/Models/CampaignModelTest.php @@ -56,6 +56,6 @@ public function testCampaignHasDefaultForm() $newDefaultForm = DonationForm::factory()->create(); give(CampaignRepository::class)->addCampaignForm($campaign, $newDefaultForm->id, true); - $this->assertEquals($newDefaultForm->id, $campaign->defaultForm()->id); + $this->assertEquals($newDefaultForm->id, $campaign->defaultFormId); } } From 72dc0f9477d40aca39f5e62d611398055bc6920d Mon Sep 17 00:00:00 2001 From: alaca Date: Wed, 27 Nov 2024 13:08:48 +0100 Subject: [PATCH 19/20] refactor: update defaultFormId prop --- src/Campaigns/Repositories/CampaignRepository.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Campaigns/Repositories/CampaignRepository.php b/src/Campaigns/Repositories/CampaignRepository.php index 6f2c0213ed..7b387e5089 100644 --- a/src/Campaigns/Repositories/CampaignRepository.php +++ b/src/Campaigns/Repositories/CampaignRepository.php @@ -215,6 +215,8 @@ public function updateDefaultCampaignForm(Campaign $campaign, int $donationFormI ->update([ 'form_id' => $donationFormId ]); + + $campaign->defaultFormId = $donationFormId; } catch (Exception $exception) { DB::query('ROLLBACK'); From 33cbcb10389ce33dcfac261b12269808934a955c Mon Sep 17 00:00:00 2001 From: alaca Date: Wed, 4 Dec 2024 09:22:17 +0100 Subject: [PATCH 20/20] refactor: remove unused code --- src/Campaigns/Routes/MergeCampaigns.php | 65 ------------------------- src/Campaigns/ServiceProvider.php | 1 - 2 files changed, 66 deletions(-) delete mode 100644 src/Campaigns/Routes/MergeCampaigns.php diff --git a/src/Campaigns/Routes/MergeCampaigns.php b/src/Campaigns/Routes/MergeCampaigns.php deleted file mode 100644 index 1999fa3638..0000000000 --- a/src/Campaigns/Routes/MergeCampaigns.php +++ /dev/null @@ -1,65 +0,0 @@ - WP_REST_Server::EDITABLE, - 'callback' => [$this, 'handleRequest'], - 'permission_callback' => function () { - return current_user_can('manage_options'); - }, - ], - 'args' => [ - 'id' => [ - 'type' => 'integer', - 'required' => true, - ], - 'campaignsToMergeIds' => [ - 'type' => 'array', - 'required' => true, - 'items' => [ - 'type' => 'integer', - ], - ], - ], - ] - ); - } - - /** - * @unreleased - * - * @throws Exception - */ - public function handleRequest(WP_REST_Request $request): WP_REST_Response - { - $destinationCampaign = Campaign::find($request->get_param('id')); - $campaignsToMerge = Campaign::query()->whereIn('id', $request->get_param('campaignsToMergeIds'))->getAll(); - - $campaignsMerged = $destinationCampaign->merge(...$campaignsToMerge); - - return new WP_REST_Response($campaignsMerged); - } -} diff --git a/src/Campaigns/ServiceProvider.php b/src/Campaigns/ServiceProvider.php index e8ec820d2c..012be96503 100644 --- a/src/Campaigns/ServiceProvider.php +++ b/src/Campaigns/ServiceProvider.php @@ -57,7 +57,6 @@ private function registerRoutes() Hooks::addAction('rest_api_init', Routes\DeleteCampaignListTable::class, 'registerRoute'); Hooks::addAction('rest_api_init', Routes\GetCampaignStatistics::class, 'registerRoute'); Hooks::addAction('rest_api_init', Routes\GetCampaignRevenue::class, 'registerRoute'); - Hooks::addAction('rest_api_init', Routes\MergeCampaigns::class, 'registerRoute'); } /**