Passed
Pull Request — master (#116)
by
unknown
04:02
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
        <?php do_action( 'wpinv_display_details_after_due_date', $invoice_id ); ?>
905 View Code Duplication
        <?php if ( $owner_vat_number = $wpinv_euvat->get_vat_number() ) { ?>
906
            <tr class="wpi-row-ovatno">
907
                <th><?php echo apply_filters( 'wpinv_invoice_owner_vat_number_label', wp_sprintf( __( 'Owner %s Number', 'invoicing' ), $vat_name ), $invoice, $vat_name ); ?></th>
908
                <td><?php echo $owner_vat_number; ?></td>
909
            </tr>
910
        <?php } ?>
911 View Code Duplication
        <?php if ( $use_taxes && ( $user_vat_number = wpinv_get_invoice_vat_number( $invoice_id ) ) ) { ?>
912
            <tr class="wpi-row-uvatno">
913
                <th><?php echo apply_filters( 'wpinv_invoice_user_vat_number_label', wp_sprintf( __( 'Invoice %s Number', 'invoicing' ), $vat_name ), $invoice, $vat_name ); ?></th>
914
                <td><?php echo $user_vat_number; ?></td>
915
            </tr>
916
        <?php } ?>
917
        <tr class="table-active tr-total wpi-row-total">
918
            <th><strong><?php _e( 'Total Amount', 'invoicing' ) ?></strong></th>
919
            <td><strong><?php echo wpinv_payment_total( $invoice_id, true ); ?></strong></td>
920
        </tr>
921
    </table>
922
<?php
923
}
924
925
function wpinv_display_to_address( $invoice_id = 0 ) {
926
    $invoice = wpinv_get_invoice( $invoice_id );
927
    
928
    if ( empty( $invoice ) ) {
929
        return NULL;
930
    }
931
    
932
    $billing_details = $invoice->get_user_info();
933
    $output = '<div class="to col-xs-2"><strong>' . __( 'To:', 'invoicing' ) . '</strong></div>';
934
    $output .= '<div class="wrapper col-xs-10">';
935
    
936
    ob_start();
937
    do_action( 'wpinv_display_to_address_top', $invoice );
938
    $output .= ob_get_clean();
939
    
940
    $output .= '<div class="name">' . esc_html( trim( $billing_details['first_name'] . ' ' . $billing_details['last_name'] ) ) . '</div>';
941
    if ( $company = $billing_details['company'] ) {
942
        $output .= '<div class="company">' . wpautop( wp_kses_post( $company ) ) . '</div>';
943
    }
944
    $address_row = '';
945
    if ( $address = $billing_details['address'] ) {
946
        $address_row .= wpautop( wp_kses_post( $address ) );
947
    }
948
    
949
    $address_fields = array();
950
    if ( !empty( $billing_details['city'] ) ) {
951
        $address_fields[] = $billing_details['city'];
952
    }
953
    
954
    $billing_country = !empty( $billing_details['country'] ) ? $billing_details['country'] : '';
955
    if ( !empty( $billing_details['state'] ) ) {
956
        $address_fields[] = wpinv_state_name( $billing_details['state'], $billing_country );
957
    }
958
    
959
    if ( !empty( $billing_country ) ) {
960
        $address_fields[] = wpinv_country_name( $billing_country );
961
    }
962
    
963 View Code Duplication
    if ( !empty( $address_fields ) ) {
964
        $address_fields = implode( ", ", $address_fields );
965
        
966
        if ( !empty( $billing_details['zip'] ) ) {
967
            $address_fields .= ' ' . $billing_details['zip'];
968
        }
969
        
970
        $address_row .= wpautop( wp_kses_post( $address_fields ) );
971
    }
972
    
973
    if ( $address_row ) {
974
        $output .= '<div class="address">' . $address_row . '</div>';
975
    }
976
    
977 View Code Duplication
    if ( $phone = $invoice->get_phone() ) {
978
        $output .= '<div class="phone">' . wp_sprintf( __( 'Phone: %s', 'invoicing' ), esc_html( $phone ) ) . '</div>';
979
    }
980 View Code Duplication
    if ( $email = $invoice->get_email() ) {
981
        $output .= '<div class="email">' . wp_sprintf( __( 'Email: %s' , 'invoicing'), esc_html( $email ) ) . '</div>';
982
    }
983
    
984
    ob_start();
985
    do_action( 'wpinv_display_to_address_bottom', $invoice );
986
    $output .= ob_get_clean();
987
    
988
    $output .= '</div>';
989
    $output = apply_filters( 'wpinv_display_to_address', $output, $invoice );
990
991
    echo $output;
992
}
993
994
function wpinv_display_line_items( $invoice_id = 0 ) {
995
    global $wpinv_euvat, $ajax_cart_details;
996
    $invoice            = wpinv_get_invoice( $invoice_id );
997
    $quantities_enabled = wpinv_item_quantities_enabled();
998
    $use_taxes          = wpinv_use_taxes();
999
    $zero_tax           = !(float)$invoice->get_tax() > 0 ? true : false;
1000
    $tax_label           = $use_taxes && $invoice->has_vat() ? $wpinv_euvat->get_vat_name() : __( 'Tax', 'invoicing' );
1001
    $tax_title          = !$zero_tax && $use_taxes ? ( wpinv_prices_include_tax() ? wp_sprintf( __( '(%s Incl.)', 'invoicing' ), $tax_label ) : wp_sprintf( __( '(%s Excl.)', 'invoicing' ), $tax_label ) ) : '';
1002
    
1003
    $cart_details       = $invoice->get_cart_details();
1004
    $ajax_cart_details  = $cart_details;
1005
    ob_start();
1006
    ?>
1007
    <table class="table table-sm table-bordered table-responsive">
1008
        <thead>
1009
            <tr>
1010
                <th class="name"><strong><?php _e( "Item Name", "invoicing" );?></strong></th>
1011
                <th class="rate"><strong><?php _e( "Price", "invoicing" );?></strong></th>
1012
                <?php if ($quantities_enabled) { ?>
1013
                    <th class="qty"><strong><?php _e( "Qty", "invoicing" );?></strong></th>
1014
                <?php } ?>
1015
                <?php if ($use_taxes && !$zero_tax) { ?>
1016
                    <th class="tax"><strong><?php echo $tax_label . ' <span class="normal small">(%)</span>'; ?></strong></th>
1017
                <?php } ?>
1018
                <th class="total"><strong><?php echo __( "Item Total", "invoicing" ) . ' <span class="normal small">' . $tax_title . '<span>';?></strong></th>
1019
            </tr>
1020
        </thead>
1021
        <tbody>
1022
        <?php 
1023
            if ( !empty( $cart_details ) ) {
1024
                do_action( 'wpinv_display_line_items_start', $invoice );
1025
                
1026
                $count = 0;
1027
                $cols  = 3;
1028
                foreach ( $cart_details as $key => $cart_item ) {
1029
                    $item_id    = !empty($cart_item['id']) ? absint( $cart_item['id'] ) : '';
1030
                    $item_price = isset($cart_item["item_price"]) ? wpinv_round_amount( $cart_item["item_price"] ) : 0;
1031
                    $line_total = isset($cart_item["subtotal"]) ? wpinv_round_amount( $cart_item["subtotal"] ) : 0;
1032
                    $quantity   = !empty($cart_item['quantity']) && (int)$cart_item['quantity'] > 0 ? absint( $cart_item['quantity'] ) : 1;
1033
                    
1034
                    $item       = $item_id ? new WPInv_Item( $item_id ) : NULL;
1035
                    $summary    = '';
1036
                    $cols       = 3;
1037
                    if ( !empty($item) ) {
1038
                        $item_name  = $item->get_name();
1039
                        $summary    = $item->get_summary();
1040
                    }
1041
                    $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...
1042
                    
1043
                    $summary = apply_filters( 'wpinv_print_invoice_line_item_summary', $summary, $cart_item, $item, $invoice );
1044
                    
1045
                    $item_tax       = '';
1046
                    $tax_rate       = '';
1047
                    if ( $use_taxes && $cart_item['tax'] > 0 && $cart_item['subtotal'] > 0 ) {
1048
                        $item_tax = wpinv_price( wpinv_format_amount( $cart_item['tax'] ), $invoice->get_currency() );
1049
                        $tax_rate = !empty( $cart_item['vat_rate'] ) ? $cart_item['vat_rate'] : ( $cart_item['tax'] / $cart_item['subtotal'] ) * 100;
1050
                        $tax_rate = $tax_rate > 0 ? (float)wpinv_round_amount( $tax_rate, 4 ) : '';
1051
                        $tax_rate = $tax_rate != '' ? ' <small class="tax-rate">(' . $tax_rate . '%)</small>' : '';
1052
                    }
1053
                    
1054
                    $line_item_tax = $item_tax . $tax_rate;
1055
                    
1056
                    if ( $line_item_tax === '' ) {
1057
                        $line_item_tax = 0; // Zero tax
1058
                    }
1059
                    
1060
                    $line_item = '<tr class="row-' . ( ($count % 2 == 0) ? 'even' : 'odd' ) . ' wpinv-item">';
1061
                        $line_item .= '<td class="name">' . esc_html__( $item_name, 'invoicing' ) . wpinv_get_item_suffix( $item );
1062
                        if ( $summary !== '' ) {
1063
                            $line_item .= '<br/><small class="meta">' . wpautop( wp_kses_post( $summary ) ) . '</small>';
1064
                        }
1065
                        $line_item .= '</td>';
1066
                        
1067
                        $line_item .= '<td class="rate">' . esc_html__( wpinv_price( wpinv_format_amount( $item_price ), $invoice->get_currency() ) ) . '</td>';
1068
                        if ($quantities_enabled) {
1069
                            $cols++;
1070
                            $line_item .= '<td class="qty">' . $quantity . '</td>';
1071
                        }
1072
                        if ($use_taxes && !$zero_tax) {
1073
                            $cols++;
1074
                            $line_item .= '<td class="tax">' . $line_item_tax . '</td>';
1075
                        }
1076
                        $line_item .= '<td class="total">' . esc_html__( wpinv_price( wpinv_format_amount( $line_total ), $invoice->get_currency() ) ) . '</td>';
1077
                    $line_item .= '</tr>';
1078
                    
1079
                    echo apply_filters( 'wpinv_display_line_item', $line_item, $cart_item, $invoice, $cols );
1080
1081
                    $count++;
1082
                }
1083
                
1084
                do_action( 'wpinv_display_before_subtotal', $invoice, $cols );
1085
                ?>
1086
                <tr class="row-sub-total row_odd">
1087
                    <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php echo apply_filters( 'wpinv_print_cart_subtotal_label', '<strong>' . __( 'Sub Total', 'invoicing' ) . ':</strong>', $invoice ); ?></td>
1088
                    <td class="total"><strong><?php _e( wpinv_subtotal( $invoice_id, true ) ) ?></strong></td>
1089
                </tr>
1090
                <?php
1091
                do_action( 'wpinv_display_after_subtotal', $invoice, $cols );
1092
                
1093 View Code Duplication
                if ( wpinv_discount( $invoice_id, false ) > 0 ) {
1094
                    do_action( 'wpinv_display_before_discount', $invoice, $cols );
1095
                    ?>
1096
                        <tr class="row-discount">
1097
                            <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php wpinv_get_discount_label( wpinv_discount_code( $invoice_id ) ); ?>:</td>
1098
                            <td class="total"><?php echo wpinv_discount( $invoice_id, true, true ); ?></td>
1099
                        </tr>
1100
                    <?php
1101
                    do_action( 'wpinv_display_after_discount', $invoice, $cols );
1102
                }
1103
                
1104
                if ( $use_taxes ) {
1105
                    do_action( 'wpinv_display_before_tax', $invoice, $cols );
1106
                    ?>
1107
                    <tr class="row-tax">
1108
                        <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php echo apply_filters( 'wpinv_print_cart_tax_label', '<strong>' . $tax_label . ':</strong>', $invoice ); ?></td>
1109
                        <td class="total"><?php _e( wpinv_tax( $invoice_id, true ) ) ?></td>
1110
                    </tr>
1111
                    <?php
1112
                    do_action( 'wpinv_display_after_tax', $invoice, $cols );
1113
                }
1114
                
1115
                do_action( 'wpinv_display_before_total', $invoice, $cols );
1116
                ?>
1117
                <tr class="table-active row-total">
1118
                    <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php echo apply_filters( 'wpinv_print_cart_total_label', '<strong>' . __( 'Total', 'invoicing' ) . ':</strong>', $invoice ); ?></td>
1119
                    <td class="total"><strong><?php _e( wpinv_payment_total( $invoice_id, true ) ) ?></strong></td>
1120
                </tr>
1121
                <?php
1122
                do_action( 'wpinv_display_after_total', $invoice, $cols );
1123
                
1124
                do_action( 'wpinv_display_line_end', $invoice, $cols );
1125
            }
1126
        ?>
1127
        </tbody>
1128
    </table>
1129
    <?php
1130
    echo ob_get_clean();
1131
}
1132
1133
function wpinv_display_invoice_totals( $invoice_id = 0 ) {    
1134
    $use_taxes = wpinv_use_taxes();
1135
    
1136
    do_action( 'wpinv_before_display_totals_table', $invoice_id ); 
1137
    ?>
1138
    <table class="table table-sm table-bordered table-responsive">
1139
        <tbody>
1140
            <?php do_action( 'wpinv_before_display_totals' ); ?>
1141
            <tr class="row-sub-total">
1142
                <td class="rate"><strong><?php _e( 'Sub Total', 'invoicing' ); ?></strong></td>
1143
                <td class="total"><strong><?php _e( wpinv_subtotal( $invoice_id, true ) ) ?></strong></td>
1144
            </tr>
1145
            <?php do_action( 'wpinv_after_display_totals' ); ?>
1146
            <?php if ( wpinv_discount( $invoice_id, false ) > 0 ) { ?>
1147
                <tr class="row-discount">
1148
                    <td class="rate"><?php wpinv_get_discount_label( wpinv_discount_code( $invoice_id ) ); ?></td>
1149
                    <td class="total"><?php echo wpinv_discount( $invoice_id, true, true ); ?></td>
1150
                </tr>
1151
            <?php do_action( 'wpinv_after_display_discount' ); ?>
1152
            <?php } ?>
1153
            <?php if ( $use_taxes ) { ?>
1154
            <tr class="row-tax">
1155
                <td class="rate"><?php _e( 'Tax', 'invoicing' ); ?></td>
1156
                <td class="total"><?php _e( wpinv_tax( $invoice_id, true ) ) ?></td>
1157
            </tr>
1158
            <?php do_action( 'wpinv_after_display_tax' ); ?>
1159
            <?php } ?>
1160
            <?php if ( $fees = wpinv_get_fees( $invoice_id ) ) { ?>
1161
                <?php foreach ( $fees as $fee ) { ?>
1162
                    <tr class="row-fee">
1163
                        <td class="rate"><?php echo $fee['label']; ?></td>
1164
                        <td class="total"><?php echo $fee['amount_display']; ?></td>
1165
                    </tr>
1166
                <?php } ?>
1167
            <?php } ?>
1168
            <tr class="table-active row-total">
1169
                <td class="rate"><strong><?php _e( 'Total', 'invoicing' ) ?></strong></td>
1170
                <td class="total"><strong><?php _e( wpinv_payment_total( $invoice_id, true ) ) ?></strong></td>
1171
            </tr>
1172
            <?php do_action( 'wpinv_after_totals' ); ?>
1173
        </tbody>
1174
1175
    </table>
1176
1177
    <?php do_action( 'wpinv_after_totals_table' );
1178
}
1179
1180
function wpinv_display_payments_info( $invoice_id = 0, $echo = true ) {
1181
    $invoice = wpinv_get_invoice( $invoice_id );
1182
    
1183
    ob_start();
1184
    do_action( 'wpinv_before_display_payments_info', $invoice_id );
1185
    if ( ( $gateway_title = $invoice->get_gateway_title() ) || $invoice->is_paid() || $invoice->is_refunded() ) {
1186
        ?>
1187
        <div class="wpi-payment-info">
1188
            <p class="wpi-payment-gateway"><?php echo wp_sprintf( __( 'Payment via %s', 'invoicing' ), $gateway_title ? $gateway_title : __( 'Manually', 'invoicing' ) ); ?></p>
1189
            <?php if ( $gateway_title ) { ?>
1190
            <p class="wpi-payment-transid"><?php echo wp_sprintf( __( 'Transaction ID: %s', 'invoicing' ), $invoice->get_transaction_id() ); ?></p>
1191
            <?php } ?>
1192
        </div>
1193
        <?php
1194
    }
1195
    do_action( 'wpinv_after_display_payments_info', $invoice_id );
1196
    $outout = ob_get_clean();
1197
    
1198
    if ( $echo ) {
1199
        echo $outout;
1200
    } else {
1201
        return $outout;
1202
    }
1203
}
1204
1205
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...
1206
    wp_register_style( 'wpinv-single-style', WPINV_PLUGIN_URL . 'assets/css/invoice.css', array(), WPINV_VERSION );
