Completed
Push — master ( 15aa29...17da96 )
by Claudio
18:39 queued 11s
created

includes/admin/class-wc-admin-profile.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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', false ) ) :
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(
40
				'woocommerce_customer_meta_fields', array(
41
					'billing'  => array(
42
						'title'  => __( 'Customer billing address', 'woocommerce' ),
43
						'fields' => array(
44
							'billing_first_name' => array(
45
								'label'       => __( 'First name', 'woocommerce' ),
46
								'description' => '',
47
							),
48
							'billing_last_name'  => array(
49
								'label'       => __( 'Last name', 'woocommerce' ),
50
								'description' => '',
51
							),
52
							'billing_company'    => array(
53
								'label'       => __( 'Company', 'woocommerce' ),
54
								'description' => '',
55
							),
56
							'billing_address_1'  => array(
57
								'label'       => __( 'Address line 1', 'woocommerce' ),
58
								'description' => '',
59
							),
60
							'billing_address_2'  => array(
61
								'label'       => __( 'Address line 2', 'woocommerce' ),
62
								'description' => '',
63
							),
64
							'billing_city'       => array(
65
								'label'       => __( 'City', 'woocommerce' ),
66
								'description' => '',
67
							),
68
							'billing_postcode'   => array(
69
								'label'       => __( 'Postcode / ZIP', 'woocommerce' ),
70
								'description' => '',
71
							),
72
							'billing_country'    => array(
73
								'label'       => __( 'Country', 'woocommerce' ),
74
								'description' => '',
75
								'class'       => 'js_field-country',
76
								'type'        => 'select',
77
								'options'     => array( '' => __( 'Select a country&hellip;', 'woocommerce' ) ) + WC()->countries->get_allowed_countries(),
78
							),
79
							'billing_state'      => array(
80
								'label'       => __( 'State / County', 'woocommerce' ),
81
								'description' => __( 'State / County or state code', 'woocommerce' ),
82
								'class'       => 'js_field-state',
83
							),
84
							'billing_phone'      => array(
85
								'label'       => __( 'Phone', 'woocommerce' ),
86
								'description' => '',
87
							),
88
							'billing_email'      => array(
89
								'label'       => __( 'Email address', 'woocommerce' ),
90
								'description' => '',
91
							),
92
						),
93
					),
94
					'shipping' => array(
95
						'title'  => __( 'Customer shipping address', 'woocommerce' ),
96
						'fields' => array(
97
							'copy_billing'        => array(
98
								'label'       => __( 'Copy from billing address', 'woocommerce' ),
99
								'description' => '',
100
								'class'       => 'js_copy-billing',
101
								'type'        => 'button',
102
								'text'        => __( 'Copy', 'woocommerce' ),
103
							),
104
							'shipping_first_name' => array(
105
								'label'       => __( 'First name', 'woocommerce' ),
106
								'description' => '',
107
							),
108
							'shipping_last_name'  => array(
109
								'label'       => __( 'Last name', 'woocommerce' ),
110
								'description' => '',
111
							),
112
							'shipping_company'    => array(
113
								'label'       => __( 'Company', 'woocommerce' ),
114
								'description' => '',
115
							),
116
							'shipping_address_1'  => array(
117
								'label'       => __( 'Address line 1', 'woocommerce' ),
118
								'description' => '',
119
							),
120
							'shipping_address_2'  => array(
121
								'label'       => __( 'Address line 2', 'woocommerce' ),
122
								'description' => '',
123
							),
124
							'shipping_city'       => array(
125
								'label'       => __( 'City', 'woocommerce' ),
126
								'description' => '',
127
							),
128
							'shipping_postcode'   => array(
129
								'label'       => __( 'Postcode / ZIP', 'woocommerce' ),
130
								'description' => '',
131
							),
132
							'shipping_country'    => array(
133
								'label'       => __( 'Country', 'woocommerce' ),
134
								'description' => '',
135
								'class'       => 'js_field-country',
136
								'type'        => 'select',
137
								'options'     => array( '' => __( 'Select a country&hellip;', 'woocommerce' ) ) + WC()->countries->get_allowed_countries(),
138
							),
139
							'shipping_state'      => array(
140
								'label'       => __( 'State / County', 'woocommerce' ),
141
								'description' => __( 'State / County or state code', 'woocommerce' ),
142
								'class'       => 'js_field-state',
143
							),
144
						),
145
					),
146
				)
147
			);
148
			return $show_fields;
149
		}
150
151
		/**
152
		 * Show Address Fields on edit user pages.
153
		 *
154
		 * @param WP_User $user
155
		 */
