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