1207
    
1208
    wp_print_styles( 'open-sans' );
1209
    wp_print_styles( 'wpinv-single-style' );
1210
}
1211
add_action( 'wpinv_invoice_print_head', 'wpinv_display_style' );
1212
add_action( 'wpinv_invalid_invoice_head', 'wpinv_display_style' );
1213
1214
function wpinv_checkout_billing_details() {  
1215
    $invoice_id = (int)wpinv_get_invoice_cart_id();
1216
    if (empty($invoice_id)) {
1217
        wpinv_error_log( 'Invoice id not found', 'ERROR', __FILE__, __LINE__ );
1218
        return null;
1219
    }
1220
    
1221
    $invoice = wpinv_get_invoice_cart( $invoice_id );
1222
    if (empty($invoice)) {
1223
        wpinv_error_log( 'Invoice not found', 'ERROR', __FILE__, __LINE__ );
1224
        return null;
1225
    }
1226
    $user_id        = $invoice->get_user_id();
1227
    $user_info      = $invoice->get_user_info();
1228
    $address_info   = wpinv_get_user_address( $user_id );
1229
    
1230 View Code Duplication
    if ( empty( $user_info['first_name'] ) && !empty( $user_info['first_name'] ) ) {
1231
        $user_info['first_name'] = $user_info['first_name'];
1232
        $user_info['last_name'] = $user_info['last_name'];
1233
    }
1234
    
1235
    if ( ( ( empty( $user_info['country'] ) && !empty( $address_info['country'] ) ) || ( empty( $user_info['state'] ) && !empty( $address_info['state'] ) && $user_info['country'] == $address_info['country'] ) ) ) {
1236
        $user_info['country']   = $address_info['country'];
1237
        $user_info['state']     = $address_info['state'];
1238
        $user_info['city']      = $address_info['city'];
1239
        $user_info['zip']       = $address_info['zip'];
1240
    }
1241
    
1242
    $address_fields = array(
1243
        'user_id',
1244
        'company',
1245
        'vat_number',
1246
        'email',
1247
        'phone',
1248
        'address'
1249
    );
1250
    
1251
    foreach ( $address_fields as $field ) {
1252
        if ( empty( $user_info[$field] ) ) {
1253
            $user_info[$field] = $address_info[$field];
1254
        }
1255
    }
1256
    
1257
    return apply_filters( 'wpinv_checkout_billing_details', $user_info, $invoice );
1258
}
1259
1260
function wpinv_admin_get_line_items($invoice = array()) {
1261
    $item_quantities    = wpinv_item_quantities_enabled();
1262
    $use_taxes          = wpinv_use_taxes();
1263
    
1264
    if ( empty( $invoice ) ) {
1265
        return NULL;
1266
    }
1267
    
1268
    $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...
1269
    if ( empty( $cart_items ) ) {
1270
        return NULL;
1271
    }
1272
    ob_start();
1273
    
1274
    do_action( 'wpinv_admin_before_line_items', $cart_items, $invoice );
1275
    
1276
    $count = 0;
1277
    foreach ( $cart_items as $key => $cart_item ) {
1278
        $item_id    = $cart_item['id'];
1279
        $wpi_item   = $item_id > 0 ? new WPInv_Item( $item_id ) : NULL;
1280
        
1281
        if (empty($wpi_item)) {
1282
            continue;
1283
        }
1284
        
1285
        $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...
1286
        $quantity       = !empty( $cart_item['quantity'] ) && $cart_item['quantity'] > 0 ? $cart_item['quantity'] : 1;
1287
        $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...
1288
        $can_remove     = true;
1289
        
1290
        $summary = apply_filters( 'wpinv_admin_invoice_line_item_summary', '', $cart_item, $wpi_item, $invoice );
1291
        
1292
        $item_tax       = '';
1293
        $tax_rate       = '';
1294 View Code Duplication
        if ( $cart_item['tax'] > 0 && $cart_item['subtotal'] > 0 ) {
1295
            $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...
1296
            $tax_rate = !empty( $cart_item['vat_rate'] ) ? $cart_item['vat_rate'] : ( $cart_item['tax'] / $cart_item['subtotal'] ) * 100;
1297
            $tax_rate = $tax_rate > 0 ? (float)wpinv_round_amount( $tax_rate, 4 ) : '';
1298
            $tax_rate = $tax_rate != '' ? ' <span class="tax-rate">(' . $tax_rate . '%)</span>' : '';
1299
        }
1300
        $line_item_tax = $item_tax . $tax_rate;
1301
        
1302
        if ( $line_item_tax === '' ) {
1303
            $line_item_tax = 0; // Zero tax
1304
        }
1305
1306
        $line_item = '<tr class="item item-' . ( ($count % 2 == 0) ? 'even' : 'odd' ) . '" data-item-id="' . $item_id . '">';
1307
            $line_item .= '<td class="id">' . $item_id . '</td>';
1308
            $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 );
1309
            if ( $summary !== '' ) {
1310
                $line_item .= '<span class="meta">' . wpautop( wp_kses_post( $summary ) ) . '</span>';
1311
            }
1312
            $line_item .= '</td>';
1313
            $line_item .= '<td class="price">' . $item_price . '</td>';
1314
            
1315
            if ( $item_quantities ) {
1316
                if ( count( $cart_items ) == 1 && $quantity <= 1 ) {
1317
                    $can_remove = false;
1318
                }
1319
                $line_item .= '<td class="qty" data-quantity="' . $quantity . '">&nbsp;&times;&nbsp;' . $quantity . '</td>';
1320
            } else {
1321
                if ( count( $cart_items ) == 1 ) {
1322
                    $can_remove = false;
1323
                }
1324
            }
1325
            $line_item .= '<td class="total">' . $item_subtotal . '</td>';
1326
            
1327
            if ( $use_taxes ) {
1328
                $line_item .= '<td class="tax">' . $line_item_tax . '</td>';
1329
            }
1330
            $line_item .= '<td class="action">';
1331
            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...
1332
                $line_item .= '<i class="fa fa-remove wpinv-item-remove"></i>';
1333
            }
1334
            $line_item .= '</td>';
1335
        $line_item .= '</tr>';
1336
        
1337
        echo apply_filters( 'wpinv_admin_line_item', $line_item, $cart_item, $invoice );
1338
        
1339
        $count++;
1340
    } 
1341
    
1342
    do_action( 'wpinv_admin_after_line_items', $cart_items, $invoice );
1343
    
1344
    return ob_get_clean();
1345
}
1346
1347
function wpinv_checkout_form() {
1348
    global $wpi_checkout_id;
1349
    
1350
    // Set current invoice id.
1351
    $wpi_checkout_id = wpinv_get_invoice_cart_id();
1352
    
1353
    $form_action  = esc_url( wpinv_get_checkout_uri() );
1354
1355
    ob_start();
1356
        echo '<div id="wpinv_checkout_wrap">';
1357
        
1358
        if ( wpinv_get_cart_contents() || wpinv_cart_has_fees() ) {
1359
            ?>
1360
            <div id="wpinv_checkout_form_wrap" class="wpinv_clearfix table-responsive">
1361
                <?php do_action( 'wpinv_before_checkout_form' ); ?>
1362
                <form id="wpinv_checkout_form" class="wpi-form" action="<?php echo $form_action; ?>" method="POST">
1363
                    <?php
1364
                    do_action( 'wpinv_checkout_form_top' );
1365
                    do_action( 'wpinv_checkout_billing_info' );
1366
                    do_action( 'wpinv_checkout_cart' );
1367
                    do_action( 'wpinv_payment_mode_select'  );
1368
                    do_action( 'wpinv_checkout_form_bottom' )
1369
                    ?>
1370
                </form>
1371
                <?php do_action( 'wpinv_after_purchase_form' ); ?>
1372
            </div><!--end #wpinv_checkout_form_wrap-->
1373
        <?php
1374
        } else {
1375
            do_action( 'wpinv_cart_empty' );
1376
        }
1377
        echo '</div><!--end #wpinv_checkout_wrap-->';
1378
    return ob_get_clean();
1379
}
1380
1381
function wpinv_checkout_cart( $cart_details = array(), $echo = true ) {
1382
    global $ajax_cart_details;
1383
    $ajax_cart_details = $cart_details;
1384
    /*
1385
    // Check if the Update cart button should be shown
1386
    if( wpinv_item_quantities_enabled() ) {
1387
        add_action( 'wpinv_cart_footer_buttons', 'wpinv_update_cart_button' );
1388
    }
1389
    
1390
    // Check if the Save Cart button should be shown
1391
    if( !wpinv_is_cart_saving_disabled() ) {
1392
        add_action( 'wpinv_cart_footer_buttons', 'wpinv_save_cart_button' );
1393
    }
1394
    */
1395
    ob_start();
1396
    do_action( 'wpinv_before_checkout_cart' );
1397
    echo '<div id="wpinv_checkout_cart_form" method="post">';
1398
        echo '<div id="wpinv_checkout_cart_wrap">';
1399
            wpinv_get_template_part( 'wpinv-checkout-cart' );
1400
        echo '</div>';
1401
    echo '</div>';
1402
    do_action( 'wpinv_after_checkout_cart' );
1403
    $content = ob_get_clean();
1404
    
1405
    if ( $echo ) {
1406
        echo $content;
1407
    } else {
1408
        return $content;
1409
    }
1410
}
1411
add_action( 'wpinv_checkout_cart', 'wpinv_checkout_cart', 10 );
1412
1413
function wpinv_empty_cart_message() {
1414
	return apply_filters( 'wpinv_empty_cart_message', '<span class="wpinv_empty_cart">' . __( 'Your cart is empty.', 'invoicing' ) . '</span>' );
1415
}
1416
1417
/**
1418
 * Echoes the Empty Cart Message
1419
 *
1420
 * @since 1.0
1421
 * @return void
1422
 */
