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