Passed
Pull Request — master (#371)
by Brian
101:52
created

wpinv_save_meta_boxes()   C

Complexity

Conditions 16
Paths 8

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 14
nc 8
nop 3
dl 0
loc 27
rs 5.5666
c 0
b 0
f 0

How to fix   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
// MUST have WordPress.
3
if ( !defined( 'WPINC' ) ) {
4
    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
5
}
6
7
function wpinv_add_meta_boxes( $post_type, $post ) {
8
    global $wpi_mb_invoice;
9
    if ( $post_type == 'wpi_invoice' && !empty( $post->ID ) ) {
10
        $wpi_mb_invoice = wpinv_get_invoice( $post->ID );
11
    }
12
    
13
    if ( !empty( $wpi_mb_invoice ) && !$wpi_mb_invoice->has_status( array( 'draft', 'auto-draft' ) ) ) {
14
        add_meta_box( 'wpinv-mb-resend-invoice', __( 'Resend Invoice', 'invoicing' ), 'WPInv_Meta_Box_Details::resend_invoice', 'wpi_invoice', 'side', 'high' );
15
    }
16
    
17
    if ( !empty( $wpi_mb_invoice ) && $wpi_mb_invoice->is_recurring() && $wpi_mb_invoice->is_parent() ) {
18
        add_meta_box( 'wpinv-mb-subscriptions', __( 'Subscriptions', 'invoicing' ), 'WPInv_Meta_Box_Details::subscriptions', 'wpi_invoice', 'side', 'high' );
19
    }
20
    
21
    if ( wpinv_is_subscription_payment( $wpi_mb_invoice ) ) {
22
        add_meta_box( 'wpinv-mb-renewals', __( 'Renewal Payment', 'invoicing' ), 'WPInv_Meta_Box_Details::renewals', 'wpi_invoice', 'side', 'high' );
23
    }
24
    
25
    add_meta_box( 'wpinv-details', __( 'Invoice Details', 'invoicing' ), 'WPInv_Meta_Box_Details::output', 'wpi_invoice', 'side', 'default' );
26
    add_meta_box( 'wpinv-payment-meta', __( 'Payment Meta', 'invoicing' ), 'WPInv_Meta_Box_Details::payment_meta', 'wpi_invoice', 'side', 'default' );
27
28
    add_meta_box( 'wpinv-payment-form-design', __( 'Payment Form', 'invoicing' ), 'WPInv_Meta_Box_Payment_Form::output', 'wpi_payment_form', 'normal' );
29
    add_meta_box( 'wpinv-payment-form-shortcode', __( 'Shortcode', 'invoicing' ), 'WPInv_Meta_Box_Payment_Form::output_shortcode', 'wpi_payment_form', 'side' );
30
   
31
    add_meta_box( 'wpinv-address', __( 'Billing Details', 'invoicing' ), 'WPInv_Meta_Box_Billing_Details::output', 'wpi_invoice', 'normal', 'high' );
32
    add_meta_box( 'wpinv-items', __( 'Invoice Items', 'invoicing' ), 'WPInv_Meta_Box_Items::output', 'wpi_invoice', 'normal', 'high' );
33
    add_meta_box( 'wpinv-notes', __( 'Invoice Notes', 'invoicing' ), 'WPInv_Meta_Box_Notes::output', 'wpi_invoice', 'normal', 'high' );
34
    
35
    if ( ! empty( $post->ID ) && get_post_meta( $post->ID, 'payment_form_data', true ) ) {
36
        add_meta_box( 'wpinv-invoice-payment-form-details', __( 'Payment Form Details', 'invoicing' ), 'WPInv_Meta_Box_Payment_Form::output_details', 'wpi_invoice', 'side', 'high' );
37
    }
38
39
	remove_meta_box('wpseo_meta', 'wpi_invoice', 'normal');
40
}
41
add_action( 'add_meta_boxes', 'wpinv_add_meta_boxes', 30, 2 );
42
43
function wpinv_save_meta_boxes( $post_id, $post, $update = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $update 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

43
function wpinv_save_meta_boxes( $post_id, $post, /** @scrutinizer ignore-unused */ $update = false ) {

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...
44
    remove_action( 'save_post', __FUNCTION__ );
45
    
46
    // $post_id and $post are required
47
    if ( empty( $post_id ) || empty( $post ) ) {
48
        return;
49
    }
50
        
51
    if ( !current_user_can( 'edit_post', $post_id ) || empty( $post->post_type ) ) {
52
        return;
53
    }
54
    
55
    // Dont' save meta boxes for revisions or autosaves
56
    if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
57
        return;
58
    }
59
        
60
    if ( $post->post_type == 'wpi_invoice' or $post->post_type == 'wpi_quote' ) {
61
        if ( ( defined( 'DOING_AJAX') && DOING_AJAX ) || isset( $_REQUEST['bulk_edit'] ) ) {
62
            return;
63
        }
64
    
65
        if ( isset( $_POST['wpinv_save_invoice'] ) && wp_verify_nonce( $_POST['wpinv_save_invoice'], 'wpinv_save_invoice' ) ) {
66
            WPInv_Meta_Box_Items::save( $post_id, $_POST, $post );
67
        }
68
    } else if ( $post->post_type == 'wpi_item' ) {
69
        GetPaid_Meta_Box_Item_Details::save( $post_id );
70
    }
71
}
72
add_action( 'save_post', 'wpinv_save_meta_boxes', 10, 3 );
73
74
function wpinv_register_item_meta_boxes() {    
75
    global $wpinv_euvat;
76
77
    // Registers the item details metabox.
78
    add_meta_box( 'wpinv_item_details', __( 'Item Details', 'invoicing' ), 'GetPaid_Meta_Box_Item_Details::output', 'wpi_item', 'normal', 'high' );
79
80
    if ( $wpinv_euvat->allow_vat_rules() ) {
81
        add_meta_box( 'wpinv_field_vat_rules', __( 'VAT rules type to use', 'invoicing' ), 'WPInv_Meta_Box_Items::vat_rules', 'wpi_item', 'normal', 'high' );
82
    }
83
    
84
    if ( $wpinv_euvat->allow_vat_classes() ) {
85
        add_meta_box( 'wpinv_field_vat_classes', __( 'VAT rates class to use', 'invoicing' ), 'WPInv_Meta_Box_Items::vat_classes', 'wpi_item', 'normal', 'high' );
86
    }
87
    
88
    add_meta_box( 'wpinv_field_item_shortcode', __( 'Shortcode', 'invoicing' ), 'WPInv_Meta_Box_Items::shortcode', 'wpi_item', 'side', 'core' );
89
    add_meta_box( 'wpinv_field_item_info', __( 'Item info', 'invoicing' ), 'WPInv_Meta_Box_Items::item_info', 'wpi_item', 'side', 'core' );
90
    add_meta_box( 'wpinv_field_meta_values', __( 'Item Meta Values', 'invoicing' ), 'WPInv_Meta_Box_Items::meta_values', 'wpi_item', 'side', 'core' );
91
}
92
93
function wpinv_register_discount_meta_boxes() {
94
    add_meta_box( 'wpinv_discount_fields', __( 'Discount Details', 'invoicing' ), 'wpinv_discount_metabox_details', 'wpi_discount', 'normal', 'high' );
95
}
96
97
function wpinv_discount_metabox_details( $post ) {
98
    $discount_id    = $post->ID;
99
    $discount       = wpinv_get_discount( $discount_id );
0 ignored issues
show
Unused Code introduced by
The assignment to $discount is dead and can be removed.
Loading history...
100
    
101
    $type               = wpinv_get_discount_type( $discount_id );
102
    $item_reqs          = wpinv_get_discount_item_reqs( $discount_id );
103
    $excluded_items     = wpinv_get_discount_excluded_items( $discount_id );
104
    $min_total          = wpinv_get_discount_min_total( $discount_id );
105
    $max_total          = wpinv_get_discount_max_total( $discount_id );
106
    $max_uses           = wpinv_get_discount_max_uses( $discount_id );
107
    $single_use         = wpinv_discount_is_single_use( $discount_id );
108
    $recurring          = (bool)wpinv_discount_is_recurring( $discount_id );
109
    $start_date         = wpinv_get_discount_start_date( $discount_id );
110
    $expiration_date    = wpinv_get_discount_expiration( $discount_id );
111
    
112
    if ( ! empty( $start_date ) && strpos( $start_date, '0000' ) === false ) {
113
        $start_time         = strtotime( $start_date );
114
        $start_h            = date_i18n( 'H', $start_time );
115
        $start_m            = date_i18n( 'i', $start_time );
116
        $start_date         = date_i18n( 'Y-m-d', $start_time );
117
    } else {
118
        $start_h            = '00';
119
        $start_m            = '00';
120
    }
121
122
    if ( ! empty( $expiration_date ) && strpos( $expiration_date, '0000' ) === false ) {
123
        $expiration_time    = strtotime( $expiration_date );
124
        $expiration_h       = date_i18n( 'H', $expiration_time );
125
        $expiration_m       = date_i18n( 'i', $expiration_time );
126
        $expiration_date    = date_i18n( 'Y-m-d', $expiration_time );
127
    } else {
128
        $expiration_h       = '23';
129
        $expiration_m       = '59';
130
    }
131
    
132
    $min_total          = $min_total > 0 ? $min_total : '';
133
    $max_total          = $max_total > 0 ? $max_total : '';
134
    $max_uses           = $max_uses > 0 ? $max_uses : '';
135
?>
136
<?php do_action( 'wpinv_discount_form_top', $post ); ?>
137
<?php wp_nonce_field( 'wpinv_discount_metabox_nonce', 'wpinv_discount_metabox_nonce' ); ;?>
138
<table class="form-table wpi-form-table">
139
    <tbody>
140
        <?php do_action( 'wpinv_discount_form_first', $post ); ?>
141
        <?php do_action( 'wpinv_discount_form_before_code', $post ); ?>
142
        <tr>
143
            <th valign="top" scope="row">
144
                <label for="wpinv_discount_code"><?php _e( 'Discount Code', 'invoicing' ); ?></label>
145
            </th>
146
            <td>
147
                <input type="text" name="code" id="wpinv_discount_code" class="medium-text" value="<?php echo esc_attr( wpinv_get_discount_code( $discount_id ) ); ?>" required>
0 ignored issues
show
Bug introduced by
It seems like wpinv_get_discount_code($discount_id) can also be of type false; however, parameter $text of esc_attr() 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

147
                <input type="text" name="code" id="wpinv_discount_code" class="medium-text" value="<?php echo esc_attr( /** @scrutinizer ignore-type */ wpinv_get_discount_code( $discount_id ) ); ?>" required>
Loading history...
148
                <p class="description"><?php _e( 'Enter a code for this discount, such as 10OFF', 'invoicing' ); ?></p>
149
            </td>
150
        </tr>
151
        <?php do_action( 'wpinv_discount_form_before_type', $post ); ?>
152
        <tr>
153
            <th valign="top" scope="row">
154
                <label for="wpinv_discount_type"><?php _e( 'Discount Type', 'invoicing' ); ?></label>
155
            </th>
156
            <td>
157
                <select id="wpinv_discount_type" name="type" class="medium-text wpi_select2">
158
                    <?php foreach ( wpinv_get_discount_types() as $value => $label ) { ?>
159
                    <option value="<?php echo $value ;?>" <?php selected( $type, $value ); ?>><?php echo $label; ?></option>
160
                    <?php } ?>
161
                </select>
162
                <p class="description"><?php _e( 'The kind of discount to apply for this discount.', 'invoicing' ); ?></p>
163
            </td>
164
        </tr>
165
        <?php do_action( 'wpinv_discount_form_before_amount', $post ); ?>
166
        <tr>
167
            <th valign="top" scope="row">
168
                <label for="wpinv_discount_amount"><?php _e( 'Amount', 'invoicing' ); ?></label>
169
            </th>
170
            <td>
171
                <input type="text" name="amount" id="wpinv_discount_amount" class="wpi-field-price wpi-price" value="<?php echo esc_attr( wpinv_get_discount_amount( $discount_id ) ); ?>" required> <font class="wpi-discount-p">%</font><font class="wpi-discount-f" style="display:none;"><?php echo wpinv_currency_symbol() ;?></font>
172
                <p style="display:none;" class="description"><?php _e( 'Enter the discount amount in USD', 'invoicing' ); ?></p>
173
                <p class="description"><?php _e( 'Enter the discount value. Ex: 10', 'invoicing' ); ?></p>
174
            </td>
175
        </tr>
176
        <?php do_action( 'wpinv_discount_form_before_items', $post ); ?>
177
        <tr>
178
            <th valign="top" scope="row">
179
                <label for="wpinv_discount_items"><?php _e( 'Items', 'invoicing' ); ?></label>
180
            </th>
181
            <td>
182
                <p><?php echo wpinv_item_dropdown( array(
183
                        'name'              => 'items[]',
184
                        'id'                => 'items',
185
                        'selected'          => $item_reqs,
186
                        'multiple'          => true,
187
                        'class'             => 'medium-text wpi_select2',
188
                        'placeholder'       => __( 'Select one or more Items', 'invoicing' ),
189
                        'show_recurring'    => true,
190
                    ) ); ?>
191
                </p>
192
                <p class="description"><?php _e( 'Items which need to be in the cart to use this discount or, for "Item Discounts", which items are discounted. If left blank, this discount can be used on any item.', 'invoicing' ); ?></p>
193
            </td>
194
        </tr>
195
        <?php do_action( 'wpinv_discount_form_before_excluded_items', $post ); ?>
196
        <tr>
197
            <th valign="top" scope="row">
198
                <label for="wpinv_discount_excluded_items"><?php _e( 'Excluded Items', 'invoicing' ); ?></label>
199
            </th>
200
            <td>
201
                <p><?php echo wpinv_item_dropdown( array(
202
                        'name'              => 'excluded_items[]',
203
                        'id'                => 'excluded_items',
204
                        'selected'          => $excluded_items,
205
                        'multiple'          => true,
206
                        'class'             => 'medium-text wpi_select2',
207
                        'placeholder'       => __( 'Select one or more Items', 'invoicing' ),
208
                        'show_recurring'    => true,
209
                    ) ); ?>
210
                </p>
211
                <p class="description"><?php _e( 'Items which are NOT allowed to use this discount.', 'invoicing' ); ?></p>
212
            </td>
213
        </tr>
214
        <?php do_action( 'wpinv_discount_form_before_start', $post ); ?>
215
        <tr>
216
            <th valign="top" scope="row">
217
                <label for="wpinv_discount_start"><?php _e( 'Start Date', 'invoicing' ); ?></label>
218
            </th>
219
            <td>
220
                <input type="text" class="w120 wpiDatepicker" id="wpinv_discount_start" data-dateFormat="yy-mm-dd" name="start" value="<?php echo esc_attr( $start_date ); ?>"> @ <select id="wpinv_discount_start_h" name="start_h">
221
                    <?php for ( $i = 0; $i <= 23; $i++ ) { $value = str_pad( $i, 2, '0', STR_PAD_LEFT ); ?>
222
                    <option value="<?php echo $value;?>" <?php selected( $value, $start_h ); ?>><?php echo $value;?></option>
223
                    <?php } ?>
224
                </select> : <select id="wpinv_discount_start_m" name="start_m">
225
                    <?php for ( $i = 0; $i <= 59; $i++ ) { $value = str_pad( $i, 2, '0', STR_PAD_LEFT ); ?>
226
                    <option value="<?php echo $value;?>" <?php selected( $value, $start_m ); ?>><?php echo $value;?></option>
227
                    <?php } ?>
228
                </select>
229
                <p class="description"><?php _e( 'Enter the start date for this discount code in the format of yyyy-mm-dd. For no start date, leave blank. If entered, the discount can only be used after or on this date.', 'invoicing' ); ?></p>
230
            </td>
231
        </tr>
232
        <?php do_action( 'wpinv_discount_form_before_expiration', $post ); ?>
233
        <tr>
234
            <th valign="top" scope="row">
235
                <label for="wpinv_discount_expiration"><?php _e( 'Expiration Date', 'invoicing' ); ?></label>
236
            </th>
237
            <td>
238
                <input type="text" class="w120 wpiDatepicker" id="wpinv_discount_expiration" data-dateFormat="yy-mm-dd" name="expiration" value="<?php echo esc_attr( $expiration_date ); ?>"> @ <select id="wpinv_discount_expiration_h" name="expiration_h">
239
                    <?php for ( $i = 0; $i <= 23; $i++ ) { $value = str_pad( $i, 2, '0', STR_PAD_LEFT ); ?>
240
                    <option value="<?php echo $value;?>" <?php selected( $value, $expiration_h ); ?>><?php echo $value;?></option>
241
                    <?php } ?>
242
                </select> : <select id="wpinv_discount_expiration_m" name="expiration_m">
243
                    <?php for ( $i = 0; $i <= 59; $i++ ) { $value = str_pad( $i, 2, '0', STR_PAD_LEFT ); ?>
244
                    <option value="<?php echo $value;?>" <?php selected( $value, $expiration_m ); ?>><?php echo $value;?></option>
245
                    <?php } ?>
246
                </select>
247
                <p class="description"><?php _e( 'Enter the expiration date for this discount code in the format of yyyy-mm-dd. Leave blank for no expiration.', 'invoicing' ); ?></p>
248
            </td>
249
        </tr>
250
        <?php do_action( 'wpinv_discount_form_before_min_total', $post ); ?>
251
        <tr>
252
            <th valign="top" scope="row">
253
                <label for="wpinv_discount_min_total"><?php _e( 'Minimum Amount', 'invoicing' ); ?></label>
254
            </th>
255
            <td>
256
                <input type="text" name="min_total" id="wpinv_discount_min_total" class="wpi-field-price wpi-price" value="<?php echo $min_total; ?>">
257
                <p class="description"><?php _e( 'This allows you to set the minimum amount (subtotal, including taxes) allowed when using the discount.', 'invoicing' ); ?></p>
258
            </td>
259
        </tr>
260
        <?php do_action( 'wpinv_discount_form_before_max_total', $post ); ?>
261
        <tr>
262
            <th valign="top" scope="row">
263
                <label for="wpinv_discount_max_total"><?php _e( 'Maximum Amount', 'invoicing' ); ?></label>
264
            </th>
265
            <td>
266
                <input type="text" name="max_total" id="wpinv_discount_max_total" class="wpi-field-price wpi-price" value="<?php echo $max_total; ?>">
267
                <p class="description"><?php _e( 'This allows you to set the maximum amount (subtotal, including taxes) allowed when using the discount.', 'invoicing' ); ?></p>
268
            </td>
269
        </tr>
270
        <?php do_action( 'wpinv_discount_form_before_recurring', $post ); ?>
271
        <tr>
272
            <th valign="top" scope="row">
273
                <label for="wpinv_discount_recurring"><?php _e( 'For recurring apply to', 'invoicing' ); ?></label>
274
            </th>
275
            <td>
276
                <select id="wpinv_discount_recurring" name="recurring" class="medium-text wpi_select2">
277
                    <option value="0" <?php selected( false, $recurring ); ?>><?php _e( 'First payment only', 'invoicing' ); ?></option>
278
                    <option value="1" <?php selected( true, $recurring ); ?>><?php _e( 'All payments', 'invoicing' ); ?></option>
279
                </select>
280
                <p class="description"><?php _e( '<b>All payments:</b> Apply this discount to all recurring payments of the recurring invoice. <br><b>First payment only:</b> Apply this discount to only first payment of the recurring invoice.', 'invoicing' ); ?></p>
281
            </td>
282
        </tr>
283
        <?php do_action( 'wpinv_discount_form_before_max_uses', $post ); ?>
284
        <tr>
285
            <th valign="top" scope="row">
286
                <label for="wpinv_discount_max_uses"><?php _e( 'Max Uses', 'invoicing' ); ?></label>
287
            </th>
288
            <td>
289
                <input type="number" min="0" step="1" id="wpinv_discount_max_uses" name="max_uses" class="medium-text" value="<?php echo $max_uses; ?>">
290
                <p class="description"><?php _e( 'The maximum number of times this discount can be used. Leave blank for unlimited.', 'invoicing' ); ?></p>
291
            </td>
292
        </tr>
293
        <?php do_action( 'wpinv_discount_form_before_single_use', $post ); ?>
294
        <tr>
295
            <th valign="top" scope="row">
296
                <label for="wpinv_discount_single_use"><?php _e( 'Use Once Per User', 'invoicing' ); ?></label>
297
            </th>
298
            <td>
299
                <input type="checkbox" value="1" name="single_use" id="wpinv_discount_single_use" <?php checked( true, $single_use ); ?>>
300
                <span class="description"><?php _e( 'Limit this discount to a single use per user?', 'invoicing' ); ?></span>
301
            </td>
302
        </tr>
303
        <?php do_action( 'wpinv_discount_form_last', $post ); ?>
304
    </tbody>
305
</table>
306
<?php do_action( 'wpinv_discount_form_bottom', $post ); ?>
307
    <?php
308
}
309
310
function wpinv_discount_metabox_save( $post_id, $post, $update = false ) {
311
    $post_type = !empty( $post ) ? $post->post_type : '';
312
    
313
    if ( $post_type != 'wpi_discount' ) {
314
        return;
315
    }
316
    
317
    if ( !isset( $_POST['wpinv_discount_metabox_nonce'] ) || ( isset( $_POST['wpinv_discount_metabox_nonce'] ) && !wp_verify_nonce( $_POST['wpinv_discount_metabox_nonce'], 'wpinv_discount_metabox_nonce' ) ) ) {
318
        return;
319
    }
320
    
321
    if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ( defined( 'DOING_AJAX') && DOING_AJAX ) || isset( $_REQUEST['bulk_edit'] ) ) {
322
        return;
323
    }
324
    
325
    if ( !current_user_can( wpinv_get_capability(), $post_id ) ) {
326
        return;
327
    }
328
    
329
    if ( !empty( $_POST['start'] ) && isset( $_POST['start_h'] ) && isset( $_POST['start_m'] ) && $_POST['start_h'] !== '' && $_POST['start_m'] !== '' ) {
330
        $_POST['start'] = $_POST['start'] . ' ' . $_POST['start_h'] . ':' . $_POST['start_m'];
331
    }
332
333
    if ( !empty( $_POST['expiration'] ) && isset( $_POST['expiration_h'] ) && isset( $_POST['expiration_m'] ) ) {
334
        $_POST['expiration'] = $_POST['expiration'] . ' ' . $_POST['expiration_h'] . ':' . $_POST['expiration_m'];
335
    }
336
    
337
    return /** @scrutinizer ignore-call */ wpinv_store_discount( $post_id, $_POST, $post, $update );
338
}
339
add_action( 'save_post', 'wpinv_discount_metabox_save', 10, 3 );
340
341
/**
342
 * Remove trash link from the default form.
343
 */
344
function getpaid_remove_action_link( $actions, $post ) {
345
    $post = get_post( $post );
346
    if ( 'wpi_payment_form' == $post->post_type && $post->ID == wpinv_get_default_payment_form() ) {
347
        unset( $actions['trash'] );
348
        unset( $actions['inline hide-if-no-js'] );
349
    }
350
    return $actions;
351
}
352
add_filter( 'post_row_actions', 'getpaid_remove_action_link', 10, 2 );
353