Skip to content

Commit

Permalink
Merge branch 'epic/campaigns' into feature/campaign-default-form-sett…
Browse files Browse the repository at this point in the history
…ing-GIVE-1493
  • Loading branch information
kjohnson authored Nov 8, 2024
2 parents bb51c53 + 1bbd809 commit 592e74f
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 18 deletions.
4 changes: 3 additions & 1 deletion src/Campaigns/Actions/LoadCampaignDetailsAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Give\Campaigns\Models\Campaign;
use Give\DonationForms\Models\DonationForm;
use Give\Framework\Support\Facades\Scripts\ScriptAsset;
use Give\Helpers\Form\Utils;

/**
* @unreleased
Expand Down Expand Up @@ -34,12 +35,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,
'donationForms' => array_map(function(DonationForm $form) {
return [
'id' => $form->id,
Expand Down
6 changes: 5 additions & 1 deletion src/Campaigns/Controllers/CampaignRequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
27 changes: 25 additions & 2 deletions src/Campaigns/Models/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Campaigns/Routes/RegisterCampaignRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,17 @@
}
}

.button:is(:global(.button)) {
border-radius: var(--givewp-rounded-8)
}

.previousButton:is(:global(.button)) {
background-color: transparent;
border: solid 1px #9ca0af;
color: #060c1a;

&:hover {
border: solid 1px #9ca0af;
color: #060c1a;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -423,14 +423,23 @@ export default function CampaignFormModal({isOpen, handleClose, apiSettings, tit
<input type="datetime-local" {...register('endDateTime')} />
</div>
</div>*/}
<div className="givewp-campaigns__form-row givewp-campaigns__form-row--half">
<button type="submit" onClick={() => setStep(1)} className={`button button-secondary`}>
<div
className="givewp-campaigns__form-row givewp-campaigns__form-row--half"
style={{marginBottom: 0}}
>
<button
type="submit"
onClick={() => setStep(1)}
className={`button button-secondary ${styles.button} ${styles.previousButton}`}
>
{__('Previous', 'give')}
</button>

<button
type="submit"
className={`button button-primary ${!goal || isSubmitting ? 'disabled' : ''}`}
className={`button button-primary ${styles.button} ${
!goal || isSubmitting ? 'disabled' : ''
}`}
aria-disabled={!goal || isSubmitting}
disabled={!goal || isSubmitting}
>
Expand Down
4 changes: 4 additions & 0 deletions src/DonationForms/V2/Endpoints/ListDonationForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -135,12 +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 = ($campaignId = $this->request->get_param('campaignId')) ? Campaign::find($campaignId)->defaultForm() : false;

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'] = $defaultCampaignForm && $item['id'] === $defaultCampaignForm->id;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ 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';
import apiFetch from '@wordpress/api-fetch';
import {addQueryArgs} from '@wordpress/url';

const donationFormsApi = new ListTableApi(window.GiveDonationForms);

Expand Down Expand Up @@ -51,11 +53,35 @@ 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 campaignId = urlParams.get('id');

const confirmDefaultCampaignFormModal = (event) => {
showConfirmModal(
__('Upgrade', 'give'),
UpgradeModalContent,
async (selected) => {
const response = await donationFormsApi.fetchWithArgs("/migrate/" + item.id, {}, 'POST');
__('Make as default', 'give'),
(selected) => (
<p>
{__('Really make the following campaign form default?', 'give')}
<br />
<Interweave content={item?.title} />
</p>
),
async () => {
const response = await apiFetch({
path: addQueryArgs('/give-api/v2/campaigns/' + campaignId, {
defaultFormId: item.id,
}),
method: 'PATCH',
});
await mutate(parameters);
return response;
}
Expand Down Expand Up @@ -99,12 +125,22 @@ export function DonationFormsRowActions({data, item, removeRow, addRow, setUpdat
displayText={__('Duplicate', 'give')}
hiddenText={item?.name}
/>
{!item.v3form && (<RowAction
onClick={confirmUpgradeModal}
actionId={item.id}
displayText={__('Upgrade', 'give')}
hiddenText={item?.name}
/>)}
{!item.v3form && (
<RowAction
onClick={confirmUpgradeModal}
actionId={item.id}
displayText={__('Upgrade', 'give')}
hiddenText={item?.name}
/>
)}
{isCampaignDetailsPage && !item.isDefaultCampaignForm && (
<RowAction
onClick={confirmDefaultCampaignFormModal}
actionId={item.id}
displayText={__('Make as default', 'give')}
hiddenText={item?.name}
/>
)}
</>
)}
</>
Expand Down
3 changes: 3 additions & 0 deletions tests/Unit/DonationForms/Endpoints/TestListDonationForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public function getMockRequest(): WP_REST_Request
}

/**
* @unreleased Add support to isDefaultCampaignForm key
* @since 2.25.0
*
* @param array $donationForms
Expand All @@ -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;
}

Expand Down

0 comments on commit 592e74f

Please sign in to comment.