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