DeleteDonor.php
3 years ago
Endpoint.php
4 years ago
ListDonors.php
3 years ago
SwitchDonorView.php
4 years ago
Endpoint.php
85 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Donors\Endpoints; |
| 4 | |
| 5 | use Give\API\RestRoute; |
| 6 | use WP_Error; |
| 7 | use WP_REST_Request; |
| 8 | |
| 9 | abstract class Endpoint implements RestRoute |
| 10 | { |
| 11 | /** |
| 12 | * @var string |
| 13 | */ |
| 14 | protected $endpoint; |
| 15 | |
| 16 | /** |
| 17 | * @param string $value |
| 18 | * @since 2.20.0 |
| 19 | * |
| 20 | * @return bool |
| 21 | */ |
| 22 | public function validateInt($value) |
| 23 | { |
| 24 | return filter_var($value, FILTER_VALIDATE_INT); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @param string $param |
| 29 | * @param WP_REST_Request $request |
| 30 | * @param string $key |
| 31 | * @since 2.20.0 |
| 32 | * |
| 33 | * @return bool |
| 34 | */ |
| 35 | public function validateDate($param, $request, $key) |
| 36 | { |
| 37 | // Check that date is valid, and formatted YYYY-MM-DD |
| 38 | list($year, $month, $day) = explode('-', $param); |
| 39 | $valid = checkdate($month, $day, $year); |
| 40 | |
| 41 | // If checking end date, check that it is after start date |
| 42 | if ('end' === $key) { |
| 43 | $start = date_create($request->get_param('start')); |
| 44 | $end = date_create($request->get_param('end')); |
| 45 | $valid = $start <= $end ? $valid : false; |
| 46 | } |
| 47 | |
| 48 | return $valid; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Check user permissions |
| 53 | * @since 2.20.0 |
| 54 | * |
| 55 | * @return bool|WP_Error |
| 56 | */ |
| 57 | public function permissionsCheck() |
| 58 | { |
| 59 | if (!current_user_can('edit_posts')) { |
| 60 | return new WP_Error( |
| 61 | 'rest_forbidden', |
| 62 | esc_html__('You dont have the right permissions to view Donors', 'give'), |
| 63 | ['status' => $this->authorizationStatusCode()] |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Sets up the proper HTTP status code for authorization. |
| 72 | * @since 2.20.0 |
| 73 | * |
| 74 | * @return int |
| 75 | */ |
| 76 | public function authorizationStatusCode() |
| 77 | { |
| 78 | if (is_user_logged_in()) { |
| 79 | return 403; |
| 80 | } |
| 81 | |
| 82 | return 401; |
| 83 | } |
| 84 | } |
| 85 |