Passed
Push — master ( 9525b5...c4c78b )
by Brian
05:18
created

GetPaid_Meta_Box_Invoice_Address::output()   C

Complexity

Conditions 12
Paths 48

Size

Total Lines 242
Code Lines 196

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 196
c 0
b 0
f 0
nc 48
nop 1
dl 0
loc 242
rs 5.5733

How to fix   Long Method    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
/**
4
 * Invoice Address
5
 *
6
 * Display the invoice address meta box.
7
 *
8
 */
9
10
if ( ! defined( 'ABSPATH' ) ) {
11
	exit; // Exit if accessed directly
12
}
13
14
/**
15
 * GetPaid_Meta_Box_Invoice_Address Class.
16
 */
17
class GetPaid_Meta_Box_Invoice_Address {
18
19
	/**
20
	 * Output the metabox.
21
	 *
22
	 * @param WP_Post $post
23
	 */
24
	public static function output( $post ) {
25
26
		// Prepare the invoice.
27
		$invoice  = new WPInv_Invoice( $post );
28
		$customer = $invoice->exists() ? $invoice->get_user_id( 'edit' ) : get_current_user_id();
29
		$customer = new WP_User( $customer );
30
		$display  = sprintf( _x( '%1$s (%2$s)', 'user dropdown', 'invoicing' ), $customer->display_name, $customer->user_email );
31
		wp_nonce_field( 'getpaid_meta_nonce', 'getpaid_meta_nonce' );
32
33
		// Address fields.
34
		$address_fields = array(
35
			'first_name' => array(
36
				'label' => __( 'First Name', 'invoicing' ),
37
				'type'  => 'text',
38
			),
39
			'last_name'  => array(
40
				'label' => __( 'Last Name', 'invoicing' ),
41
				'type'  => 'text',
42
			),
43
			'company'    => array(
44
				'label' => __( 'Company', 'invoicing' ),
45
				'type'  => 'text',
46
				'class' => 'getpaid-recalculate-prices-on-change',
47
			),
48
			'vat_number' => array(
49
				'label' => __( 'VAT Number', 'invoicing' ),
50
				'type'  => 'text',
51
			),
52
			'address'    => array(
53
				'label' => __( 'Address', 'invoicing' ),
54
				'type'  => 'text',
55
			),
56
			'city'       => array(
57
				'label' => __( 'City', 'invoicing' ),
58
				'type'  => 'text',
59
			),
60
			'country'    => array(
61
				'label'       => __( 'Country', 'invoicing' ),
62
				'type'        => 'select',
63
				'class'       => 'getpaid-recalculate-prices-on-change',
64
				'options'     => wpinv_get_country_list(),
65
				'placeholder' => __( 'Choose a country', 'invoicing' ),
66
			),
67
			'state'      => array(
68
				'label' => __( 'State', 'invoicing' ),
69
				'type'  => 'text',
70
				'class' => 'getpaid-recalculate-prices-on-change',
71
			),
72
			'zip'        => array(
73
				'label' => __( 'Zip', 'invoicing' ),
74
				'type'  => 'text',
75
			),
76
			'phone'      => array(
77
				'label' => __( 'Phone', 'invoicing' ),
78
				'type'  => 'text',
79
			),
80
		);
81
82
		$states = wpinv_get_country_states( $invoice->get_country( 'edit' ) );
83
84
		if ( ! empty( $states ) ) {
85
			$address_fields['state']['type']        = 'select';
86
			$address_fields['state']['options']     = $states;
87
			$address_fields['state']['placeholder'] = __( 'Choose a state', 'invoicing' );
88
		}
89
90
		$address_fields = apply_filters( 'getpaid_admin_edit_invoice_address_fields', $address_fields, $invoice );
91
		?>
92
93
		<style>
94
			#wpinv-address label {
95
				margin-bottom: 3px;
96
				font-weight: 600;
97
			}
98
		</style>
99
			<div class="bsui" style="margin-top: 1.5rem; max-width: 820px;">
100
					<div class="row">
101
						<div class="col-12 col-sm-6">
102
							<div id="getpaid-invoice-user-id-wrapper" class="form-group mb-3">
103
								<div>
104
									<label for="post_author_override"><?php esc_html_e( 'Customer', 'invoicing' ); ?></label>
105
								</div>
106
								<div>
107
									<select name="post_author_override" id="wpinv_post_author_override" class="getpaid-customer-search form-control regular-text" data-placeholder="<?php esc_attr_e( 'Search for a customer by email or name', 'invoicing' ); ?>">
108
										<option selected="selected" value="<?php echo (int) $customer->ID; ?>"><?php echo esc_html( $display ); ?> </option>)
