| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\API\REST\V3\Routes\Campaigns; |
| 4 |
|
| 5 |
use DateInterval; |
| 6 |
use DatePeriod; |
| 7 |
use DateTimeImmutable; |
| 8 |
use Exception; |
| 9 |
use Give\API\REST\V3\Routes\Campaigns\ValueObjects\CampaignRoute; |
| 10 |
use Give\Campaigns\CampaignDonationQuery; |
| 11 |
use Give\Campaigns\Models\Campaign; |
| 12 |
use Give\Framework\Permissions\Facades\UserPermissions; |
| 13 |
use Give\Framework\Support\Facades\DateTime\Temporal; |
| 14 |
use WP_Error; |
| 15 |
use WP_REST_Controller; |
| 16 |
use WP_REST_Response; |
| 17 |
use WP_REST_Server; |
| 18 |
|
| 19 |
/** |
| 20 |
* @since 4.13.1 |
| 21 |
*/ |
| 22 |
class CampaignStatisticsController extends WP_REST_Controller |
| 23 |
{ |
| 24 |
/** |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
protected $namespace; |
| 28 |
|
| 29 |
/** |
| 30 |
* @since 4.13.1 |
| 31 |
*/ |
| 32 |
public function __construct() |
| 33 |
{ |
| 34 |
$this->namespace = CampaignRoute::NAMESPACE; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @since 4.14.0 update permission capability to use facade |
| 39 |
* @since 4.13.0 add schema |
| 40 |
* @since 4.0.0 |
| 41 |
*/ |
| 42 |
public function register_routes() |
| 43 |
{ |
| 44 |
register_rest_route( |
| 45 |
$this->namespace, |
| 46 |
'/' . CampaignRoute::CAMPAIGN . '/statistics', |
| 47 |
[ |
| 48 |
[ |
| 49 |
'methods' => WP_REST_Server::READABLE, |
| 50 |
'callback' => [$this, 'get_items'], |
| 51 |
'permission_callback' => function () { |
| 52 |
return UserPermissions::campaigns()->canView(); |
| 53 |
}, |
| 54 |
'args' => [ |
| 55 |
'id' => [ |
| 56 |
'type' => 'integer', |
| 57 |
'required' => true, |
| 58 |
'sanitize_callback' => 'absint', |
| 59 |
], |
| 60 |
'rangeInDays' => [ |
| 61 |
'type' => 'integer', |
| 62 |
'required' => false, |
| 63 |
'sanitize_callback' => 'absint', |
| 64 |
'default' => 0, |
| 65 |
], |
| 66 |
], |
| 67 |
], |
| 68 |
'schema' => [$this, 'get_public_item_schema'], |
| 69 |
] |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* @since 4.13.0 return 404 error if campaign is not found |
| 75 |
* @since 4.0.0 |
| 76 |
* |
| 77 |
* @throws Exception |
| 78 |
*/ |
| 79 |
public function get_items($request): WP_REST_Response |
| 80 |
{ |
| 81 |
$campaign = Campaign::find($request->get_param('id')); |
| 82 |
|
| 83 |
if (!$campaign) { |
| 84 |
$response = new WP_Error('campaign_not_found', __('Campaign not found', 'give'), ['status' => 404]); |
| 85 |
|
| 86 |
return rest_ensure_response($response); |
| 87 |
} |
| 88 |
|
| 89 |
$query = new CampaignDonationQuery($campaign); |
| 90 |
|
| 91 |
if (!$request->get_param('rangeInDays')) { |
| 92 |
$data = [[ |
| 93 |
'amountRaised' => $query->sumIntendedAmount(), |
| 94 |
'donationCount' => $query->countDonations(), |
| 95 |
'donorCount' => $query->countDonors(), |
| 96 |
]]; |
| 97 |
|
| 98 |
$items = new WP_REST_Response($data); |
| 99 |
|
| 100 |
return rest_ensure_response($items); |
| 101 |
} |
| 102 |
|
| 103 |
$days = (int)$request->get_param('rangeInDays'); |
| 104 |
$date = new DateTimeImmutable('now', wp_timezone()); |
| 105 |
$interval = DateInterval::createFromDateString("-$days days"); |
| 106 |
$period = new DatePeriod($date, $interval, 1); |
| 107 |
|
| 108 |
$data = array_map(function ($targetDate) use ($query, $interval) { |
| 109 |
$rangeQuery = $query->between( |
| 110 |
Temporal::withStartOfDay($targetDate->add($interval)), |
| 111 |
Temporal::withEndOfDay($targetDate) |
| 112 |
); |
| 113 |
|
| 114 |
return [ |
| 115 |
'amountRaised' => $rangeQuery->sumIntendedAmount(), |
| 116 |
'donationCount' => $rangeQuery->countDonations(), |
| 117 |
'donorCount' => $rangeQuery->countDonors(), |
| 118 |
]; |
| 119 |
}, iterator_to_array($period)); |
| 120 |
|
| 121 |
$items = new WP_REST_Response($data); |
| 122 |
|
| 123 |
return rest_ensure_response($items); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* @since 4.13.0 |
| 128 |
*/ |
| 129 |
public function get_item_schema(): array |
| 130 |
{ |
| 131 |
return [ |
| 132 |
'title' => 'givewp/campaign-statistics', |
| 133 |
'description' => esc_html__('Provides statistics for a specific campaign.', 'give'), |
| 134 |
'type' => 'array', |
| 135 |
'readonly' => true, |
| 136 |
'items' => [ |
| 137 |
'type' => 'object', |
| 138 |
'properties' => [ |
| 139 |
'amountRaised' => [ |
| 140 |
'type' => ['integer', 'number'], |
| 141 |
'description' => esc_html__('The amount raised for the campaign.', 'give'), |
| 142 |
], |
| 143 |
'donationCount' => [ |
| 144 |
'type' => 'integer', |
| 145 |
'description' => esc_html__('The number of donations for the campaign.', 'give'), |
| 146 |
], |
| 147 |
'donorCount' => [ |
| 148 |
'type' => 'integer', |
| 149 |
'description' => esc_html__('The number of donors for the campaign.', 'give'), |
| 150 |
], |
| 151 |
], |
| 152 |
], |
| 153 |
]; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|