Passed
Push — master ( 99a69d...f79e9b )
by Brian
123:35 queued 12s
created

wpinv_display_invoice_details()   D

Complexity

Conditions 14
Paths 256

Size

Total Lines 73
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 59
nc 256
nop 1
dl 0
loc 73
rs 4.7333
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Contains functions related to Invoicing plugin.
4
 *
5
 * @since 1.0.0
6
 * @package Invoicing
7
 */
8
 
9
// MUST have WordPress.
10
if ( !defined( 'WPINC' ) ) {
11
    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
12
}
13
14
if ( !is_admin() ) {
15
    add_filter( 'template_include', 'wpinv_template', 10, 1 );
16
    add_action( 'wpinv_invoice_print_body_start', 'wpinv_display_invoice_top_bar' );
17
    add_action( 'wpinv_invoice_top_bar_left', 'wpinv_invoice_display_left_actions' );
18
    add_action( 'wpinv_invoice_top_bar_right', 'wpinv_invoice_display_right_actions' );
19
}
20
21
function wpinv_template_path() {
22
    return apply_filters( 'wpinv_template_path', wpinv_get_theme_template_dir_name() );
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 btn-print-invoice" 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 btn-invoice-history" href="<?php echo esc_url( wpinv_get_history_page_uri() ); ?>"><?php _e( 'Invoice History', 'invoicing' ); ?></a>
0 ignored issues
show
Bug introduced by
It seems like wpinv_get_history_page_uri() can also be of type false; however, parameter $url of esc_url() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

64
        &nbsp;&nbsp;<a class="btn btn-warning btn-sm btn-invoice-history" href="<?php echo esc_url( /** @scrutinizer ignore-type */ wpinv_get_history_page_uri() ); ?>"><?php _e( 'Invoice History', 'invoicing' ); ?></a>
Loading history...
65
        <?php }
66
    }
67
    do_action('wpinv_invoice_display_right_actions', $invoice);
68
}
69
70
function wpinv_before_invoice_content( $content ) {
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
function wpinv_after_invoice_content( $content ) {
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 ) )
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', 'invoicing' ) );
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
334
    $options = array(
335
        '1'  => __( 'January', 'invoicing' ),
336
        '2'  => __( 'February', 'invoicing' ),
337
        '3'  => __( 'March', 'invoicing' ),
338
        '4'  => __( 'April', 'invoicing' ),
339
        '5'  => __( 'May', 'invoicing' ),
340
        '6'  => __( 'June', 'invoicing' ),
341
        '7'  => __( 'July', 'invoicing' ),
342
        '8'  => __( 'August', 'invoicing' ),
343
        '9'  => __( 'September', 'invoicing' ),
344
        '10' => __( 'October', 'invoicing' ),
345
        '11' => __( 'November', 'invoicing' ),
346
        '12' => __( 'December', 'invoicing' ),
347
    );
348
349
    // If no month is selected, default to the current month
350
    $selected = empty( $selected ) ? date( 'n' ) : $selected;
351
352
    $output = wpinv_html_select( array(
353
        'name'             => $name,
354
        'selected'         => $selected,
355
        'options'          => $options,
356
        'show_option_all'  => false,
357
        'show_option_none' => false
358
    ) );
359
360
    return $output;
361
}
362
363
function wpinv_html_select( $args = array() ) {
364
    $defaults = array(
365
        'options'          => array(),
366
        'name'             => null,
367
        'class'            => '',
368
        'id'               => '',
369
        'selected'         => 0,
370
        'placeholder'      => null,
371
        'multiple'         => false,
372
        'show_option_all'  => _x( 'All', 'all dropdown items', 'invoicing' ),
373
        'show_option_none' => _x( 'None', 'no dropdown items', 'invoicing' ),
374
        'data'             => array(),
375
        'onchange'         => null,
376
        'required'         => false,
377
        'disabled'         => false,
378
        'readonly'         => false,
379
    );
380
381
    $args = wp_parse_args( $args, $defaults );
0 ignored issues
show
Security Variable Injection introduced by
$args can contain request data and is used in variable name context(s) leading to a potential security vulnerability.

2 paths for user data to reach this point

  1. Path: Read from $_GET in includes/admin/wpinv-admin-functions.php on line 367
  1. Read from $_GET
    in includes/admin/wpinv-admin-functions.php on line 367
  2. wpinv_html_select() is called
    in includes/admin/wpinv-admin-functions.php on line 363
  3. Enters via parameter $args
    in includes/wpinv-template-functions.php on line 363
  2. Path: Read from $_GET, and $_GET['vat_class'] is assigned to $class in includes/admin/wpinv-admin-functions.php on line 382
  1. Read from $_GET, and $_GET['vat_class'] is assigned to $class
    in includes/admin/wpinv-admin-functions.php on line 382
  2. wpinv_html_select() is called
    in includes/admin/wpinv-admin-functions.php on line 385
  3. Enters via parameter $args
    in includes/wpinv-template-functions.php on line 363

Used in variable context

  1. wp_parse_args() is called
    in includes/wpinv-template-functions.php on line 381
  2. Enters via parameter $args
    in wordpress/wp-includes/functions.php on line 4294
  3. wp_parse_str() is called
    in wordpress/wp-includes/functions.php on line 4300
  4. Enters via parameter $string
    in wordpress/wp-includes/formatting.php on line 4865
  5. parse_str() is called
    in wordpress/wp-includes/formatting.php on line 4866

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
382
383
    $data_elements = '';
384
    foreach ( $args['data'] as $key => $value ) {
385
        $data_elements .= ' data-' . esc_attr( $key ) . '="' . esc_attr( $value ) . '"';
386
    }
387
388
    if( $args['multiple'] ) {
389
        $multiple = ' MULTIPLE';
390
    } else {
391
        $multiple = '';
392
    }
393
394
    if( $args['placeholder'] ) {
395
        $placeholder = $args['placeholder'];
396
    } else {
397
        $placeholder = '';
398
    }
399
    
400
    $options = '';
401
    if( !empty( $args['onchange'] ) ) {
402
        $options .= ' onchange="' . esc_attr( $args['onchange'] ) . '"';
403
    }
404
    
405
    if( !empty( $args['required'] ) ) {
406
        $options .= ' required="required"';
407
    }
408
    
409
    if( !empty( $args['disabled'] ) ) {
410
        $options .= ' disabled';
411
    }
412
    
413
    if( !empty( $args['readonly'] ) ) {
414
        $options .= ' readonly';
415
    }
416
417
    $class  = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
418
    $output = '<select name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( $args['id'] ) . '" class="wpinv-select ' . $class . '"' . $multiple . ' data-placeholder="' . $placeholder . '" ' . trim( $options ) . $data_elements . '>';
419
420
    if ( $args['show_option_all'] ) {
421
        if( $args['multiple'] ) {
422
            $selected = selected( true, in_array( 0, $args['selected'] ), false );
423
        } else {
424
            $selected = selected( $args['selected'], 0, false );
425
        }
426
        $output .= '<option value="all"' . $selected . '>' . esc_html( $args['show_option_all'] ) . '</option>';
427
    }
428
429
    if ( !empty( $args['options'] ) ) {
430
431
        if ( $args['show_option_none'] ) {
432
            if( $args['multiple'] ) {
433
                $selected = selected( true, in_array( "", $args['selected'] ), false );
434
            } else {
435
                $selected = selected( $args['selected'] === "", true, false );
436
            }
437
            $output .= '<option value=""' . $selected . '>' . esc_html( $args['show_option_none'] ) . '</option>';
438
        }
439
440
        foreach( $args['options'] as $key => $option ) {
441
442
            if( $args['multiple'] && is_array( $args['selected'] ) ) {
443
                $selected = selected( true, (bool)in_array( $key, $args['selected'] ), false );
444
            } else {
445
                $selected = selected( $args['selected'], $key, false );
446
            }
447
448
            $output .= '<option value="' . esc_attr( $key ) . '"' . $selected . '>' . esc_html( $option ) . '</option>';
449
        }
450
    }
451
452
    $output .= '</select>';
453
454
    return $output;
455
}
456
457
function wpinv_item_dropdown( $args = array() ) {
458
    $defaults = array(
459
        'name'              => 'wpi_item',
460
        'id'                => 'wpi_item',
461
        'class'             => '',
462
        'multiple'          => false,
463
        'selected'          => 0,
464
        'number'            => 100,
465
        'placeholder'       => __( 'Choose a item', 'invoicing' ),
466
        'data'              => array( 'search-type' => 'item' ),
467
        'show_option_all'   => false,
468
        'show_option_none'  => false,
469
        'show_recurring'    => false,
470
    );
471
472
    $args = wp_parse_args( $args, $defaults );
473
474
    $item_args = array(
475
        'post_type'      => 'wpi_item',
476
        'orderby'        => 'title',
477
        'order'          => 'ASC',
478
        'posts_per_page' => $args['number']
479
    );
480
481
    $item_args  = apply_filters( 'wpinv_item_dropdown_query_args', $item_args, $args, $defaults );
482
483
    $items      = get_posts( $item_args );
484
    $options    = array();
485
    if ( $items ) {
486
        foreach ( $items as $item ) {
487
            $title = esc_html( $item->post_title );
488
            
489
            if ( !empty( $args['show_recurring'] ) ) {
490
                $title .= wpinv_get_item_suffix( $item->ID, false );
491
            }
492
            
493
            $options[ absint( $item->ID ) ] = $title;
494
        }
495
    }
496
497
    // This ensures that any selected items are included in the drop down
498
    if( is_array( $args['selected'] ) ) {
499
        foreach( $args['selected'] as $item ) {
500
            if( ! in_array( $item, $options ) ) {
501
                $title = get_the_title( $item );
502
                if ( !empty( $args['show_recurring'] ) ) {
503
                    $title .= wpinv_get_item_suffix( $item, false );
504
                }
505
                $options[$item] = $title;
506
            }
507
        }
508
    } elseif ( is_numeric( $args['selected'] ) && $args['selected'] !== 0 ) {
509
        if ( ! in_array( $args['selected'], $options ) ) {
510
            $title = get_the_title( $args['selected'] );
511
            if ( !empty( $args['show_recurring'] ) ) {
512
                $title .= wpinv_get_item_suffix( $args['selected'], false );
513
            }
514
            $options[$args['selected']] = get_the_title( $args['selected'] );
515
        }
516
    }
517
518
    $output = wpinv_html_select( array(
519
        'name'             => $args['name'],
520
        'selected'         => $args['selected'],
521
        'id'               => $args['id'],
522
        'class'            => $args['class'],
523
        'options'          => $options,
524
        'multiple'         => $args['multiple'],
525
        'placeholder'      => $args['placeholder'],
526
        'show_option_all'  => $args['show_option_all'],
527
        'show_option_none' => $args['show_option_none'],
528
        'data'             => $args['data'],
529
    ) );
530
531
    return $output;
532
}
533
534
function wpinv_html_checkbox( $args = array() ) {
535
    $defaults = array(
536
        'name'     => null,
537
        'current'  => null,
538
        'class'    => 'wpinv-checkbox',
539
        'options'  => array(
540
            'disabled' => false,
541
            'readonly' => false
542
        )
543
    );
544
545
    $args = wp_parse_args( $args, $defaults );
546
547
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
548
    $options = '';
549
    if ( ! empty( $args['options']['disabled'] ) ) {
550
        $options .= ' disabled="disabled"';
551
    } elseif ( ! empty( $args['options']['readonly'] ) ) {
552
        $options .= ' readonly';
553
    }
554
555
    $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 ) . ' />';
556
557
    return $output;
558
}
559
560
function wpinv_html_text( $args = array() ) {
561
    // Backwards compatibility
562
    if ( func_num_args() > 1 ) {
563
        $args = func_get_args();
564
565
        $name  = $args[0];
566
        $value = isset( $args[1] ) ? $args[1] : '';
567
        $label = isset( $args[2] ) ? $args[2] : '';
568
        $desc  = isset( $args[3] ) ? $args[3] : '';
569
    }
570
571
    $defaults = array(
572
        'id'           => '',
573
        'name'         => isset( $name )  ? $name  : 'text',
574
        'value'        => isset( $value ) ? $value : null,
575
        'label'        => isset( $label ) ? $label : null,
576
        'desc'         => isset( $desc )  ? $desc  : null,
577
        'placeholder'  => '',
578
        'class'        => 'regular-text',
579
        'disabled'     => false,
580
        'readonly'     => false,
581
        'required'     => false,
582
        'autocomplete' => '',
583
        'data'         => false
584
    );
585
586
    $args = wp_parse_args( $args, $defaults );
587
588
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
589
    $options = '';
590
    if( $args['required'] ) {
591
        $options .= ' required="required"';
592
    }
593
    if( $args['readonly'] ) {
594
        $options .= ' readonly';
595
    }
596
    if( $args['readonly'] ) {
597
        $options .= ' readonly';
598
    }
599
600
    $data = '';
601
    if ( !empty( $args['data'] ) ) {
602
        foreach ( $args['data'] as $key => $value ) {
603
            $data .= 'data-' . wpinv_sanitize_key( $key ) . '="' . esc_attr( $value ) . '" ';
604
        }
605
    }
606
607
    $output = '<span id="wpinv-' . wpinv_sanitize_key( $args['name'] ) . '-wrap">';
608
    $output .= '<label class="wpinv-label" for="' . wpinv_sanitize_key( $args['id'] ) . '">' . esc_html( $args['label'] ) . '</label>';
609
    if ( ! empty( $args['desc'] ) ) {
610
        $output .= '<span class="wpinv-description">' . esc_html( $args['desc'] ) . '</span>';
611
    }
612
613
    $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 ) . '/>';
