Passed
Push — master ( 10624e...03e582 )
by Brian
04:27
created

GetPaid_Meta_Box_Invoice_Address   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 452
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 341
dl 0
loc 452
rs 9.0399
c 1
b 0
f 0
wmc 42

2 Methods

Rating   Name   Duplication   Size   Complexity  
D save() 0 96 36
B output() 0 341 6

How to fix   Complexity   

Complex Class

Complex classes like GetPaid_Meta_Box_Invoice_Address often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use GetPaid_Meta_Box_Invoice_Address, and based on these observations, apply Extract Interface, too.

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
        ?>
34
35
        <style>
36
            #wpinv-address label {
37
                margin-bottom: 3px;
38
                font-weight: 600;
39
            }
40
        </style>
41
            <div class="bsui" style="margin-top: 1.5rem; max-width: 820px;">
42
                    <div class="row">
43
                        <div class="col-12 col-sm-6">
44
                            <div id="getpaid-invoice-user-id-wrapper" class="form-group">
45
                                <div>
46
                                    <label for="post_author_override"><?php _e( 'Customer', 'invoicing' );?></label>
47
                                </div>
48
                                <div>
49
                                    <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' ); ?>">
50
                                        <option selected="selected" value="<?php echo (int) $customer->ID; ?>"><?php echo sanitize_text_field( $display ); ?> </option>)
51
                                    </select>
52
                                </div>
53
                            </div>
54
55
                            <div id="getpaid-invoice-email-wrapper" class="d-none">
56
                                <input type="hidden" id="getpaid-invoice-create-new-user" name="wpinv_new_user" value="" />
57
                                <?php
58
                                    echo aui()->input(
59
                                        array(
60
                                            'type'        => 'text',
61
                                            'id'          => 'getpaid-invoice-new-user-email',
62
                                            'name'        => 'wpinv_email',
63
                                            'label'       => __( 'Email', 'invoicing' ) . '<span class="required">*</span>',
64
                                            'label_type'  => 'vertical',
65
                                            'placeholder' => '[email protected]',
66
                                            'class'       => 'form-control-sm',
67
                                        )
68
                                    );
69
                                ?>
70
                            </div>
71
                        </div>
72
                        <div class="col-12 col-sm-6 form-group mt-sm-4">
73
                            <?php if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) : ?>
74
                                <a id="getpaid-invoice-fill-user-details" class="button button-small button-secondary" href="javascript:void(0)">
75
                                    <i aria-hidden="true" class="fa fa-refresh"></i>
76
                                    <?php _e( 'Fill User Details', 'invoicing' );?>
77
                                </a>
78
                                <a id="getpaid-invoice-create-new-user-button" class="button button-small button-secondary" href="javascript:void(0)">
79
                                    <i aria-hidden="true" class="fa fa-plus"></i>
80
                                    <?php _e( 'Add New User', 'invoicing' );?>
81
                                </a>
82
                                <a id="getpaid-invoice-cancel-create-new-user" class="button button-small button-secondary d-none" href="javascript:void(0)">
83
                                    <i aria-hidden="true" class="fa fa-close"></i>
84
                                    <?php _e( 'Cancel', 'invoicing' );?>
85
                                </a>
86
                            <?php endif; ?>
87
                        </div>
88
                    </div>
89
                    <div class="row">
90
                        <div class="col-12 col-sm-6">
91
                            <?php
92
                                echo aui()->input(
93
                                    array(
94
                                        'type'        => 'text',
95
                                        'id'          => 'wpinv_first_name',
96
                                        'name'        => 'wpinv_first_name',
97
                                        'label'       => __( 'First Name', 'invoicing' ),
98
                                        'label_type'  => 'vertical',
99
                                        'placeholder' => '',
100
                                        'class'       => 'form-control-sm',
101
                                        'value'       => $invoice->get_first_name( 'edit' ),
102
                                    )
103
                                );
104
                            ?>
105
                        </div>
106
                        <div class="col-12 col-sm-6">
107
                            <?php
