Endpoint.php
1 year ago
ListSubscriptions.php
2 years ago
SubscriptionActions.php
1 year ago
SwitchSubscriptionView.php
3 years ago
Endpoint.php
86 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Subscriptions\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 4.3.1 updates permissions |
| 54 | * @since 2.20.0 |
| 55 | * |
| 56 | * @return bool|WP_Error |
| 57 | */ |
| 58 | public function permissionsCheck() |
| 59 | { |
| 60 | if (current_user_can('manage_options') || current_user_can('edit_give_payments')) { |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | return new WP_Error( |
| 65 | 'rest_forbidden', |
| 66 | esc_html__("You don't have permission to view Subscriptions", 'give'), |
| 67 | ['status' => is_user_logged_in() ? 403 : 401] |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Sets up the proper HTTP status code for authorization. |
| 73 | * @since 2.20.0 |
| 74 | * |
| 75 | * @return int |
| 76 | */ |
| 77 | public function authorizationStatusCode() |
| 78 | { |
| 79 | if (is_user_logged_in()) { |
| 80 | return 403; |
| 81 | } |
| 82 | |
| 83 | return 401; |
| 84 | } |
| 85 | } |
| 86 |