Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Form inherits campaign goal #7569

Merged
merged 37 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
009839b
initial commit
alaca Oct 14, 2024
c0f912b
refactor: donations value
alaca Oct 14, 2024
9bf55c6
refactor: simplify modal title logic; use correct value for the donat…
alaca Oct 14, 2024
ded5bef
feature: copy campaign goal settings
alaca Oct 14, 2024
f9df069
refactor: remove unused styles
alaca Oct 14, 2024
c465abd
feature: add campaign id migration
alaca Oct 15, 2024
4bee4fc
feature: associate donations to campaign migration
alaca Oct 15, 2024
2baa68c
feature: add migrations
alaca Oct 15, 2024
7e3e2c7
fix: add migrations
alaca Oct 15, 2024
9d05a52
feature: add indexes to revenue table
alaca Oct 15, 2024
4ca3399
feature: addcampaign goal type as default
alaca Oct 17, 2024
e244837
feature: add findByFormId method
alaca Oct 17, 2024
afa847d
feature: add getByFormId method
alaca Oct 17, 2024
4a1ecdd
fix: goal type value
alaca Oct 17, 2024
a8ec44c
feature: use CampaignDonationQuery for campaign goal type
alaca Oct 17, 2024
04d0336
feature: add campaign goal type
alaca Oct 17, 2024
84edd20
refactor: hide goal options for campaign goal type
alaca Oct 17, 2024
75183fd
feature: add campaign goal type option
alaca Oct 17, 2024
b8fc35a
Merge remote-tracking branch 'refs/remotes/origin/epic/campaigns' int…
alaca Oct 17, 2024
9d0bf68
feature: campaign goal dto
alaca Oct 18, 2024
e7e4b5b
feature: get campaign goal settings
alaca Oct 18, 2024
3dc325f
refactor: use campaign goal dto
alaca Oct 18, 2024
8f35098
refactor: move helper function outside component
alaca Oct 18, 2024
1a52741
feature: add campaign goal type
alaca Oct 18, 2024
15ff7a5
refactor: rollback
alaca Oct 18, 2024
7a9a183
fix: migration name
alaca Oct 18, 2024
34dd9f9
refactor: remove campaign goal type
alaca Oct 18, 2024
47091c6
feature: format goal
alaca Oct 18, 2024
c21405f
feature: filter amount
alaca Oct 18, 2024
b89e886
chore: remove unused
alaca Oct 18, 2024
20115bf
fix: return type
alaca Oct 21, 2024
4ca37fe
chore: remove unused imports
alaca Oct 21, 2024
9d80e7d
chore: remove tag
alaca Oct 21, 2024
7acbe09
refactor: form inherit campaign goal
alaca Oct 21, 2024
363a15c
fix: pass correct goal type
alaca Oct 21, 2024
cb91501
chore: add unreleased tags
alaca Oct 21, 2024
c6ffe58
refactor: use await in async functions
alaca Oct 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Campaigns/Actions/CreateDefaultCampaignForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use Give\Campaigns\Models\Campaign;
use Give\Campaigns\Repositories\CampaignRepository;
use Give\DonationForms\Models\DonationForm;
use Give\DonationForms\Properties\FormSettings;
use Give\DonationForms\ValueObjects\DonationFormStatus;
use Give\DonationForms\ValueObjects\GoalType;

