Passed
Pull Request — master (#103)
by
unknown
04:24
created

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

Complexity

Conditions 7
Paths 64

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 21
nc 64
nop 1
dl 0
loc 35
rs 6.7272
c 0
b 0
f 0
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
	$pages[] = wpinv_get_option( 'invoice_subscription_page' );
235
236
	if( !wpinv_is_checkout() && !is_page( $pages ) ) {
237
		return;
238
	}
239
240
	echo '<meta name="robots" content="noindex,nofollow" />' . "\n";
241
}
242
add_action( 'wp_head', 'wpinv_checkout_meta_tags' );
243
244
function wpinv_add_body_classes( $class ) {
245
	$classes = (array)$class;
246
247
	if( wpinv_is_checkout() ) {
248
		$classes[] = 'wpinv-checkout';
249
		$classes[] = 'wpinv-page';
250
	}
251
252
	if( wpinv_is_success_page() ) {
253
		$classes[] = 'wpinv-success';
254
		$classes[] = 'wpinv-page';
255
	}
256
257
	if( wpinv_is_failed_transaction_page() ) {
258
		$classes[] = 'wpinv-failed-transaction';
259
		$classes[] = 'wpinv-page';
260
	}
261
262
	if( wpinv_is_invoice_history_page() ) {
263
		$classes[] = 'wpinv-history';
264
		$classes[] = 'wpinv-page';
265
	}
266
267
    if( wpinv_is_subscriptions_history_page() ) {
268
        $classes[] = 'wpinv-subscription';
269
        $classes[] = 'wpinv-page';
270
    }
271
272
	if( wpinv_is_test_mode() ) {
273
		$classes[] = 'wpinv-test-mode';
274
		$classes[] = 'wpinv-page';
275
	}
276
277
	return array_unique( $classes );
278
}
279
add_filter( 'body_class', 'wpinv_add_body_classes' );
280
281
function wpinv_html_dropdown( $name = 'wpinv_discounts', $selected = 0, $status = '' ) {
282
    $args = array( 'nopaging' => true );
283
284
    if ( ! empty( $status ) )
285
        $args['post_status'] = $status;
286
287
    $discounts = wpinv_get_discounts( $args );
288
    $options   = array();
289
290
    if ( $discounts ) {
291
        foreach ( $discounts as $discount ) {
292
            $options[ absint( $discount->ID ) ] = esc_html( get_the_title( $discount->ID ) );
293
        }
294
    } else {
295
        $options[0] = __( 'No discounts found', 'invoicing' );
296
    }
297
298
    $output = wpinv_html_select( array(
299
        'name'             => $name,
300
        'selected'         => $selected,
301
        'options'          => $options,
302
        'show_option_all'  => false,
303
        'show_option_none' => false,
304
    ) );
305
306
    return $output;
307
}
308
309
function wpinv_html_year_dropdown( $name = 'year', $selected = 0, $years_before = 5, $years_after = 0 ) {
310
    $current     = date( 'Y' );
311
    $start_year  = $current - absint( $years_before );
312
    $end_year    = $current + absint( $years_after );
313
    $selected    = empty( $selected ) ? date( 'Y' ) : $selected;
314
    $options     = array();
315
316
    while ( $start_year <= $end_year ) {
317
        $options[ absint( $start_year ) ] = $start_year;
318
        $start_year++;
319
    }
320
321
    $output = wpinv_html_select( array(
322
        'name'             => $name,
323
        'selected'         => $selected,
324
        'options'          => $options,
325
        'show_option_all'  => false,
326
        'show_option_none' => false
327
    ) );
328
329
    return $output;
330
}
331
332
function wpinv_html_month_dropdown( $name = 'month', $selected = 0 ) {
333
    $month   = 1;
334
    $options = array();
335
    $selected = empty( $selected ) ? date( 'n' ) : $selected;
336
337
    while ( $month <= 12 ) {
338
        $options[ absint( $month ) ] = wpinv_month_num_to_name( $month );
339
        $month++;
340
    }
341
342
    $output = wpinv_html_select( array(
343
        'name'             => $name,
344
        'selected'         => $selected,
345
        'options'          => $options,
346
        'show_option_all'  => false,
347
        'show_option_none' => false
348
    ) );
349
350
    return $output;
351
}
352
353
function wpinv_html_select( $args = array() ) {
354
    $defaults = array(
355
        'options'          => array(),
356
        'name'             => null,
357
        'class'            => '',
358
        'id'               => '',
359
        'selected'         => 0,
360
        'chosen'           => false,
361
        'placeholder'      => null,
362
        'multiple'         => false,
363
        'show_option_all'  => _x( 'All', 'all dropdown items', 'invoicing' ),
364
        'show_option_none' => _x( 'None', 'no dropdown items', 'invoicing' ),
365
        'data'             => array(),
366
        'onchange'         => null,
367
        'required'         => false,
368
        'disabled'         => false,
369
        'readonly'         => false,
370
    );
371
372
    $args = wp_parse_args( $args, $defaults );
373
374
    $data_elements = '';
375
    foreach ( $args['data'] as $key => $value ) {
376
        $data_elements .= ' data-' . esc_attr( $key ) . '="' . esc_attr( $value ) . '"';
377
    }
378
379
    if( $args['multiple'] ) {
380
        $multiple = ' MULTIPLE';
381
    } else {
382
        $multiple = '';
383
    }
384
385
    if( $args['chosen'] ) {
386
        $args['class'] .= ' wpinv-select-chosen';
387
    }
388
389
    if( $args['placeholder'] ) {
390
        $placeholder = $args['placeholder'];
391
    } else {
392
        $placeholder = '';
393
    }
394
    
395
    $options = '';
396 View Code Duplication
    if( !empty( $args['onchange'] ) ) {
397
        $options .= ' onchange="' . esc_attr( $args['onchange'] ) . '"';
398
    }
399
    
400
    if( !empty( $args['required'] ) ) {
401
        $options .= ' required="required"';
402
    }
403
    
404
    if( !empty( $args['disabled'] ) ) {
405
        $options .= ' disabled';
406
    }
407
    
408
    if( !empty( $args['readonly'] ) ) {
409
        $options .= ' readonly';
410
    }
411
412
    $class  = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
413
    $output = '<select name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( $args['id'] ) . '" class="wpinv-select ' . $class . '"' . $multiple . ' data-placeholder="' . $placeholder . '" ' . trim( $options ) . $data_elements . '>';
414
415 View Code Duplication
    if ( $args['show_option_all'] ) {
416
        if( $args['multiple'] ) {
417
            $selected = selected( true, in_array( 0, $args['selected'] ), false );
418
        } else {
419
            $selected = selected( $args['selected'], 0, false );
420
        }
421
        $output .= '<option value="all"' . $selected . '>' . esc_html( $args['show_option_all'] ) . '</option>';
422
    }
423
424
    if ( !empty( $args['options'] ) ) {
425
426 View Code Duplication
        if ( $args['show_option_none'] ) {
427
            if( $args['multiple'] ) {
428
                $selected = selected( true, in_array( "", $args['selected'] ), false );
429
            } else {
430
                $selected = selected( $args['selected'] === "", true, false );
431
            }
432
            $output .= '<option value=""' . $selected . '>' . esc_html( $args['show_option_none'] ) . '</option>';
433
        }
434
435
        foreach( $args['options'] as $key => $option ) {
436
437
            if( $args['multiple'] && is_array( $args['selected'] ) ) {
438
                $selected = selected( true, (bool)in_array( $key, $args['selected'] ), false );
439
            } else {
440
                $selected = selected( $args['selected'], $key, false );
441
            }
442
443
            $output .= '<option value="' . esc_attr( $key ) . '"' . $selected . '>' . esc_html( $option ) . '</option>';
444
        }
445
    }
446
447
    $output .= '</select>';
448
449
    return $output;
450
}
451
452
function wpinv_item_dropdown( $args = array() ) {
453
    $defaults = array(
454
        'name'              => 'wpi_item',
455
        'id'                => 'wpi_item',
456
        'class'             => '',
457
        'multiple'          => false,
458
        'selected'          => 0,
459
        'chosen'            => false,
460
        'number'            => 100,
461
        'placeholder'       => __( 'Choose a item', 'invoicing' ),
462
        'data'              => array( 'search-type' => 'item' ),
463
        'show_option_all'   => false,
464
        'show_option_none'  => false,
465
        'show_recurring'    => false,
466
    );
467
468
    $args = wp_parse_args( $args, $defaults );
469
470
    $item_args = array(
471
        'post_type'      => 'wpi_item',
472
        'orderby'        => 'title',
473
        'order'          => 'ASC',
474
        'posts_per_page' => $args['number']
475
    );
476
    
477
    $item_args  = apply_filters( 'wpinv_item_dropdown_query_args', $item_args, $args, $defaults );
478
479
    $items      = get_posts( $item_args );
480
    $options    = array();
481
    if ( $items ) {
482
        foreach ( $items as $item ) {
483
            $title = esc_html( $item->post_title );
484
            
485
            if ( !empty( $args['show_recurring'] ) ) {
486
                $title .= wpinv_get_item_suffix( $item->ID, false );
487
            }
488
            
489
            $options[ absint( $item->ID ) ] = $title;
490
        }
491
    }
492
493
    // This ensures that any selected items are included in the drop down
494
    if( is_array( $args['selected'] ) ) {
495
        foreach( $args['selected'] as $item ) {
496
            if( ! in_array( $item, $options ) ) {
497
                $title = get_the_title( $item );
498
                if ( !empty( $args['show_recurring'] ) ) {
499
                    $title .= wpinv_get_item_suffix( $item, false );
500
                }
501
                $options[$item] = $title;
502
            }
503
        }
504
    } elseif ( is_numeric( $args['selected'] ) && $args['selected'] !== 0 ) {
505
        if ( ! in_array( $args['selected'], $options ) ) {
506
            $title = get_the_title( $args['selected'] );
507
            if ( !empty( $args['show_recurring'] ) ) {
508
                $title .= wpinv_get_item_suffix( $args['selected'], false );
509
            }
510
            $options[$args['selected']] = get_the_title( $args['selected'] );
511
        }
512
    }
513
514
    $output = wpinv_html_select( array(
515
        'name'             => $args['name'],
516
        'selected'         => $args['selected'],
517
        'id'               => $args['id'],
518
        'class'            => $args['class'],
519
        'options'          => $options,
520
        'chosen'           => $args['chosen'],
521
        'multiple'         => $args['multiple'],
522
        'placeholder'      => $args['placeholder'],
523
        'show_option_all'  => $args['show_option_all'],
524
        'show_option_none' => $args['show_option_none'],
525
        'data'             => $args['data'],
526
    ) );
527
528
    return $output;
529
}
530
531
function wpinv_html_checkbox( $args = array() ) {
532
    $defaults = array(
533
        'name'     => null,
534
        'current'  => null,
535
        'class'    => 'wpinv-checkbox',
536
        'options'  => array(
537
            'disabled' => false,
538
            'readonly' => false
539
        )
540
    );
541
542
    $args = wp_parse_args( $args, $defaults );
543
544
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
545
    $options = '';
546
    if ( ! empty( $args['options']['disabled'] ) ) {
547
        $options .= ' disabled="disabled"';
548
    } elseif ( ! empty( $args['options']['readonly'] ) ) {
549
        $options .= ' readonly';
550
    }
551
552
    $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 ) . ' />';
