Passed
Push — main ( aaef5c...e4c121 )
by TARIQ
71:39
created
packages/woocommerce-blocks/src/StoreApi/Routes/V1/CartUpdateItem.php 2 patches
Indentation   +54 added lines, -54 removed lines patch added patch discarded remove patch
@@ -5,63 +5,63 @@
 block discarded – undo
5 5
  * CartUpdateItem class.
6 6
  */
7 7
 class CartUpdateItem extends AbstractCartRoute {
8
-	/**
9
-	 * The route identifier.
10
-	 *
11
-	 * @var string
12
-	 */
13
-	const IDENTIFIER = 'cart-update-item';
8
+    /**
9
+     * The route identifier.
10
+     *
11
+     * @var string
12
+     */
13
+    const IDENTIFIER = 'cart-update-item';
14 14
 
15
-	/**
16
-	 * Get the path of this REST route.
17
-	 *
18
-	 * @return string
19
-	 */
20
-	public function get_path() {
21
-		return '/cart/update-item';
22
-	}
15
+    /**
16
+     * Get the path of this REST route.
17
+     *
18
+     * @return string
19
+     */
20
+    public function get_path() {
21
+        return '/cart/update-item';
22
+    }
23 23
 
24
-	/**
25
-	 * Get method arguments for this REST route.
26
-	 *
27
-	 * @return array An array of endpoints.
28
-	 */
29
-	public function get_args() {
30
-		return [
31
-			[
32
-				'methods'             => \WP_REST_Server::CREATABLE,
33
-				'callback'            => [ $this, 'get_response' ],
34
-				'permission_callback' => '__return_true',
35
-				'args'                => [
36
-					'key'      => [
37
-						'description' => __( 'Unique identifier (key) for the cart item to update.', 'woocommerce' ),
38
-						'type'        => 'string',
39
-					],
40
-					'quantity' => [
41
-						'description' => __( 'New quantity of the item in the cart.', 'woocommerce' ),
42
-						'type'        => 'integer',
43
-					],
44
-				],
45
-			],
46
-			'schema'      => [ $this->schema, 'get_public_item_schema' ],
47
-			'allow_batch' => [ 'v1' => true ],
48
-		];
49
-	}
24
+    /**
25
+     * Get method arguments for this REST route.
26
+     *
27
+     * @return array An array of endpoints.
28
+     */
29
+    public function get_args() {
30
+        return [
31
+            [
32
+                'methods'             => \WP_REST_Server::CREATABLE,
33
+                'callback'            => [ $this, 'get_response' ],
34
+                'permission_callback' => '__return_true',
35
+                'args'                => [
36
+                    'key'      => [
37
+                        'description' => __( 'Unique identifier (key) for the cart item to update.', 'woocommerce' ),
38
+                        'type'        => 'string',
39
+                    ],
40
+                    'quantity' => [
41
+                        'description' => __( 'New quantity of the item in the cart.', 'woocommerce' ),
42
+                        'type'        => 'integer',
43
+                    ],
44
+                ],
45
+            ],
46
+            'schema'      => [ $this->schema, 'get_public_item_schema' ],
47
+            'allow_batch' => [ 'v1' => true ],
48
+        ];
49
+    }
50 50
 
51
-	/**
52
-	 * Handle the request and return a valid response for this endpoint.
53
-	 * .
54
-	 *
55
-	 * @param \WP_REST_Request $request Request object.
56
-	 * @return \WP_REST_Response
57
-	 */
58
-	protected function get_route_post_response( \WP_REST_Request $request ) {
59
-		$cart = $this->cart_controller->get_cart_instance();
51
+    /**
52
+     * Handle the request and return a valid response for this endpoint.
53
+     * .
54
+     *
55
+     * @param \WP_REST_Request $request Request object.
56
+     * @return \WP_REST_Response
57
+     */
58
+    protected function get_route_post_response( \WP_REST_Request $request ) {
59
+        $cart = $this->cart_controller->get_cart_instance();
60 60
 
61
-		if ( isset( $request['quantity'] ) ) {
62
-			$this->cart_controller->set_cart_item_quantity( $request['key'], $request['quantity'] );
63
-		}
61
+        if ( isset( $request['quantity'] ) ) {
62
+            $this->cart_controller->set_cart_item_quantity( $request['key'], $request['quantity'] );
63
+        }
64 64
 
65
-		return rest_ensure_response( $this->schema->get_item_response( $cart ) );
66
-	}
65
+        return rest_ensure_response( $this->schema->get_item_response( $cart ) );
66
+    }
67 67
 }
Please login to merge, or discard this patch.
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -30,21 +30,21 @@  discard block
 block discarded – undo
30 30
 		return [
31 31
 			[
32 32
 				'methods'             => \WP_REST_Server::CREATABLE,
33
-				'callback'            => [ $this, 'get_response' ],
33
+				'callback'            => [$this, 'get_response'],
34 34
 				'permission_callback' => '__return_true',
35 35
 				'args'                => [
36 36
 					'key'      => [
37
-						'description' => __( 'Unique identifier (key) for the cart item to update.', 'woocommerce' ),
37
+						'description' => __('Unique identifier (key) for the cart item to update.', 'woocommerce'),
38 38
 						'type'        => 'string',
39 39
 					],
40 40
 					'quantity' => [
41
-						'description' => __( 'New quantity of the item in the cart.', 'woocommerce' ),
41
+						'description' => __('New quantity of the item in the cart.', 'woocommerce'),
42 42
 						'type'        => 'integer',
43 43
 					],
44 44
 				],
45 45
 			],
46
-			'schema'      => [ $this->schema, 'get_public_item_schema' ],
47
-			'allow_batch' => [ 'v1' => true ],
46
+			'schema'      => [$this->schema, 'get_public_item_schema'],
47
+			'allow_batch' => ['v1' => true],
48 48
 		];
49 49
 	}
50 50
 
@@ -55,13 +55,13 @@  discard block
 block discarded – undo
55 55
 	 * @param \WP_REST_Request $request Request object.
56 56
 	 * @return \WP_REST_Response
57 57
 	 */
58
-	protected function get_route_post_response( \WP_REST_Request $request ) {
58
+	protected function get_route_post_response(\WP_REST_Request $request) {
59 59
 		$cart = $this->cart_controller->get_cart_instance();
60 60
 
61
-		if ( isset( $request['quantity'] ) ) {
62
-			$this->cart_controller->set_cart_item_quantity( $request['key'], $request['quantity'] );
61
+		if (isset($request['quantity'])) {
62
+			$this->cart_controller->set_cart_item_quantity($request['key'], $request['quantity']);
63 63
 		}
64 64
 
65
-		return rest_ensure_response( $this->schema->get_item_response( $cart ) );
65
+		return rest_ensure_response($this->schema->get_item_response($cart));
66 66
 	}
67 67
 }
Please login to merge, or discard this patch.
woocommerce-blocks/src/StoreApi/Routes/V1/ProductAttributeTerms.php 2 patches
Indentation   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -7,59 +7,59 @@
 block discarded – undo
7 7
  * ProductAttributeTerms class.
8 8
  */
9 9
 class ProductAttributeTerms extends AbstractTermsRoute {
10
-	/**
11
-	 * The route identifier.
12
-	 *
13
-	 * @var string
14
-	 */
15
-	const IDENTIFIER = 'product-attribute-terms';
10
+    /**
11
+     * The route identifier.
12
+     *
13
+     * @var string
14
+     */
15
+    const IDENTIFIER = 'product-attribute-terms';
16 16
 
17
-	/**
18
-	 * Get the path of this REST route.
19
-	 *
20
-	 * @return string
21
-	 */
22
-	public function get_path() {
23
-		return '/products/attributes/(?P<attribute_id>[\d]+)/terms';
24
-	}
17
+    /**
18
+     * Get the path of this REST route.
19
+     *
20
+     * @return string
21
+     */
22
+    public function get_path() {
23
+        return '/products/attributes/(?P<attribute_id>[\d]+)/terms';
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
-			'args'   => array(
34
-				'attribute_id' => array(
35
-					'description' => __( 'Unique identifier for the attribute.', 'woocommerce' ),
36
-					'type'        => 'integer',
37
-				),
38
-			),
39
-			[
40
-				'methods'             => \WP_REST_Server::READABLE,
41
-				'callback'            => [ $this, 'get_response' ],
42
-				'permission_callback' => '__return_true',
43
-				'args'                => $this->get_collection_params(),
44
-			],
45
-			'schema' => [ $this->schema, 'get_public_item_schema' ],
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
+            'args'   => array(
34
+                'attribute_id' => array(
35
+                    'description' => __( 'Unique identifier for the attribute.', 'woocommerce' ),
36
+                    'type'        => 'integer',
37
+                ),
38
+            ),
39
+            [
40
+                'methods'             => \WP_REST_Server::READABLE,
41
+                'callback'            => [ $this, 'get_response' ],
42
+                'permission_callback' => '__return_true',
43
+                'args'                => $this->get_collection_params(),
44
+            ],
45
+            'schema' => [ $this->schema, 'get_public_item_schema' ],
46
+        ];
47
+    }
48 48
 
49
-	/**
50
-	 * Get a collection of attribute terms.
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_response( \WP_REST_Request $request ) {
57
-		$attribute = wc_get_attribute( $request['attribute_id'] );
49
+    /**
50
+     * Get a collection of attribute terms.
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_response( \WP_REST_Request $request ) {
57
+        $attribute = wc_get_attribute( $request['attribute_id'] );
58 58
 
59
-		if ( ! $attribute || ! taxonomy_exists( $attribute->slug ) ) {
60
-			throw new RouteException( 'woocommerce_rest_taxonomy_invalid', __( 'Attribute does not exist.', 'woocommerce' ), 404 );
61
-		}
59
+        if ( ! $attribute || ! taxonomy_exists( $attribute->slug ) ) {
60
+            throw new RouteException( 'woocommerce_rest_taxonomy_invalid', __( 'Attribute does not exist.', 'woocommerce' ), 404 );
61
+        }
62 62
 
63
-		return $this->get_terms_response( $attribute->slug, $request );
64
-	}
63
+        return $this->get_terms_response( $attribute->slug, $request );
64
+    }
65 65
 }
Please login to merge, or discard this patch.
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -32,17 +32,17 @@  discard block
 block discarded – undo
32 32
 		return [
33 33
 			'args'   => array(
34 34
 				'attribute_id' => array(
35
-					'description' => __( 'Unique identifier for the attribute.', 'woocommerce' ),
35
+					'description' => __('Unique identifier for the attribute.', 'woocommerce'),
36 36
 					'type'        => 'integer',
37 37
 				),
38 38
 			),
39 39
 			[
40 40
 				'methods'             => \WP_REST_Server::READABLE,
41
-				'callback'            => [ $this, 'get_response' ],
41
+				'callback'            => [$this, 'get_response'],
42 42
 				'permission_callback' => '__return_true',
43 43
 				'args'                => $this->get_collection_params(),
44 44
 			],
45
-			'schema' => [ $this->schema, 'get_public_item_schema' ],
45
+			'schema' => [$this->schema, 'get_public_item_schema'],
46 46
 		];
47 47
 	}
48 48
 
@@ -53,13 +53,13 @@  discard block
 block discarded – undo
53 53
 	 * @param \WP_REST_Request $request Request object.
54 54
 	 * @return \WP_REST_Response
55 55
 	 */
56
-	protected function get_route_response( \WP_REST_Request $request ) {
57
-		$attribute = wc_get_attribute( $request['attribute_id'] );
56
+	protected function get_route_response(\WP_REST_Request $request) {
57
+		$attribute = wc_get_attribute($request['attribute_id']);
58 58
 
59
-		if ( ! $attribute || ! taxonomy_exists( $attribute->slug ) ) {
60
-			throw new RouteException( 'woocommerce_rest_taxonomy_invalid', __( 'Attribute does not exist.', 'woocommerce' ), 404 );
59
+		if (!$attribute || !taxonomy_exists($attribute->slug)) {
60
+			throw new RouteException('woocommerce_rest_taxonomy_invalid', __('Attribute does not exist.', 'woocommerce'), 404);
61 61
 		}
62 62
 
63
-		return $this->get_terms_response( $attribute->slug, $request );
63
+		return $this->get_terms_response($attribute->slug, $request);
64 64
 	}
65 65
 }
Please login to merge, or discard this patch.
packages/woocommerce-blocks/src/StoreApi/Routes/V1/CartApplyCoupon.php 2 patches
Indentation   +56 added lines, -56 removed lines patch added patch discarded remove patch
@@ -7,66 +7,66 @@
 block discarded – undo
7 7
  * CartApplyCoupon class.
8 8
  */
9 9
 class CartApplyCoupon extends AbstractCartRoute {
10
-	/**
11
-	 * The route identifier.
12
-	 *
13
-	 * @var string
14
-	 */
15
-	const IDENTIFIER = 'cart-apply-coupon';
10
+    /**
11
+     * The route identifier.
12
+     *
13
+     * @var string
14
+     */
15
+    const IDENTIFIER = 'cart-apply-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/apply-coupon';
24
-	}
17
+    /**
18
+     * Get the path of this REST route.
19
+     *
20
+     * @return string
21
+     */
22
+    public function get_path() {
23
+        return '/cart/apply-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( wp_unslash( $request['code'] ) );
61
+        $cart        = $this->cart_controller->get_cart_instance();
62
+        $coupon_code = wc_format_coupon_code( wp_unslash( $request['code'] ) );
63 63
 
64
-		try {
65
-			$this->cart_controller->apply_coupon( $coupon_code );
66
-		} catch ( \WC_REST_Exception $e ) {
67
-			throw new RouteException( $e->getErrorCode(), $e->getMessage(), $e->getCode() );
68
-		}
64
+        try {
65
+            $this->cart_controller->apply_coupon( $coupon_code );
66
+        } catch ( \WC_REST_Exception $e ) {
67
+            throw new RouteException( $e->getErrorCode(), $e->getMessage(), $e->getCode() );
68
+        }
69 69
 
70
-		return rest_ensure_response( $this->schema->get_item_response( $cart ) );
71
-	}
70
+        return rest_ensure_response( $this->schema->get_item_response( $cart ) );
71
+    }
72 72
 }
