Passed
Push — master ( eba4be...60f62d )
by Brian
09:36 queued 04:47
created

GetPaid_Meta_Box_Invoice_Address   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 349
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 31
eloc 271
c 1
b 0
f 0
dl 0
loc 349
rs 9.92

2 Methods

Rating   Name   Duplication   Size   Complexity  
D save() 0 59 26
B output() 0 271 5
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
29
        wp_nonce_field( 'getpaid_meta_nonce', 'getpaid_meta_nonce' );
30
31
        ?>
32
33
        <style>
34
            #gdmbx2-metabox-wpinv_address label {
35
                margin-bottom: 3px;
36
                font-weight: 600;
37
            }
38
        </style>
39
            <div class="bsui" style="margin-top: 1.5rem; max-width: 820px;">
40
                <div id="gdmbx2-metabox-wpinv_address">
41
                    <div class="row">
42
                        <div class="col-12 col-sm-6">
43
                            <div id="getpaid-invoice-user-id-wrapper" class="form-group">
44
                                <div>
45
                                    <label for="post_author_override"><?php _e( 'Customer', 'invoicing' );?></label>
46
                                </div>
47
                                <?php 
48
                                    wpinv_dropdown_users(
49
                                        array(
50
                                            'name'             => 'post_author_override',
51
                                            'selected'         => $invoice->get_id() ? $invoice->get_user_id( 'edit' ) : get_current_user_id(),
52
                                            'include_selected' => true,
53
                                            'show'             => 'display_name_with_email',
54
                                            'orderby'          => 'user_email',
55
                                            'class'            => 'wpi_select2 form-control'
56
                                        )
57
                                    );
58
                                ?>
59
                            </div>
60
61
                            <div id="getpaid-invoice-email-wrapper" class="d-none">
62
                                <input type="hidden" id="getpaid-invoice-create-new-user" name="wpinv_new_user" value="" />
63
                                <?php
64
                                    echo aui()->input(
65
                                        array(
66
                                            'type'        => 'text',
67
                                            'id'          => 'getpaid-invoice-new-user-email',
68
                                            'name'        => 'wpinv_email',
69
                                            'label'       => __( 'Email', 'invoicing' ) . '<span class="required">*</span>',
70
                                            'label_type'  => 'vertical',
71
                                            'placeholder' => '[email protected]',
72
                                            'class'       => 'form-control-sm',
73
                                        )
74
                                    );
75
                                ?>
76
                            </div>
77
                        </div>
78
                        <div class="col-12 col-sm-6 form-group mt-sm-4">
79
                            <?php if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) : ?>
80
                                <a id="getpaid-invoice-fill-user-details" class="button button-small button-secondary" href="javascript:void(0)">
81
                                    <i aria-hidden="true" class="fa fa-refresh"></i>
82
                                    <?php _e( 'Fill User Details', 'invoicing' );?>
83
                                </a>
84
                                <a id="getpaid-invoice-create-new-user-button" class="button button-small button-secondary" href="javascript:void(0)">
85
                                    <i aria-hidden="true" class="fa fa-plus"></i>
86
                                    <?php _e( 'Add New User', 'invoicing' );?>
87
                                </a>
88
                                <a id="getpaid-invoice-cancel-create-new-user" class="button button-small button-secondary d-none" href="javascript:void(0)">
89
                                    <i aria-hidden="true" class="fa fa-close"></i>
90
                                    <?php _e( 'Cancel', 'invoicing' );?>
91
                                </a>
92
                            <?php endif; ?>
93
                        </div>
94
                    </div>
95
                    <div class="row">
96
                        <div class="col-12 col-sm-6">
97
                            <?php
98
                                echo aui()->input(
99
                                    array(
100
                                        'type'        => 'text',
101
                                        'id'          => 'wpinv_first_name',
102
                                        'name'        => 'wpinv_first_name',
103
                                        'label'       => __( 'First Name', 'invoicing' ),
104
                                        'label_type'  => 'vertical',
105
                                        'placeholder' => 'Jane',
106
                                        'class'       => 'form-control-sm',
107
                                        'value'       => $invoice->get_first_name( 'edit' ),
108
                                    )
109
                                );
110
                            ?>
111
                        </div>
112
                        <div class="col-12 col-sm-6">
113
                            <?php
114
                                echo aui()->input(
115
                                    array(
116
                                        'type'        => 'text',
117
                                        'id'          => 'wpinv_last_name',
118
                                        'name'        => 'wpinv_last_name',
119
                                        'label'       => __( 'Last Name', 'invoicing' ),
120
                                        'label_type'  => 'vertical',
121
                                        'placeholder' => 'Doe',
122
                                        'class'       => 'form-control-sm',
123
                                        'value'       => $invoice->get_last_name( 'edit' ),
124
                                    )
125
                                );
126
                            ?>
127
                        </div>
128
                    </div>
129
130
                    <div class="row">
131
                        <div class="col-12 col-sm-6">
132
                            <?php