553
554
    return $output;
555
}
556
557
function wpinv_html_text( $args = array() ) {
558
    // Backwards compatibility
559
    if ( func_num_args() > 1 ) {
560
        $args = func_get_args();
561
562
        $name  = $args[0];
563
        $value = isset( $args[1] ) ? $args[1] : '';
564
        $label = isset( $args[2] ) ? $args[2] : '';
565
        $desc  = isset( $args[3] ) ? $args[3] : '';
566
    }
567
568
    $defaults = array(
569
        'id'           => '',
570
        'name'         => isset( $name )  ? $name  : 'text',
571
        'value'        => isset( $value ) ? $value : null,
572
        'label'        => isset( $label ) ? $label : null,
573
        'desc'         => isset( $desc )  ? $desc  : null,
574
        'placeholder'  => '',
575
        'class'        => 'regular-text',
576
        'disabled'     => false,
577
        'readonly'     => false,
578
        'required'     => false,
579
        'autocomplete' => '',
580
        'data'         => false
581
    );
582
583
    $args = wp_parse_args( $args, $defaults );
584
585
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
586
    $options = '';
587
    if( $args['required'] ) {
588
        $options .= ' required="required"';
589
    }
590
    if( $args['readonly'] ) {
591
        $options .= ' readonly';
592
    }
593
    if( $args['readonly'] ) {
594
        $options .= ' readonly';
595
    }
596
597
    $data = '';
598
    if ( !empty( $args['data'] ) ) {
599
        foreach ( $args['data'] as $key => $value ) {
600
            $data .= 'data-' . wpinv_sanitize_key( $key ) . '="' . esc_attr( $value ) . '" ';
601
        }
602
    }
603
604
    $output = '<span id="wpinv-' . wpinv_sanitize_key( $args['name'] ) . '-wrap">';
605
    $output .= '<label class="wpinv-label" for="' . wpinv_sanitize_key( $args['id'] ) . '">' . esc_html( $args['label'] ) . '</label>';
606 View Code Duplication
    if ( ! empty( $args['desc'] ) ) {
607
        $output .= '<span class="wpinv-description">' . esc_html( $args['desc'] ) . '</span>';
608
    }
609
610
    $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 ) . '/>';
611
612
    $output .= '</span>';
613
614
    return $output;
