DonationActions.php
4 years ago
Endpoint.php
4 years ago
ListDonations.php
4 years ago
SwitchDonationView.php
4 years ago
ListDonations.php
101 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Donations\Endpoints; |
| 4 | |
| 5 | use Give\Donations\Controllers\DonationsRequestController; |
| 6 | use Give\Donations\DataTransferObjects\DonationResponseData; |
| 7 | use WP_REST_Request; |
| 8 | use WP_REST_Response; |
| 9 | |
| 10 | class ListDonations extends Endpoint |
| 11 | { |
| 12 | /** |
| 13 | * @var string |
| 14 | */ |
| 15 | protected $endpoint = 'admin/donations'; |
| 16 | |
| 17 | /** |
| 18 | * @inheritDoc |
| 19 | */ |
| 20 | public function registerRoute() |
| 21 | { |
| 22 | register_rest_route( |
| 23 | 'give-api/v2', |
| 24 | $this->endpoint, |
| 25 | [ |
| 26 | [ |
| 27 | 'methods' => 'GET', |
| 28 | 'callback' => [$this, 'handleRequest'], |
| 29 | 'permission_callback' => [$this, 'permissionsCheck'], |
| 30 | ], |
| 31 | 'args' => [ |
| 32 | 'page' => [ |
| 33 | 'type' => 'integer', |
| 34 | 'required' => false, |
| 35 | 'default' => 1, |
| 36 | 'minimum' => 1 |
| 37 | ], |
| 38 | 'perPage' => [ |
| 39 | 'type' => 'integer', |
| 40 | 'required' => false, |
| 41 | 'default' => 30, |
| 42 | 'minimum' => 1 |
| 43 | ], |
| 44 | 'form' => [ |
| 45 | 'type' => 'integer', |
| 46 | 'required' => false, |
| 47 | 'default' => 0 |
| 48 | ], |
| 49 | 'search' => [ |
| 50 | 'type' => 'string', |
| 51 | 'required' => false, |
| 52 | 'sanitize_callback' => 'sanitize_text_field', |
| 53 | ], |
| 54 | 'start' => [ |
| 55 | 'type' => 'string', |
| 56 | 'required' => false, |
| 57 | 'validate_callback' => [$this, 'validateDate'] |
| 58 | ], |
| 59 | 'end' => [ |
| 60 | 'type' => 'string', |
| 61 | 'required' => false, |
| 62 | 'validate_callback' => [$this, 'validateDate'] |
| 63 | ], |
| 64 | 'donor' => [ |
| 65 | 'type' => 'string', |
| 66 | 'required' => false, |
| 67 | 'sanitize_callback' => 'sanitize_text_field', |
| 68 | ], |
| 69 | ], |
| 70 | ] |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @param WP_REST_Request $request |
| 76 | * @since 2.20.0 |
| 77 | * |
| 78 | * @return WP_REST_Response |
| 79 | */ |
| 80 | public function handleRequest(WP_REST_Request $request): WP_REST_Response |
| 81 | { |
| 82 | $data = []; |
| 83 | $controller = new DonationsRequestController($request); |
| 84 | $donations = $controller->getDonations(); |
| 85 | $donationsCount = $controller->getTotalDonationsCount(); |
| 86 | $totalPages = (int)ceil($donationsCount / $request->get_param('perPage')); |
| 87 | |
| 88 | foreach ($donations as $donation) { |
| 89 | $data[] = DonationResponseData::fromObject($donation)->toArray(); |
| 90 | } |
| 91 | |
| 92 | return new WP_REST_Response( |
| 93 | [ |
| 94 | 'items' => $data, |
| 95 | 'totalItems' => $donationsCount, |
| 96 | 'totalPages' => $totalPages |
| 97 | ] |
| 98 | ); |
| 99 | } |
| 100 | } |
| 101 |