@@ -8,7 +8,7 @@ |
||
8 | 8 | * @since 2.6.0 |
9 | 9 | */ |
10 | 10 | |
11 | -defined( 'ABSPATH' ) || exit; |
|
11 | +defined('ABSPATH') || exit; |
|
12 | 12 | |
13 | 13 | /** |
14 | 14 | * REST API Webhooks controller class. |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | * @since 2.6.0 |
9 | 9 | */ |
10 | 10 | |
11 | -defined( 'ABSPATH' ) || exit; |
|
11 | +defined('ABSPATH') || exit; |
|
12 | 12 | |
13 | 13 | /** |
14 | 14 | * REST API Orders controller class. |
@@ -33,27 +33,27 @@ discard block |
||
33 | 33 | * @param WC_Order $order Order data. |
34 | 34 | * @return bool |
35 | 35 | */ |
36 | - protected function calculate_coupons( $request, $order ) { |
|
37 | - if ( ! isset( $request['coupon_lines'] ) || ! is_array( $request['coupon_lines'] ) ) { |
|
36 | + protected function calculate_coupons($request, $order) { |
|
37 | + if ( ! isset($request['coupon_lines']) || ! is_array($request['coupon_lines'])) { |
|
38 | 38 | return false; |
39 | 39 | } |
40 | 40 | |
41 | 41 | // Remove all coupons first to ensure calculation is correct. |
42 | - foreach ( $order->get_items( 'coupon' ) as $coupon ) { |
|
43 | - $order->remove_coupon( $coupon->get_code() ); |
|
42 | + foreach ($order->get_items('coupon') as $coupon) { |
|
43 | + $order->remove_coupon($coupon->get_code()); |
|
44 | 44 | } |
45 | 45 | |
46 | - foreach ( $request['coupon_lines'] as $item ) { |
|
47 | - if ( is_array( $item ) ) { |
|
48 | - if ( empty( $item['id'] ) ) { |
|
49 | - if ( empty( $item['code'] ) ) { |
|
50 | - throw new WC_REST_Exception( 'woocommerce_rest_invalid_coupon', __( 'Coupon code is required.', 'woocommerce' ), 400 ); |
|
46 | + foreach ($request['coupon_lines'] as $item) { |
|
47 | + if (is_array($item)) { |
|
48 | + if (empty($item['id'])) { |
|
49 | + if (empty($item['code'])) { |
|
50 | + throw new WC_REST_Exception('woocommerce_rest_invalid_coupon', __('Coupon code is required.', 'woocommerce'), 400); |
|
51 | 51 | } |
52 | 52 | |
53 | - $results = $order->apply_coupon( wc_clean( $item['code'] ) ); |
|
53 | + $results = $order->apply_coupon(wc_clean($item['code'])); |
|
54 | 54 | |
55 | - if ( is_wp_error( $results ) ) { |
|
56 | - throw new WC_REST_Exception( 'woocommerce_rest_' . $results->get_error_code(), $results->get_error_message(), 400 ); |
|
55 | + if (is_wp_error($results)) { |
|
56 | + throw new WC_REST_Exception('woocommerce_rest_' . $results->get_error_code(), $results->get_error_message(), 400); |
|
57 | 57 | } |
58 | 58 | } |
59 | 59 | } |
@@ -70,51 +70,51 @@ discard block |
||
70 | 70 | * @param bool $creating If is creating a new object. |
71 | 71 | * @return WP_Error|WC_Data |
72 | 72 | */ |
73 | - protected function prepare_object_for_database( $request, $creating = false ) { |
|
74 | - $id = isset( $request['id'] ) ? absint( $request['id'] ) : 0; |
|
75 | - $order = new WC_Order( $id ); |
|
73 | + protected function prepare_object_for_database($request, $creating = false) { |
|
74 | + $id = isset($request['id']) ? absint($request['id']) : 0; |
|
75 | + $order = new WC_Order($id); |
|
76 | 76 | $schema = $this->get_item_schema(); |
77 | - $data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) ); |
|
77 | + $data_keys = array_keys(array_filter($schema['properties'], array($this, 'filter_writable_props'))); |
|
78 | 78 | |
79 | 79 | // Handle all writable props. |
80 | - foreach ( $data_keys as $key ) { |
|
81 | - $value = $request[ $key ]; |
|
80 | + foreach ($data_keys as $key) { |
|
81 | + $value = $request[$key]; |
|
82 | 82 | |
83 | - if ( ! is_null( $value ) ) { |
|
84 | - switch ( $key ) { |
|
83 | + if ( ! is_null($value)) { |
|
84 | + switch ($key) { |
|
85 | 85 | case 'coupon_lines': |
86 | 86 | case 'status': |
87 | 87 | // Change should be done later so transitions have new data. |
88 | 88 | break; |
89 | 89 | case 'billing': |
90 | 90 | case 'shipping': |
91 | - $this->update_address( $order, $value, $key ); |
|
91 | + $this->update_address($order, $value, $key); |
|
92 | 92 | break; |
93 | 93 | case 'line_items': |
94 | 94 | case 'shipping_lines': |
95 | 95 | case 'fee_lines': |
96 | - if ( is_array( $value ) ) { |
|
97 | - foreach ( $value as $item ) { |
|
98 | - if ( is_array( $item ) ) { |
|
99 | - if ( $this->item_is_null( $item ) || ( isset( $item['quantity'] ) && 0 === $item['quantity'] ) ) { |
|
100 | - $order->remove_item( $item['id'] ); |
|
96 | + if (is_array($value)) { |
|
97 | + foreach ($value as $item) { |
|
98 | + if (is_array($item)) { |
|
99 | + if ($this->item_is_null($item) || (isset($item['quantity']) && 0 === $item['quantity'])) { |
|
100 | + $order->remove_item($item['id']); |
|
101 | 101 | } else { |
102 | - $this->set_item( $order, $key, $item ); |
|
102 | + $this->set_item($order, $key, $item); |
|
103 | 103 | } |
104 | 104 | } |
105 | 105 | } |
106 | 106 | } |
107 | 107 | break; |
108 | 108 | case 'meta_data': |
109 | - if ( is_array( $value ) ) { |
|
110 | - foreach ( $value as $meta ) { |
|
111 | - $order->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' ); |
|
109 | + if (is_array($value)) { |
|
110 | + foreach ($value as $meta) { |
|
111 | + $order->update_meta_data($meta['key'], $meta['value'], isset($meta['id']) ? $meta['id'] : ''); |
|
112 | 112 | } |
113 | 113 | } |
114 | 114 | break; |
115 | 115 | default: |
116 | - if ( is_callable( array( $order, "set_{$key}" ) ) ) { |
|
117 | - $order->{"set_{$key}"}( $value ); |
|
116 | + if (is_callable(array($order, "set_{$key}"))) { |
|
117 | + $order->{"set_{$key}"}($value); |
|
118 | 118 | } |
119 | 119 | break; |
120 | 120 | } |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | * @param WP_REST_Request $request Request object. |
132 | 132 | * @param bool $creating If is creating a new object. |
133 | 133 | */ |
134 | - return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $order, $request, $creating ); |
|
134 | + return apply_filters("woocommerce_rest_pre_insert_{$this->post_type}_object", $order, $request, $creating); |
|
135 | 135 | } |
136 | 136 | |
137 | 137 | /** |
@@ -143,62 +143,62 @@ discard block |
||
143 | 143 | * @param bool $creating If is creating a new object. |
144 | 144 | * @return WC_Data|WP_Error |
145 | 145 | */ |
146 | - protected function save_object( $request, $creating = false ) { |
|
146 | + protected function save_object($request, $creating = false) { |
|
147 | 147 | try { |
148 | - $object = $this->prepare_object_for_database( $request, $creating ); |
|
148 | + $object = $this->prepare_object_for_database($request, $creating); |
|
149 | 149 | |
150 | - if ( is_wp_error( $object ) ) { |
|
150 | + if (is_wp_error($object)) { |
|
151 | 151 | return $object; |
152 | 152 | } |
153 | 153 | |
154 | 154 | // Make sure gateways are loaded so hooks from gateways fire on save/create. |
155 | 155 | WC()->payment_gateways(); |
156 | 156 | |
157 | - if ( ! is_null( $request['customer_id'] ) && 0 !== $request['customer_id'] ) { |
|
157 | + if ( ! is_null($request['customer_id']) && 0 !== $request['customer_id']) { |
|
158 | 158 | // Make sure customer exists. |
159 | - if ( false === get_user_by( 'id', $request['customer_id'] ) ) { |
|
160 | - throw new WC_REST_Exception( 'woocommerce_rest_invalid_customer_id', __( 'Customer ID is invalid.', 'woocommerce' ), 400 ); |
|
159 | + if (false === get_user_by('id', $request['customer_id'])) { |
|
160 | + throw new WC_REST_Exception('woocommerce_rest_invalid_customer_id', __('Customer ID is invalid.', 'woocommerce'), 400); |
|
161 | 161 | } |
162 | 162 | |
163 | 163 | // Make sure customer is part of blog. |
164 | - if ( is_multisite() && ! is_user_member_of_blog( $request['customer_id'] ) ) { |
|
165 | - add_user_to_blog( get_current_blog_id(), $request['customer_id'], 'customer' ); |
|
164 | + if (is_multisite() && ! is_user_member_of_blog($request['customer_id'])) { |
|
165 | + add_user_to_blog(get_current_blog_id(), $request['customer_id'], 'customer'); |
|
166 | 166 | } |
167 | 167 | } |
168 | 168 | |
169 | - if ( $creating ) { |
|
170 | - $object->set_created_via( 'rest-api' ); |
|
171 | - $object->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) ); |
|
169 | + if ($creating) { |
|
170 | + $object->set_created_via('rest-api'); |
|
171 | + $object->set_prices_include_tax('yes' === get_option('woocommerce_prices_include_tax')); |
|
172 | 172 | $object->calculate_totals(); |
173 | 173 | } else { |
174 | 174 | // If items have changed, recalculate order totals. |
175 | - if ( isset( $request['billing'] ) || isset( $request['shipping'] ) || isset( $request['line_items'] ) || isset( $request['shipping_lines'] ) || isset( $request['fee_lines'] ) || isset( $request['coupon_lines'] ) ) { |
|
176 | - $object->calculate_totals( true ); |
|
175 | + if (isset($request['billing']) || isset($request['shipping']) || isset($request['line_items']) || isset($request['shipping_lines']) || isset($request['fee_lines']) || isset($request['coupon_lines'])) { |
|
176 | + $object->calculate_totals(true); |
|
177 | 177 | } |
178 | 178 | } |
179 | 179 | |
180 | 180 | // Set coupons. |
181 | - $this->calculate_coupons( $request, $object ); |
|
181 | + $this->calculate_coupons($request, $object); |
|
182 | 182 | |
183 | 183 | // Set status. |
184 | - if ( ! empty( $request['status'] ) ) { |
|
185 | - $object->set_status( $request['status'] ); |
|
184 | + if ( ! empty($request['status'])) { |
|
185 | + $object->set_status($request['status']); |
|
186 | 186 | } |
187 | 187 | |
188 | 188 | $object->save(); |
189 | 189 | |
190 | 190 | // Actions for after the order is saved. |
191 | - if ( true === $request['set_paid'] ) { |
|
192 | - if ( $creating || $object->needs_payment() ) { |
|
193 | - $object->payment_complete( $request['transaction_id'] ); |
|
191 | + if (true === $request['set_paid']) { |
|
192 | + if ($creating || $object->needs_payment()) { |
|
193 | + $object->payment_complete($request['transaction_id']); |
|
194 | 194 | } |
195 | 195 | } |
196 | 196 | |
197 | - return $this->get_object( $object->get_id() ); |
|
198 | - } catch ( WC_Data_Exception $e ) { |
|
199 | - return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() ); |
|
200 | - } catch ( WC_REST_Exception $e ) { |
|
201 | - return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); |
|
197 | + return $this->get_object($object->get_id()); |
|
198 | + } catch (WC_Data_Exception $e) { |
|
199 | + return new WP_Error($e->getErrorCode(), $e->getMessage(), $e->getErrorData()); |
|
200 | + } catch (WC_REST_Exception $e) { |
|
201 | + return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode())); |
|
202 | 202 | } |
203 | 203 | } |
204 | 204 | |
@@ -209,17 +209,17 @@ discard block |
||
209 | 209 | * @param WP_REST_Request $request Full details about the request. |
210 | 210 | * @return array |
211 | 211 | */ |
212 | - protected function prepare_objects_query( $request ) { |
|
212 | + protected function prepare_objects_query($request) { |
|
213 | 213 | // This is needed to get around an array to string notice in WC_REST_Orders_V2_Controller::prepare_objects_query. |
214 | 214 | $statuses = $request['status']; |
215 | - unset( $request['status'] ); |
|
216 | - $args = parent::prepare_objects_query( $request ); |
|
215 | + unset($request['status']); |
|
216 | + $args = parent::prepare_objects_query($request); |
|
217 | 217 | |
218 | 218 | $args['post_status'] = array(); |
219 | - foreach ( $statuses as $status ) { |
|
220 | - if ( in_array( $status, $this->get_order_statuses(), true ) ) { |
|
219 | + foreach ($statuses as $status) { |
|
220 | + if (in_array($status, $this->get_order_statuses(), true)) { |
|
221 | 221 | $args['post_status'][] = 'wc-' . $status; |
222 | - } elseif ( 'any' === $status ) { |
|
222 | + } elseif ('any' === $status) { |
|
223 | 223 | // Set status to "any" and short-circuit out. |
224 | 224 | $args['post_status'] = 'any'; |
225 | 225 | break; |
@@ -257,11 +257,11 @@ discard block |
||
257 | 257 | |
258 | 258 | $params['status'] = array( |
259 | 259 | 'default' => 'any', |
260 | - 'description' => __( 'Limit result set to orders which have specific statuses.', 'woocommerce' ), |
|
260 | + 'description' => __('Limit result set to orders which have specific statuses.', 'woocommerce'), |
|
261 | 261 | 'type' => 'array', |
262 | 262 | 'items' => array( |
263 | 263 | 'type' => 'string', |
264 | - 'enum' => array_merge( array( 'any', 'trash' ), $this->get_order_statuses() ), |
|
264 | + 'enum' => array_merge(array('any', 'trash'), $this->get_order_statuses()), |
|
265 | 265 | ), |
266 | 266 | 'validate_callback' => 'rest_validate_request_arg', |
267 | 267 | ); |
@@ -8,7 +8,7 @@ |
||
8 | 8 | * @since 2.6.0 |
9 | 9 | */ |
10 | 10 | |
11 | -defined( 'ABSPATH' ) || exit; |
|
11 | +defined('ABSPATH') || exit; |
|
12 | 12 | |
13 | 13 | /** |
14 | 14 | * REST API Taxes controller class. |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | * @since 3.5.0 |
9 | 9 | */ |
10 | 10 | |
11 | -defined( 'ABSPATH' ) || exit; |
|
11 | +defined('ABSPATH') || exit; |
|
12 | 12 | |
13 | 13 | /** |
14 | 14 | * REST API Data controller class. |
@@ -42,10 +42,10 @@ discard block |
||
42 | 42 | $this->namespace, '/' . $this->rest_base, array( |
43 | 43 | array( |
44 | 44 | 'methods' => WP_REST_Server::READABLE, |
45 | - 'callback' => array( $this, 'get_items' ), |
|
46 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
45 | + 'callback' => array($this, 'get_items'), |
|
46 | + 'permission_callback' => array($this, 'get_items_permissions_check'), |
|
47 | 47 | ), |
48 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
48 | + 'schema' => array($this, 'get_public_item_schema'), |
|
49 | 49 | ) |
50 | 50 | ); |
51 | 51 | } |
@@ -56,9 +56,9 @@ discard block |
||
56 | 56 | * @param WP_REST_Request $request Full details about the request. |
57 | 57 | * @return WP_Error|boolean |
58 | 58 | */ |
59 | - public function get_items_permissions_check( $request ) { |
|
60 | - if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) { |
|
61 | - return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
59 | + public function get_items_permissions_check($request) { |
|
60 | + if ( ! wc_rest_check_manager_permissions('settings', 'read')) { |
|
61 | + return new WP_Error('woocommerce_rest_cannot_view', __('Sorry, you cannot list resources.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | return true; |
@@ -70,9 +70,9 @@ discard block |
||
70 | 70 | * @param WP_REST_Request $request Full details about the request. |
71 | 71 | * @return WP_Error|boolean |
72 | 72 | */ |
73 | - public function get_item_permissions_check( $request ) { |
|
74 | - if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) { |
|
75 | - return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
73 | + public function get_item_permissions_check($request) { |
|
74 | + if ( ! wc_rest_check_manager_permissions('settings', 'read')) { |
|
75 | + return new WP_Error('woocommerce_rest_cannot_view', __('Sorry, you cannot view this resource.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
76 | 76 | } |
77 | 77 | |
78 | 78 | return true; |
@@ -85,29 +85,29 @@ discard block |
||
85 | 85 | * @param WP_REST_Request $request Request data. |
86 | 86 | * @return WP_Error|WP_REST_Response |
87 | 87 | */ |
88 | - public function get_items( $request ) { |
|
88 | + public function get_items($request) { |
|
89 | 89 | $data = array(); |
90 | 90 | $resources = array( |
91 | 91 | array( |
92 | 92 | 'slug' => 'continents', |
93 | - 'description' => __( 'List of supported continents, countries, and states.', 'woocommerce' ), |
|
93 | + 'description' => __('List of supported continents, countries, and states.', 'woocommerce'), |
|
94 | 94 | ), |
95 | 95 | array( |
96 | 96 | 'slug' => 'countries', |
97 | - 'description' => __( 'List of supported states in a given country.', 'woocommerce' ), |
|
97 | + 'description' => __('List of supported states in a given country.', 'woocommerce'), |
|
98 | 98 | ), |
99 | 99 | array( |
100 | 100 | 'slug' => 'currencies', |
101 | - 'description' => __( 'List of supported currencies.', 'woocommerce' ), |
|
101 | + 'description' => __('List of supported currencies.', 'woocommerce'), |
|
102 | 102 | ), |
103 | 103 | ); |
104 | 104 | |
105 | - foreach ( $resources as $resource ) { |
|
106 | - $item = $this->prepare_item_for_response( (object) $resource, $request ); |
|
107 | - $data[] = $this->prepare_response_for_collection( $item ); |
|
105 | + foreach ($resources as $resource) { |
|
106 | + $item = $this->prepare_item_for_response((object) $resource, $request); |
|
107 | + $data[] = $this->prepare_response_for_collection($item); |
|
108 | 108 | } |
109 | 109 | |
110 | - return rest_ensure_response( $data ); |
|
110 | + return rest_ensure_response($data); |
|
111 | 111 | } |
112 | 112 | |
113 | 113 | /** |
@@ -117,18 +117,18 @@ discard block |
||
117 | 117 | * @param WP_REST_Request $request Request object. |
118 | 118 | * @return WP_REST_Response $response Response data. |
119 | 119 | */ |
120 | - public function prepare_item_for_response( $resource, $request ) { |
|
120 | + public function prepare_item_for_response($resource, $request) { |
|
121 | 121 | $data = array( |
122 | 122 | 'slug' => $resource->slug, |
123 | 123 | 'description' => $resource->description, |
124 | 124 | ); |
125 | 125 | |
126 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
127 | - $data = $this->filter_response_by_context( $data, 'view' ); |
|
126 | + $data = $this->add_additional_fields_to_object($data, $request); |
|
127 | + $data = $this->filter_response_by_context($data, 'view'); |
|
128 | 128 | |
129 | 129 | // Wrap the data in a response object. |
130 | - $response = rest_ensure_response( $data ); |
|
131 | - $response->add_links( $this->prepare_links( $resource ) ); |
|
130 | + $response = rest_ensure_response($data); |
|
131 | + $response->add_links($this->prepare_links($resource)); |
|
132 | 132 | |
133 | 133 | return $response; |
134 | 134 | } |
@@ -139,13 +139,13 @@ discard block |
||
139 | 139 | * @param object $item Data object. |
140 | 140 | * @return array Links for the given country. |
141 | 141 | */ |
142 | - protected function prepare_links( $item ) { |
|
142 | + protected function prepare_links($item) { |
|
143 | 143 | $links = array( |
144 | 144 | 'self' => array( |
145 | - 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $item->slug ) ), |
|
145 | + 'href' => rest_url(sprintf('/%s/%s/%s', $this->namespace, $this->rest_base, $item->slug)), |
|
146 | 146 | ), |
147 | 147 | 'collection' => array( |
148 | - 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), |
|
148 | + 'href' => rest_url(sprintf('%s/%s', $this->namespace, $this->rest_base)), |
|
149 | 149 | ), |
150 | 150 | ); |
151 | 151 | |
@@ -165,20 +165,20 @@ discard block |
||
165 | 165 | 'type' => 'object', |
166 | 166 | 'properties' => array( |
167 | 167 | 'slug' => array( |
168 | - 'description' => __( 'Data resource ID.', 'woocommerce' ), |
|
168 | + 'description' => __('Data resource ID.', 'woocommerce'), |
|
169 | 169 | 'type' => 'string', |
170 | - 'context' => array( 'view' ), |
|
170 | + 'context' => array('view'), |
|
171 | 171 | 'readonly' => true, |
172 | 172 | ), |
173 | 173 | 'description' => array( |
174 | - 'description' => __( 'Data resource description.', 'woocommerce' ), |
|
174 | + 'description' => __('Data resource description.', 'woocommerce'), |
|
175 | 175 | 'type' => 'string', |
176 | - 'context' => array( 'view' ), |
|
176 | + 'context' => array('view'), |
|
177 | 177 | 'readonly' => true, |
178 | 178 | ), |
179 | 179 | ), |
180 | 180 | ); |
181 | 181 | |
182 | - return $this->add_additional_fields_schema( $schema ); |
|
182 | + return $this->add_additional_fields_schema($schema); |
|
183 | 183 | } |
184 | 184 | } |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | * @since 2.6.0 |
9 | 9 | */ |
10 | 10 | |
11 | -defined( 'ABSPATH' ) || exit; |
|
11 | +defined('ABSPATH') || exit; |
|
12 | 12 | |
13 | 13 | /** |
14 | 14 | * REST API Product Categories controller class. |
@@ -32,12 +32,12 @@ discard block |
||
32 | 32 | * @param WP_REST_Request $request Request instance. |
33 | 33 | * @return WP_REST_Response |
34 | 34 | */ |
35 | - public function prepare_item_for_response( $item, $request ) { |
|
35 | + public function prepare_item_for_response($item, $request) { |
|
36 | 36 | // Get category display type. |
37 | - $display_type = get_term_meta( $item->term_id, 'display_type', true ); |
|
37 | + $display_type = get_term_meta($item->term_id, 'display_type', true); |
|
38 | 38 | |
39 | 39 | // Get category order. |
40 | - $menu_order = get_term_meta( $item->term_id, 'order', true ); |
|
40 | + $menu_order = get_term_meta($item->term_id, 'order', true); |
|
41 | 41 | |
42 | 42 | $data = array( |
43 | 43 | 'id' => (int) $item->term_id, |
@@ -52,29 +52,29 @@ discard block |
||
52 | 52 | ); |
53 | 53 | |
54 | 54 | // Get category image. |
55 | - $image_id = get_term_meta( $item->term_id, 'thumbnail_id', true ); |
|
56 | - if ( $image_id ) { |
|
57 | - $attachment = get_post( $image_id ); |
|
55 | + $image_id = get_term_meta($item->term_id, 'thumbnail_id', true); |
|
56 | + if ($image_id) { |
|
57 | + $attachment = get_post($image_id); |
|
58 | 58 | |
59 | 59 | $data['image'] = array( |
60 | 60 | 'id' => (int) $image_id, |
61 | - 'date_created' => wc_rest_prepare_date_response( $attachment->post_date ), |
|
62 | - 'date_created_gmt' => wc_rest_prepare_date_response( $attachment->post_date_gmt ), |
|
63 | - 'date_modified' => wc_rest_prepare_date_response( $attachment->post_modified ), |
|
64 | - 'date_modified_gmt' => wc_rest_prepare_date_response( $attachment->post_modified_gmt ), |
|
65 | - 'src' => wp_get_attachment_url( $image_id ), |
|
66 | - 'name' => get_the_title( $attachment ), |
|
67 | - 'alt' => get_post_meta( $image_id, '_wp_attachment_image_alt', true ), |
|
61 | + 'date_created' => wc_rest_prepare_date_response($attachment->post_date), |
|
62 | + 'date_created_gmt' => wc_rest_prepare_date_response($attachment->post_date_gmt), |
|
63 | + 'date_modified' => wc_rest_prepare_date_response($attachment->post_modified), |
|
64 | + 'date_modified_gmt' => wc_rest_prepare_date_response($attachment->post_modified_gmt), |
|
65 | + 'src' => wp_get_attachment_url($image_id), |
|
66 | + 'name' => get_the_title($attachment), |
|
67 | + 'alt' => get_post_meta($image_id, '_wp_attachment_image_alt', true), |
|
68 | 68 | ); |
69 | 69 | } |
70 | 70 | |
71 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
72 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
73 | - $data = $this->filter_response_by_context( $data, $context ); |
|
71 | + $context = ! empty($request['context']) ? $request['context'] : 'view'; |
|
72 | + $data = $this->add_additional_fields_to_object($data, $request); |
|
73 | + $data = $this->filter_response_by_context($data, $context); |
|
74 | 74 | |
75 | - $response = rest_ensure_response( $data ); |
|
75 | + $response = rest_ensure_response($data); |
|
76 | 76 | |
77 | - $response->add_links( $this->prepare_links( $item, $request ) ); |
|
77 | + $response->add_links($this->prepare_links($item, $request)); |
|
78 | 78 | |
79 | 79 | /** |
80 | 80 | * Filter a term item returned from the API. |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | * @param object $item The original term object. |
86 | 86 | * @param WP_REST_Request $request Request used to generate the response. |
87 | 87 | */ |
88 | - return apply_filters( "woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request ); |
|
88 | + return apply_filters("woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request); |
|
89 | 89 | } |
90 | 90 | |
91 | 91 | /** |
@@ -100,114 +100,114 @@ discard block |
||
100 | 100 | 'type' => 'object', |
101 | 101 | 'properties' => array( |
102 | 102 | 'id' => array( |
103 | - 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
|
103 | + 'description' => __('Unique identifier for the resource.', 'woocommerce'), |
|
104 | 104 | 'type' => 'integer', |
105 | - 'context' => array( 'view', 'edit' ), |
|
105 | + 'context' => array('view', 'edit'), |
|
106 | 106 | 'readonly' => true, |
107 | 107 | ), |
108 | 108 | 'name' => array( |
109 | - 'description' => __( 'Category name.', 'woocommerce' ), |
|
109 | + 'description' => __('Category name.', 'woocommerce'), |
|
110 | 110 | 'type' => 'string', |
111 | - 'context' => array( 'view', 'edit' ), |
|
111 | + 'context' => array('view', 'edit'), |
|
112 | 112 | 'arg_options' => array( |
113 | 113 | 'sanitize_callback' => 'sanitize_text_field', |
114 | 114 | ), |
115 | 115 | ), |
116 | 116 | 'slug' => array( |
117 | - 'description' => __( 'An alphanumeric identifier for the resource unique to its type.', 'woocommerce' ), |
|
117 | + 'description' => __('An alphanumeric identifier for the resource unique to its type.', 'woocommerce'), |
|
118 | 118 | 'type' => 'string', |
119 | - 'context' => array( 'view', 'edit' ), |
|
119 | + 'context' => array('view', 'edit'), |
|
120 | 120 | 'arg_options' => array( |
121 | 121 | 'sanitize_callback' => 'sanitize_title', |
122 | 122 | ), |
123 | 123 | ), |
124 | 124 | 'parent' => array( |
125 | - 'description' => __( 'The ID for the parent of the resource.', 'woocommerce' ), |
|
125 | + 'description' => __('The ID for the parent of the resource.', 'woocommerce'), |
|
126 | 126 | 'type' => 'integer', |
127 | - 'context' => array( 'view', 'edit' ), |
|
127 | + 'context' => array('view', 'edit'), |
|
128 | 128 | ), |
129 | 129 | 'description' => array( |
130 | - 'description' => __( 'HTML description of the resource.', 'woocommerce' ), |
|
130 | + 'description' => __('HTML description of the resource.', 'woocommerce'), |
|
131 | 131 | 'type' => 'string', |
132 | - 'context' => array( 'view', 'edit' ), |
|
132 | + 'context' => array('view', 'edit'), |
|
133 | 133 | 'arg_options' => array( |
134 | 134 | 'sanitize_callback' => 'wp_filter_post_kses', |
135 | 135 | ), |
136 | 136 | ), |
137 | 137 | 'display' => array( |
138 | - 'description' => __( 'Category archive display type.', 'woocommerce' ), |
|
138 | + 'description' => __('Category archive display type.', 'woocommerce'), |
|
139 | 139 | 'type' => 'string', |
140 | 140 | 'default' => 'default', |
141 | - 'enum' => array( 'default', 'products', 'subcategories', 'both' ), |
|
142 | - 'context' => array( 'view', 'edit' ), |
|
141 | + 'enum' => array('default', 'products', 'subcategories', 'both'), |
|
142 | + 'context' => array('view', 'edit'), |
|
143 | 143 | ), |
144 | 144 | 'image' => array( |
145 | - 'description' => __( 'Image data.', 'woocommerce' ), |
|
145 | + 'description' => __('Image data.', 'woocommerce'), |
|
146 | 146 | 'type' => 'object', |
147 | - 'context' => array( 'view', 'edit' ), |
|
147 | + 'context' => array('view', 'edit'), |
|
148 | 148 | 'properties' => array( |
149 | 149 | 'id' => array( |
150 | - 'description' => __( 'Image ID.', 'woocommerce' ), |
|
150 | + 'description' => __('Image ID.', 'woocommerce'), |
|
151 | 151 | 'type' => 'integer', |
152 | - 'context' => array( 'view', 'edit' ), |
|
152 | + 'context' => array('view', 'edit'), |
|
153 | 153 | ), |
154 | 154 | 'date_created' => array( |
155 | - 'description' => __( "The date the image was created, in the site's timezone.", 'woocommerce' ), |
|
155 | + 'description' => __("The date the image was created, in the site's timezone.", 'woocommerce'), |
|
156 | 156 | 'type' => 'date-time', |
157 | - 'context' => array( 'view', 'edit' ), |
|
157 | + 'context' => array('view', 'edit'), |
|
158 | 158 | 'readonly' => true, |
159 | 159 | ), |
160 | 160 | 'date_created_gmt' => array( |
161 | - 'description' => __( 'The date the image was created, as GMT.', 'woocommerce' ), |
|
161 | + 'description' => __('The date the image was created, as GMT.', 'woocommerce'), |
|
162 | 162 | 'type' => 'date-time', |
163 | - 'context' => array( 'view', 'edit' ), |
|
163 | + 'context' => array('view', 'edit'), |
|
164 | 164 | 'readonly' => true, |
165 | 165 | ), |
166 | 166 | 'date_modified' => array( |
167 | - 'description' => __( "The date the image was last modified, in the site's timezone.", 'woocommerce' ), |
|
167 | + 'description' => __("The date the image was last modified, in the site's timezone.", 'woocommerce'), |
|
168 | 168 | 'type' => 'date-time', |
169 | - 'context' => array( 'view', 'edit' ), |
|
169 | + 'context' => array('view', 'edit'), |
|
170 | 170 | 'readonly' => true, |
171 | 171 | ), |
172 | 172 | 'date_modified_gmt' => array( |
173 | - 'description' => __( 'The date the image was last modified, as GMT.', 'woocommerce' ), |
|
173 | + 'description' => __('The date the image was last modified, as GMT.', 'woocommerce'), |
|
174 | 174 | 'type' => 'date-time', |
175 | - 'context' => array( 'view', 'edit' ), |
|
175 | + 'context' => array('view', 'edit'), |
|
176 | 176 | 'readonly' => true, |
177 | 177 | ), |
178 | 178 | 'src' => array( |
179 | - 'description' => __( 'Image URL.', 'woocommerce' ), |
|
179 | + 'description' => __('Image URL.', 'woocommerce'), |
|
180 | 180 | 'type' => 'string', |
181 | 181 | 'format' => 'uri', |
182 | - 'context' => array( 'view', 'edit' ), |
|
182 | + 'context' => array('view', 'edit'), |
|
183 | 183 | ), |
184 | 184 | 'name' => array( |
185 | - 'description' => __( 'Image name.', 'woocommerce' ), |
|
185 | + 'description' => __('Image name.', 'woocommerce'), |
|
186 | 186 | 'type' => 'string', |
187 | - 'context' => array( 'view', 'edit' ), |
|
187 | + 'context' => array('view', 'edit'), |
|
188 | 188 | ), |
189 | 189 | 'alt' => array( |
190 | - 'description' => __( 'Image alternative text.', 'woocommerce' ), |
|
190 | + 'description' => __('Image alternative text.', 'woocommerce'), |
|
191 | 191 | 'type' => 'string', |
192 | - 'context' => array( 'view', 'edit' ), |
|
192 | + 'context' => array('view', 'edit'), |
|
193 | 193 | ), |
194 | 194 | ), |
195 | 195 | ), |
196 | 196 | 'menu_order' => array( |
197 | - 'description' => __( 'Menu order, used to custom sort the resource.', 'woocommerce' ), |
|
197 | + 'description' => __('Menu order, used to custom sort the resource.', 'woocommerce'), |
|
198 | 198 | 'type' => 'integer', |
199 | - 'context' => array( 'view', 'edit' ), |
|
199 | + 'context' => array('view', 'edit'), |
|
200 | 200 | ), |
201 | 201 | 'count' => array( |
202 | - 'description' => __( 'Number of published products for the resource.', 'woocommerce' ), |
|
202 | + 'description' => __('Number of published products for the resource.', 'woocommerce'), |
|
203 | 203 | 'type' => 'integer', |
204 | - 'context' => array( 'view', 'edit' ), |
|
204 | + 'context' => array('view', 'edit'), |
|
205 | 205 | 'readonly' => true, |
206 | 206 | ), |
207 | 207 | ), |
208 | 208 | ); |
209 | 209 | |
210 | - return $this->add_additional_fields_schema( $schema ); |
|
210 | + return $this->add_additional_fields_schema($schema); |
|
211 | 211 | } |
212 | 212 | |
213 | 213 | /** |
@@ -219,50 +219,50 @@ discard block |
||
219 | 219 | * |
220 | 220 | * @since 3.5.5 |
221 | 221 | */ |
222 | - protected function update_term_meta_fields( $term, $request ) { |
|
222 | + protected function update_term_meta_fields($term, $request) { |
|
223 | 223 | $id = (int) $term->term_id; |
224 | 224 | |
225 | - if ( isset( $request['display'] ) ) { |
|
226 | - update_term_meta( $id, 'display_type', 'default' === $request['display'] ? '' : $request['display'] ); |
|
225 | + if (isset($request['display'])) { |
|
226 | + update_term_meta($id, 'display_type', 'default' === $request['display'] ? '' : $request['display']); |
|
227 | 227 | } |
228 | 228 | |
229 | - if ( isset( $request['menu_order'] ) ) { |
|
230 | - update_term_meta( $id, 'order', $request['menu_order'] ); |
|
229 | + if (isset($request['menu_order'])) { |
|
230 | + update_term_meta($id, 'order', $request['menu_order']); |
|
231 | 231 | } |
232 | 232 | |
233 | - if ( isset( $request['image'] ) ) { |
|
234 | - if ( empty( $request['image']['id'] ) && ! empty( $request['image']['src'] ) ) { |
|
235 | - $upload = wc_rest_upload_image_from_url( esc_url_raw( $request['image']['src'] ) ); |
|
233 | + if (isset($request['image'])) { |
|
234 | + if (empty($request['image']['id']) && ! empty($request['image']['src'])) { |
|
235 | + $upload = wc_rest_upload_image_from_url(esc_url_raw($request['image']['src'])); |
|
236 | 236 | |
237 | - if ( is_wp_error( $upload ) ) { |
|
237 | + if (is_wp_error($upload)) { |
|
238 | 238 | return $upload; |
239 | 239 | } |
240 | 240 | |
241 | - $image_id = wc_rest_set_uploaded_image_as_attachment( $upload ); |
|
241 | + $image_id = wc_rest_set_uploaded_image_as_attachment($upload); |
|
242 | 242 | } else { |
243 | - $image_id = isset( $request['image']['id'] ) ? absint( $request['image']['id'] ) : 0; |
|
243 | + $image_id = isset($request['image']['id']) ? absint($request['image']['id']) : 0; |
|
244 | 244 | } |
245 | 245 | |
246 | 246 | // Check if image_id is a valid image attachment before updating the term meta. |
247 | - if ( $image_id && wp_attachment_is_image( $image_id ) ) { |
|
248 | - update_term_meta( $id, 'thumbnail_id', $image_id ); |
|
247 | + if ($image_id && wp_attachment_is_image($image_id)) { |
|
248 | + update_term_meta($id, 'thumbnail_id', $image_id); |
|
249 | 249 | |
250 | 250 | // Set the image alt. |
251 | - if ( ! empty( $request['image']['alt'] ) ) { |
|
252 | - update_post_meta( $image_id, '_wp_attachment_image_alt', wc_clean( $request['image']['alt'] ) ); |
|
251 | + if ( ! empty($request['image']['alt'])) { |
|
252 | + update_post_meta($image_id, '_wp_attachment_image_alt', wc_clean($request['image']['alt'])); |
|
253 | 253 | } |
254 | 254 | |
255 | 255 | // Set the image title. |
256 | - if ( ! empty( $request['image']['name'] ) ) { |
|
256 | + if ( ! empty($request['image']['name'])) { |
|
257 | 257 | wp_update_post( |
258 | 258 | array( |
259 | 259 | 'ID' => $image_id, |
260 | - 'post_title' => wc_clean( $request['image']['name'] ), |
|
260 | + 'post_title' => wc_clean($request['image']['name']), |
|
261 | 261 | ) |
262 | 262 | ); |
263 | 263 | } |
264 | 264 | } else { |
265 | - delete_term_meta( $id, 'thumbnail_id' ); |
|
265 | + delete_term_meta($id, 'thumbnail_id'); |
|
266 | 266 | } |
267 | 267 | } |
268 | 268 |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | * @since 3.5.0 |
9 | 9 | */ |
10 | 10 | |
11 | -defined( 'ABSPATH' ) || exit; |
|
11 | +defined('ABSPATH') || exit; |
|
12 | 12 | |
13 | 13 | /** |
14 | 14 | * REST API Reports Orders Totals controller class. |
@@ -39,16 +39,16 @@ discard block |
||
39 | 39 | * @return array |
40 | 40 | */ |
41 | 41 | protected function get_reports() { |
42 | - $totals = wp_count_posts( 'shop_order' ); |
|
42 | + $totals = wp_count_posts('shop_order'); |
|
43 | 43 | $data = array(); |
44 | 44 | |
45 | - foreach ( wc_get_order_statuses() as $slug => $name ) { |
|
46 | - if ( ! isset( $totals->$slug ) ) { |
|
45 | + foreach (wc_get_order_statuses() as $slug => $name) { |
|
46 | + if ( ! isset($totals->$slug)) { |
|
47 | 47 | continue; |
48 | 48 | } |
49 | 49 | |
50 | 50 | $data[] = array( |
51 | - 'slug' => str_replace( 'wc-', '', $slug ), |
|
51 | + 'slug' => str_replace('wc-', '', $slug), |
|
52 | 52 | 'name' => $name, |
53 | 53 | 'total' => (int) $totals->$slug, |
54 | 54 | ); |
@@ -64,19 +64,19 @@ discard block |
||
64 | 64 | * @param WP_REST_Request $request Request object. |
65 | 65 | * @return WP_REST_Response $response Response data. |
66 | 66 | */ |
67 | - public function prepare_item_for_response( $report, $request ) { |
|
67 | + public function prepare_item_for_response($report, $request) { |
|
68 | 68 | $data = array( |
69 | 69 | 'slug' => $report->slug, |
70 | 70 | 'name' => $report->name, |
71 | 71 | 'total' => $report->total, |
72 | 72 | ); |
73 | 73 | |
74 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
75 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
76 | - $data = $this->filter_response_by_context( $data, $context ); |
|
74 | + $context = ! empty($request['context']) ? $request['context'] : 'view'; |
|
75 | + $data = $this->add_additional_fields_to_object($data, $request); |
|
76 | + $data = $this->filter_response_by_context($data, $context); |
|
77 | 77 | |
78 | 78 | // Wrap the data in a response object. |
79 | - $response = rest_ensure_response( $data ); |
|
79 | + $response = rest_ensure_response($data); |
|
80 | 80 | |
81 | 81 | /** |
82 | 82 | * Filter a report returned from the API. |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | * @param object $report The original report object. |
88 | 88 | * @param WP_REST_Request $request Request used to generate the response. |
89 | 89 | */ |
90 | - return apply_filters( 'woocommerce_rest_prepare_report_orders_count', $response, $report, $request ); |
|
90 | + return apply_filters('woocommerce_rest_prepare_report_orders_count', $response, $report, $request); |
|
91 | 91 | } |
92 | 92 | |
93 | 93 | /** |
@@ -102,26 +102,26 @@ discard block |
||
102 | 102 | 'type' => 'object', |
103 | 103 | 'properties' => array( |
104 | 104 | 'slug' => array( |
105 | - 'description' => __( 'An alphanumeric identifier for the resource.', 'woocommerce' ), |
|
105 | + 'description' => __('An alphanumeric identifier for the resource.', 'woocommerce'), |
|
106 | 106 | 'type' => 'string', |
107 | - 'context' => array( 'view' ), |
|
107 | + 'context' => array('view'), |
|
108 | 108 | 'readonly' => true, |
109 | 109 | ), |
110 | 110 | 'name' => array( |
111 | - 'description' => __( 'Order status name.', 'woocommerce' ), |
|
111 | + 'description' => __('Order status name.', 'woocommerce'), |
|
112 | 112 | 'type' => 'string', |
113 | - 'context' => array( 'view' ), |
|
113 | + 'context' => array('view'), |
|
114 | 114 | 'readonly' => true, |
115 | 115 | ), |
116 | 116 | 'total' => array( |
117 | - 'description' => __( 'Amount of orders.', 'woocommerce' ), |
|
117 | + 'description' => __('Amount of orders.', 'woocommerce'), |
|
118 | 118 | 'type' => 'string', |
119 | - 'context' => array( 'view' ), |
|
119 | + 'context' => array('view'), |
|
120 | 120 | 'readonly' => true, |
121 | 121 | ), |
122 | 122 | ), |
123 | 123 | ); |
124 | 124 | |
125 | - return $this->add_additional_fields_schema( $schema ); |
|
125 | + return $this->add_additional_fields_schema($schema); |
|
126 | 126 | } |
127 | 127 | } |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | * @since 2.6.0 |
9 | 9 | */ |
10 | 10 | |
11 | -defined( 'ABSPATH' ) || exit; |
|
11 | +defined('ABSPATH') || exit; |
|
12 | 12 | |
13 | 13 | /** |
14 | 14 | * REST API Order Refunds controller class. |
@@ -33,15 +33,15 @@ discard block |
||
33 | 33 | * @param bool $creating If is creating a new object. |
34 | 34 | * @return WP_Error|WC_Data The prepared item, or WP_Error object on failure. |
35 | 35 | */ |
36 | - protected function prepare_object_for_database( $request, $creating = false ) { |
|
37 | - $order = wc_get_order( (int) $request['order_id'] ); |
|
36 | + protected function prepare_object_for_database($request, $creating = false) { |
|
37 | + $order = wc_get_order((int) $request['order_id']); |
|
38 | 38 | |
39 | - if ( ! $order ) { |
|
40 | - return new WP_Error( 'woocommerce_rest_invalid_order_id', __( 'Invalid order ID.', 'woocommerce' ), 404 ); |
|
39 | + if ( ! $order) { |
|
40 | + return new WP_Error('woocommerce_rest_invalid_order_id', __('Invalid order ID.', 'woocommerce'), 404); |
|
41 | 41 | } |
42 | 42 | |
43 | - if ( 0 > $request['amount'] ) { |
|
44 | - return new WP_Error( 'woocommerce_rest_invalid_order_refund', __( 'Refund amount must be greater than zero.', 'woocommerce' ), 400 ); |
|
43 | + if (0 > $request['amount']) { |
|
44 | + return new WP_Error('woocommerce_rest_invalid_order_refund', __('Refund amount must be greater than zero.', 'woocommerce'), 400); |
|
45 | 45 | } |
46 | 46 | |
47 | 47 | // Create the refund. |
@@ -49,24 +49,24 @@ discard block |
||
49 | 49 | array( |
50 | 50 | 'order_id' => $order->get_id(), |
51 | 51 | 'amount' => $request['amount'], |
52 | - 'reason' => empty( $request['reason'] ) ? null : $request['reason'], |
|
53 | - 'line_items' => empty( $request['line_items'] ) ? array() : $request['line_items'], |
|
54 | - 'refund_payment' => is_bool( $request['api_refund'] ) ? $request['api_refund'] : true, |
|
52 | + 'reason' => empty($request['reason']) ? null : $request['reason'], |
|
53 | + 'line_items' => empty($request['line_items']) ? array() : $request['line_items'], |
|
54 | + 'refund_payment' => is_bool($request['api_refund']) ? $request['api_refund'] : true, |
|
55 | 55 | 'restock_items' => true, |
56 | 56 | ) |
57 | 57 | ); |
58 | 58 | |
59 | - if ( is_wp_error( $refund ) ) { |
|
60 | - return new WP_Error( 'woocommerce_rest_cannot_create_order_refund', $refund->get_error_message(), 500 ); |
|
59 | + if (is_wp_error($refund)) { |
|
60 | + return new WP_Error('woocommerce_rest_cannot_create_order_refund', $refund->get_error_message(), 500); |
|
61 | 61 | } |
62 | 62 | |
63 | - if ( ! $refund ) { |
|
64 | - return new WP_Error( 'woocommerce_rest_cannot_create_order_refund', __( 'Cannot create order refund, please try again.', 'woocommerce' ), 500 ); |
|
63 | + if ( ! $refund) { |
|
64 | + return new WP_Error('woocommerce_rest_cannot_create_order_refund', __('Cannot create order refund, please try again.', 'woocommerce'), 500); |
|
65 | 65 | } |
66 | 66 | |
67 | - if ( ! empty( $request['meta_data'] ) && is_array( $request['meta_data'] ) ) { |
|
68 | - foreach ( $request['meta_data'] as $meta ) { |
|
69 | - $refund->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' ); |
|
67 | + if ( ! empty($request['meta_data']) && is_array($request['meta_data'])) { |
|
68 | + foreach ($request['meta_data'] as $meta) { |
|
69 | + $refund->update_meta_data($meta['key'], $meta['value'], isset($meta['id']) ? $meta['id'] : ''); |
|
70 | 70 | } |
71 | 71 | $refund->save_meta_data(); |
72 | 72 | } |
@@ -81,6 +81,6 @@ discard block |
||
81 | 81 | * @param WP_REST_Request $request Request object. |
82 | 82 | * @param bool $creating If is creating a new object. |
83 | 83 | */ |
84 | - return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $refund, $request, $creating ); |
|
84 | + return apply_filters("woocommerce_rest_pre_insert_{$this->post_type}_object", $refund, $request, $creating); |
|
85 | 85 | } |
86 | 86 | } |
@@ -8,7 +8,7 @@ |
||
8 | 8 | * @since 2.6.0 |
9 | 9 | */ |
10 | 10 | |
11 | -defined( 'ABSPATH' ) || exit; |
|
11 | +defined('ABSPATH') || exit; |
|
12 | 12 | |
13 | 13 | /** |
14 | 14 | * REST API Product Attribute Terms controller class. |
@@ -6,7 +6,7 @@ discard block |
||
6 | 6 | * @package WooCommerce/RestApi |
7 | 7 | */ |
8 | 8 | |
9 | -if ( ! defined( 'ABSPATH' ) ) { |
|
9 | +if ( ! defined('ABSPATH')) { |
|
10 | 10 | exit; |
11 | 11 | } |
12 | 12 | |
@@ -52,9 +52,9 @@ discard block |
||
52 | 52 | * @param WP_REST_Request $request Full details about the request. |
53 | 53 | * @return WP_Error|boolean |
54 | 54 | */ |
55 | - public function get_items_permissions_check( $request ) { |
|
56 | - if ( ! wc_rest_check_post_permissions( $this->post_type, 'read' ) ) { |
|
57 | - return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
55 | + public function get_items_permissions_check($request) { |
|
56 | + if ( ! wc_rest_check_post_permissions($this->post_type, 'read')) { |
|
57 | + return new WP_Error('woocommerce_rest_cannot_view', __('Sorry, you cannot list resources.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
58 | 58 | } |
59 | 59 | |
60 | 60 | return true; |
@@ -66,9 +66,9 @@ discard block |
||
66 | 66 | * @param WP_REST_Request $request Full details about the request. |
67 | 67 | * @return WP_Error|boolean |
68 | 68 | */ |
69 | - public function create_item_permissions_check( $request ) { |
|
70 | - if ( ! wc_rest_check_post_permissions( $this->post_type, 'create' ) ) { |
|
71 | - return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
69 | + public function create_item_permissions_check($request) { |
|
70 | + if ( ! wc_rest_check_post_permissions($this->post_type, 'create')) { |
|
71 | + return new WP_Error('woocommerce_rest_cannot_create', __('Sorry, you are not allowed to create resources.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | return true; |
@@ -80,11 +80,11 @@ discard block |
||
80 | 80 | * @param WP_REST_Request $request Full details about the request. |
81 | 81 | * @return WP_Error|boolean |
82 | 82 | */ |
83 | - public function get_item_permissions_check( $request ) { |
|
84 | - $post = get_post( (int) $request['id'] ); |
|
83 | + public function get_item_permissions_check($request) { |
|
84 | + $post = get_post((int) $request['id']); |
|
85 | 85 | |
86 | - if ( $post && ! wc_rest_check_post_permissions( $this->post_type, 'read', $post->ID ) ) { |
|
87 | - return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
86 | + if ($post && ! wc_rest_check_post_permissions($this->post_type, 'read', $post->ID)) { |
|
87 | + return new WP_Error('woocommerce_rest_cannot_view', __('Sorry, you cannot view this resource.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
88 | 88 | } |
89 | 89 | |
90 | 90 | return true; |
@@ -96,11 +96,11 @@ discard block |
||
96 | 96 | * @param WP_REST_Request $request Full details about the request. |
97 | 97 | * @return WP_Error|boolean |
98 | 98 | */ |
99 | - public function update_item_permissions_check( $request ) { |
|
100 | - $post = get_post( (int) $request['id'] ); |
|
99 | + public function update_item_permissions_check($request) { |
|
100 | + $post = get_post((int) $request['id']); |
|
101 | 101 | |
102 | - if ( $post && ! wc_rest_check_post_permissions( $this->post_type, 'edit', $post->ID ) ) { |
|
103 | - return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
102 | + if ($post && ! wc_rest_check_post_permissions($this->post_type, 'edit', $post->ID)) { |
|
103 | + return new WP_Error('woocommerce_rest_cannot_edit', __('Sorry, you are not allowed to edit this resource.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
104 | 104 | } |
105 | 105 | |
106 | 106 | return true; |
@@ -112,11 +112,11 @@ discard block |
||
112 | 112 | * @param WP_REST_Request $request Full details about the request. |
113 | 113 | * @return bool|WP_Error |
114 | 114 | */ |
115 | - public function delete_item_permissions_check( $request ) { |
|
116 | - $post = get_post( (int) $request['id'] ); |
|
115 | + public function delete_item_permissions_check($request) { |
|
116 | + $post = get_post((int) $request['id']); |
|
117 | 117 | |
118 | - if ( $post && ! wc_rest_check_post_permissions( $this->post_type, 'delete', $post->ID ) ) { |
|
119 | - return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
118 | + if ($post && ! wc_rest_check_post_permissions($this->post_type, 'delete', $post->ID)) { |
|
119 | + return new WP_Error('woocommerce_rest_cannot_delete', __('Sorry, you are not allowed to delete this resource.', 'woocommerce'), array('status' => rest_authorization_required_code())); |
|
120 | 120 | } |
121 | 121 | |
122 | 122 | return true; |
@@ -129,9 +129,9 @@ discard block |
||
129 | 129 | * |
130 | 130 | * @return boolean|WP_Error |
131 | 131 | */ |
132 | - public function batch_items_permissions_check( $request ) { |
|
133 | - if ( ! wc_rest_check_post_permissions( $this->post_type, 'batch' ) ) { |
|
134 | - 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() ) ); |
|
132 | + public function batch_items_permissions_check($request) { |
|
133 | + if ( ! wc_rest_check_post_permissions($this->post_type, 'batch')) { |
|
134 | + 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())); |
|
135 | 135 | } |
136 | 136 | |
137 | 137 | return true; |
@@ -143,21 +143,21 @@ discard block |
||
143 | 143 | * @param WP_REST_Request $request Full details about the request. |
144 | 144 | * @return WP_Error|WP_REST_Response |
145 | 145 | */ |
146 | - public function get_item( $request ) { |
|
146 | + public function get_item($request) { |
|
147 | 147 | $id = (int) $request['id']; |
148 | - $post = get_post( $id ); |
|
148 | + $post = get_post($id); |
|
149 | 149 | |
150 | - if ( ! empty( $post->post_type ) && 'product_variation' === $post->post_type && 'product' === $this->post_type ) { |
|
151 | - return new WP_Error( "woocommerce_rest_invalid_{$this->post_type}_id", __( 'To manipulate product variations you should use the /products/<product_id>/variations/<id> endpoint.', 'woocommerce' ), array( 'status' => 404 ) ); |
|
152 | - } elseif ( empty( $id ) || empty( $post->ID ) || $post->post_type !== $this->post_type ) { |
|
153 | - return new WP_Error( "woocommerce_rest_invalid_{$this->post_type}_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) ); |
|
150 | + if ( ! empty($post->post_type) && 'product_variation' === $post->post_type && 'product' === $this->post_type) { |
|
151 | + return new WP_Error("woocommerce_rest_invalid_{$this->post_type}_id", __('To manipulate product variations you should use the /products/<product_id>/variations/<id> endpoint.', 'woocommerce'), array('status' => 404)); |
|
152 | + } elseif (empty($id) || empty($post->ID) || $post->post_type !== $this->post_type) { |
|
153 | + return new WP_Error("woocommerce_rest_invalid_{$this->post_type}_id", __('Invalid ID.', 'woocommerce'), array('status' => 404)); |
|
154 | 154 | } |
155 | 155 | |
156 | - $data = $this->prepare_item_for_response( $post, $request ); |
|
157 | - $response = rest_ensure_response( $data ); |
|
156 | + $data = $this->prepare_item_for_response($post, $request); |
|
157 | + $response = rest_ensure_response($data); |
|
158 | 158 | |
159 | - if ( $this->public ) { |
|
160 | - $response->link_header( 'alternate', get_permalink( $id ), array( 'type' => 'text/html' ) ); |
|
159 | + if ($this->public) { |
|
160 | + $response->link_header('alternate', get_permalink($id), array('type' => 'text/html')); |
|
161 | 161 | } |
162 | 162 | |
163 | 163 | return $response; |
@@ -169,39 +169,39 @@ discard block |
||
169 | 169 | * @param WP_REST_Request $request Full details about the request. |
170 | 170 | * @return WP_Error|WP_REST_Response |
171 | 171 | */ |
172 | - public function create_item( $request ) { |
|
173 | - if ( ! empty( $request['id'] ) ) { |
|
172 | + public function create_item($request) { |
|
173 | + if ( ! empty($request['id'])) { |
|
174 | 174 | /* translators: %s: post type */ |
175 | - return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) ); |
|
175 | + return new WP_Error("woocommerce_rest_{$this->post_type}_exists", sprintf(__('Cannot create existing %s.', 'woocommerce'), $this->post_type), array('status' => 400)); |
|
176 | 176 | } |
177 | 177 | |
178 | - $post = $this->prepare_item_for_database( $request ); |
|
179 | - if ( is_wp_error( $post ) ) { |
|
178 | + $post = $this->prepare_item_for_database($request); |
|
179 | + if (is_wp_error($post)) { |
|
180 | 180 | return $post; |
181 | 181 | } |
182 | 182 | |
183 | 183 | $post->post_type = $this->post_type; |
184 | - $post_id = wp_insert_post( $post, true ); |
|
184 | + $post_id = wp_insert_post($post, true); |
|
185 | 185 | |
186 | - if ( is_wp_error( $post_id ) ) { |
|
186 | + if (is_wp_error($post_id)) { |
|
187 | 187 | |
188 | - if ( in_array( $post_id->get_error_code(), array( 'db_insert_error' ) ) ) { |
|
189 | - $post_id->add_data( array( 'status' => 500 ) ); |
|
188 | + if (in_array($post_id->get_error_code(), array('db_insert_error'))) { |
|
189 | + $post_id->add_data(array('status' => 500)); |
|
190 | 190 | } else { |
191 | - $post_id->add_data( array( 'status' => 400 ) ); |
|
191 | + $post_id->add_data(array('status' => 400)); |
|
192 | 192 | } |
193 | 193 | return $post_id; |
194 | 194 | } |
195 | 195 | $post->ID = $post_id; |
196 | - $post = get_post( $post_id ); |
|
196 | + $post = get_post($post_id); |
|
197 | 197 | |
198 | - $this->update_additional_fields_for_object( $post, $request ); |
|
198 | + $this->update_additional_fields_for_object($post, $request); |
|
199 | 199 | |
200 | 200 | // Add meta fields. |
201 | - $meta_fields = $this->add_post_meta_fields( $post, $request ); |
|
202 | - if ( is_wp_error( $meta_fields ) ) { |
|
201 | + $meta_fields = $this->add_post_meta_fields($post, $request); |
|
202 | + if (is_wp_error($meta_fields)) { |
|
203 | 203 | // Remove post. |
204 | - $this->delete_post( $post ); |
|
204 | + $this->delete_post($post); |
|
205 | 205 | |
206 | 206 | return $meta_fields; |
207 | 207 | } |
@@ -213,13 +213,13 @@ discard block |
||
213 | 213 | * @param WP_REST_Request $request Request object. |
214 | 214 | * @param boolean $creating True when creating item, false when updating. |
215 | 215 | */ |
216 | - do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, true ); |
|
216 | + do_action("woocommerce_rest_insert_{$this->post_type}", $post, $request, true); |
|
217 | 217 | |
218 | - $request->set_param( 'context', 'edit' ); |
|
219 | - $response = $this->prepare_item_for_response( $post, $request ); |
|
220 | - $response = rest_ensure_response( $response ); |
|
221 | - $response->set_status( 201 ); |
|
222 | - $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post_id ) ) ); |
|
218 | + $request->set_param('context', 'edit'); |
|
219 | + $response = $this->prepare_item_for_response($post, $request); |
|
220 | + $response = rest_ensure_response($response); |
|
221 | + $response->set_status(201); |
|
222 | + $response->header('Location', rest_url(sprintf('/%s/%s/%d', $this->namespace, $this->rest_base, $post_id))); |
|
223 | 223 | |
224 | 224 | return $response; |
225 | 225 | } |
@@ -231,7 +231,7 @@ discard block |
||
231 | 231 | * @param WP_REST_Request $request WP_REST_Request Object. |
232 | 232 | * @return bool|WP_Error |
233 | 233 | */ |
234 | - protected function add_post_meta_fields( $post, $request ) { |
|
234 | + protected function add_post_meta_fields($post, $request) { |
|
235 | 235 | return true; |
236 | 236 | } |
237 | 237 | |
@@ -240,8 +240,8 @@ discard block |
||
240 | 240 | * |
241 | 241 | * @param WP_Post $post Post object. |
242 | 242 | */ |
243 | - protected function delete_post( $post ) { |
|
244 | - wp_delete_post( $post->ID, true ); |
|
243 | + protected function delete_post($post) { |
|
244 | + wp_delete_post($post->ID, true); |
|
245 | 245 | } |
246 | 246 | |
247 | 247 | /** |
@@ -250,37 +250,37 @@ discard block |
||
250 | 250 | * @param WP_REST_Request $request Full details about the request. |
251 | 251 | * @return WP_Error|WP_REST_Response |
252 | 252 | */ |
253 | - public function update_item( $request ) { |
|
253 | + public function update_item($request) { |
|
254 | 254 | $id = (int) $request['id']; |
255 | - $post = get_post( $id ); |
|
255 | + $post = get_post($id); |
|
256 | 256 | |
257 | - if ( ! empty( $post->post_type ) && 'product_variation' === $post->post_type && 'product' === $this->post_type ) { |
|
258 | - return new WP_Error( "woocommerce_rest_invalid_{$this->post_type}_id", __( 'To manipulate product variations you should use the /products/<product_id>/variations/<id> endpoint.', 'woocommerce' ), array( 'status' => 404 ) ); |
|
259 | - } elseif ( empty( $id ) || empty( $post->ID ) || $post->post_type !== $this->post_type ) { |
|
260 | - return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) ); |
|
257 | + if ( ! empty($post->post_type) && 'product_variation' === $post->post_type && 'product' === $this->post_type) { |
|
258 | + return new WP_Error("woocommerce_rest_invalid_{$this->post_type}_id", __('To manipulate product variations you should use the /products/<product_id>/variations/<id> endpoint.', 'woocommerce'), array('status' => 404)); |
|
259 | + } elseif (empty($id) || empty($post->ID) || $post->post_type !== $this->post_type) { |
|
260 | + return new WP_Error("woocommerce_rest_{$this->post_type}_invalid_id", __('ID is invalid.', 'woocommerce'), array('status' => 400)); |
|
261 | 261 | } |
262 | 262 | |
263 | - $post = $this->prepare_item_for_database( $request ); |
|
264 | - if ( is_wp_error( $post ) ) { |
|
263 | + $post = $this->prepare_item_for_database($request); |
|
264 | + if (is_wp_error($post)) { |
|
265 | 265 | return $post; |
266 | 266 | } |
267 | 267 | // Convert the post object to an array, otherwise wp_update_post will expect non-escaped input. |
268 | - $post_id = wp_update_post( (array) $post, true ); |
|
269 | - if ( is_wp_error( $post_id ) ) { |
|
270 | - if ( in_array( $post_id->get_error_code(), array( 'db_update_error' ) ) ) { |
|
271 | - $post_id->add_data( array( 'status' => 500 ) ); |
|
268 | + $post_id = wp_update_post((array) $post, true); |
|
269 | + if (is_wp_error($post_id)) { |
|
270 | + if (in_array($post_id->get_error_code(), array('db_update_error'))) { |
|
271 | + $post_id->add_data(array('status' => 500)); |
|
272 | 272 | } else { |
273 | - $post_id->add_data( array( 'status' => 400 ) ); |
|
273 | + $post_id->add_data(array('status' => 400)); |
|
274 | 274 | } |
275 | 275 | return $post_id; |
276 | 276 | } |
277 | 277 | |
278 | - $post = get_post( $post_id ); |
|
279 | - $this->update_additional_fields_for_object( $post, $request ); |
|
278 | + $post = get_post($post_id); |
|
279 | + $this->update_additional_fields_for_object($post, $request); |
|
280 | 280 | |
281 | 281 | // Update meta fields. |
282 | - $meta_fields = $this->update_post_meta_fields( $post, $request ); |
|
283 | - if ( is_wp_error( $meta_fields ) ) { |
|
282 | + $meta_fields = $this->update_post_meta_fields($post, $request); |
|
283 | + if (is_wp_error($meta_fields)) { |
|
284 | 284 | return $meta_fields; |
285 | 285 | } |
286 | 286 | |
@@ -291,11 +291,11 @@ discard block |
||
291 | 291 | * @param WP_REST_Request $request Request object. |
292 | 292 | * @param boolean $creating True when creating item, false when updating. |
293 | 293 | */ |
294 | - do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, false ); |
|
294 | + do_action("woocommerce_rest_insert_{$this->post_type}", $post, $request, false); |
|
295 | 295 | |
296 | - $request->set_param( 'context', 'edit' ); |
|
297 | - $response = $this->prepare_item_for_response( $post, $request ); |
|
298 | - return rest_ensure_response( $response ); |
|
296 | + $request->set_param('context', 'edit'); |
|
297 | + $response = $this->prepare_item_for_response($post, $request); |
|
298 | + return rest_ensure_response($response); |
|
299 | 299 | } |
300 | 300 | |
301 | 301 | /** |
@@ -304,7 +304,7 @@ discard block |
||
304 | 304 | * @param WP_REST_Request $request Full details about the request. |
305 | 305 | * @return WP_Error|WP_REST_Response |
306 | 306 | */ |
307 | - public function get_items( $request ) { |
|
307 | + public function get_items($request) { |
|
308 | 308 | $args = array(); |
309 | 309 | $args['offset'] = $request['offset']; |
310 | 310 | $args['order'] = $request['order']; |
@@ -320,19 +320,19 @@ discard block |
||
320 | 320 | |
321 | 321 | $args['date_query'] = array(); |
322 | 322 | // Set before into date query. Date query must be specified as an array of an array. |
323 | - if ( isset( $request['before'] ) ) { |
|
323 | + if (isset($request['before'])) { |
|
324 | 324 | $args['date_query'][0]['before'] = $request['before']; |
325 | 325 | } |
326 | 326 | |
327 | 327 | // Set after into date query. Date query must be specified as an array of an array. |
328 | - if ( isset( $request['after'] ) ) { |
|
328 | + if (isset($request['after'])) { |
|
329 | 329 | $args['date_query'][0]['after'] = $request['after']; |
330 | 330 | } |
331 | 331 | |
332 | - if ( 'wc/v1' === $this->namespace ) { |
|
333 | - if ( is_array( $request['filter'] ) ) { |
|
334 | - $args = array_merge( $args, $request['filter'] ); |
|
335 | - unset( $args['filter'] ); |
|
332 | + if ('wc/v1' === $this->namespace) { |
|
333 | + if (is_array($request['filter'])) { |
|
334 | + $args = array_merge($args, $request['filter']); |
|
335 | + unset($args['filter']); |
|
336 | 336 | } |
337 | 337 | } |
338 | 338 | |
@@ -348,59 +348,59 @@ discard block |
||
348 | 348 | * @param array $args Key value array of query var to query value. |
349 | 349 | * @param WP_REST_Request $request The request used. |
350 | 350 | */ |
351 | - $args = apply_filters( "woocommerce_rest_{$this->post_type}_query", $args, $request ); |
|
352 | - $query_args = $this->prepare_items_query( $args, $request ); |
|
351 | + $args = apply_filters("woocommerce_rest_{$this->post_type}_query", $args, $request); |
|
352 | + $query_args = $this->prepare_items_query($args, $request); |
|
353 | 353 | |
354 | 354 | $posts_query = new WP_Query(); |
355 | - $query_result = $posts_query->query( $query_args ); |
|
355 | + $query_result = $posts_query->query($query_args); |
|
356 | 356 | |
357 | 357 | $posts = array(); |
358 | - foreach ( $query_result as $post ) { |
|
359 | - if ( ! wc_rest_check_post_permissions( $this->post_type, 'read', $post->ID ) ) { |
|
358 | + foreach ($query_result as $post) { |
|
359 | + if ( ! wc_rest_check_post_permissions($this->post_type, 'read', $post->ID)) { |
|
360 | 360 | continue; |
361 | 361 | } |
362 | 362 | |
363 | - $data = $this->prepare_item_for_response( $post, $request ); |
|
364 | - $posts[] = $this->prepare_response_for_collection( $data ); |
|
363 | + $data = $this->prepare_item_for_response($post, $request); |
|
364 | + $posts[] = $this->prepare_response_for_collection($data); |
|
365 | 365 | } |
366 | 366 | |
367 | 367 | $page = (int) $query_args['paged']; |
368 | 368 | $total_posts = $posts_query->found_posts; |
369 | 369 | |
370 | - if ( $total_posts < 1 ) { |
|
370 | + if ($total_posts < 1) { |
|
371 | 371 | // Out-of-bounds, run the query again without LIMIT for total count. |
372 | - unset( $query_args['paged'] ); |
|
372 | + unset($query_args['paged']); |
|
373 | 373 | $count_query = new WP_Query(); |
374 | - $count_query->query( $query_args ); |
|
374 | + $count_query->query($query_args); |
|
375 | 375 | $total_posts = $count_query->found_posts; |
376 | 376 | } |
377 | 377 | |
378 | - $max_pages = ceil( $total_posts / (int) $query_args['posts_per_page'] ); |
|
378 | + $max_pages = ceil($total_posts / (int) $query_args['posts_per_page']); |
|
379 | 379 | |
380 | - $response = rest_ensure_response( $posts ); |
|
381 | - $response->header( 'X-WP-Total', (int) $total_posts ); |
|
382 | - $response->header( 'X-WP-TotalPages', (int) $max_pages ); |
|
380 | + $response = rest_ensure_response($posts); |
|
381 | + $response->header('X-WP-Total', (int) $total_posts); |
|
382 | + $response->header('X-WP-TotalPages', (int) $max_pages); |
|
383 | 383 | |
384 | 384 | $request_params = $request->get_query_params(); |
385 | - if ( ! empty( $request_params['filter'] ) ) { |
|
385 | + if ( ! empty($request_params['filter'])) { |
|
386 | 386 | // Normalize the pagination params. |
387 | - unset( $request_params['filter']['posts_per_page'] ); |
|
388 | - unset( $request_params['filter']['paged'] ); |
|
387 | + unset($request_params['filter']['posts_per_page']); |
|
388 | + unset($request_params['filter']['paged']); |
|
389 | 389 | } |
390 | - $base = add_query_arg( $request_params, rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) ); |
|
390 | + $base = add_query_arg($request_params, rest_url(sprintf('/%s/%s', $this->namespace, $this->rest_base))); |
|
391 | 391 | |
392 | - if ( $page > 1 ) { |
|
392 | + if ($page > 1) { |
|
393 | 393 | $prev_page = $page - 1; |
394 | - if ( $prev_page > $max_pages ) { |
|
394 | + if ($prev_page > $max_pages) { |
|
395 | 395 | $prev_page = $max_pages; |
396 | 396 | } |
397 | - $prev_link = add_query_arg( 'page', $prev_page, $base ); |
|
398 | - $response->link_header( 'prev', $prev_link ); |
|
397 | + $prev_link = add_query_arg('page', $prev_page, $base); |
|
398 | + $response->link_header('prev', $prev_link); |
|
399 | 399 | } |
400 | - if ( $max_pages > $page ) { |
|
400 | + if ($max_pages > $page) { |
|
401 | 401 | $next_page = $page + 1; |
402 | - $next_link = add_query_arg( 'page', $next_page, $base ); |
|
403 | - $response->link_header( 'next', $next_link ); |
|
402 | + $next_link = add_query_arg('page', $next_page, $base); |
|
403 | + $response->link_header('next', $next_link); |
|
404 | 404 | } |
405 | 405 | |
406 | 406 | return $response; |
@@ -412,13 +412,13 @@ discard block |
||
412 | 412 | * @param WP_REST_Request $request Full details about the request. |
413 | 413 | * @return WP_REST_Response|WP_Error |
414 | 414 | */ |
415 | - public function delete_item( $request ) { |
|
415 | + public function delete_item($request) { |
|
416 | 416 | $id = (int) $request['id']; |
417 | 417 | $force = (bool) $request['force']; |
418 | - $post = get_post( $id ); |
|
418 | + $post = get_post($id); |
|
419 | 419 | |
420 | - if ( empty( $id ) || empty( $post->ID ) || $post->post_type !== $this->post_type ) { |
|
421 | - return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 404 ) ); |
|
420 | + if (empty($id) || empty($post->ID) || $post->post_type !== $this->post_type) { |
|
421 | + return new WP_Error("woocommerce_rest_{$this->post_type}_invalid_id", __('ID is invalid.', 'woocommerce'), array('status' => 404)); |
|
422 | 422 | } |
423 | 423 | |
424 | 424 | $supports_trash = EMPTY_TRASH_DAYS > 0; |
@@ -431,40 +431,40 @@ discard block |
||
431 | 431 | * @param boolean $supports_trash Whether the item type support trashing. |
432 | 432 | * @param WP_Post $post The Post object being considered for trashing support. |
433 | 433 | */ |
434 | - $supports_trash = apply_filters( "woocommerce_rest_{$this->post_type}_trashable", $supports_trash, $post ); |
|
434 | + $supports_trash = apply_filters("woocommerce_rest_{$this->post_type}_trashable", $supports_trash, $post); |
|
435 | 435 | |
436 | - if ( ! wc_rest_check_post_permissions( $this->post_type, 'delete', $post->ID ) ) { |
|
436 | + if ( ! wc_rest_check_post_permissions($this->post_type, 'delete', $post->ID)) { |
|
437 | 437 | /* translators: %s: post type */ |
438 | - 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() ) ); |
|
438 | + 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())); |
|
439 | 439 | } |
440 | 440 | |
441 | - $request->set_param( 'context', 'edit' ); |
|
442 | - $response = $this->prepare_item_for_response( $post, $request ); |
|
441 | + $request->set_param('context', 'edit'); |
|
442 | + $response = $this->prepare_item_for_response($post, $request); |
|
443 | 443 | |
444 | 444 | // If we're forcing, then delete permanently. |
445 | - if ( $force ) { |
|
446 | - $result = wp_delete_post( $id, true ); |
|
445 | + if ($force) { |
|
446 | + $result = wp_delete_post($id, true); |
|
447 | 447 | } else { |
448 | 448 | // If we don't support trashing for this type, error out. |
449 | - if ( ! $supports_trash ) { |
|
449 | + if ( ! $supports_trash) { |
|
450 | 450 | /* translators: %s: post type */ |
451 | - return new WP_Error( 'woocommerce_rest_trash_not_supported', sprintf( __( 'The %s does not support trashing.', 'woocommerce' ), $this->post_type ), array( 'status' => 501 ) ); |
|
451 | + return new WP_Error('woocommerce_rest_trash_not_supported', sprintf(__('The %s does not support trashing.', 'woocommerce'), $this->post_type), array('status' => 501)); |
|
452 | 452 | } |
453 | 453 | |
454 | 454 | // Otherwise, only trash if we haven't already. |
455 | - if ( 'trash' === $post->post_status ) { |
|
455 | + if ('trash' === $post->post_status) { |
|
456 | 456 | /* translators: %s: post type */ |
457 | - return new WP_Error( 'woocommerce_rest_already_trashed', sprintf( __( 'The %s has already been deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 410 ) ); |
|
457 | + return new WP_Error('woocommerce_rest_already_trashed', sprintf(__('The %s has already been deleted.', 'woocommerce'), $this->post_type), array('status' => 410)); |
|
458 | 458 | } |
459 | 459 | |
460 | 460 | // (Note that internally this falls through to `wp_delete_post` if |
461 | 461 | // the trash is disabled.) |
462 | - $result = wp_trash_post( $id ); |
|
462 | + $result = wp_trash_post($id); |
|
463 | 463 | } |
464 | 464 | |
465 | - if ( ! $result ) { |
|
465 | + if ( ! $result) { |
|
466 | 466 | /* translators: %s: post type */ |
467 | - return new WP_Error( 'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 500 ) ); |
|
467 | + return new WP_Error('woocommerce_rest_cannot_delete', sprintf(__('The %s cannot be deleted.', 'woocommerce'), $this->post_type), array('status' => 500)); |
|
468 | 468 | } |
469 | 469 | |
470 | 470 | /** |
@@ -474,7 +474,7 @@ discard block |
||
474 | 474 | * @param WP_REST_Response $response The response data. |
475 | 475 | * @param WP_REST_Request $request The request sent to the API. |
476 | 476 | */ |
477 | - do_action( "woocommerce_rest_delete_{$this->post_type}", $post, $response, $request ); |
|
477 | + do_action("woocommerce_rest_delete_{$this->post_type}", $post, $response, $request); |
|
478 | 478 | |
479 | 479 | return $response; |
480 | 480 | } |
@@ -486,13 +486,13 @@ discard block |
||
486 | 486 | * @param WP_REST_Request $request Request object. |
487 | 487 | * @return array Links for the given post. |
488 | 488 | */ |
489 | - protected function prepare_links( $post, $request ) { |
|
489 | + protected function prepare_links($post, $request) { |
|
490 | 490 | $links = array( |
491 | 491 | 'self' => array( |
492 | - 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post->ID ) ), |
|
492 | + 'href' => rest_url(sprintf('/%s/%s/%d', $this->namespace, $this->rest_base, $post->ID)), |
|
493 | 493 | ), |
494 | 494 | 'collection' => array( |
495 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
495 | + 'href' => rest_url(sprintf('/%s/%s', $this->namespace, $this->rest_base)), |
|
496 | 496 | ), |
497 | 497 | ); |
498 | 498 | |
@@ -507,12 +507,12 @@ discard block |
||
507 | 507 | * @param WP_REST_Request $request Request object. |
508 | 508 | * @return array $query_args |
509 | 509 | */ |
510 | - protected function prepare_items_query( $prepared_args = array(), $request = null ) { |
|
510 | + protected function prepare_items_query($prepared_args = array(), $request = null) { |
|
511 | 511 | |
512 | - $valid_vars = array_flip( $this->get_allowed_query_vars() ); |
|
512 | + $valid_vars = array_flip($this->get_allowed_query_vars()); |
|
513 | 513 | $query_args = array(); |
514 | - foreach ( $valid_vars as $var => $index ) { |
|
515 | - if ( isset( $prepared_args[ $var ] ) ) { |
|
514 | + foreach ($valid_vars as $var => $index) { |
|
515 | + if (isset($prepared_args[$var])) { |
|
516 | 516 | /** |
517 | 517 | * Filter the query_vars used in `get_items` for the constructed query. |
518 | 518 | * |
@@ -520,17 +520,17 @@ discard block |
||
520 | 520 | * |
521 | 521 | * @param mixed $prepared_args[ $var ] The query_var value. |
522 | 522 | */ |
523 | - $query_args[ $var ] = apply_filters( "woocommerce_rest_query_var-{$var}", $prepared_args[ $var ] ); |
|
523 | + $query_args[$var] = apply_filters("woocommerce_rest_query_var-{$var}", $prepared_args[$var]); |
|
524 | 524 | } |
525 | 525 | } |
526 | 526 | |
527 | 527 | $query_args['ignore_sticky_posts'] = true; |
528 | 528 | |
529 | - if ( 'include' === $query_args['orderby'] ) { |
|
529 | + if ('include' === $query_args['orderby']) { |
|
530 | 530 | $query_args['orderby'] = 'post__in'; |
531 | - } elseif ( 'id' === $query_args['orderby'] ) { |
|
531 | + } elseif ('id' === $query_args['orderby']) { |
|
532 | 532 | $query_args['orderby'] = 'ID'; // ID must be capitalized. |
533 | - } elseif ( 'slug' === $query_args['orderby'] ) { |
|
533 | + } elseif ('slug' === $query_args['orderby']) { |
|
534 | 534 | $query_args['orderby'] = 'name'; |
535 | 535 | } |
536 | 536 | |
@@ -552,10 +552,10 @@ discard block |
||
552 | 552 | * |
553 | 553 | * @param array Array of allowed WP_Query query vars. |
554 | 554 | */ |
555 | - $valid_vars = apply_filters( 'query_vars', $wp->public_query_vars ); |
|
555 | + $valid_vars = apply_filters('query_vars', $wp->public_query_vars); |
|
556 | 556 | |
557 | - $post_type_obj = get_post_type_object( $this->post_type ); |
|
558 | - if ( current_user_can( $post_type_obj->cap->edit_posts ) ) { |
|
557 | + $post_type_obj = get_post_type_object($this->post_type); |
|
558 | + if (current_user_can($post_type_obj->cap->edit_posts)) { |
|
559 | 559 | /** |
560 | 560 | * Filter the allowed 'private' query vars for authorized users. |
561 | 561 | * |
@@ -569,8 +569,8 @@ discard block |
||
569 | 569 | * @param array $private_query_vars Array of allowed query vars for authorized users. |
570 | 570 | * } |
571 | 571 | */ |
572 | - $private = apply_filters( 'woocommerce_rest_private_query_vars', $wp->private_query_vars ); |
|
573 | - $valid_vars = array_merge( $valid_vars, $private ); |
|
572 | + $private = apply_filters('woocommerce_rest_private_query_vars', $wp->private_query_vars); |
|
573 | + $valid_vars = array_merge($valid_vars, $private); |
|
574 | 574 | } |
575 | 575 | // Define our own in addition to WP's normal vars. |
576 | 576 | $rest_valid = array( |
@@ -590,7 +590,7 @@ discard block |
||
590 | 590 | 'meta_compare', |
591 | 591 | 'meta_value_num', |
592 | 592 | ); |
593 | - $valid_vars = array_merge( $valid_vars, $rest_valid ); |
|
593 | + $valid_vars = array_merge($valid_vars, $rest_valid); |
|
594 | 594 | |
595 | 595 | /** |
596 | 596 | * Filter allowed query vars for the REST API. |
@@ -605,7 +605,7 @@ discard block |
||
605 | 605 | * @param string $allowed_query_var The query var to allow. |
606 | 606 | * } |
607 | 607 | */ |
608 | - $valid_vars = apply_filters( 'woocommerce_rest_query_vars', $valid_vars ); |
|
608 | + $valid_vars = apply_filters('woocommerce_rest_query_vars', $valid_vars); |
|
609 | 609 | |
610 | 610 | return $valid_vars; |
611 | 611 | } |
@@ -621,19 +621,19 @@ discard block |
||
621 | 621 | $params['context']['default'] = 'view'; |
622 | 622 | |
623 | 623 | $params['after'] = array( |
624 | - 'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce' ), |
|
624 | + 'description' => __('Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce'), |
|
625 | 625 | 'type' => 'string', |
626 | 626 | 'format' => 'date-time', |
627 | 627 | 'validate_callback' => 'rest_validate_request_arg', |
628 | 628 | ); |
629 | 629 | $params['before'] = array( |
630 | - 'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'woocommerce' ), |
|
630 | + 'description' => __('Limit response to resources published before a given ISO8601 compliant date.', 'woocommerce'), |
|
631 | 631 | 'type' => 'string', |
632 | 632 | 'format' => 'date-time', |
633 | 633 | 'validate_callback' => 'rest_validate_request_arg', |
634 | 634 | ); |
635 | 635 | $params['exclude'] = array( |
636 | - 'description' => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ), |
|
636 | + 'description' => __('Ensure result set excludes specific IDs.', 'woocommerce'), |
|
637 | 637 | 'type' => 'array', |
638 | 638 | 'items' => array( |
639 | 639 | 'type' => 'integer', |
@@ -642,7 +642,7 @@ discard block |
||
642 | 642 | 'sanitize_callback' => 'wp_parse_id_list', |
643 | 643 | ); |
644 | 644 | $params['include'] = array( |
645 | - 'description' => __( 'Limit result set to specific ids.', 'woocommerce' ), |
|
645 | + 'description' => __('Limit result set to specific ids.', 'woocommerce'), |
|
646 | 646 | 'type' => 'array', |
647 | 647 | 'items' => array( |
648 | 648 | 'type' => 'integer', |
@@ -651,20 +651,20 @@ discard block |
||
651 | 651 | 'sanitize_callback' => 'wp_parse_id_list', |
652 | 652 | ); |
653 | 653 | $params['offset'] = array( |
654 | - 'description' => __( 'Offset the result set by a specific number of items.', 'woocommerce' ), |
|
654 | + 'description' => __('Offset the result set by a specific number of items.', 'woocommerce'), |
|
655 | 655 | 'type' => 'integer', |
656 | 656 | 'sanitize_callback' => 'absint', |
657 | 657 | 'validate_callback' => 'rest_validate_request_arg', |
658 | 658 | ); |
659 | 659 | $params['order'] = array( |
660 | - 'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ), |
|
660 | + 'description' => __('Order sort attribute ascending or descending.', 'woocommerce'), |
|
661 | 661 | 'type' => 'string', |
662 | 662 | 'default' => 'desc', |
663 | - 'enum' => array( 'asc', 'desc' ), |
|
663 | + 'enum' => array('asc', 'desc'), |
|
664 | 664 | 'validate_callback' => 'rest_validate_request_arg', |
665 | 665 | ); |
666 | 666 | $params['orderby'] = array( |
667 | - 'description' => __( 'Sort collection by object attribute.', 'woocommerce' ), |
|
667 | + 'description' => __('Sort collection by object attribute.', 'woocommerce'), |
|
668 | 668 | 'type' => 'string', |
669 | 669 | 'default' => 'date', |
670 | 670 | 'enum' => array( |
@@ -677,11 +677,11 @@ discard block |
||
677 | 677 | 'validate_callback' => 'rest_validate_request_arg', |
678 | 678 | ); |
679 | 679 | |
680 | - $post_type_obj = get_post_type_object( $this->post_type ); |
|
680 | + $post_type_obj = get_post_type_object($this->post_type); |
|
681 | 681 | |
682 | - if ( isset( $post_type_obj->hierarchical ) && $post_type_obj->hierarchical ) { |
|
682 | + if (isset($post_type_obj->hierarchical) && $post_type_obj->hierarchical) { |
|
683 | 683 | $params['parent'] = array( |
684 | - 'description' => __( 'Limit result set to those of particular parent IDs.', 'woocommerce' ), |
|
684 | + 'description' => __('Limit result set to those of particular parent IDs.', 'woocommerce'), |
|
685 | 685 | 'type' => 'array', |
686 | 686 | 'items' => array( |
687 | 687 | 'type' => 'integer', |
@@ -690,7 +690,7 @@ discard block |
||
690 | 690 | 'default' => array(), |
691 | 691 | ); |
692 | 692 | $params['parent_exclude'] = array( |
693 | - 'description' => __( 'Limit result set to all items except those of a particular parent ID.', 'woocommerce' ), |
|
693 | + 'description' => __('Limit result set to all items except those of a particular parent ID.', 'woocommerce'), |
|
694 | 694 | 'type' => 'array', |
695 | 695 | 'items' => array( |
696 | 696 | 'type' => 'integer', |
@@ -700,10 +700,10 @@ discard block |
||
700 | 700 | ); |
701 | 701 | } |
702 | 702 | |
703 | - if ( 'wc/v1' === $this->namespace ) { |
|
703 | + if ('wc/v1' === $this->namespace) { |
|
704 | 704 | $params['filter'] = array( |
705 | 705 | 'type' => 'object', |
706 | - 'description' => __( 'Use WP Query arguments to modify the response; private query vars require appropriate authorization.', 'woocommerce' ), |
|
706 | + 'description' => __('Use WP Query arguments to modify the response; private query vars require appropriate authorization.', 'woocommerce'), |
|
707 | 707 | ); |
708 | 708 | } |
709 | 709 | |
@@ -717,7 +717,7 @@ discard block |
||
717 | 717 | * @param WP_REST_Request $request Request object. |
718 | 718 | * @return bool|WP_Error |
719 | 719 | */ |
720 | - protected function update_post_meta_fields( $post, $request ) { |
|
720 | + protected function update_post_meta_fields($post, $request) { |
|
721 | 721 | return true; |
722 | 722 | } |
723 | 723 | } |