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: Update all form goal blocks/shortcodes to respect the new v3 fields/queries #7380

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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']) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might suggest instantiating the query and setting the ->form() value and then conditionally setting a between() value, as opposed to duplicating the new DonationQuery code.

On that note, I've considered adding a ->tap($callback) method to the DonationQuery class which would be great for this.

For example:

(new DonationQuery)
    ->form(1)
    ->tap(function($query) {
       if($startDate && $endDate) {
           $query->between($startDate, $endDate);
        }
    })
    ->sumIndendedAmount()
;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I like that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored it a bit 7ea042e

If you could add the tap method, that would be great.

$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
Loading