/**
* @unreleased
Expand All @@ -21,6 +23,12 @@ public function __invoke(Campaign $campaign)
$defaultCampaignForm = DonationForm::factory()->create([
'title' => $campaign->title,
'status' => DonationFormStatus::DRAFT(),
'settings' => FormSettings::fromArray([
'enableDonationGoal' => true,
'goalAmount' => $campaign->goal,
'goalType' => $campaign->goalType->getValue(),
'designId' => 'classic',
]),
]);

give(CampaignRepository::class)->addCampaignForm($campaign, $defaultCampaignForm, true);
Expand Down
33 changes: 33 additions & 0 deletions src/Campaigns/Actions/FormInheritsCampaignGoal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Give\Campaigns\Actions;

use Give\Campaigns\Models\Campaign;
use Give\DonationForms\Models\DonationForm;
use Give\DonationForms\ValueObjects\GoalType;

/**
* @unreleased
*
* Form inherits campaign goal
*
* @event givewp_donation_form_creating
*/
class FormInheritsCampaignGoal
{
/**
* @unreleased
*/
public function __invoke(DonationForm $donationForm): void
{
if (isset($_GET['campaignId'])) {
$campaign = Campaign::find((int)$_GET['campaignId']);

if ($campaign) {
$donationForm->settings->enableDonationGoal = true;
$donationForm->settings->goalAmount = $campaign->goal;
$donationForm->settings->goalType = new GoalType($campaign->goalType->getValue());
}
}
}
}
123 changes: 123 additions & 0 deletions src/Campaigns/DataTransferObjects/CampaignGoalData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace Give\Campaigns\DataTransferObjects;

use Give\Campaigns\CampaignDonationQuery;
use Give\Campaigns\Models\Campaign;
use Give\DonationForms\ValueObjects\GoalType;
use Give\Framework\Support\Contracts\Arrayable;

/**
* @unreleased
*/
class CampaignGoalData implements Arrayable
{
/**
* @var Campaign
*/
private $campaign;

/**
* @var int
*/
public $actual;

/**
* @var int
*/
public $percentage;

/**
* @var int
*/
private $goal;

/**
* @var int|string
*/
public $goalFormatted;

/**
* @var int|string
*/
public $actualFormatted;

/**
* @unreleased
*/
public function __construct(Campaign $campaign)
{
$this->campaign = $campaign;
$this->actual = $this->getActual();
$this->actualFormatted = $this->getActualFormatted();
$this->percentage = $this->getPercentage();
$this->goal = $campaign->goal;
$this->goalFormatted = $this->getGoalFormatted();
}

/**
* @unreleased
*/
private function getActual(): int
{
$query = new CampaignDonationQuery($this->campaign);

switch ($this->campaign->goalType->getValue()) {
case GoalType::DONATIONS():
return $query->countDonations();

case GoalType::DONORS():
return $query->countDonors();

case GoalType::AMOUNT():
default:
return $query->sumIntendedAmount();
}
}

/**
* @unreleased
*/
private function getPercentage(): float
{
return round($this->actual / $this->campaign->goal * 100, 2);
}

/**
* @unreleased
*/
private function getActualFormatted(): string
{
if ($this->campaign->goalType == GoalType::AMOUNT) {
return give_currency_filter(give_format_amount($this->actual));
}

return $this->actual;
}

/**
* @unreleased
*/
private function getGoalFormatted(): string
{
if ($this->campaign->goalType == GoalType::AMOUNT) {
return give_currency_filter(give_format_amount($this->goal));
}

return $this->goal;
}

/**
* @unreleased
*/
public function toArray(): array
{
return [
'actual' => $this->actual,
'actualFormatted' => $this->actualFormatted,
'percentage' => $this->percentage,
'goal' => $this->goal,
'goalFormatted' => $this->goalFormatted,
];
}
}
26 changes: 7 additions & 19 deletions src/Campaigns/ListTable/Columns/GoalColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Give\Campaigns\ListTable\Columns;

use Give\Campaigns\DataTransferObjects\CampaignGoalData;
use Give\Campaigns\Models\Campaign;
use Give\Framework\ListTable\ModelColumn;