109
									</select>
110
								</div>
111
							</div>
112
113
							<div id="getpaid-invoice-email-wrapper" class="d-none">
114
								<input type="hidden" id="getpaid-invoice-create-new-user" name="wpinv_new_user" value="" />
115
								<?php
116
									aui()->input(
117
										array(
118
											'type'        => 'text',
119
											'id'          => 'getpaid-invoice-new-user-email',
120
											'name'        => 'wpinv_email',
121
											'label'       => __( 'Email', 'invoicing' ) . '<span class="required">*</span>',
122
											'label_type'  => 'vertical',
123
											'placeholder' => '[email protected]',
124
											'class'       => 'form-control-sm',
125
										),
126
										true
127
									);
128
								?>
129
							</div>
130
						</div>
131
						<div class="col-12 col-sm-6 form-group mb-3 mt-sm-4">
132
							<?php if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) : ?>
133
								<a id="getpaid-invoice-fill-user-details" class="button button-small button-secondary" href="javascript:void(0)">
134
									<i aria-hidden="true" class="fa fa-refresh"></i>
135
									<?php esc_html_e( 'Fill User Details', 'invoicing' ); ?>
136
								</a>
137
								<a id="getpaid-invoice-create-new-user-button" class="button button-small button-secondary" href="javascript:void(0)">
138
									<i aria-hidden="true" class="fa fa-plus"></i>
139
									<?php esc_html_e( 'Add New User', 'invoicing' ); ?>
140
								</a>
141
								<a id="getpaid-invoice-cancel-create-new-user" class="button button-small button-secondary d-none" href="javascript:void(0)">
142
									<i aria-hidden="true" class="fa fa-close"></i>
143
									<?php esc_html_e( 'Cancel', 'invoicing' ); ?>
144
								</a>
145
							<?php endif; ?>
146
						</div>
147
148
						<?php foreach ( $address_fields as $key => $field ) : ?>
149
							<div class="col-12 col-sm-6 getpaid-invoice-address-field__<?php echo esc_attr( $key ); ?>--wrapper">
150
								<?php
151
152
									if ( 'select' === $field['type'] ) {
153
										aui()->select(
154
											array(
155
												'id'               => 'wpinv_' . $key,
156
												'name'             => 'wpinv_' . $key,
157
												'label'            => $field['label'],
158
												'label_type'       => 'vertical',
159
												'placeholder'      => isset( $field['placeholder'] ) ? $field['placeholder'] : '',
160
												'class'            => 'form-control-sm ' . ( isset( $field['class'] ) ? $field['class'] : '' ),
161
												'value'            => $invoice->get( $key, 'edit' ),
162
												'options'          => $field['options'],
163
												'data-allow-clear' => 'false',
164
												'select2'          => true,
165
											),
166
											true
167
										);
168
									} else {
169
										aui()->input(
170
											array(
171
												'type'        => $field['type'],
172
												'id'          => 'wpinv_' . $key,
173
												'name'        => 'wpinv_' . $key,
174
												'label'       => $field['label'],
175
												'label_type'  => 'vertical',
176
												'placeholder' => isset( $field['placeholder'] ) ? $field['placeholder'] : '',
177
												'class'       => 'form-control-sm ' . ( isset( $field['class'] ) ? $field['class'] : '' ),
178
												'value'       => $invoice->get( $key, 'edit' ),
179
											),
180
											true
181
										);
182
									}
183
184
								?>
185
							</div>
186
						<?php endforeach; ?>
187
					</div>
188
189
					<?php if ( ! apply_filters( 'getpaid_use_new_invoice_items_metabox', false ) ) : ?>
190
						<?php do_action( 'wpinv_meta_box_before_invoice_template_row', $invoice->get_id() ); ?>
191
192
						<div class="row">
193
							<div class="col-12 col-sm-6">
194
								<?php
