Skip to content

Commit

Permalink
Feature: Add campaign pages (#7510)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjohnson authored Sep 3, 2024
1 parent 0b24a19 commit f12b328
Show file tree
Hide file tree
Showing 12 changed files with 569 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/Campaigns/Actions/ConvertQueryDataToCampaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public function __invoke(object $queryObject): Campaign
{
return new Campaign([
'id' => (int)$queryObject->id,
'pageId' => (int)$queryObject->pageId,
'type' => new CampaignType($queryObject->type),
'title' => $queryObject->title,
'shortDescription' => $queryObject->shortDescription,
Expand Down
30 changes: 30 additions & 0 deletions src/Campaigns/Actions/EditCampaignPageRedirect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Give\Campaigns\Actions;

use Give\Campaigns\Models\Campaign;
use Give\Campaigns\Models\CampaignPage;

/**
* @unreleased
*/
class EditCampaignPageRedirect
{
/**
* @unreleased
*/
public function __invoke()
{
$campaign = Campaign::find(
// @TODO (Maybe) refactor to request object.
isset($_GET['campaign_id']) ? absint($_GET['campaign_id']) : 0
);

$page = $campaign->page() ?: CampaignPage::create([
'campaignId' => $campaign->id,
]);

wp_safe_redirect($page->getEditLinkUrl(), 303);
exit();
}
}
32 changes: 32 additions & 0 deletions src/Campaigns/Actions/RegisterCampaignPagePostType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Give\Campaigns\Actions;

/**
* @unreleased
*/
class RegisterCampaignPagePostType
{
/**
* @unreleased
*/
public function __invoke()
{
register_post_type( 'give_campaign_page', [
'label' => __('Campaign Page', 'give'),
'public' => true,
'show_ui' => true,
'show_in_menu' => false,
'show_in_rest' => true,
'supports' => [
'editor'
],
'rewrite' => [
'slug' => 'campaign'
],
'template' => [
// TODO: Add default blocks template.
],
] );
}
}
1 change: 0 additions & 1 deletion src/Campaigns/Factories/CampaignFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public function definition(): array
$currentDate = Temporal::getCurrentDateTime();

return [
'pageId' => 1,
'type' => CampaignType::CORE(),
'title' => __('GiveWP Campaign', 'give'),
'shortDescription' => __('Campaign short description', 'give'),
Expand Down
1 change: 0 additions & 1 deletion src/Campaigns/Migrations/MigrateFormsToCampaignForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public function run()
public function createParentCampaignForDonationForm(DonationForm $form)
{
$campaign = Campaign::create([
'pageId' => 0,
'type' => CampaignType::CORE(),
'title' => $form->title,
'shortDescription' => $form->settings->formExcerpt,
Expand Down
11 changes: 9 additions & 2 deletions src/Campaigns/Models/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DateTime;
use Give\Campaigns\Actions\ConvertQueryDataToCampaign;
use Give\Campaigns\Factories\CampaignFactory;
use Give\Campaigns\Repositories\CampaignPageRepository;
use Give\Campaigns\Repositories\CampaignRepository;
use Give\Campaigns\ValueObjects\CampaignStatus;
use Give\Campaigns\ValueObjects\CampaignType;
Expand All @@ -19,7 +20,6 @@
* @unreleased
*
* @property int $id
* @property int $pageId
* @property CampaignType $type
* @property string $title
* @property string $url
Expand All @@ -42,7 +42,6 @@ class Campaign extends Model implements ModelCrud, ModelHasFactory
*/
protected $properties = [
'id' => 'int',
'pageId' => 'int',
'type' => CampaignType::class,
'title' => 'string',
'shortDescription' => 'string',
Expand All @@ -58,6 +57,14 @@ class Campaign extends Model implements ModelCrud, ModelHasFactory
'createdAt' => DateTime::class,
];

/**
* @unreleased
*/
public function page()
{
return give(CampaignPageRepository::class)->findByCampaignId($this->id);
}

/**
* @unreleased
*/
Expand Down
117 changes: 117 additions & 0 deletions src/Campaigns/Models/CampaignPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Give\Campaigns\Models;

use DateTime;
use Give\Campaigns\Repositories\CampaignPageRepository;
use Give\Framework\Models\Contracts\ModelCrud;
use Give\Framework\Models\Model;
use Give\Framework\Models\ModelQueryBuilder;
use Give\Framework\Models\ValueObjects\Relationship;
use Give\Framework\Support\Facades\DateTime\Temporal;

/**
* @unreleased
*
* @property int $id
* @property int $campaignId
* @property DateTime $createdAt
* @property DateTime $updatedAt
*/
class CampaignPage extends Model implements ModelCrud
{
public $properties = [
'id' => 'int',
'campaignId' => 'int',
'createdAt' => DateTime::class,
'updatedAt' => DateTime::class,
];

public $relationships = [
'campaign' => Relationship::BELONGS_TO,
];

/**
* @unreleased
*/
public function getEditLinkUrl(): string
{
// By default, the URL is encoded for display purposes.
// Setting any other value prevents encoding the URL.
return get_edit_post_link($this->id, 'redirect');
}

/**
* @unreleased
*/
public function campaign()
{
return Campaign::find($this->campaignId);
}

/**
* @unreleased
*/
public static function find($id)
{
return give(CampaignPageRepository::class)
->prepareQuery()
->where('ID', $id)
->get();
}

/**
* @unreleased
*/
public static function create(array $attributes): CampaignPage
{
$campaignPage = new static($attributes);

give(CampaignPageRepository::class)->insert($campaignPage);

return $campaignPage;
}

/**
* @unreleased
*/
public function save(): void
{
if (!$this->id) {
give(CampaignPageRepository::class)->insert($this);
} else {
give(CampaignPageRepository::class)->update($this);
}
}

/**
* @unreleased
*/
public function delete(): bool
{
return give(CampaignPageRepository::class)->delete($this);
}

/**
* @unreleased
*
* @return ModelQueryBuilder<CampaignPage>
*/
public static function query(): ModelQueryBuilder
{
return give(CampaignPageRepository::class)->prepareQuery();
}

/**
* @unreleased
*/
public static function fromQueryBuilderObject($object): CampaignPage
{
return new CampaignPage([
'id' => (int) $object->id,
'campaignId' => (int) $object->campaignId,
'createdAt' => Temporal::toDateTime($object->createdAt),
'updatedAt' => Temporal::toDateTime($object->updatedAt),
]);
}
}
Loading

0 comments on commit f12b328

Please sign in to comment.