| 1 |
<?php |
| 2 |
/** |
| 3 |
* WordPress Rest controller class. |
| 4 |
* |
| 5 |
* @link https://www.webyes.com/ |
| 6 |
* @since 3.0.0 |
| 7 |
* |
| 8 |
* @package WebYes\AccessibilityPlus\Lite\Includes |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace WebYes\AccessibilityPlus\Lite\Includes; |
| 12 |
|
| 13 |
use WP_REST_Controller; |
| 14 |
use WP_Error; |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Abstract Rest Controller Class |
| 22 |
* |
| 23 |
* @package WebYes\AccessibilityPlus\Lite\Includes |
| 24 |
* @extends WP_REST_Controller |
| 25 |
* @version 3.0.0 |
| 26 |
*/ |
| 27 |
abstract class Rest_Controller extends WP_REST_Controller { |
| 28 |
|
| 29 |
/** |
| 30 |
* Endpoint namespace. |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
protected $namespace = 'wya11y/v1'; |
| 35 |
|
| 36 |
/** |
| 37 |
* Route base. |
| 38 |
* |
| 39 |
* @var string |
| 40 |
*/ |
| 41 |
protected $rest_base = ''; |
| 42 |
|
| 43 |
/** |
| 44 |
* Check if a given request has access to read items. |
| 45 |
* |
| 46 |
* @param WP_REST_Request $request Full details about the request. |
| 47 |
* @return WP_Error|boolean |
| 48 |
*/ |
| 49 |
public function get_items_permissions_check( $request ) { |
| 50 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 51 |
return new WP_Error( 'wya11y_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'accessibility-plus' ), array( 'status' => rest_authorization_required_code() ) ); |
| 52 |
} |
| 53 |
|
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Check if a given request has access to create an item. |
| 59 |
* |
| 60 |
* @param WP_REST_Request $request Full details about the request. |
| 61 |
* @return WP_Error|boolean |
| 62 |
*/ |
| 63 |
public function create_item_permissions_check( $request ) { |
| 64 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 65 |
return new WP_Error( 'wya11y_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'accessibility-plus' ), array( 'status' => rest_authorization_required_code() ) ); |
| 66 |
} |
| 67 |
|
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Check if a given request has access to read an item. |
| 73 |
* |
| 74 |
* @param WP_REST_Request $request Full details about the request. |
| 75 |
* @return WP_Error|boolean |
| 76 |
*/ |
| 77 |
public function get_item_permissions_check( $request ) { |
| 78 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 79 |
return new WP_Error( 'wya11y_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'accessibility-plus' ), array( 'status' => rest_authorization_required_code() ) ); |
| 80 |
} |
| 81 |
|
| 82 |
return true; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Check if a given request has access to update an item. |
| 87 |
* |
| 88 |
* @param WP_REST_Request $request Full details about the request. |
| 89 |
* @return WP_Error|boolean |
| 90 |
*/ |
| 91 |
public function update_item_permissions_check( $request ) { |
| 92 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 93 |
return new WP_Error( 'wya11y_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'accessibility-plus' ), array( 'status' => rest_authorization_required_code() ) ); |
| 94 |
} |
| 95 |
|
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Check if a given request has access to delete an item. |
| 101 |
* |
| 102 |
* @param WP_REST_Request $request Full details about the request. |
| 103 |
* @return bool|WP_Error |
| 104 |
*/ |
| 105 |
public function delete_item_permissions_check( $request ) { |
| 106 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 107 |
return new WP_Error( 'wya11y_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'accessibility-plus' ), array( 'status' => rest_authorization_required_code() ) ); |
| 108 |
} |
| 109 |
return true; |
| 110 |
} |
| 111 |
} |
| 112 |
|