195
									aui()->select(
196
										array(
197
											'id'          => 'wpinv_template',
198
											'name'        => 'wpinv_template',
199
											'label'       => __( 'Template', 'invoicing' ),
200
											'label_type'  => 'vertical',
201
											'placeholder' => __( 'Choose a template', 'invoicing' ),
202
											'class'       => 'form-control-sm',
203
											'value'       => $invoice->get_template( 'edit' ),
204
											'options'     => array(
205
												'quantity' => __( 'Quantity', 'invoicing' ),
206
												'hours'    => __( 'Hours', 'invoicing' ),
207
												//'amount'   => __( 'Amount Only', 'invoicing' ),
208
											),
209
											'data-allow-clear' => 'false',
210
											'select2'     => true,
211
										),
212
										true
213
									);
214
								?>
215
							</div>
216
							<div class="col-12 col-sm-6">
217
								<?php
218
219
									// Set currency.
220
									aui()->select(
221
										array(
222
											'id'          => 'wpinv_currency',
223
											'name'        => 'wpinv_currency',
224
											'label'       => __( 'Currency', 'invoicing' ),
225
											'label_type'  => 'vertical',
226
											'placeholder' => __( 'Select Invoice Currency', 'invoicing' ),
227
											'class'       => 'form-control-sm getpaid-recalculate-prices-on-change',
228
											'value'       => $invoice->get_currency( 'edit' ),
229
											'required'    => false,
230
											'data-allow-clear' => 'false',
231
											'select2'     => true,
232
											'options'     => wpinv_get_currencies(),
233
										),
234
										true
235
									);
236
237
								?>
238
							</div>
239
						</div>
240
241
						<?php do_action( 'wpinv_meta_box_invoice_template_row', $invoice->get_id() ); ?>
242
					<?php endif; ?>
243
244
					<div class="row">
245
						<div class="col-12 col-sm-6">
246
							<?php
247
								aui()->input(
248
									array(
249
										'type'        => 'text',
250
										'id'          => 'wpinv_company_id',
251
										'name'        => 'wpinv_company_id',
252
										'label'       => __( 'Company ID', 'invoicing' ),
253
										'label_type'  => 'vertical',
254
										'placeholder' => '',
255
										'class'       => 'form-control-sm',
256
										'value'       => $invoice->get_company_id( 'edit' ),
257
									),
258
									true
259
								);
260
							?>
261
						</div>
262
					</div>
263
264
					<?php do_action( 'getpaid_after_metabox_invoice_address', $invoice ); ?>
265
			</div>
266
		<?php
267
	}
268
269
	/**
270
	 * Save meta box data.
271
	 *
272
	 * @param int $post_id
273
	 */
