Endpoint.php
1 year ago
FormActions.php
1 year ago
ListDonationForms.php
8 months ago
SwitchDonationFormView.php
3 years ago
Endpoint.php
66 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms\V2\Endpoints; |
| 4 | |
| 5 | use Give\API\RestRoute; |
| 6 | use WP_Error; |
| 7 | |
| 8 | /** |
| 9 | * @since 2.19.0 |
| 10 | */ |
| 11 | abstract class Endpoint implements RestRoute |
| 12 | { |
| 13 | |
| 14 | /** |
| 15 | * @var string |
| 16 | */ |
| 17 | protected $endpoint; |
| 18 | |
| 19 | /** |
| 20 | * @param string $value |
| 21 | * |
| 22 | * @return bool |
| 23 | */ |
| 24 | public function validateInt($value) |
| 25 | { |
| 26 | return filter_var($value, FILTER_VALIDATE_INT); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @since 3.14.0 |
| 31 | * @param string $id |
| 32 | * @return bool |
| 33 | */ |
| 34 | public function validatePostType(string $id) |
| 35 | { |
| 36 | return get_post_type($id) === 'give_forms'; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Check user permissions |
| 41 | * @return bool|WP_Error |
| 42 | */ |
| 43 | public function permissionsCheck() |
| 44 | { |
| 45 | if (current_user_can('manage_options') || current_user_can('edit_give_forms')) { |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | return new WP_Error( |
| 50 | 'rest_forbidden', |
| 51 | esc_html__("You don't have permission to view forms", 'give'), |
| 52 | ['status' => is_user_logged_in() ? 403 : 401] |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | // Sets up the proper HTTP status code for authorization. |
| 57 | public function authorizationStatusCode() |
| 58 | { |
| 59 | if (is_user_logged_in()) { |
| 60 | return 403; |
| 61 | } |
| 62 | |
| 63 | return 401; |
| 64 | } |
| 65 | } |
| 66 |