Code Duplication    Length = 32-32 lines in 2 locations

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

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

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.