133
                                echo aui()->input(
134
                                    array(
135
                                        'type'        => 'text',
136
                                        'id'          => 'wpinv_company',
137
                                        'name'        => 'wpinv_company',
138
                                        'label'       => __( 'Company', 'invoicing' ),
139
                                        'label_type'  => 'vertical',
140
                                        'placeholder' => 'Acme Corporation',
141
                                        'class'       => 'form-control-sm',
142
                                        'value'       => $invoice->get_company( 'edit' ),
143
                                    )
144
                                );
145
                            ?>
146
                        </div>
147
                        <div class="col-12 col-sm-6">
148
                            <?php
149
                                echo aui()->input(
150
                                    array(
151
                                        'type'        => 'text',
152
                                        'id'          => 'wpinv_vat_number',
153
                                        'name'        => 'wpinv_vat_number',
154
                                        'label'       => __( 'Vat Number', 'invoicing' ),
155
                                        'label_type'  => 'vertical',
156
                                        'placeholder' => '1234567890',
157
                                        'class'       => 'form-control-sm',
158
                                        'value'       => $invoice->get_vat_number( 'edit' ),
159
                                    )
160
                                );
161
                            ?>
162
                        </div>
163
                    </div>
164
165
                    <div class="row">
166
                        <div class="col-12 col-sm-6">
167
                            <?php
168
                                echo aui()->input(
169
                                    array(
170
                                        'type'        => 'text',
171
                                        'id'          => 'wpinv_address',
172
                                        'name'        => 'wpinv_address',
173
                                        'label'       => __( 'Address', 'invoicing' ),
174
                                        'label_type'  => 'vertical',
175
                                        'placeholder' => 'Blekersdijk 295',
176
                                        'class'       => 'form-control-sm',
177
                                        'value'       => $invoice->get_address( 'edit' ),
178
                                    )
179
                                );
180
                            ?>
181
                        </div>
182
                        <div class="col-12 col-sm-6">
183
                            <?php
184
                                echo aui()->input(
185
                                    array(
186
                                        'type'        => 'text',
187
                                        'id'          => 'wpinv_city',
188
                                        'name'        => 'wpinv_city',
189
                                        'label'       => __( 'City', 'invoicing' ),
190
                                        'label_type'  => 'vertical',
191
                                        'placeholder' => 'Dolembreux',
192
                                        'class'       => 'form-control-sm',
193
                                        'value'       => $invoice->get_vat_number( 'edit' ),
194
                                    )
195
                                );
196
                            ?>
197
                        </div>
198
                    </div>
199
200
                    <div class="row">
201
                        <div class="col-12 col-sm-6">
202
                            <?php
203
                                echo aui()->select(
204
                                    array(
205
                                        'id'          => 'wpinv_country',
206
                                        'name'        => 'wpinv_country',
207
                                        'label'       => __( 'Country', 'invoicing' ),
208
                                        'label_type'  => 'vertical',
209
                                        'placeholder' => __( 'Choose a country', 'invoicing' ),
210
                                        'class'       => 'form-control-sm',
211
                                        'value'       => $invoice->get_country( 'edit' ),
212
                                        'options'     => wpinv_get_country_list(),
213
                                        'data-allow-clear' => 'false',
214
                                        'select2'          => true,
215
                                    )
216
                                );
217
                            ?>
218
                        </div>
219
                        <div class="col-12 col-sm-6">
220
                            <?php
221
222
                                $states = wpinv_get_country_states( $invoice->get_country( 'edit' ) );
223
224
                                if ( empty( $states ) ) {
225
226
                                    echo aui()->input(
227
                                        array(
228
                                            'type'        => 'text',
229
                                            'id'          => 'wpinv_state',
230
                                            'name'        => 'wpinv_state',
231
                                            'label'       => __( 'State', 'invoicing' ),
232
                                            'label_type'  => 'vertical',
233
                                            'placeholder' => 'Liège',
234
                                            'class'       => 'form-control-sm',
235
                                            'value'       => $invoice->get_state( 'edit' ),
236
                                        )
237
                                    );
238
239
                                } else {
240
241
                                    echo aui()->select(
242
                                        array(
243
                                            'id'          => 'wpinv_state',
244
                                            'name'        => 'wpinv_state',
245
                                            'label'       => __( 'State', 'invoicing' ),
246
                                            'label_type'  => 'vertical',
247
                                            'placeholder' => __( 'Select a state', 'invoicing' ),
248
                                            'class'       => 'form-control-sm',
249
                                            'value'       => $invoice->get_state( 'edit' ),
250
                                            'options'     => $states,
251
                                            'data-allow-clear' => 'false',
252
                                            'select2'          => true,
253
                                        )
254
                                    );
255
256
                                }
257
                                
258
                            ?>
259
                        </div>
260
                    </div>
261
262
                    <div class="row">
263
                        <div class="col-12 col-sm-6">
264
                            <?php
265
                                echo aui()->input(
266
                                    array(
267
                                        'type'        => 'text',
268
                                        'id'          => 'wpinv_zip',
269
                                        'name'        => 'wpinv_zip',
270
                                        'label'       => __( 'Zip / Postal Code', 'invoicing' ),
271
                                        'label_type'  => 'vertical',
272
                                        'placeholder' => '4140',
273
                                        'class'       => 'form-control-sm',
274
                                        'value'       => $invoice->get_zip( 'edit' ),
275
                                    )
276
                                );
