Passed
Pull Request — master (#782)
by Kiran
15:30
created

GetPaid_Admin_Profile::get_customer_meta_fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 61
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 44
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 61
rs 9.216

How to fix   Long Method   

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
 */
6
7
if ( ! defined( 'ABSPATH' ) ) {
8
	exit; // Exit if accessed directly
9
}
10
11
if ( ! class_exists( 'GetPaid_Admin_Profile', false ) ) :
12
13
	/**
14
	 * GetPaid_Admin_Profile Class.
15
	 */
16
	class GetPaid_Admin_Profile {
17
18
		/**
19
		 * Hook in tabs.
20
		 */
21
		public function __construct() {
22
			add_action( 'show_user_profile', array( $this, 'add_customer_meta_fields' ), 100 );
23
			add_action( 'edit_user_profile', array( $this, 'add_customer_meta_fields' ), 100 );
24
25
			add_action( 'personal_options_update', array( $this, 'save_customer_meta_fields' ) );
26
			add_action( 'edit_user_profile_update', array( $this, 'save_customer_meta_fields' ) );
27
		}
28
29
		/**
30
		 * Get Address Fields for the edit user pages.
31
		 *
32
		 * @return array Fields to display which are filtered through invoicing_customer_meta_fields before being returned
33
		 */
34
		public function get_customer_meta_fields() {
35
36
			$show_fields = apply_filters(
37
				'getpaid_customer_meta_fields',
38
				array(
39
					'billing' => array(
40
						'title'  => __( 'Billing Details (GetPaid)', 'invoicing' ),
41
						'fields' => array(
42
							'_wpinv_first_name' => array(
43
								'label'       => __( 'First name', 'invoicing' ),
44
								'description' => '',
45
							),
46
							'_wpinv_last_name'  => array(
47
								'label'       => __( 'Last name', 'invoicing' ),
48
								'description' => '',
49
							),
50
							'_wpinv_company'    => array(
51
								'label'       => __( 'Company', 'invoicing' ),
52
								'description' => '',
53
							),
54
							'_wpinv_company_id' => array(
55
								'label'       => __( 'Company ID', 'invoicing' ),
56
								'description' => '',
57
							),
58
							'_wpinv_address'    => array(
59
								'label'       => __( 'Address', 'invoicing' ),
60
								'description' => '',
61
							),
62
							'_wpinv_city'       => array(
63
								'label'       => __( 'City', 'invoicing' ),
64
								'description' => '',
65
							),
66
							'_wpinv_zip'        => array(
67
								'label'       => __( 'Postcode / ZIP', 'invoicing' ),
68
								'description' => '',
69
							),
70
							'_wpinv_country'    => array(
71
								'label'       => __( 'Country / Region', 'invoicing' ),
72
								'description' => '',
73
								'class'       => 'getpaid_js_field-country',
74
								'type'        => 'select',
75
								'options'     => array( '' => __( 'Select a country / region&hellip;', 'invoicing' ) ) + wpinv_get_country_list(),
76
							),
77
							'_wpinv_state'      => array(
78
								'label'       => __( 'State / County', 'invoicing' ),
79
								'description' => __( 'State / County or state code', 'invoicing' ),
80
								'class'       => 'getpaid_js_field-state regular-text',
81
							),
82
							'_wpinv_phone'      => array(
83
								'label'       => __( 'Phone', 'invoicing' ),
84
								'description' => '',
85
							),
86
							'_wpinv_vat_number' => array(
87
								'label'       => __( 'VAT Number', 'invoicing' ),
88
								'description' => '',
89
							),
90
						),
91
					),
92
				)
93
			);
94
			return $show_fields;
95
		}
96
97
		/**
98
		 * Show Address Fields on edit user pages.
99
		 *
100
		 * @param WP_User $user
101
		 */
102
		public function add_customer_meta_fields( $user ) {
103
			if ( ! apply_filters( 'getpaid_current_user_can_edit_customer_meta_fields', current_user_can( 'manage_options' ), $user->ID ) ) {
104
				return;
105
			}
106
107
			$show_fields = $this->get_customer_meta_fields();
108
109
			$customer = getpaid_get_customer_by_user_id( (int) $user->ID );
110
111
			foreach ( $show_fields as $fieldset_key => $fieldset ) :
112
				?>
113
				<h2><?php echo esc_html( $fieldset['title'] ); ?></h2>
114
				<table class="form-table" id="<?php echo esc_attr( 'getpaid-fieldset-' . $fieldset_key ); ?>">
115
					<?php foreach ( $fieldset['fields'] as $key => $field ) :
116
						if ( ! empty( $customer ) ) {
117
							if ( strpos( $key, '_wpinv_' ) === 0 ) {
118
								$save_key = substr( $key , 7 );
119
							} else {
120
								$save_key = $key;
121
							}
122
123
							$value = $customer->get( $save_key );
124
						} else {
125
							$value = $this->get_user_meta( $user->ID, $key );
126
						}
127
						?>
128
						<tr>
129
							<th>
130
								<label for="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $field['label'] ); ?></label>
131
							</th>
132
							<td>
133
								<?php if ( ! empty( $field['type'] ) && 'select' === $field['type'] ) : ?>
134
									<select name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" class="<?php echo esc_attr( $field['class'] ); ?> wpi_select2" style="width: 25em;">
135
										<?php foreach ( $field['options'] as $option_key => $option_value ) : ?>
136
											<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $value, $option_key, true ); ?>><?php echo esc_html( $option_value ); ?></option>
137
										<?php endforeach; ?>
138
									</select>
139
								<?php elseif ( ! empty( $field['type'] ) && 'checkbox' === $field['type'] ) : ?>
140
									<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) $value, 1, true ); ?> />
