| 1 |
<?php |
| 2 |
|
| 3 |
namespace Leadin\api; |
| 4 |
|
| 5 |
use Leadin\data\Filters; |
| 6 |
|
| 7 |
/** |
| 8 |
* Base class to set a rest endpoint |
| 9 |
*/ |
| 10 |
class Base_Api_Controller { |
| 11 |
|
| 12 |
/** |
| 13 |
* Register a route with given parameters |
| 14 |
* |
| 15 |
* @param string $path The path for the route to register the service on. Route gets namespaced with leadin/v1. |
| 16 |
* @param string $methods Comma seperated list of methods allowed for this route. |
| 17 |
* @param array $callback Method to execute when this endpoint is requested. |
| 18 |
*/ |
| 19 |
public function register_leadin_route( $path, $methods, $callback ) { |
| 20 |
register_rest_route( |
| 21 |
'leadin/v1', |
| 22 |
$path, |
| 23 |
array( |
| 24 |
'methods' => $methods, |
| 25 |
'callback' => $callback, |
| 26 |
'permission_callback' => array( $this, 'verify_permissions' ), |
| 27 |
) |
| 28 |
); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Register an admin route with given parameters |
| 33 |
* |
| 34 |
* @param string $path The path for the route to register the service on. Route gets namespaced with leadin/v1. |
| 35 |
* @param string $methods Comma seperated list of methods allowed for this route. |
| 36 |
* @param array $callback Method to execute when this endpoint is requested. |
| 37 |
*/ |
| 38 |
public function register_leadin_admin_route( $path, $methods, $callback ) { |
| 39 |
register_rest_route( |
| 40 |
'leadin/v1', |
| 41 |
$path, |
| 42 |
array( |
| 43 |
'methods' => $methods, |
| 44 |
'callback' => $callback, |
| 45 |
'permission_callback' => array( $this, 'verify_admin_permissions' ), |
| 46 |
) |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* Permissions required by user to execute the request. User permissions are already |
| 53 |
* verified by nonce 'wp_rest' automatically. |
| 54 |
* |
| 55 |
* @return bool true if the user has adequate permissions for this endpoint. |
| 56 |
*/ |
| 57 |
public function verify_permissions() { |
| 58 |
return current_user_can( Filters::apply_view_plugin_menu_capability_filters() ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Permissions required by user to execute the request. User permissions are already |
| 63 |
* verified by nonce 'wp_rest' automatically. |
| 64 |
* |
| 65 |
* @return bool true if the user has adequate admin permissions for this endpoint. |
| 66 |
*/ |
| 67 |
public function verify_admin_permissions() { |
| 68 |
return current_user_can( Filters::apply_connect_plugin_capability_filters() ); |
| 69 |
} |
| 70 |
|
| 71 |
} |
| 72 |
|