Code Duplication    Length = 29-31 lines in 2 locations

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

@@ 688-716 (lines=29) @@
685
	 * @param WP_REST_Request $request Full details about the request.
686
	 * @return WP_Error|WP_REST_Response
687
	 */
688
	public function create_item( $request ) {
689
		if ( ! empty( $request['id'] ) ) {
690
			return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
691
		}
692
693
		$order_id = $this->create_order( $request );
694
		if ( is_wp_error( $order_id ) ) {
695
			return $order_id;
696
		}
697
698
		$post = get_post( $order_id );
699
		$this->update_additional_fields_for_object( $post, $request );
700
701
		/**
702
		 * Fires after a single item is created or updated via the REST API.
703
		 *
704
		 * @param object          $post      Inserted object (not a WP_Post object).
705
		 * @param WP_REST_Request $request   Request object.
706
		 * @param boolean         $creating  True when creating item, false when updating.
707
		 */
708
		do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, true );
709
		$request->set_param( 'context', 'edit' );
710
		$response = $this->prepare_item_for_response( $post, $request );
711
		$response = rest_ensure_response( $response );
712
		$response->set_status( 201 );
713
		$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post->ID ) ) );
714
715
		return $response;
716
	}
717
718
	/**
719
	 * Update a single order.

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

@@ 276-306 (lines=31) @@
273
	 * @param WP_REST_Request $request Full details about the request.
274
	 * @return WP_Error|WP_REST_Response
275
	 */
276
	public function create_item( $request ) {
277
		if ( ! empty( $request['id'] ) ) {
278
			return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
279
		}
280
281
		$coupon_id = $this->save_coupon( $request );
282
		if ( is_wp_error( $coupon_id ) ) {
283
			return $coupon_id;
284
		}
285
286
		$post = get_post( $coupon_id );
287
		$this->update_additional_fields_for_object( $post, $request );
288
289
		$this->add_post_meta_fields( $post, $request );
290
291
		/**
292
		 * Fires after a single item is created or updated via the REST API.
293
		 *
294
		 * @param object          $post      Inserted object (not a WP_Post object).
295
		 * @param WP_REST_Request $request   Request object.
296
		 * @param boolean         $creating  True when creating item, false when updating.
297
		 */
298
		do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, true );
299
		$request->set_param( 'context', 'edit' );
300
		$response = $this->prepare_item_for_response( $post, $request );
301
		$response = rest_ensure_response( $response );
302
		$response->set_status( 201 );
303
		$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post->ID ) ) );
304
305
		return $response;
306
	}
307
308
	/**
309
	 * Update a single coupon.