Please login to merge, or discard this patch.
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -32,17 +32,17 @@  discard block
 block discarded – undo
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,20 +53,20 @@  discard block
 block discarded – undo
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( wp_unslash( $request['code'] ) );
62
+		$coupon_code = wc_format_coupon_code(wp_unslash($request['code']));
63 63
 
64 64
 		try {
65
-			$this->cart_controller->apply_coupon( $coupon_code );
66
-		} catch ( \WC_REST_Exception $e ) {
67
-			throw new RouteException( $e->getErrorCode(), $e->getMessage(), $e->getCode() );
65
+			$this->cart_controller->apply_coupon($coupon_code);
66
+		} catch (\WC_REST_Exception $e) {
67
+			throw new RouteException($e->getErrorCode(), $e->getMessage(), $e->getCode());
68 68
 		}
69 69
 
70
-		return rest_ensure_response( $this->schema->get_item_response( $cart ) );
70
+		return rest_ensure_response($this->schema->get_item_response($cart));
71 71
 	}
72 72
 }
Please login to merge, or discard this patch.
woocommerce-blocks/src/StoreApi/Routes/V1/ProductCollectionData.php 2 patches
Indentation   +202 added lines, -202 removed lines patch added patch discarded remove patch
@@ -10,206 +10,206 @@
 block discarded – undo
10 10
  * Supports the same parameters as /products, but returns a different response.
11 11
  */
12 12
 class ProductCollectionData extends AbstractRoute {
13
-	/**
14
-	 * The route identifier.
15
-	 *
16
-	 * @var string
17
-	 */
18
-	const IDENTIFIER = 'product-collection-data';
19
-
20
-	/**
21
-	 * The routes schema.
22
-	 *
23
-	 * @var string
24
-	 */
25
-	const SCHEMA_TYPE = 'product-collection-data';
26
-
27
-	/**
28
-	 * Get the path of this REST route.
29
-	 *
30
-	 * @return string
31
-	 */
32
-	public function get_path() {
33
-		return '/products/collection-data';
34
-	}
35
-
36
-	/**
37
-	 * Get method arguments for this REST route.
38
-	 *
39
-	 * @return array An array of endpoints.
40
-	 */
41
-	public function get_args() {
42
-		return [
43
-			[
44
-				'methods'             => \WP_REST_Server::READABLE,
45
-				'callback'            => [ $this, 'get_response' ],
46
-				'permission_callback' => '__return_true',
47
-				'args'                => $this->get_collection_params(),
48
-			],
49
-			'schema' => [ $this->schema, 'get_public_item_schema' ],
50
-		];
51
-	}
52
-
53
-	/**
54
-	 * Get a collection of posts and add the post title filter option to \WP_Query.
55
-	 *
56
-	 * @param \WP_REST_Request $request Request object.
57
-	 * @return \WP_REST_Response
58
-	 */
59
-	protected function get_route_response( \WP_REST_Request $request ) {
60
-		$data    = [
61
-			'min_price'           => null,
62
-			'max_price'           => null,
63
-			'attribute_counts'    => null,
64
-			'stock_status_counts' => null,
65
-			'rating_counts'       => null,
66
-		];
67
-		$filters = new ProductQueryFilters();
68
-
69
-		if ( ! empty( $request['calculate_price_range'] ) ) {
70
-			$filter_request = clone $request;
71
-			$filter_request->set_param( 'min_price', null );
72
-			$filter_request->set_param( 'max_price', null );
73
-
74
-			$price_results     = $filters->get_filtered_price( $filter_request );
75
-			$data['min_price'] = $price_results->min_price;
76
-			$data['max_price'] = $price_results->max_price;
77
-		}
78
-
79
-		if ( ! empty( $request['calculate_stock_status_counts'] ) ) {
80
-			$filter_request = clone $request;
81
-			$counts         = $filters->get_stock_status_counts( $filter_request );
82
-
83
-			$data['stock_status_counts'] = [];
84
-
85
-			foreach ( $counts as $key => $value ) {
86
-				$data['stock_status_counts'][] = (object) [
87
-					'status' => $key,
88
-					'count'  => $value,
89
-				];
90
-			}
91
-		}
92
-
93
-		if ( ! empty( $request['calculate_attribute_counts'] ) ) {
94
-			$taxonomy__or_queries  = [];
95
-			$taxonomy__and_queries = [];
96
-
97
-			foreach ( $request['calculate_attribute_counts'] as $attributes_to_count ) {
98
-				if ( ! empty( $attributes_to_count['taxonomy'] ) ) {
99
-					if ( empty( $attributes_to_count['query_type'] ) || 'or' === $attributes_to_count['query_type'] ) {
100
-						$taxonomy__or_queries[] = $attributes_to_count['taxonomy'];
101
-					} else {
102
-						$taxonomy__and_queries[] = $attributes_to_count['taxonomy'];
103
-					}
104
-				}
105
-			}
106
-
107
-			$data['attribute_counts'] = [];
108
-			// Or type queries need special handling because the attribute, if set, needs removing from the query first otherwise counts would not be correct.
109
-			if ( $taxonomy__or_queries ) {
110
-				foreach ( $taxonomy__or_queries as $taxonomy ) {
111
-					$filter_request    = clone $request;
112
-					$filter_attributes = $filter_request->get_param( 'attributes' );
113
-
114
-					if ( ! empty( $filter_attributes ) ) {
115
-						$filter_attributes = array_filter(
116
-							$filter_attributes,
117
-							function( $query ) use ( $taxonomy ) {
118
-								return $query['attribute'] !== $taxonomy;
119
-							}
120
-						);
121
-					}
122
-
123
-					$filter_request->set_param( 'attributes', $filter_attributes );
124
-					$counts = $filters->get_attribute_counts( $filter_request, [ $taxonomy ] );
125
-
126
-					foreach ( $counts as $key => $value ) {
127
-						$data['attribute_counts'][] = (object) [
128
-							'term'  => $key,
129
-							'count' => $value,
130
-						];
131
-					}
132
-				}
133
-			}
134
-
135
-			if ( $taxonomy__and_queries ) {
136
-				$counts = $filters->get_attribute_counts( $request, $taxonomy__and_queries );
137
-
138
-				foreach ( $counts as $key => $value ) {
139
-					$data['attribute_counts'][] = (object) [
140
-						'term'  => $key,
141
-						'count' => $value,
142
-					];
143
-				}
144
-			}
145
-		}
146
-
147
-		if ( ! empty( $request['calculate_rating_counts'] ) ) {
148
-			$filter_request        = clone $request;
149
-			$counts                = $filters->get_rating_counts( $filter_request );
150
-			$data['rating_counts'] = [];
151
-
152
-			foreach ( $counts as $key => $value ) {
153
-				$data['rating_counts'][] = (object) [
154
-					'rating' => $key,
155
-					'count'  => $value,
156
-				];
157
-			}
158
-		}
159
-
160
-		return rest_ensure_response( $this->schema->get_item_response( $data ) );
161
-	}
162
-
163
-	/**
164
-	 * Get the query params for collections of products.
165
-	 *
166
-	 * @return array
167
-	 */
168
-	public function get_collection_params() {
169
-		$params = ( new Products( $this->schema_controller, $this->schema ) )->get_collection_params();
170
-
171
-		$params['calculate_price_range'] = [
172
-			'description' => __( 'If true, calculates the minimum and maximum product prices for the collection.', 'woocommerce' ),
173
-			'type'        => 'boolean',
174
-			'default'     => false,
175
-		];
176
-
177
-		$params['calculate_stock_status_counts'] = [
178
-			'description' => __( 'If true, calculates stock counts for products in the collection.', 'woocommerce' ),
179
-			'type'        => 'boolean',
180
-			'default'     => false,
181
-		];
182
-
183
-		$params['calculate_attribute_counts'] = [
184
-			'description' => __( 'If requested, calculates attribute term counts for products in the collection.', 'woocommerce' ),
185
-			'type'        => 'array',
186
-			'items'       => [
187
-				'type'       => 'object',
188
-				'properties' => [
189
-					'taxonomy'   => [
190
-						'description' => __( 'Taxonomy name.', 'woocommerce' ),
191
-						'type'        => 'string',
192
-						'context'     => [ 'view', 'edit' ],
193
-						'readonly'    => true,
194
-					],
195
-					'query_type' => [
196
-						'description' => __( 'Query type being performed which may affect counts. Valid values include "and" and "or".', 'woocommerce' ),
197
-						'type'        => 'string',
198
-						'enum'        => [ 'and', 'or' ],
199
-						'context'     => [ 'view', 'edit' ],
200
-						'readonly'    => true,
201
-					],
202
-				],
203
-			],
204
-			'default'     => [],
205
-		];
206
-
207
-		$params['calculate_rating_counts'] = [
208
-			'description' => __( 'If true, calculates rating counts for products in the collection.', 'woocommerce' ),
209
-			'type'        => 'boolean',
210
-			'default'     => false,
211
-		];
212
-
213
-		return $params;
214
-	}
13
+    /**
14
+     * The route identifier.
15
+     *
16
+     * @var string
17
+     */
18
+    const IDENTIFIER = 'product-collection-data';
19
+
20
+    /**
21
+     * The routes schema.
22
+     *
23
+     * @var string
24
+     */
25
+    const SCHEMA_TYPE = 'product-collection-data';
26
+
27
+    /**
28
+     * Get the path of this REST route.
29
+     *
30
+     * @return string
31
+     */
32
+    public function get_path() {
33
+        return '/products/collection-data';
34
+    }
35
+
36
+    /**
37
+     * Get method arguments for this REST route.
38
+     *
39
+     * @return array An array of endpoints.
40
+     */
41
+    public function get_args() {
42
+        return [
43
+            [
44
+                'methods'             => \WP_REST_Server::READABLE,
45
+                'callback'            => [ $this, 'get_response' ],
46
+                'permission_callback' => '__return_true',
47
+                'args'                => $this->get_collection_params(),
48
+            ],
49
+            'schema' => [ $this->schema, 'get_public_item_schema' ],
50
+        ];
51
+    }
52
+
53
+    /**
54
+     * Get a collection of posts and add the post title filter option to \WP_Query.
55
+     *
56
+     * @param \WP_REST_Request $request Request object.
57
+     * @return \WP_REST_Response
58
+     */
59
+    protected function get_route_response( \WP_REST_Request $request ) {
60
+        $data    = [
61
+            'min_price'           => null,
62
+            'max_price'           => null,
63
+            'attribute_counts'    => null,
64
+            'stock_status_counts' => null,
65
+            'rating_counts'       => null,
66
+        ];
67
+        $filters = new ProductQueryFilters();
68
+
69
+        if ( ! empty( $request['calculate_price_range'] ) ) {
70
+            $filter_request = clone $request;
71
+            $filter_request->set_param( 'min_price', null );
72
+            $filter_request->set_param( 'max_price', null );
73
+
74
+            $price_results     = $filters->get_filtered_price( $filter_request );
75
+            $data['min_price'] = $price_results->min_price;
76
+            $data['max_price'] = $price_results->max_price;
77
+        }
78
+
79
+        if ( ! empty( $request['calculate_stock_status_counts'] ) ) {
80
+            $filter_request = clone $request;
81
+            $counts         = $filters->get_stock_status_counts( $filter_request );
82
+
83
+            $data['stock_status_counts'] = [];
84
+
85
+            foreach ( $counts as $key => $value ) {
86
+                $data['stock_status_counts'][] = (object) [
87
+                    'status' => $key,
88
+                    'count'  => $value,
89
+                ];
90
+            }
91
+        }
92
+
93
+        if ( ! empty( $request['calculate_attribute_counts'] ) ) {
94
+            $taxonomy__or_queries  = [];
95
+            $taxonomy__and_queries = [];
96
+
97
+            foreach ( $request['calculate_attribute_counts'] as $attributes_to_count ) {
98
+                if ( ! empty( $attributes_to_count['taxonomy'] ) ) {
99
+                    if ( empty( $attributes_to_count['query_type'] ) || 'or' === $attributes_to_count['query_type'] ) {
100
+                        $taxonomy__or_queries[] = $attributes_to_count['taxonomy'];
101
+                    } else {
102
+                        $taxonomy__and_queries[] = $attributes_to_count['taxonomy'];
103
+                    }
104
+                }
105
+            }
106
+
107
+            $data['attribute_counts'] = [];
108
+            // Or type queries need special handling because the attribute, if set, needs removing from the query first otherwise counts would not be correct.
109
+            if ( $taxonomy__or_queries ) {
110
+                foreach ( $taxonomy__or_queries as $taxonomy ) {
111
+                    $filter_request    = clone $request;
112
+                    $filter_attributes = $filter_request->get_param( 'attributes' );
113
+
114
+                    if ( ! empty( $filter_attributes ) ) {
115
+                        $filter_attributes = array_filter(
116
+                            $filter_attributes,
117
+                            function( $query ) use ( $taxonomy ) {
118
+                                return $query['attribute'] !== $taxonomy;
119
+                            }
120
+                        );
121
+                    }
122
+
123
+                    $filter_request->set_param( 'attributes', $filter_attributes );
124
+                    $counts = $filters->get_attribute_counts( $filter_request, [ $taxonomy ] );
125
+
126
+                    foreach ( $counts as $key => $value ) {
127
+                        $data['attribute_counts'][] = (object) [
128
+                            'term'  => $key,
129
+                            'count' => $value,
130
+                        ];
131
+                    }
132
+                }
133
+            }
134
+
135
+            if ( $taxonomy__and_queries ) {
136
+                $counts = $filters->get_attribute_counts( $request, $taxonomy__and_queries );
137
+
138
+                foreach ( $counts as $key => $value ) {
139
+                    $data['attribute_counts'][] = (object) [
140
+                        'term'  => $key,
141
+                        'count' => $value,
142
+                    ];
143
+                }
144
+            }
145
+        }
146
+
147
+        if ( ! empty( $request['calculate_rating_counts'] ) ) {
148
+            $filter_request        = clone $request;
149
+            $counts                = $filters->get_rating_counts( $filter_request );
150
+            $data['rating_counts'] = [];
151
+
152
+            foreach ( $counts as $key => $value ) {
153
+                $data['rating_counts'][] = (object) [
154
+                    'rating' => $key,
155
+                    'count'  => $value,
156
+                ];
157
+            }
158
+        }
159
+
160
+        return rest_ensure_response( $this->schema->get_item_response( $data ) );
161
+    }
162
+
163
+    /**
164
+     * Get the query params for collections of products.
165
+     *
166
+     * @return array
167
+     */
168
+    public function get_collection_params() {
169
+        $params = ( new Products( $this->schema_controller, $this->schema ) )->get_collection_params();
170
+
171
+        $params['calculate_price_range'] = [
172
+            'description' => __( 'If true, calculates the minimum and maximum product prices for the collection.', 'woocommerce' ),
173
+            'type'        => 'boolean',
174
+            'default'     => false,
175
+        ];
176
+
177
+        $params['calculate_stock_status_counts'] = [
178
+            'description' => __( 'If true, calculates stock counts for products in the collection.', 'woocommerce' ),
179
+            'type'        => 'boolean',
180
+            'default'     => false,
181
+        ];
182
+
183
+        $params['calculate_attribute_counts'] = [
184
+            'description' => __( 'If requested, calculates attribute term counts for products in the collection.', 'woocommerce' ),
185
+            'type'        => 'array',
186
+            'items'       => [
187
+                'type'       => 'object',
188
+                'properties' => [
189
+                    'taxonomy'   => [
190
+                        'description' => __( 'Taxonomy name.', 'woocommerce' ),
191
+                        'type'        => 'string',
192
+                        'context'     => [ 'view', 'edit' ],
193
+                        'readonly'    => true,
194
+                    ],
195
+                    'query_type' => [
196
+                        'description' => __( 'Query type being performed which may affect counts. Valid values include "and" and "or".', 'woocommerce' ),
197
+                        'type'        => 'string',
198
+                        'enum'        => [ 'and', 'or' ],
199
+                        'context'     => [ 'view', 'edit' ],
200
+                        'readonly'    => true,
201
+                    ],
202
+                ],
203
+            ],
204
+            'default'     => [],
205
+        ];
206
+
207
+        $params['calculate_rating_counts'] = [
208
+            'description' => __( 'If true, calculates rating counts for products in the collection.', 'woocommerce' ),
209
+            'type'        => 'boolean',
210
+            'default'     => false,
211
+        ];
212
+
213
+        return $params;
214
+    }
215 215
 }
