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