277
                            ?>
278
                        </div>
279
                        <div class="col-12 col-sm-6">
280
                            <?php
281
                                echo aui()->input(
282
                                    array(
283
                                        'type'        => 'text',
284
                                        'id'          => 'wpinv_phone',
285
                                        'name'        => 'wpinv_phone',
286
                                        'label'       => __( 'Phone', 'invoicing' ),
287
                                        'label_type'  => 'vertical',
288
                                        'placeholder' => '0493 18 45822',
289
                                        'class'       => 'form-control-sm',
290
                                        'value'       => $invoice->get_phone( 'edit' ),
291
                                    )
292
                                );
293
                            ?>
294
                        </div>
295
                    </div>
296
297
                </div>
298
            </div>
299
        <?php
300
    }
301
302
    /**
303
	 * Save meta box data.
304
	 *
305
	 * @param int $post_id
306
	 */
307
	public static function save( $post_id ) {
308
309
        // Prepare the invoice.
310
        $invoice = new WPInv_Invoice( $post_id );
311
312
        // Load new data.
313
        $invoice->set_props(
314
			array(
315
                'template'             => isset( $_POST['wpinv_template'] ) ? wpinv_clean( $_POST['wpinv_template'] ) : null,
316
                'email_cc'             => isset( $_POST['wpinv_cc'] ) ? wpinv_clean( $_POST['wpinv_cc'] ) : null,
317
                'disable_taxes'        => isset( $_POST['disable_taxes'] ),
318
                'currency'             => isset( $_POST['wpinv_currency'] ) ? wpinv_clean( $_POST['wpinv_currency'] ) : null,
319
                'gateway'              => isset( $_POST['wpinv_gateway'] ) ? wpinv_clean( $_POST['wpinv_gateway'] ) : null,
320
                'address'              => isset( $_POST['wpinv_address'] ) ? wpinv_clean( $_POST['wpinv_address'] ) : null,
321
                'vat_number'           => isset( $_POST['wpinv_vat_number'] ) ? wpinv_clean( $_POST['wpinv_vat_number'] ) : null,
322
                'company'              => isset( $_POST['wpinv_company'] ) ? wpinv_clean( $_POST['wpinv_company'] ) : null,
323
                'zip'                  => isset( $_POST['wpinv_zip'] ) ? wpinv_clean( $_POST['wpinv_zip'] ) : null,
324
                'state'                => isset( $_POST['wpinv_state'] ) ? wpinv_clean( $_POST['wpinv_state'] ) : null,
325
                'city'                 => isset( $_POST['wpinv_city'] ) ? wpinv_clean( $_POST['wpinv_city'] ) : null,
326
                'country'              => isset( $_POST['wpinv_country'] ) ? wpinv_clean( $_POST['wpinv_country'] ) : null,
327
                'phone'                => isset( $_POST['wpinv_phone'] ) ? wpinv_clean( $_POST['wpinv_phone'] ) : null,
328
                'first_name'           => isset( $_POST['wpinv_first_name'] ) ? wpinv_clean( $_POST['wpinv_first_name'] ) : null,
329
                'last_name'            => isset( $_POST['wpinv_last_name'] ) ? wpinv_clean( $_POST['wpinv_last_name'] ) : null,
330
                'author'               => isset( $_POST['post_author_override'] ) ? wpinv_clean( $_POST['post_author_override'] ) : null,
331
                'date_created'         => isset( $_POST['date_created'] ) ? wpinv_clean( $_POST['date_created'] ) : null,
332
                'due_date'             => isset( $_POST['wpinv_due_date'] ) ? wpinv_clean( $_POST['wpinv_due_date'] ) : null,
333
                'number'               => isset( $_POST['wpinv_number'] ) ? wpinv_clean( $_POST['wpinv_number'] ) : null,
334
                'status'               => isset( $_POST['wpinv_status'] ) ? wpinv_clean( $_POST['wpinv_status'] ) : null,
335
			)
336
        );
337
338
        // Recalculate totals.
339
        $invoice->recalculate_total();
340
341
        // If we're creating a new user...
342
        if ( ! empty( $_POST['wpinv_new_user'] ) && is_email( $_POST['wpinv_email'] ) ) {
343
344
            // Attempt to create the user.
345
            $user = wpinv_create_user( sanitize_email( $_POST['wpinv_email'] ) );
346
347
348
            // If successful, update the invoice author.
349
            if ( is_numeric( $user ) ) {
350
                $invoice->set_author( $user );
351
            } else {
352
                wpinv_error_log( $user->get_error_message(), __( 'Invoice add new user', 'invoicing' ), __FILE__, __LINE__ );
353
            }
354
        }
355
356
        // Save the invoice.
357
        $invoice->save();
358
359
        // (Maybe) send new user notification.
360
        if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', true ) ) {
361
            wp_send_new_user_notifications( $user, 'user' );
362
        }
363
364
        // Fires after an invoice is saved.
365
		do_action( 'wpinv_invoice_metabox_saved', $invoice );
366
	}
367
}
368