| Total Complexity | 147 |
| Total Lines | 1768 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Orders often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Orders, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Orders extends AbstractObjectsController { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Route base. |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $rest_base = 'orders'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Post type. |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $post_type = 'shop_order'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * If object is hierarchical. |
||
| 35 | * |
||
| 36 | * @var bool |
||
| 37 | */ |
||
| 38 | protected $hierarchical = true; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Stores the request. |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $request = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Register the routes for orders. |
||
| 49 | */ |
||
| 50 | public function register_routes() { |
||
| 51 | register_rest_route( |
||
| 52 | $this->namespace, |
||
| 53 | '/' . $this->rest_base, |
||
| 54 | array( |
||
| 55 | array( |
||
| 56 | 'methods' => \WP_REST_Server::READABLE, |
||
|
|
|||
| 57 | 'callback' => array( $this, 'get_items' ), |
||
| 58 | 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
||
| 59 | 'args' => $this->get_collection_params(), |
||
| 60 | ), |
||
| 61 | array( |
||
| 62 | 'methods' => \WP_REST_Server::CREATABLE, |
||
| 63 | 'callback' => array( $this, 'create_item' ), |
||
| 64 | 'permission_callback' => array( $this, 'create_item_permissions_check' ), |
||
| 65 | 'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ), |
||
| 66 | ), |
||
| 67 | 'schema' => array( $this, 'get_public_item_schema' ), |
||
| 68 | ), |
||
| 69 | true |
||
| 70 | ); |
||
| 71 | |||
| 72 | register_rest_route( |
||
| 73 | $this->namespace, |
||
| 74 | '/' . $this->rest_base . '/(?P<id>[\d]+)', |
||
| 75 | array( |
||
| 76 | 'args' => array( |
||
| 77 | 'id' => array( |
||
| 78 | 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
||
| 79 | 'type' => 'integer', |
||
| 80 | ), |
||
| 81 | ), |
||
| 82 | array( |
||
| 83 | 'methods' => \WP_REST_Server::READABLE, |
||
| 84 | 'callback' => array( $this, 'get_item' ), |
||
| 85 | 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
||
| 86 | 'args' => array( |
||
| 87 | 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
||
| 88 | ), |
||
| 89 | ), |
||
| 90 | array( |
||
| 91 | 'methods' => \WP_REST_Server::EDITABLE, |
||
| 92 | 'callback' => array( $this, 'update_item' ), |
||
| 93 | 'permission_callback' => array( $this, 'update_item_permissions_check' ), |
||
| 94 | 'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::EDITABLE ), |
||
| 95 | ), |
||
| 96 | array( |
||
| 97 | 'methods' => \WP_REST_Server::DELETABLE, |
||
| 98 | 'callback' => array( $this, 'delete_item' ), |
||
| 99 | 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
||
| 100 | 'args' => array( |
||
| 101 | 'force' => array( |
||
| 102 | 'default' => false, |
||
| 103 | 'type' => 'boolean', |
||
| 104 | 'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ), |
||
| 105 | ), |
||
| 106 | ), |
||
| 107 | ), |
||
| 108 | 'schema' => array( $this, 'get_public_item_schema' ), |
||
| 109 | ), |
||
| 110 | true |
||
| 111 | ); |
||
| 112 | |||
| 113 | register_rest_route( |
||
| 114 | $this->namespace, |
||
| 115 | '/' . $this->rest_base . '/batch', |
||
| 116 | array( |
||
| 117 | array( |
||
| 118 | 'methods' => \WP_REST_Server::EDITABLE, |
||
| 119 | 'callback' => array( $this, 'batch_items' ), |
||
| 120 | 'permission_callback' => array( $this, 'batch_items_permissions_check' ), |
||
| 121 | 'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::EDITABLE ), |
||
| 122 | ), |
||
| 123 | 'schema' => array( $this, 'get_public_batch_schema' ), |
||
| 124 | ), |
||
| 125 | true |
||
| 126 | ); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get object. Return false if object is not of required type. |
||
| 131 | * |
||
| 132 | * @since 3.0.0 |
||
| 133 | * @param int $id Object ID. |
||
| 134 | * @return WC_Data|bool |
||
| 135 | */ |
||
| 136 | protected function get_object( $id ) { |
||
| 137 | $order = wc_get_order( $id ); |
||
| 138 | // In case id is a refund's id (or it's not an order at all), don't expose it via /orders/ path. |
||
| 139 | if ( ! $order || 'shop_order_refund' === $order->get_type() ) { |
||
| 140 | return false; |
||
| 141 | } |
||
| 142 | |||
| 143 | return $order; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Expands an order item to get its data. |
||
| 148 | * |
||
| 149 | * @param WC_Order_item $item Order item data. |
||
| 150 | * @return array |
||
| 151 | */ |
||
| 152 | protected function get_order_item_data( $item ) { |
||
| 153 | $data = $item->get_data(); |
||
| 154 | $format_decimal = array( 'subtotal', 'subtotal_tax', 'total', 'total_tax', 'tax_total', 'shipping_tax_total' ); |
||
| 155 | |||
| 156 | // Format decimal values. |
||
| 157 | foreach ( $format_decimal as $key ) { |
||
| 158 | if ( isset( $data[ $key ] ) ) { |
||
| 159 | $data[ $key ] = wc_format_decimal( $data[ $key ], $this->request['dp'] ); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | // Add SKU and PRICE to products. |
||
| 164 | if ( is_callable( array( $item, 'get_product' ) ) ) { |
||
| 165 | $data['sku'] = $item->get_product() ? $item->get_product()->get_sku() : null; |
||
| 166 | $data['price'] = $item->get_quantity() ? $item->get_total() / $item->get_quantity() : 0; |
||
| 167 | } |
||
| 168 | |||
| 169 | // Format taxes. |
||
| 170 | if ( ! empty( $data['taxes']['total'] ) ) { |
||
| 171 | $taxes = array(); |
||
| 172 | |||
| 173 | foreach ( $data['taxes']['total'] as $tax_rate_id => $tax ) { |
||
| 174 | $taxes[] = array( |
||
| 175 | 'id' => $tax_rate_id, |
||
| 176 | 'total' => $tax, |
||
| 177 | 'subtotal' => isset( $data['taxes']['subtotal'][ $tax_rate_id ] ) ? $data['taxes']['subtotal'][ $tax_rate_id ] : '', |
||
| 178 | ); |
||
| 179 | } |
||
| 180 | $data['taxes'] = $taxes; |
||
| 181 | } elseif ( isset( $data['taxes'] ) ) { |
||
| 182 | $data['taxes'] = array(); |
||
| 183 | } |
||
| 184 | |||
| 185 | // Remove names for coupons, taxes and shipping. |
||
| 186 | if ( isset( $data['code'] ) || isset( $data['rate_code'] ) || isset( $data['method_title'] ) ) { |
||
| 187 | unset( $data['name'] ); |
||
| 188 | } |
||
| 189 | |||
| 190 | // Remove props we don't want to expose. |
||
| 191 | unset( $data['order_id'] ); |
||
| 192 | unset( $data['type'] ); |
||
| 193 | |||
| 194 | return $data; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Get formatted item data. |
||
| 199 | * |
||
| 200 | * @since 3.0.0 |
||
| 201 | * @param WC_Data $object WC_Data instance. |
||
| 202 | * @return array |
||
| 203 | */ |
||
| 204 | protected function get_formatted_item_data( $object ) { |
||
| 205 | $data = $object->get_data(); |
||
| 206 | $format_decimal = array( 'discount_total', 'discount_tax', 'shipping_total', 'shipping_tax', 'shipping_total', 'shipping_tax', 'cart_tax', 'total', 'total_tax' ); |
||
| 207 | $format_date = array( 'date_created', 'date_modified', 'date_completed', 'date_paid' ); |
||
| 208 | $format_line_items = array( 'line_items', 'tax_lines', 'shipping_lines', 'fee_lines', 'coupon_lines' ); |
||
| 209 | |||
| 210 | // Format decimal values. |
||
| 211 | foreach ( $format_decimal as $key ) { |
||
| 212 | $data[ $key ] = wc_format_decimal( $data[ $key ], $this->request['dp'] ); |
||
| 213 | } |
||
| 214 | |||
| 215 | // Format date values. |
||
| 216 | foreach ( $format_date as $key ) { |
||
| 217 | $datetime = $data[ $key ]; |
||
| 218 | $data[ $key ] = wc_rest_prepare_date_response( $datetime, false ); |
||
| 219 | $data[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime ); |
||
| 220 | } |
||
| 221 | |||
| 222 | // Format the order status. |
||
| 223 | $data['status'] = 'wc-' === substr( $data['status'], 0, 3 ) ? substr( $data['status'], 3 ) : $data['status']; |
||
| 224 | |||
| 225 | // Format line items. |
||
| 226 | foreach ( $format_line_items as $key ) { |
||
| 227 | $data[ $key ] = array_values( array_map( array( $this, 'get_order_item_data' ), $data[ $key ] ) ); |
||
| 228 | } |
||
| 229 | |||
| 230 | // Refunds. |
||
| 231 | $data['refunds'] = array(); |
||
| 232 | foreach ( $object->get_refunds() as $refund ) { |
||
| 233 | $data['refunds'][] = array( |
||
| 234 | 'id' => $refund->get_id(), |
||
| 235 | 'reason' => $refund->get_reason() ? $refund->get_reason() : '', |
||
| 236 | 'total' => '-' . wc_format_decimal( $refund->get_amount(), $this->request['dp'] ), |
||
| 237 | ); |
||
| 238 | } |
||
| 239 | |||
| 240 | // Currency symbols. |
||
| 241 | $currency_symbol = get_woocommerce_currency_symbol( $data['currency'] ); |
||
| 242 | $data['currency_symbol'] = html_entity_decode( $currency_symbol ); |
||
| 243 | |||
| 244 | return array( |
||
| 245 | 'id' => $object->get_id(), |
||
| 246 | 'parent_id' => $data['parent_id'], |
||
| 247 | 'number' => $data['number'], |
||
| 248 | 'order_key' => $data['order_key'], |
||
| 249 | 'created_via' => $data['created_via'], |
||
| 250 | 'version' => $data['version'], |
||
| 251 | 'status' => $data['status'], |
||
| 252 | 'currency' => $data['currency'], |
||
| 253 | 'currency_symbol' => $data['currency_symbol'], |
||
| 254 | 'date_created' => $data['date_created'], |
||
| 255 | 'date_created_gmt' => $data['date_created_gmt'], |
||
| 256 | 'date_modified' => $data['date_modified'], |
||
| 257 | 'date_modified_gmt' => $data['date_modified_gmt'], |
||
| 258 | 'discount_total' => $data['discount_total'], |
||
| 259 | 'discount_tax' => $data['discount_tax'], |
||
| 260 | 'shipping_total' => $data['shipping_total'], |
||
| 261 | 'shipping_tax' => $data['shipping_tax'], |
||
| 262 | 'cart_tax' => $data['cart_tax'], |
||
| 263 | 'total' => $data['total'], |
||
| 264 | 'total_tax' => $data['total_tax'], |
||
| 265 | 'prices_include_tax' => $data['prices_include_tax'], |
||
| 266 | 'customer_id' => $data['customer_id'], |
||
| 267 | 'customer_ip_address' => $data['customer_ip_address'], |
||
| 268 | 'customer_user_agent' => $data['customer_user_agent'], |
||
| 269 | 'customer_note' => $data['customer_note'], |
||
| 270 | 'billing' => $data['billing'], |
||
| 271 | 'shipping' => $data['shipping'], |
||
| 272 | 'payment_method' => $data['payment_method'], |
||
| 273 | 'payment_method_title' => $data['payment_method_title'], |
||
| 274 | 'transaction_id' => $data['transaction_id'], |
||
| 275 | 'date_paid' => $data['date_paid'], |
||
| 276 | 'date_paid_gmt' => $data['date_paid_gmt'], |
||
| 277 | 'date_completed' => $data['date_completed'], |
||
| 278 | 'date_completed_gmt' => $data['date_completed_gmt'], |
||
| 279 | 'cart_hash' => $data['cart_hash'], |
||
| 280 | 'meta_data' => $data['meta_data'], |
||
| 281 | 'line_items' => $data['line_items'], |
||
| 282 | 'tax_lines' => $data['tax_lines'], |
||
| 283 | 'shipping_lines' => $data['shipping_lines'], |
||
| 284 | 'fee_lines' => $data['fee_lines'], |
||
| 285 | 'coupon_lines' => $data['coupon_lines'], |
||
| 286 | 'refunds' => $data['refunds'], |
||
| 287 | ); |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Prepare a single order output for response. |
||
| 292 | * |
||
| 293 | * @since 3.0.0 |
||
| 294 | * @param \WC_Data $object Object data. |
||
| 295 | * @param \WP_REST_Request $request Request object. |
||
| 296 | * @return \WP_REST_Response |
||
| 297 | */ |
||
| 298 | public function prepare_object_for_response( $object, $request ) { |
||
| 299 | $this->request = $request; |
||
| 300 | $this->request['dp'] = is_null( $this->request['dp'] ) ? wc_get_price_decimals() : absint( $this->request['dp'] ); |
||
| 301 | $data = $this->get_formatted_item_data( $object ); |
||
| 302 | $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
||
| 303 | $data = $this->add_additional_fields_to_object( $data, $request ); |
||
| 304 | $data = $this->filter_response_by_context( $data, $context ); |
||
| 305 | $response = rest_ensure_response( $data ); |
||
| 306 | $response->add_links( $this->prepare_links( $object, $request ) ); |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Filter the data for a response. |
||
| 310 | * |
||
| 311 | * The dynamic portion of the hook name, $this->post_type, |
||
| 312 | * refers to object type being prepared for the response. |
||
| 313 | * |
||
| 314 | * @param \WP_REST_Response $response The response object. |
||
| 315 | * @param WC_Data $object Object data. |
||
| 316 | * @param \WP_REST_Request $request Request object. |
||
| 317 | */ |
||
| 318 | return apply_filters( "woocommerce_rest_prepare_{$this->post_type}_object", $response, $object, $request ); |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Prepare links for the request. |
||
| 323 | * |
||
| 324 | * @param WC_Data $object Object data. |
||
| 325 | * @param \WP_REST_Request $request Request object. |
||
| 326 | * @return array Links for the given post. |
||
| 327 | */ |
||
| 328 | protected function prepare_links( $object, $request ) { |
||
| 329 | $links = array( |
||
| 330 | 'self' => array( |
||
| 331 | 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ), |
||
| 332 | ), |
||
| 333 | 'collection' => array( |
||
| 334 | 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
||
| 335 | ), |
||
| 336 | ); |
||
| 337 | |||
| 338 | if ( 0 !== (int) $object->get_customer_id() ) { |
||
| 339 | $links['customer'] = array( |
||
| 340 | 'href' => rest_url( sprintf( '/%s/customers/%d', $this->namespace, $object->get_customer_id() ) ), |
||
| 341 | ); |
||
| 342 | } |
||
| 343 | |||
| 344 | if ( 0 !== (int) $object->get_parent_id() ) { |
||
| 345 | $links['up'] = array( |
||
| 346 | 'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $object->get_parent_id() ) ), |
||
| 347 | ); |
||
| 348 | } |
||
| 349 | |||
| 350 | return $links; |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Prepare objects query. |
||
| 355 | * |
||
| 356 | * @since 3.0.0 |
||
| 357 | * @param \WP_REST_Request $request Full details about the request. |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | protected function prepare_objects_query( $request ) { |
||
| 361 | global $wpdb; |
||
| 362 | |||
| 363 | // This is needed to get around an array to string notice in \WC_REST_Orders_V2_Controller::prepare_objects_query. |
||
| 364 | $statuses = $request['status']; |
||
| 365 | unset( $request['status'] ); |
||
| 366 | $args = parent::prepare_objects_query( $request ); |
||
| 367 | |||
| 368 | $args['post_status'] = array(); |
||
| 369 | foreach ( $statuses as $status ) { |
||
| 370 | if ( in_array( $status, $this->get_order_statuses(), true ) ) { |
||
| 371 | $args['post_status'][] = 'wc-' . $status; |
||
| 372 | } elseif ( 'any' === $status ) { |
||
| 373 | // Set status to "any" and short-circuit out. |
||
| 374 | $args['post_status'] = 'any'; |
||
| 375 | break; |
||
| 376 | } else { |
||
| 377 | $args['post_status'][] = $status; |
||
| 378 | } |
||
| 379 | } |
||
| 380 | |||
| 381 | // Put the statuses back for further processing (next/prev links, etc). |
||
| 382 | $request['status'] = $statuses; |
||
| 383 | |||
| 384 | // Customer. |
||
| 385 | if ( isset( $request['customer'] ) ) { |
||
| 386 | if ( ! empty( $args['meta_query'] ) ) { |
||
| 387 | $args['meta_query'] = array(); // WPCS: slow query ok. |
||
| 388 | } |
||
| 389 | |||
| 390 | $args['meta_query'][] = array( |
||
| 391 | 'key' => '_customer_user', |
||
| 392 | 'value' => $request['customer'], |
||
| 393 | 'type' => 'NUMERIC', |
||
| 394 | ); |
||
| 395 | } |
||
| 396 | |||
| 397 | // Search by product. |
||
| 398 | if ( ! empty( $request['product'] ) ) { |
||
| 399 | $order_ids = $wpdb->get_col( |
||
| 400 | $wpdb->prepare( |
||
| 401 | "SELECT order_id |
||
| 402 | FROM {$wpdb->prefix}woocommerce_order_items |
||
| 403 | WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d ) |
||
| 404 | AND order_item_type = 'line_item'", |
||
| 405 | $request['product'] |
||
| 406 | ) |
||
| 407 | ); |
||
| 408 | |||
| 409 | // Force WP_Query return empty if don't found any order. |
||
| 410 | $order_ids = ! empty( $order_ids ) ? $order_ids : array( 0 ); |
||
| 411 | |||
| 412 | $args['post__in'] = $order_ids; |
||
| 413 | } |
||
| 414 | |||
| 415 | // Search. |
||
| 416 | if ( ! empty( $args['s'] ) ) { |
||
| 417 | $order_ids = wc_order_search( $args['s'] ); |
||
| 418 | |||
| 419 | if ( ! empty( $order_ids ) ) { |
||
| 420 | unset( $args['s'] ); |
||
| 421 | $args['post__in'] = array_merge( $order_ids, array( 0 ) ); |
||
| 422 | } |
||
| 423 | } |
||
| 424 | |||
| 425 | // Search by partial order number. |
||
| 426 | if ( ! empty( $request['number'] ) ) { |
||
| 427 | $partial_number = trim( $request['number'] ); |
||
| 428 | $limit = intval( $args['posts_per_page'] ); |
||
| 429 | $order_ids = $wpdb->get_col( |
||
| 430 | $wpdb->prepare( |
||
| 431 | "SELECT ID |
||
| 432 | FROM {$wpdb->prefix}posts |
||
| 433 | WHERE post_type = 'shop_order' |
||
| 434 | AND ID LIKE %s |
||
| 435 | LIMIT %d", |
||
| 436 | $wpdb->esc_like( absint( $partial_number ) ) . '%', |
||
| 437 | $limit |
||
| 438 | ) |
||
| 439 | ); |
||
| 440 | |||
| 441 | // Force \WP_Query return empty if don't found any order. |
||
| 442 | $order_ids = empty( $order_ids ) ? array( 0 ) : $order_ids; |
||
| 443 | $args['post__in'] = $order_ids; |
||
| 444 | } |
||
| 445 | |||
| 446 | return $args; |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Only return writable props from schema. |
||
| 451 | * |
||
| 452 | * @param array $schema Schema. |
||
| 453 | * @return bool |
||
| 454 | */ |
||
| 455 | protected function filter_writable_props( $schema ) { |
||
| 456 | return empty( $schema['readonly'] ); |
||
| 457 | } |
||
| 458 | |||
| 459 | |||
| 460 | /** |
||
| 461 | * Prepare a single order for create or update. |
||
| 462 | * |
||
| 463 | * @throws WC_REST_Exception When fails to set any item. |
||
| 464 | * @param \WP_REST_Request $request Request object. |
||
| 465 | * @param bool $creating If is creating a new object. |
||
| 466 | * @return \WP_Error|WC_Data |
||
| 467 | */ |
||
| 468 | protected function prepare_object_for_database( $request, $creating = false ) { |
||
| 469 | $id = isset( $request['id'] ) ? absint( $request['id'] ) : 0; |
||
| 470 | $order = new \WC_Order( $id ); |
||
| 471 | $schema = $this->get_item_schema(); |
||
| 472 | $data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) ); |
||
| 473 | |||
| 474 | // Handle all writable props. |
||
| 475 | foreach ( $data_keys as $key ) { |
||
| 476 | $value = $request[ $key ]; |
||
| 477 | |||
| 478 | if ( ! is_null( $value ) ) { |
||
| 479 | switch ( $key ) { |
||
| 480 | case 'coupon_lines': |
||
| 481 | case 'status': |
||
| 482 | // Change should be done later so transitions have new data. |
||
| 483 | break; |
||
| 484 | case 'billing': |
||
| 485 | case 'shipping': |
||
| 486 | $this->update_address( $order, $value, $key ); |
||
| 487 | break; |
||
| 488 | case 'line_items': |
||
| 489 | case 'shipping_lines': |
||
| 490 | case 'fee_lines': |
||
| 491 | if ( is_array( $value ) ) { |
||
| 492 | foreach ( $value as $item ) { |
||
| 493 | if ( is_array( $item ) ) { |
||
| 494 | if ( $this->item_is_null( $item ) || ( isset( $item['quantity'] ) && 0 === $item['quantity'] ) ) { |
||
| 495 | $order->remove_item( $item['id'] ); |
||
| 496 | } else { |
||
| 497 | $this->set_item( $order, $key, $item ); |
||
| 498 | } |
||
| 499 | } |
||
| 500 | } |
||
| 501 | } |
||
| 502 | break; |
||
| 503 | case 'meta_data': |
||
| 504 | if ( is_array( $value ) ) { |
||
| 505 | foreach ( $value as $meta ) { |
||
| 506 | $order->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' ); |
||
| 507 | } |
||
| 508 | } |
||
| 509 | break; |
||
| 510 | default: |
||
| 511 | if ( is_callable( array( $order, "set_{$key}" ) ) ) { |
||
| 512 | $order->{"set_{$key}"}( $value ); |
||
| 513 | } |
||
| 514 | break; |
||
| 515 | } |
||
| 516 | } |
||
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Filters an object before it is inserted via the REST API. |
||
| 521 | * |
||
| 522 | * The dynamic portion of the hook name, `$this->post_type`, |
||
| 523 | * refers to the object type slug. |
||
| 524 | * |
||
| 525 | * @param WC_Data $order Object object. |
||
| 526 | * @param \WP_REST_Request $request Request object. |
||
| 527 | * @param bool $creating If is creating a new object. |
||
| 528 | */ |
||
| 529 | return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $order, $request, $creating ); |
||
| 530 | } |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Save an object data. |
||
| 534 | * * |
||
| 535 | * @param \WP_REST_Request $request Full details about the request. |
||
| 536 | * @param bool $creating If is creating a new object. |
||
| 537 | * @return WC_Data|\WP_Error |
||
| 538 | * @throws \WC_REST_Exception But all errors are validated before returning any data. |
||
| 539 | */ |
||
| 540 | protected function save_object( $request, $creating = false ) { |
||
| 541 | try { |
||
| 542 | $object = $this->prepare_object_for_database( $request, $creating ); |
||
| 543 | |||
| 544 | if ( is_wp_error( $object ) ) { |
||
| 545 | return $object; |
||
| 546 | } |
||
| 547 | |||
| 548 | // Make sure gateways are loaded so hooks from gateways fire on save/create. |
||
| 549 | WC()->payment_gateways(); |
||
| 550 | |||
| 551 | if ( ! is_null( $request['customer_id'] ) && 0 !== $request['customer_id'] ) { |
||
| 552 | // Make sure customer exists. |
||
| 553 | if ( false === get_user_by( 'id', $request['customer_id'] ) ) { |
||
| 554 | throw new \WC_REST_Exception( 'woocommerce_rest_invalid_customer_id', __( 'Customer ID is invalid.', 'woocommerce' ), 400 ); |
||
| 555 | } |
||
| 556 | |||
| 557 | // Make sure customer is part of blog. |
||
| 558 | if ( is_multisite() && ! is_user_member_of_blog( $request['customer_id'] ) ) { |
||
| 559 | add_user_to_blog( get_current_blog_id(), $request['customer_id'], 'customer' ); |
||
| 560 | } |
||
| 561 | } |
||
| 562 | |||
| 563 | if ( $creating ) { |
||
| 564 | $object->set_created_via( 'rest-api' ); |
||
| 565 | $object->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) ); |
||
| 566 | $object->calculate_totals(); |
||
| 567 | } else { |
||
| 568 | // If items have changed, recalculate order totals. |
||
| 569 | if ( isset( $request['billing'] ) || isset( $request['shipping'] ) || isset( $request['line_items'] ) || isset( $request['shipping_lines'] ) || isset( $request['fee_lines'] ) || isset( $request['coupon_lines'] ) ) { |
||
| 570 | $object->calculate_totals( true ); |
||
| 571 | } |
||
| 572 | } |
||
| 573 | |||
| 574 | // Set coupons. |
||
| 575 | $this->calculate_coupons( $request, $object ); |
||
| 576 | |||
| 577 | // Set status. |
||
| 578 | if ( ! empty( $request['status'] ) ) { |
||
| 579 | $object->set_status( $request['status'] ); |
||
| 580 | } |
||
| 581 | |||
| 582 | $object->save(); |
||
| 583 | |||
| 584 | // Actions for after the order is saved. |
||
| 585 | if ( true === $request['set_paid'] ) { |
||
| 586 | if ( $creating || $object->needs_payment() ) { |
||
| 587 | $object->payment_complete( $request['transaction_id'] ); |
||
| 588 | } |
||
| 589 | } |
||
| 590 | |||
| 591 | return $this->get_object( $object->get_id() ); |
||
| 592 | } catch ( \WC_Data_Exception $e ) { |
||
| 593 | return new \WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() ); |
||
| 594 | } catch ( \WC_REST_Exception $e ) { |
||
| 595 | return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); |
||
| 596 | } |
||
| 597 | } |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Update address. |
||
| 601 | * |
||
| 602 | * @param WC_Order $order Order data. |
||
| 603 | * @param array $posted Posted data. |
||
| 604 | * @param string $type Address type. |
||
| 605 | */ |
||
| 606 | protected function update_address( $order, $posted, $type = 'billing' ) { |
||
| 607 | foreach ( $posted as $key => $value ) { |
||
| 608 | if ( is_callable( array( $order, "set_{$type}_{$key}" ) ) ) { |
||
| 609 | $order->{"set_{$type}_{$key}"}( $value ); |
||
| 610 | } |
||
| 611 | } |
||
| 612 | } |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Gets the product ID from the SKU or posted ID. |
||
| 616 | * |
||
| 617 | * @param array $posted Request data. |
||
| 618 | * @return int |
||
| 619 | * @throws \WC_REST_Exception When SKU or ID is not valid. |
||
| 620 | */ |
||
| 621 | protected function get_product_id( $posted ) { |
||
| 622 | if ( ! empty( $posted['sku'] ) ) { |
||
| 623 | $product_id = (int) wc_get_product_id_by_sku( $posted['sku'] ); |
||
| 624 | } elseif ( ! empty( $posted['product_id'] ) && empty( $posted['variation_id'] ) ) { |
||
| 625 | $product_id = (int) $posted['product_id']; |
||
| 626 | } elseif ( ! empty( $posted['variation_id'] ) ) { |
||
| 627 | $product_id = (int) $posted['variation_id']; |
||
| 628 | } else { |
||
| 629 | throw new \WC_REST_Exception( 'woocommerce_rest_required_product_reference', __( 'Product ID or SKU is required.', 'woocommerce' ), 400 ); |
||
| 630 | } |
||
| 631 | return $product_id; |
||
| 632 | } |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Maybe set an item prop if the value was posted. |
||
| 636 | * |
||
| 637 | * @param WC_Order_Item $item Order item. |
||
| 638 | * @param string $prop Order property. |
||
| 639 | * @param array $posted Request data. |
||
| 640 | */ |
||
| 641 | protected function maybe_set_item_prop( $item, $prop, $posted ) { |
||
| 642 | if ( isset( $posted[ $prop ] ) ) { |
||
| 643 | $item->{"set_$prop"}( $posted[ $prop ] ); |
||
| 644 | } |
||
| 645 | } |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Maybe set item props if the values were posted. |
||
| 649 | * |
||
| 650 | * @param WC_Order_Item $item Order item data. |
||
| 651 | * @param string[] $props Properties. |
||
| 652 | * @param array $posted Request data. |
||
| 653 | */ |
||
| 654 | protected function maybe_set_item_props( $item, $props, $posted ) { |
||
| 655 | foreach ( $props as $prop ) { |
||
| 656 | $this->maybe_set_item_prop( $item, $prop, $posted ); |
||
| 657 | } |
||
| 658 | } |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Maybe set item meta if posted. |
||
| 662 | * |
||
| 663 | * @param WC_Order_Item $item Order item data. |
||
| 664 | * @param array $posted Request data. |
||
| 665 | */ |
||
| 666 | protected function maybe_set_item_meta_data( $item, $posted ) { |
||
| 667 | if ( ! empty( $posted['meta_data'] ) && is_array( $posted['meta_data'] ) ) { |
||
| 668 | foreach ( $posted['meta_data'] as $meta ) { |
||
| 669 | if ( isset( $meta['key'] ) ) { |
||
| 670 | $value = isset( $meta['value'] ) ? $meta['value'] : null; |
||
| 671 | $item->update_meta_data( $meta['key'], $value, isset( $meta['id'] ) ? $meta['id'] : '' ); |
||
| 672 | } |
||
| 673 | } |
||
| 674 | } |
||
| 675 | } |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Create or update a line item. |
||
| 679 | * |
||
| 680 | * @param array $posted Line item data. |
||
| 681 | * @param string $action 'create' to add line item or 'update' to update it. |
||
| 682 | * @param object $item Passed when updating an item. Null during creation. |
||
| 683 | * @return WC_Order_Item_Product |
||
| 684 | * @throws WC_REST_Exception Invalid data, server error. |
||
| 685 | */ |
||
| 686 | protected function prepare_line_items( $posted, $action = 'create', $item = null ) { |
||
| 705 | } |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Create or update an order shipping method. |
||
| 709 | * |
||
| 710 | * @param array $posted $shipping Item data. |
||
| 711 | * @param string $action 'create' to add shipping or 'update' to update it. |
||
| 712 | * @param object $item Passed when updating an item. Null during creation. |
||
| 713 | * @return WC_Order_Item_Shipping |
||
| 714 | * @throws WC_REST_Exception Invalid data, server error. |
||
| 715 | */ |
||
| 716 | protected function prepare_shipping_lines( $posted, $action = 'create', $item = null ) { |
||
| 717 | $item = is_null( $item ) ? new \WC_Order_Item_Shipping( ! empty( $posted['id'] ) ? $posted['id'] : '' ) : $item; |
||
| 718 | |||
| 719 | if ( 'create' === $action ) { |
||
| 720 | if ( empty( $posted['method_id'] ) ) { |
||
| 721 | throw new \WC_REST_Exception( 'woocommerce_rest_invalid_shipping_item', __( 'Shipping method ID is required.', 'woocommerce' ), 400 ); |
||
| 722 | } |
||
| 723 | } |
||
| 724 | |||
| 725 | $this->maybe_set_item_props( $item, array( 'method_id', 'method_title', 'total' ), $posted ); |
||
| 726 | $this->maybe_set_item_meta_data( $item, $posted ); |
||
| 727 | |||
| 728 | return $item; |
||
| 729 | } |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Create or update an order fee. |
||
| 733 | * |
||
| 734 | * @param array $posted Item data. |
||
| 735 | * @param string $action 'create' to add fee or 'update' to update it. |
||
| 736 | * @param object $item Passed when updating an item. Null during creation. |
||
| 737 | * @return WC_Order_Item_Fee |
||
| 738 | * @throws WC_REST_Exception Invalid data, server error. |
||
| 739 | */ |
||
| 740 | protected function prepare_fee_lines( $posted, $action = 'create', $item = null ) { |
||
| 741 | $item = is_null( $item ) ? new \WC_Order_Item_Fee( ! empty( $posted['id'] ) ? $posted['id'] : '' ) : $item; |
||
| 742 | |||
| 743 | if ( 'create' === $action ) { |
||
| 744 | if ( empty( $posted['name'] ) ) { |
||
| 745 | throw new \WC_REST_Exception( 'woocommerce_rest_invalid_fee_item', __( 'Fee name is required.', 'woocommerce' ), 400 ); |
||
| 746 | } |
||
| 747 | } |
||
| 748 | |||
| 749 | $this->maybe_set_item_props( $item, array( 'name', 'tax_class', 'tax_status', 'total' ), $posted ); |
||
| 750 | $this->maybe_set_item_meta_data( $item, $posted ); |
||
| 751 | |||
| 752 | return $item; |
||
| 753 | } |
||
| 754 | |||
| 755 | /** |
||
| 756 | * Create or update an order coupon. |
||
| 757 | * |
||
| 758 | * @param array $posted Item data. |
||
| 759 | * @param string $action 'create' to add coupon or 'update' to update it. |
||
| 760 | * @param object $item Passed when updating an item. Null during creation. |
||
| 761 | * @return WC_Order_Item_Coupon |
||
| 762 | * @throws WC_REST_Exception Invalid data, server error. |
||
| 763 | */ |
||
| 764 | protected function prepare_coupon_lines( $posted, $action = 'create', $item = null ) { |
||
| 765 | $item = is_null( $item ) ? new \WC_Order_Item_Coupon( ! empty( $posted['id'] ) ? $posted['id'] : '' ) : $item; |
||
| 766 | |||
| 767 | if ( 'create' === $action ) { |
||
| 768 | if ( empty( $posted['code'] ) ) { |
||
| 769 | throw new \WC_REST_Exception( 'woocommerce_rest_invalid_coupon_coupon', __( 'Coupon code is required.', 'woocommerce' ), 400 ); |
||
| 770 | } |
||
| 771 | } |
||
| 772 | |||
| 773 | $this->maybe_set_item_props( $item, array( 'code', 'discount' ), $posted ); |
||
| 774 | $this->maybe_set_item_meta_data( $item, $posted ); |
||
| 775 | |||
| 776 | return $item; |
||
| 777 | } |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Wrapper method to create/update order items. |
||
| 781 | * When updating, the item ID provided is checked to ensure it is associated |
||
| 782 | * with the order. |
||
| 783 | * |
||
| 784 | * @param WC_Order $order order object. |
||
| 785 | * @param string $item_type The item type. |
||
| 786 | * @param array $posted item provided in the request body. |
||
| 787 | * @throws WC_REST_Exception If item ID is not associated with order. |
||
| 788 | */ |
||
| 789 | protected function set_item( $order, $item_type, $posted ) { |
||
| 790 | global $wpdb; |
||
| 791 | |||
| 792 | if ( ! empty( $posted['id'] ) ) { |
||
| 793 | $action = 'update'; |
||
| 794 | } else { |
||
| 795 | $action = 'create'; |
||
| 796 | } |
||
| 797 | |||
| 798 | $method = 'prepare_' . $item_type; |
||
| 799 | $item = null; |
||
| 800 | |||
| 801 | // Verify provided line item ID is associated with order. |
||
| 802 | if ( 'update' === $action ) { |
||
| 803 | $item = $order->get_item( absint( $posted['id'] ), false ); |
||
| 804 | |||
| 805 | if ( ! $item ) { |
||
| 806 | throw new \WC_REST_Exception( 'woocommerce_rest_invalid_item_id', __( 'Order item ID provided is not associated with order.', 'woocommerce' ), 400 ); |
||
| 807 | } |
||
| 808 | } |
||
| 809 | |||
| 810 | // Prepare item data. |
||
| 811 | $item = $this->$method( $posted, $action, $item ); |
||
| 812 | |||
| 813 | do_action( 'woocommerce_rest_set_order_item', $item, $posted ); |
||
| 814 | |||
| 815 | // If creating the order, add the item to it. |
||
| 816 | if ( 'create' === $action ) { |
||
| 817 | $order->add_item( $item ); |
||
| 818 | } else { |
||
| 819 | $item->save(); |
||
| 820 | } |
||
| 821 | } |
||
| 822 | |||
| 823 | /** |
||
| 824 | * Helper method to check if the resource ID associated with the provided item is null. |
||
| 825 | * Items can be deleted by setting the resource ID to null. |
||
| 826 | * |
||
| 827 | * @param array $item Item provided in the request body. |
||
| 828 | * @return bool True if the item resource ID is null, false otherwise. |
||
| 829 | */ |
||
| 830 | protected function item_is_null( $item ) { |
||
| 831 | $keys = array( 'product_id', 'method_id', 'method_title', 'name', 'code' ); |
||
| 832 | |||
| 833 | foreach ( $keys as $key ) { |
||
| 834 | if ( array_key_exists( $key, $item ) && is_null( $item[ $key ] ) ) { |
||
| 835 | return true; |
||
| 836 | } |
||
| 837 | } |
||
| 838 | |||
| 839 | return false; |
||
| 840 | } |
||
| 841 | |||
| 842 | /** |
||
| 843 | * Get order statuses without prefixes. |
||
| 844 | * |
||
| 845 | * @return array |
||
| 846 | */ |
||
| 847 | protected function get_order_statuses() { |
||
| 848 | $order_statuses = array(); |
||
| 849 | |||
| 850 | foreach ( array_keys( wc_get_order_statuses() ) as $status ) { |
||
| 851 | $order_statuses[] = str_replace( 'wc-', '', $status ); |
||
| 852 | } |
||
| 853 | |||
| 854 | return $order_statuses; |
||
| 855 | } |
||
| 856 | |||
| 857 | /** |
||
| 858 | * Get the Order's schema, conforming to JSON Schema. |
||
| 859 | * |
||
| 860 | * @return array |
||
| 861 | */ |
||
| 862 | public function get_item_schema() { |
||
| 1700 | } |
||
| 1701 | |||
| 1702 | /** |
||
| 1703 | * Get the query params for collections. |
||
| 1704 | * |
||
| 1705 | * @return array |
||
| 1706 | */ |
||
| 1707 | public function get_collection_params() { |
||
| 1708 | $params = parent::get_collection_params(); |
||
| 1709 | |||
| 1710 | $params['status'] = array( |
||
| 1711 | 'default' => 'any', |
||
| 1712 | 'description' => __( 'Limit result set to orders which have specific statuses.', 'woocommerce' ), |
||
| 1713 | 'type' => 'array', |
||
| 1714 | 'items' => array( |
||
| 1715 | 'type' => 'string', |
||
| 1716 | 'enum' => array_merge( array( 'any', 'trash' ), $this->get_order_statuses() ), |
||
| 1717 | ), |
||
| 1718 | 'validate_callback' => 'rest_validate_request_arg', |
||
| 1719 | ); |
||
| 1720 | $params['customer'] = array( |
||
| 1721 | 'description' => __( 'Limit result set to orders assigned a specific customer.', 'woocommerce' ), |
||
| 1722 | 'type' => 'integer', |
||
| 1723 | 'sanitize_callback' => 'absint', |
||
| 1724 | 'validate_callback' => 'rest_validate_request_arg', |
||
| 1725 | ); |
||
| 1726 | $params['product'] = array( |
||
| 1727 | 'description' => __( 'Limit result set to orders assigned a specific product.', 'woocommerce' ), |
||
| 1728 | 'type' => 'integer', |
||
| 1729 | 'sanitize_callback' => 'absint', |
||
| 1730 | 'validate_callback' => 'rest_validate_request_arg', |
||
| 1731 | ); |
||
| 1732 | $params['dp'] = array( |
||
| 1733 | 'default' => wc_get_price_decimals(), |
||
| 1734 | 'description' => __( 'Number of decimal points to use in each resource.', 'woocommerce' ), |
||
| 1735 | 'type' => 'integer', |
||
| 1736 | 'sanitize_callback' => 'absint', |
||
| 1737 | 'validate_callback' => 'rest_validate_request_arg', |
||
| 1738 | ); |
||
| 1739 | // This needs to remain a string to support extensions that filter Order Number. |
||
| 1740 | $params['number'] = array( |
||
| 1741 | 'description' => __( 'Limit result set to orders matching part of an order number.', 'woocommerce' ), |
||
| 1742 | 'type' => 'string', |
||
| 1743 | 'validate_callback' => 'rest_validate_request_arg', |
||
| 1744 | ); |
||
| 1745 | |||
| 1746 | return $params; |
||
| 1747 | } |
||
| 1748 | |||
| 1749 | /** |
||
| 1750 | * Calculate coupons. |
||
| 1751 | * |
||
| 1752 | * @throws \WC_REST_Exception When fails to set any item. |
||
| 1753 | * |
||
| 1754 | * @param \WP_REST_Request $request Request object. |
||
| 1755 | * @param \WC_Order $order Order data. |
||
| 1756 | * @return bool |
||
| 1757 | */ |
||
| 1758 | protected function calculate_coupons( $request, $order ) { |
||
| 1785 | } |
||
| 1786 | } |
||
| 1787 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths