| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Onboarding\Routes; |
| 4 |
|
| 5 |
use Give\API\RestRoute; |
| 6 |
use Give\Onboarding\FormRepository; |
| 7 |
use WP_REST_Request; |
| 8 |
|
| 9 |
/** |
| 10 |
* @since 2.8.0 |
| 11 |
*/ |
| 12 |
class FormRoute implements RestRoute |
| 13 |
{ |
| 14 |
|
| 15 |
/** @var string */ |
| 16 |
protected $endpoint = 'onboarding/form'; |
| 17 |
|
| 18 |
/** @var FormRepository */ |
| 19 |
protected $formRepository; |
| 20 |
|
| 21 |
/** |
| 22 |
* @since 2.8.0 |
| 23 |
* |
| 24 |
* @param FormRepository $formRepository |
| 25 |
* |
| 26 |
*/ |
| 27 |
public function __construct(FormRepository $formRepository) |
| 28 |
{ |
| 29 |
$this->formRepository = $formRepository; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @since 2.8.0 |
| 34 |
* |
| 35 |
* @param WP_REST_Request $request |
| 36 |
* |
| 37 |
* @return array |
| 38 |
* |
| 39 |
*/ |
| 40 |
public function handleRequest(WP_REST_Request $request) |
| 41 |
{ |
| 42 |
return [ |
| 43 |
'formID' => $this->formRepository->getOrMake(), |
| 44 |
]; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @inheritDoc |
| 49 |
*/ |
| 50 |
public function registerRoute() |
| 51 |
{ |
| 52 |
register_rest_route( |
| 53 |
'give-api/v2', |
| 54 |
$this->endpoint, |
| 55 |
[ |
| 56 |
[ |
| 57 |
'methods' => 'POST', |
| 58 |
'callback' => [$this, 'handleRequest'], |
| 59 |
'permission_callback' => function () { |
| 60 |
return current_user_can('manage_options'); |
| 61 |
}, |
| 62 |
], |
| 63 |
'schema' => [$this, 'getSchema'], |
| 64 |
] |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @since 2.8.0 |
| 70 |
* @return array |
| 71 |
* |
| 72 |
*/ |
| 73 |
public function getSchema() |
| 74 |
{ |
| 75 |
return [ |
| 76 |
// This tells the spec of JSON Schema we are using which is draft 4. |
| 77 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 78 |
// The title property marks the identity of the resource. |
| 79 |
'title' => 'onboarding', |
| 80 |
'type' => 'object', |
| 81 |
// In JSON Schema you can specify object properties in the properties attribute. |
| 82 |
'properties' => [ |
| 83 |
// ... |
| 84 |
], |
| 85 |
]; |
| 86 |
} |
| 87 |
} |
| 88 |
|