Нет описания

class-wc-rest-data-countries-controller.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * REST API Data countries controller.
  4. *
  5. * Handles requests to the /data/countries endpoint.
  6. *
  7. * @package WooCommerce\RestApi
  8. * @since 3.5.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * REST API Data countries controller class.
  13. *
  14. * @package WooCommerce\RestApi
  15. * @extends WC_REST_Controller
  16. */
  17. class WC_REST_Data_Countries_Controller extends WC_REST_Data_Controller {
  18. /**
  19. * Endpoint namespace.
  20. *
  21. * @var string
  22. */
  23. protected $namespace = 'wc/v3';
  24. /**
  25. * Route base.
  26. *
  27. * @var string
  28. */
  29. protected $rest_base = 'data/countries';
  30. /**
  31. * Register routes.
  32. *
  33. * @since 3.5.0
  34. */
  35. public function register_routes() {
  36. register_rest_route(
  37. $this->namespace,
  38. '/' . $this->rest_base,
  39. array(
  40. array(
  41. 'methods' => WP_REST_Server::READABLE,
  42. 'callback' => array( $this, 'get_items' ),
  43. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  44. ),
  45. 'schema' => array( $this, 'get_public_item_schema' ),
  46. )
  47. );
  48. register_rest_route(
  49. $this->namespace,
  50. '/' . $this->rest_base . '/(?P<location>[\w-]+)',
  51. array(
  52. array(
  53. 'methods' => WP_REST_Server::READABLE,
  54. 'callback' => array( $this, 'get_item' ),
  55. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  56. 'args' => array(
  57. 'location' => array(
  58. 'description' => __( 'ISO3166 alpha-2 country code.', 'woocommerce' ),
  59. 'type' => 'string',
  60. ),
  61. ),
  62. ),
  63. 'schema' => array( $this, 'get_public_item_schema' ),
  64. )
  65. );
  66. }
  67. /**
  68. * Get a list of countries and states.
  69. *
  70. * @param string $country_code Country code.
  71. * @param WP_REST_Request $request Request data.
  72. * @return array|mixed Response data, ready for insertion into collection data.
  73. */
  74. public function get_country( $country_code, $request ) {
  75. $countries = WC()->countries->get_countries();
  76. $states = WC()->countries->get_states();
  77. $data = array();
  78. if ( ! array_key_exists( $country_code, $countries ) ) {
  79. return false;
  80. }
  81. $country = array(
  82. 'code' => $country_code,
  83. 'name' => $countries[ $country_code ],
  84. );
  85. $local_states = array();
  86. if ( isset( $states[ $country_code ] ) ) {
  87. foreach ( $states[ $country_code ] as $state_code => $state_name ) {
  88. $local_states[] = array(
  89. 'code' => $state_code,
  90. 'name' => $state_name,
  91. );
  92. }
  93. }
  94. $country['states'] = $local_states;
  95. return $country;
  96. }
  97. /**
  98. * Return the list of states for all countries.
  99. *
  100. * @since 3.5.0
  101. * @param WP_REST_Request $request Request data.
  102. * @return WP_Error|WP_REST_Response
  103. */
  104. public function get_items( $request ) {
  105. $countries = WC()->countries->get_countries();
  106. $data = array();
  107. foreach ( array_keys( $countries ) as $country_code ) {
  108. $country = $this->get_country( $country_code, $request );
  109. $response = $this->prepare_item_for_response( $country, $request );
  110. $data[] = $this->prepare_response_for_collection( $response );
  111. }
  112. return rest_ensure_response( $data );
  113. }
  114. /**
  115. * Return the list of states for a given country.
  116. *
  117. * @since 3.5.0
  118. * @param WP_REST_Request $request Request data.
  119. * @return WP_Error|WP_REST_Response
  120. */
  121. public function get_item( $request ) {
  122. $data = $this->get_country( strtoupper( $request['location'] ), $request );
  123. if ( empty( $data ) ) {
  124. return new WP_Error( 'woocommerce_rest_data_invalid_location', __( 'There are no locations matching these parameters.', 'woocommerce' ), array( 'status' => 404 ) );
  125. }
  126. return $this->prepare_item_for_response( $data, $request );
  127. }
  128. /**
  129. * Prepare the data object for response.
  130. *
  131. * @since 3.5.0
  132. * @param object $item Data object.
  133. * @param WP_REST_Request $request Request object.
  134. * @return WP_REST_Response $response Response data.
  135. */
  136. public function prepare_item_for_response( $item, $request ) {
  137. $data = $this->add_additional_fields_to_object( $item, $request );
  138. $data = $this->filter_response_by_context( $data, 'view' );
  139. $response = rest_ensure_response( $data );
  140. $response->add_links( $this->prepare_links( $item ) );
  141. /**
  142. * Filter the states list for a country returned from the API.
  143. *
  144. * Allows modification of the loction data right before it is returned.
  145. *
  146. * @param WP_REST_Response $response The response object.
  147. * @param array $data The original country's states list.
  148. * @param WP_REST_Request $request Request used to generate the response.
  149. */
  150. return apply_filters( 'woocommerce_rest_prepare_data_country', $response, $item, $request );
  151. }
  152. /**
  153. * Prepare links for the request.
  154. *
  155. * @param object $item Data object.
  156. * @return array Links for the given country.
  157. */
  158. protected function prepare_links( $item ) {
  159. $country_code = strtolower( $item['code'] );
  160. $links = array(
  161. 'self' => array(
  162. 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $country_code ) ),
  163. ),
  164. 'collection' => array(
  165. 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
  166. ),
  167. );
  168. return $links;
  169. }
  170. /**
  171. * Get the location schema, conforming to JSON Schema.
  172. *
  173. * @since 3.5.0
  174. * @return array
  175. */
  176. public function get_item_schema() {
  177. $schema = array(
  178. '$schema' => 'http://json-schema.org/draft-04/schema#',
  179. 'title' => 'data_countries',
  180. 'type' => 'object',
  181. 'properties' => array(
  182. 'code' => array(
  183. 'type' => 'string',
  184. 'description' => __( 'ISO3166 alpha-2 country code.', 'woocommerce' ),
  185. 'context' => array( 'view' ),
  186. 'readonly' => true,
  187. ),
  188. 'name' => array(
  189. 'type' => 'string',
  190. 'description' => __( 'Full name of country.', 'woocommerce' ),
  191. 'context' => array( 'view' ),
  192. 'readonly' => true,
  193. ),
  194. 'states' => array(
  195. 'type' => 'array',
  196. 'description' => __( 'List of states in this country.', 'woocommerce' ),
  197. 'context' => array( 'view' ),
  198. 'readonly' => true,
  199. 'items' => array(
  200. 'type' => 'object',
  201. 'context' => array( 'view' ),
  202. 'readonly' => true,
  203. 'properties' => array(
  204. 'code' => array(
  205. 'type' => 'string',
  206. 'description' => __( 'State code.', 'woocommerce' ),
  207. 'context' => array( 'view' ),
  208. 'readonly' => true,
  209. ),
  210. 'name' => array(
  211. 'type' => 'string',
  212. 'description' => __( 'Full name of state.', 'woocommerce' ),
  213. 'context' => array( 'view' ),
  214. 'readonly' => true,
  215. ),
  216. ),
  217. ),
  218. ),
  219. ),
  220. );
  221. return $this->add_additional_fields_schema( $schema );
  222. }
  223. }