@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | |
10 | 10 | namespace WooCommerce\RestApi\Controllers\Version4\Data; |
11 | 11 | |
12 | -defined( 'ABSPATH' ) || exit; |
|
12 | +defined('ABSPATH') || exit; |
|
13 | 13 | |
14 | 14 | use \WooCommerce\RestApi\Controllers\Version4\Data as DataController; |
15 | 15 | |
@@ -35,10 +35,10 @@ discard block |
||
35 | 35 | array( |
36 | 36 | array( |
37 | 37 | 'methods' => \WP_REST_Server::READABLE, |
38 | - 'callback' => array( $this, 'get_items' ), |
|
39 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
38 | + 'callback' => array($this, 'get_items'), |
|
39 | + 'permission_callback' => array($this, 'get_items_permissions_check'), |
|
40 | 40 | ), |
41 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
41 | + 'schema' => array($this, 'get_public_item_schema'), |
|
42 | 42 | ) |
43 | 43 | ); |
44 | 44 | register_rest_route( |
@@ -47,10 +47,10 @@ discard block |
||
47 | 47 | array( |
48 | 48 | array( |
49 | 49 | 'methods' => \WP_REST_Server::READABLE, |
50 | - 'callback' => array( $this, 'get_current_item' ), |
|
51 | - 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
|
50 | + 'callback' => array($this, 'get_current_item'), |
|
51 | + 'permission_callback' => array($this, 'get_item_permissions_check'), |
|
52 | 52 | ), |
53 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
53 | + 'schema' => array($this, 'get_public_item_schema'), |
|
54 | 54 | ) |
55 | 55 | ); |
56 | 56 | register_rest_route( |
@@ -59,16 +59,16 @@ discard block |
||
59 | 59 | array( |
60 | 60 | array( |
61 | 61 | 'methods' => \WP_REST_Server::READABLE, |
62 | - 'callback' => array( $this, 'get_item' ), |
|
63 | - 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
|
62 | + 'callback' => array($this, 'get_item'), |
|
63 | + 'permission_callback' => array($this, 'get_item_permissions_check'), |
|
64 | 64 | 'args' => array( |
65 | 65 | 'location' => array( |
66 | - 'description' => __( 'ISO4217 currency code.', 'woocommerce' ), |
|
66 | + 'description' => __('ISO4217 currency code.', 'woocommerce'), |
|
67 | 67 | 'type' => 'string', |
68 | 68 | ), |
69 | 69 | ), |
70 | 70 | ), |
71 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
71 | + 'schema' => array($this, 'get_public_item_schema'), |
|
72 | 72 | ) |
73 | 73 | ); |
74 | 74 | } |
@@ -80,18 +80,18 @@ discard block |
||
80 | 80 | * @param \WP_REST_Request $request Request data. |
81 | 81 | * @return array|mixed Response data, ready for insertion into collection data. |
82 | 82 | */ |
83 | - public function get_currency( $code = false, $request ) { |
|
83 | + public function get_currency($code = false, $request) { |
|
84 | 84 | $currencies = get_woocommerce_currencies(); |
85 | 85 | $data = array(); |
86 | 86 | |
87 | - if ( ! array_key_exists( $code, $currencies ) ) { |
|
87 | + if ( ! array_key_exists($code, $currencies)) { |
|
88 | 88 | return false; |
89 | 89 | } |
90 | 90 | |
91 | 91 | $currency = array( |
92 | 92 | 'code' => $code, |
93 | - 'name' => $currencies[ $code ], |
|
94 | - 'symbol' => get_woocommerce_currency_symbol( $code ), |
|
93 | + 'name' => $currencies[$code], |
|
94 | + 'symbol' => get_woocommerce_currency_symbol($code), |
|
95 | 95 | ); |
96 | 96 | |
97 | 97 | return $currency; |
@@ -103,15 +103,15 @@ discard block |
||
103 | 103 | * @param \WP_REST_Request $request Request data. |
104 | 104 | * @return \WP_Error\WP_REST_Response |
105 | 105 | */ |
106 | - public function get_items( $request ) { |
|
106 | + public function get_items($request) { |
|
107 | 107 | $currencies = get_woocommerce_currencies(); |
108 | - foreach ( array_keys( $currencies ) as $code ) { |
|
109 | - $currency = $this->get_currency( $code, $request ); |
|
110 | - $response = $this->prepare_item_for_response( $currency, $request ); |
|
111 | - $data[] = $this->prepare_response_for_collection( $response ); |
|
108 | + foreach (array_keys($currencies) as $code) { |
|
109 | + $currency = $this->get_currency($code, $request); |
|
110 | + $response = $this->prepare_item_for_response($currency, $request); |
|
111 | + $data[] = $this->prepare_response_for_collection($response); |
|
112 | 112 | } |
113 | 113 | |
114 | - return rest_ensure_response( $data ); |
|
114 | + return rest_ensure_response($data); |
|
115 | 115 | } |
116 | 116 | |
117 | 117 | /** |
@@ -120,12 +120,12 @@ discard block |
||
120 | 120 | * @param \WP_REST_Request $request Request data. |
121 | 121 | * @return \WP_Error\WP_REST_Response |
122 | 122 | */ |
123 | - public function get_item( $request ) { |
|
124 | - $data = $this->get_currency( strtoupper( $request['currency'] ), $request ); |
|
125 | - if ( empty( $data ) ) { |
|
126 | - return new \WP_Error( 'woocommerce_rest_data_invalid_currency', __( 'There are no currencies matching these parameters.', 'woocommerce' ), array( 'status' => 404 ) ); |
|
123 | + public function get_item($request) { |
|
124 | + $data = $this->get_currency(strtoupper($request['currency']), $request); |
|
125 | + if (empty($data)) { |
|
126 | + return new \WP_Error('woocommerce_rest_data_invalid_currency', __('There are no currencies matching these parameters.', 'woocommerce'), array('status' => 404)); |
|
127 | 127 | } |
128 | - return $this->prepare_item_for_response( $data, $request ); |
|
128 | + return $this->prepare_item_for_response($data, $request); |
|
129 | 129 | } |
130 | 130 | |
131 | 131 | /** |
@@ -134,9 +134,9 @@ discard block |
||
134 | 134 | * @param \WP_REST_Request $request Request data. |
135 | 135 | * @return \WP_Error\WP_REST_Response |
136 | 136 | */ |
137 | - public function get_current_item( $request ) { |
|
138 | - $currency = get_option( 'woocommerce_currency' ); |
|
139 | - return $this->prepare_item_for_response( $this->get_currency( $currency, $request ), $request ); |
|
137 | + public function get_current_item($request) { |
|
138 | + $currency = get_option('woocommerce_currency'); |
|
139 | + return $this->prepare_item_for_response($this->get_currency($currency, $request), $request); |
|
140 | 140 | } |
141 | 141 | |
142 | 142 | /** |
@@ -146,12 +146,12 @@ discard block |
||
146 | 146 | * @param \WP_REST_Request $request Request object. |
147 | 147 | * @return \WP_REST_Response $response Response data. |
148 | 148 | */ |
149 | - public function prepare_item_for_response( $item, $request ) { |
|
150 | - $data = $this->add_additional_fields_to_object( $item, $request ); |
|
151 | - $data = $this->filter_response_by_context( $data, 'view' ); |
|
152 | - $response = rest_ensure_response( $data ); |
|
149 | + public function prepare_item_for_response($item, $request) { |
|
150 | + $data = $this->add_additional_fields_to_object($item, $request); |
|
151 | + $data = $this->filter_response_by_context($data, 'view'); |
|
152 | + $response = rest_ensure_response($data); |
|
153 | 153 | |
154 | - $response->add_links( $this->prepare_links( $item ) ); |
|
154 | + $response->add_links($this->prepare_links($item)); |
|
155 | 155 | |
156 | 156 | /** |
157 | 157 | * Filter currency returned from the API. |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | * @param array $item Currency data. |
161 | 161 | * @param \WP_REST_Request $request Request used to generate the response. |
162 | 162 | */ |
163 | - return apply_filters( 'woocommerce_rest_prepare_data_currency', $response, $item, $request ); |
|
163 | + return apply_filters('woocommerce_rest_prepare_data_currency', $response, $item, $request); |
|
164 | 164 | } |
165 | 165 | |
166 | 166 | /** |
@@ -169,14 +169,14 @@ discard block |
||
169 | 169 | * @param object $item Data object. |
170 | 170 | * @return array Links for the given currency. |
171 | 171 | */ |
172 | - protected function prepare_links( $item ) { |
|
173 | - $code = strtoupper( $item['code'] ); |
|
172 | + protected function prepare_links($item) { |
|
173 | + $code = strtoupper($item['code']); |
|
174 | 174 | $links = array( |
175 | 175 | 'self' => array( |
176 | - 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $code ) ), |
|
176 | + 'href' => rest_url(sprintf('/%s/%s/%s', $this->namespace, $this->rest_base, $code)), |
|
177 | 177 | ), |
178 | 178 | 'collection' => array( |
179 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
179 | + 'href' => rest_url(sprintf('/%s/%s', $this->namespace, $this->rest_base)), |
|
180 | 180 | ), |
181 | 181 | ); |
182 | 182 | |
@@ -197,25 +197,25 @@ discard block |
||
197 | 197 | 'properties' => array( |
198 | 198 | 'code' => array( |
199 | 199 | 'type' => 'string', |
200 | - 'description' => __( 'ISO4217 currency code.', 'woocommerce' ), |
|
201 | - 'context' => array( 'view' ), |
|
200 | + 'description' => __('ISO4217 currency code.', 'woocommerce'), |
|
201 | + 'context' => array('view'), |
|
202 | 202 | 'readonly' => true, |
203 | 203 | ), |
204 | 204 | 'name' => array( |
205 | 205 | 'type' => 'string', |
206 | - 'description' => __( 'Full name of currency.', 'woocommerce' ), |
|
207 | - 'context' => array( 'view' ), |
|
206 | + 'description' => __('Full name of currency.', 'woocommerce'), |
|
207 | + 'context' => array('view'), |
|
208 | 208 | 'readonly' => true, |
209 | 209 | ), |
210 | 210 | 'symbol' => array( |
211 | 211 | 'type' => 'string', |
212 | - 'description' => __( 'Currency symbol.', 'woocommerce' ), |
|
213 | - 'context' => array( 'view' ), |
|
212 | + 'description' => __('Currency symbol.', 'woocommerce'), |
|
213 | + 'context' => array('view'), |
|
214 | 214 | 'readonly' => true, |
215 | 215 | ), |
216 | 216 | ), |
217 | 217 | ); |
218 | 218 | |
219 | - return $this->add_additional_fields_schema( $schema ); |
|
219 | + return $this->add_additional_fields_schema($schema); |
|
220 | 220 | } |
221 | 221 | } |
@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | |
10 | 10 | namespace WooCommerce\RestApi\Controllers\Version4\Data; |
11 | 11 | |
12 | -defined( 'ABSPATH' ) || exit; |
|
12 | +defined('ABSPATH') || exit; |
|
13 | 13 | |
14 | 14 | use \WooCommerce\RestApi\Controllers\Version4\Data as DataController; |
15 | 15 | |
@@ -37,10 +37,10 @@ discard block |
||
37 | 37 | array( |
38 | 38 | array( |
39 | 39 | 'methods' => \WP_REST_Server::READABLE, |
40 | - 'callback' => array( $this, 'get_items' ), |
|
41 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
40 | + 'callback' => array($this, 'get_items'), |
|
41 | + 'permission_callback' => array($this, 'get_items_permissions_check'), |
|
42 | 42 | ), |
43 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
43 | + 'schema' => array($this, 'get_public_item_schema'), |
|
44 | 44 | ) |
45 | 45 | ); |
46 | 46 | } |
@@ -52,10 +52,10 @@ discard block |
||
52 | 52 | * @param \WP_REST_Request $request Request data. |
53 | 53 | * @return \WP_Error\WP_REST_Response |
54 | 54 | */ |
55 | - public function get_items( $request ) { |
|
55 | + public function get_items($request) { |
|
56 | 56 | global $wpdb; |
57 | 57 | |
58 | - if ( isset( $request['match'] ) ) { |
|
58 | + if (isset($request['match'])) { |
|
59 | 59 | $downloads = $wpdb->get_results( |
60 | 60 | $wpdb->prepare( |
61 | 61 | "SELECT DISTINCT( user_ip_address ) FROM {$wpdb->prefix}wc_download_log |
@@ -65,19 +65,19 @@ discard block |
||
65 | 65 | ) |
66 | 66 | ); |
67 | 67 | } else { |
68 | - return new \WP_Error( 'woocommerce_rest_data_download_ips_invalid_request', __( 'Invalid request. Please pass the match parameter.', 'woocommerce' ), array( 'status' => 400 ) ); |
|
68 | + return new \WP_Error('woocommerce_rest_data_download_ips_invalid_request', __('Invalid request. Please pass the match parameter.', 'woocommerce'), array('status' => 400)); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | $data = array(); |
72 | 72 | |
73 | - if ( ! empty( $downloads ) ) { |
|
74 | - foreach ( $downloads as $download ) { |
|
75 | - $response = $this->prepare_item_for_response( $download, $request ); |
|
76 | - $data[] = $this->prepare_response_for_collection( $response ); |
|
73 | + if ( ! empty($downloads)) { |
|
74 | + foreach ($downloads as $download) { |
|
75 | + $response = $this->prepare_item_for_response($download, $request); |
|
76 | + $data[] = $this->prepare_response_for_collection($response); |
|
77 | 77 | } |
78 | 78 | } |
79 | 79 | |
80 | - return rest_ensure_response( $data ); |
|
80 | + return rest_ensure_response($data); |
|
81 | 81 | } |
82 | 82 | |
83 | 83 | /** |
@@ -88,12 +88,12 @@ discard block |
||
88 | 88 | * @param \WP_REST_Request $request Request object. |
89 | 89 | * @return \WP_REST_Response $response Response data. |
90 | 90 | */ |
91 | - public function prepare_item_for_response( $item, $request ) { |
|
92 | - $data = $this->add_additional_fields_to_object( $item, $request ); |
|
93 | - $data = $this->filter_response_by_context( $data, 'view' ); |
|
94 | - $response = rest_ensure_response( $data ); |
|
91 | + public function prepare_item_for_response($item, $request) { |
|
92 | + $data = $this->add_additional_fields_to_object($item, $request); |
|
93 | + $data = $this->filter_response_by_context($data, 'view'); |
|
94 | + $response = rest_ensure_response($data); |
|
95 | 95 | |
96 | - $response->add_links( $this->prepare_links( $item ) ); |
|
96 | + $response->add_links($this->prepare_links($item)); |
|
97 | 97 | |
98 | 98 | /** |
99 | 99 | * Filter the list returned from the API. |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | * @param array $item The original item. |
103 | 103 | * @param \WP_REST_Request $request Request used to generate the response. |
104 | 104 | */ |
105 | - return apply_filters( 'woocommerce_rest_prepare_data_download_ip', $response, $item, $request ); |
|
105 | + return apply_filters('woocommerce_rest_prepare_data_download_ip', $response, $item, $request); |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | /** |
@@ -111,10 +111,10 @@ discard block |
||
111 | 111 | * @param object $item Data object. |
112 | 112 | * @return array Links for the given object. |
113 | 113 | */ |
114 | - protected function prepare_links( $item ) { |
|
114 | + protected function prepare_links($item) { |
|
115 | 115 | $links = array( |
116 | 116 | 'collection' => array( |
117 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
117 | + 'href' => rest_url(sprintf('/%s/%s', $this->namespace, $this->rest_base)), |
|
118 | 118 | ), |
119 | 119 | ); |
120 | 120 | return $links; |
@@ -127,9 +127,9 @@ discard block |
||
127 | 127 | */ |
128 | 128 | public function get_collection_params() { |
129 | 129 | $params = array(); |
130 | - $params['context'] = $this->get_context_param( array( 'default' => 'view' ) ); |
|
130 | + $params['context'] = $this->get_context_param(array('default' => 'view')); |
|
131 | 131 | $params['match'] = array( |
132 | - 'description' => __( 'A partial IP address can be passed and matching results will be returned.', 'woocommerce' ), |
|
132 | + 'description' => __('A partial IP address can be passed and matching results will be returned.', 'woocommerce'), |
|
133 | 133 | 'type' => 'string', |
134 | 134 | 'validate_callback' => 'rest_validate_request_arg', |
135 | 135 | ); |
@@ -150,13 +150,13 @@ discard block |
||
150 | 150 | 'properties' => array( |
151 | 151 | 'user_ip_address' => array( |
152 | 152 | 'type' => 'string', |
153 | - 'description' => __( 'IP address.', 'woocommerce' ), |
|
154 | - 'context' => array( 'view' ), |
|
153 | + 'description' => __('IP address.', 'woocommerce'), |
|
154 | + 'context' => array('view'), |
|
155 | 155 | 'readonly' => true, |
156 | 156 | ), |
157 | 157 | ), |
158 | 158 | ); |
159 | 159 | |
160 | - return $this->add_additional_fields_schema( $schema ); |
|
160 | + return $this->add_additional_fields_schema($schema); |
|
161 | 161 | } |
162 | 162 | } |
@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | |
10 | 10 | namespace WooCommerce\RestApi\Controllers\Version4\Data; |
11 | 11 | |
12 | -defined( 'ABSPATH' ) || exit; |
|
12 | +defined('ABSPATH') || exit; |
|
13 | 13 | |
14 | 14 | use \WooCommerce\RestApi\Controllers\Version4\Data as DataController; |
15 | 15 | |
@@ -37,10 +37,10 @@ discard block |
||
37 | 37 | array( |
38 | 38 | array( |
39 | 39 | 'methods' => \WP_REST_Server::READABLE, |
40 | - 'callback' => array( $this, 'get_items' ), |
|
41 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
40 | + 'callback' => array($this, 'get_items'), |
|
41 | + 'permission_callback' => array($this, 'get_items_permissions_check'), |
|
42 | 42 | ), |
43 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
43 | + 'schema' => array($this, 'get_public_item_schema'), |
|
44 | 44 | ) |
45 | 45 | ); |
46 | 46 | register_rest_route( |
@@ -49,16 +49,16 @@ discard block |
||
49 | 49 | array( |
50 | 50 | array( |
51 | 51 | 'methods' => \WP_REST_Server::READABLE, |
52 | - 'callback' => array( $this, 'get_item' ), |
|
53 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
52 | + 'callback' => array($this, 'get_item'), |
|
53 | + 'permission_callback' => array($this, 'get_items_permissions_check'), |
|
54 | 54 | 'args' => array( |
55 | 55 | 'location' => array( |
56 | - 'description' => __( 'ISO3166 alpha-2 country code.', 'woocommerce' ), |
|
56 | + 'description' => __('ISO3166 alpha-2 country code.', 'woocommerce'), |
|
57 | 57 | 'type' => 'string', |
58 | 58 | ), |
59 | 59 | ), |
60 | 60 | ), |
61 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
61 | + 'schema' => array($this, 'get_public_item_schema'), |
|
62 | 62 | ) |
63 | 63 | ); |
64 | 64 | } |
@@ -70,23 +70,23 @@ discard block |
||
70 | 70 | * @param \WP_REST_Request $request Request data. |
71 | 71 | * @return array|mixed Response data, ready for insertion into collection data. |
72 | 72 | */ |
73 | - public function get_country( $country_code = false, $request ) { |
|
73 | + public function get_country($country_code = false, $request) { |
|
74 | 74 | $countries = WC()->countries->get_countries(); |
75 | 75 | $states = WC()->countries->get_states(); |
76 | 76 | $data = array(); |
77 | 77 | |
78 | - if ( ! array_key_exists( $country_code, $countries ) ) { |
|
78 | + if ( ! array_key_exists($country_code, $countries)) { |
|
79 | 79 | return false; |
80 | 80 | } |
81 | 81 | |
82 | 82 | $country = array( |
83 | 83 | 'code' => $country_code, |
84 | - 'name' => $countries[ $country_code ], |
|
84 | + 'name' => $countries[$country_code], |
|
85 | 85 | ); |
86 | 86 | |
87 | 87 | $local_states = array(); |
88 | - if ( isset( $states[ $country_code ] ) ) { |
|
89 | - foreach ( $states[ $country_code ] as $state_code => $state_name ) { |
|
88 | + if (isset($states[$country_code])) { |
|
89 | + foreach ($states[$country_code] as $state_code => $state_name) { |
|
90 | 90 | $local_states[] = array( |
91 | 91 | 'code' => $state_code, |
92 | 92 | 'name' => $state_name, |
@@ -104,17 +104,17 @@ discard block |
||
104 | 104 | * @param \WP_REST_Request $request Request data. |
105 | 105 | * @return \WP_Error\WP_REST_Response |
106 | 106 | */ |
107 | - public function get_items( $request ) { |
|
107 | + public function get_items($request) { |
|
108 | 108 | $countries = WC()->countries->get_countries(); |
109 | 109 | $data = array(); |
110 | 110 | |
111 | - foreach ( array_keys( $countries ) as $country_code ) { |
|
112 | - $country = $this->get_country( $country_code, $request ); |
|
113 | - $response = $this->prepare_item_for_response( $country, $request ); |
|
114 | - $data[] = $this->prepare_response_for_collection( $response ); |
|
111 | + foreach (array_keys($countries) as $country_code) { |
|
112 | + $country = $this->get_country($country_code, $request); |
|
113 | + $response = $this->prepare_item_for_response($country, $request); |
|
114 | + $data[] = $this->prepare_response_for_collection($response); |
|
115 | 115 | } |
116 | 116 | |
117 | - return rest_ensure_response( $data ); |
|
117 | + return rest_ensure_response($data); |
|
118 | 118 | } |
119 | 119 | |
120 | 120 | /** |
@@ -124,12 +124,12 @@ discard block |
||
124 | 124 | * @param \WP_REST_Request $request Request data. |
125 | 125 | * @return \WP_Error\WP_REST_Response |
126 | 126 | */ |
127 | - public function get_item( $request ) { |
|
128 | - $data = $this->get_country( strtoupper( $request['location'] ), $request ); |
|
129 | - if ( empty( $data ) ) { |
|
130 | - return new \WP_Error( 'woocommerce_rest_data_invalid_location', __( 'There are no locations matching these parameters.', 'woocommerce' ), array( 'status' => 404 ) ); |
|
127 | + public function get_item($request) { |
|
128 | + $data = $this->get_country(strtoupper($request['location']), $request); |
|
129 | + if (empty($data)) { |
|
130 | + return new \WP_Error('woocommerce_rest_data_invalid_location', __('There are no locations matching these parameters.', 'woocommerce'), array('status' => 404)); |
|
131 | 131 | } |
132 | - return $this->prepare_item_for_response( $data, $request ); |
|
132 | + return $this->prepare_item_for_response($data, $request); |
|
133 | 133 | } |
134 | 134 | |
135 | 135 | /** |
@@ -140,12 +140,12 @@ discard block |
||
140 | 140 | * @param \WP_REST_Request $request Request object. |
141 | 141 | * @return \WP_REST_Response $response Response data. |
142 | 142 | */ |
143 | - public function prepare_item_for_response( $item, $request ) { |
|
144 | - $data = $this->add_additional_fields_to_object( $item, $request ); |
|
145 | - $data = $this->filter_response_by_context( $data, 'view' ); |
|
146 | - $response = rest_ensure_response( $data ); |
|
143 | + public function prepare_item_for_response($item, $request) { |
|
144 | + $data = $this->add_additional_fields_to_object($item, $request); |
|
145 | + $data = $this->filter_response_by_context($data, 'view'); |
|
146 | + $response = rest_ensure_response($data); |
|
147 | 147 | |
148 | - $response->add_links( $this->prepare_links( $item ) ); |
|
148 | + $response->add_links($this->prepare_links($item)); |
|
149 | 149 | |
150 | 150 | /** |
151 | 151 | * Filter the states list for a country returned from the API. |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | * @param array $data The original country's states list. |
157 | 157 | * @param \WP_REST_Request $request Request used to generate the response. |
158 | 158 | */ |
159 | - return apply_filters( 'woocommerce_rest_prepare_data_country', $response, $item, $request ); |
|
159 | + return apply_filters('woocommerce_rest_prepare_data_country', $response, $item, $request); |
|
160 | 160 | } |
161 | 161 | |
162 | 162 | /** |
@@ -165,14 +165,14 @@ discard block |
||
165 | 165 | * @param object $item Data object. |
166 | 166 | * @return array Links for the given country. |
167 | 167 | */ |
168 | - protected function prepare_links( $item ) { |
|
169 | - $country_code = strtolower( $item['code'] ); |
|
168 | + protected function prepare_links($item) { |
|
169 | + $country_code = strtolower($item['code']); |
|
170 | 170 | $links = array( |
171 | 171 | 'self' => array( |
172 | - 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $country_code ) ), |
|
172 | + 'href' => rest_url(sprintf('/%s/%s/%s', $this->namespace, $this->rest_base, $country_code)), |
|
173 | 173 | ), |
174 | 174 | 'collection' => array( |
175 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
175 | + 'href' => rest_url(sprintf('/%s/%s', $this->namespace, $this->rest_base)), |
|
176 | 176 | ), |
177 | 177 | ); |
178 | 178 | |
@@ -194,36 +194,36 @@ discard block |
||
194 | 194 | 'properties' => array( |
195 | 195 | 'code' => array( |
196 | 196 | 'type' => 'string', |
197 | - 'description' => __( 'ISO3166 alpha-2 country code.', 'woocommerce' ), |
|
198 | - 'context' => array( 'view' ), |
|
197 | + 'description' => __('ISO3166 alpha-2 country code.', 'woocommerce'), |
|
198 | + 'context' => array('view'), |
|
199 | 199 | 'readonly' => true, |
200 | 200 | ), |
201 | 201 | 'name' => array( |
202 | 202 | 'type' => 'string', |
203 | - 'description' => __( 'Full name of country.', 'woocommerce' ), |
|
204 | - 'context' => array( 'view' ), |
|
203 | + 'description' => __('Full name of country.', 'woocommerce'), |
|
204 | + 'context' => array('view'), |
|
205 | 205 | 'readonly' => true, |
206 | 206 | ), |
207 | 207 | 'states' => array( |
208 | 208 | 'type' => 'array', |
209 | - 'description' => __( 'List of states in this country.', 'woocommerce' ), |
|
210 | - 'context' => array( 'view' ), |
|
209 | + 'description' => __('List of states in this country.', 'woocommerce'), |
|
210 | + 'context' => array('view'), |
|
211 | 211 | 'readonly' => true, |
212 | 212 | 'items' => array( |
213 | 213 | 'type' => 'object', |
214 | - 'context' => array( 'view' ), |
|
214 | + 'context' => array('view'), |
|
215 | 215 | 'readonly' => true, |
216 | 216 | 'properties' => array( |
217 | 217 | 'code' => array( |
218 | 218 | 'type' => 'string', |
219 | - 'description' => __( 'State code.', 'woocommerce' ), |
|
220 | - 'context' => array( 'view' ), |
|
219 | + 'description' => __('State code.', 'woocommerce'), |
|
220 | + 'context' => array('view'), |
|
221 | 221 | 'readonly' => true, |
222 | 222 | ), |
223 | 223 | 'name' => array( |
224 | 224 | 'type' => 'string', |
225 | - 'description' => __( 'Full name of state.', 'woocommerce' ), |
|
226 | - 'context' => array( 'view' ), |
|
225 | + 'description' => __('Full name of state.', 'woocommerce'), |
|
226 | + 'context' => array('view'), |
|
227 | 227 | 'readonly' => true, |
228 | 228 | ), |
229 | 229 | ), |
@@ -232,6 +232,6 @@ discard block |
||
232 | 232 | ), |
233 | 233 | ); |
234 | 234 | |
235 | - return $this->add_additional_fields_schema( $schema ); |
|
235 | + return $this->add_additional_fields_schema($schema); |
|
236 | 236 | } |
237 | 237 | } |
@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | |
10 | 10 | namespace WooCommerce\RestApi\Controllers\Version4\Data; |
11 | 11 | |
12 | -defined( 'ABSPATH' ) || exit; |
|
12 | +defined('ABSPATH') || exit; |
|
13 | 13 | |
14 | 14 | use \WooCommerce\RestApi\Controllers\Version4\Data as DataController; |
15 | 15 | |
@@ -37,10 +37,10 @@ discard block |
||
37 | 37 | array( |
38 | 38 | array( |
39 | 39 | 'methods' => \WP_REST_Server::READABLE, |
40 | - 'callback' => array( $this, 'get_items' ), |
|
41 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
40 | + 'callback' => array($this, 'get_items'), |
|
41 | + 'permission_callback' => array($this, 'get_items_permissions_check'), |
|
42 | 42 | ), |
43 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
43 | + 'schema' => array($this, 'get_public_item_schema'), |
|
44 | 44 | ) |
45 | 45 | ); |
46 | 46 | register_rest_route( |
@@ -49,16 +49,16 @@ discard block |
||
49 | 49 | array( |
50 | 50 | array( |
51 | 51 | 'methods' => \WP_REST_Server::READABLE, |
52 | - 'callback' => array( $this, 'get_item' ), |
|
53 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
52 | + 'callback' => array($this, 'get_item'), |
|
53 | + 'permission_callback' => array($this, 'get_items_permissions_check'), |
|
54 | 54 | 'args' => array( |
55 | 55 | 'continent' => array( |
56 | - 'description' => __( '2 character continent code.', 'woocommerce' ), |
|
56 | + 'description' => __('2 character continent code.', 'woocommerce'), |
|
57 | 57 | 'type' => 'string', |
58 | 58 | ), |
59 | 59 | ), |
60 | 60 | ), |
61 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
61 | + 'schema' => array($this, 'get_public_item_schema'), |
|
62 | 62 | ) |
63 | 63 | ); |
64 | 64 | } |
@@ -71,18 +71,18 @@ discard block |
||
71 | 71 | * @param \WP_REST_Request $request Request data. |
72 | 72 | * @return array|mixed Response data, ready for insertion into collection data. |
73 | 73 | */ |
74 | - public function get_continent( $continent_code = false, $request ) { |
|
74 | + public function get_continent($continent_code = false, $request) { |
|
75 | 75 | $continents = WC()->countries->get_continents(); |
76 | 76 | $countries = WC()->countries->get_countries(); |
77 | 77 | $states = WC()->countries->get_states(); |
78 | 78 | $locale_info = include WC()->plugin_path() . '/i18n/locale-info.php'; |
79 | 79 | $data = array(); |
80 | 80 | |
81 | - if ( ! array_key_exists( $continent_code, $continents ) ) { |
|
81 | + if ( ! array_key_exists($continent_code, $continents)) { |
|
82 | 82 | return false; |
83 | 83 | } |
84 | 84 | |
85 | - $continent_list = $continents[ $continent_code ]; |
|
85 | + $continent_list = $continents[$continent_code]; |
|
86 | 86 | |
87 | 87 | $continent = array( |
88 | 88 | 'code' => $continent_code, |
@@ -90,18 +90,18 @@ discard block |
||
90 | 90 | ); |
91 | 91 | |
92 | 92 | $local_countries = array(); |
93 | - foreach ( $continent_list['countries'] as $country_code ) { |
|
94 | - if ( isset( $countries[ $country_code ] ) ) { |
|
93 | + foreach ($continent_list['countries'] as $country_code) { |
|
94 | + if (isset($countries[$country_code])) { |
|
95 | 95 | $country = array( |
96 | 96 | 'code' => $country_code, |
97 | - 'name' => $countries[ $country_code ], |
|
97 | + 'name' => $countries[$country_code], |
|
98 | 98 | ); |
99 | 99 | |
100 | 100 | // If we have detailed locale information include that in the response. |
101 | - if ( array_key_exists( $country_code, $locale_info ) ) { |
|
101 | + if (array_key_exists($country_code, $locale_info)) { |
|
102 | 102 | // Defensive programming against unexpected changes in locale-info.php. |
103 | 103 | $country_data = wp_parse_args( |
104 | - $locale_info[ $country_code ], |
|
104 | + $locale_info[$country_code], |
|
105 | 105 | array( |
106 | 106 | 'currency_code' => 'USD', |
107 | 107 | 'currency_pos' => 'left', |
@@ -113,12 +113,12 @@ discard block |
||
113 | 113 | ) |
114 | 114 | ); |
115 | 115 | |
116 | - $country = array_merge( $country, $country_data ); |
|
116 | + $country = array_merge($country, $country_data); |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | $local_states = array(); |
120 | - if ( isset( $states[ $country_code ] ) ) { |
|
121 | - foreach ( $states[ $country_code ] as $state_code => $state_name ) { |
|
120 | + if (isset($states[$country_code])) { |
|
121 | + foreach ($states[$country_code] as $state_code => $state_name) { |
|
122 | 122 | $local_states[] = array( |
123 | 123 | 'code' => $state_code, |
124 | 124 | 'name' => $state_name, |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | 'thousand_sep', |
141 | 141 | 'weight_unit', |
142 | 142 | ); |
143 | - $country = array_intersect_key( $country, array_flip( $allowed ) ); |
|
143 | + $country = array_intersect_key($country, array_flip($allowed)); |
|
144 | 144 | |
145 | 145 | $local_countries[] = $country; |
146 | 146 | } |
@@ -157,17 +157,17 @@ discard block |
||
157 | 157 | * @param \WP_REST_Request $request Request data. |
158 | 158 | * @return \WP_Error\WP_REST_Response |
159 | 159 | */ |
160 | - public function get_items( $request ) { |
|
160 | + public function get_items($request) { |
|
161 | 161 | $continents = WC()->countries->get_continents(); |
162 | 162 | $data = array(); |
163 | 163 | |
164 | - foreach ( array_keys( $continents ) as $continent_code ) { |
|
165 | - $continent = $this->get_continent( $continent_code, $request ); |
|
166 | - $response = $this->prepare_item_for_response( $continent, $request ); |
|
167 | - $data[] = $this->prepare_response_for_collection( $response ); |
|
164 | + foreach (array_keys($continents) as $continent_code) { |
|
165 | + $continent = $this->get_continent($continent_code, $request); |
|
166 | + $response = $this->prepare_item_for_response($continent, $request); |
|
167 | + $data[] = $this->prepare_response_for_collection($response); |
|
168 | 168 | } |
169 | 169 | |
170 | - return rest_ensure_response( $data ); |
|
170 | + return rest_ensure_response($data); |
|
171 | 171 | } |
172 | 172 | |
173 | 173 | /** |
@@ -177,12 +177,12 @@ discard block |
||
177 | 177 | * @param \WP_REST_Request $request Request data. |
178 | 178 | * @return \WP_Error\WP_REST_Response |
179 | 179 | */ |
180 | - public function get_item( $request ) { |
|
181 | - $data = $this->get_continent( strtoupper( $request['location'] ), $request ); |
|
182 | - if ( empty( $data ) ) { |
|
183 | - return new \WP_Error( 'woocommerce_rest_data_invalid_location', __( 'There are no locations matching these parameters.', 'woocommerce' ), array( 'status' => 404 ) ); |
|
180 | + public function get_item($request) { |
|
181 | + $data = $this->get_continent(strtoupper($request['location']), $request); |
|
182 | + if (empty($data)) { |
|
183 | + return new \WP_Error('woocommerce_rest_data_invalid_location', __('There are no locations matching these parameters.', 'woocommerce'), array('status' => 404)); |
|
184 | 184 | } |
185 | - return $this->prepare_item_for_response( $data, $request ); |
|
185 | + return $this->prepare_item_for_response($data, $request); |
|
186 | 186 | } |
187 | 187 | |
188 | 188 | /** |
@@ -193,12 +193,12 @@ discard block |
||
193 | 193 | * @param \WP_REST_Request $request Request object. |
194 | 194 | * @return \WP_REST_Response $response Response data. |
195 | 195 | */ |
196 | - public function prepare_item_for_response( $item, $request ) { |
|
197 | - $data = $this->add_additional_fields_to_object( $item, $request ); |
|
198 | - $data = $this->filter_response_by_context( $data, 'view' ); |
|
199 | - $response = rest_ensure_response( $data ); |
|
196 | + public function prepare_item_for_response($item, $request) { |
|
197 | + $data = $this->add_additional_fields_to_object($item, $request); |
|
198 | + $data = $this->filter_response_by_context($data, 'view'); |
|
199 | + $response = rest_ensure_response($data); |
|
200 | 200 | |
201 | - $response->add_links( $this->prepare_links( $item ) ); |
|
201 | + $response->add_links($this->prepare_links($item)); |
|
202 | 202 | |
203 | 203 | /** |
204 | 204 | * Filter the location list returned from the API. |
@@ -209,7 +209,7 @@ discard block |
||
209 | 209 | * @param array $item The original list of continent(s), countries, and states. |
210 | 210 | * @param \WP_REST_Request $request Request used to generate the response. |
211 | 211 | */ |
212 | - return apply_filters( 'woocommerce_rest_prepare_data_continent', $response, $item, $request ); |
|
212 | + return apply_filters('woocommerce_rest_prepare_data_continent', $response, $item, $request); |
|
213 | 213 | } |
214 | 214 | |
215 | 215 | /** |
@@ -218,14 +218,14 @@ discard block |
||
218 | 218 | * @param object $item Data object. |
219 | 219 | * @return array Links for the given continent. |
220 | 220 | */ |
221 | - protected function prepare_links( $item ) { |
|
222 | - $continent_code = strtolower( $item['code'] ); |
|
221 | + protected function prepare_links($item) { |
|
222 | + $continent_code = strtolower($item['code']); |
|
223 | 223 | $links = array( |
224 | 224 | 'self' => array( |
225 | - 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $continent_code ) ), |
|
225 | + 'href' => rest_url(sprintf('/%s/%s/%s', $this->namespace, $this->rest_base, $continent_code)), |
|
226 | 226 | ), |
227 | 227 | 'collection' => array( |
228 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
228 | + 'href' => rest_url(sprintf('/%s/%s', $this->namespace, $this->rest_base)), |
|
229 | 229 | ), |
230 | 230 | ); |
231 | 231 | return $links; |
@@ -245,88 +245,88 @@ discard block |
||
245 | 245 | 'properties' => array( |
246 | 246 | 'code' => array( |
247 | 247 | 'type' => 'string', |
248 | - 'description' => __( '2 character continent code.', 'woocommerce' ), |
|
249 | - 'context' => array( 'view' ), |
|
248 | + 'description' => __('2 character continent code.', 'woocommerce'), |
|
249 | + 'context' => array('view'), |
|
250 | 250 | 'readonly' => true, |
251 | 251 | ), |
252 | 252 | 'name' => array( |
253 | 253 | 'type' => 'string', |
254 | - 'description' => __( 'Full name of continent.', 'woocommerce' ), |
|
255 | - 'context' => array( 'view' ), |
|
254 | + 'description' => __('Full name of continent.', 'woocommerce'), |
|
255 | + 'context' => array('view'), |
|
256 | 256 | 'readonly' => true, |
257 | 257 | ), |
258 | 258 | 'countries' => array( |
259 | 259 | 'type' => 'array', |
260 | - 'description' => __( 'List of countries on this continent.', 'woocommerce' ), |
|
261 | - 'context' => array( 'view' ), |
|
260 | + 'description' => __('List of countries on this continent.', 'woocommerce'), |
|
261 | + 'context' => array('view'), |
|
262 | 262 | 'readonly' => true, |
263 | 263 | 'items' => array( |
264 | 264 | 'type' => 'object', |
265 | - 'context' => array( 'view' ), |
|
265 | + 'context' => array('view'), |
|
266 | 266 | 'readonly' => true, |
267 | 267 | 'properties' => array( |
268 | 268 | 'code' => array( |
269 | 269 | 'type' => 'string', |
270 | - 'description' => __( 'ISO3166 alpha-2 country code.', 'woocommerce' ), |
|
271 | - 'context' => array( 'view' ), |
|
270 | + 'description' => __('ISO3166 alpha-2 country code.', 'woocommerce'), |
|
271 | + 'context' => array('view'), |
|
272 | 272 | 'readonly' => true, |
273 | 273 | ), |
274 | 274 | 'currency_code' => array( |
275 | 275 | 'type' => 'string', |
276 | - 'description' => __( 'Default ISO4127 alpha-3 currency code for the country.', 'woocommerce' ), |
|
277 | - 'context' => array( 'view' ), |
|
276 | + 'description' => __('Default ISO4127 alpha-3 currency code for the country.', 'woocommerce'), |
|
277 | + 'context' => array('view'), |
|
278 | 278 | 'readonly' => true, |
279 | 279 | ), |
280 | 280 | 'currency_pos' => array( |
281 | 281 | 'type' => 'string', |
282 | - 'description' => __( 'Currency symbol position for this country.', 'woocommerce' ), |
|
283 | - 'context' => array( 'view' ), |
|
282 | + 'description' => __('Currency symbol position for this country.', 'woocommerce'), |
|
283 | + 'context' => array('view'), |
|
284 | 284 | 'readonly' => true, |
285 | 285 | ), |
286 | 286 | 'decimal_sep' => array( |
287 | 287 | 'type' => 'string', |
288 | - 'description' => __( 'Decimal separator for displayed prices for this country.', 'woocommerce' ), |
|
289 | - 'context' => array( 'view' ), |
|
288 | + 'description' => __('Decimal separator for displayed prices for this country.', 'woocommerce'), |
|
289 | + 'context' => array('view'), |
|
290 | 290 | 'readonly' => true, |
291 | 291 | ), |
292 | 292 | 'dimension_unit' => array( |
293 | 293 | 'type' => 'string', |
294 | - 'description' => __( 'The unit lengths are defined in for this country.', 'woocommerce' ), |
|
295 | - 'context' => array( 'view' ), |
|
294 | + 'description' => __('The unit lengths are defined in for this country.', 'woocommerce'), |
|
295 | + 'context' => array('view'), |
|
296 | 296 | 'readonly' => true, |
297 | 297 | ), |
298 | 298 | 'name' => array( |
299 | 299 | 'type' => 'string', |
300 | - 'description' => __( 'Full name of country.', 'woocommerce' ), |
|
301 | - 'context' => array( 'view' ), |
|
300 | + 'description' => __('Full name of country.', 'woocommerce'), |
|
301 | + 'context' => array('view'), |
|
302 | 302 | 'readonly' => true, |
303 | 303 | ), |
304 | 304 | 'num_decimals' => array( |
305 | 305 | 'type' => 'integer', |
306 | - 'description' => __( 'Number of decimal points shown in displayed prices for this country.', 'woocommerce' ), |
|
307 | - 'context' => array( 'view' ), |
|
306 | + 'description' => __('Number of decimal points shown in displayed prices for this country.', 'woocommerce'), |
|
307 | + 'context' => array('view'), |
|
308 | 308 | 'readonly' => true, |
309 | 309 | ), |
310 | 310 | 'states' => array( |
311 | 311 | 'type' => 'array', |
312 | - 'description' => __( 'List of states in this country.', 'woocommerce' ), |
|
313 | - 'context' => array( 'view' ), |
|
312 | + 'description' => __('List of states in this country.', 'woocommerce'), |
|
313 | + 'context' => array('view'), |
|
314 | 314 | 'readonly' => true, |
315 | 315 | 'items' => array( |
316 | 316 | 'type' => 'object', |
317 | - 'context' => array( 'view' ), |
|
317 | + 'context' => array('view'), |
|
318 | 318 | 'readonly' => true, |
319 | 319 | 'properties' => array( |
320 | 320 | 'code' => array( |
321 | 321 | 'type' => 'string', |
322 | - 'description' => __( 'State code.', 'woocommerce' ), |
|
323 | - 'context' => array( 'view' ), |
|
322 | + 'description' => __('State code.', 'woocommerce'), |
|
323 | + 'context' => array('view'), |
|
324 | 324 | 'readonly' => true, |
325 | 325 | ), |
326 | 326 | 'name' => array( |
327 | 327 | 'type' => 'string', |
328 | - 'description' => __( 'Full name of state.', 'woocommerce' ), |
|
329 | - 'context' => array( 'view' ), |
|
328 | + 'description' => __('Full name of state.', 'woocommerce'), |
|
329 | + 'context' => array('view'), |
|
330 | 330 | 'readonly' => true, |
331 | 331 | ), |
332 | 332 | ), |
@@ -334,14 +334,14 @@ discard block |
||
334 | 334 | ), |
335 | 335 | 'thousand_sep' => array( |
336 | 336 | 'type' => 'string', |
337 | - 'description' => __( 'Thousands separator for displayed prices in this country.', 'woocommerce' ), |
|
338 | - 'context' => array( 'view' ), |
|
337 | + 'description' => __('Thousands separator for displayed prices in this country.', 'woocommerce'), |
|
338 | + 'context' => array('view'), |
|
339 | 339 | 'readonly' => true, |
340 | 340 | ), |
341 | 341 | 'weight_unit' => array( |
342 | 342 | 'type' => 'string', |
343 | - 'description' => __( 'The unit weights are defined in for this country.', 'woocommerce' ), |
|
344 | - 'context' => array( 'view' ), |
|
343 | + 'description' => __('The unit weights are defined in for this country.', 'woocommerce'), |
|
344 | + 'context' => array('view'), |
|
345 | 345 | 'readonly' => true, |
346 | 346 | ), |
347 | 347 | ), |
@@ -350,6 +350,6 @@ discard block |
||
350 | 350 | ), |
351 | 351 | ); |
352 | 352 | |
353 | - return $this->add_additional_fields_schema( $schema ); |
|
353 | + return $this->add_additional_fields_schema($schema); |
|
354 | 354 | } |
355 | 355 | } |
@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | |
10 | 10 | namespace WooCommerce\RestApi\Controllers\Version4; |
11 | 11 | |
12 | -defined( 'ABSPATH' ) || exit; |
|
12 | +defined('ABSPATH') || exit; |
|
13 | 13 | |
14 | 14 | /** |
15 | 15 | * REST API Network Orders controller class. |
@@ -20,18 +20,18 @@ discard block |
||
20 | 20 | * Register the routes for network orders. |
21 | 21 | */ |
22 | 22 | public function register_routes() { |
23 | - if ( is_multisite() ) { |
|
23 | + if (is_multisite()) { |
|
24 | 24 | register_rest_route( |
25 | 25 | $this->namespace, |
26 | 26 | '/' . $this->rest_base . '/network', |
27 | 27 | array( |
28 | 28 | array( |
29 | 29 | 'methods' => \WP_REST_Server::READABLE, |
30 | - 'callback' => array( $this, 'network_orders' ), |
|
31 | - 'permission_callback' => array( $this, 'network_orders_permissions_check' ), |
|
30 | + 'callback' => array($this, 'network_orders'), |
|
31 | + 'permission_callback' => array($this, 'network_orders_permissions_check'), |
|
32 | 32 | 'args' => $this->get_collection_params(), |
33 | 33 | ), |
34 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
34 | + 'schema' => array($this, 'get_public_item_schema'), |
|
35 | 35 | ), |
36 | 36 | true |
37 | 37 | ); |
@@ -46,34 +46,34 @@ discard block |
||
46 | 46 | public function get_public_item_schema() { |
47 | 47 | $schema = parent::get_public_item_schema(); |
48 | 48 | |
49 | - $schema['properties']['blog'] = array( |
|
50 | - 'description' => __( 'Blog id of the record on the multisite.', 'woocommerce' ), |
|
49 | + $schema['properties']['blog'] = array( |
|
50 | + 'description' => __('Blog id of the record on the multisite.', 'woocommerce'), |
|
51 | 51 | 'type' => 'integer', |
52 | - 'context' => array( 'view' ), |
|
52 | + 'context' => array('view'), |
|
53 | 53 | 'readonly' => true, |
54 | 54 | ); |
55 | - $schema['properties']['edit_url'] = array( |
|
56 | - 'description' => __( 'URL to edit the order', 'woocommerce' ), |
|
55 | + $schema['properties']['edit_url'] = array( |
|
56 | + 'description' => __('URL to edit the order', 'woocommerce'), |
|
57 | 57 | 'type' => 'string', |
58 | - 'context' => array( 'view' ), |
|
58 | + 'context' => array('view'), |
|
59 | 59 | 'readonly' => true, |
60 | 60 | ); |
61 | - $schema['properties']['customer'][] = array( |
|
62 | - 'description' => __( 'Name of the customer for the order', 'woocommerce' ), |
|
61 | + $schema['properties']['customer'][] = array( |
|
62 | + 'description' => __('Name of the customer for the order', 'woocommerce'), |
|
63 | 63 | 'type' => 'string', |
64 | - 'context' => array( 'view' ), |
|
64 | + 'context' => array('view'), |
|
65 | 65 | 'readonly' => true, |
66 | 66 | ); |
67 | - $schema['properties']['status_name'][] = array( |
|
68 | - 'description' => __( 'Order Status', 'woocommerce' ), |
|
67 | + $schema['properties']['status_name'][] = array( |
|
68 | + 'description' => __('Order Status', 'woocommerce'), |
|
69 | 69 | 'type' => 'string', |
70 | - 'context' => array( 'view' ), |
|
70 | + 'context' => array('view'), |
|
71 | 71 | 'readonly' => true, |
72 | 72 | ); |
73 | 73 | $schema['properties']['formatted_total'][] = array( |
74 | - 'description' => __( 'Order total formatted for locale', 'woocommerce' ), |
|
74 | + 'description' => __('Order total formatted for locale', 'woocommerce'), |
|
75 | 75 | 'type' => 'string', |
76 | - 'context' => array( 'view' ), |
|
76 | + 'context' => array('view'), |
|
77 | 77 | 'readonly' => true, |
78 | 78 | ); |
79 | 79 | |
@@ -87,13 +87,13 @@ discard block |
||
87 | 87 | * |
88 | 88 | * @return bool $permission |
89 | 89 | */ |
90 | - public function network_orders_permissions_check( $request ) { |
|
91 | - $blog_id = $request->get_param( 'blog_id' ); |
|
92 | - $blog_id = ! empty( $blog_id ) ? $blog_id : get_current_blog_id(); |
|
90 | + public function network_orders_permissions_check($request) { |
|
91 | + $blog_id = $request->get_param('blog_id'); |
|
92 | + $blog_id = ! empty($blog_id) ? $blog_id : get_current_blog_id(); |
|
93 | 93 | |
94 | - switch_to_blog( $blog_id ); |
|
94 | + switch_to_blog($blog_id); |
|
95 | 95 | |
96 | - $permission = $this->get_items_permissions_check( $request ); |
|
96 | + $permission = $this->get_items_permissions_check($request); |
|
97 | 97 | |
98 | 98 | restore_current_blog(); |
99 | 99 | |
@@ -107,39 +107,39 @@ discard block |
||
107 | 107 | * |
108 | 108 | * @return \WP_REST_Response |
109 | 109 | */ |
110 | - public function network_orders( $request ) { |
|
111 | - $blog_id = $request->get_param( 'blog_id' ); |
|
112 | - $blog_id = ! empty( $blog_id ) ? $blog_id : get_current_blog_id(); |
|
113 | - $active_plugins = get_blog_option( $blog_id, 'active_plugins', array() ); |
|
114 | - $network_active_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) ); |
|
110 | + public function network_orders($request) { |
|
111 | + $blog_id = $request->get_param('blog_id'); |
|
112 | + $blog_id = ! empty($blog_id) ? $blog_id : get_current_blog_id(); |
|
113 | + $active_plugins = get_blog_option($blog_id, 'active_plugins', array()); |
|
114 | + $network_active_plugins = array_keys(get_site_option('active_sitewide_plugins', array())); |
|
115 | 115 | |
116 | - $plugins = array_merge( $active_plugins, $network_active_plugins ); |
|
116 | + $plugins = array_merge($active_plugins, $network_active_plugins); |
|
117 | 117 | $wc_active = false; |
118 | - foreach ( $plugins as $plugin ) { |
|
119 | - if ( substr_compare( $plugin, '/woocommerce.php', strlen( $plugin ) - strlen( '/woocommerce.php' ), strlen( '/woocommerce.php' ) ) === 0 ) { |
|
118 | + foreach ($plugins as $plugin) { |
|
119 | + if (substr_compare($plugin, '/woocommerce.php', strlen($plugin) - strlen('/woocommerce.php'), strlen('/woocommerce.php')) === 0) { |
|
120 | 120 | $wc_active = true; |
121 | 121 | } |
122 | 122 | } |
123 | 123 | |
124 | 124 | // If WooCommerce not active for site, return an empty response. |
125 | - if ( ! $wc_active ) { |
|
126 | - $response = rest_ensure_response( array() ); |
|
125 | + if ( ! $wc_active) { |
|
126 | + $response = rest_ensure_response(array()); |
|
127 | 127 | return $response; |
128 | 128 | } |
129 | 129 | |
130 | - switch_to_blog( $blog_id ); |
|
131 | - add_filter( 'woocommerce_rest_orders_prepare_object_query', array( $this, 'network_orders_filter_args' ) ); |
|
132 | - $items = $this->get_items( $request ); |
|
133 | - remove_filter( 'woocommerce_rest_orders_prepare_object_query', array( $this, 'network_orders_filter_args' ) ); |
|
130 | + switch_to_blog($blog_id); |
|
131 | + add_filter('woocommerce_rest_orders_prepare_object_query', array($this, 'network_orders_filter_args')); |
|
132 | + $items = $this->get_items($request); |
|
133 | + remove_filter('woocommerce_rest_orders_prepare_object_query', array($this, 'network_orders_filter_args')); |
|
134 | 134 | |
135 | - foreach ( $items->data as &$current_order ) { |
|
136 | - $order = wc_get_order( $current_order['id'] ); |
|
135 | + foreach ($items->data as &$current_order) { |
|
136 | + $order = wc_get_order($current_order['id']); |
|
137 | 137 | |
138 | - $current_order['blog'] = get_blog_details( get_current_blog_id() ); |
|
139 | - $current_order['edit_url'] = get_admin_url( $blog_id, 'post.php?post=' . absint( $order->get_id() ) . '&action=edit' ); |
|
138 | + $current_order['blog'] = get_blog_details(get_current_blog_id()); |
|
139 | + $current_order['edit_url'] = get_admin_url($blog_id, 'post.php?post=' . absint($order->get_id()) . '&action=edit'); |
|
140 | 140 | /* translators: 1: first name 2: last name */ |
141 | - $current_order['customer'] = trim( sprintf( _x( '%1$s %2$s', 'full name', 'woocommerce' ), $order->get_billing_first_name(), $order->get_billing_last_name() ) ); |
|
142 | - $current_order['status_name'] = wc_get_order_status_name( $order->get_status() ); |
|
141 | + $current_order['customer'] = trim(sprintf(_x('%1$s %2$s', 'full name', 'woocommerce'), $order->get_billing_first_name(), $order->get_billing_last_name())); |
|
142 | + $current_order['status_name'] = wc_get_order_status_name($order->get_status()); |
|
143 | 143 | $current_order['formatted_total'] = $order->get_formatted_order_total(); |
144 | 144 | } |
145 | 145 | |
@@ -155,7 +155,7 @@ discard block |
||
155 | 155 | * |
156 | 156 | * @return array |
157 | 157 | */ |
158 | - public function network_orders_filter_args( $args ) { |
|
158 | + public function network_orders_filter_args($args) { |
|
159 | 159 | $args['post_status'] = array( |
160 | 160 | 'wc-on-hold', |
161 | 161 | 'wc-processing', |
@@ -7,7 +7,7 @@ discard block |
||
7 | 7 | |
8 | 8 | namespace WooCommerce\RestApi\Controllers\Version4; |
9 | 9 | |
10 | -defined( 'ABSPATH' ) || exit; |
|
10 | +defined('ABSPATH') || exit; |
|
11 | 11 | |
12 | 12 | /** |
13 | 13 | * CRUD Object Controller. |
@@ -27,9 +27,9 @@ discard block |
||
27 | 27 | * @param int $id Object ID. |
28 | 28 | * @return object WC_Data object or \WP_Error object. |
29 | 29 | */ |
30 | - protected function get_object( $id ) { |
|
30 | + protected function get_object($id) { |
|
31 | 31 | // translators: %s: Class method name. |
32 | - return new \WP_Error( 'invalid-method', sprintf( __( "Method '%s' not implemented. Must be overridden in subclass.", 'woocommerce' ), __METHOD__ ), array( 'status' => 405 ) ); |
|
32 | + return new \WP_Error('invalid-method', sprintf(__("Method '%s' not implemented. Must be overridden in subclass.", 'woocommerce'), __METHOD__), array('status' => 405)); |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | /** |
@@ -38,11 +38,11 @@ discard block |
||
38 | 38 | * @param \WP_REST_Request $request Full details about the request. |
39 | 39 | * @return \WP_Error|boolean |
40 | 40 | */ |
41 | - public function get_item_permissions_check( $request ) { |
|
42 | - $object = $this->get_object( (int) $request['id'] ); |
|
41 | + public function get_item_permissions_check($request) { |
|
42 | + $object = $this->get_object((int) $request['id']); |
|
43 | 43 | |
44 | - if ( $object && 0 !== $object->get_id() && ! wc_rest_check_post_permissions( $this->post_type, 'read', $object->get_id() ) ) { |
|
45 | - return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
44 | + if ($object && 0 !== $object->get_id() && ! wc_rest_check_post_permissions($this->post_type, 'read', $object->get_id())) { |
|
45 | + return new \WP_Error('woocommerce_rest_cannot_view', __('Sorry, you cannot view this resource.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | return true; |
@@ -54,11 +54,11 @@ discard block |
||
54 | 54 | * @param \WP_REST_Request $request Full details about the request. |
55 | 55 | * @return \WP_Error|boolean |
56 | 56 | */ |
57 | - public function update_item_permissions_check( $request ) { |
|
58 | - $object = $this->get_object( (int) $request['id'] ); |
|
57 | + public function update_item_permissions_check($request) { |
|
58 | + $object = $this->get_object((int) $request['id']); |
|
59 | 59 | |
60 | - if ( $object && 0 !== $object->get_id() && ! wc_rest_check_post_permissions( $this->post_type, 'edit', $object->get_id() ) ) { |
|
61 | - return new \WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
60 | + if ($object && 0 !== $object->get_id() && ! wc_rest_check_post_permissions($this->post_type, 'edit', $object->get_id())) { |
|
61 | + return new \WP_Error('woocommerce_rest_cannot_edit', __('Sorry, you are not allowed to edit this resource.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | return true; |
@@ -70,11 +70,11 @@ discard block |
||
70 | 70 | * @param \WP_REST_Request $request Full details about the request. |
71 | 71 | * @return bool|\WP_Error |
72 | 72 | */ |
73 | - public function delete_item_permissions_check( $request ) { |
|
74 | - $object = $this->get_object( (int) $request['id'] ); |
|
73 | + public function delete_item_permissions_check($request) { |
|
74 | + $object = $this->get_object((int) $request['id']); |
|
75 | 75 | |
76 | - if ( $object && 0 !== $object->get_id() && ! wc_rest_check_post_permissions( $this->post_type, 'delete', $object->get_id() ) ) { |
|
77 | - return new \WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
76 | + if ($object && 0 !== $object->get_id() && ! wc_rest_check_post_permissions($this->post_type, 'delete', $object->get_id())) { |
|
77 | + return new \WP_Error('woocommerce_rest_cannot_delete', __('Sorry, you are not allowed to delete this resource.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
78 | 78 | } |
79 | 79 | |
80 | 80 | return true; |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | * @param object $object Object. |
87 | 87 | * @return string |
88 | 88 | */ |
89 | - protected function get_permalink( $object ) { |
|
89 | + protected function get_permalink($object) { |
|
90 | 90 | return ''; |
91 | 91 | } |
92 | 92 | |
@@ -98,9 +98,9 @@ discard block |
||
98 | 98 | * @param \WP_REST_Request $request Request object. |
99 | 99 | * @return \WP_Error|\WP_REST_Response Response object on success, or \WP_Error object on failure. |
100 | 100 | */ |
101 | - protected function prepare_object_for_response( $object, $request ) { |
|
101 | + protected function prepare_object_for_response($object, $request) { |
|
102 | 102 | // translators: %s: Class method name. |
103 | - return new \WP_Error( 'invalid-method', sprintf( __( "Method '%s' not implemented. Must be overridden in subclass.", 'woocommerce' ), __METHOD__ ), array( 'status' => 405 ) ); |
|
103 | + return new \WP_Error('invalid-method', sprintf(__("Method '%s' not implemented. Must be overridden in subclass.", 'woocommerce'), __METHOD__), array('status' => 405)); |
|
104 | 104 | } |
105 | 105 | |
106 | 106 | /** |
@@ -111,9 +111,9 @@ discard block |
||
111 | 111 | * @param bool $creating If is creating a new object. |
112 | 112 | * @return \WP_Error|WC_Data The prepared item, or \WP_Error object on failure. |
113 | 113 | */ |
114 | - protected function prepare_object_for_database( $request, $creating = false ) { |
|
114 | + protected function prepare_object_for_database($request, $creating = false) { |
|
115 | 115 | // translators: %s: Class method name. |
116 | - return new \WP_Error( 'invalid-method', sprintf( __( "Method '%s' not implemented. Must be overridden in subclass.", 'woocommerce' ), __METHOD__ ), array( 'status' => 405 ) ); |
|
116 | + return new \WP_Error('invalid-method', sprintf(__("Method '%s' not implemented. Must be overridden in subclass.", 'woocommerce'), __METHOD__), array('status' => 405)); |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | /** |
@@ -122,18 +122,18 @@ discard block |
||
122 | 122 | * @param \WP_REST_Request $request Full details about the request. |
123 | 123 | * @return \WP_Error|\WP_REST_Response |
124 | 124 | */ |
125 | - public function get_item( $request ) { |
|
126 | - $object = $this->get_object( (int) $request['id'] ); |
|
125 | + public function get_item($request) { |
|
126 | + $object = $this->get_object((int) $request['id']); |
|
127 | 127 | |
128 | - if ( ! $object || 0 === $object->get_id() ) { |
|
129 | - return new \WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) ); |
|
128 | + if ( ! $object || 0 === $object->get_id()) { |
|
129 | + return new \WP_Error("woocommerce_rest_{$this->post_type}_invalid_id", __('Invalid ID.', 'woocommerce'), array('status' => 404)); |
|
130 | 130 | } |
131 | 131 | |
132 | - $data = $this->prepare_object_for_response( $object, $request ); |
|
133 | - $response = rest_ensure_response( $data ); |
|
132 | + $data = $this->prepare_object_for_response($object, $request); |
|
133 | + $response = rest_ensure_response($data); |
|
134 | 134 | |
135 | - if ( $this->public ) { |
|
136 | - $response->link_header( 'alternate', $this->get_permalink( $object ), array( 'type' => 'text/html' ) ); |
|
135 | + if ($this->public) { |
|
136 | + $response->link_header('alternate', $this->get_permalink($object), array('type' => 'text/html')); |
|
137 | 137 | } |
138 | 138 | |
139 | 139 | return $response; |
@@ -147,21 +147,21 @@ discard block |
||
147 | 147 | * @param bool $creating If is creating a new object. |
148 | 148 | * @return WC_Data|\WP_Error |
149 | 149 | */ |
150 | - protected function save_object( $request, $creating = false ) { |
|
150 | + protected function save_object($request, $creating = false) { |
|
151 | 151 | try { |
152 | - $object = $this->prepare_object_for_database( $request, $creating ); |
|
152 | + $object = $this->prepare_object_for_database($request, $creating); |
|
153 | 153 | |
154 | - if ( is_wp_error( $object ) ) { |
|
154 | + if (is_wp_error($object)) { |
|
155 | 155 | return $object; |
156 | 156 | } |
157 | 157 | |
158 | 158 | $object->save(); |
159 | 159 | |
160 | - return $this->get_object( $object->get_id() ); |
|
161 | - } catch ( \WC_Data_Exception $e ) { |
|
162 | - return new \WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() ); |
|
163 | - } catch ( \WC_REST_Exception $e ) { |
|
164 | - return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); |
|
160 | + return $this->get_object($object->get_id()); |
|
161 | + } catch (\WC_Data_Exception $e) { |
|
162 | + return new \WP_Error($e->getErrorCode(), $e->getMessage(), $e->getErrorData()); |
|
163 | + } catch (\WC_REST_Exception $e) { |
|
164 | + return new \WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode())); |
|
165 | 165 | } |
166 | 166 | } |
167 | 167 | |
@@ -171,26 +171,26 @@ discard block |
||
171 | 171 | * @param \WP_REST_Request $request Full details about the request. |
172 | 172 | * @return \WP_Error\WP_REST_Response |
173 | 173 | */ |
174 | - public function create_item( $request ) { |
|
175 | - if ( ! empty( $request['id'] ) ) { |
|
174 | + public function create_item($request) { |
|
175 | + if ( ! empty($request['id'])) { |
|
176 | 176 | /* translators: %s: post type */ |
177 | - return new \WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) ); |
|
177 | + return new \WP_Error("woocommerce_rest_{$this->post_type}_exists", sprintf(__('Cannot create existing %s.', 'woocommerce'), $this->post_type), array('status' => 400)); |
|
178 | 178 | } |
179 | 179 | |
180 | - $object = $this->save_object( $request, true ); |
|
180 | + $object = $this->save_object($request, true); |
|
181 | 181 | |
182 | - if ( is_wp_error( $object ) ) { |
|
182 | + if (is_wp_error($object)) { |
|
183 | 183 | return $object; |
184 | 184 | } |
185 | 185 | |
186 | 186 | try { |
187 | - $this->update_additional_fields_for_object( $object, $request ); |
|
188 | - } catch ( \WC_Data_Exception $e ) { |
|
187 | + $this->update_additional_fields_for_object($object, $request); |
|
188 | + } catch (\WC_Data_Exception $e) { |
|
189 | 189 | $object->delete(); |
190 | - return new \WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() ); |
|
191 | - } catch ( \WC_REST_Exception $e ) { |
|
190 | + return new \WP_Error($e->getErrorCode(), $e->getMessage(), $e->getErrorData()); |
|
191 | + } catch (\WC_REST_Exception $e) { |
|
192 | 192 | $object->delete(); |
193 | - return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); |
|
193 | + return new \WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode())); |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | /** |
@@ -200,13 +200,13 @@ discard block |
||
200 | 200 | * @param \WP_REST_Request $request Request object. |
201 | 201 | * @param boolean $creating True when creating object, false when updating. |
202 | 202 | */ |
203 | - do_action( "woocommerce_rest_insert_{$this->post_type}_object", $object, $request, true ); |
|
203 | + do_action("woocommerce_rest_insert_{$this->post_type}_object", $object, $request, true); |
|
204 | 204 | |
205 | - $request->set_param( 'context', 'edit' ); |
|
206 | - $response = $this->prepare_object_for_response( $object, $request ); |
|
207 | - $response = rest_ensure_response( $response ); |
|
208 | - $response->set_status( 201 ); |
|
209 | - $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ) ); |
|
205 | + $request->set_param('context', 'edit'); |
|
206 | + $response = $this->prepare_object_for_response($object, $request); |
|
207 | + $response = rest_ensure_response($response); |
|
208 | + $response->set_status(201); |
|
209 | + $response->header('Location', rest_url(sprintf('/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id()))); |
|
210 | 210 | |
211 | 211 | return $response; |
212 | 212 | } |
@@ -217,25 +217,25 @@ discard block |
||
217 | 217 | * @param \WP_REST_Request $request Full details about the request. |
218 | 218 | * @return \WP_Error\WP_REST_Response |
219 | 219 | */ |
220 | - public function update_item( $request ) { |
|
221 | - $object = $this->get_object( (int) $request['id'] ); |
|
220 | + public function update_item($request) { |
|
221 | + $object = $this->get_object((int) $request['id']); |
|
222 | 222 | |
223 | - if ( ! $object || 0 === $object->get_id() ) { |
|
224 | - return new \WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) ); |
|
223 | + if ( ! $object || 0 === $object->get_id()) { |
|
224 | + return new \WP_Error("woocommerce_rest_{$this->post_type}_invalid_id", __('Invalid ID.', 'woocommerce'), array('status' => 404)); |
|
225 | 225 | } |
226 | 226 | |
227 | - $object = $this->save_object( $request, false ); |
|
227 | + $object = $this->save_object($request, false); |
|
228 | 228 | |
229 | - if ( is_wp_error( $object ) ) { |
|
229 | + if (is_wp_error($object)) { |
|
230 | 230 | return $object; |
231 | 231 | } |
232 | 232 | |
233 | 233 | try { |
234 | - $this->update_additional_fields_for_object( $object, $request ); |
|
235 | - } catch ( \WC_Data_Exception $e ) { |
|
236 | - return new \WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() ); |
|
237 | - } catch ( \WC_REST_Exception $e ) { |
|
238 | - return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); |
|
234 | + $this->update_additional_fields_for_object($object, $request); |
|
235 | + } catch (\WC_Data_Exception $e) { |
|
236 | + return new \WP_Error($e->getErrorCode(), $e->getMessage(), $e->getErrorData()); |
|
237 | + } catch (\WC_REST_Exception $e) { |
|
238 | + return new \WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode())); |
|
239 | 239 | } |
240 | 240 | |
241 | 241 | /** |
@@ -245,11 +245,11 @@ discard block |
||
245 | 245 | * @param \WP_REST_Request $request Request object. |
246 | 246 | * @param boolean $creating True when creating object, false when updating. |
247 | 247 | */ |
248 | - do_action( "woocommerce_rest_insert_{$this->post_type}_object", $object, $request, false ); |
|
248 | + do_action("woocommerce_rest_insert_{$this->post_type}_object", $object, $request, false); |
|
249 | 249 | |
250 | - $request->set_param( 'context', 'edit' ); |
|
251 | - $response = $this->prepare_object_for_response( $object, $request ); |
|
252 | - return rest_ensure_response( $response ); |
|
250 | + $request->set_param('context', 'edit'); |
|
251 | + $response = $this->prepare_object_for_response($object, $request); |
|
252 | + return rest_ensure_response($response); |
|
253 | 253 | } |
254 | 254 | |
255 | 255 | /** |
@@ -259,7 +259,7 @@ discard block |
||
259 | 259 | * @param \WP_REST_Request $request Full details about the request. |
260 | 260 | * @return array |
261 | 261 | */ |
262 | - protected function prepare_objects_query( $request ) { |
|
262 | + protected function prepare_objects_query($request) { |
|
263 | 263 | $args = array(); |
264 | 264 | $args['offset'] = $request['offset']; |
265 | 265 | $args['order'] = $request['order']; |
@@ -274,18 +274,18 @@ discard block |
||
274 | 274 | $args['s'] = $request['search']; |
275 | 275 | $args['fields'] = 'ids'; |
276 | 276 | |
277 | - if ( 'date' === $args['orderby'] ) { |
|
277 | + if ('date' === $args['orderby']) { |
|
278 | 278 | $args['orderby'] = 'date ID'; |
279 | 279 | } |
280 | 280 | |
281 | 281 | $args['date_query'] = array(); |
282 | 282 | // Set before into date query. Date query must be specified as an array of an array. |
283 | - if ( isset( $request['before'] ) ) { |
|
283 | + if (isset($request['before'])) { |
|
284 | 284 | $args['date_query'][0]['before'] = $request['before']; |
285 | 285 | } |
286 | 286 | |
287 | 287 | // Set after into date query. Date query must be specified as an array of an array. |
288 | - if ( isset( $request['after'] ) ) { |
|
288 | + if (isset($request['after'])) { |
|
289 | 289 | $args['date_query'][0]['after'] = $request['after']; |
290 | 290 | } |
291 | 291 | |
@@ -301,9 +301,9 @@ discard block |
||
301 | 301 | * @param array $args Key value array of query var to query value. |
302 | 302 | * @param \WP_REST_Request $request The request used. |
303 | 303 | */ |
304 | - $args = apply_filters( "woocommerce_rest_{$this->post_type}_object_query", $args, $request ); |
|
304 | + $args = apply_filters("woocommerce_rest_{$this->post_type}_object_query", $args, $request); |
|
305 | 305 | |
306 | - return $this->prepare_items_query( $args, $request ); |
|
306 | + return $this->prepare_items_query($args, $request); |
|
307 | 307 | } |
308 | 308 | |
309 | 309 | /** |
@@ -313,23 +313,23 @@ discard block |
||
313 | 313 | * @param array $query_args Query args. |
314 | 314 | * @return array |
315 | 315 | */ |
316 | - protected function get_objects( $query_args ) { |
|
316 | + protected function get_objects($query_args) { |
|
317 | 317 | $query = new \WP_Query(); |
318 | - $result = $query->query( $query_args ); |
|
318 | + $result = $query->query($query_args); |
|
319 | 319 | |
320 | 320 | $total_posts = $query->found_posts; |
321 | - if ( $total_posts < 1 ) { |
|
321 | + if ($total_posts < 1) { |
|
322 | 322 | // Out-of-bounds, run the query again without LIMIT for total count. |
323 | - unset( $query_args['paged'] ); |
|
323 | + unset($query_args['paged']); |
|
324 | 324 | $count_query = new \WP_Query(); |
325 | - $count_query->query( $query_args ); |
|
325 | + $count_query->query($query_args); |
|
326 | 326 | $total_posts = $count_query->found_posts; |
327 | 327 | } |
328 | 328 | |
329 | 329 | return array( |
330 | - 'objects' => array_map( array( $this, 'get_object' ), $result ), |
|
330 | + 'objects' => array_map(array($this, 'get_object'), $result), |
|
331 | 331 | 'total' => (int) $total_posts, |
332 | - 'pages' => (int) ceil( $total_posts / (int) $query->query_vars['posts_per_page'] ), |
|
332 | + 'pages' => (int) ceil($total_posts / (int) $query->query_vars['posts_per_page']), |
|
333 | 333 | ); |
334 | 334 | } |
335 | 335 | |
@@ -339,55 +339,55 @@ discard block |
||
339 | 339 | * @param \WP_REST_Request $request Full details about the request. |
340 | 340 | * @return \WP_Error\WP_REST_Response |
341 | 341 | */ |
342 | - public function get_items( $request ) { |
|
343 | - $query_args = $this->prepare_objects_query( $request ); |
|
344 | - $query_results = $this->get_objects( $query_args ); |
|
342 | + public function get_items($request) { |
|
343 | + $query_args = $this->prepare_objects_query($request); |
|
344 | + $query_results = $this->get_objects($query_args); |
|
345 | 345 | |
346 | 346 | $objects = array(); |
347 | - foreach ( $query_results['objects'] as $object ) { |
|
348 | - if ( ! wc_rest_check_post_permissions( $this->post_type, 'read', $object->get_id() ) ) { |
|
347 | + foreach ($query_results['objects'] as $object) { |
|
348 | + if ( ! wc_rest_check_post_permissions($this->post_type, 'read', $object->get_id())) { |
|
349 | 349 | continue; |
350 | 350 | } |
351 | 351 | |
352 | - $data = $this->prepare_object_for_response( $object, $request ); |
|
353 | - $objects[] = $this->prepare_response_for_collection( $data ); |
|
352 | + $data = $this->prepare_object_for_response($object, $request); |
|
353 | + $objects[] = $this->prepare_response_for_collection($data); |
|
354 | 354 | } |
355 | 355 | |
356 | 356 | $page = (int) $query_args['paged']; |
357 | 357 | $max_pages = $query_results['pages']; |
358 | 358 | |
359 | - $response = rest_ensure_response( $objects ); |
|
360 | - $response->header( 'X-WP-Total', $query_results['total'] ); |
|
361 | - $response->header( 'X-WP-TotalPages', (int) $max_pages ); |
|
359 | + $response = rest_ensure_response($objects); |
|
360 | + $response->header('X-WP-Total', $query_results['total']); |
|
361 | + $response->header('X-WP-TotalPages', (int) $max_pages); |
|
362 | 362 | |
363 | 363 | $base = $this->rest_base; |
364 | 364 | $attrib_prefix = '(?P<'; |
365 | - if ( strpos( $base, $attrib_prefix ) !== false ) { |
|
365 | + if (strpos($base, $attrib_prefix) !== false) { |
|
366 | 366 | $attrib_names = array(); |
367 | - preg_match( '/\(\?P<[^>]+>.*\)/', $base, $attrib_names, PREG_OFFSET_CAPTURE ); |
|
368 | - foreach ( $attrib_names as $attrib_name_match ) { |
|
369 | - $beginning_offset = strlen( $attrib_prefix ); |
|
370 | - $attrib_name_end = strpos( $attrib_name_match[0], '>', $attrib_name_match[1] ); |
|
371 | - $attrib_name = substr( $attrib_name_match[0], $beginning_offset, $attrib_name_end - $beginning_offset ); |
|
372 | - if ( isset( $request[ $attrib_name ] ) ) { |
|
373 | - $base = str_replace( "(?P<$attrib_name>[\d]+)", $request[ $attrib_name ], $base ); |
|
367 | + preg_match('/\(\?P<[^>]+>.*\)/', $base, $attrib_names, PREG_OFFSET_CAPTURE); |
|
368 | + foreach ($attrib_names as $attrib_name_match) { |
|
369 | + $beginning_offset = strlen($attrib_prefix); |
|
370 | + $attrib_name_end = strpos($attrib_name_match[0], '>', $attrib_name_match[1]); |
|
371 | + $attrib_name = substr($attrib_name_match[0], $beginning_offset, $attrib_name_end - $beginning_offset); |
|
372 | + if (isset($request[$attrib_name])) { |
|
373 | + $base = str_replace("(?P<$attrib_name>[\d]+)", $request[$attrib_name], $base); |
|
374 | 374 | } |
375 | 375 | } |
376 | 376 | } |
377 | - $base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ) ); |
|
377 | + $base = add_query_arg($request->get_query_params(), rest_url(sprintf('/%s/%s', $this->namespace, $base))); |
|
378 | 378 | |
379 | - if ( $page > 1 ) { |
|
379 | + if ($page > 1) { |
|
380 | 380 | $prev_page = $page - 1; |
381 | - if ( $prev_page > $max_pages ) { |
|
381 | + if ($prev_page > $max_pages) { |
|
382 | 382 | $prev_page = $max_pages; |
383 | 383 | } |
384 | - $prev_link = add_query_arg( 'page', $prev_page, $base ); |
|
385 | - $response->link_header( 'prev', $prev_link ); |
|
384 | + $prev_link = add_query_arg('page', $prev_page, $base); |
|
385 | + $response->link_header('prev', $prev_link); |
|
386 | 386 | } |
387 | - if ( $max_pages > $page ) { |
|
387 | + if ($max_pages > $page) { |
|
388 | 388 | $next_page = $page + 1; |
389 | - $next_link = add_query_arg( 'page', $next_page, $base ); |
|
390 | - $response->link_header( 'next', $next_link ); |
|
389 | + $next_link = add_query_arg('page', $next_page, $base); |
|
390 | + $response->link_header('next', $next_link); |
|
391 | 391 | } |
392 | 392 | |
393 | 393 | return $response; |
@@ -399,49 +399,49 @@ discard block |
||
399 | 399 | * @param \WP_REST_Request $request Full details about the request. |
400 | 400 | * @return \WP_REST_Response|\WP_Error |
401 | 401 | */ |
402 | - public function delete_item( $request ) { |
|
402 | + public function delete_item($request) { |
|
403 | 403 | $force = (bool) $request['force']; |
404 | - $object = $this->get_object( (int) $request['id'] ); |
|
404 | + $object = $this->get_object((int) $request['id']); |
|
405 | 405 | $result = false; |
406 | 406 | |
407 | - if ( ! $object || 0 === $object->get_id() ) { |
|
408 | - return new \WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) ); |
|
407 | + if ( ! $object || 0 === $object->get_id()) { |
|
408 | + return new \WP_Error("woocommerce_rest_{$this->post_type}_invalid_id", __('Invalid ID.', 'woocommerce'), array('status' => 404)); |
|
409 | 409 | } |
410 | 410 | |
411 | - $supports_trash = $this->supports_trash( $object ); |
|
411 | + $supports_trash = $this->supports_trash($object); |
|
412 | 412 | |
413 | - if ( ! wc_rest_check_post_permissions( $this->post_type, 'delete', $object->get_id() ) ) { |
|
413 | + if ( ! wc_rest_check_post_permissions($this->post_type, 'delete', $object->get_id())) { |
|
414 | 414 | /* translators: %s: post type */ |
415 | - return new \WP_Error( "woocommerce_rest_user_cannot_delete_{$this->post_type}", sprintf( __( 'Sorry, you are not allowed to delete %s.', 'woocommerce' ), $this->post_type ), array( 'status' => rest_authorization_required_code() ) ); |
|
415 | + return new \WP_Error("woocommerce_rest_user_cannot_delete_{$this->post_type}", sprintf(__('Sorry, you are not allowed to delete %s.', 'woocommerce'), $this->post_type), array('status' => rest_authorization_required_code())); |
|
416 | 416 | } |
417 | 417 | |
418 | - $request->set_param( 'context', 'edit' ); |
|
419 | - $previous = $this->prepare_object_for_response( $object, $request ); |
|
418 | + $request->set_param('context', 'edit'); |
|
419 | + $previous = $this->prepare_object_for_response($object, $request); |
|
420 | 420 | |
421 | 421 | // If we're forcing, then delete permanently. |
422 | - if ( $force ) { |
|
423 | - $object->delete( true ); |
|
422 | + if ($force) { |
|
423 | + $object->delete(true); |
|
424 | 424 | $result = 0 === $object->get_id(); |
425 | 425 | } else { |
426 | 426 | // If we don't support trashing for this type, error out. |
427 | - if ( ! $supports_trash ) { |
|
427 | + if ( ! $supports_trash) { |
|
428 | 428 | /* translators: %s: post type */ |
429 | - return new \WP_Error( 'woocommerce_rest_trash_not_supported', sprintf( __( 'The %s does not support trashing.', 'woocommerce' ), $this->post_type ), array( 'status' => 501 ) ); |
|
429 | + return new \WP_Error('woocommerce_rest_trash_not_supported', sprintf(__('The %s does not support trashing.', 'woocommerce'), $this->post_type), array('status' => 501)); |
|
430 | 430 | } |
431 | 431 | |
432 | 432 | // Otherwise, only trash if we haven't already. |
433 | - if ( is_callable( array( $object, 'get_status' ) ) && 'trash' === $object->get_status() ) { |
|
433 | + if (is_callable(array($object, 'get_status')) && 'trash' === $object->get_status()) { |
|
434 | 434 | /* translators: %s: post type */ |
435 | - return new \WP_Error( 'woocommerce_rest_already_trashed', sprintf( __( 'The %s has already been deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 410 ) ); |
|
435 | + return new \WP_Error('woocommerce_rest_already_trashed', sprintf(__('The %s has already been deleted.', 'woocommerce'), $this->post_type), array('status' => 410)); |
|
436 | 436 | } else { |
437 | 437 | $object->delete(); |
438 | - $result = is_callable( array( $object, 'get_status' ) ) ? 'trash' === $object->get_status() : true; |
|
438 | + $result = is_callable(array($object, 'get_status')) ? 'trash' === $object->get_status() : true; |
|
439 | 439 | } |
440 | 440 | } |
441 | 441 | |
442 | - if ( ! $result ) { |
|
442 | + if ( ! $result) { |
|
443 | 443 | /* translators: %s: post type */ |
444 | - return new \WP_Error( 'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 500 ) ); |
|
444 | + return new \WP_Error('woocommerce_rest_cannot_delete', sprintf(__('The %s cannot be deleted.', 'woocommerce'), $this->post_type), array('status' => 500)); |
|
445 | 445 | } |
446 | 446 | |
447 | 447 | $response = new \WP_REST_Response(); |
@@ -459,7 +459,7 @@ discard block |
||
459 | 459 | * @param \WP_REST_Response $response The response data. |
460 | 460 | * @param \WP_REST_Request $request The request sent to the API. |
461 | 461 | */ |
462 | - do_action( "woocommerce_rest_delete_{$this->post_type}_object", $object, $response, $request ); |
|
462 | + do_action("woocommerce_rest_delete_{$this->post_type}_object", $object, $response, $request); |
|
463 | 463 | |
464 | 464 | return $response; |
465 | 465 | } |
@@ -470,7 +470,7 @@ discard block |
||
470 | 470 | * @oaram object $object Object to check. |
471 | 471 | * @return boolean |
472 | 472 | */ |
473 | - protected function supports_trash( $object ) { |
|
473 | + protected function supports_trash($object) { |
|
474 | 474 | $supports_trash = EMPTY_TRASH_DAYS > 0; |
475 | 475 | |
476 | 476 | /** |
@@ -481,7 +481,7 @@ discard block |
||
481 | 481 | * @param boolean $supports_trash Whether the object type support trashing. |
482 | 482 | * @param WC_Data $object The object being considered for trashing support. |
483 | 483 | */ |
484 | - return apply_filters( "woocommerce_rest_{$this->post_type}_object_trashable", $supports_trash, $object ); |
|
484 | + return apply_filters("woocommerce_rest_{$this->post_type}_object_trashable", $supports_trash, $object); |
|
485 | 485 | } |
486 | 486 | |
487 | 487 | /** |
@@ -491,13 +491,13 @@ discard block |
||
491 | 491 | * @param \WP_REST_Request $request Request object. |
492 | 492 | * @return array Links for the given post. |
493 | 493 | */ |
494 | - protected function prepare_links( $object, $request ) { |
|
494 | + protected function prepare_links($object, $request) { |
|
495 | 495 | $links = array( |
496 | 496 | 'self' => array( |
497 | - 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ), |
|
497 | + 'href' => rest_url(sprintf('/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id())), |
|
498 | 498 | ), |
499 | 499 | 'collection' => array( |
500 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
500 | + 'href' => rest_url(sprintf('/%s/%s', $this->namespace, $this->rest_base)), |
|
501 | 501 | ), |
502 | 502 | ); |
503 | 503 | |
@@ -515,7 +515,7 @@ discard block |
||
515 | 515 | $params['context']['default'] = 'view'; |
516 | 516 | |
517 | 517 | $params['page'] = array( |
518 | - 'description' => __( 'Current page of the collection.', 'woocommerce' ), |
|
518 | + 'description' => __('Current page of the collection.', 'woocommerce'), |
|
519 | 519 | 'type' => 'integer', |
520 | 520 | 'default' => 1, |
521 | 521 | 'sanitize_callback' => 'absint', |
@@ -523,7 +523,7 @@ discard block |
||
523 | 523 | 'minimum' => 1, |
524 | 524 | ); |
525 | 525 | $params['per_page'] = array( |
526 | - 'description' => __( 'Maximum number of items to be returned in result set.', 'woocommerce' ), |
|
526 | + 'description' => __('Maximum number of items to be returned in result set.', 'woocommerce'), |
|
527 | 527 | 'type' => 'integer', |
528 | 528 | 'default' => 10, |
529 | 529 | 'minimum' => 1, |
@@ -532,25 +532,25 @@ discard block |
||
532 | 532 | 'validate_callback' => 'rest_validate_request_arg', |
533 | 533 | ); |
534 | 534 | $params['search'] = array( |
535 | - 'description' => __( 'Limit results to those matching a string.', 'woocommerce' ), |
|
535 | + 'description' => __('Limit results to those matching a string.', 'woocommerce'), |
|
536 | 536 | 'type' => 'string', |
537 | 537 | 'sanitize_callback' => 'sanitize_text_field', |
538 | 538 | 'validate_callback' => 'rest_validate_request_arg', |
539 | 539 | ); |
540 | 540 | $params['after'] = array( |
541 | - 'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce' ), |
|
541 | + 'description' => __('Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce'), |
|
542 | 542 | 'type' => 'string', |
543 | 543 | 'format' => 'date-time', |
544 | 544 | 'validate_callback' => 'rest_validate_request_arg', |
545 | 545 | ); |
546 | 546 | $params['before'] = array( |
547 | - 'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'woocommerce' ), |
|
547 | + 'description' => __('Limit response to resources published before a given ISO8601 compliant date.', 'woocommerce'), |
|
548 | 548 | 'type' => 'string', |
549 | 549 | 'format' => 'date-time', |
550 | 550 | 'validate_callback' => 'rest_validate_request_arg', |
551 | 551 | ); |
552 | 552 | $params['exclude'] = array( |
553 | - 'description' => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ), |
|
553 | + 'description' => __('Ensure result set excludes specific IDs.', 'woocommerce'), |
|
554 | 554 | 'type' => 'array', |
555 | 555 | 'items' => array( |
556 | 556 | 'type' => 'integer', |
@@ -559,7 +559,7 @@ discard block |
||
559 | 559 | 'sanitize_callback' => 'wp_parse_id_list', |
560 | 560 | ); |
561 | 561 | $params['include'] = array( |
562 | - 'description' => __( 'Limit result set to specific ids.', 'woocommerce' ), |
|
562 | + 'description' => __('Limit result set to specific ids.', 'woocommerce'), |
|
563 | 563 | 'type' => 'array', |
564 | 564 | 'items' => array( |
565 | 565 | 'type' => 'integer', |
@@ -568,20 +568,20 @@ discard block |
||
568 | 568 | 'sanitize_callback' => 'wp_parse_id_list', |
569 | 569 | ); |
570 | 570 | $params['offset'] = array( |
571 | - 'description' => __( 'Offset the result set by a specific number of items.', 'woocommerce' ), |
|
571 | + 'description' => __('Offset the result set by a specific number of items.', 'woocommerce'), |
|
572 | 572 | 'type' => 'integer', |
573 | 573 | 'sanitize_callback' => 'absint', |
574 | 574 | 'validate_callback' => 'rest_validate_request_arg', |
575 | 575 | ); |
576 | 576 | $params['order'] = array( |
577 | - 'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ), |
|
577 | + 'description' => __('Order sort attribute ascending or descending.', 'woocommerce'), |
|
578 | 578 | 'type' => 'string', |
579 | 579 | 'default' => 'desc', |
580 | - 'enum' => array( 'asc', 'desc' ), |
|
580 | + 'enum' => array('asc', 'desc'), |
|
581 | 581 | 'validate_callback' => 'rest_validate_request_arg', |
582 | 582 | ); |
583 | 583 | $params['orderby'] = array( |
584 | - 'description' => __( 'Sort collection by object attribute.', 'woocommerce' ), |
|
584 | + 'description' => __('Sort collection by object attribute.', 'woocommerce'), |
|
585 | 585 | 'type' => 'string', |
586 | 586 | 'default' => 'date', |
587 | 587 | 'enum' => array( |
@@ -594,9 +594,9 @@ discard block |
||
594 | 594 | 'validate_callback' => 'rest_validate_request_arg', |
595 | 595 | ); |
596 | 596 | |
597 | - if ( $this->hierarchical ) { |
|
597 | + if ($this->hierarchical) { |
|
598 | 598 | $params['parent'] = array( |
599 | - 'description' => __( 'Limit result set to those of particular parent IDs.', 'woocommerce' ), |
|
599 | + 'description' => __('Limit result set to those of particular parent IDs.', 'woocommerce'), |
|
600 | 600 | 'type' => 'array', |
601 | 601 | 'items' => array( |
602 | 602 | 'type' => 'integer', |
@@ -605,7 +605,7 @@ discard block |
||
605 | 605 | 'default' => array(), |
606 | 606 | ); |
607 | 607 | $params['parent_exclude'] = array( |
608 | - 'description' => __( 'Limit result set to all items except those of a particular parent ID.', 'woocommerce' ), |
|
608 | + 'description' => __('Limit result set to all items except those of a particular parent ID.', 'woocommerce'), |
|
609 | 609 | 'type' => 'array', |
610 | 610 | 'items' => array( |
611 | 611 | 'type' => 'integer', |
@@ -628,6 +628,6 @@ discard block |
||
628 | 628 | * @param array $query_params JSON Schema-formatted collection parameters. |
629 | 629 | * @param WP_Post_Type $post_type Post type object. |
630 | 630 | */ |
631 | - return apply_filters( "rest_{$this->post_type}_collection_params", $params, $this->post_type ); |
|
631 | + return apply_filters("rest_{$this->post_type}_collection_params", $params, $this->post_type); |
|
632 | 632 | } |
633 | 633 | } |
@@ -7,7 +7,7 @@ discard block |
||
7 | 7 | |
8 | 8 | namespace WooCommerce\RestApi\Controllers\Version4; |
9 | 9 | |
10 | -defined( 'ABSPATH' ) || exit; |
|
10 | +defined('ABSPATH') || exit; |
|
11 | 11 | |
12 | 12 | /** |
13 | 13 | * Terms controller class. |
@@ -38,26 +38,26 @@ discard block |
||
38 | 38 | array( |
39 | 39 | array( |
40 | 40 | 'methods' => \WP_REST_Server::READABLE, |
41 | - 'callback' => array( $this, 'get_items' ), |
|
42 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
41 | + 'callback' => array($this, 'get_items'), |
|
42 | + 'permission_callback' => array($this, 'get_items_permissions_check'), |
|
43 | 43 | 'args' => $this->get_collection_params(), |
44 | 44 | ), |
45 | 45 | array( |
46 | 46 | 'methods' => \WP_REST_Server::CREATABLE, |
47 | - 'callback' => array( $this, 'create_item' ), |
|
48 | - 'permission_callback' => array( $this, 'create_item_permissions_check' ), |
|
47 | + 'callback' => array($this, 'create_item'), |
|
48 | + 'permission_callback' => array($this, 'create_item_permissions_check'), |
|
49 | 49 | 'args' => array_merge( |
50 | - $this->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ), |
|
50 | + $this->get_endpoint_args_for_item_schema(\WP_REST_Server::CREATABLE), |
|
51 | 51 | array( |
52 | 52 | 'name' => array( |
53 | 53 | 'type' => 'string', |
54 | - 'description' => __( 'Name for the resource.', 'woocommerce' ), |
|
54 | + 'description' => __('Name for the resource.', 'woocommerce'), |
|
55 | 55 | 'required' => true, |
56 | 56 | ), |
57 | 57 | ) |
58 | 58 | ), |
59 | 59 | ), |
60 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
60 | + 'schema' => array($this, 'get_public_item_schema'), |
|
61 | 61 | ), |
62 | 62 | true |
63 | 63 | ); |
@@ -68,37 +68,37 @@ discard block |
||
68 | 68 | array( |
69 | 69 | 'args' => array( |
70 | 70 | 'id' => array( |
71 | - 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
|
71 | + 'description' => __('Unique identifier for the resource.', 'woocommerce'), |
|
72 | 72 | 'type' => 'integer', |
73 | 73 | ), |
74 | 74 | ), |
75 | 75 | array( |
76 | 76 | 'methods' => \WP_REST_Server::READABLE, |
77 | - 'callback' => array( $this, 'get_item' ), |
|
78 | - 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
|
77 | + 'callback' => array($this, 'get_item'), |
|
78 | + 'permission_callback' => array($this, 'get_item_permissions_check'), |
|
79 | 79 | 'args' => array( |
80 | - 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
|
80 | + 'context' => $this->get_context_param(array('default' => 'view')), |
|
81 | 81 | ), |
82 | 82 | ), |
83 | 83 | array( |
84 | 84 | 'methods' => \WP_REST_Server::EDITABLE, |
85 | - 'callback' => array( $this, 'update_item' ), |
|
86 | - 'permission_callback' => array( $this, 'update_item_permissions_check' ), |
|
87 | - 'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::EDITABLE ), |
|
85 | + 'callback' => array($this, 'update_item'), |
|
86 | + 'permission_callback' => array($this, 'update_item_permissions_check'), |
|
87 | + 'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::EDITABLE), |
|
88 | 88 | ), |
89 | 89 | array( |
90 | 90 | 'methods' => \WP_REST_Server::DELETABLE, |
91 | - 'callback' => array( $this, 'delete_item' ), |
|
92 | - 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
|
91 | + 'callback' => array($this, 'delete_item'), |
|
92 | + 'permission_callback' => array($this, 'delete_item_permissions_check'), |
|
93 | 93 | 'args' => array( |
94 | 94 | 'force' => array( |
95 | 95 | 'default' => false, |
96 | 96 | 'type' => 'boolean', |
97 | - 'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ), |
|
97 | + 'description' => __('Required to be true, as resource does not support trashing.', 'woocommerce'), |
|
98 | 98 | ), |
99 | 99 | ), |
100 | 100 | ), |
101 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
101 | + 'schema' => array($this, 'get_public_item_schema'), |
|
102 | 102 | ), |
103 | 103 | true |
104 | 104 | ); |
@@ -109,11 +109,11 @@ discard block |
||
109 | 109 | array( |
110 | 110 | array( |
111 | 111 | 'methods' => \WP_REST_Server::EDITABLE, |
112 | - 'callback' => array( $this, 'batch_items' ), |
|
113 | - 'permission_callback' => array( $this, 'batch_items_permissions_check' ), |
|
114 | - 'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::EDITABLE ), |
|
112 | + 'callback' => array($this, 'batch_items'), |
|
113 | + 'permission_callback' => array($this, 'batch_items_permissions_check'), |
|
114 | + 'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::EDITABLE), |
|
115 | 115 | ), |
116 | - 'schema' => array( $this, 'get_public_batch_schema' ), |
|
116 | + 'schema' => array($this, 'get_public_batch_schema'), |
|
117 | 117 | ), |
118 | 118 | true |
119 | 119 | ); |
@@ -125,14 +125,14 @@ discard block |
||
125 | 125 | * @param \WP_REST_Request $request Full details about the request. |
126 | 126 | * @return \WP_Error|boolean |
127 | 127 | */ |
128 | - public function get_items_permissions_check( $request ) { |
|
129 | - $permissions = $this->check_permissions( $request, 'read' ); |
|
130 | - if ( is_wp_error( $permissions ) ) { |
|
128 | + public function get_items_permissions_check($request) { |
|
129 | + $permissions = $this->check_permissions($request, 'read'); |
|
130 | + if (is_wp_error($permissions)) { |
|
131 | 131 | return $permissions; |
132 | 132 | } |
133 | 133 | |
134 | - if ( ! $permissions ) { |
|
135 | - return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
134 | + if ( ! $permissions) { |
|
135 | + return new \WP_Error('woocommerce_rest_cannot_view', __('Sorry, you cannot list resources.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
136 | 136 | } |
137 | 137 | |
138 | 138 | return true; |
@@ -144,14 +144,14 @@ discard block |
||
144 | 144 | * @param \WP_REST_Request $request Full details about the request. |
145 | 145 | * @return \WP_Error|boolean |
146 | 146 | */ |
147 | - public function create_item_permissions_check( $request ) { |
|
148 | - $permissions = $this->check_permissions( $request, 'create' ); |
|
149 | - if ( is_wp_error( $permissions ) ) { |
|
147 | + public function create_item_permissions_check($request) { |
|
148 | + $permissions = $this->check_permissions($request, 'create'); |
|
149 | + if (is_wp_error($permissions)) { |
|
150 | 150 | return $permissions; |
151 | 151 | } |
152 | 152 | |
153 | - if ( ! $permissions ) { |
|
154 | - return new \WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
153 | + if ( ! $permissions) { |
|
154 | + return new \WP_Error('woocommerce_rest_cannot_create', __('Sorry, you are not allowed to create resources.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
155 | 155 | } |
156 | 156 | |
157 | 157 | return true; |
@@ -163,14 +163,14 @@ discard block |
||
163 | 163 | * @param \WP_REST_Request $request Full details about the request. |
164 | 164 | * @return \WP_Error|boolean |
165 | 165 | */ |
166 | - public function get_item_permissions_check( $request ) { |
|
167 | - $permissions = $this->check_permissions( $request, 'read' ); |
|
168 | - if ( is_wp_error( $permissions ) ) { |
|
166 | + public function get_item_permissions_check($request) { |
|
167 | + $permissions = $this->check_permissions($request, 'read'); |
|
168 | + if (is_wp_error($permissions)) { |
|
169 | 169 | return $permissions; |
170 | 170 | } |
171 | 171 | |
172 | - if ( ! $permissions ) { |
|
173 | - return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
172 | + if ( ! $permissions) { |
|
173 | + return new \WP_Error('woocommerce_rest_cannot_view', __('Sorry, you cannot view this resource.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
174 | 174 | } |
175 | 175 | |
176 | 176 | return true; |
@@ -182,14 +182,14 @@ discard block |
||
182 | 182 | * @param \WP_REST_Request $request Full details about the request. |
183 | 183 | * @return \WP_Error|boolean |
184 | 184 | */ |
185 | - public function update_item_permissions_check( $request ) { |
|
186 | - $permissions = $this->check_permissions( $request, 'edit' ); |
|
187 | - if ( is_wp_error( $permissions ) ) { |
|
185 | + public function update_item_permissions_check($request) { |
|
186 | + $permissions = $this->check_permissions($request, 'edit'); |
|
187 | + if (is_wp_error($permissions)) { |
|
188 | 188 | return $permissions; |
189 | 189 | } |
190 | 190 | |
191 | - if ( ! $permissions ) { |
|
192 | - return new \WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
191 | + if ( ! $permissions) { |
|
192 | + return new \WP_Error('woocommerce_rest_cannot_edit', __('Sorry, you are not allowed to edit this resource.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
193 | 193 | } |
194 | 194 | |
195 | 195 | return true; |
@@ -201,14 +201,14 @@ discard block |
||
201 | 201 | * @param \WP_REST_Request $request Full details about the request. |
202 | 202 | * @return \WP_Error|boolean |
203 | 203 | */ |
204 | - public function delete_item_permissions_check( $request ) { |
|
205 | - $permissions = $this->check_permissions( $request, 'delete' ); |
|
206 | - if ( is_wp_error( $permissions ) ) { |
|
204 | + public function delete_item_permissions_check($request) { |
|
205 | + $permissions = $this->check_permissions($request, 'delete'); |
|
206 | + if (is_wp_error($permissions)) { |
|
207 | 207 | return $permissions; |
208 | 208 | } |
209 | 209 | |
210 | - if ( ! $permissions ) { |
|
211 | - return new \WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
210 | + if ( ! $permissions) { |
|
211 | + return new \WP_Error('woocommerce_rest_cannot_delete', __('Sorry, you are not allowed to delete this resource.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
212 | 212 | } |
213 | 213 | |
214 | 214 | return true; |
@@ -220,14 +220,14 @@ discard block |
||
220 | 220 | * @param \WP_REST_Request $request Full details about the request. |
221 | 221 | * @return boolean|\WP_Error |
222 | 222 | */ |
223 | - public function batch_items_permissions_check( $request ) { |
|
224 | - $permissions = $this->check_permissions( $request, 'batch' ); |
|
225 | - if ( is_wp_error( $permissions ) ) { |
|
223 | + public function batch_items_permissions_check($request) { |
|
224 | + $permissions = $this->check_permissions($request, 'batch'); |
|
225 | + if (is_wp_error($permissions)) { |
|
226 | 226 | return $permissions; |
227 | 227 | } |
228 | 228 | |
229 | - if ( ! $permissions ) { |
|
230 | - return new \WP_Error( 'woocommerce_rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
229 | + if ( ! $permissions) { |
|
230 | + return new \WP_Error('woocommerce_rest_cannot_batch', __('Sorry, you are not allowed to batch manipulate this resource.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
231 | 231 | } |
232 | 232 | |
233 | 233 | return true; |
@@ -240,26 +240,26 @@ discard block |
||
240 | 240 | * @param string $context Request context. |
241 | 241 | * @return bool|\WP_Error |
242 | 242 | */ |
243 | - protected function check_permissions( $request, $context = 'read' ) { |
|
243 | + protected function check_permissions($request, $context = 'read') { |
|
244 | 244 | // Get taxonomy. |
245 | - $taxonomy = $this->get_taxonomy( $request ); |
|
246 | - if ( ! $taxonomy || ! taxonomy_exists( $taxonomy ) ) { |
|
247 | - return new \WP_Error( 'woocommerce_rest_taxonomy_invalid', __( 'Taxonomy does not exist.', 'woocommerce' ), array( 'status' => 404 ) ); |
|
245 | + $taxonomy = $this->get_taxonomy($request); |
|
246 | + if ( ! $taxonomy || ! taxonomy_exists($taxonomy)) { |
|
247 | + return new \WP_Error('woocommerce_rest_taxonomy_invalid', __('Taxonomy does not exist.', 'woocommerce'), array('status' => 404)); |
|
248 | 248 | } |
249 | 249 | |
250 | 250 | // Check permissions for a single term. |
251 | - $id = intval( $request['id'] ); |
|
252 | - if ( $id ) { |
|
253 | - $term = get_term( $id, $taxonomy ); |
|
251 | + $id = intval($request['id']); |
|
252 | + if ($id) { |
|
253 | + $term = get_term($id, $taxonomy); |
|
254 | 254 | |
255 | - if ( is_wp_error( $term ) || ! $term || $term->taxonomy !== $taxonomy ) { |
|
256 | - return new \WP_Error( 'woocommerce_rest_term_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) ); |
|
255 | + if (is_wp_error($term) || ! $term || $term->taxonomy !== $taxonomy) { |
|
256 | + return new \WP_Error('woocommerce_rest_term_invalid', __('Resource does not exist.', 'woocommerce'), array('status' => 404)); |
|
257 | 257 | } |
258 | 258 | |
259 | - return wc_rest_check_product_term_permissions( $taxonomy, $context, $term->term_id ); |
|
259 | + return wc_rest_check_product_term_permissions($taxonomy, $context, $term->term_id); |
|
260 | 260 | } |
261 | 261 | |
262 | - return wc_rest_check_product_term_permissions( $taxonomy, $context ); |
|
262 | + return wc_rest_check_product_term_permissions($taxonomy, $context); |
|
263 | 263 | } |
264 | 264 | |
265 | 265 | /** |
@@ -268,8 +268,8 @@ discard block |
||
268 | 268 | * @param \WP_REST_Request $request Full details about the request. |
269 | 269 | * @return \WP_REST_Response|\WP_Error |
270 | 270 | */ |
271 | - public function get_items( $request ) { |
|
272 | - $taxonomy = $this->get_taxonomy( $request ); |
|
271 | + public function get_items($request) { |
|
272 | + $taxonomy = $this->get_taxonomy($request); |
|
273 | 273 | $prepared_args = array( |
274 | 274 | 'exclude' => $request['exclude'], |
275 | 275 | 'include' => $request['include'], |
@@ -282,20 +282,20 @@ discard block |
||
282 | 282 | 'slug' => $request['slug'], |
283 | 283 | ); |
284 | 284 | |
285 | - if ( ! empty( $request['offset'] ) ) { |
|
285 | + if ( ! empty($request['offset'])) { |
|
286 | 286 | $prepared_args['offset'] = $request['offset']; |
287 | 287 | } else { |
288 | - $prepared_args['offset'] = ( $request['page'] - 1 ) * $prepared_args['number']; |
|
288 | + $prepared_args['offset'] = ($request['page'] - 1) * $prepared_args['number']; |
|
289 | 289 | } |
290 | 290 | |
291 | - $taxonomy_obj = get_taxonomy( $taxonomy ); |
|
291 | + $taxonomy_obj = get_taxonomy($taxonomy); |
|
292 | 292 | |
293 | - if ( $taxonomy_obj->hierarchical && isset( $request['parent'] ) ) { |
|
294 | - if ( 0 === $request['parent'] ) { |
|
293 | + if ($taxonomy_obj->hierarchical && isset($request['parent'])) { |
|
294 | + if (0 === $request['parent']) { |
|
295 | 295 | // Only query top-level terms. |
296 | 296 | $prepared_args['parent'] = 0; |
297 | 297 | } else { |
298 | - if ( $request['parent'] ) { |
|
298 | + if ($request['parent']) { |
|
299 | 299 | $prepared_args['parent'] = $request['parent']; |
300 | 300 | } |
301 | 301 | } |
@@ -313,60 +313,60 @@ discard block |
||
313 | 313 | * passed to get_terms. |
314 | 314 | * @param \WP_REST_Request $request The current request. |
315 | 315 | */ |
316 | - $prepared_args = apply_filters( "woocommerce_rest_{$taxonomy}_query", $prepared_args, $request ); |
|
316 | + $prepared_args = apply_filters("woocommerce_rest_{$taxonomy}_query", $prepared_args, $request); |
|
317 | 317 | |
318 | - if ( ! empty( $prepared_args['product'] ) ) { |
|
319 | - $query_result = $this->get_terms_for_product( $prepared_args, $request ); |
|
318 | + if ( ! empty($prepared_args['product'])) { |
|
319 | + $query_result = $this->get_terms_for_product($prepared_args, $request); |
|
320 | 320 | $total_terms = $this->total_terms; |
321 | 321 | } else { |
322 | - $query_result = get_terms( $taxonomy, $prepared_args ); |
|
322 | + $query_result = get_terms($taxonomy, $prepared_args); |
|
323 | 323 | |
324 | 324 | $count_args = $prepared_args; |
325 | - unset( $count_args['number'] ); |
|
326 | - unset( $count_args['offset'] ); |
|
327 | - $total_terms = wp_count_terms( $taxonomy, $count_args ); |
|
325 | + unset($count_args['number']); |
|
326 | + unset($count_args['offset']); |
|
327 | + $total_terms = wp_count_terms($taxonomy, $count_args); |
|
328 | 328 | |
329 | 329 | // Ensure we don't return results when offset is out of bounds. |
330 | 330 | // See https://core.trac.wordpress.org/ticket/35935. |
331 | - if ( $prepared_args['offset'] && $prepared_args['offset'] >= $total_terms ) { |
|
331 | + if ($prepared_args['offset'] && $prepared_args['offset'] >= $total_terms) { |
|
332 | 332 | $query_result = array(); |
333 | 333 | } |
334 | 334 | |
335 | 335 | // wp_count_terms can return a falsy value when the term has no children. |
336 | - if ( ! $total_terms ) { |
|
336 | + if ( ! $total_terms) { |
|
337 | 337 | $total_terms = 0; |
338 | 338 | } |
339 | 339 | } |
340 | 340 | $response = array(); |
341 | - foreach ( $query_result as $term ) { |
|
342 | - $data = $this->prepare_item_for_response( $term, $request ); |
|
343 | - $response[] = $this->prepare_response_for_collection( $data ); |
|
341 | + foreach ($query_result as $term) { |
|
342 | + $data = $this->prepare_item_for_response($term, $request); |
|
343 | + $response[] = $this->prepare_response_for_collection($data); |
|
344 | 344 | } |
345 | 345 | |
346 | - $response = rest_ensure_response( $response ); |
|
346 | + $response = rest_ensure_response($response); |
|
347 | 347 | |
348 | 348 | // Store pagination values for headers then unset for count query. |
349 | 349 | $per_page = (int) $prepared_args['number']; |
350 | - $page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 ); |
|
350 | + $page = ceil((((int) $prepared_args['offset']) / $per_page) + 1); |
|
351 | 351 | |
352 | - $response->header( 'X-WP-Total', (int) $total_terms ); |
|
353 | - $max_pages = ceil( $total_terms / $per_page ); |
|
354 | - $response->header( 'X-WP-TotalPages', (int) $max_pages ); |
|
352 | + $response->header('X-WP-Total', (int) $total_terms); |
|
353 | + $max_pages = ceil($total_terms / $per_page); |
|
354 | + $response->header('X-WP-TotalPages', (int) $max_pages); |
|
355 | 355 | |
356 | - $base = str_replace( '(?P<attribute_id>[\d]+)', $request['attribute_id'], $this->rest_base ); |
|
357 | - $base = add_query_arg( $request->get_query_params(), rest_url( '/' . $this->namespace . '/' . $base ) ); |
|
358 | - if ( $page > 1 ) { |
|
356 | + $base = str_replace('(?P<attribute_id>[\d]+)', $request['attribute_id'], $this->rest_base); |
|
357 | + $base = add_query_arg($request->get_query_params(), rest_url('/' . $this->namespace . '/' . $base)); |
|
358 | + if ($page > 1) { |
|
359 | 359 | $prev_page = $page - 1; |
360 | - if ( $prev_page > $max_pages ) { |
|
360 | + if ($prev_page > $max_pages) { |
|
361 | 361 | $prev_page = $max_pages; |
362 | 362 | } |
363 | - $prev_link = add_query_arg( 'page', $prev_page, $base ); |
|
364 | - $response->link_header( 'prev', $prev_link ); |
|
363 | + $prev_link = add_query_arg('page', $prev_page, $base); |
|
364 | + $response->link_header('prev', $prev_link); |
|
365 | 365 | } |
366 | - if ( $max_pages > $page ) { |
|
366 | + if ($max_pages > $page) { |
|
367 | 367 | $next_page = $page + 1; |
368 | - $next_link = add_query_arg( 'page', $next_page, $base ); |
|
369 | - $response->link_header( 'next', $next_link ); |
|
368 | + $next_link = add_query_arg('page', $next_page, $base); |
|
369 | + $response->link_header('next', $next_link); |
|
370 | 370 | } |
371 | 371 | |
372 | 372 | return $response; |
@@ -378,47 +378,47 @@ discard block |
||
378 | 378 | * @param \WP_REST_Request $request Full details about the request. |
379 | 379 | * @return \WP_REST_Request|\WP_Error |
380 | 380 | */ |
381 | - public function create_item( $request ) { |
|
382 | - $taxonomy = $this->get_taxonomy( $request ); |
|
381 | + public function create_item($request) { |
|
382 | + $taxonomy = $this->get_taxonomy($request); |
|
383 | 383 | $name = $request['name']; |
384 | 384 | $args = array(); |
385 | 385 | $schema = $this->get_item_schema(); |
386 | 386 | |
387 | - if ( ! empty( $schema['properties']['description'] ) && isset( $request['description'] ) ) { |
|
387 | + if ( ! empty($schema['properties']['description']) && isset($request['description'])) { |
|
388 | 388 | $args['description'] = $request['description']; |
389 | 389 | } |
390 | - if ( isset( $request['slug'] ) ) { |
|
390 | + if (isset($request['slug'])) { |
|
391 | 391 | $args['slug'] = $request['slug']; |
392 | 392 | } |
393 | - if ( isset( $request['parent'] ) ) { |
|
394 | - if ( ! is_taxonomy_hierarchical( $taxonomy ) ) { |
|
395 | - return new \WP_Error( 'woocommerce_rest_taxonomy_not_hierarchical', __( 'Can not set resource parent, taxonomy is not hierarchical.', 'woocommerce' ), array( 'status' => 400 ) ); |
|
393 | + if (isset($request['parent'])) { |
|
394 | + if ( ! is_taxonomy_hierarchical($taxonomy)) { |
|
395 | + return new \WP_Error('woocommerce_rest_taxonomy_not_hierarchical', __('Can not set resource parent, taxonomy is not hierarchical.', 'woocommerce'), array('status' => 400)); |
|
396 | 396 | } |
397 | 397 | $args['parent'] = $request['parent']; |
398 | 398 | } |
399 | 399 | |
400 | - $term = wp_insert_term( $name, $taxonomy, $args ); |
|
401 | - if ( is_wp_error( $term ) ) { |
|
402 | - $error_data = array( 'status' => 400 ); |
|
400 | + $term = wp_insert_term($name, $taxonomy, $args); |
|
401 | + if (is_wp_error($term)) { |
|
402 | + $error_data = array('status' => 400); |
|
403 | 403 | |
404 | 404 | // If we're going to inform the client that the term exists, |
405 | 405 | // give them the identifier they can actually use. |
406 | - $term_id = $term->get_error_data( 'term_exists' ); |
|
407 | - if ( $term_id ) { |
|
406 | + $term_id = $term->get_error_data('term_exists'); |
|
407 | + if ($term_id) { |
|
408 | 408 | $error_data['resource_id'] = $term_id; |
409 | 409 | } |
410 | 410 | |
411 | - return new \WP_Error( $term->get_error_code(), $term->get_error_message(), $error_data ); |
|
411 | + return new \WP_Error($term->get_error_code(), $term->get_error_message(), $error_data); |
|
412 | 412 | } |
413 | 413 | |
414 | - $term = get_term( $term['term_id'], $taxonomy ); |
|
414 | + $term = get_term($term['term_id'], $taxonomy); |
|
415 | 415 | |
416 | - $this->update_additional_fields_for_object( $term, $request ); |
|
416 | + $this->update_additional_fields_for_object($term, $request); |
|
417 | 417 | |
418 | 418 | // Add term data. |
419 | - $meta_fields = $this->update_term_meta_fields( $term, $request ); |
|
420 | - if ( is_wp_error( $meta_fields ) ) { |
|
421 | - wp_delete_term( $term->term_id, $taxonomy ); |
|
419 | + $meta_fields = $this->update_term_meta_fields($term, $request); |
|
420 | + if (is_wp_error($meta_fields)) { |
|
421 | + wp_delete_term($term->term_id, $taxonomy); |
|
422 | 422 | |
423 | 423 | return $meta_fields; |
424 | 424 | } |
@@ -430,19 +430,19 @@ discard block |
||
430 | 430 | * @param \WP_REST_Request $request Request object. |
431 | 431 | * @param boolean $creating True when creating term, false when updating. |
432 | 432 | */ |
433 | - do_action( "woocommerce_rest_insert_{$taxonomy}", $term, $request, true ); |
|
433 | + do_action("woocommerce_rest_insert_{$taxonomy}", $term, $request, true); |
|
434 | 434 | |
435 | - $request->set_param( 'context', 'edit' ); |
|
436 | - $response = $this->prepare_item_for_response( $term, $request ); |
|
437 | - $response = rest_ensure_response( $response ); |
|
438 | - $response->set_status( 201 ); |
|
435 | + $request->set_param('context', 'edit'); |
|
436 | + $response = $this->prepare_item_for_response($term, $request); |
|
437 | + $response = rest_ensure_response($response); |
|
438 | + $response->set_status(201); |
|
439 | 439 | |
440 | 440 | $base = '/' . $this->namespace . '/' . $this->rest_base; |
441 | - if ( ! empty( $request['attribute_id'] ) ) { |
|
442 | - $base = str_replace( '(?P<attribute_id>[\d]+)', (int) $request['attribute_id'], $base ); |
|
441 | + if ( ! empty($request['attribute_id'])) { |
|
442 | + $base = str_replace('(?P<attribute_id>[\d]+)', (int) $request['attribute_id'], $base); |
|
443 | 443 | } |
444 | 444 | |
445 | - $response->header( 'Location', rest_url( $base . '/' . $term->term_id ) ); |
|
445 | + $response->header('Location', rest_url($base . '/' . $term->term_id)); |
|
446 | 446 | |
447 | 447 | return $response; |
448 | 448 | } |
@@ -453,17 +453,17 @@ discard block |
||
453 | 453 | * @param \WP_REST_Request $request Full details about the request. |
454 | 454 | * @return \WP_REST_Request|\WP_Error |
455 | 455 | */ |
456 | - public function get_item( $request ) { |
|
457 | - $taxonomy = $this->get_taxonomy( $request ); |
|
458 | - $term = get_term( (int) $request['id'], $taxonomy ); |
|
456 | + public function get_item($request) { |
|
457 | + $taxonomy = $this->get_taxonomy($request); |
|
458 | + $term = get_term((int) $request['id'], $taxonomy); |
|
459 | 459 | |
460 | - if ( is_wp_error( $term ) ) { |
|
460 | + if (is_wp_error($term)) { |
|
461 | 461 | return $term; |
462 | 462 | } |
463 | 463 | |
464 | - $response = $this->prepare_item_for_response( $term, $request ); |
|
464 | + $response = $this->prepare_item_for_response($term, $request); |
|
465 | 465 | |
466 | - return rest_ensure_response( $response ); |
|
466 | + return rest_ensure_response($response); |
|
467 | 467 | } |
468 | 468 | |
469 | 469 | /** |
@@ -472,43 +472,43 @@ discard block |
||
472 | 472 | * @param \WP_REST_Request $request Full details about the request. |
473 | 473 | * @return \WP_REST_Request|\WP_Error |
474 | 474 | */ |
475 | - public function update_item( $request ) { |
|
476 | - $taxonomy = $this->get_taxonomy( $request ); |
|
477 | - $term = get_term( (int) $request['id'], $taxonomy ); |
|
475 | + public function update_item($request) { |
|
476 | + $taxonomy = $this->get_taxonomy($request); |
|
477 | + $term = get_term((int) $request['id'], $taxonomy); |
|
478 | 478 | $schema = $this->get_item_schema(); |
479 | 479 | $prepared_args = array(); |
480 | 480 | |
481 | - if ( isset( $request['name'] ) ) { |
|
481 | + if (isset($request['name'])) { |
|
482 | 482 | $prepared_args['name'] = $request['name']; |
483 | 483 | } |
484 | - if ( ! empty( $schema['properties']['description'] ) && isset( $request['description'] ) ) { |
|
484 | + if ( ! empty($schema['properties']['description']) && isset($request['description'])) { |
|
485 | 485 | $prepared_args['description'] = $request['description']; |
486 | 486 | } |
487 | - if ( isset( $request['slug'] ) ) { |
|
487 | + if (isset($request['slug'])) { |
|
488 | 488 | $prepared_args['slug'] = $request['slug']; |
489 | 489 | } |
490 | - if ( isset( $request['parent'] ) ) { |
|
491 | - if ( ! is_taxonomy_hierarchical( $taxonomy ) ) { |
|
492 | - return new \WP_Error( 'woocommerce_rest_taxonomy_not_hierarchical', __( 'Can not set resource parent, taxonomy is not hierarchical.', 'woocommerce' ), array( 'status' => 400 ) ); |
|
490 | + if (isset($request['parent'])) { |
|
491 | + if ( ! is_taxonomy_hierarchical($taxonomy)) { |
|
492 | + return new \WP_Error('woocommerce_rest_taxonomy_not_hierarchical', __('Can not set resource parent, taxonomy is not hierarchical.', 'woocommerce'), array('status' => 400)); |
|
493 | 493 | } |
494 | 494 | $prepared_args['parent'] = $request['parent']; |
495 | 495 | } |
496 | 496 | |
497 | 497 | // Only update the term if we haz something to update. |
498 | - if ( ! empty( $prepared_args ) ) { |
|
499 | - $update = wp_update_term( $term->term_id, $term->taxonomy, $prepared_args ); |
|
500 | - if ( is_wp_error( $update ) ) { |
|
498 | + if ( ! empty($prepared_args)) { |
|
499 | + $update = wp_update_term($term->term_id, $term->taxonomy, $prepared_args); |
|
500 | + if (is_wp_error($update)) { |
|
501 | 501 | return $update; |
502 | 502 | } |
503 | 503 | } |
504 | 504 | |
505 | - $term = get_term( (int) $request['id'], $taxonomy ); |
|
505 | + $term = get_term((int) $request['id'], $taxonomy); |
|
506 | 506 | |
507 | - $this->update_additional_fields_for_object( $term, $request ); |
|
507 | + $this->update_additional_fields_for_object($term, $request); |
|
508 | 508 | |
509 | 509 | // Update term data. |
510 | - $meta_fields = $this->update_term_meta_fields( $term, $request ); |
|
511 | - if ( is_wp_error( $meta_fields ) ) { |
|
510 | + $meta_fields = $this->update_term_meta_fields($term, $request); |
|
511 | + if (is_wp_error($meta_fields)) { |
|
512 | 512 | return $meta_fields; |
513 | 513 | } |
514 | 514 | |
@@ -519,11 +519,11 @@ discard block |
||
519 | 519 | * @param \WP_REST_Request $request Request object. |
520 | 520 | * @param boolean $creating True when creating term, false when updating. |
521 | 521 | */ |
522 | - do_action( "woocommerce_rest_insert_{$taxonomy}", $term, $request, false ); |
|
522 | + do_action("woocommerce_rest_insert_{$taxonomy}", $term, $request, false); |
|
523 | 523 | |
524 | - $request->set_param( 'context', 'edit' ); |
|
525 | - $response = $this->prepare_item_for_response( $term, $request ); |
|
526 | - return rest_ensure_response( $response ); |
|
524 | + $request->set_param('context', 'edit'); |
|
525 | + $response = $this->prepare_item_for_response($term, $request); |
|
526 | + return rest_ensure_response($response); |
|
527 | 527 | } |
528 | 528 | |
529 | 529 | /** |
@@ -532,30 +532,30 @@ discard block |
||
532 | 532 | * @param \WP_REST_Request $request Full details about the request. |
533 | 533 | * @return \WP_REST_Response|\WP_Error |
534 | 534 | */ |
535 | - public function delete_item( $request ) { |
|
536 | - $taxonomy = $this->get_taxonomy( $request ); |
|
537 | - $force = isset( $request['force'] ) ? (bool) $request['force'] : false; |
|
535 | + public function delete_item($request) { |
|
536 | + $taxonomy = $this->get_taxonomy($request); |
|
537 | + $force = isset($request['force']) ? (bool) $request['force'] : false; |
|
538 | 538 | |
539 | 539 | // We don't support trashing for this type, error out. |
540 | - if ( ! $force ) { |
|
541 | - return new \WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Resource does not support trashing.', 'woocommerce' ), array( 'status' => 501 ) ); |
|
540 | + if ( ! $force) { |
|
541 | + return new \WP_Error('woocommerce_rest_trash_not_supported', __('Resource does not support trashing.', 'woocommerce'), array('status' => 501)); |
|
542 | 542 | } |
543 | 543 | |
544 | - $term = get_term( (int) $request['id'], $taxonomy ); |
|
544 | + $term = get_term((int) $request['id'], $taxonomy); |
|
545 | 545 | // Get default category id. |
546 | - $default_category_id = absint( get_option( 'default_product_cat', 0 ) ); |
|
546 | + $default_category_id = absint(get_option('default_product_cat', 0)); |
|
547 | 547 | |
548 | 548 | // Prevent deleting the default product category. |
549 | - if ( $default_category_id === (int) $request['id'] ) { |
|
550 | - return new \WP_Error( 'woocommerce_rest_cannot_delete', __( 'Default product category cannot be deleted.', 'woocommerce' ), array( 'status' => 500 ) ); |
|
549 | + if ($default_category_id === (int) $request['id']) { |
|
550 | + return new \WP_Error('woocommerce_rest_cannot_delete', __('Default product category cannot be deleted.', 'woocommerce'), array('status' => 500)); |
|
551 | 551 | } |
552 | 552 | |
553 | - $request->set_param( 'context', 'edit' ); |
|
554 | - $response = $this->prepare_item_for_response( $term, $request ); |
|
553 | + $request->set_param('context', 'edit'); |
|
554 | + $response = $this->prepare_item_for_response($term, $request); |
|
555 | 555 | |
556 | - $retval = wp_delete_term( $term->term_id, $term->taxonomy ); |
|
557 | - if ( ! $retval ) { |
|
558 | - return new \WP_Error( 'woocommerce_rest_cannot_delete', __( 'The resource cannot be deleted.', 'woocommerce' ), array( 'status' => 500 ) ); |
|
556 | + $retval = wp_delete_term($term->term_id, $term->taxonomy); |
|
557 | + if ( ! $retval) { |
|
558 | + return new \WP_Error('woocommerce_rest_cannot_delete', __('The resource cannot be deleted.', 'woocommerce'), array('status' => 500)); |
|
559 | 559 | } |
560 | 560 | |
561 | 561 | /** |
@@ -565,7 +565,7 @@ discard block |
||
565 | 565 | * @param \WP_REST_Response $response The response data. |
566 | 566 | * @param \WP_REST_Request $request The request sent to the API. |
567 | 567 | */ |
568 | - do_action( "woocommerce_rest_delete_{$taxonomy}", $term, $response, $request ); |
|
568 | + do_action("woocommerce_rest_delete_{$taxonomy}", $term, $response, $request); |
|
569 | 569 | |
570 | 570 | return $response; |
571 | 571 | } |
@@ -577,27 +577,27 @@ discard block |
||
577 | 577 | * @param \WP_REST_Request $request Full details about the request. |
578 | 578 | * @return array Links for the given term. |
579 | 579 | */ |
580 | - protected function prepare_links( $term, $request ) { |
|
580 | + protected function prepare_links($term, $request) { |
|
581 | 581 | $base = '/' . $this->namespace . '/' . $this->rest_base; |
582 | 582 | |
583 | - if ( ! empty( $request['attribute_id'] ) ) { |
|
584 | - $base = str_replace( '(?P<attribute_id>[\d]+)', (int) $request['attribute_id'], $base ); |
|
583 | + if ( ! empty($request['attribute_id'])) { |
|
584 | + $base = str_replace('(?P<attribute_id>[\d]+)', (int) $request['attribute_id'], $base); |
|
585 | 585 | } |
586 | 586 | |
587 | 587 | $links = array( |
588 | 588 | 'self' => array( |
589 | - 'href' => rest_url( trailingslashit( $base ) . $term->term_id ), |
|
589 | + 'href' => rest_url(trailingslashit($base) . $term->term_id), |
|
590 | 590 | ), |
591 | 591 | 'collection' => array( |
592 | - 'href' => rest_url( $base ), |
|
592 | + 'href' => rest_url($base), |
|
593 | 593 | ), |
594 | 594 | ); |
595 | 595 | |
596 | - if ( $term->parent ) { |
|
597 | - $parent_term = get_term( (int) $term->parent, $term->taxonomy ); |
|
598 | - if ( $parent_term ) { |
|
596 | + if ($term->parent) { |
|
597 | + $parent_term = get_term((int) $term->parent, $term->taxonomy); |
|
598 | + if ($parent_term) { |
|
599 | 599 | $links['up'] = array( |
600 | - 'href' => rest_url( trailingslashit( $base ) . $parent_term->term_id ), |
|
600 | + 'href' => rest_url(trailingslashit($base) . $parent_term->term_id), |
|
601 | 601 | ); |
602 | 602 | } |
603 | 603 | } |
@@ -612,7 +612,7 @@ discard block |
||
612 | 612 | * @param \WP_REST_Request $request Full details about the request. |
613 | 613 | * @return bool|\WP_Error |
614 | 614 | */ |
615 | - protected function update_term_meta_fields( $term, $request ) { |
|
615 | + protected function update_term_meta_fields($term, $request) { |
|
616 | 616 | return true; |
617 | 617 | } |
618 | 618 | |
@@ -628,19 +628,19 @@ discard block |
||
628 | 628 | * @param \WP_REST_Request $request Full details about the request. |
629 | 629 | * @return array List of term objects. (Total count in `$this->total_terms`). |
630 | 630 | */ |
631 | - protected function get_terms_for_product( $prepared_args, $request ) { |
|
632 | - $taxonomy = $this->get_taxonomy( $request ); |
|
631 | + protected function get_terms_for_product($prepared_args, $request) { |
|
632 | + $taxonomy = $this->get_taxonomy($request); |
|
633 | 633 | |
634 | - $query_result = get_the_terms( $prepared_args['product'], $taxonomy ); |
|
635 | - if ( empty( $query_result ) ) { |
|
634 | + $query_result = get_the_terms($prepared_args['product'], $taxonomy); |
|
635 | + if (empty($query_result)) { |
|
636 | 636 | $this->total_terms = 0; |
637 | 637 | return array(); |
638 | 638 | } |
639 | 639 | |
640 | 640 | // get_items() verifies that we don't have `include` set, and default. |
641 | 641 | // ordering is by `name`. |
642 | - if ( ! in_array( $prepared_args['orderby'], array( 'name', 'none', 'include' ), true ) ) { |
|
643 | - switch ( $prepared_args['orderby'] ) { |
|
642 | + if ( ! in_array($prepared_args['orderby'], array('name', 'none', 'include'), true)) { |
|
643 | + switch ($prepared_args['orderby']) { |
|
644 | 644 | case 'id': |
645 | 645 | $this->sort_column = 'term_id'; |
646 | 646 | break; |
@@ -651,15 +651,15 @@ discard block |
||
651 | 651 | $this->sort_column = $prepared_args['orderby']; |
652 | 652 | break; |
653 | 653 | } |
654 | - usort( $query_result, array( $this, 'compare_terms' ) ); |
|
654 | + usort($query_result, array($this, 'compare_terms')); |
|
655 | 655 | } |
656 | - if ( strtolower( $prepared_args['order'] ) !== 'asc' ) { |
|
657 | - $query_result = array_reverse( $query_result ); |
|
656 | + if (strtolower($prepared_args['order']) !== 'asc') { |
|
657 | + $query_result = array_reverse($query_result); |
|
658 | 658 | } |
659 | 659 | |
660 | 660 | // Pagination. |
661 | - $this->total_terms = count( $query_result ); |
|
662 | - $query_result = array_slice( $query_result, $prepared_args['offset'], $prepared_args['number'] ); |
|
661 | + $this->total_terms = count($query_result); |
|
662 | + $query_result = array_slice($query_result, $prepared_args['offset'], $prepared_args['number']); |
|
663 | 663 | |
664 | 664 | return $query_result; |
665 | 665 | } |
@@ -673,16 +673,16 @@ discard block |
||
673 | 673 | * @param stdClass $right Term object. |
674 | 674 | * @return int <0 if left is higher "priority" than right, 0 if equal, >0 if right is higher "priority" than left. |
675 | 675 | */ |
676 | - protected function compare_terms( $left, $right ) { |
|
676 | + protected function compare_terms($left, $right) { |
|
677 | 677 | $col = $this->sort_column; |
678 | 678 | $left_val = $left->$col; |
679 | 679 | $right_val = $right->$col; |
680 | 680 | |
681 | - if ( is_int( $left_val ) && is_int( $right_val ) ) { |
|
681 | + if (is_int($left_val) && is_int($right_val)) { |
|
682 | 682 | return $left_val - $right_val; |
683 | 683 | } |
684 | 684 | |
685 | - return strcmp( $left_val, $right_val ); |
|
685 | + return strcmp($left_val, $right_val); |
|
686 | 686 | } |
687 | 687 | |
688 | 688 | /** |
@@ -693,8 +693,8 @@ discard block |
||
693 | 693 | public function get_collection_params() { |
694 | 694 | $params = parent::get_collection_params(); |
695 | 695 | |
696 | - if ( '' !== $this->taxonomy && taxonomy_exists( $this->taxonomy ) ) { |
|
697 | - $taxonomy = get_taxonomy( $this->taxonomy ); |
|
696 | + if ('' !== $this->taxonomy && taxonomy_exists($this->taxonomy)) { |
|
697 | + $taxonomy = get_taxonomy($this->taxonomy); |
|
698 | 698 | } else { |
699 | 699 | $taxonomy = new \stdClass(); |
700 | 700 | $taxonomy->hierarchical = true; |
@@ -703,7 +703,7 @@ discard block |
||
703 | 703 | $params['context']['default'] = 'view'; |
704 | 704 | |
705 | 705 | $params['exclude'] = array( |
706 | - 'description' => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ), |
|
706 | + 'description' => __('Ensure result set excludes specific IDs.', 'woocommerce'), |
|
707 | 707 | 'type' => 'array', |
708 | 708 | 'items' => array( |
709 | 709 | 'type' => 'integer', |
@@ -712,7 +712,7 @@ discard block |
||
712 | 712 | 'sanitize_callback' => 'wp_parse_id_list', |
713 | 713 | ); |
714 | 714 | $params['include'] = array( |
715 | - 'description' => __( 'Limit result set to specific ids.', 'woocommerce' ), |
|
715 | + 'description' => __('Limit result set to specific ids.', 'woocommerce'), |
|
716 | 716 | 'type' => 'array', |
717 | 717 | 'items' => array( |
718 | 718 | 'type' => 'integer', |
@@ -720,16 +720,16 @@ discard block |
||
720 | 720 | 'default' => array(), |
721 | 721 | 'sanitize_callback' => 'wp_parse_id_list', |
722 | 722 | ); |
723 | - if ( ! $taxonomy->hierarchical ) { |
|
723 | + if ( ! $taxonomy->hierarchical) { |
|
724 | 724 | $params['offset'] = array( |
725 | - 'description' => __( 'Offset the result set by a specific number of items.', 'woocommerce' ), |
|
725 | + 'description' => __('Offset the result set by a specific number of items.', 'woocommerce'), |
|
726 | 726 | 'type' => 'integer', |
727 | 727 | 'sanitize_callback' => 'absint', |
728 | 728 | 'validate_callback' => 'rest_validate_request_arg', |
729 | 729 | ); |
730 | 730 | } |
731 | - $params['order'] = array( |
|
732 | - 'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ), |
|
731 | + $params['order'] = array( |
|
732 | + 'description' => __('Order sort attribute ascending or descending.', 'woocommerce'), |
|
733 | 733 | 'type' => 'string', |
734 | 734 | 'sanitize_callback' => 'sanitize_key', |
735 | 735 | 'default' => 'asc', |
@@ -739,8 +739,8 @@ discard block |
||
739 | 739 | ), |
740 | 740 | 'validate_callback' => 'rest_validate_request_arg', |
741 | 741 | ); |
742 | - $params['orderby'] = array( |
|
743 | - 'description' => __( 'Sort collection by resource attribute.', 'woocommerce' ), |
|
742 | + $params['orderby'] = array( |
|
743 | + 'description' => __('Sort collection by resource attribute.', 'woocommerce'), |
|
744 | 744 | 'type' => 'string', |
745 | 745 | 'sanitize_callback' => 'sanitize_key', |
746 | 746 | 'default' => 'name', |
@@ -756,27 +756,27 @@ discard block |
||
756 | 756 | 'validate_callback' => 'rest_validate_request_arg', |
757 | 757 | ); |
758 | 758 | $params['hide_empty'] = array( |
759 | - 'description' => __( 'Whether to hide resources not assigned to any products.', 'woocommerce' ), |
|
759 | + 'description' => __('Whether to hide resources not assigned to any products.', 'woocommerce'), |
|
760 | 760 | 'type' => 'boolean', |
761 | 761 | 'default' => false, |
762 | 762 | 'validate_callback' => 'rest_validate_request_arg', |
763 | 763 | ); |
764 | - if ( $taxonomy->hierarchical ) { |
|
764 | + if ($taxonomy->hierarchical) { |
|
765 | 765 | $params['parent'] = array( |
766 | - 'description' => __( 'Limit result set to resources assigned to a specific parent.', 'woocommerce' ), |
|
766 | + 'description' => __('Limit result set to resources assigned to a specific parent.', 'woocommerce'), |
|
767 | 767 | 'type' => 'integer', |
768 | 768 | 'sanitize_callback' => 'absint', |
769 | 769 | 'validate_callback' => 'rest_validate_request_arg', |
770 | 770 | ); |
771 | 771 | } |
772 | 772 | $params['product'] = array( |
773 | - 'description' => __( 'Limit result set to resources assigned to a specific product.', 'woocommerce' ), |
|
773 | + 'description' => __('Limit result set to resources assigned to a specific product.', 'woocommerce'), |
|
774 | 774 | 'type' => 'integer', |
775 | 775 | 'default' => null, |
776 | 776 | 'validate_callback' => 'rest_validate_request_arg', |
777 | 777 | ); |
778 | - $params['slug'] = array( |
|
779 | - 'description' => __( 'Limit result set to resources with a specific slug.', 'woocommerce' ), |
|
778 | + $params['slug'] = array( |
|
779 | + 'description' => __('Limit result set to resources with a specific slug.', 'woocommerce'), |
|
780 | 780 | 'type' => 'string', |
781 | 781 | 'validate_callback' => 'rest_validate_request_arg', |
782 | 782 | ); |
@@ -790,15 +790,15 @@ discard block |
||
790 | 790 | * @param \WP_REST_Request $request Full details about the request. |
791 | 791 | * @return int|\WP_Error |
792 | 792 | */ |
793 | - protected function get_taxonomy( $request ) { |
|
793 | + protected function get_taxonomy($request) { |
|
794 | 794 | // Check if taxonomy is defined. |
795 | 795 | // Prevents check for attribute taxonomy more than one time for each query. |
796 | - if ( '' !== $this->taxonomy ) { |
|
796 | + if ('' !== $this->taxonomy) { |
|
797 | 797 | return $this->taxonomy; |
798 | 798 | } |
799 | 799 | |
800 | - if ( ! empty( $request['attribute_id'] ) ) { |
|
801 | - $taxonomy = wc_attribute_taxonomy_name_by_id( (int) $request['attribute_id'] ); |
|
800 | + if ( ! empty($request['attribute_id'])) { |
|
801 | + $taxonomy = wc_attribute_taxonomy_name_by_id((int) $request['attribute_id']); |
|
802 | 802 | |
803 | 803 | $this->taxonomy = $taxonomy; |
804 | 804 | } |
@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | |
10 | 10 | namespace WooCommerce\RestApi\Controllers\Version4; |
11 | 11 | |
12 | -defined( 'ABSPATH' ) || exit; |
|
12 | +defined('ABSPATH') || exit; |
|
13 | 13 | |
14 | 14 | /** |
15 | 15 | * REST API Order Refunds controller class. |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | * Order refunds actions. |
42 | 42 | */ |
43 | 43 | public function __construct() { |
44 | - add_filter( "woocommerce_rest_{$this->post_type}_object_trashable", '__return_false' ); |
|
44 | + add_filter("woocommerce_rest_{$this->post_type}_object_trashable", '__return_false'); |
|
45 | 45 | } |
46 | 46 | |
47 | 47 | /** |
@@ -54,23 +54,23 @@ discard block |
||
54 | 54 | array( |
55 | 55 | 'args' => array( |
56 | 56 | 'order_id' => array( |
57 | - 'description' => __( 'The order ID.', 'woocommerce' ), |
|
57 | + 'description' => __('The order ID.', 'woocommerce'), |
|
58 | 58 | 'type' => 'integer', |
59 | 59 | ), |
60 | 60 | ), |
61 | 61 | array( |
62 | 62 | 'methods' => \WP_REST_Server::READABLE, |
63 | - 'callback' => array( $this, 'get_items' ), |
|
64 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
63 | + 'callback' => array($this, 'get_items'), |
|
64 | + 'permission_callback' => array($this, 'get_items_permissions_check'), |
|
65 | 65 | 'args' => $this->get_collection_params(), |
66 | 66 | ), |
67 | 67 | array( |
68 | 68 | 'methods' => \WP_REST_Server::CREATABLE, |
69 | - 'callback' => array( $this, 'create_item' ), |
|
70 | - 'permission_callback' => array( $this, 'create_item_permissions_check' ), |
|
71 | - 'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ), |
|
69 | + 'callback' => array($this, 'create_item'), |
|
70 | + 'permission_callback' => array($this, 'create_item_permissions_check'), |
|
71 | + 'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::CREATABLE), |
|
72 | 72 | ), |
73 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
73 | + 'schema' => array($this, 'get_public_item_schema'), |
|
74 | 74 | ), |
75 | 75 | true |
76 | 76 | ); |
@@ -81,35 +81,35 @@ discard block |
||
81 | 81 | array( |
82 | 82 | 'args' => array( |
83 | 83 | 'order_id' => array( |
84 | - 'description' => __( 'The order ID.', 'woocommerce' ), |
|
84 | + 'description' => __('The order ID.', 'woocommerce'), |
|
85 | 85 | 'type' => 'integer', |
86 | 86 | ), |
87 | 87 | 'id' => array( |
88 | - 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
|
88 | + 'description' => __('Unique identifier for the resource.', 'woocommerce'), |
|
89 | 89 | 'type' => 'integer', |
90 | 90 | ), |
91 | 91 | ), |
92 | 92 | array( |
93 | 93 | 'methods' => \WP_REST_Server::READABLE, |
94 | - 'callback' => array( $this, 'get_item' ), |
|
95 | - 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
|
94 | + 'callback' => array($this, 'get_item'), |
|
95 | + 'permission_callback' => array($this, 'get_item_permissions_check'), |
|
96 | 96 | 'args' => array( |
97 | - 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
|
97 | + 'context' => $this->get_context_param(array('default' => 'view')), |
|
98 | 98 | ), |
99 | 99 | ), |
100 | 100 | array( |
101 | 101 | 'methods' => \WP_REST_Server::DELETABLE, |
102 | - 'callback' => array( $this, 'delete_item' ), |
|
103 | - 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
|
102 | + 'callback' => array($this, 'delete_item'), |
|
103 | + 'permission_callback' => array($this, 'delete_item_permissions_check'), |
|
104 | 104 | 'args' => array( |
105 | 105 | 'force' => array( |
106 | 106 | 'default' => true, |
107 | 107 | 'type' => 'boolean', |
108 | - 'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ), |
|
108 | + 'description' => __('Required to be true, as resource does not support trashing.', 'woocommerce'), |
|
109 | 109 | ), |
110 | 110 | ), |
111 | 111 | ), |
112 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
112 | + 'schema' => array($this, 'get_public_item_schema'), |
|
113 | 113 | ), |
114 | 114 | true |
115 | 115 | ); |
@@ -122,8 +122,8 @@ discard block |
||
122 | 122 | * @param int $id Object ID. |
123 | 123 | * @return WC_Data |
124 | 124 | */ |
125 | - protected function get_object( $id ) { |
|
126 | - return wc_get_order( $id ); |
|
125 | + protected function get_object($id) { |
|
126 | + return wc_get_order($id); |
|
127 | 127 | } |
128 | 128 | |
129 | 129 | /** |
@@ -133,27 +133,27 @@ discard block |
||
133 | 133 | * @param WC_Data $object WC_Data instance. |
134 | 134 | * @return array |
135 | 135 | */ |
136 | - protected function get_formatted_item_data( $object ) { |
|
136 | + protected function get_formatted_item_data($object) { |
|
137 | 137 | $data = $object->get_data(); |
138 | - $format_decimal = array( 'amount' ); |
|
139 | - $format_date = array( 'date_created' ); |
|
140 | - $format_line_items = array( 'line_items' ); |
|
138 | + $format_decimal = array('amount'); |
|
139 | + $format_date = array('date_created'); |
|
140 | + $format_line_items = array('line_items'); |
|
141 | 141 | |
142 | 142 | // Format decimal values. |
143 | - foreach ( $format_decimal as $key ) { |
|
144 | - $data[ $key ] = wc_format_decimal( $data[ $key ], $this->request['dp'] ); |
|
143 | + foreach ($format_decimal as $key) { |
|
144 | + $data[$key] = wc_format_decimal($data[$key], $this->request['dp']); |
|
145 | 145 | } |
146 | 146 | |
147 | 147 | // Format date values. |
148 | - foreach ( $format_date as $key ) { |
|
149 | - $datetime = $data[ $key ]; |
|
150 | - $data[ $key ] = wc_rest_prepare_date_response( $datetime, false ); |
|
151 | - $data[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime ); |
|
148 | + foreach ($format_date as $key) { |
|
149 | + $datetime = $data[$key]; |
|
150 | + $data[$key] = wc_rest_prepare_date_response($datetime, false); |
|
151 | + $data[$key . '_gmt'] = wc_rest_prepare_date_response($datetime); |
|
152 | 152 | } |
153 | 153 | |
154 | 154 | // Format line items. |
155 | - foreach ( $format_line_items as $key ) { |
|
156 | - $data[ $key ] = array_values( array_map( array( $this, 'get_order_item_data' ), $data[ $key ] ) ); |
|
155 | + foreach ($format_line_items as $key) { |
|
156 | + $data[$key] = array_values(array_map(array($this, 'get_order_item_data'), $data[$key])); |
|
157 | 157 | } |
158 | 158 | |
159 | 159 | return array( |
@@ -179,28 +179,28 @@ discard block |
||
179 | 179 | * |
180 | 180 | * @return \WP_Error\WP_REST_Response |
181 | 181 | */ |
182 | - public function prepare_object_for_response( $object, $request ) { |
|
182 | + public function prepare_object_for_response($object, $request) { |
|
183 | 183 | $this->request = $request; |
184 | - $this->request['dp'] = is_null( $this->request['dp'] ) ? wc_get_price_decimals() : absint( $this->request['dp'] ); |
|
185 | - $order = wc_get_order( (int) $request['order_id'] ); |
|
184 | + $this->request['dp'] = is_null($this->request['dp']) ? wc_get_price_decimals() : absint($this->request['dp']); |
|
185 | + $order = wc_get_order((int) $request['order_id']); |
|
186 | 186 | |
187 | - if ( ! $order ) { |
|
188 | - return new \WP_Error( 'woocommerce_rest_invalid_order_id', __( 'Invalid order ID.', 'woocommerce' ), 404 ); |
|
187 | + if ( ! $order) { |
|
188 | + return new \WP_Error('woocommerce_rest_invalid_order_id', __('Invalid order ID.', 'woocommerce'), 404); |
|
189 | 189 | } |
190 | 190 | |
191 | - if ( ! $object || $object->get_parent_id() !== $order->get_id() ) { |
|
192 | - return new \WP_Error( 'woocommerce_rest_invalid_order_refund_id', __( 'Invalid order refund ID.', 'woocommerce' ), 404 ); |
|
191 | + if ( ! $object || $object->get_parent_id() !== $order->get_id()) { |
|
192 | + return new \WP_Error('woocommerce_rest_invalid_order_refund_id', __('Invalid order refund ID.', 'woocommerce'), 404); |
|
193 | 193 | } |
194 | 194 | |
195 | - $data = $this->get_formatted_item_data( $object ); |
|
196 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
197 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
198 | - $data = $this->filter_response_by_context( $data, $context ); |
|
195 | + $data = $this->get_formatted_item_data($object); |
|
196 | + $context = ! empty($request['context']) ? $request['context'] : 'view'; |
|
197 | + $data = $this->add_additional_fields_to_object($data, $request); |
|
198 | + $data = $this->filter_response_by_context($data, $context); |
|
199 | 199 | |
200 | 200 | // Wrap the data in a response object. |
201 | - $response = rest_ensure_response( $data ); |
|
201 | + $response = rest_ensure_response($data); |
|
202 | 202 | |
203 | - $response->add_links( $this->prepare_links( $object, $request ) ); |
|
203 | + $response->add_links($this->prepare_links($object, $request)); |
|
204 | 204 | |
205 | 205 | /** |
206 | 206 | * Filter the data for a response. |
@@ -212,7 +212,7 @@ discard block |
||
212 | 212 | * @param WC_Data $object Object data. |
213 | 213 | * @param \WP_REST_Request $request Request object. |
214 | 214 | */ |
215 | - return apply_filters( "woocommerce_rest_prepare_{$this->post_type}_object", $response, $object, $request ); |
|
215 | + return apply_filters("woocommerce_rest_prepare_{$this->post_type}_object", $response, $object, $request); |
|
216 | 216 | } |
217 | 217 | |
218 | 218 | /** |
@@ -223,38 +223,38 @@ discard block |
||
223 | 223 | * |
224 | 224 | * @return \WP_Error\WP_REST_Response |
225 | 225 | */ |
226 | - public function prepare_item_for_response( $post, $request ) { |
|
227 | - $order = wc_get_order( (int) $request['order_id'] ); |
|
226 | + public function prepare_item_for_response($post, $request) { |
|
227 | + $order = wc_get_order((int) $request['order_id']); |
|
228 | 228 | |
229 | - if ( ! $order ) { |
|
230 | - return new \WP_Error( 'woocommerce_rest_invalid_order_id', __( 'Invalid order ID.', 'woocommerce' ), 404 ); |
|
229 | + if ( ! $order) { |
|
230 | + return new \WP_Error('woocommerce_rest_invalid_order_id', __('Invalid order ID.', 'woocommerce'), 404); |
|
231 | 231 | } |
232 | 232 | |
233 | - $refund = wc_get_order( $post ); |
|
233 | + $refund = wc_get_order($post); |
|
234 | 234 | |
235 | - if ( ! $refund || $refund->get_parent_id() !== $order->get_id() ) { |
|
236 | - return new \WP_Error( 'woocommerce_rest_invalid_order_refund_id', __( 'Invalid order refund ID.', 'woocommerce' ), 404 ); |
|
235 | + if ( ! $refund || $refund->get_parent_id() !== $order->get_id()) { |
|
236 | + return new \WP_Error('woocommerce_rest_invalid_order_refund_id', __('Invalid order refund ID.', 'woocommerce'), 404); |
|
237 | 237 | } |
238 | 238 | |
239 | - $dp = is_null( $request['dp'] ) ? wc_get_price_decimals() : absint( $request['dp'] ); |
|
239 | + $dp = is_null($request['dp']) ? wc_get_price_decimals() : absint($request['dp']); |
|
240 | 240 | |
241 | 241 | $data = array( |
242 | 242 | 'id' => $refund->get_id(), |
243 | - 'date_created' => wc_rest_prepare_date_response( $refund->get_date_created() ), |
|
244 | - 'amount' => wc_format_decimal( $refund->get_amount(), $dp ), |
|
243 | + 'date_created' => wc_rest_prepare_date_response($refund->get_date_created()), |
|
244 | + 'amount' => wc_format_decimal($refund->get_amount(), $dp), |
|
245 | 245 | 'reason' => $refund->get_reason(), |
246 | 246 | 'line_items' => array(), |
247 | 247 | ); |
248 | 248 | |
249 | 249 | // Add line items. |
250 | - foreach ( $refund->get_items() as $item_id => $item ) { |
|
251 | - $product = $refund->get_product_from_item( $item ); |
|
250 | + foreach ($refund->get_items() as $item_id => $item) { |
|
251 | + $product = $refund->get_product_from_item($item); |
|
252 | 252 | $product_id = 0; |
253 | 253 | $variation_id = 0; |
254 | 254 | $product_sku = null; |
255 | 255 | |
256 | 256 | // Check if the product exists. |
257 | - if ( is_object( $product ) ) { |
|
257 | + if (is_object($product)) { |
|
258 | 258 | $product_id = $item->get_product_id(); |
259 | 259 | $variation_id = $item->get_variation_id(); |
260 | 260 | $product_sku = $product->get_sku(); |
@@ -264,11 +264,11 @@ discard block |
||
264 | 264 | |
265 | 265 | $hideprefix = 'true' === $request['all_item_meta'] ? null : '_'; |
266 | 266 | |
267 | - foreach ( $item->get_formatted_meta_data( $hideprefix, true ) as $meta_key => $formatted_meta ) { |
|
267 | + foreach ($item->get_formatted_meta_data($hideprefix, true) as $meta_key => $formatted_meta) { |
|
268 | 268 | $item_meta[] = array( |
269 | 269 | 'key' => $formatted_meta->key, |
270 | 270 | 'label' => $formatted_meta->display_key, |
271 | - 'value' => wc_clean( $formatted_meta->display_value ), |
|
271 | + 'value' => wc_clean($formatted_meta->display_value), |
|
272 | 272 | ); |
273 | 273 | } |
274 | 274 | |
@@ -278,47 +278,47 @@ discard block |
||
278 | 278 | 'sku' => $product_sku, |
279 | 279 | 'product_id' => (int) $product_id, |
280 | 280 | 'variation_id' => (int) $variation_id, |
281 | - 'quantity' => wc_stock_amount( $item['qty'] ), |
|
282 | - 'tax_class' => ! empty( $item['tax_class'] ) ? $item['tax_class'] : '', |
|
283 | - 'price' => wc_format_decimal( $refund->get_item_total( $item, false, false ), $dp ), |
|
284 | - 'subtotal' => wc_format_decimal( $refund->get_line_subtotal( $item, false, false ), $dp ), |
|
285 | - 'subtotal_tax' => wc_format_decimal( $item['line_subtotal_tax'], $dp ), |
|
286 | - 'total' => wc_format_decimal( $refund->get_line_total( $item, false, false ), $dp ), |
|
287 | - 'total_tax' => wc_format_decimal( $item['line_tax'], $dp ), |
|
281 | + 'quantity' => wc_stock_amount($item['qty']), |
|
282 | + 'tax_class' => ! empty($item['tax_class']) ? $item['tax_class'] : '', |
|
283 | + 'price' => wc_format_decimal($refund->get_item_total($item, false, false), $dp), |
|
284 | + 'subtotal' => wc_format_decimal($refund->get_line_subtotal($item, false, false), $dp), |
|
285 | + 'subtotal_tax' => wc_format_decimal($item['line_subtotal_tax'], $dp), |
|
286 | + 'total' => wc_format_decimal($refund->get_line_total($item, false, false), $dp), |
|
287 | + 'total_tax' => wc_format_decimal($item['line_tax'], $dp), |
|
288 | 288 | 'taxes' => array(), |
289 | 289 | 'meta' => $item_meta, |
290 | 290 | ); |
291 | 291 | |
292 | - $item_line_taxes = maybe_unserialize( $item['line_tax_data'] ); |
|
293 | - if ( isset( $item_line_taxes['total'] ) ) { |
|
292 | + $item_line_taxes = maybe_unserialize($item['line_tax_data']); |
|
293 | + if (isset($item_line_taxes['total'])) { |
|
294 | 294 | $line_tax = array(); |
295 | 295 | |
296 | - foreach ( $item_line_taxes['total'] as $tax_rate_id => $tax ) { |
|
297 | - $line_tax[ $tax_rate_id ] = array( |
|
296 | + foreach ($item_line_taxes['total'] as $tax_rate_id => $tax) { |
|
297 | + $line_tax[$tax_rate_id] = array( |
|
298 | 298 | 'id' => $tax_rate_id, |
299 | 299 | 'total' => $tax, |
300 | 300 | 'subtotal' => '', |
301 | 301 | ); |
302 | 302 | } |
303 | 303 | |
304 | - foreach ( $item_line_taxes['subtotal'] as $tax_rate_id => $tax ) { |
|
305 | - $line_tax[ $tax_rate_id ]['subtotal'] = $tax; |
|
304 | + foreach ($item_line_taxes['subtotal'] as $tax_rate_id => $tax) { |
|
305 | + $line_tax[$tax_rate_id]['subtotal'] = $tax; |
|
306 | 306 | } |
307 | 307 | |
308 | - $line_item['taxes'] = array_values( $line_tax ); |
|
308 | + $line_item['taxes'] = array_values($line_tax); |
|
309 | 309 | } |
310 | 310 | |
311 | 311 | $data['line_items'][] = $line_item; |
312 | 312 | } |
313 | 313 | |
314 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
315 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
316 | - $data = $this->filter_response_by_context( $data, $context ); |
|
314 | + $context = ! empty($request['context']) ? $request['context'] : 'view'; |
|
315 | + $data = $this->add_additional_fields_to_object($data, $request); |
|
316 | + $data = $this->filter_response_by_context($data, $context); |
|
317 | 317 | |
318 | 318 | // Wrap the data in a response object. |
319 | - $response = rest_ensure_response( $data ); |
|
319 | + $response = rest_ensure_response($data); |
|
320 | 320 | |
321 | - $response->add_links( $this->prepare_links( $refund, $request ) ); |
|
321 | + $response->add_links($this->prepare_links($refund, $request)); |
|
322 | 322 | |
323 | 323 | /** |
324 | 324 | * Filter the data for a response. |
@@ -330,7 +330,7 @@ discard block |
||
330 | 330 | * @param WP_Post $post Post object. |
331 | 331 | * @param \WP_REST_Request $request Request object. |
332 | 332 | */ |
333 | - return apply_filters( "woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request ); |
|
333 | + return apply_filters("woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request); |
|
334 | 334 | } |
335 | 335 | |
336 | 336 | /** |
@@ -340,17 +340,17 @@ discard block |
||
340 | 340 | * @param \WP_REST_Request $request Request object. |
341 | 341 | * @return array Links for the given post. |
342 | 342 | */ |
343 | - protected function prepare_links( $object, $request ) { |
|
344 | - $base = str_replace( '(?P<order_id>[\d]+)', $object->get_parent_id(), $this->rest_base ); |
|
343 | + protected function prepare_links($object, $request) { |
|
344 | + $base = str_replace('(?P<order_id>[\d]+)', $object->get_parent_id(), $this->rest_base); |
|
345 | 345 | $links = array( |
346 | 346 | 'self' => array( |
347 | - 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $object->get_id() ) ), |
|
347 | + 'href' => rest_url(sprintf('/%s/%s/%d', $this->namespace, $base, $object->get_id())), |
|
348 | 348 | ), |
349 | 349 | 'collection' => array( |
350 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ), |
|
350 | + 'href' => rest_url(sprintf('/%s/%s', $this->namespace, $base)), |
|
351 | 351 | ), |
352 | 352 | 'up' => array( |
353 | - 'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $object->get_parent_id() ) ), |
|
353 | + 'href' => rest_url(sprintf('/%s/orders/%d', $this->namespace, $object->get_parent_id())), |
|
354 | 354 | ), |
355 | 355 | ); |
356 | 356 | |
@@ -364,11 +364,11 @@ discard block |
||
364 | 364 | * @param \WP_REST_Request $request Full details about the request. |
365 | 365 | * @return array |
366 | 366 | */ |
367 | - protected function prepare_objects_query( $request ) { |
|
368 | - $args = parent::prepare_objects_query( $request ); |
|
367 | + protected function prepare_objects_query($request) { |
|
368 | + $args = parent::prepare_objects_query($request); |
|
369 | 369 | |
370 | - $args['post_status'] = array_keys( wc_get_order_statuses() ); |
|
371 | - $args['post_parent__in'] = array( absint( $request['order_id'] ) ); |
|
370 | + $args['post_status'] = array_keys(wc_get_order_statuses()); |
|
371 | + $args['post_parent__in'] = array(absint($request['order_id'])); |
|
372 | 372 | |
373 | 373 | return $args; |
374 | 374 | } |
@@ -379,20 +379,20 @@ discard block |
||
379 | 379 | * @param \WP_REST_Request $request Full details about the request. |
380 | 380 | * @return \WP_Error\WP_REST_Response |
381 | 381 | */ |
382 | - public function create_item( $request ) { |
|
383 | - if ( ! empty( $request['id'] ) ) { |
|
382 | + public function create_item($request) { |
|
383 | + if ( ! empty($request['id'])) { |
|
384 | 384 | /* translators: %s: post type */ |
385 | - return new \WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) ); |
|
385 | + return new \WP_Error("woocommerce_rest_{$this->post_type}_exists", sprintf(__('Cannot create existing %s.', 'woocommerce'), $this->post_type), array('status' => 400)); |
|
386 | 386 | } |
387 | 387 | |
388 | - $order_data = get_post( (int) $request['order_id'] ); |
|
388 | + $order_data = get_post((int) $request['order_id']); |
|
389 | 389 | |
390 | - if ( empty( $order_data ) ) { |
|
391 | - return new \WP_Error( 'woocommerce_rest_invalid_order', __( 'Order is invalid', 'woocommerce' ), 400 ); |
|
390 | + if (empty($order_data)) { |
|
391 | + return new \WP_Error('woocommerce_rest_invalid_order', __('Order is invalid', 'woocommerce'), 400); |
|
392 | 392 | } |
393 | 393 | |
394 | - if ( 0 > $request['amount'] ) { |
|
395 | - return new \WP_Error( 'woocommerce_rest_invalid_order_refund', __( 'Refund amount must be greater than zero.', 'woocommerce' ), 400 ); |
|
394 | + if (0 > $request['amount']) { |
|
395 | + return new \WP_Error('woocommerce_rest_invalid_order_refund', __('Refund amount must be greater than zero.', 'woocommerce'), 400); |
|
396 | 396 | } |
397 | 397 | |
398 | 398 | // Create the refund. |
@@ -400,22 +400,22 @@ discard block |
||
400 | 400 | array( |
401 | 401 | 'order_id' => $order_data->ID, |
402 | 402 | 'amount' => $request['amount'], |
403 | - 'reason' => empty( $request['reason'] ) ? null : $request['reason'], |
|
404 | - 'refund_payment' => is_bool( $request['api_refund'] ) ? $request['api_refund'] : true, |
|
403 | + 'reason' => empty($request['reason']) ? null : $request['reason'], |
|
404 | + 'refund_payment' => is_bool($request['api_refund']) ? $request['api_refund'] : true, |
|
405 | 405 | 'restock_items' => true, |
406 | 406 | ) |
407 | 407 | ); |
408 | 408 | |
409 | - if ( is_wp_error( $refund ) ) { |
|
410 | - return new \WP_Error( 'woocommerce_rest_cannot_create_order_refund', $refund->get_error_message(), 500 ); |
|
409 | + if (is_wp_error($refund)) { |
|
410 | + return new \WP_Error('woocommerce_rest_cannot_create_order_refund', $refund->get_error_message(), 500); |
|
411 | 411 | } |
412 | 412 | |
413 | - if ( ! $refund ) { |
|
414 | - return new \WP_Error( 'woocommerce_rest_cannot_create_order_refund', __( 'Cannot create order refund, please try again.', 'woocommerce' ), 500 ); |
|
413 | + if ( ! $refund) { |
|
414 | + return new \WP_Error('woocommerce_rest_cannot_create_order_refund', __('Cannot create order refund, please try again.', 'woocommerce'), 500); |
|
415 | 415 | } |
416 | 416 | |
417 | - $post = get_post( $refund->get_id() ); |
|
418 | - $this->update_additional_fields_for_object( $post, $request ); |
|
417 | + $post = get_post($refund->get_id()); |
|
418 | + $this->update_additional_fields_for_object($post, $request); |
|
419 | 419 | |
420 | 420 | /** |
421 | 421 | * Fires after a single item is created or updated via the REST API. |
@@ -424,13 +424,13 @@ discard block |
||
424 | 424 | * @param \WP_REST_Request $request Request object. |
425 | 425 | * @param boolean $creating True when creating item, false when updating. |
426 | 426 | */ |
427 | - do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, true ); |
|
427 | + do_action("woocommerce_rest_insert_{$this->post_type}", $post, $request, true); |
|
428 | 428 | |
429 | - $request->set_param( 'context', 'edit' ); |
|
430 | - $response = $this->prepare_item_for_response( $post, $request ); |
|
431 | - $response = rest_ensure_response( $response ); |
|
432 | - $response->set_status( 201 ); |
|
433 | - $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post->ID ) ) ); |
|
429 | + $request->set_param('context', 'edit'); |
|
430 | + $response = $this->prepare_item_for_response($post, $request); |
|
431 | + $response = rest_ensure_response($response); |
|
432 | + $response->set_status(201); |
|
433 | + $response->header('Location', rest_url(sprintf('/%s/%s/%d', $this->namespace, $this->rest_base, $post->ID))); |
|
434 | 434 | |
435 | 435 | return $response; |
436 | 436 | } |
@@ -443,15 +443,15 @@ discard block |
||
443 | 443 | * @param bool $creating If is creating a new object. |
444 | 444 | * @return \WP_Error|WC_Data The prepared item, or \WP_Error object on failure. |
445 | 445 | */ |
446 | - protected function prepare_object_for_database( $request, $creating = false ) { |
|
447 | - $order = wc_get_order( (int) $request['order_id'] ); |
|
446 | + protected function prepare_object_for_database($request, $creating = false) { |
|
447 | + $order = wc_get_order((int) $request['order_id']); |
|
448 | 448 | |
449 | - if ( ! $order ) { |
|
450 | - return new \WP_Error( 'woocommerce_rest_invalid_order_id', __( 'Invalid order ID.', 'woocommerce' ), 404 ); |
|
449 | + if ( ! $order) { |
|
450 | + return new \WP_Error('woocommerce_rest_invalid_order_id', __('Invalid order ID.', 'woocommerce'), 404); |
|
451 | 451 | } |
452 | 452 | |
453 | - if ( 0 > $request['amount'] ) { |
|
454 | - return new \WP_Error( 'woocommerce_rest_invalid_order_refund', __( 'Refund amount must be greater than zero.', 'woocommerce' ), 400 ); |
|
453 | + if (0 > $request['amount']) { |
|
454 | + return new \WP_Error('woocommerce_rest_invalid_order_refund', __('Refund amount must be greater than zero.', 'woocommerce'), 400); |
|
455 | 455 | } |
456 | 456 | |
457 | 457 | // Create the refund. |
@@ -459,24 +459,24 @@ discard block |
||
459 | 459 | array( |
460 | 460 | 'order_id' => $order->get_id(), |
461 | 461 | 'amount' => $request['amount'], |
462 | - 'reason' => empty( $request['reason'] ) ? null : $request['reason'], |
|
463 | - 'line_items' => empty( $request['line_items'] ) ? array() : $request['line_items'], |
|
464 | - 'refund_payment' => is_bool( $request['api_refund'] ) ? $request['api_refund'] : true, |
|
462 | + 'reason' => empty($request['reason']) ? null : $request['reason'], |
|
463 | + 'line_items' => empty($request['line_items']) ? array() : $request['line_items'], |
|
464 | + 'refund_payment' => is_bool($request['api_refund']) ? $request['api_refund'] : true, |
|
465 | 465 | 'restock_items' => true, |
466 | 466 | ) |
467 | 467 | ); |
468 | 468 | |
469 | - if ( is_wp_error( $refund ) ) { |
|
470 | - return new \WP_Error( 'woocommerce_rest_cannot_create_order_refund', $refund->get_error_message(), 500 ); |
|
469 | + if (is_wp_error($refund)) { |
|
470 | + return new \WP_Error('woocommerce_rest_cannot_create_order_refund', $refund->get_error_message(), 500); |
|
471 | 471 | } |
472 | 472 | |
473 | - if ( ! $refund ) { |
|
474 | - return new \WP_Error( 'woocommerce_rest_cannot_create_order_refund', __( 'Cannot create order refund, please try again.', 'woocommerce' ), 500 ); |
|
473 | + if ( ! $refund) { |
|
474 | + return new \WP_Error('woocommerce_rest_cannot_create_order_refund', __('Cannot create order refund, please try again.', 'woocommerce'), 500); |
|
475 | 475 | } |
476 | 476 | |
477 | - if ( ! empty( $request['meta_data'] ) && is_array( $request['meta_data'] ) ) { |
|
478 | - foreach ( $request['meta_data'] as $meta ) { |
|
479 | - $refund->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' ); |
|
477 | + if ( ! empty($request['meta_data']) && is_array($request['meta_data'])) { |
|
478 | + foreach ($request['meta_data'] as $meta) { |
|
479 | + $refund->update_meta_data($meta['key'], $meta['value'], isset($meta['id']) ? $meta['id'] : ''); |
|
480 | 480 | } |
481 | 481 | $refund->save_meta_data(); |
482 | 482 | } |
@@ -491,7 +491,7 @@ discard block |
||
491 | 491 | * @param \WP_REST_Request $request Request object. |
492 | 492 | * @param bool $creating If is creating a new object. |
493 | 493 | */ |
494 | - return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $refund, $request, $creating ); |
|
494 | + return apply_filters("woocommerce_rest_pre_insert_{$this->post_type}_object", $refund, $request, $creating); |
|
495 | 495 | } |
496 | 496 | |
497 | 497 | /** |
@@ -502,19 +502,19 @@ discard block |
||
502 | 502 | * @param bool $creating If is creating a new object. |
503 | 503 | * @return \WC_Data|\WP_Error |
504 | 504 | */ |
505 | - protected function save_object( $request, $creating = false ) { |
|
505 | + protected function save_object($request, $creating = false) { |
|
506 | 506 | try { |
507 | - $object = $this->prepare_object_for_database( $request, $creating ); |
|
507 | + $object = $this->prepare_object_for_database($request, $creating); |
|
508 | 508 | |
509 | - if ( is_wp_error( $object ) ) { |
|
509 | + if (is_wp_error($object)) { |
|
510 | 510 | return $object; |
511 | 511 | } |
512 | 512 | |
513 | - return $this->get_object( $object->get_id() ); |
|
514 | - } catch ( \WC_Data_Exception $e ) { |
|
515 | - return new \WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() ); |
|
516 | - } catch ( \WC_REST_Exception $e ) { |
|
517 | - return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); |
|
513 | + return $this->get_object($object->get_id()); |
|
514 | + } catch (\WC_Data_Exception $e) { |
|
515 | + return new \WP_Error($e->getErrorCode(), $e->getMessage(), $e->getErrorData()); |
|
516 | + } catch (\WC_REST_Exception $e) { |
|
517 | + return new \WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode())); |
|
518 | 518 | } |
519 | 519 | } |
520 | 520 | |
@@ -530,221 +530,221 @@ discard block |
||
530 | 530 | 'type' => 'object', |
531 | 531 | 'properties' => array( |
532 | 532 | 'id' => array( |
533 | - 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
|
533 | + 'description' => __('Unique identifier for the resource.', 'woocommerce'), |
|
534 | 534 | 'type' => 'integer', |
535 | - 'context' => array( 'view', 'edit' ), |
|
535 | + 'context' => array('view', 'edit'), |
|
536 | 536 | 'readonly' => true, |
537 | 537 | ), |
538 | 538 | 'date_created' => array( |
539 | - 'description' => __( "The date the order refund was created, in the site's timezone.", 'woocommerce' ), |
|
539 | + 'description' => __("The date the order refund was created, in the site's timezone.", 'woocommerce'), |
|
540 | 540 | 'type' => 'date-time', |
541 | - 'context' => array( 'view', 'edit' ), |
|
541 | + 'context' => array('view', 'edit'), |
|
542 | 542 | 'readonly' => true, |
543 | 543 | ), |
544 | 544 | 'date_created_gmt' => array( |
545 | - 'description' => __( 'The date the order refund was created, as GMT.', 'woocommerce' ), |
|
545 | + 'description' => __('The date the order refund was created, as GMT.', 'woocommerce'), |
|
546 | 546 | 'type' => 'date-time', |
547 | - 'context' => array( 'view', 'edit' ), |
|
547 | + 'context' => array('view', 'edit'), |
|
548 | 548 | 'readonly' => true, |
549 | 549 | ), |
550 | 550 | 'amount' => array( |
551 | - 'description' => __( 'Refund amount.', 'woocommerce' ), |
|
551 | + 'description' => __('Refund amount.', 'woocommerce'), |
|
552 | 552 | 'type' => 'string', |
553 | - 'context' => array( 'view', 'edit' ), |
|
553 | + 'context' => array('view', 'edit'), |
|
554 | 554 | ), |
555 | 555 | 'reason' => array( |
556 | - 'description' => __( 'Reason for refund.', 'woocommerce' ), |
|
556 | + 'description' => __('Reason for refund.', 'woocommerce'), |
|
557 | 557 | 'type' => 'string', |
558 | - 'context' => array( 'view', 'edit' ), |
|
558 | + 'context' => array('view', 'edit'), |
|
559 | 559 | ), |
560 | 560 | 'refunded_by' => array( |
561 | - 'description' => __( 'User ID of user who created the refund.', 'woocommerce' ), |
|
561 | + 'description' => __('User ID of user who created the refund.', 'woocommerce'), |
|
562 | 562 | 'type' => 'integer', |
563 | - 'context' => array( 'view', 'edit' ), |
|
563 | + 'context' => array('view', 'edit'), |
|
564 | 564 | ), |
565 | 565 | 'refunded_payment' => array( |
566 | - 'description' => __( 'If the payment was refunded via the API.', 'woocommerce' ), |
|
566 | + 'description' => __('If the payment was refunded via the API.', 'woocommerce'), |
|
567 | 567 | 'type' => 'boolean', |
568 | - 'context' => array( 'view' ), |
|
568 | + 'context' => array('view'), |
|
569 | 569 | 'readonly' => true, |
570 | 570 | ), |
571 | 571 | 'meta_data' => array( |
572 | - 'description' => __( 'Meta data.', 'woocommerce' ), |
|
572 | + 'description' => __('Meta data.', 'woocommerce'), |
|
573 | 573 | 'type' => 'array', |
574 | - 'context' => array( 'view', 'edit' ), |
|
574 | + 'context' => array('view', 'edit'), |
|
575 | 575 | 'items' => array( |
576 | 576 | 'type' => 'object', |
577 | 577 | 'properties' => array( |
578 | 578 | 'id' => array( |
579 | - 'description' => __( 'Meta ID.', 'woocommerce' ), |
|
579 | + 'description' => __('Meta ID.', 'woocommerce'), |
|
580 | 580 | 'type' => 'integer', |
581 | - 'context' => array( 'view', 'edit' ), |
|
581 | + 'context' => array('view', 'edit'), |
|
582 | 582 | 'readonly' => true, |
583 | 583 | ), |
584 | 584 | 'key' => array( |
585 | - 'description' => __( 'Meta key.', 'woocommerce' ), |
|
585 | + 'description' => __('Meta key.', 'woocommerce'), |
|
586 | 586 | 'type' => 'string', |
587 | - 'context' => array( 'view', 'edit' ), |
|
587 | + 'context' => array('view', 'edit'), |
|
588 | 588 | ), |
589 | 589 | 'value' => array( |
590 | - 'description' => __( 'Meta value.', 'woocommerce' ), |
|
590 | + 'description' => __('Meta value.', 'woocommerce'), |
|
591 | 591 | 'type' => 'mixed', |
592 | - 'context' => array( 'view', 'edit' ), |
|
592 | + 'context' => array('view', 'edit'), |
|
593 | 593 | ), |
594 | 594 | ), |
595 | 595 | ), |
596 | 596 | ), |
597 | 597 | 'line_items' => array( |
598 | - 'description' => __( 'Line items data.', 'woocommerce' ), |
|
598 | + 'description' => __('Line items data.', 'woocommerce'), |
|
599 | 599 | 'type' => 'array', |
600 | - 'context' => array( 'view', 'edit' ), |
|
600 | + 'context' => array('view', 'edit'), |
|
601 | 601 | 'readonly' => true, |
602 | 602 | 'items' => array( |
603 | 603 | 'type' => 'object', |
604 | 604 | 'properties' => array( |
605 | 605 | 'id' => array( |
606 | - 'description' => __( 'Item ID.', 'woocommerce' ), |
|
606 | + 'description' => __('Item ID.', 'woocommerce'), |
|
607 | 607 | 'type' => 'integer', |
608 | - 'context' => array( 'view', 'edit' ), |
|
608 | + 'context' => array('view', 'edit'), |
|
609 | 609 | 'readonly' => true, |
610 | 610 | ), |
611 | 611 | 'name' => array( |
612 | - 'description' => __( 'Product name.', 'woocommerce' ), |
|
612 | + 'description' => __('Product name.', 'woocommerce'), |
|
613 | 613 | 'type' => 'mixed', |
614 | - 'context' => array( 'view', 'edit' ), |
|
614 | + 'context' => array('view', 'edit'), |
|
615 | 615 | 'readonly' => true, |
616 | 616 | ), |
617 | 617 | 'product_id' => array( |
618 | - 'description' => __( 'Product ID.', 'woocommerce' ), |
|
618 | + 'description' => __('Product ID.', 'woocommerce'), |
|
619 | 619 | 'type' => 'mixed', |
620 | - 'context' => array( 'view', 'edit' ), |
|
620 | + 'context' => array('view', 'edit'), |
|
621 | 621 | 'readonly' => true, |
622 | 622 | ), |
623 | 623 | 'variation_id' => array( |
624 | - 'description' => __( 'Variation ID, if applicable.', 'woocommerce' ), |
|
624 | + 'description' => __('Variation ID, if applicable.', 'woocommerce'), |
|
625 | 625 | 'type' => 'integer', |
626 | - 'context' => array( 'view', 'edit' ), |
|
626 | + 'context' => array('view', 'edit'), |
|
627 | 627 | 'readonly' => true, |
628 | 628 | ), |
629 | 629 | 'quantity' => array( |
630 | - 'description' => __( 'Quantity ordered.', 'woocommerce' ), |
|
630 | + 'description' => __('Quantity ordered.', 'woocommerce'), |
|
631 | 631 | 'type' => 'integer', |
632 | - 'context' => array( 'view', 'edit' ), |
|
632 | + 'context' => array('view', 'edit'), |
|
633 | 633 | 'readonly' => true, |
634 | 634 | ), |
635 | 635 | 'tax_class' => array( |
636 | - 'description' => __( 'Tax class of product.', 'woocommerce' ), |
|
636 | + 'description' => __('Tax class of product.', 'woocommerce'), |
|
637 | 637 | 'type' => 'string', |
638 | - 'context' => array( 'view', 'edit' ), |
|
638 | + 'context' => array('view', 'edit'), |
|
639 | 639 | 'readonly' => true, |
640 | 640 | ), |
641 | 641 | 'subtotal' => array( |
642 | - 'description' => __( 'Line subtotal (before discounts).', 'woocommerce' ), |
|
642 | + 'description' => __('Line subtotal (before discounts).', 'woocommerce'), |
|
643 | 643 | 'type' => 'string', |
644 | - 'context' => array( 'view', 'edit' ), |
|
644 | + 'context' => array('view', 'edit'), |
|
645 | 645 | 'readonly' => true, |
646 | 646 | ), |
647 | 647 | 'subtotal_tax' => array( |
648 | - 'description' => __( 'Line subtotal tax (before discounts).', 'woocommerce' ), |
|
648 | + 'description' => __('Line subtotal tax (before discounts).', 'woocommerce'), |
|
649 | 649 | 'type' => 'string', |
650 | - 'context' => array( 'view', 'edit' ), |
|
650 | + 'context' => array('view', 'edit'), |
|
651 | 651 | 'readonly' => true, |
652 | 652 | ), |
653 | 653 | 'total' => array( |
654 | - 'description' => __( 'Line total (after discounts).', 'woocommerce' ), |
|
654 | + 'description' => __('Line total (after discounts).', 'woocommerce'), |
|
655 | 655 | 'type' => 'string', |
656 | - 'context' => array( 'view', 'edit' ), |
|
656 | + 'context' => array('view', 'edit'), |
|
657 | 657 | 'readonly' => true, |
658 | 658 | ), |
659 | 659 | 'total_tax' => array( |
660 | - 'description' => __( 'Line total tax (after discounts).', 'woocommerce' ), |
|
660 | + 'description' => __('Line total tax (after discounts).', 'woocommerce'), |
|
661 | 661 | 'type' => 'string', |
662 | - 'context' => array( 'view', 'edit' ), |
|
662 | + 'context' => array('view', 'edit'), |
|
663 | 663 | 'readonly' => true, |
664 | 664 | ), |
665 | 665 | 'taxes' => array( |
666 | - 'description' => __( 'Line taxes.', 'woocommerce' ), |
|
666 | + 'description' => __('Line taxes.', 'woocommerce'), |
|
667 | 667 | 'type' => 'array', |
668 | - 'context' => array( 'view', 'edit' ), |
|
668 | + 'context' => array('view', 'edit'), |
|
669 | 669 | 'readonly' => true, |
670 | 670 | 'items' => array( |
671 | 671 | 'type' => 'object', |
672 | 672 | 'properties' => array( |
673 | 673 | 'id' => array( |
674 | - 'description' => __( 'Tax rate ID.', 'woocommerce' ), |
|
674 | + 'description' => __('Tax rate ID.', 'woocommerce'), |
|
675 | 675 | 'type' => 'integer', |
676 | - 'context' => array( 'view', 'edit' ), |
|
676 | + 'context' => array('view', 'edit'), |
|
677 | 677 | 'readonly' => true, |
678 | 678 | ), |
679 | 679 | 'total' => array( |
680 | - 'description' => __( 'Tax total.', 'woocommerce' ), |
|
680 | + 'description' => __('Tax total.', 'woocommerce'), |
|
681 | 681 | 'type' => 'string', |
682 | - 'context' => array( 'view', 'edit' ), |
|
682 | + 'context' => array('view', 'edit'), |
|
683 | 683 | 'readonly' => true, |
684 | 684 | ), |
685 | 685 | 'subtotal' => array( |
686 | - 'description' => __( 'Tax subtotal.', 'woocommerce' ), |
|
686 | + 'description' => __('Tax subtotal.', 'woocommerce'), |
|
687 | 687 | 'type' => 'string', |
688 | - 'context' => array( 'view', 'edit' ), |
|
688 | + 'context' => array('view', 'edit'), |
|
689 | 689 | 'readonly' => true, |
690 | 690 | ), |
691 | 691 | ), |
692 | 692 | ), |
693 | 693 | ), |
694 | 694 | 'meta_data' => array( |
695 | - 'description' => __( 'Meta data.', 'woocommerce' ), |
|
695 | + 'description' => __('Meta data.', 'woocommerce'), |
|
696 | 696 | 'type' => 'array', |
697 | - 'context' => array( 'view', 'edit' ), |
|
697 | + 'context' => array('view', 'edit'), |
|
698 | 698 | 'readonly' => true, |
699 | 699 | 'items' => array( |
700 | 700 | 'type' => 'object', |
701 | 701 | 'properties' => array( |
702 | 702 | 'id' => array( |
703 | - 'description' => __( 'Meta ID.', 'woocommerce' ), |
|
703 | + 'description' => __('Meta ID.', 'woocommerce'), |
|
704 | 704 | 'type' => 'integer', |
705 | - 'context' => array( 'view', 'edit' ), |
|
705 | + 'context' => array('view', 'edit'), |
|
706 | 706 | 'readonly' => true, |
707 | 707 | ), |
708 | 708 | 'key' => array( |
709 | - 'description' => __( 'Meta key.', 'woocommerce' ), |
|
709 | + 'description' => __('Meta key.', 'woocommerce'), |
|
710 | 710 | 'type' => 'string', |
711 | - 'context' => array( 'view', 'edit' ), |
|
711 | + 'context' => array('view', 'edit'), |
|
712 | 712 | 'readonly' => true, |
713 | 713 | ), |
714 | 714 | 'value' => array( |
715 | - 'description' => __( 'Meta value.', 'woocommerce' ), |
|
715 | + 'description' => __('Meta value.', 'woocommerce'), |
|
716 | 716 | 'type' => 'mixed', |
717 | - 'context' => array( 'view', 'edit' ), |
|
717 | + 'context' => array('view', 'edit'), |
|
718 | 718 | 'readonly' => true, |
719 | 719 | ), |
720 | 720 | ), |
721 | 721 | ), |
722 | 722 | ), |
723 | 723 | 'sku' => array( |
724 | - 'description' => __( 'Product SKU.', 'woocommerce' ), |
|
724 | + 'description' => __('Product SKU.', 'woocommerce'), |
|
725 | 725 | 'type' => 'string', |
726 | - 'context' => array( 'view', 'edit' ), |
|
726 | + 'context' => array('view', 'edit'), |
|
727 | 727 | 'readonly' => true, |
728 | 728 | ), |
729 | 729 | 'price' => array( |
730 | - 'description' => __( 'Product price.', 'woocommerce' ), |
|
730 | + 'description' => __('Product price.', 'woocommerce'), |
|
731 | 731 | 'type' => 'number', |
732 | - 'context' => array( 'view', 'edit' ), |
|
732 | + 'context' => array('view', 'edit'), |
|
733 | 733 | 'readonly' => true, |
734 | 734 | ), |
735 | 735 | ), |
736 | 736 | ), |
737 | 737 | ), |
738 | 738 | 'api_refund' => array( |
739 | - 'description' => __( 'When true, the payment gateway API is used to generate the refund.', 'woocommerce' ), |
|
739 | + 'description' => __('When true, the payment gateway API is used to generate the refund.', 'woocommerce'), |
|
740 | 740 | 'type' => 'boolean', |
741 | - 'context' => array( 'edit' ), |
|
741 | + 'context' => array('edit'), |
|
742 | 742 | 'default' => true, |
743 | 743 | ), |
744 | 744 | ), |
745 | 745 | ); |
746 | 746 | |
747 | - return $this->add_additional_fields_schema( $schema ); |
|
747 | + return $this->add_additional_fields_schema($schema); |
|
748 | 748 | } |
749 | 749 | |
750 | 750 | /** |
@@ -757,13 +757,13 @@ discard block |
||
757 | 757 | |
758 | 758 | $params['dp'] = array( |
759 | 759 | 'default' => wc_get_price_decimals(), |
760 | - 'description' => __( 'Number of decimal points to use in each resource.', 'woocommerce' ), |
|
760 | + 'description' => __('Number of decimal points to use in each resource.', 'woocommerce'), |
|
761 | 761 | 'type' => 'integer', |
762 | 762 | 'sanitize_callback' => 'absint', |
763 | 763 | 'validate_callback' => 'rest_validate_request_arg', |
764 | 764 | ); |
765 | 765 | |
766 | - unset( $params['status'], $params['customer'], $params['product'] ); |
|
766 | + unset($params['status'], $params['customer'], $params['product']); |
|
767 | 767 | |
768 | 768 | return $params; |
769 | 769 | } |
@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | |
10 | 10 | namespace WooCommerce\RestApi\Controllers\Version4; |
11 | 11 | |
12 | -defined( 'ABSPATH' ) || exit; |
|
12 | +defined('ABSPATH') || exit; |
|
13 | 13 | |
14 | 14 | /** |
15 | 15 | * REST API Product Reviews controller class. |
@@ -33,41 +33,41 @@ discard block |
||
33 | 33 | array( |
34 | 34 | array( |
35 | 35 | 'methods' => \WP_REST_Server::READABLE, |
36 | - 'callback' => array( $this, 'get_items' ), |
|
37 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
36 | + 'callback' => array($this, 'get_items'), |
|
37 | + 'permission_callback' => array($this, 'get_items_permissions_check'), |
|
38 | 38 | 'args' => $this->get_collection_params(), |
39 | 39 | ), |
40 | 40 | array( |
41 | 41 | 'methods' => \WP_REST_Server::CREATABLE, |
42 | - 'callback' => array( $this, 'create_item' ), |
|
43 | - 'permission_callback' => array( $this, 'create_item_permissions_check' ), |
|
42 | + 'callback' => array($this, 'create_item'), |
|
43 | + 'permission_callback' => array($this, 'create_item_permissions_check'), |
|
44 | 44 | 'args' => array_merge( |
45 | - $this->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ), |
|
45 | + $this->get_endpoint_args_for_item_schema(\WP_REST_Server::CREATABLE), |
|
46 | 46 | array( |
47 | 47 | 'product_id' => array( |
48 | 48 | 'required' => true, |
49 | - 'description' => __( 'Unique identifier for the product.', 'woocommerce' ), |
|
49 | + 'description' => __('Unique identifier for the product.', 'woocommerce'), |
|
50 | 50 | 'type' => 'integer', |
51 | 51 | ), |
52 | 52 | 'review' => array( |
53 | 53 | 'required' => true, |
54 | 54 | 'type' => 'string', |
55 | - 'description' => __( 'Review content.', 'woocommerce' ), |
|
55 | + 'description' => __('Review content.', 'woocommerce'), |
|
56 | 56 | ), |
57 | 57 | 'reviewer' => array( |
58 | 58 | 'required' => true, |
59 | 59 | 'type' => 'string', |
60 | - 'description' => __( 'Name of the reviewer.', 'woocommerce' ), |
|
60 | + 'description' => __('Name of the reviewer.', 'woocommerce'), |
|
61 | 61 | ), |
62 | 62 | 'reviewer_email' => array( |
63 | 63 | 'required' => true, |
64 | 64 | 'type' => 'string', |
65 | - 'description' => __( 'Email of the reviewer.', 'woocommerce' ), |
|
65 | + 'description' => __('Email of the reviewer.', 'woocommerce'), |
|
66 | 66 | ), |
67 | 67 | ) |
68 | 68 | ), |
69 | 69 | ), |
70 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
70 | + 'schema' => array($this, 'get_public_item_schema'), |
|
71 | 71 | ), |
72 | 72 | true |
73 | 73 | ); |
@@ -78,37 +78,37 @@ discard block |
||
78 | 78 | array( |
79 | 79 | 'args' => array( |
80 | 80 | 'id' => array( |
81 | - 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
|
81 | + 'description' => __('Unique identifier for the resource.', 'woocommerce'), |
|
82 | 82 | 'type' => 'integer', |
83 | 83 | ), |
84 | 84 | ), |
85 | 85 | array( |
86 | 86 | 'methods' => \WP_REST_Server::READABLE, |
87 | - 'callback' => array( $this, 'get_item' ), |
|
88 | - 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
|
87 | + 'callback' => array($this, 'get_item'), |
|
88 | + 'permission_callback' => array($this, 'get_item_permissions_check'), |
|
89 | 89 | 'args' => array( |
90 | - 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
|
90 | + 'context' => $this->get_context_param(array('default' => 'view')), |
|
91 | 91 | ), |
92 | 92 | ), |
93 | 93 | array( |
94 | 94 | 'methods' => \WP_REST_Server::EDITABLE, |
95 | - 'callback' => array( $this, 'update_item' ), |
|
96 | - 'permission_callback' => array( $this, 'update_item_permissions_check' ), |
|
97 | - 'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::EDITABLE ), |
|
95 | + 'callback' => array($this, 'update_item'), |
|
96 | + 'permission_callback' => array($this, 'update_item_permissions_check'), |
|
97 | + 'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::EDITABLE), |
|
98 | 98 | ), |
99 | 99 | array( |
100 | 100 | 'methods' => \WP_REST_Server::DELETABLE, |
101 | - 'callback' => array( $this, 'delete_item' ), |
|
102 | - 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
|
101 | + 'callback' => array($this, 'delete_item'), |
|
102 | + 'permission_callback' => array($this, 'delete_item_permissions_check'), |
|
103 | 103 | 'args' => array( |
104 | 104 | 'force' => array( |
105 | 105 | 'default' => false, |
106 | 106 | 'type' => 'boolean', |
107 | - 'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ), |
|
107 | + 'description' => __('Whether to bypass trash and force deletion.', 'woocommerce'), |
|
108 | 108 | ), |
109 | 109 | ), |
110 | 110 | ), |
111 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
111 | + 'schema' => array($this, 'get_public_item_schema'), |
|
112 | 112 | ), |
113 | 113 | true |
114 | 114 | ); |
@@ -119,11 +119,11 @@ discard block |
||
119 | 119 | array( |
120 | 120 | array( |
121 | 121 | 'methods' => \WP_REST_Server::EDITABLE, |
122 | - 'callback' => array( $this, 'batch_items' ), |
|
123 | - 'permission_callback' => array( $this, 'batch_items_permissions_check' ), |
|
124 | - 'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::EDITABLE ), |
|
122 | + 'callback' => array($this, 'batch_items'), |
|
123 | + 'permission_callback' => array($this, 'batch_items_permissions_check'), |
|
124 | + 'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::EDITABLE), |
|
125 | 125 | ), |
126 | - 'schema' => array( $this, 'get_public_batch_schema' ), |
|
126 | + 'schema' => array($this, 'get_public_batch_schema'), |
|
127 | 127 | ), |
128 | 128 | true |
129 | 129 | ); |
@@ -135,9 +135,9 @@ discard block |
||
135 | 135 | * @param \WP_REST_Request $request Full details about the request. |
136 | 136 | * @return \WP_Error|boolean |
137 | 137 | */ |
138 | - public function get_items_permissions_check( $request ) { |
|
139 | - if ( ! wc_rest_check_product_reviews_permissions( 'read' ) ) { |
|
140 | - return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
138 | + public function get_items_permissions_check($request) { |
|
139 | + if ( ! wc_rest_check_product_reviews_permissions('read')) { |
|
140 | + return new \WP_Error('woocommerce_rest_cannot_view', __('Sorry, you cannot list resources.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
141 | 141 | } |
142 | 142 | |
143 | 143 | return true; |
@@ -149,12 +149,12 @@ discard block |
||
149 | 149 | * @param \WP_REST_Request $request Full details about the request. |
150 | 150 | * @return \WP_Error|boolean |
151 | 151 | */ |
152 | - public function get_item_permissions_check( $request ) { |
|
152 | + public function get_item_permissions_check($request) { |
|
153 | 153 | $id = (int) $request['id']; |
154 | - $review = get_comment( $id ); |
|
154 | + $review = get_comment($id); |
|
155 | 155 | |
156 | - if ( $review && ! wc_rest_check_product_reviews_permissions( 'read', $review->comment_ID ) ) { |
|
157 | - return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
156 | + if ($review && ! wc_rest_check_product_reviews_permissions('read', $review->comment_ID)) { |
|
157 | + return new \WP_Error('woocommerce_rest_cannot_view', __('Sorry, you cannot view this resource.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
158 | 158 | } |
159 | 159 | |
160 | 160 | return true; |
@@ -166,9 +166,9 @@ discard block |
||
166 | 166 | * @param \WP_REST_Request $request Full details about the request. |
167 | 167 | * @return \WP_Error|boolean |
168 | 168 | */ |
169 | - public function create_item_permissions_check( $request ) { |
|
170 | - if ( ! wc_rest_check_product_reviews_permissions( 'create' ) ) { |
|
171 | - return new \WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
169 | + public function create_item_permissions_check($request) { |
|
170 | + if ( ! wc_rest_check_product_reviews_permissions('create')) { |
|
171 | + return new \WP_Error('woocommerce_rest_cannot_create', __('Sorry, you are not allowed to create resources.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
172 | 172 | } |
173 | 173 | |
174 | 174 | return true; |
@@ -180,12 +180,12 @@ discard block |
||
180 | 180 | * @param \WP_REST_Request $request Full details about the request. |
181 | 181 | * @return \WP_Error|boolean |
182 | 182 | */ |
183 | - public function update_item_permissions_check( $request ) { |
|
183 | + public function update_item_permissions_check($request) { |
|
184 | 184 | $id = (int) $request['id']; |
185 | - $review = get_comment( $id ); |
|
185 | + $review = get_comment($id); |
|
186 | 186 | |
187 | - if ( $review && ! wc_rest_check_product_reviews_permissions( 'edit', $review->comment_ID ) ) { |
|
188 | - return new \WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
187 | + if ($review && ! wc_rest_check_product_reviews_permissions('edit', $review->comment_ID)) { |
|
188 | + return new \WP_Error('woocommerce_rest_cannot_edit', __('Sorry, you cannot edit this resource.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
189 | 189 | } |
190 | 190 | |
191 | 191 | return true; |
@@ -197,12 +197,12 @@ discard block |
||
197 | 197 | * @param \WP_REST_Request $request Full details about the request. |
198 | 198 | * @return \WP_Error|boolean |
199 | 199 | */ |
200 | - public function delete_item_permissions_check( $request ) { |
|
200 | + public function delete_item_permissions_check($request) { |
|
201 | 201 | $id = (int) $request['id']; |
202 | - $review = get_comment( $id ); |
|
202 | + $review = get_comment($id); |
|
203 | 203 | |
204 | - if ( $review && ! wc_rest_check_product_reviews_permissions( 'delete', $review->comment_ID ) ) { |
|
205 | - return new \WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
204 | + if ($review && ! wc_rest_check_product_reviews_permissions('delete', $review->comment_ID)) { |
|
205 | + return new \WP_Error('woocommerce_rest_cannot_edit', __('Sorry, you cannot delete this resource.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
206 | 206 | } |
207 | 207 | |
208 | 208 | return true; |
@@ -214,9 +214,9 @@ discard block |
||
214 | 214 | * @param \WP_REST_Request $request Full details about the request. |
215 | 215 | * @return boolean|\WP_Error |
216 | 216 | */ |
217 | - public function batch_items_permissions_check( $request ) { |
|
218 | - if ( ! wc_rest_check_product_reviews_permissions( 'create' ) ) { |
|
219 | - return new \WP_Error( 'woocommerce_rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
217 | + public function batch_items_permissions_check($request) { |
|
218 | + if ( ! wc_rest_check_product_reviews_permissions('create')) { |
|
219 | + return new \WP_Error('woocommerce_rest_cannot_batch', __('Sorry, you are not allowed to batch manipulate this resource.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
220 | 220 | } |
221 | 221 | |
222 | 222 | return true; |
@@ -228,7 +228,7 @@ discard block |
||
228 | 228 | * @param \WP_REST_Request $request Full details about the request. |
229 | 229 | * @return array|\WP_Error |
230 | 230 | */ |
231 | - public function get_items( $request ) { |
|
231 | + public function get_items($request) { |
|
232 | 232 | // Retrieve the list of registered collection query parameters. |
233 | 233 | $registered = $this->get_collection_params(); |
234 | 234 | |
@@ -258,24 +258,24 @@ discard block |
||
258 | 258 | * For each known parameter which is both registered and present in the request, |
259 | 259 | * set the parameter's value on the query $prepared_args. |
260 | 260 | */ |
261 | - foreach ( $parameter_mappings as $api_param => $wp_param ) { |
|
262 | - if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) { |
|
263 | - $prepared_args[ $wp_param ] = $request[ $api_param ]; |
|
261 | + foreach ($parameter_mappings as $api_param => $wp_param) { |
|
262 | + if (isset($registered[$api_param], $request[$api_param])) { |
|
263 | + $prepared_args[$wp_param] = $request[$api_param]; |
|
264 | 264 | } |
265 | 265 | } |
266 | 266 | |
267 | 267 | // Ensure certain parameter values default to empty strings. |
268 | - foreach ( array( 'author_email', 'search' ) as $param ) { |
|
269 | - if ( ! isset( $prepared_args[ $param ] ) ) { |
|
270 | - $prepared_args[ $param ] = ''; |
|
268 | + foreach (array('author_email', 'search') as $param) { |
|
269 | + if ( ! isset($prepared_args[$param])) { |
|
270 | + $prepared_args[$param] = ''; |
|
271 | 271 | } |
272 | 272 | } |
273 | 273 | |
274 | - if ( isset( $registered['orderby'] ) ) { |
|
275 | - $prepared_args['orderby'] = $this->normalize_query_param( $request['orderby'] ); |
|
274 | + if (isset($registered['orderby'])) { |
|
275 | + $prepared_args['orderby'] = $this->normalize_query_param($request['orderby']); |
|
276 | 276 | } |
277 | 277 | |
278 | - if ( isset( $prepared_args['status'] ) ) { |
|
278 | + if (isset($prepared_args['status'])) { |
|
279 | 279 | $prepared_args['status'] = 'approved' === $prepared_args['status'] ? 'approve' : $prepared_args['status']; |
280 | 280 | } |
281 | 281 | |
@@ -283,17 +283,17 @@ discard block |
||
283 | 283 | $prepared_args['date_query'] = array(); |
284 | 284 | |
285 | 285 | // Set before into date query. Date query must be specified as an array of an array. |
286 | - if ( isset( $registered['before'], $request['before'] ) ) { |
|
286 | + if (isset($registered['before'], $request['before'])) { |
|
287 | 287 | $prepared_args['date_query'][0]['before'] = $request['before']; |
288 | 288 | } |
289 | 289 | |
290 | 290 | // Set after into date query. Date query must be specified as an array of an array. |
291 | - if ( isset( $registered['after'], $request['after'] ) ) { |
|
291 | + if (isset($registered['after'], $request['after'])) { |
|
292 | 292 | $prepared_args['date_query'][0]['after'] = $request['after']; |
293 | 293 | } |
294 | 294 | |
295 | - if ( isset( $registered['page'] ) && empty( $request['offset'] ) ) { |
|
296 | - $prepared_args['offset'] = $prepared_args['number'] * ( absint( $request['page'] ) - 1 ); |
|
295 | + if (isset($registered['page']) && empty($request['offset'])) { |
|
296 | + $prepared_args['offset'] = $prepared_args['number'] * (absint($request['page']) - 1); |
|
297 | 297 | } |
298 | 298 | |
299 | 299 | /** |
@@ -304,61 +304,61 @@ discard block |
||
304 | 304 | * @param array $prepared_args Array of arguments for \WP_Comment_Query. |
305 | 305 | * @param \WP_REST_Request $request The current request. |
306 | 306 | */ |
307 | - $prepared_args = apply_filters( 'woocommerce_rest_product_review_query', $prepared_args, $request ); |
|
307 | + $prepared_args = apply_filters('woocommerce_rest_product_review_query', $prepared_args, $request); |
|
308 | 308 | |
309 | 309 | // Make sure that returns only reviews. |
310 | 310 | $prepared_args['type'] = 'review'; |
311 | 311 | |
312 | 312 | // Query reviews. |
313 | 313 | $query = new \WP_Comment_Query(); |
314 | - $query_result = $query->query( $prepared_args ); |
|
314 | + $query_result = $query->query($prepared_args); |
|
315 | 315 | $reviews = array(); |
316 | 316 | |
317 | - foreach ( $query_result as $review ) { |
|
318 | - if ( ! wc_rest_check_product_reviews_permissions( 'read', $review->comment_ID ) ) { |
|
317 | + foreach ($query_result as $review) { |
|
318 | + if ( ! wc_rest_check_product_reviews_permissions('read', $review->comment_ID)) { |
|
319 | 319 | continue; |
320 | 320 | } |
321 | 321 | |
322 | - $data = $this->prepare_item_for_response( $review, $request ); |
|
323 | - $reviews[] = $this->prepare_response_for_collection( $data ); |
|
322 | + $data = $this->prepare_item_for_response($review, $request); |
|
323 | + $reviews[] = $this->prepare_response_for_collection($data); |
|
324 | 324 | } |
325 | 325 | |
326 | 326 | $total_reviews = (int) $query->found_comments; |
327 | 327 | $max_pages = (int) $query->max_num_pages; |
328 | 328 | |
329 | - if ( $total_reviews < 1 ) { |
|
329 | + if ($total_reviews < 1) { |
|
330 | 330 | // Out-of-bounds, run the query again without LIMIT for total count. |
331 | - unset( $prepared_args['number'], $prepared_args['offset'] ); |
|
331 | + unset($prepared_args['number'], $prepared_args['offset']); |
|
332 | 332 | |
333 | 333 | $query = new \WP_Comment_Query(); |
334 | 334 | $prepared_args['count'] = true; |
335 | 335 | |
336 | - $total_reviews = $query->query( $prepared_args ); |
|
337 | - $max_pages = ceil( $total_reviews / $request['per_page'] ); |
|
336 | + $total_reviews = $query->query($prepared_args); |
|
337 | + $max_pages = ceil($total_reviews / $request['per_page']); |
|
338 | 338 | } |
339 | 339 | |
340 | - $response = rest_ensure_response( $reviews ); |
|
341 | - $response->header( 'X-WP-Total', $total_reviews ); |
|
342 | - $response->header( 'X-WP-TotalPages', $max_pages ); |
|
340 | + $response = rest_ensure_response($reviews); |
|
341 | + $response->header('X-WP-Total', $total_reviews); |
|
342 | + $response->header('X-WP-TotalPages', $max_pages); |
|
343 | 343 | |
344 | - $base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) ); |
|
344 | + $base = add_query_arg($request->get_query_params(), rest_url(sprintf('%s/%s', $this->namespace, $this->rest_base))); |
|
345 | 345 | |
346 | - if ( $request['page'] > 1 ) { |
|
346 | + if ($request['page'] > 1) { |
|
347 | 347 | $prev_page = $request['page'] - 1; |
348 | 348 | |
349 | - if ( $prev_page > $max_pages ) { |
|
349 | + if ($prev_page > $max_pages) { |
|
350 | 350 | $prev_page = $max_pages; |
351 | 351 | } |
352 | 352 | |
353 | - $prev_link = add_query_arg( 'page', $prev_page, $base ); |
|
354 | - $response->link_header( 'prev', $prev_link ); |
|
353 | + $prev_link = add_query_arg('page', $prev_page, $base); |
|
354 | + $response->link_header('prev', $prev_link); |
|
355 | 355 | } |
356 | 356 | |
357 | - if ( $max_pages > $request['page'] ) { |
|
357 | + if ($max_pages > $request['page']) { |
|
358 | 358 | $next_page = $request['page'] + 1; |
359 | - $next_link = add_query_arg( 'page', $next_page, $base ); |
|
359 | + $next_link = add_query_arg('page', $next_page, $base); |
|
360 | 360 | |
361 | - $response->link_header( 'next', $next_link ); |
|
361 | + $response->link_header('next', $next_link); |
|
362 | 362 | } |
363 | 363 | |
364 | 364 | return $response; |
@@ -370,19 +370,19 @@ discard block |
||
370 | 370 | * @param \WP_REST_Request $request Full details about the request. |
371 | 371 | * @return \WP_Error|\WP_REST_Response |
372 | 372 | */ |
373 | - public function create_item( $request ) { |
|
374 | - if ( ! empty( $request['id'] ) ) { |
|
375 | - return new \WP_Error( 'woocommerce_rest_review_exists', __( 'Cannot create existing product review.', 'woocommerce' ), array( 'status' => 400 ) ); |
|
373 | + public function create_item($request) { |
|
374 | + if ( ! empty($request['id'])) { |
|
375 | + return new \WP_Error('woocommerce_rest_review_exists', __('Cannot create existing product review.', 'woocommerce'), array('status' => 400)); |
|
376 | 376 | } |
377 | 377 | |
378 | 378 | $product_id = (int) $request['product_id']; |
379 | 379 | |
380 | - if ( 'product' !== get_post_type( $product_id ) ) { |
|
381 | - return new \WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) ); |
|
380 | + if ('product' !== get_post_type($product_id)) { |
|
381 | + return new \WP_Error('woocommerce_rest_product_invalid_id', __('Invalid product ID.', 'woocommerce'), array('status' => 404)); |
|
382 | 382 | } |
383 | 383 | |
384 | - $prepared_review = $this->prepare_item_for_database( $request ); |
|
385 | - if ( is_wp_error( $prepared_review ) ) { |
|
384 | + $prepared_review = $this->prepare_item_for_database($request); |
|
385 | + if (is_wp_error($prepared_review)) { |
|
386 | 386 | return $prepared_review; |
387 | 387 | } |
388 | 388 | |
@@ -391,49 +391,49 @@ discard block |
||
391 | 391 | /* |
392 | 392 | * Do not allow a comment to be created with missing or empty comment_content. See wp_handle_comment_submission(). |
393 | 393 | */ |
394 | - if ( empty( $prepared_review['comment_content'] ) ) { |
|
395 | - return new \WP_Error( 'woocommerce_rest_review_content_invalid', __( 'Invalid review content.', 'woocommerce' ), array( 'status' => 400 ) ); |
|
394 | + if (empty($prepared_review['comment_content'])) { |
|
395 | + return new \WP_Error('woocommerce_rest_review_content_invalid', __('Invalid review content.', 'woocommerce'), array('status' => 400)); |
|
396 | 396 | } |
397 | 397 | |
398 | 398 | // Setting remaining values before wp_insert_comment so we can use wp_allow_comment(). |
399 | - if ( ! isset( $prepared_review['comment_date_gmt'] ) ) { |
|
400 | - $prepared_review['comment_date_gmt'] = current_time( 'mysql', true ); |
|
399 | + if ( ! isset($prepared_review['comment_date_gmt'])) { |
|
400 | + $prepared_review['comment_date_gmt'] = current_time('mysql', true); |
|
401 | 401 | } |
402 | 402 | |
403 | - if ( ! empty( $_SERVER['REMOTE_ADDR'] ) && rest_is_ip_address( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) ) { // WPCS: input var ok, sanitization ok. |
|
404 | - $prepared_review['comment_author_IP'] = wc_clean( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ); // WPCS: input var ok. |
|
403 | + if ( ! empty($_SERVER['REMOTE_ADDR']) && rest_is_ip_address(wp_unslash($_SERVER['REMOTE_ADDR']))) { // WPCS: input var ok, sanitization ok. |
|
404 | + $prepared_review['comment_author_IP'] = wc_clean(wp_unslash($_SERVER['REMOTE_ADDR'])); // WPCS: input var ok. |
|
405 | 405 | } else { |
406 | 406 | $prepared_review['comment_author_IP'] = '127.0.0.1'; |
407 | 407 | } |
408 | 408 | |
409 | - if ( ! empty( $request['author_user_agent'] ) ) { |
|
409 | + if ( ! empty($request['author_user_agent'])) { |
|
410 | 410 | $prepared_review['comment_agent'] = $request['author_user_agent']; |
411 | - } elseif ( $request->get_header( 'user_agent' ) ) { |
|
412 | - $prepared_review['comment_agent'] = $request->get_header( 'user_agent' ); |
|
411 | + } elseif ($request->get_header('user_agent')) { |
|
412 | + $prepared_review['comment_agent'] = $request->get_header('user_agent'); |
|
413 | 413 | } else { |
414 | 414 | $prepared_review['comment_agent'] = ''; |
415 | 415 | } |
416 | 416 | |
417 | - $check_comment_lengths = wp_check_comment_data_max_lengths( $prepared_review ); |
|
418 | - if ( is_wp_error( $check_comment_lengths ) ) { |
|
419 | - $error_code = str_replace( array( 'comment_author', 'comment_content' ), array( 'reviewer', 'review_content' ), $check_comment_lengths->get_error_code() ); |
|
420 | - return new \WP_Error( 'woocommerce_rest_' . $error_code, __( 'Product review field exceeds maximum length allowed.', 'woocommerce' ), array( 'status' => 400 ) ); |
|
417 | + $check_comment_lengths = wp_check_comment_data_max_lengths($prepared_review); |
|
418 | + if (is_wp_error($check_comment_lengths)) { |
|
419 | + $error_code = str_replace(array('comment_author', 'comment_content'), array('reviewer', 'review_content'), $check_comment_lengths->get_error_code()); |
|
420 | + return new \WP_Error('woocommerce_rest_' . $error_code, __('Product review field exceeds maximum length allowed.', 'woocommerce'), array('status' => 400)); |
|
421 | 421 | } |
422 | 422 | |
423 | 423 | $prepared_review['comment_parent'] = 0; |
424 | 424 | $prepared_review['comment_author_url'] = ''; |
425 | - $prepared_review['comment_approved'] = wp_allow_comment( $prepared_review, true ); |
|
425 | + $prepared_review['comment_approved'] = wp_allow_comment($prepared_review, true); |
|
426 | 426 | |
427 | - if ( is_wp_error( $prepared_review['comment_approved'] ) ) { |
|
427 | + if (is_wp_error($prepared_review['comment_approved'])) { |
|
428 | 428 | $error_code = $prepared_review['comment_approved']->get_error_code(); |
429 | 429 | $error_message = $prepared_review['comment_approved']->get_error_message(); |
430 | 430 | |
431 | - if ( 'comment_duplicate' === $error_code ) { |
|
432 | - return new \WP_Error( 'woocommerce_rest_' . $error_code, $error_message, array( 'status' => 409 ) ); |
|
431 | + if ('comment_duplicate' === $error_code) { |
|
432 | + return new \WP_Error('woocommerce_rest_' . $error_code, $error_message, array('status' => 409)); |
|
433 | 433 | } |
434 | 434 | |
435 | - if ( 'comment_flood' === $error_code ) { |
|
436 | - return new \WP_Error( 'woocommerce_rest_' . $error_code, $error_message, array( 'status' => 400 ) ); |
|
435 | + if ('comment_flood' === $error_code) { |
|
436 | + return new \WP_Error('woocommerce_rest_' . $error_code, $error_message, array('status' => 400)); |
|
437 | 437 | } |
438 | 438 | |
439 | 439 | return $prepared_review['comment_approved']; |
@@ -450,24 +450,24 @@ discard block |
||
450 | 450 | * @param array|\WP_Error $prepared_review The prepared review data for wp_insert_comment(). |
451 | 451 | * @param \WP_REST_Request $request Request used to insert the review. |
452 | 452 | */ |
453 | - $prepared_review = apply_filters( 'woocommerce_rest_pre_insert_product_review', $prepared_review, $request ); |
|
454 | - if ( is_wp_error( $prepared_review ) ) { |
|
453 | + $prepared_review = apply_filters('woocommerce_rest_pre_insert_product_review', $prepared_review, $request); |
|
454 | + if (is_wp_error($prepared_review)) { |
|
455 | 455 | return $prepared_review; |
456 | 456 | } |
457 | 457 | |
458 | - $review_id = wp_insert_comment( wp_filter_comment( wp_slash( (array) $prepared_review ) ) ); |
|
458 | + $review_id = wp_insert_comment(wp_filter_comment(wp_slash((array) $prepared_review))); |
|
459 | 459 | |
460 | - if ( ! $review_id ) { |
|
461 | - return new \WP_Error( 'woocommerce_rest_review_failed_create', __( 'Creating product review failed.', 'woocommerce' ), array( 'status' => 500 ) ); |
|
460 | + if ( ! $review_id) { |
|
461 | + return new \WP_Error('woocommerce_rest_review_failed_create', __('Creating product review failed.', 'woocommerce'), array('status' => 500)); |
|
462 | 462 | } |
463 | 463 | |
464 | - if ( isset( $request['status'] ) ) { |
|
465 | - $this->handle_status_param( $request['status'], $review_id ); |
|
464 | + if (isset($request['status'])) { |
|
465 | + $this->handle_status_param($request['status'], $review_id); |
|
466 | 466 | } |
467 | 467 | |
468 | - update_comment_meta( $review_id, 'rating', ! empty( $request['rating'] ) ? $request['rating'] : '0' ); |
|
468 | + update_comment_meta($review_id, 'rating', ! empty($request['rating']) ? $request['rating'] : '0'); |
|
469 | 469 | |
470 | - $review = get_comment( $review_id ); |
|
470 | + $review = get_comment($review_id); |
|
471 | 471 | |
472 | 472 | /** |
473 | 473 | * Fires after a comment is created or updated via the REST API. |
@@ -476,21 +476,21 @@ discard block |
||
476 | 476 | * @param \WP_REST_Request $request Request object. |
477 | 477 | * @param bool $creating True when creating a comment, false when updating. |
478 | 478 | */ |
479 | - do_action( 'woocommerce_rest_insert_product_review', $review, $request, true ); |
|
479 | + do_action('woocommerce_rest_insert_product_review', $review, $request, true); |
|
480 | 480 | |
481 | - $fields_update = $this->update_additional_fields_for_object( $review, $request ); |
|
482 | - if ( is_wp_error( $fields_update ) ) { |
|
481 | + $fields_update = $this->update_additional_fields_for_object($review, $request); |
|
482 | + if (is_wp_error($fields_update)) { |
|
483 | 483 | return $fields_update; |
484 | 484 | } |
485 | 485 | |
486 | - $context = current_user_can( 'moderate_comments' ) ? 'edit' : 'view'; |
|
487 | - $request->set_param( 'context', $context ); |
|
486 | + $context = current_user_can('moderate_comments') ? 'edit' : 'view'; |
|
487 | + $request->set_param('context', $context); |
|
488 | 488 | |
489 | - $response = $this->prepare_item_for_response( $review, $request ); |
|
490 | - $response = rest_ensure_response( $response ); |
|
489 | + $response = $this->prepare_item_for_response($review, $request); |
|
490 | + $response = rest_ensure_response($response); |
|
491 | 491 | |
492 | - $response->set_status( 201 ); |
|
493 | - $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $review_id ) ) ); |
|
492 | + $response->set_status(201); |
|
493 | + $response->header('Location', rest_url(sprintf('%s/%s/%d', $this->namespace, $this->rest_base, $review_id))); |
|
494 | 494 | |
495 | 495 | return $response; |
496 | 496 | } |
@@ -501,14 +501,14 @@ discard block |
||
501 | 501 | * @param \WP_REST_Request $request Full details about the request. |
502 | 502 | * @return \WP_Error|\WP_REST_Response |
503 | 503 | */ |
504 | - public function get_item( $request ) { |
|
505 | - $review = $this->get_review( $request['id'] ); |
|
506 | - if ( is_wp_error( $review ) ) { |
|
504 | + public function get_item($request) { |
|
505 | + $review = $this->get_review($request['id']); |
|
506 | + if (is_wp_error($review)) { |
|
507 | 507 | return $review; |
508 | 508 | } |
509 | 509 | |
510 | - $data = $this->prepare_item_for_response( $review, $request ); |
|
511 | - $response = rest_ensure_response( $data ); |
|
510 | + $data = $this->prepare_item_for_response($review, $request); |
|
511 | + $response = rest_ensure_response($data); |
|
512 | 512 | |
513 | 513 | return $response; |
514 | 514 | } |
@@ -519,84 +519,84 @@ discard block |
||
519 | 519 | * @param \WP_REST_Request $request Full details about the request. |
520 | 520 | * @return \WP_Error|\WP_REST_Response Response object on success, or error object on failure. |
521 | 521 | */ |
522 | - public function update_item( $request ) { |
|
523 | - $review = $this->get_review( $request['id'] ); |
|
524 | - if ( is_wp_error( $review ) ) { |
|
522 | + public function update_item($request) { |
|
523 | + $review = $this->get_review($request['id']); |
|
524 | + if (is_wp_error($review)) { |
|
525 | 525 | return $review; |
526 | 526 | } |
527 | 527 | |
528 | 528 | $id = (int) $review->comment_ID; |
529 | 529 | |
530 | - if ( isset( $request['type'] ) && 'review' !== get_comment_type( $id ) ) { |
|
531 | - return new \WP_Error( 'woocommerce_rest_review_invalid_type', __( 'Sorry, you are not allowed to change the comment type.', 'woocommerce' ), array( 'status' => 404 ) ); |
|
530 | + if (isset($request['type']) && 'review' !== get_comment_type($id)) { |
|
531 | + return new \WP_Error('woocommerce_rest_review_invalid_type', __('Sorry, you are not allowed to change the comment type.', 'woocommerce'), array('status' => 404)); |
|
532 | 532 | } |
533 | 533 | |
534 | - $prepared_args = $this->prepare_item_for_database( $request ); |
|
535 | - if ( is_wp_error( $prepared_args ) ) { |
|
534 | + $prepared_args = $this->prepare_item_for_database($request); |
|
535 | + if (is_wp_error($prepared_args)) { |
|
536 | 536 | return $prepared_args; |
537 | 537 | } |
538 | 538 | |
539 | - if ( ! empty( $prepared_args['comment_post_ID'] ) ) { |
|
540 | - if ( 'product' !== get_post_type( (int) $prepared_args['comment_post_ID'] ) ) { |
|
541 | - return new \WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) ); |
|
539 | + if ( ! empty($prepared_args['comment_post_ID'])) { |
|
540 | + if ('product' !== get_post_type((int) $prepared_args['comment_post_ID'])) { |
|
541 | + return new \WP_Error('woocommerce_rest_product_invalid_id', __('Invalid product ID.', 'woocommerce'), array('status' => 404)); |
|
542 | 542 | } |
543 | 543 | } |
544 | 544 | |
545 | - if ( empty( $prepared_args ) && isset( $request['status'] ) ) { |
|
545 | + if (empty($prepared_args) && isset($request['status'])) { |
|
546 | 546 | // Only the comment status is being changed. |
547 | - $change = $this->handle_status_param( $request['status'], $id ); |
|
547 | + $change = $this->handle_status_param($request['status'], $id); |
|
548 | 548 | |
549 | - if ( ! $change ) { |
|
550 | - return new \WP_Error( 'woocommerce_rest_review_failed_edit', __( 'Updating review status failed.', 'woocommerce' ), array( 'status' => 500 ) ); |
|
549 | + if ( ! $change) { |
|
550 | + return new \WP_Error('woocommerce_rest_review_failed_edit', __('Updating review status failed.', 'woocommerce'), array('status' => 500)); |
|
551 | 551 | } |
552 | - } elseif ( ! empty( $prepared_args ) ) { |
|
553 | - if ( is_wp_error( $prepared_args ) ) { |
|
552 | + } elseif ( ! empty($prepared_args)) { |
|
553 | + if (is_wp_error($prepared_args)) { |
|
554 | 554 | return $prepared_args; |
555 | 555 | } |
556 | 556 | |
557 | - if ( isset( $prepared_args['comment_content'] ) && empty( $prepared_args['comment_content'] ) ) { |
|
558 | - return new \WP_Error( 'woocommerce_rest_review_content_invalid', __( 'Invalid review content.', 'woocommerce' ), array( 'status' => 400 ) ); |
|
557 | + if (isset($prepared_args['comment_content']) && empty($prepared_args['comment_content'])) { |
|
558 | + return new \WP_Error('woocommerce_rest_review_content_invalid', __('Invalid review content.', 'woocommerce'), array('status' => 400)); |
|
559 | 559 | } |
560 | 560 | |
561 | 561 | $prepared_args['comment_ID'] = $id; |
562 | 562 | |
563 | - $check_comment_lengths = wp_check_comment_data_max_lengths( $prepared_args ); |
|
564 | - if ( is_wp_error( $check_comment_lengths ) ) { |
|
565 | - $error_code = str_replace( array( 'comment_author', 'comment_content' ), array( 'reviewer', 'review_content' ), $check_comment_lengths->get_error_code() ); |
|
566 | - return new \WP_Error( 'woocommerce_rest_' . $error_code, __( 'Product review field exceeds maximum length allowed.', 'woocommerce' ), array( 'status' => 400 ) ); |
|
563 | + $check_comment_lengths = wp_check_comment_data_max_lengths($prepared_args); |
|
564 | + if (is_wp_error($check_comment_lengths)) { |
|
565 | + $error_code = str_replace(array('comment_author', 'comment_content'), array('reviewer', 'review_content'), $check_comment_lengths->get_error_code()); |
|
566 | + return new \WP_Error('woocommerce_rest_' . $error_code, __('Product review field exceeds maximum length allowed.', 'woocommerce'), array('status' => 400)); |
|
567 | 567 | } |
568 | 568 | |
569 | - $updated = wp_update_comment( wp_slash( (array) $prepared_args ) ); |
|
569 | + $updated = wp_update_comment(wp_slash((array) $prepared_args)); |
|
570 | 570 | |
571 | - if ( false === $updated ) { |
|
572 | - return new \WP_Error( 'woocommerce_rest_comment_failed_edit', __( 'Updating review failed.', 'woocommerce' ), array( 'status' => 500 ) ); |
|
571 | + if (false === $updated) { |
|
572 | + return new \WP_Error('woocommerce_rest_comment_failed_edit', __('Updating review failed.', 'woocommerce'), array('status' => 500)); |
|
573 | 573 | } |
574 | 574 | |
575 | - if ( isset( $request['status'] ) ) { |
|
576 | - $this->handle_status_param( $request['status'], $id ); |
|
575 | + if (isset($request['status'])) { |
|
576 | + $this->handle_status_param($request['status'], $id); |
|
577 | 577 | } |
578 | 578 | } |
579 | 579 | |
580 | - if ( ! empty( $request['rating'] ) ) { |
|
581 | - update_comment_meta( $id, 'rating', $request['rating'] ); |
|
580 | + if ( ! empty($request['rating'])) { |
|
581 | + update_comment_meta($id, 'rating', $request['rating']); |
|
582 | 582 | } |
583 | 583 | |
584 | - $review = get_comment( $id ); |
|
584 | + $review = get_comment($id); |
|
585 | 585 | |
586 | 586 | /** This action is documented in includes/api/class-wc-rest-product-reviews-controller.php */ |
587 | - do_action( 'woocommerce_rest_insert_product_review', $review, $request, false ); |
|
587 | + do_action('woocommerce_rest_insert_product_review', $review, $request, false); |
|
588 | 588 | |
589 | - $fields_update = $this->update_additional_fields_for_object( $review, $request ); |
|
589 | + $fields_update = $this->update_additional_fields_for_object($review, $request); |
|
590 | 590 | |
591 | - if ( is_wp_error( $fields_update ) ) { |
|
591 | + if (is_wp_error($fields_update)) { |
|
592 | 592 | return $fields_update; |
593 | 593 | } |
594 | 594 | |
595 | - $request->set_param( 'context', 'edit' ); |
|
595 | + $request->set_param('context', 'edit'); |
|
596 | 596 | |
597 | - $response = $this->prepare_item_for_response( $review, $request ); |
|
597 | + $response = $this->prepare_item_for_response($review, $request); |
|
598 | 598 | |
599 | - return rest_ensure_response( $response ); |
|
599 | + return rest_ensure_response($response); |
|
600 | 600 | } |
601 | 601 | |
602 | 602 | /** |
@@ -605,13 +605,13 @@ discard block |
||
605 | 605 | * @param \WP_REST_Request $request Full details about the request. |
606 | 606 | * @return \WP_Error|\WP_REST_Response Response object on success, or error object on failure. |
607 | 607 | */ |
608 | - public function delete_item( $request ) { |
|
609 | - $review = $this->get_review( $request['id'] ); |
|
610 | - if ( is_wp_error( $review ) ) { |
|
608 | + public function delete_item($request) { |
|
609 | + $review = $this->get_review($request['id']); |
|
610 | + if (is_wp_error($review)) { |
|
611 | 611 | return $review; |
612 | 612 | } |
613 | 613 | |
614 | - $force = isset( $request['force'] ) ? (bool) $request['force'] : false; |
|
614 | + $force = isset($request['force']) ? (bool) $request['force'] : false; |
|
615 | 615 | |
616 | 616 | /** |
617 | 617 | * Filters whether a review can be trashed. |
@@ -622,13 +622,13 @@ discard block |
||
622 | 622 | * @param bool $supports_trash Whether the post type support trashing. |
623 | 623 | * @param WP_Comment $review The review object being considered for trashing support. |
624 | 624 | */ |
625 | - $supports_trash = apply_filters( 'woocommerce_rest_product_review_trashable', ( EMPTY_TRASH_DAYS > 0 ), $review ); |
|
625 | + $supports_trash = apply_filters('woocommerce_rest_product_review_trashable', (EMPTY_TRASH_DAYS > 0), $review); |
|
626 | 626 | |
627 | - $request->set_param( 'context', 'edit' ); |
|
627 | + $request->set_param('context', 'edit'); |
|
628 | 628 | |
629 | - if ( $force ) { |
|
630 | - $previous = $this->prepare_item_for_response( $review, $request ); |
|
631 | - $result = wp_delete_comment( $review->comment_ID, true ); |
|
629 | + if ($force) { |
|
630 | + $previous = $this->prepare_item_for_response($review, $request); |
|
631 | + $result = wp_delete_comment($review->comment_ID, true); |
|
632 | 632 | $response = new \WP_REST_Response(); |
633 | 633 | $response->set_data( |
634 | 634 | array( |
@@ -638,22 +638,22 @@ discard block |
||
638 | 638 | ); |
639 | 639 | } else { |
640 | 640 | // If this type doesn't support trashing, error out. |
641 | - if ( ! $supports_trash ) { |
|
641 | + if ( ! $supports_trash) { |
|
642 | 642 | /* translators: %s: force=true */ |
643 | - return new \WP_Error( 'woocommerce_rest_trash_not_supported', sprintf( __( "The object does not support trashing. Set '%s' to delete.", 'woocommerce' ), 'force=true' ), array( 'status' => 501 ) ); |
|
643 | + return new \WP_Error('woocommerce_rest_trash_not_supported', sprintf(__("The object does not support trashing. Set '%s' to delete.", 'woocommerce'), 'force=true'), array('status' => 501)); |
|
644 | 644 | } |
645 | 645 | |
646 | - if ( 'trash' === $review->comment_approved ) { |
|
647 | - return new \WP_Error( 'woocommerce_rest_already_trashed', __( 'The object has already been trashed.', 'woocommerce' ), array( 'status' => 410 ) ); |
|
646 | + if ('trash' === $review->comment_approved) { |
|
647 | + return new \WP_Error('woocommerce_rest_already_trashed', __('The object has already been trashed.', 'woocommerce'), array('status' => 410)); |
|
648 | 648 | } |
649 | 649 | |
650 | - $result = wp_trash_comment( $review->comment_ID ); |
|
651 | - $review = get_comment( $review->comment_ID ); |
|
652 | - $response = $this->prepare_item_for_response( $review, $request ); |
|
650 | + $result = wp_trash_comment($review->comment_ID); |
|
651 | + $review = get_comment($review->comment_ID); |
|
652 | + $response = $this->prepare_item_for_response($review, $request); |
|
653 | 653 | } |
654 | 654 | |
655 | - if ( ! $result ) { |
|
656 | - return new \WP_Error( 'woocommerce_rest_cannot_delete', __( 'The object cannot be deleted.', 'woocommerce' ), array( 'status' => 500 ) ); |
|
655 | + if ( ! $result) { |
|
656 | + return new \WP_Error('woocommerce_rest_cannot_delete', __('The object cannot be deleted.', 'woocommerce'), array('status' => 500)); |
|
657 | 657 | } |
658 | 658 | |
659 | 659 | /** |
@@ -663,7 +663,7 @@ discard block |
||
663 | 663 | * @param \WP_REST_Response $response The response returned from the API. |
664 | 664 | * @param \WP_REST_Request $request The request sent to the API. |
665 | 665 | */ |
666 | - do_action( 'woocommerce_rest_delete_review', $review, $response, $request ); |
|
666 | + do_action('woocommerce_rest_delete_review', $review, $response, $request); |
|
667 | 667 | |
668 | 668 | return $response; |
669 | 669 | } |
@@ -675,52 +675,52 @@ discard block |
||
675 | 675 | * @param \WP_REST_Request $request Request object. |
676 | 676 | * @return \WP_REST_Response $response Response data. |
677 | 677 | */ |
678 | - public function prepare_item_for_response( $review, $request ) { |
|
679 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
680 | - $fields = $this->get_fields_for_response( $request ); |
|
678 | + public function prepare_item_for_response($review, $request) { |
|
679 | + $context = ! empty($request['context']) ? $request['context'] : 'view'; |
|
680 | + $fields = $this->get_fields_for_response($request); |
|
681 | 681 | $data = array(); |
682 | 682 | |
683 | - if ( in_array( 'id', $fields, true ) ) { |
|
683 | + if (in_array('id', $fields, true)) { |
|
684 | 684 | $data['id'] = (int) $review->comment_ID; |
685 | 685 | } |
686 | - if ( in_array( 'date_created', $fields, true ) ) { |
|
687 | - $data['date_created'] = wc_rest_prepare_date_response( $review->comment_date ); |
|
686 | + if (in_array('date_created', $fields, true)) { |
|
687 | + $data['date_created'] = wc_rest_prepare_date_response($review->comment_date); |
|
688 | 688 | } |
689 | - if ( in_array( 'date_created_gmt', $fields, true ) ) { |
|
690 | - $data['date_created_gmt'] = wc_rest_prepare_date_response( $review->comment_date_gmt ); |
|
689 | + if (in_array('date_created_gmt', $fields, true)) { |
|
690 | + $data['date_created_gmt'] = wc_rest_prepare_date_response($review->comment_date_gmt); |
|
691 | 691 | } |
692 | - if ( in_array( 'product_id', $fields, true ) ) { |
|
692 | + if (in_array('product_id', $fields, true)) { |
|
693 | 693 | $data['product_id'] = (int) $review->comment_post_ID; |
694 | 694 | } |
695 | - if ( in_array( 'status', $fields, true ) ) { |
|
696 | - $data['status'] = $this->prepare_status_response( (string) $review->comment_approved ); |
|
695 | + if (in_array('status', $fields, true)) { |
|
696 | + $data['status'] = $this->prepare_status_response((string) $review->comment_approved); |
|
697 | 697 | } |
698 | - if ( in_array( 'reviewer', $fields, true ) ) { |
|
698 | + if (in_array('reviewer', $fields, true)) { |
|
699 | 699 | $data['reviewer'] = $review->comment_author; |
700 | 700 | } |
701 | - if ( in_array( 'reviewer_email', $fields, true ) ) { |
|
701 | + if (in_array('reviewer_email', $fields, true)) { |
|
702 | 702 | $data['reviewer_email'] = $review->comment_author_email; |
703 | 703 | } |
704 | - if ( in_array( 'review', $fields, true ) ) { |
|
705 | - $data['review'] = 'view' === $context ? wpautop( $review->comment_content ) : $review->comment_content; |
|
704 | + if (in_array('review', $fields, true)) { |
|
705 | + $data['review'] = 'view' === $context ? wpautop($review->comment_content) : $review->comment_content; |
|
706 | 706 | } |
707 | - if ( in_array( 'rating', $fields, true ) ) { |
|
708 | - $data['rating'] = (int) get_comment_meta( $review->comment_ID, 'rating', true ); |
|
707 | + if (in_array('rating', $fields, true)) { |
|
708 | + $data['rating'] = (int) get_comment_meta($review->comment_ID, 'rating', true); |
|
709 | 709 | } |
710 | - if ( in_array( 'verified', $fields, true ) ) { |
|
711 | - $data['verified'] = wc_review_is_from_verified_owner( $review->comment_ID ); |
|
710 | + if (in_array('verified', $fields, true)) { |
|
711 | + $data['verified'] = wc_review_is_from_verified_owner($review->comment_ID); |
|
712 | 712 | } |
713 | - if ( in_array( 'reviewer_avatar_urls', $fields, true ) ) { |
|
714 | - $data['reviewer_avatar_urls'] = rest_get_avatar_urls( $review->comment_author_email ); |
|
713 | + if (in_array('reviewer_avatar_urls', $fields, true)) { |
|
714 | + $data['reviewer_avatar_urls'] = rest_get_avatar_urls($review->comment_author_email); |
|
715 | 715 | } |
716 | 716 | |
717 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
718 | - $data = $this->filter_response_by_context( $data, $context ); |
|
717 | + $data = $this->add_additional_fields_to_object($data, $request); |
|
718 | + $data = $this->filter_response_by_context($data, $context); |
|
719 | 719 | |
720 | 720 | // Wrap the data in a response object. |
721 | - $response = rest_ensure_response( $data ); |
|
721 | + $response = rest_ensure_response($data); |
|
722 | 722 | |
723 | - $response->add_links( $this->prepare_links( $review ) ); |
|
723 | + $response->add_links($this->prepare_links($review)); |
|
724 | 724 | |
725 | 725 | /** |
726 | 726 | * Filter product reviews object returned from the REST API. |
@@ -729,7 +729,7 @@ discard block |
||
729 | 729 | * @param \WP_Comment $review Product review object used to create response. |
730 | 730 | * @param \WP_REST_Request $request Request object. |
731 | 731 | */ |
732 | - return apply_filters( 'woocommerce_rest_prepare_product_review', $response, $review, $request ); |
|
732 | + return apply_filters('woocommerce_rest_prepare_product_review', $response, $review, $request); |
|
733 | 733 | } |
734 | 734 | |
735 | 735 | /** |
@@ -738,38 +738,38 @@ discard block |
||
738 | 738 | * @param \WP_REST_Request $request Request object. |
739 | 739 | * @return array|\WP_Error $prepared_review |
740 | 740 | */ |
741 | - protected function prepare_item_for_database( $request ) { |
|
742 | - if ( isset( $request['id'] ) ) { |
|
741 | + protected function prepare_item_for_database($request) { |
|
742 | + if (isset($request['id'])) { |
|
743 | 743 | $prepared_review['comment_ID'] = (int) $request['id']; |
744 | 744 | } |
745 | 745 | |
746 | - if ( isset( $request['review'] ) ) { |
|
746 | + if (isset($request['review'])) { |
|
747 | 747 | $prepared_review['comment_content'] = $request['review']; |
748 | 748 | } |
749 | 749 | |
750 | - if ( isset( $request['product_id'] ) ) { |
|
750 | + if (isset($request['product_id'])) { |
|
751 | 751 | $prepared_review['comment_post_ID'] = (int) $request['product_id']; |
752 | 752 | } |
753 | 753 | |
754 | - if ( isset( $request['reviewer'] ) ) { |
|
754 | + if (isset($request['reviewer'])) { |
|
755 | 755 | $prepared_review['comment_author'] = $request['reviewer']; |
756 | 756 | } |
757 | 757 | |
758 | - if ( isset( $request['reviewer_email'] ) ) { |
|
758 | + if (isset($request['reviewer_email'])) { |
|
759 | 759 | $prepared_review['comment_author_email'] = $request['reviewer_email']; |
760 | 760 | } |
761 | 761 | |
762 | - if ( ! empty( $request['date_created'] ) ) { |
|
763 | - $date_data = rest_get_date_with_gmt( $request['date_created'] ); |
|
762 | + if ( ! empty($request['date_created'])) { |
|
763 | + $date_data = rest_get_date_with_gmt($request['date_created']); |
|
764 | 764 | |
765 | - if ( ! empty( $date_data ) ) { |
|
766 | - list( $prepared_review['comment_date'], $prepared_review['comment_date_gmt'] ) = $date_data; |
|
765 | + if ( ! empty($date_data)) { |
|
766 | + list($prepared_review['comment_date'], $prepared_review['comment_date_gmt']) = $date_data; |
|
767 | 767 | } |
768 | - } elseif ( ! empty( $request['date_created_gmt'] ) ) { |
|
769 | - $date_data = rest_get_date_with_gmt( $request['date_created_gmt'], true ); |
|
768 | + } elseif ( ! empty($request['date_created_gmt'])) { |
|
769 | + $date_data = rest_get_date_with_gmt($request['date_created_gmt'], true); |
|
770 | 770 | |
771 | - if ( ! empty( $date_data ) ) { |
|
772 | - list( $prepared_review['comment_date'], $prepared_review['comment_date_gmt'] ) = $date_data; |
|
771 | + if ( ! empty($date_data)) { |
|
772 | + list($prepared_review['comment_date'], $prepared_review['comment_date_gmt']) = $date_data; |
|
773 | 773 | } |
774 | 774 | } |
775 | 775 | |
@@ -782,7 +782,7 @@ discard block |
||
782 | 782 | * @param array $prepared_review The prepared review data for `wp_insert_comment`. |
783 | 783 | * @param \WP_REST_Request $request The current request. |
784 | 784 | */ |
785 | - return apply_filters( 'woocommerce_rest_preprocess_product_review', $prepared_review, $request ); |
|
785 | + return apply_filters('woocommerce_rest_preprocess_product_review', $prepared_review, $request); |
|
786 | 786 | } |
787 | 787 | |
788 | 788 | /** |
@@ -791,24 +791,24 @@ discard block |
||
791 | 791 | * @param WP_Comment $review Product review object. |
792 | 792 | * @return array Links for the given product review. |
793 | 793 | */ |
794 | - protected function prepare_links( $review ) { |
|
794 | + protected function prepare_links($review) { |
|
795 | 795 | $links = array( |
796 | 796 | 'self' => array( |
797 | - 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $review->comment_ID ) ), |
|
797 | + 'href' => rest_url(sprintf('/%s/%s/%d', $this->namespace, $this->rest_base, $review->comment_ID)), |
|
798 | 798 | ), |
799 | 799 | 'collection' => array( |
800 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
800 | + 'href' => rest_url(sprintf('/%s/%s', $this->namespace, $this->rest_base)), |
|
801 | 801 | ), |
802 | 802 | ); |
803 | - if ( 0 !== (int) $review->comment_post_ID ) { |
|
803 | + if (0 !== (int) $review->comment_post_ID) { |
|
804 | 804 | $links['up'] = array( |
805 | - 'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $review->comment_post_ID ) ), |
|
805 | + 'href' => rest_url(sprintf('/%s/products/%d', $this->namespace, $review->comment_post_ID)), |
|
806 | 806 | 'embeddable' => true, |
807 | 807 | ); |
808 | 808 | } |
809 | - if ( 0 !== (int) $review->user_id ) { |
|
809 | + if (0 !== (int) $review->user_id) { |
|
810 | 810 | $links['reviewer'] = array( |
811 | - 'href' => rest_url( 'wp/v2/users/' . $review->user_id ), |
|
811 | + 'href' => rest_url('wp/v2/users/' . $review->user_id), |
|
812 | 812 | 'embeddable' => true, |
813 | 813 | ); |
814 | 814 | } |
@@ -827,91 +827,91 @@ discard block |
||
827 | 827 | 'type' => 'object', |
828 | 828 | 'properties' => array( |
829 | 829 | 'id' => array( |
830 | - 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
|
830 | + 'description' => __('Unique identifier for the resource.', 'woocommerce'), |
|
831 | 831 | 'type' => 'integer', |
832 | - 'context' => array( 'view', 'edit' ), |
|
832 | + 'context' => array('view', 'edit'), |
|
833 | 833 | 'readonly' => true, |
834 | 834 | ), |
835 | 835 | 'date_created' => array( |
836 | - 'description' => __( "The date the review was created, in the site's timezone.", 'woocommerce' ), |
|
836 | + 'description' => __("The date the review was created, in the site's timezone.", 'woocommerce'), |
|
837 | 837 | 'type' => 'date-time', |
838 | - 'context' => array( 'view', 'edit' ), |
|
838 | + 'context' => array('view', 'edit'), |
|
839 | 839 | 'readonly' => true, |
840 | 840 | ), |
841 | 841 | 'date_created_gmt' => array( |
842 | - 'description' => __( 'The date the review was created, as GMT.', 'woocommerce' ), |
|
842 | + 'description' => __('The date the review was created, as GMT.', 'woocommerce'), |
|
843 | 843 | 'type' => 'date-time', |
844 | - 'context' => array( 'view', 'edit' ), |
|
844 | + 'context' => array('view', 'edit'), |
|
845 | 845 | 'readonly' => true, |
846 | 846 | ), |
847 | 847 | 'product_id' => array( |
848 | - 'description' => __( 'Unique identifier for the product that the review belongs to.', 'woocommerce' ), |
|
848 | + 'description' => __('Unique identifier for the product that the review belongs to.', 'woocommerce'), |
|
849 | 849 | 'type' => 'integer', |
850 | - 'context' => array( 'view', 'edit' ), |
|
850 | + 'context' => array('view', 'edit'), |
|
851 | 851 | ), |
852 | 852 | 'status' => array( |
853 | - 'description' => __( 'Status of the review.', 'woocommerce' ), |
|
853 | + 'description' => __('Status of the review.', 'woocommerce'), |
|
854 | 854 | 'type' => 'string', |
855 | 855 | 'default' => 'approved', |
856 | - 'enum' => array( 'approved', 'hold', 'spam', 'unspam', 'trash', 'untrash' ), |
|
857 | - 'context' => array( 'view', 'edit' ), |
|
856 | + 'enum' => array('approved', 'hold', 'spam', 'unspam', 'trash', 'untrash'), |
|
857 | + 'context' => array('view', 'edit'), |
|
858 | 858 | ), |
859 | 859 | 'reviewer' => array( |
860 | - 'description' => __( 'Reviewer name.', 'woocommerce' ), |
|
860 | + 'description' => __('Reviewer name.', 'woocommerce'), |
|
861 | 861 | 'type' => 'string', |
862 | - 'context' => array( 'view', 'edit' ), |
|
862 | + 'context' => array('view', 'edit'), |
|
863 | 863 | ), |
864 | 864 | 'reviewer_email' => array( |
865 | - 'description' => __( 'Reviewer email.', 'woocommerce' ), |
|
865 | + 'description' => __('Reviewer email.', 'woocommerce'), |
|
866 | 866 | 'type' => 'string', |
867 | 867 | 'format' => 'email', |
868 | - 'context' => array( 'view', 'edit' ), |
|
868 | + 'context' => array('view', 'edit'), |
|
869 | 869 | ), |
870 | 870 | 'review' => array( |
871 | - 'description' => __( 'The content of the review.', 'woocommerce' ), |
|
871 | + 'description' => __('The content of the review.', 'woocommerce'), |
|
872 | 872 | 'type' => 'string', |
873 | - 'context' => array( 'view', 'edit' ), |
|
873 | + 'context' => array('view', 'edit'), |
|
874 | 874 | 'arg_options' => array( |
875 | 875 | 'sanitize_callback' => 'wp_filter_post_kses', |
876 | 876 | ), |
877 | 877 | ), |
878 | 878 | 'rating' => array( |
879 | - 'description' => __( 'Review rating (0 to 5).', 'woocommerce' ), |
|
879 | + 'description' => __('Review rating (0 to 5).', 'woocommerce'), |
|
880 | 880 | 'type' => 'integer', |
881 | - 'context' => array( 'view', 'edit' ), |
|
881 | + 'context' => array('view', 'edit'), |
|
882 | 882 | ), |
883 | 883 | 'verified' => array( |
884 | - 'description' => __( 'Shows if the reviewer bought the product or not.', 'woocommerce' ), |
|
884 | + 'description' => __('Shows if the reviewer bought the product or not.', 'woocommerce'), |
|
885 | 885 | 'type' => 'boolean', |
886 | - 'context' => array( 'view', 'edit' ), |
|
886 | + 'context' => array('view', 'edit'), |
|
887 | 887 | 'readonly' => true, |
888 | 888 | ), |
889 | 889 | ), |
890 | 890 | ); |
891 | 891 | |
892 | - if ( get_option( 'show_avatars' ) ) { |
|
892 | + if (get_option('show_avatars')) { |
|
893 | 893 | $avatar_properties = array(); |
894 | 894 | $avatar_sizes = rest_get_avatar_sizes(); |
895 | 895 | |
896 | - foreach ( $avatar_sizes as $size ) { |
|
897 | - $avatar_properties[ $size ] = array( |
|
896 | + foreach ($avatar_sizes as $size) { |
|
897 | + $avatar_properties[$size] = array( |
|
898 | 898 | /* translators: %d: avatar image size in pixels */ |
899 | - 'description' => sprintf( __( 'Avatar URL with image size of %d pixels.', 'woocommerce' ), $size ), |
|
899 | + 'description' => sprintf(__('Avatar URL with image size of %d pixels.', 'woocommerce'), $size), |
|
900 | 900 | 'type' => 'string', |
901 | 901 | 'format' => 'uri', |
902 | - 'context' => array( 'embed', 'view', 'edit' ), |
|
902 | + 'context' => array('embed', 'view', 'edit'), |
|
903 | 903 | ); |
904 | 904 | } |
905 | 905 | $schema['properties']['reviewer_avatar_urls'] = array( |
906 | - 'description' => __( 'Avatar URLs for the object reviewer.', 'woocommerce' ), |
|
906 | + 'description' => __('Avatar URLs for the object reviewer.', 'woocommerce'), |
|
907 | 907 | 'type' => 'object', |
908 | - 'context' => array( 'view', 'edit' ), |
|
908 | + 'context' => array('view', 'edit'), |
|
909 | 909 | 'readonly' => true, |
910 | 910 | 'properties' => $avatar_properties, |
911 | 911 | ); |
912 | 912 | } |
913 | 913 | |
914 | - return $this->add_additional_fields_schema( $schema ); |
|
914 | + return $this->add_additional_fields_schema($schema); |
|
915 | 915 | } |
916 | 916 | |
917 | 917 | /** |
@@ -924,38 +924,38 @@ discard block |
||
924 | 924 | |
925 | 925 | $params['context']['default'] = 'view'; |
926 | 926 | |
927 | - $params['after'] = array( |
|
928 | - 'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce' ), |
|
927 | + $params['after'] = array( |
|
928 | + 'description' => __('Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce'), |
|
929 | 929 | 'type' => 'string', |
930 | 930 | 'format' => 'date-time', |
931 | 931 | ); |
932 | - $params['before'] = array( |
|
933 | - 'description' => __( 'Limit response to reviews published before a given ISO8601 compliant date.', 'woocommerce' ), |
|
932 | + $params['before'] = array( |
|
933 | + 'description' => __('Limit response to reviews published before a given ISO8601 compliant date.', 'woocommerce'), |
|
934 | 934 | 'type' => 'string', |
935 | 935 | 'format' => 'date-time', |
936 | 936 | ); |
937 | - $params['exclude'] = array( |
|
938 | - 'description' => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ), |
|
937 | + $params['exclude'] = array( |
|
938 | + 'description' => __('Ensure result set excludes specific IDs.', 'woocommerce'), |
|
939 | 939 | 'type' => 'array', |
940 | 940 | 'items' => array( |
941 | 941 | 'type' => 'integer', |
942 | 942 | ), |
943 | 943 | 'default' => array(), |
944 | 944 | ); |
945 | - $params['include'] = array( |
|
946 | - 'description' => __( 'Limit result set to specific IDs.', 'woocommerce' ), |
|
945 | + $params['include'] = array( |
|
946 | + 'description' => __('Limit result set to specific IDs.', 'woocommerce'), |
|
947 | 947 | 'type' => 'array', |
948 | 948 | 'items' => array( |
949 | 949 | 'type' => 'integer', |
950 | 950 | ), |
951 | 951 | 'default' => array(), |
952 | 952 | ); |
953 | - $params['offset'] = array( |
|
954 | - 'description' => __( 'Offset the result set by a specific number of items.', 'woocommerce' ), |
|
953 | + $params['offset'] = array( |
|
954 | + 'description' => __('Offset the result set by a specific number of items.', 'woocommerce'), |
|
955 | 955 | 'type' => 'integer', |
956 | 956 | ); |
957 | - $params['order'] = array( |
|
958 | - 'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ), |
|
957 | + $params['order'] = array( |
|
958 | + 'description' => __('Order sort attribute ascending or descending.', 'woocommerce'), |
|
959 | 959 | 'type' => 'string', |
960 | 960 | 'default' => 'desc', |
961 | 961 | 'enum' => array( |
@@ -963,8 +963,8 @@ discard block |
||
963 | 963 | 'desc', |
964 | 964 | ), |
965 | 965 | ); |
966 | - $params['orderby'] = array( |
|
967 | - 'description' => __( 'Sort collection by object attribute.', 'woocommerce' ), |
|
966 | + $params['orderby'] = array( |
|
967 | + 'description' => __('Sort collection by object attribute.', 'woocommerce'), |
|
968 | 968 | 'type' => 'string', |
969 | 969 | 'default' => 'date_gmt', |
970 | 970 | 'enum' => array( |
@@ -975,37 +975,37 @@ discard block |
||
975 | 975 | 'product', |
976 | 976 | ), |
977 | 977 | ); |
978 | - $params['reviewer'] = array( |
|
979 | - 'description' => __( 'Limit result set to reviews assigned to specific user IDs.', 'woocommerce' ), |
|
978 | + $params['reviewer'] = array( |
|
979 | + 'description' => __('Limit result set to reviews assigned to specific user IDs.', 'woocommerce'), |
|
980 | 980 | 'type' => 'array', |
981 | 981 | 'items' => array( |
982 | 982 | 'type' => 'integer', |
983 | 983 | ), |
984 | 984 | ); |
985 | 985 | $params['reviewer_exclude'] = array( |
986 | - 'description' => __( 'Ensure result set excludes reviews assigned to specific user IDs.', 'woocommerce' ), |
|
986 | + 'description' => __('Ensure result set excludes reviews assigned to specific user IDs.', 'woocommerce'), |
|
987 | 987 | 'type' => 'array', |
988 | 988 | 'items' => array( |
989 | 989 | 'type' => 'integer', |
990 | 990 | ), |
991 | 991 | ); |
992 | - $params['reviewer_email'] = array( |
|
992 | + $params['reviewer_email'] = array( |
|
993 | 993 | 'default' => null, |
994 | - 'description' => __( 'Limit result set to that from a specific author email.', 'woocommerce' ), |
|
994 | + 'description' => __('Limit result set to that from a specific author email.', 'woocommerce'), |
|
995 | 995 | 'format' => 'email', |
996 | 996 | 'type' => 'string', |
997 | 997 | ); |
998 | - $params['product'] = array( |
|
998 | + $params['product'] = array( |
|
999 | 999 | 'default' => array(), |
1000 | - 'description' => __( 'Limit result set to reviews assigned to specific product IDs.', 'woocommerce' ), |
|
1000 | + 'description' => __('Limit result set to reviews assigned to specific product IDs.', 'woocommerce'), |
|
1001 | 1001 | 'type' => 'array', |
1002 | 1002 | 'items' => array( |
1003 | 1003 | 'type' => 'integer', |
1004 | 1004 | ), |
1005 | 1005 | ); |
1006 | - $params['status'] = array( |
|
1006 | + $params['status'] = array( |
|
1007 | 1007 | 'default' => 'approved', |
1008 | - 'description' => __( 'Limit result set to reviews assigned a specific status.', 'woocommerce' ), |
|
1008 | + 'description' => __('Limit result set to reviews assigned a specific status.', 'woocommerce'), |
|
1009 | 1009 | 'sanitize_callback' => 'sanitize_key', |
1010 | 1010 | 'type' => 'string', |
1011 | 1011 | 'enum' => array( |
@@ -1027,7 +1027,7 @@ discard block |
||
1027 | 1027 | * @since 3.5.0 |
1028 | 1028 | * @param array $params JSON Schema-formatted collection parameters. |
1029 | 1029 | */ |
1030 | - return apply_filters( 'woocommerce_rest_product_review_collection_params', $params ); |
|
1030 | + return apply_filters('woocommerce_rest_product_review_collection_params', $params); |
|
1031 | 1031 | } |
1032 | 1032 | |
1033 | 1033 | /** |
@@ -1037,24 +1037,24 @@ discard block |
||
1037 | 1037 | * @param int $id Supplied ID. |
1038 | 1038 | * @return WP_Comment|\WP_Error Comment object if ID is valid, \WP_Error otherwise. |
1039 | 1039 | */ |
1040 | - protected function get_review( $id ) { |
|
1040 | + protected function get_review($id) { |
|
1041 | 1041 | $id = (int) $id; |
1042 | - $error = new \WP_Error( 'woocommerce_rest_review_invalid_id', __( 'Invalid review ID.', 'woocommerce' ), array( 'status' => 404 ) ); |
|
1042 | + $error = new \WP_Error('woocommerce_rest_review_invalid_id', __('Invalid review ID.', 'woocommerce'), array('status' => 404)); |
|
1043 | 1043 | |
1044 | - if ( 0 >= $id ) { |
|
1044 | + if (0 >= $id) { |
|
1045 | 1045 | return $error; |
1046 | 1046 | } |
1047 | 1047 | |
1048 | - $review = get_comment( $id ); |
|
1049 | - if ( empty( $review ) ) { |
|
1048 | + $review = get_comment($id); |
|
1049 | + if (empty($review)) { |
|
1050 | 1050 | return $error; |
1051 | 1051 | } |
1052 | 1052 | |
1053 | - if ( ! empty( $review->comment_post_ID ) ) { |
|
1054 | - $post = get_post( (int) $review->comment_post_ID ); |
|
1053 | + if ( ! empty($review->comment_post_ID)) { |
|
1054 | + $post = get_post((int) $review->comment_post_ID); |
|
1055 | 1055 | |
1056 | - if ( 'product' !== get_post_type( (int) $review->comment_post_ID ) ) { |
|
1057 | - return new \WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) ); |
|
1056 | + if ('product' !== get_post_type((int) $review->comment_post_ID)) { |
|
1057 | + return new \WP_Error('woocommerce_rest_product_invalid_id', __('Invalid product ID.', 'woocommerce'), array('status' => 404)); |
|
1058 | 1058 | } |
1059 | 1059 | } |
1060 | 1060 | |
@@ -1068,10 +1068,10 @@ discard block |
||
1068 | 1068 | * @param string $query_param Query parameter. |
1069 | 1069 | * @return string |
1070 | 1070 | */ |
1071 | - protected function normalize_query_param( $query_param ) { |
|
1071 | + protected function normalize_query_param($query_param) { |
|
1072 | 1072 | $prefix = 'comment_'; |
1073 | 1073 | |
1074 | - switch ( $query_param ) { |
|
1074 | + switch ($query_param) { |
|
1075 | 1075 | case 'id': |
1076 | 1076 | $normalized = $prefix . 'ID'; |
1077 | 1077 | break; |
@@ -1096,8 +1096,8 @@ discard block |
||
1096 | 1096 | * @param string|int $comment_approved comment status. |
1097 | 1097 | * @return string Comment status. |
1098 | 1098 | */ |
1099 | - protected function prepare_status_response( $comment_approved ) { |
|
1100 | - switch ( $comment_approved ) { |
|
1099 | + protected function prepare_status_response($comment_approved) { |
|
1100 | + switch ($comment_approved) { |
|
1101 | 1101 | case 'hold': |
1102 | 1102 | case '0': |
1103 | 1103 | $status = 'hold'; |
@@ -1124,34 +1124,34 @@ discard block |
||
1124 | 1124 | * @param int $id Review ID. |
1125 | 1125 | * @return bool Whether the status was changed. |
1126 | 1126 | */ |
1127 | - protected function handle_status_param( $new_status, $id ) { |
|
1128 | - $old_status = wp_get_comment_status( $id ); |
|
1127 | + protected function handle_status_param($new_status, $id) { |
|
1128 | + $old_status = wp_get_comment_status($id); |
|
1129 | 1129 | |
1130 | - if ( $new_status === $old_status ) { |
|
1130 | + if ($new_status === $old_status) { |
|
1131 | 1131 | return false; |
1132 | 1132 | } |
1133 | 1133 | |
1134 | - switch ( $new_status ) { |
|
1134 | + switch ($new_status) { |
|
1135 | 1135 | case 'approved': |
1136 | 1136 | case 'approve': |
1137 | 1137 | case '1': |
1138 | - $changed = wp_set_comment_status( $id, 'approve' ); |
|
1138 | + $changed = wp_set_comment_status($id, 'approve'); |
|
1139 | 1139 | break; |
1140 | 1140 | case 'hold': |
1141 | 1141 | case '0': |
1142 | - $changed = wp_set_comment_status( $id, 'hold' ); |
|
1142 | + $changed = wp_set_comment_status($id, 'hold'); |
|
1143 | 1143 | break; |
1144 | 1144 | case 'spam': |
1145 | - $changed = wp_spam_comment( $id ); |
|
1145 | + $changed = wp_spam_comment($id); |
|
1146 | 1146 | break; |
1147 | 1147 | case 'unspam': |
1148 | - $changed = wp_unspam_comment( $id ); |
|
1148 | + $changed = wp_unspam_comment($id); |
|
1149 | 1149 | break; |
1150 | 1150 | case 'trash': |
1151 | - $changed = wp_trash_comment( $id ); |
|
1151 | + $changed = wp_trash_comment($id); |
|
1152 | 1152 | break; |
1153 | 1153 | case 'untrash': |
1154 | - $changed = wp_untrash_comment( $id ); |
|
1154 | + $changed = wp_untrash_comment($id); |
|
1155 | 1155 | break; |
1156 | 1156 | default: |
1157 | 1157 | $changed = false; |