614
615
    $output .= '</span>';
616
617
    return $output;
618
}
619
620
function wpinv_html_date_field( $args = array() ) {
621
    if( empty( $args['class'] ) ) {
622
        $args['class'] = 'wpiDatepicker';
623
    } elseif( ! strpos( $args['class'], 'wpiDatepicker' ) ) {
624
        $args['class'] .= ' wpiDatepicker';
625
    }
626
627
    return wpinv_html_text( $args );
628
}
629
630
function wpinv_html_textarea( $args = array() ) {
631
    $defaults = array(
632
        'name'        => 'textarea',
633
        'value'       => null,
634
        'label'       => null,
635
        'desc'        => null,
636
        'class'       => 'large-text',
637
        'disabled'    => false,
638
        'placeholder' => '',
639
    );
640
641
    $args = wp_parse_args( $args, $defaults );
642
643
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
644
    $disabled = '';
645
    if( $args['disabled'] ) {
646
        $disabled = ' disabled="disabled"';
647
    }
648
649
    $output = '<span id="wpinv-' . wpinv_sanitize_key( $args['name'] ) . '-wrap">';
650
    $output .= '<label class="wpinv-label" for="' . wpinv_sanitize_key( $args['name'] ) . '">' . esc_html( $args['label'] ) . '</label>';
651
    $output .= '<textarea name="' . esc_attr( $args['name'] ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" id="' . wpinv_sanitize_key( $args['name'] ) . '" class="' . $class . '"' . $disabled . '>' . esc_attr( $args['value'] ) . '</textarea>';
652
653
    if ( ! empty( $args['desc'] ) ) {
654
        $output .= '<span class="wpinv-description">' . esc_html( $args['desc'] ) . '</span>';
655
    }
656
    $output .= '</span>';
657
658
    return $output;
659
}
660
661
function wpinv_html_ajax_user_search( $args = array() ) {
662
    $defaults = array(
663
        'name'        => 'user_id',
664
        'value'       => null,
665
        'placeholder' => __( 'Enter username', 'invoicing' ),
666
        'label'       => null,
667
        'desc'        => null,
668
        'class'       => '',
669
        'disabled'    => false,
670
        'autocomplete'=> 'off',
671
        'data'        => false
672
    );
673
674
    $args = wp_parse_args( $args, $defaults );
675
676
    $args['class'] = 'wpinv-ajax-user-search ' . $args['class'];
677
678
    $output  = '<span class="wpinv_user_search_wrap">';
679
        $output .= wpinv_html_text( $args );
680
        $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>';
681
    $output .= '</span>';
682
683
    return $output;
684
}
685
686
function wpinv_ip_geolocation() {
687
    global $wpinv_euvat;
688
    
689
    $ip         = !empty( $_GET['ip'] ) ? sanitize_text_field( $_GET['ip'] ) : '';    
690
    $content    = '';
691
    $iso        = '';
692
    $country    = '';
693
    $region     = '';
694
    $city       = '';
695
    $longitude  = '';
696
    $latitude   = '';
697
    $credit     = '';
698
    $address    = '';
699
    
700
    if ( wpinv_get_option( 'vat_ip_lookup' ) == 'geoip2' && $geoip2_city = $wpinv_euvat->geoip2_city_record( $ip ) ) {
701
        try {
702
            $iso        = $geoip2_city->country->isoCode;
703
            $country    = $geoip2_city->country->name;
704
            $region     = !empty( $geoip2_city->subdivisions ) && !empty( $geoip2_city->subdivisions[0]->name ) ? $geoip2_city->subdivisions[0]->name : '';
705
            $city       = $geoip2_city->city->name;
706
            $longitude  = $geoip2_city->location->longitude;
707
            $latitude   = $geoip2_city->location->latitude;
708
            $credit     = __( 'Geolocated using the information by MaxMind, available from <a href="http://www.maxmind.com" target="_blank">www.maxmind.com</a>', 'invoicing' );
709
        } catch( Exception $e ) { }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
710
    }
711
    
712
    if ( !( $iso && $longitude && $latitude ) && function_exists( 'simplexml_load_file' ) ) {
713
        try {
714
            $load_xml = simplexml_load_file( 'http://www.geoplugin.net/xml.gp?ip=' . $ip );
715
            
716
            if ( !empty( $load_xml ) && isset( $load_xml->geoplugin_countryCode ) && !empty( $load_xml->geoplugin_latitude ) && !empty( $load_xml->geoplugin_longitude ) ) {
717
                $iso        = $load_xml->geoplugin_countryCode;
718
                $country    = $load_xml->geoplugin_countryName;
719
                $region     = !empty( $load_xml->geoplugin_regionName ) ? $load_xml->geoplugin_regionName : '';
720
                $city       = !empty( $load_xml->geoplugin_city ) ? $load_xml->geoplugin_city : '';
721
                $longitude  = $load_xml->geoplugin_longitude;
722
                $latitude   = $load_xml->geoplugin_latitude;
723
                $credit     = $load_xml->geoplugin_credit;
0 ignored issues
show
Unused Code introduced by
The assignment to $credit is dead and can be removed.
Loading history...
724
                $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;
725
            }
726
        } catch( Exception $e ) { }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
727
    }
728
    
729
    if ( $iso && $longitude && $latitude ) {
730
        if ( $city ) {
731
            $address .= $city . ', ';
732
        }
733
        
734
        if ( $region ) {
735
            $address .= $region . ', ';
736
        }
737
        
738
        $address .= $country . ' (' . $iso . ')';
739
        $content = '<p>'. sprintf( __( '<b>Address:</b> %s', 'invoicing' ), $address ) . '</p>';
740
        $content .= '<p>'. $credit . '</p>';
741
    } else {
742
        $content = '<p>'. sprintf( __( 'Unable to find geolocation for the IP address: %s', 'invoicing' ), $ip ) . '</p>';
743
    }
744
    ?>
745
<!DOCTYPE html>
746
<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>
747
<body>
748
    <?php if ( $latitude && $latitude ) { ?>
749
    <div id="map"></div>
750
        <script src="//cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.0-rc.1/leaflet.js"></script>
751
        <script type="text/javascript">
752
        var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
753
            osmAttrib = '&copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors',
754
            osm = L.tileLayer(osmUrl, {maxZoom: 18, attribution: osmAttrib}),
755
            latlng = new L.LatLng(<?php echo $latitude;?>, <?php echo $longitude;?>);
756
757
        var map = new L.Map('map', {center: latlng, zoom: 12, layers: [osm]});
758
759
        var marker = new L.Marker(latlng);
760
        map.addLayer(marker);
761
762
        marker.bindPopup("<p><?php esc_attr_e( $address );?></p>");
763
    </script>
764
    <?php } ?>
765
    <div style="height:100px"><?php echo $content; ?></div>
766
</body></html>
767
<?php
768
    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
769
}
770
add_action( 'wp_ajax_wpinv_ip_geolocation', 'wpinv_ip_geolocation' );
771
add_action( 'wp_ajax_nopriv_wpinv_ip_geolocation', 'wpinv_ip_geolocation' );
772
773
// Set up the template for the invoice.
774
function wpinv_template( $template ) {
775
    global $post, $wp_query;
776
    
777
    if ( ( is_single() || is_404() ) && !empty( $post->ID ) && (get_post_type( $post->ID ) == 'wpi_invoice' or get_post_type( $post->ID ) == 'wpi_quote')) {
778
        if ( wpinv_user_can_view_invoice( $post->ID ) ) {
779
            $template = wpinv_get_template_part( 'wpinv-invoice-print', false, false );
780
        } else {
781
            $template = wpinv_get_template_part( 'wpinv-invalid-access', false, false );
782
        }
783
    }
784
785
    return $template;
786
}
787
788
function wpinv_get_business_address() {
789
    $business_address   = wpinv_store_address();
790
    $business_address   = !empty( $business_address ) ? wpautop( wp_kses_post( $business_address ) ) : '';
791
    
792
    /*
793
    $default_country    = wpinv_get_default_country();
794
    $default_state      = wpinv_get_default_state();
795
    
796
    $address_fields = array();
797
    if ( !empty( $default_state ) ) {
798
        $address_fields[] = wpinv_state_name( $default_state, $default_country );
799
    }
800
    
801
    if ( !empty( $default_country ) ) {
802
        $address_fields[] = wpinv_country_name( $default_country );
803
    }
804
    
805
    if ( !empty( $address_fields ) ) {
806
        $address_fields = implode( ", ", $address_fields );
807
                
808
        $business_address .= wpautop( wp_kses_post( $address_fields ) );
809
    }
810
    */
811
    
812
    $business_address = $business_address ? '<div class="address">' . $business_address . '</div>' : '';
813
    
814
    return apply_filters( 'wpinv_get_business_address', $business_address );
815
}
816
817
function wpinv_display_from_address() {
818
    global $wpinv_euvat;
819
    
820
    $from_name = $wpinv_euvat->get_company_name();
821
    if (empty($from_name)) {
822
        $from_name = wpinv_get_business_name();
823
    }
824
    ?><div class="from col-xs-2"><strong><?php _e( 'From:', 'invoicing' ) ?></strong></div>
825
    <div class="wrapper col-xs-10">
826
        <div class="name"><?php echo esc_html( $from_name ); ?></div>
827
        <?php if ( $address = wpinv_get_business_address() ) { ?>
828
        <div class="address"><?php echo wpautop( wp_kses_post( $address ) );?></div>
829
        <?php } ?>
830
        <?php if ( $email_from = wpinv_mail_get_from_address() ) { ?>
831
        <div class="email_from"><?php echo wp_sprintf( __( 'Email: %s', 'invoicing' ), $email_from );?></div>
832
        <?php } ?>
833
    </div>
834
    <?php
835
}
836
837
function wpinv_watermark( $id = 0 ) {
838
    $output = wpinv_get_watermark( $id );
839
    
840
    return apply_filters( 'wpinv_get_watermark', $output, $id );
841
}
842
843
function wpinv_get_watermark( $id ) {
844
    if ( !$id > 0 ) {
845
        return NULL;
846
    }
847
    $invoice = wpinv_get_invoice( $id );
848
    
849
    if ( !empty( $invoice ) && "wpi_invoice" === $invoice->post_type ) {
850
        if ( $invoice->is_paid() ) {
851
            return __( 'Paid', 'invoicing' );
852
        }
853
        if ( $invoice->is_refunded() ) {
854
            return __( 'Refunded', 'invoicing' );
855
        }
856
        if ( $invoice->has_status( array( 'wpi-cancelled' ) ) ) {
857
            return __( 'Cancelled', 'invoicing' );
858
        }
859
    }
860
    
861
    return NULL;
862
}
863
864
function wpinv_display_invoice_details( $invoice ) {
865
    global $wpinv_euvat;
866
    
867
    $invoice_id = $invoice->ID;
868
    $vat_name   = $wpinv_euvat->get_vat_name();
869
    $use_taxes  = wpinv_use_taxes();
870
    
871
    $invoice_status = wpinv_get_invoice_status( $invoice_id );
872
    ?>
873
    <table class="table table-bordered table-sm">
874
        <?php if ( $invoice_number = wpinv_get_invoice_number( $invoice_id ) ) { ?>
875
            <tr class="wpi-row-number">
876
                <th><?php echo apply_filters( 'wpinv_invoice_number_label', __( 'Invoice Number', 'invoicing' ), $invoice ); ?></th>
877
                <td><?php echo esc_html( $invoice_number ); ?></td>
878
            </tr>
879
        <?php } ?>
880
        <tr class="wpi-row-status">
881
            <th><?php echo apply_filters( 'wpinv_invoice_status_label', __( 'Invoice Status', 'invoicing' ), $invoice ); ?></th>
882
            <td><?php echo wpinv_invoice_status_label( $invoice_status, wpinv_get_invoice_status( $invoice_id, true ) ); ?></td>
883
        </tr>
884
        <?php if ( $invoice->is_renewal() ) { ?>
885
        <tr class="wpi-row-parent">
886
            <th><?php echo apply_filters( 'wpinv_invoice_parent_invoice_label', __( 'Parent Invoice', 'invoicing' ), $invoice ); ?></th>
887
            <td><?php echo wpinv_invoice_link( $invoice->parent_invoice ); ?></td>
888
        </tr>
889
        <?php } ?>
890
        <?php if ( ( $gateway_name = wpinv_get_payment_gateway_name( $invoice_id ) ) && ( $invoice->is_paid() || $invoice->is_refunded() ) ) { ?>
891
            <tr class="wpi-row-gateway">
892
                <th><?php echo apply_filters( 'wpinv_invoice_payment_method_label', __( 'Payment Method', 'invoicing' ), $invoice ); ?></th>
893
                <td><?php echo $gateway_name; ?></td>
894
            </tr>
895
        <?php } ?>
896
        <?php if ( $invoice_date = wpinv_get_invoice_date( $invoice_id ) ) { ?>
897
            <tr class="wpi-row-date">
898
                <th><?php echo apply_filters( 'wpinv_invoice_date_label', __( 'Invoice Date', 'invoicing' ), $invoice ); ?></th>
899
                <td><?php echo $invoice_date; ?></td>
900
            </tr>
901
        <?php } ?>
902
        <?php do_action( 'wpinv_display_details_before_due_date', $invoice_id ); ?>
903
        <?php if ( wpinv_get_option( 'overdue_active' ) && $invoice->needs_payment() && ( $due_date = $invoice->get_due_date( true ) ) ) { ?>
904
            <tr class="wpi-row-date">
905
                <th><?php echo apply_filters( 'wpinv_invoice_due_date_label', __( 'Due Date', 'invoicing' ), $invoice ); ?></th>
906
                <td><?php echo $due_date; ?></td>
907
            </tr>
908
        <?php } ?>
909
        <?php do_action( 'wpinv_display_details_after_due_date', $invoice_id ); ?>
910
        <?php if ( $owner_vat_number = $wpinv_euvat->get_vat_number() ) { ?>
911
            <tr class="wpi-row-ovatno">
912
                <th><?php echo apply_filters( 'wpinv_invoice_owner_vat_number_label', wp_sprintf( __( 'Owner %s Number', 'invoicing' ), $vat_name ), $invoice, $vat_name ); ?></th>
913
                <td><?php echo $owner_vat_number; ?></td>
914
            </tr>
915
        <?php } ?>
916
        <?php if ( $use_taxes && ( $user_vat_number = wpinv_get_invoice_vat_number( $invoice_id ) ) ) { ?>
917
            <tr class="wpi-row-uvatno">
918
                <th><?php echo apply_filters( 'wpinv_invoice_user_vat_number_label', wp_sprintf( __( 'Invoice %s Number', 'invoicing' ), $vat_name ), $invoice, $vat_name ); ?></th>
919
                <td><?php echo $user_vat_number; ?></td>
920
            </tr>
921
        <?php } ?>
922
        <tr class="table-active tr-total wpi-row-total">
923
            <th><strong><?php _e( 'Total Amount', 'invoicing' ) ?></strong></th>
924
            <td><strong><?php echo wpinv_payment_total( $invoice_id, true ); ?></strong></td>
925
        </tr>
926
        <?php if ( $subscription = wpinv_get_subscription( $invoice_id ) ) { ?>
927
        <tr class="table-active wpi-row-recurring-total">
928
            <th><?php echo apply_filters( 'wpinv_invoice_parent_invoice_label', __( 'Recurring Amount', 'invoicing' ), $invoice ); ?></th>
929
            <td><strong><?php echo wpinv_price( wpinv_format_amount( $subscription->recurring_amount ), $invoice->get_currency() ); ?></strong></td>
930
        </tr>
931
        <tr class="wpi-row-expires">
932
            <th><?php echo apply_filters( 'wpinv_invoice_parent_invoice_label', __( 'Renews On', 'invoicing' ), $invoice ); ?></th>
933
            <td><?php echo sanitize_text_field( $subscription->expiration ); ?></td>
934
        </tr>
935
        <?php } ?>
936
    </table>
937
<?php
938
}
939
940
/**
941
 * Retrieves the address markup to use on Invoices.
942
 * 
943
 * @since 1.0.13
944
 * @see `wpinv_get_full_address_format`
945
 * @see `wpinv_get_invoice_address_replacements`
946
 * @param array $billing_details customer's billing details
947
 * @param  string $separator How to separate address lines.
948
 * @return string
949
 */
950
function wpinv_get_invoice_address_markup( $billing_details, $separator = '<br/>' ) {
951
952
    // Retrieve the address markup...
953
    $country= empty( $billing_details['country'] ) ? '' : $billing_details['country'];
954
    $format = wpinv_get_full_address_format( $country );
955
956
    // ... and the replacements.
957
    $replacements = wpinv_get_invoice_address_replacements( $billing_details );
958
959
    $formatted_address = str_ireplace( array_keys( $replacements ), $replacements, $format );
960
    
961
	// Remove unavailable tags.
962
    $formatted_address = preg_replace( "/\{\{\w+\}\}/", '', $formatted_address );
963
964
    // Clean up white space.
965
	$formatted_address = preg_replace( '/  +/', ' ', trim( $formatted_address ) );
966
    $formatted_address = preg_replace( '/\n\n+/', "\n", $formatted_address );
967
    
968
    // Break newlines apart and remove empty lines/trim commas and white space.
969
	$formatted_address = array_filter( array_map( 'wpinv_trim_formatted_address_line', explode( "\n", $formatted_address ) ) );
970
971
    // Add html breaks.
972
	$formatted_address = implode( $separator, $formatted_address );
973
974
	// We're done!
975
	return $formatted_address;
976
    
977
}
978
979
function wpinv_display_to_address( $invoice_id = 0 ) {
980
    $invoice = wpinv_get_invoice( $invoice_id );
981
    
982
    if ( empty( $invoice ) ) {
983
        return NULL;
984
    }
985
    
986
    $billing_details = $invoice->get_user_info();
987
    $output = '<div class="to col-xs-2"><strong>' . __( 'To:', 'invoicing' ) . '</strong></div>';
988
    $output .= '<div class="wrapper col-xs-10">';
989
    
990
    ob_start();
991
    do_action( 'wpinv_display_to_address_top', $invoice );
992
    $output .= ob_get_clean();
993
    
994
    $address_row = wpinv_get_invoice_address_markup( $billing_details );
995
996
    if ( $address_row ) {
997
        $output .= '<div class="address">' . $address_row . '</div>';
998
    }
999
1000
    if ( $phone = $invoice->get_phone() ) {
1001
        $output .= '<div class="phone">' . wp_sprintf( __( 'Phone: %s', 'invoicing' ), esc_html( $phone ) ) . '</div>';
1002
    }
1003
    if ( $email = $invoice->get_email() ) {
1004
        $output .= '<div class="email">' . wp_sprintf( __( 'Email: %s' , 'invoicing'), esc_html( $email ) ) . '</div>';
1005
    }
1006
1007
    ob_start();
1008
    do_action( 'wpinv_display_to_address_bottom', $invoice );
1009
    $output .= ob_get_clean();
1010
    
1011
    $output .= '</div>';
1012
    $output = apply_filters( 'wpinv_display_to_address', $output, $invoice );
1013
1014
    echo $output;
1015
}
1016
1017
function wpinv_display_line_items( $invoice_id = 0 ) {
1018
    global $wpinv_euvat, $ajax_cart_details;
1019
    $invoice            = wpinv_get_invoice( $invoice_id );
1020
    $quantities_enabled = wpinv_item_quantities_enabled();
1021
    $use_taxes          = wpinv_use_taxes();
1022
    if ( !$use_taxes && (float)$invoice->get_tax() > 0 ) {
1023
        $use_taxes = true;
1024
    }
1025
    $zero_tax           = !(float)$invoice->get_tax() > 0 ? true : false;
1026
    $tax_label           = $use_taxes && $invoice->has_vat() ? $wpinv_euvat->get_vat_name() : __( 'Tax', 'invoicing' );
1027
    $tax_title          = !$zero_tax && $use_taxes ? ( wpinv_prices_include_tax() ? wp_sprintf( __( '(%s Incl.)', 'invoicing' ), $tax_label ) : wp_sprintf( __( '(%s Excl.)', 'invoicing' ), $tax_label ) ) : '';
1028
1029
    $cart_details       = $invoice->get_cart_details();
1030
    $ajax_cart_details  = $cart_details;
1031
    ob_start();
1032
    ?>
1033
    <table class="table table-sm table-bordered">
1034
        <thead>
1035
            <tr>
1036
                <th class="name"><strong><?php _e( "Item Name", "invoicing" );?></strong></th>
1037
                <th class="rate"><strong><?php _e( "Price", "invoicing" );?></strong></th>
1038
                <?php if ($quantities_enabled) { ?>
1039
                    <th class="qty"><strong><?php _e( "Qty", "invoicing" );?></strong></th>
1040
                <?php } ?>
1041
                <?php if ($use_taxes && !$zero_tax) { ?>
1042
                    <th class="tax"><strong><?php echo $tax_label . ' <span class="normal small">(%)</span>'; ?></strong></th>
1043
                <?php } ?>
1044
                <th class="total"><strong><?php echo __( "Item Total", "invoicing" ) . ' <span class="normal small">' . $tax_title . '<span>';?></strong></th>
1045
            </tr>
1046
        </thead>
1047
        <tbody>
1048
        <?php 
1049
            if ( !empty( $cart_details ) ) {
1050
                do_action( 'wpinv_display_line_items_start', $invoice );
1051
1052
                $count = 0;
1053
                $cols  = 3;
1054
                foreach ( $cart_details as $key => $cart_item ) {
1055
                    $item_id    = !empty($cart_item['id']) ? absint( $cart_item['id'] ) : '';
1056
                    $item_price = isset($cart_item["item_price"]) ? wpinv_round_amount( $cart_item["item_price"] ) : 0;
1057
                    $line_total = isset($cart_item["subtotal"]) ? wpinv_round_amount( $cart_item["subtotal"] ) : 0;
1058
                    $quantity   = !empty($cart_item['quantity']) && (int)$cart_item['quantity'] > 0 ? absint( $cart_item['quantity'] ) : 1;
1059
1060
                    $item       = $item_id ? new WPInv_Item( $item_id ) : NULL;
1061
                    $summary    = '';
1062
	                $item_name    = '';
1063
                    $cols       = 3;
1064
                    if ( !empty($item) ) {
1065
                        $item_name  = $item->get_name();
1066
                        $summary    = $item->get_summary();
1067
                    }
1068
                    $item_name  = !empty($cart_item['name']) ? $cart_item['name'] : $item_name;
1069
1070
                    $summary = apply_filters( 'wpinv_print_invoice_line_item_summary', $summary, $cart_item, $item, $invoice );
1071
1072
                    $item_tax       = '';
1073
                    $tax_rate       = '';
1074
                    if ( $use_taxes && $cart_item['tax'] > 0 && $cart_item['subtotal'] > 0 ) {
1075
                        $item_tax = wpinv_price( wpinv_format_amount( $cart_item['tax'] ), $invoice->get_currency() );
1076
                        $tax_rate = !empty( $cart_item['vat_rate'] ) ? $cart_item['vat_rate'] : ( $cart_item['tax'] / $cart_item['subtotal'] ) * 100;
1077
                        $tax_rate = $tax_rate > 0 ? (float)wpinv_round_amount( $tax_rate, 4 ) : '';
1078
                        $tax_rate = $tax_rate != '' ? ' <small class="tax-rate">(' . $tax_rate . '%)</small>' : '';
1079
                    }
1080
1081
                    $line_item_tax = $item_tax . $tax_rate;
1082
1083
                    if ( $line_item_tax === '' ) {
1084
                        $line_item_tax = 0; // Zero tax
1085
                    }
1086
1087
                    $action = apply_filters( 'wpinv_display_line_item_action', '', $cart_item, $invoice, $cols );
1088
1089
                    $line_item = '<tr class="row-' . ( ($count % 2 == 0) ? 'even' : 'odd' ) . ' wpinv-item">';
1090
                        $line_item .= '<td class="name">' . $action. esc_html__( $item_name, 'invoicing' ) . wpinv_get_item_suffix( $item );
1091
                        if ( $summary !== '' ) {
1092
                            $line_item .= '<br/><small class="meta">' . wpautop( wp_kses_post( $summary ) ) . '</small>';
1093
                        }
1094
                        $line_item .= '</td>';
1095
1096
                        $line_item .= '<td class="rate">' . esc_html__( wpinv_price( wpinv_format_amount( $item_price ), $invoice->get_currency() ) ) . '</td>';
1097
                        if ($quantities_enabled) {
1098
                            $cols++;
1099
                            $line_item .= '<td class="qty">' . $quantity . '</td>';
1100
                        }
1101
                        if ($use_taxes && !$zero_tax) {
1102
                            $cols++;
1103
                            $line_item .= '<td class="tax">' . $line_item_tax . '</td>';
1104
                        }
1105
                        $line_item .= '<td class="total">' . esc_html__( wpinv_price( wpinv_format_amount( $line_total ), $invoice->get_currency() ) ) . '</td>';
1106
                    $line_item .= '</tr>';
1107
1108
                    echo apply_filters( 'wpinv_display_line_item', $line_item, $cart_item, $invoice, $cols );
1109
1110
                    $count++;
1111
                }
1112
1113
                do_action( 'wpinv_display_before_subtotal', $invoice, $cols );
1114
                ?>
1115
                <tr class="row-sub-total row_odd">
1116
                    <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php echo apply_filters( 'wpinv_print_cart_subtotal_label', '<strong>' . __( 'Sub Total', 'invoicing' ) . ':</strong>', $invoice ); ?></td>
1117
                    <td class="total"><strong><?php _e( wpinv_subtotal( $invoice_id, true ) ) ?></strong></td>
1118
                </tr>
1119
                <?php
1120
                do_action( 'wpinv_display_after_subtotal', $invoice, $cols );
1121
                
1122
                if ( wpinv_discount( $invoice_id, false ) > 0 ) {
1123
                    do_action( 'wpinv_display_before_discount', $invoice, $cols );
1124
                    ?>
1125
                        <tr class="row-discount">
1126
                            <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php wpinv_get_discount_label( wpinv_discount_code( $invoice_id ) ); ?>:</td>
1127
                            <td class="total"><?php echo wpinv_discount( $invoice_id, true, true ); ?></td>
1128
                        </tr>
1129
                    <?php
1130
                    do_action( 'wpinv_display_after_discount', $invoice, $cols );
1131
                }
1132
1133
                if ( $use_taxes ) {
1134
                    do_action( 'wpinv_display_before_tax', $invoice, $cols );
1135
                    ?>
1136
                    <tr class="row-tax">
1137
                        <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php echo apply_filters( 'wpinv_print_cart_tax_label', '<strong>' . $tax_label . ':</strong>', $invoice ); ?></td>
1138
                        <td class="total"><?php _e( wpinv_tax( $invoice_id, true ) ) ?></td>
1139
                    </tr>
1140
                    <?php
1141
                    do_action( 'wpinv_display_after_tax', $invoice, $cols );
1142
                }
1143
1144
                do_action( 'wpinv_display_before_total', $invoice, $cols );
1145
                ?>
1146
                <tr class="table-active row-total">
1147
                    <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php echo apply_filters( 'wpinv_print_cart_total_label', '<strong>' . __( 'Total', 'invoicing' ) . ':</strong>', $invoice ); ?></td>
1148
                    <td class="total"><strong><?php _e( wpinv_payment_total( $invoice_id, true ) ) ?></strong></td>
1149
                </tr>
1150
                <?php
1151
                do_action( 'wpinv_display_after_total', $invoice, $cols );
1152
1153
                do_action( 'wpinv_display_line_end', $invoice, $cols );
1154
            }
1155
        ?>
1156
        </tbody>
1157
    </table>
1158
    <?php
1159
    echo ob_get_clean();
1160
}
1161
1162
/**
1163
 * @param WPInv_Invoice $invoice
1164
 */
1165
function wpinv_display_invoice_notes( $invoice ) {
1166
1167
    $notes = wpinv_get_invoice_notes( $invoice->ID, 'customer' );
1168
1169
    if ( empty( $notes ) ) {
1170
        return;
1171
    }
1172
1173
    echo '<div class="wpi_invoice_notes_container">';
1174
    echo '<h2>' . __( 'Invoice Notes', 'invoicing' ) .'</h2>';
1175
    echo '<ul class="wpi_invoice_notes">';
1176
1177
    foreach( $notes as $note ) {
1178
        wpinv_get_invoice_note_line_item( $note );
1179
    }
1180
1181
    echo '</ul>';
1182
    echo '</div>';
1183
}
1184
add_action( 'wpinv_invoice_print_after_line_items', 'wpinv_display_invoice_notes' );
1185
1186
function wpinv_display_invoice_totals( $invoice_id = 0 ) {
1187
    $use_taxes = wpinv_use_taxes();
1188
1189
    do_action( 'wpinv_before_display_totals_table', $invoice_id ); 
1190
    ?>
1191
    <table class="table table-sm table-bordered table-responsive">
1192
        <tbody>
1193
            <?php do_action( 'wpinv_before_display_totals' ); ?>
1194
            <tr class="row-sub-total">
1195
                <td class="rate"><strong><?php _e( 'Sub Total', 'invoicing' ); ?></strong></td>
1196
                <td class="total"><strong><?php _e( wpinv_subtotal( $invoice_id, true ) ) ?></strong></td>
1197
            </tr>
1198
            <?php do_action( 'wpinv_after_display_totals' ); ?>
1199
            <?php if ( wpinv_discount( $invoice_id, false ) > 0 ) { ?>
1200
                <tr class="row-discount">
1201
                    <td class="rate"><?php wpinv_get_discount_label( wpinv_discount_code( $invoice_id ) ); ?></td>
1202
                    <td class="total"><?php echo wpinv_discount( $invoice_id, true, true ); ?></td>
1203
                </tr>
1204
            <?php do_action( 'wpinv_after_display_discount' ); ?>
1205
            <?php } ?>
1206
            <?php if ( $use_taxes ) { ?>
1207
            <tr class="row-tax">
1208
                <td class="rate"><?php _e( 'Tax', 'invoicing' ); ?></td>
1209
                <td class="total"><?php _e( wpinv_tax( $invoice_id, true ) ) ?></td>
1210
            </tr>
1211
            <?php do_action( 'wpinv_after_display_tax' ); ?>
1212
            <?php } ?>
1213
            <?php if ( $fees = wpinv_get_fees( $invoice_id ) ) { ?>
1214
                <?php foreach ( $fees as $fee ) { ?>
1215
                    <tr class="row-fee">
1216
                        <td class="rate"><?php echo $fee['label']; ?></td>
1217
                        <td class="total"><?php echo $fee['amount_display']; ?></td>
1218
                    </tr>
1219
                <?php } ?>
1220
            <?php } ?>
1221
            <tr class="table-active row-total">
1222
                <td class="rate"><strong><?php _e( 'Total', 'invoicing' ) ?></strong></td>
1223
                <td class="total"><strong><?php _e( wpinv_payment_total( $invoice_id, true ) ) ?></strong></td>
1224
            </tr>
1225
            <?php do_action( 'wpinv_after_totals' ); ?>
1226
        </tbody>
1227
1228
    </table>
1229
1230
    <?php do_action( 'wpinv_after_totals_table' );
1231
}
1232
1233
function wpinv_display_payments_info( $invoice_id = 0, $echo = true ) {
1234
    $invoice = wpinv_get_invoice( $invoice_id );
1235
1236
    ob_start();
1237
    do_action( 'wpinv_before_display_payments_info', $invoice_id );
1238
    if ( ( $gateway_title = $invoice->get_gateway_title() ) || $invoice->is_paid() || $invoice->is_refunded() ) {
1239
        ?>
1240
        <div class="wpi-payment-info">
1241
            <p class="wpi-payment-gateway"><?php echo wp_sprintf( __( 'Payment via %s', 'invoicing' ), $gateway_title ? $gateway_title : __( 'Manually', 'invoicing' ) ); ?></p>
1242
            <?php if ( $gateway_title ) { ?>
1243
            <p class="wpi-payment-transid"><?php echo wp_sprintf( __( 'Transaction ID: %s', 'invoicing' ), $invoice->get_transaction_id() ); ?></p>
1244
            <?php } ?>
1245
        </div>
1246
        <?php
1247
    }
1248
    do_action( 'wpinv_after_display_payments_info', $invoice_id );
1249
    $outout = ob_get_clean();
1250
1251
    if ( $echo ) {
1252
        echo $outout;
1253
    } else {
1254
        return $outout;
1255
    }
1256
}
1257
1258
function wpinv_display_style( $invoice ) {
1259
    wp_register_style( 'wpinv-single-style', WPINV_PLUGIN_URL . 'assets/css/invoice.css', array(), WPINV_VERSION );
1260
1261
    wp_print_styles( 'open-sans' );
1262
    wp_print_styles( 'wpinv-single-style' );
1263
1264
    $custom_css = wpinv_get_option('template_custom_css');
1265
    if(isset($custom_css) && !empty($custom_css)){
1266
        $custom_css     = wp_kses( $custom_css, array( '\'', '\"' ) );
0 ignored issues
show
Bug introduced by
array(''', '\"') of type array<integer,string> is incompatible with the type array<mixed,array>|string expected by parameter $allowed_html of wp_kses(). ( Ignorable by Annotation )

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

1266
        $custom_css     = wp_kses( $custom_css, /** @scrutinizer ignore-type */ array( '\'', '\"' ) );
Loading history...
1267
        $custom_css     = str_replace( '&gt;', '>', $custom_css );
1268
        echo '<style type="text/css">';
1269
        echo $custom_css;
1270
        echo '</style>';
1271
    }
1272
}
1273
add_action( 'wpinv_invoice_print_head', 'wpinv_display_style' );
1274
add_action( 'wpinv_invalid_invoice_head', 'wpinv_display_style' );
1275
1276
function wpinv_checkout_billing_details() {
1277
    $invoice_id = (int)wpinv_get_invoice_cart_id();
1278
    if (empty($invoice_id)) {
1279
        wpinv_error_log( 'Invoice id not found', 'ERROR', __FILE__, __LINE__ );
1280
        return null;
1281
    }
1282
1283
    $invoice = wpinv_get_invoice_cart( $invoice_id );
1284
    if (empty($invoice)) {
1285
        wpinv_error_log( 'Invoice not found', 'ERROR', __FILE__, __LINE__ );
1286
        return null;
1287
    }
1288
    $user_id        = $invoice->get_user_id();
1289
    $user_info      = $invoice->get_user_info();
1290
    $address_info   = wpinv_get_user_address( $user_id );
1291
1292
    if ( empty( $user_info['first_name'] ) && !empty( $user_info['first_name'] ) ) {
1293
        $user_info['first_name'] = $user_info['first_name'];
1294
        $user_info['last_name'] = $user_info['last_name'];
1295
    }
1296
1297
    if ( ( ( empty( $user_info['country'] ) && !empty( $address_info['country'] ) ) || ( empty( $user_info['state'] ) && !empty( $address_info['state'] ) && $user_info['country'] == $address_info['country'] ) ) ) {
1298
        $user_info['country']   = $address_info['country'];
1299
        $user_info['state']     = $address_info['state'];
1300
        $user_info['city']      = $address_info['city'];
1301
        $user_info['zip']       = $address_info['zip'];
1302
    }
1303
1304
    $address_fields = array(
1305
        'user_id',
1306
        'company',
1307
        'vat_number',
1308
        'email',
1309
        'phone',
1310
        'address'
1311
    );
1312
1313
    foreach ( $address_fields as $field ) {
1314
        if ( empty( $user_info[$field] ) ) {
1315
            $user_info[$field] = $address_info[$field];
1316
        }
1317
    }
1318
1319
    return apply_filters( 'wpinv_checkout_billing_details', $user_info, $invoice );
1320
}
1321
1322
function wpinv_admin_get_line_items($invoice = array()) {
1323
    $item_quantities    = wpinv_item_quantities_enabled();
1324
    $use_taxes          = wpinv_use_taxes();
1325
1326
    if ( empty( $invoice ) ) {
1327
        return NULL;
1328
    }
1329
1330
    $cart_items = $invoice->get_cart_details();
1331
    if ( empty( $cart_items ) ) {
1332
        return NULL;
1333
    }
1334
1335
    ob_start();
1336
1337
    do_action( 'wpinv_admin_before_line_items', $cart_items, $invoice );
1338
1339
    $count = 0;
1340
    foreach ( $cart_items as $key => $cart_item ) {
1341
        $item_id    = $cart_item['id'];
1342
        $wpi_item   = $item_id > 0 ? new WPInv_Item( $item_id ) : NULL;
1343
1344
        if (empty($wpi_item)) {
1345
            continue;
1346
        }
1347
1348
        $item_price     = wpinv_price( wpinv_format_amount( $cart_item['item_price'] ), $invoice->get_currency() );
1349
        $quantity       = !empty( $cart_item['quantity'] ) && $cart_item['quantity'] > 0 ? $cart_item['quantity'] : 1;
1350
        $item_subtotal  = wpinv_price( wpinv_format_amount( $cart_item['subtotal'] ), $invoice->get_currency() );
1351
        $can_remove     = true;
1352
1353
        $summary = apply_filters( 'wpinv_admin_invoice_line_item_summary', '', $cart_item, $wpi_item, $invoice );
1354
1355
        $item_tax       = '';
1356
        $tax_rate       = '';
1357
        if ( $invoice->is_taxable() && $cart_item['tax'] > 0 && $cart_item['subtotal'] > 0 ) {
1358
            $item_tax = wpinv_price( wpinv_format_amount( $cart_item['tax'] ), $invoice->get_currency() );
1359
            $tax_rate = !empty( $cart_item['vat_rate'] ) ? $cart_item['vat_rate'] : ( $cart_item['tax'] / $cart_item['subtotal'] ) * 100;
1360
            $tax_rate = $tax_rate > 0 ? (float)wpinv_round_amount( $tax_rate, 4 ) : '';
1361
            $tax_rate = $tax_rate != '' ? ' <span class="tax-rate">(' . $tax_rate . '%)</span>' : '';
1362
        }
1363
        $line_item_tax = $item_tax . $tax_rate;
1364
1365
        if ( $line_item_tax === '' ) {
1366
            $line_item_tax = 0; // Zero tax
1367
        }
1368
1369
        $line_item = '<tr class="item item-' . ( ($count % 2 == 0) ? 'even' : 'odd' ) . '" data-item-id="' . $item_id . '">';
1370
            $line_item .= '<td class="id">' . $item_id . '</td>';
1371
            $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 );
1372
            if ( $summary !== '' ) {
1373
                $line_item .= '<span class="meta">' . wpautop( wp_kses_post( $summary ) ) . '</span>';
1374
            }
1375
            $line_item .= '</td>';
1376
            $line_item .= '<td class="price">' . $item_price . '</td>';
1377
            
1378
            if ( $item_quantities ) {
1379
                if ( count( $cart_items ) == 1 && $quantity <= 1 ) {
1380
                    $can_remove = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $can_remove is dead and can be removed.
Loading history...
1381
                }
1382
                $line_item .= '<td class="qty" data-quantity="' . $quantity . '">&nbsp;&times;&nbsp;' . $quantity . '</td>';
1383
            } else {
1384
                if ( count( $cart_items ) == 1 ) {
1385
                    $can_remove = false;
1386
                }
1387
            }
1388
            $line_item .= '<td class="total">' . $item_subtotal . '</td>';
1389
            
1390
            if ( $use_taxes ) {
1391
                $line_item .= '<td class="tax">' . $line_item_tax . '</td>';
1392
            }
1393
            $line_item .= '<td class="action">';
1394
            if ( !$invoice->is_paid() && !$invoice->is_refunded() ) {
1395
                $line_item .= '<i class="fa fa-remove wpinv-item-remove"></i>';
1396
            }
1397
            $line_item .= '</td>';
1398
        $line_item .= '</tr>';
1399
1400
        echo apply_filters( 'wpinv_admin_line_item', $line_item, $cart_item, $invoice );
1401
1402
        $count++;
1403
    } 
1404
1405
    do_action( 'wpinv_admin_after_line_items', $cart_items, $invoice );
1406
1407
    return ob_get_clean();
1408
}
1409
1410
function wpinv_checkout_form() {
1411
    global $wpi_checkout_id, $invoicing;
1412
1413
    // Set current invoice id.
1414
    $wpi_checkout_id = wpinv_get_invoice_cart_id();
1415
    $form_action     = esc_url( wpinv_get_checkout_uri() );
0 ignored issues
show
Bug introduced by
It seems like wpinv_get_checkout_uri() can also be of type false; however, parameter $url of esc_url() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

1415
    $form_action     = esc_url( /** @scrutinizer ignore-type */ wpinv_get_checkout_uri() );
Loading history...
1416
    $payment_form    = wpinv_get_default_payment_form();
1417
1418
    ob_start();
1419
	    do_action( 'wpinv_checkout_content_before' );
1420
1421
        if ( wpinv_get_cart_contents() ) {
1422
1423
            // Get the form elements and items.
1424
	        $elements = $invoicing->form_elements->get_form_elements( $payment_form );
1425
	        $items    = $invoicing->form_elements->convert_checkout_items( wpinv_get_cart_contents(), wpinv_get_invoice_cart() );
1426
            ?>
1427
            <form class="wpinv_payment_form" action="<?php echo $form_action; ?>" method="POST">
1428
                <?php do_action( 'wpinv_main_checkout_form_top' ); ?>
1429
                <input type='hidden' name='form_id' value='<?php echo esc_attr( $payment_form ); ?>'/>
1430
                <input type='hidden' name='invoice_id' value='<?php echo esc_attr( $wpi_checkout_id ); ?>'/>
1431
                    <?php
1432
                        wp_nonce_field( 'wpinv_payment_form', 'wpinv_payment_form' );
1433
                        wp_nonce_field( 'vat_validation', '_wpi_nonce' );
1434
1435
                        foreach ( $elements as $element ) {
1436
                            do_action( 'wpinv_frontend_render_payment_form_element', $element, $items, $payment_form );
1437
                            do_action( "wpinv_frontend_render_payment_form_{$element['type']}", $element, $items, $payment_form );
1438
                        }
1439
                    ?>
1440
                <div class='wpinv_payment_form_errors alert alert-danger d-none'></div>
1441
                <?php do_action( 'wpinv_main_checkout_form_bottom' ); ?>
1442
            </form>
1443
        <?php
1444
1445
        } else {
1446
            do_action( 'wpinv_cart_empty' );
1447
        }
1448
        echo '</div><!--end #wpinv_checkout_wrap-->';
1449
	    do_action( 'wpinv_checkout_content_after' );
1450
        $content = ob_get_clean();
1451
1452
		return str_replace( 'sr-only', '', $content );
1453
}
1454
1455
function wpinv_checkout_cart( $cart_details = array(), $echo = true ) {
1456
    global $ajax_cart_details;
1457
    $ajax_cart_details = $cart_details;
1458
1459
    ob_start();
1460
    do_action( 'wpinv_before_checkout_cart' );
1461
    echo '<div id="wpinv_checkout_cart_form" method="post">';
1462
        echo '<div id="wpinv_checkout_cart_wrap">';
1463
            wpinv_get_template_part( 'wpinv-checkout-cart' );
1464
        echo '</div>';
1465
    echo '</div>';
1466
    do_action( 'wpinv_after_checkout_cart' );
1467
    $content = ob_get_clean();
1468
1469
    if ( $echo ) {
1470
        echo $content;
1471
    } else {
1472
        return $content;
1473
    }
1474
}
1475
add_action( 'wpinv_checkout_cart', 'wpinv_checkout_cart', 10 );
1476
1477
function wpinv_empty_cart_message() {
1478
	return apply_filters( 'wpinv_empty_cart_message', '<span class="wpinv_empty_cart">' . __( 'Your cart is empty.', 'invoicing' ) . '</span>' );
1479
}
1480
1481
/**
1482
 * Echoes the Empty Cart Message
1483
 *
1484
 * @since 1.0
1485
 * @return void
1486
 */
1487
function wpinv_empty_checkout_cart() {
1488
    echo aui()->alert(
0 ignored issues
show
Bug introduced by
The function aui was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1488
    echo /** @scrutinizer ignore-call */ aui()->alert(
Loading history...
1489
        array(
1490
            'type'    => 'warning',
1491
            'content' => wpinv_empty_cart_message(),
1492
        )
1493
    );
1494
}
1495
add_action( 'wpinv_cart_empty', 'wpinv_empty_checkout_cart' );
1496
1497
function wpinv_update_cart_button() {
1498
    if ( !wpinv_item_quantities_enabled() )
1499
        return;
1500
?>
1501
    <input type="submit" name="wpinv_update_cart_submit" class="wpinv-submit wpinv-no-js button" value="<?php _e( 'Update Cart', 'invoicing' ); ?>"/>
1502
    <input type="hidden" name="wpi_action" value="update_cart"/>
1503
<?php
1504
}
1505
1506
function wpinv_checkout_cart_columns() {
1507
    $default = 3;
1508
    if ( wpinv_item_quantities_enabled() ) {
1509
        $default++;
1510
    }
1511
    
1512
    if ( wpinv_use_taxes() ) {
1513
        $default++;
1514
    }
1515
1516
    return apply_filters( 'wpinv_checkout_cart_columns', $default );
1517
}
1518
1519
function wpinv_display_cart_messages() {
1520
    global $wpi_session;
1521
1522
    $messages = $wpi_session->get( 'wpinv_cart_messages' );
1523
1524
    if ( $messages ) {
1525
        foreach ( $messages as $message_id => $message ) {
1526
            // Try and detect what type of message this is
1527
            if ( strpos( strtolower( $message ), 'error' ) ) {
1528
                $type = 'error';
1529
            } elseif ( strpos( strtolower( $message ), 'success' ) ) {
1530
                $type = 'success';
1531
            } else {
1532
                $type = 'info';
1533
            }
1534
1535
            $classes = apply_filters( 'wpinv_' . $type . '_class', array( 'wpinv_errors', 'wpinv-alert', 'wpinv-alert-' . $type ) );
1536
1537
            echo '<div class="' . implode( ' ', $classes ) . '">';
1538
                // Loop message codes and display messages
1539
                    echo '<p class="wpinv_error" id="wpinv_msg_' . $message_id . '">' . $message . '</p>';
1540
            echo '</div>';
1541
        }
1542
1543
        // Remove all of the cart saving messages
1544
        $wpi_session->set( 'wpinv_cart_messages', null );
1545
    }
1546
}
1547
add_action( 'wpinv_before_checkout_cart', 'wpinv_display_cart_messages' );
1548
1549
function wpinv_discount_field() {
1550
    if ( isset( $_GET['wpi-gateway'] ) && wpinv_is_ajax_disabled() ) {
1551
        return; // Only show before a payment method has been selected if ajax is disabled
1552
    }
1553
1554
    if ( !wpinv_is_checkout() ) {
1555
        return;
1556
    }
1557
1558
    if ( wpinv_has_active_discounts() && wpinv_get_cart_total() ) {
1559
    ?>
1560
    <div id="wpinv-discount-field" class="panel panel-default">
1561
        <div class="panel-body">
1562
            <p>
1563
                <label class="wpinv-label" for="wpinv_discount_code"><strong><?php _e( 'Discount', 'invoicing' ); ?></strong></label>
1564
                <span class="wpinv-description"><?php _e( 'Enter a discount code if you have one.', 'invoicing' ); ?></span>
1565
            </p>
1566
            <div class="form-group row">
1567
                <div class="col-sm-4">
1568
                    <input class="wpinv-input form-control" type="text" id="wpinv_discount_code" name="wpinv_discount_code" placeholder="<?php _e( 'Enter discount code', 'invoicing' ); ?>"/>
1569
                </div>
1570
                <div class="col-sm-3">
1571
                    <button id="wpi-apply-discount" type="button" class="btn btn-success btn-sm"><?php _e( 'Apply Discount', 'invoicing' ); ?></button>
1572
                </div>
1573
                <div style="clear:both"></div>
1574
                <div class="col-sm-12 wpinv-discount-msg">
1575
                    <div class="alert alert-success"><i class="fa fa-check-circle"></i><span class="wpi-msg"></span></div>
1576
                    <div class="alert alert-error"><i class="fa fa-warning"></i><span class="wpi-msg"></span></div>
1577
                </div>
1578
            </div>
1579
        </div>
1580
    </div>
1581
<?php
1582
    }
1583
}
1584
add_action( 'wpinv_after_checkout_cart', 'wpinv_discount_field', -10 );
1585
1586
function wpinv_agree_to_terms_js() {
1587
    if ( wpinv_get_option( 'show_agree_to_terms', false ) ) {
1588
?>
1589
<script type="text/javascript">
1590
    jQuery(document).ready(function($){
1591
        $( document.body ).on('click', '.wpinv_terms_links', function(e) {
1592
            //e.preventDefault();
1593
            $('#wpinv_terms').slideToggle();
1594
            $('.wpinv_terms_links').toggle();
1595
            return false;
1596
        });
1597
    });
1598
</script>
1599
<?php
1600
    }
1601
}
1602
add_action( 'wpinv_checkout_form_top', 'wpinv_agree_to_terms_js' );
1603
1604
function wpinv_payment_mode_select() {
1605
    $gateways = wpinv_get_enabled_payment_gateways( true );
1606
    $gateways = apply_filters( 'wpinv_payment_gateways_on_cart', $gateways );
1607
    $invoice = wpinv_get_invoice( 0, true );
1608
1609
    do_action('wpinv_payment_mode_top');
1610
    $invoice_id = $invoice ? (int)$invoice->ID : 0;
1611
    $chosen_gateway = wpinv_get_chosen_gateway( $invoice_id );
1612
    ?>
1613
    <div id="wpinv_payment_mode_select" data-gateway="<?php echo $chosen_gateway; ?>" <?php echo ( ( $invoice && $invoice->is_free() ) ? 'style="display:none;" data-free="1"' : '' ); ?>>
1614
            <?php do_action( 'wpinv_payment_mode_before_gateways_wrap' ); ?>
1615
            <div id="wpinv-payment-mode-wrap" class="panel panel-default">
1616
                <div class="panel-heading wpi-payment_methods_title"><h3 class="panel-title"><?php _e( 'Select Payment Method', 'invoicing' ); ?></h3></div>
1617
                <div class="panel-body list-group wpi-payment_methods">
1618
                    <?php
1619
                    do_action( 'wpinv_payment_mode_before_gateways' );
1620
1621
                    if ( !empty( $gateways ) ) {
1622
                        foreach ( $gateways as $gateway_id => $gateway ) {
1623
                            $checked       = checked( $gateway_id, $chosen_gateway, false );
1624
                            $button_label  = wpinv_get_gateway_button_label( $gateway_id );
1625
                            $gateway_label = wpinv_get_gateway_checkout_label( $gateway_id );
1626
                            $description   = wpinv_get_gateway_description( $gateway_id );
1627
                            ?>
1628
                            <div class="list-group-item">
1629
                                <div class="radio">
1630
                                    <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_label ); ?></label>
1631
                                </div>
1632
                                <div style="display:none;" class="payment_box wpi_gateway_<?php echo esc_attr( $gateway_id );?>" role="alert">
1633
                                    <?php if ( !empty( $description ) ) { ?>
1634
                                        <div class="wpi-gateway-desc alert alert-info"><?php _e( $description, 'invoicing' ); ?></div>
1635
                                    <?php } ?>
1636
                                    <?php do_action( 'wpinv_' . $gateway_id . '_cc_form', $invoice_id ) ;?>
1637
                                </div>
1638
                            </div>
1639
                            <?php
1640
                        }
1641
                    } else {
1642
                        echo '<div class="alert alert-warning">'. __( 'No payment gateway active', 'invoicing' ) .'</div>';
1643
                    }
1644
1645
                    do_action( 'wpinv_payment_mode_after_gateways' );
1646
                    ?>
1647
                </div>
1648
            </div>
1649
            <?php do_action( 'wpinv_payment_mode_after_gateways_wrap' ); ?>
1650
    </div>
1651
    <?php
1652
    do_action('wpinv_payment_mode_bottom');
1653
}
1654
add_action( 'wpinv_payment_mode_select', 'wpinv_payment_mode_select' );
1655
1656
function wpinv_checkout_billing_info() {
1657
    if ( wpinv_is_checkout() ) {
1658
        $billing_details    = wpinv_checkout_billing_details();
1659
        $selected_country   = !empty( $billing_details['country'] ) ? $billing_details['country'] : wpinv_default_billing_country();
1660
        ?>
1661
        <div id="wpinv-fields" class="clearfix">
1662
            <div id="wpi-billing" class="wpi-billing clearfix panel panel-default">
1663
                <div class="panel-heading"><h3 class="panel-title"><?php _e( 'Billing Details', 'invoicing' );?></h3></div>
1664
                <div id="wpinv-fields-box" class="panel-body">
1665
                    <?php do_action( 'wpinv_checkout_billing_fields_first', $billing_details ); ?>
1666
                    <p class="wpi-cart-field wpi-col2 wpi-colf">
1667
                        <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>
1668
                        <?php
1669
                        echo wpinv_html_text( array(
1670
                                'id'            => 'wpinv_first_name',
1671
                                'name'          => 'wpinv_first_name',
1672
                                'value'         => $billing_details['first_name'],
1673
                                'class'         => 'wpi-input form-control',
1674
                                'placeholder'   => __( 'First name', 'invoicing' ),
1675
                                'required'      => (bool)wpinv_get_option( 'fname_mandatory' ),
1676
                            ) );
1677
                        ?>
1678
                    </p>
1679
                    <p class="wpi-cart-field wpi-col2 wpi-coll">
1680
                        <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>
1681
                        <?php
1682
                        echo wpinv_html_text( array(
1683
                                'id'            => 'wpinv_last_name',
1684
                                'name'          => 'wpinv_last_name',
1685
                                'value'         => $billing_details['last_name'],
1686
                                'class'         => 'wpi-input form-control',
1687
                                'placeholder'   => __( 'Last name', 'invoicing' ),
1688
                                'required'      => (bool)wpinv_get_option( 'lname_mandatory' ),
1689
                            ) );
1690
                        ?>
1691
                    </p>
1692
                    <p class="wpi-cart-field wpi-col2 wpi-colf">
1693
                        <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>
1694
                        <?php
1695
                        echo wpinv_html_text( array(
1696
                                'id'            => 'wpinv_address',
1697
                                'name'          => 'wpinv_address',
1698
                                'value'         => $billing_details['address'],
1699
                                'class'         => 'wpi-input form-control',
1700
                                'placeholder'   => __( 'Address', 'invoicing' ),
1701
                                'required'      => (bool)wpinv_get_option( 'address_mandatory' ),
1702
                            ) );
1703
                        ?>
1704
                    </p>
1705
                    <p class="wpi-cart-field wpi-col2 wpi-coll">
1706
                        <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>
1707
                        <?php
1708
                        echo wpinv_html_text( array(
1709
                                'id'            => 'wpinv_city',
1710
                                'name'          => 'wpinv_city',
1711
                                'value'         => $billing_details['city'],
1712
                                'class'         => 'wpi-input form-control',
1713
                                'placeholder'   => __( 'City', 'invoicing' ),
1714
                                'required'      => (bool)wpinv_get_option( 'city_mandatory' ),
1715
                            ) );
1716
                        ?>
1717
                    </p>
1718
                    <p id="wpinv_country_box" class="wpi-cart-field wpi-col2 wpi-colf">
1719
                        <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>
1720
                        <?php echo wpinv_html_select( array(
1721
                            'options'          => wpinv_get_country_list(),
1722
                            'name'             => 'wpinv_country',
1723
                            'id'               => 'wpinv_country',
1724
                            'selected'         => $selected_country,
1725
                            'show_option_all'  => false,
1726
                            'show_option_none' => false,
1727
                            'class'            => 'wpi-input form-control wpi_select2',
1728
                            'placeholder'      => __( 'Choose a country', 'invoicing' ),
1729
                            'required'         => (bool)wpinv_get_option( 'country_mandatory' ),
1730
                        ) ); ?>
1731
                    </p>
1732
                    <p id="wpinv_state_box" class="wpi-cart-field wpi-col2 wpi-coll">
1733
                        <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>
1734
                        <?php
1735
                        $states = wpinv_get_country_states( $selected_country );
1736
                        if( !empty( $states ) ) {
1737
                            echo wpinv_html_select( array(
1738
                                'options'          => $states,
1739
                                'name'             => 'wpinv_state',
1740
                                'id'               => 'wpinv_state',
1741
                                'selected'         => $billing_details['state'],
1742
                                'show_option_all'  => false,
1743
                                'show_option_none' => false,
1744
                                'class'            => 'wpi-input form-control wpi_select2',
1745
                                'placeholder'      => __( 'Choose a state', 'invoicing' ),
1746
                                'required'         => (bool)wpinv_get_option( 'state_mandatory' ),
1747
                            ) );
1748
                        } else {
1749
                            echo wpinv_html_text( array(
1750
                                'name'          => 'wpinv_state',
1751
                                'value'         => $billing_details['state'],
1752
                                'id'            => 'wpinv_state',
1753
                                'class'         => 'wpi-input form-control',
1754
                                'placeholder'   => __( 'State / Province', 'invoicing' ),
1755
                                'required'      => (bool)wpinv_get_option( 'state_mandatory' ),
1756
                            ) );
1757
                        }
1758
                        ?>
1759
                    </p>
1760
                    <p class="wpi-cart-field wpi-col2 wpi-colf">
1761
                        <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>
1762
                        <?php
1763
                        echo wpinv_html_text( array(
1764
                                'name'          => 'wpinv_zip',
1765
                                'value'         => $billing_details['zip'],
1766
                                'id'            => 'wpinv_zip',
1767
                                'class'         => 'wpi-input form-control',
1768
                                'placeholder'   => __( 'ZIP / Postcode', 'invoicing' ),
1769
                                'required'      => (bool)wpinv_get_option( 'zip_mandatory' ),
1770
                            ) );
1771
                        ?>
1772
                    </p>
1773
                    <p class="wpi-cart-field wpi-col2 wpi-coll">
1774
                        <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>
1775
                        <?php
1776
                        echo wpinv_html_text( array(
1777
                                'id'            => 'wpinv_phone',
1778
                                'name'          => 'wpinv_phone',
1779
                                'value'         => $billing_details['phone'],
1780
                                'class'         => 'wpi-input form-control',
1781
                                'placeholder'   => __( 'Phone', 'invoicing' ),
1782
                                'required'      => (bool)wpinv_get_option( 'phone_mandatory' ),
1783
                            ) );
1784
                        ?>
1785
                    </p>
1786
                    <?php do_action( 'wpinv_checkout_billing_fields_last', $billing_details ); ?>
1787
                    <div class="clearfix"></div>
1788
                </div>
1789
            </div>
1790
            <?php do_action( 'wpinv_after_billing_fields', $billing_details ); ?>
1791
        </div>
1792
        <?php
1793
    }
1794
}
1795
add_action( 'wpinv_checkout_billing_info', 'wpinv_checkout_billing_info' );
1796
1797
function wpinv_checkout_hidden_fields() {
1798
?>
1799
    <?php if ( is_user_logged_in() ) { ?>
1800
    <input type="hidden" name="wpinv_user_id" value="<?php echo get_current_user_id(); ?>"/>
1801
    <?php } ?>
1802
    <input type="hidden" name="wpi_action" value="payment" />
1803
<?php
1804
}
1805
1806
function wpinv_checkout_button_purchase() {
1807
    ob_start();
1808
?>
1809
    <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' ) ?>"/>
1810
<?php
1811
    return apply_filters( 'wpinv_checkout_button_purchase', ob_get_clean() );
1812
}
1813
1814
function wpinv_checkout_total() {
1815
    global $cart_total;
1816
?>
1817
<div id="wpinv_checkout_total" class="panel panel-info">
1818
    <div class="panel-body">
1819
    <?php
1820
    do_action( 'wpinv_purchase_form_before_checkout_total' );
1821
    ?>
1822
    <strong><?php _e( 'Invoice Total:', 'invoicing' ) ?></strong> <span class="wpinv-chdeckout-total"><?php echo $cart_total;?></span>
1823
    <?php
1824
    do_action( 'wpinv_purchase_form_after_checkout_total' );
1825
    ?>
1826
    </div>
1827
</div>
1828
<?php
1829
}
1830
add_action( 'wpinv_checkout_form_bottom', 'wpinv_checkout_total', 9998 );
1831
1832
function wpinv_checkout_submit() {
1833
?>
1834
<div id="wpinv_purchase_submit" class="panel panel-success">
1835
    <div class="panel-body text-center">
1836
    <?php
1837
    do_action( 'wpinv_purchase_form_before_submit' );
1838
    wpinv_checkout_hidden_fields();
1839
    echo wpinv_checkout_button_purchase();
1840
    do_action( 'wpinv_purchase_form_after_submit' );
1841
    ?>
1842
    </div>
1843
</div>
1844
<?php
1845
}
1846
add_action( 'wpinv_checkout_form_bottom', 'wpinv_checkout_submit', 9999 );
1847
1848
function wpinv_receipt_billing_address( $invoice_id = 0 ) {
1849
    $invoice = wpinv_get_invoice( $invoice_id );
1850
1851
    if ( empty( $invoice ) ) {
1852
        return NULL;
1853
    }
1854
1855
    $billing_details = $invoice->get_user_info();
1856
    $address_row = wpinv_get_invoice_address_markup( $billing_details );
1857
1858
    ob_start();
1859
    ?>
1860
    <table class="table table-bordered table-sm wpi-billing-details">
1861
        <tbody>
1862
            <tr class="wpi-receipt-name">
1863
                <th class="text-left"><?php _e( 'Name', 'invoicing' ); ?></th>
1864
                <td><?php echo esc_html( trim( $billing_details['first_name'] . ' ' . $billing_details['last_name'] ) ) ;?></td>
1865
            </tr>
1866
            <tr class="wpi-receipt-email">
1867
                <th class="text-left"><?php _e( 'Email', 'invoicing' ); ?></th>
1868
                <td><?php echo $billing_details['email'] ;?></td>
1869
            </tr>
1870
            <tr class="wpi-receipt-address">
1871
                <th class="text-left"><?php _e( 'Address', 'invoicing' ); ?></th>
1872
                <td><?php echo $address_row ;?></td>
1873
            </tr>
1874
            <?php if ( $billing_details['phone'] ) { ?>
1875
            <tr class="wpi-receipt-phone">
1876
                <th class="text-left"><?php _e( 'Phone', 'invoicing' ); ?></th>
1877
                <td><?php echo esc_html( $billing_details['phone'] ) ;?></td>
1878
            </tr>
1879
            <?php } ?>
1880
        </tbody>
1881
    </table>
1882
    <?php
1883
    $output = ob_get_clean();
1884
    
1885
    $output = apply_filters( 'wpinv_receipt_billing_address', $output, $invoice_id );
1886
1887
    echo $output;
1888
}
1889
1890
function wpinv_filter_success_page_content( $content ) {
1891
    if ( isset( $_GET['payment-confirm'] ) && wpinv_is_success_page() ) {
1892
        if ( has_filter( 'wpinv_payment_confirm_' . sanitize_text_field( $_GET['payment-confirm'] ) ) ) {
1893
            $content = apply_filters( 'wpinv_payment_confirm_' . sanitize_text_field( $_GET['payment-confirm'] ), $content );
1894
        }
1895
    }
1896
1897
    return $content;
1898
}
1899
add_filter( 'the_content', 'wpinv_filter_success_page_content', 99999 );
1900
1901
function wpinv_receipt_actions( $invoice ) {
1902
    if ( !empty( $invoice ) ) {
1903
        $actions = array();
1904
1905
        if ( wpinv_user_can_view_invoice( $invoice->ID ) ) {
1906
            $actions['print']   = array(
1907
                'url'  => $invoice->get_view_url( true ),
1908
                'name' => __( 'Print Invoice', 'invoicing' ),
1909
                'class' => 'btn-primary',
1910
            );
1911
        }
1912
1913
        if ( is_user_logged_in() ) {
1914
            $actions['history'] = array(
1915
                'url'  => wpinv_get_history_page_uri(),
1916
                'name' => __( 'Invoice History', 'invoicing' ),
1917
                'class' => 'btn-warning',
1918
            );
1919
        }
1920
1921
        $actions = apply_filters( 'wpinv_invoice_receipt_actions', $actions, $invoice );
1922
1923
        if ( !empty( $actions ) ) {
1924
        ?>
1925
        <div class="wpinv-receipt-actions text-right">
1926
            <?php foreach ( $actions as $key => $action ) { $class = !empty($action['class']) ? sanitize_html_class( $action['class'] ) : ''; ?>
1927
            <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>
1928
            <?php } ?>
1929
        </div>
1930
        <?php
1931
        }
1932
    }
1933
}
1934
add_action( 'wpinv_receipt_start', 'wpinv_receipt_actions', -10, 1 );
1935
1936
function wpinv_invoice_link( $invoice_id ) {
1937
    $invoice = wpinv_get_invoice( $invoice_id );
1938
1939
    if ( empty( $invoice ) ) {
1940
        return NULL;
1941
    }
1942
1943
    $invoice_link = '<a href="' . esc_url( $invoice->get_view_url() ) . '">' . $invoice->get_number() . '</a>';
0 ignored issues
show
Bug introduced by
It seems like $invoice->get_view_url() can also be of type false; however, parameter $url of esc_url() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

1943
    $invoice_link = '<a href="' . esc_url( /** @scrutinizer ignore-type */ $invoice->get_view_url() ) . '">' . $invoice->get_number() . '</a>';
Loading history...
1944
1945
    return apply_filters( 'wpinv_get_invoice_link', $invoice_link, $invoice );
1946
}
1947
1948
function wpinv_invoice_subscription_details( $invoice ) {
1949
    if ( !empty( $invoice ) && $invoice->is_recurring() && ! wpinv_is_subscription_payment( $invoice ) ) {
1950
        $subscription = wpinv_get_subscription( $invoice, true );
1951
1952
        if ( empty( $subscription ) ) {
1953
            return;
1954
        }
1955
1956
        $frequency = WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency($subscription->period, $subscription->frequency);
0 ignored issues
show
Bug introduced by
It seems like $subscription->frequency can also be of type WP_Error; however, parameter $frequency_count of WPInv_Subscriptions::wpi...ubscription_frequency() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

1956
        $frequency = WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency($subscription->period, /** @scrutinizer ignore-type */ $subscription->frequency);
Loading history...
Bug Best Practice introduced by
The property frequency does not exist on WPInv_Subscription. Since you implemented __get, consider adding a @property annotation.
Loading history...
1957
        $billing = wpinv_price(wpinv_format_amount($subscription->recurring_amount), wpinv_get_invoice_currency_code($subscription->parent_payment_id)) . ' / ' . $frequency;
1958
        $initial = wpinv_price(wpinv_format_amount($subscription->initial_amount), wpinv_get_invoice_currency_code($subscription->parent_payment_id));
1959
1960
        $payments = $subscription->get_child_payments();
1961
        ?>
1962
        <div class="wpinv-subscriptions-details">
1963
            <h3 class="wpinv-subscriptions-t"><?php echo apply_filters( 'wpinv_subscription_details_title', __( 'Subscription Details', 'invoicing' ) ); ?></h3>
1964
            <table class="table">
1965
                <thead>
1966
                    <tr>
1967
                        <th><?php _e( 'Billing Cycle', 'invoicing' ) ;?></th>
1968
                        <th><?php _e( 'Start Date', 'invoicing' ) ;?></th>
1969
                        <th><?php _e( 'Expiration Date', 'invoicing' ) ;?></th>
1970
                        <th class="text-center"><?php _e( 'Times Billed', 'invoicing' ) ;?></th>
1971
                        <th class="text-center"><?php _e( 'Status', 'invoicing' ) ;?></th>
1972
                    </tr>
1973
                </thead>
1974
                <tbody>
1975
                    <tr>
1976
                        <td><?php printf(_x('%s then %s', 'Initial subscription amount then billing cycle and amount', 'invoicing'), $initial, $billing); ?></td>
1977
                        <td><?php echo date_i18n(get_option('date_format'), strtotime($subscription->created, current_time('timestamp'))); ?></td>
0 ignored issues
show
Bug introduced by
It seems like get_option('date_format') can also be of type false; however, parameter $format of date_i18n() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

1977
                        <td><?php echo date_i18n(/** @scrutinizer ignore-type */ get_option('date_format'), strtotime($subscription->created, current_time('timestamp'))); ?></td>
Loading history...
1978
                        <td><?php echo date_i18n(get_option('date_format'), strtotime($subscription->expiration, current_time('timestamp'))); ?></td>
1979
                        <td class="text-center"><?php echo $subscription->get_times_billed() . ' / ' . (($subscription->bill_times == 0) ? 'Until Cancelled' : $subscription->bill_times); ?></td>
1980
                        <td class="text-center wpi-sub-status"><?php echo $subscription->get_status_label(); ?></td>
1981
                    </tr>
1982
                </tbody>
1983
            </table>
1984
        </div>
1985
        <?php if ( !empty( $payments ) ) { ?>
1986
        <div class="wpinv-renewal-payments">
1987
            <h3 class="wpinv-renewals-t"><?php echo apply_filters( 'wpinv_renewal_payments_title', __( 'Renewal Payments', 'invoicing' ) ); ?></h3>
1988
            <table class="table">
1989
                <thead>
1990
                    <tr>
1991
                        <th>#</th>
1992
                        <th><?php _e( 'Invoice', 'invoicing' ) ;?></th>
1993
                        <th><?php _e( 'Date', 'invoicing' ) ;?></th>
1994
                        <th class="text-right"><?php _e( 'Amount', 'invoicing' ) ;?></th>
1995
                    </tr>
1996
                </thead>
1997
                <tbody>
1998
                    <?php
1999
                        $i = 1;
2000
                        foreach ( $payments as $payment ) {
2001
                            $invoice_id = $payment->ID;
2002
                    ?>
2003
                    <tr>
2004
                        <th scope="row"><?php echo $i;?></th>
2005
                        <td><?php echo wpinv_invoice_link( $invoice_id ) ;?></td>
2006
                        <td><?php echo wpinv_get_invoice_date( $invoice_id ); ?></td>
2007
                        <td class="text-right"><?php echo wpinv_payment_total( $invoice_id, true ); ?></td>
2008
                    </tr>
2009
                    <?php $i++; } ?>
2010
                </tbody>
2011
            </table>
2012
        </div>
2013
        <?php } ?>
2014
        <?php
2015
    }
2016
}
2017
2018
function wpinv_cart_total_label( $label, $invoice ) {
2019
    if ( empty( $invoice ) ) {
2020
        return $label;
2021
    }
2022
2023
    $prefix_label = '';
2024
    if ( $invoice->is_parent() && $item_id = $invoice->get_recurring() ) {
0 ignored issues
show
Unused Code introduced by
The assignment to $item_id is dead and can be removed.
Loading history...
2025
        $prefix_label   = '<span class="label label-primary label-recurring">' . __( 'Recurring Payment', 'invoicing' ) . '</span> ' . wpinv_subscription_payment_desc( $invoice );
2026
    } else if ( $invoice->is_renewal() ) {
2027
        $prefix_label   = '<span class="label label-primary label-renewal">' . __( 'Renewal Payment', 'invoicing' ) . '</span> ';        
2028
    }
2029
2030
    if ( $prefix_label != '' ) {
2031
        $label  = '<span class="wpinv-cart-sub-desc">' . $prefix_label . '</span> ' . $label;
2032
    }
2033
2034
    return $label;
2035
}
2036
add_filter( 'wpinv_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
2037
add_filter( 'wpinv_email_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
2038
add_filter( 'wpinv_print_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
2039
2040
add_action( 'wpinv_invoice_print_middle', 'wpinv_invoice_subscription_details', 10, 1 );
2041
2042
function wpinv_invoice_print_description( $invoice ) {
2043
    if ( empty( $invoice ) ) {
2044
        return NULL;
2045
    }
2046
    if ( $description = wpinv_get_invoice_description( $invoice->ID ) ) {
2047
        ?>
2048
        <div class="row wpinv-lower">
2049
            <div class="col-sm-12 wpinv-description">
2050
                <?php echo wpautop( $description ); ?>
2051
            </div>
2052
        </div>
2053
        <?php
2054
    }
2055
}
2056
add_action( 'wpinv_invoice_print_middle', 'wpinv_invoice_print_description', 10.1, 1 );
0 ignored issues
show
Bug introduced by
10.1 of type double is incompatible with the type integer expected by parameter $priority of add_action(). ( Ignorable by Annotation )

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

2056
add_action( 'wpinv_invoice_print_middle', 'wpinv_invoice_print_description', /** @scrutinizer ignore-type */ 10.1, 1 );
Loading history...
2057
2058
function wpinv_invoice_print_payment_info( $invoice ) {
2059
    if ( empty( $invoice ) ) {
2060
        return NULL;
2061
    }
2062
2063
    if ( $payments_info = wpinv_display_payments_info( $invoice->ID, false ) ) {
2064
        ?>
2065
        <div class="row wpinv-payments">
2066
            <div class="col-sm-12">
2067
                <?php echo $payments_info; ?>
2068
            </div>
2069
        </div>
2070
        <?php 
2071
    }
2072
}
2073
// add_action( 'wpinv_invoice_print_after_line_items', 'wpinv_invoice_print_payment_info', 10, 1 );
2074
2075
function wpinv_get_invoice_note_line_item( $note, $echo = true ) {
2076
    if ( empty( $note ) ) {
2077
        return NULL;
2078
    }
2079
2080
    if ( is_int( $note ) ) {
2081
        $note = get_comment( $note );
2082
    }
2083
2084
    if ( !( is_object( $note ) && is_a( $note, 'WP_Comment' ) ) ) {
2085
        return NULL;
2086
    }
2087
2088
    $note_classes   = array( 'note' );
2089
    $note_classes[] = get_comment_meta( $note->comment_ID, '_wpi_customer_note', true ) ? 'customer-note' : '';
2090
    $note_classes[] = $note->comment_author === 'System' ? 'system-note' : '';
2091
    $note_classes   = apply_filters( 'wpinv_invoice_note_class', array_filter( $note_classes ), $note );
2092
    $note_classes   = !empty( $note_classes ) ? implode( ' ', $note_classes ) : '';
2093
2094
    ob_start();
2095
    ?>
2096
    <li rel="<?php echo absint( $note->comment_ID ) ; ?>" class="<?php echo esc_attr( $note_classes ); ?>">
2097
        <div class="note_content">
2098
            <?php echo wpautop( wptexturize( wp_kses_post( $note->comment_content ) ) ); ?>
2099
        </div>
2100
        <p class="meta">
2101
            <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;
0 ignored issues
show
Bug introduced by
It seems like get_option('date_format') can also be of type false; however, parameter $format of date_i18n() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

2101
            <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( /** @scrutinizer ignore-type */ get_option( 'date_format' ), strtotime( $note->comment_date ) ), date_i18n( get_option( 'time_format' ), strtotime( $note->comment_date ) ) ); ?></abbr>&nbsp;&nbsp;
Loading history...
2102
            <?php if ( is_admin() && ( $note->comment_author !== 'System' || wpinv_current_user_can_manage_invoicing() ) ) { ?>
2103
                <a href="#" class="delete_note"><?php _e( 'Delete note', 'invoicing' ); ?></a>
2104
            <?php } ?>
2105
        </p>
2106
    </li>
2107
    <?php
2108
    $note_content = ob_get_clean();
2109
    $note_content = apply_filters( 'wpinv_get_invoice_note_line_item', $note_content, $note, $echo );
2110
2111
    if ( $echo ) {
2112
        echo $note_content;
2113
    } else {
2114
        return $note_content;
2115
    }
2116
}
2117
2118
function wpinv_invalid_invoice_content() {
2119
    global $post;
2120
2121
    $invoice = wpinv_get_invoice( $post->ID );
2122
2123
    $error = __( 'This invoice is only viewable by clicking on the invoice link that was sent to you via email.', 'invoicing' );
2124
    if ( !empty( $invoice->ID ) && $invoice->has_status( array_keys( wpinv_get_invoice_statuses() ) ) ) {
2125
        if ( is_user_logged_in() ) {
2126
            if ( wpinv_require_login_to_checkout() ) {
2127
                if ( isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
2128
                    $error = __( 'You are not allowed to view this invoice.', 'invoicing' );
2129
                }
2130
            }
2131
        } else {
2132
            if ( wpinv_require_login_to_checkout() ) {
2133
                if ( isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
2134
                    $error = __( 'You must be logged in to view this invoice.', 'invoicing' );
2135
                }
2136
            }
2137
        }
2138
    } else {
2139
        $error = __( 'This invoice is deleted or does not exist.', 'invoicing' );
2140
    }
2141
    ?>
2142
    <div class="row wpinv-row-invalid">
2143
        <div class="col-md-6 col-md-offset-3 wpinv-message error">
2144
            <h3><?php _e( 'Access Denied', 'invoicing' ); ?></h3>
2145
            <p class="wpinv-msg-text"><?php echo $error; ?></p>
2146
        </div>
2147
    </div>
2148
    <?php
2149
}
2150
add_action( 'wpinv_invalid_invoice_content', 'wpinv_invalid_invoice_content' );
2151
2152
add_action( 'wpinv_checkout_billing_fields_last', 'wpinv_force_company_name_field');
2153
function wpinv_force_company_name_field(){
2154
    $invoice = wpinv_get_invoice_cart();
2155
    $user_id = wpinv_get_user_id( $invoice->ID );
2156
    $company = empty( $user_id ) ? "" : get_user_meta( $user_id, '_wpinv_company', true );
2157
    if ( 1 == wpinv_get_option( 'force_show_company' ) && !wpinv_use_taxes() ) {
2158
        ?>
2159
        <p class="wpi-cart-field wpi-col2 wpi-colf">
2160
            <label for="wpinv_company" class="wpi-label"><?php _e('Company Name', 'invoicing'); ?></label>
2161
            <?php
2162
            echo wpinv_html_text(array(
2163
                'id' => 'wpinv_company',
2164
                'name' => 'wpinv_company',
2165
                'value' => $company,
2166
                'class' => 'wpi-input form-control',
2167
                'placeholder' => __('Company name', 'invoicing'),
2168
                'required'      => true,
2169
            ));
2170
            ?>
2171
        </p>
2172
        <?php
2173
    }
2174
}
2175
2176
/**
2177
 * Function to get privacy policy text.
2178
 *
2179
 * @since 1.0.13
2180
 * @return string
2181
 */
2182
function wpinv_get_policy_text() {
2183
    $privacy_page_id = get_option( 'wp_page_for_privacy_policy', 0 );
2184
2185
    $text = wpinv_get_option('invoicing_privacy_checkout_message', sprintf( __( 'Your personal data will be used to process your invoice, payment and for other purposes described in our %s.', 'invoicing' ), '[wpinv_privacy_policy]' ));
2186
2187
    if(!$privacy_page_id){
2188
        $privacy_page_id = wpinv_get_option( 'privacy_page', 0 );
2189
    }
2190
2191
    $privacy_link    = $privacy_page_id ? '<a href="' . esc_url( get_permalink( $privacy_page_id ) ) . '" class="wpinv-privacy-policy-link" target="_blank">' . __( 'privacy policy', 'invoicing' ) . '</a>' : __( 'privacy policy', 'invoicing' );
0 ignored issues
show
Bug introduced by
It seems like get_permalink($privacy_page_id) can also be of type false; however, parameter $url of esc_url() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

2191
    $privacy_link    = $privacy_page_id ? '<a href="' . esc_url( /** @scrutinizer ignore-type */ get_permalink( $privacy_page_id ) ) . '" class="wpinv-privacy-policy-link" target="_blank">' . __( 'privacy policy', 'invoicing' ) . '</a>' : __( 'privacy policy', 'invoicing' );
Loading history...
2192
2193
    $find_replace = array(
2194
        '[wpinv_privacy_policy]' => $privacy_link,
2195
    );
2196
2197
    $privacy_text = str_replace( array_keys( $find_replace ), array_values( $find_replace ), $text );
2198
2199
    return wp_kses_post(wpautop($privacy_text));
2200
}
2201
2202
2203
/**
2204
 * Allows the user to set their own price for an invoice item
2205
 */
2206
function wpinv_checkout_cart_item_name_your_price( $cart_item, $key ) {
2207
    
2208
    //Ensure we have an item id
2209
    if(! is_array( $cart_item ) || empty( $cart_item['id'] ) ) {
2210
        return;
2211
    }
2212
2213
    //Fetch the item
2214
    $item_id = $cart_item['id'];
2215
    $item    = new WPInv_Item( $item_id );
2216
    
2217
    if(! $item->supports_dynamic_pricing() || !$item->get_is_dynamic_pricing() ) {
2218
        return;
2219
    }
2220
2221
    //Fetch the dynamic pricing "strings"
2222
    $suggested_price_text = esc_html( wpinv_get_option( 'suggested_price_text', __( 'Suggested Price:', 'invoicing' ) ) );
2223
    $minimum_price_text   = esc_html( wpinv_get_option( 'minimum_price_text', __( 'Minimum Price:', 'invoicing' ) ) );
2224
    $name_your_price_text = esc_html( wpinv_get_option( 'name_your_price_text', __( 'Name Your Price', 'invoicing' ) ) );
2225
2226
    //Display a "name_your_price" button
2227
    echo " &mdash; <a href='#' class='wpinv-name-your-price-frontend small'>$name_your_price_text</a></div>";
2228
2229
    //Display a name_your_price form
2230
    echo '<div class="name-your-price-miniform">';
2231
    
2232
    //Maybe display the recommended price
2233
    if( $item->get_price() > 0 && !empty( $suggested_price_text ) ) {
2234
        $suggested_price = $item->get_the_price();
2235
        echo "<div>$suggested_price_text &mdash; $suggested_price</div>";
2236
    }
2237
2238
    //Display the update price form
2239
    $symbol         = wpinv_currency_symbol();
2240
    $position       = wpinv_currency_position();
2241
    $minimum        = esc_attr( $item->get_minimum_price() );
2242
    $price          = esc_attr( $cart_item['item_price'] );
2243
    $update         = esc_attr__( "Update", 'invoicing' );
2244
2245
    //Ensure it supports dynamic prici
2246
    if( $price < $minimum ) {
2247
        $price = $minimum;
2248
    }
2249
2250
    echo '<label>';
2251
    echo $position != 'right' ? $symbol . '&nbsp;' : '';
2252
    echo "<input type='number' min='$minimum' placeholder='$price' value='$price' class='wpi-field-price' />";
2253
    echo $position == 'right' ? '&nbsp;' . $symbol : '' ;
2254
    echo "</label>";
2255
    echo "<input type='hidden' value='$item_id' class='wpi-field-item' />";
2256
    echo "<a class='btn btn-success wpinv-submit wpinv-update-dynamic-price-frontend'>$update</a>";
2257
2258
    //Maybe display the minimum price
2259
    if( $item->get_minimum_price() > 0 && !empty( $minimum_price_text ) ) {
2260
        $minimum_price = wpinv_price( wpinv_format_amount( $item->get_minimum_price() ) );
2261
        echo "<div>$minimum_price_text &mdash; $minimum_price</div>";
2262
    }
2263
2264
    echo "</div>";
2265
2266
}
2267
add_action( 'wpinv_checkout_cart_item_price_after', 'wpinv_checkout_cart_item_name_your_price', 10, 2 );
2268
2269
function wpinv_oxygen_fix_conflict() {
2270
    global $ct_ignore_post_types;
2271
2272
    if ( ! is_array( $ct_ignore_post_types ) ) {
2273
        $ct_ignore_post_types = array();
2274
    }
2275
2276
    $post_types = array( 'wpi_discount', 'wpi_invoice', 'wpi_item' );
2277
2278
    foreach ( $post_types as $post_type ) {
2279
        $ct_ignore_post_types[] = $post_type;
2280
2281
        // Ignore post type
2282
        add_filter( 'pre_option_oxygen_vsb_ignore_post_type_' . $post_type, '__return_true', 999 );
2283
    }
2284
2285
    remove_filter( 'template_include', 'wpinv_template', 10, 1 );
0 ignored issues
show
Unused Code introduced by
The call to remove_filter() has too many arguments starting with 1. ( Ignorable by Annotation )

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

2285
    /** @scrutinizer ignore-call */ 
2286
    remove_filter( 'template_include', 'wpinv_template', 10, 1 );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
2286
    add_filter( 'template_include', 'wpinv_template', 999, 1 );
2287
}
2288
2289
/**
2290
 * Helper function to display a payment form on the frontend.
2291
 */
2292
function getpaid_display_payment_form( $form ) {
2293
    global $invoicing;
2294
2295
    // Ensure that it is published.
2296
	if ( 'publish' != get_post_status( $form ) ) {
2297
		return aui()->alert(
0 ignored issues
show
Bug introduced by
The function aui was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2297
		return /** @scrutinizer ignore-call */ aui()->alert(
Loading history...
2298
			array(
2299
				'type'    => 'warning',
2300
				'content' => __( 'This payment form is no longer active', 'invoicing' ),
2301
			)
2302
		);
2303
	}
2304
2305
    // Get the form elements and items.
2306
    $form     = absint( $form );
2307
	$elements = $invoicing->form_elements->get_form_elements( $form );
2308
	$items    = $invoicing->form_elements->get_form_items( $form );
2309
2310
	ob_start();
2311
	echo "<form class='wpinv_payment_form'>";
2312
	do_action( 'wpinv_payment_form_top' );
2313
	echo "<input type='hidden' name='form_id' value='$form'/>";
2314
	wp_nonce_field( 'wpinv_payment_form', 'wpinv_payment_form' );
2315
	wp_nonce_field( 'vat_validation', '_wpi_nonce' );
2316
2317
	foreach ( $elements as $element ) {
2318
		do_action( 'wpinv_frontend_render_payment_form_element', $element, $items, $form );
2319
		do_action( "wpinv_frontend_render_payment_form_{$element['type']}", $element, $items, $form );
2320
	}
2321
2322
	echo "<div class='wpinv_payment_form_errors alert alert-danger d-none'></div>";
2323
	do_action( 'wpinv_payment_form_bottom' );
2324
	echo '</form>';
2325
2326
	$content = ob_get_clean();
2327
	return str_replace( 'sr-only', '', $content );
2328
}
2329
2330
/**
2331
 * Helper function to display a item payment form on the frontend.
2332
 */
2333
function getpaid_display_item_payment_form( $items ) {
2334
    global $invoicing;
2335
2336
    foreach ( array_keys( $items ) as $id ) {
2337
	    if ( 'publish' != get_post_status( $id ) ) {
2338
		    unset( $items[ $id ] );
2339
	    }
2340
    }
2341
2342
    if ( empty( $items ) ) {
2343
		return aui()->alert(
0 ignored issues
show
Bug introduced by
The function aui was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2343
		return /** @scrutinizer ignore-call */ aui()->alert(
Loading history...
2344
			array(
2345
				'type'    => 'warning',
2346
				'content' => __( 'No published items found', 'invoicing' ),
2347
			)
2348
		);
2349
    }
2350
2351
    $item_key = getpaid_convert_items_to_string( $items );
2352
2353
    // Get the form elements and items.
2354
    $form     = wpinv_get_default_payment_form();
2355
	$elements = $invoicing->form_elements->get_form_elements( $form );
2356
	$items    = $invoicing->form_elements->convert_normal_items( $items );
2357
2358
	ob_start();
2359
	echo "<form class='wpinv_payment_form'>";
2360
	do_action( 'wpinv_payment_form_top' );
2361
    echo "<input type='hidden' name='form_id' value='$form'/>";
2362
    echo "<input type='hidden' name='form_items' value='$item_key'/>";
2363
	wp_nonce_field( 'wpinv_payment_form', 'wpinv_payment_form' );
2364
	wp_nonce_field( 'vat_validation', '_wpi_nonce' );
2365
2366
	foreach ( $elements as $element ) {
2367
		do_action( 'wpinv_frontend_render_payment_form_element', $element, $items, $form );
2368
		do_action( "wpinv_frontend_render_payment_form_{$element['type']}", $element, $items, $form );
2369
	}
2370
2371
	echo "<div class='wpinv_payment_form_errors alert alert-danger d-none'></div>";
2372
	do_action( 'wpinv_payment_form_bottom' );
2373
	echo '</form>';
2374
2375
	$content = ob_get_clean();
2376
	return str_replace( 'sr-only', '', $content );
2377
}
2378
2379
/**
2380
 * Helper function to display an invoice payment form on the frontend.
2381
 */
2382
function getpaid_display_invoice_payment_form( $invoice_id ) {
2383
    global $invoicing;
2384
2385
    $invoice = wpinv_get_invoice( $invoice_id );
2386
2387
    if ( empty( $invoice ) ) {
2388
		return aui()->alert(
0 ignored issues
show
Bug introduced by
The function aui was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2388
		return /** @scrutinizer ignore-call */ aui()->alert(
Loading history...
2389
			array(
2390
				'type'    => 'warning',
2391
				'content' => __( 'Invoice not found', 'invoicing' ),
2392
			)
2393
		);
2394
    }
2395
2396
    if ( $invoice->is_paid() ) {
2397
		return aui()->alert(
2398
			array(
2399
				'type'    => 'warning',
2400
				'content' => __( 'Invoice has already been paid', 'invoicing' ),
2401
			)
2402
		);
2403
    }
2404
2405
    // Get the form elements and items.
2406
    $form     = wpinv_get_default_payment_form();
2407
	$elements = $invoicing->form_elements->get_form_elements( $form );
2408
	$items    = $invoicing->form_elements->convert_checkout_items( $invoice->cart_details, $invoice );
2409
2410
	ob_start();
2411
	echo "<form class='wpinv_payment_form'>";
2412
	do_action( 'wpinv_payment_form_top' );
2413
    echo "<input type='hidden' name='form_id' value='$form'/>";
2414
    echo "<input type='hidden' name='invoice_id' value='$invoice_id'/>";
2415
	wp_nonce_field( 'wpinv_payment_form', 'wpinv_payment_form' );
2416
	wp_nonce_field( 'vat_validation', '_wpi_nonce' );
2417
2418
	foreach ( $elements as $element ) {
2419
		do_action( 'wpinv_frontend_render_payment_form_element', $element, $items, $form );
2420
		do_action( "wpinv_frontend_render_payment_form_{$element['type']}", $element, $items, $form );
2421
	}
2422
2423
	echo "<div class='wpinv_payment_form_errors alert alert-danger d-none'></div>";
2424
	do_action( 'wpinv_payment_form_bottom' );
2425
	echo '</form>';
2426
2427
	$content = ob_get_clean();
2428
	return str_replace( 'sr-only', '', $content );
2429
}
2430
2431
/**
2432
 * Helper function to convert item string to array.
2433
 */
2434
function getpaid_convert_items_to_array( $items ) {
2435
    $items    = array_filter( array_map( 'trim', explode( ',', $items ) ) );
2436
    $prepared = array();
2437
2438
    foreach ( $items as $item ) {
2439
        $data = array_map( 'trim', explode( '|', $item ) );
2440
2441
        if ( empty( $data[0] ) || ! is_numeric( $data[0] ) ) {
2442
            continue;
2443
        }
2444
2445
        $quantity = 1;
2446
        if ( isset( $data[1] ) && is_numeric( $data[1] ) ) {
2447
            $quantity = $data[1];
2448
        }
2449
2450
        $prepared[ $data[0] ] = $quantity;
2451
2452
    }
2453
2454
    return $prepared;
2455
}
2456
2457
/**
2458
 * Helper function to convert item array to string.
2459
 */
2460
function getpaid_convert_items_to_string( $items ) {
2461
    $prepared = array();
2462
2463
    foreach ( $items as $item => $quantity ) {
2464
        $prepared[] = "$item|$quantity";
2465
    }
2466
    return implode( ',', $prepared );
2467
}
2468
2469
/**
2470
 * Helper function to display a payment item.
2471
 * 
2472
 * Provide a label and one of $form, $items or $invoice.
2473
 */
2474
function getpaid_get_payment_button( $label, $form = null, $items = null, $invoice = null ) {
2475
    $label = sanitize_text_field( $label );
2476
    $nonce = wp_create_nonce('getpaid_ajax_form');
2477
2478
    if ( ! empty( $form ) ) {
2479
        $form  = esc_attr( $form );
2480
        return "<button class='btn btn-primary getpaid-payment-button' type='button' data-nonce='$nonce' data-form='$form'>$label</button>"; 
2481
    }
2482
	
2483
	if ( ! empty( $items ) ) {
2484
        $items  = esc_attr( $items );
2485
        return "<button class='btn btn-primary getpaid-payment-button' type='button' data-nonce='$nonce' data-item='$items'>$label</button>"; 
2486
    }
2487
    
2488
    if ( ! empty( $invoice ) ) {
2489
        $invoice  = esc_attr( $invoice );
2490
        return "<button class='btn btn-primary getpaid-payment-button' type='button' data-nonce='$nonce' data-invoice='$invoice'>$label</button>"; 
2491
    }
2492
2493
}
2494
2495