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
LocationRoute.php
84 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\FormatList; |
| 8 | use Give\Onboarding\Helpers\CountryCode; |
| 9 | use Give\Onboarding\Helpers\LocationList; |
| 10 | |
| 11 | /** |
| 12 | * @since 2.8.0 |
| 13 | */ |
| 14 | class LocationRoute implements RestRoute { |
| 15 | |
| 16 | /** @var string */ |
| 17 | protected $endpoint = 'onboarding/location'; |
| 18 | |
| 19 | /** |
| 20 | * @param WP_REST_Request $request |
| 21 | * |
| 22 | * @return array |
| 23 | * |
| 24 | * @since 2.8.0 |
| 25 | */ |
| 26 | public function handleRequest( WP_REST_Request $request ) { |
| 27 | return [ |
| 28 | 'states' => LocationList::getStates( |
| 29 | $request->get_param( 'countryCode' ) |
| 30 | ), |
| 31 | ]; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @inheritDoc |
| 36 | */ |
| 37 | public function registerRoute() { |
| 38 | register_rest_route( |
| 39 | 'give-api/v2', |
| 40 | $this->endpoint, |
| 41 | [ |
| 42 | [ |
| 43 | 'methods' => 'GET', |
| 44 | 'callback' => [ $this, 'handleRequest' ], |
| 45 | 'permission_callback' => function() { |
| 46 | return current_user_can( 'manage_options' ); |
| 47 | }, |
| 48 | 'args' => [ |
| 49 | 'countryCode' => [ |
| 50 | 'type' => 'string', |
| 51 | 'required' => true, |
| 52 | 'validate_callback' => 'give_get_country_name_by_key', |
| 53 | 'sanitize_callback' => 'sanitize_text_field', |
| 54 | ], |
| 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 | 'countryCode' => [ |
| 77 | 'description' => esc_html__( 'A short alphabetic geographical code representing a country.', 'give' ), |
| 78 | 'type' => 'string', |
| 79 | ], |
| 80 | ], |
| 81 | ]; |
| 82 | } |
| 83 | } |
| 84 |