156
		public function add_customer_meta_fields( $user ) {
157
			if ( ! apply_filters( 'woocommerce_current_user_can_edit_customer_meta_fields', current_user_can( 'manage_woocommerce' ), $user->ID ) ) {
158
				return;
159
			}
160
161
			$show_fields = $this->get_customer_meta_fields();
162
163
			foreach ( $show_fields as $fieldset_key => $fieldset ) :
164
				?>
165
				<h2><?php echo $fieldset['title']; ?></h2>
166
				<table class="form-table" id="<?php echo esc_attr( 'fieldset-' . $fieldset_key ); ?>">
167
					<?php foreach ( $fieldset['fields'] as $key => $field ) : ?>
168
						<tr>
169
							<th>
170
								<label for="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $field['label'] ); ?></label>
171
							</th>
172
							<td>
173
								<?php if ( ! empty( $field['type'] ) && 'select' === $field['type'] ) : ?>
174
									<select name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" class="<?php echo esc_attr( $field['class'] ); ?>" style="width: 25em;">
175
										<?php
176
											$selected = esc_attr( get_user_meta( $user->ID, $key, true ) );
0 ignored issues
show
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
177
										foreach ( $field['options'] as $option_key => $option_value ) :
178
											?>
179
											<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $selected, $option_key, true ); ?>><?php echo esc_attr( $option_value ); ?></option>
180
										<?php endforeach; ?>
181
									</select>
182
								<?php elseif ( ! empty( $field['type'] ) && 'checkbox' === $field['type'] ) : ?>
183
									<input type="checkbox" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" value="1" class="<?php echo esc_attr( $field['class'] ); ?>" <?php checked( (int) get_user_meta( $user->ID, $key, true ), 1, true ); ?> />
184 View Code Duplication
								<?php elseif ( ! empty( $field['type'] ) && 'button' === $field['type'] ) : ?>
185
									<button type="button" id="<?php echo esc_attr( $key ); ?>" class="button <?php echo esc_attr( $field['class'] ); ?>"><?php echo esc_html( $field['text'] ); ?></button>
186
								<?php else : ?>
187
									<input type="text" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( $this->get_user_meta( $user->ID, $key ) ); ?>" class="<?php echo ( ! empty( $field['class'] ) ? esc_attr( $field['class'] ) : 'regular-text' ); ?>" />
188
								<?php endif; ?>
189
								<p class="description"><?php echo wp_kses_post( $field['description'] ); ?></p>
190
							</td>
191
						</tr>
192
					<?php endforeach; ?>
193
				</table>
194
				<?php
195
			endforeach;
196
		}
197
198
		/**
199
		 * Save Address Fields on edit user pages.
200
		 *
201
		 * @param int $user_id User ID of the user being saved
202
		 */
203
		public function save_customer_meta_fields( $user_id ) {
204
			if ( ! apply_filters( 'woocommerce_current_user_can_edit_customer_meta_fields', current_user_can( 'manage_woocommerce' ), $user_id ) ) {
205
				return;
206
			}
207
208
			$save_fields = $this->get_customer_meta_fields();
209
210
			foreach ( $save_fields as $fieldset ) {
211
212
				foreach ( $fieldset['fields'] as $key => $field ) {
213
214
					if ( isset( $field['type'] ) && 'checkbox' === $field['type'] ) {
215
						update_user_meta( $user_id, $key, isset( $_POST[ $key ] ) );
0 ignored issues
show
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
216
					} elseif ( isset( $_POST[ $key ] ) ) {
217
						update_user_meta( $user_id, $key, wc_clean( $_POST[ $key ] ) );
218
					}
219
				}
220
			}
221
		}
222
223
		/**
224
		 * Get user meta for a given key, with fallbacks to core user info for pre-existing fields.
225
		 *
226
		 * @since 3.1.0
227
		 * @param int    $user_id User ID of the user being edited
228
		 * @param string $key     Key for user meta field
229
		 * @return string
230
		 */
231
		protected function get_user_meta( $user_id, $key ) {
232
			$value           = get_user_meta( $user_id, $key, true );
233
			$existing_fields = array( 'billing_first_name', 'billing_last_name' );
234
			if ( ! $value && in_array( $key, $existing_fields ) ) {
235
				$value = get_user_meta( $user_id, str_replace( 'billing_', '', $key ), true );
236
			} elseif ( ! $value && ( 'billing_email' === $key ) ) {
237
				$user  = get_userdata( $user_id );
238
				$value = $user->user_email;
239
			}
240
241
			return $value;
242
		}
243
	}
244
245
endif;
246
247
return new WC_Admin_Profile();
248