Completed
Push — master ( 0745f1...4c8a60 )
by Claudio
12:41
created

WC_Admin_Profile::add_customer_meta_fields()   C

Complexity

Conditions 12
Paths 6

Size

Total Lines 41
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 33
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 41
rs 5.1612

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Add extra profile fields for users in admin
4
 *
5
 * @author   WooThemes
6
 * @category Admin
7
 * @package  WooCommerce/Admin
8
 * @version  2.4.0
9
 */
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit; // Exit if accessed directly
13
}
14
15
if ( ! class_exists( 'WC_Admin_Profile' ) ) :
16
17
/**
18
 * WC_Admin_Profile Class.
19
 */
20
class WC_Admin_Profile {
21
22
	/**
23
	 * Hook in tabs.
24
	 */
25
	public function __construct() {
26
		add_action( 'show_user_profile', array( $this, 'add_customer_meta_fields' ) );
27
		add_action( 'edit_user_profile', array( $this, 'add_customer_meta_fields' ) );
28
29
		add_action( 'personal_options_update', array( $this, 'save_customer_meta_fields' ) );
30
		add_action( 'edit_user_profile_update', array( $this, 'save_customer_meta_fields' ) );
31
	}
32
33
	/**
34
	 * Get Address Fields for the edit user pages.
35
	 *
36
	 * @return array Fields to display which are filtered through woocommerce_customer_meta_fields before being returned
37
	 */
38
	public function get_customer_meta_fields() {
39
		$show_fields = apply_filters('woocommerce_customer_meta_fields', array(
40
			'billing' => array(
41
				'title' => __( 'Customer Billing Address', 'woocommerce' ),
42
				'fields' => array(
43
					'billing_first_name' => array(
44
						'label'       => __( 'First name', 'woocommerce' ),
45
						'description' => ''
46
					),
47
					'billing_last_name' => array(
48
						'label'       => __( 'Last name', 'woocommerce' ),
49
						'description' => ''
50
					),
51
					'billing_company' => array(
52
						'label'       => __( 'Company', 'woocommerce' ),
53
						'description' => ''
54
					),
55
					'billing_address_1' => array(
56
						'label'       => __( 'Address 1', 'woocommerce' ),
57
						'description' => ''
58
					),
59
					'billing_address_2' => array(
60
						'label'       => __( 'Address 2', 'woocommerce' ),
61
						'description' => ''
62
					),
63
					'billing_city' => array(
64
						'label'       => __( 'City', 'woocommerce' ),
65
						'description' => ''
66
					),
67
					'billing_postcode' => array(
68
						'label'       => __( 'Postcode', 'woocommerce' ),
69
						'description' => ''
70
					),
71
					'billing_country' => array(
72
						'label'       => __( 'Country', 'woocommerce' ),
73
						'description' => '',
74
						'class'       => 'js_field-country',
75
						'type'        => 'select',
76
						'options'     => array( '' => __( 'Select a country&hellip;', 'woocommerce' ) ) + WC()->countries->get_allowed_countries()
77
					),
78
					'billing_state' => array(
79
						'label'       => __( 'State/County', 'woocommerce' ),
80
						'description' => __( 'State/County or state code', 'woocommerce' ),
81
						'class'       => 'js_field-state'
82
					),
83
					'billing_phone' => array(
84
						'label'       => __( 'Telephone', 'woocommerce' ),
85
						'description' => ''
86
					),
87
					'billing_email' => array(
88
						'label'       => __( 'Email', 'woocommerce' ),
89
						'description' => ''
90
					)
91
				)
92
			),
93
			'shipping' => array(
94
				'title' => __( 'Customer Shipping Address', 'woocommerce' ),
95
				'fields' => array(
96
					'shipping_first_name' => array(
97
						'label'       => __( 'First name', 'woocommerce' ),
98
						'description' => ''
99
					),
100
					'shipping_last_name' => array(
101
						'label'       => __( 'Last name', 'woocommerce' ),
102
						'description' => ''
103
					),
104
					'shipping_company' => array(
105
						'label'       => __( 'Company', 'woocommerce' ),
106
						'description' => ''
107
					),
108
					'shipping_address_1' => array(
109
						'label'       => __( 'Address 1', 'woocommerce' ),
110
						'description' => ''
111
					),
112
					'shipping_address_2' => array(
113
						'label'       => __( 'Address 2', 'woocommerce' ),
114
						'description' => ''
115
					),
116
					'shipping_city' => array(
117
						'label'       => __( 'City', 'woocommerce' ),
118
						'description' => ''
119
					),
120
					'shipping_postcode' => array(
121
						'label'       => __( 'Postcode', 'woocommerce' ),
122
						'description' => ''
123
					),
124
					'shipping_country' => array(
125
						'label'       => __( 'Country', 'woocommerce' ),
126
						'description' => '',
127
						'class'       => 'js_field-country',
128
						'type'        => 'select',
129
						'options'     => array( '' => __( 'Select a country&hellip;', 'woocommerce' ) ) + WC()->countries->get_allowed_countries()
130
					),
131
					'shipping_state' => array(
132
						'label'       => __( 'State/County', 'woocommerce' ),
133
						'description' => __( 'State/County or state code', 'woocommerce' ),
134
						'class'       => 'js_field-state'
135
					)
136
				)
137
			)
138
		) );
139
		return $show_fields;
140
	}
141
142
	/**
143
	 * Show Address Fields on edit user pages.
144
	 *
145
	 * @param WP_User $user
146
	 */
147
	public function add_customer_meta_fields( $user ) {
148
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
149
			return;
150
		}
151
152
		$show_fields = $this->get_customer_meta_fields();
153
154
		foreach ( $show_fields as $fieldset ) :
155
			?>
156
			<h2><?php echo $fieldset['title']; ?></h2>
157
			<table class="form-table">
158
				<?php
159
				foreach ( $fieldset['fields'] as $key => $field ) :
160
					?>
161
					<tr>
162
						<th><label for="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $field['label'] ); ?></label></th>
163
						<td>
164
							<?php if ( ! empty( $field['type'] ) && 'select' === $field['type'] ) : ?>
165
								<select name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" class="<?php echo ( ! empty( $field['class'] ) ? $field['class'] : '' ); ?>" style="width: 25em;">
166
									<?php
167
										$selected = esc_attr( get_user_meta( $user->ID, $key, true ) );
168
										foreach ( $field['options'] as $option_key => $option_value ) : ?>
169
										<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $selected, $option_key, true ); ?>><?php echo esc_attr( $option_value ); ?></option>
