From 22fda3e480f6b783e53ee969c3fc75db2666615f Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Tue, 5 Nov 2024 17:24:33 -0300 Subject: [PATCH 1/8] feature: add default form action to list view --- .../UpdateDefaultCampaignFormListTable.php | 60 +++++++++++++++++++ src/Campaigns/ServiceProvider.php | 3 +- .../V2/Endpoints/ListDonationForms.php | 2 + .../components/DonationFormsRowActions.tsx | 52 ++++++++++++---- 4 files changed, 106 insertions(+), 11 deletions(-) create mode 100644 src/Campaigns/Routes/UpdateDefaultCampaignFormListTable.php diff --git a/src/Campaigns/Routes/UpdateDefaultCampaignFormListTable.php b/src/Campaigns/Routes/UpdateDefaultCampaignFormListTable.php new file mode 100644 index 0000000000..695c1092a1 --- /dev/null +++ b/src/Campaigns/Routes/UpdateDefaultCampaignFormListTable.php @@ -0,0 +1,60 @@ +\d+)'; + + /** + * @unreleased + */ + public function registerRoute() + { + register_rest_route( + 'give-api/v2', + 'admin/forms/updateDefaultCampaignForm/(?P\d+)', + [ + 'methods' => WP_REST_Server::EDITABLE, + 'callback' => [$this, 'handleRequest'], + 'permission_callback' => function () { + return current_user_can('manage_options'); + }, + 'args' => [ + 'id' => [ + 'type' => 'integer', + 'description' => __('The ID of the new default form for the campaign.', 'give'), + ], + ], + ] + ); + } + + /** + * @unreleased + * + * @throws Exception + */ + public function handleRequest(WP_REST_Request $request): WP_REST_Response + { + $formId = $request->get_param('id'); + $campaign = Campaign::findByFormId($formId); + give(CampaignRepository::class)->updateDefaultCampaignForm($campaign, $formId); + + return new WP_REST_Response($campaign->toArray()); + } +} diff --git a/src/Campaigns/ServiceProvider.php b/src/Campaigns/ServiceProvider.php index e46e6baa92..2efe3755fa 100644 --- a/src/Campaigns/ServiceProvider.php +++ b/src/Campaigns/ServiceProvider.php @@ -55,6 +55,7 @@ private function registerRoutes() Hooks::addAction('rest_api_init', Routes\RegisterCampaignRoutes::class); Hooks::addAction('rest_api_init', Routes\GetCampaignsListTable::class, 'registerRoute'); Hooks::addAction('rest_api_init', Routes\DeleteCampaignListTable::class, 'registerRoute'); + Hooks::addAction('rest_api_init', Routes\UpdateDefaultCampaignFormListTable::class, 'registerRoute', 9); Hooks::addAction('rest_api_init', Routes\GetCampaignStatistics::class, 'registerRoute'); } @@ -138,7 +139,7 @@ private function setupCampaignForms() if ( ! defined('GIVE_IS_ALL_STATS_COLUMNS_ASYNC_ON_ADMIN_FORM_LIST_VIEWS')) { define('GIVE_IS_ALL_STATS_COLUMNS_ASYNC_ON_ADMIN_FORM_LIST_VIEWS', false); } - + Hooks::addAction('save_post_give_forms', AddCampaignFormFromRequest::class, 'optionBasedFormEditor', 10, 3); Hooks::addAction('givewp_donation_form_created', AddCampaignFormFromRequest::class, 'visualFormBuilder'); Hooks::addAction('givewp_campaign_created', CreateDefaultCampaignForm::class); diff --git a/src/DonationForms/V2/Endpoints/ListDonationForms.php b/src/DonationForms/V2/Endpoints/ListDonationForms.php index 44241a0a05..b076e5b4bd 100644 --- a/src/DonationForms/V2/Endpoints/ListDonationForms.php +++ b/src/DonationForms/V2/Endpoints/ListDonationForms.php @@ -2,6 +2,7 @@ namespace Give\DonationForms\V2\Endpoints; +use Give\Campaigns\Models\Campaign; use Give\DonationForms\V2\ListTable\DonationFormsListTable; use Give\Framework\Database\DB; use Give\Framework\QueryBuilder\JoinQueryBuilder; @@ -141,6 +142,7 @@ public function handleRequest(WP_REST_Request $request): WP_REST_Response $item['permalink'] = get_permalink($item['id']); $item['v3form'] = (bool)give_get_meta($item['id'], 'formBuilderSettings'); $item['status_raw'] = $forms[$i]->status->getValue(); + $item['isDefaultCampaignForm'] = ($campaign = Campaign::findByFormId($item['id'])) && $item['id'] === $campaign->defaultForm()->id; } } diff --git a/src/DonationForms/V2/resources/components/DonationFormsRowActions.tsx b/src/DonationForms/V2/resources/components/DonationFormsRowActions.tsx index 6ba994d34b..b71adc5d02 100644 --- a/src/DonationForms/V2/resources/components/DonationFormsRowActions.tsx +++ b/src/DonationForms/V2/resources/components/DonationFormsRowActions.tsx @@ -6,7 +6,7 @@ import {useContext} from 'react'; import {ShowConfirmModalContext} from '@givewp/components/ListTable/ListTablePage'; import {Interweave} from 'interweave'; import {OnboardingContext} from './Onboarding'; -import {UpgradeModalContent} from "./Migration"; +import {UpgradeModalContent} from './Migration'; const donationFormsApi = new ListTableApi(window.GiveDonationForms); @@ -51,11 +51,33 @@ export function DonationFormsRowActions({data, item, removeRow, addRow, setUpdat }; const confirmUpgradeModal = (event) => { + showConfirmModal(__('Upgrade', 'give'), UpgradeModalContent, async (selected) => { + const response = await donationFormsApi.fetchWithArgs('/migrate/' + item.id, {}, 'POST'); + await mutate(parameters); + return response; + }); + }; + + const urlParams = new URLSearchParams(window.location.search); + const isCampaignDetailsPage = + urlParams.get('id') && urlParams.get('page') && 'give-campaigns' === urlParams.get('page'); + + const confirmDefaultCampaignFormModal = (event) => { showConfirmModal( - __('Upgrade', 'give'), - UpgradeModalContent, + __('Make as default', 'give'), + (selected) => ( +