615
}
616
617
function wpinv_html_date_field( $args = array() ) {
618
    if( empty( $args['class'] ) ) {
619
        $args['class'] = 'wpiDatepicker';
620
    } elseif( ! strpos( $args['class'], 'wpiDatepicker' ) ) {
621
        $args['class'] .= ' wpiDatepicker';
622
    }
623
624
    return wpinv_html_text( $args );
625
}
626
627
function wpinv_html_textarea( $args = array() ) {
628
    $defaults = array(
629
        'name'        => 'textarea',
630
        'value'       => null,
631
        'label'       => null,
632
        'desc'        => null,
633
        'class'       => 'large-text',
634
        'disabled'    => false
635
    );
636
637
    $args = wp_parse_args( $args, $defaults );
638
639
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
640
    $disabled = '';
641
    if( $args['disabled'] ) {
642
        $disabled = ' disabled="disabled"';
643
    }
644
645
    $output = '<span id="wpinv-' . wpinv_sanitize_key( $args['name'] ) . '-wrap">';
646
    $output .= '<label class="wpinv-label" for="' . wpinv_sanitize_key( $args['name'] ) . '">' . esc_html( $args['label'] ) . '</label>';
647
    $output .= '<textarea name="' . esc_attr( $args['name'] ) . '" id="' . wpinv_sanitize_key( $args['name'] ) . '" class="' . $class . '"' . $disabled . '>' . esc_attr( $args['value'] ) . '</textarea>';
648
649 View Code Duplication
    if ( ! empty( $args['desc'] ) ) {
650
        $output .= '<span class="wpinv-description">' . esc_html( $args['desc'] ) . '</span>';
651
    }
652
    $output .= '</span>';
653
654
    return $output;
655
}
656
657
function wpinv_html_ajax_user_search( $args = array() ) {
658
    $defaults = array(
659
        'name'        => 'user_id',
660
        'value'       => null,
661
        'placeholder' => __( 'Enter username', 'invoicing' ),
662
        'label'       => null,
663
        'desc'        => null,
664
        'class'       => '',
665
        'disabled'    => false,
666
        'autocomplete'=> 'off',
667
        'data'        => false
668
    );
669
670
    $args = wp_parse_args( $args, $defaults );
671
672
    $args['class'] = 'wpinv-ajax-user-search ' . $args['class'];
673
674
    $output  = '<span class="wpinv_user_search_wrap">';
675
        $output .= wpinv_html_text( $args );
676
        $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>';
677
    $output .= '</span>';
678
679
    return $output;
680
}
681
682
function wpinv_ip_geolocation() {
683
    global $wpinv_euvat;
684
    
685
    $ip         = !empty( $_GET['ip'] ) ? sanitize_text_field( $_GET['ip'] ) : '';    
686
    $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...
687
    $iso        = '';
688
    $country    = '';
689
    $region     = '';
690
    $city       = '';
691
    $longitude  = '';
692
    $latitude   = '';
693
    $credit     = '';
694
    $address    = '';
695
    
696
    if ( wpinv_get_option( 'vat_ip_lookup' ) == 'geoip2' && $geoip2_city = $wpinv_euvat->geoip2_city_record( $ip ) ) {
697
        try {
698
            $iso        = $geoip2_city->country->isoCode;
699
            $country    = $geoip2_city->country->name;
700
            $region     = !empty( $geoip2_city->subdivisions ) && !empty( $geoip2_city->subdivisions[0]->name ) ? $geoip2_city->subdivisions[0]->name : '';
701
            $city       = $geoip2_city->city->name;
702
            $longitude  = $geoip2_city->location->longitude;
703
            $latitude   = $geoip2_city->location->latitude;
704
            $credit     = __( 'Geolocated using the information by MaxMind, available from <a href="http://www.maxmind.com" target="_blank">www.maxmind.com</a>', 'invoicing' );
705
        } catch( Exception $e ) { }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
706
    }
707
    
708
    if ( !( $iso && $longitude && $latitude ) && function_exists( 'simplexml_load_file' ) ) {
709
        try {
710
            $load_xml = simplexml_load_file( 'http://www.geoplugin.net/xml.gp?ip=' . $ip );
711
            
712
            if ( !empty( $load_xml ) && isset( $load_xml->geoplugin_countryCode ) && !empty( $load_xml->geoplugin_latitude ) && !empty( $load_xml->geoplugin_longitude ) ) {
713
                $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...
714
                $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...
715
                $region     = !empty( $load_xml->geoplugin_regionName ) ? $load_xml->geoplugin_regionName : '';
716
                $city       = !empty( $load_xml->geoplugin_city ) ? $load_xml->geoplugin_city : '';
717
                $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...
718
                $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...
719
                $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...
720
                $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;
721
            }
722
        } catch( Exception $e ) { }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
723
    }
724
    
725
    if ( $iso && $longitude && $latitude ) {
726
        if ( $city ) {
727
            $address .= $city . ', ';
728
        }
729
        
730
        if ( $region ) {
731
            $address .= $region . ', ';
732
        }
733
        
734
        $address .= $country . ' (' . $iso . ')';
735
        $content = '<p>'. sprintf( __( '<b>Address:</b> %s', 'invoicing' ), $address ) . '</p>';
736
        $content .= '<p>'. $credit . '</p>';
737
    } else {
738
        $content = '<p>'. sprintf( __( 'Unable to find geolocation for the IP address: %s', 'invoicing' ), $ip ) . '</p>';
739
    }
740
    ?>
741
<!DOCTYPE html>
742
<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>
743
<body>
744
    <?php if ( $latitude && $latitude ) { ?>
745
    <div id="map"></div>
746
        <script src="//cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.0-rc.1/leaflet.js"></script>
747
        <script type="text/javascript">
748
        var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
749
            osmAttrib = '&copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors',
750
            osm = L.tileLayer(osmUrl, {maxZoom: 18, attribution: osmAttrib}),
751
            latlng = new L.LatLng(<?php echo $latitude;?>, <?php echo $longitude;?>);
752
753
        var map = new L.Map('map', {center: latlng, zoom: 12, layers: [osm]});
754
755
        var marker = new L.Marker(latlng);
756
        map.addLayer(marker);
757
758
        marker.bindPopup("<p><?php esc_attr_e( $address );?></p>");
759
    </script>
760
    <?php } ?>
761
    <div style="height:100px"><?php echo $content; ?></div>
762
</body></html>
763
<?php
764
    exit;
765
}
766
add_action( 'wp_ajax_wpinv_ip_geolocation', 'wpinv_ip_geolocation' );
767
add_action( 'wp_ajax_nopriv_wpinv_ip_geolocation', 'wpinv_ip_geolocation' );
768
769
// Set up the template for the invoice.
770
function wpinv_template( $template ) {
771
    global $post, $wp_query;
772
    
773
    if ( ( is_single() || is_404() ) && !empty( $post->ID ) && (get_post_type( $post->ID ) == 'wpi_invoice' or get_post_type( $post->ID ) == 'wpi_quote')) {
774
        if ( wpinv_user_can_view_invoice( $post->ID ) ) {
775
            $template = wpinv_get_template_part( 'wpinv-invoice-print', false, false );
776
        } else {
777
            $template = wpinv_get_template_part( 'wpinv-invalid-access', false, false );
778
        }
779
    }
780
781
    return $template;
782
}
783
784
function wpinv_get_business_address() {
785
    $business_address   = wpinv_store_address();
786
    $business_address   = !empty( $business_address ) ? wpautop( wp_kses_post( $business_address ) ) : '';
787
    
788
    /*
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...
789
    $default_country    = wpinv_get_default_country();
790
    $default_state      = wpinv_get_default_state();
791
    
792
    $address_fields = array();
793
    if ( !empty( $default_state ) ) {
794
        $address_fields[] = wpinv_state_name( $default_state, $default_country );
795
    }
796
    
797
    if ( !empty( $default_country ) ) {
798
        $address_fields[] = wpinv_country_name( $default_country );
799
    }
800
    
801
    if ( !empty( $address_fields ) ) {
802
        $address_fields = implode( ", ", $address_fields );
803
                
804
        $business_address .= wpautop( wp_kses_post( $address_fields ) );
805
    }
806
    */
807
    
808
    $business_address = $business_address ? '<div class="address">' . $business_address . '</div>' : '';
809
    
810
    return apply_filters( 'wpinv_get_business_address', $business_address );
811
}
812
813
function wpinv_display_from_address() {
814
    global $wpinv_euvat;
815
    
816
    $from_name = $wpinv_euvat->get_company_name();
817
    if (empty($from_name)) {
818
        $from_name = wpinv_get_business_name();
819
    }
820
    ?><div class="from col-xs-2"><strong><?php _e( 'From:', 'invoicing' ) ?></strong></div>
821
    <div class="wrapper col-xs-10">
822
        <div class="name"><?php echo esc_html( $from_name ); ?></div>
823
        <?php if ( $address = wpinv_get_business_address() ) { ?>
824
        <div class="address"><?php echo wpautop( wp_kses_post( $address ) );?></div>
825
        <?php } ?>
826
        <?php if ( $email_from = wpinv_mail_get_from_address() ) { ?>
827
        <div class="email_from"><?php echo wp_sprintf( __( 'Email: %s', 'invoicing' ), $email_from );?></div>
828
        <?php } ?>
829
    </div>
830
    <?php
831
}
832
833
function wpinv_watermark( $id = 0 ) {
834
    $output = wpinv_get_watermark( $id );
835
    
836
    return apply_filters( 'wpinv_get_watermark', $output, $id );
837
}
838
839
function wpinv_get_watermark( $id ) {
840
    if ( !$id > 0 ) {
841
        return NULL;
842
    }
843
    $invoice = wpinv_get_invoice( $id );
844
    
845
    if ( !empty( $invoice ) && "wpi_invoice" === $invoice->post_type ) {
846
        if ( $invoice->is_paid() ) {
847
            return __( 'Paid', 'invoicing' );
848
        }
849
        if ( $invoice->is_refunded() ) {
850
            return __( 'Refunded', 'invoicing' );
851
        }
852
        if ( $invoice->has_status( array( 'wpi-cancelled' ) ) ) {
853
            return __( 'Cancelled', 'invoicing' );
854
        }
855
    }
856
    
857
    return NULL;
858
}
859
860
function wpinv_display_invoice_details( $invoice ) {
861
    global $wpinv_euvat;
862
    
863
    $invoice_id = $invoice->ID;
864
    $vat_name   = $wpinv_euvat->get_vat_name();
865
    $use_taxes  = wpinv_use_taxes();
866
    
867
    $invoice_status = wpinv_get_invoice_status( $invoice_id );
868
    ?>
869
    <table class="table table-bordered table-sm">
870 View Code Duplication
        <?php if ( $invoice_number = wpinv_get_invoice_number( $invoice_id ) ) { ?>
871
            <tr class="wpi-row-number">
872
                <th><?php echo apply_filters( 'wpinv_invoice_number_label', __( 'Invoice Number', 'invoicing' ), $invoice ); ?></th>
873
                <td><?php echo esc_html( $invoice_number ); ?></td>
874
            </tr>
875
        <?php } ?>
876
        <tr class="wpi-row-status">
877
            <th><?php echo apply_filters( 'wpinv_invoice_status_label', __( 'Invoice Status', 'invoicing' ), $invoice ); ?></th>
878
            <td><?php echo wpinv_invoice_status_label( $invoice_status, wpinv_get_invoice_status( $invoice_id, true ) ); ?></td>
879
        </tr>
880 View Code Duplication
        <?php if ( $invoice->is_renewal() ) { ?>
881
        <tr class="wpi-row-parent">
882
            <th><?php echo apply_filters( 'wpinv_invoice_parent_invoice_label', __( 'Parent Invoice', 'invoicing' ), $invoice ); ?></th>
883
            <td><?php echo wpinv_invoice_link( $invoice->parent_invoice ); ?></td>
884
        </tr>
885
        <?php } ?>
886 View Code Duplication
        <?php if ( ( $gateway_name = wpinv_get_payment_gateway_name( $invoice_id ) ) && ( $invoice->is_paid() || $invoice->is_refunded() ) ) { ?>
887
            <tr class="wpi-row-gateway">
888
                <th><?php echo apply_filters( 'wpinv_invoice_payment_method_label', __( 'Payment Method', 'invoicing' ), $invoice ); ?></th>
889
                <td><?php echo $gateway_name; ?></td>
890
            </tr>
891
        <?php } ?>
892
        <?php if ( $invoice_date = wpinv_get_invoice_date( $invoice_id ) ) { ?>
893
            <tr class="wpi-row-date">
894
                <th><?php echo apply_filters( 'wpinv_invoice_date_label', __( 'Invoice Date', 'invoicing' ), $invoice ); ?></th>
895
                <td><?php echo $invoice_date; ?></td>
896
            </tr>
897
        <?php } ?>
898
        <?php if ( wpinv_get_option( 'overdue_active' ) && $invoice->needs_payment() && ( $due_date = $invoice->get_due_date( true ) ) ) { ?>
899
            <tr class="wpi-row-date">
900
                <th><?php echo apply_filters( 'wpinv_invoice_due_date_label', __( 'Due Date', 'invoicing' ), $invoice ); ?></th>
901
                <td><?php echo $due_date; ?></td>
902
            </tr>
903
        <?php } ?>
904 View Code Duplication
        <?php if ( $owner_vat_number = $wpinv_euvat->get_vat_number() ) { ?>
905
            <tr class="wpi-row-ovatno">
906
                <th><?php echo apply_filters( 'wpinv_invoice_owner_vat_number_label', wp_sprintf( __( 'Owner %s Number', 'invoicing' ), $vat_name ), $invoice, $vat_name ); ?></th>
907
                <td><?php echo $owner_vat_number; ?></td>
908
            </tr>
909
        <?php } ?>
910 View Code Duplication
        <?php if ( $use_taxes && ( $user_vat_number = wpinv_get_invoice_vat_number( $invoice_id ) ) ) { ?>
911
            <tr class="wpi-row-uvatno">
912
                <th><?php echo apply_filters( 'wpinv_invoice_user_vat_number_label', wp_sprintf( __( 'Invoice %s Number', 'invoicing' ), $vat_name ), $invoice, $vat_name ); ?></th>
913
                <td><?php echo $user_vat_number; ?></td>
914
            </tr>
915
        <?php } ?>
916
        <tr class="table-active tr-total wpi-row-total">
917
            <th><strong><?php _e( 'Total Amount', 'invoicing' ) ?></strong></th>
918
            <td><strong><?php echo wpinv_payment_total( $invoice_id, true ); ?></strong></td>
919
        </tr>
920
    </table>
921
<?php
922
}
923
924
function wpinv_display_to_address( $invoice_id = 0 ) {
925
    $invoice = wpinv_get_invoice( $invoice_id );
926
    
927
    if ( empty( $invoice ) ) {
928
        return NULL;
929
    }
930
    
931
    $billing_details = $invoice->get_user_info();
932
    $output = '<div class="to col-xs-2"><strong>' . __( 'To:', 'invoicing' ) . '</strong></div>';
933
    $output .= '<div class="wrapper col-xs-10">';
934
    
935
    ob_start();
936
    do_action( 'wpinv_display_to_address_top', $invoice );
937
    $output .= ob_get_clean();
938
    
939
    $output .= '<div class="name">' . esc_html( trim( $billing_details['first_name'] . ' ' . $billing_details['last_name'] ) ) . '</div>';
940
    if ( $company = $billing_details['company'] ) {
941
        $output .= '<div class="company">' . wpautop( wp_kses_post( $company ) ) . '</div>';
942
    }
943
    $address_row = '';
944
    if ( $address = $billing_details['address'] ) {
945
        $address_row .= wpautop( wp_kses_post( $address ) );
946
    }
947
    
948
    $address_fields = array();
949
    if ( !empty( $billing_details['city'] ) ) {
950
        $address_fields[] = $billing_details['city'];
951
    }
952
    
953
    $billing_country = !empty( $billing_details['country'] ) ? $billing_details['country'] : '';
954
    if ( !empty( $billing_details['state'] ) ) {
955
        $address_fields[] = wpinv_state_name( $billing_details['state'], $billing_country );
956
    }
957
    
958
    if ( !empty( $billing_country ) ) {
959
        $address_fields[] = wpinv_country_name( $billing_country );
960
    }
961
    
962 View Code Duplication
    if ( !empty( $address_fields ) ) {
963
        $address_fields = implode( ", ", $address_fields );
964
        
965
        if ( !empty( $billing_details['zip'] ) ) {
966
            $address_fields .= ' ' . $billing_details['zip'];
967
        }
968
        
969
        $address_row .= wpautop( wp_kses_post( $address_fields ) );
970
    }
971
    
972
    if ( $address_row ) {
973
        $output .= '<div class="address">' . $address_row . '</div>';
974
    }
975
    
976 View Code Duplication
    if ( $phone = $invoice->get_phone() ) {
977
        $output .= '<div class="phone">' . wp_sprintf( __( 'Phone: %s', 'invoicing' ), esc_html( $phone ) ) . '</div>';
978
    }
979 View Code Duplication
    if ( $email = $invoice->get_email() ) {
980
        $output .= '<div class="email">' . wp_sprintf( __( 'Email: %s' , 'invoicing'), esc_html( $email ) ) . '</div>';
981
    }
982
    
983
    ob_start();
984
    do_action( 'wpinv_display_to_address_bottom', $invoice );
985
    $output .= ob_get_clean();
986
    
987
    $output .= '</div>';
988
    $output = apply_filters( 'wpinv_display_to_address', $output, $invoice );
989
990
    echo $output;
991
}
992
993
function wpinv_display_line_items( $invoice_id = 0 ) {
994
    global $wpinv_euvat, $ajax_cart_details;
995
    $invoice            = wpinv_get_invoice( $invoice_id );
996
    $quantities_enabled = wpinv_item_quantities_enabled();
997
    $use_taxes          = wpinv_use_taxes();
998
    $zero_tax           = !(float)$invoice->get_tax() > 0 ? true : false;
999
    $tax_label           = $use_taxes && $invoice->has_vat() ? $wpinv_euvat->get_vat_name() : __( 'Tax', 'invoicing' );
1000
    $tax_title          = !$zero_tax && $use_taxes ? ( wpinv_prices_include_tax() ? wp_sprintf( __( '(%s Incl.)', 'invoicing' ), $tax_label ) : wp_sprintf( __( '(%s Excl.)', 'invoicing' ), $tax_label ) ) : '';
1001
    
1002
    $cart_details       = $invoice->get_cart_details();
1003
    $ajax_cart_details  = $cart_details;
1004
    ob_start();
1005
    ?>
1006
    <table class="table table-sm table-bordered table-responsive">
1007
        <thead>
1008
            <tr>
1009
                <th class="name"><strong><?php _e( "Item Name", "invoicing" );?></strong></th>
1010
                <th class="rate"><strong><?php _e( "Price", "invoicing" );?></strong></th>
1011
                <?php if ($quantities_enabled) { ?>
1012
                    <th class="qty"><strong><?php _e( "Qty", "invoicing" );?></strong></th>
1013
                <?php } ?>
1014
                <?php if ($use_taxes && !$zero_tax) { ?>
1015
                    <th class="tax"><strong><?php echo $tax_label . ' <span class="normal small">(%)</span>'; ?></strong></th>
1016
                <?php } ?>
1017
                <th class="total"><strong><?php echo __( "Item Total", "invoicing" ) . ' <span class="normal small">' . $tax_title . '<span>';?></strong></th>
1018
            </tr>
1019
        </thead>
1020
        <tbody>
1021
        <?php 
1022
            if ( !empty( $cart_details ) ) {
1023
                do_action( 'wpinv_display_line_items_start', $invoice );
1024
                
1025
                $count = 0;
1026
                $cols  = 3;
1027
                foreach ( $cart_details as $key => $cart_item ) {
1028
                    $item_id    = !empty($cart_item['id']) ? absint( $cart_item['id'] ) : '';
1029
                    $item_price = isset($cart_item["item_price"]) ? wpinv_round_amount( $cart_item["item_price"] ) : 0;
1030
                    $line_total = isset($cart_item["subtotal"]) ? wpinv_round_amount( $cart_item["subtotal"] ) : 0;
1031
                    $quantity   = !empty($cart_item['quantity']) && (int)$cart_item['quantity'] > 0 ? absint( $cart_item['quantity'] ) : 1;
1032
                    
1033
                    $item       = $item_id ? new WPInv_Item( $item_id ) : NULL;
1034
                    $summary    = '';
1035
                    $cols       = 3;
1036
                    if ( !empty($item) ) {
1037
                        $item_name  = $item->get_name();
1038
                        $summary    = $item->get_summary();
1039
                    }
1040
                    $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...
1041
                    
1042
                    $summary = apply_filters( 'wpinv_print_invoice_line_item_summary', $summary, $cart_item, $item, $invoice );
1043
                    
1044
                    $item_tax       = '';
1045
                    $tax_rate       = '';
1046
                    if ( $use_taxes && $cart_item['tax'] > 0 && $cart_item['subtotal'] > 0 ) {
1047
                        $item_tax = wpinv_price( wpinv_format_amount( $cart_item['tax'] ), $invoice->get_currency() );
1048
                        $tax_rate = !empty( $cart_item['vat_rate'] ) ? $cart_item['vat_rate'] : ( $cart_item['tax'] / $cart_item['subtotal'] ) * 100;
1049
                        $tax_rate = $tax_rate > 0 ? (float)wpinv_round_amount( $tax_rate, 4 ) : '';
1050
                        $tax_rate = $tax_rate != '' ? ' <small class="tax-rate">(' . $tax_rate . '%)</small>' : '';
1051
                    }
1052
                    
1053
                    $line_item_tax = $item_tax . $tax_rate;
1054
                    
1055
                    if ( $line_item_tax === '' ) {
1056
                        $line_item_tax = 0; // Zero tax
1057
                    }
1058
                    
1059
                    $line_item = '<tr class="row-' . ( ($count % 2 == 0) ? 'even' : 'odd' ) . ' wpinv-item">';
1060
                        $line_item .= '<td class="name">' . esc_html__( $item_name, 'invoicing' ) . wpinv_get_item_suffix( $item );
1061
                        if ( $summary !== '' ) {
1062
                            $line_item .= '<br/><small class="meta">' . wpautop( wp_kses_post( $summary ) ) . '</small>';
1063
                        }
1064
                        $line_item .= '</td>';
1065
                        
1066
                        $line_item .= '<td class="rate">' . esc_html__( wpinv_price( wpinv_format_amount( $item_price ), $invoice->get_currency() ) ) . '</td>';
1067
                        if ($quantities_enabled) {
1068
                            $cols++;
1069
                            $line_item .= '<td class="qty">' . $quantity . '</td>';
1070
                        }
1071
                        if ($use_taxes && !$zero_tax) {
1072
                            $cols++;
1073
                            $line_item .= '<td class="tax">' . $line_item_tax . '</td>';
1074
                        }
1075
                        $line_item .= '<td class="total">' . esc_html__( wpinv_price( wpinv_format_amount( $line_total ), $invoice->get_currency() ) ) . '</td>';
1076
                    $line_item .= '</tr>';
1077
                    
1078
                    echo apply_filters( 'wpinv_display_line_item', $line_item, $cart_item, $invoice, $cols );
1079
1080
                    $count++;
1081
                }
1082
                
1083
                do_action( 'wpinv_display_before_subtotal', $invoice, $cols );
1084
                ?>
1085
                <tr class="row-sub-total row_odd">
1086
                    <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php echo apply_filters( 'wpinv_print_cart_subtotal_label', '<strong>' . __( 'Sub Total', 'invoicing' ) . ':</strong>', $invoice ); ?></td>
1087
                    <td class="total"><strong><?php _e( wpinv_subtotal( $invoice_id, true ) ) ?></strong></td>
1088
                </tr>
1089
                <?php
1090
                do_action( 'wpinv_display_after_subtotal', $invoice, $cols );
1091
                
1092 View Code Duplication
                if ( wpinv_discount( $invoice_id, false ) > 0 ) {
1093
                    do_action( 'wpinv_display_before_discount', $invoice, $cols );
1094
                    ?>
1095
                        <tr class="row-discount">
1096
                            <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php wpinv_get_discount_label( wpinv_discount_code( $invoice_id ) ); ?>:</td>
1097
                            <td class="total"><?php echo wpinv_discount( $invoice_id, true, true ); ?></td>
1098
                        </tr>
1099
                    <?php
1100
                    do_action( 'wpinv_display_after_discount', $invoice, $cols );
1101
                }
1102
                
1103
                if ( $use_taxes ) {
1104
                    do_action( 'wpinv_display_before_tax', $invoice, $cols );
1105
                    ?>
1106
                    <tr class="row-tax">
1107
                        <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php echo apply_filters( 'wpinv_print_cart_tax_label', '<strong>' . $tax_label . ':</strong>', $invoice ); ?></td>
1108
                        <td class="total"><?php _e( wpinv_tax( $invoice_id, true ) ) ?></td>
1109
                    </tr>
1110
                    <?php
1111
                    do_action( 'wpinv_display_after_tax', $invoice, $cols );
1112
                }
1113
                
1114
                do_action( 'wpinv_display_before_total', $invoice, $cols );
1115
                ?>
1116
                <tr class="table-active row-total">
1117
                    <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php echo apply_filters( 'wpinv_print_cart_total_label', '<strong>' . __( 'Total', 'invoicing' ) . ':</strong>', $invoice ); ?></td>
1118
                    <td class="total"><strong><?php _e( wpinv_payment_total( $invoice_id, true ) ) ?></strong></td>
1119
                </tr>
1120
                <?php
1121
                do_action( 'wpinv_display_after_total', $invoice, $cols );
1122
                
1123
                do_action( 'wpinv_display_line_end', $invoice, $cols );
1124
            }
1125
        ?>
1126
        </tbody>
1127
    </table>
1128
    <?php
1129
    echo ob_get_clean();
1130
}
1131
1132
function wpinv_display_invoice_totals( $invoice_id = 0 ) {    
1133
    $use_taxes = wpinv_use_taxes();
1134
    
1135
    do_action( 'wpinv_before_display_totals_table', $invoice_id ); 
1136
    ?>
1137
    <table class="table table-sm table-bordered table-responsive">
1138
        <tbody>
1139
            <?php do_action( 'wpinv_before_display_totals' ); ?>
1140
            <tr class="row-sub-total">
1141
                <td class="rate"><strong><?php _e( 'Sub Total', 'invoicing' ); ?></strong></td>
1142
                <td class="total"><strong><?php _e( wpinv_subtotal( $invoice_id, true ) ) ?></strong></td>
1143
            </tr>
1144
            <?php do_action( 'wpinv_after_display_totals' ); ?>
1145
            <?php if ( wpinv_discount( $invoice_id, false ) > 0 ) { ?>
1146
                <tr class="row-discount">
1147
                    <td class="rate"><?php wpinv_get_discount_label( wpinv_discount_code( $invoice_id ) ); ?></td>
1148
                    <td class="total"><?php echo wpinv_discount( $invoice_id, true, true ); ?></td>
1149
                </tr>
1150
            <?php do_action( 'wpinv_after_display_discount' ); ?>
1151
            <?php } ?>
1152
            <?php if ( $use_taxes ) { ?>
1153
            <tr class="row-tax">
1154
                <td class="rate"><?php _e( 'Tax', 'invoicing' ); ?></td>
1155
                <td class="total"><?php _e( wpinv_tax( $invoice_id, true ) ) ?></td>
1156
            </tr>
1157
            <?php do_action( 'wpinv_after_display_tax' ); ?>
1158
            <?php } ?>
1159
            <?php if ( $fees = wpinv_get_fees( $invoice_id ) ) { ?>
1160
                <?php foreach ( $fees as $fee ) { ?>
1161
                    <tr class="row-fee">
1162
                        <td class="rate"><?php echo $fee['label']; ?></td>
1163
                        <td class="total"><?php echo $fee['amount_display']; ?></td>
1164
                    </tr>
1165
                <?php } ?>
1166
            <?php } ?>
1167
            <tr class="table-active row-total">
1168
                <td class="rate"><strong><?php _e( 'Total', 'invoicing' ) ?></strong></td>
1169
                <td class="total"><strong><?php _e( wpinv_payment_total( $invoice_id, true ) ) ?></strong></td>
1170
            </tr>
1171
            <?php do_action( 'wpinv_after_totals' ); ?>
1172
        </tbody>
1173
1174
    </table>
1175
1176
    <?php do_action( 'wpinv_after_totals_table' );
1177
}
1178
1179
function wpinv_display_payments_info( $invoice_id = 0, $echo = true ) {
1180
    $invoice = wpinv_get_invoice( $invoice_id );
1181
    
1182
    ob_start();
1183
    do_action( 'wpinv_before_display_payments_info', $invoice_id );
1184
    if ( ( $gateway_title = $invoice->get_gateway_title() ) || $invoice->is_paid() || $invoice->is_refunded() ) {
1185
        ?>
1186
        <div class="wpi-payment-info">
1187
            <p class="wpi-payment-gateway"><?php echo wp_sprintf( __( 'Payment via %s', 'invoicing' ), $gateway_title ? $gateway_title : __( 'Manually', 'invoicing' ) ); ?></p>
1188
            <?php if ( $gateway_title ) { ?>
1189
            <p class="wpi-payment-transid"><?php echo wp_sprintf( __( 'Transaction ID: %s', 'invoicing' ), $invoice->get_transaction_id() ); ?></p>
1190
            <?php } ?>
1191
        </div>
1192
        <?php
1193
    }
1194
    do_action( 'wpinv_after_display_payments_info', $invoice_id );
1195
    $outout = ob_get_clean();
1196
    
1197
    if ( $echo ) {
1198
        echo $outout;
1199
    } else {
1200
        return $outout;
1201
    }
1202
}
1203
1204
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...
1205
    wp_register_style( 'wpinv-single-style', WPINV_PLUGIN_URL . 'assets/css/invoice.css', array(), WPINV_VERSION );