170
									<?php endforeach; ?>
171
								</select>
172
							<?php elseif ( ! empty( $field['type'] ) && 'checkbox' === $field['type'] ) : ?>
173
								<input type="checkbox" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" value="1" class="<?php echo ( ! empty( $field['class'] ) ? $field['class'] : '' ); ?>" <?php checked( (int) esc_attr( get_user_meta( $user->ID, $key, true ) ), 1, true ); ?> />
174
							<?php else : ?>
175
								<input type="text" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( get_user_meta( $user->ID, $key, true ) ); ?>" class="<?php echo ( ! empty( $field['class'] ) ? $field['class'] : 'regular-text' ); ?>" />
176
							<?php endif; ?>
177
							<br/>
178
							<span class="description"><?php echo wp_kses_post( $field['description'] ); ?></span>
179
						</td>
180
					</tr>
181
					<?php
182
				endforeach;
183
				?>
184
			</table>
185
			<?php
186
		endforeach;
187
	}
188
189
	/**
190
	 * Save Address Fields on edit user pages.
191
	 *
192
	 * @param int $user_id User ID of the user being saved
193
	 */
194
	public function save_customer_meta_fields( $user_id ) {
195
		$save_fields = $this->get_customer_meta_fields();
196
197
		foreach ( $save_fields as $fieldset ) {
198
199
			foreach ( $fieldset['fields'] as $key => $field ) {
200
201
				if ( 'checkbox' == $field['type'] ) {
202
					update_user_meta( $user_id, $key, isset( $_POST[ $key ] ) );
203
				} elseif ( isset( $_POST[ $key ] ) ) {
204
					update_user_meta( $user_id, $key, wc_clean( $_POST[ $key ] ) );
205
				}
206
			}
207
		}
208
	}
209
}
210
211
endif;
212
213
return new WC_Admin_Profile();
214