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

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