Geen omschrijving

class-wc-rest-data-currencies-controller.php 6.1KB

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