+ {__('Really make the following campaign form default?', 'give')} +
+ +

+ ), async (selected) => { - const response = await donationFormsApi.fetchWithArgs("/migrate/" + item.id, {}, 'POST'); + const response = await donationFormsApi.fetchWithArgs( + '/updateDefaultCampaignForm/' + item.id, + {}, + 'POST' + ); await mutate(parameters); return response; } @@ -99,12 +121,22 @@ export function DonationFormsRowActions({data, item, removeRow, addRow, setUpdat displayText={__('Duplicate', 'give')} hiddenText={item?.name} /> - {!item.v3form && ()} + {!item.v3form && ( + + )} + {isCampaignDetailsPage && !item.isDefaultCampaignForm && ( + + )} )} From c9c84ea5b40793aa06fde23ccf3ff7f78f09ead3 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Tue, 5 Nov 2024 17:56:37 -0300 Subject: [PATCH 2/8] refactor: add support to isDefaultCampaignForm key --- tests/Unit/DonationForms/Endpoints/TestListDonationForms.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Unit/DonationForms/Endpoints/TestListDonationForms.php b/tests/Unit/DonationForms/Endpoints/TestListDonationForms.php index e1c3e509b1..dfb77647e4 100644 --- a/tests/Unit/DonationForms/Endpoints/TestListDonationForms.php +++ b/tests/Unit/DonationForms/Endpoints/TestListDonationForms.php @@ -90,6 +90,7 @@ public function getMockRequest(): WP_REST_Request } /** + * @unreleased Add support to isDefaultCampaignForm key * @since 2.25.0 * * @param array $donationForms @@ -115,6 +116,8 @@ public function getMockColumns(array $donationForms, string $sortDirection = 'de $expectedItem['v3form'] = false; $expectedItem['status_raw'] = $donationForm->status->getValue(); + $expectedItem['isDefaultCampaignForm'] = false; + $expectedItems[] = $expectedItem; } From e09ee33de4195af459da4017a110a85142c11f2b Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Tue, 5 Nov 2024 20:22:50 -0300 Subject: [PATCH 3/8] refactor: update route --- .../Routes/UpdateDefaultCampaignForm.php | 59 ++++++++++++++++++ .../UpdateDefaultCampaignFormListTable.php | 60 ------------------- src/Campaigns/ServiceProvider.php | 2 +- .../components/DonationFormsRowActions.tsx | 14 +++-- 4 files changed, 69 insertions(+), 66 deletions(-) create mode 100644 src/Campaigns/Routes/UpdateDefaultCampaignForm.php delete mode 100644 src/Campaigns/Routes/UpdateDefaultCampaignFormListTable.php diff --git a/src/Campaigns/Routes/UpdateDefaultCampaignForm.php b/src/Campaigns/Routes/UpdateDefaultCampaignForm.php new file mode 100644 index 0000000000..ca278ed56d --- /dev/null +++ b/src/Campaigns/Routes/UpdateDefaultCampaignForm.php @@ -0,0 +1,59 @@ + WP_REST_Server::EDITABLE, + 'callback' => [$this, 'handleRequest'], + 'permission_callback' => function () { + return current_user_can('manage_options'); + }, + ], + 'args' => [ + 'id' => [ + 'type' => 'integer', + 'required' => true, + ], + 'formId' => [ + 'type' => 'integer', + 'required' => true, + ], + ], + ] + ); + } + + /** + * @unreleased + * + * @throws Exception + */ + public function handleRequest($request): WP_REST_Response + { + $campaign = Campaign::find($request->get_param('id')); + give(CampaignRepository::class)->updateDefaultCampaignForm($campaign, $request->get_param('formId')); + + return new WP_REST_Response($campaign->toArray()); + } +} diff --git a/src/Campaigns/Routes/UpdateDefaultCampaignFormListTable.php b/src/Campaigns/Routes/UpdateDefaultCampaignFormListTable.php deleted file mode 100644 index 695c1092a1..0000000000 --- a/src/Campaigns/Routes/UpdateDefaultCampaignFormListTable.php +++ /dev/null @@ -1,60 +0,0 @@ -\d+)'; - - /** - * @unreleased - */ - public function registerRoute() - { - register_rest_route( - 'give-api/v2', - 'admin/forms/updateDefaultCampaignForm/(?P\d+)', - [ - 'methods' => WP_REST_Server::EDITABLE, - 'callback' => [$this, 'handleRequest'], - 'permission_callback' => function () { - return current_user_can('manage_options'); - }, - 'args' => [ - 'id' => [ - 'type' => 'integer', - 'description' => __('The ID of the new default form for the campaign.', 'give'), - ], - ], - ] - ); - } - - /** - * @unreleased - * - * @throws Exception - */ - public function handleRequest(WP_REST_Request $request): WP_REST_Response - { - $formId = $request->get_param('id'); - $campaign = Campaign::findByFormId($formId); - give(CampaignRepository::class)->updateDefaultCampaignForm($campaign, $formId); - - return new WP_REST_Response($campaign->toArray()); - } -} diff --git a/src/Campaigns/ServiceProvider.php b/src/Campaigns/ServiceProvider.php index 2efe3755fa..c3c35bbba7 100644 --- a/src/Campaigns/ServiceProvider.php +++ b/src/Campaigns/ServiceProvider.php @@ -55,7 +55,7 @@ private function registerRoutes() Hooks::addAction('rest_api_init', Routes\RegisterCampaignRoutes::class); Hooks::addAction('rest_api_init', Routes\GetCampaignsListTable::class, 'registerRoute'); Hooks::addAction('rest_api_init', Routes\DeleteCampaignListTable::class, 'registerRoute'); - Hooks::addAction('rest_api_init', Routes\UpdateDefaultCampaignFormListTable::class, 'registerRoute', 9); + Hooks::addAction('rest_api_init', Routes\UpdateDefaultCampaignForm::class, 'registerRoute', 9); Hooks::addAction('rest_api_init', Routes\GetCampaignStatistics::class, 'registerRoute'); } diff --git a/src/DonationForms/V2/resources/components/DonationFormsRowActions.tsx b/src/DonationForms/V2/resources/components/DonationFormsRowActions.tsx index b71adc5d02..8c4cc7fc8d 100644 --- a/src/DonationForms/V2/resources/components/DonationFormsRowActions.tsx +++ b/src/DonationForms/V2/resources/components/DonationFormsRowActions.tsx @@ -7,6 +7,8 @@ import {ShowConfirmModalContext} from '@givewp/components/ListTable/ListTablePag import {Interweave} from 'interweave'; import {OnboardingContext} from './Onboarding'; import {UpgradeModalContent} from './Migration'; +import apiFetch from '@wordpress/api-fetch'; +import {addQueryArgs} from '@wordpress/url'; const donationFormsApi = new ListTableApi(window.GiveDonationForms); @@ -61,6 +63,7 @@ export function DonationFormsRowActions({data, item, removeRow, addRow, setUpdat const urlParams = new URLSearchParams(window.location.search); const isCampaignDetailsPage = urlParams.get('id') && urlParams.get('page') && 'give-campaigns' === urlParams.get('page'); + const campaignId = urlParams.get('id'); const confirmDefaultCampaignFormModal = (event) => { showConfirmModal( @@ -73,11 +76,12 @@ export function DonationFormsRowActions({data, item, removeRow, addRow, setUpdat

), async (selected) => { - const response = await donationFormsApi.fetchWithArgs( - '/updateDefaultCampaignForm/' + item.id, - {}, - 'POST' - ); + const response = await apiFetch({ + path: addQueryArgs('/give-api/v2/campaigns/' + campaignId + '/updateDefaultForm', { + formId: item.id, + }), + method: 'PUT', + }); await mutate(parameters); return response; } From 85524863d9e5e1c925a8b4fda4b6a9c03089d036 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Wed, 6 Nov 2024 10:11:08 -0300 Subject: [PATCH 4/8] refactor: remove unnecessary endpoint --- .../Controllers/CampaignRequestController.php | 6 +- .../Routes/RegisterCampaignRoutes.php | 4 ++ .../Routes/UpdateDefaultCampaignForm.php | 59 ------------------- src/Campaigns/ServiceProvider.php | 1 - .../components/DonationFormsRowActions.tsx | 6 +- 5 files changed, 12 insertions(+), 64 deletions(-) delete mode 100644 src/Campaigns/Routes/UpdateDefaultCampaignForm.php diff --git a/src/Campaigns/Controllers/CampaignRequestController.php b/src/Campaigns/Controllers/CampaignRequestController.php index 6820800e53..787a657f3b 100644 --- a/src/Campaigns/Controllers/CampaignRequestController.php +++ b/src/Campaigns/Controllers/CampaignRequestController.php @@ -2,13 +2,13 @@ namespace Give\Campaigns\Controllers; +use Exception; use Give\Campaigns\Models\Campaign; use Give\Campaigns\Repositories\CampaignRepository; use Give\Campaigns\ValueObjects\CampaignGoalType; use Give\Campaigns\ValueObjects\CampaignRoute; use Give\Campaigns\ValueObjects\CampaignStatus; use Give\Campaigns\ValueObjects\CampaignType; -use Give\Framework\Exceptions\Primitives\Exception; use WP_Error; use WP_REST_Request; use WP_REST_Response; @@ -124,6 +124,10 @@ public function updateCampaign(WP_REST_Request $request) case 'goalType': $campaign->goalType = new CampaignGoalType($value); break; + case 'defaultFormId': + give(CampaignRepository::class)->updateDefaultCampaignForm($campaign, + $request->get_param('defaultFormId')); + break; default: if ($campaign->hasProperty($key)) { $campaign->$key = $value; diff --git a/src/Campaigns/Routes/RegisterCampaignRoutes.php b/src/Campaigns/Routes/RegisterCampaignRoutes.php index 8f4739fc08..7c56edeaf9 100644 --- a/src/Campaigns/Routes/RegisterCampaignRoutes.php +++ b/src/Campaigns/Routes/RegisterCampaignRoutes.php @@ -230,6 +230,10 @@ public function getSchema(): array ], 'description' => esc_html__('Campaign goal type', 'give'), ], + 'defaultFormId' => [ + 'type' => 'integer', + 'description' => esc_html__('Default campaign form ID', 'give'), + ], ], 'required' => ['id', 'title', 'goal', 'goalType'], 'allOf' => [ diff --git a/src/Campaigns/Routes/UpdateDefaultCampaignForm.php b/src/Campaigns/Routes/UpdateDefaultCampaignForm.php deleted file mode 100644 index ca278ed56d..0000000000 --- a/src/Campaigns/Routes/UpdateDefaultCampaignForm.php +++ /dev/null @@ -1,59 +0,0 @@ - WP_REST_Server::EDITABLE, - 'callback' => [$this, 'handleRequest'], - 'permission_callback' => function () { - return current_user_can('manage_options'); - }, - ], - 'args' => [ - 'id' => [ - 'type' => 'integer', - 'required' => true, - ], - 'formId' => [ - 'type' => 'integer', - 'required' => true, - ], - ], - ] - ); - } - - /** - * @unreleased - * - * @throws Exception - */ - public function handleRequest($request): WP_REST_Response - { - $campaign = Campaign::find($request->get_param('id')); - give(CampaignRepository::class)->updateDefaultCampaignForm($campaign, $request->get_param('formId')); - - return new WP_REST_Response($campaign->toArray()); - } -} diff --git a/src/Campaigns/ServiceProvider.php b/src/Campaigns/ServiceProvider.php index c3c35bbba7..5879dde2e5 100644 --- a/src/Campaigns/ServiceProvider.php +++ b/src/Campaigns/ServiceProvider.php @@ -55,7 +55,6 @@ private function registerRoutes() Hooks::addAction('rest_api_init', Routes\RegisterCampaignRoutes::class); Hooks::addAction('rest_api_init', Routes\GetCampaignsListTable::class, 'registerRoute'); Hooks::addAction('rest_api_init', Routes\DeleteCampaignListTable::class, 'registerRoute'); - Hooks::addAction('rest_api_init', Routes\UpdateDefaultCampaignForm::class, 'registerRoute', 9); Hooks::addAction('rest_api_init', Routes\GetCampaignStatistics::class, 'registerRoute'); } diff --git a/src/DonationForms/V2/resources/components/DonationFormsRowActions.tsx b/src/DonationForms/V2/resources/components/DonationFormsRowActions.tsx index 8c4cc7fc8d..b17054b17f 100644 --- a/src/DonationForms/V2/resources/components/DonationFormsRowActions.tsx +++ b/src/DonationForms/V2/resources/components/DonationFormsRowActions.tsx @@ -77,10 +77,10 @@ export function DonationFormsRowActions({data, item, removeRow, addRow, setUpdat ), async (selected) => { const response = await apiFetch({ - path: addQueryArgs('/give-api/v2/campaigns/' + campaignId + '/updateDefaultForm', { - formId: item.id, + path: addQueryArgs('/give-api/v2/campaigns/' + campaignId, { + defaultFormId: item.id, }), - method: 'PUT', + method: 'PATCH', }); await mutate(parameters); return response; From b9492c919ce6e2ac6b335f3fac8119ce38b9358a Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Wed, 6 Nov 2024 17:59:56 -0300 Subject: [PATCH 5/8] fix: default form logic --- .../Actions/LoadCampaignDetailsAssets.php | 4 ++- src/Campaigns/Models/Campaign.php | 27 +++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Campaigns/Actions/LoadCampaignDetailsAssets.php b/src/Campaigns/Actions/LoadCampaignDetailsAssets.php index 79df96bdc5..56339ae55d 100644 --- a/src/Campaigns/Actions/LoadCampaignDetailsAssets.php +++ b/src/Campaigns/Actions/LoadCampaignDetailsAssets.php @@ -4,6 +4,7 @@ use Give\Campaigns\Models\Campaign; use Give\Framework\Support\Facades\Scripts\ScriptAsset; +use Give\Helpers\Form\Utils; /** * @unreleased @@ -33,12 +34,13 @@ public function __invoke() ); $defaultForm = $campaign->defaultForm(); + $defaultFormTitle = Utils::isV3Form($defaultForm->id) ? $defaultForm->settings->formTitle : $defaultForm->title; wp_localize_script($handleName, 'GiveCampaignDetails', [ 'adminUrl' => admin_url(), 'currency' => give_get_currency(), 'isRecurringEnabled' => defined('GIVE_RECURRING_VERSION') ? GIVE_RECURRING_VERSION : null, - 'defaultForm' => $defaultForm ? $defaultForm->settings->formTitle : null, + 'defaultForm' => $defaultFormTitle, ] ); diff --git a/src/Campaigns/Models/Campaign.php b/src/Campaigns/Models/Campaign.php index bb56e5477b..abebaad052 100644 --- a/src/Campaigns/Models/Campaign.php +++ b/src/Campaigns/Models/Campaign.php @@ -12,6 +12,7 @@ use Give\Campaigns\ValueObjects\CampaignStatus; use Give\Campaigns\ValueObjects\CampaignType; use Give\DonationForms\Models\DonationForm; +use Give\DonationForms\V2\Models\DonationForm as LegacyDonationForm; use Give\Framework\Exceptions\Primitives\InvalidArgumentException; use Give\Framework\Models\Contracts\ModelCrud; use Give\Framework\Models\Contracts\ModelHasFactory; @@ -64,12 +65,22 @@ class Campaign extends Model implements ModelCrud, ModelHasFactory /** * @unreleased + * + * @return DonationForm | LegacyDonationForm */ - public function defaultForm(): ?DonationForm + public function defaultForm() { - return $this->forms() + $defaultForm = $this->forms() ->where('campaign_forms.is_default', true) ->get(); + + if (is_null($defaultForm)) { + $defaultForm = $this->legacyForms() + ->where('campaign_forms.is_default', true) + ->get(); + } + + return $defaultForm; } /** @@ -84,6 +95,18 @@ public function forms(): ModelQueryBuilder })->where('campaign_forms.campaign_id', $this->id); } + /** + * @unreleased + */ + public function legacyForms(): ModelQueryBuilder + { + return LegacyDonationForm::query() + ->join(function (JoinQueryBuilder $builder) { + $builder->leftJoin('give_campaign_forms', 'campaign_forms') + ->on('campaign_forms.form_id', 'id'); + })->where('campaign_forms.campaign_id', $this->id); + } + /** * @unreleased */ From 204f5575db7be9b0b9c22ec28c67cb062eb7ecf9 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Fri, 8 Nov 2024 10:35:35 -0300 Subject: [PATCH 6/8] refactor: remove unused parameter --- .../V2/resources/components/DonationFormsRowActions.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DonationForms/V2/resources/components/DonationFormsRowActions.tsx b/src/DonationForms/V2/resources/components/DonationFormsRowActions.tsx index b17054b17f..88ba9af6e0 100644 --- a/src/DonationForms/V2/resources/components/DonationFormsRowActions.tsx +++ b/src/DonationForms/V2/resources/components/DonationFormsRowActions.tsx @@ -75,7 +75,7 @@ export function DonationFormsRowActions({data, item, removeRow, addRow, setUpdat

), - async (selected) => { + async () => { const response = await apiFetch({ path: addQueryArgs('/give-api/v2/campaigns/' + campaignId, { defaultFormId: item.id, From e27a1c4d729ebe225064a9a5c69578743fcf1834 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Fri, 8 Nov 2024 11:35:38 -0300 Subject: [PATCH 7/8] refactor: change how to set the isDefaultCampaignForm value --- src/DonationForms/V2/Endpoints/ListDonationForms.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/DonationForms/V2/Endpoints/ListDonationForms.php b/src/DonationForms/V2/Endpoints/ListDonationForms.php index b076e5b4bd..f90aff393d 100644 --- a/src/DonationForms/V2/Endpoints/ListDonationForms.php +++ b/src/DonationForms/V2/Endpoints/ListDonationForms.php @@ -136,13 +136,15 @@ public function handleRequest(WP_REST_Request $request): WP_REST_Response $this->listTable->items($forms, $this->request->get_param('locale') ?? ''); $items = $this->listTable->getItems(); + $defaultCampaignForm = Campaign::find($this->request->get_param('campaignId'))->defaultForm(); + foreach ($items as $i => &$item) { $item['name'] = get_the_title($item['id']); $item['edit'] = get_edit_post_link($item['id'], 'edit'); $item['permalink'] = get_permalink($item['id']); $item['v3form'] = (bool)give_get_meta($item['id'], 'formBuilderSettings'); $item['status_raw'] = $forms[$i]->status->getValue(); - $item['isDefaultCampaignForm'] = ($campaign = Campaign::findByFormId($item['id'])) && $item['id'] === $campaign->defaultForm()->id; + $item['isDefaultCampaignForm'] = $item['id'] === $defaultCampaignForm->id; } } From 0bdfe43d198c9d29bc5932e0372daa1c0ea8e782 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Fri, 8 Nov 2024 11:52:26 -0300 Subject: [PATCH 8/8] fix: broken tests --- src/DonationForms/V2/Endpoints/ListDonationForms.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DonationForms/V2/Endpoints/ListDonationForms.php b/src/DonationForms/V2/Endpoints/ListDonationForms.php index f90aff393d..f5d1472fa8 100644 --- a/src/DonationForms/V2/Endpoints/ListDonationForms.php +++ b/src/DonationForms/V2/Endpoints/ListDonationForms.php @@ -136,7 +136,7 @@ public function handleRequest(WP_REST_Request $request): WP_REST_Response $this->listTable->items($forms, $this->request->get_param('locale') ?? ''); $items = $this->listTable->getItems(); - $defaultCampaignForm = Campaign::find($this->request->get_param('campaignId'))->defaultForm(); + $defaultCampaignForm = ($campaignId = $this->request->get_param('campaignId')) ? Campaign::find($campaignId)->defaultForm() : false; foreach ($items as $i => &$item) { $item['name'] = get_the_title($item['id']); @@ -144,7 +144,7 @@ public function handleRequest(WP_REST_Request $request): WP_REST_Response $item['permalink'] = get_permalink($item['id']); $item['v3form'] = (bool)give_get_meta($item['id'], 'formBuilderSettings'); $item['status_raw'] = $forms[$i]->status->getValue(); - $item['isDefaultCampaignForm'] = $item['id'] === $defaultCampaignForm->id; + $item['isDefaultCampaignForm'] = $defaultCampaignForm && $item['id'] === $defaultCampaignForm->id; } }