108
                                echo aui()->input(
109
                                    array(
110
                                        'type'        => 'text',
111
                                        'id'          => 'wpinv_last_name',
112
                                        'name'        => 'wpinv_last_name',
113
                                        'label'       => __( 'Last Name', 'invoicing' ),
114
                                        'label_type'  => 'vertical',
115
                                        'placeholder' => '',
116
                                        'class'       => 'form-control-sm',
117
                                        'value'       => $invoice->get_last_name( 'edit' ),
118
                                    )
119
                                );
120
                            ?>
121
                        </div>
122
                    </div>
123
124
                    <div class="row">
125
                        <div class="col-12 col-sm-6">
126
                            <?php
127
                                echo aui()->input(
128
                                    array(
129
                                        'type'        => 'text',
130
                                        'id'          => 'wpinv_company',
131
                                        'name'        => 'wpinv_company',
132
                                        'label'       => __( 'Company', 'invoicing' ),
133
                                        'label_type'  => 'vertical',
134
                                        'placeholder' => '',
135
                                        'class'       => 'form-control-sm',
136
                                        'value'       => $invoice->get_company( 'edit' ),
137
                                    )
138
                                );
139
                            ?>
140
                        </div>
141
                        <div class="col-12 col-sm-6">
142
                            <?php
143
                                echo aui()->input(
144
                                    array(
145
                                        'type'        => 'text',
146
                                        'id'          => 'wpinv_vat_number',
147
                                        'name'        => 'wpinv_vat_number',
148
                                        'label'       => __( 'Vat Number', 'invoicing' ),
149
                                        'label_type'  => 'vertical',
150
                                        'placeholder' => '',
151
                                        'class'       => 'form-control-sm getpaid-recalculate-prices-on-change',
152
                                        'value'       => $invoice->get_vat_number( 'edit' ),
153
                                    )
154
                                );
155
                            ?>
156
                        </div>
157
                    </div>
158
159
                    <div class="row">
160
                        <div class="col-12 col-sm-6">
161
                            <?php
162
                                echo aui()->input(
163
                                    array(
164
                                        'type'        => 'text',
165
                                        'id'          => 'wpinv_address',
166
                                        'name'        => 'wpinv_address',
167
                                        'label'       => __( 'Address', 'invoicing' ),
168
                                        'label_type'  => 'vertical',
169
                                        'placeholder' => '',
170
                                        'class'       => 'form-control-sm',
171
                                        'value'       => $invoice->get_address( 'edit' ),
172
                                    )
173
                                );
174
                            ?>
175
                        </div>
176
                        <div class="col-12 col-sm-6">
177
                            <?php
178
                                echo aui()->input(
179
                                    array(
180
                                        'type'        => 'text',
181
                                        'id'          => 'wpinv_city',
182
                                        'name'        => 'wpinv_city',
183
                                        'label'       => __( 'City', 'invoicing' ),
184
                                        'label_type'  => 'vertical',
185
                                        'placeholder' => '',
186
                                        'class'       => 'form-control-sm',
187
                                        'value'       => $invoice->get_city( 'edit' ),
188
                                    )
189
                                );
190
                            ?>
191
                        </div>
192
                    </div>
193
194
                    <div class="row">
195
                        <div class="col-12 col-sm-6">
196
                            <?php
197
                                echo aui()->select(
198
                                    array(
199
                                        'id'          => 'wpinv_country',
200
                                        'name'        => 'wpinv_country',
201
                                        'label'       => __( 'Country', 'invoicing' ),
202
                                        'label_type'  => 'vertical',
203
                                        'placeholder' => __( 'Choose a country', 'invoicing' ),
204
                                        'class'       => 'form-control-sm getpaid-recalculate-prices-on-change',
205
                                        'value'       => $invoice->get_country( 'edit' ),
206
                                        'options'     => wpinv_get_country_list(),
207
                                        'data-allow-clear' => 'false',
208
                                        'select2'          => true,
209
                                    )
210
                                );
211
                            ?>
212
                        </div>
213
                        <div class="col-12 col-sm-6">
