Skip to content

Commit

Permalink
Feature: Update all form goal blocks/shortcodes to respect the new v3…
Browse files Browse the repository at this point in the history
… fields/queries (#7380)

Co-authored-by: Ante Laca <[email protected]>
  • Loading branch information
alaca authored May 6, 2024
1 parent 7b1b2e2 commit 0314393
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
5 changes: 4 additions & 1 deletion includes/shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ function give_form_shortcode( $atts ) {
*
* Show the Give donation form goals.
*
* @unreleased add start_date and end_date attributes
* @since 3.7.0 Sanitize attributes
* @since 3.4.0 Add additional validations to check if the form is valid and has the 'published' status.
* @since 1.0
Expand All @@ -228,7 +229,9 @@ function give_goal_shortcode( $atts ) {
'id' => '',
'show_text' => true,
'show_bar' => true,
'color' => '',
'color' => '#66BB6A',
'start_date' => '',
'end_date' => '',
],
$atts,
'give_goal'
Expand Down
18 changes: 17 additions & 1 deletion src/DonationForms/DonationQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,30 @@ public function form($formId)
return $this;
}


/**
* An opinionated where method for the multiple donation form IDs meta field.
* @unreleased
*/
public function forms(array $formIds)
{
$this->joinMeta('_give_payment_form_id', 'formId');
$this->whereIn('formId.meta_value', $formIds);
return $this;
}

/**
* An opinionated whereBetween method for the completed date meta field.
* @unreleased
*/
public function between($startDate, $endDate)
{
$this->joinMeta('_give_completed_date', 'completed');
$this->whereBetween('completed.meta_value', $startDate, $endDate);
$this->whereBetween(
'completed.meta_value',
date('Y-m-d H:i:s', strtotime($startDate)),
date('Y-m-d H:i:s', strtotime($endDate))
);
return $this;
}

Expand Down
7 changes: 3 additions & 4 deletions src/MultiFormGoals/ProgressBar/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Give\MultiFormGoals\ProgressBar;

use Give\DonationForms\DonationQuery;
use Give\ValueObjects\Money;

class Model
Expand Down Expand Up @@ -132,14 +133,12 @@ public function getDonationRevenueResults()
/**
* Get raw earnings value for Progress Bar
*
* @unreleased use DonationQuery
* @since 2.9.0
*/
public function getTotal(): string
{
$query = new Query($this->getForms());
$results = $query->getResults();

return Money::ofMinor($results->total, give_get_option('currency'))->getAmount();
return (new DonationQuery())->forms($this->getForms())->sumIntendedAmount();
}

/**
Expand Down
21 changes: 20 additions & 1 deletion templates/shortcode-goal.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

use Give\Log\Log;
use Give\DonationForms\DonationQuery;

/**
* This template is used to display the goal with [give_goal]
*/

/**
* @var int $form_id form id passed from the give_show_goal_progress() context
* @var $args array shortcode args
*/

if ( empty($form_id) ) {
Expand Down Expand Up @@ -36,6 +38,23 @@
$show_text = isset( $args['show_text'] ) ? filter_var( $args['show_text'], FILTER_VALIDATE_BOOLEAN ) : true;
$show_bar = isset( $args['show_bar'] ) ? filter_var( $args['show_bar'], FILTER_VALIDATE_BOOLEAN ) : true;

/**
* @unreleased use DonationQuery to get donation amounts
*/
$form_income = 0;
$donationQuery = (new DonationQuery())->form($form->ID);

if ($args['start_date'] === $args['end_date']) {
$form_income = $donationQuery->sumIntendedAmount();
} else {
// If end date is not set, we have to use the current datetime.
if ( ! $args['end_date']) {
$args['end_date'] = date('Y-m-d H:i:s');
}

$form_income = $donationQuery->between($args['start_date'], $args['end_date'])->sumIntendedAmount();
}

/**
* Allow filtering the goal stats used for this shortcode context.
*
Expand All @@ -49,7 +68,7 @@
$shortcode_stats = apply_filters(
'give_goal_shortcode_stats',
array(
'income' => $form->get_earnings(),
'income' => $form_income,
'goal' => $goal_progress_stats['raw_goal'],
),
$form_id,
Expand Down

0 comments on commit 0314393

Please sign in to comment.