1206
    
1207
    wp_print_styles( 'open-sans' );
1208
    wp_print_styles( 'wpinv-single-style' );
1209
}
1210
add_action( 'wpinv_invoice_print_head', 'wpinv_display_style' );
1211
add_action( 'wpinv_invalid_invoice_head', 'wpinv_display_style' );
1212
1213
function wpinv_checkout_billing_details() {  
1214
    $invoice_id = (int)wpinv_get_invoice_cart_id();
1215
    if (empty($invoice_id)) {
1216
        wpinv_error_log( 'Invoice id not found', 'ERROR', __FILE__, __LINE__ );
1217
        return null;
1218
    }
1219
    
1220
    $invoice = wpinv_get_invoice_cart( $invoice_id );
1221
    if (empty($invoice)) {
1222
        wpinv_error_log( 'Invoice not found', 'ERROR', __FILE__, __LINE__ );
1223
        return null;
1224
    }
1225
    $user_id        = $invoice->get_user_id();
1226
    $user_info      = $invoice->get_user_info();
1227
    $address_info   = wpinv_get_user_address( $user_id );
1228
    
1229 View Code Duplication
    if ( empty( $user_info['first_name'] ) && !empty( $user_info['first_name'] ) ) {
1230
        $user_info['first_name'] = $user_info['first_name'];
1231
        $user_info['last_name'] = $user_info['last_name'];
1232
    }
1233
    
1234
    if ( ( ( empty( $user_info['country'] ) && !empty( $address_info['country'] ) ) || ( empty( $user_info['state'] ) && !empty( $address_info['state'] ) && $user_info['country'] == $address_info['country'] ) ) ) {
1235
        $user_info['country']   = $address_info['country'];
1236
        $user_info['state']     = $address_info['state'];
1237
        $user_info['city']      = $address_info['city'];
1238
        $user_info['zip']       = $address_info['zip'];
1239
    }
1240
    
1241
    $address_fields = array(
1242
        'user_id',
1243
        'company',
1244
        'vat_number',
1245
        'email',
1246
        'phone',
1247
        'address'
1248
    );
1249
    
1250
    foreach ( $address_fields as $field ) {
1251
        if ( empty( $user_info[$field] ) ) {
1252
            $user_info[$field] = $address_info[$field];
1253
        }
1254
    }
1255
    
1256
    return apply_filters( 'wpinv_checkout_billing_details', $user_info, $invoice );
1257
}
1258
1259
function wpinv_admin_get_line_items($invoice = array()) {
1260
    $item_quantities    = wpinv_item_quantities_enabled();
1261
    $use_taxes          = wpinv_use_taxes();
1262
    
1263
    if ( empty( $invoice ) ) {
1264
        return NULL;
1265
    }
1266
    
1267
    $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...
1268
    if ( empty( $cart_items ) ) {
1269
        return NULL;
1270
    }
1271
    ob_start();
1272
    
1273
    do_action( 'wpinv_admin_before_line_items', $cart_items, $invoice );
1274
    
1275
    $count = 0;
1276
    foreach ( $cart_items as $key => $cart_item ) {
1277
        $item_id    = $cart_item['id'];
1278
        $wpi_item   = $item_id > 0 ? new WPInv_Item( $item_id ) : NULL;
1279
        
1280
        if (empty($wpi_item)) {
1281
            continue;
1282
        }
1283
        
1284
        $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...
1285
        $quantity       = !empty( $cart_item['quantity'] ) && $cart_item['quantity'] > 0 ? $cart_item['quantity'] : 1;
1286
        $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...
1287
        $can_remove     = true;
1288
        
1289
        $summary = apply_filters( 'wpinv_admin_invoice_line_item_summary', '', $cart_item, $wpi_item, $invoice );
1290
        
1291
        $item_tax       = '';
1292
        $tax_rate       = '';
1293 View Code Duplication
        if ( $cart_item['tax'] > 0 && $cart_item['subtotal'] > 0 ) {
1294
            $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...
1295
            $tax_rate = !empty( $cart_item['vat_rate'] ) ? $cart_item['vat_rate'] : ( $cart_item['tax'] / $cart_item['subtotal'] ) * 100;
1296
            $tax_rate = $tax_rate > 0 ? (float)wpinv_round_amount( $tax_rate, 4 ) : '';
1297
            $tax_rate = $tax_rate != '' ? ' <span class="tax-rate">(' . $tax_rate . '%)</span>' : '';
1298
        }
1299
        $line_item_tax = $item_tax . $tax_rate;
1300
        
1301
        if ( $line_item_tax === '' ) {
1302
            $line_item_tax = 0; // Zero tax
1303
        }
1304
1305
        $line_item = '<tr class="item item-' . ( ($count % 2 == 0) ? 'even' : 'odd' ) . '" data-item-id="' . $item_id . '">';
1306
            $line_item .= '<td class="id">' . $item_id . '</td>';
1307
            $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 );
1308
            if ( $summary !== '' ) {
1309
                $line_item .= '<span class="meta">' . wpautop( wp_kses_post( $summary ) ) . '</span>';
1310
            }
1311
            $line_item .= '</td>';
1312
            $line_item .= '<td class="price">' . $item_price . '</td>';
1313
            
1314
            if ( $item_quantities ) {
1315
                if ( count( $cart_items ) == 1 && $quantity <= 1 ) {
1316
                    $can_remove = false;
1317
                }
1318
                $line_item .= '<td class="qty" data-quantity="' . $quantity . '">&nbsp;&times;&nbsp;' . $quantity . '</td>';
1319
            } else {
1320
                if ( count( $cart_items ) == 1 ) {
1321
                    $can_remove = false;
1322
                }
1323
            }
1324
            $line_item .= '<td class="total">' . $item_subtotal . '</td>';
1325
            
1326
            if ( $use_taxes ) {
1327
                $line_item .= '<td class="tax">' . $line_item_tax . '</td>';
1328
            }
1329
            $line_item .= '<td class="action">';
1330
            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...
1331
                $line_item .= '<i class="fa fa-remove wpinv-item-remove"></i>';
1332
            }
