| 1 |
<?php |
| 2 |
|
| 3 |
namespace SureCart\Rest; |
| 4 |
|
| 5 |
use SureCart\Controllers\Rest\CheckEmailController; |
| 6 |
use SureCart\Rest\RestServiceInterface; |
| 7 |
use SureCart\Controllers\Rest\VerificationCodeController; |
| 8 |
|
| 9 |
/** |
| 10 |
* Service provider for Price Rest Requests |
| 11 |
*/ |
| 12 |
class VerificationCodeRestServiceProvider extends RestServiceProvider implements RestServiceInterface { |
| 13 |
/** |
| 14 |
* Endpoint. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
protected $endpoint = 'verification_codes'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Rest Controller |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $controller = VerificationCodeController::class; |
| 26 |
|
| 27 |
/** |
| 28 |
* Methods allowed for the model. |
| 29 |
* |
| 30 |
* @var array |
| 31 |
*/ |
| 32 |
protected $methods = [ 'create' ]; |
| 33 |
|
| 34 |
/** |
| 35 |
* Register additional routes (the /verify route). |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function registerRoutes() { |
| 40 |
register_rest_route( |
| 41 |
"$this->name/v$this->version", |
| 42 |
$this->endpoint . '/verify/', |
| 43 |
[ |
| 44 |
[ |
| 45 |
'methods' => \WP_REST_Server::EDITABLE, |
| 46 |
'callback' => $this->callback( $this->controller, 'verify' ), |
| 47 |
'permission_callback' => [ $this, 'verify_permissions_check' ], |
| 48 |
], |
| 49 |
// Register our schema callback. |
| 50 |
'schema' => [ $this, 'get_item_schema' ], |
| 51 |
] |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get our sample schema for a post. |
| 57 |
* |
| 58 |
* @return array The sample schema for a post |
| 59 |
*/ |
| 60 |
public function get_item_schema() { |
| 61 |
if ( $this->schema ) { |
| 62 |
// Since WordPress 5.3, the schema can be cached in the $schema property. |
| 63 |
return $this->schema; |
| 64 |
} |
| 65 |
|
| 66 |
$this->schema = [ |
| 67 |
// This tells the spec of JSON Schema we are using which is draft 4. |
| 68 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 69 |
// The title property marks the identity of the resource. |
| 70 |
'title' => $this->endpoint, |
| 71 |
'type' => 'object', |
| 72 |
]; |
| 73 |
|
| 74 |
return $this->schema; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Anyone can get verify a code. |
| 79 |
* |
| 80 |
* @param \WP_REST_Request $request Full details about the request. |
| 81 |
* @return true |
| 82 |
*/ |
| 83 |
public function verify_permissions_check( $request ) { |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Must be a WordPress user to generate a verification code. |
| 89 |
* |
| 90 |
* @param \WP_REST_Request $request Full details about the request. |
| 91 |
* @return boolean |
| 92 |
*/ |
| 93 |
public function create_item_permissions_check( $request ) { |
| 94 |
return ( new CheckEmailController() )->checkEmail( $request ); |
| 95 |
} |
| 96 |
} |
| 97 |
|