Expand Down Expand Up @@ -35,20 +36,7 @@ public function getLabel(): string
*/
public function getCellValue($model): string
{
// Temp value considering only the default form associated with the campaign
if ($model->defaultForm()) {
$goal = give_goal_progress_stats($model->defaultForm()->id);
$goalPercentage = ('percentage' === $goal['format']) ? str_replace('%', '',
$goal['actual']) : max(min($goal['progress'], 100), 0);
$goalActual = $goal['actual'];
$goalFormat = $goal['format'];
$campaignGoal = $model->goal;
} else {
$goalPercentage = 0;
$goalActual = 0;
$goalFormat = '';
$campaignGoal = $model->goal;
}
$goalData = new CampaignGoalData($model);

$template = '
<div
Expand All @@ -69,16 +57,16 @@ class="goalProgress"
return sprintf(
$template,
$model->id,
$goalPercentage,
$goalActual,
$goalData->percentage,
$goalData->actualFormatted,
sprintf(
($goalFormat !== 'percentage' ? ' %s %s' : ''),
' %s %s',
__('of', 'give'),
$campaignGoal
$goalData->goalFormatted
),
sprintf(
'<span style="opacity:%1$s" class="goalProgress--achieved"><img src="%2$s" alt="%3$s" />%4$s</span>',
apply_filters('givewp_list_table_goal_progress_achieved_opacity', $goalPercentage >= 100 ? 1 : 0),
apply_filters('givewp_list_table_goal_progress_achieved_opacity', $goalData->percentage >= 100 ? 1 : 0),
GIVE_PLUGIN_URL . 'assets/dist/images/list-table/star-icon.svg',
__('Goal achieved icon', 'give'),
__('Goal achieved!', 'give')
Expand Down
2 changes: 1 addition & 1 deletion src/Campaigns/ListTable/Columns/RevenueColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function getLabel(): string
public function getCellValue($model, $locale = ''): string
{
$query = new CampaignDonationQuery($model);
$revenue = $query->sumIntendedAmount();
$revenue = give_currency_filter(give_format_amount($query->sumIntendedAmount()));

return sprintf(
'<a class="column-earnings-value" href="%s" aria-label="%s">%s</a>',
Expand Down
59 changes: 59 additions & 0 deletions src/Campaigns/Migrations/RevenueTable/AddCampaignID.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Give\Campaigns\Migrations\RevenueTable;

use Give\Framework\Database\Exceptions\DatabaseQueryException;
use Give\Framework\Migrations\Contracts\Migration;
use Give\Framework\Migrations\Exceptions\DatabaseMigrationException;

/**
* @unreleased
*/
class AddCampaignID extends Migration
{
/**
* @inheritDoc
*/
public static function id(): string
{
return 'add_campaign_id_to_revenue_table';
}

/**
* @inheritDoc
*/
public static function title(): string
{
return 'Add Campaign ID to revenue table';
}

/**
* @inheritdoc
*/
public static function timestamp(): string
{
return strtotime('2024-10-14 00:00:00');
}

/**
* @inheritDoc
* @throws DatabaseMigrationException
*/
public function run()
{
global $wpdb;

$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);
}
}
}
54 changes: 54 additions & 0 deletions src/Campaigns/Migrations/RevenueTable/AddIndexes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Give\Campaigns\Migrations\RevenueTable;

use Give\Framework\Database\DB;
use Give\Framework\Database\Exceptions\DatabaseQueryException;
use Give\Framework\Migrations\Contracts\Migration;
use Give\Framework\Migrations\Exceptions\DatabaseMigrationException;

/**
* @unreleased
*/
class AddIndexes extends Migration
{
/**
* @inheritDoc
*/
public static function id(): string
{
return 'add_indexes_to_revenue_table';
}

/**
* @inheritDoc
*/
public static function title(): string
{
return 'Add indexes to revenue table';
}

/**
* @inheritdoc
*/
public static function timestamp(): string
{
return strtotime('2024-10-14 00:00:02');
}

/**
* @inheritDoc
* @throws DatabaseMigrationException
*/
public function run()
{
global $wpdb;

try {
DB::query("ALTER TABLE {$wpdb->give_revenue} ADD INDEX (form_id), ADD INDEX (campaign_id)");
} catch (DatabaseQueryException $exception) {
throw new DatabaseMigrationException("An error occurred while updating the {$wpdb->give_revenue} table", 0,
$exception);
}
}
}
Loading
Loading