Code Duplication    Length = 32-32 lines in 2 locations

includes/api/class-wc-rest-orders-controller.php 1 location

@@ 724-755 (lines=32) @@
721
	 * @param WP_REST_Request $request Full details about the request.
722
	 * @return WP_Error|WP_REST_Response
723
	 */
724
	public function update_item( $request ) {
725
		try {
726
			$post_id = (int) $request['id'];
727
728
			if ( empty( $post_id ) || $this->post_type !== get_post_type( $post_id ) ) {
729
				return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
730
			}
731
732
			$order_id = $this->update_order( $request );
733
			if ( is_wp_error( $order_id ) ) {
734
				return $order_id;
735
			}
736
737
			$post = get_post( $order_id );
738
			$this->update_additional_fields_for_object( $post, $request );
739
740
			/**
741
			 * Fires after a single item is created or updated via the REST API.
742
			 *
743
			 * @param object          $post      Inserted object (not a WP_Post object).
744
			 * @param WP_REST_Request $request   Request object.
745
			 * @param boolean         $creating  True when creating item, false when updating.
746
			 */
747
			do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, false );
748
			$request->set_param( 'context', 'edit' );
749
			$response = $this->prepare_item_for_response( $post, $request );
750
			return rest_ensure_response( $response );
751
752
		} catch ( Exception $e ) {
753
			return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
754
		}
755
	}
756
757
	/**
758
	 * Get order statuses without prefixes.

includes/api/class-wc-rest-coupons-controller.php 1 location

@@ 314-345 (lines=32) @@
311
	 * @param WP_REST_Request $request Full details about the request.
312
	 * @return WP_Error|WP_REST_Response
313
	 */
314
	public function update_item( $request ) {
315
		try {
316
			$post_id = (int) $request['id'];
317
318
			if ( empty( $post_id ) || $this->post_type !== get_post_type( $post_id ) ) {
319
				return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
320
			}
321
322
			$coupon_id = $this->save_coupon( $request );
323
			if ( is_wp_error( $coupon_id ) ) {
324
				return $coupon_id;
325
			}
326
327
			$post = get_post( $coupon_id );
328
			$this->update_additional_fields_for_object( $post, $request );
329
330
			/**
331
			 * Fires after a single item is created or updated via the REST API.
332
			 *
333
			 * @param object          $post      Inserted object (not a WP_Post object).
334
			 * @param WP_REST_Request $request   Request object.
335
			 * @param boolean         $creating  True when creating item, false when updating.
336
			 */
337
			do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, false );
338
			$request->set_param( 'context', 'edit' );
339
			$response = $this->prepare_item_for_response( $post, $request );
340
			return rest_ensure_response( $response );
341
342
		} catch ( Exception $e ) {
343
			return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
344
		}
345
	}
346
347
	/**
348
	 * Saves a coupon to the database.