ValueObjects
1 year ago
GetCampaignComments.php
1 year ago
GetCampaignRevenue.php
1 year ago
GetCampaignStatistics.php
1 year ago
RegisterCampaignRoutes.php
1 year ago
GetCampaignStatistics.php
94 lines
| 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\API\RestRoute; |
| 11 | use Give\Campaigns\CampaignDonationQuery; |
| 12 | use Give\Campaigns\Models\Campaign; |
| 13 | use Give\Framework\Support\Facades\DateTime\Temporal; |
| 14 | use WP_REST_Response; |
| 15 | use WP_REST_Server; |
| 16 | |
| 17 | /** |
| 18 | * @since 4.0.0 |
| 19 | */ |
| 20 | class GetCampaignStatistics implements RestRoute |
| 21 | { |
| 22 | /** |
| 23 | * @since 4.0.0 |
| 24 | */ |
| 25 | public function registerRoute() |
| 26 | { |
| 27 | register_rest_route( |
| 28 | CampaignRoute::NAMESPACE, |
| 29 | CampaignRoute::CAMPAIGN . '/statistics', |
| 30 | [ |
| 31 | [ |
| 32 | 'methods' => WP_REST_Server::READABLE, |
| 33 | 'callback' => [$this, 'handleRequest'], |
| 34 | 'permission_callback' => function () { |
| 35 | return current_user_can('manage_options'); |
| 36 | }, |
| 37 | ], |
| 38 | 'args' => [ |
| 39 | 'id' => [ |
| 40 | 'type' => 'integer', |
| 41 | 'required' => true, |
| 42 | 'sanitize_callback' => 'absint', |
| 43 | ], |
| 44 | 'rangeInDays' => [ |
| 45 | 'type' => 'integer', |
| 46 | 'required' => false, |
| 47 | 'sanitize_callback' => 'absint', |
| 48 | 'default' => 0, // Zero to mean "all time". |
| 49 | ], |
| 50 | ], |
| 51 | ] |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @since 4.0.0 |
| 57 | * |
| 58 | * @throws Exception |
| 59 | */ |
| 60 | public function handleRequest($request): WP_REST_Response |
| 61 | { |
| 62 | $campaign = Campaign::find($request->get_param('id')); |
| 63 | |
| 64 | $query = new CampaignDonationQuery($campaign); |
| 65 | |
| 66 | if(!$request->get_param('rangeInDays')) { |
| 67 | return new WP_REST_Response([[ |
| 68 | 'amountRaised' => $query->sumIntendedAmount(), |
| 69 | 'donationCount' => $query->countDonations(), |
| 70 | 'donorCount' => $query->countDonors(), |
| 71 | ]]); |
| 72 | } |
| 73 | |
| 74 | $days = $request->get_param('rangeInDays'); |
| 75 | $date = new DateTimeImmutable('now', wp_timezone()); |
| 76 | $interval = DateInterval::createFromDateString("-$days days"); |
| 77 | $period = new DatePeriod($date, $interval, 1); |
| 78 | |
| 79 | return new WP_REST_Response(array_map(function($targetDate) use ($query, $interval) { |
| 80 | |
| 81 | $query = $query->between( |
| 82 | Temporal::withStartOfDay($targetDate->add($interval)), |
| 83 | Temporal::withEndOfDay($targetDate) |
| 84 | ); |
| 85 | |
| 86 | return [ |
| 87 | 'amountRaised' => $query->sumIntendedAmount(), |
| 88 | 'donationCount' => $query->countDonations(), |
| 89 | 'donorCount' => $query->countDonors(), |
| 90 | ]; |
| 91 | }, iterator_to_array($period) )); |
| 92 | } |
| 93 | } |
| 94 |