Passed
Push — master ( a2648f...42db48 )
by Brian
195:36 queued 107:03
created

WPInv_Meta_Box_Items::save()   F

Complexity

Conditions 25
Paths > 20000

Size

Total Lines 129
Code Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 25
eloc 83
nc 49152
nop 3
dl 0
loc 129
rs 0
c 0
b 0
f 0

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
 * Item Data
5
 *
6
 * Display the item data meta box.
7
 *
8
 */
9
10
if ( ! defined( 'ABSPATH' ) ) {
11
	exit; // Exit if accessed directly
12
}
13
14
/**
15
 * WPInv_Meta_Box_Items Class.
16
 */
17
class WPInv_Meta_Box_Items {
18
19
    /**
20
	 * Output the metabox.
21
	 *
22
	 * @param WP_Post $post
23
	 */
24
    public static function output( $post ) {
25
        global $wpinv_euvat, $ajax_cart_details;
26
27
        $post_id            = !empty( $post->ID ) ? $post->ID : 0;
28
        $invoice            = new WPInv_Invoice( $post_id );
29
        $ajax_cart_details  = $invoice->get_cart_details();
30
        $subtotal           = $invoice->get_subtotal( true );
31
        $discount_raw       = $invoice->get_discount();
32
        $discount           = wpinv_price( $discount_raw, $invoice->get_currency() );
0 ignored issues
show
Unused Code introduced by
The assignment to $discount is dead and can be removed.
Loading history...
33
        $discounts          = $discount_raw > 0 ? $invoice->get_discounts() : '';
0 ignored issues
show
Unused Code introduced by
The assignment to $discounts is dead and can be removed.
Loading history...
34
        $tax                = $invoice->get_tax( true );
35
        $total              = $invoice->get_total( true );
36
        $item_quantities    = wpinv_item_quantities_enabled();
37
        $use_taxes          = wpinv_use_taxes();
38
        if ( !$use_taxes && (float)$invoice->get_tax() > 0 ) {
39
            $use_taxes = true;
40
        }
41
        $item_types         = apply_filters( 'wpinv_item_types_for_quick_add_item', wpinv_get_item_types(), $post );
42
        $is_recurring       = $invoice->is_recurring();
43
        $post_type_object   = get_post_type_object($invoice->post_type);
44
        $type_title         = $post_type_object->labels->singular_name;
45
46
        $cols = 5;
47
        if ( $item_quantities ) {
48
            $cols++;
49
        }
50
        if ( $use_taxes ) {
51
            $cols++;
52
        }
53
        $class = '';
54
        if ( $invoice->is_paid() ) {
55
            $class .= ' wpinv-paid';
56
        }
57
        if ( $invoice->is_refunded() ) {
58
            $class .= ' wpinv-refunded';
59
        }
60
        if ( $is_recurring ) {
61
            $class .= ' wpi-recurring';
62
        }
63
        ?>
64
        <div class="wpinv-items-wrap<?php echo $class; ?>" id="wpinv_items_wrap" data-status="<?php echo $invoice->status; ?>">
65
            <table id="wpinv_items" class="wpinv-items" cellspacing="0" cellpadding="0">
66
                <thead>
67
                    <tr>
68
                        <th class="id"><?php _e( 'ID', 'invoicing' );?></th>
69
                        <th class="title"><?php _e( 'Item', 'invoicing' );?></th>
70
                        <th class="price"><?php _e( 'Price', 'invoicing' );?></th>
71
                        <?php if ( $item_quantities ) { ?>
72
                        <th class="qty"><?php _e( 'Qty', 'invoicing' );?></th>
73
                        <?php } ?>
74
                        <th class="total"><?php _e( 'Total', 'invoicing' );?></th>
75
                        <?php if ( $use_taxes ) { ?>
76
                        <th class="tax"><?php _e( 'Tax (%)', 'invoicing' );?></th>
77
                        <?php } ?>
78
                        <th class="action"></th>
79
                    </tr>
80
                </thead>
81
                <tbody class="wpinv-line-items">
82
                    <?php echo wpinv_admin_get_line_items( $invoice ); ?>
83
                </tbody>
84
                <tfoot class="wpinv-totals">
85
                    <tr>
86
                        <td colspan="<?php echo $cols; ?>" style="padding:0;border:0">
87
                            <div id="wpinv-quick-add">
88
                                <table cellspacing="0" cellpadding="0">
89
                                    <tr>
90
                                        <td class="id">
91
                                        </td>
92
                                        <td class="title">
93
                                            <input type="text" class="regular-text" placeholder="<?php _e( 'Item Name', 'invoicing' ); ?>" value="" name="_wpinv_quick[name]">
94
                                            <?php if ( $wpinv_euvat->allow_vat_rules() ) { ?>
95
                                            <div class="wp-clearfix">
96
                                                <label class="wpi-vat-rule">
97
                                                    <span class="title"><?php _e( 'VAT rule type', 'invoicing' );?></span>
98
                                                    <span class="input-text-wrap">
99
                                                        <?php echo wpinv_html_select( array(
100
                                                            'options'          => $wpinv_euvat->get_rules(),
101
                                                            'name'             => '_wpinv_quick[vat_rule]',
102
                                                            'id'               => '_wpinv_quick_vat_rule',
103
                                                            'show_option_all'  => false,
104
                                                            'show_option_none' => false,
105
                                                            'class'            => 'gdmbx2-text-medium wpinv-quick-vat-rule wpi_select2',
106
                                                        ) ); ?>
107
                                                    </span>
108
                                                </label>
109
                                            </div>
110
                                            <?php } if ( $wpinv_euvat->allow_vat_classes() ) { ?>
111
                                            <div class="wp-clearfix">
112
                                                <label class="wpi-vat-class">
113
                                                    <span class="title"><?php _e( 'VAT class', 'invoicing' );?></span>
114
                                                    <span class="input-text-wrap">
115
                                                        <?php echo wpinv_html_select( array(
116
                                                            'options'          => $wpinv_euvat->get_all_classes(),
117
                                                            'name'             => '_wpinv_quick[vat_class]',
118
                                                            'id'               => '_wpinv_quick_vat_class',
119
                                                            'show_option_all'  => false,
120
                                                            'show_option_none' => false,
121
                                                            'class'            => 'gdmbx2-text-medium wpinv-quick-vat-class wpi_select2',
122
                                                        ) ); ?>
123
                                                    </span>
124
                                                </label>
125
                                            </div>
126
                                            <?php } ?>
127
                                            <div class="wp-clearfix">
128
                                                <label class="wpi-item-type">
129
                                                    <span class="title"><?php _e( 'Item type', 'invoicing' );?></span>
130
                                                    <span class="input-text-wrap">
131
                                                        <?php echo wpinv_html_select( array(
132
                                                            'options'          => $item_types,
133
                                                            'name'             => '_wpinv_quick[type]',
134
                                                            'id'               => '_wpinv_quick_type',
135
                                                            'selected'         => 'custom',
136
                                                            'show_option_all'  => false,
137
                                                            'show_option_none' => false,
138
                                                            'class'            => 'gdmbx2-text-medium wpinv-quick-type wpi_select2',
139
                                                        ) ); ?>
140
                                                    </span>
141
                                                </label>
142
                                            </div>
143
144
                                            <div class="wp-clearfix">
145
                                                <?php 
146
                                                    echo wpinv_html_textarea( array(
147
                                                        'name'  => '_wpinv_quick[excerpt]',
148
                                                        'id'    => '_wpinv_quick_excerpt',
149
                                                        'value' => '',
150
                                                        'class' => 'large-text',
151
                                                        'label' => __( 'Item description', 'invoicing' ),
152
                                                    ) ); 
153
                                                ?>
154
                                            </div>
155
156
                                            <div class="wp-clearfix">
157
                                                <label class="wpi-item-actions">
158
                                                    <span class="input-text-wrap">
159
                                                        <input type="button" value="<?php esc_attr_e( 'Add', 'invoicing' ); ?>" class="button button-primary" id="wpinv-save-item"><input type="button" value="Cancel" class="button button-secondary" id="wpinv-cancel-item">
160
                                                    </span>
161
                                                </label>
162
                                            </div>
163
                                        </td>
164
                                        <td class="price"><input type="text" placeholder="0.00" class="wpi-field-price wpi-price" name="_wpinv_quick[price]" /></td>
165
                                        <?php if ( $item_quantities ) { ?>
166
                                        <td class="qty"><input type="number" class="small-text" step="1" min="1" value="1" name="_wpinv_quick[qty]" /></td>
167
                                        <?php } ?>
168
                                        <td class="total"></td>
169
                                        <?php if ( $use_taxes ) { ?>
170
                                        <td class="tax"></td>
171
                                        <?php } ?>
172
                                        <td class="action"></td>
173
                                    </tr>
174
                                </table>
175
                            </div>
176
                        </td>
177
                    </tr>
178
                    <tr class="clear">
179
                        <td colspan="<?php echo $cols; ?>"></td>
180
                    </tr>
181
                    <tr class="totals">
182
                        <td colspan="<?php echo ( $cols - 4 ); ?>"></td>
183
                        <td colspan="4">
184
                            <table cellspacing="0" cellpadding="0">
185
                                <tr class="subtotal">
186
                                    <td class="name"><?php _e( 'Sub Total:', 'invoicing' );?></td>
187
                                    <td class="total"><?php echo $subtotal;?></td>
188
                                    <td class="action"></td>
189
                                </tr>
190
                                <tr class="discount">
191
                                    <td class="name"><?php wpinv_get_discount_label( wpinv_discount_code( $invoice->ID ) ); ?>:</td>
192
                                    <td class="total"><?php echo wpinv_discount( $invoice->ID, true, true ); ?></td>
193
                                    <td class="action"></td>
194
                                </tr>
195
                                <?php if ( $use_taxes ) { ?>
196
                                <tr class="tax">
197
                                    <td class="name"><?php _e( 'Tax:', 'invoicing' );?></td>
198
                                    <td class="total"><?php echo $tax;?></td>
199
                                    <td class="action"></td>
200
                                </tr>
201
                                <?php } ?>
202
                                <tr class="total">
203
                                    <td class="name"><?php echo apply_filters( 'wpinv_invoice_items_total_label', __( 'Invoice Total:', 'invoicing' ), $invoice );?></td>
204
                                    <td class="total"><?php echo $total;?></td>
205
                                    <td class="action"></td>
206
                                </tr>
207
                            </table>
208
                        </td>
209
                    </tr>
210
                </tfoot>
211
            </table>
212
            <div class="wpinv-actions">
213
                <?php ob_start(); ?>
214
                <?php
215
                    if ( !$invoice->is_paid() && !$invoice->is_refunded() ) {
216
                        if ( !$invoice->is_recurring() ) {
217
                            echo wpinv_item_dropdown( array(
218
                                'name'             => 'wpinv_invoice_item',
219
                                'id'               => 'wpinv_invoice_item',
220
                                'show_recurring'   => true,
221
                                'class'            => 'wpi_select2',
222
                            ) );
223
                    ?>
224
                <input type="button" value="<?php echo sprintf(esc_attr__( 'Add item to %s', 'invoicing'), $type_title); ?>" class="button button-primary" id="wpinv-add-item"><input type="button" value="<?php esc_attr_e( 'Create new item', 'invoicing' );?>" class="button button-primary" id="wpinv-new-item"><?php } ?><input type="button" value="<?php esc_attr_e( 'Recalculate Totals', 'invoicing' );?>" class="button button-primary wpinv-flr" id="wpinv-recalc-totals">
225
                    <?php } ?>
226
                <?php do_action( 'wpinv_invoice_items_actions', $invoice ); ?>
227
                <?php $item_actions = ob_get_clean(); echo apply_filters( 'wpinv_invoice_items_actions_content', $item_actions, $invoice, $post ); ?>
228
            </div>
229
        </div>
230
        <?php
231
    }
232
233
    /**
234
     * Display the items buy now shortcode.
235
     */
236
    public static function shortcode( $post ) {
237
238
        if ( ! is_numeric( $post ) ) {
239
            $post = $post->ID;
240
        }
241
242
        echo "<input type='text' style='min-width: 100%; font-size: small;' value='[getpaid item=$post]' disabled>";
243
    }
244
245
    public static function save( $post_id, $data, $post ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post is not used and could be removed. ( Ignorable by Annotation )

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

245
    public static function save( $post_id, $data, /** @scrutinizer ignore-unused */ $post ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
246
        $invoice        = new WPInv_Invoice( $post_id );
247
248
        // Billing
249
        $first_name     = sanitize_text_field( $data['wpinv_first_name'] );
250
        $last_name      = sanitize_text_field( $data['wpinv_last_name'] );
251
        $company        = sanitize_text_field( $data['wpinv_company'] );
252
        $vat_number     = sanitize_text_field( $data['wpinv_vat_number'] );
253
        $phone          = sanitize_text_field( $data['wpinv_phone'] );
254
        $address        = sanitize_text_field( $data['wpinv_address'] );
255
        $city           = sanitize_text_field( $data['wpinv_city'] );
256
        $zip            = sanitize_text_field( $data['wpinv_zip'] );
257
        $country        = sanitize_text_field( $data['wpinv_country'] );
258
        $state          = sanitize_text_field( $data['wpinv_state'] );
259
260
        // Details
261
        $status         = sanitize_text_field( $data['wpinv_status'] );
262
        $old_status     = !empty( $data['original_post_status'] ) ? sanitize_text_field( $data['original_post_status'] ) : $status;
0 ignored issues
show
Unused Code introduced by
The assignment to $old_status is dead and can be removed.
Loading history...
263
        $number         = sanitize_text_field( $data['wpinv_number'] );
0 ignored issues
show
Unused Code introduced by
The assignment to $number is dead and can be removed.
Loading history...
264
        $due_date       = isset( $data['wpinv_due_date'] ) ? sanitize_text_field( $data['wpinv_due_date'] ) : '';
265
        $date_created   = isset( $data['date_created'] ) ? sanitize_text_field( $data['date_created'] ) : '';
266
        //$discounts      = sanitize_text_field( $data['wpinv_discounts'] );
267
        //$discount       = sanitize_text_field( $data['wpinv_discount'] );
268
269
        $disable_taxes = 0;
270
271
        if ( ! empty( $data['disable_taxes'] ) ) {
272
            $disable_taxes = 1;
273
        }
274
275
        $ip             = $invoice->get_ip() ? $invoice->get_ip() : wpinv_get_ip();
276
277
        $invoice->set( 'due_date', $due_date );
278
        $invoice->set( 'first_name', $first_name );
279
        $invoice->set( 'last_name', $last_name );
280
        $invoice->set( 'company', $company );
281
        $invoice->set( 'vat_number', $vat_number );
282
        $invoice->set( 'phone', $phone );
283
        $invoice->set( 'address', $address );
284
        $invoice->set( 'city', $city );
285
        $invoice->set( 'zip', $zip );
286
        $invoice->set( 'country', $country );
287
        $invoice->set( 'state', $state );
288
        $invoice->set( 'status', $status );
289
        $invoice->set( 'set', $status );
290
        //$invoice->set( 'number', $number );
291
        //$invoice->set( 'discounts', $discounts );
292
        //$invoice->set( 'discount', $discount );
293
        $invoice->set( 'disable_taxes', $disable_taxes );
294
        $invoice->set( 'ip', $ip );
295
        $invoice->old_status = $_POST['original_post_status'];
296
        
297
        $currency = $invoice->get_currency();
298
        if ( ! empty( $data['wpinv_currency'] ) ) {
299
            $currency = sanitize_text_field( $data['wpinv_currency'] );
300
        }
301
302
        if ( empty( $currency ) ) {
303
            $currency = wpinv_get_currency();
304
        }
305
306
        if ( ! $invoice->is_paid() ) {
307
            $invoice->currency = $currency;
308
        }
309
310
        if ( !empty( $data['wpinv_gateway'] ) ) {
311
            $invoice->set( 'gateway', sanitize_text_field( $data['wpinv_gateway'] ) );
312
        }
313
        $saved = $invoice->save();
314
315
        $emails = '';
316
        if ( ! empty( $data['wpinv_cc'] ) ) {
317
            $emails = wpinv_clean( $data['wpinv_cc'] );
318
        }
319
        update_post_meta( $invoice->ID, 'wpinv_email_cc', $emails );
320
321
        if ( ! empty( $date_created ) && strtotime( $date_created, current_time( 'timestamp' ) ) ) {
322
323
            $time = strtotime( $date_created, current_time( 'timestamp' ) );
324
            $date = date( 'Y-m-d H:i:s', $time );
325
            $date_gmt = get_gmt_from_date( $date );
326
327
            wp_update_post(
328
                array(
329
                    'ID'            => $invoice->ID,
330
                    'post_date'     => $date,
331
                    'post_date_gmt' => $date_gmt,
332
                    'edit_date'     => true,
333
                )
334
            );
335
336
            $invoice->date = $date;
337
        }
338
339
        // Check for payment notes
340
        if ( !empty( $data['invoice_note'] ) ) {
341
            $note               = wp_kses( $data['invoice_note'], array() );
342
            $note_type          = sanitize_text_field( $data['invoice_note_type'] );
343
            $is_customer_note   = $note_type == 'customer' ? 1 : 0;
344
345
            wpinv_insert_payment_note( $invoice->ID, $note, $is_customer_note );
346
        }
347
348
        // Update user address if empty.
349
        if ( $saved && !empty( $invoice ) ) {
350
            if ( $user_id = $invoice->get_user_id() ) {
351
                $user_address = wpinv_get_user_address( $user_id, false );
352
353
                if (empty($user_address['first_name'])) {
354
                    update_user_meta( $user_id, '_wpinv_first_name', $first_name );
355
                    update_user_meta( $user_id, '_wpinv_last_name', $last_name );
356
                } else if (empty($user_address['last_name']) && $user_address['first_name'] == $first_name) {
357
                    update_user_meta( $user_id, '_wpinv_last_name', $last_name );
358
                }
359
360
                if (empty($user_address['address']) || empty($user_address['city']) || empty($user_address['state']) || empty($user_address['country'])) {
361
                    update_user_meta( $user_id, '_wpinv_address', $address );
362
                    update_user_meta( $user_id, '_wpinv_city', $city );
363
                    update_user_meta( $user_id, '_wpinv_state', $state );
364
                    update_user_meta( $user_id, '_wpinv_country', $country );
365
                    update_user_meta( $user_id, '_wpinv_zip', $zip );
366
                    update_user_meta( $user_id, '_wpinv_phone', $phone );
367
                }
368
            }
369
370
            do_action( 'wpinv_invoice_metabox_saved', $invoice );
371
        }
372
373
        return $saved;
374
    }
375
}
376