Code Duplication    Length = 29-31 lines in 2 locations

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

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

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.