Passed
Push — master ( 3232cf...3b5782 )
by Brian
276:38 queued 189:29
created

GetPaid_Admin_Profile::add_customer_meta_fields()   B

Complexity

Conditions 10
Paths 6

Size

Total Lines 37
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 30
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 37
rs 7.6666

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
 */
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_address'  => array(
55
								'label'       => __( 'Address', 'invoicing' ),
56
								'description' => '',
57
							),
58
							'_wpinv_city'       => array(
59
								'label'       => __( 'City', 'invoicing' ),
60
								'description' => '',
61
							),
62
							'_wpinv_zip'   => array(
63
								'label'       => __( 'Postcode / ZIP', 'invoicing' ),
64
								'description' => '',
65
							),
66
							'_wpinv_country'    => array(
67
								'label'       => __( 'Country / Region', 'invoicing' ),
68
								'description' => '',
69
								'class'       => 'getpaid_js_field-country',
70
								'type'        => 'select',
71
								'options'     => array( '' => __( 'Select a country / region&hellip;', 'invoicing' ) ) + wpinv_get_country_list(),
72
							),
73
							'_wpinv_state'      => array(
74
								'label'       => __( 'State / County', 'invoicing' ),
75
								'description' => __( 'State / County or state code', 'invoicing' ),
76
								'class'       => 'getpaid_js_field-state regular-text',
77
							),
78
							'_wpinv_phone'      => array(
79
								'label'       => __( 'Phone', 'invoicing' ),
80
								'description' => '',
81
							),
82
							'_wpinv_vat_number'      => array(
83
								'label'       => __( 'VAT Number', 'invoicing' ),
84
								'description' => '',
85
							),
86
						),
87
					),
88
				)
89
			);
90
			return $show_fields;
91
		}
92
93
		/**
94
		 * Show Address Fields on edit user pages.
95
		 *
96
		 * @param WP_User $user
97
		 */
98
		public function add_customer_meta_fields( $user ) {
99
100
			if ( ! apply_filters( 'getpaid_current_user_can_edit_customer_meta_fields', current_user_can( 'manage_options' ), $user->ID ) ) {
101
				return;
102
			}
103
104
			$show_fields = $this->get_customer_meta_fields();
105
106
			foreach ( $show_fields as $fieldset_key => $fieldset ) :
107
				?>
108
				<h2><?php echo $fieldset['title']; ?></h2>
109
				<table class="form-table" id="<?php echo esc_attr( 'getpaid-fieldset-' . $fieldset_key ); ?>">
110
					<?php foreach ( $fieldset['fields'] as $key => $field ) : ?>
111
						<tr>
112
							<th>
113
								<label for="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $field['label'] ); ?></label>
114
							</th>
115
							<td>
116
								<?php if ( ! empty( $field['type'] ) && 'select' === $field['type'] ) : ?>
117
									<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;">
118
										<?php
119
											$selected = esc_attr( get_user_meta( $user->ID, $key, true ) );
0 ignored issues
show
Bug introduced by
It seems like get_user_meta($user->ID, $key, true) can also be of type false; however, parameter $text of esc_attr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

119
											$selected = esc_attr( /** @scrutinizer ignore-type */ get_user_meta( $user->ID, $key, true ) );
Loading history...
120
										foreach ( $field['options'] as $option_key => $option_value ) :
121
											?>
122
											<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $selected, $option_key, true ); ?>><?php echo esc_html( $option_value ); ?></option>
123
										<?php endforeach; ?>
124
									</select>
125
								<?php elseif ( ! empty( $field['type'] ) && 'checkbox' === $field['type'] ) : ?>
126
									<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 ); ?> />
127
								<?php else : ?>
128
									<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' ); ?>" />
129
								<?php endif; ?>
130
								<p class="description"><?php echo wp_kses_post( $field['description'] ); ?></p>
131
							</td>
132
						</tr>
133
					<?php endforeach; ?>
134
				</table>
135
				<?php
136
			endforeach;
137
		}
138
139
		/**
140
		 * Save Address Fields on edit user pages.
141
		 *
142
		 * @param int $user_id User ID of the user being saved
143
		 */
144
		public function save_customer_meta_fields( $user_id ) {
145
			if ( ! apply_filters( 'getpaid_current_user_can_edit_customer_meta_fields', current_user_can( 'manage_options' ), $user_id ) ) {
146
				return;
147
			}
148
149
			$save_fields = $this->get_customer_meta_fields();
150
151
			foreach ( $save_fields as $fieldset ) {
152
153
				foreach ( $fieldset['fields'] as $key => $field ) {
154
155
					if ( isset( $field['type'] ) && 'checkbox' === $field['type'] ) {
156
						update_user_meta( $user_id, $key, isset( $_POST[ $key ] ) );
157
					} elseif ( isset( $_POST[ $key ] ) ) {
158
						update_user_meta( $user_id, $key, wpinv_clean( $_POST[ $key ] ) );
159
					}
160
				}
161
			}
162
		}
163
164
		/**
165
		 * Get user meta for a given key, with fallbacks to core user info for pre-existing fields.
166
		 *
167
		 * @since 3.1.0
168
		 * @param int    $user_id User ID of the user being edited
169
		 * @param string $key     Key for user meta field
170
		 * @return string
171
		 */
172
		protected function get_user_meta( $user_id, $key ) {
173
			$value           = get_user_meta( $user_id, $key, true );
174
			$existing_fields = array( '_wpinv_first_name', '_wpinv_last_name' );
175
			if ( ! $value && in_array( $key, $existing_fields ) ) {
176
				$value = get_user_meta( $user_id, str_replace( '_wpinv_', '', $key ), true );
177
			}
178
179
			return $value;
180
		}
181
	}
182
183
endif;
184
185
return new GetPaid_Admin_Profile();