214
                            <?php
215
216
                                $states = wpinv_get_country_states( $invoice->get_country( 'edit' ) );
217
218
                                if ( empty( $states ) ) {
219
220
                                    echo aui()->input(
221
                                        array(
222
                                            'type'        => 'text',
223
                                            'id'          => 'wpinv_state',
224
                                            'name'        => 'wpinv_state',
225
                                            'label'       => __( 'State', 'invoicing' ),
226
                                            'label_type'  => 'vertical',
227
                                            'placeholder' => '',
228
                                            'class'       => 'form-control-sm',
229
                                            'value'       => $invoice->get_state( 'edit' ),
230
                                        )
231
                                    );
232
233
                                } else {
234
235
                                    echo aui()->select(
236
                                        array(
237
                                            'id'          => 'wpinv_state',
238
                                            'name'        => 'wpinv_state',
239
                                            'label'       => __( 'State', 'invoicing' ),
240
                                            'label_type'  => 'vertical',
241
                                            'placeholder' => __( 'Select a state', 'invoicing' ),
242
                                            'class'       => 'form-control-sm',
243
                                            'value'       => $invoice->get_state( 'edit' ),
244
                                            'options'     => $states,
245
                                            'data-allow-clear' => 'false',
246
                                            'select2'          => true,
247
                                        )
248
                                    );
249
250
                                }
251
                                
252
                            ?>
253
                        </div>
254
                    </div>
255
256
                    <div class="row">
257
                        <div class="col-12 col-sm-6">
258
                            <?php
259
                                echo aui()->input(
260
                                    array(
261
                                        'type'        => 'text',
262
                                        'id'          => 'wpinv_zip',
263
                                        'name'        => 'wpinv_zip',
264
                                        'label'       => __( 'Zip / Postal Code', 'invoicing' ),
265
                                        'label_type'  => 'vertical',
266
                                        'placeholder' => '',
267
                                        'class'       => 'form-control-sm',
268
                                        'value'       => $invoice->get_zip( 'edit' ),
269
                                    )
270
                                );
271
                            ?>
272
                        </div>
273
                        <div class="col-12 col-sm-6">
274
                            <?php
275
                                echo aui()->input(
276
                                    array(
277
                                        'type'        => 'text',
278
                                        'id'          => 'wpinv_phone',
279
                                        'name'        => 'wpinv_phone',
280
                                        'label'       => __( 'Phone', 'invoicing' ),
281
                                        'label_type'  => 'vertical',
282
                                        'placeholder' => '',
283
                                        'class'       => 'form-control-sm',
284
                                        'value'       => $invoice->get_phone( 'edit' ),
285
                                    )
286
                                );
287
                            ?>
288
                        </div>
289
                    </div>
290
291
                    <?php if ( ! apply_filters( 'getpaid_use_new_invoice_items_metabox', false ) ) : ?>
292
                        <?php do_action( 'wpinv_meta_box_before_invoice_template_row', $invoice->get_id() ); ?>
293
294
                        <div class="row">
295
                            <div class="col-12 col-sm-6">
296
                                <?php
297
                                    echo aui()->select(
298
                                        array(
299
                                            'id'          => 'wpinv_template',
300
                                            'name'        => 'wpinv_template',
301
                                            'label'       => __( 'Template', 'invoicing' ),
302
                                            'label_type'  => 'vertical',
303
                                            'placeholder' => __( 'Choose a template', 'invoicing' ),
304
                                            'class'       => 'form-control-sm',
305
                                            'value'       => $invoice->get_template( 'edit' ),
306
                                            'options'     => array(
307
                                                'quantity' => __( 'Quantity', 'invoicing' ),
308
                                                'hours'    => __( 'Hours', 'invoicing' ),
309
                                                //'amount'   => __( 'Amount Only', 'invoicing' ),
310
                                            ),
311
                                            'data-allow-clear' => 'false',
312
                                            'select2'          => true,
313
                                        )
314
                                    );
315
                                ?>
316
                            </div>
317
                            <div class="col-12 col-sm-6">
318
                                <?php
319
320
                                    // Set currency.
321
                                    echo aui()->select(
322
                                        array(
323
                                            'id'          => 'wpinv_currency',
324
                                            'name'        => 'wpinv_currency',
325
                                            'label'       => __( 'Currency', 'invoicing' ),
326
                                            'label_type'  => 'vertical',
327
                                            'placeholder' => __( 'Select Invoice Currency', 'invoicing' ),
328
                                            'class'       => 'form-control-sm getpaid-recalculate-prices-on-change',
329
                                            'value'       => $invoice->get_currency( 'edit' ),
330
                                            'required'    => false,
331
                                            'data-allow-clear' => 'false',
332
                                            'select2'          => true,
333
                                            'options'     => wpinv_get_currencies(),
334
                                        )
335
                                    );
336
337
                                ?>
338
                            </div>
339
                        </div>
340
341
                        <?php do_action( 'wpinv_meta_box_invoice_template_row', $invoice->get_id() ); ?>
342
                    <?php endif; ?>
343
344
                    <div class="row">
345
                        <div class="col-12 col-sm-6">
346
                            <?php
347
                                echo aui()->input(
348
                                    array(
349
                                        'type'        => 'text',
350
                                        'id'          => 'wpinv_company_id',
351
                                        'name'        => 'wpinv_company_id',
352
                                        'label'       => __( 'Company ID', 'invoicing' ),
353
                                        'label_type'  => 'vertical',
354
                                        'placeholder' => '',
355
                                        'class'       => 'form-control-sm',
356
                                        'value'       => $invoice->get_company_id( 'edit' ),
357
                                    )
358
                                );
359
                            ?>
360
                        </div>
361
                    </div>
362
363
                    <?php do_action( 'getpaid_after_metabox_invoice_address', $invoice ); ?>
364
            </div>
365
        <?php
366
    }
367
368
    /**
369
	 * Save meta box data.
370
	 *
371
	 * @param int $post_id
372
	 */