141
								<?php else : ?>
142
									<input type="text" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( $value ); ?>" class="<?php echo ( ! empty( $field['class'] ) ? esc_attr( $field['class'] ) : 'regular-text' ); ?>" />
143
								<?php endif; ?>
144
								<p class="description"><?php echo wp_kses_post( $field['description'] ); ?></p>
145
							</td>
146
						</tr>
147
					<?php endforeach; ?>
148
				</table>
149
				<?php
150
			endforeach;
151
		}
152
153
		/**
154
		 * Save Address Fields on edit user pages.
155
		 *
156
		 * @param int $user_id User ID of the user being saved
157
		 */
158
		public function save_customer_meta_fields( $user_id ) {
159
			if ( ! apply_filters( 'getpaid_current_user_can_edit_customer_meta_fields', current_user_can( 'manage_options' ), $user_id ) ) {
160
				return;
161
			}
162
163
			$save_fields = $this->get_customer_meta_fields();
164
			$save_data = array();
165
166
			foreach ( $save_fields as $fieldset ) {
167
				foreach ( $fieldset['fields'] as $key => $field ) {
168
					if ( strpos( $key, '_wpinv_' ) === 0 ) {
169
						$save_key = substr( $key , 7 );
170
					} else {
171
						$save_key = $key;
172
					}
173
174
					if ( $save_key && isset( $field['type'] ) && 'checkbox' === $field['type'] ) {
175
						$save_data[ $save_key ] = ! empty( $_POST[ $key ] ) ? true : false;
176
					} else if ( $save_key && isset( $_POST[ $key ] ) ) {
177
						$save_data[ $save_key ] = wpinv_clean( $_POST[ $key ] );
178
					}
179
				}
180
			}
181
182
			if ( empty( $save_data ) ) {
183
				return;
184
			}
185
186
			$customer = getpaid_get_customer_by_user_id( (int) $user_id );
187
188
			if ( empty( $customer ) ) {
189
				$customer = new GetPaid_Customer( 0 );
190
				$customer->clone_user( (int) $user_id );
191
			}
192
193
			foreach ( $save_data as $key => $value ) {
194
				$customer->set( $key, $value );
195
			}
196
197
			$customer->save();
198
		}
199
200
		/**
201
		 * Get user meta for a given key, with fallbacks to core user info for pre-existing fields.
202
		 *
203
		 * @since 3.1.0
204
		 * @param int    $user_id User ID of the user being edited
205
		 * @param string $key     Key for user meta field
206
		 * @return string
207
		 */
208
		protected function get_user_meta( $user_id, $key ) {
209
			$value           = get_user_meta( $user_id, $key, true );
210
			$existing_fields = array( '_wpinv_first_name', '_wpinv_last_name' );
211
212
			if ( ! $value && in_array( $key, $existing_fields ) ) {
213
				$value = get_user_meta( $user_id, str_replace( '_wpinv_', '', $key ), true );
214
			}
215
216
			return $value;
217
		}
218
	}
219
220
endif;
221
222
return new GetPaid_Admin_Profile();
223