274
	public static function save( $post_id ) {
275
276
		// Prepare the invoice.
277
		$invoice = new WPInv_Invoice( $post_id );
278
279
		// Load new data.
280
		$invoice->set_props(
281
			array(
282
				'template'       => isset( $_POST['wpinv_template'] ) ? wpinv_clean( $_POST['wpinv_template'] ) : null,
283
				'email_cc'       => isset( $_POST['wpinv_cc'] ) ? wpinv_clean( $_POST['wpinv_cc'] ) : null,
284
				'disable_taxes'  => ! empty( $_POST['disable_taxes'] ),
285
				'currency'       => isset( $_POST['wpinv_currency'] ) ? wpinv_clean( $_POST['wpinv_currency'] ) : null,
286
				'gateway'        => ( $invoice->needs_payment() && isset( $_POST['wpinv_gateway'] ) ) ? wpinv_clean( $_POST['wpinv_gateway'] ) : null,
287
				'address'        => isset( $_POST['wpinv_address'] ) ? wpinv_clean( $_POST['wpinv_address'] ) : null,
288
				'vat_number'     => isset( $_POST['wpinv_vat_number'] ) ? wpinv_clean( $_POST['wpinv_vat_number'] ) : null,
289
				'company'        => isset( $_POST['wpinv_company'] ) ? wpinv_clean( $_POST['wpinv_company'] ) : null,
290
				'company_id'     => isset( $_POST['wpinv_company_id'] ) ? wpinv_clean( $_POST['wpinv_company_id'] ) : null,
291
				'zip'            => isset( $_POST['wpinv_zip'] ) ? wpinv_clean( $_POST['wpinv_zip'] ) : null,
292
				'state'          => isset( $_POST['wpinv_state'] ) ? wpinv_clean( $_POST['wpinv_state'] ) : null,
293
				'city'           => isset( $_POST['wpinv_city'] ) ? wpinv_clean( $_POST['wpinv_city'] ) : null,
294
				'country'        => isset( $_POST['wpinv_country'] ) ? wpinv_clean( $_POST['wpinv_country'] ) : null,
295
				'phone'          => isset( $_POST['wpinv_phone'] ) ? wpinv_clean( $_POST['wpinv_phone'] ) : null,
296
				'first_name'     => isset( $_POST['wpinv_first_name'] ) ? wpinv_clean( $_POST['wpinv_first_name'] ) : null,
297
				'last_name'      => isset( $_POST['wpinv_last_name'] ) ? wpinv_clean( $_POST['wpinv_last_name'] ) : null,
298
				'author'         => isset( $_POST['post_author_override'] ) ? wpinv_clean( $_POST['post_author_override'] ) : null,
299
				'date_created'   => isset( $_POST['date_created'] ) ? wpinv_clean( $_POST['date_created'] ) : null,
300
				'date_completed' => isset( $_POST['wpinv_date_completed'] ) ? wpinv_clean( $_POST['wpinv_date_completed'] ) : null,
301
				'due_date'       => isset( $_POST['wpinv_due_date'] ) ? wpinv_clean( $_POST['wpinv_due_date'] ) : null,
302
				'number'         => isset( $_POST['wpinv_number'] ) ? wpinv_clean( $_POST['wpinv_number'] ) : null,
303
				'status'         => isset( $_POST['wpinv_status'] ) ? wpinv_clean( $_POST['wpinv_status'] ) : null,
304
			)
305
		);
306
307
		// Discount code.
308
		if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) {
309
310
			if ( isset( $_POST['wpinv_discount_code'] ) ) {
311
				$invoice->set_discount_code( wpinv_clean( $_POST['wpinv_discount_code'] ) );
0 ignored issues
show
Bug introduced by
It seems like wpinv_clean($_POST['wpinv_discount_code']) can also be of type array; however, parameter $value of WPInv_Invoice::set_discount_code() 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

311
				$invoice->set_discount_code( /** @scrutinizer ignore-type */ wpinv_clean( $_POST['wpinv_discount_code'] ) );
Loading history...
312
			}
313
314
			$discount = new WPInv_Discount( $invoice->get_discount_code() );
315
			if ( $discount->exists() ) {
316
				$invoice->add_discount( getpaid_calculate_invoice_discount( $invoice, $discount ) );
317
			} else {
318
				$invoice->remove_discount( 'discount_code' );
319
			}
320
321
			// Recalculate totals.
322
			$invoice->recalculate_total();
323
324
		}
325
326
		// If we're creating a new user...
327
		if ( ! empty( $_POST['wpinv_new_user'] ) && is_email( stripslashes( $_POST['wpinv_email'] ) ) ) {
328
329
			// Attempt to create the user.
330
			$user = wpinv_create_user( sanitize_email( stripslashes( $_POST['wpinv_email'] ) ), $invoice->get_first_name() . $invoice->get_last_name() );
331
332
			// If successful, update the invoice author.
333
			if ( is_numeric( $user ) ) {
0 ignored issues
show
introduced by
The condition is_numeric($user) is always false.
Loading history...
334
				$invoice->set_author( $user );
335
			} else {
336
				wpinv_error_log( $user->get_error_message(), __( 'Invoice add new user', 'invoicing' ), __FILE__, __LINE__ );
337
			}
338
		}
339
340
		// Do not send new invoice notifications.
341
		$GLOBALS['wpinv_skip_invoice_notification'] = true;
342
343
		// Save the invoice.
344
		$invoice->save();
345
346
		// Undo do not send new invoice notifications.
347
		$GLOBALS['wpinv_skip_invoice_notification'] = false;
348
349
		// (Maybe) send new user notification.
350
		$should_send_notification = wpinv_get_option( 'disable_new_user_emails' );
351
		if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ) ) ) {
352
			wp_send_new_user_notifications( $user, 'user' );
353
		}
354
355
		if ( ! empty( $_POST['send_to_customer'] ) && ! $invoice->is_draft() ) {
356
			getpaid()->get( 'invoice_emails' )->user_invoice( $invoice, true );
357
		}
358
359
		// Fires after an invoice is saved.
360
		do_action( 'wpinv_invoice_metabox_saved', $invoice );
361
	}
362
}
363