1333
            $line_item .= '</td>';
1334
        $line_item .= '</tr>';
1335
        
1336
        echo apply_filters( 'wpinv_admin_line_item', $line_item, $cart_item, $invoice );
1337
        
1338
        $count++;
1339
    } 
1340
    
1341
    do_action( 'wpinv_admin_after_line_items', $cart_items, $invoice );
1342
    
1343
    return ob_get_clean();
1344
}
1345
1346
function wpinv_checkout_form() {
1347
    global $wpi_checkout_id;
1348
    
1349
    // Set current invoice id.
1350
    $wpi_checkout_id = wpinv_get_invoice_cart_id();
1351
    
1352
    $form_action  = esc_url( wpinv_get_checkout_uri() );
1353
1354
    ob_start();
1355
        echo '<div id="wpinv_checkout_wrap">';
1356
        
1357
        if ( wpinv_get_cart_contents() || wpinv_cart_has_fees() ) {
1358
            ?>
1359
            <div id="wpinv_checkout_form_wrap" class="wpinv_clearfix table-responsive">
1360
                <?php do_action( 'wpinv_before_checkout_form' ); ?>
1361
                <form id="wpinv_checkout_form" class="wpi-form" action="<?php echo $form_action; ?>" method="POST">
1362
                    <?php
1363
                    do_action( 'wpinv_checkout_form_top' );
1364
                    do_action( 'wpinv_checkout_billing_info' );
1365
                    do_action( 'wpinv_checkout_cart' );
1366
                    do_action( 'wpinv_payment_mode_select'  );
1367
                    do_action( 'wpinv_checkout_form_bottom' )
1368
                    ?>
1369
                </form>
1370
                <?php do_action( 'wpinv_after_purchase_form' ); ?>
1371
            </div><!--end #wpinv_checkout_form_wrap-->
1372
        <?php
1373
        } else {
1374
            do_action( 'wpinv_cart_empty' );
1375
        }
1376
        echo '</div><!--end #wpinv_checkout_wrap-->';
1377
    return ob_get_clean();
1378
}
1379
1380
function wpinv_checkout_cart( $cart_details = array(), $echo = true ) {
1381
    global $ajax_cart_details;
1382
    $ajax_cart_details = $cart_details;
1383
    /*
1384
    // Check if the Update cart button should be shown
1385
    if( wpinv_item_quantities_enabled() ) {
1386
        add_action( 'wpinv_cart_footer_buttons', 'wpinv_update_cart_button' );
1387
    }
1388
    
1389
    // Check if the Save Cart button should be shown
1390
    if( !wpinv_is_cart_saving_disabled() ) {
1391
        add_action( 'wpinv_cart_footer_buttons', 'wpinv_save_cart_button' );
1392
    }
1393
    */
1394
    ob_start();
1395
    do_action( 'wpinv_before_checkout_cart' );
1396
    echo '<div id="wpinv_checkout_cart_form" method="post">';
1397
        echo '<div id="wpinv_checkout_cart_wrap">';
1398
            wpinv_get_template_part( 'wpinv-checkout-cart' );
1399
        echo '</div>';
1400
    echo '</div>';
1401
    do_action( 'wpinv_after_checkout_cart' );
1402
    $content = ob_get_clean();
1403
    
1404
    if ( $echo ) {
1405
        echo $content;
1406
    } else {
1407
        return $content;
1408
    }
1409
}
1410
add_action( 'wpinv_checkout_cart', 'wpinv_checkout_cart', 10 );
1411
1412
function wpinv_empty_cart_message() {
1413
	return apply_filters( 'wpinv_empty_cart_message', '<span class="wpinv_empty_cart">' . __( 'Your cart is empty.', 'invoicing' ) . '</span>' );
1414
}
1415
1416
/**
1417
 * Echoes the Empty Cart Message
1418
 *
1419
 * @since 1.0
1420
 * @return void
1421
 */
1422
function wpinv_empty_checkout_cart() {
1423
	echo wpinv_empty_cart_message();
1424
}
1425
add_action( 'wpinv_cart_empty', 'wpinv_empty_checkout_cart' );
1426
1427
function wpinv_save_cart_button() {
1428
    if ( wpinv_is_cart_saving_disabled() )
1429
        return;
1430
?>
1431
    <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>
1432
<?php
1433
}
1434
1435
function wpinv_update_cart_button() {
1436
    if ( !wpinv_item_quantities_enabled() )
1437
        return;
1438
?>
1439
    <input type="submit" name="wpinv_update_cart_submit" class="wpinv-submit wpinv-no-js button" value="<?php _e( 'Update Cart', 'invoicing' ); ?>"/>
1440
    <input type="hidden" name="wpi_action" value="update_cart"/>
1441
<?php
1442
}
1443
1444
function wpinv_checkout_cart_columns() {
1445
    $default = 3;
1446
    if ( wpinv_item_quantities_enabled() ) {
1447
        $default++;
1448
    }
1449
    
1450
    if ( wpinv_use_taxes() ) {
1451
        $default++;
1452
    }
1453
1454
    return apply_filters( 'wpinv_checkout_cart_columns', $default );
1455
}
1456
1457
function wpinv_display_cart_messages() {
1458
    global $wpi_session;
1459
1460
    $messages = $wpi_session->get( 'wpinv_cart_messages' );
1461
1462
    if ( $messages ) {
1463
        foreach ( $messages as $message_id => $message ) {
1464
            // Try and detect what type of message this is
1465
            if ( strpos( strtolower( $message ), 'error' ) ) {
1466
                $type = 'error';
1467
            } elseif ( strpos( strtolower( $message ), 'success' ) ) {
1468
                $type = 'success';
1469
            } else {
1470
                $type = 'info';
1471
            }
1472
1473
            $classes = apply_filters( 'wpinv_' . $type . '_class', array( 'wpinv_errors', 'wpinv-alert', 'wpinv-alert-' . $type ) );
1474
1475
            echo '<div class="' . implode( ' ', $classes ) . '">';
1476
                // Loop message codes and display messages
1477
                    echo '<p class="wpinv_error" id="wpinv_msg_' . $message_id . '">' . $message . '</p>';
1478
            echo '</div>';
1479
        }
1480
1481
        // Remove all of the cart saving messages
1482
        $wpi_session->set( 'wpinv_cart_messages', null );
1483
    }
1484
}
1485
add_action( 'wpinv_before_checkout_cart', 'wpinv_display_cart_messages' );
1486
1487
function wpinv_discount_field() {
1488
    if ( isset( $_GET['wpi-gateway'] ) && wpinv_is_ajax_disabled() ) {
1489
        return; // Only show before a payment method has been selected if ajax is disabled
1490
    }
1491
1492
    if ( !wpinv_is_checkout() ) {
1493
        return;
1494
    }
1495
1496
    if ( wpinv_has_active_discounts() && wpinv_get_cart_total() ) {
1497
    ?>
1498
    <div id="wpinv-discount-field" class="panel panel-default">
1499
        <div class="panel-body">
1500
            <p>
1501
                <label class="wpinv-label" for="wpinv_discount_code"><strong><?php _e( 'Discount', 'invoicing' ); ?></strong></label>
1502
                <span class="wpinv-description"><?php _e( 'Enter a discount code if you have one.', 'invoicing' ); ?></span>
1503
            </p>
1504
            <div class="form-group row">
1505
                <div class="col-sm-4">
1506
                    <input class="wpinv-input form-control" type="text" id="wpinv_discount_code" name="wpinv_discount_code" placeholder="<?php _e( 'Enter discount code', 'invoicing' ); ?>"/>
1507
                </div>
1508
                <div class="col-sm-3">
1509
                    <button id="wpi-apply-discount" type="button" class="btn btn-success btn-sm"><?php _e( 'Apply Discount', 'invoicing' ); ?></button>
1510
                </div>
1511
                <div style="clear:both"></div>
1512
                <div class="col-sm-12 wpinv-discount-msg">
1513
                    <div class="alert alert-success"><i class="fa fa-check-circle"></i><span class="wpi-msg"></span></div>
1514
                    <div class="alert alert-error"><i class="fa fa-warning"></i><span class="wpi-msg"></span></div>
1515
                </div>
1516
            </div>
1517
        </div>
1518
    </div>
1519
<?php
1520
    }
1521
}
1522
add_action( 'wpinv_after_checkout_cart', 'wpinv_discount_field', -10 );
1523
1524
function wpinv_agree_to_terms_js() {
1525
    if ( wpinv_get_option( 'show_agree_to_terms', false ) ) {
1526
?>
1527
<script type="text/javascript">
1528
    jQuery(document).ready(function($){
1529
        $( document.body ).on('click', '.wpinv_terms_links', function(e) {
1530
            //e.preventDefault();
1531
            $('#wpinv_terms').slideToggle();
1532
            $('.wpinv_terms_links').toggle();
1533
            return false;
1534
        });
1535
    });
1536
</script>
1537
<?php
1538
    }
1539
}
1540
add_action( 'wpinv_checkout_form_top', 'wpinv_agree_to_terms_js' );
1541
1542
function wpinv_payment_mode_select() {
1543
    $gateways = wpinv_get_enabled_payment_gateways( true );
1544
    $gateways = apply_filters( 'wpinv_payment_gateways_on_cart', $gateways );
1545
    $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...
1546
    $invoice = wpinv_get_invoice( 0, true );
1547
    
1548
    do_action('wpinv_payment_mode_top');
1549
    $invoice_id = (int)$invoice->ID;
1550
    $chosen_gateway = wpinv_get_chosen_gateway( $invoice_id );
1551
    ?>
1552
    <div id="wpinv_payment_mode_select" data-gateway="<?php echo $chosen_gateway; ?>" <?php echo ( $invoice->is_free() ? 'style="display:none;"' : '' ); ?>>
1553
            <?php do_action( 'wpinv_payment_mode_before_gateways_wrap' ); ?>
1554
            <div id="wpinv-payment-mode-wrap" class="panel panel-default">
1555
                <div class="panel-heading"><h3 class="panel-title"><?php _e( 'Select Payment Method', 'invoicing' ); ?></h3></div>
1556
                <div class="panel-body list-group wpi-payment_methods">
1557
                    <?php
1558
                    do_action( 'wpinv_payment_mode_before_gateways' );
1559
                    
1560
                    if(!empty($gateways)){
1561
	                    foreach ( $gateways as $gateway_id => $gateway ) {
1562
		                    $checked = checked( $gateway_id, $chosen_gateway, false );
1563
		                    $button_label = wpinv_get_gateway_button_label( $gateway_id );
1564
		                    $description = wpinv_get_gateway_description( $gateway_id );
1565
		                    ?>
1566
		                    <div class="list-group-item">
1567
			                    <div class="radio">
1568
				                    <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>
1569
			                    </div>
1570
			                    <div style="display:none;" class="payment_box wpi_gateway_<?php echo esc_attr( $gateway_id );?>" role="alert">
1571
				                    <?php if ( !empty( $description ) ) { ?>
1572
					                    <div class="wpi-gateway-desc alert alert-info"><?php echo $description;?></div>
1573
				                    <?php } ?>
1574
				                    <?php do_action( 'wpinv_' . $gateway_id . '_cc_form', $invoice_id ) ;?>
1575
			                    </div>
1576
		                    </div>
1577
		                    <?php
1578
	                    }
1579
                    }else{
1580
	                    echo '<div class="alert alert-warning">'. __('No payment gateway active','invoicing') .'</div>';
1581
                    }
1582
1583
                    do_action( 'wpinv_payment_mode_after_gateways' );
1584
                    ?>
1585
                </div>
1586
            </div>
1587
            <?php do_action( 'wpinv_payment_mode_after_gateways_wrap' ); ?>
1588
    </div>
1589
    <?php
1590
    do_action('wpinv_payment_mode_bottom');
1591
}
1592
add_action( 'wpinv_payment_mode_select', 'wpinv_payment_mode_select' );
1593
1594
function wpinv_checkout_billing_info() {    
1595
    if ( wpinv_is_checkout() ) {
1596
        $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...
1597
        $billing_details    = wpinv_checkout_billing_details();
1598
        $selected_country   = !empty( $billing_details['country'] ) ? $billing_details['country'] : wpinv_default_billing_country();
1599
        ?>
1600
        <div id="wpinv-fields" class="clearfix">
1601
            <div id="wpi-billing" class="wpi-billing clearfix panel panel-default">
1602
                <div class="panel-heading"><h3 class="panel-title"><?php _e( 'Billing Details', 'invoicing' );?></h3></div>
1603
                <div id="wpinv-fields-box" class="panel-body">
1604
                    <?php do_action( 'wpinv_checkout_billing_fields_first', $billing_details ); ?>
1605
                    <p class="wpi-cart-field wpi-col2 wpi-colf">
1606
                        <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>
1607
                        <?php
1608
                        echo wpinv_html_text( array(
1609
                                'id'            => 'wpinv_first_name',
1610
                                'name'          => 'wpinv_first_name',
1611
                                'value'         => $billing_details['first_name'],
1612
                                'class'         => 'wpi-input form-control',
1613
                                'placeholder'   => __( 'First name', 'invoicing' ),
1614
                                'required'      => (bool)wpinv_get_option( 'fname_mandatory' ),
1615
                            ) );
1616
                        ?>
1617
                    </p>
1618
                    <p class="wpi-cart-field wpi-col2 wpi-coll">
1619
                        <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>
1620
                        <?php
1621
                        echo wpinv_html_text( array(
1622
                                'id'            => 'wpinv_last_name',
1623
                                'name'          => 'wpinv_last_name',
1624
                                'value'         => $billing_details['last_name'],
1625
                                'class'         => 'wpi-input form-control',
1626
                                'placeholder'   => __( 'Last name', 'invoicing' ),
1627
                                'required'      => (bool)wpinv_get_option( 'lname_mandatory' ),
1628
                            ) );
1629
                        ?>
1630
                    </p>
1631
                    <p class="wpi-cart-field wpi-col2 wpi-colf">
1632
                        <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>
1633
                        <?php
1634
                        echo wpinv_html_text( array(
1635
                                'id'            => 'wpinv_address',
1636
                                'name'          => 'wpinv_address',
1637
                                'value'         => $billing_details['address'],
1638
                                'class'         => 'wpi-input form-control',
1639
                                'placeholder'   => __( 'Address', 'invoicing' ),
1640
                                'required'      => (bool)wpinv_get_option( 'address_mandatory' ),
1641
                            ) );
1642
                        ?>
1643
                    </p>
1644
                    <p class="wpi-cart-field wpi-col2 wpi-coll">
1645
                        <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>
1646
                        <?php
1647
                        echo wpinv_html_text( array(
1648
                                'id'            => 'wpinv_city',
1649
                                'name'          => 'wpinv_city',
1650
                                'value'         => $billing_details['city'],
1651
                                'class'         => 'wpi-input form-control',
1652
                                'placeholder'   => __( 'City', 'invoicing' ),
1653
                                'required'      => (bool)wpinv_get_option( 'city_mandatory' ),
1654
                            ) );
1655
                        ?>
1656
                    </p>
1657
                    <p id="wpinv_country_box" class="wpi-cart-field wpi-col2 wpi-colf">
1658
                        <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>
1659
                        <?php echo wpinv_html_select( array(
1660
                            'options'          => wpinv_get_country_list(),
1661
                            'name'             => 'wpinv_country',
1662
                            'id'               => 'wpinv_country',
1663
                            'selected'         => $selected_country,
1664
                            'show_option_all'  => false,
1665
                            'show_option_none' => false,
1666
                            'class'            => 'wpi-input form-control',
1667
                            'placeholder'      => __( 'Choose a country', 'invoicing' ),
1668
                            'required'         => (bool)wpinv_get_option( 'country_mandatory' ),
1669
                        ) ); ?>
1670
                    </p>
1671
                    <p id="wpinv_state_box" class="wpi-cart-field wpi-col2 wpi-coll">
1672
                        <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>
1673
                        <?php
1674
                        $states = wpinv_get_country_states( $selected_country );
1675
                        if( !empty( $states ) ) {
1676
                            echo wpinv_html_select( array(
1677
                                'options'          => $states,
1678
                                'name'             => 'wpinv_state',
1679
                                'id'               => 'wpinv_state',
1680
                                'selected'         => $billing_details['state'],
1681
                                'show_option_all'  => false,
1682
                                'show_option_none' => false,
1683
                                'class'            => 'wpi-input form-control',
1684
                                'placeholder'      => __( 'Choose a state', 'invoicing' ),
1685
                                'required'         => (bool)wpinv_get_option( 'state_mandatory' ),
1686
                            ) );
1687
                        } else {
1688
                            echo wpinv_html_text( array(
1689
                                'name'          => 'wpinv_state',
1690
                                'value'         => $billing_details['state'],
1691
                                'id'            => 'wpinv_state',
1692
                                'class'         => 'wpi-input form-control',
1693
                                'placeholder'   => __( 'State / Province', 'invoicing' ),
1694
                                'required'      => (bool)wpinv_get_option( 'state_mandatory' ),
1695
                            ) );
1696
                        }
1697
                        ?>
1698
                    </p>
1699
                    <p class="wpi-cart-field wpi-col2 wpi-colf">
1700
                        <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>
1701
                        <?php
1702
                        echo wpinv_html_text( array(
1703
                                'name'          => 'wpinv_zip',
1704
                                'value'         => $billing_details['zip'],
1705
                                'id'            => 'wpinv_zip',
1706
                                'class'         => 'wpi-input form-control',
1707
                                'placeholder'   => __( 'ZIP / Postcode', 'invoicing' ),
1708
                                'required'      => (bool)wpinv_get_option( 'zip_mandatory' ),
1709
                            ) );
1710
                        ?>
1711
                    </p>
1712
                    <p class="wpi-cart-field wpi-col2 wpi-coll">
1713
                        <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>
1714
                        <?php
1715
                        echo wpinv_html_text( array(
1716
                                'id'            => 'wpinv_phone',
1717
                                'name'          => 'wpinv_phone',
1718
                                'value'         => $billing_details['phone'],
1719
                                'class'         => 'wpi-input form-control',
1720
                                'placeholder'   => __( 'Phone', 'invoicing' ),
1721
                                'required'      => (bool)wpinv_get_option( 'phone_mandatory' ),
1722
                            ) );
1723
                        ?>
1724
                    </p>
1725
                    <?php do_action( 'wpinv_checkout_billing_fields_last', $billing_details ); ?>
1726
                    <div class="clearfix"></div>
1727
                </div>
1728
            </div>
1729
            <?php do_action( 'wpinv_after_billing_fields', $billing_details ); ?>
1730
        </div>
1731
        <?php
1732
    }
1733
}
1734
add_action( 'wpinv_checkout_billing_info', 'wpinv_checkout_billing_info' );
1735
1736
function wpinv_checkout_hidden_fields() {
1737
?>
1738
    <?php if ( is_user_logged_in() ) { ?>
1739
    <input type="hidden" name="wpinv_user_id" value="<?php echo get_current_user_id(); ?>"/>
1740
    <?php } ?>
1741
    <input type="hidden" name="wpi_action" value="payment" />
1742
<?php
1743
}
1744
1745
function wpinv_checkout_button_purchase() {
1746
    ob_start();
1747
?>
1748
    <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' ) ?>"/>
1749
<?php
1750
    return apply_filters( 'wpinv_checkout_button_purchase', ob_get_clean() );
1751
}
1752
1753
function wpinv_checkout_total() {
1754
    global $cart_total;
1755
?>
1756
<div id="wpinv_checkout_total" class="panel panel-info">
1757
    <div class="panel-body">
1758
    <?php
1759
    do_action( 'wpinv_purchase_form_before_checkout_total' );
1760
    ?>
1761
    <strong><?php _e( 'Invoice Total:', 'invoicing' ) ?></strong> <span class="wpinv-chdeckout-total"><?php echo $cart_total;?></span>
1762
    <?php
1763
    do_action( 'wpinv_purchase_form_after_checkout_total' );
1764
    ?>
1765
    </div>
1766
</div>
1767
<?php
1768
}
1769
add_action( 'wpinv_checkout_form_bottom', 'wpinv_checkout_total', 9998 );
1770
1771
function wpinv_checkout_submit() {
1772
?>
1773
<div id="wpinv_purchase_submit" class="panel panel-success">
1774
    <div class="panel-body text-center">
1775
    <?php
1776
    do_action( 'wpinv_purchase_form_before_submit' );
1777
    wpinv_checkout_hidden_fields();
1778
    echo wpinv_checkout_button_purchase();
1779
    do_action( 'wpinv_purchase_form_after_submit' );
1780
    ?>
1781
    </div>
1782
</div>
1783
<?php
1784
}
1785
add_action( 'wpinv_checkout_form_bottom', 'wpinv_checkout_submit', 9999 );
1786
1787
function wpinv_receipt_billing_address( $invoice_id = 0 ) {
1788
    $invoice = wpinv_get_invoice( $invoice_id );
1789
    
1790
    if ( empty( $invoice ) ) {
1791
        return NULL;
1792
    }
1793
    
1794
    $billing_details = $invoice->get_user_info();
1795
    $address_row = '';
1796
    if ( $address = $billing_details['address'] ) {
1797
        $address_row .= wpautop( wp_kses_post( $address ) );
1798
    }
1799
    
1800
    $address_fields = array();
1801
    if ( !empty( $billing_details['city'] ) ) {
1802
        $address_fields[] = $billing_details['city'];
1803
    }
1804
    
1805
    $billing_country = !empty( $billing_details['country'] ) ? $billing_details['country'] : '';
1806
    if ( !empty( $billing_details['state'] ) ) {
1807
        $address_fields[] = wpinv_state_name( $billing_details['state'], $billing_country );
1808
    }
1809
    
1810
    if ( !empty( $billing_country ) ) {
1811
        $address_fields[] = wpinv_country_name( $billing_country );
1812
    }
1813
    
1814 View Code Duplication
    if ( !empty( $address_fields ) ) {
1815
        $address_fields = implode( ", ", $address_fields );
1816
        
1817
        if ( !empty( $billing_details['zip'] ) ) {
1818
            $address_fields .= ' ' . $billing_details['zip'];
1819
        }
1820
        
1821
        $address_row .= wpautop( wp_kses_post( $address_fields ) );
1822
    }
1823
    ob_start();
1824
    ?>
1825
    <table class="table table-bordered table-sm wpi-billing-details">
1826
        <tbody>
1827
            <tr class="wpi-receipt-name">
1828
                <th class="text-left"><?php _e( 'Name', 'invoicing' ); ?></th>
1829
                <td><?php echo esc_html( trim( $billing_details['first_name'] . ' ' . $billing_details['last_name'] ) ) ;?></td>
1830
            </tr>
1831
            <tr class="wpi-receipt-email">
1832
                <th class="text-left"><?php _e( 'Email', 'invoicing' ); ?></th>
1833
                <td><?php echo $billing_details['email'] ;?></td>
1834
            </tr>
1835 View Code Duplication
            <?php if ( $billing_details['company'] ) { ?>
1836
            <tr class="wpi-receipt-company">
1837
                <th class="text-left"><?php _e( 'Company', 'invoicing' ); ?></th>
1838
                <td><?php echo esc_html( $billing_details['company'] ) ;?></td>
1839
            </tr>
1840
            <?php } ?>
1841
            <tr class="wpi-receipt-address">
1842
                <th class="text-left"><?php _e( 'Address', 'invoicing' ); ?></th>
1843
                <td><?php echo $address_row ;?></td>
1844
            </tr>
1845 View Code Duplication
            <?php if ( $billing_details['phone'] ) { ?>
1846
            <tr class="wpi-receipt-phone">
1847
                <th class="text-left"><?php _e( 'Phone', 'invoicing' ); ?></th>
1848
                <td><?php echo esc_html( $billing_details['phone'] ) ;?></td>
1849
            </tr>
1850
            <?php } ?>
1851
        </tbody>
1852
    </table>
1853
    <?php
1854
    $output = ob_get_clean();
1855
    
1856
    $output = apply_filters( 'wpinv_receipt_billing_address', $output, $invoice_id );
1857
1858
    echo $output;
1859
}
1860
1861
function wpinv_filter_success_page_content( $content ) {
1862
    if ( isset( $_GET['payment-confirm'] ) && wpinv_is_success_page() ) {
1863
        if ( has_filter( 'wpinv_payment_confirm_' . sanitize_text_field( $_GET['payment-confirm'] ) ) ) {
1864
            $content = apply_filters( 'wpinv_payment_confirm_' . sanitize_text_field( $_GET['payment-confirm'] ), $content );
1865
        }
1866
    }
1867
1868
    return $content;
1869
}
1870
add_filter( 'the_content', 'wpinv_filter_success_page_content', 99999 );
1871
1872
function wpinv_receipt_actions( $invoice ) {
1873
    if ( !empty( $invoice ) ) {
1874
        $actions = array();
1875
1876
        if ( wpinv_user_can_view_invoice( $invoice->ID ) ) {
1877
            $actions['print']   = array(
1878
                'url'  => $invoice->get_view_url( true ),
1879
                'name' => __( 'Print Invoice', 'invoicing' ),
1880
                'class' => 'btn-primary',
1881
            );
1882
        }
1883
1884
        if ( is_user_logged_in() ) {
1885
            $actions['history'] = array(
1886
                'url'  => wpinv_get_history_page_uri(),
1887
                'name' => __( 'Invoice History', 'invoicing' ),
1888
                'class' => 'btn-warning',
1889
            );
1890
        }
1891
1892
        $actions = apply_filters( 'wpinv_invoice_receipt_actions', $actions, $invoice );
1893
        
1894
        if ( !empty( $actions ) ) {
1895
        ?>
1896
        <div class="wpinv-receipt-actions text-right">
1897
            <?php foreach ( $actions as $key => $action ) { $class = !empty($action['class']) ? sanitize_html_class( $action['class'] ) : ''; ?>
1898
            <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>
1899
            <?php } ?>
1900
        </div>
1901
        <?php
1902
        }
1903
    }
1904
}
1905
add_action( 'wpinv_receipt_start', 'wpinv_receipt_actions', -10, 1 );
1906
1907
function wpinv_invoice_link( $invoice_id ) {
1908
    $invoice = wpinv_get_invoice( $invoice_id );
1909
    
1910
    if ( empty( $invoice ) ) {
1911
        return NULL;
1912
    }
1913
    
1914
    $invoice_link = '<a href="' . esc_url( $invoice->get_view_url() ) . '">' . $invoice->get_number() . '</a>';
1915
    
1916
    return apply_filters( 'wpinv_get_invoice_link', $invoice_link, $invoice );
1917
}
1918
1919
function wpinv_invoice_subscription_details( $invoice ) {
1920
    if ( !empty( $invoice ) && $invoice->is_recurring() && !wpinv_is_subscription_payment( $invoice ) ) {
1921
        $total_payments = (int)$invoice->get_total_payments();
1922
        $payments       = $invoice->get_child_payments();
1923
        
1924
        $subscription   = $invoice->get_subscription_data();
1925
        
1926
        if ( !( !empty( $subscription ) && !empty( $subscription['item_id'] ) ) ) {
1927
            return;
1928
        }
1929
        
1930
        $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() );
1931
        $times_billed   = $total_payments . ' / ' . ( ( (int)$subscription['bill_times'] == 0 ) ? __( 'Until cancelled', 'invoicing' ) : $subscription['bill_times'] );
1932
        
1933
        $subscription_status = $invoice->get_subscription_status();
1934
        
1935
        $status_desc = '';
1936
        if ( $subscription_status == 'trialing' && $trial_end_date = $invoice->get_trial_end_date() ) {
1937
            $status_desc = wp_sprintf( __( 'Until: %s', 'invoicing' ), $trial_end_date );
1938
        } else if ( $subscription_status == 'cancelled' && $cancelled_date = $invoice->get_cancelled_date() ) {
1939
            $status_desc = wp_sprintf( __( 'On: %s', 'invoicing' ), $cancelled_date );
1940
        }
1941
        $status_desc = $status_desc != '' ? '<span class="meta">' . $status_desc . '</span>' : '';
1942
        ?>
1943
        <div class="wpinv-subscriptions-details">
1944
            <h3 class="wpinv-subscriptions-t"><?php echo apply_filters( 'wpinv_subscription_details_title', __( 'Subscription Details', 'invoicing' ) ); ?></h3>
1945
            <table class="table">
1946
                <thead>
1947
                    <tr>
1948
                        <th><?php _e( 'Billing Cycle', 'invoicing' ) ;?></th>
1949
                        <th><?php _e( 'Start Date', 'invoicing' ) ;?></th>
1950
                        <th><?php _e( 'Expiration Date', 'invoicing' ) ;?></th>
1951
                        <th class="text-center"><?php _e( 'Times Billed', 'invoicing' ) ;?></th>
1952
                        <th class="text-center"><?php _e( 'Status', 'invoicing' ) ;?></th>
1953
                    </tr>
1954
                </thead>
1955
                <tbody>
1956
                    <tr>
1957
                        <td><?php echo $billing_cycle; ?></td>
1958
                        <td><?php echo $invoice->get_subscription_start(); ?></td>
1959
                        <td><?php echo $invoice->get_subscription_end(); ?></td>
1960
                        <td class="text-center"><?php echo $times_billed; ?></td>
1961
                        <td class="text-center wpi-sub-status"><?php echo $invoice->get_subscription_status_label() ;?>
1962
                        <?php echo $status_desc; ?>
1963
                        </td>
1964
                    </tr>
1965
                </tbody>
1966
            </table>
1967
        </div>
1968
        <?php if ( !empty( $payments ) ) { ?>
1969
        <div class="wpinv-renewal-payments">
1970
            <h3 class="wpinv-renewals-t"><?php echo apply_filters( 'wpinv_renewal_payments_title', __( 'Renewal Payments', 'invoicing' ) ); ?></h3>
1971
            <table class="table">
1972
                <thead>
1973
                    <tr>
1974
                        <th>#</th>
1975
                        <th><?php _e( 'Invoice', 'invoicing' ) ;?></th>
1976
                        <th><?php _e( 'Date', 'invoicing' ) ;?></th>
1977
                        <th class="text-right"><?php _e( 'Amount', 'invoicing' ) ;?></th>
1978
                    </tr>
1979
                </thead>
1980
                <tbody>
1981
                    <?php foreach ( $payments as $key => $invoice_id ) { ?>
1982
                    <tr>
1983
                        <th scope="row"><?php echo ( $key + 1 );?></th>
1984
                        <td><?php echo wpinv_invoice_link( $invoice_id ) ;?></td>
1985
                        <td><?php echo wpinv_get_invoice_date( $invoice_id ); ?></td>
1986
                        <td class="text-right"><?php echo wpinv_payment_total( $invoice_id, true ); ?></td>
1987
                    </tr>
1988
                    <?php } ?>
1989
                    <tr><td colspan="4" style="padding:0"></td></tr>
1990
                </tbody>
1991
            </table>
1992
        </div>
1993
        <?php } ?>
1994
        <?php
1995
    }
1996
}
1997
1998
function wpinv_cart_total_label( $label, $invoice ) {
1999
    if ( empty( $invoice ) ) {
2000
        return $label;
2001
    }
2002
    
2003
    $prefix_label = '';
2004
    if ( $invoice->is_parent() && $item_id = $invoice->get_recurring() ) {        
2005
        $prefix_label   = '<span class="label label-primary label-recurring">' . __( 'Recurring Payment', 'invoicing' ) . '</span> ' . wpinv_subscription_payment_desc( $invoice );
2006
    } else if ( $invoice->is_renewal() ) {
2007
        $prefix_label   = '<span class="label label-primary label-renewal">' . __( 'Renewal Payment', 'invoicing' ) . '</span> ';        
2008
    }
2009
    
2010
    if ( $prefix_label != '' ) {
2011
        $label  = '<span class="wpinv-cart-sub-desc">' . $prefix_label . '</span> ' . $label;
2012
    }
2013
    
2014
    return $label;
2015
}
2016
add_filter( 'wpinv_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
2017
add_filter( 'wpinv_email_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
2018
add_filter( 'wpinv_print_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
2019
2020
add_action( 'wpinv_invoice_print_middle', 'wpinv_invoice_subscription_details', 10, 1 );
2021
2022
function wpinv_invoice_print_description( $invoice ) {
2023
    if ( empty( $invoice ) ) {
2024
        return NULL;
2025
    }
2026
    if ( $description = wpinv_get_invoice_description( $invoice->ID ) ) {
2027
        ?>
2028
        <div class="row wpinv-lower">
2029
            <div class="col-sm-12 wpinv-description">
2030
                <?php echo wpautop( $description ); ?>
2031
            </div>
2032
        </div>
2033
        <?php
2034
    }
2035
}
2036
add_action( 'wpinv_invoice_print_middle', 'wpinv_invoice_print_description', 10.1, 1 );
2037
2038
function wpinv_invoice_print_payment_info( $invoice ) {
2039
    if ( empty( $invoice ) ) {
2040
        return NULL;
2041
    }
2042
    
2043
    if ( $payments_info = wpinv_display_payments_info( $invoice->ID, false ) ) {
2044
        ?>
2045
        <div class="row wpinv-payments">
2046
            <div class="col-sm-12">
2047
                <?php echo $payments_info; ?>
2048
            </div>
2049
        </div>
2050
        <?php 
2051
    }
2052
}
2053
// 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...
2054
2055
function wpinv_get_invoice_note_line_item( $note, $echo = true ) {
2056
    if ( empty( $note ) ) {
2057
        return NULL;
2058
    }
2059
    
2060
    if ( is_int( $note ) ) {
2061
        $note = get_comment( $note );
2062
    }
2063
    
2064
    if ( !( is_object( $note ) && is_a( $note, 'WP_Comment' ) ) ) {
2065
        return NULL;
2066
    }
2067
    
2068
    $note_classes   = array( 'note' );
2069
    $note_classes[] = get_comment_meta( $note->comment_ID, '_wpi_customer_note', true ) ? 'customer-note' : '';
2070
    $note_classes[] = $note->comment_author === __( 'System', 'invoicing' ) ? 'system-note' : '';
2071
    $note_classes   = apply_filters( 'wpinv_invoice_note_class', array_filter( $note_classes ), $note );
2072
    $note_classes   = !empty( $note_classes ) ? implode( ' ', $note_classes ) : '';
2073
    
2074
    ob_start();
2075
    ?>
2076
    <li rel="<?php echo absint( $note->comment_ID ) ; ?>" class="<?php echo esc_attr( $note_classes ); ?>">
2077
        <div class="note_content">
2078
            <?php echo wpautop( wptexturize( wp_kses_post( $note->comment_content ) ) ); ?>
2079
        </div>
2080
        <p class="meta">
2081
            <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;
2082
            <?php if($note->comment_author !== 'System') {?>
2083
                <a href="#" class="delete_note"><?php _e( 'Delete note', 'invoicing' ); ?></a>
2084
            <?php } ?>
2085
        </p>
2086
    </li>
2087
    <?php
2088
    $note_content = ob_get_clean();
2089
    $note_content = apply_filters( 'wpinv_get_invoice_note_line_item', $note_content, $note, $echo );
2090
    
2091
    if ( $echo ) {
2092
        echo $note_content;
2093
    } else {
2094
        return $note_content;
2095
    }
2096
}
2097
2098
function wpinv_invalid_invoice_content() {
2099
    global $post;
2100
2101
    $invoice = wpinv_get_invoice( $post->ID );
2102
2103
    $error = __( 'This invoice is only viewable by clicking on the invoice link that sent to you via email.', 'invoicing' );
2104
    if ( !empty( $invoice->ID ) && $invoice->has_status( array_keys( wpinv_get_invoice_statuses() ) ) ) {
2105
        if ( is_user_logged_in() ) {
2106 View Code Duplication
            if ( wpinv_require_login_to_checkout() ) {
2107
                if ( isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
2108
                    $error = __( 'You are not allowed to view this invoice.', 'invoicing' );
2109
                }
2110
            }
2111 View Code Duplication
        } else {
2112
            if ( wpinv_require_login_to_checkout() ) {
2113
                if ( isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
2114
                    $error = __( 'You must be logged in to view this invoice.', 'invoicing' );
2115
                }
2116
            }
2117
        }
2118
    } else {
2119
        $error = __( 'This invoice is deleted or does not exist.', 'invoicing' );
2120
    }
2121
    ?>
2122
    <div class="row wpinv-row-invalid">
2123
        <div class="col-md-6 col-md-offset-3 wpinv-message error">
2124
            <h3><?php _e( 'Access Denied', 'invoicing' ); ?></h3>
2125
            <p class="wpinv-msg-text"><?php echo $error; ?></p>
2126
        </div>
2127
    </div>
2128
    <?php
2129
}
2130
add_action( 'wpinv_invalid_invoice_content', 'wpinv_invalid_invoice_content' );