Code Duplication    Length = 50-56 lines in 2 locations

includes/cli/class-wc-cli-order.php 1 location

@@ 793-842 (lines=50) @@
790
	 * @param WC_Order $order
791
	 * @param array $data
792
	 */
793
	protected function set_order_addresses( $order, $data ) {
794
		$address_fields = array(
795
			'first_name',
796
			'last_name',
797
			'company',
798
			'email',
799
			'phone',
800
			'address_1',
801
			'address_2',
802
			'city',
803
			'state',
804
			'postcode',
805
			'country',
806
		);
807
		$billing_address = $shipping_address = array();
808
809
		// billing address.
810
		if ( isset( $data['billing_address'] ) && is_array( $data['billing_address'] ) ) {
811
			foreach ( $address_fields as $field ) {
812
				if ( isset( $data['billing_address'][ $field ] ) ) {
813
					$billing_address[ $field ] = wc_clean( $data['billing_address'][ $field ] );
814
				}
815
			}
816
817
			unset( $address_fields['email'] );
818
			unset( $address_fields['phone'] );
819
		}
820
821
		// shipping address.
822
		if ( isset( $data['shipping_address'] ) && is_array( $data['shipping_address'] ) ) {
823
			foreach ( $address_fields as $field ) {
824
				if ( isset( $data['shipping_address'][ $field ] ) ) {
825
					$shipping_address[ $field ] = wc_clean( $data['shipping_address'][ $field ] );
826
				}
827
			}
828
		}
829
830
		$order->set_address( $billing_address, 'billing' );
831
		$order->set_address( $shipping_address, 'shipping' );
832
833
		// update user meta
834
		if ( $order->get_user_id() ) {
835
			foreach ( $billing_address as $key => $value ) {
836
				update_user_meta( $order->get_user_id(), 'billing_' . $key, $value );
837
			}
838
			foreach ( $shipping_address as $key => $value ) {
839
				update_user_meta( $order->get_user_id(), 'shipping_' . $key, $value );
840
			}
841
		}
842
	}
843
844
	/**
845
	 * Helper method to add/update order meta, with two restrictions:

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

@@ 758-813 (lines=56) @@
755
	 * @param \WC_Order $order
756
	 * @param array $data
757
	 */
758
	protected function set_order_addresses( $order, $data ) {
759
760
		$address_fields = array(
761
			'first_name',
762
			'last_name',
763
			'company',
764
			'email',
765
			'phone',
766
			'address_1',
767
			'address_2',
768
			'city',
769
			'state',
770
			'postcode',
771
			'country',
772
		);
773
774
		$billing_address = $shipping_address = array();
775
776
		// billing address
777
		if ( isset( $data['billing_address'] ) && is_array( $data['billing_address'] ) ) {
778
779
			foreach ( $address_fields as $field ) {
780
781
				if ( isset( $data['billing_address'][ $field ] ) ) {
782
					$billing_address[ $field ] = wc_clean( $data['billing_address'][ $field ] );
783
				}
784
			}
785
786
			unset( $address_fields['email'] );
787
			unset( $address_fields['phone'] );
788
		}
789
790
		// shipping address
791
		if ( isset( $data['shipping_address'] ) && is_array( $data['shipping_address'] ) ) {
792
793
			foreach ( $address_fields as $field ) {
794
795
				if ( isset( $data['shipping_address'][ $field ] ) ) {
796
					$shipping_address[ $field ] = wc_clean( $data['shipping_address'][ $field ] );
797
				}
798
			}
799
		}
800
801
		$order->set_address( $billing_address, 'billing' );
802
		$order->set_address( $shipping_address, 'shipping' );
803
804
		// update user meta
805
		if ( $order->get_user_id() ) {
806
			foreach ( $billing_address as $key => $value ) {
807
				update_user_meta( $order->get_user_id(), 'billing_' . $key, $value );
808
			}
809
			foreach ( $shipping_address as $key => $value ) {
810
				update_user_meta( $order->get_user_id(), 'shipping_' . $key, $value );
811
			}
812
		}
813
	}
814
815
	/**
816
	 * Helper method to add/update order meta, with two restrictions: