PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.1
GiveWP – Donation Plugin and Fundraising Platform v4.16.1
4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 All 256 releases
give / src / DonorDashboards / Tabs / Contracts / Route.php

Route.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.1, at src/DonorDashboards/Tabs/Contracts/Route.php

125 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\DonorDashboards\Tabs\Contracts;
4
5 use Exception;
6 use Give\API\RestRoute;
7 use Give\DonorDashboards\Helpers as DonorDashboardHelpers;
8 use Give\Log\Log;
9 use WP_Error;
10 use WP_REST_Request;
11 use WP_REST_Response;
12
13 /**
14 * @since 2.10.0
15 */
16 abstract class Route implements RestRoute
17 {
18 /**
19 * Returns string to complete Route endpoint
20 * Full route will be donor-profile/{endpoint}
21 *
22 * @since 2.10.0
23 * @return string
24 *
25 */
26 abstract public function endpoint();
27
28 /**
29 * Returns arguments for Route
30 * For more information, see: https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/#arguments
31 *
32 * @since 2.10.0
33 * @return array
34 *
35 */
36 abstract public function args();
37
38 /**
39 * Handles route request, and returns response
40 *
41 * @since 2.10.0
42 *
43 * @param WP_REST_Request $request
44 *
45 * @return WP_REST_Response|array
46 *
47 */
48 abstract public function handleRequest(WP_REST_Request $request);
49
50 /** @var string */
51 protected $root = 'donor-dashboard/';
52
53 /**
54 * @inheritDoc
55 */
56 public function registerRoute()
57 {
58 register_rest_route(
59 'give-api/v2',
60 "{$this->root}{$this->endpoint()}",
61 [
62 [
63 'methods' => 'POST',
64 'callback' => [$this, 'handleRequestWithDonorIdCheck'],
65 'permission_callback' => function () {
66 return DonorDashboardHelpers::isDonorLoggedIn();
67 },
68 ],
69 'args' => $this->args(),
70 ]
71 );
72 }
73
74 /**
75 * @since 2.26.0 add try/catch to handleRequest
76 */
77 public function handleRequestWithDonorIdCheck(WP_REST_Request $request)
78 {
79 $donorId = $request->get_param('donor_id') !== null
80 ? $request->get_param('donor_id')
81 : give()->donorDashboard->getId();
82
83 // Check that the provided donor ID is valid
84 if (!Give()->donors->get_donor_by('id', $donorId)) {
85 Log::error(
86 esc_html__('An error occurred while validating donor ID on request.', 'give'),
87 [
88 'source' => 'Donor Dashboard',
89 'Donor ID' => give()->donorDashboard->getId(),
90 'Current User ID' => get_current_user_id(),
91 'Email Access Token' => give()->email_access->token_email,
92 'Session Email' => give()->session->get('give_email'),
93 'Session Expiration' => give()->session->get_session_expiration(),
94 'Error' => __('Donor ID could not be validated for request.', 'give'),
95 ]
96 );
97
98 return new WP_REST_Response(
99 [
100 'status' => 400,
101 'response' => 'invalid_donor_id',
102 'body_response' => [
103 'message' => html_entity_decode(
104 esc_html__(
105 'An error occurred while retrieving your donation records. Contact a site administrator and have them search the logs at Donations > Tools > Logs for a more specific cause of the problem.',
106 'give'
107 )
108 ),
109 ],
110 ]
111 );
112 }
113
114 try {
115 $response = $this->handleRequest($request);
116
117 return rest_ensure_response($response ?? []);
118 } catch (Exception $exception) {
119 $error = new WP_Error('error', $exception->getMessage());
120
121 return rest_ensure_response($error);
122 }
123 }
124 }
125