Passed
Push — master ( c4c78b...c15536 )
by Brian
08:03 queued 02:54
created
includes/admin/meta-boxes/class-getpaid-meta-box-invoice-address.php 1 patch
Indentation   +273 added lines, -273 removed lines patch added patch discarded remove patch
@@ -8,7 +8,7 @@  discard block
 block discarded – undo
8 8
  */
9 9
 
10 10
 if ( ! defined( 'ABSPATH' ) ) {
11
-	exit; // Exit if accessed directly
11
+    exit; // Exit if accessed directly
12 12
 }
13 13
 
14 14
 /**
@@ -16,84 +16,84 @@  discard block
 block discarded – undo
16 16
  */
17 17
 class GetPaid_Meta_Box_Invoice_Address {
18 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
-		// Maybe remove the VAT field.
91
-		if ( ! wpinv_use_taxes() ) {
92
-			unset( $address_fields['vat_number'] );
93
-		}
94
-
95
-		$address_fields = apply_filters( 'getpaid_admin_edit_invoice_address_fields', $address_fields, $invoice );
96
-		?>
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
+        // Maybe remove the VAT field.
91
+        if ( ! wpinv_use_taxes() ) {
92
+            unset( $address_fields['vat_number'] );
93
+        }
94
+
95
+        $address_fields = apply_filters( 'getpaid_admin_edit_invoice_address_fields', $address_fields, $invoice );
96
+        ?>
97 97
 
98 98
 		<style>
99 99
 			#wpinv-address label {
@@ -118,19 +118,19 @@  discard block
 block discarded – undo
118 118
 							<div id="getpaid-invoice-email-wrapper" class="d-none">
119 119
 								<input type="hidden" id="getpaid-invoice-create-new-user" name="wpinv_new_user" value="" />
120 120
 								<?php
121
-									aui()->input(
122
-										array(
123
-											'type'        => 'text',
124
-											'id'          => 'getpaid-invoice-new-user-email',
125
-											'name'        => 'wpinv_email',
126
-											'label'       => __( 'Email', 'invoicing' ) . '<span class="required">*</span>',
127
-											'label_type'  => 'vertical',
128
-											'placeholder' => '[email protected]',
129
-											'class'       => 'form-control-sm',
130
-										),
131
-										true
132
-									);
133
-								?>
121
+                                    aui()->input(
122
+                                        array(
123
+                                            'type'        => 'text',
124
+                                            'id'          => 'getpaid-invoice-new-user-email',
125
+                                            'name'        => 'wpinv_email',
126
+                                            'label'       => __( 'Email', 'invoicing' ) . '<span class="required">*</span>',
127
+                                            'label_type'  => 'vertical',
128
+                                            'placeholder' => '[email protected]',
129
+                                            'class'       => 'form-control-sm',
130
+                                        ),
131
+                                        true
132
+                                    );
133
+                                ?>
134 134
 							</div>
135 135
 						</div>
136 136
 						<div class="col-12 col-sm-6 form-group mb-3 mt-sm-4">
@@ -154,39 +154,39 @@  discard block
 block discarded – undo
154 154
 							<div class="col-12 col-sm-6 getpaid-invoice-address-field__<?php echo esc_attr( $key ); ?>--wrapper">
155 155
 								<?php
156 156
 
157
-									if ( 'select' === $field['type'] ) {
158
-										aui()->select(
159
-											array(
160
-												'id'               => 'wpinv_' . $key,
161
-												'name'             => 'wpinv_' . $key,
162
-												'label'            => $field['label'],
163
-												'label_type'       => 'vertical',
164
-												'placeholder'      => isset( $field['placeholder'] ) ? $field['placeholder'] : '',
165
-												'class'            => 'form-control-sm ' . ( isset( $field['class'] ) ? $field['class'] : '' ),
166
-												'value'            => $invoice->get( $key, 'edit' ),
167
-												'options'          => $field['options'],
168
-												'data-allow-clear' => 'false',
169
-												'select2'          => true,
170
-											),
171
-											true
172
-										);
173
-									} else {
174
-										aui()->input(
175
-											array(
176
-												'type'        => $field['type'],
177
-												'id'          => 'wpinv_' . $key,
178
-												'name'        => 'wpinv_' . $key,
179
-												'label'       => $field['label'],
180
-												'label_type'  => 'vertical',
181
-												'placeholder' => isset( $field['placeholder'] ) ? $field['placeholder'] : '',
182
-												'class'       => 'form-control-sm ' . ( isset( $field['class'] ) ? $field['class'] : '' ),
183
-												'value'       => $invoice->get( $key, 'edit' ),
184
-											),
185
-											true
186
-										);
187
-									}
188
-
189
-								?>
157
+                                    if ( 'select' === $field['type'] ) {
158
+                                        aui()->select(
159
+                                            array(
160
+                                                'id'               => 'wpinv_' . $key,
161
+                                                'name'             => 'wpinv_' . $key,
162
+                                                'label'            => $field['label'],
163
+                                                'label_type'       => 'vertical',
164
+                                                'placeholder'      => isset( $field['placeholder'] ) ? $field['placeholder'] : '',
165
+                                                'class'            => 'form-control-sm ' . ( isset( $field['class'] ) ? $field['class'] : '' ),
166
+                                                'value'            => $invoice->get( $key, 'edit' ),
167
+                                                'options'          => $field['options'],
168
+                                                'data-allow-clear' => 'false',
169
+                                                'select2'          => true,
170
+                                            ),
171
+                                            true
172
+                                        );
173
+                                    } else {
174
+                                        aui()->input(
175
+                                            array(
176
+                                                'type'        => $field['type'],
177
+                                                'id'          => 'wpinv_' . $key,
178
+                                                'name'        => 'wpinv_' . $key,
179
+                                                'label'       => $field['label'],
180
+                                                'label_type'  => 'vertical',
181
+                                                'placeholder' => isset( $field['placeholder'] ) ? $field['placeholder'] : '',
182
+                                                'class'       => 'form-control-sm ' . ( isset( $field['class'] ) ? $field['class'] : '' ),
183
+                                                'value'       => $invoice->get( $key, 'edit' ),
184
+                                            ),
185
+                                            true
186
+                                        );
187
+                                    }
188
+
189
+                                ?>
190 190
 							</div>
191 191
 						<?php endforeach; ?>
192 192
 					</div>
@@ -197,49 +197,49 @@  discard block
 block discarded – undo
197 197
 						<div class="row">
198 198
 							<div class="col-12 col-sm-6">
199 199
 								<?php
200
-									aui()->select(
201
-										array(
202
-											'id'          => 'wpinv_template',
203
-											'name'        => 'wpinv_template',
204
-											'label'       => __( 'Template', 'invoicing' ),
205
-											'label_type'  => 'vertical',
206
-											'placeholder' => __( 'Choose a template', 'invoicing' ),
207
-											'class'       => 'form-control-sm',
208
-											'value'       => $invoice->get_template( 'edit' ),
209
-											'options'     => array(
210
-												'quantity' => __( 'Quantity', 'invoicing' ),
211
-												'hours'    => __( 'Hours', 'invoicing' ),
212
-												//'amount'   => __( 'Amount Only', 'invoicing' ),
213
-											),
214
-											'data-allow-clear' => 'false',
215
-											'select2'     => true,
216
-										),
217
-										true
218
-									);
219
-								?>
200
+                                    aui()->select(
201
+                                        array(
202
+                                            'id'          => 'wpinv_template',
203
+                                            'name'        => 'wpinv_template',
204
+                                            'label'       => __( 'Template', 'invoicing' ),
205
+                                            'label_type'  => 'vertical',
206
+                                            'placeholder' => __( 'Choose a template', 'invoicing' ),
207
+                                            'class'       => 'form-control-sm',
208
+                                            'value'       => $invoice->get_template( 'edit' ),
209
+                                            'options'     => array(
210
+                                                'quantity' => __( 'Quantity', 'invoicing' ),
211
+                                                'hours'    => __( 'Hours', 'invoicing' ),
212
+                                                //'amount'   => __( 'Amount Only', 'invoicing' ),
213
+                                            ),
214
+                                            'data-allow-clear' => 'false',
215
+                                            'select2'     => true,
216
+                                        ),
217
+                                        true
218
+                                    );
219
+                                ?>
220 220
 							</div>
221 221
 							<div class="col-12 col-sm-6">
222 222
 								<?php
223 223
 
224
-									// Set currency.
225
-									aui()->select(
226
-										array(
227
-											'id'          => 'wpinv_currency',
228
-											'name'        => 'wpinv_currency',
229
-											'label'       => __( 'Currency', 'invoicing' ),
230
-											'label_type'  => 'vertical',
231
-											'placeholder' => __( 'Select Invoice Currency', 'invoicing' ),
232
-											'class'       => 'form-control-sm getpaid-recalculate-prices-on-change',
233
-											'value'       => $invoice->get_currency( 'edit' ),
234
-											'required'    => false,
235
-											'data-allow-clear' => 'false',
236
-											'select2'     => true,
237
-											'options'     => wpinv_get_currencies(),
238
-										),
239
-										true
240
-									);
241
-
242
-								?>
224
+                                    // Set currency.
225
+                                    aui()->select(
226
+                                        array(
227
+                                            'id'          => 'wpinv_currency',
228
+                                            'name'        => 'wpinv_currency',
229
+                                            'label'       => __( 'Currency', 'invoicing' ),
230
+                                            'label_type'  => 'vertical',
231
+                                            'placeholder' => __( 'Select Invoice Currency', 'invoicing' ),
232
+                                            'class'       => 'form-control-sm getpaid-recalculate-prices-on-change',
233
+                                            'value'       => $invoice->get_currency( 'edit' ),
234
+                                            'required'    => false,
235
+                                            'data-allow-clear' => 'false',
236
+                                            'select2'     => true,
237
+                                            'options'     => wpinv_get_currencies(),
238
+                                        ),
239
+                                        true
240
+                                    );
241
+
242
+                                ?>
243 243
 							</div>
244 244
 						</div>
245 245
 
@@ -249,119 +249,119 @@  discard block
 block discarded – undo
249 249
 					<div class="row">
250 250
 						<div class="col-12 col-sm-6">
251 251
 							<?php
252
-								aui()->input(
253
-									array(
254
-										'type'        => 'text',
255
-										'id'          => 'wpinv_company_id',
256
-										'name'        => 'wpinv_company_id',
257
-										'label'       => __( 'Company ID', 'invoicing' ),
258
-										'label_type'  => 'vertical',
259
-										'placeholder' => '',
260
-										'class'       => 'form-control-sm',
261
-										'value'       => $invoice->get_company_id( 'edit' ),
262
-									),
263
-									true
264
-								);
265
-							?>
252
+                                aui()->input(
253
+                                    array(
254
+                                        'type'        => 'text',
255
+                                        'id'          => 'wpinv_company_id',
256
+                                        'name'        => 'wpinv_company_id',
257
+                                        'label'       => __( 'Company ID', 'invoicing' ),
258
+                                        'label_type'  => 'vertical',
259
+                                        'placeholder' => '',
260
+                                        'class'       => 'form-control-sm',
261
+                                        'value'       => $invoice->get_company_id( 'edit' ),
262
+                                    ),
263
+                                    true
264
+                                );
265
+                            ?>
266 266
 						</div>
267 267
 					</div>
268 268
 
269 269
 					<?php do_action( 'getpaid_after_metabox_invoice_address', $invoice ); ?>
270 270
 			</div>
271 271
 		<?php
272
-	}
273
-
274
-	/**
275
-	 * Save meta box data.
276
-	 *
277
-	 * @param int $post_id
278
-	 */
279
-	public static function save( $post_id ) {
280
-
281
-		// Prepare the invoice.
282
-		$invoice = new WPInv_Invoice( $post_id );
283
-
284
-		// Load new data.
285
-		$invoice->set_props(
286
-			array(
287
-				'template'       => isset( $_POST['wpinv_template'] ) ? wpinv_clean( $_POST['wpinv_template'] ) : null,
288
-				'email_cc'       => isset( $_POST['wpinv_cc'] ) ? wpinv_clean( $_POST['wpinv_cc'] ) : null,
289
-				'disable_taxes'  => ! empty( $_POST['disable_taxes'] ),
290
-				'currency'       => isset( $_POST['wpinv_currency'] ) ? wpinv_clean( $_POST['wpinv_currency'] ) : null,
291
-				'gateway'        => ( $invoice->needs_payment() && isset( $_POST['wpinv_gateway'] ) ) ? wpinv_clean( $_POST['wpinv_gateway'] ) : null,
292
-				'address'        => isset( $_POST['wpinv_address'] ) ? wpinv_clean( $_POST['wpinv_address'] ) : null,
293
-				'vat_number'     => isset( $_POST['wpinv_vat_number'] ) ? wpinv_clean( $_POST['wpinv_vat_number'] ) : null,
294
-				'company'        => isset( $_POST['wpinv_company'] ) ? wpinv_clean( $_POST['wpinv_company'] ) : null,
295
-				'company_id'     => isset( $_POST['wpinv_company_id'] ) ? wpinv_clean( $_POST['wpinv_company_id'] ) : null,
296
-				'zip'            => isset( $_POST['wpinv_zip'] ) ? wpinv_clean( $_POST['wpinv_zip'] ) : null,
297
-				'state'          => isset( $_POST['wpinv_state'] ) ? wpinv_clean( $_POST['wpinv_state'] ) : null,
298
-				'city'           => isset( $_POST['wpinv_city'] ) ? wpinv_clean( $_POST['wpinv_city'] ) : null,
299
-				'country'        => isset( $_POST['wpinv_country'] ) ? wpinv_clean( $_POST['wpinv_country'] ) : null,
300
-				'phone'          => isset( $_POST['wpinv_phone'] ) ? wpinv_clean( $_POST['wpinv_phone'] ) : null,
301
-				'first_name'     => isset( $_POST['wpinv_first_name'] ) ? wpinv_clean( $_POST['wpinv_first_name'] ) : null,
302
-				'last_name'      => isset( $_POST['wpinv_last_name'] ) ? wpinv_clean( $_POST['wpinv_last_name'] ) : null,
303
-				'author'         => isset( $_POST['post_author_override'] ) ? wpinv_clean( $_POST['post_author_override'] ) : null,
304
-				'date_created'   => isset( $_POST['date_created'] ) ? wpinv_clean( $_POST['date_created'] ) : null,
305
-				'date_completed' => isset( $_POST['wpinv_date_completed'] ) ? wpinv_clean( $_POST['wpinv_date_completed'] ) : null,
306
-				'due_date'       => isset( $_POST['wpinv_due_date'] ) ? wpinv_clean( $_POST['wpinv_due_date'] ) : null,
307
-				'number'         => isset( $_POST['wpinv_number'] ) ? wpinv_clean( $_POST['wpinv_number'] ) : null,
308
-				'status'         => isset( $_POST['wpinv_status'] ) ? wpinv_clean( $_POST['wpinv_status'] ) : null,
309
-			)
310
-		);
311
-
312
-		// Discount code.
313
-		if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) {
314
-
315
-			if ( isset( $_POST['wpinv_discount_code'] ) ) {
316
-				$invoice->set_discount_code( wpinv_clean( $_POST['wpinv_discount_code'] ) );
317
-			}
318
-
319
-			$discount = new WPInv_Discount( $invoice->get_discount_code() );
320
-			if ( $discount->exists() ) {
321
-				$invoice->add_discount( getpaid_calculate_invoice_discount( $invoice, $discount ) );
322
-			} else {
323
-				$invoice->remove_discount( 'discount_code' );
324
-			}
325
-
326
-			// Recalculate totals.
327
-			$invoice->recalculate_total();
328
-
329
-		}
330
-
331
-		// If we're creating a new user...
332
-		if ( ! empty( $_POST['wpinv_new_user'] ) && is_email( stripslashes( $_POST['wpinv_email'] ) ) ) {
333
-
334
-			// Attempt to create the user.
335
-			$user = wpinv_create_user( sanitize_email( stripslashes( $_POST['wpinv_email'] ) ), $invoice->get_first_name() . $invoice->get_last_name() );
336
-
337
-			// If successful, update the invoice author.
338
-			if ( is_numeric( $user ) ) {
339
-				$invoice->set_author( $user );
340
-			} else {
341
-				wpinv_error_log( $user->get_error_message(), __( 'Invoice add new user', 'invoicing' ), __FILE__, __LINE__ );
342
-			}
343
-		}
344
-
345
-		// Do not send new invoice notifications.
346
-		$GLOBALS['wpinv_skip_invoice_notification'] = true;
347
-
348
-		// Save the invoice.
349
-		$invoice->save();
350
-
351
-		// Undo do not send new invoice notifications.
352
-		$GLOBALS['wpinv_skip_invoice_notification'] = false;
353
-
354
-		// (Maybe) send new user notification.
355
-		$should_send_notification = wpinv_get_option( 'disable_new_user_emails' );
356
-		if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ) ) ) {
357
-			wp_send_new_user_notifications( $user, 'user' );
358
-		}
359
-
360
-		if ( ! empty( $_POST['send_to_customer'] ) && ! $invoice->is_draft() ) {
361
-			getpaid()->get( 'invoice_emails' )->user_invoice( $invoice, true );
362
-		}
363
-
364
-		// Fires after an invoice is saved.
365
-		do_action( 'wpinv_invoice_metabox_saved', $invoice );
366
-	}
272
+    }
273
+
274
+    /**
275
+     * Save meta box data.
276
+     *
277
+     * @param int $post_id
278
+     */
279
+    public static function save( $post_id ) {
280
+
281
+        // Prepare the invoice.
282
+        $invoice = new WPInv_Invoice( $post_id );
283
+
284
+        // Load new data.
285
+        $invoice->set_props(
286
+            array(
287
+                'template'       => isset( $_POST['wpinv_template'] ) ? wpinv_clean( $_POST['wpinv_template'] ) : null,
288
+                'email_cc'       => isset( $_POST['wpinv_cc'] ) ? wpinv_clean( $_POST['wpinv_cc'] ) : null,
289
+                'disable_taxes'  => ! empty( $_POST['disable_taxes'] ),
290
+                'currency'       => isset( $_POST['wpinv_currency'] ) ? wpinv_clean( $_POST['wpinv_currency'] ) : null,
291
+                'gateway'        => ( $invoice->needs_payment() && isset( $_POST['wpinv_gateway'] ) ) ? wpinv_clean( $_POST['wpinv_gateway'] ) : null,
292
+                'address'        => isset( $_POST['wpinv_address'] ) ? wpinv_clean( $_POST['wpinv_address'] ) : null,
293
+                'vat_number'     => isset( $_POST['wpinv_vat_number'] ) ? wpinv_clean( $_POST['wpinv_vat_number'] ) : null,
294
+                'company'        => isset( $_POST['wpinv_company'] ) ? wpinv_clean( $_POST['wpinv_company'] ) : null,
295
+                'company_id'     => isset( $_POST['wpinv_company_id'] ) ? wpinv_clean( $_POST['wpinv_company_id'] ) : null,
296
+                'zip'            => isset( $_POST['wpinv_zip'] ) ? wpinv_clean( $_POST['wpinv_zip'] ) : null,
297
+                'state'          => isset( $_POST['wpinv_state'] ) ? wpinv_clean( $_POST['wpinv_state'] ) : null,
298
+                'city'           => isset( $_POST['wpinv_city'] ) ? wpinv_clean( $_POST['wpinv_city'] ) : null,
299
+                'country'        => isset( $_POST['wpinv_country'] ) ? wpinv_clean( $_POST['wpinv_country'] ) : null,
300
+                'phone'          => isset( $_POST['wpinv_phone'] ) ? wpinv_clean( $_POST['wpinv_phone'] ) : null,
301
+                'first_name'     => isset( $_POST['wpinv_first_name'] ) ? wpinv_clean( $_POST['wpinv_first_name'] ) : null,
302
+                'last_name'      => isset( $_POST['wpinv_last_name'] ) ? wpinv_clean( $_POST['wpinv_last_name'] ) : null,
303
+                'author'         => isset( $_POST['post_author_override'] ) ? wpinv_clean( $_POST['post_author_override'] ) : null,
304
+                'date_created'   => isset( $_POST['date_created'] ) ? wpinv_clean( $_POST['date_created'] ) : null,
305
+                'date_completed' => isset( $_POST['wpinv_date_completed'] ) ? wpinv_clean( $_POST['wpinv_date_completed'] ) : null,
306
+                'due_date'       => isset( $_POST['wpinv_due_date'] ) ? wpinv_clean( $_POST['wpinv_due_date'] ) : null,
307
+                'number'         => isset( $_POST['wpinv_number'] ) ? wpinv_clean( $_POST['wpinv_number'] ) : null,
308
+                'status'         => isset( $_POST['wpinv_status'] ) ? wpinv_clean( $_POST['wpinv_status'] ) : null,
309
+            )
310
+        );
311
+
312
+        // Discount code.
313
+        if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) {
314
+
315
+            if ( isset( $_POST['wpinv_discount_code'] ) ) {
316
+                $invoice->set_discount_code( wpinv_clean( $_POST['wpinv_discount_code'] ) );
317
+            }
318
+
319
+            $discount = new WPInv_Discount( $invoice->get_discount_code() );
320
+            if ( $discount->exists() ) {
321
+                $invoice->add_discount( getpaid_calculate_invoice_discount( $invoice, $discount ) );
322
+            } else {
323
+                $invoice->remove_discount( 'discount_code' );
324
+            }
325
+
326
+            // Recalculate totals.
327
+            $invoice->recalculate_total();
328
+
329
+        }
330
+
331
+        // If we're creating a new user...
332
+        if ( ! empty( $_POST['wpinv_new_user'] ) && is_email( stripslashes( $_POST['wpinv_email'] ) ) ) {
333
+
334
+            // Attempt to create the user.
335
+            $user = wpinv_create_user( sanitize_email( stripslashes( $_POST['wpinv_email'] ) ), $invoice->get_first_name() . $invoice->get_last_name() );
336
+
337
+            // If successful, update the invoice author.
338
+            if ( is_numeric( $user ) ) {
339
+                $invoice->set_author( $user );
340
+            } else {
341
+                wpinv_error_log( $user->get_error_message(), __( 'Invoice add new user', 'invoicing' ), __FILE__, __LINE__ );
342
+            }
343
+        }
344
+
345
+        // Do not send new invoice notifications.
346
+        $GLOBALS['wpinv_skip_invoice_notification'] = true;
347
+
348
+        // Save the invoice.
349
+        $invoice->save();
350
+
351
+        // Undo do not send new invoice notifications.
352
+        $GLOBALS['wpinv_skip_invoice_notification'] = false;
353
+
354
+        // (Maybe) send new user notification.
355
+        $should_send_notification = wpinv_get_option( 'disable_new_user_emails' );
356
+        if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ) ) ) {
357
+            wp_send_new_user_notifications( $user, 'user' );
358
+        }
359
+
360
+        if ( ! empty( $_POST['send_to_customer'] ) && ! $invoice->is_draft() ) {
361
+            getpaid()->get( 'invoice_emails' )->user_invoice( $invoice, true );
362
+        }
363
+
364
+        // Fires after an invoice is saved.
365
+        do_action( 'wpinv_invoice_metabox_saved', $invoice );
366
+    }
367 367
 }
Please login to merge, or discard this patch.