Passed
Pull Request — master (#104)
by Kiran
03:41
created

wpinv-template-functions.php ➔ wpinv_item_dropdown()   C

Complexity

Conditions 12
Paths 10

Size

Total Lines 78
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 55
nc 10
nop 1
dl 0
loc 78
rs 5.1746
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
 * Contains functions related to Invoicing plugin.
4
 *
5
 * @since 1.0.0
6
 * @package Invoicing
7
 */
8
 
9
// MUST have WordPress.
10
if ( !defined( 'WPINC' ) ) {
11
    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
12
}
13
14
if ( !is_admin() ) {
15
    add_filter( 'template_include', 'wpinv_template', 10, 1 );
16
    add_action( 'wpinv_invoice_print_body_start', 'wpinv_display_invoice_top_bar' );
17
    add_action( 'wpinv_invoice_top_bar_left', 'wpinv_invoice_display_left_actions' );
18
    add_action( 'wpinv_invoice_top_bar_right', 'wpinv_invoice_display_right_actions' );
19
}
20
21
function wpinv_template_path() {
22
    return apply_filters( 'wpinv_template_path', 'invoicing/' );
23
}
24
25
function wpinv_display_invoice_top_bar( $invoice ) {
26
    if ( empty( $invoice ) ) {
27
        return;
28
    }
29
    ?>
30
    <div class="row wpinv-top-bar no-print">
31
        <div class="container">
32
            <div class="col-xs-6">
33
                <?php do_action( 'wpinv_invoice_top_bar_left', $invoice );?>
34
            </div>
35
            <div class="col-xs-6 text-right">
36
                <?php do_action( 'wpinv_invoice_top_bar_right', $invoice );?>
37
            </div>
38
        </div>
39
    </div>
40
    <?php
41
}
42
43
function wpinv_invoice_display_left_actions( $invoice ) {
44
    if ( empty( $invoice ) ) {
45
        return; // Exit if invoice is not set.
46
    }
47
    
48
    if ( $invoice->post_type == 'wpi_invoice' ) {
49
        if ( $invoice->needs_payment() ) {
50
            ?> <a class="btn btn-success btn-sm" title="<?php esc_attr_e( 'Pay This Invoice', 'invoicing' ); ?>" href="<?php echo esc_url( $invoice->get_checkout_payment_url() ); ?>"><?php _e( 'Pay For Invoice', 'invoicing' ); ?></a><?php
51
        }
52
    }
53
    do_action('wpinv_invoice_display_left_actions', $invoice);
54
}
55
56
function wpinv_invoice_display_right_actions( $invoice ) {
57
    if ( empty( $invoice ) ) {
58
        return; // Exit if invoice is not set.
59
    }
60
61
    if ( $invoice->post_type == 'wpi_invoice' ) { ?>
62
        <a class="btn btn-primary btn-sm" onclick="window.print();" href="javascript:void(0)"><?php _e( 'Print Invoice', 'invoicing' ); ?></a>
63
        <?php if ( is_user_logged_in() ) { ?>
64
        &nbsp;&nbsp;<a class="btn btn-warning btn-sm" href="<?php echo esc_url( wpinv_get_history_page_uri() ); ?>"><?php _e( 'Invoice History', 'invoicing' ); ?></a>
65
        <?php }
66
    }
67
    do_action('wpinv_invoice_display_right_actions', $invoice);
68
}
69
70 View Code Duplication
function wpinv_before_invoice_content( $content ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    global $post;
72
73
    if ( !empty( $post ) && $post->post_type == 'wpi_invoice' && is_singular( 'wpi_invoice' ) && is_main_query() ) {
74
        ob_start();
75
        do_action( 'wpinv_before_invoice_content', $post->ID );
76
        $content = ob_get_clean() . $content;
77
    }
78
79
    return $content;
80
}
81
add_filter( 'the_content', 'wpinv_before_invoice_content' );
82
83 View Code Duplication
function wpinv_after_invoice_content( $content ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    global $post;
85
86
    if ( !empty( $post ) && $post->post_type == 'wpi_invoice' && is_singular( 'wpi_invoice' ) && is_main_query() ) {
87
        ob_start();
88
        do_action( 'wpinv_after_invoice_content', $post->ID );
89
        $content .= ob_get_clean();
90
    }
91
92
    return $content;
93
}
94
add_filter( 'the_content', 'wpinv_after_invoice_content' );
95
96
function wpinv_get_templates_dir() {
97
    return WPINV_PLUGIN_DIR . 'templates';
98
}
99
100
function wpinv_get_templates_url() {
101
    return WPINV_PLUGIN_URL . 'templates';
102
}
103
104
function wpinv_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
105
    if ( ! empty( $args ) && is_array( $args ) ) {
106
		extract( $args );
107
	}
108
109
	$located = wpinv_locate_template( $template_name, $template_path, $default_path );
110
	// Allow 3rd party plugin filter template file from their plugin.
111
	$located = apply_filters( 'wpinv_get_template', $located, $template_name, $args, $template_path, $default_path );
112
113
	if ( ! file_exists( $located ) ) {
114
        _doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $located ), '2.1' );
115
		return;
116
	}
117
118
	do_action( 'wpinv_before_template_part', $template_name, $template_path, $located, $args );
119
120
	include( $located );
121
122
	do_action( 'wpinv_after_template_part', $template_name, $template_path, $located, $args );