373
	public static function save( $post_id ) {
374
375
        // Prepare the invoice.
376
        $invoice = new WPInv_Invoice( $post_id );
377
378
        // Load new data.
379
        $invoice->set_props(
380
			array(
381
                'template'             => isset( $_POST['wpinv_template'] ) ? wpinv_clean( $_POST['wpinv_template'] ) : null,
382
                'email_cc'             => isset( $_POST['wpinv_cc'] ) ? wpinv_clean( $_POST['wpinv_cc'] ) : null,
383
                'disable_taxes'        => isset( $_POST['disable_taxes'] ),
384
                'currency'             => isset( $_POST['wpinv_currency'] ) ? wpinv_clean( $_POST['wpinv_currency'] ) : null,
385
                'gateway'              => ( $invoice->needs_payment() && isset( $_POST['wpinv_gateway'] ) ) ? wpinv_clean( $_POST['wpinv_gateway'] ) : null,
386
                'address'              => isset( $_POST['wpinv_address'] ) ? wpinv_clean( $_POST['wpinv_address'] ) : null,
387
                'vat_number'           => isset( $_POST['wpinv_vat_number'] ) ? wpinv_clean( $_POST['wpinv_vat_number'] ) : null,
388
                'company'              => isset( $_POST['wpinv_company'] ) ? wpinv_clean( $_POST['wpinv_company'] ) : null,
389
                'company_id'           => isset( $_POST['wpinv_company_id'] ) ? wpinv_clean( $_POST['wpinv_company_id'] ) : null,
390
                'zip'                  => isset( $_POST['wpinv_zip'] ) ? wpinv_clean( $_POST['wpinv_zip'] ) : null,
391
                'state'                => isset( $_POST['wpinv_state'] ) ? wpinv_clean( $_POST['wpinv_state'] ) : null,
392
                'city'                 => isset( $_POST['wpinv_city'] ) ? wpinv_clean( $_POST['wpinv_city'] ) : null,
393
                'country'              => isset( $_POST['wpinv_country'] ) ? wpinv_clean( $_POST['wpinv_country'] ) : null,
394
                'phone'                => isset( $_POST['wpinv_phone'] ) ? wpinv_clean( $_POST['wpinv_phone'] ) : null,
395
                'first_name'           => isset( $_POST['wpinv_first_name'] ) ? wpinv_clean( $_POST['wpinv_first_name'] ) : null,
396
                'last_name'            => isset( $_POST['wpinv_last_name'] ) ? wpinv_clean( $_POST['wpinv_last_name'] ) : null,
397
                'author'               => isset( $_POST['post_author_override'] ) ? wpinv_clean( $_POST['post_author_override'] ) : null,
398
                'date_created'         => isset( $_POST['date_created'] ) ? wpinv_clean( $_POST['date_created'] ) : null,
399
                'date_completed'       => isset( $_POST['wpinv_date_completed'] ) ? wpinv_clean( $_POST['wpinv_date_completed'] ) : null,
400
                'due_date'             => isset( $_POST['wpinv_due_date'] ) ? wpinv_clean( $_POST['wpinv_due_date'] ) : null,
401
                'number'               => isset( $_POST['wpinv_number'] ) ? wpinv_clean( $_POST['wpinv_number'] ) : null,
402
                'status'               => isset( $_POST['wpinv_status'] ) ? wpinv_clean( $_POST['wpinv_status'] ) : null,
403
			)
404
        );
405
406
        // Discount code.
407
        if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) {
408
409
            if ( isset( $_POST['wpinv_discount_code'] ) ) {
410
                $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

410
                $invoice->set_discount_code( /** @scrutinizer ignore-type */ wpinv_clean( $_POST['wpinv_discount_code'] ) );
Loading history...
411
            }
412
413
            $discount = new WPInv_Discount( $invoice->get_discount_code() );
414
            if ( $discount->exists() ) {
415
                $invoice->add_discount( getpaid_calculate_invoice_discount( $invoice, $discount ) );
416
            } else {
417
                $invoice->remove_discount( 'discount_code' );
418
            }
419
420
            // Recalculate totals.
421
            $invoice->recalculate_total();
422
423
        }
424
425
        // If we're creating a new user...
426
        if ( ! empty( $_POST['wpinv_new_user'] ) && is_email( stripslashes( $_POST['wpinv_email'] ) ) ) {
427
428
            // Attempt to create the user.
429
            $user = wpinv_create_user( sanitize_email( stripslashes( $_POST['wpinv_email'] ) ) );
430
431
432
            // If successful, update the invoice author.
433
            if ( is_numeric( $user ) ) {
434
                $invoice->set_author( $user );
435
            } else {
436
                wpinv_error_log( $user->get_error_message(), __( 'Invoice add new user', 'invoicing' ), __FILE__, __LINE__ );
437
            }
438
        }
439
440
        // Do not send new invoice notifications.
441
        $GLOBALS['wpinv_skip_invoice_notification'] = true;
442
443
        // Save the invoice.
444
        $invoice->save();
445
446
        // Undo do not send new invoice notifications.
447
        $GLOBALS['wpinv_skip_invoice_notification'] = false;
448
449
        // (Maybe) send new user notification.
450
        $should_send_notification = wpinv_get_option( 'disable_new_user_emails' );
451
        if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ) ) ) {
452
            wp_send_new_user_notifications( $user, 'user' );
453
        }
454
455
        if ( ! empty( $_POST['send_to_customer'] ) && ! $invoice->is_draft() ) {
456
457
            $sent = getpaid()->get( 'invoice_emails' )->user_invoice( $invoice, true );
458
459
            if ( $sent ) {
460
                getpaid_admin()->show_success( __( 'Invoice was successfully sent to the customer', 'invoicing' ) );
461
            } else {
462
                getpaid_admin()->show_error( __( 'Could not send the invoice to the customer', 'invoicing' ) );
463
            }
464
465
        }
466
467
        // Fires after an invoice is saved.
468
		do_action( 'wpinv_invoice_metabox_saved', $invoice );
469
	}
470
}
471