@@ -8,415 +8,415 @@ |
||
| 8 | 8 | * Products class. |
| 9 | 9 | */ |
| 10 | 10 | class Products extends AbstractRoute { |
| 11 | - /** |
|
| 12 | - * The route identifier. |
|
| 13 | - * |
|
| 14 | - * @var string |
|
| 15 | - */ |
|
| 16 | - const IDENTIFIER = 'products'; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * The routes schema. |
|
| 20 | - * |
|
| 21 | - * @var string |
|
| 22 | - */ |
|
| 23 | - const SCHEMA_TYPE = 'product'; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Get the path of this REST route. |
|
| 27 | - * |
|
| 28 | - * @return string |
|
| 29 | - */ |
|
| 30 | - public function get_path() { |
|
| 31 | - return '/products'; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Get method arguments for this REST route. |
|
| 36 | - * |
|
| 37 | - * @return array An array of endpoints. |
|
| 38 | - */ |
|
| 39 | - public function get_args() { |
|
| 40 | - return [ |
|
| 41 | - [ |
|
| 42 | - 'methods' => \WP_REST_Server::READABLE, |
|
| 43 | - 'callback' => [ $this, 'get_response' ], |
|
| 44 | - 'permission_callback' => '__return_true', |
|
| 45 | - 'args' => $this->get_collection_params(), |
|
| 46 | - ], |
|
| 47 | - 'schema' => [ $this->schema, 'get_public_item_schema' ], |
|
| 48 | - ]; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Get a collection of posts and add the post title filter option to \WP_Query. |
|
| 53 | - * |
|
| 54 | - * @param \WP_REST_Request $request Request object. |
|
| 55 | - * @return \WP_REST_Response |
|
| 56 | - */ |
|
| 57 | - protected function get_route_response( \WP_REST_Request $request ) { |
|
| 58 | - $response = new \WP_REST_Response(); |
|
| 59 | - $product_query = new ProductQuery(); |
|
| 60 | - |
|
| 61 | - // Only get objects during GET requests. |
|
| 62 | - if ( \WP_REST_Server::READABLE === $request->get_method() ) { |
|
| 63 | - $query_results = $product_query->get_objects( $request ); |
|
| 64 | - $response_objects = []; |
|
| 65 | - |
|
| 66 | - foreach ( $query_results['objects'] as $object ) { |
|
| 67 | - $data = rest_ensure_response( $this->schema->get_item_response( $object ) ); |
|
| 68 | - $response_objects[] = $this->prepare_response_for_collection( $data ); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - $response->set_data( $response_objects ); |
|
| 72 | - } else { |
|
| 73 | - $query_results = $product_query->get_results( $request ); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - $response = ( new Pagination() )->add_headers( $response, $request, $query_results['total'], $query_results['pages'] ); |
|
| 77 | - $response->header( 'Last-Modified', $product_query->get_last_modified() ); |
|
| 78 | - |
|
| 79 | - return $response; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Prepare links for the request. |
|
| 84 | - * |
|
| 85 | - * @param \WC_Product $item Product object. |
|
| 86 | - * @param \WP_REST_Request $request Request object. |
|
| 87 | - * @return array |
|
| 88 | - */ |
|
| 89 | - protected function prepare_links( $item, $request ) { |
|
| 90 | - $links = array( |
|
| 91 | - 'self' => array( |
|
| 92 | - 'href' => rest_url( $this->get_namespace() . $this->get_path() . '/' . $item->get_id() ), |
|
| 93 | - ), |
|
| 94 | - 'collection' => array( |
|
| 95 | - 'href' => rest_url( $this->get_namespace() . $this->get_path() ), |
|
| 96 | - ), |
|
| 97 | - ); |
|
| 98 | - |
|
| 99 | - if ( $item->get_parent_id() ) { |
|
| 100 | - $links['up'] = array( |
|
| 101 | - 'href' => rest_url( $this->get_namespace() . $this->get_path() . '/' . $item->get_parent_id() ), |
|
| 102 | - ); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - return $links; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Get the query params for collections of products. |
|
| 110 | - * |
|
| 111 | - * @return array |
|
| 112 | - */ |
|
| 113 | - public function get_collection_params() { |
|
| 114 | - $params = []; |
|
| 115 | - $params['context'] = $this->get_context_param(); |
|
| 116 | - $params['context']['default'] = 'view'; |
|
| 117 | - |
|
| 118 | - $params['page'] = array( |
|
| 119 | - 'description' => __( 'Current page of the collection.', 'woocommerce' ), |
|
| 120 | - 'type' => 'integer', |
|
| 121 | - 'default' => 1, |
|
| 122 | - 'sanitize_callback' => 'absint', |
|
| 123 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 124 | - 'minimum' => 1, |
|
| 125 | - ); |
|
| 126 | - |
|
| 127 | - $params['per_page'] = array( |
|
| 128 | - 'description' => __( 'Maximum number of items to be returned in result set. Defaults to no limit if left blank.', 'woocommerce' ), |
|
| 129 | - 'type' => 'integer', |
|
| 130 | - 'default' => 10, |
|
| 131 | - 'minimum' => 0, |
|
| 132 | - 'maximum' => 100, |
|
| 133 | - 'sanitize_callback' => 'absint', |
|
| 134 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 135 | - ); |
|
| 136 | - |
|
| 137 | - $params['search'] = array( |
|
| 138 | - 'description' => __( 'Limit results to those matching a string.', 'woocommerce' ), |
|
| 139 | - 'type' => 'string', |
|
| 140 | - 'sanitize_callback' => 'sanitize_text_field', |
|
| 141 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 142 | - ); |
|
| 143 | - |
|
| 144 | - $params['after'] = array( |
|
| 145 | - 'description' => __( 'Limit response to resources created after a given ISO8601 compliant date.', 'woocommerce' ), |
|
| 146 | - 'type' => 'string', |
|
| 147 | - 'format' => 'date-time', |
|
| 148 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 149 | - ); |
|
| 150 | - |
|
| 151 | - $params['before'] = array( |
|
| 152 | - 'description' => __( 'Limit response to resources created before a given ISO8601 compliant date.', 'woocommerce' ), |
|
| 153 | - 'type' => 'string', |
|
| 154 | - 'format' => 'date-time', |
|
| 155 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 156 | - ); |
|
| 157 | - |
|
| 158 | - $params['date_column'] = array( |
|
| 159 | - 'description' => __( 'When limiting response using after/before, which date column to compare against.', 'woocommerce' ), |
|
| 160 | - 'type' => 'string', |
|
| 161 | - 'default' => 'date', |
|
| 162 | - 'enum' => array( |
|
| 163 | - 'date', |
|
| 164 | - 'date_gmt', |
|
| 165 | - 'modified', |
|
| 166 | - 'modified_gmt', |
|
| 167 | - ), |
|
| 168 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 169 | - ); |
|
| 170 | - |
|
| 171 | - $params['exclude'] = array( |
|
| 172 | - 'description' => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ), |
|
| 173 | - 'type' => 'array', |
|
| 174 | - 'items' => array( |
|
| 175 | - 'type' => 'integer', |
|
| 176 | - ), |
|
| 177 | - 'default' => [], |
|
| 178 | - 'sanitize_callback' => 'wp_parse_id_list', |
|
| 179 | - ); |
|
| 180 | - |
|
| 181 | - $params['include'] = array( |
|
| 182 | - 'description' => __( 'Limit result set to specific ids.', 'woocommerce' ), |
|
| 183 | - 'type' => 'array', |
|
| 184 | - 'items' => array( |
|
| 185 | - 'type' => 'integer', |
|
| 186 | - ), |
|
| 187 | - 'default' => [], |
|
| 188 | - 'sanitize_callback' => 'wp_parse_id_list', |
|
| 189 | - ); |
|
| 190 | - |
|
| 191 | - $params['offset'] = array( |
|
| 192 | - 'description' => __( 'Offset the result set by a specific number of items.', 'woocommerce' ), |
|
| 193 | - 'type' => 'integer', |
|
| 194 | - 'sanitize_callback' => 'absint', |
|
| 195 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 196 | - ); |
|
| 197 | - |
|
| 198 | - $params['order'] = array( |
|
| 199 | - 'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ), |
|
| 200 | - 'type' => 'string', |
|
| 201 | - 'default' => 'desc', |
|
| 202 | - 'enum' => array( 'asc', 'desc' ), |
|
| 203 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 204 | - ); |
|
| 205 | - |
|
| 206 | - $params['orderby'] = array( |
|
| 207 | - 'description' => __( 'Sort collection by object attribute.', 'woocommerce' ), |
|
| 208 | - 'type' => 'string', |
|
| 209 | - 'default' => 'date', |
|
| 210 | - 'enum' => array( |
|
| 211 | - 'date', |
|
| 212 | - 'modified', |
|
| 213 | - 'id', |
|
| 214 | - 'include', |
|
| 215 | - 'title', |
|
| 216 | - 'slug', |
|
| 217 | - 'price', |
|
| 218 | - 'popularity', |
|
| 219 | - 'rating', |
|
| 220 | - 'menu_order', |
|
| 221 | - 'comment_count', |
|
| 222 | - ), |
|
| 223 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 224 | - ); |
|
| 225 | - |
|
| 226 | - $params['parent'] = array( |
|
| 227 | - 'description' => __( 'Limit result set to those of particular parent IDs.', 'woocommerce' ), |
|
| 228 | - 'type' => 'array', |
|
| 229 | - 'items' => array( |
|
| 230 | - 'type' => 'integer', |
|
| 231 | - ), |
|
| 232 | - 'default' => [], |
|
| 233 | - 'sanitize_callback' => 'wp_parse_id_list', |
|
| 234 | - ); |
|
| 235 | - |
|
| 236 | - $params['parent_exclude'] = array( |
|
| 237 | - 'description' => __( 'Limit result set to all items except those of a particular parent ID.', 'woocommerce' ), |
|
| 238 | - 'type' => 'array', |
|
| 239 | - 'items' => array( |
|
| 240 | - 'type' => 'integer', |
|
| 241 | - ), |
|
| 242 | - 'sanitize_callback' => 'wp_parse_id_list', |
|
| 243 | - 'default' => [], |
|
| 244 | - ); |
|
| 245 | - |
|
| 246 | - $params['type'] = array( |
|
| 247 | - 'description' => __( 'Limit result set to products assigned a specific type.', 'woocommerce' ), |
|
| 248 | - 'type' => 'string', |
|
| 249 | - 'enum' => array_merge( array_keys( wc_get_product_types() ), [ 'variation' ] ), |
|
| 250 | - 'sanitize_callback' => 'sanitize_key', |
|
| 251 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 252 | - ); |
|
| 253 | - |
|
| 254 | - $params['sku'] = array( |
|
| 255 | - 'description' => __( 'Limit result set to products with specific SKU(s). Use commas to separate.', 'woocommerce' ), |
|
| 256 | - 'type' => 'string', |
|
| 257 | - 'sanitize_callback' => 'sanitize_text_field', |
|
| 258 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 259 | - ); |
|
| 260 | - |
|
| 261 | - $params['featured'] = array( |
|
| 262 | - 'description' => __( 'Limit result set to featured products.', 'woocommerce' ), |
|
| 263 | - 'type' => 'boolean', |
|
| 264 | - 'sanitize_callback' => 'wc_string_to_bool', |
|
| 265 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 266 | - ); |
|
| 267 | - |
|
| 268 | - $params['category'] = array( |
|
| 269 | - 'description' => __( 'Limit result set to products assigned a specific category ID.', 'woocommerce' ), |
|
| 270 | - 'type' => 'string', |
|
| 271 | - 'sanitize_callback' => 'wp_parse_id_list', |
|
| 272 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 273 | - ); |
|
| 274 | - |
|
| 275 | - $params['category_operator'] = array( |
|
| 276 | - 'description' => __( 'Operator to compare product category terms.', 'woocommerce' ), |
|
| 277 | - 'type' => 'string', |
|
| 278 | - 'enum' => [ 'in', 'not_in', 'and' ], |
|
| 279 | - 'default' => 'in', |
|
| 280 | - 'sanitize_callback' => 'sanitize_key', |
|
| 281 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 282 | - ); |
|
| 283 | - |
|
| 284 | - // If the $_REQUEST contains a taxonomy query, add it to the params and sanitize it. |
|
| 285 | - foreach ( $_REQUEST as $param => $value ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 286 | - if ( str_starts_with( $param, '_unstable_tax_' ) && ! str_ends_with( $param, '_operator' ) ) { |
|
| 287 | - $params[ $param ] = array( |
|
| 288 | - 'description' => __( 'Limit result set to products assigned a specific category ID.', 'woocommerce' ), |
|
| 289 | - 'type' => 'string', |
|
| 290 | - 'sanitize_callback' => 'wp_parse_id_list', |
|
| 291 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 292 | - ); |
|
| 293 | - } |
|
| 294 | - if ( str_starts_with( $param, '_unstable_tax_' ) && str_ends_with( $param, '_operator' ) ) { |
|
| 295 | - $params[ $param ] = array( |
|
| 296 | - 'description' => __( 'Operator to compare product category terms.', 'woocommerce' ), |
|
| 297 | - 'type' => 'string', |
|
| 298 | - 'enum' => [ 'in', 'not_in', 'and' ], |
|
| 299 | - 'default' => 'in', |
|
| 300 | - 'sanitize_callback' => 'sanitize_key', |
|
| 301 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 302 | - ); |
|
| 303 | - } |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - $params['tag'] = array( |
|
| 307 | - 'description' => __( 'Limit result set to products assigned a specific tag ID.', 'woocommerce' ), |
|
| 308 | - 'type' => 'string', |
|
| 309 | - 'sanitize_callback' => 'wp_parse_id_list', |
|
| 310 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 311 | - ); |
|
| 312 | - |
|
| 313 | - $params['tag_operator'] = array( |
|
| 314 | - 'description' => __( 'Operator to compare product tags.', 'woocommerce' ), |
|
| 315 | - 'type' => 'string', |
|
| 316 | - 'enum' => [ 'in', 'not_in', 'and' ], |
|
| 317 | - 'default' => 'in', |
|
| 318 | - 'sanitize_callback' => 'sanitize_key', |
|
| 319 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 320 | - ); |
|
| 321 | - |
|
| 322 | - $params['on_sale'] = array( |
|
| 323 | - 'description' => __( 'Limit result set to products on sale.', 'woocommerce' ), |
|
| 324 | - 'type' => 'boolean', |
|
| 325 | - 'sanitize_callback' => 'wc_string_to_bool', |
|
| 326 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 327 | - ); |
|
| 328 | - |
|
| 329 | - $params['min_price'] = array( |
|
| 330 | - 'description' => __( 'Limit result set to products based on a minimum price, provided using the smallest unit of the currency.', 'woocommerce' ), |
|
| 331 | - 'type' => 'string', |
|
| 332 | - 'sanitize_callback' => 'sanitize_text_field', |
|
| 333 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 334 | - ); |
|
| 335 | - |
|
| 336 | - $params['max_price'] = array( |
|
| 337 | - 'description' => __( 'Limit result set to products based on a maximum price, provided using the smallest unit of the currency.', 'woocommerce' ), |
|
| 338 | - 'type' => 'string', |
|
| 339 | - 'sanitize_callback' => 'sanitize_text_field', |
|
| 340 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 341 | - ); |
|
| 342 | - |
|
| 343 | - $params['stock_status'] = array( |
|
| 344 | - 'description' => __( 'Limit result set to products with specified stock status.', 'woocommerce' ), |
|
| 345 | - 'type' => 'array', |
|
| 346 | - 'items' => array( |
|
| 347 | - 'type' => 'string', |
|
| 348 | - 'enum' => array_keys( wc_get_product_stock_status_options() ), |
|
| 349 | - 'sanitize_callback' => 'sanitize_text_field', |
|
| 350 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 351 | - ), |
|
| 352 | - 'default' => [], |
|
| 353 | - ); |
|
| 354 | - |
|
| 355 | - $params['attributes'] = array( |
|
| 356 | - 'description' => __( 'Limit result set to products with selected global attributes.', 'woocommerce' ), |
|
| 357 | - 'type' => 'array', |
|
| 358 | - 'items' => array( |
|
| 359 | - 'type' => 'object', |
|
| 360 | - 'properties' => array( |
|
| 361 | - 'attribute' => array( |
|
| 362 | - 'description' => __( 'Attribute taxonomy name.', 'woocommerce' ), |
|
| 363 | - 'type' => 'string', |
|
| 364 | - 'sanitize_callback' => 'wc_sanitize_taxonomy_name', |
|
| 365 | - ), |
|
| 366 | - 'term_id' => array( |
|
| 367 | - 'description' => __( 'List of attribute term IDs.', 'woocommerce' ), |
|
| 368 | - 'type' => 'array', |
|
| 369 | - 'items' => [ |
|
| 370 | - 'type' => 'integer', |
|
| 371 | - ], |
|
| 372 | - 'sanitize_callback' => 'wp_parse_id_list', |
|
| 373 | - ), |
|
| 374 | - 'slug' => array( |
|
| 375 | - 'description' => __( 'List of attribute slug(s). If a term ID is provided, this will be ignored.', 'woocommerce' ), |
|
| 376 | - 'type' => 'array', |
|
| 377 | - 'items' => [ |
|
| 378 | - 'type' => 'string', |
|
| 379 | - ], |
|
| 380 | - 'sanitize_callback' => 'wp_parse_slug_list', |
|
| 381 | - ), |
|
| 382 | - 'operator' => array( |
|
| 383 | - 'description' => __( 'Operator to compare product attribute terms.', 'woocommerce' ), |
|
| 384 | - 'type' => 'string', |
|
| 385 | - 'enum' => [ 'in', 'not_in', 'and' ], |
|
| 386 | - ), |
|
| 387 | - ), |
|
| 388 | - ), |
|
| 389 | - 'default' => [], |
|
| 390 | - ); |
|
| 391 | - |
|
| 392 | - $params['attribute_relation'] = array( |
|
| 393 | - 'description' => __( 'The logical relationship between attributes when filtering across multiple at once.', 'woocommerce' ), |
|
| 394 | - 'type' => 'string', |
|
| 395 | - 'enum' => [ 'in', 'and' ], |
|
| 396 | - 'default' => 'and', |
|
| 397 | - 'sanitize_callback' => 'sanitize_key', |
|
| 398 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 399 | - ); |
|
| 400 | - |
|
| 401 | - $params['catalog_visibility'] = array( |
|
| 402 | - 'description' => __( 'Determines if hidden or visible catalog products are shown.', 'woocommerce' ), |
|
| 403 | - 'type' => 'string', |
|
| 404 | - 'enum' => array( 'any', 'visible', 'catalog', 'search', 'hidden' ), |
|
| 405 | - 'sanitize_callback' => 'sanitize_key', |
|
| 406 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 407 | - ); |
|
| 408 | - |
|
| 409 | - $params['rating'] = array( |
|
| 410 | - 'description' => __( 'Limit result set to products with a certain average rating.', 'woocommerce' ), |
|
| 411 | - 'type' => 'array', |
|
| 412 | - 'items' => array( |
|
| 413 | - 'type' => 'integer', |
|
| 414 | - 'enum' => range( 1, 5 ), |
|
| 415 | - ), |
|
| 416 | - 'default' => [], |
|
| 417 | - 'sanitize_callback' => 'wp_parse_id_list', |
|
| 418 | - ); |
|
| 419 | - |
|
| 420 | - return $params; |
|
| 421 | - } |
|
| 11 | + /** |
|
| 12 | + * The route identifier. |
|
| 13 | + * |
|
| 14 | + * @var string |
|
| 15 | + */ |
|
| 16 | + const IDENTIFIER = 'products'; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * The routes schema. |
|
| 20 | + * |
|
| 21 | + * @var string |
|
| 22 | + */ |
|
| 23 | + const SCHEMA_TYPE = 'product'; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Get the path of this REST route. |
|
| 27 | + * |
|
| 28 | + * @return string |
|
| 29 | + */ |
|
| 30 | + public function get_path() { |
|
| 31 | + return '/products'; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Get method arguments for this REST route. |
|
| 36 | + * |
|
| 37 | + * @return array An array of endpoints. |
|
| 38 | + */ |
|
| 39 | + public function get_args() { |
|
| 40 | + return [ |
|
| 41 | + [ |
|
| 42 | + 'methods' => \WP_REST_Server::READABLE, |
|
| 43 | + 'callback' => [ $this, 'get_response' ], |
|
| 44 | + 'permission_callback' => '__return_true', |
|
| 45 | + 'args' => $this->get_collection_params(), |
|
| 46 | + ], |
|
| 47 | + 'schema' => [ $this->schema, 'get_public_item_schema' ], |
|
| 48 | + ]; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Get a collection of posts and add the post title filter option to \WP_Query. |
|
| 53 | + * |
|
| 54 | + * @param \WP_REST_Request $request Request object. |
|
| 55 | + * @return \WP_REST_Response |
|
| 56 | + */ |
|
| 57 | + protected function get_route_response( \WP_REST_Request $request ) { |
|
| 58 | + $response = new \WP_REST_Response(); |
|
| 59 | + $product_query = new ProductQuery(); |
|
| 60 | + |
|
| 61 | + // Only get objects during GET requests. |
|
| 62 | + if ( \WP_REST_Server::READABLE === $request->get_method() ) { |
|
| 63 | + $query_results = $product_query->get_objects( $request ); |
|
| 64 | + $response_objects = []; |
|
| 65 | + |
|
| 66 | + foreach ( $query_results['objects'] as $object ) { |
|
| 67 | + $data = rest_ensure_response( $this->schema->get_item_response( $object ) ); |
|
| 68 | + $response_objects[] = $this->prepare_response_for_collection( $data ); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + $response->set_data( $response_objects ); |
|
| 72 | + } else { |
|
| 73 | + $query_results = $product_query->get_results( $request ); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + $response = ( new Pagination() )->add_headers( $response, $request, $query_results['total'], $query_results['pages'] ); |
|
| 77 | + $response->header( 'Last-Modified', $product_query->get_last_modified() ); |
|
| 78 | + |
|
| 79 | + return $response; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Prepare links for the request. |
|
| 84 | + * |
|
| 85 | + * @param \WC_Product $item Product object. |
|
| 86 | + * @param \WP_REST_Request $request Request object. |
|
| 87 | + * @return array |
|
| 88 | + */ |
|
| 89 | + protected function prepare_links( $item, $request ) { |
|
| 90 | + $links = array( |
|
| 91 | + 'self' => array( |
|
| 92 | + 'href' => rest_url( $this->get_namespace() . $this->get_path() . '/' . $item->get_id() ), |
|
| 93 | + ), |
|
| 94 | + 'collection' => array( |
|
| 95 | + 'href' => rest_url( $this->get_namespace() . $this->get_path() ), |
|
| 96 | + ), |
|
| 97 | + ); |
|
| 98 | + |
|
| 99 | + if ( $item->get_parent_id() ) { |
|
| 100 | + $links['up'] = array( |
|
| 101 | + 'href' => rest_url( $this->get_namespace() . $this->get_path() . '/' . $item->get_parent_id() ), |
|
| 102 | + ); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + return $links; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Get the query params for collections of products. |
|
| 110 | + * |
|
| 111 | + * @return array |
|
| 112 | + */ |
|
| 113 | + public function get_collection_params() { |
|
| 114 | + $params = []; |
|
| 115 | + $params['context'] = $this->get_context_param(); |
|
| 116 | + $params['context']['default'] = 'view'; |
|
| 117 | + |
|
| 118 | + $params['page'] = array( |
|
| 119 | + 'description' => __( 'Current page of the collection.', 'woocommerce' ), |
|
| 120 | + 'type' => 'integer', |
|
| 121 | + 'default' => 1, |
|
| 122 | + 'sanitize_callback' => 'absint', |
|
| 123 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 124 | + 'minimum' => 1, |
|
| 125 | + ); |
|
| 126 | + |
|
| 127 | + $params['per_page'] = array( |
|
| 128 | + 'description' => __( 'Maximum number of items to be returned in result set. Defaults to no limit if left blank.', 'woocommerce' ), |
|
| 129 | + 'type' => 'integer', |
|
| 130 | + 'default' => 10, |
|
| 131 | + 'minimum' => 0, |
|
| 132 | + 'maximum' => 100, |
|
| 133 | + 'sanitize_callback' => 'absint', |
|
| 134 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 135 | + ); |
|
| 136 | + |
|
| 137 | + $params['search'] = array( |
|
| 138 | + 'description' => __( 'Limit results to those matching a string.', 'woocommerce' ), |
|
| 139 | + 'type' => 'string', |
|
| 140 | + 'sanitize_callback' => 'sanitize_text_field', |
|
| 141 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 142 | + ); |
|
| 143 | + |
|
| 144 | + $params['after'] = array( |
|
| 145 | + 'description' => __( 'Limit response to resources created after a given ISO8601 compliant date.', 'woocommerce' ), |
|
| 146 | + 'type' => 'string', |
|
| 147 | + 'format' => 'date-time', |
|
| 148 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 149 | + ); |
|
| 150 | + |
|
| 151 | + $params['before'] = array( |
|
| 152 | + 'description' => __( 'Limit response to resources created before a given ISO8601 compliant date.', 'woocommerce' ), |
|
| 153 | + 'type' => 'string', |
|
| 154 | + 'format' => 'date-time', |
|
| 155 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 156 | + ); |
|
| 157 | + |
|
| 158 | + $params['date_column'] = array( |
|
| 159 | + 'description' => __( 'When limiting response using after/before, which date column to compare against.', 'woocommerce' ), |
|
| 160 | + 'type' => 'string', |
|
| 161 | + 'default' => 'date', |
|
| 162 | + 'enum' => array( |
|
| 163 | + 'date', |
|
| 164 | + 'date_gmt', |
|
| 165 | + 'modified', |
|
| 166 | + 'modified_gmt', |
|
| 167 | + ), |
|
| 168 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 169 | + ); |
|
| 170 | + |
|
| 171 | + $params['exclude'] = array( |
|
| 172 | + 'description' => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ), |
|
| 173 | + 'type' => 'array', |
|
| 174 | + 'items' => array( |
|
| 175 | + 'type' => 'integer', |
|
| 176 | + ), |
|
| 177 | + 'default' => [], |
|
| 178 | + 'sanitize_callback' => 'wp_parse_id_list', |
|
| 179 | + ); |
|
| 180 | + |
|
| 181 | + $params['include'] = array( |
|
| 182 | + 'description' => __( 'Limit result set to specific ids.', 'woocommerce' ), |
|
| 183 | + 'type' => 'array', |
|
| 184 | + 'items' => array( |
|
| 185 | + 'type' => 'integer', |
|
| 186 | + ), |
|
| 187 | + 'default' => [], |
|
| 188 | + 'sanitize_callback' => 'wp_parse_id_list', |
|
| 189 | + ); |
|
| 190 | + |
|
| 191 | + $params['offset'] = array( |
|
| 192 | + 'description' => __( 'Offset the result set by a specific number of items.', 'woocommerce' ), |
|
| 193 | + 'type' => 'integer', |
|
| 194 | + 'sanitize_callback' => 'absint', |
|
| 195 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 196 | + ); |
|
| 197 | + |
|
| 198 | + $params['order'] = array( |
|
| 199 | + 'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ), |
|
| 200 | + 'type' => 'string', |
|
| 201 | + 'default' => 'desc', |
|
| 202 | + 'enum' => array( 'asc', 'desc' ), |
|
| 203 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 204 | + ); |
|
| 205 | + |
|
| 206 | + $params['orderby'] = array( |
|
| 207 | + 'description' => __( 'Sort collection by object attribute.', 'woocommerce' ), |
|
| 208 | + 'type' => 'string', |
|
| 209 | + 'default' => 'date', |
|
| 210 | + 'enum' => array( |
|
| 211 | + 'date', |
|
| 212 | + 'modified', |
|
| 213 | + 'id', |
|
| 214 | + 'include', |
|
| 215 | + 'title', |
|
| 216 | + 'slug', |
|
| 217 | + 'price', |
|
| 218 | + 'popularity', |
|
| 219 | + 'rating', |
|
| 220 | + 'menu_order', |
|
| 221 | + 'comment_count', |
|
| 222 | + ), |
|
| 223 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 224 | + ); |
|
| 225 | + |
|
| 226 | + $params['parent'] = array( |
|
| 227 | + 'description' => __( 'Limit result set to those of particular parent IDs.', 'woocommerce' ), |
|
| 228 | + 'type' => 'array', |
|
| 229 | + 'items' => array( |
|
| 230 | + 'type' => 'integer', |
|
| 231 | + ), |
|
| 232 | + 'default' => [], |
|
| 233 | + 'sanitize_callback' => 'wp_parse_id_list', |
|
| 234 | + ); |
|
| 235 | + |
|
| 236 | + $params['parent_exclude'] = array( |
|
| 237 | + 'description' => __( 'Limit result set to all items except those of a particular parent ID.', 'woocommerce' ), |
|
| 238 | + 'type' => 'array', |
|
| 239 | + 'items' => array( |
|
| 240 | + 'type' => 'integer', |
|
| 241 | + ), |
|
| 242 | + 'sanitize_callback' => 'wp_parse_id_list', |
|
| 243 | + 'default' => [], |
|
| 244 | + ); |
|
| 245 | + |
|
| 246 | + $params['type'] = array( |
|
| 247 | + 'description' => __( 'Limit result set to products assigned a specific type.', 'woocommerce' ), |
|
| 248 | + 'type' => 'string', |
|
| 249 | + 'enum' => array_merge( array_keys( wc_get_product_types() ), [ 'variation' ] ), |
|
| 250 | + 'sanitize_callback' => 'sanitize_key', |
|
| 251 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 252 | + ); |
|
| 253 | + |
|
| 254 | + $params['sku'] = array( |
|
| 255 | + 'description' => __( 'Limit result set to products with specific SKU(s). Use commas to separate.', 'woocommerce' ), |
|
| 256 | + 'type' => 'string', |
|
| 257 | + 'sanitize_callback' => 'sanitize_text_field', |
|
| 258 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 259 | + ); |
|
| 260 | + |
|
| 261 | + $params['featured'] = array( |
|
| 262 | + 'description' => __( 'Limit result set to featured products.', 'woocommerce' ), |
|
| 263 | + 'type' => 'boolean', |
|
| 264 | + 'sanitize_callback' => 'wc_string_to_bool', |
|
| 265 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 266 | + ); |
|
| 267 | + |
|
| 268 | + $params['category'] = array( |
|
| 269 | + 'description' => __( 'Limit result set to products assigned a specific category ID.', 'woocommerce' ), |
|
| 270 | + 'type' => 'string', |
|
| 271 | + 'sanitize_callback' => 'wp_parse_id_list', |
|
| 272 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 273 | + ); |
|
| 274 | + |
|
| 275 | + $params['category_operator'] = array( |
|
| 276 | + 'description' => __( 'Operator to compare product category terms.', 'woocommerce' ), |
|
| 277 | + 'type' => 'string', |
|
| 278 | + 'enum' => [ 'in', 'not_in', 'and' ], |
|
| 279 | + 'default' => 'in', |
|
| 280 | + 'sanitize_callback' => 'sanitize_key', |
|
| 281 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 282 | + ); |
|
| 283 | + |
|
| 284 | + // If the $_REQUEST contains a taxonomy query, add it to the params and sanitize it. |
|
| 285 | + foreach ( $_REQUEST as $param => $value ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 286 | + if ( str_starts_with( $param, '_unstable_tax_' ) && ! str_ends_with( $param, '_operator' ) ) { |
|
| 287 | + $params[ $param ] = array( |
|
| 288 | + 'description' => __( 'Limit result set to products assigned a specific category ID.', 'woocommerce' ), |
|
| 289 | + 'type' => 'string', |
|
| 290 | + 'sanitize_callback' => 'wp_parse_id_list', |
|
| 291 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 292 | + ); |
|
| 293 | + } |
|
| 294 | + if ( str_starts_with( $param, '_unstable_tax_' ) && str_ends_with( $param, '_operator' ) ) { |
|
| 295 | + $params[ $param ] = array( |
|
| 296 | + 'description' => __( 'Operator to compare product category terms.', 'woocommerce' ), |
|
| 297 | + 'type' => 'string', |
|
| 298 | + 'enum' => [ 'in', 'not_in', 'and' ], |
|
| 299 | + 'default' => 'in', |
|
| 300 | + 'sanitize_callback' => 'sanitize_key', |
|
| 301 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 302 | + ); |
|
| 303 | + } |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + $params['tag'] = array( |
|
| 307 | + 'description' => __( 'Limit result set to products assigned a specific tag ID.', 'woocommerce' ), |
|
| 308 | + 'type' => 'string', |
|
| 309 | + 'sanitize_callback' => 'wp_parse_id_list', |
|
| 310 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 311 | + ); |
|
| 312 | + |
|
| 313 | + $params['tag_operator'] = array( |
|
| 314 | + 'description' => __( 'Operator to compare product tags.', 'woocommerce' ), |
|
| 315 | + 'type' => 'string', |
|
| 316 | + 'enum' => [ 'in', 'not_in', 'and' ], |
|
| 317 | + 'default' => 'in', |
|
| 318 | + 'sanitize_callback' => 'sanitize_key', |
|
| 319 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 320 | + ); |
|
| 321 | + |
|
| 322 | + $params['on_sale'] = array( |
|
| 323 | + 'description' => __( 'Limit result set to products on sale.', 'woocommerce' ), |
|
| 324 | + 'type' => 'boolean', |
|
| 325 | + 'sanitize_callback' => 'wc_string_to_bool', |
|
| 326 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 327 | + ); |
|
| 328 | + |
|
| 329 | + $params['min_price'] = array( |
|
| 330 | + 'description' => __( 'Limit result set to products based on a minimum price, provided using the smallest unit of the currency.', 'woocommerce' ), |
|
| 331 | + 'type' => 'string', |
|
| 332 | + 'sanitize_callback' => 'sanitize_text_field', |
|
| 333 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 334 | + ); |
|
| 335 | + |
|
| 336 | + $params['max_price'] = array( |
|
| 337 | + 'description' => __( 'Limit result set to products based on a maximum price, provided using the smallest unit of the currency.', 'woocommerce' ), |
|
| 338 | + 'type' => 'string', |
|
| 339 | + 'sanitize_callback' => 'sanitize_text_field', |
|
| 340 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 341 | + ); |
|
| 342 | + |
|
| 343 | + $params['stock_status'] = array( |
|
| 344 | + 'description' => __( 'Limit result set to products with specified stock status.', 'woocommerce' ), |
|
| 345 | + 'type' => 'array', |
|
| 346 | + 'items' => array( |
|
| 347 | + 'type' => 'string', |
|
| 348 | + 'enum' => array_keys( wc_get_product_stock_status_options() ), |
|
| 349 | + 'sanitize_callback' => 'sanitize_text_field', |
|
| 350 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 351 | + ), |
|
| 352 | + 'default' => [], |
|
| 353 | + ); |
|
| 354 | + |
|
| 355 | + $params['attributes'] = array( |
|
| 356 | + 'description' => __( 'Limit result set to products with selected global attributes.', 'woocommerce' ), |
|
| 357 | + 'type' => 'array', |
|
| 358 | + 'items' => array( |
|
| 359 | + 'type' => 'object', |
|
| 360 | + 'properties' => array( |
|
| 361 | + 'attribute' => array( |
|
| 362 | + 'description' => __( 'Attribute taxonomy name.', 'woocommerce' ), |
|
| 363 | + 'type' => 'string', |
|
| 364 | + 'sanitize_callback' => 'wc_sanitize_taxonomy_name', |
|
| 365 | + ), |
|
| 366 | + 'term_id' => array( |
|
| 367 | + 'description' => __( 'List of attribute term IDs.', 'woocommerce' ), |
|
| 368 | + 'type' => 'array', |
|
| 369 | + 'items' => [ |
|
| 370 | + 'type' => 'integer', |
|
| 371 | + ], |
|
| 372 | + 'sanitize_callback' => 'wp_parse_id_list', |
|
| 373 | + ), |
|
| 374 | + 'slug' => array( |
|
| 375 | + 'description' => __( 'List of attribute slug(s). If a term ID is provided, this will be ignored.', 'woocommerce' ), |
|
| 376 | + 'type' => 'array', |
|
| 377 | + 'items' => [ |
|
| 378 | + 'type' => 'string', |
|
| 379 | + ], |
|
| 380 | + 'sanitize_callback' => 'wp_parse_slug_list', |
|
| 381 | + ), |
|
| 382 | + 'operator' => array( |
|
| 383 | + 'description' => __( 'Operator to compare product attribute terms.', 'woocommerce' ), |
|
| 384 | + 'type' => 'string', |
|
| 385 | + 'enum' => [ 'in', 'not_in', 'and' ], |
|
| 386 | + ), |
|
| 387 | + ), |
|
| 388 | + ), |
|
| 389 | + 'default' => [], |
|
| 390 | + ); |
|
| 391 | + |
|
| 392 | + $params['attribute_relation'] = array( |
|
| 393 | + 'description' => __( 'The logical relationship between attributes when filtering across multiple at once.', 'woocommerce' ), |
|
| 394 | + 'type' => 'string', |
|
| 395 | + 'enum' => [ 'in', 'and' ], |
|
| 396 | + 'default' => 'and', |
|
| 397 | + 'sanitize_callback' => 'sanitize_key', |
|
| 398 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 399 | + ); |
|
| 400 | + |
|
| 401 | + $params['catalog_visibility'] = array( |
|
| 402 | + 'description' => __( 'Determines if hidden or visible catalog products are shown.', 'woocommerce' ), |
|
| 403 | + 'type' => 'string', |
|
| 404 | + 'enum' => array( 'any', 'visible', 'catalog', 'search', 'hidden' ), |
|
| 405 | + 'sanitize_callback' => 'sanitize_key', |
|
| 406 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 407 | + ); |
|
| 408 | + |
|
| 409 | + $params['rating'] = array( |
|
| 410 | + 'description' => __( 'Limit result set to products with a certain average rating.', 'woocommerce' ), |
|
| 411 | + 'type' => 'array', |
|
| 412 | + 'items' => array( |
|
| 413 | + 'type' => 'integer', |
|
| 414 | + 'enum' => range( 1, 5 ), |
|
| 415 | + ), |
|
| 416 | + 'default' => [], |
|
| 417 | + 'sanitize_callback' => 'wp_parse_id_list', |
|
| 418 | + ); |
|
| 419 | + |
|
| 420 | + return $params; |
|
| 421 | + } |
|
| 422 | 422 | } |
@@ -40,11 +40,11 @@ discard block |
||
| 40 | 40 | return [ |
| 41 | 41 | [ |
| 42 | 42 | 'methods' => \WP_REST_Server::READABLE, |
| 43 | - 'callback' => [ $this, 'get_response' ], |
|
| 43 | + 'callback' => [$this, 'get_response'], |
|
| 44 | 44 | 'permission_callback' => '__return_true', |
| 45 | 45 | 'args' => $this->get_collection_params(), |
| 46 | 46 | ], |
| 47 | - 'schema' => [ $this->schema, 'get_public_item_schema' ], |
|
| 47 | + 'schema' => [$this->schema, 'get_public_item_schema'], |
|
| 48 | 48 | ]; |
| 49 | 49 | } |
| 50 | 50 | |
@@ -54,27 +54,27 @@ discard block |
||
| 54 | 54 | * @param \WP_REST_Request $request Request object. |
| 55 | 55 | * @return \WP_REST_Response |
| 56 | 56 | */ |
| 57 | - protected function get_route_response( \WP_REST_Request $request ) { |
|
| 57 | + protected function get_route_response(\WP_REST_Request $request) { |
|
| 58 | 58 | $response = new \WP_REST_Response(); |
| 59 | 59 | $product_query = new ProductQuery(); |
| 60 | 60 | |
| 61 | 61 | // Only get objects during GET requests. |
| 62 | - if ( \WP_REST_Server::READABLE === $request->get_method() ) { |
|
| 63 | - $query_results = $product_query->get_objects( $request ); |
|
| 62 | + if (\WP_REST_Server::READABLE === $request->get_method()) { |
|
| 63 | + $query_results = $product_query->get_objects($request); |
|
| 64 | 64 | $response_objects = []; |
| 65 | 65 | |
| 66 | - foreach ( $query_results['objects'] as $object ) { |
|
| 67 | - $data = rest_ensure_response( $this->schema->get_item_response( $object ) ); |
|
| 68 | - $response_objects[] = $this->prepare_response_for_collection( $data ); |
|
| 66 | + foreach ($query_results['objects'] as $object) { |
|
| 67 | + $data = rest_ensure_response($this->schema->get_item_response($object)); |
|
| 68 | + $response_objects[] = $this->prepare_response_for_collection($data); |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - $response->set_data( $response_objects ); |
|
| 71 | + $response->set_data($response_objects); |
|
| 72 | 72 | } else { |
| 73 | - $query_results = $product_query->get_results( $request ); |
|
| 73 | + $query_results = $product_query->get_results($request); |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | - $response = ( new Pagination() )->add_headers( $response, $request, $query_results['total'], $query_results['pages'] ); |
|
| 77 | - $response->header( 'Last-Modified', $product_query->get_last_modified() ); |
|
| 76 | + $response = (new Pagination())->add_headers($response, $request, $query_results['total'], $query_results['pages']); |
|
| 77 | + $response->header('Last-Modified', $product_query->get_last_modified()); |
|
| 78 | 78 | |
| 79 | 79 | return $response; |
| 80 | 80 | } |
@@ -86,19 +86,19 @@ discard block |
||
| 86 | 86 | * @param \WP_REST_Request $request Request object. |
| 87 | 87 | * @return array |
| 88 | 88 | */ |
| 89 | - protected function prepare_links( $item, $request ) { |
|
| 89 | + protected function prepare_links($item, $request) { |
|
| 90 | 90 | $links = array( |
| 91 | 91 | 'self' => array( |
| 92 | - 'href' => rest_url( $this->get_namespace() . $this->get_path() . '/' . $item->get_id() ), |
|
| 92 | + 'href' => rest_url($this->get_namespace() . $this->get_path() . '/' . $item->get_id()), |
|
| 93 | 93 | ), |
| 94 | 94 | 'collection' => array( |
| 95 | - 'href' => rest_url( $this->get_namespace() . $this->get_path() ), |
|
| 95 | + 'href' => rest_url($this->get_namespace() . $this->get_path()), |
|
| 96 | 96 | ), |
| 97 | 97 | ); |
| 98 | 98 | |
| 99 | - if ( $item->get_parent_id() ) { |
|
| 99 | + if ($item->get_parent_id()) { |
|
| 100 | 100 | $links['up'] = array( |
| 101 | - 'href' => rest_url( $this->get_namespace() . $this->get_path() . '/' . $item->get_parent_id() ), |
|
| 101 | + 'href' => rest_url($this->get_namespace() . $this->get_path() . '/' . $item->get_parent_id()), |
|
| 102 | 102 | ); |
| 103 | 103 | } |
| 104 | 104 | |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | $params['context']['default'] = 'view'; |
| 117 | 117 | |
| 118 | 118 | $params['page'] = array( |
| 119 | - 'description' => __( 'Current page of the collection.', 'woocommerce' ), |
|
| 119 | + 'description' => __('Current page of the collection.', 'woocommerce'), |
|
| 120 | 120 | 'type' => 'integer', |
| 121 | 121 | 'default' => 1, |
| 122 | 122 | 'sanitize_callback' => 'absint', |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | ); |
| 126 | 126 | |
| 127 | 127 | $params['per_page'] = array( |
| 128 | - 'description' => __( 'Maximum number of items to be returned in result set. Defaults to no limit if left blank.', 'woocommerce' ), |
|
| 128 | + 'description' => __('Maximum number of items to be returned in result set. Defaults to no limit if left blank.', 'woocommerce'), |
|
| 129 | 129 | 'type' => 'integer', |
| 130 | 130 | 'default' => 10, |
| 131 | 131 | 'minimum' => 0, |
@@ -135,28 +135,28 @@ discard block |
||
| 135 | 135 | ); |
| 136 | 136 | |
| 137 | 137 | $params['search'] = array( |
| 138 | - 'description' => __( 'Limit results to those matching a string.', 'woocommerce' ), |
|
| 138 | + 'description' => __('Limit results to those matching a string.', 'woocommerce'), |
|
| 139 | 139 | 'type' => 'string', |
| 140 | 140 | 'sanitize_callback' => 'sanitize_text_field', |
| 141 | 141 | 'validate_callback' => 'rest_validate_request_arg', |
| 142 | 142 | ); |
| 143 | 143 | |
| 144 | 144 | $params['after'] = array( |
| 145 | - 'description' => __( 'Limit response to resources created after a given ISO8601 compliant date.', 'woocommerce' ), |
|
| 145 | + 'description' => __('Limit response to resources created after a given ISO8601 compliant date.', 'woocommerce'), |
|
| 146 | 146 | 'type' => 'string', |
| 147 | 147 | 'format' => 'date-time', |
| 148 | 148 | 'validate_callback' => 'rest_validate_request_arg', |
| 149 | 149 | ); |
| 150 | 150 | |
| 151 | 151 | $params['before'] = array( |
| 152 | - 'description' => __( 'Limit response to resources created before a given ISO8601 compliant date.', 'woocommerce' ), |
|
| 152 | + 'description' => __('Limit response to resources created before a given ISO8601 compliant date.', 'woocommerce'), |
|
| 153 | 153 | 'type' => 'string', |
| 154 | 154 | 'format' => 'date-time', |
| 155 | 155 | 'validate_callback' => 'rest_validate_request_arg', |
| 156 | 156 | ); |
| 157 | 157 | |
| 158 | 158 | $params['date_column'] = array( |
| 159 | - 'description' => __( 'When limiting response using after/before, which date column to compare against.', 'woocommerce' ), |
|
| 159 | + 'description' => __('When limiting response using after/before, which date column to compare against.', 'woocommerce'), |
|
| 160 | 160 | 'type' => 'string', |
| 161 | 161 | 'default' => 'date', |
| 162 | 162 | 'enum' => array( |
@@ -169,7 +169,7 @@ discard block |
||
| 169 | 169 | ); |
| 170 | 170 | |
| 171 | 171 | $params['exclude'] = array( |
| 172 | - 'description' => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ), |
|
| 172 | + 'description' => __('Ensure result set excludes specific IDs.', 'woocommerce'), |
|
| 173 | 173 | 'type' => 'array', |
| 174 | 174 | 'items' => array( |
| 175 | 175 | 'type' => 'integer', |
@@ -179,7 +179,7 @@ discard block |
||
| 179 | 179 | ); |
| 180 | 180 | |
| 181 | 181 | $params['include'] = array( |
| 182 | - 'description' => __( 'Limit result set to specific ids.', 'woocommerce' ), |
|
| 182 | + 'description' => __('Limit result set to specific ids.', 'woocommerce'), |
|
| 183 | 183 | 'type' => 'array', |
| 184 | 184 | 'items' => array( |
| 185 | 185 | 'type' => 'integer', |
@@ -189,22 +189,22 @@ discard block |
||
| 189 | 189 | ); |
| 190 | 190 | |
| 191 | 191 | $params['offset'] = array( |
| 192 | - 'description' => __( 'Offset the result set by a specific number of items.', 'woocommerce' ), |
|
| 192 | + 'description' => __('Offset the result set by a specific number of items.', 'woocommerce'), |
|
| 193 | 193 | 'type' => 'integer', |
| 194 | 194 | 'sanitize_callback' => 'absint', |
| 195 | 195 | 'validate_callback' => 'rest_validate_request_arg', |
| 196 | 196 | ); |
| 197 | 197 | |
| 198 | 198 | $params['order'] = array( |
| 199 | - 'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ), |
|
| 199 | + 'description' => __('Order sort attribute ascending or descending.', 'woocommerce'), |
|
| 200 | 200 | 'type' => 'string', |
| 201 | 201 | 'default' => 'desc', |
| 202 | - 'enum' => array( 'asc', 'desc' ), |
|
| 202 | + 'enum' => array('asc', 'desc'), |
|
| 203 | 203 | 'validate_callback' => 'rest_validate_request_arg', |
| 204 | 204 | ); |
| 205 | 205 | |
| 206 | 206 | $params['orderby'] = array( |
| 207 | - 'description' => __( 'Sort collection by object attribute.', 'woocommerce' ), |
|
| 207 | + 'description' => __('Sort collection by object attribute.', 'woocommerce'), |
|
| 208 | 208 | 'type' => 'string', |
| 209 | 209 | 'default' => 'date', |
| 210 | 210 | 'enum' => array( |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | ); |
| 225 | 225 | |
| 226 | 226 | $params['parent'] = array( |
| 227 | - 'description' => __( 'Limit result set to those of particular parent IDs.', 'woocommerce' ), |
|
| 227 | + 'description' => __('Limit result set to those of particular parent IDs.', 'woocommerce'), |
|
| 228 | 228 | 'type' => 'array', |
| 229 | 229 | 'items' => array( |
| 230 | 230 | 'type' => 'integer', |
@@ -234,7 +234,7 @@ discard block |
||
| 234 | 234 | ); |
| 235 | 235 | |
| 236 | 236 | $params['parent_exclude'] = array( |
| 237 | - 'description' => __( 'Limit result set to all items except those of a particular parent ID.', 'woocommerce' ), |
|
| 237 | + 'description' => __('Limit result set to all items except those of a particular parent ID.', 'woocommerce'), |
|
| 238 | 238 | 'type' => 'array', |
| 239 | 239 | 'items' => array( |
| 240 | 240 | 'type' => 'integer', |
@@ -244,58 +244,58 @@ discard block |
||
| 244 | 244 | ); |
| 245 | 245 | |
| 246 | 246 | $params['type'] = array( |
| 247 | - 'description' => __( 'Limit result set to products assigned a specific type.', 'woocommerce' ), |
|
| 247 | + 'description' => __('Limit result set to products assigned a specific type.', 'woocommerce'), |
|
| 248 | 248 | 'type' => 'string', |
| 249 | - 'enum' => array_merge( array_keys( wc_get_product_types() ), [ 'variation' ] ), |
|
| 249 | + 'enum' => array_merge(array_keys(wc_get_product_types()), ['variation']), |
|
| 250 | 250 | 'sanitize_callback' => 'sanitize_key', |
| 251 | 251 | 'validate_callback' => 'rest_validate_request_arg', |
| 252 | 252 | ); |
| 253 | 253 | |
| 254 | 254 | $params['sku'] = array( |
| 255 | - 'description' => __( 'Limit result set to products with specific SKU(s). Use commas to separate.', 'woocommerce' ), |
|
| 255 | + 'description' => __('Limit result set to products with specific SKU(s). Use commas to separate.', 'woocommerce'), |
|
| 256 | 256 | 'type' => 'string', |
| 257 | 257 | 'sanitize_callback' => 'sanitize_text_field', |
| 258 | 258 | 'validate_callback' => 'rest_validate_request_arg', |
| 259 | 259 | ); |
| 260 | 260 | |
| 261 | 261 | $params['featured'] = array( |
| 262 | - 'description' => __( 'Limit result set to featured products.', 'woocommerce' ), |
|
| 262 | + 'description' => __('Limit result set to featured products.', 'woocommerce'), |
|
| 263 | 263 | 'type' => 'boolean', |
| 264 | 264 | 'sanitize_callback' => 'wc_string_to_bool', |
| 265 | 265 | 'validate_callback' => 'rest_validate_request_arg', |
| 266 | 266 | ); |
| 267 | 267 | |
| 268 | 268 | $params['category'] = array( |
| 269 | - 'description' => __( 'Limit result set to products assigned a specific category ID.', 'woocommerce' ), |
|
| 269 | + 'description' => __('Limit result set to products assigned a specific category ID.', 'woocommerce'), |
|
| 270 | 270 | 'type' => 'string', |
| 271 | 271 | 'sanitize_callback' => 'wp_parse_id_list', |
| 272 | 272 | 'validate_callback' => 'rest_validate_request_arg', |
| 273 | 273 | ); |
| 274 | 274 | |
| 275 | 275 | $params['category_operator'] = array( |
| 276 | - 'description' => __( 'Operator to compare product category terms.', 'woocommerce' ), |
|
| 276 | + 'description' => __('Operator to compare product category terms.', 'woocommerce'), |
|
| 277 | 277 | 'type' => 'string', |
| 278 | - 'enum' => [ 'in', 'not_in', 'and' ], |
|
| 278 | + 'enum' => ['in', 'not_in', 'and'], |
|
| 279 | 279 | 'default' => 'in', |
| 280 | 280 | 'sanitize_callback' => 'sanitize_key', |
| 281 | 281 | 'validate_callback' => 'rest_validate_request_arg', |
| 282 | 282 | ); |
| 283 | 283 | |
| 284 | 284 | // If the $_REQUEST contains a taxonomy query, add it to the params and sanitize it. |
| 285 | - foreach ( $_REQUEST as $param => $value ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 286 | - if ( str_starts_with( $param, '_unstable_tax_' ) && ! str_ends_with( $param, '_operator' ) ) { |
|
| 287 | - $params[ $param ] = array( |
|
| 288 | - 'description' => __( 'Limit result set to products assigned a specific category ID.', 'woocommerce' ), |
|
| 285 | + foreach ($_REQUEST as $param => $value) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 286 | + if (str_starts_with($param, '_unstable_tax_') && !str_ends_with($param, '_operator')) { |
|
| 287 | + $params[$param] = array( |
|
| 288 | + 'description' => __('Limit result set to products assigned a specific category ID.', 'woocommerce'), |
|
| 289 | 289 | 'type' => 'string', |
| 290 | 290 | 'sanitize_callback' => 'wp_parse_id_list', |
| 291 | 291 | 'validate_callback' => 'rest_validate_request_arg', |
| 292 | 292 | ); |
| 293 | 293 | } |
| 294 | - if ( str_starts_with( $param, '_unstable_tax_' ) && str_ends_with( $param, '_operator' ) ) { |
|
| 295 | - $params[ $param ] = array( |
|
| 296 | - 'description' => __( 'Operator to compare product category terms.', 'woocommerce' ), |
|
| 294 | + if (str_starts_with($param, '_unstable_tax_') && str_ends_with($param, '_operator')) { |
|
| 295 | + $params[$param] = array( |
|
| 296 | + 'description' => __('Operator to compare product category terms.', 'woocommerce'), |
|
| 297 | 297 | 'type' => 'string', |
| 298 | - 'enum' => [ 'in', 'not_in', 'and' ], |
|
| 298 | + 'enum' => ['in', 'not_in', 'and'], |
|
| 299 | 299 | 'default' => 'in', |
| 300 | 300 | 'sanitize_callback' => 'sanitize_key', |
| 301 | 301 | 'validate_callback' => 'rest_validate_request_arg', |
@@ -304,48 +304,48 @@ discard block |
||
| 304 | 304 | } |
| 305 | 305 | |
| 306 | 306 | $params['tag'] = array( |
| 307 | - 'description' => __( 'Limit result set to products assigned a specific tag ID.', 'woocommerce' ), |
|
| 307 | + 'description' => __('Limit result set to products assigned a specific tag ID.', 'woocommerce'), |
|
| 308 | 308 | 'type' => 'string', |
| 309 | 309 | 'sanitize_callback' => 'wp_parse_id_list', |
| 310 | 310 | 'validate_callback' => 'rest_validate_request_arg', |
| 311 | 311 | ); |
| 312 | 312 | |
| 313 | 313 | $params['tag_operator'] = array( |
| 314 | - 'description' => __( 'Operator to compare product tags.', 'woocommerce' ), |
|
| 314 | + 'description' => __('Operator to compare product tags.', 'woocommerce'), |
|
| 315 | 315 | 'type' => 'string', |
| 316 | - 'enum' => [ 'in', 'not_in', 'and' ], |
|
| 316 | + 'enum' => ['in', 'not_in', 'and'], |
|
| 317 | 317 | 'default' => 'in', |
| 318 | 318 | 'sanitize_callback' => 'sanitize_key', |
| 319 | 319 | 'validate_callback' => 'rest_validate_request_arg', |
| 320 | 320 | ); |
| 321 | 321 | |
| 322 | 322 | $params['on_sale'] = array( |
| 323 | - 'description' => __( 'Limit result set to products on sale.', 'woocommerce' ), |
|
| 323 | + 'description' => __('Limit result set to products on sale.', 'woocommerce'), |
|
| 324 | 324 | 'type' => 'boolean', |
| 325 | 325 | 'sanitize_callback' => 'wc_string_to_bool', |
| 326 | 326 | 'validate_callback' => 'rest_validate_request_arg', |
| 327 | 327 | ); |
| 328 | 328 | |
| 329 | 329 | $params['min_price'] = array( |
| 330 | - 'description' => __( 'Limit result set to products based on a minimum price, provided using the smallest unit of the currency.', 'woocommerce' ), |
|
| 330 | + 'description' => __('Limit result set to products based on a minimum price, provided using the smallest unit of the currency.', 'woocommerce'), |
|
| 331 | 331 | 'type' => 'string', |
| 332 | 332 | 'sanitize_callback' => 'sanitize_text_field', |
| 333 | 333 | 'validate_callback' => 'rest_validate_request_arg', |
| 334 | 334 | ); |
| 335 | 335 | |
| 336 | 336 | $params['max_price'] = array( |
| 337 | - 'description' => __( 'Limit result set to products based on a maximum price, provided using the smallest unit of the currency.', 'woocommerce' ), |
|
| 337 | + 'description' => __('Limit result set to products based on a maximum price, provided using the smallest unit of the currency.', 'woocommerce'), |
|
| 338 | 338 | 'type' => 'string', |
| 339 | 339 | 'sanitize_callback' => 'sanitize_text_field', |
| 340 | 340 | 'validate_callback' => 'rest_validate_request_arg', |
| 341 | 341 | ); |
| 342 | 342 | |
| 343 | 343 | $params['stock_status'] = array( |
| 344 | - 'description' => __( 'Limit result set to products with specified stock status.', 'woocommerce' ), |
|
| 344 | + 'description' => __('Limit result set to products with specified stock status.', 'woocommerce'), |
|
| 345 | 345 | 'type' => 'array', |
| 346 | 346 | 'items' => array( |
| 347 | 347 | 'type' => 'string', |
| 348 | - 'enum' => array_keys( wc_get_product_stock_status_options() ), |
|
| 348 | + 'enum' => array_keys(wc_get_product_stock_status_options()), |
|
| 349 | 349 | 'sanitize_callback' => 'sanitize_text_field', |
| 350 | 350 | 'validate_callback' => 'rest_validate_request_arg', |
| 351 | 351 | ), |
@@ -353,18 +353,18 @@ discard block |
||
| 353 | 353 | ); |
| 354 | 354 | |
| 355 | 355 | $params['attributes'] = array( |
| 356 | - 'description' => __( 'Limit result set to products with selected global attributes.', 'woocommerce' ), |
|
| 356 | + 'description' => __('Limit result set to products with selected global attributes.', 'woocommerce'), |
|
| 357 | 357 | 'type' => 'array', |
| 358 | 358 | 'items' => array( |
| 359 | 359 | 'type' => 'object', |
| 360 | 360 | 'properties' => array( |
| 361 | 361 | 'attribute' => array( |
| 362 | - 'description' => __( 'Attribute taxonomy name.', 'woocommerce' ), |
|
| 362 | + 'description' => __('Attribute taxonomy name.', 'woocommerce'), |
|
| 363 | 363 | 'type' => 'string', |
| 364 | 364 | 'sanitize_callback' => 'wc_sanitize_taxonomy_name', |
| 365 | 365 | ), |
| 366 | 366 | 'term_id' => array( |
| 367 | - 'description' => __( 'List of attribute term IDs.', 'woocommerce' ), |
|
| 367 | + 'description' => __('List of attribute term IDs.', 'woocommerce'), |
|
| 368 | 368 | 'type' => 'array', |
| 369 | 369 | 'items' => [ |
| 370 | 370 | 'type' => 'integer', |
@@ -372,7 +372,7 @@ discard block |
||
| 372 | 372 | 'sanitize_callback' => 'wp_parse_id_list', |
| 373 | 373 | ), |
| 374 | 374 | 'slug' => array( |
| 375 | - 'description' => __( 'List of attribute slug(s). If a term ID is provided, this will be ignored.', 'woocommerce' ), |
|
| 375 | + 'description' => __('List of attribute slug(s). If a term ID is provided, this will be ignored.', 'woocommerce'), |
|
| 376 | 376 | 'type' => 'array', |
| 377 | 377 | 'items' => [ |
| 378 | 378 | 'type' => 'string', |
@@ -380,9 +380,9 @@ discard block |
||
| 380 | 380 | 'sanitize_callback' => 'wp_parse_slug_list', |
| 381 | 381 | ), |
| 382 | 382 | 'operator' => array( |
| 383 | - 'description' => __( 'Operator to compare product attribute terms.', 'woocommerce' ), |
|
| 383 | + 'description' => __('Operator to compare product attribute terms.', 'woocommerce'), |
|
| 384 | 384 | 'type' => 'string', |
| 385 | - 'enum' => [ 'in', 'not_in', 'and' ], |
|
| 385 | + 'enum' => ['in', 'not_in', 'and'], |
|
| 386 | 386 | ), |
| 387 | 387 | ), |
| 388 | 388 | ), |
@@ -390,28 +390,28 @@ discard block |
||
| 390 | 390 | ); |
| 391 | 391 | |
| 392 | 392 | $params['attribute_relation'] = array( |
| 393 | - 'description' => __( 'The logical relationship between attributes when filtering across multiple at once.', 'woocommerce' ), |
|
| 393 | + 'description' => __('The logical relationship between attributes when filtering across multiple at once.', 'woocommerce'), |
|
| 394 | 394 | 'type' => 'string', |
| 395 | - 'enum' => [ 'in', 'and' ], |
|
| 395 | + 'enum' => ['in', 'and'], |
|
| 396 | 396 | 'default' => 'and', |
| 397 | 397 | 'sanitize_callback' => 'sanitize_key', |
| 398 | 398 | 'validate_callback' => 'rest_validate_request_arg', |
| 399 | 399 | ); |
| 400 | 400 | |
| 401 | 401 | $params['catalog_visibility'] = array( |
| 402 | - 'description' => __( 'Determines if hidden or visible catalog products are shown.', 'woocommerce' ), |
|
| 402 | + 'description' => __('Determines if hidden or visible catalog products are shown.', 'woocommerce'), |
|
| 403 | 403 | 'type' => 'string', |
| 404 | - 'enum' => array( 'any', 'visible', 'catalog', 'search', 'hidden' ), |
|
| 404 | + 'enum' => array('any', 'visible', 'catalog', 'search', 'hidden'), |
|
| 405 | 405 | 'sanitize_callback' => 'sanitize_key', |
| 406 | 406 | 'validate_callback' => 'rest_validate_request_arg', |
| 407 | 407 | ); |
| 408 | 408 | |
| 409 | 409 | $params['rating'] = array( |
| 410 | - 'description' => __( 'Limit result set to products with a certain average rating.', 'woocommerce' ), |
|
| 410 | + 'description' => __('Limit result set to products with a certain average rating.', 'woocommerce'), |
|
| 411 | 411 | 'type' => 'array', |
| 412 | 412 | 'items' => array( |
| 413 | 413 | 'type' => 'integer', |
| 414 | - 'enum' => range( 1, 5 ), |
|
| 414 | + 'enum' => range(1, 5), |
|
| 415 | 415 | ), |
| 416 | 416 | 'default' => [], |
| 417 | 417 | 'sanitize_callback' => 'wp_parse_id_list', |
@@ -7,73 +7,73 @@ |
||
| 7 | 7 | * CartRemoveCoupon class. |
| 8 | 8 | */ |
| 9 | 9 | class CartRemoveCoupon extends AbstractCartRoute { |
| 10 | - /** |
|
| 11 | - * The route identifier. |
|
| 12 | - * |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - const IDENTIFIER = 'cart-remove-coupon'; |
|
| 10 | + /** |
|
| 11 | + * The route identifier. |
|
| 12 | + * |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + const IDENTIFIER = 'cart-remove-coupon'; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Get the path of this REST route. |
|
| 19 | - * |
|
| 20 | - * @return string |
|
| 21 | - */ |
|
| 22 | - public function get_path() { |
|
| 23 | - return '/cart/remove-coupon'; |
|
| 24 | - } |
|
| 17 | + /** |
|
| 18 | + * Get the path of this REST route. |
|
| 19 | + * |
|
| 20 | + * @return string |
|
| 21 | + */ |
|
| 22 | + public function get_path() { |
|
| 23 | + return '/cart/remove-coupon'; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Get method arguments for this REST route. |
|
| 28 | - * |
|
| 29 | - * @return array An array of endpoints. |
|
| 30 | - */ |
|
| 31 | - public function get_args() { |
|
| 32 | - return [ |
|
| 33 | - [ |
|
| 34 | - 'methods' => \WP_REST_Server::CREATABLE, |
|
| 35 | - 'callback' => [ $this, 'get_response' ], |
|
| 36 | - 'permission_callback' => '__return_true', |
|
| 37 | - 'args' => [ |
|
| 38 | - 'code' => [ |
|
| 39 | - 'description' => __( 'Unique identifier for the coupon within the cart.', 'woocommerce' ), |
|
| 40 | - 'type' => 'string', |
|
| 41 | - ], |
|
| 42 | - ], |
|
| 43 | - ], |
|
| 44 | - 'schema' => [ $this->schema, 'get_public_item_schema' ], |
|
| 45 | - 'allow_batch' => [ 'v1' => true ], |
|
| 46 | - ]; |
|
| 47 | - } |
|
| 26 | + /** |
|
| 27 | + * Get method arguments for this REST route. |
|
| 28 | + * |
|
| 29 | + * @return array An array of endpoints. |
|
| 30 | + */ |
|
| 31 | + public function get_args() { |
|
| 32 | + return [ |
|
| 33 | + [ |
|
| 34 | + 'methods' => \WP_REST_Server::CREATABLE, |
|
| 35 | + 'callback' => [ $this, 'get_response' ], |
|
| 36 | + 'permission_callback' => '__return_true', |
|
| 37 | + 'args' => [ |
|
| 38 | + 'code' => [ |
|
| 39 | + 'description' => __( 'Unique identifier for the coupon within the cart.', 'woocommerce' ), |
|
| 40 | + 'type' => 'string', |
|
| 41 | + ], |
|
| 42 | + ], |
|
| 43 | + ], |
|
| 44 | + 'schema' => [ $this->schema, 'get_public_item_schema' ], |
|
| 45 | + 'allow_batch' => [ 'v1' => true ], |
|
| 46 | + ]; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Handle the request and return a valid response for this endpoint. |
|
| 51 | - * |
|
| 52 | - * @throws RouteException On error. |
|
| 53 | - * @param \WP_REST_Request $request Request object. |
|
| 54 | - * @return \WP_REST_Response |
|
| 55 | - */ |
|
| 56 | - protected function get_route_post_response( \WP_REST_Request $request ) { |
|
| 57 | - if ( ! wc_coupons_enabled() ) { |
|
| 58 | - throw new RouteException( 'woocommerce_rest_cart_coupon_disabled', __( 'Coupons are disabled.', 'woocommerce' ), 404 ); |
|
| 59 | - } |
|
| 49 | + /** |
|
| 50 | + * Handle the request and return a valid response for this endpoint. |
|
| 51 | + * |
|
| 52 | + * @throws RouteException On error. |
|
| 53 | + * @param \WP_REST_Request $request Request object. |
|
| 54 | + * @return \WP_REST_Response |
|
| 55 | + */ |
|
| 56 | + protected function get_route_post_response( \WP_REST_Request $request ) { |
|
| 57 | + if ( ! wc_coupons_enabled() ) { |
|
| 58 | + throw new RouteException( 'woocommerce_rest_cart_coupon_disabled', __( 'Coupons are disabled.', 'woocommerce' ), 404 ); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - $cart = $this->cart_controller->get_cart_instance(); |
|
| 62 | - $coupon_code = wc_format_coupon_code( $request['code'] ); |
|
| 63 | - $coupon = new \WC_Coupon( $coupon_code ); |
|
| 61 | + $cart = $this->cart_controller->get_cart_instance(); |
|
| 62 | + $coupon_code = wc_format_coupon_code( $request['code'] ); |
|
| 63 | + $coupon = new \WC_Coupon( $coupon_code ); |
|
| 64 | 64 | |
| 65 | - if ( $coupon->get_code() !== $coupon_code || ! $coupon->is_valid() ) { |
|
| 66 | - throw new RouteException( 'woocommerce_rest_cart_coupon_error', __( 'Invalid coupon code.', 'woocommerce' ), 400 ); |
|
| 67 | - } |
|
| 65 | + if ( $coupon->get_code() !== $coupon_code || ! $coupon->is_valid() ) { |
|
| 66 | + throw new RouteException( 'woocommerce_rest_cart_coupon_error', __( 'Invalid coupon code.', 'woocommerce' ), 400 ); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - if ( ! $this->cart_controller->has_coupon( $coupon_code ) ) { |
|
| 70 | - throw new RouteException( 'woocommerce_rest_cart_coupon_invalid_code', __( 'Coupon cannot be removed because it is not already applied to the cart.', 'woocommerce' ), 409 ); |
|
| 71 | - } |
|
| 69 | + if ( ! $this->cart_controller->has_coupon( $coupon_code ) ) { |
|
| 70 | + throw new RouteException( 'woocommerce_rest_cart_coupon_invalid_code', __( 'Coupon cannot be removed because it is not already applied to the cart.', 'woocommerce' ), 409 ); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - $cart = $this->cart_controller->get_cart_instance(); |
|
| 74 | - $cart->remove_coupon( $coupon_code ); |
|
| 75 | - $cart->calculate_totals(); |
|
| 73 | + $cart = $this->cart_controller->get_cart_instance(); |
|
| 74 | + $cart->remove_coupon( $coupon_code ); |
|
| 75 | + $cart->calculate_totals(); |
|
| 76 | 76 | |
| 77 | - return rest_ensure_response( $this->schema->get_item_response( $cart ) ); |
|
| 78 | - } |
|
| 77 | + return rest_ensure_response( $this->schema->get_item_response( $cart ) ); |
|
| 78 | + } |
|
| 79 | 79 | } |
@@ -32,17 +32,17 @@ discard block |
||
| 32 | 32 | return [ |
| 33 | 33 | [ |
| 34 | 34 | 'methods' => \WP_REST_Server::CREATABLE, |
| 35 | - 'callback' => [ $this, 'get_response' ], |
|
| 35 | + 'callback' => [$this, 'get_response'], |
|
| 36 | 36 | 'permission_callback' => '__return_true', |
| 37 | 37 | 'args' => [ |
| 38 | 38 | 'code' => [ |
| 39 | - 'description' => __( 'Unique identifier for the coupon within the cart.', 'woocommerce' ), |
|
| 39 | + 'description' => __('Unique identifier for the coupon within the cart.', 'woocommerce'), |
|
| 40 | 40 | 'type' => 'string', |
| 41 | 41 | ], |
| 42 | 42 | ], |
| 43 | 43 | ], |
| 44 | - 'schema' => [ $this->schema, 'get_public_item_schema' ], |
|
| 45 | - 'allow_batch' => [ 'v1' => true ], |
|
| 44 | + 'schema' => [$this->schema, 'get_public_item_schema'], |
|
| 45 | + 'allow_batch' => ['v1' => true], |
|
| 46 | 46 | ]; |
| 47 | 47 | } |
| 48 | 48 | |
@@ -53,27 +53,27 @@ discard block |
||
| 53 | 53 | * @param \WP_REST_Request $request Request object. |
| 54 | 54 | * @return \WP_REST_Response |
| 55 | 55 | */ |
| 56 | - protected function get_route_post_response( \WP_REST_Request $request ) { |
|
| 57 | - if ( ! wc_coupons_enabled() ) { |
|
| 58 | - throw new RouteException( 'woocommerce_rest_cart_coupon_disabled', __( 'Coupons are disabled.', 'woocommerce' ), 404 ); |
|
| 56 | + protected function get_route_post_response(\WP_REST_Request $request) { |
|
| 57 | + if (!wc_coupons_enabled()) { |
|
| 58 | + throw new RouteException('woocommerce_rest_cart_coupon_disabled', __('Coupons are disabled.', 'woocommerce'), 404); |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | $cart = $this->cart_controller->get_cart_instance(); |
| 62 | - $coupon_code = wc_format_coupon_code( $request['code'] ); |
|
| 63 | - $coupon = new \WC_Coupon( $coupon_code ); |
|
| 62 | + $coupon_code = wc_format_coupon_code($request['code']); |
|
| 63 | + $coupon = new \WC_Coupon($coupon_code); |
|
| 64 | 64 | |
| 65 | - if ( $coupon->get_code() !== $coupon_code || ! $coupon->is_valid() ) { |
|
| 66 | - throw new RouteException( 'woocommerce_rest_cart_coupon_error', __( 'Invalid coupon code.', 'woocommerce' ), 400 ); |
|
| 65 | + if ($coupon->get_code() !== $coupon_code || !$coupon->is_valid()) { |
|
| 66 | + throw new RouteException('woocommerce_rest_cart_coupon_error', __('Invalid coupon code.', 'woocommerce'), 400); |
|
| 67 | 67 | } |
| 68 | 68 | |
| 69 | - if ( ! $this->cart_controller->has_coupon( $coupon_code ) ) { |
|
| 70 | - throw new RouteException( 'woocommerce_rest_cart_coupon_invalid_code', __( 'Coupon cannot be removed because it is not already applied to the cart.', 'woocommerce' ), 409 ); |
|
| 69 | + if (!$this->cart_controller->has_coupon($coupon_code)) { |
|
| 70 | + throw new RouteException('woocommerce_rest_cart_coupon_invalid_code', __('Coupon cannot be removed because it is not already applied to the cart.', 'woocommerce'), 409); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | $cart = $this->cart_controller->get_cart_instance(); |
| 74 | - $cart->remove_coupon( $coupon_code ); |
|
| 74 | + $cart->remove_coupon($coupon_code); |
|
| 75 | 75 | $cart->calculate_totals(); |
| 76 | 76 | |
| 77 | - return rest_ensure_response( $this->schema->get_item_response( $cart ) ); |
|
| 77 | + return rest_ensure_response($this->schema->get_item_response($cart)); |
|
| 78 | 78 | } |
| 79 | 79 | } |
@@ -7,80 +7,80 @@ |
||
| 7 | 7 | * CartSelectShippingRate class. |
| 8 | 8 | */ |
| 9 | 9 | class CartSelectShippingRate extends AbstractCartRoute { |
| 10 | - /** |
|
| 11 | - * The route identifier. |
|
| 12 | - * |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - const IDENTIFIER = 'cart-select-shipping-rate'; |
|
| 10 | + /** |
|
| 11 | + * The route identifier. |
|
| 12 | + * |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + const IDENTIFIER = 'cart-select-shipping-rate'; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Get the path of this REST route. |
|
| 19 | - * |
|
| 20 | - * @return string |
|
| 21 | - */ |
|
| 22 | - public function get_path() { |
|
| 23 | - return '/cart/select-shipping-rate'; |
|
| 24 | - } |
|
| 17 | + /** |
|
| 18 | + * Get the path of this REST route. |
|
| 19 | + * |
|
| 20 | + * @return string |
|
| 21 | + */ |
|
| 22 | + public function get_path() { |
|
| 23 | + return '/cart/select-shipping-rate'; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Get method arguments for this REST route. |
|
| 28 | - * |
|
| 29 | - * @return array An array of endpoints. |
|
| 30 | - */ |
|
| 31 | - public function get_args() { |
|
| 32 | - return [ |
|
| 33 | - [ |
|
| 34 | - 'methods' => \WP_REST_Server::CREATABLE, |
|
| 35 | - 'callback' => [ $this, 'get_response' ], |
|
| 36 | - 'permission_callback' => '__return_true', |
|
| 37 | - 'args' => [ |
|
| 38 | - 'package_id' => array( |
|
| 39 | - 'description' => __( 'The ID of the package being shipped.', 'woocommerce' ), |
|
| 40 | - 'type' => [ 'integer', 'string' ], |
|
| 41 | - 'required' => true, |
|
| 42 | - ), |
|
| 43 | - 'rate_id' => [ |
|
| 44 | - 'description' => __( 'The chosen rate ID for the package.', 'woocommerce' ), |
|
| 45 | - 'type' => 'string', |
|
| 46 | - 'required' => true, |
|
| 47 | - ], |
|
| 48 | - ], |
|
| 49 | - ], |
|
| 50 | - 'schema' => [ $this->schema, 'get_public_item_schema' ], |
|
| 51 | - 'allow_batch' => [ 'v1' => true ], |
|
| 52 | - ]; |
|
| 53 | - } |
|
| 26 | + /** |
|
| 27 | + * Get method arguments for this REST route. |
|
| 28 | + * |
|
| 29 | + * @return array An array of endpoints. |
|
| 30 | + */ |
|
| 31 | + public function get_args() { |
|
| 32 | + return [ |
|
| 33 | + [ |
|
| 34 | + 'methods' => \WP_REST_Server::CREATABLE, |
|
| 35 | + 'callback' => [ $this, 'get_response' ], |
|
| 36 | + 'permission_callback' => '__return_true', |
|
| 37 | + 'args' => [ |
|
| 38 | + 'package_id' => array( |
|
| 39 | + 'description' => __( 'The ID of the package being shipped.', 'woocommerce' ), |
|
| 40 | + 'type' => [ 'integer', 'string' ], |
|
| 41 | + 'required' => true, |
|
| 42 | + ), |
|
| 43 | + 'rate_id' => [ |
|
| 44 | + 'description' => __( 'The chosen rate ID for the package.', 'woocommerce' ), |
|
| 45 | + 'type' => 'string', |
|
| 46 | + 'required' => true, |
|
| 47 | + ], |
|
| 48 | + ], |
|
| 49 | + ], |
|
| 50 | + 'schema' => [ $this->schema, 'get_public_item_schema' ], |
|
| 51 | + 'allow_batch' => [ 'v1' => true ], |
|
| 52 | + ]; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Handle the request and return a valid response for this endpoint. |
|
| 57 | - * |
|
| 58 | - * @throws RouteException On error. |
|
| 59 | - * @param \WP_REST_Request $request Request object. |
|
| 60 | - * @return \WP_REST_Response |
|
| 61 | - */ |
|
| 62 | - protected function get_route_post_response( \WP_REST_Request $request ) { |
|
| 63 | - if ( ! wc_shipping_enabled() ) { |
|
| 64 | - throw new RouteException( 'woocommerce_rest_shipping_disabled', __( 'Shipping is disabled.', 'woocommerce' ), 404 ); |
|
| 65 | - } |
|
| 55 | + /** |
|
| 56 | + * Handle the request and return a valid response for this endpoint. |
|
| 57 | + * |
|
| 58 | + * @throws RouteException On error. |
|
| 59 | + * @param \WP_REST_Request $request Request object. |
|
| 60 | + * @return \WP_REST_Response |
|
| 61 | + */ |
|
| 62 | + protected function get_route_post_response( \WP_REST_Request $request ) { |
|
| 63 | + if ( ! wc_shipping_enabled() ) { |
|
| 64 | + throw new RouteException( 'woocommerce_rest_shipping_disabled', __( 'Shipping is disabled.', 'woocommerce' ), 404 ); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - if ( ! isset( $request['package_id'] ) ) { |
|
| 68 | - throw new RouteException( 'woocommerce_rest_cart_missing_package_id', __( 'Invalid Package ID.', 'woocommerce' ), 400 ); |
|
| 69 | - } |
|
| 67 | + if ( ! isset( $request['package_id'] ) ) { |
|
| 68 | + throw new RouteException( 'woocommerce_rest_cart_missing_package_id', __( 'Invalid Package ID.', 'woocommerce' ), 400 ); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - $cart = $this->cart_controller->get_cart_instance(); |
|
| 72 | - $package_id = wc_clean( wp_unslash( $request['package_id'] ) ); |
|
| 73 | - $rate_id = wc_clean( wp_unslash( $request['rate_id'] ) ); |
|
| 71 | + $cart = $this->cart_controller->get_cart_instance(); |
|
| 72 | + $package_id = wc_clean( wp_unslash( $request['package_id'] ) ); |
|
| 73 | + $rate_id = wc_clean( wp_unslash( $request['rate_id'] ) ); |
|
| 74 | 74 | |
| 75 | - try { |
|
| 76 | - $this->cart_controller->select_shipping_rate( $package_id, $rate_id ); |
|
| 77 | - } catch ( \WC_Rest_Exception $e ) { |
|
| 78 | - throw new RouteException( $e->getErrorCode(), $e->getMessage(), $e->getCode() ); |
|
| 79 | - } |
|
| 75 | + try { |
|
| 76 | + $this->cart_controller->select_shipping_rate( $package_id, $rate_id ); |
|
| 77 | + } catch ( \WC_Rest_Exception $e ) { |
|
| 78 | + throw new RouteException( $e->getErrorCode(), $e->getMessage(), $e->getCode() ); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - $cart->calculate_shipping(); |
|
| 82 | - $cart->calculate_totals(); |
|
| 81 | + $cart->calculate_shipping(); |
|
| 82 | + $cart->calculate_totals(); |
|
| 83 | 83 | |
| 84 | - return rest_ensure_response( $this->schema->get_item_response( $cart ) ); |
|
| 85 | - } |
|
| 84 | + return rest_ensure_response( $this->schema->get_item_response( $cart ) ); |
|
| 85 | + } |
|
| 86 | 86 | } |
@@ -32,23 +32,23 @@ discard block |
||
| 32 | 32 | return [ |
| 33 | 33 | [ |
| 34 | 34 | 'methods' => \WP_REST_Server::CREATABLE, |
| 35 | - 'callback' => [ $this, 'get_response' ], |
|
| 35 | + 'callback' => [$this, 'get_response'], |
|
| 36 | 36 | 'permission_callback' => '__return_true', |
| 37 | 37 | 'args' => [ |
| 38 | 38 | 'package_id' => array( |
| 39 | - 'description' => __( 'The ID of the package being shipped.', 'woocommerce' ), |
|
| 40 | - 'type' => [ 'integer', 'string' ], |
|
| 39 | + 'description' => __('The ID of the package being shipped.', 'woocommerce'), |
|
| 40 | + 'type' => ['integer', 'string'], |
|
| 41 | 41 | 'required' => true, |
| 42 | 42 | ), |
| 43 | 43 | 'rate_id' => [ |
| 44 | - 'description' => __( 'The chosen rate ID for the package.', 'woocommerce' ), |
|
| 44 | + 'description' => __('The chosen rate ID for the package.', 'woocommerce'), |
|
| 45 | 45 | 'type' => 'string', |
| 46 | 46 | 'required' => true, |
| 47 | 47 | ], |
| 48 | 48 | ], |
| 49 | 49 | ], |
| 50 | - 'schema' => [ $this->schema, 'get_public_item_schema' ], |
|
| 51 | - 'allow_batch' => [ 'v1' => true ], |
|
| 50 | + 'schema' => [$this->schema, 'get_public_item_schema'], |
|
| 51 | + 'allow_batch' => ['v1' => true], |
|
| 52 | 52 | ]; |
| 53 | 53 | } |
| 54 | 54 | |
@@ -59,28 +59,28 @@ discard block |
||
| 59 | 59 | * @param \WP_REST_Request $request Request object. |
| 60 | 60 | * @return \WP_REST_Response |
| 61 | 61 | */ |
| 62 | - protected function get_route_post_response( \WP_REST_Request $request ) { |
|
| 63 | - if ( ! wc_shipping_enabled() ) { |
|
| 64 | - throw new RouteException( 'woocommerce_rest_shipping_disabled', __( 'Shipping is disabled.', 'woocommerce' ), 404 ); |
|
| 62 | + protected function get_route_post_response(\WP_REST_Request $request) { |
|
| 63 | + if (!wc_shipping_enabled()) { |
|
| 64 | + throw new RouteException('woocommerce_rest_shipping_disabled', __('Shipping is disabled.', 'woocommerce'), 404); |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | - if ( ! isset( $request['package_id'] ) ) { |
|
| 68 | - throw new RouteException( 'woocommerce_rest_cart_missing_package_id', __( 'Invalid Package ID.', 'woocommerce' ), 400 ); |
|
| 67 | + if (!isset($request['package_id'])) { |
|
| 68 | + throw new RouteException('woocommerce_rest_cart_missing_package_id', __('Invalid Package ID.', 'woocommerce'), 400); |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | $cart = $this->cart_controller->get_cart_instance(); |
| 72 | - $package_id = wc_clean( wp_unslash( $request['package_id'] ) ); |
|
| 73 | - $rate_id = wc_clean( wp_unslash( $request['rate_id'] ) ); |
|
| 72 | + $package_id = wc_clean(wp_unslash($request['package_id'])); |
|
| 73 | + $rate_id = wc_clean(wp_unslash($request['rate_id'])); |
|
| 74 | 74 | |
| 75 | 75 | try { |
| 76 | - $this->cart_controller->select_shipping_rate( $package_id, $rate_id ); |
|
| 77 | - } catch ( \WC_Rest_Exception $e ) { |
|
| 78 | - throw new RouteException( $e->getErrorCode(), $e->getMessage(), $e->getCode() ); |
|
| 76 | + $this->cart_controller->select_shipping_rate($package_id, $rate_id); |
|
| 77 | + } catch (\WC_Rest_Exception $e) { |
|
| 78 | + throw new RouteException($e->getErrorCode(), $e->getMessage(), $e->getCode()); |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | $cart->calculate_shipping(); |
| 82 | 82 | $cart->calculate_totals(); |
| 83 | 83 | |
| 84 | - return rest_ensure_response( $this->schema->get_item_response( $cart ) ); |
|
| 84 | + return rest_ensure_response($this->schema->get_item_response($cart)); |
|
| 85 | 85 | } |
| 86 | 86 | } |
@@ -5,62 +5,62 @@ |
||
| 5 | 5 | * ProductAttributes class. |
| 6 | 6 | */ |
| 7 | 7 | class ProductAttributes extends AbstractRoute { |
| 8 | - /** |
|
| 9 | - * The route identifier. |
|
| 10 | - * |
|
| 11 | - * @var string |
|
| 12 | - */ |
|
| 13 | - const IDENTIFIER = 'product-attributes'; |
|
| 8 | + /** |
|
| 9 | + * The route identifier. |
|
| 10 | + * |
|
| 11 | + * @var string |
|
| 12 | + */ |
|
| 13 | + const IDENTIFIER = 'product-attributes'; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * The routes schema. |
|
| 17 | - * |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 20 | - const SCHEMA_TYPE = 'product-attribute'; |
|
| 15 | + /** |
|
| 16 | + * The routes schema. |
|
| 17 | + * |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | + const SCHEMA_TYPE = 'product-attribute'; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Get the path of this REST route. |
|
| 24 | - * |
|
| 25 | - * @return string |
|
| 26 | - */ |
|
| 27 | - public function get_path() { |
|
| 28 | - return '/products/attributes'; |
|
| 29 | - } |
|
| 22 | + /** |
|
| 23 | + * Get the path of this REST route. |
|
| 24 | + * |
|
| 25 | + * @return string |
|
| 26 | + */ |
|
| 27 | + public function get_path() { |
|
| 28 | + return '/products/attributes'; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Get method arguments for this REST route. |
|
| 33 | - * |
|
| 34 | - * @return array An array of endpoints. |
|
| 35 | - */ |
|
| 36 | - public function get_args() { |
|
| 37 | - return [ |
|
| 38 | - [ |
|
| 39 | - 'methods' => \WP_REST_Server::READABLE, |
|
| 40 | - 'callback' => [ $this, 'get_response' ], |
|
| 41 | - 'permission_callback' => '__return_true', |
|
| 42 | - 'args' => $this->get_collection_params(), |
|
| 43 | - ], |
|
| 44 | - 'schema' => [ $this->schema, 'get_public_item_schema' ], |
|
| 45 | - ]; |
|
| 46 | - } |
|
| 31 | + /** |
|
| 32 | + * Get method arguments for this REST route. |
|
| 33 | + * |
|
| 34 | + * @return array An array of endpoints. |
|
| 35 | + */ |
|
| 36 | + public function get_args() { |
|
| 37 | + return [ |
|
| 38 | + [ |
|
| 39 | + 'methods' => \WP_REST_Server::READABLE, |
|
| 40 | + 'callback' => [ $this, 'get_response' ], |
|
| 41 | + 'permission_callback' => '__return_true', |
|
| 42 | + 'args' => $this->get_collection_params(), |
|
| 43 | + ], |
|
| 44 | + 'schema' => [ $this->schema, 'get_public_item_schema' ], |
|
| 45 | + ]; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Get a collection of attributes. |
|
| 50 | - * |
|
| 51 | - * @param \WP_REST_Request $request Request object. |
|
| 52 | - * @return \WP_REST_Response |
|
| 53 | - */ |
|
| 54 | - protected function get_route_response( \WP_REST_Request $request ) { |
|
| 55 | - $ids = wc_get_attribute_taxonomy_ids(); |
|
| 56 | - $return = []; |
|
| 48 | + /** |
|
| 49 | + * Get a collection of attributes. |
|
| 50 | + * |
|
| 51 | + * @param \WP_REST_Request $request Request object. |
|
| 52 | + * @return \WP_REST_Response |
|
| 53 | + */ |
|
| 54 | + protected function get_route_response( \WP_REST_Request $request ) { |
|
| 55 | + $ids = wc_get_attribute_taxonomy_ids(); |
|
| 56 | + $return = []; |
|
| 57 | 57 | |
| 58 | - foreach ( $ids as $id ) { |
|
| 59 | - $object = wc_get_attribute( $id ); |
|
| 60 | - $data = $this->prepare_item_for_response( $object, $request ); |
|
| 61 | - $return[] = $this->prepare_response_for_collection( $data ); |
|
| 62 | - } |
|
| 58 | + foreach ( $ids as $id ) { |
|
| 59 | + $object = wc_get_attribute( $id ); |
|
| 60 | + $data = $this->prepare_item_for_response( $object, $request ); |
|
| 61 | + $return[] = $this->prepare_response_for_collection( $data ); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - return rest_ensure_response( $return ); |
|
| 65 | - } |
|
| 64 | + return rest_ensure_response( $return ); |
|
| 65 | + } |
|
| 66 | 66 | } |
@@ -37,11 +37,11 @@ discard block |
||
| 37 | 37 | return [ |
| 38 | 38 | [ |
| 39 | 39 | 'methods' => \WP_REST_Server::READABLE, |
| 40 | - 'callback' => [ $this, 'get_response' ], |
|
| 40 | + 'callback' => [$this, 'get_response'], |
|
| 41 | 41 | 'permission_callback' => '__return_true', |
| 42 | 42 | 'args' => $this->get_collection_params(), |
| 43 | 43 | ], |
| 44 | - 'schema' => [ $this->schema, 'get_public_item_schema' ], |
|
| 44 | + 'schema' => [$this->schema, 'get_public_item_schema'], |
|
| 45 | 45 | ]; |
| 46 | 46 | } |
| 47 | 47 | |
@@ -51,16 +51,16 @@ discard block |
||
| 51 | 51 | * @param \WP_REST_Request $request Request object. |
| 52 | 52 | * @return \WP_REST_Response |
| 53 | 53 | */ |
| 54 | - protected function get_route_response( \WP_REST_Request $request ) { |
|
| 54 | + protected function get_route_response(\WP_REST_Request $request) { |
|
| 55 | 55 | $ids = wc_get_attribute_taxonomy_ids(); |
| 56 | 56 | $return = []; |
| 57 | 57 | |
| 58 | - foreach ( $ids as $id ) { |
|
| 59 | - $object = wc_get_attribute( $id ); |
|
| 60 | - $data = $this->prepare_item_for_response( $object, $request ); |
|
| 61 | - $return[] = $this->prepare_response_for_collection( $data ); |
|
| 58 | + foreach ($ids as $id) { |
|
| 59 | + $object = wc_get_attribute($id); |
|
| 60 | + $data = $this->prepare_item_for_response($object, $request); |
|
| 61 | + $return[] = $this->prepare_response_for_collection($data); |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | - return rest_ensure_response( $return ); |
|
| 64 | + return rest_ensure_response($return); |
|
| 65 | 65 | } |
| 66 | 66 | } |
@@ -7,136 +7,136 @@ |
||
| 7 | 7 | * CartItems class. |
| 8 | 8 | */ |
| 9 | 9 | class CartItems extends AbstractCartRoute { |
| 10 | - /** |
|
| 11 | - * The route identifier. |
|
| 12 | - * |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - const IDENTIFIER = 'cart-items'; |
|
| 10 | + /** |
|
| 11 | + * The route identifier. |
|
| 12 | + * |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + const IDENTIFIER = 'cart-items'; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * The routes schema. |
|
| 19 | - * |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - const SCHEMA_TYPE = 'cart-item'; |
|
| 17 | + /** |
|
| 18 | + * The routes schema. |
|
| 19 | + * |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + const SCHEMA_TYPE = 'cart-item'; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Get the path of this REST route. |
|
| 26 | - * |
|
| 27 | - * @return string |
|
| 28 | - */ |
|
| 29 | - public function get_path() { |
|
| 30 | - return '/cart/items'; |
|
| 31 | - } |
|
| 24 | + /** |
|
| 25 | + * Get the path of this REST route. |
|
| 26 | + * |
|
| 27 | + * @return string |
|
| 28 | + */ |
|
| 29 | + public function get_path() { |
|
| 30 | + return '/cart/items'; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Get method arguments for this REST route. |
|
| 35 | - * |
|
| 36 | - * @return array An array of endpoints. |
|
| 37 | - */ |
|
| 38 | - public function get_args() { |
|
| 39 | - return [ |
|
| 40 | - [ |
|
| 41 | - 'methods' => \WP_REST_Server::READABLE, |
|
| 42 | - 'callback' => [ $this, 'get_response' ], |
|
| 43 | - 'permission_callback' => '__return_true', |
|
| 44 | - 'args' => [ |
|
| 45 | - 'context' => $this->get_context_param( [ 'default' => 'view' ] ), |
|
| 46 | - ], |
|
| 47 | - ], |
|
| 48 | - [ |
|
| 49 | - 'methods' => \WP_REST_Server::CREATABLE, |
|
| 50 | - 'callback' => array( $this, 'get_response' ), |
|
| 51 | - 'permission_callback' => '__return_true', |
|
| 52 | - 'args' => $this->schema->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ), |
|
| 53 | - ], |
|
| 54 | - [ |
|
| 55 | - 'methods' => \WP_REST_Server::DELETABLE, |
|
| 56 | - 'callback' => [ $this, 'get_response' ], |
|
| 57 | - 'permission_callback' => '__return_true', |
|
| 58 | - ], |
|
| 59 | - 'schema' => [ $this->schema, 'get_public_item_schema' ], |
|
| 60 | - 'allow_batch' => [ 'v1' => true ], |
|
| 61 | - ]; |
|
| 62 | - } |
|
| 33 | + /** |
|
| 34 | + * Get method arguments for this REST route. |
|
| 35 | + * |
|
| 36 | + * @return array An array of endpoints. |
|
| 37 | + */ |
|
| 38 | + public function get_args() { |
|
| 39 | + return [ |
|
| 40 | + [ |
|
| 41 | + 'methods' => \WP_REST_Server::READABLE, |
|
| 42 | + 'callback' => [ $this, 'get_response' ], |
|
| 43 | + 'permission_callback' => '__return_true', |
|
| 44 | + 'args' => [ |
|
| 45 | + 'context' => $this->get_context_param( [ 'default' => 'view' ] ), |
|
| 46 | + ], |
|
| 47 | + ], |
|
| 48 | + [ |
|
| 49 | + 'methods' => \WP_REST_Server::CREATABLE, |
|
| 50 | + 'callback' => array( $this, 'get_response' ), |
|
| 51 | + 'permission_callback' => '__return_true', |
|
| 52 | + 'args' => $this->schema->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ), |
|
| 53 | + ], |
|
| 54 | + [ |
|
| 55 | + 'methods' => \WP_REST_Server::DELETABLE, |
|
| 56 | + 'callback' => [ $this, 'get_response' ], |
|
| 57 | + 'permission_callback' => '__return_true', |
|
| 58 | + ], |
|
| 59 | + 'schema' => [ $this->schema, 'get_public_item_schema' ], |
|
| 60 | + 'allow_batch' => [ 'v1' => true ], |
|
| 61 | + ]; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Get a collection of cart items. |
|
| 66 | - * |
|
| 67 | - * @throws RouteException On error. |
|
| 68 | - * @param \WP_REST_Request $request Request object. |
|
| 69 | - * @return \WP_REST_Response |
|
| 70 | - */ |
|
| 71 | - protected function get_route_response( \WP_REST_Request $request ) { |
|
| 72 | - $cart_items = $this->cart_controller->get_cart_items(); |
|
| 73 | - $items = []; |
|
| 64 | + /** |
|
| 65 | + * Get a collection of cart items. |
|
| 66 | + * |
|
| 67 | + * @throws RouteException On error. |
|
| 68 | + * @param \WP_REST_Request $request Request object. |
|
| 69 | + * @return \WP_REST_Response |
|
| 70 | + */ |
|
| 71 | + protected function get_route_response( \WP_REST_Request $request ) { |
|
| 72 | + $cart_items = $this->cart_controller->get_cart_items(); |
|
| 73 | + $items = []; |
|
| 74 | 74 | |
| 75 | - foreach ( $cart_items as $cart_item ) { |
|
| 76 | - $data = $this->prepare_item_for_response( $cart_item, $request ); |
|
| 77 | - $items[] = $this->prepare_response_for_collection( $data ); |
|
| 78 | - } |
|
| 75 | + foreach ( $cart_items as $cart_item ) { |
|
| 76 | + $data = $this->prepare_item_for_response( $cart_item, $request ); |
|
| 77 | + $items[] = $this->prepare_response_for_collection( $data ); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - $response = rest_ensure_response( $items ); |
|
| 80 | + $response = rest_ensure_response( $items ); |
|
| 81 | 81 | |
| 82 | - return $response; |
|
| 83 | - } |
|
| 82 | + return $response; |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * Creates one item from the collection. |
|
| 87 | - * |
|
| 88 | - * @throws RouteException On error. |
|
| 89 | - * @param \WP_REST_Request $request Request object. |
|
| 90 | - * @return \WP_REST_Response |
|
| 91 | - */ |
|
| 92 | - protected function get_route_post_response( \WP_REST_Request $request ) { |
|
| 93 | - // Do not allow key to be specified during creation. |
|
| 94 | - if ( ! empty( $request['key'] ) ) { |
|
| 95 | - throw new RouteException( 'woocommerce_rest_cart_item_exists', __( 'Cannot create an existing cart item.', 'woocommerce' ), 400 ); |
|
| 96 | - } |
|
| 85 | + /** |
|
| 86 | + * Creates one item from the collection. |
|
| 87 | + * |
|
| 88 | + * @throws RouteException On error. |
|
| 89 | + * @param \WP_REST_Request $request Request object. |
|
| 90 | + * @return \WP_REST_Response |
|
| 91 | + */ |
|
| 92 | + protected function get_route_post_response( \WP_REST_Request $request ) { |
|
| 93 | + // Do not allow key to be specified during creation. |
|
| 94 | + if ( ! empty( $request['key'] ) ) { |
|
| 95 | + throw new RouteException( 'woocommerce_rest_cart_item_exists', __( 'Cannot create an existing cart item.', 'woocommerce' ), 400 ); |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - $result = $this->cart_controller->add_to_cart( |
|
| 99 | - [ |
|
| 100 | - 'id' => $request['id'], |
|
| 101 | - 'quantity' => $request['quantity'], |
|
| 102 | - 'variation' => $request['variation'], |
|
| 103 | - ] |
|
| 104 | - ); |
|
| 98 | + $result = $this->cart_controller->add_to_cart( |
|
| 99 | + [ |
|
| 100 | + 'id' => $request['id'], |
|
| 101 | + 'quantity' => $request['quantity'], |
|
| 102 | + 'variation' => $request['variation'], |
|
| 103 | + ] |
|
| 104 | + ); |
|
| 105 | 105 | |
| 106 | - $response = rest_ensure_response( $this->prepare_item_for_response( $this->cart_controller->get_cart_item( $result ), $request ) ); |
|
| 107 | - $response->set_status( 201 ); |
|
| 108 | - return $response; |
|
| 109 | - } |
|
| 106 | + $response = rest_ensure_response( $this->prepare_item_for_response( $this->cart_controller->get_cart_item( $result ), $request ) ); |
|
| 107 | + $response->set_status( 201 ); |
|
| 108 | + return $response; |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * Deletes all items in the cart. |
|
| 113 | - * |
|
| 114 | - * @throws RouteException On error. |
|
| 115 | - * @param \WP_REST_Request $request Request object. |
|
| 116 | - * @return \WP_REST_Response |
|
| 117 | - */ |
|
| 118 | - protected function get_route_delete_response( \WP_REST_Request $request ) { |
|
| 119 | - $this->cart_controller->empty_cart(); |
|
| 120 | - return new \WP_REST_Response( [], 200 ); |
|
| 121 | - } |
|
| 111 | + /** |
|
| 112 | + * Deletes all items in the cart. |
|
| 113 | + * |
|
| 114 | + * @throws RouteException On error. |
|
| 115 | + * @param \WP_REST_Request $request Request object. |
|
| 116 | + * @return \WP_REST_Response |
|
| 117 | + */ |
|
| 118 | + protected function get_route_delete_response( \WP_REST_Request $request ) { |
|
| 119 | + $this->cart_controller->empty_cart(); |
|
| 120 | + return new \WP_REST_Response( [], 200 ); |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * Prepare links for the request. |
|
| 125 | - * |
|
| 126 | - * @param array $cart_item Object to prepare. |
|
| 127 | - * @param \WP_REST_Request $request Request object. |
|
| 128 | - * @return array |
|
| 129 | - */ |
|
| 130 | - protected function prepare_links( $cart_item, $request ) { |
|
| 131 | - $base = $this->get_namespace() . $this->get_path(); |
|
| 132 | - $links = array( |
|
| 133 | - 'self' => array( |
|
| 134 | - 'href' => rest_url( trailingslashit( $base ) . $cart_item['key'] ), |
|
| 135 | - ), |
|
| 136 | - 'collection' => array( |
|
| 137 | - 'href' => rest_url( $base ), |
|
| 138 | - ), |
|
| 139 | - ); |
|
| 140 | - return $links; |
|
| 141 | - } |
|
| 123 | + /** |
|
| 124 | + * Prepare links for the request. |
|
| 125 | + * |
|
| 126 | + * @param array $cart_item Object to prepare. |
|
| 127 | + * @param \WP_REST_Request $request Request object. |
|
| 128 | + * @return array |
|
| 129 | + */ |
|
| 130 | + protected function prepare_links( $cart_item, $request ) { |
|
| 131 | + $base = $this->get_namespace() . $this->get_path(); |
|
| 132 | + $links = array( |
|
| 133 | + 'self' => array( |
|
| 134 | + 'href' => rest_url( trailingslashit( $base ) . $cart_item['key'] ), |
|
| 135 | + ), |
|
| 136 | + 'collection' => array( |
|
| 137 | + 'href' => rest_url( $base ), |
|
| 138 | + ), |
|
| 139 | + ); |
|
| 140 | + return $links; |
|
| 141 | + } |
|
| 142 | 142 | } |
@@ -39,25 +39,25 @@ discard block |
||
| 39 | 39 | return [ |
| 40 | 40 | [ |
| 41 | 41 | 'methods' => \WP_REST_Server::READABLE, |
| 42 | - 'callback' => [ $this, 'get_response' ], |
|
| 42 | + 'callback' => [$this, 'get_response'], |
|
| 43 | 43 | 'permission_callback' => '__return_true', |
| 44 | 44 | 'args' => [ |
| 45 | - 'context' => $this->get_context_param( [ 'default' => 'view' ] ), |
|
| 45 | + 'context' => $this->get_context_param(['default' => 'view']), |
|
| 46 | 46 | ], |
| 47 | 47 | ], |
| 48 | 48 | [ |
| 49 | 49 | 'methods' => \WP_REST_Server::CREATABLE, |
| 50 | - 'callback' => array( $this, 'get_response' ), |
|
| 50 | + 'callback' => array($this, 'get_response'), |
|
| 51 | 51 | 'permission_callback' => '__return_true', |
| 52 | - 'args' => $this->schema->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ), |
|
| 52 | + 'args' => $this->schema->get_endpoint_args_for_item_schema(\WP_REST_Server::CREATABLE), |
|
| 53 | 53 | ], |
| 54 | 54 | [ |
| 55 | 55 | 'methods' => \WP_REST_Server::DELETABLE, |
| 56 | - 'callback' => [ $this, 'get_response' ], |
|
| 56 | + 'callback' => [$this, 'get_response'], |
|
| 57 | 57 | 'permission_callback' => '__return_true', |
| 58 | 58 | ], |
| 59 | - 'schema' => [ $this->schema, 'get_public_item_schema' ], |
|
| 60 | - 'allow_batch' => [ 'v1' => true ], |
|
| 59 | + 'schema' => [$this->schema, 'get_public_item_schema'], |
|
| 60 | + 'allow_batch' => ['v1' => true], |
|
| 61 | 61 | ]; |
| 62 | 62 | } |
| 63 | 63 | |
@@ -68,16 +68,16 @@ discard block |
||
| 68 | 68 | * @param \WP_REST_Request $request Request object. |
| 69 | 69 | * @return \WP_REST_Response |
| 70 | 70 | */ |
| 71 | - protected function get_route_response( \WP_REST_Request $request ) { |
|
| 71 | + protected function get_route_response(\WP_REST_Request $request) { |
|
| 72 | 72 | $cart_items = $this->cart_controller->get_cart_items(); |
| 73 | 73 | $items = []; |
| 74 | 74 | |
| 75 | - foreach ( $cart_items as $cart_item ) { |
|
| 76 | - $data = $this->prepare_item_for_response( $cart_item, $request ); |
|
| 77 | - $items[] = $this->prepare_response_for_collection( $data ); |
|
| 75 | + foreach ($cart_items as $cart_item) { |
|
| 76 | + $data = $this->prepare_item_for_response($cart_item, $request); |
|
| 77 | + $items[] = $this->prepare_response_for_collection($data); |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - $response = rest_ensure_response( $items ); |
|
| 80 | + $response = rest_ensure_response($items); |
|
| 81 | 81 | |
| 82 | 82 | return $response; |
| 83 | 83 | } |
@@ -89,10 +89,10 @@ discard block |
||
| 89 | 89 | * @param \WP_REST_Request $request Request object. |
| 90 | 90 | * @return \WP_REST_Response |
| 91 | 91 | */ |
| 92 | - protected function get_route_post_response( \WP_REST_Request $request ) { |
|
| 92 | + protected function get_route_post_response(\WP_REST_Request $request) { |
|
| 93 | 93 | // Do not allow key to be specified during creation. |
| 94 | - if ( ! empty( $request['key'] ) ) { |
|
| 95 | - throw new RouteException( 'woocommerce_rest_cart_item_exists', __( 'Cannot create an existing cart item.', 'woocommerce' ), 400 ); |
|
| 94 | + if (!empty($request['key'])) { |
|
| 95 | + throw new RouteException('woocommerce_rest_cart_item_exists', __('Cannot create an existing cart item.', 'woocommerce'), 400); |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | $result = $this->cart_controller->add_to_cart( |
@@ -103,8 +103,8 @@ discard block |
||
| 103 | 103 | ] |
| 104 | 104 | ); |
| 105 | 105 | |
| 106 | - $response = rest_ensure_response( $this->prepare_item_for_response( $this->cart_controller->get_cart_item( $result ), $request ) ); |
|
| 107 | - $response->set_status( 201 ); |
|
| 106 | + $response = rest_ensure_response($this->prepare_item_for_response($this->cart_controller->get_cart_item($result), $request)); |
|
| 107 | + $response->set_status(201); |
|
| 108 | 108 | return $response; |
| 109 | 109 | } |
| 110 | 110 | |
@@ -115,9 +115,9 @@ discard block |
||
| 115 | 115 | * @param \WP_REST_Request $request Request object. |
| 116 | 116 | * @return \WP_REST_Response |
| 117 | 117 | */ |
| 118 | - protected function get_route_delete_response( \WP_REST_Request $request ) { |
|
| 118 | + protected function get_route_delete_response(\WP_REST_Request $request) { |
|
| 119 | 119 | $this->cart_controller->empty_cart(); |
| 120 | - return new \WP_REST_Response( [], 200 ); |
|
| 120 | + return new \WP_REST_Response([], 200); |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | /** |
@@ -127,14 +127,14 @@ discard block |
||
| 127 | 127 | * @param \WP_REST_Request $request Request object. |
| 128 | 128 | * @return array |
| 129 | 129 | */ |
| 130 | - protected function prepare_links( $cart_item, $request ) { |
|
| 130 | + protected function prepare_links($cart_item, $request) { |
|
| 131 | 131 | $base = $this->get_namespace() . $this->get_path(); |
| 132 | 132 | $links = array( |
| 133 | 133 | 'self' => array( |
| 134 | - 'href' => rest_url( trailingslashit( $base ) . $cart_item['key'] ), |
|
| 134 | + 'href' => rest_url(trailingslashit($base) . $cart_item['key']), |
|
| 135 | 135 | ), |
| 136 | 136 | 'collection' => array( |
| 137 | - 'href' => rest_url( $base ), |
|
| 137 | + 'href' => rest_url($base), |
|
| 138 | 138 | ), |
| 139 | 139 | ); |
| 140 | 140 | return $links; |
@@ -7,99 +7,99 @@ |
||
| 7 | 7 | * CartAddItem class. |
| 8 | 8 | */ |
| 9 | 9 | class CartAddItem extends AbstractCartRoute { |
| 10 | - /** |
|
| 11 | - * The route identifier. |
|
| 12 | - * |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - const IDENTIFIER = 'cart-add-item'; |
|
| 10 | + /** |
|
| 11 | + * The route identifier. |
|
| 12 | + * |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + const IDENTIFIER = 'cart-add-item'; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Get the path of this REST route. |
|
| 19 | - * |
|
| 20 | - * @return string |
|
| 21 | - */ |
|
| 22 | - public function get_path() { |
|
| 23 | - return '/cart/add-item'; |
|
| 24 | - } |
|
| 17 | + /** |
|
| 18 | + * Get the path of this REST route. |
|
| 19 | + * |
|
| 20 | + * @return string |
|
| 21 | + */ |
|
| 22 | + public function get_path() { |
|
| 23 | + return '/cart/add-item'; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Get method arguments for this REST route. |
|
| 28 | - * |
|
| 29 | - * @return array An array of endpoints. |
|
| 30 | - */ |
|
| 31 | - public function get_args() { |
|
| 32 | - return [ |
|
| 33 | - [ |
|
| 34 | - 'methods' => \WP_REST_Server::CREATABLE, |
|
| 35 | - 'callback' => [ $this, 'get_response' ], |
|
| 36 | - 'permission_callback' => '__return_true', |
|
| 37 | - 'args' => [ |
|
| 38 | - 'id' => [ |
|
| 39 | - 'description' => __( 'The cart item product or variation ID.', 'woocommerce' ), |
|
| 40 | - 'type' => 'integer', |
|
| 41 | - 'context' => [ 'view', 'edit' ], |
|
| 42 | - 'sanitize_callback' => 'absint', |
|
| 43 | - ], |
|
| 44 | - 'quantity' => [ |
|
| 45 | - 'description' => __( 'Quantity of this item to add to the cart.', 'woocommerce' ), |
|
| 46 | - 'type' => 'integer', |
|
| 47 | - 'context' => [ 'view', 'edit' ], |
|
| 48 | - 'arg_options' => [ |
|
| 49 | - 'sanitize_callback' => 'wc_stock_amount', |
|
| 50 | - ], |
|
| 51 | - ], |
|
| 52 | - 'variation' => [ |
|
| 53 | - 'description' => __( 'Chosen attributes (for variations).', 'woocommerce' ), |
|
| 54 | - 'type' => 'array', |
|
| 55 | - 'context' => [ 'view', 'edit' ], |
|
| 56 | - 'items' => [ |
|
| 57 | - 'type' => 'object', |
|
| 58 | - 'properties' => [ |
|
| 59 | - 'attribute' => [ |
|
| 60 | - 'description' => __( 'Variation attribute name.', 'woocommerce' ), |
|
| 61 | - 'type' => 'string', |
|
| 62 | - 'context' => [ 'view', 'edit' ], |
|
| 63 | - ], |
|
| 64 | - 'value' => [ |
|
| 65 | - 'description' => __( 'Variation attribute value.', 'woocommerce' ), |
|
| 66 | - 'type' => 'string', |
|
| 67 | - 'context' => [ 'view', 'edit' ], |
|
| 68 | - ], |
|
| 69 | - ], |
|
| 70 | - ], |
|
| 71 | - ], |
|
| 72 | - ], |
|
| 73 | - ], |
|
| 74 | - 'schema' => [ $this->schema, 'get_public_item_schema' ], |
|
| 75 | - 'allow_batch' => [ 'v1' => true ], |
|
| 76 | - ]; |
|
| 77 | - } |
|
| 26 | + /** |
|
| 27 | + * Get method arguments for this REST route. |
|
| 28 | + * |
|
| 29 | + * @return array An array of endpoints. |
|
| 30 | + */ |
|
| 31 | + public function get_args() { |
|
| 32 | + return [ |
|
| 33 | + [ |
|
| 34 | + 'methods' => \WP_REST_Server::CREATABLE, |
|
| 35 | + 'callback' => [ $this, 'get_response' ], |
|
| 36 | + 'permission_callback' => '__return_true', |
|
| 37 | + 'args' => [ |
|
| 38 | + 'id' => [ |
|
| 39 | + 'description' => __( 'The cart item product or variation ID.', 'woocommerce' ), |
|
| 40 | + 'type' => 'integer', |
|
| 41 | + 'context' => [ 'view', 'edit' ], |
|
| 42 | + 'sanitize_callback' => 'absint', |
|
| 43 | + ], |
|
| 44 | + 'quantity' => [ |
|
| 45 | + 'description' => __( 'Quantity of this item to add to the cart.', 'woocommerce' ), |
|
| 46 | + 'type' => 'integer', |
|
| 47 | + 'context' => [ 'view', 'edit' ], |
|
| 48 | + 'arg_options' => [ |
|
| 49 | + 'sanitize_callback' => 'wc_stock_amount', |
|
| 50 | + ], |
|
| 51 | + ], |
|
| 52 | + 'variation' => [ |
|
| 53 | + 'description' => __( 'Chosen attributes (for variations).', 'woocommerce' ), |
|
| 54 | + 'type' => 'array', |
|
| 55 | + 'context' => [ 'view', 'edit' ], |
|
| 56 | + 'items' => [ |
|
| 57 | + 'type' => 'object', |
|
| 58 | + 'properties' => [ |
|
| 59 | + 'attribute' => [ |
|
| 60 | + 'description' => __( 'Variation attribute name.', 'woocommerce' ), |
|
| 61 | + 'type' => 'string', |
|
| 62 | + 'context' => [ 'view', 'edit' ], |
|
| 63 | + ], |
|
| 64 | + 'value' => [ |
|
| 65 | + 'description' => __( 'Variation attribute value.', 'woocommerce' ), |
|
| 66 | + 'type' => 'string', |
|
| 67 | + 'context' => [ 'view', 'edit' ], |
|
| 68 | + ], |
|
| 69 | + ], |
|
| 70 | + ], |
|
| 71 | + ], |
|
| 72 | + ], |
|
| 73 | + ], |
|
| 74 | + 'schema' => [ $this->schema, 'get_public_item_schema' ], |
|
| 75 | + 'allow_batch' => [ 'v1' => true ], |
|
| 76 | + ]; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * Handle the request and return a valid response for this endpoint. |
|
| 81 | - * |
|
| 82 | - * @throws RouteException On error. |
|
| 83 | - * @param \WP_REST_Request $request Request object. |
|
| 84 | - * @return \WP_REST_Response |
|
| 85 | - */ |
|
| 86 | - protected function get_route_post_response( \WP_REST_Request $request ) { |
|
| 87 | - // Do not allow key to be specified during creation. |
|
| 88 | - if ( ! empty( $request['key'] ) ) { |
|
| 89 | - throw new RouteException( 'woocommerce_rest_cart_item_exists', __( 'Cannot create an existing cart item.', 'woocommerce' ), 400 ); |
|
| 90 | - } |
|
| 79 | + /** |
|
| 80 | + * Handle the request and return a valid response for this endpoint. |
|
| 81 | + * |
|
| 82 | + * @throws RouteException On error. |
|
| 83 | + * @param \WP_REST_Request $request Request object. |
|
| 84 | + * @return \WP_REST_Response |
|
| 85 | + */ |
|
| 86 | + protected function get_route_post_response( \WP_REST_Request $request ) { |
|
| 87 | + // Do not allow key to be specified during creation. |
|
| 88 | + if ( ! empty( $request['key'] ) ) { |
|
| 89 | + throw new RouteException( 'woocommerce_rest_cart_item_exists', __( 'Cannot create an existing cart item.', 'woocommerce' ), 400 ); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - $cart = $this->cart_controller->get_cart_instance(); |
|
| 93 | - $result = $this->cart_controller->add_to_cart( |
|
| 94 | - [ |
|
| 95 | - 'id' => $request['id'], |
|
| 96 | - 'quantity' => $request['quantity'], |
|
| 97 | - 'variation' => $request['variation'], |
|
| 98 | - ] |
|
| 99 | - ); |
|
| 92 | + $cart = $this->cart_controller->get_cart_instance(); |
|
| 93 | + $result = $this->cart_controller->add_to_cart( |
|
| 94 | + [ |
|
| 95 | + 'id' => $request['id'], |
|
| 96 | + 'quantity' => $request['quantity'], |
|
| 97 | + 'variation' => $request['variation'], |
|
| 98 | + ] |
|
| 99 | + ); |
|
| 100 | 100 | |
| 101 | - $response = rest_ensure_response( $this->schema->get_item_response( $cart ) ); |
|
| 102 | - $response->set_status( 201 ); |
|
| 103 | - return $response; |
|
| 104 | - } |
|
| 101 | + $response = rest_ensure_response( $this->schema->get_item_response( $cart ) ); |
|
| 102 | + $response->set_status( 201 ); |
|
| 103 | + return $response; |
|
| 104 | + } |
|
| 105 | 105 | } |
@@ -32,47 +32,47 @@ discard block |
||
| 32 | 32 | return [ |
| 33 | 33 | [ |
| 34 | 34 | 'methods' => \WP_REST_Server::CREATABLE, |
| 35 | - 'callback' => [ $this, 'get_response' ], |
|
| 35 | + 'callback' => [$this, 'get_response'], |
|
| 36 | 36 | 'permission_callback' => '__return_true', |
| 37 | 37 | 'args' => [ |
| 38 | 38 | 'id' => [ |
| 39 | - 'description' => __( 'The cart item product or variation ID.', 'woocommerce' ), |
|
| 39 | + 'description' => __('The cart item product or variation ID.', 'woocommerce'), |
|
| 40 | 40 | 'type' => 'integer', |
| 41 | - 'context' => [ 'view', 'edit' ], |
|
| 41 | + 'context' => ['view', 'edit'], |
|
| 42 | 42 | 'sanitize_callback' => 'absint', |
| 43 | 43 | ], |
| 44 | 44 | 'quantity' => [ |
| 45 | - 'description' => __( 'Quantity of this item to add to the cart.', 'woocommerce' ), |
|
| 45 | + 'description' => __('Quantity of this item to add to the cart.', 'woocommerce'), |
|
| 46 | 46 | 'type' => 'integer', |
| 47 | - 'context' => [ 'view', 'edit' ], |
|
| 47 | + 'context' => ['view', 'edit'], |
|
| 48 | 48 | 'arg_options' => [ |
| 49 | 49 | 'sanitize_callback' => 'wc_stock_amount', |
| 50 | 50 | ], |
| 51 | 51 | ], |
| 52 | 52 | 'variation' => [ |
| 53 | - 'description' => __( 'Chosen attributes (for variations).', 'woocommerce' ), |
|
| 53 | + 'description' => __('Chosen attributes (for variations).', 'woocommerce'), |
|
| 54 | 54 | 'type' => 'array', |
| 55 | - 'context' => [ 'view', 'edit' ], |
|
| 55 | + 'context' => ['view', 'edit'], |
|
| 56 | 56 | 'items' => [ |
| 57 | 57 | 'type' => 'object', |
| 58 | 58 | 'properties' => [ |
| 59 | 59 | 'attribute' => [ |
| 60 | - 'description' => __( 'Variation attribute name.', 'woocommerce' ), |
|
| 60 | + 'description' => __('Variation attribute name.', 'woocommerce'), |
|
| 61 | 61 | 'type' => 'string', |
| 62 | - 'context' => [ 'view', 'edit' ], |
|
| 62 | + 'context' => ['view', 'edit'], |
|
| 63 | 63 | ], |
| 64 | 64 | 'value' => [ |
| 65 | - 'description' => __( 'Variation attribute value.', 'woocommerce' ), |
|
| 65 | + 'description' => __('Variation attribute value.', 'woocommerce'), |
|
| 66 | 66 | 'type' => 'string', |
| 67 | - 'context' => [ 'view', 'edit' ], |
|
| 67 | + 'context' => ['view', 'edit'], |
|
| 68 | 68 | ], |
| 69 | 69 | ], |
| 70 | 70 | ], |
| 71 | 71 | ], |
| 72 | 72 | ], |
| 73 | 73 | ], |
| 74 | - 'schema' => [ $this->schema, 'get_public_item_schema' ], |
|
| 75 | - 'allow_batch' => [ 'v1' => true ], |
|
| 74 | + 'schema' => [$this->schema, 'get_public_item_schema'], |
|
| 75 | + 'allow_batch' => ['v1' => true], |
|
| 76 | 76 | ]; |
| 77 | 77 | } |
| 78 | 78 | |
@@ -83,10 +83,10 @@ discard block |
||
| 83 | 83 | * @param \WP_REST_Request $request Request object. |
| 84 | 84 | * @return \WP_REST_Response |
| 85 | 85 | */ |
| 86 | - protected function get_route_post_response( \WP_REST_Request $request ) { |
|
| 86 | + protected function get_route_post_response(\WP_REST_Request $request) { |
|
| 87 | 87 | // Do not allow key to be specified during creation. |
| 88 | - if ( ! empty( $request['key'] ) ) { |
|
| 89 | - throw new RouteException( 'woocommerce_rest_cart_item_exists', __( 'Cannot create an existing cart item.', 'woocommerce' ), 400 ); |
|
| 88 | + if (!empty($request['key'])) { |
|
| 89 | + throw new RouteException('woocommerce_rest_cart_item_exists', __('Cannot create an existing cart item.', 'woocommerce'), 400); |
|
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | $cart = $this->cart_controller->get_cart_instance(); |
@@ -98,8 +98,8 @@ discard block |
||
| 98 | 98 | ] |
| 99 | 99 | ); |
| 100 | 100 | |
| 101 | - $response = rest_ensure_response( $this->schema->get_item_response( $cart ) ); |
|
| 102 | - $response->set_status( 201 ); |
|
| 101 | + $response = rest_ensure_response($this->schema->get_item_response($cart)); |
|
| 102 | + $response->set_status(201); |
|
| 103 | 103 | return $response; |
| 104 | 104 | } |
| 105 | 105 | } |
@@ -7,72 +7,72 @@ |
||
| 7 | 7 | * ProductsById class. |
| 8 | 8 | */ |
| 9 | 9 | class ProductsById extends AbstractRoute { |
| 10 | - /** |
|
| 11 | - * The route identifier. |
|
| 12 | - * |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - const IDENTIFIER = 'products-by-id'; |
|
| 10 | + /** |
|
| 11 | + * The route identifier. |
|
| 12 | + * |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + const IDENTIFIER = 'products-by-id'; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * The routes schema. |
|
| 19 | - * |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - const SCHEMA_TYPE = 'product'; |
|
| 17 | + /** |
|
| 18 | + * The routes schema. |
|
| 19 | + * |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + const SCHEMA_TYPE = 'product'; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Get the path of this REST route. |
|
| 26 | - * |
|
| 27 | - * @return string |
|
| 28 | - */ |
|
| 29 | - public function get_path() { |
|
| 30 | - return '/products/(?P<id>[\d]+)'; |
|
| 31 | - } |
|
| 24 | + /** |
|
| 25 | + * Get the path of this REST route. |
|
| 26 | + * |
|
| 27 | + * @return string |
|
| 28 | + */ |
|
| 29 | + public function get_path() { |
|
| 30 | + return '/products/(?P<id>[\d]+)'; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Get method arguments for this REST route. |
|
| 35 | - * |
|
| 36 | - * @return array An array of endpoints. |
|
| 37 | - */ |
|
| 38 | - public function get_args() { |
|
| 39 | - return [ |
|
| 40 | - 'args' => array( |
|
| 41 | - 'id' => array( |
|
| 42 | - 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
|
| 43 | - 'type' => 'integer', |
|
| 44 | - ), |
|
| 45 | - ), |
|
| 46 | - [ |
|
| 47 | - 'methods' => \WP_REST_Server::READABLE, |
|
| 48 | - 'callback' => [ $this, 'get_response' ], |
|
| 49 | - 'permission_callback' => '__return_true', |
|
| 50 | - 'args' => array( |
|
| 51 | - 'context' => $this->get_context_param( |
|
| 52 | - array( |
|
| 53 | - 'default' => 'view', |
|
| 54 | - ) |
|
| 55 | - ), |
|
| 56 | - ), |
|
| 57 | - ], |
|
| 58 | - 'schema' => [ $this->schema, 'get_public_item_schema' ], |
|
| 59 | - ]; |
|
| 60 | - } |
|
| 33 | + /** |
|
| 34 | + * Get method arguments for this REST route. |
|
| 35 | + * |
|
| 36 | + * @return array An array of endpoints. |
|
| 37 | + */ |
|
| 38 | + public function get_args() { |
|
| 39 | + return [ |
|
| 40 | + 'args' => array( |
|
| 41 | + 'id' => array( |
|
| 42 | + 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
|
| 43 | + 'type' => 'integer', |
|
| 44 | + ), |
|
| 45 | + ), |
|
| 46 | + [ |
|
| 47 | + 'methods' => \WP_REST_Server::READABLE, |
|
| 48 | + 'callback' => [ $this, 'get_response' ], |
|
| 49 | + 'permission_callback' => '__return_true', |
|
| 50 | + 'args' => array( |
|
| 51 | + 'context' => $this->get_context_param( |
|
| 52 | + array( |
|
| 53 | + 'default' => 'view', |
|
| 54 | + ) |
|
| 55 | + ), |
|
| 56 | + ), |
|
| 57 | + ], |
|
| 58 | + 'schema' => [ $this->schema, 'get_public_item_schema' ], |
|
| 59 | + ]; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Get a single item. |
|
| 64 | - * |
|
| 65 | - * @throws RouteException On error. |
|
| 66 | - * @param \WP_REST_Request $request Request object. |
|
| 67 | - * @return \WP_REST_Response |
|
| 68 | - */ |
|
| 69 | - protected function get_route_response( \WP_REST_Request $request ) { |
|
| 70 | - $object = wc_get_product( (int) $request['id'] ); |
|
| 62 | + /** |
|
| 63 | + * Get a single item. |
|
| 64 | + * |
|
| 65 | + * @throws RouteException On error. |
|
| 66 | + * @param \WP_REST_Request $request Request object. |
|
| 67 | + * @return \WP_REST_Response |
|
| 68 | + */ |
|
| 69 | + protected function get_route_response( \WP_REST_Request $request ) { |
|
| 70 | + $object = wc_get_product( (int) $request['id'] ); |
|
| 71 | 71 | |
| 72 | - if ( ! $object || 0 === $object->get_id() ) { |
|
| 73 | - throw new RouteException( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), 404 ); |
|
| 74 | - } |
|
| 72 | + if ( ! $object || 0 === $object->get_id() ) { |
|
| 73 | + throw new RouteException( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), 404 ); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - return rest_ensure_response( $this->schema->get_item_response( $object ) ); |
|
| 77 | - } |
|
| 76 | + return rest_ensure_response( $this->schema->get_item_response( $object ) ); |
|
| 77 | + } |
|
| 78 | 78 | } |
@@ -39,13 +39,13 @@ discard block |
||
| 39 | 39 | return [ |
| 40 | 40 | 'args' => array( |
| 41 | 41 | 'id' => array( |
| 42 | - 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
|
| 42 | + 'description' => __('Unique identifier for the resource.', 'woocommerce'), |
|
| 43 | 43 | 'type' => 'integer', |
| 44 | 44 | ), |
| 45 | 45 | ), |
| 46 | 46 | [ |
| 47 | 47 | 'methods' => \WP_REST_Server::READABLE, |
| 48 | - 'callback' => [ $this, 'get_response' ], |
|
| 48 | + 'callback' => [$this, 'get_response'], |
|
| 49 | 49 | 'permission_callback' => '__return_true', |
| 50 | 50 | 'args' => array( |
| 51 | 51 | 'context' => $this->get_context_param( |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | ), |
| 56 | 56 | ), |
| 57 | 57 | ], |
| 58 | - 'schema' => [ $this->schema, 'get_public_item_schema' ], |
|
| 58 | + 'schema' => [$this->schema, 'get_public_item_schema'], |
|
| 59 | 59 | ]; |
| 60 | 60 | } |
| 61 | 61 | |
@@ -66,13 +66,13 @@ discard block |
||
| 66 | 66 | * @param \WP_REST_Request $request Request object. |
| 67 | 67 | * @return \WP_REST_Response |
| 68 | 68 | */ |
| 69 | - protected function get_route_response( \WP_REST_Request $request ) { |
|
| 70 | - $object = wc_get_product( (int) $request['id'] ); |
|
| 69 | + protected function get_route_response(\WP_REST_Request $request) { |
|
| 70 | + $object = wc_get_product((int) $request['id']); |
|
| 71 | 71 | |
| 72 | - if ( ! $object || 0 === $object->get_id() ) { |
|
| 73 | - throw new RouteException( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), 404 ); |
|
| 72 | + if (!$object || 0 === $object->get_id()) { |
|
| 73 | + throw new RouteException('woocommerce_rest_product_invalid_id', __('Invalid product ID.', 'woocommerce'), 404); |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | - return rest_ensure_response( $this->schema->get_item_response( $object ) ); |
|
| 76 | + return rest_ensure_response($this->schema->get_item_response($object)); |
|
| 77 | 77 | } |
| 78 | 78 | } |
@@ -16,86 +16,86 @@ |
||
| 16 | 16 | * StoreApi Main Class. |
| 17 | 17 | */ |
| 18 | 18 | final class StoreApi { |
| 19 | - /** |
|
| 20 | - * Init and hook in Store API functionality. |
|
| 21 | - */ |
|
| 22 | - public function init() { |
|
| 23 | - add_action( |
|
| 24 | - 'rest_api_init', |
|
| 25 | - function() { |
|
| 26 | - self::container()->get( Authentication::class )->init(); |
|
| 27 | - self::container()->get( Legacy::class )->init(); |
|
| 28 | - self::container()->get( RoutesController::class )->register_all_routes(); |
|
| 29 | - } |
|
| 30 | - ); |
|
| 31 | - } |
|
| 19 | + /** |
|
| 20 | + * Init and hook in Store API functionality. |
|
| 21 | + */ |
|
| 22 | + public function init() { |
|
| 23 | + add_action( |
|
| 24 | + 'rest_api_init', |
|
| 25 | + function() { |
|
| 26 | + self::container()->get( Authentication::class )->init(); |
|
| 27 | + self::container()->get( Legacy::class )->init(); |
|
| 28 | + self::container()->get( RoutesController::class )->register_all_routes(); |
|
| 29 | + } |
|
| 30 | + ); |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Loads the DI container for Store API. |
|
| 35 | - * |
|
| 36 | - * @internal This uses the Blocks DI container. If Store API were to move to core, this container could be replaced |
|
| 37 | - * with a different compatible container. |
|
| 38 | - * |
|
| 39 | - * @param boolean $reset Used to reset the container to a fresh instance. Note: this means all dependencies will be reconstructed. |
|
| 40 | - * @return mixed |
|
| 41 | - */ |
|
| 42 | - public static function container( $reset = false ) { |
|
| 43 | - static $container; |
|
| 33 | + /** |
|
| 34 | + * Loads the DI container for Store API. |
|
| 35 | + * |
|
| 36 | + * @internal This uses the Blocks DI container. If Store API were to move to core, this container could be replaced |
|
| 37 | + * with a different compatible container. |
|
| 38 | + * |
|
| 39 | + * @param boolean $reset Used to reset the container to a fresh instance. Note: this means all dependencies will be reconstructed. |
|
| 40 | + * @return mixed |
|
| 41 | + */ |
|
| 42 | + public static function container( $reset = false ) { |
|
| 43 | + static $container; |
|
| 44 | 44 | |
| 45 | - if ( $reset ) { |
|
| 46 | - $container = null; |
|
| 47 | - } |
|
| 45 | + if ( $reset ) { |
|
| 46 | + $container = null; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found |
|
| 50 | - return $container = $container ?: ( function() { |
|
| 51 | - $container = new Container(); |
|
| 52 | - $container->register( |
|
| 53 | - Authentication::class, |
|
| 54 | - function () { |
|
| 55 | - return new Authentication(); |
|
| 56 | - } |
|
| 57 | - ); |
|
| 58 | - $container->register( |
|
| 59 | - Legacy::class, |
|
| 60 | - function () { |
|
| 61 | - return new Legacy(); |
|
| 62 | - } |
|
| 63 | - ); |
|
| 64 | - $container->register( |
|
| 65 | - RoutesController::class, |
|
| 66 | - function ( $container ) { |
|
| 67 | - return new RoutesController( |
|
| 68 | - $container->get( SchemaController::class ) |
|
| 69 | - ); |
|
| 70 | - } |
|
| 71 | - ); |
|
| 72 | - $container->register( |
|
| 73 | - SchemaController::class, |
|
| 74 | - function ( $container ) { |
|
| 75 | - return new SchemaController( |
|
| 76 | - $container->get( ExtendSchema::class ) |
|
| 77 | - ); |
|
| 78 | - } |
|
| 79 | - ); |
|
| 80 | - $container->register( |
|
| 81 | - ExtendSchema::class, |
|
| 82 | - function ( $container ) { |
|
| 83 | - return new ExtendSchema( |
|
| 84 | - $container->get( Formatters::class ) |
|
| 85 | - ); |
|
| 86 | - } |
|
| 87 | - ); |
|
| 88 | - $container->register( |
|
| 89 | - Formatters::class, |
|
| 90 | - function ( $container ) { |
|
| 91 | - $formatters = new Formatters(); |
|
| 92 | - $formatters->register( 'money', MoneyFormatter::class ); |
|
| 93 | - $formatters->register( 'html', HtmlFormatter::class ); |
|
| 94 | - $formatters->register( 'currency', CurrencyFormatter::class ); |
|
| 95 | - return $formatters; |
|
| 96 | - } |
|
| 97 | - ); |
|
| 98 | - return $container; |
|
| 99 | - } )(); |
|
| 100 | - } |
|
| 49 | + // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found |
|
| 50 | + return $container = $container ?: ( function() { |
|
| 51 | + $container = new Container(); |
|
| 52 | + $container->register( |
|
| 53 | + Authentication::class, |
|
| 54 | + function () { |
|
| 55 | + return new Authentication(); |
|
| 56 | + } |
|
| 57 | + ); |
|
| 58 | + $container->register( |
|
| 59 | + Legacy::class, |
|
| 60 | + function () { |
|
| 61 | + return new Legacy(); |
|
| 62 | + } |
|
| 63 | + ); |
|
| 64 | + $container->register( |
|
| 65 | + RoutesController::class, |
|
| 66 | + function ( $container ) { |
|
| 67 | + return new RoutesController( |
|
| 68 | + $container->get( SchemaController::class ) |
|
| 69 | + ); |
|
| 70 | + } |
|
| 71 | + ); |
|
| 72 | + $container->register( |
|
| 73 | + SchemaController::class, |
|
| 74 | + function ( $container ) { |
|
| 75 | + return new SchemaController( |
|
| 76 | + $container->get( ExtendSchema::class ) |
|
| 77 | + ); |
|
| 78 | + } |
|
| 79 | + ); |
|
| 80 | + $container->register( |
|
| 81 | + ExtendSchema::class, |
|
| 82 | + function ( $container ) { |
|
| 83 | + return new ExtendSchema( |
|
| 84 | + $container->get( Formatters::class ) |
|
| 85 | + ); |
|
| 86 | + } |
|
| 87 | + ); |
|
| 88 | + $container->register( |
|
| 89 | + Formatters::class, |
|
| 90 | + function ( $container ) { |
|
| 91 | + $formatters = new Formatters(); |
|
| 92 | + $formatters->register( 'money', MoneyFormatter::class ); |
|
| 93 | + $formatters->register( 'html', HtmlFormatter::class ); |
|
| 94 | + $formatters->register( 'currency', CurrencyFormatter::class ); |
|
| 95 | + return $formatters; |
|
| 96 | + } |
|
| 97 | + ); |
|
| 98 | + return $container; |
|
| 99 | + } )(); |
|
| 100 | + } |
|
| 101 | 101 | } |
@@ -23,9 +23,9 @@ discard block |
||
| 23 | 23 | add_action( |
| 24 | 24 | 'rest_api_init', |
| 25 | 25 | function() { |
| 26 | - self::container()->get( Authentication::class )->init(); |
|
| 27 | - self::container()->get( Legacy::class )->init(); |
|
| 28 | - self::container()->get( RoutesController::class )->register_all_routes(); |
|
| 26 | + self::container()->get(Authentication::class)->init(); |
|
| 27 | + self::container()->get(Legacy::class)->init(); |
|
| 28 | + self::container()->get(RoutesController::class)->register_all_routes(); |
|
| 29 | 29 | } |
| 30 | 30 | ); |
| 31 | 31 | } |
@@ -39,59 +39,59 @@ discard block |
||
| 39 | 39 | * @param boolean $reset Used to reset the container to a fresh instance. Note: this means all dependencies will be reconstructed. |
| 40 | 40 | * @return mixed |
| 41 | 41 | */ |
| 42 | - public static function container( $reset = false ) { |
|
| 42 | + public static function container($reset = false) { |
|
| 43 | 43 | static $container; |
| 44 | 44 | |
| 45 | - if ( $reset ) { |
|
| 45 | + if ($reset) { |
|
| 46 | 46 | $container = null; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found |
| 50 | - return $container = $container ?: ( function() { |
|
| 50 | + return $container = $container ?: (function() { |
|
| 51 | 51 | $container = new Container(); |
| 52 | 52 | $container->register( |
| 53 | 53 | Authentication::class, |
| 54 | - function () { |
|
| 54 | + function() { |
|
| 55 | 55 | return new Authentication(); |
| 56 | 56 | } |
| 57 | 57 | ); |
| 58 | 58 | $container->register( |
| 59 | 59 | Legacy::class, |
| 60 | - function () { |
|
| 60 | + function() { |
|
| 61 | 61 | return new Legacy(); |
| 62 | 62 | } |
| 63 | 63 | ); |
| 64 | 64 | $container->register( |
| 65 | 65 | RoutesController::class, |
| 66 | - function ( $container ) { |
|
| 66 | + function($container) { |
|
| 67 | 67 | return new RoutesController( |
| 68 | - $container->get( SchemaController::class ) |
|
| 68 | + $container->get(SchemaController::class) |
|
| 69 | 69 | ); |
| 70 | 70 | } |
| 71 | 71 | ); |
| 72 | 72 | $container->register( |
| 73 | 73 | SchemaController::class, |
| 74 | - function ( $container ) { |
|
| 74 | + function($container) { |
|
| 75 | 75 | return new SchemaController( |
| 76 | - $container->get( ExtendSchema::class ) |
|
| 76 | + $container->get(ExtendSchema::class) |
|
| 77 | 77 | ); |
| 78 | 78 | } |
| 79 | 79 | ); |
| 80 | 80 | $container->register( |
| 81 | 81 | ExtendSchema::class, |
| 82 | - function ( $container ) { |
|
| 82 | + function($container) { |
|
| 83 | 83 | return new ExtendSchema( |
| 84 | - $container->get( Formatters::class ) |
|
| 84 | + $container->get(Formatters::class) |
|
| 85 | 85 | ); |
| 86 | 86 | } |
| 87 | 87 | ); |
| 88 | 88 | $container->register( |
| 89 | 89 | Formatters::class, |
| 90 | - function ( $container ) { |
|
| 90 | + function($container) { |
|
| 91 | 91 | $formatters = new Formatters(); |
| 92 | - $formatters->register( 'money', MoneyFormatter::class ); |
|
| 93 | - $formatters->register( 'html', HtmlFormatter::class ); |
|
| 94 | - $formatters->register( 'currency', CurrencyFormatter::class ); |
|
| 92 | + $formatters->register('money', MoneyFormatter::class); |
|
| 93 | + $formatters->register('html', HtmlFormatter::class); |
|
| 94 | + $formatters->register('currency', CurrencyFormatter::class); |
|
| 95 | 95 | return $formatters; |
| 96 | 96 | } |
| 97 | 97 | ); |
@@ -10,78 +10,78 @@ |
||
| 10 | 10 | |
| 11 | 11 | if ( ! function_exists( 'woocommerce_store_api_register_endpoint_data' ) ) { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * Register endpoint data under a specified namespace. |
|
| 15 | - * |
|
| 16 | - * @see Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema::register_endpoint_data() |
|
| 17 | - * |
|
| 18 | - * @param array $args Args to pass to register_endpoint_data. |
|
| 19 | - * @returns boolean|\WP_Error True on success, WP_Error on fail. |
|
| 20 | - */ |
|
| 21 | - function woocommerce_store_api_register_endpoint_data( $args ) { |
|
| 22 | - try { |
|
| 23 | - $extend = StoreApi::container()->get( ExtendSchema::class ); |
|
| 24 | - $extend->register_endpoint_data( $args ); |
|
| 25 | - } catch ( \Exception $error ) { |
|
| 26 | - return new \WP_Error( 'error', $error->getMessage() ); |
|
| 27 | - } |
|
| 28 | - return true; |
|
| 29 | - } |
|
| 13 | + /** |
|
| 14 | + * Register endpoint data under a specified namespace. |
|
| 15 | + * |
|
| 16 | + * @see Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema::register_endpoint_data() |
|
| 17 | + * |
|
| 18 | + * @param array $args Args to pass to register_endpoint_data. |
|
| 19 | + * @returns boolean|\WP_Error True on success, WP_Error on fail. |
|
| 20 | + */ |
|
| 21 | + function woocommerce_store_api_register_endpoint_data( $args ) { |
|
| 22 | + try { |
|
| 23 | + $extend = StoreApi::container()->get( ExtendSchema::class ); |
|
| 24 | + $extend->register_endpoint_data( $args ); |
|
| 25 | + } catch ( \Exception $error ) { |
|
| 26 | + return new \WP_Error( 'error', $error->getMessage() ); |
|
| 27 | + } |
|
| 28 | + return true; |
|
| 29 | + } |
|
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | if ( ! function_exists( 'woocommerce_store_api_register_update_callback' ) ) { |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Add callback functions that can be executed by the cart/extensions endpoint. |
|
| 36 | - * |
|
| 37 | - * @see Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema::register_update_callback() |
|
| 38 | - * |
|
| 39 | - * @param array $args Args to pass to register_update_callback. |
|
| 40 | - * @returns boolean|\WP_Error True on success, WP_Error on fail. |
|
| 41 | - */ |
|
| 42 | - function woocommerce_store_api_register_update_callback( $args ) { |
|
| 43 | - try { |
|
| 44 | - $extend = StoreApi::container()->get( ExtendSchema::class ); |
|
| 45 | - $extend->register_update_callback( $args ); |
|
| 46 | - } catch ( \Exception $error ) { |
|
| 47 | - return new \WP_Error( 'error', $error->getMessage() ); |
|
| 48 | - } |
|
| 49 | - return true; |
|
| 50 | - } |
|
| 34 | + /** |
|
| 35 | + * Add callback functions that can be executed by the cart/extensions endpoint. |
|
| 36 | + * |
|
| 37 | + * @see Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema::register_update_callback() |
|
| 38 | + * |
|
| 39 | + * @param array $args Args to pass to register_update_callback. |
|
| 40 | + * @returns boolean|\WP_Error True on success, WP_Error on fail. |
|
| 41 | + */ |
|
| 42 | + function woocommerce_store_api_register_update_callback( $args ) { |
|
| 43 | + try { |
|
| 44 | + $extend = StoreApi::container()->get( ExtendSchema::class ); |
|
| 45 | + $extend->register_update_callback( $args ); |
|
| 46 | + } catch ( \Exception $error ) { |
|
| 47 | + return new \WP_Error( 'error', $error->getMessage() ); |
|
| 48 | + } |
|
| 49 | + return true; |
|
| 50 | + } |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | if ( ! function_exists( 'woocommerce_store_api_register_payment_requirements' ) ) { |
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Registers and validates payment requirements callbacks. |
|
| 57 | - * |
|
| 58 | - * @see Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema::register_payment_requirements() |
|
| 59 | - * |
|
| 60 | - * @param array $args Args to pass to register_payment_requirements. |
|
| 61 | - * @returns boolean|\WP_Error True on success, WP_Error on fail. |
|
| 62 | - */ |
|
| 63 | - function woocommerce_store_api_register_payment_requirements( $args ) { |
|
| 64 | - try { |
|
| 65 | - $extend = StoreApi::container()->get( ExtendSchema::class ); |
|
| 66 | - $extend->register_payment_requirements( $args ); |
|
| 67 | - } catch ( \Exception $error ) { |
|
| 68 | - return new \WP_Error( 'error', $error->getMessage() ); |
|
| 69 | - } |
|
| 70 | - return true; |
|
| 71 | - } |
|
| 55 | + /** |
|
| 56 | + * Registers and validates payment requirements callbacks. |
|
| 57 | + * |
|
| 58 | + * @see Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema::register_payment_requirements() |
|
| 59 | + * |
|
| 60 | + * @param array $args Args to pass to register_payment_requirements. |
|
| 61 | + * @returns boolean|\WP_Error True on success, WP_Error on fail. |
|
| 62 | + */ |
|
| 63 | + function woocommerce_store_api_register_payment_requirements( $args ) { |
|
| 64 | + try { |
|
| 65 | + $extend = StoreApi::container()->get( ExtendSchema::class ); |
|
| 66 | + $extend->register_payment_requirements( $args ); |
|
| 67 | + } catch ( \Exception $error ) { |
|
| 68 | + return new \WP_Error( 'error', $error->getMessage() ); |
|
| 69 | + } |
|
| 70 | + return true; |
|
| 71 | + } |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | if ( ! function_exists( 'woocommerce_store_api_get_formatter' ) ) { |
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * Returns a formatter instance. |
|
| 78 | - * |
|
| 79 | - * @see Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema::get_formatter() |
|
| 80 | - * |
|
| 81 | - * @param string $name Formatter name. |
|
| 82 | - * @return Automattic\WooCommerce\StoreApi\Formatters\FormatterInterface |
|
| 83 | - */ |
|
| 84 | - function woocommerce_store_api_get_formatter( $name ) { |
|
| 85 | - return StoreApi::container()->get( ExtendSchema::class )->get_formatter( $name ); |
|
| 86 | - } |
|
| 76 | + /** |
|
| 77 | + * Returns a formatter instance. |
|
| 78 | + * |
|
| 79 | + * @see Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema::get_formatter() |
|
| 80 | + * |
|
| 81 | + * @param string $name Formatter name. |
|
| 82 | + * @return Automattic\WooCommerce\StoreApi\Formatters\FormatterInterface |
|
| 83 | + */ |
|
| 84 | + function woocommerce_store_api_get_formatter( $name ) { |
|
| 85 | + return StoreApi::container()->get( ExtendSchema::class )->get_formatter( $name ); |
|
| 86 | + } |
|
| 87 | 87 | } |
@@ -8,7 +8,7 @@ discard block |
||
| 8 | 8 | use Automattic\WooCommerce\StoreApi\StoreApi; |
| 9 | 9 | use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema; |
| 10 | 10 | |
| 11 | -if ( ! function_exists( 'woocommerce_store_api_register_endpoint_data' ) ) { |
|
| 11 | +if (!function_exists('woocommerce_store_api_register_endpoint_data')) { |
|
| 12 | 12 | |
| 13 | 13 | /** |
| 14 | 14 | * Register endpoint data under a specified namespace. |
@@ -18,18 +18,18 @@ discard block |
||
| 18 | 18 | * @param array $args Args to pass to register_endpoint_data. |
| 19 | 19 | * @returns boolean|\WP_Error True on success, WP_Error on fail. |
| 20 | 20 | */ |
| 21 | - function woocommerce_store_api_register_endpoint_data( $args ) { |
|
| 21 | + function woocommerce_store_api_register_endpoint_data($args) { |
|
| 22 | 22 | try { |
| 23 | - $extend = StoreApi::container()->get( ExtendSchema::class ); |
|
| 24 | - $extend->register_endpoint_data( $args ); |
|
| 25 | - } catch ( \Exception $error ) { |
|
| 26 | - return new \WP_Error( 'error', $error->getMessage() ); |
|
| 23 | + $extend = StoreApi::container()->get(ExtendSchema::class); |
|
| 24 | + $extend->register_endpoint_data($args); |
|
| 25 | + } catch (\Exception $error) { |
|
| 26 | + return new \WP_Error('error', $error->getMessage()); |
|
| 27 | 27 | } |
| 28 | 28 | return true; |
| 29 | 29 | } |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | -if ( ! function_exists( 'woocommerce_store_api_register_update_callback' ) ) { |
|
| 32 | +if (!function_exists('woocommerce_store_api_register_update_callback')) { |
|
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | 35 | * Add callback functions that can be executed by the cart/extensions endpoint. |
@@ -39,18 +39,18 @@ discard block |
||
| 39 | 39 | * @param array $args Args to pass to register_update_callback. |
| 40 | 40 | * @returns boolean|\WP_Error True on success, WP_Error on fail. |
| 41 | 41 | */ |
| 42 | - function woocommerce_store_api_register_update_callback( $args ) { |
|
| 42 | + function woocommerce_store_api_register_update_callback($args) { |
|
| 43 | 43 | try { |
| 44 | - $extend = StoreApi::container()->get( ExtendSchema::class ); |
|
| 45 | - $extend->register_update_callback( $args ); |
|
| 46 | - } catch ( \Exception $error ) { |
|
| 47 | - return new \WP_Error( 'error', $error->getMessage() ); |
|
| 44 | + $extend = StoreApi::container()->get(ExtendSchema::class); |
|
| 45 | + $extend->register_update_callback($args); |
|
| 46 | + } catch (\Exception $error) { |
|
| 47 | + return new \WP_Error('error', $error->getMessage()); |
|
| 48 | 48 | } |
| 49 | 49 | return true; |
| 50 | 50 | } |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | -if ( ! function_exists( 'woocommerce_store_api_register_payment_requirements' ) ) { |
|
| 53 | +if (!function_exists('woocommerce_store_api_register_payment_requirements')) { |
|
| 54 | 54 | |
| 55 | 55 | /** |
| 56 | 56 | * Registers and validates payment requirements callbacks. |
@@ -60,18 +60,18 @@ discard block |
||
| 60 | 60 | * @param array $args Args to pass to register_payment_requirements. |
| 61 | 61 | * @returns boolean|\WP_Error True on success, WP_Error on fail. |
| 62 | 62 | */ |
| 63 | - function woocommerce_store_api_register_payment_requirements( $args ) { |
|
| 63 | + function woocommerce_store_api_register_payment_requirements($args) { |
|
| 64 | 64 | try { |
| 65 | - $extend = StoreApi::container()->get( ExtendSchema::class ); |
|
| 66 | - $extend->register_payment_requirements( $args ); |
|
| 67 | - } catch ( \Exception $error ) { |
|
| 68 | - return new \WP_Error( 'error', $error->getMessage() ); |
|
| 65 | + $extend = StoreApi::container()->get(ExtendSchema::class); |
|
| 66 | + $extend->register_payment_requirements($args); |
|
| 67 | + } catch (\Exception $error) { |
|
| 68 | + return new \WP_Error('error', $error->getMessage()); |
|
| 69 | 69 | } |
| 70 | 70 | return true; |
| 71 | 71 | } |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | -if ( ! function_exists( 'woocommerce_store_api_get_formatter' ) ) { |
|
| 74 | +if (!function_exists('woocommerce_store_api_get_formatter')) { |
|
| 75 | 75 | |
| 76 | 76 | /** |
| 77 | 77 | * Returns a formatter instance. |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | * @param string $name Formatter name. |
| 82 | 82 | * @return Automattic\WooCommerce\StoreApi\Formatters\FormatterInterface |
| 83 | 83 | */ |
| 84 | - function woocommerce_store_api_get_formatter( $name ) { |
|
| 85 | - return StoreApi::container()->get( ExtendSchema::class )->get_formatter( $name ); |
|
| 84 | + function woocommerce_store_api_get_formatter($name) { |
|
| 85 | + return StoreApi::container()->get(ExtendSchema::class)->get_formatter($name); |
|
| 86 | 86 | } |
| 87 | 87 | } |