DonationsRoute.php
111 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonorDashboards\Tabs\DonationHistoryTab; |
| 4 | |
| 5 | use Give\DonorDashboards\Repositories\Donations as DonationsRepository; |
| 6 | use Give\DonorDashboards\Tabs\Contracts\Route as RouteAbstract; |
| 7 | use Give\Log\Log; |
| 8 | use WP_REST_Request; |
| 9 | use WP_REST_Response; |
| 10 | |
| 11 | /** |
| 12 | * @since 2.10.2 |
| 13 | */ |
| 14 | class DonationsRoute extends RouteAbstract |
| 15 | { |
| 16 | |
| 17 | /** |
| 18 | * @return string |
| 19 | */ |
| 20 | public function endpoint() |
| 21 | { |
| 22 | return 'donations'; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @return array |
| 27 | */ |
| 28 | public function args() |
| 29 | { |
| 30 | return []; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @since 2.10.2 |
| 35 | * |
| 36 | * @param WP_REST_Request $request |
| 37 | * |
| 38 | * @return WP_REST_Response |
| 39 | * |
| 40 | */ |
| 41 | public function handleRequest(WP_REST_Request $request) |
| 42 | { |
| 43 | $donorId = give()->donorDashboard->getId(); |
| 44 | |
| 45 | $repository = new DonationsRepository(); |
| 46 | |
| 47 | return $this->getData($repository, $donorId); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @since 2.10.2 |
| 52 | * |
| 53 | * @param DonationsRepository $repository |
| 54 | * @param $donorId |
| 55 | * |
| 56 | * @return WP_REST_Response |
| 57 | */ |
| 58 | protected function getData(DonationsRepository $repository, $donorId) |
| 59 | { |
| 60 | // If the provided donor ID is valid, attempt to query data |
| 61 | try { |
| 62 | $donations = $repository->getDonations($donorId); |
| 63 | $count = $repository->getDonationCount($donorId); |
| 64 | $revenue = $repository->getRevenue($donorId); |
| 65 | $average = $repository->getAverageRevenue($donorId); |
| 66 | $currency = [ |
| 67 | 'symbol' => give_currency_symbol(give_get_currency(), true), |
| 68 | 'position' => give_get_currency_position(), |
| 69 | ]; |
| 70 | |
| 71 | return new WP_REST_Response( |
| 72 | [ |
| 73 | 'status' => 200, |
| 74 | 'response' => 'success', |
| 75 | 'body_response' => [ |
| 76 | [ |
| 77 | 'donations' => $donations, |
| 78 | 'count' => $count, |
| 79 | 'revenue' => $revenue, |
| 80 | 'average' => $average, |
| 81 | 'currency' => $currency, |
| 82 | ], |
| 83 | ], |
| 84 | ] |
| 85 | ); |
| 86 | } catch (\Exception $e) { |
| 87 | Log::error( |
| 88 | esc_html__('An error occurred while retrieving donation records', 'give'), |
| 89 | [ |
| 90 | 'source' => 'Donor Dashboard', |
| 91 | 'Donor ID' => $donorId, |
| 92 | 'Error' => $e->getMessage(), |
| 93 | ] |
| 94 | ); |
| 95 | |
| 96 | return new WP_REST_Response( |
| 97 | [ |
| 98 | 'status' => 400, |
| 99 | 'response' => 'database_error', |
| 100 | 'body_response' => [ |
| 101 | 'message' => esc_html__( |
| 102 | 'An error occurred while retrieving your donation records. Contact the site administrator for assistance.', |
| 103 | 'give' |
| 104 | ), |
| 105 | ], |
| 106 | ] |
| 107 | ); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 |