1423
function wpinv_empty_checkout_cart() {
1424
	echo wpinv_empty_cart_message();
1425
}
1426
add_action( 'wpinv_cart_empty', 'wpinv_empty_checkout_cart' );
1427
1428
function wpinv_save_cart_button() {
1429
    if ( wpinv_is_cart_saving_disabled() )
1430
        return;
1431
?>
1432
    <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>
1433
<?php
1434
}
1435
1436
function wpinv_update_cart_button() {
1437
    if ( !wpinv_item_quantities_enabled() )
1438
        return;
1439
?>
1440
    <input type="submit" name="wpinv_update_cart_submit" class="wpinv-submit wpinv-no-js button" value="<?php _e( 'Update Cart', 'invoicing' ); ?>"/>
1441
    <input type="hidden" name="wpi_action" value="update_cart"/>
1442
<?php
1443
}
1444
1445
function wpinv_checkout_cart_columns() {
1446
    $default = 3;
1447
    if ( wpinv_item_quantities_enabled() ) {
1448
        $default++;
1449
    }
1450
    
1451
    if ( wpinv_use_taxes() ) {
1452
        $default++;
1453
    }
1454
1455
    return apply_filters( 'wpinv_checkout_cart_columns', $default );
1456
}
1457
1458
function wpinv_display_cart_messages() {
1459
    global $wpi_session;
1460
1461
    $messages = $wpi_session->get( 'wpinv_cart_messages' );
1462
1463
    if ( $messages ) {
1464
        foreach ( $messages as $message_id => $message ) {
1465
            // Try and detect what type of message this is
1466
            if ( strpos( strtolower( $message ), 'error' ) ) {
1467
                $type = 'error';
1468
            } elseif ( strpos( strtolower( $message ), 'success' ) ) {
1469
                $type = 'success';
1470
            } else {
1471
                $type = 'info';
1472
            }
1473
1474
            $classes = apply_filters( 'wpinv_' . $type . '_class', array( 'wpinv_errors', 'wpinv-alert', 'wpinv-alert-' . $type ) );
1475
1476
            echo '<div class="' . implode( ' ', $classes ) . '">';
1477
                // Loop message codes and display messages
1478
                    echo '<p class="wpinv_error" id="wpinv_msg_' . $message_id . '">' . $message . '</p>';
1479
            echo '</div>';
1480
        }
1481
1482
        // Remove all of the cart saving messages
1483
        $wpi_session->set( 'wpinv_cart_messages', null );
1484
    }
1485
}
1486
add_action( 'wpinv_before_checkout_cart', 'wpinv_display_cart_messages' );
1487
1488
function wpinv_discount_field() {
1489
    if ( isset( $_GET['wpi-gateway'] ) && wpinv_is_ajax_disabled() ) {
1490
        return; // Only show before a payment method has been selected if ajax is disabled
1491
    }
1492
1493
    if ( !wpinv_is_checkout() ) {
1494
        return;
1495
    }
1496
1497
    if ( wpinv_has_active_discounts() && wpinv_get_cart_total() ) {
1498
    ?>
1499
    <div id="wpinv-discount-field" class="panel panel-default">
1500
        <div class="panel-body">
1501
            <p>
1502
                <label class="wpinv-label" for="wpinv_discount_code"><strong><?php _e( 'Discount', 'invoicing' ); ?></strong></label>
1503
                <span class="wpinv-description"><?php _e( 'Enter a discount code if you have one.', 'invoicing' ); ?></span>
1504
            </p>
1505
            <div class="form-group row">
1506
                <div class="col-sm-4">
1507
                    <input class="wpinv-input form-control" type="text" id="wpinv_discount_code" name="wpinv_discount_code" placeholder="<?php _e( 'Enter discount code', 'invoicing' ); ?>"/>
1508
                </div>
1509
                <div class="col-sm-3">
1510
                    <button id="wpi-apply-discount" type="button" class="btn btn-success btn-sm"><?php _e( 'Apply Discount', 'invoicing' ); ?></button>
1511
                </div>
1512
                <div style="clear:both"></div>
1513
                <div class="col-sm-12 wpinv-discount-msg">
1514
                    <div class="alert alert-success"><i class="fa fa-check-circle"></i><span class="wpi-msg"></span></div>
1515
                    <div class="alert alert-error"><i class="fa fa-warning"></i><span class="wpi-msg"></span></div>
1516
                </div>
1517
            </div>
1518
        </div>
1519
    </div>
1520
<?php
1521
    }
1522
}
1523
add_action( 'wpinv_after_checkout_cart', 'wpinv_discount_field', -10 );
1524
1525
function wpinv_agree_to_terms_js() {
1526
    if ( wpinv_get_option( 'show_agree_to_terms', false ) ) {
1527
?>
1528
<script type="text/javascript">
1529
    jQuery(document).ready(function($){
1530
        $( document.body ).on('click', '.wpinv_terms_links', function(e) {
1531
            //e.preventDefault();
1532
            $('#wpinv_terms').slideToggle();
1533
            $('.wpinv_terms_links').toggle();
1534
            return false;
1535
        });
1536
    });
1537
</script>
1538
<?php
1539
    }
1540
}
1541
add_action( 'wpinv_checkout_form_top', 'wpinv_agree_to_terms_js' );
1542
1543
function wpinv_payment_mode_select() {
1544
    $gateways = wpinv_get_enabled_payment_gateways( true );
1545
    $gateways = apply_filters( 'wpinv_payment_gateways_on_cart', $gateways );
1546
    $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...
1547
    $invoice = wpinv_get_invoice( 0, true );
1548
    
1549
    do_action('wpinv_payment_mode_top');
1550
    $invoice_id = (int)$invoice->ID;
1551
    $chosen_gateway = wpinv_get_chosen_gateway( $invoice_id );
1552
    ?>
1553
    <div id="wpinv_payment_mode_select" data-gateway="<?php echo $chosen_gateway; ?>" <?php echo ( $invoice->is_free() ? 'style="display:none;"' : '' ); ?>>
1554
            <?php do_action( 'wpinv_payment_mode_before_gateways_wrap' ); ?>
1555
            <div id="wpinv-payment-mode-wrap" class="panel panel-default">
1556
                <div class="panel-heading"><h3 class="panel-title"><?php _e( 'Select Payment Method', 'invoicing' ); ?></h3></div>
1557
                <div class="panel-body list-group wpi-payment_methods">
1558
                    <?php
1559
                    do_action( 'wpinv_payment_mode_before_gateways' );
1560
                    
1561
                    if(!empty($gateways)){
1562
	                    foreach ( $gateways as $gateway_id => $gateway ) {
1563
		                    $checked = checked( $gateway_id, $chosen_gateway, false );
1564
		                    $button_label = wpinv_get_gateway_button_label( $gateway_id );
1565
		                    $description = wpinv_get_gateway_description( $gateway_id );
1566
		                    ?>
1567
		                    <div class="list-group-item">
1568
			                    <div class="radio">
1569
				                    <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>
1570
			                    </div>
1571
			                    <div style="display:none;" class="payment_box wpi_gateway_<?php echo esc_attr( $gateway_id );?>" role="alert">
1572
				                    <?php if ( !empty( $description ) ) { ?>
1573
					                    <div class="wpi-gateway-desc alert alert-info"><?php echo $description;?></div>
1574
				                    <?php } ?>
1575
				                    <?php do_action( 'wpinv_' . $gateway_id . '_cc_form', $invoice_id ) ;?>
1576
			                    </div>
1577
		                    </div>
1578
		                    <?php
1579
	                    }
1580
                    }else{
1581
	                    echo '<div class="alert alert-warning">'. __('No payment gateway active','invoicing') .'</div>';
1582
                    }
1583
1584
                    do_action( 'wpinv_payment_mode_after_gateways' );
1585
                    ?>
1586
                </div>
1587
            </div>
1588
            <?php do_action( 'wpinv_payment_mode_after_gateways_wrap' ); ?>
1589
    </div>
1590
    <?php
1591
    do_action('wpinv_payment_mode_bottom');
1592
}
1593
add_action( 'wpinv_payment_mode_select', 'wpinv_payment_mode_select' );
1594
1595
function wpinv_checkout_billing_info() {    
1596
    if ( wpinv_is_checkout() ) {
1597
        $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...
1598
        $billing_details    = wpinv_checkout_billing_details();
1599
        $selected_country   = !empty( $billing_details['country'] ) ? $billing_details['country'] : wpinv_default_billing_country();
1600
        ?>
1601
        <div id="wpinv-fields" class="clearfix">
1602
            <div id="wpi-billing" class="wpi-billing clearfix panel panel-default">
1603
                <div class="panel-heading"><h3 class="panel-title"><?php _e( 'Billing Details', 'invoicing' );?></h3></div>
1604
                <div id="wpinv-fields-box" class="panel-body">
1605
                    <?php do_action( 'wpinv_checkout_billing_fields_first', $billing_details ); ?>
1606
                    <p class="wpi-cart-field wpi-col2 wpi-colf">
1607
                        <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>
1608
                        <?php
1609
                        echo wpinv_html_text( array(
1610
                                'id'            => 'wpinv_first_name',
1611
                                'name'          => 'wpinv_first_name',
1612
                                'value'         => $billing_details['first_name'],
1613
                                'class'         => 'wpi-input form-control',
1614
                                'placeholder'   => __( 'First name', 'invoicing' ),
1615
                                'required'      => (bool)wpinv_get_option( 'fname_mandatory' ),
1616
                            ) );
1617
                        ?>
1618
                    </p>
1619
                    <p class="wpi-cart-field wpi-col2 wpi-coll">
1620
                        <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>
1621
                        <?php
1622
                        echo wpinv_html_text( array(
1623
                                'id'            => 'wpinv_last_name',
1624
                                'name'          => 'wpinv_last_name',
1625
                                'value'         => $billing_details['last_name'],
1626
                                'class'         => 'wpi-input form-control',
1627
                                'placeholder'   => __( 'Last name', 'invoicing' ),
1628
                                'required'      => (bool)wpinv_get_option( 'lname_mandatory' ),
1629
                            ) );
1630
                        ?>
1631
                    </p>
1632
                    <p class="wpi-cart-field wpi-col2 wpi-colf">
1633
                        <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>
1634
                        <?php
1635
                        echo wpinv_html_text( array(
1636
                                'id'            => 'wpinv_address',
1637
                                'name'          => 'wpinv_address',
1638
                                'value'         => $billing_details['address'],
1639
                                'class'         => 'wpi-input form-control',
1640
                                'placeholder'   => __( 'Address', 'invoicing' ),
1641
                                'required'      => (bool)wpinv_get_option( 'address_mandatory' ),
1642
                            ) );
1643
                        ?>
1644
                    </p>
1645
                    <p class="wpi-cart-field wpi-col2 wpi-coll">
1646
                        <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>
1647
                        <?php
1648
                        echo wpinv_html_text( array(
1649
                                'id'            => 'wpinv_city',
1650
                                'name'          => 'wpinv_city',
1651
                                'value'         => $billing_details['city'],
1652
                                'class'         => 'wpi-input form-control',
1653
                                'placeholder'   => __( 'City', 'invoicing' ),
1654
                                'required'      => (bool)wpinv_get_option( 'city_mandatory' ),
1655
                            ) );
1656
                        ?>
1657
                    </p>
1658
                    <p id="wpinv_country_box" class="wpi-cart-field wpi-col2 wpi-colf">
1659
                        <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>
1660
                        <?php echo wpinv_html_select( array(
1661
                            'options'          => wpinv_get_country_list(),
1662
                            'name'             => 'wpinv_country',
1663
                            'id'               => 'wpinv_country',
1664
                            'selected'         => $selected_country,
1665
                            'show_option_all'  => false,
1666
                            'show_option_none' => false,
1667
                            'class'            => 'wpi-input form-control',
1668
                            'placeholder'      => __( 'Choose a country', 'invoicing' ),
1669
                            'required'         => (bool)wpinv_get_option( 'country_mandatory' ),
1670
                        ) ); ?>
1671
                    </p>
1672
                    <p id="wpinv_state_box" class="wpi-cart-field wpi-col2 wpi-coll">
1673
                        <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>
1674
                        <?php
1675
                        $states = wpinv_get_country_states( $selected_country );
1676
                        if( !empty( $states ) ) {
1677
                            echo wpinv_html_select( array(
1678
                                'options'          => $states,
1679
                                'name'             => 'wpinv_state',
1680
                                'id'               => 'wpinv_state',
1681
                                'selected'         => $billing_details['state'],
1682
                                'show_option_all'  => false,
1683
                                'show_option_none' => false,
1684
                                'class'            => 'wpi-input form-control',
1685
                                'placeholder'      => __( 'Choose a state', 'invoicing' ),
1686
                                'required'         => (bool)wpinv_get_option( 'state_mandatory' ),
1687
                            ) );
1688
                        } else {
1689
                            echo wpinv_html_text( array(
1690
                                'name'          => 'wpinv_state',
1691
                                'value'         => $billing_details['state'],
1692
                                'id'            => 'wpinv_state',
1693
                                'class'         => 'wpi-input form-control',
1694
                                'placeholder'   => __( 'State / Province', 'invoicing' ),
1695
                                'required'      => (bool)wpinv_get_option( 'state_mandatory' ),
1696
                            ) );
1697
                        }
1698
                        ?>
1699
                    </p>
1700
                    <p class="wpi-cart-field wpi-col2 wpi-colf">
1701
                        <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>
1702
                        <?php
1703
                        echo wpinv_html_text( array(
1704
                                'name'          => 'wpinv_zip',
1705
                                'value'         => $billing_details['zip'],
1706
                                'id'            => 'wpinv_zip',
1707
                                'class'         => 'wpi-input form-control',
1708
                                'placeholder'   => __( 'ZIP / Postcode', 'invoicing' ),
1709
                                'required'      => (bool)wpinv_get_option( 'zip_mandatory' ),
1710
                            ) );
1711
                        ?>
1712
                    </p>
1713
                    <p class="wpi-cart-field wpi-col2 wpi-coll">
1714
                        <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>
1715
                        <?php
1716
                        echo wpinv_html_text( array(
1717
                                'id'            => 'wpinv_phone',
1718
                                'name'          => 'wpinv_phone',
1719
                                'value'         => $billing_details['phone'],
1720
                                'class'         => 'wpi-input form-control',
1721
                                'placeholder'   => __( 'Phone', 'invoicing' ),
1722
                                'required'      => (bool)wpinv_get_option( 'phone_mandatory' ),
1723
                            ) );
1724
                        ?>
1725
                    </p>
1726
                    <?php do_action( 'wpinv_checkout_billing_fields_last', $billing_details ); ?>
1727
                    <div class="clearfix"></div>
1728
                </div>
1729
            </div>
1730
            <?php do_action( 'wpinv_after_billing_fields', $billing_details ); ?>
1731
        </div>
1732
        <?php
1733
    }
1734
}
1735
add_action( 'wpinv_checkout_billing_info', 'wpinv_checkout_billing_info' );
1736
1737
function wpinv_checkout_hidden_fields() {
1738
?>
1739
    <?php if ( is_user_logged_in() ) { ?>
1740
    <input type="hidden" name="wpinv_user_id" value="<?php echo get_current_user_id(); ?>"/>
1741
    <?php } ?>
1742
    <input type="hidden" name="wpi_action" value="payment" />
1743
<?php
1744
}
1745
1746
function wpinv_checkout_button_purchase() {
1747
    ob_start();
1748
?>
1749
    <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' ) ?>"/>
1750
<?php
1751
    return apply_filters( 'wpinv_checkout_button_purchase', ob_get_clean() );
1752
}
1753
1754
function wpinv_checkout_total() {
1755
    global $cart_total;
1756
?>
1757
<div id="wpinv_checkout_total" class="panel panel-info">
1758
    <div class="panel-body">
1759
    <?php
1760
    do_action( 'wpinv_purchase_form_before_checkout_total' );
1761
    ?>
1762
    <strong><?php _e( 'Invoice Total:', 'invoicing' ) ?></strong> <span class="wpinv-chdeckout-total"><?php echo $cart_total;?></span>
1763
    <?php
1764
    do_action( 'wpinv_purchase_form_after_checkout_total' );
1765
    ?>
1766
    </div>
1767
</div>
1768
<?php
1769
}
1770
add_action( 'wpinv_checkout_form_bottom', 'wpinv_checkout_total', 9998 );
1771
1772
function wpinv_checkout_submit() {
1773
?>
1774
<div id="wpinv_purchase_submit" class="panel panel-success">
1775
    <div class="panel-body text-center">
1776
    <?php
1777
    do_action( 'wpinv_purchase_form_before_submit' );
1778
    wpinv_checkout_hidden_fields();
1779
    echo wpinv_checkout_button_purchase();
1780
    do_action( 'wpinv_purchase_form_after_submit' );
1781
    ?>
1782
    </div>
1783
</div>
1784
<?php
1785
}
1786
add_action( 'wpinv_checkout_form_bottom', 'wpinv_checkout_submit', 9999 );
1787
1788
function wpinv_receipt_billing_address( $invoice_id = 0 ) {
1789
    $invoice = wpinv_get_invoice( $invoice_id );
1790
    
1791
    if ( empty( $invoice ) ) {
1792
        return NULL;
1793
    }
1794
    
1795
    $billing_details = $invoice->get_user_info();
1796
    $address_row = '';
1797
    if ( $address = $billing_details['address'] ) {
1798
        $address_row .= wpautop( wp_kses_post( $address ) );
1799
    }
1800
    
1801
    $address_fields = array();
1802
    if ( !empty( $billing_details['city'] ) ) {
1803
        $address_fields[] = $billing_details['city'];
1804
    }
1805
    
1806
    $billing_country = !empty( $billing_details['country'] ) ? $billing_details['country'] : '';
1807
    if ( !empty( $billing_details['state'] ) ) {
1808
        $address_fields[] = wpinv_state_name( $billing_details['state'], $billing_country );
1809
    }
1810
    
1811
    if ( !empty( $billing_country ) ) {
1812
        $address_fields[] = wpinv_country_name( $billing_country );
1813
    }
1814
    
1815 View Code Duplication
    if ( !empty( $address_fields ) ) {
1816
        $address_fields = implode( ", ", $address_fields );
1817
        
1818
        if ( !empty( $billing_details['zip'] ) ) {
1819
            $address_fields .= ' ' . $billing_details['zip'];
1820
        }
1821
        
1822
        $address_row .= wpautop( wp_kses_post( $address_fields ) );
1823
    }
1824
    ob_start();
1825
    ?>
1826
    <table class="table table-bordered table-sm wpi-billing-details">
1827
        <tbody>
1828
            <tr class="wpi-receipt-name">
1829
                <th class="text-left"><?php _e( 'Name', 'invoicing' ); ?></th>
1830
                <td><?php echo esc_html( trim( $billing_details['first_name'] . ' ' . $billing_details['last_name'] ) ) ;?></td>
1831
            </tr>
1832
            <tr class="wpi-receipt-email">
1833
                <th class="text-left"><?php _e( 'Email', 'invoicing' ); ?></th>
1834
                <td><?php echo $billing_details['email'] ;?></td>
1835
            </tr>
1836 View Code Duplication
            <?php if ( $billing_details['company'] ) { ?>
1837
            <tr class="wpi-receipt-company">
1838
                <th class="text-left"><?php _e( 'Company', 'invoicing' ); ?></th>
1839
                <td><?php echo esc_html( $billing_details['company'] ) ;?></td>
1840
            </tr>
1841
            <?php } ?>
1842
            <tr class="wpi-receipt-address">
1843
                <th class="text-left"><?php _e( 'Address', 'invoicing' ); ?></th>
1844
                <td><?php echo $address_row ;?></td>
1845
            </tr>
1846 View Code Duplication
            <?php if ( $billing_details['phone'] ) { ?>
1847
            <tr class="wpi-receipt-phone">
1848
                <th class="text-left"><?php _e( 'Phone', 'invoicing' ); ?></th>
1849
                <td><?php echo esc_html( $billing_details['phone'] ) ;?></td>
1850
            </tr>
1851
            <?php } ?>
1852
        </tbody>
1853
    </table>
1854
    <?php
1855
    $output = ob_get_clean();
1856
    
1857
    $output = apply_filters( 'wpinv_receipt_billing_address', $output, $invoice_id );
1858
1859
    echo $output;
1860
}
1861
1862
function wpinv_filter_success_page_content( $content ) {
1863
    if ( isset( $_GET['payment-confirm'] ) && wpinv_is_success_page() ) {
1864
        if ( has_filter( 'wpinv_payment_confirm_' . sanitize_text_field( $_GET['payment-confirm'] ) ) ) {
1865
            $content = apply_filters( 'wpinv_payment_confirm_' . sanitize_text_field( $_GET['payment-confirm'] ), $content );
1866
        }
1867
    }
1868
1869
    return $content;
1870
}
1871
add_filter( 'the_content', 'wpinv_filter_success_page_content', 99999 );
1872
1873
function wpinv_receipt_actions( $invoice ) {
1874
    if ( !empty( $invoice ) ) {
1875
        $actions = array();
1876
1877
        if ( wpinv_user_can_view_invoice( $invoice->ID ) ) {
1878
            $actions['print']   = array(
1879
                'url'  => $invoice->get_view_url( true ),
1880
                'name' => __( 'Print Invoice', 'invoicing' ),
1881
                'class' => 'btn-primary',
1882
            );
1883
        }
1884
1885
        if ( is_user_logged_in() ) {
1886
            $actions['history'] = array(
1887
                'url'  => wpinv_get_history_page_uri(),
1888
                'name' => __( 'Invoice History', 'invoicing' ),
1889
                'class' => 'btn-warning',
1890
            );
1891
        }
1892
1893
        $actions = apply_filters( 'wpinv_invoice_receipt_actions', $actions, $invoice );
1894
        
1895
        if ( !empty( $actions ) ) {
1896
        ?>
1897
        <div class="wpinv-receipt-actions text-right">
1898
            <?php foreach ( $actions as $key => $action ) { $class = !empty($action['class']) ? sanitize_html_class( $action['class'] ) : ''; ?>
1899
            <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>
1900
            <?php } ?>
1901
        </div>
1902
        <?php
1903
        }
1904
    }
1905
}
1906
add_action( 'wpinv_receipt_start', 'wpinv_receipt_actions', -10, 1 );
1907
1908
function wpinv_invoice_link( $invoice_id ) {
1909
    $invoice = wpinv_get_invoice( $invoice_id );
1910
    
1911
    if ( empty( $invoice ) ) {
1912
        return NULL;
1913
    }
1914
    
1915
    $invoice_link = '<a href="' . esc_url( $invoice->get_view_url() ) . '">' . $invoice->get_number() . '</a>';
1916
    
1917
    return apply_filters( 'wpinv_get_invoice_link', $invoice_link, $invoice );
1918
}
1919
1920
function wpinv_invoice_subscription_details( $invoice ) {
1921
    if ( !empty( $invoice ) && $invoice->is_recurring() && !wpinv_is_subscription_payment( $invoice ) ) {
1922
        $subs_db = new WPInv_Subscriptions_DB;
1923
        $subs = $subs_db->get_subscriptions(array('parent_payment_id' => $invoice->ID, 'number' => 1));
1924
        $sub = reset($subs);
1925
        if(!$sub){
1926
            return;
1927
        }
1928
        $frequency = WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency($sub->period, $sub->frequency);
1929
        $billing = wpinv_price(wpinv_format_amount($sub->recurring_amount), wpinv_get_invoice_currency_code($sub->parent_payment_id)) . ' / ' . $frequency;
1930
        $initial = wpinv_price(wpinv_format_amount($sub->initial_amount), wpinv_get_invoice_currency_code($sub->parent_payment_id));
1931
        ?>
1932
        <div class="wpinv-subscriptions-details">
1933
            <h3 class="wpinv-subscriptions-t"><?php echo apply_filters( 'wpinv_subscription_details_title', __( 'Subscription Details', 'invoicing' ) ); ?></h3>
1934
            <table class="table">
1935
                <thead>
1936
                    <tr>
1937
                        <th><?php _e( 'Billing Cycle', 'invoicing' ) ;?></th>
1938
                        <th><?php _e( 'Start Date', 'invoicing' ) ;?></th>
1939
                        <th><?php _e( 'Expiration Date', 'invoicing' ) ;?></th>
1940
                        <th class="text-center"><?php _e( 'Times Billed', 'invoicing' ) ;?></th>
1941
                        <th class="text-center"><?php _e( 'Status', 'invoicing' ) ;?></th>
1942
                    </tr>
1943
                </thead>
1944
                <tbody>
1945
                    <tr>
1946
                        <td><?php printf(_x('%s then %s', 'Initial subscription amount then billing cycle and amount', 'invoicing'), $initial, $billing); ?></td>
1947
                        <td><?php echo date_i18n(get_option('date_format'), strtotime($sub->created, current_time('timestamp'))); ?></td>
1948
                        <td><?php echo date_i18n(get_option('date_format'), strtotime($sub->expiration, current_time('timestamp'))); ?></td>
1949
                        <td class="text-center"><?php echo $sub->get_times_billed() . ' / ' . (($sub->bill_times == 0) ? 'Until Cancelled' : $sub->bill_times); ?></td>
1950
                        <td class="text-center wpi-sub-status"><?php echo $sub->get_status_label(); ?>
1951
                        </td>
1952
                    </tr>
1953
                </tbody>
1954
            </table>
1955
        </div>
1956
        <?php
1957
        $payments = $sub->get_child_payments();
1958
        if ( !empty( $payments ) ) { ?>
1959
        <div class="wpinv-renewal-payments">
1960
            <h3 class="wpinv-renewals-t"><?php echo apply_filters( 'wpinv_renewal_payments_title', __( 'Renewal Payments', 'invoicing' ) ); ?></h3>
1961
            <table class="table">
1962
                <thead>
1963
                    <tr>
1964
                        <th>#</th>
1965
                        <th><?php _e( 'Invoice', 'invoicing' ) ;?></th>
1966
                        <th><?php _e( 'Date', 'invoicing' ) ;?></th>
1967
                        <th class="text-right"><?php _e( 'Amount', 'invoicing' ) ;?></th>
1968
                    </tr>
1969
                </thead>
1970
                <tbody>
1971
                    <?php
1972
                        $i = 1;
1973
                        foreach ( $payments as $payment ) {
1974
                        $invoice_id = $payment->ID;
1975
                    ?>
1976
                    <tr>
1977
                        <th scope="row"><?php echo $i++;?></th>
1978
                        <td><?php echo wpinv_invoice_link( $invoice_id ) ;?></td>
1979
                        <td><?php echo wpinv_get_invoice_date( $invoice_id ); ?></td>
1980
                        <td class="text-right"><?php echo wpinv_payment_total( $invoice_id, true ); ?></td>
1981
                    </tr>
1982
                    <?php } ?>
1983
                    <tr><td colspan="4" style="padding:0"></td></tr>
1984
                </tbody>
1985
            </table>
1986
        </div>
1987
        <?php } ?>
1988
        <?php
1989
    }
1990
}
1991
1992
function wpinv_cart_total_label( $label, $invoice ) {
1993
    if ( empty( $invoice ) ) {
1994
        return $label;
1995
    }
1996
    
1997
    $prefix_label = '';
1998
    if ( $invoice->is_parent() && $item_id = $invoice->get_recurring() ) {        
1999
        $prefix_label   = '<span class="label label-primary label-recurring">' . __( 'Recurring Payment', 'invoicing' ) . '</span> ' . wpinv_subscription_payment_desc( $invoice );
2000
    } else if ( $invoice->is_renewal() ) {
2001
        $prefix_label   = '<span class="label label-primary label-renewal">' . __( 'Renewal Payment', 'invoicing' ) . '</span> ';        
2002
    }
2003
    
2004
    if ( $prefix_label != '' ) {
2005
        $label  = '<span class="wpinv-cart-sub-desc">' . $prefix_label . '</span> ' . $label;
2006
    }
2007
    
2008
    return $label;
2009
}
2010
add_filter( 'wpinv_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
2011
add_filter( 'wpinv_email_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
2012
add_filter( 'wpinv_print_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
2013
2014
add_action( 'wpinv_invoice_print_middle', 'wpinv_invoice_subscription_details', 10, 1 );
2015
2016
function wpinv_invoice_print_description( $invoice ) {
2017
    if ( empty( $invoice ) ) {
2018
        return NULL;
2019
    }
2020
    if ( $description = wpinv_get_invoice_description( $invoice->ID ) ) {
2021
        ?>
2022
        <div class="row wpinv-lower">
2023
            <div class="col-sm-12 wpinv-description">
2024
                <?php echo wpautop( $description ); ?>
2025
            </div>
2026
        </div>
2027
        <?php
2028
    }
2029
}
2030
add_action( 'wpinv_invoice_print_middle', 'wpinv_invoice_print_description', 10.1, 1 );
2031
2032
function wpinv_invoice_print_payment_info( $invoice ) {
2033
    if ( empty( $invoice ) ) {
2034
        return NULL;
2035
    }
2036
    
2037
    if ( $payments_info = wpinv_display_payments_info( $invoice->ID, false ) ) {
2038
        ?>
2039
        <div class="row wpinv-payments">
2040
            <div class="col-sm-12">
2041
                <?php echo $payments_info; ?>
2042
            </div>
2043
        </div>
2044
        <?php 
2045
    }
2046
}
2047
// add_action( 'wpinv_invoice_print_after_line_items', 'wpinv_invoice_print_payment_info', 10, 1 );
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

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