123
}
124
125
function wpinv_get_template_html( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
126
	ob_start();
127
	wpinv_get_template( $template_name, $args, $template_path, $default_path );
128
	return ob_get_clean();
129
}
130
131
function wpinv_locate_template( $template_name, $template_path = '', $default_path = '' ) {
132
    if ( ! $template_path ) {
133
        $template_path = wpinv_template_path();
134
    }
135
136
    if ( ! $default_path ) {
137
        $default_path = WPINV_PLUGIN_DIR . 'templates/';
138
    }
139
140
    // Look within passed path within the theme - this is priority.
141
    $template = locate_template(
142
        array(
143
            trailingslashit( $template_path ) . $template_name,
144
            $template_name
145
        )
146
    );
147
148
    // Get default templates/
149
    if ( !$template && $default_path ) {
150
        $template = trailingslashit( $default_path ) . $template_name;
151
    }
152
153
    // Return what we found.
154
    return apply_filters( 'wpinv_locate_template', $template, $template_name, $template_path );
155
}
156
157
function wpinv_get_template_part( $slug, $name = null, $load = true ) {
158
	do_action( 'get_template_part_' . $slug, $slug, $name );
159
160
	// Setup possible parts
161
	$templates = array();
162
	if ( isset( $name ) )
163
		$templates[] = $slug . '-' . $name . '.php';
164
	$templates[] = $slug . '.php';
165
166
	// Allow template parts to be filtered
167
	$templates = apply_filters( 'wpinv_get_template_part', $templates, $slug, $name );
168
169
	// Return the part that is found
170
	return wpinv_locate_tmpl( $templates, $load, false );
171
}
172
173
function wpinv_locate_tmpl( $template_names, $load = false, $require_once = true ) {
174
	// No file found yet
175
	$located = false;
176
177
	// Try to find a template file
178
	foreach ( (array)$template_names as $template_name ) {
179
180
		// Continue if template is empty
181
		if ( empty( $template_name ) )
182
			continue;
183
184
		// Trim off any slashes from the template name
185
		$template_name = ltrim( $template_name, '/' );
186
187
		// try locating this template file by looping through the template paths
188
		foreach( wpinv_get_theme_template_paths() as $template_path ) {
189
190
			if( file_exists( $template_path . $template_name ) ) {
191
				$located = $template_path . $template_name;
192
				break;
193
			}
194
		}
195
196
		if( !empty( $located ) ) {
197
			break;
198
		}
199
	}
200
201
	if ( ( true == $load ) && ! empty( $located ) )
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
202
		load_template( $located, $require_once );
203
204
	return $located;
205
}
206
207
function wpinv_get_theme_template_paths() {
208
	$template_dir = wpinv_get_theme_template_dir_name();
209
210
	$file_paths = array(
211
		1 => trailingslashit( get_stylesheet_directory() ) . $template_dir,
212
		10 => trailingslashit( get_template_directory() ) . $template_dir,
213
		100 => wpinv_get_templates_dir()
214
	);
215
216
	$file_paths = apply_filters( 'wpinv_template_paths', $file_paths );
217
218
	// sort the file paths based on priority
219
	ksort( $file_paths, SORT_NUMERIC );
220
221
	return array_map( 'trailingslashit', $file_paths );
222
}
223
224
function wpinv_get_theme_template_dir_name() {
225
	return trailingslashit( apply_filters( 'wpinv_templates_dir', 'wpinv_templates' ) );
226
}
227
228
function wpinv_checkout_meta_tags() {
229
230
	$pages   = array();
231
	$pages[] = wpinv_get_option( 'success_page' );
232
	$pages[] = wpinv_get_option( 'failure_page' );
233
	$pages[] = wpinv_get_option( 'invoice_history_page' );
234
235
	if( !wpinv_is_checkout() && !is_page( $pages ) ) {
236
		return;
237
	}
238
239
	echo '<meta name="robots" content="noindex,nofollow" />' . "\n";
240
}
241
add_action( 'wp_head', 'wpinv_checkout_meta_tags' );
242
243
function wpinv_add_body_classes( $class ) {
244
	$classes = (array)$class;
245
246
	if( wpinv_is_checkout() ) {
247
		$classes[] = 'wpinv-checkout';
248
		$classes[] = 'wpinv-page';
249
	}
250
251
	if( wpinv_is_success_page() ) {
252
		$classes[] = 'wpinv-success';
253
		$classes[] = 'wpinv-page';
254
	}
255
256
	if( wpinv_is_failed_transaction_page() ) {
257
		$classes[] = 'wpinv-failed-transaction';
258
		$classes[] = 'wpinv-page';
259
	}
260
261
	if( wpinv_is_invoice_history_page() ) {
262
		$classes[] = 'wpinv-history';
263
		$classes[] = 'wpinv-page';
264
	}
265
266
	if( wpinv_is_test_mode() ) {
267
		$classes[] = 'wpinv-test-mode';
268
		$classes[] = 'wpinv-page';
269
	}
270
271
	return array_unique( $classes );
272
}
273
add_filter( 'body_class', 'wpinv_add_body_classes' );
274
275
function wpinv_html_dropdown( $name = 'wpinv_discounts', $selected = 0, $status = '' ) {
276
    $args = array( 'nopaging' => true );
277
278
    if ( ! empty( $status ) )
279
        $args['post_status'] = $status;
280
281
    $discounts = wpinv_get_discounts( $args );
282
    $options   = array();
283
284
    if ( $discounts ) {
285
        foreach ( $discounts as $discount ) {
286
            $options[ absint( $discount->ID ) ] = esc_html( get_the_title( $discount->ID ) );
287
        }
288
    } else {
289
        $options[0] = __( 'No discounts found', 'invoicing' );
290
    }
291
292
    $output = wpinv_html_select( array(
293
        'name'             => $name,
294
        'selected'         => $selected,
295
        'options'          => $options,
296
        'show_option_all'  => false,
297
        'show_option_none' => false,
298
    ) );
299
300
    return $output;
301
}
302
303
function wpinv_html_year_dropdown( $name = 'year', $selected = 0, $years_before = 5, $years_after = 0 ) {
304
    $current     = date( 'Y' );
305
    $start_year  = $current - absint( $years_before );
306
    $end_year    = $current + absint( $years_after );
307
    $selected    = empty( $selected ) ? date( 'Y' ) : $selected;
308
    $options     = array();
309
310
    while ( $start_year <= $end_year ) {
311
        $options[ absint( $start_year ) ] = $start_year;
312
        $start_year++;
313
    }
314
315
    $output = wpinv_html_select( array(
316
        'name'             => $name,
317
        'selected'         => $selected,
318
        'options'          => $options,
319
        'show_option_all'  => false,
320
        'show_option_none' => false
321
    ) );
322
323
    return $output;
324
}
325
326
function wpinv_html_month_dropdown( $name = 'month', $selected = 0 ) {
327
    $month   = 1;
328
    $options = array();
329
    $selected = empty( $selected ) ? date( 'n' ) : $selected;
330
331
    while ( $month <= 12 ) {
332
        $options[ absint( $month ) ] = wpinv_month_num_to_name( $month );
333
        $month++;
334
    }
335
336
    $output = wpinv_html_select( array(
337
        'name'             => $name,
338
        'selected'         => $selected,
339
        'options'          => $options,
340
        'show_option_all'  => false,
341
        'show_option_none' => false
342
    ) );
343
344
    return $output;
345
}
346
347
function wpinv_html_select( $args = array() ) {
348
    $defaults = array(
349
        'options'          => array(),
350
        'name'             => null,
351
        'class'            => '',
352
        'id'               => '',
353
        'selected'         => 0,
354
        'chosen'           => false,
355
        'placeholder'      => null,
356
        'multiple'         => false,
357
        'show_option_all'  => _x( 'All', 'all dropdown items', 'invoicing' ),
358
        'show_option_none' => _x( 'None', 'no dropdown items', 'invoicing' ),
359
        'data'             => array(),
360
        'onchange'         => null,
361
        'required'         => false,
362
        'disabled'         => false,
363
        'readonly'         => false,
364
    );
365
366
    $args = wp_parse_args( $args, $defaults );
367
368
    $data_elements = '';
369
    foreach ( $args['data'] as $key => $value ) {
370
        $data_elements .= ' data-' . esc_attr( $key ) . '="' . esc_attr( $value ) . '"';
371
    }
372
373
    if( $args['multiple'] ) {
374
        $multiple = ' MULTIPLE';
375
    } else {
376
        $multiple = '';
377
    }
378
379
    if( $args['chosen'] ) {
380
        $args['class'] .= ' wpinv-select-chosen';
381
    }
382
383
    if( $args['placeholder'] ) {
384
        $placeholder = $args['placeholder'];
385
    } else {
386
        $placeholder = '';
387
    }
388
    
389
    $options = '';
390 View Code Duplication
    if( !empty( $args['onchange'] ) ) {
391
        $options .= ' onchange="' . esc_attr( $args['onchange'] ) . '"';
392
    }
393
    
394
    if( !empty( $args['required'] ) ) {
395
        $options .= ' required="required"';
396
    }
397
    
398
    if( !empty( $args['disabled'] ) ) {
399
        $options .= ' disabled';
400
    }
401
    
402
    if( !empty( $args['readonly'] ) ) {
403
        $options .= ' readonly';
404
    }
405
406
    $class  = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
407
    $output = '<select name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( $args['id'] ) . '" class="wpinv-select ' . $class . '"' . $multiple . ' data-placeholder="' . $placeholder . '" ' . trim( $options ) . $data_elements . '>';
408
409 View Code Duplication
    if ( $args['show_option_all'] ) {
410
        if( $args['multiple'] ) {
411
            $selected = selected( true, in_array( 0, $args['selected'] ), false );
412
        } else {
413
            $selected = selected( $args['selected'], 0, false );
414
        }
415
        $output .= '<option value="all"' . $selected . '>' . esc_html( $args['show_option_all'] ) . '</option>';
416
    }
417
418
    if ( !empty( $args['options'] ) ) {
419
420 View Code Duplication
        if ( $args['show_option_none'] ) {
421
            if( $args['multiple'] ) {
422
                $selected = selected( true, in_array( "", $args['selected'] ), false );
423
            } else {
424
                $selected = selected( $args['selected'] === "", true, false );
425
            }
426
            $output .= '<option value=""' . $selected . '>' . esc_html( $args['show_option_none'] ) . '</option>';
427
        }
428
429
        foreach( $args['options'] as $key => $option ) {
430
431
            if( $args['multiple'] && is_array( $args['selected'] ) ) {
432
                $selected = selected( true, (bool)in_array( $key, $args['selected'] ), false );
433
            } else {
434
                $selected = selected( $args['selected'], $key, false );
435
            }
436
437
            $output .= '<option value="' . esc_attr( $key ) . '"' . $selected . '>' . esc_html( $option ) . '</option>';
438
        }
439
    }
440
441
    $output .= '</select>';
442
443
    return $output;
444
}
445
446
function wpinv_item_dropdown( $args = array() ) {
447
    $defaults = array(
448
        'name'              => 'wpi_item',
449
        'id'                => 'wpi_item',
450
        'class'             => '',
451
        'multiple'          => false,
452
        'selected'          => 0,
453
        'chosen'            => false,
454
        'number'            => 100,
455
        'placeholder'       => __( 'Choose a item', 'invoicing' ),
456
        'data'              => array( 'search-type' => 'item' ),
457
        'show_option_all'   => false,
458
        'show_option_none'  => false,
459
        'show_recurring'    => false,
460
    );
461
462
    $args = wp_parse_args( $args, $defaults );
463
464
    $item_args = array(
465
        'post_type'      => 'wpi_item',
466
        'orderby'        => 'title',
467
        'order'          => 'ASC',
468
        'posts_per_page' => $args['number']
469
    );
470
    
471
    $item_args  = apply_filters( 'wpinv_item_dropdown_query_args', $item_args, $args, $defaults );
472
473
    $items      = get_posts( $item_args );
474
    $options    = array();
475
    if ( $items ) {
476
        foreach ( $items as $item ) {
477
            $title = esc_html( $item->post_title );
478
            
479
            if ( !empty( $args['show_recurring'] ) ) {
480
                $title .= wpinv_get_item_suffix( $item->ID, false );
481
            }
482
            
483
            $options[ absint( $item->ID ) ] = $title;
484
        }
485
    }
486
487
    // This ensures that any selected items are included in the drop down
488
    if( is_array( $args['selected'] ) ) {
489
        foreach( $args['selected'] as $item ) {
490
            if( ! in_array( $item, $options ) ) {
491
                $title = get_the_title( $item );
492
                if ( !empty( $args['show_recurring'] ) ) {
493
                    $title .= wpinv_get_item_suffix( $item, false );
494
                }
495
                $options[$item] = $title;
496
            }
497
        }
498
    } elseif ( is_numeric( $args['selected'] ) && $args['selected'] !== 0 ) {
499
        if ( ! in_array( $args['selected'], $options ) ) {
500
            $title = get_the_title( $args['selected'] );
501
            if ( !empty( $args['show_recurring'] ) ) {
502
                $title .= wpinv_get_item_suffix( $args['selected'], false );
503
            }
504
            $options[$args['selected']] = get_the_title( $args['selected'] );
505
        }
506
    }
507
508
    $output = wpinv_html_select( array(
509
        'name'             => $args['name'],
510
        'selected'         => $args['selected'],
511
        'id'               => $args['id'],
512
        'class'            => $args['class'],
513
        'options'          => $options,
514
        'chosen'           => $args['chosen'],
515
        'multiple'         => $args['multiple'],
516
        'placeholder'      => $args['placeholder'],
517
        'show_option_all'  => $args['show_option_all'],
518
        'show_option_none' => $args['show_option_none'],
519
        'data'             => $args['data'],
520
    ) );
521
522
    return $output;
523
}
524
525
function wpinv_html_checkbox( $args = array() ) {
526
    $defaults = array(
527
        'name'     => null,
528
        'current'  => null,
529
        'class'    => 'wpinv-checkbox',
530
        'options'  => array(
531
            'disabled' => false,
532
            'readonly' => false
533
        )
534
    );
535
536
    $args = wp_parse_args( $args, $defaults );
537
538
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
539
    $options = '';
540
    if ( ! empty( $args['options']['disabled'] ) ) {
541
        $options .= ' disabled="disabled"';
542
    } elseif ( ! empty( $args['options']['readonly'] ) ) {
543
        $options .= ' readonly';
544
    }
545
546
    $output = '<input type="checkbox"' . $options . ' name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( $args['name'] ) . '" class="' . $class . ' ' . esc_attr( $args['name'] ) . '" ' . checked( 1, $args['current'], false ) . ' />';
547
548
    return $output;
549
}
550
551
function wpinv_html_text( $args = array() ) {
552
    // Backwards compatibility
553
    if ( func_num_args() > 1 ) {
554
        $args = func_get_args();
555
556
        $name  = $args[0];
557
        $value = isset( $args[1] ) ? $args[1] : '';
558
        $label = isset( $args[2] ) ? $args[2] : '';
559
        $desc  = isset( $args[3] ) ? $args[3] : '';
560
    }
561
562
    $defaults = array(
563
        'id'           => '',
564
        'name'         => isset( $name )  ? $name  : 'text',
565
        'value'        => isset( $value ) ? $value : null,
566
        'label'        => isset( $label ) ? $label : null,
567
        'desc'         => isset( $desc )  ? $desc  : null,
568
        'placeholder'  => '',
569
        'class'        => 'regular-text',
570
        'disabled'     => false,
571
        'readonly'     => false,
572
        'required'     => false,
573
        'autocomplete' => '',
574
        'data'         => false
575
    );
576
577
    $args = wp_parse_args( $args, $defaults );
578
579
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
580
    $options = '';
581
    if( $args['required'] ) {
582
        $options .= ' required="required"';
583
    }
584
    if( $args['readonly'] ) {
585
        $options .= ' readonly';
586
    }
587
    if( $args['readonly'] ) {
588
        $options .= ' readonly';
589
    }
590
591
    $data = '';
592
    if ( !empty( $args['data'] ) ) {
593
        foreach ( $args['data'] as $key => $value ) {
594
            $data .= 'data-' . wpinv_sanitize_key( $key ) . '="' . esc_attr( $value ) . '" ';
595
        }
596
    }
597
598
    $output = '<span id="wpinv-' . wpinv_sanitize_key( $args['name'] ) . '-wrap">';
599
    $output .= '<label class="wpinv-label" for="' . wpinv_sanitize_key( $args['id'] ) . '">' . esc_html( $args['label'] ) . '</label>';
600 View Code Duplication
    if ( ! empty( $args['desc'] ) ) {
601
        $output .= '<span class="wpinv-description">' . esc_html( $args['desc'] ) . '</span>';
602
    }
603
604
    $output .= '<input type="text" name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( $args['id'] )  . '" autocomplete="' . esc_attr( $args['autocomplete'] )  . '" value="' . esc_attr( $args['value'] ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" class="' . $class . '" ' . $data . ' ' . trim( $options ) . '/>';
605
606
    $output .= '</span>';
607
608
    return $output;
609
}
610
611
function wpinv_html_date_field( $args = array() ) {
612
    if( empty( $args['class'] ) ) {
613
        $args['class'] = 'wpiDatepicker';
614
    } elseif( ! strpos( $args['class'], 'wpiDatepicker' ) ) {
615
        $args['class'] .= ' wpiDatepicker';
616
    }
617
618
    return wpinv_html_text( $args );
619
}
620
621
function wpinv_html_textarea( $args = array() ) {
622
    $defaults = array(
623
        'name'        => 'textarea',
624
        'value'       => null,
625
        'label'       => null,
626
        'desc'        => null,
627
        'class'       => 'large-text',
628
        'disabled'    => false
629
    );
630
631
    $args = wp_parse_args( $args, $defaults );
632
633
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
634
    $disabled = '';
635
    if( $args['disabled'] ) {
636
        $disabled = ' disabled="disabled"';
637
    }
638
639
    $output = '<span id="wpinv-' . wpinv_sanitize_key( $args['name'] ) . '-wrap">';
640
    $output .= '<label class="wpinv-label" for="' . wpinv_sanitize_key( $args['name'] ) . '">' . esc_html( $args['label'] ) . '</label>';
641
    $output .= '<textarea name="' . esc_attr( $args['name'] ) . '" id="' . wpinv_sanitize_key( $args['name'] ) . '" class="' . $class . '"' . $disabled . '>' . esc_attr( $args['value'] ) . '</textarea>';
642
643 View Code Duplication
    if ( ! empty( $args['desc'] ) ) {
644
        $output .= '<span class="wpinv-description">' . esc_html( $args['desc'] ) . '</span>';
645
    }
646
    $output .= '</span>';
647
648
    return $output;
649
}
650
651
function wpinv_html_ajax_user_search( $args = array() ) {
652
    $defaults = array(
653
        'name'        => 'user_id',
654
        'value'       => null,
655
        'placeholder' => __( 'Enter username', 'invoicing' ),
656
        'label'       => null,
657
        'desc'        => null,
658
        'class'       => '',
659
        'disabled'    => false,
660
        'autocomplete'=> 'off',
661
        'data'        => false
662
    );
663
664
    $args = wp_parse_args( $args, $defaults );
665
666
    $args['class'] = 'wpinv-ajax-user-search ' . $args['class'];
667
668
    $output  = '<span class="wpinv_user_search_wrap">';
669
        $output .= wpinv_html_text( $args );
670
        $output .= '<span class="wpinv_user_search_results hidden"><a class="wpinv-ajax-user-cancel" title="' . __( 'Cancel', 'invoicing' ) . '" aria-label="' . __( 'Cancel', 'invoicing' ) . '" href="#">x</a><span></span></span>';
671
    $output .= '</span>';
672
673
    return $output;
674
}
675
676
function wpinv_ip_geolocation() {
677
    global $wpinv_euvat;
678
    
679
    $ip         = !empty( $_GET['ip'] ) ? sanitize_text_field( $_GET['ip'] ) : '';    
680
    $content    = '';
0 ignored issues
show
Unused Code introduced by
$content is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
681
    $iso        = '';
682
    $country    = '';
683
    $region     = '';
684
    $city       = '';
685
    $longitude  = '';
686
    $latitude   = '';
687
    $credit     = '';
688
    $address    = '';
689
    
690
    if ( wpinv_get_option( 'vat_ip_lookup' ) == 'geoip2' && $geoip2_city = $wpinv_euvat->geoip2_city_record( $ip ) ) {
691
        try {
692
            $iso        = $geoip2_city->country->isoCode;
693
            $country    = $geoip2_city->country->name;
694
            $region     = !empty( $geoip2_city->subdivisions ) && !empty( $geoip2_city->subdivisions[0]->name ) ? $geoip2_city->subdivisions[0]->name : '';
695
            $city       = $geoip2_city->city->name;
696
            $longitude  = $geoip2_city->location->longitude;
697
            $latitude   = $geoip2_city->location->latitude;
698
            $credit     = __( 'Geolocated using the information by MaxMind, available from <a href="http://www.maxmind.com" target="_blank">www.maxmind.com</a>', 'invoicing' );
699
        } catch( Exception $e ) { }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
700
    }
701
    
702
    if ( !( $iso && $longitude && $latitude ) && function_exists( 'simplexml_load_file' ) ) {
703
        try {
704
            $load_xml = simplexml_load_file( 'http://www.geoplugin.net/xml.gp?ip=' . $ip );
705
            
706
            if ( !empty( $load_xml ) && isset( $load_xml->geoplugin_countryCode ) && !empty( $load_xml->geoplugin_latitude ) && !empty( $load_xml->geoplugin_longitude ) ) {
707
                $iso        = $load_xml->geoplugin_countryCode;
0 ignored issues
show
Bug introduced by
The property geoplugin_countryCode does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
708
                $country    = $load_xml->geoplugin_countryName;
0 ignored issues
show
Bug introduced by
The property geoplugin_countryName does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
709
                $region     = !empty( $load_xml->geoplugin_regionName ) ? $load_xml->geoplugin_regionName : '';
710
                $city       = !empty( $load_xml->geoplugin_city ) ? $load_xml->geoplugin_city : '';
711
                $longitude  = $load_xml->geoplugin_longitude;
0 ignored issues
show
Bug introduced by
The property geoplugin_longitude does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
712
                $latitude   = $load_xml->geoplugin_latitude;
0 ignored issues
show
Bug introduced by
The property geoplugin_latitude does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
713
                $credit     = $load_xml->geoplugin_credit;
0 ignored issues
show
Bug introduced by
The property geoplugin_credit does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
714
                $credit     = __( 'Geolocated using the information by geoPlugin, available from <a href="http://www.geoplugin.com" target="_blank">www.geoplugin.com</a>', 'invoicing' ) . '<br>' . $load_xml->geoplugin_credit;
715
            }
716
        } catch( Exception $e ) { }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
717
    }
718
    
719
    if ( $iso && $longitude && $latitude ) {
720
        if ( $city ) {
721
            $address .= $city . ', ';
722
        }
723
        
724
        if ( $region ) {
725
            $address .= $region . ', ';
726
        }
727
        
728
        $address .= $country . ' (' . $iso . ')';
729
        $content = '<p>'. sprintf( __( '<b>Address:</b> %s', 'invoicing' ), $address ) . '</p>';
730
        $content .= '<p>'. $credit . '</p>';
731
    } else {
732
        $content = '<p>'. sprintf( __( 'Unable to find geolocation for the IP address: %s', 'invoicing' ), $ip ) . '</p>';
733
    }
734
    ?>
735
<!DOCTYPE html>
736
<html><head><title><?php echo sprintf( __( 'IP: %s', 'invoicing' ), $ip );?></title><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.0-rc.1/leaflet.css" /><style>html,body{height:100%;margin:0;padding:0;width:100%}body{text-align:center;background:#fff;color:#222;font-size:small;}body,p{font-family: arial,sans-serif}#map{margin:auto;width:100%;height:calc(100% - 120px);min-height:240px}</style></head>
737
<body>
738
    <?php if ( $latitude && $latitude ) { ?>
739
    <div id="map"></div>
740
        <script src="//cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.0-rc.1/leaflet.js"></script>
741
        <script type="text/javascript">
742
        var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
743
            osmAttrib = '&copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors',
744
            osm = L.tileLayer(osmUrl, {maxZoom: 18, attribution: osmAttrib}),
745
            latlng = new L.LatLng(<?php echo $latitude;?>, <?php echo $longitude;?>);
746
747
        var map = new L.Map('map', {center: latlng, zoom: 12, layers: [osm]});
748
749
        var marker = new L.Marker(latlng);
750
        map.addLayer(marker);
751
752
        marker.bindPopup("<p><?php esc_attr_e( $address );?></p>");
753
    </script>
754
    <?php } ?>
755
    <div style="height:100px"><?php echo $content; ?></div>
756
</body></html>
757
<?php
758
    exit;
759
}
760
add_action( 'wp_ajax_wpinv_ip_geolocation', 'wpinv_ip_geolocation' );
761
add_action( 'wp_ajax_nopriv_wpinv_ip_geolocation', 'wpinv_ip_geolocation' );
762
763
// Set up the template for the invoice.
764
function wpinv_template( $template ) {
765
    global $post, $wp_query;
766
    
767
    if ( ( is_single() || is_404() ) && !empty( $post->ID ) && (get_post_type( $post->ID ) == 'wpi_invoice' or get_post_type( $post->ID ) == 'wpi_quote')) {
768
        if ( wpinv_user_can_view_invoice( $post->ID ) ) {
769
            $template = wpinv_get_template_part( 'wpinv-invoice-print', false, false );
770
        } else {
771
            $template = wpinv_get_template_part( 'wpinv-invalid-access', false, false );
772
        }
773
    }
774
775
    return $template;
776
}
777
778
function wpinv_get_business_address() {
779
    $business_address   = wpinv_store_address();
780
    $business_address   = !empty( $business_address ) ? wpautop( wp_kses_post( $business_address ) ) : '';
781
    
782
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
783
    $default_country    = wpinv_get_default_country();
784
    $default_state      = wpinv_get_default_state();
785
    
786
    $address_fields = array();
787
    if ( !empty( $default_state ) ) {
788
        $address_fields[] = wpinv_state_name( $default_state, $default_country );
789
    }
790
    
791
    if ( !empty( $default_country ) ) {
792
        $address_fields[] = wpinv_country_name( $default_country );
793
    }
794
    
795
    if ( !empty( $address_fields ) ) {
796
        $address_fields = implode( ", ", $address_fields );
797
                
798
        $business_address .= wpautop( wp_kses_post( $address_fields ) );
799
    }
800
    */
801
    
802
    $business_address = $business_address ? '<div class="address">' . $business_address . '</div>' : '';
803
    
804
    return apply_filters( 'wpinv_get_business_address', $business_address );
805
}
806
807
function wpinv_display_from_address() {
808
    global $wpinv_euvat;
809
    
810
    $from_name = $wpinv_euvat->get_company_name();
811
    if (empty($from_name)) {
812
        $from_name = wpinv_get_business_name();
813
    }
814
    ?><div class="from col-xs-2"><strong><?php _e( 'From:', 'invoicing' ) ?></strong></div>
815
    <div class="wrapper col-xs-10">
816
        <div class="name"><?php echo esc_html( $from_name ); ?></div>
817
        <?php if ( $address = wpinv_get_business_address() ) { ?>
818
        <div class="address"><?php echo wpautop( wp_kses_post( $address ) );?></div>
819
        <?php } ?>
820
        <?php if ( $email_from = wpinv_mail_get_from_address() ) { ?>
821
        <div class="email_from"><?php echo wp_sprintf( __( 'Email: %s', 'invoicing' ), $email_from );?></div>
822
        <?php } ?>
823
    </div>
824
    <?php
825
}
826
827
function wpinv_watermark( $id = 0 ) {
828
    $output = wpinv_get_watermark( $id );
829
    
830
    return apply_filters( 'wpinv_get_watermark', $output, $id );
831
}
832
833
function wpinv_get_watermark( $id ) {
834
    if ( !$id > 0 ) {
835
        return NULL;
836
    }
837
    $invoice = wpinv_get_invoice( $id );
838
    
839
    if ( !empty( $invoice ) && "wpi_invoice" === $invoice->post_type ) {
840
        if ( $invoice->is_paid() ) {
841
            return __( 'Paid', 'invoicing' );
842
        }
843
        if ( $invoice->is_refunded() ) {
844
            return __( 'Refunded', 'invoicing' );
845
        }
846
        if ( $invoice->has_status( array( 'wpi-cancelled' ) ) ) {
847
            return __( 'Cancelled', 'invoicing' );
848
        }
849
    }
850
    
851
    return NULL;
852
}
853
854
function wpinv_display_invoice_details( $invoice ) {
855
    global $wpinv_euvat;
856
    
857
    $invoice_id = $invoice->ID;
858
    $vat_name   = $wpinv_euvat->get_vat_name();
859
    $use_taxes  = wpinv_use_taxes();
860
    
861
    $invoice_status = wpinv_get_invoice_status( $invoice_id );
862
    ?>
863
    <table class="table table-bordered table-sm">
864 View Code Duplication
        <?php if ( $invoice_number = wpinv_get_invoice_number( $invoice_id ) ) { ?>
865
            <tr class="wpi-row-number">
866
                <th><?php echo apply_filters( 'wpinv_invoice_number_label', __( 'Invoice Number', 'invoicing' ), $invoice ); ?></th>
867
                <td><?php echo esc_html( $invoice_number ); ?></td>
868
            </tr>
869
        <?php } ?>
870
        <tr class="wpi-row-status">
871
            <th><?php echo apply_filters( 'wpinv_invoice_status_label', __( 'Invoice Status', 'invoicing' ), $invoice ); ?></th>
872
            <td><?php echo wpinv_invoice_status_label( $invoice_status, wpinv_get_invoice_status( $invoice_id, true ) ); ?></td>
873
        </tr>
874 View Code Duplication
        <?php if ( $invoice->is_renewal() ) { ?>
875
        <tr class="wpi-row-parent">
876
            <th><?php echo apply_filters( 'wpinv_invoice_parent_invoice_label', __( 'Parent Invoice', 'invoicing' ), $invoice ); ?></th>
877
            <td><?php echo wpinv_invoice_link( $invoice->parent_invoice ); ?></td>
878
        </tr>
879
        <?php } ?>
880 View Code Duplication
        <?php if ( $gateway_name = wpinv_get_payment_gateway_name( $invoice_id ) ) { ?>
881
            <tr class="wpi-row-gateway">
882
                <th><?php echo apply_filters( 'wpinv_invoice_payment_method_label', __( 'Payment Method', 'invoicing' ), $invoice ); ?></th>
883
                <td><?php echo $gateway_name; ?></td>
884
            </tr>
885
        <?php } ?>
886
        <?php if ( $invoice_date = wpinv_get_invoice_date( $invoice_id ) ) { ?>
887
            <tr class="wpi-row-date">
888
                <th><?php echo apply_filters( 'wpinv_invoice_date_label', __( 'Invoice Date', 'invoicing' ), $invoice ); ?></th>
889
                <td><?php echo $invoice_date; ?></td>
890
            </tr>
891
        <?php } ?>
892
        <?php if ( wpinv_get_option( 'overdue_active' ) && $invoice->needs_payment() && ( $due_date = $invoice->get_due_date( true ) ) ) { ?>
893
            <tr class="wpi-row-date">
894
                <th><?php echo apply_filters( 'wpinv_invoice_due_date_label', __( 'Due Date', 'invoicing' ), $invoice ); ?></th>
895
                <td><?php echo $due_date; ?></td>
896
            </tr>
897
        <?php } ?>
898 View Code Duplication
        <?php if ( $owner_vat_number = $wpinv_euvat->get_vat_number() ) { ?>
899
            <tr class="wpi-row-ovatno">
900
                <th><?php echo apply_filters( 'wpinv_invoice_owner_vat_number_label', wp_sprintf( __( 'Owner %s Number', 'invoicing' ), $vat_name ), $invoice, $vat_name ); ?></th>
901
                <td><?php echo $owner_vat_number; ?></td>
902
            </tr>
903
        <?php } ?>
904 View Code Duplication
        <?php if ( $use_taxes && $user_vat_number = wpinv_get_invoice_vat_number( $invoice_id ) ) { ?>
905
            <tr class="wpi-row-uvatno">
906
                <th><?php echo apply_filters( 'wpinv_invoice_user_vat_number_label', wp_sprintf( __( 'Invoice %s Number', 'invoicing' ), $vat_name ), $invoice, $vat_name ); ?></th>
907
                <td><?php echo $user_vat_number; ?></td>
908
            </tr>
909
        <?php } ?>
910
        <tr class="table-active tr-total wpi-row-total">
911
            <th><strong><?php _e( 'Total Amount', 'invoicing' ) ?></strong></th>
912
            <td><strong><?php echo wpinv_payment_total( $invoice_id, true ); ?></strong></td>
913
        </tr>
914
    </table>
915
<?php
916
}
917
918
function wpinv_display_to_address( $invoice_id = 0 ) {
919
    $invoice = wpinv_get_invoice( $invoice_id );
920
    
921
    if ( empty( $invoice ) ) {
922
        return NULL;
923
    }
924
    
925
    $billing_details = $invoice->get_user_info();
926
    $output = '<div class="to col-xs-2"><strong>' . __( 'To:', 'invoicing' ) . '</strong></div>';
927
    $output .= '<div class="wrapper col-xs-10">';
928
    
929
    ob_start();
930
    do_action( 'wpinv_display_to_address_top', $invoice );
931
    $output .= ob_get_clean();
932
    
933
    $output .= '<div class="name">' . esc_html( trim( $billing_details['first_name'] . ' ' . $billing_details['last_name'] ) ) . '</div>';
934
    if ( $company = $billing_details['company'] ) {
935
        $output .= '<div class="company">' . wpautop( wp_kses_post( $company ) ) . '</div>';
936
    }
937
    $address_row = '';
938
    if ( $address = $billing_details['address'] ) {
939
        $address_row .= wpautop( wp_kses_post( $address ) );
940
    }
941
    
942
    $address_fields = array();
943
    if ( !empty( $billing_details['city'] ) ) {
944
        $address_fields[] = $billing_details['city'];
945
    }
946
    
947
    $billing_country = !empty( $billing_details['country'] ) ? $billing_details['country'] : '';
948
    if ( !empty( $billing_details['state'] ) ) {
949
        $address_fields[] = wpinv_state_name( $billing_details['state'], $billing_country );
950
    }
951
    
952
    if ( !empty( $billing_country ) ) {
953
        $address_fields[] = wpinv_country_name( $billing_country );
954
    }
955
    
956 View Code Duplication
    if ( !empty( $address_fields ) ) {
957
        $address_fields = implode( ", ", $address_fields );
958
        
959
        if ( !empty( $billing_details['zip'] ) ) {
960
            $address_fields .= ' ' . $billing_details['zip'];
961
        }
962
        
963
        $address_row .= wpautop( wp_kses_post( $address_fields ) );
964
    }
965
    
966
    if ( $address_row ) {
967
        $output .= '<div class="address">' . $address_row . '</div>';
968
    }
969
    
970 View Code Duplication
    if ( $phone = $invoice->get_phone() ) {
971
        $output .= '<div class="phone">' . wp_sprintf( __( 'Phone: %s', 'invoicing' ), esc_html( $phone ) ) . '</div>';
972
    }
973 View Code Duplication
    if ( $email = $invoice->get_email() ) {
974
        $output .= '<div class="email">' . wp_sprintf( __( 'Email: %s' , 'invoicing'), esc_html( $email ) ) . '</div>';
975
    }
976
    
977
    ob_start();
978
    do_action( 'wpinv_display_to_address_bottom', $invoice );
979
    $output .= ob_get_clean();
980
    
981
    $output .= '</div>';
982
    $output = apply_filters( 'wpinv_display_to_address', $output, $invoice );
983
984
    echo $output;
985
}
986
987
function wpinv_display_line_items( $invoice_id = 0 ) {
988
    global $wpinv_euvat, $ajax_cart_details;
989
    $invoice            = wpinv_get_invoice( $invoice_id );
990
    $quantities_enabled = wpinv_item_quantities_enabled();
991
    $use_taxes          = wpinv_use_taxes();
992
    $zero_tax           = !(float)$invoice->get_tax() > 0 ? true : false;
993
    $tax_label           = $use_taxes && $invoice->has_vat() ? $wpinv_euvat->get_vat_name() : __( 'Tax', 'invoicing' );
994
    $tax_title          = !$zero_tax && $use_taxes ? ( wpinv_prices_include_tax() ? wp_sprintf( __( '(%s Incl.)', 'invoicing' ), $tax_label ) : wp_sprintf( __( '(%s Excl.)', 'invoicing' ), $tax_label ) ) : '';
995
    
996
    $cart_details       = $invoice->get_cart_details();
997
    $ajax_cart_details  = $cart_details;
998
    ob_start();
999
    ?>
1000
    <table class="table table-sm table-bordered table-responsive">
1001
        <thead>
1002
            <tr>
1003
                <th class="name"><strong><?php _e( "Item Name", "invoicing" );?></strong></th>
1004
                <th class="rate"><strong><?php _e( "Price", "invoicing" );?></strong></th>
1005
                <?php if ($quantities_enabled) { ?>
1006
                    <th class="qty"><strong><?php _e( "Qty", "invoicing" );?></strong></th>
1007
                <?php } ?>
1008
                <?php if ($use_taxes && !$zero_tax) { ?>
1009
                    <th class="tax"><strong><?php echo $tax_label . ' <span class="normal small">(%)</span>'; ?></strong></th>
1010
                <?php } ?>
1011
                <th class="total"><strong><?php echo __( "Item Total", "invoicing" ) . ' <span class="normal small">' . $tax_title . '<span>';?></strong></th>
1012
            </tr>
1013
        </thead>
1014
        <tbody>
1015
        <?php 
1016
            if ( !empty( $cart_details ) ) {
1017
                do_action( 'wpinv_display_line_items_start', $invoice );
1018
                
1019
                $count = 0;
1020
                $cols  = 3;
1021
                foreach ( $cart_details as $key => $cart_item ) {
1022
                    $item_id    = !empty($cart_item['id']) ? absint( $cart_item['id'] ) : '';
1023
                    $item_price = isset($cart_item["item_price"]) ? wpinv_round_amount( $cart_item["item_price"] ) : 0;
1024
                    $line_total = isset($cart_item["subtotal"]) ? wpinv_round_amount( $cart_item["subtotal"] ) : 0;
1025
                    $quantity   = !empty($cart_item['quantity']) && (int)$cart_item['quantity'] > 0 ? absint( $cart_item['quantity'] ) : 1;
1026
                    
1027
                    $item       = $item_id ? new WPInv_Item( $item_id ) : NULL;
1028
                    $summary    = '';
1029
                    $cols       = 3;
1030
                    if ( !empty($item) ) {
1031
                        $item_name  = $item->get_name();
1032
                        $summary    = $item->get_summary();
1033
                    }
1034
                    $item_name  = !empty($cart_item['name']) ? $cart_item['name'] : $item_name;
0 ignored issues
show
Bug introduced by
The variable $item_name does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
1035
                    
1036
                    $summary = apply_filters( 'wpinv_print_invoice_line_item_summary', $summary, $cart_item, $item, $invoice );
1037
                    
1038
                    $item_tax       = '';
1039
                    $tax_rate       = '';
1040
                    if ( $use_taxes && $cart_item['tax'] > 0 && $cart_item['subtotal'] > 0 ) {
1041
                        $item_tax = wpinv_price( wpinv_format_amount( $cart_item['tax'] ), $invoice->get_currency() );
1042
                        $tax_rate = !empty( $cart_item['vat_rate'] ) ? $cart_item['vat_rate'] : ( $cart_item['tax'] / $cart_item['subtotal'] ) * 100;
1043
                        $tax_rate = $tax_rate > 0 ? (float)wpinv_round_amount( $tax_rate, 4 ) : '';
1044
                        $tax_rate = $tax_rate != '' ? ' <small class="tax-rate">(' . $tax_rate . '%)</small>' : '';
1045
                    }
1046
                    
1047
                    $line_item_tax = $item_tax . $tax_rate;
1048
                    
1049
                    if ( $line_item_tax === '' ) {
1050
                        $line_item_tax = 0; // Zero tax
1051
                    }
1052
                    
1053
                    $line_item = '<tr class="row-' . ( ($count % 2 == 0) ? 'even' : 'odd' ) . ' wpinv-item">';
1054
                        $line_item .= '<td class="name">' . esc_html__( $item_name, 'invoicing' ) . wpinv_get_item_suffix( $item );
1055
                        if ( $summary !== '' ) {
1056
                            $line_item .= '<br/><small class="meta">' . wpautop( wp_kses_post( $summary ) ) . '</small>';
1057
                        }
1058
                        $line_item .= '</td>';
1059
                        
1060
                        $line_item .= '<td class="rate">' . esc_html__( wpinv_price( wpinv_format_amount( $item_price ), $invoice->get_currency() ) ) . '</td>';
1061
                        if ($quantities_enabled) {
1062
                            $cols++;
1063
                            $line_item .= '<td class="qty">' . $quantity . '</td>';
1064
                        }
1065
                        if ($use_taxes && !$zero_tax) {
1066
                            $cols++;
1067
                            $line_item .= '<td class="tax">' . $line_item_tax . '</td>';
1068
                        }
1069
                        $line_item .= '<td class="total">' . esc_html__( wpinv_price( wpinv_format_amount( $line_total ), $invoice->get_currency() ) ) . '</td>';
1070
                    $line_item .= '</tr>';
1071
                    
1072
                    echo apply_filters( 'wpinv_display_line_item', $line_item, $cart_item, $invoice, $cols );
1073
1074
                    $count++;
1075
                }
1076
                
1077
                do_action( 'wpinv_display_before_subtotal', $invoice, $cols );
1078
                ?>
1079
                <tr class="row-sub-total row_odd">
1080
                    <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php echo apply_filters( 'wpinv_print_cart_subtotal_label', '<strong>' . __( 'Sub Total', 'invoicing' ) . ':</strong>', $invoice ); ?></td>
1081
                    <td class="total"><strong><?php _e( wpinv_subtotal( $invoice_id, true ) ) ?></strong></td>
1082
                </tr>
1083
                <?php
1084
                do_action( 'wpinv_display_after_subtotal', $invoice, $cols );
1085
                
1086 View Code Duplication
                if ( wpinv_discount( $invoice_id, false ) > 0 ) {
1087
                    do_action( 'wpinv_display_before_discount', $invoice, $cols );
1088
                    ?>
1089
                        <tr class="row-discount">
1090
                            <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php wpinv_get_discount_label( wpinv_discount_code( $invoice_id ) ); ?>:</td>
1091
                            <td class="total"><?php echo wpinv_discount( $invoice_id, true, true ); ?></td>
1092
                        </tr>
1093
                    <?php
1094
                    do_action( 'wpinv_display_after_discount', $invoice, $cols );
1095
                }
1096
                
1097
                if ( $use_taxes ) {
1098
                    do_action( 'wpinv_display_before_tax', $invoice, $cols );
1099
                    ?>
1100
                    <tr class="row-tax">
1101
                        <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php echo apply_filters( 'wpinv_print_cart_tax_label', '<strong>' . $tax_label . ':</strong>', $invoice ); ?></td>
1102
                        <td class="total"><?php _e( wpinv_tax( $invoice_id, true ) ) ?></td>
1103
                    </tr>
1104
                    <?php
1105
                    do_action( 'wpinv_display_after_tax', $invoice, $cols );
1106
                }
1107
                
1108
                do_action( 'wpinv_display_before_total', $invoice, $cols );
1109
                ?>
1110
                <tr class="table-active row-total">
1111
                    <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php echo apply_filters( 'wpinv_print_cart_total_label', '<strong>' . __( 'Total', 'invoicing' ) . ':</strong>', $invoice ); ?></td>
1112
                    <td class="total"><strong><?php _e( wpinv_payment_total( $invoice_id, true ) ) ?></strong></td>
1113
                </tr>
1114
                <?php
1115
                do_action( 'wpinv_display_after_total', $invoice, $cols );
1116
                
1117
                do_action( 'wpinv_display_line_end', $invoice, $cols );
1118
            }
1119
        ?>
1120
        </tbody>
1121
    </table>
1122
    <?php
1123
    echo ob_get_clean();
1124
}
1125
1126
function wpinv_display_invoice_totals( $invoice_id = 0 ) {    
1127
    $use_taxes = wpinv_use_taxes();
1128
    
1129
    do_action( 'wpinv_before_display_totals_table', $invoice_id ); 
1130
    ?>
1131
    <table class="table table-sm table-bordered table-responsive">
1132
        <tbody>
1133
            <?php do_action( 'wpinv_before_display_totals' ); ?>
1134
            <tr class="row-sub-total">
1135
                <td class="rate"><strong><?php _e( 'Sub Total', 'invoicing' ); ?></strong></td>
1136
                <td class="total"><strong><?php _e( wpinv_subtotal( $invoice_id, true ) ) ?></strong></td>
1137
            </tr>
1138
            <?php do_action( 'wpinv_after_display_totals' ); ?>
1139
            <?php if ( wpinv_discount( $invoice_id, false ) > 0 ) { ?>
1140
                <tr class="row-discount">
1141
                    <td class="rate"><?php wpinv_get_discount_label( wpinv_discount_code( $invoice_id ) ); ?></td>
1142
                    <td class="total"><?php echo wpinv_discount( $invoice_id, true, true ); ?></td>
1143
                </tr>
1144
            <?php do_action( 'wpinv_after_display_discount' ); ?>
1145
            <?php } ?>
1146
            <?php if ( $use_taxes ) { ?>
1147
            <tr class="row-tax">
1148
                <td class="rate"><?php _e( 'Tax', 'invoicing' ); ?></td>
1149
                <td class="total"><?php _e( wpinv_tax( $invoice_id, true ) ) ?></td>
1150
            </tr>
1151
            <?php do_action( 'wpinv_after_display_tax' ); ?>
1152
            <?php } ?>
1153
            <?php if ( $fees = wpinv_get_fees( $invoice_id ) ) { ?>
1154
                <?php foreach ( $fees as $fee ) { ?>
1155
                    <tr class="row-fee">
1156
                        <td class="rate"><?php echo $fee['label']; ?></td>
1157
                        <td class="total"><?php echo $fee['amount_display']; ?></td>
1158
                    </tr>
1159
                <?php } ?>
1160
            <?php } ?>
1161
            <tr class="table-active row-total">
1162
                <td class="rate"><strong><?php _e( 'Total', 'invoicing' ) ?></strong></td>
1163
                <td class="total"><strong><?php _e( wpinv_payment_total( $invoice_id, true ) ) ?></strong></td>
1164
            </tr>
1165
            <?php do_action( 'wpinv_after_totals' ); ?>
1166
        </tbody>
1167
1168
    </table>
1169
1170
    <?php do_action( 'wpinv_after_totals_table' );
1171
}
1172
1173
function wpinv_display_payments_info( $invoice_id = 0, $echo = true ) {
1174
    $invoice = wpinv_get_invoice( $invoice_id );
1175
    
1176
    ob_start();
1177
    do_action( 'wpinv_before_display_payments_info', $invoice_id );
1178
    if ( ( $gateway_title = $invoice->get_gateway_title() ) || $invoice->is_paid() || $invoice->is_refunded() ) {
1179
        ?>
1180
        <div class="wpi-payment-info">
1181
            <p class="wpi-payment-gateway"><?php echo wp_sprintf( __( 'Payment via %s', 'invoicing' ), $gateway_title ? $gateway_title : __( 'Manually', 'invoicing' ) ); ?></p>
1182
            <?php if ( $gateway_title ) { ?>
1183
            <p class="wpi-payment-transid"><?php echo wp_sprintf( __( 'Transaction ID: %s', 'invoicing' ), $invoice->get_transaction_id() ); ?></p>
1184
            <?php } ?>
1185
        </div>
1186
        <?php
1187
    }
1188
    do_action( 'wpinv_after_display_payments_info', $invoice_id );
1189
    $outout = ob_get_clean();
1190
    
1191
    if ( $echo ) {
1192
        echo $outout;
1193
    } else {
1194
        return $outout;
1195
    }
1196
}
1197
1198
function wpinv_display_style( $invoice ) {
0 ignored issues
show
Unused Code introduced by
The parameter $invoice is not used and could be removed.

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

Loading history...
1199
    wp_register_style( 'wpinv-single-style', WPINV_PLUGIN_URL . 'assets/css/invoice.css', array(), WPINV_VERSION );
1200
    
1201
    wp_print_styles( 'open-sans' );
1202
    wp_print_styles( 'wpinv-single-style' );
1203
}
1204
add_action( 'wpinv_invoice_print_head', 'wpinv_display_style' );
1205
add_action( 'wpinv_invalid_invoice_head', 'wpinv_display_style' );
1206
1207
function wpinv_checkout_billing_details() {  
1208
    $invoice_id = (int)wpinv_get_invoice_cart_id();
1209
    if (empty($invoice_id)) {
1210
        wpinv_error_log( 'Invoice id not found', 'ERROR', __FILE__, __LINE__ );
1211
        return null;
1212
    }
1213
    
1214
    $invoice = wpinv_get_invoice_cart( $invoice_id );
1215
    if (empty($invoice)) {
1216
        wpinv_error_log( 'Invoice not found', 'ERROR', __FILE__, __LINE__ );
1217
        return null;
1218
    }
1219
    $user_id        = $invoice->get_user_id();
1220
    $user_info      = $invoice->get_user_info();
1221
    $address_info   = wpinv_get_user_address( $user_id );
1222
    
1223 View Code Duplication
    if ( empty( $user_info['first_name'] ) && !empty( $user_info['first_name'] ) ) {
1224
        $user_info['first_name'] = $user_info['first_name'];
1225
        $user_info['last_name'] = $user_info['last_name'];
1226
    }
1227
    
1228
    if ( ( ( empty( $user_info['country'] ) && !empty( $address_info['country'] ) ) || ( empty( $user_info['state'] ) && !empty( $address_info['state'] ) && $user_info['country'] == $address_info['country'] ) ) ) {
1229
        $user_info['country']   = $address_info['country'];
1230
        $user_info['state']     = $address_info['state'];
1231
        $user_info['city']      = $address_info['city'];
1232
        $user_info['zip']       = $address_info['zip'];
1233
    }
1234
    
1235
    $address_fields = array(
1236
        'user_id',
1237
        'company',
1238
        'vat_number',
1239
        'email',
1240
        'phone',
1241
        'address'
1242
    );
1243
    
1244
    foreach ( $address_fields as $field ) {
1245
        if ( empty( $user_info[$field] ) ) {
1246
            $user_info[$field] = $address_info[$field];
1247
        }
1248
    }
1249
    
1250
    return apply_filters( 'wpinv_checkout_billing_details', $user_info, $invoice );
1251
}
1252
1253
function wpinv_admin_get_line_items($invoice = array()) {
1254
    $item_quantities    = wpinv_item_quantities_enabled();
1255
    $use_taxes          = wpinv_use_taxes();
1256
    
1257
    if ( empty( $invoice ) ) {
1258
        return NULL;
1259
    }
1260
    
1261
    $cart_items = $invoice->get_cart_details();
0 ignored issues
show
Bug introduced by
The method get_cart_details cannot be called on $invoice (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1262
    if ( empty( $cart_items ) ) {
1263
        return NULL;
1264
    }
1265
    ob_start();
1266
    
1267
    do_action( 'wpinv_admin_before_line_items', $cart_items, $invoice );
1268
    
1269
    $count = 0;
1270
    foreach ( $cart_items as $key => $cart_item ) {
1271
        $item_id    = $cart_item['id'];
1272
        $wpi_item   = $item_id > 0 ? new WPInv_Item( $item_id ) : NULL;
1273
        
1274
        if (empty($wpi_item)) {
1275
            continue;
1276
        }
1277
        
1278
        $item_price     = wpinv_price( wpinv_format_amount( $cart_item['item_price'] ), $invoice->get_currency() );
0 ignored issues
show
Bug introduced by
The method get_currency cannot be called on $invoice (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1279
        $quantity       = !empty( $cart_item['quantity'] ) && $cart_item['quantity'] > 0 ? $cart_item['quantity'] : 1;
1280
        $item_subtotal  = wpinv_price( wpinv_format_amount( $cart_item['subtotal'] ), $invoice->get_currency() );
0 ignored issues
show
Bug introduced by
The method get_currency cannot be called on $invoice (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1281
        $can_remove     = true;
1282
        
1283
        $summary = apply_filters( 'wpinv_admin_invoice_line_item_summary', '', $cart_item, $wpi_item, $invoice );
1284
        
1285
        $item_tax       = '';
1286
        $tax_rate       = '';
1287 View Code Duplication
        if ( $cart_item['tax'] > 0 && $cart_item['subtotal'] > 0 ) {
1288
            $item_tax = wpinv_price( wpinv_format_amount( $cart_item['tax'] ), $invoice->get_currency() );
0 ignored issues
show
Bug introduced by
The method get_currency cannot be called on $invoice (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1289
            $tax_rate = !empty( $cart_item['vat_rate'] ) ? $cart_item['vat_rate'] : ( $cart_item['tax'] / $cart_item['subtotal'] ) * 100;
1290
            $tax_rate = $tax_rate > 0 ? (float)wpinv_round_amount( $tax_rate, 4 ) : '';
1291
            $tax_rate = $tax_rate != '' ? ' <span class="tax-rate">(' . $tax_rate . '%)</span>' : '';
1292
        }
1293
        $line_item_tax = $item_tax . $tax_rate;
1294
        
1295
        if ( $line_item_tax === '' ) {
1296
            $line_item_tax = 0; // Zero tax
1297
        }
1298
1299
        $line_item = '<tr class="item item-' . ( ($count % 2 == 0) ? 'even' : 'odd' ) . '" data-item-id="' . $item_id . '">';
1300
            $line_item .= '<td class="id">' . $item_id . '</td>';
1301
            $line_item .= '<td class="title"><a href="' . get_edit_post_link( $item_id ) . '" target="_blank">' . $cart_item['name'] . '</a>' . wpinv_get_item_suffix( $wpi_item );
1302
            if ( $summary !== '' ) {
1303
                $line_item .= '<span class="meta">' . wpautop( wp_kses_post( $summary ) ) . '</span>';
1304
            }
1305
            $line_item .= '</td>';
1306
            $line_item .= '<td class="price">' . $item_price . '</td>';
1307
            
1308
            if ( $item_quantities ) {
1309
                if ( count( $cart_items ) == 1 && $quantity <= 1 ) {
1310
                    $can_remove = false;
1311
                }
1312
                $line_item .= '<td class="qty" data-quantity="' . $quantity . '">&nbsp;&times;&nbsp;' . $quantity . '</td>';
1313
            } else {
1314
                if ( count( $cart_items ) == 1 ) {
1315
                    $can_remove = false;
1316
                }
1317
            }
1318
            $line_item .= '<td class="total">' . $item_subtotal . '</td>';
1319
            
1320
            if ( $use_taxes ) {
1321
                $line_item .= '<td class="tax">' . $line_item_tax . '</td>';
1322
            }
1323
            $line_item .= '<td class="action">';
1324
            if ( !$invoice->is_paid() && !$invoice->is_refunded() && $can_remove ) {
0 ignored issues
show
Bug introduced by
The method is_paid cannot be called on $invoice (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
Bug introduced by
The method is_refunded cannot be called on $invoice (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1325
                $line_item .= '<i class="fa fa-remove wpinv-item-remove"></i>';
1326
            }
1327
            $line_item .= '</td>';
1328
        $line_item .= '</tr>';
1329
        
1330
        echo apply_filters( 'wpinv_admin_line_item', $line_item, $cart_item, $invoice );
1331
        
1332
        $count++;
1333
    } 
1334
    
1335
    do_action( 'wpinv_admin_after_line_items', $cart_items, $invoice );
1336
    
1337
    return ob_get_clean();
1338
}
1339
1340
function wpinv_checkout_form() {
1341
    global $wpi_checkout_id;
1342
    
1343
    // Set current invoice id.
1344
    $wpi_checkout_id = wpinv_get_invoice_cart_id();
1345
    
1346
    $form_action  = esc_url( wpinv_get_checkout_uri() );
1347
1348
    ob_start();
1349
        echo '<div id="wpinv_checkout_wrap">';
1350
        
1351
        if ( wpinv_get_cart_contents() || wpinv_cart_has_fees() ) {
1352
            ?>
1353
            <div id="wpinv_checkout_form_wrap" class="wpinv_clearfix table-responsive">
1354
                <?php do_action( 'wpinv_before_checkout_form' ); ?>
1355
                <form id="wpinv_checkout_form" class="wpi-form" action="<?php echo $form_action; ?>" method="POST">
1356
                    <?php
1357
                    do_action( 'wpinv_checkout_form_top' );
1358
                    do_action( 'wpinv_checkout_billing_info' );
1359
                    do_action( 'wpinv_checkout_cart' );
1360
                    do_action( 'wpinv_payment_mode_select'  );
1361
                    do_action( 'wpinv_checkout_form_bottom' )
1362
                    ?>
1363
                </form>
1364
                <?php do_action( 'wpinv_after_purchase_form' ); ?>
1365
            </div><!--end #wpinv_checkout_form_wrap-->
1366
        <?php
1367
        } else {
1368
            do_action( 'wpinv_cart_empty' );
1369
        }
1370
        echo '</div><!--end #wpinv_checkout_wrap-->';
1371
    return ob_get_clean();
1372
}
1373
1374
function wpinv_checkout_cart( $cart_details = array(), $echo = true ) {
1375
    global $ajax_cart_details;
1376
    $ajax_cart_details = $cart_details;
1377
    /*
1378
    // Check if the Update cart button should be shown
1379
    if( wpinv_item_quantities_enabled() ) {
1380
        add_action( 'wpinv_cart_footer_buttons', 'wpinv_update_cart_button' );
1381
    }
1382
    
1383
    // Check if the Save Cart button should be shown
1384
    if( !wpinv_is_cart_saving_disabled() ) {
1385
        add_action( 'wpinv_cart_footer_buttons', 'wpinv_save_cart_button' );
1386
    }
1387
    */
1388
    ob_start();
1389
    do_action( 'wpinv_before_checkout_cart' );
1390
    echo '<div id="wpinv_checkout_cart_form" method="post">';
1391
        echo '<div id="wpinv_checkout_cart_wrap">';
1392
            wpinv_get_template_part( 'wpinv-checkout-cart' );
1393
        echo '</div>';
1394
    echo '</div>';
1395
    do_action( 'wpinv_after_checkout_cart' );
1396
    $content = ob_get_clean();
1397
    
1398
    if ( $echo ) {
1399
        echo $content;
1400
    } else {
1401
        return $content;
1402
    }
1403
}
1404
add_action( 'wpinv_checkout_cart', 'wpinv_checkout_cart', 10 );
1405
1406
function wpinv_empty_cart_message() {
1407
	return apply_filters( 'wpinv_empty_cart_message', '<span class="wpinv_empty_cart">' . __( 'Your cart is empty.', 'invoicing' ) . '</span>' );
1408
}
1409
1410
/**
1411
 * Echoes the Empty Cart Message
1412
 *
1413
 * @since 1.0
1414
 * @return void
1415
 */
1416
function wpinv_empty_checkout_cart() {
1417
	echo wpinv_empty_cart_message();
1418
}
1419
add_action( 'wpinv_cart_empty', 'wpinv_empty_checkout_cart' );
1420
1421
function wpinv_save_cart_button() {
1422
    if ( wpinv_is_cart_saving_disabled() )
1423
        return;
1424
?>
1425
    <a class="wpinv-cart-saving-button wpinv-submit button" id="wpinv-save-cart-button" href="<?php echo esc_url( add_query_arg( 'wpi_action', 'save_cart' ) ); ?>"><?php _e( 'Save Cart', 'invoicing' ); ?></a>
1426
<?php
1427
}
1428
1429
function wpinv_update_cart_button() {
1430
    if ( !wpinv_item_quantities_enabled() )
1431
        return;
1432
?>
1433
    <input type="submit" name="wpinv_update_cart_submit" class="wpinv-submit wpinv-no-js button" value="<?php _e( 'Update Cart', 'invoicing' ); ?>"/>
1434
    <input type="hidden" name="wpi_action" value="update_cart"/>
1435
<?php
1436
}
1437
1438
function wpinv_checkout_cart_columns() {
1439
    $default = 3;
1440
    if ( wpinv_item_quantities_enabled() ) {
1441
        $default++;
1442
    }
1443
    
1444
    if ( wpinv_use_taxes() ) {
1445
        $default++;
1446
    }
1447
1448
    return apply_filters( 'wpinv_checkout_cart_columns', $default );
1449
}
1450
1451
function wpinv_display_cart_messages() {
1452
    global $wpi_session;
1453
1454
    $messages = $wpi_session->get( 'wpinv_cart_messages' );
1455
1456
    if ( $messages ) {
1457
        foreach ( $messages as $message_id => $message ) {
1458
            // Try and detect what type of message this is
1459
            if ( strpos( strtolower( $message ), 'error' ) ) {
1460
                $type = 'error';
1461
            } elseif ( strpos( strtolower( $message ), 'success' ) ) {
1462
                $type = 'success';
1463
            } else {
1464
                $type = 'info';
1465
            }
1466
1467
            $classes = apply_filters( 'wpinv_' . $type . '_class', array( 'wpinv_errors', 'wpinv-alert', 'wpinv-alert-' . $type ) );
1468
1469
            echo '<div class="' . implode( ' ', $classes ) . '">';
1470
                // Loop message codes and display messages
1471
                    echo '<p class="wpinv_error" id="wpinv_msg_' . $message_id . '">' . $message . '</p>';
1472
            echo '</div>';
1473
        }
1474
1475
        // Remove all of the cart saving messages
1476
        $wpi_session->set( 'wpinv_cart_messages', null );
1477
    }
1478
}
1479
add_action( 'wpinv_before_checkout_cart', 'wpinv_display_cart_messages' );
1480
1481
function wpinv_discount_field() {
1482
    if ( isset( $_GET['wpi-gateway'] ) && wpinv_is_ajax_disabled() ) {
1483
        return; // Only show before a payment method has been selected if ajax is disabled
1484
    }
1485
1486
    if ( !wpinv_is_checkout() ) {
1487
        return;
1488
    }
1489
1490
    if ( wpinv_has_active_discounts() && wpinv_get_cart_total() ) {
1491
    ?>
1492
    <div id="wpinv-discount-field" class="panel panel-default">
1493
        <div class="panel-body">
1494
            <p>
1495
                <label class="wpinv-label" for="wpinv_discount_code"><strong><?php _e( 'Discount', 'invoicing' ); ?></strong></label>
1496
                <span class="wpinv-description"><?php _e( 'Enter a discount code if you have one.', 'invoicing' ); ?></span>
1497
            </p>
1498
            <div class="form-group row">
1499
                <div class="col-sm-4">
1500
                    <input class="wpinv-input form-control" type="text" id="wpinv_discount_code" name="wpinv_discount_code" placeholder="<?php _e( 'Enter discount code', 'invoicing' ); ?>"/>
1501
                </div>
1502
                <div class="col-sm-3">
1503
                    <button id="wpi-apply-discount" type="button" class="btn btn-success btn-sm"><?php _e( 'Apply Discount', 'invoicing' ); ?></button>
1504
                </div>
1505
                <div style="clear:both"></div>
1506
                <div class="col-sm-12 wpinv-discount-msg">
1507
                    <div class="alert alert-success"><i class="fa fa-check-circle"></i><span class="wpi-msg"></span></div>
1508
                    <div class="alert alert-error"><i class="fa fa-warning"></i><span class="wpi-msg"></span></div>
1509
                </div>
1510
            </div>
1511
        </div>
1512
    </div>
1513
<?php
1514
    }
1515
}
1516
add_action( 'wpinv_after_checkout_cart', 'wpinv_discount_field', -10 );
1517
1518
function wpinv_agree_to_terms_js() {
1519
    if ( wpinv_get_option( 'show_agree_to_terms', false ) ) {
1520
?>
1521
<script type="text/javascript">
1522
    jQuery(document).ready(function($){
1523
        $( document.body ).on('click', '.wpinv_terms_links', function(e) {
1524
            //e.preventDefault();
1525
            $('#wpinv_terms').slideToggle();
1526
            $('.wpinv_terms_links').toggle();
1527
            return false;
1528
        });
1529
    });
1530
</script>
1531
<?php
1532
    }
1533
}
1534
add_action( 'wpinv_checkout_form_top', 'wpinv_agree_to_terms_js' );
1535
1536
function wpinv_payment_mode_select() {
1537
    $gateways = wpinv_get_enabled_payment_gateways( true );
1538
    $gateways = apply_filters( 'wpinv_payment_gateways_on_cart', $gateways );
1539
    $page_URL = wpinv_get_current_page_url();
0 ignored issues
show
Unused Code introduced by
$page_URL is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1540
    $invoice = wpinv_get_invoice( 0, true );
1541
    
1542
    do_action('wpinv_payment_mode_top');
1543
    $invoice_id = (int)$invoice->ID;
1544
    $chosen_gateway = wpinv_get_chosen_gateway( $invoice_id );
1545
    ?>
1546
    <div id="wpinv_payment_mode_select" data-gateway="<?php echo $chosen_gateway; ?>" <?php echo ( $invoice->is_free() ? 'style="display:none;"' : '' ); ?>>
1547
            <?php do_action( 'wpinv_payment_mode_before_gateways_wrap' ); ?>
1548
            <div id="wpinv-payment-mode-wrap" class="panel panel-default">
1549
                <div class="panel-heading"><h3 class="panel-title"><?php _e( 'Select Payment Method', 'invoicing' ); ?></h3></div>
1550
                <div class="panel-body list-group wpi-payment_methods">
1551
                    <?php
1552
                    do_action( 'wpinv_payment_mode_before_gateways' );
1553
                    
1554
                    if(!empty($gateways)){
1555
	                    foreach ( $gateways as $gateway_id => $gateway ) {
1556
		                    $checked = checked( $gateway_id, $chosen_gateway, false );
1557
		                    $button_label = wpinv_get_gateway_button_label( $gateway_id );
1558
		                    $description = wpinv_get_gateway_description( $gateway_id );
1559
		                    ?>
1560
		                    <div class="list-group-item">
1561
			                    <div class="radio">
1562
				                    <label><input type="radio" data-button-text="<?php echo esc_attr( $button_label );?>" value="<?php echo esc_attr( $gateway_id ) ;?>" <?php echo $checked ;?> id="wpi_gateway_<?php echo esc_attr( $gateway_id );?>" name="wpi-gateway" class="wpi-pmethod"><?php echo esc_html( $gateway['checkout_label'] ); ?></label>
1563
			                    </div>
1564
			                    <div style="display:none;" class="payment_box wpi_gateway_<?php echo esc_attr( $gateway_id );?>" role="alert">
1565
				                    <?php if ( !empty( $description ) ) { ?>
1566
					                    <div class="wpi-gateway-desc alert alert-info"><?php echo $description;?></div>
1567
				                    <?php } ?>
1568
				                    <?php do_action( 'wpinv_' . $gateway_id . '_cc_form', $invoice_id ) ;?>
1569
			                    </div>
1570
		                    </div>
1571
		                    <?php
1572
	                    }
1573
                    }else{
1574
	                    echo '<div class="alert alert-warning">'. __('No payment gateway active','invoicing') .'</div>';
1575
                    }
1576
1577
                    do_action( 'wpinv_payment_mode_after_gateways' );
1578
                    ?>
1579
                </div>
1580
            </div>
1581
            <?php do_action( 'wpinv_payment_mode_after_gateways_wrap' ); ?>
1582
    </div>
1583
    <?php
1584
    do_action('wpinv_payment_mode_bottom');
1585
}
1586
add_action( 'wpinv_payment_mode_select', 'wpinv_payment_mode_select' );
1587
1588
function wpinv_checkout_billing_info() {    
1589
    if ( wpinv_is_checkout() ) {
1590
        $logged_in          = is_user_logged_in();
0 ignored issues
show
Unused Code introduced by
$logged_in is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1591
        $billing_details    = wpinv_checkout_billing_details();
1592
        $selected_country   = !empty( $billing_details['country'] ) ? $billing_details['country'] : wpinv_default_billing_country();
1593
        ?>
1594
        <div id="wpinv-fields" class="clearfix">
1595
            <div id="wpi-billing" class="wpi-billing clearfix panel panel-default">
1596
                <div class="panel-heading"><h3 class="panel-title"><?php _e( 'Billing Details', 'invoicing' );?></h3></div>
1597
                <div id="wpinv-fields-box" class="panel-body">
1598
                    <?php do_action( 'wpinv_checkout_billing_fields_first', $billing_details ); ?>
1599
                    <p class="wpi-cart-field wpi-col2 wpi-colf">
1600
                        <label for="wpinv_first_name" class="wpi-label"><?php _e( 'First Name', 'invoicing' );?><?php if ( wpinv_get_option( 'fname_mandatory' ) ) { echo '<span class="wpi-required">*</span>'; } ?></label>
1601
                        <?php
1602
                        echo wpinv_html_text( array(
1603
                                'id'            => 'wpinv_first_name',
1604
                                'name'          => 'wpinv_first_name',
1605
                                'value'         => $billing_details['first_name'],
1606
                                'class'         => 'wpi-input form-control',
1607
                                'placeholder'   => __( 'First name', 'invoicing' ),
1608
                                'required'      => (bool)wpinv_get_option( 'fname_mandatory' ),
1609
                            ) );
1610
                        ?>
1611
                    </p>
1612
                    <p class="wpi-cart-field wpi-col2 wpi-coll">
1613
                        <label for="wpinv_last_name" class="wpi-label"><?php _e( 'Last Name', 'invoicing' );?><?php if ( wpinv_get_option( 'lname_mandatory' ) ) { echo '<span class="wpi-required">*</span>'; } ?></label>
1614
                        <?php
1615
                        echo wpinv_html_text( array(
1616
                                'id'            => 'wpinv_last_name',
1617
                                'name'          => 'wpinv_last_name',
1618
                                'value'         => $billing_details['last_name'],
1619
                                'class'         => 'wpi-input form-control',
1620
                                'placeholder'   => __( 'Last name', 'invoicing' ),
1621
                                'required'      => (bool)wpinv_get_option( 'lname_mandatory' ),
1622
                            ) );
1623
                        ?>
1624
                    </p>
1625
                    <p class="wpi-cart-field wpi-col2 wpi-colf">
1626
                        <label for="wpinv_address" class="wpi-label"><?php _e( 'Address', 'invoicing' );?><?php if ( wpinv_get_option( 'address_mandatory' ) ) { echo '<span class="wpi-required">*</span>'; } ?></label>
1627
                        <?php
1628
                        echo wpinv_html_text( array(
1629
                                'id'            => 'wpinv_address',
1630
                                'name'          => 'wpinv_address',
1631
                                'value'         => $billing_details['address'],
1632
                                'class'         => 'wpi-input form-control',
1633
                                'placeholder'   => __( 'Address', 'invoicing' ),
1634
                                'required'      => (bool)wpinv_get_option( 'address_mandatory' ),
1635
                            ) );
1636
                        ?>
1637
                    </p>
1638
                    <p class="wpi-cart-field wpi-col2 wpi-coll">
1639
                        <label for="wpinv_city" class="wpi-label"><?php _e( 'City', 'invoicing' );?><?php if ( wpinv_get_option( 'city_mandatory' ) ) { echo '<span class="wpi-required">*</span>'; } ?></label>
1640
                        <?php
1641
                        echo wpinv_html_text( array(
1642
                                'id'            => 'wpinv_city',
1643
                                'name'          => 'wpinv_city',
1644
                                'value'         => $billing_details['city'],
1645
                                'class'         => 'wpi-input form-control',
1646
                                'placeholder'   => __( 'City', 'invoicing' ),
1647
                                'required'      => (bool)wpinv_get_option( 'city_mandatory' ),
1648
                            ) );
1649
                        ?>
1650
                    </p>
1651
                    <p id="wpinv_country_box" class="wpi-cart-field wpi-col2 wpi-colf">
1652
                        <label for="wpinv_country" class="wpi-label"><?php _e( 'Country', 'invoicing' );?><?php if ( wpinv_get_option( 'country_mandatory' ) ) { echo '<span class="wpi-required">*</span>'; } ?></label>
1653
                        <?php echo wpinv_html_select( array(
1654
                            'options'          => wpinv_get_country_list(),
1655
                            'name'             => 'wpinv_country',
1656
                            'id'               => 'wpinv_country',
1657
                            'selected'         => $selected_country,
1658
                            'show_option_all'  => false,
1659
                            'show_option_none' => false,
1660
                            'class'            => 'wpi-input form-control',
1661
                            'placeholder'      => __( 'Choose a country', 'invoicing' ),
1662
                            'required'         => (bool)wpinv_get_option( 'country_mandatory' ),
1663
                        ) ); ?>
1664
                    </p>
1665
                    <p id="wpinv_state_box" class="wpi-cart-field wpi-col2 wpi-coll">
1666
                        <label for="wpinv_state" class="wpi-label"><?php _e( 'State / Province', 'invoicing' );?><?php if ( wpinv_get_option( 'state_mandatory' ) ) { echo '<span class="wpi-required">*</span>'; } ?></label>
1667
                        <?php
1668
                        $states = wpinv_get_country_states( $selected_country );
1669
                        if( !empty( $states ) ) {
1670
                            echo wpinv_html_select( array(
1671
                                'options'          => $states,
1672
                                'name'             => 'wpinv_state',
1673
                                'id'               => 'wpinv_state',
1674
                                'selected'         => $billing_details['state'],
1675
                                'show_option_all'  => false,
1676
                                'show_option_none' => false,
1677
                                'class'            => 'wpi-input form-control',
1678
                                'placeholder'      => __( 'Choose a state', 'invoicing' ),
1679
                                'required'         => (bool)wpinv_get_option( 'state_mandatory' ),
1680
                            ) );
1681
                        } else {
1682
                            echo wpinv_html_text( array(
1683
                                'name'          => 'wpinv_state',
1684
                                'value'         => $billing_details['state'],
1685
                                'id'            => 'wpinv_state',
1686
                                'class'         => 'wpi-input form-control',
1687
                                'placeholder'   => __( 'State / Province', 'invoicing' ),
1688
                                'required'      => (bool)wpinv_get_option( 'state_mandatory' ),
1689
                            ) );
1690
                        }
1691
                        ?>
1692
                    </p>
1693
                    <p class="wpi-cart-field wpi-col2 wpi-colf">
1694
                        <label for="wpinv_zip" class="wpi-label"><?php _e( 'ZIP / Postcode', 'invoicing' );?><?php if ( wpinv_get_option( 'zip_mandatory' ) ) { echo '<span class="wpi-required">*</span>'; } ?></label>
1695
                        <?php
1696
                        echo wpinv_html_text( array(
1697
                                'name'          => 'wpinv_zip',
1698
                                'value'         => $billing_details['zip'],
1699
                                'id'            => 'wpinv_zip',
1700
                                'class'         => 'wpi-input form-control',
1701
                                'placeholder'   => __( 'ZIP / Postcode', 'invoicing' ),
1702
                                'required'      => (bool)wpinv_get_option( 'zip_mandatory' ),
1703
                            ) );
1704
                        ?>
1705
                    </p>
1706
                    <p class="wpi-cart-field wpi-col2 wpi-coll">
1707
                        <label for="wpinv_phone" class="wpi-label"><?php _e( 'Phone', 'invoicing' );?><?php if ( wpinv_get_option( 'phone_mandatory' ) ) { echo '<span class="wpi-required">*</span>'; } ?></label>
1708
                        <?php
1709
                        echo wpinv_html_text( array(
1710
                                'id'            => 'wpinv_phone',
1711
                                'name'          => 'wpinv_phone',
1712
                                'value'         => $billing_details['phone'],
1713
                                'class'         => 'wpi-input form-control',
1714
                                'placeholder'   => __( 'Phone', 'invoicing' ),
1715
                                'required'      => (bool)wpinv_get_option( 'phone_mandatory' ),
1716
                            ) );
1717
                        ?>
1718
                    </p>
1719
                    <?php do_action( 'wpinv_checkout_billing_fields_last', $billing_details ); ?>
1720
                    <div class="clearfix"></div>
1721
                </div>
1722
            </div>
1723
            <?php do_action( 'wpinv_after_billing_fields', $billing_details ); ?>
1724
        </div>
1725
        <?php
1726
    }
1727
}
1728
add_action( 'wpinv_checkout_billing_info', 'wpinv_checkout_billing_info' );
1729
1730
function wpinv_checkout_hidden_fields() {
1731
?>
1732
    <?php if ( is_user_logged_in() ) { ?>
1733
    <input type="hidden" name="wpinv_user_id" value="<?php echo get_current_user_id(); ?>"/>
1734
    <?php } ?>
1735
    <input type="hidden" name="wpi_action" value="payment" />
1736
<?php
1737
}
1738
1739
function wpinv_checkout_button_purchase() {
1740
    ob_start();
1741
?>
1742
    <input type="submit" class="btn btn-success wpinv-submit" id="wpinv-payment-button" data-value="<?php esc_attr_e( 'Proceed to Pay', 'invoicing' ) ?>" name="wpinv_payment" value="<?php esc_attr_e( 'Proceed to Pay', 'invoicing' ) ?>"/>
1743
<?php
1744
    return apply_filters( 'wpinv_checkout_button_purchase', ob_get_clean() );
1745
}
1746
1747
function wpinv_checkout_total() {
1748
    global $cart_total;
1749
?>
1750
<div id="wpinv_checkout_total" class="panel panel-info">
1751
    <div class="panel-body">
1752
    <?php
1753
    do_action( 'wpinv_purchase_form_before_checkout_total' );
1754
    ?>
1755
    <strong><?php _e( 'Invoice Total:', 'invoicing' ) ?></strong> <span class="wpinv-chdeckout-total"><?php echo $cart_total;?></span>
1756
    <?php
1757
    do_action( 'wpinv_purchase_form_after_checkout_total' );
1758
    ?>
1759
    </div>
1760
</div>
1761
<?php
1762
}
1763
add_action( 'wpinv_checkout_form_bottom', 'wpinv_checkout_total', 9998 );
1764
1765
function wpinv_checkout_submit() {
1766
?>
1767
<div id="wpinv_purchase_submit" class="panel panel-success">
1768
    <div class="panel-body text-center">
1769
    <?php
1770
    do_action( 'wpinv_purchase_form_before_submit' );
1771
    wpinv_checkout_hidden_fields();
1772
    echo wpinv_checkout_button_purchase();
1773
    do_action( 'wpinv_purchase_form_after_submit' );
1774
    ?>
1775
    </div>
1776
</div>
1777
<?php
1778
}
1779
add_action( 'wpinv_checkout_form_bottom', 'wpinv_checkout_submit', 9999 );
1780
1781
function wpinv_receipt_billing_address( $invoice_id = 0 ) {
1782
    $invoice = wpinv_get_invoice( $invoice_id );
1783
    
1784
    if ( empty( $invoice ) ) {
1785
        return NULL;
1786
    }
1787
    
1788
    $billing_details = $invoice->get_user_info();
1789
    $address_row = '';
1790
    if ( $address = $billing_details['address'] ) {
1791
        $address_row .= wpautop( wp_kses_post( $address ) );
1792
    }
1793
    
1794
    $address_fields = array();
1795
    if ( !empty( $billing_details['city'] ) ) {
1796
        $address_fields[] = $billing_details['city'];
1797
    }
1798
    
1799
    $billing_country = !empty( $billing_details['country'] ) ? $billing_details['country'] : '';
1800
    if ( !empty( $billing_details['state'] ) ) {
1801
        $address_fields[] = wpinv_state_name( $billing_details['state'], $billing_country );
1802
    }
1803
    
1804
    if ( !empty( $billing_country ) ) {
1805
        $address_fields[] = wpinv_country_name( $billing_country );
1806
    }
1807
    
1808 View Code Duplication
    if ( !empty( $address_fields ) ) {
1809
        $address_fields = implode( ", ", $address_fields );
1810
        
1811
        if ( !empty( $billing_details['zip'] ) ) {
1812
            $address_fields .= ' ' . $billing_details['zip'];
1813
        }
1814
        
1815
        $address_row .= wpautop( wp_kses_post( $address_fields ) );
1816
    }
1817
    ob_start();
1818
    ?>
1819
    <table class="table table-bordered table-sm wpi-billing-details">
1820
        <tbody>
1821
            <tr class="wpi-receipt-name">
1822
                <th class="text-left"><?php _e( 'Name', 'invoicing' ); ?></th>
1823
                <td><?php echo esc_html( trim( $billing_details['first_name'] . ' ' . $billing_details['last_name'] ) ) ;?></td>
1824
            </tr>
1825
            <tr class="wpi-receipt-email">
1826
                <th class="text-left"><?php _e( 'Email', 'invoicing' ); ?></th>
1827
                <td><?php echo $billing_details['email'] ;?></td>
1828
            </tr>
1829 View Code Duplication
            <?php if ( $billing_details['company'] ) { ?>
1830
            <tr class="wpi-receipt-company">
1831
                <th class="text-left"><?php _e( 'Company', 'invoicing' ); ?></th>
1832
                <td><?php echo esc_html( $billing_details['company'] ) ;?></td>
1833
            </tr>
1834
            <?php } ?>
1835
            <tr class="wpi-receipt-address">
1836
                <th class="text-left"><?php _e( 'Address', 'invoicing' ); ?></th>
1837
                <td><?php echo $address_row ;?></td>
1838
            </tr>
1839 View Code Duplication
            <?php if ( $billing_details['phone'] ) { ?>
1840
            <tr class="wpi-receipt-phone">
1841
                <th class="text-left"><?php _e( 'Phone', 'invoicing' ); ?></th>
1842
                <td><?php echo esc_html( $billing_details['phone'] ) ;?></td>
1843
            </tr>
1844
            <?php } ?>
1845
        </tbody>
1846
    </table>
1847
    <?php
1848
    $output = ob_get_clean();
1849
    
1850
    $output = apply_filters( 'wpinv_receipt_billing_address', $output, $invoice_id );
1851
1852
    echo $output;
1853
}
1854
1855
function wpinv_filter_success_page_content( $content ) {
1856
    if ( isset( $_GET['payment-confirm'] ) && wpinv_is_success_page() ) {
1857
        if ( has_filter( 'wpinv_payment_confirm_' . sanitize_text_field( $_GET['payment-confirm'] ) ) ) {
1858
            $content = apply_filters( 'wpinv_payment_confirm_' . sanitize_text_field( $_GET['payment-confirm'] ), $content );
1859
        }
1860
    }
1861
1862
    return $content;
1863
}
1864
add_filter( 'the_content', 'wpinv_filter_success_page_content', 99999 );
1865
1866
function wpinv_receipt_actions( $invoice ) {
1867
    if ( !empty( $invoice ) ) {
1868
        $actions = array();
1869
1870
        if ( wpinv_user_can_view_invoice( $invoice->ID ) ) {
1871
            $actions['print']   = array(
1872
                'url'  => $invoice->get_view_url( true ),
1873
                'name' => __( 'Print Invoice', 'invoicing' ),
1874
                'class' => 'btn-primary',
1875
            );
1876
        }
1877
1878
        if ( is_user_logged_in() ) {
1879
            $actions['history'] = array(
1880
                'url'  => wpinv_get_history_page_uri(),
1881
                'name' => __( 'Invoice History', 'invoicing' ),
1882
                'class' => 'btn-warning',
1883
            );
1884
        }
1885
1886
        $actions = apply_filters( 'wpinv_invoice_receipt_actions', $actions, $invoice );
1887
        
1888
        if ( !empty( $actions ) ) {
1889
        ?>
1890
        <div class="wpinv-receipt-actions text-right">
1891
            <?php foreach ( $actions as $key => $action ) { $class = !empty($action['class']) ? sanitize_html_class( $action['class'] ) : ''; ?>
1892
            <a href="<?php echo esc_url( $action['url'] );?>" class="btn btn-sm <?php echo $class . ' ' . sanitize_html_class( $key );?>" <?php echo ( !empty($action['attrs']) ? $action['attrs'] : '' ) ;?>><?php echo esc_html( $action['name'] );?></a>
1893
            <?php } ?>
1894
        </div>
1895
        <?php
1896
        }
1897
    }
1898
}
1899
add_action( 'wpinv_receipt_start', 'wpinv_receipt_actions', -10, 1 );
1900
1901
function wpinv_invoice_link( $invoice_id ) {
1902
    $invoice = wpinv_get_invoice( $invoice_id );
1903
    
1904
    if ( empty( $invoice ) ) {
1905
        return NULL;
1906
    }
1907
    
1908
    $invoice_link = '<a href="' . esc_url( $invoice->get_view_url() ) . '">' . $invoice->get_number() . '</a>';
1909
    
1910
    return apply_filters( 'wpinv_get_invoice_link', $invoice_link, $invoice );
1911
}
1912
1913
function wpinv_invoice_subscription_details( $invoice ) {
1914
    if ( !empty( $invoice ) && $invoice->is_recurring() && !wpinv_is_subscription_payment( $invoice ) ) {
1915
        $total_payments = (int)$invoice->get_total_payments();
1916
        $payments       = $invoice->get_child_payments();
1917
        
1918
        $subscription   = $invoice->get_subscription_data();
1919
        
1920
        if ( !( !empty( $subscription ) && !empty( $subscription['item_id'] ) ) ) {
1921
            return;
1922
        }
1923
        
1924
        $billing_cycle  = wpinv_get_billing_cycle( $subscription['initial_amount'], $subscription['recurring_amount'], $subscription['period'], $subscription['interval'], $subscription['bill_times'], $subscription['trial_period'], $subscription['trial_interval'], $invoice->get_currency() );
1925
        $times_billed   = $total_payments . ' / ' . ( ( (int)$subscription['bill_times'] == 0 ) ? __( 'Until cancelled', 'invoicing' ) : $subscription['bill_times'] );
1926
        
1927
        $subscription_status = $invoice->get_subscription_status();
1928
        
1929
        $status_desc = '';
1930
        if ( $subscription_status == 'trialing' && $trial_end_date = $invoice->get_trial_end_date() ) {
1931
            $status_desc = wp_sprintf( __( 'Until: %s', 'invoicing' ), $trial_end_date );
1932
        } else if ( $subscription_status == 'cancelled' && $cancelled_date = $invoice->get_cancelled_date() ) {
1933
            $status_desc = wp_sprintf( __( 'On: %s', 'invoicing' ), $cancelled_date );
1934
        }
1935
        $status_desc = $status_desc != '' ? '<span class="meta">' . $status_desc . '</span>' : '';
1936
        ?>
1937
        <div class="wpinv-subscriptions-details">
1938
            <h3 class="wpinv-subscriptions-t"><?php echo apply_filters( 'wpinv_subscription_details_title', __( 'Subscription Details', 'invoicing' ) ); ?></h3>
1939
            <table class="table">
1940
                <thead>
1941
                    <tr>
1942
                        <th><?php _e( 'Billing Cycle', 'invoicing' ) ;?></th>
1943
                        <th><?php _e( 'Start Date', 'invoicing' ) ;?></th>
1944
                        <th><?php _e( 'Expiration Date', 'invoicing' ) ;?></th>
1945
                        <th class="text-center"><?php _e( 'Times Billed', 'invoicing' ) ;?></th>
1946
                        <th class="text-center"><?php _e( 'Status', 'invoicing' ) ;?></th>
1947
                    </tr>
1948
                </thead>
1949
                <tbody>
1950
                    <tr>
1951
                        <td><?php echo $billing_cycle; ?></td>
1952
                        <td><?php echo $invoice->get_subscription_start(); ?></td>
1953
                        <td><?php echo $invoice->get_subscription_end(); ?></td>
1954
                        <td class="text-center"><?php echo $times_billed; ?></td>
1955
                        <td class="text-center wpi-sub-status"><?php echo $invoice->get_subscription_status_label() ;?>
1956
                        <?php echo $status_desc; ?>
1957
                        </td>
1958
                    </tr>
1959
                </tbody>
1960
            </table>
1961
        </div>
1962
        <?php if ( !empty( $payments ) ) { ?>
1963
        <div class="wpinv-renewal-payments">
1964
            <h3 class="wpinv-renewals-t"><?php echo apply_filters( 'wpinv_renewal_payments_title', __( 'Renewal Payments', 'invoicing' ) ); ?></h3>
1965
            <table class="table">
1966
                <thead>
1967
                    <tr>
1968
                        <th>#</th>
1969
                        <th><?php _e( 'Invoice', 'invoicing' ) ;?></th>
1970
                        <th><?php _e( 'Date', 'invoicing' ) ;?></th>
1971
                        <th class="text-right"><?php _e( 'Amount', 'invoicing' ) ;?></th>
1972
                    </tr>
1973
                </thead>
1974
                <tbody>
1975
                    <?php foreach ( $payments as $key => $invoice_id ) { ?>
1976
                    <tr>
1977
                        <th scope="row"><?php echo ( $key + 1 );?></th>
1978
                        <td><?php echo wpinv_invoice_link( $invoice_id ) ;?></td>
1979
                        <td><?php echo wpinv_get_invoice_date( $invoice_id ); ?></td>
1980
                        <td class="text-right"><?php echo wpinv_payment_total( $invoice_id, true ); ?></td>
1981
                    </tr>
1982
                    <?php } ?>
1983
                    <tr><td colspan="4" style="padding:0"></td></tr>
1984
                </tbody>
1985
            </table>
1986
        </div>
1987
        <?php } ?>
1988
        <?php
1989
    }
1990
}
1991
1992
function wpinv_cart_total_label( $label, $invoice ) {
1993
    if ( empty( $invoice ) ) {
1994
        return $label;
1995
    }
1996
    
1997
    $prefix_label = '';
1998
    if ( $invoice->is_parent() && $item_id = $invoice->get_recurring() ) {        
1999
        $prefix_label   = '<span class="label label-primary label-recurring">' . __( 'Recurring Payment', 'invoicing' ) . '</span> ' . wpinv_subscription_payment_desc( $invoice );
2000
    } else if ( $invoice->is_renewal() ) {
2001
        $prefix_label   = '<span class="label label-primary label-renewal">' . __( 'Renewal Payment', 'invoicing' ) . '</span> ';        
2002
    }
2003
    
2004
    if ( $prefix_label != '' ) {
2005
        $label  = '<span class="wpinv-cart-sub-desc">' . $prefix_label . '</span> ' . $label;
2006
    }
2007
    
2008
    return $label;
2009
}
2010
add_filter( 'wpinv_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
2011
add_filter( 'wpinv_email_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
2012
add_filter( 'wpinv_print_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
2013
2014
add_action( 'wpinv_invoice_print_middle', 'wpinv_invoice_subscription_details', 10, 1 );
2015
2016
function wpinv_invoice_print_description( $invoice ) {
2017
    if ( empty( $invoice ) ) {
2018
        return NULL;
2019
    }
2020
    if ( $description = wpinv_get_invoice_description( $invoice->ID ) ) {
2021
        ?>
2022
        <div class="row wpinv-lower">
2023
            <div class="col-sm-12 wpinv-description">
2024
                <?php echo wpautop( $description ); ?>
2025
            </div>
2026
        </div>
2027
        <?php
2028
    }
2029
}
2030
add_action( 'wpinv_invoice_print_middle', 'wpinv_invoice_print_description', 10.1, 1 );
2031
2032
function wpinv_invoice_print_payment_info( $invoice ) {
2033
    if ( empty( $invoice ) ) {
2034
        return NULL;
2035
    }
2036
    
2037
    if ( $payments_info = wpinv_display_payments_info( $invoice->ID, false ) ) {
2038
        ?>
2039
        <div class="row wpinv-payments">
2040
            <div class="col-sm-12">
2041
                <?php echo $payments_info; ?>
2042
            </div>
2043
        </div>
2044
        <?php 
2045
    }
2046
}
2047
// add_action( 'wpinv_invoice_print_after_line_items', 'wpinv_invoice_print_payment_info', 10, 1 );
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
2048
2049
function wpinv_get_invoice_note_line_item( $note, $echo = true ) {
2050
    if ( empty( $note ) ) {
2051
        return NULL;
2052
    }
2053
    
2054
    if ( is_int( $note ) ) {
2055
        $note = get_comment( $note );
2056
    }
2057
    
2058
    if ( !( is_object( $note ) && is_a( $note, 'WP_Comment' ) ) ) {
2059
        return NULL;
2060
    }
2061
    
2062
    $note_classes   = array( 'note' );
2063
    $note_classes[] = get_comment_meta( $note->comment_ID, '_wpi_customer_note', true ) ? 'customer-note' : '';
2064
    $note_classes[] = $note->comment_author === __( 'System', 'invoicing' ) ? 'system-note' : '';
2065
    $note_classes   = apply_filters( 'wpinv_invoice_note_class', array_filter( $note_classes ), $note );
2066
    $note_classes   = !empty( $note_classes ) ? implode( ' ', $note_classes ) : '';
2067
    
2068
    ob_start();
2069
    ?>
2070
    <li rel="<?php echo absint( $note->comment_ID ) ; ?>" class="<?php echo esc_attr( $note_classes ); ?>">
2071
        <div class="note_content">
2072
            <?php echo wpautop( wptexturize( wp_kses_post( $note->comment_content ) ) ); ?>
2073
        </div>
2074
        <p class="meta">
2075
            <abbr class="exact-date" title="<?php echo $note->comment_date; ?>"><?php printf( __( '%1$s - %2$s at %3$s', 'invoicing' ), $note->comment_author, date_i18n( get_option( 'date_format' ), strtotime( $note->comment_date ) ), date_i18n( get_option( 'time_format' ), strtotime( $note->comment_date ) ) ); ?></abbr>&nbsp;&nbsp;
2076
            <?php if($note->comment_author !== 'System') {?>
2077
                <a href="#" class="delete_note"><?php _e( 'Delete note', 'invoicing' ); ?></a>
2078
            <?php } ?>
2079
        </p>
2080
    </li>
2081
    <?php
2082
    $note_content = ob_get_clean();
2083
    $note_content = apply_filters( 'wpinv_get_invoice_note_line_item', $note_content, $note, $echo );
2084
    
2085
    if ( $echo ) {
2086
        echo $note_content;
2087
    } else {
2088
        return $note_content;
2089
    }
2090
}
2091
2092
function wpinv_invalid_invoice_content() {
2093
    global $post;
2094
2095
    $invoice = wpinv_get_invoice( $post->ID );
2096
2097
    $error = __( 'This invoice is only viewable by clicking on the invoice link that sent to you via email.', 'invoicing' );
2098
    if ( !empty( $invoice->ID ) && $invoice->has_status( array_keys( wpinv_get_invoice_statuses() ) ) ) {
2099
        if ( is_user_logged_in() ) {
2100 View Code Duplication
            if ( wpinv_require_login_to_checkout() ) {
2101
                if ( isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
2102
                    $error = __( 'You are not allowed to view this invoice.', 'invoicing' );
2103
                }
2104
            }
2105 View Code Duplication
        } else {
2106
            if ( wpinv_require_login_to_checkout() ) {
2107
                if ( isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
2108
                    $error = __( 'You must be logged in to view this invoice.', 'invoicing' );
2109
                }
2110
            }
2111
        }
2112
    } else {
2113
        $error = __( 'This invoice is deleted or does not exist.', 'invoicing' );
2114
    }
2115
    ?>
2116
    <div class="row wpinv-row-invalid">
2117
        <div class="col-md-6 col-md-offset-3 wpinv-message error">
2118
            <h3><?php _e( 'Access Denied', 'invoicing' ); ?></h3>
2119
            <p class="wpinv-msg-text"><?php echo $error; ?></p>
2120
        </div>
2121
    </div>
2122
    <?php
2123
}
2124
add_action( 'wpinv_invalid_invoice_content', 'wpinv_invalid_invoice_content' );