Please login to merge, or discard this patch.
Spacing   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -42,11 +42,11 @@  discard block
 block discarded – undo
42 42
 		return [
43 43
 			[
44 44
 				'methods'             => \WP_REST_Server::READABLE,
45
-				'callback'            => [ $this, 'get_response' ],
45
+				'callback'            => [$this, 'get_response'],
46 46
 				'permission_callback' => '__return_true',
47 47
 				'args'                => $this->get_collection_params(),
48 48
 			],
49
-			'schema' => [ $this->schema, 'get_public_item_schema' ],
49
+			'schema' => [$this->schema, 'get_public_item_schema'],
50 50
 		];
51 51
 	}
52 52
 
@@ -56,8 +56,8 @@  discard block
 block discarded – undo
56 56
 	 * @param \WP_REST_Request $request Request object.
57 57
 	 * @return \WP_REST_Response
58 58
 	 */
59
-	protected function get_route_response( \WP_REST_Request $request ) {
60
-		$data    = [
59
+	protected function get_route_response(\WP_REST_Request $request) {
60
+		$data = [
61 61
 			'min_price'           => null,
62 62
 			'max_price'           => null,
63 63
 			'attribute_counts'    => null,
@@ -66,23 +66,23 @@  discard block
 block discarded – undo
66 66
 		];
67 67
 		$filters = new ProductQueryFilters();
68 68
 
69
-		if ( ! empty( $request['calculate_price_range'] ) ) {
69
+		if (!empty($request['calculate_price_range'])) {
70 70
 			$filter_request = clone $request;
71
-			$filter_request->set_param( 'min_price', null );
72
-			$filter_request->set_param( 'max_price', null );
71
+			$filter_request->set_param('min_price', null);
72
+			$filter_request->set_param('max_price', null);
73 73
 
74
-			$price_results     = $filters->get_filtered_price( $filter_request );
74
+			$price_results     = $filters->get_filtered_price($filter_request);
75 75
 			$data['min_price'] = $price_results->min_price;
76 76
 			$data['max_price'] = $price_results->max_price;
77 77
 		}
78 78
 
79
-		if ( ! empty( $request['calculate_stock_status_counts'] ) ) {
79
+		if (!empty($request['calculate_stock_status_counts'])) {
80 80
 			$filter_request = clone $request;
81
-			$counts         = $filters->get_stock_status_counts( $filter_request );
81
+			$counts         = $filters->get_stock_status_counts($filter_request);
82 82
 
83 83
 			$data['stock_status_counts'] = [];
84 84
 
85
-			foreach ( $counts as $key => $value ) {
85
+			foreach ($counts as $key => $value) {
86 86
 				$data['stock_status_counts'][] = (object) [
87 87
 					'status' => $key,
88 88
 					'count'  => $value,
@@ -90,13 +90,13 @@  discard block
 block discarded – undo
90 90
 			}
91 91
 		}
92 92
 
93
-		if ( ! empty( $request['calculate_attribute_counts'] ) ) {
93
+		if (!empty($request['calculate_attribute_counts'])) {
94 94
 			$taxonomy__or_queries  = [];
95 95
 			$taxonomy__and_queries = [];
96 96
 
97
-			foreach ( $request['calculate_attribute_counts'] as $attributes_to_count ) {
98
-				if ( ! empty( $attributes_to_count['taxonomy'] ) ) {
99
-					if ( empty( $attributes_to_count['query_type'] ) || 'or' === $attributes_to_count['query_type'] ) {
97
+			foreach ($request['calculate_attribute_counts'] as $attributes_to_count) {
98
+				if (!empty($attributes_to_count['taxonomy'])) {
99
+					if (empty($attributes_to_count['query_type']) || 'or' === $attributes_to_count['query_type']) {
100 100
 						$taxonomy__or_queries[] = $attributes_to_count['taxonomy'];
101 101
 					} else {
102 102
 						$taxonomy__and_queries[] = $attributes_to_count['taxonomy'];
@@ -106,24 +106,24 @@  discard block
 block discarded – undo
106 106
 
107 107
 			$data['attribute_counts'] = [];
108 108
 			// Or type queries need special handling because the attribute, if set, needs removing from the query first otherwise counts would not be correct.
109
-			if ( $taxonomy__or_queries ) {
110
-				foreach ( $taxonomy__or_queries as $taxonomy ) {
109
+			if ($taxonomy__or_queries) {
110
+				foreach ($taxonomy__or_queries as $taxonomy) {
111 111
 					$filter_request    = clone $request;
112
-					$filter_attributes = $filter_request->get_param( 'attributes' );
112
+					$filter_attributes = $filter_request->get_param('attributes');
113 113
 
114
-					if ( ! empty( $filter_attributes ) ) {
114
+					if (!empty($filter_attributes)) {
115 115
 						$filter_attributes = array_filter(
116 116
 							$filter_attributes,
117
-							function( $query ) use ( $taxonomy ) {
117
+							function($query) use ($taxonomy) {
118 118
 								return $query['attribute'] !== $taxonomy;
119 119
 							}
120 120
 						);
121 121
 					}
122 122
 
123
-					$filter_request->set_param( 'attributes', $filter_attributes );
124
-					$counts = $filters->get_attribute_counts( $filter_request, [ $taxonomy ] );
123
+					$filter_request->set_param('attributes', $filter_attributes);
124
+					$counts = $filters->get_attribute_counts($filter_request, [$taxonomy]);
125 125
 
126
-					foreach ( $counts as $key => $value ) {
126
+					foreach ($counts as $key => $value) {
127 127
 						$data['attribute_counts'][] = (object) [
128 128
 							'term'  => $key,
129 129
 							'count' => $value,
@@ -132,10 +132,10 @@  discard block
 block discarded – undo
132 132
 				}
133 133
 			}
134 134
 
135
-			if ( $taxonomy__and_queries ) {
136
-				$counts = $filters->get_attribute_counts( $request, $taxonomy__and_queries );
135
+			if ($taxonomy__and_queries) {
136
+				$counts = $filters->get_attribute_counts($request, $taxonomy__and_queries);
137 137
 
138
-				foreach ( $counts as $key => $value ) {
138
+				foreach ($counts as $key => $value) {
139 139
 					$data['attribute_counts'][] = (object) [
140 140
 						'term'  => $key,
141 141
 						'count' => $value,
@@ -144,12 +144,12 @@  discard block
 block discarded – undo
144 144
 			}
145 145
 		}
146 146
 
147
-		if ( ! empty( $request['calculate_rating_counts'] ) ) {
147
+		if (!empty($request['calculate_rating_counts'])) {
148 148
 			$filter_request        = clone $request;
149
-			$counts                = $filters->get_rating_counts( $filter_request );
149
+			$counts                = $filters->get_rating_counts($filter_request);
150 150
 			$data['rating_counts'] = [];
151 151
 
152
-			foreach ( $counts as $key => $value ) {
152
+			foreach ($counts as $key => $value) {
153 153
 				$data['rating_counts'][] = (object) [
154 154
 					'rating' => $key,
155 155
 					'count'  => $value,
@@ -157,7 +157,7 @@  discard block
 block discarded – undo
157 157
 			}
158 158
 		}
159 159
 
160
-		return rest_ensure_response( $this->schema->get_item_response( $data ) );
160
+		return rest_ensure_response($this->schema->get_item_response($data));
161 161
 	}
162 162
 
163 163
 	/**
@@ -166,37 +166,37 @@  discard block
 block discarded – undo
166 166
 	 * @return array
167 167
 	 */
168 168
 	public function get_collection_params() {
169
-		$params = ( new Products( $this->schema_controller, $this->schema ) )->get_collection_params();
169
+		$params = (new Products($this->schema_controller, $this->schema))->get_collection_params();
170 170
 
171 171
 		$params['calculate_price_range'] = [
172
-			'description' => __( 'If true, calculates the minimum and maximum product prices for the collection.', 'woocommerce' ),
172
+			'description' => __('If true, calculates the minimum and maximum product prices for the collection.', 'woocommerce'),
173 173
 			'type'        => 'boolean',
174 174
 			'default'     => false,
175 175
 		];
176 176
 
177 177
 		$params['calculate_stock_status_counts'] = [
178
-			'description' => __( 'If true, calculates stock counts for products in the collection.', 'woocommerce' ),
178
+			'description' => __('If true, calculates stock counts for products in the collection.', 'woocommerce'),
179 179
 			'type'        => 'boolean',
180 180
 			'default'     => false,
181 181
 		];
182 182
 
183 183
 		$params['calculate_attribute_counts'] = [
184
-			'description' => __( 'If requested, calculates attribute term counts for products in the collection.', 'woocommerce' ),
184
+			'description' => __('If requested, calculates attribute term counts for products in the collection.', 'woocommerce'),
185 185
 			'type'        => 'array',
186 186
 			'items'       => [
187 187
 				'type'       => 'object',
188 188
 				'properties' => [
189 189
 					'taxonomy'   => [
190
-						'description' => __( 'Taxonomy name.', 'woocommerce' ),
190
+						'description' => __('Taxonomy name.', 'woocommerce'),
191 191
 						'type'        => 'string',
192
-						'context'     => [ 'view', 'edit' ],
192
+						'context'     => ['view', 'edit'],
193 193
 						'readonly'    => true,
194 194
 					],
195 195
 					'query_type' => [
196
-						'description' => __( 'Query type being performed which may affect counts. Valid values include "and" and "or".', 'woocommerce' ),
196
+						'description' => __('Query type being performed which may affect counts. Valid values include "and" and "or".', 'woocommerce'),
197 197
 						'type'        => 'string',
198
-						'enum'        => [ 'and', 'or' ],
199
-						'context'     => [ 'view', 'edit' ],
198
+						'enum'        => ['and', 'or'],
199
+						'context'     => ['view', 'edit'],
200 200
 						'readonly'    => true,
201 201
 					],
202 202
 				],
@@ -205,7 +205,7 @@  discard block
 block discarded – undo
205 205
 		];
206 206
 
207 207
 		$params['calculate_rating_counts'] = [
208
-			'description' => __( 'If true, calculates rating counts for products in the collection.', 'woocommerce' ),
208
+			'description' => __('If true, calculates rating counts for products in the collection.', 'woocommerce'),
209 209
 			'type'        => 'boolean',
210 210
 			'default'     => false,
211 211
 		];
Please login to merge, or discard this patch.
woocommerce-blocks/src/StoreApi/Routes/V1/ProductCategoriesById.php 2 patches
Indentation   +63 added lines, -63 removed lines patch added patch discarded remove patch
@@ -7,73 +7,73 @@
 block discarded – undo
7 7
  * ProductCategoriesById class.
8 8
  */
9 9
 class ProductCategoriesById extends AbstractRoute {
10
-	/**
11
-	 * The route identifier.
12
-	 *
13
-	 * @var string
14
-	 */
15
-	const IDENTIFIER = 'product-categories-by-id';
10
+    /**
11
+     * The route identifier.
12
+     *
13
+     * @var string
14
+     */
15
+    const IDENTIFIER = 'product-categories-by-id';
16 16
 
17
-	/**
18
-	 * The routes schema.
19
-	 *
20
-	 * @var string
21
-	 */
22
-	const SCHEMA_TYPE = 'product-category';
17
+    /**
18
+     * The routes schema.
19
+     *
20
+     * @var string
21
+     */
22
+    const SCHEMA_TYPE = 'product-category';
23 23
 
24
-	/**
25
-	 * Get the path of this REST route.
26
-	 *
27
-	 * @return string
28
-	 */
29
-	public function get_path() {
30
-		return '/products/categories/(?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/categories/(?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 = get_term( (int) $request['id'], 'product_cat' );
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 = get_term( (int) $request['id'], 'product_cat' );
71 71
 
72
-		if ( ! $object || 0 === $object->id ) {
73
-			throw new RouteException( 'woocommerce_rest_category_invalid_id', __( 'Invalid category ID.', 'woocommerce' ), 404 );
74
-		}
72
+        if ( ! $object || 0 === $object->id ) {
73
+            throw new RouteException( 'woocommerce_rest_category_invalid_id', __( 'Invalid category ID.', 'woocommerce' ), 404 );
74
+        }
75 75
 
76
-		$data = $this->prepare_item_for_response( $object, $request );
77
-		return rest_ensure_response( $data );
78
-	}
76
+        $data = $this->prepare_item_for_response( $object, $request );
77
+        return rest_ensure_response( $data );
78
+    }
79 79
 }
Please login to merge, or discard this patch.
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -39,13 +39,13 @@  discard block
 block discarded – undo
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
 block discarded – undo
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,14 +66,14 @@  discard block
 block discarded – undo
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 = get_term( (int) $request['id'], 'product_cat' );
69
+	protected function get_route_response(\WP_REST_Request $request) {
70
+		$object = get_term((int) $request['id'], 'product_cat');
71 71
 
72
-		if ( ! $object || 0 === $object->id ) {
73
-			throw new RouteException( 'woocommerce_rest_category_invalid_id', __( 'Invalid category ID.', 'woocommerce' ), 404 );
72
+		if (!$object || 0 === $object->id) {
73
+			throw new RouteException('woocommerce_rest_category_invalid_id', __('Invalid category ID.', 'woocommerce'), 404);
74 74
 		}
75 75
 
76
-		$data = $this->prepare_item_for_response( $object, $request );
77
-		return rest_ensure_response( $data );
76
+		$data = $this->prepare_item_for_response($object, $request);
77
+		return rest_ensure_response($data);
78 78
 	}
79 79
 }
Please login to merge, or discard this patch.
packages/woocommerce-blocks/src/StoreApi/Routes/V1/AbstractTermsRoute.php 2 patches
Indentation   +146 added lines, -146 removed lines patch added patch discarded remove patch
@@ -8,150 +8,150 @@
 block discarded – undo
8 8
  * AbstractTermsRoute class.
9 9
  */
10 10
 abstract class AbstractTermsRoute extends AbstractRoute {
11
-	/**
12
-	 * The routes schema.
13
-	 *
14
-	 * @var string
15
-	 */
16
-	const SCHEMA_TYPE = 'term';
17
-
18
-	/**
19
-	 * Get the query params for collections of attributes.
20
-	 *
21
-	 * @return array
22
-	 */
23
-	public function get_collection_params() {
24
-		$params                       = array();
25
-		$params['context']            = $this->get_context_param();
26
-		$params['context']['default'] = 'view';
27
-
28
-		$params['page'] = array(
29
-			'description'       => __( 'Current page of the collection.', 'woocommerce' ),
30
-			'type'              => 'integer',
31
-			'default'           => 1,
32
-			'sanitize_callback' => 'absint',
33
-			'validate_callback' => 'rest_validate_request_arg',
34
-			'minimum'           => 1,
35
-		);
36
-
37
-		$params['per_page'] = array(
38
-			'description'       => __( 'Maximum number of items to be returned in result set. Defaults to no limit if left blank.', 'woocommerce' ),
39
-			'type'              => 'integer',
40
-			'minimum'           => 0,
41
-			'maximum'           => 100,
42
-			'sanitize_callback' => 'absint',
43
-			'validate_callback' => 'rest_validate_request_arg',
44
-		);
45
-
46
-		$params['search'] = array(
47
-			'description'       => __( 'Limit results to those matching a string.', 'woocommerce' ),
48
-			'type'              => 'string',
49
-			'sanitize_callback' => 'sanitize_text_field',
50
-			'validate_callback' => 'rest_validate_request_arg',
51
-		);
52
-
53
-		$params['exclude'] = array(
54
-			'description'       => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ),
55
-			'type'              => 'array',
56
-			'items'             => array(
57
-				'type' => 'integer',
58
-			),
59
-			'default'           => array(),
60
-			'sanitize_callback' => 'wp_parse_id_list',
61
-		);
62
-
63
-		$params['include'] = array(
64
-			'description'       => __( 'Limit result set to specific ids.', 'woocommerce' ),
65
-			'type'              => 'array',
66
-			'items'             => array(
67
-				'type' => 'integer',
68
-			),
69
-			'default'           => array(),
70
-			'sanitize_callback' => 'wp_parse_id_list',
71
-		);
72
-
73
-		$params['order'] = array(
74
-			'description'       => __( 'Sort ascending or descending.', 'woocommerce' ),
75
-			'type'              => 'string',
76
-			'default'           => 'asc',
77
-			'enum'              => array( 'asc', 'desc' ),
78
-			'validate_callback' => 'rest_validate_request_arg',
79
-		);
80
-
81
-		$params['orderby'] = array(
82
-			'description'       => __( 'Sort by term property.', 'woocommerce' ),
83
-			'type'              => 'string',
84
-			'default'           => 'name',
85
-			'enum'              => array(
86
-				'name',
87
-				'slug',
88
-				'count',
89
-			),
90
-			'validate_callback' => 'rest_validate_request_arg',
91
-		);
92
-
93
-		$params['hide_empty'] = array(
94
-			'description' => __( 'If true, empty terms will not be returned.', 'woocommerce' ),
95
-			'type'        => 'boolean',
96
-			'default'     => true,
97
-		);
98
-
99
-		return $params;
100
-	}
101
-
102
-	/**
103
-	 * Get terms matching passed in args.
104
-	 *
105
-	 * @param string           $taxonomy Taxonomy to get terms from.
106
-	 * @param \WP_REST_Request $request Request object.
107
-	 *
108
-	 * @return \WP_REST_Response
109
-	 */
110
-	protected function get_terms_response( $taxonomy, $request ) {
111
-		$page          = (int) $request['page'];
112
-		$per_page      = $request['per_page'] ? (int) $request['per_page'] : 0;
113
-		$prepared_args = array(
114
-			'taxonomy'   => $taxonomy,
115
-			'exclude'    => $request['exclude'],
116
-			'include'    => $request['include'],
117
-			'order'      => $request['order'],
118
-			'orderby'    => $request['orderby'],
119
-			'hide_empty' => (bool) $request['hide_empty'],
120
-			'number'     => $per_page,
121
-			'offset'     => $per_page > 0 ? ( $page - 1 ) * $per_page : 0,
122
-			'search'     => $request['search'],
123
-		);
124
-
125
-		$term_query = new WP_Term_Query();
126
-		$objects    = $term_query->query( $prepared_args );
127
-		$return     = [];
128
-
129
-		foreach ( $objects as $object ) {
130
-			$data     = $this->prepare_item_for_response( $object, $request );
131
-			$return[] = $this->prepare_response_for_collection( $data );
132
-		}
133
-
134
-		$response = rest_ensure_response( $return );
135
-
136
-		// See if pagination is needed before calculating.
137
-		if ( $per_page > 0 && ( count( $objects ) === $per_page || $page > 1 ) ) {
138
-			$term_count = $this->get_term_count( $taxonomy, $prepared_args );
139
-			$response   = ( new Pagination() )->add_headers( $response, $request, $term_count, ceil( $term_count / $per_page ) );
140
-		}
141
-
142
-		return $response;
143
-	}
144
-
145
-	/**
146
-	 * Get count of terms for current query.
147
-	 *
148
-	 * @param string $taxonomy Taxonomy to get terms from.
149
-	 * @param array  $args Array of args to pass to wp_count_terms.
150
-	 * @return int
151
-	 */
152
-	protected function get_term_count( $taxonomy, $args ) {
153
-		$count_args = $args;
154
-		unset( $count_args['number'], $count_args['offset'] );
155
-		return (int) wp_count_terms( $taxonomy, $count_args );
156
-	}
11
+    /**
12
+     * The routes schema.
13
+     *
14
+     * @var string
15
+     */
16
+    const SCHEMA_TYPE = 'term';
17
+
18
+    /**
19
+     * Get the query params for collections of attributes.
20
+     *
21
+     * @return array
22
+     */
23
+    public function get_collection_params() {
24
+        $params                       = array();
25
+        $params['context']            = $this->get_context_param();
26
+        $params['context']['default'] = 'view';
27
+
28
+        $params['page'] = array(
29
+            'description'       => __( 'Current page of the collection.', 'woocommerce' ),
30
+            'type'              => 'integer',
31
+            'default'           => 1,
32
+            'sanitize_callback' => 'absint',
33
+            'validate_callback' => 'rest_validate_request_arg',
34
+            'minimum'           => 1,
35
+        );
36
+
37
+        $params['per_page'] = array(
38
+            'description'       => __( 'Maximum number of items to be returned in result set. Defaults to no limit if left blank.', 'woocommerce' ),
39
+            'type'              => 'integer',
40
+            'minimum'           => 0,
41
+            'maximum'           => 100,
42
+            'sanitize_callback' => 'absint',
43
+            'validate_callback' => 'rest_validate_request_arg',
44
+        );
45
+
46
+        $params['search'] = array(
47
+            'description'       => __( 'Limit results to those matching a string.', 'woocommerce' ),
48
+            'type'              => 'string',
49
+            'sanitize_callback' => 'sanitize_text_field',
50
+            'validate_callback' => 'rest_validate_request_arg',
51
+        );
52
+
53
+        $params['exclude'] = array(
54
+            'description'       => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ),
55
+            'type'              => 'array',
56
+            'items'             => array(
57
+                'type' => 'integer',
58
+            ),
59
+            'default'           => array(),
60
+            'sanitize_callback' => 'wp_parse_id_list',
61
+        );
62
+
63
+        $params['include'] = array(
64
+            'description'       => __( 'Limit result set to specific ids.', 'woocommerce' ),
65
+            'type'              => 'array',
66
+            'items'             => array(
67
+                'type' => 'integer',
68
+            ),
69
+            'default'           => array(),
70
+            'sanitize_callback' => 'wp_parse_id_list',
71
+        );
72
+
73
+        $params['order'] = array(
74
+            'description'       => __( 'Sort ascending or descending.', 'woocommerce' ),
75
+            'type'              => 'string',
76
+            'default'           => 'asc',
77
+            'enum'              => array( 'asc', 'desc' ),
78
+            'validate_callback' => 'rest_validate_request_arg',
79
+        );
80
+
81
+        $params['orderby'] = array(
82
+            'description'       => __( 'Sort by term property.', 'woocommerce' ),
83
+            'type'              => 'string',
84
+            'default'           => 'name',
85
+            'enum'              => array(
86
+                'name',
87
+                'slug',
88
+                'count',
89
+            ),
90
+            'validate_callback' => 'rest_validate_request_arg',
91
+        );
92
+
93
+        $params['hide_empty'] = array(
94
+            'description' => __( 'If true, empty terms will not be returned.', 'woocommerce' ),
95
+            'type'        => 'boolean',
96
+            'default'     => true,
97
+        );
98
+
99
+        return $params;
100
+    }
101
+
102
+    /**
103
+     * Get terms matching passed in args.
104
+     *
105
+     * @param string           $taxonomy Taxonomy to get terms from.
106
+     * @param \WP_REST_Request $request Request object.
107
+     *
108
+     * @return \WP_REST_Response
109
+     */
110
+    protected function get_terms_response( $taxonomy, $request ) {
111
+        $page          = (int) $request['page'];
112
+        $per_page      = $request['per_page'] ? (int) $request['per_page'] : 0;
113
+        $prepared_args = array(
114
+            'taxonomy'   => $taxonomy,
115
+            'exclude'    => $request['exclude'],
116
+            'include'    => $request['include'],
117
+            'order'      => $request['order'],
118
+            'orderby'    => $request['orderby'],
119
+            'hide_empty' => (bool) $request['hide_empty'],
120
+            'number'     => $per_page,
121
+            'offset'     => $per_page > 0 ? ( $page - 1 ) * $per_page : 0,
122
+            'search'     => $request['search'],
123
+        );
124
+
125
+        $term_query = new WP_Term_Query();
126
+        $objects    = $term_query->query( $prepared_args );
127
+        $return     = [];
128
+
129
+        foreach ( $objects as $object ) {
130
+            $data     = $this->prepare_item_for_response( $object, $request );
131
+            $return[] = $this->prepare_response_for_collection( $data );
132
+        }
133
+
134
+        $response = rest_ensure_response( $return );
135
+
136
+        // See if pagination is needed before calculating.
137
+        if ( $per_page > 0 && ( count( $objects ) === $per_page || $page > 1 ) ) {
138
+            $term_count = $this->get_term_count( $taxonomy, $prepared_args );
139
+            $response   = ( new Pagination() )->add_headers( $response, $request, $term_count, ceil( $term_count / $per_page ) );
140
+        }
141
+
142
+        return $response;
143
+    }
144
+
145
+    /**
146
+     * Get count of terms for current query.
147
+     *
148
+     * @param string $taxonomy Taxonomy to get terms from.
149
+     * @param array  $args Array of args to pass to wp_count_terms.
150
+     * @return int
151
+     */
152
+    protected function get_term_count( $taxonomy, $args ) {
153
+        $count_args = $args;
154
+        unset( $count_args['number'], $count_args['offset'] );
155
+        return (int) wp_count_terms( $taxonomy, $count_args );
156
+    }
157 157
 }
Please login to merge, or discard this patch.
Spacing   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -26,7 +26,7 @@  discard block
 block discarded – undo
26 26
 		$params['context']['default'] = 'view';
27 27
 
28 28
 		$params['page'] = array(
29
-			'description'       => __( 'Current page of the collection.', 'woocommerce' ),
29
+			'description'       => __('Current page of the collection.', 'woocommerce'),
30 30
 			'type'              => 'integer',
31 31
 			'default'           => 1,
32 32
 			'sanitize_callback' => 'absint',
@@ -35,7 +35,7 @@  discard block
 block discarded – undo
35 35
 		);
36 36
 
37 37
 		$params['per_page'] = array(
38
-			'description'       => __( 'Maximum number of items to be returned in result set. Defaults to no limit if left blank.', 'woocommerce' ),
38
+			'description'       => __('Maximum number of items to be returned in result set. Defaults to no limit if left blank.', 'woocommerce'),
39 39
 			'type'              => 'integer',
40 40
 			'minimum'           => 0,
41 41
 			'maximum'           => 100,
@@ -44,14 +44,14 @@  discard block
 block discarded – undo
44 44
 		);
45 45
 
46 46
 		$params['search'] = array(
47
-			'description'       => __( 'Limit results to those matching a string.', 'woocommerce' ),
47
+			'description'       => __('Limit results to those matching a string.', 'woocommerce'),
48 48
 			'type'              => 'string',
49 49
 			'sanitize_callback' => 'sanitize_text_field',
50 50
 			'validate_callback' => 'rest_validate_request_arg',
51 51
 		);
52 52
 
53 53
 		$params['exclude'] = array(
54
-			'description'       => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ),
54
+			'description'       => __('Ensure result set excludes specific IDs.', 'woocommerce'),
55 55
 			'type'              => 'array',
56 56
 			'items'             => array(
57 57
 				'type' => 'integer',
@@ -61,7 +61,7 @@  discard block
 block discarded – undo
61 61
 		);
62 62
 
63 63
 		$params['include'] = array(
64
-			'description'       => __( 'Limit result set to specific ids.', 'woocommerce' ),
64
+			'description'       => __('Limit result set to specific ids.', 'woocommerce'),
65 65
 			'type'              => 'array',
66 66
 			'items'             => array(
67 67
 				'type' => 'integer',
@@ -71,15 +71,15 @@  discard block
 block discarded – undo
71 71
 		);
72 72
 
73 73
 		$params['order'] = array(
74
-			'description'       => __( 'Sort ascending or descending.', 'woocommerce' ),
74
+			'description'       => __('Sort ascending or descending.', 'woocommerce'),
75 75
 			'type'              => 'string',
76 76
 			'default'           => 'asc',
77
-			'enum'              => array( 'asc', 'desc' ),
77
+			'enum'              => array('asc', 'desc'),
78 78
 			'validate_callback' => 'rest_validate_request_arg',
79 79
 		);
80 80
 
81 81
 		$params['orderby'] = array(
82
-			'description'       => __( 'Sort by term property.', 'woocommerce' ),
82
+			'description'       => __('Sort by term property.', 'woocommerce'),
83 83
 			'type'              => 'string',
84 84
 			'default'           => 'name',
85 85
 			'enum'              => array(
@@ -91,7 +91,7 @@  discard block
 block discarded – undo
91 91
 		);
92 92
 
93 93
 		$params['hide_empty'] = array(
94
-			'description' => __( 'If true, empty terms will not be returned.', 'woocommerce' ),
94
+			'description' => __('If true, empty terms will not be returned.', 'woocommerce'),
95 95
 			'type'        => 'boolean',
96 96
 			'default'     => true,
97 97
 		);
@@ -107,7 +107,7 @@  discard block
 block discarded – undo
107 107
 	 *
108 108
 	 * @return \WP_REST_Response
109 109
 	 */
110
-	protected function get_terms_response( $taxonomy, $request ) {
110
+	protected function get_terms_response($taxonomy, $request) {
111 111
 		$page          = (int) $request['page'];
112 112
 		$per_page      = $request['per_page'] ? (int) $request['per_page'] : 0;
113 113
 		$prepared_args = array(
@@ -118,25 +118,25 @@  discard block
 block discarded – undo
118 118
 			'orderby'    => $request['orderby'],
119 119
 			'hide_empty' => (bool) $request['hide_empty'],
120 120
 			'number'     => $per_page,
121
-			'offset'     => $per_page > 0 ? ( $page - 1 ) * $per_page : 0,
121
+			'offset'     => $per_page > 0 ? ($page - 1) * $per_page : 0,
122 122
 			'search'     => $request['search'],
123 123
 		);
124 124
 
125 125
 		$term_query = new WP_Term_Query();
126
-		$objects    = $term_query->query( $prepared_args );
126
+		$objects    = $term_query->query($prepared_args);
127 127
 		$return     = [];
128 128
 
129
-		foreach ( $objects as $object ) {
130
-			$data     = $this->prepare_item_for_response( $object, $request );
131
-			$return[] = $this->prepare_response_for_collection( $data );
129
+		foreach ($objects as $object) {
130
+			$data     = $this->prepare_item_for_response($object, $request);
131
+			$return[] = $this->prepare_response_for_collection($data);
132 132
 		}
133 133
 
134
-		$response = rest_ensure_response( $return );
134
+		$response = rest_ensure_response($return);
135 135
 
136 136
 		// See if pagination is needed before calculating.
137
-		if ( $per_page > 0 && ( count( $objects ) === $per_page || $page > 1 ) ) {
138
-			$term_count = $this->get_term_count( $taxonomy, $prepared_args );
139
-			$response   = ( new Pagination() )->add_headers( $response, $request, $term_count, ceil( $term_count / $per_page ) );
137
+		if ($per_page > 0 && (count($objects) === $per_page || $page > 1)) {
138
+			$term_count = $this->get_term_count($taxonomy, $prepared_args);
139
+			$response   = (new Pagination())->add_headers($response, $request, $term_count, ceil($term_count / $per_page));
140 140
 		}
141 141
 
142 142
 		return $response;
@@ -149,9 +149,9 @@  discard block
 block discarded – undo
149 149
 	 * @param array  $args Array of args to pass to wp_count_terms.
150 150
 	 * @return int
151 151
 	 */
152
-	protected function get_term_count( $taxonomy, $args ) {
152
+	protected function get_term_count($taxonomy, $args) {
153 153
 		$count_args = $args;
154
-		unset( $count_args['number'], $count_args['offset'] );
155
-		return (int) wp_count_terms( $taxonomy, $count_args );
154
+		unset($count_args['number'], $count_args['offset']);
155
+		return (int) wp_count_terms($taxonomy, $count_args);
156 156
 	}
157 157
 }
Please login to merge, or discard this patch.
woocommerce-blocks/src/StoreApi/Routes/V1/ProductAttributesById.php 2 patches
Indentation   +64 added lines, -64 removed lines patch added patch discarded remove patch
@@ -7,75 +7,75 @@
 block discarded – undo
7 7
  * ProductAttributesById class.
8 8
  */
9 9
 class ProductAttributesById extends AbstractRoute {
10
-	/**
11
-	 * The route identifier.
12
-	 *
13
-	 * @var string
14
-	 */
15
-	const IDENTIFIER = 'product-attributes-by-id';
10
+    /**
11
+     * The route identifier.
12
+     *
13
+     * @var string
14
+     */
15
+    const IDENTIFIER = 'product-attributes-by-id';
16 16
 
17
-	/**
18
-	 * The routes schema.
19
-	 *
20
-	 * @var string
21
-	 */
22
-	const SCHEMA_TYPE = 'product-attribute';
17
+    /**
18
+     * The routes schema.
19
+     *
20
+     * @var string
21
+     */
22
+    const SCHEMA_TYPE = 'product-attribute';
23 23
 
24
-	/**
25
-	 * Get the path of this REST route.
26
-	 *
27
-	 * @return string
28
-	 */
29
-	public function get_path() {
30
-		return '/products/attributes/(?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/attributes/(?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_attribute( (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_attribute( (int) $request['id'] );
71 71
 
72
-		if ( ! $object || 0 === $object->id ) {
73
-			throw new RouteException( 'woocommerce_rest_attribute_invalid_id', __( 'Invalid attribute ID.', 'woocommerce' ), 404 );
74
-		}
72
+        if ( ! $object || 0 === $object->id ) {
73
+            throw new RouteException( 'woocommerce_rest_attribute_invalid_id', __( 'Invalid attribute ID.', 'woocommerce' ), 404 );
74
+        }
75 75
 
76
-		$data     = $this->prepare_item_for_response( $object, $request );
77
-		$response = rest_ensure_response( $data );
76
+        $data     = $this->prepare_item_for_response( $object, $request );
77
+        $response = rest_ensure_response( $data );
78 78
 
79
-		return $response;
80
-	}
79
+        return $response;
80
+    }
81 81
 }
Please login to merge, or discard this patch.
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -39,13 +39,13 @@  discard block
 block discarded – undo
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
 block discarded – undo
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,15 +66,15 @@  discard block
 block discarded – undo
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_attribute( (int) $request['id'] );
69
+	protected function get_route_response(\WP_REST_Request $request) {
70
+		$object = wc_get_attribute((int) $request['id']);
71 71
 
72
-		if ( ! $object || 0 === $object->id ) {
73
-			throw new RouteException( 'woocommerce_rest_attribute_invalid_id', __( 'Invalid attribute ID.', 'woocommerce' ), 404 );
72
+		if (!$object || 0 === $object->id) {
73
+			throw new RouteException('woocommerce_rest_attribute_invalid_id', __('Invalid attribute ID.', 'woocommerce'), 404);
74 74
 		}
75 75
 
76
-		$data     = $this->prepare_item_for_response( $object, $request );
77
-		$response = rest_ensure_response( $data );
76
+		$data     = $this->prepare_item_for_response($object, $request);
77
+		$response = rest_ensure_response($data);
78 78
 
79 79
 		return $response;
80 80
 	}
Please login to merge, or discard this patch.
woocommerce/packages/woocommerce-blocks/src/StoreApi/Routes/V1/Cart.php 2 patches
Indentation   +41 added lines, -41 removed lines patch added patch discarded remove patch
@@ -5,48 +5,48 @@
 block discarded – undo
5 5
  * Cart class.
6 6
  */
7 7
 class Cart extends AbstractCartRoute {
8
-	/**
9
-	 * The route identifier.
10
-	 *
11
-	 * @var string
12
-	 */
13
-	const IDENTIFIER = 'cart';
8
+    /**
9
+     * The route identifier.
10
+     *
11
+     * @var string
12
+     */
13
+    const IDENTIFIER = 'cart';
14 14
 
15
-	/**
16
-	 * Get the path of this REST route.
17
-	 *
18
-	 * @return string
19
-	 */
20
-	public function get_path() {
21
-		return '/cart';
22
-	}
15
+    /**
16
+     * Get the path of this REST route.
17
+     *
18
+     * @return string
19
+     */
20
+    public function get_path() {
21
+        return '/cart';
22
+    }
23 23
 
24
-	/**
25
-	 * Get method arguments for this REST route.
26
-	 *
27
-	 * @return array An array of endpoints.
28
-	 */
29
-	public function get_args() {
30
-		return [
31
-			[
32
-				'methods'             => \WP_REST_Server::READABLE,
33
-				'callback'            => [ $this, 'get_response' ],
34
-				'permission_callback' => '__return_true',
35
-				'args'                => [
36
-					'context' => $this->get_context_param( [ 'default' => 'view' ] ),
37
-				],
38
-			],
39
-			'schema' => [ $this->schema, 'get_public_item_schema' ],
40
-		];
41
-	}
24
+    /**
25
+     * Get method arguments for this REST route.
26
+     *
27
+     * @return array An array of endpoints.
28
+     */
29
+    public function get_args() {
30
+        return [
31
+            [
32
+                'methods'             => \WP_REST_Server::READABLE,
33
+                'callback'            => [ $this, 'get_response' ],
34
+                'permission_callback' => '__return_true',
35
+                'args'                => [
36
+                    'context' => $this->get_context_param( [ 'default' => 'view' ] ),
37
+                ],
38
+            ],
39
+            'schema' => [ $this->schema, 'get_public_item_schema' ],
40
+        ];
41
+    }
42 42
 
43
-	/**
44
-	 * Handle the request and return a valid response for this endpoint.
45
-	 *
46
-	 * @param \WP_REST_Request $request Request object.
47
-	 * @return \WP_REST_Response
48
-	 */
49
-	protected function get_route_response( \WP_REST_Request $request ) {
50
-		return rest_ensure_response( $this->schema->get_item_response( $this->cart_controller->get_cart_instance() ) );
51
-	}
43
+    /**
44
+     * Handle the request and return a valid response for this endpoint.
45
+     *
46
+     * @param \WP_REST_Request $request Request object.
47
+     * @return \WP_REST_Response
48
+     */
49
+    protected function get_route_response( \WP_REST_Request $request ) {
50
+        return rest_ensure_response( $this->schema->get_item_response( $this->cart_controller->get_cart_instance() ) );
51
+    }
52 52
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -30,13 +30,13 @@  discard block
 block discarded – undo
30 30
 		return [
31 31
 			[
32 32
 				'methods'             => \WP_REST_Server::READABLE,
33
-				'callback'            => [ $this, 'get_response' ],
33
+				'callback'            => [$this, 'get_response'],
34 34
 				'permission_callback' => '__return_true',
35 35
 				'args'                => [
36
-					'context' => $this->get_context_param( [ 'default' => 'view' ] ),
36
+					'context' => $this->get_context_param(['default' => 'view']),
37 37
 				],
38 38
 			],
39
-			'schema' => [ $this->schema, 'get_public_item_schema' ],
39
+			'schema' => [$this->schema, 'get_public_item_schema'],
40 40
 		];
41 41
 	}
42 42
 
@@ -46,7 +46,7 @@  discard block
 block discarded – undo
46 46
 	 * @param \WP_REST_Request $request Request object.
47 47
 	 * @return \WP_REST_Response
48 48
 	 */
49
-	protected function get_route_response( \WP_REST_Request $request ) {
50
-		return rest_ensure_response( $this->schema->get_item_response( $this->cart_controller->get_cart_instance() ) );
49
+	protected function get_route_response(\WP_REST_Request $request) {
50
+		return rest_ensure_response($this->schema->get_item_response($this->cart_controller->get_cart_instance()));
51 51
 	}
52 52
 }
Please login to merge, or discard this patch.
packages/woocommerce-blocks/src/StoreApi/Routes/V1/AbstractCartRoute.php 2 patches
Indentation   +227 added lines, -227 removed lines patch added patch discarded remove patch
@@ -13,231 +13,231 @@
 block discarded – undo
13 13
  * Abstract Cart Route
14 14
  */
15 15
 abstract class AbstractCartRoute extends AbstractRoute {
16
-	use DraftOrderTrait;
17
-
18
-	/**
19
-	 * The routes schema.
20
-	 *
21
-	 * @var string
22
-	 */
23
-	const SCHEMA_TYPE = 'cart';
24
-
25
-	/**
26
-	 * Schema class for the cart.
27
-	 *
28
-	 * @var CartSchema
29
-	 */
30
-	protected $cart_schema;
31
-
32
-	/**
33
-	 * Cart controller class instance.
34
-	 *
35
-	 * @var CartController
36
-	 */
37
-	protected $cart_controller;
38
-
39
-	/**
40
-	 * Order controller class instance.
41
-	 *
42
-	 * @var OrderController
43
-	 */
44
-	protected $order_controller;
45
-
46
-	/**
47
-	 * Constructor.
48
-	 *
49
-	 * @param SchemaController $schema_controller Schema Controller instance.
50
-	 * @param AbstractSchema   $schema Schema class for this route.
51
-	 */
52
-	public function __construct( SchemaController $schema_controller, AbstractSchema $schema ) {
53
-		$this->schema_controller = $schema_controller;
54
-		$this->schema            = $schema;
55
-		$this->cart_schema       = $this->schema_controller->get( CartSchema::IDENTIFIER );
56
-		$this->cart_item_schema  = $this->schema_controller->get( CartItemSchema::IDENTIFIER );
57
-		$this->cart_controller   = new CartController();
58
-		$this->order_controller  = new OrderController();
59
-	}
60
-
61
-	/**
62
-	 * Get the route response based on the type of request.
63
-	 *
64
-	 * @param \WP_REST_Request $request Request object.
65
-	 * @return \WP_Error|\WP_REST_Response
66
-	 */
67
-	public function get_response( \WP_REST_Request $request ) {
68
-		$this->cart_controller->load_cart();
69
-		$this->calculate_totals();
70
-
71
-		if ( $this->requires_nonce( $request ) ) {
72
-			$nonce_check = $this->check_nonce( $request );
73
-
74
-			if ( is_wp_error( $nonce_check ) ) {
75
-				return $this->add_nonce_headers( $this->error_to_response( $nonce_check ) );
76
-			}
77
-		}
78
-
79
-		try {
80
-			$response = parent::get_response( $request );
81
-		} catch ( RouteException $error ) {
82
-			$response = $this->get_route_error_response( $error->getErrorCode(), $error->getMessage(), $error->getCode(), $error->getAdditionalData() );
83
-		} catch ( \Exception $error ) {
84
-			$response = $this->get_route_error_response( 'woocommerce_rest_unknown_server_error', $error->getMessage(), 500 );
85
-		}
86
-
87
-		if ( is_wp_error( $response ) ) {
88
-			$response = $this->error_to_response( $response );
89
-		} elseif ( in_array( $request->get_method(), [ 'POST', 'PUT', 'PATCH', 'DELETE' ], true ) ) {
90
-			$this->cart_updated( $request );
91
-		}
92
-
93
-		return $this->add_nonce_headers( $response );
94
-	}
95
-
96
-	/**
97
-	 * Add nonce headers to a response object.
98
-	 *
99
-	 * @param \WP_REST_Response $response The response object.
100
-	 * @return \WP_REST_Response
101
-	 */
102
-	protected function add_nonce_headers( \WP_REST_Response $response ) {
103
-		$nonce = wp_create_nonce( 'wc_store_api' );
104
-
105
-		$response->header( 'Nonce', $nonce );
106
-		$response->header( 'Nonce-Timestamp', time() );
107
-		$response->header( 'User-ID', get_current_user_id() );
108
-
109
-		// The following headers are deprecated and should be removed in a future version.
110
-		$response->header( 'X-WC-Store-API-Nonce', $nonce );
111
-
112
-		return $response;
113
-	}
114
-
115
-	/**
116
-	 * Checks if a nonce is required for the route.
117
-	 *
118
-	 * @param \WP_REST_Request $request Request.
119
-	 * @return bool
120
-	 */
121
-	protected function requires_nonce( \WP_REST_Request $request ) {
122
-		return 'GET' !== $request->get_method();
123
-	}
124
-
125
-	/**
126
-	 * Triggered after an update to cart data. Re-calculates totals and updates draft orders (if they already exist) to
127
-	 * keep all data in sync.
128
-	 *
129
-	 * @param \WP_REST_Request $request Request object.
130
-	 */
131
-	protected function cart_updated( \WP_REST_Request $request ) {
132
-		$draft_order = $this->get_draft_order();
133
-
134
-		if ( $draft_order ) {
135
-			$this->order_controller->update_order_from_cart( $draft_order );
136
-
137
-			wc_do_deprecated_action(
138
-				'woocommerce_blocks_cart_update_order_from_request',
139
-				array(
140
-					$draft_order,
141
-					$request,
142
-				),
143
-				'7.2.0',
144
-				'woocommerce_store_api_cart_update_order_from_request',
145
-				'This action was deprecated in WooCommerce Blocks version 7.2.0. Please use woocommerce_store_api_cart_update_order_from_request instead.'
146
-			);
147
-
148
-			/**
149
-			 * Fires when the order is synced with cart data from a cart route.
150
-			 *
151
-			 * @param \WC_Order $draft_order Order object.
152
-			 * @param \WC_Customer $customer Customer object.
153
-			 * @param \WP_REST_Request $request Full details about the request.
154
-			 */
155
-			do_action( 'woocommerce_store_api_cart_update_order_from_request', $draft_order, $request );
156
-		}
157
-	}
158
-
159
-	/**
160
-	 * Ensures the cart totals are calculated before an API response is generated.
161
-	 */
162
-	protected function calculate_totals() {
163
-		wc()->cart->get_cart();
164
-		wc()->cart->calculate_fees();
165
-		wc()->cart->calculate_shipping();
166
-		wc()->cart->calculate_totals();
167
-	}
168
-
169
-	/**
170
-	 * For non-GET endpoints, require and validate a nonce to prevent CSRF attacks.
171
-	 *
172
-	 * Nonces will mismatch if the logged in session cookie is different! If using a client to test, set this cookie
173
-	 * to match the logged in cookie in your browser.
174
-	 *
175
-	 * @param \WP_REST_Request $request Request object.
176
-	 * @return \WP_Error|boolean
177
-	 */
178
-	protected function check_nonce( \WP_REST_Request $request ) {
179
-		$nonce = null;
180
-
181
-		if ( $request->get_header( 'Nonce' ) ) {
182
-			$nonce = $request->get_header( 'Nonce' );
183
-		} elseif ( $request->get_header( 'X-WC-Store-API-Nonce' ) ) {
184
-			$nonce = $request->get_header( 'X-WC-Store-API-Nonce' );
185
-
186
-			// @todo Remove handling and sending of deprecated X-WC-Store-API-Nonce Header (Blocks 7.5.0)
187
-			wc_deprecated_argument( 'X-WC-Store-API-Nonce', '7.2.0', 'Use the "Nonce" Header instead. This header will be removed after Blocks release 7.5' );
188
-			rest_handle_deprecated_argument( 'X-WC-Store-API-Nonce', 'Use the "Nonce" Header instead. This header will be removed after Blocks release 7.5', '7.2.0' );
189
-		}
190
-
191
-		/**
192
-		 * Filters the Store API nonce check.
193
-		 *
194
-		 * This can be used to disable the nonce check when testing API endpoints via a REST API client.
195
-		 *
196
-		 * @param boolean $disable_nonce_check If true, nonce checks will be disabled.
197
-		 * @return boolean
198
-		 */
199
-		if ( apply_filters( 'woocommerce_store_api_disable_nonce_check', false ) ) {
200
-			return true;
201
-		}
202
-
203
-		if ( null === $nonce ) {
204
-			return $this->get_route_error_response( 'woocommerce_rest_missing_nonce', __( 'Missing the Nonce header. This endpoint requires a valid nonce.', 'woocommerce' ), 401 );
205
-		}
206
-
207
-		if ( ! wp_verify_nonce( $nonce, 'wc_store_api' ) ) {
208
-			return $this->get_route_error_response( 'woocommerce_rest_invalid_nonce', __( 'Nonce is invalid.', 'woocommerce' ), 403 );
209
-		}
210
-
211
-		return true;
212
-	}
213
-
214
-	/**
215
-	 * Get route response when something went wrong.
216
-	 *
217
-	 * @param string $error_code String based error code.
218
-	 * @param string $error_message User facing error message.
219
-	 * @param int    $http_status_code HTTP status. Defaults to 500.
220
-	 * @param array  $additional_data  Extra data (key value pairs) to expose in the error response.
221
-	 * @return \WP_Error WP Error object.
222
-	 */
223
-	protected function get_route_error_response( $error_code, $error_message, $http_status_code = 500, $additional_data = [] ) {
224
-		switch ( $http_status_code ) {
225
-			case 409:
226
-				// If there was a conflict, return the cart so the client can resolve it.
227
-				$cart = $this->cart_controller->get_cart_instance();
228
-
229
-				return new \WP_Error(
230
-					$error_code,
231
-					$error_message,
232
-					array_merge(
233
-						$additional_data,
234
-						[
235
-							'status' => $http_status_code,
236
-							'cart'   => $this->cart_schema->get_item_response( $cart ),
237
-						]
238
-					)
239
-				);
240
-		}
241
-		return new \WP_Error( $error_code, $error_message, [ 'status' => $http_status_code ] );
242
-	}
16
+    use DraftOrderTrait;
17
+
18
+    /**
19
+     * The routes schema.
20
+     *
21
+     * @var string
22
+     */
23
+    const SCHEMA_TYPE = 'cart';
24
+
25
+    /**
26
+     * Schema class for the cart.
27
+     *
28
+     * @var CartSchema
29
+     */
30
+    protected $cart_schema;
31
+
32
+    /**
33
+     * Cart controller class instance.
34
+     *
35
+     * @var CartController
36
+     */
37
+    protected $cart_controller;
38
+
39
+    /**
40
+     * Order controller class instance.
41
+     *
42
+     * @var OrderController
43
+     */
44
+    protected $order_controller;
45
+
46
+    /**
47
+     * Constructor.
48
+     *
49
+     * @param SchemaController $schema_controller Schema Controller instance.
50
+     * @param AbstractSchema   $schema Schema class for this route.
51
+     */
52
+    public function __construct( SchemaController $schema_controller, AbstractSchema $schema ) {
53
+        $this->schema_controller = $schema_controller;
54
+        $this->schema            = $schema;
55
+        $this->cart_schema       = $this->schema_controller->get( CartSchema::IDENTIFIER );
56
+        $this->cart_item_schema  = $this->schema_controller->get( CartItemSchema::IDENTIFIER );
57
+        $this->cart_controller   = new CartController();
58
+        $this->order_controller  = new OrderController();
59
+    }
60
+
61
+    /**
62
+     * Get the route response based on the type of request.
63
+     *
64
+     * @param \WP_REST_Request $request Request object.
65
+     * @return \WP_Error|\WP_REST_Response
66
+     */
67
+    public function get_response( \WP_REST_Request $request ) {
68
+        $this->cart_controller->load_cart();
69
+        $this->calculate_totals();
70
+
71
+        if ( $this->requires_nonce( $request ) ) {
72
+            $nonce_check = $this->check_nonce( $request );
73
+
74
+            if ( is_wp_error( $nonce_check ) ) {
75
+                return $this->add_nonce_headers( $this->error_to_response( $nonce_check ) );
76
+            }
77
+        }
78
+
79
+        try {
80
+            $response = parent::get_response( $request );
81
+        } catch ( RouteException $error ) {
82
+            $response = $this->get_route_error_response( $error->getErrorCode(), $error->getMessage(), $error->getCode(), $error->getAdditionalData() );
83
+        } catch ( \Exception $error ) {
84
+            $response = $this->get_route_error_response( 'woocommerce_rest_unknown_server_error', $error->getMessage(), 500 );
85
+        }
86
+
87
+        if ( is_wp_error( $response ) ) {
88
+            $response = $this->error_to_response( $response );
89
+        } elseif ( in_array( $request->get_method(), [ 'POST', 'PUT', 'PATCH', 'DELETE' ], true ) ) {
90
+            $this->cart_updated( $request );
91
+        }
92
+
93
+        return $this->add_nonce_headers( $response );
94
+    }
95
+
96
+    /**
97
+     * Add nonce headers to a response object.
98
+     *
99
+     * @param \WP_REST_Response $response The response object.
100
+     * @return \WP_REST_Response
101
+     */
102
+    protected function add_nonce_headers( \WP_REST_Response $response ) {
103
+        $nonce = wp_create_nonce( 'wc_store_api' );
104
+
105
+        $response->header( 'Nonce', $nonce );
106
+        $response->header( 'Nonce-Timestamp', time() );
107
+        $response->header( 'User-ID', get_current_user_id() );
108
+
109
+        // The following headers are deprecated and should be removed in a future version.
110
+        $response->header( 'X-WC-Store-API-Nonce', $nonce );
111
+
112
+        return $response;
113
+    }
114
+
115
+    /**
116
+     * Checks if a nonce is required for the route.
117
+     *
118
+     * @param \WP_REST_Request $request Request.
119
+     * @return bool
120
+     */
121
+    protected function requires_nonce( \WP_REST_Request $request ) {
122
+        return 'GET' !== $request->get_method();
123
+    }
124
+
125
+    /**
126
+     * Triggered after an update to cart data. Re-calculates totals and updates draft orders (if they already exist) to
127
+     * keep all data in sync.
128
+     *
129
+     * @param \WP_REST_Request $request Request object.
130
+     */
131
+    protected function cart_updated( \WP_REST_Request $request ) {
132
+        $draft_order = $this->get_draft_order();
133
+
134
+        if ( $draft_order ) {
135
+            $this->order_controller->update_order_from_cart( $draft_order );
136
+
137
+            wc_do_deprecated_action(
138
+                'woocommerce_blocks_cart_update_order_from_request',
139
+                array(
140
+                    $draft_order,
141
+                    $request,
142
+                ),
143
+                '7.2.0',
144
+                'woocommerce_store_api_cart_update_order_from_request',
145
+                'This action was deprecated in WooCommerce Blocks version 7.2.0. Please use woocommerce_store_api_cart_update_order_from_request instead.'
146
+            );
147
+
148
+            /**
149
+             * Fires when the order is synced with cart data from a cart route.
150
+             *
151
+             * @param \WC_Order $draft_order Order object.
152
+             * @param \WC_Customer $customer Customer object.
153
+             * @param \WP_REST_Request $request Full details about the request.
154
+             */
155
+            do_action( 'woocommerce_store_api_cart_update_order_from_request', $draft_order, $request );
156
+        }
157
+    }
158
+
159
+    /**
160
+     * Ensures the cart totals are calculated before an API response is generated.
161
+     */
162
+    protected function calculate_totals() {
163
+        wc()->cart->get_cart();
164
+        wc()->cart->calculate_fees();
165
+        wc()->cart->calculate_shipping();
166
+        wc()->cart->calculate_totals();
167
+    }
168
+
169
+    /**
170
+     * For non-GET endpoints, require and validate a nonce to prevent CSRF attacks.
171
+     *
172
+     * Nonces will mismatch if the logged in session cookie is different! If using a client to test, set this cookie
173
+     * to match the logged in cookie in your browser.
174
+     *
175
+     * @param \WP_REST_Request $request Request object.
176
+     * @return \WP_Error|boolean
177
+     */
178
+    protected function check_nonce( \WP_REST_Request $request ) {
179
+        $nonce = null;
180
+
181
+        if ( $request->get_header( 'Nonce' ) ) {
182
+            $nonce = $request->get_header( 'Nonce' );
183
+        } elseif ( $request->get_header( 'X-WC-Store-API-Nonce' ) ) {
184
+            $nonce = $request->get_header( 'X-WC-Store-API-Nonce' );
185
+
186
+            // @todo Remove handling and sending of deprecated X-WC-Store-API-Nonce Header (Blocks 7.5.0)
187
+            wc_deprecated_argument( 'X-WC-Store-API-Nonce', '7.2.0', 'Use the "Nonce" Header instead. This header will be removed after Blocks release 7.5' );
188
+            rest_handle_deprecated_argument( 'X-WC-Store-API-Nonce', 'Use the "Nonce" Header instead. This header will be removed after Blocks release 7.5', '7.2.0' );
189
+        }
190
+
191
+        /**
192
+         * Filters the Store API nonce check.
193
+         *
194
+         * This can be used to disable the nonce check when testing API endpoints via a REST API client.
195
+         *
196
+         * @param boolean $disable_nonce_check If true, nonce checks will be disabled.
197
+         * @return boolean
198
+         */
199
+        if ( apply_filters( 'woocommerce_store_api_disable_nonce_check', false ) ) {
200
+            return true;
201
+        }
202
+
203
+        if ( null === $nonce ) {
204
+            return $this->get_route_error_response( 'woocommerce_rest_missing_nonce', __( 'Missing the Nonce header. This endpoint requires a valid nonce.', 'woocommerce' ), 401 );
205
+        }
206
+
207
+        if ( ! wp_verify_nonce( $nonce, 'wc_store_api' ) ) {
208
+            return $this->get_route_error_response( 'woocommerce_rest_invalid_nonce', __( 'Nonce is invalid.', 'woocommerce' ), 403 );
209
+        }
210
+
211
+        return true;
212
+    }
213
+
214
+    /**
215
+     * Get route response when something went wrong.
216
+     *
217
+     * @param string $error_code String based error code.
218
+     * @param string $error_message User facing error message.
219
+     * @param int    $http_status_code HTTP status. Defaults to 500.
220
+     * @param array  $additional_data  Extra data (key value pairs) to expose in the error response.
221
+     * @return \WP_Error WP Error object.
222
+     */
223
+    protected function get_route_error_response( $error_code, $error_message, $http_status_code = 500, $additional_data = [] ) {
224
+        switch ( $http_status_code ) {
225
+            case 409:
226
+                // If there was a conflict, return the cart so the client can resolve it.
227
+                $cart = $this->cart_controller->get_cart_instance();
228
+
229
+                return new \WP_Error(
230
+                    $error_code,
231
+                    $error_message,
232
+                    array_merge(
233
+                        $additional_data,
234
+                        [
235
+                            'status' => $http_status_code,
236
+                            'cart'   => $this->cart_schema->get_item_response( $cart ),
237
+                        ]
238
+                    )
239
+                );
240
+        }
241
+        return new \WP_Error( $error_code, $error_message, [ 'status' => $http_status_code ] );
242
+    }
243 243
 }
Please login to merge, or discard this patch.
Spacing   +45 added lines, -45 removed lines patch added patch discarded remove patch
@@ -49,11 +49,11 @@  discard block
 block discarded – undo
49 49
 	 * @param SchemaController $schema_controller Schema Controller instance.
50 50
 	 * @param AbstractSchema   $schema Schema class for this route.
51 51
 	 */
52
-	public function __construct( SchemaController $schema_controller, AbstractSchema $schema ) {
52
+	public function __construct(SchemaController $schema_controller, AbstractSchema $schema) {
53 53
 		$this->schema_controller = $schema_controller;
54 54
 		$this->schema            = $schema;
55
-		$this->cart_schema       = $this->schema_controller->get( CartSchema::IDENTIFIER );
56
-		$this->cart_item_schema  = $this->schema_controller->get( CartItemSchema::IDENTIFIER );
55
+		$this->cart_schema       = $this->schema_controller->get(CartSchema::IDENTIFIER);
56
+		$this->cart_item_schema  = $this->schema_controller->get(CartItemSchema::IDENTIFIER);
57 57
 		$this->cart_controller   = new CartController();
58 58
 		$this->order_controller  = new OrderController();
59 59
 	}
@@ -64,33 +64,33 @@  discard block
 block discarded – undo
64 64
 	 * @param \WP_REST_Request $request Request object.
65 65
 	 * @return \WP_Error|\WP_REST_Response
66 66
 	 */
67
-	public function get_response( \WP_REST_Request $request ) {
67
+	public function get_response(\WP_REST_Request $request) {
68 68
 		$this->cart_controller->load_cart();
69 69
 		$this->calculate_totals();
70 70
 
71
-		if ( $this->requires_nonce( $request ) ) {
72
-			$nonce_check = $this->check_nonce( $request );
71
+		if ($this->requires_nonce($request)) {
72
+			$nonce_check = $this->check_nonce($request);
73 73
 
74
-			if ( is_wp_error( $nonce_check ) ) {
75
-				return $this->add_nonce_headers( $this->error_to_response( $nonce_check ) );
74
+			if (is_wp_error($nonce_check)) {
75
+				return $this->add_nonce_headers($this->error_to_response($nonce_check));
76 76
 			}
77 77
 		}
78 78
 
79 79
 		try {
80
-			$response = parent::get_response( $request );
81
-		} catch ( RouteException $error ) {
82
-			$response = $this->get_route_error_response( $error->getErrorCode(), $error->getMessage(), $error->getCode(), $error->getAdditionalData() );
83
-		} catch ( \Exception $error ) {
84
-			$response = $this->get_route_error_response( 'woocommerce_rest_unknown_server_error', $error->getMessage(), 500 );
80
+			$response = parent::get_response($request);
81
+		} catch (RouteException $error) {
82
+			$response = $this->get_route_error_response($error->getErrorCode(), $error->getMessage(), $error->getCode(), $error->getAdditionalData());
83
+		} catch (\Exception $error) {
84
+			$response = $this->get_route_error_response('woocommerce_rest_unknown_server_error', $error->getMessage(), 500);
85 85
 		}
86 86
 
87
-		if ( is_wp_error( $response ) ) {
88
-			$response = $this->error_to_response( $response );
89
-		} elseif ( in_array( $request->get_method(), [ 'POST', 'PUT', 'PATCH', 'DELETE' ], true ) ) {
90
-			$this->cart_updated( $request );
87
+		if (is_wp_error($response)) {
88
+			$response = $this->error_to_response($response);
89
+		} elseif (in_array($request->get_method(), ['POST', 'PUT', 'PATCH', 'DELETE'], true)) {
90
+			$this->cart_updated($request);
91 91
 		}
92 92
 
93
-		return $this->add_nonce_headers( $response );
93
+		return $this->add_nonce_headers($response);
94 94
 	}
95 95
 
96 96
 	/**
@@ -99,15 +99,15 @@  discard block
 block discarded – undo
99 99
 	 * @param \WP_REST_Response $response The response object.
100 100
 	 * @return \WP_REST_Response
101 101
 	 */
102
-	protected function add_nonce_headers( \WP_REST_Response $response ) {
103
-		$nonce = wp_create_nonce( 'wc_store_api' );
102
+	protected function add_nonce_headers(\WP_REST_Response $response) {
103
+		$nonce = wp_create_nonce('wc_store_api');
104 104
 
105
-		$response->header( 'Nonce', $nonce );
106
-		$response->header( 'Nonce-Timestamp', time() );
107
-		$response->header( 'User-ID', get_current_user_id() );
105
+		$response->header('Nonce', $nonce);
106
+		$response->header('Nonce-Timestamp', time());
107
+		$response->header('User-ID', get_current_user_id());
108 108
 
109 109
 		// The following headers are deprecated and should be removed in a future version.
110
-		$response->header( 'X-WC-Store-API-Nonce', $nonce );
110
+		$response->header('X-WC-Store-API-Nonce', $nonce);
111 111
 
112 112
 		return $response;
113 113
 	}
@@ -118,7 +118,7 @@  discard block
 block discarded – undo
118 118
 	 * @param \WP_REST_Request $request Request.
119 119
 	 * @return bool
120 120
 	 */
121
-	protected function requires_nonce( \WP_REST_Request $request ) {
121
+	protected function requires_nonce(\WP_REST_Request $request) {
122 122
 		return 'GET' !== $request->get_method();
123 123
 	}
124 124
 
@@ -128,11 +128,11 @@  discard block
 block discarded – undo
128 128
 	 *
129 129
 	 * @param \WP_REST_Request $request Request object.
130 130
 	 */
131
-	protected function cart_updated( \WP_REST_Request $request ) {
131
+	protected function cart_updated(\WP_REST_Request $request) {
132 132
 		$draft_order = $this->get_draft_order();
133 133
 
134
-		if ( $draft_order ) {
135
-			$this->order_controller->update_order_from_cart( $draft_order );
134
+		if ($draft_order) {
135
+			$this->order_controller->update_order_from_cart($draft_order);
136 136
 
137 137
 			wc_do_deprecated_action(
138 138
 				'woocommerce_blocks_cart_update_order_from_request',
@@ -152,7 +152,7 @@  discard block
 block discarded – undo
152 152
 			 * @param \WC_Customer $customer Customer object.
153 153
 			 * @param \WP_REST_Request $request Full details about the request.
154 154
 			 */
155
-			do_action( 'woocommerce_store_api_cart_update_order_from_request', $draft_order, $request );
155
+			do_action('woocommerce_store_api_cart_update_order_from_request', $draft_order, $request);
156 156
 		}
157 157
 	}
158 158
 
@@ -175,17 +175,17 @@  discard block
 block discarded – undo
175 175
 	 * @param \WP_REST_Request $request Request object.
176 176
 	 * @return \WP_Error|boolean
177 177
 	 */
178
-	protected function check_nonce( \WP_REST_Request $request ) {
178
+	protected function check_nonce(\WP_REST_Request $request) {
179 179
 		$nonce = null;
180 180
 
181
-		if ( $request->get_header( 'Nonce' ) ) {
182
-			$nonce = $request->get_header( 'Nonce' );
183
-		} elseif ( $request->get_header( 'X-WC-Store-API-Nonce' ) ) {
184
-			$nonce = $request->get_header( 'X-WC-Store-API-Nonce' );
181
+		if ($request->get_header('Nonce')) {
182
+			$nonce = $request->get_header('Nonce');
183
+		} elseif ($request->get_header('X-WC-Store-API-Nonce')) {
184
+			$nonce = $request->get_header('X-WC-Store-API-Nonce');
185 185
 
186 186
 			// @todo Remove handling and sending of deprecated X-WC-Store-API-Nonce Header (Blocks 7.5.0)
187
-			wc_deprecated_argument( 'X-WC-Store-API-Nonce', '7.2.0', 'Use the "Nonce" Header instead. This header will be removed after Blocks release 7.5' );
188
-			rest_handle_deprecated_argument( 'X-WC-Store-API-Nonce', 'Use the "Nonce" Header instead. This header will be removed after Blocks release 7.5', '7.2.0' );
187
+			wc_deprecated_argument('X-WC-Store-API-Nonce', '7.2.0', 'Use the "Nonce" Header instead. This header will be removed after Blocks release 7.5');
188
+			rest_handle_deprecated_argument('X-WC-Store-API-Nonce', 'Use the "Nonce" Header instead. This header will be removed after Blocks release 7.5', '7.2.0');
189 189
 		}
190 190
 
191 191
 		/**
@@ -196,16 +196,16 @@  discard block
 block discarded – undo
196 196
 		 * @param boolean $disable_nonce_check If true, nonce checks will be disabled.
197 197
 		 * @return boolean
198 198
 		 */
199
-		if ( apply_filters( 'woocommerce_store_api_disable_nonce_check', false ) ) {
199
+		if (apply_filters('woocommerce_store_api_disable_nonce_check', false)) {
200 200
 			return true;
201 201
 		}
202 202
 
203
-		if ( null === $nonce ) {
204
-			return $this->get_route_error_response( 'woocommerce_rest_missing_nonce', __( 'Missing the Nonce header. This endpoint requires a valid nonce.', 'woocommerce' ), 401 );
203
+		if (null === $nonce) {
204
+			return $this->get_route_error_response('woocommerce_rest_missing_nonce', __('Missing the Nonce header. This endpoint requires a valid nonce.', 'woocommerce'), 401);
205 205
 		}
206 206
 
207
-		if ( ! wp_verify_nonce( $nonce, 'wc_store_api' ) ) {
208
-			return $this->get_route_error_response( 'woocommerce_rest_invalid_nonce', __( 'Nonce is invalid.', 'woocommerce' ), 403 );
207
+		if (!wp_verify_nonce($nonce, 'wc_store_api')) {
208
+			return $this->get_route_error_response('woocommerce_rest_invalid_nonce', __('Nonce is invalid.', 'woocommerce'), 403);
209 209
 		}
210 210
 
211 211
 		return true;
@@ -220,8 +220,8 @@  discard block
 block discarded – undo
220 220
 	 * @param array  $additional_data  Extra data (key value pairs) to expose in the error response.
221 221
 	 * @return \WP_Error WP Error object.
222 222
 	 */
223
-	protected function get_route_error_response( $error_code, $error_message, $http_status_code = 500, $additional_data = [] ) {
224
-		switch ( $http_status_code ) {
223
+	protected function get_route_error_response($error_code, $error_message, $http_status_code = 500, $additional_data = []) {
224
+		switch ($http_status_code) {
225 225
 			case 409:
226 226
 				// If there was a conflict, return the cart so the client can resolve it.
227 227
 				$cart = $this->cart_controller->get_cart_instance();
@@ -233,11 +233,11 @@  discard block
 block discarded – undo
233 233
 						$additional_data,
234 234
 						[
235 235
 							'status' => $http_status_code,
236
-							'cart'   => $this->cart_schema->get_item_response( $cart ),
236
+							'cart'   => $this->cart_schema->get_item_response($cart),
237 237
 						]
238 238
 					)
239 239
 				);
240 240
 		}
241
-		return new \WP_Error( $error_code, $error_message, [ 'status' => $http_status_code ] );
241
+		return new \WP_Error($error_code, $error_message, ['status' => $http_status_code]);
242 242
 	}
243 243
 }
Please login to merge, or discard this patch.