Passed
Push — master ( 578d12...5023d9 )
by Brian
04:33
created

getpaid_item_recurring_price_help_text()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 69
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 34
c 2
b 0
f 0
dl 0
loc 69
rs 8.7537
cc 6
nc 9
nop 2

How to fix   Long Method   

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
 * Contains item functions.
4
 *
5
 * @since 1.0.0
6
 * @package Invoicing
7
 */
8
 
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * Retrieves an item by it's ID.
13
 * 
14
 * @param int the item ID to retrieve.
15
 * @return WPInv_Item|false
16
 */
17
function wpinv_get_item_by_id( $id ) {
18
    $item = wpinv_get_item( $id );
19
    return empty( $item ) || $id != $item->get_id() ? false : $item;
20
}
21
22
/**
23
 * Retrieves an item by it's ID, Name, Slug or custom id.
24
 * 
25
 * @return WPInv_Item|false
26
 */
27
function wpinv_get_item_by( $field = '', $value = '', $type = '' ) {
28
29
    if ( 'id' == strtolower( $field ) ) {
30
        return wpinv_get_item_by_id( $field );
31
    }
32
33
    $id = WPInv_Item::get_item_id_by_field( $value, strtolower( $field ), $type );
34
    return $id ? wpinv_get_item( $id ) : false;
35
36
}
37
38
/**
39
 * Retrieves an item by it's ID, name or custom_name.
40
 * 
41
 * @param int|WPInv_Item the item to retrieve.
42
 * @return WPInv_Item|false
43
 */
44
function wpinv_get_item( $item = 0 ) {
45
    
46
    if ( empty( $item ) ) {
47
        return false;
48
    }
49
50
    $item = new WPInv_Item( $item );
51
    return $item->get_id() ? $item : false;
52
53
}
54
55
function wpinv_get_all_items( $args = array() ) {
56
57
    $args = wp_parse_args( $args, array(
58
        'status'         => array( 'publish' ),
59
        'limit'          => get_option( 'posts_per_page' ),
60
        'page'           => 1,
61
        'exclude'        => array(),
62
        'orderby'        => 'date',
63
        'order'          => 'DESC',
64
        'type'           => wpinv_item_types(),
65
        'meta_query'     => array(),
66
        'return'         => 'objects',
67
        'paginate'       => false,
68
    ) );
69
70
    $wp_query_args = array(
71
        'post_type'      => 'wpi_item',
72
        'post_status'    => $args['status'],
73
        'posts_per_page' => $args['limit'],
74
        'meta_query'     => $args['meta_query'],
75
        'fields'         => 'ids',
76
        'orderby'        => $args['orderby'],
77
        'order'          => $args['order'],
78
        'paged'          => absint( $args['page'] ),
79
    );
80
81
    if ( ! empty( $args['exclude'] ) ) {
82
        $wp_query_args['post__not_in'] = array_map( 'absint', $args['exclude'] );
83
    }
84
85
    if ( ! $args['paginate' ] ) {
86
        $wp_query_args['no_found_rows'] = true;
87
    }
88
89
    if ( ! empty( $args['search'] ) ) {
90
        $wp_query_args['s'] = $args['search'];
91
    }
92
93
    if ( ! empty( $args['type'] ) && $args['type'] !== wpinv_item_types() ) {
94
        $types = wpinv_parse_list( $args['type'] );
95
        $wp_query_args['meta_query'][] = array(
96
            'key'     => '_wpinv_type',
97
            'value'   => implode( ',', $types ),
98
            'compare' => 'IN',
99
        );
100
    }
101
102
    $wp_query_args = apply_filters('wpinv_get_items_args', $wp_query_args, $args);
103
104
    // Get results.
105
    $items = new WP_Query( $wp_query_args );
106
107
    if ( 'objects' === $args['return'] ) {
108
        $return = array_map( 'wpinv_get_item_by_id', $items->posts );
109
    } elseif ( 'self' === $args['return'] ) {
110
        return $items;
111
    } else {
112
        $return = $items->posts;
113
    }
114
115
    if ( $args['paginate' ] ) {
116
        return (object) array(
117
            'items'      => $return,
118
            'total'         => $items->found_posts,
119
            'max_num_pages' => $items->max_num_pages,
120
        );
121
    } else {
122
        return $return;
123
    }
124
125
}
126
127
function wpinv_is_free_item( $item_id = 0 ) {
128
    if( empty( $item_id ) ) {
129
        return false;
130
    }
131
132
    $item = new WPInv_Item( $item_id );
133
    
134
    return $item->is_free();
135
}
136
137
/**
138
 * Checks whether an item is editable.
139
 * 
140
 * @param WP_Post|WPInv_Item|Int $item The item to check for.
141
 */
