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