diff --git a/includes/shortcodes.php b/includes/shortcodes.php index b297901a01..bdba978969 100644 --- a/includes/shortcodes.php +++ b/includes/shortcodes.php @@ -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 @@ -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' diff --git a/src/DonationForms/DonationQuery.php b/src/DonationForms/DonationQuery.php index b4ed4ac58f..a89e53c998 100644 --- a/src/DonationForms/DonationQuery.php +++ b/src/DonationForms/DonationQuery.php @@ -52,6 +52,18 @@ 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 @@ -59,7 +71,11 @@ public function form($formId) 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; } diff --git a/src/MultiFormGoals/ProgressBar/Model.php b/src/MultiFormGoals/ProgressBar/Model.php index 22b1b87db2..53be5ef87d 100644 --- a/src/MultiFormGoals/ProgressBar/Model.php +++ b/src/MultiFormGoals/ProgressBar/Model.php @@ -2,6 +2,7 @@ namespace Give\MultiFormGoals\ProgressBar; +use Give\DonationForms\DonationQuery; use Give\ValueObjects\Money; class Model @@ -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(); } /** diff --git a/templates/shortcode-goal.php b/templates/shortcode-goal.php index ba977a1645..b23dd8c144 100644 --- a/templates/shortcode-goal.php +++ b/templates/shortcode-goal.php @@ -1,6 +1,7 @@ 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. * @@ -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,