142
function wpinv_item_is_editable( $item = 0 ) {
143
144
    // Fetch the item.
145
    $item = new WPInv_Item( $item );
146
147
    // Check if it is editable.
148
    return $item->is_editable();
149
}
150
151
function wpinv_get_item_price( $item_id = 0 ) {
152
    if( empty( $item_id ) ) {
153
        return false;
154
    }
155
156
    $item = new WPInv_Item( $item_id );
157
    
158
    return $item->get_price();
159
}
160
161
function wpinv_is_recurring_item( $item_id = 0 ) {
162
    if( empty( $item_id ) ) {
163
        return false;
164
    }
165
166
    $item = new WPInv_Item( $item_id );
167
    
168
    return $item->is_recurring();
169
}
170
171
function wpinv_item_price( $item_id = 0 ) {
172
    if( empty( $item_id ) ) {
173
        return false;
174
    }
175
176
    $price = wpinv_get_item_price( $item_id );
177
    $price = wpinv_price( wpinv_format_amount( $price ) );
0 ignored issues
show
Bug introduced by
wpinv_format_amount($price) of type string is incompatible with the type double expected by parameter $amount of wpinv_price(). ( Ignorable by Annotation )

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

177
    $price = wpinv_price( /** @scrutinizer ignore-type */ wpinv_format_amount( $price ) );
Loading history...
Bug introduced by
It seems like $price can also be of type false; however, parameter $amount of wpinv_format_amount() does only seem to accept double, 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

177
    $price = wpinv_price( wpinv_format_amount( /** @scrutinizer ignore-type */ $price ) );
Loading history...
178
    
179
    return apply_filters( 'wpinv_item_price', $price, $item_id );
180
}
181
182
function wpinv_item_show_price( $item_id = 0, $echo = true ) {
183
    if ( empty( $item_id ) ) {
184
        $item_id = get_the_ID();
185
    }
186
187
    $price = wpinv_item_price( $item_id );
188
189
    $price           = apply_filters( 'wpinv_item_price', wpinv_sanitize_amount( $price ), $item_id );
0 ignored issues
show
Bug introduced by
It seems like $price can also be of type false; however, parameter $amount of wpinv_sanitize_amount() 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

189
    $price           = apply_filters( 'wpinv_item_price', wpinv_sanitize_amount( /** @scrutinizer ignore-type */ $price ), $item_id );
Loading history...
190
    $formatted_price = '<span class="wpinv_price" id="wpinv_item_' . $item_id . '">' . $price . '</span>';
191
    $formatted_price = apply_filters( 'wpinv_item_price_after_html', $formatted_price, $item_id, $price );
192
193
    if ( $echo ) {
194
        echo $formatted_price;
195
    } else {
196
        return $formatted_price;
197
    }
198
}
199
200
function wpinv_get_item_final_price( $item_id = 0, $amount_override = null ) {
201
    if ( is_null( $amount_override ) ) {
202
        $original_price = get_post_meta( $item_id, '_wpinv_price', true );
203
    } else {
204
        $original_price = $amount_override;
205
    }
206
    
207
    $price = $original_price;
208
209
    return apply_filters( 'wpinv_get_item_final_price', $price, $item_id );
210
}
211
212
function wpinv_item_custom_singular_name( $item_id ) {
213
    if( empty( $item_id ) ) {
214
        return false;
215
    }
216
217
    $item = new WPInv_Item( $item_id );
218
    
219
    return $item->get_custom_singular_name();
220
}
221
222
function wpinv_get_item_types() {
223
    $item_types = array(
224
            'custom'    => __( 'Standard', 'invoicing' ),
225
            'fee'       => __( 'Fee', 'invoicing' ),
226
        );
227
    return apply_filters( 'wpinv_get_item_types', $item_types );
228
}
229
230
function wpinv_item_types() {
231
    $item_types = wpinv_get_item_types();
232
    
233
    return ( !empty( $item_types ) ? array_keys( $item_types ) : array() );
234
}
235
236
function wpinv_get_item_type( $item_id ) {
237
    if( empty( $item_id ) ) {
238
        return false;
239
    }
240
241
    $item = new WPInv_Item( $item_id );
242
    
243
    return $item->get_type();
244
}
245
246
function wpinv_item_type( $item_id ) {
247
    $item_types = wpinv_get_item_types();
248
    
249
    $item_type = wpinv_get_item_type( $item_id );
250
    
251
    if ( empty( $item_type ) ) {
252
        $item_type = '-';
253
    }
254
    
255
    $item_type = isset( $item_types[$item_type] ) ? $item_types[$item_type] : __( $item_type, 'invoicing' );
256
257
    return apply_filters( 'wpinv_item_type', $item_type, $item_id );
258
}
259
260
function wpinv_record_item_in_log( $item_id = 0, $file_id, $user_info, $ip, $invoice_id ) {
261
    global $wpinv_logs;
262
    
263
    if ( empty( $wpinv_logs ) ) {
264
        return false;
265
    }
266
267
    $log_data = array(
268
        'post_parent'	=> $item_id,
269
        'log_type'		=> 'wpi_item'
270
    );
271
272
    $user_id = isset( $user_info['user_id'] ) ? $user_info['user_id'] : (int) -1;
273
274
    $log_meta = array(
275
        'user_info'	=> $user_info,
276
        'user_id'	=> $user_id,
277
        'file_id'	=> (int)$file_id,
278
        'ip'		=> $ip,
279
        'invoice_id'=> $invoice_id,
280
    );
281
282
    $wpinv_logs->insert_log( $log_data, $log_meta );
283
}
284
285
function wpinv_remove_item_logs_on_delete( $item_id = 0 ) {
286
    if ( 'wpi_item' !== get_post_type( $item_id ) )
287
        return;
288
289
    global $wpinv_logs;
290
    
291
    if ( empty( $wpinv_logs ) ) {
292
        return false;
293
    }
294
295
    // Remove all log entries related to this item
296
    $wpinv_logs->delete_logs( $item_id );
297
}
298
add_action( 'delete_post', 'wpinv_remove_item_logs_on_delete' );
299
300
function wpinv_get_random_item( $post_ids = true ) {
301
    wpinv_get_random_items( 1, $post_ids );
302
}
303
304
function wpinv_get_random_items( $num = 3, $post_ids = true ) {
305
    if ( $post_ids ) {
306
        $args = array( 'post_type' => 'wpi_item', 'orderby' => 'rand', 'post_count' => $num, 'fields' => 'ids' );
307
    } else {
308
        $args = array( 'post_type' => 'wpi_item', 'orderby' => 'rand', 'post_count' => $num );
309
    }
310
    
311
    $args  = apply_filters( 'wpinv_get_random_items', $args );
312
    
313
    return get_posts( $args );
314
}
315
316
function wpinv_get_item_token( $url = '' ) {
317
    $args    = array();
318
    $hash    = apply_filters( 'wpinv_get_url_token_algorithm', 'sha256' );
319
    $secret  = apply_filters( 'wpinv_get_url_token_secret', hash( $hash, wp_salt() ) );
320
321
    $parts   = parse_url( $url );
322
    $options = array();
323
324
    if ( isset( $parts['query'] ) ) {
325
        wp_parse_str( $parts['query'], $query_args );
326
327
        if ( ! empty( $query_args['o'] ) ) {
328
            $options = explode( ':', rawurldecode( $query_args['o'] ) );
329
330
            if ( in_array( 'ip', $options ) ) {
331
                $args['ip'] = wpinv_get_ip();
332
            }
333
334
            if ( in_array( 'ua', $options ) ) {
335
                $ua = wpinv_get_user_agent();
336
                $args['user_agent'] = rawurlencode( $ua );
337
            }
338
        }
339
    }
340
341
    $args = apply_filters( 'wpinv_get_url_token_args', $args, $url, $options );
342
343
    $args['secret'] = $secret;
344
    $args['token']  = false;
345
346
    $url   = add_query_arg( $args, $url );
347
    $parts = parse_url( $url );
348
349
    if ( ! isset( $parts['path'] ) ) {
350
        $parts['path'] = '';
351
    }
352
353
    $token = md5( $parts['path'] . '?' . $parts['query'] );
354
355
    return $token;
356
}
357
358
function wpinv_validate_url_token( $url = '' ) {
359
    $ret   = false;
360
    $parts = parse_url( $url );
361
362
    if ( isset( $parts['query'] ) ) {
363
        wp_parse_str( $parts['query'], $query_args );
364
365
        $allowed = apply_filters( 'wpinv_url_token_allowed_params', array(
366
            'item',
367
            'ttl',
368
            'token'
369
        ) );
370
371
        $remove = array();
372
373
        foreach( $query_args as $key => $value ) {
374
            if( false === in_array( $key, $allowed ) ) {
375
                $remove[] = $key;
376
            }
377
        }
378
379
        if( ! empty( $remove ) ) {
380
            $url = remove_query_arg( $remove, $url );
381
        }
382
383
        if ( isset( $query_args['ttl'] ) && current_time( 'timestamp' ) > $query_args['ttl'] ) {
384
            wp_die( apply_filters( 'wpinv_item_link_expired_text', __( 'Sorry but your item link has expired.', 'invoicing' ) ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) );
385
        }
386
387
        if ( isset( $query_args['token'] ) && $query_args['token'] == wpinv_get_item_token( $url ) ) {
388
            $ret = true;
389
        }
390
391
    }
392
393
    return apply_filters( 'wpinv_validate_url_token', $ret, $url, $query_args );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $query_args does not seem to be defined for all execution paths leading up to this point.
Loading history...
394
}
395
396
function wpinv_item_in_cart( $item_id = 0, $options = array() ) {
397
    $cart_items = wpinv_get_cart_contents();
0 ignored issues
show
Deprecated Code introduced by
The function wpinv_get_cart_contents() has been deprecated. ( Ignorable by Annotation )

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

397
    $cart_items = /** @scrutinizer ignore-deprecated */ wpinv_get_cart_contents();
Loading history...
398
399
    $ret = false;
400
401
    if ( is_array( $cart_items ) ) {
0 ignored issues
show
introduced by
The condition is_array($cart_items) is always true.
Loading history...
402
        foreach ( $cart_items as $item ) {
403
            if ( $item['id'] == $item_id ) {
404
                $ret = true;
405
                break;
406
            }
407
        }
408
    }
409
410
    return (bool) apply_filters( 'wpinv_item_in_cart', $ret, $item_id, $options );
411
}
412
413
function wpinv_get_cart_item_tax( $item_id = 0, $subtotal = '', $options = array() ) {
414
    $tax = 0;
415
    if ( ! wpinv_item_is_tax_exclusive( $item_id ) ) {
416
        $country = !empty( $_POST['country'] ) ? $_POST['country'] : false;
417
        $state   = isset( $_POST['state'] ) ? $_POST['state'] : '';
418
419
        $tax = wpinv_calculate_tax( $subtotal, $country, $state, $item_id );
420
    }
421
422
    return apply_filters( 'wpinv_get_cart_item_tax', $tax, $item_id, $subtotal, $options );
423
}
424
425
function wpinv_cart_item_price( $item, $currency = '' ) {
426
427
    if( empty( $currency ) ) {
428
        $currency = wpinv_get_currency();
429
    }
430
431
    $item_id    = isset( $item['id'] ) ? $item['id'] : 0;
432
    $price      = isset( $item['item_price'] ) ? wpinv_round_amount( $item['item_price'] ) : 0;
433
    $tax        = wpinv_price( wpinv_format_amount( $item['tax'] ) );
0 ignored issues
show
Bug introduced by
wpinv_format_amount($item['tax']) of type string is incompatible with the type double expected by parameter $amount of wpinv_price(). ( Ignorable by Annotation )

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

433
    $tax        = wpinv_price( /** @scrutinizer ignore-type */ wpinv_format_amount( $item['tax'] ) );
Loading history...
434
    
435
    if ( !wpinv_is_free_item( $item_id ) && !wpinv_item_is_tax_exclusive( $item_id ) ) {
436
        if ( wpinv_prices_show_tax_on_checkout() && !wpinv_prices_include_tax() ) {
437
            $price += $tax;
438
        }
439
        
440
        if( !wpinv_prices_show_tax_on_checkout() && wpinv_prices_include_tax() ) {
441
            $price -= $tax;
442
        }        
443
    }
444
445
    $price = wpinv_price( wpinv_format_amount( $price ), $currency );
446
447
    return apply_filters( 'wpinv_cart_item_price_label', $price, $item );
448
}
449
450
function wpinv_cart_item_subtotal( $item, $currency = '' ) {
451
452
    if( empty( $currency ) ) {
453
        $currency = wpinv_get_currency();
454
    }
455
456
    $subtotal   = isset( $item['subtotal'] ) ? $item['subtotal'] : 0;
457
    $subtotal   = wpinv_price( wpinv_format_amount( $subtotal ), $currency );
0 ignored issues
show
Bug introduced by
wpinv_format_amount($subtotal) of type string is incompatible with the type double expected by parameter $amount of wpinv_price(). ( Ignorable by Annotation )

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

457
    $subtotal   = wpinv_price( /** @scrutinizer ignore-type */ wpinv_format_amount( $subtotal ), $currency );
Loading history...
458
459
    return apply_filters( 'wpinv_cart_item_subtotal_label', $subtotal, $item );
460
}
461
462
function wpinv_cart_item_tax( $item, $currency = '' ) {
463
    $tax        = '';
464
    $tax_rate   = '';
465
466
    if( empty( $currency ) ) {
467
        $currency = wpinv_get_currency();
468
    }
469
    
470
    if ( isset( $item['tax'] ) && $item['tax'] > 0 && $item['subtotal'] > 0 ) {
471
        $tax      = wpinv_price( wpinv_format_amount( $item['tax'] ), $currency );
0 ignored issues
show
Bug introduced by
wpinv_format_amount($item['tax']) of type string is incompatible with the type double expected by parameter $amount of wpinv_price(). ( Ignorable by Annotation )

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

471
        $tax      = wpinv_price( /** @scrutinizer ignore-type */ wpinv_format_amount( $item['tax'] ), $currency );
Loading history...
472
        $tax_rate = !empty( $item['vat_rate'] ) ? $item['vat_rate'] : ( $item['tax'] / $item['subtotal'] ) * 100;
473
        $tax_rate = $tax_rate > 0 ? (float)wpinv_round_amount( $tax_rate, 4 ) : '';
474
        $tax_rate = $tax_rate != '' ? ' <small class="tax-rate normal small">(' . $tax_rate . '%)</small>' : '';
475
    }
476
    
477
    $tax        = $tax . $tax_rate;
478
    
479
    if ( $tax === '' ) {
480
        $tax = 0; // Zero tax
481
    }
482
483
    return apply_filters( 'wpinv_cart_item_tax_label', $tax, $item );
484
}
485
486
function wpinv_get_cart_item_price( $item_id = 0, $cart_item = array(), $options = array(), $remove_tax_from_inclusive = false ) {
487
    $price = 0;
488
    
489
    // Set custom price
490
    if ( isset( $cart_item['custom_price'] ) && $cart_item['custom_price'] !== '' ) {
491
        $price = $cart_item['custom_price'];
492
    } else {
493
        $variable_prices = wpinv_has_variable_prices( $item_id );
494
495
        if ( $variable_prices ) {
0 ignored issues
show
introduced by
The condition $variable_prices is always false.
Loading history...
496
            $prices = wpinv_get_variable_prices( $item_id );
0 ignored issues
show
Bug introduced by
The function wpinv_get_variable_prices was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

496
            $prices = /** @scrutinizer ignore-call */ wpinv_get_variable_prices( $item_id );
Loading history...
497
498
            if ( $prices ) {
499
                if( ! empty( $options ) ) {
500
                    $price = isset( $prices[ $options['price_id'] ] ) ? $prices[ $options['price_id'] ]['amount'] : false;
501
                } else {
502
                    $price = false;
503
                }
504
            }
505
        }
506
507
        if( ! $variable_prices || false === $price ) {
0 ignored issues
show
introduced by
The condition $variable_prices is always false.
Loading history...
508
            if($cart_item['item_price'] > 0){
509
                $price = $cart_item['item_price'];
510
            } else {
511
                // Get the standard Item price if not using variable prices
512
                $price = wpinv_get_item_price( $item_id );
513
            }
514
        }
515
    }
516
517
    if ( $remove_tax_from_inclusive && wpinv_prices_include_tax() ) {
518
        $price -= wpinv_get_cart_item_tax( $item_id, $price, $options );
519
    }
520
521
    return apply_filters( 'wpinv_cart_item_price', $price, $item_id, $cart_item, $options, $remove_tax_from_inclusive );
522
}
523
524
function wpinv_get_cart_item_price_id( $item = array() ) {
525
    if( isset( $item['item_number'] ) ) {
526
        $price_id = isset( $item['item_number']['options']['price_id'] ) ? $item['item_number']['options']['price_id'] : null;
527
    } else {
528
        $price_id = isset( $item['options']['price_id'] ) ? $item['options']['price_id'] : null;
529
    }
530
    return $price_id;
531
}
532
533
function wpinv_get_cart_item_price_name( $item = array() ) {
534
    $price_id = (int)wpinv_get_cart_item_price_id( $item );
535
    $prices   = wpinv_get_variable_prices( $item['id'] );
0 ignored issues
show
Bug introduced by
The function wpinv_get_variable_prices was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

535
    $prices   = /** @scrutinizer ignore-call */ wpinv_get_variable_prices( $item['id'] );
Loading history...
536
    $name     = ! empty( $prices[ $price_id ] ) ? $prices[ $price_id ]['name'] : '';
537
    return apply_filters( 'wpinv_get_cart_item_price_name', $name, $item['id'], $price_id, $item );
538
}
539
540
function wpinv_get_cart_item_name( $item = array() ) {
541
    $item_title = !empty( $item['name'] ) ? $item['name'] : get_the_title( $item['id'] );
542
543
    if ( empty( $item_title ) ) {
544
        $item_title = $item['id'];
545
    }
546
547
    /*
548
    if ( wpinv_has_variable_prices( $item['id'] ) && false !== wpinv_get_cart_item_price_id( $item ) ) {
549
        $item_title .= ' - ' . wpinv_get_cart_item_price_name( $item );
550
    }
551
    */
552
553
    return apply_filters( 'wpinv_get_cart_item_name', $item_title, $item['id'], $item );
554
}
555
556
function wpinv_has_variable_prices( $item_id = 0 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $item_id 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

556
function wpinv_has_variable_prices( /** @scrutinizer ignore-unused */ $item_id = 0 ) {

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...
557
    return false;
558
}
559
560
function wpinv_get_item_position_in_cart( $item_id = 0, $options = array() ) {
561
    $cart_items = wpinv_get_cart_contents();
0 ignored issues
show
Deprecated Code introduced by
The function wpinv_get_cart_contents() has been deprecated. ( Ignorable by Annotation )

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

561
    $cart_items = /** @scrutinizer ignore-deprecated */ wpinv_get_cart_contents();
Loading history...
562
563
    if ( !is_array( $cart_items ) ) {
0 ignored issues
show
introduced by
The condition is_array($cart_items) is always true.
Loading history...
564
        return false; // Empty cart
565
    } else {
566
        foreach ( $cart_items as $position => $item ) {
567
            if ( $item['id'] == $item_id ) {
568
                if ( isset( $options['price_id'] ) && isset( $item['options']['price_id'] ) ) {
569
                    if ( (int) $options['price_id'] == (int) $item['options']['price_id'] ) {
570
                        return $position;
571
                    }
572
                } else {
573
                    return $position;
574
                }
575
            }
576
        }
577
    }
578
579
    return false; // Not found
580
}
581
582
function wpinv_get_cart_item_quantity( $item ) {
583
    if ( wpinv_item_quantities_enabled() ) {
584
        $quantity = !empty( $item['quantity'] ) && (int)$item['quantity'] > 0 ? absint( $item['quantity'] ) : 1;
585
    } else {
586
        $quantity = 1;
587
    }
588
    
589
    if ( $quantity < 1 ) {
590
        $quantity = 1;
591
    }
592
    
593
    return apply_filters( 'wpinv_get_cart_item_quantity', $quantity, $item );
594
}
595
596
function wpinv_get_item_suffix( $item, $html = true ) {
597
    if ( empty( $item ) ) {
598
        return NULL;
599
    }
600
    
601
    if ( is_int( $item ) ) {
602
        $item = new WPInv_Item( $item );
603
    }
604
    
605
    if ( !( is_object( $item ) && is_a( $item, 'WPInv_Item' ) ) ) {
606
        return NULL;
607
    }
608
    
609
    $suffix = $item->is_recurring() ? ' <span class="wpi-suffix">' . __( '(r)', 'invoicing' ) . '</span>' : '';
610
    
611
    if ( !$html && $suffix ) {
612
        $suffix = strip_tags( $suffix );
613
    }
614
    
615
    return apply_filters( 'wpinv_get_item_suffix', $suffix, $item, $html );
616
}
617
618
function wpinv_remove_item( $item = 0, $force_delete = false ) {
619
    if ( empty( $item ) ) {
620
        return NULL;
621
    }
622
    
623
    if ( is_int( $item ) ) {
624
        $item = new WPInv_Item( $item );
625
    }
626
    
627
    if ( !( is_object( $item ) && is_a( $item, 'WPInv_Item' ) ) ) {
628
        return NULL;
629
    }
630
    
631
    do_action( 'wpinv_pre_delete_item', $item );
632
633
    wp_delete_post( $item->ID, $force_delete );
634
635
    do_action( 'wpinv_post_delete_item', $item );
636
}
637
638
function wpinv_can_delete_item( $post_id ) {
639
    $return = wpinv_current_user_can_manage_invoicing() ? true : false;
640
    
641
    if ( $return && wpinv_item_in_use( $post_id ) ) {
642
        $return = false; // Don't delete item already use in invoices.
643
    }
644
    
645
    return apply_filters( 'wpinv_can_delete_item', $return, $post_id );
646
}
647
648
function wpinv_admin_action_delete() {
649
    $screen = get_current_screen();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $screen is correct as get_current_screen() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
650
    
651
    if ( !empty( $screen->post_type ) && $screen->post_type == 'wpi_item' && !empty( $_REQUEST['post'] ) && is_array( $_REQUEST['post'] ) ) {
652
        $post_ids = array();
653
        
654
        foreach ( $_REQUEST['post'] as $post_id ) {
655
            if ( !wpinv_can_delete_item( $post_id ) ) {
656
                continue;
657
            }
658
            
659
            $post_ids[] = $post_id;
660
        }
661
        
662
        $_REQUEST['post'] = $post_ids;
663
    }
664
}
665
add_action( 'admin_action_trash', 'wpinv_admin_action_delete', -10 );
666
add_action( 'admin_action_delete', 'wpinv_admin_action_delete', -10 );
667
668
function wpinv_check_delete_item( $check, $post, $force_delete ) {
669
    if ( $post->post_type == 'wpi_item' ) {
670
        if ( $force_delete && !wpinv_can_delete_item( $post->ID ) ) {
671
            return true;
672
        }
673
    }
674
    
675
    return $check;
676
}
677
add_filter( 'pre_delete_post', 'wpinv_check_delete_item', 10, 3 );
678
679
function wpinv_item_in_use( $item_id ) {
680
    global $wpdb, $wpi_items_in_use;
681
    
682
    if ( !$item_id > 0 ) {
683
        return false;
684
    }
685
    
686
    if ( !empty( $wpi_items_in_use ) ) {
687
        if ( isset( $wpi_items_in_use[$item_id] ) ) {
688
            return $wpi_items_in_use[$item_id];
689
        }
690
    } else {
691
        $wpi_items_in_use = array();
692
    }
693
    
694
    $statuses   = array_keys( wpinv_get_invoice_statuses( true, true ) );
695
    
696
    $query  = "SELECT p.ID FROM " . $wpdb->posts . " AS p INNER JOIN " . $wpdb->postmeta . " AS pm ON p.ID = pm.post_id WHERE p.post_type = 'wpi_invoice' AND p.post_status IN( '" . implode( "','", $statuses ) . "' ) AND pm.meta_key = '_wpinv_item_ids' AND FIND_IN_SET( '" . (int)$item_id . "', pm.meta_value )";
697
    $in_use = $wpdb->get_var( $query ) > 0 ? true : false;
698
    
699
    $wpi_items_in_use[$item_id] = $in_use;
700
    
701
    return $in_use;
702
}
703
704
/**
705
 * Create/Update an item.
706
 * 
707
 * @param array $args an array of arguments to create the item.
708
 * 
709
 *    Here are all the args (with defaults) that you can set/modify.
710
 *    array(
711
 *		  'ID'                   => 0,           - If specified, the item with that ID will be updated.
712
 *        'parent_id'            => 0,           - Int. Parent item ID.
713
 *		  'status'               => 'draft',     - String. Item status - either draft, pending or publish.
714
 *		  'date_created'         => null,        - String. strtotime() compatible string.
715
 *        'date_modified'        => null,        - String. strtotime() compatible string.
716
 *        'name'                 => '',          - String. Required. Item name.
717
 *        'description'          => '',          - String. Item description.
718
 *        'author'               => 1,           - int. Owner of the item.
719
 *        'price'                => 0,           - float. Item price.
720
 *        'vat_rule'             => 'digital',   - string. VAT rule.
721
 *        'vat_class'            => '_standard', - string. VAT class.
722
 *        'type'                 => 'custom',    - string. Item type.
723
 *        'custom_id'            => null,        - string. Custom item id.
724
 *        'custom_name'          => null,        - string. Custom item name.
725
 *        'custom_singular_name' => null,        - string. Custom item singular name.
726
 *        'is_editable'          => 1,           - int|bool. Whether or not the item is editable.
727
 *        'is_dynamic_pricing'   => 0,           - int|bool. Whether or not users can update the item price.
728
 *        'minimum_price'        => 0,           - float. If dynamic, set the minimum price that a user can set..
729
 *        'is_recurring'         => 0,           - int|bool. Whether or not this item is recurring.
730
 *        'recurring_period'     => 'D',         - string. If recurring, set the recurring period as either days (D), weeks (W), months (M) or years (Y).
731
 *        'recurring_interval'   => 1,           - int. The recurring interval.
732
 *        'recurring_limit'      => 0,           - int. The recurring limit. Enter 0 for unlimited.
733
 *        'is_free_trial'        => false,       - int|bool. Whether or not this item has a free trial.
734
 *        'trial_period'         => 'D',         - string. If it has a free trial, set the trial period as either days (D), weeks (W), months (M) or years (Y).
735
 *        'trial_interval'       => 1,           - int. The trial interval.
736
 *    );
737
 * @param bool $wp_error whether or not to return a WP_Error on failure.
738
 * @return bool|WP_Error|WPInv_Item
739
 */
740
function wpinv_create_item( $args = array(), $wp_error = false ) {
741
742
    // Prepare the item.
743
    if ( ! empty( $args['custom_id'] ) && empty( $args['ID'] ) ) {
744
        $type = empty( $args['type'] ) ? 'custom' : $args['type'];
745
        $item = wpinv_get_item_by( 'custom_id', $args['custom_id'], $type );
746
747
        if ( ! empty( $item ) ) {
748
            $args['ID'] = $item->get_id();
749
        }
750
751
    }
752
753
    // Do we have an item?
754
    if ( ! empty( $args['ID'] ) ) {
755
        $item = new WPInv_Item( $args['ID'] );
756
    } else {
757
        $item = new WPInv_Item();
758
    }
759
760
    // Do we have an error?
761
    if ( ! empty( $item->last_error ) ) {
762
        return $wp_error ? new WP_Error( 'invalid_item', $item->last_error ) : false;
763
    }
764
765
    // Update item props.
766
    $item->set_props( $args );
767
768
    // Save the item.
769
    $item->save();
770
771
    // Do we have an error?
772
    if ( ! empty( $item->last_error ) ) {
773
        return $wp_error ? new WP_Error( 'not_saved', $item->last_error ) : false;
774
    }
775
776
    // Was the item saved?
777
    if ( ! $item->get_id() ) {
778
        return $wp_error ? new WP_Error( 'not_saved', __( 'An error occured while saving the item', 'invoicing' ) ) : false;
779
    }
780
781
    return $item;
782
783
}
784
785
/**
786
 * Updates an item.
787
 * 
788
 * @see wpinv_create_item()
789
 */
790
function wpinv_update_item( $args = array(), $wp_error = false ) {
791
    return wpinv_create_item( $args, $wp_error );
792
}
793
794
/**
795
 * Sanitizes a recurring period
796
 */
797
function getpaid_sanitize_recurring_period( $period, $full = false ) {
798
799
    $periods = array(
800
        'D' => 'day',
801
        'W' => 'week',
802
        'M' => 'month',
803
        'Y' => 'year',
804
    );
805
806
    if ( ! isset( $periods[ $period ] ) ) {
807
        $period = 'D';
808
    }
809
810
    return $full ? $periods[ $period ] : $period;
811
812
}
813
814
/**
815
 * Retrieves recurring price description.
816
 * 
817
 * @param WPInv_Item|GetPaid_Form_Item $item
818
 */
819
function getpaid_item_recurring_price_help_text( $item, $currency = '' ) {
820
821
    // Abort if it is not recurring.
822
    if ( ! $item->is_recurring() ) {
823
        return '';
824
    }
825
826
    $initial_price   = wpinv_price( $item->get_initial_price(), $currency );
827
    $recurring_price = wpinv_price( $item->get_recurring_price(), $currency );
828
    $period          = getpaid_get_subscription_period_label( $item->get_recurring_period(), $item->get_recurring_interval(), '' );
829
    $initial_class   = 'getpaid-item-initial-price';
830
    $recurring_class = 'getpaid-item-recurring-price';
831
832
    if ( $item instanceof GetPaid_Form_Item ) {
833
        $initial_price   = wpinv_price( $item->get_sub_total(), $currency );
834
        $recurring_price = wpinv_price( $item->get_recurring_sub_total(), $currency );
835
    }
836
837
    // For free trial items.
838
    if ( $item->has_free_trial() ) {
839
        $trial_period = getpaid_get_subscription_period_label( $item->get_trial_period(), $item->get_trial_interval() );
840
841
        if ( 0 == $item->get_initial_price() ) {
842
843
            return sprintf(
844
845
                // translators: $1: is the trial period, $2: is the recurring price, $3: is the susbcription period
846
                _x( 'Free for %1$s then %2$s / %3$s', 'Item subscription amount. (e.g.: Free for 1 month then $120 / year)', 'invoicing' ),
847
                $trial_period,
848
                "<span class='$recurring_class'>$recurring_price</span>",
849
                $period
850
851
            );
852
853
        }
854
855
        return sprintf(
856
857
            // translators: $1: is the initial price, $2: is the trial period, $3: is the recurring price, $4: is the susbcription period
858
            _x( '%1$s for %2$s then %3$s / %4$s', 'Item subscription amount. (e.g.: $7 for 1 month then $120 / year)', 'invoicing' ),
859
            "<span class='$initial_class'>$initial_price</span>",
860
            $trial_period,
861
            "<span class='$recurring_class'>$recurring_price</span>",
862
            $period
863
864
        );
865
866
    }
867
868
    if ( $initial_price == $recurring_price ) {
869
870
        return sprintf(
871
872
            // translators: $1: is the recurring price, $2: is the susbcription period
873
            _x( '%1$s / %2$s', 'Item subscription amount. (e.g.: $120 / year)', 'invoicing' ),
874
            "<span class='$recurring_class'>$recurring_price</span>",
875
            $period
876
877
        );
878
879
    }
880
881
    return sprintf(
882
883
        // translators: $1: is the initial price, $2: is the recurring price, $3: is the susbcription period
884
        _x( 'Initial payment of %1$s then %2$s / %3$s', 'Item subscription amount. (e.g.: Initial payment of $7 then $120 / year)', 'invoicing' ),
885
        "<span class='$initial_class'>$initial_price</span>",
886
        "<span class='$recurring_class'>$recurring_price</span>",
887
        $period
888
889
    );
890
891
}
892