Passed
Pull Request — master (#336)
by Brian
04:34
created

getpaid_get_payment_button()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 4
nop 4
dl 0
loc 17
rs 9.9
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, 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
  2. 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

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 4366
  3. wp_parse_str() is called
    in wordpress/wp-includes/functions.php on line 4372
  4. Enters via parameter $string
    in wordpress/wp-includes/formatting.php on line 4886
  5. parse_str() is called
    in wordpress/wp-includes/formatting.php on line 4887

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
    </table>
927
<?php
928
}
929
930
/**
931
 * Retrieves the address markup to use on Invoices.
932
 * 
933
 * @since 1.0.13
934
 * @see `wpinv_get_full_address_format`
935
 * @see `wpinv_get_invoice_address_replacements`
936
 * @param array $billing_details customer's billing details
937
 * @param  string $separator How to separate address lines.
938
 * @return string
939
 */
940
function wpinv_get_invoice_address_markup( $billing_details, $separator = '<br/>' ) {
941
942
    // Retrieve the address markup...
943
    $country= empty( $billing_details['country'] ) ? '' : $billing_details['country'];
944
    $format = wpinv_get_full_address_format( $country );
945
946
    // ... and the replacements.
947
    $replacements = wpinv_get_invoice_address_replacements( $billing_details );
948
949
    $formatted_address = str_ireplace( array_keys( $replacements ), $replacements, $format );
950
    
951
	// Remove unavailable tags.
952
    $formatted_address = preg_replace( "/\{\{\w+\}\}/", '', $formatted_address );
953
954
    // Clean up white space.
955
	$formatted_address = preg_replace( '/  +/', ' ', trim( $formatted_address ) );
956
    $formatted_address = preg_replace( '/\n\n+/', "\n", $formatted_address );
957
    
958
    // Break newlines apart and remove empty lines/trim commas and white space.
959
	$formatted_address = array_filter( array_map( 'wpinv_trim_formatted_address_line', explode( "\n", $formatted_address ) ) );
960
961
    // Add html breaks.
962
	$formatted_address = implode( $separator, $formatted_address );
963
964
	// We're done!
965
	return $formatted_address;
966
    
967
}
968
969
function wpinv_display_to_address( $invoice_id = 0 ) {
970
    $invoice = wpinv_get_invoice( $invoice_id );
971
    
972
    if ( empty( $invoice ) ) {
973
        return NULL;
974
    }
975
    
976
    $billing_details = $invoice->get_user_info();
977
    $output = '<div class="to col-xs-2"><strong>' . __( 'To:', 'invoicing' ) . '</strong></div>';
978
    $output .= '<div class="wrapper col-xs-10">';
979
    
980
    ob_start();
981
    do_action( 'wpinv_display_to_address_top', $invoice );
982
    $output .= ob_get_clean();
983
    
984
    $address_row = wpinv_get_invoice_address_markup( $billing_details );
985
986
    if ( $address_row ) {
987
        $output .= '<div class="address">' . $address_row . '</div>';
988
    }
989
990
    if ( $phone = $invoice->get_phone() ) {
991
        $output .= '<div class="phone">' . wp_sprintf( __( 'Phone: %s', 'invoicing' ), esc_html( $phone ) ) . '</div>';
992
    }
993
    if ( $email = $invoice->get_email() ) {
994
        $output .= '<div class="email">' . wp_sprintf( __( 'Email: %s' , 'invoicing'), esc_html( $email ) ) . '</div>';
995
    }
996
997
    ob_start();
998
    do_action( 'wpinv_display_to_address_bottom', $invoice );
999
    $output .= ob_get_clean();
1000
    
1001
    $output .= '</div>';
1002
    $output = apply_filters( 'wpinv_display_to_address', $output, $invoice );
1003
1004
    echo $output;
1005
}
1006
1007
function wpinv_display_line_items( $invoice_id = 0 ) {
1008
    global $wpinv_euvat, $ajax_cart_details;
1009
    $invoice            = wpinv_get_invoice( $invoice_id );
1010
    $quantities_enabled = wpinv_item_quantities_enabled();
1011
    $use_taxes          = wpinv_use_taxes();
1012
    if ( !$use_taxes && (float)$invoice->get_tax() > 0 ) {
1013
        $use_taxes = true;
1014
    }
1015
    $zero_tax           = !(float)$invoice->get_tax() > 0 ? true : false;
1016
    $tax_label           = $use_taxes && $invoice->has_vat() ? $wpinv_euvat->get_vat_name() : __( 'Tax', 'invoicing' );
1017
    $tax_title          = !$zero_tax && $use_taxes ? ( wpinv_prices_include_tax() ? wp_sprintf( __( '(%s Incl.)', 'invoicing' ), $tax_label ) : wp_sprintf( __( '(%s Excl.)', 'invoicing' ), $tax_label ) ) : '';
1018
1019
    $cart_details       = $invoice->get_cart_details();
1020
    $ajax_cart_details  = $cart_details;
1021
    ob_start();
1022
    ?>
1023
    <table class="table table-sm table-bordered">
1024
        <thead>
1025
            <tr>
1026
                <th class="name"><strong><?php _e( "Item Name", "invoicing" );?></strong></th>
1027
                <th class="rate"><strong><?php _e( "Price", "invoicing" );?></strong></th>
1028
                <?php if ($quantities_enabled) { ?>
1029
                    <th class="qty"><strong><?php _e( "Qty", "invoicing" );?></strong></th>
1030
                <?php } ?>
1031
                <?php if ($use_taxes && !$zero_tax) { ?>
1032
                    <th class="tax"><strong><?php echo $tax_label . ' <span class="normal small">(%)</span>'; ?></strong></th>
1033
                <?php } ?>
1034
                <th class="total"><strong><?php echo __( "Item Total", "invoicing" ) . ' <span class="normal small">' . $tax_title . '<span>';?></strong></th>
1035
            </tr>
1036
        </thead>
1037
        <tbody>
1038
        <?php 
1039
            if ( !empty( $cart_details ) ) {
1040
                do_action( 'wpinv_display_line_items_start', $invoice );
1041
1042
                $count = 0;
1043
                $cols  = 3;
1044
                foreach ( $cart_details as $key => $cart_item ) {
1045
                    $item_id    = !empty($cart_item['id']) ? absint( $cart_item['id'] ) : '';
1046
                    $item_price = isset($cart_item["item_price"]) ? wpinv_round_amount( $cart_item["item_price"] ) : 0;
1047
                    $line_total = isset($cart_item["subtotal"]) ? wpinv_round_amount( $cart_item["subtotal"] ) : 0;
1048
                    $quantity   = !empty($cart_item['quantity']) && (int)$cart_item['quantity'] > 0 ? absint( $cart_item['quantity'] ) : 1;
1049
1050
                    $item       = $item_id ? new WPInv_Item( $item_id ) : NULL;
1051
                    $summary    = '';
1052
	                $item_name    = '';
1053
                    $cols       = 3;
1054
                    if ( !empty($item) ) {
1055
                        $item_name  = $item->get_name();
1056
                        $summary    = $item->get_summary();
1057
                    }
1058
                    $item_name  = !empty($cart_item['name']) ? $cart_item['name'] : $item_name;
1059
1060
                    $summary = apply_filters( 'wpinv_print_invoice_line_item_summary', $summary, $cart_item, $item, $invoice );
1061
1062
                    $item_tax       = '';
1063
                    $tax_rate       = '';
1064
                    if ( $use_taxes && $cart_item['tax'] > 0 && $cart_item['subtotal'] > 0 ) {
1065
                        $item_tax = wpinv_price( wpinv_format_amount( $cart_item['tax'] ), $invoice->get_currency() );
1066
                        $tax_rate = !empty( $cart_item['vat_rate'] ) ? $cart_item['vat_rate'] : ( $cart_item['tax'] / $cart_item['subtotal'] ) * 100;
1067
                        $tax_rate = $tax_rate > 0 ? (float)wpinv_round_amount( $tax_rate, 4 ) : '';
1068
                        $tax_rate = $tax_rate != '' ? ' <small class="tax-rate">(' . $tax_rate . '%)</small>' : '';
1069
                    }
1070
1071
                    $line_item_tax = $item_tax . $tax_rate;
1072
1073
                    if ( $line_item_tax === '' ) {
1074
                        $line_item_tax = 0; // Zero tax
1075
                    }
1076
1077
                    $action = apply_filters( 'wpinv_display_line_item_action', '', $cart_item, $invoice, $cols );
1078
1079
                    $line_item = '<tr class="row-' . ( ($count % 2 == 0) ? 'even' : 'odd' ) . ' wpinv-item">';
1080
                        $line_item .= '<td class="name">' . $action. esc_html__( $item_name, 'invoicing' ) . wpinv_get_item_suffix( $item );
1081
                        if ( $summary !== '' ) {
1082
                            $line_item .= '<br/><small class="meta">' . wpautop( wp_kses_post( $summary ) ) . '</small>';
1083
                        }
1084
                        $line_item .= '</td>';
1085
1086
                        $line_item .= '<td class="rate">' . esc_html__( wpinv_price( wpinv_format_amount( $item_price ), $invoice->get_currency() ) ) . '</td>';
1087
                        if ($quantities_enabled) {
1088
                            $cols++;
1089
                            $line_item .= '<td class="qty">' . $quantity . '</td>';
1090
                        }
1091
                        if ($use_taxes && !$zero_tax) {
1092
                            $cols++;
1093
                            $line_item .= '<td class="tax">' . $line_item_tax . '</td>';
1094
                        }
1095
                        $line_item .= '<td class="total">' . esc_html__( wpinv_price( wpinv_format_amount( $line_total ), $invoice->get_currency() ) ) . '</td>';
1096
                    $line_item .= '</tr>';
1097
1098
                    echo apply_filters( 'wpinv_display_line_item', $line_item, $cart_item, $invoice, $cols );
1099
1100
                    $count++;
1101
                }
1102
1103
                do_action( 'wpinv_display_before_subtotal', $invoice, $cols );
1104
                ?>
1105
                <tr class="row-sub-total row_odd">
1106
                    <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php echo apply_filters( 'wpinv_print_cart_subtotal_label', '<strong>' . __( 'Sub Total', 'invoicing' ) . ':</strong>', $invoice ); ?></td>
1107
                    <td class="total"><strong><?php _e( wpinv_subtotal( $invoice_id, true ) ) ?></strong></td>
1108
                </tr>
1109
                <?php
1110
                do_action( 'wpinv_display_after_subtotal', $invoice, $cols );
1111
                
1112
                if ( wpinv_discount( $invoice_id, false ) > 0 ) {
1113
                    do_action( 'wpinv_display_before_discount', $invoice, $cols );
1114
                    ?>
1115
                        <tr class="row-discount">
1116
                            <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php wpinv_get_discount_label( wpinv_discount_code( $invoice_id ) ); ?>:</td>
1117
                            <td class="total"><?php echo wpinv_discount( $invoice_id, true, true ); ?></td>
1118
                        </tr>
1119
                    <?php
1120
                    do_action( 'wpinv_display_after_discount', $invoice, $cols );
1121
                }
1122
1123
                if ( $use_taxes ) {
1124
                    do_action( 'wpinv_display_before_tax', $invoice, $cols );
1125
                    ?>
1126
                    <tr class="row-tax">
1127
                        <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php echo apply_filters( 'wpinv_print_cart_tax_label', '<strong>' . $tax_label . ':</strong>', $invoice ); ?></td>
1128
                        <td class="total"><?php _e( wpinv_tax( $invoice_id, true ) ) ?></td>
1129
                    </tr>
1130
                    <?php
1131
                    do_action( 'wpinv_display_after_tax', $invoice, $cols );
1132
                }
1133
1134
                do_action( 'wpinv_display_before_total', $invoice, $cols );
1135
                ?>
1136
                <tr class="table-active row-total">
1137
                    <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php echo apply_filters( 'wpinv_print_cart_total_label', '<strong>' . __( 'Total', 'invoicing' ) . ':</strong>', $invoice ); ?></td>
1138
                    <td class="total"><strong><?php _e( wpinv_payment_total( $invoice_id, true ) ) ?></strong></td>
1139
                </tr>
1140
                <?php
1141
                do_action( 'wpinv_display_after_total', $invoice, $cols );
1142
1143
                do_action( 'wpinv_display_line_end', $invoice, $cols );
1144
            }
1145
        ?>
1146
        </tbody>
1147
    </table>
1148
    <?php
1149
    echo ob_get_clean();
1150
}
1151
1152
/**
1153
 * @param WPInv_Invoice $invoice
1154
 */
1155
function wpinv_display_invoice_notes( $invoice ) {
1156
1157
    $notes = wpinv_get_invoice_notes( $invoice->ID, 'customer' );
1158
1159
    if ( empty( $notes ) ) {
1160
        return;
1161
    }
1162
1163
    echo '<div class="wpi_invoice_notes_container">';
1164
    echo '<h2>' . __( 'Invoice Notes', 'invoicing' ) .'</h2>';
1165
    echo '<ul class="wpi_invoice_notes">';
1166
1167
    foreach( $notes as $note ) {
1168
        wpinv_get_invoice_note_line_item( $note );
1169
    }
1170
1171
    echo '</ul>';
1172
    echo '</div>';
1173
}
1174
add_action( 'wpinv_invoice_print_after_line_items', 'wpinv_display_invoice_notes' );
1175
1176
function wpinv_display_invoice_totals( $invoice_id = 0 ) {
1177
    $use_taxes = wpinv_use_taxes();
1178
1179
    do_action( 'wpinv_before_display_totals_table', $invoice_id ); 
1180
    ?>
1181
    <table class="table table-sm table-bordered table-responsive">
1182
        <tbody>
1183
            <?php do_action( 'wpinv_before_display_totals' ); ?>
1184
            <tr class="row-sub-total">
1185
                <td class="rate"><strong><?php _e( 'Sub Total', 'invoicing' ); ?></strong></td>
1186
                <td class="total"><strong><?php _e( wpinv_subtotal( $invoice_id, true ) ) ?></strong></td>
1187
            </tr>
1188
            <?php do_action( 'wpinv_after_display_totals' ); ?>
1189
            <?php if ( wpinv_discount( $invoice_id, false ) > 0 ) { ?>
1190
                <tr class="row-discount">
1191
                    <td class="rate"><?php wpinv_get_discount_label( wpinv_discount_code( $invoice_id ) ); ?></td>
1192
                    <td class="total"><?php echo wpinv_discount( $invoice_id, true, true ); ?></td>
1193
                </tr>
1194
            <?php do_action( 'wpinv_after_display_discount' ); ?>
1195
            <?php } ?>
1196
            <?php if ( $use_taxes ) { ?>
1197
            <tr class="row-tax">
1198
                <td class="rate"><?php _e( 'Tax', 'invoicing' ); ?></td>
1199
                <td class="total"><?php _e( wpinv_tax( $invoice_id, true ) ) ?></td>
1200
            </tr>
1201
            <?php do_action( 'wpinv_after_display_tax' ); ?>
1202
            <?php } ?>
1203
            <?php if ( $fees = wpinv_get_fees( $invoice_id ) ) { ?>
1204
                <?php foreach ( $fees as $fee ) { ?>
1205
                    <tr class="row-fee">
1206
                        <td class="rate"><?php echo $fee['label']; ?></td>
1207
                        <td class="total"><?php echo $fee['amount_display']; ?></td>
1208
                    </tr>
1209
                <?php } ?>
1210
            <?php } ?>
1211
            <tr class="table-active row-total">
1212
                <td class="rate"><strong><?php _e( 'Total', 'invoicing' ) ?></strong></td>
1213
                <td class="total"><strong><?php _e( wpinv_payment_total( $invoice_id, true ) ) ?></strong></td>
1214
            </tr>
1215
            <?php do_action( 'wpinv_after_totals' ); ?>
1216
        </tbody>
1217
1218
    </table>
1219
1220
    <?php do_action( 'wpinv_after_totals_table' );
1221
}
1222
1223
function wpinv_display_payments_info( $invoice_id = 0, $echo = true ) {
1224
    $invoice = wpinv_get_invoice( $invoice_id );
1225
1226
    ob_start();
1227
    do_action( 'wpinv_before_display_payments_info', $invoice_id );
1228
    if ( ( $gateway_title = $invoice->get_gateway_title() ) || $invoice->is_paid() || $invoice->is_refunded() ) {
1229
        ?>
1230
        <div class="wpi-payment-info">
1231
            <p class="wpi-payment-gateway"><?php echo wp_sprintf( __( 'Payment via %s', 'invoicing' ), $gateway_title ? $gateway_title : __( 'Manually', 'invoicing' ) ); ?></p>
1232
            <?php if ( $gateway_title ) { ?>
1233
            <p class="wpi-payment-transid"><?php echo wp_sprintf( __( 'Transaction ID: %s', 'invoicing' ), $invoice->get_transaction_id() ); ?></p>
1234
            <?php } ?>
1235
        </div>
1236
        <?php
1237
    }
1238
    do_action( 'wpinv_after_display_payments_info', $invoice_id );
1239
    $outout = ob_get_clean();
1240
1241
    if ( $echo ) {
1242
        echo $outout;
1243
    } else {
1244
        return $outout;
1245
    }
1246
}
1247
1248
function wpinv_display_style( $invoice ) {
1249
    wp_register_style( 'wpinv-single-style', WPINV_PLUGIN_URL . 'assets/css/invoice.css', array(), WPINV_VERSION );
1250
1251
    wp_print_styles( 'open-sans' );
1252
    wp_print_styles( 'wpinv-single-style' );
1253
1254
    $custom_css = wpinv_get_option('template_custom_css');
1255
    if(isset($custom_css) && !empty($custom_css)){
1256
        $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

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

1405
    $form_action     = esc_url( /** @scrutinizer ignore-type */ wpinv_get_checkout_uri() );
Loading history...
1406
    $payment_form    = wpinv_get_default_payment_form();
1407
1408
    ob_start();
1409
	    do_action( 'wpinv_checkout_content_before' );
1410
1411
        if ( wpinv_get_cart_contents() ) {
1412
1413
            // Get the form elements and items.
1414
	        $elements = $invoicing->form_elements->get_form_elements( $payment_form );
1415
	        $items    = $invoicing->form_elements->convert_checkout_items( wpinv_get_cart_contents(), wpinv_get_invoice_cart() );
1416
            ?>
1417
            <form class="wpinv_payment_form" action="<?php echo $form_action; ?>" method="POST">
1418
                <?php do_action( 'wpinv_main_checkout_form_top' ); ?>
1419
                <input type='hidden' name='form_id' value='<?php echo esc_attr( $payment_form ); ?>'/>
1420
                <input type='hidden' name='invoice_id' value='<?php echo esc_attr( $wpi_checkout_id ); ?>'/>
1421
                    <?php
1422
                        wp_nonce_field( 'wpinv_payment_form', 'wpinv_payment_form' );
1423
                        wp_nonce_field( 'vat_validation', '_wpi_nonce' );
1424
1425
                        foreach ( $elements as $element ) {
1426
                            do_action( 'wpinv_frontend_render_payment_form_element', $element, $items, $payment_form );
1427
                            do_action( "wpinv_frontend_render_payment_form_{$element['type']}", $element, $items, $payment_form );
1428
                        }
1429
                    ?>
1430
                <div class='wpinv_payment_form_errors alert alert-danger d-none'></div>
1431
                <?php do_action( 'wpinv_main_checkout_form_bottom' ); ?>
1432
            </form>
1433
        <?php
1434
1435
        } else {
1436
            do_action( 'wpinv_cart_empty' );
1437
        }
1438
        echo '</div><!--end #wpinv_checkout_wrap-->';
1439
	    do_action( 'wpinv_checkout_content_after' );
1440
        $content = ob_get_clean();
1441
1442
		return str_replace( 'sr-only', '', $content );
1443
}
1444
1445
function wpinv_checkout_cart( $cart_details = array(), $echo = true ) {
1446
    global $ajax_cart_details;
1447
    $ajax_cart_details = $cart_details;
1448
1449
    ob_start();
1450
    do_action( 'wpinv_before_checkout_cart' );
1451
    echo '<div id="wpinv_checkout_cart_form" method="post">';
1452
        echo '<div id="wpinv_checkout_cart_wrap">';
1453
            wpinv_get_template_part( 'wpinv-checkout-cart' );
1454
        echo '</div>';
1455
    echo '</div>';
1456
    do_action( 'wpinv_after_checkout_cart' );
1457
    $content = ob_get_clean();
1458
1459
    if ( $echo ) {
1460
        echo $content;
1461
    } else {
1462
        return $content;
1463
    }
1464
}
1465
add_action( 'wpinv_checkout_cart', 'wpinv_checkout_cart', 10 );
1466
1467
function wpinv_empty_cart_message() {
1468
	return apply_filters( 'wpinv_empty_cart_message', '<span class="wpinv_empty_cart">' . __( 'Your cart is empty.', 'invoicing' ) . '</span>' );
1469
}
1470
1471
/**
1472
 * Echoes the Empty Cart Message
1473
 *
1474
 * @since 1.0
1475
 * @return void
1476
 */
1477
function wpinv_empty_checkout_cart() {
1478
    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

1478
    echo /** @scrutinizer ignore-call */ aui()->alert(
Loading history...
1479
        array(
1480
            'type'    => 'warning',
1481
            'content' => wpinv_empty_cart_message(),
1482
        )
1483
    );
1484
}
1485
add_action( 'wpinv_cart_empty', 'wpinv_empty_checkout_cart' );
1486
1487
function wpinv_update_cart_button() {
1488
    if ( !wpinv_item_quantities_enabled() )
1489
        return;
1490
?>
1491
    <input type="submit" name="wpinv_update_cart_submit" class="wpinv-submit wpinv-no-js button" value="<?php _e( 'Update Cart', 'invoicing' ); ?>"/>
1492
    <input type="hidden" name="wpi_action" value="update_cart"/>
1493
<?php
1494
}
1495
1496
function wpinv_checkout_cart_columns() {
1497
    $default = 3;
1498
    if ( wpinv_item_quantities_enabled() ) {
1499
        $default++;
1500
    }
1501
    
1502
    if ( wpinv_use_taxes() ) {
1503
        $default++;
1504
    }
1505
1506
    return apply_filters( 'wpinv_checkout_cart_columns', $default );
1507
}
1508
1509
function wpinv_display_cart_messages() {
1510
    global $wpi_session;
1511
1512
    $messages = $wpi_session->get( 'wpinv_cart_messages' );
1513
1514
    if ( $messages ) {
1515
        foreach ( $messages as $message_id => $message ) {
1516
            // Try and detect what type of message this is
1517
            if ( strpos( strtolower( $message ), 'error' ) ) {
1518
                $type = 'error';
1519
            } elseif ( strpos( strtolower( $message ), 'success' ) ) {
1520
                $type = 'success';
1521
            } else {
1522
                $type = 'info';
1523
            }
1524
1525
            $classes = apply_filters( 'wpinv_' . $type . '_class', array( 'wpinv_errors', 'wpinv-alert', 'wpinv-alert-' . $type ) );
1526
1527
            echo '<div class="' . implode( ' ', $classes ) . '">';
1528
                // Loop message codes and display messages
1529
                    echo '<p class="wpinv_error" id="wpinv_msg_' . $message_id . '">' . $message . '</p>';
1530
            echo '</div>';
1531
        }
1532
1533
        // Remove all of the cart saving messages
1534
        $wpi_session->set( 'wpinv_cart_messages', null );
1535
    }
1536
}
1537
add_action( 'wpinv_before_checkout_cart', 'wpinv_display_cart_messages' );
1538
1539
function wpinv_discount_field() {
1540
    if ( isset( $_GET['wpi-gateway'] ) && wpinv_is_ajax_disabled() ) {
1541
        return; // Only show before a payment method has been selected if ajax is disabled
1542
    }
1543
1544
    if ( !wpinv_is_checkout() ) {
1545
        return;
1546
    }
1547
1548
    if ( wpinv_has_active_discounts() && wpinv_get_cart_total() ) {
1549
    ?>
1550
    <div id="wpinv-discount-field" class="panel panel-default">
1551
        <div class="panel-body">
1552
            <p>
1553
                <label class="wpinv-label" for="wpinv_discount_code"><strong><?php _e( 'Discount', 'invoicing' ); ?></strong></label>
1554
                <span class="wpinv-description"><?php _e( 'Enter a discount code if you have one.', 'invoicing' ); ?></span>
1555
            </p>
1556
            <div class="form-group row">
1557
                <div class="col-sm-4">
1558
                    <input class="wpinv-input form-control" type="text" id="wpinv_discount_code" name="wpinv_discount_code" placeholder="<?php _e( 'Enter discount code', 'invoicing' ); ?>"/>
1559
                </div>
1560
                <div class="col-sm-3">
1561
                    <button id="wpi-apply-discount" type="button" class="btn btn-success btn-sm"><?php _e( 'Apply Discount', 'invoicing' ); ?></button>
1562
                </div>
1563
                <div style="clear:both"></div>
1564
                <div class="col-sm-12 wpinv-discount-msg">
1565
                    <div class="alert alert-success"><i class="fa fa-check-circle"></i><span class="wpi-msg"></span></div>
1566
                    <div class="alert alert-error"><i class="fa fa-warning"></i><span class="wpi-msg"></span></div>
1567
                </div>
1568
            </div>
1569
        </div>
1570
    </div>
1571
<?php
1572
    }
1573
}
1574
add_action( 'wpinv_after_checkout_cart', 'wpinv_discount_field', -10 );
1575
1576
function wpinv_agree_to_terms_js() {
1577
    if ( wpinv_get_option( 'show_agree_to_terms', false ) ) {
1578
?>
1579
<script type="text/javascript">
1580
    jQuery(document).ready(function($){
1581
        $( document.body ).on('click', '.wpinv_terms_links', function(e) {
1582
            //e.preventDefault();
1583
            $('#wpinv_terms').slideToggle();
1584
            $('.wpinv_terms_links').toggle();
1585
            return false;
1586
        });
1587
    });
1588
</script>
1589
<?php
1590
    }
1591
}
1592
add_action( 'wpinv_checkout_form_top', 'wpinv_agree_to_terms_js' );
1593
1594
function wpinv_payment_mode_select() {
1595
    $gateways = wpinv_get_enabled_payment_gateways( true );
1596
    $gateways = apply_filters( 'wpinv_payment_gateways_on_cart', $gateways );
1597
    $invoice = wpinv_get_invoice( 0, true );
1598
1599
    do_action('wpinv_payment_mode_top');
1600
    $invoice_id = $invoice ? (int)$invoice->ID : 0;
1601
    $chosen_gateway = wpinv_get_chosen_gateway( $invoice_id );
1602
    ?>
1603
    <div id="wpinv_payment_mode_select" data-gateway="<?php echo $chosen_gateway; ?>" <?php echo ( ( $invoice && $invoice->is_free() ) ? 'style="display:none;" data-free="1"' : '' ); ?>>
1604
            <?php do_action( 'wpinv_payment_mode_before_gateways_wrap' ); ?>
1605
            <div id="wpinv-payment-mode-wrap" class="panel panel-default">
1606
                <div class="panel-heading wpi-payment_methods_title"><h3 class="panel-title"><?php _e( 'Select Payment Method', 'invoicing' ); ?></h3></div>
1607
                <div class="panel-body list-group wpi-payment_methods">
1608
                    <?php
1609
                    do_action( 'wpinv_payment_mode_before_gateways' );
1610
1611
                    if ( !empty( $gateways ) ) {
1612
                        foreach ( $gateways as $gateway_id => $gateway ) {
1613
                            $checked       = checked( $gateway_id, $chosen_gateway, false );
1614
                            $button_label  = wpinv_get_gateway_button_label( $gateway_id );
1615
                            $gateway_label = wpinv_get_gateway_checkout_label( $gateway_id );
1616
                            $description   = wpinv_get_gateway_description( $gateway_id );
1617
                            ?>
1618
                            <div class="list-group-item">
1619
                                <div class="radio">
1620
                                    <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>
1621
                                </div>
1622
                                <div style="display:none;" class="payment_box wpi_gateway_<?php echo esc_attr( $gateway_id );?>" role="alert">
1623
                                    <?php if ( !empty( $description ) ) { ?>
1624
                                        <div class="wpi-gateway-desc alert alert-info"><?php _e( $description, 'invoicing' ); ?></div>
1625
                                    <?php } ?>
1626
                                    <?php do_action( 'wpinv_' . $gateway_id . '_cc_form', $invoice_id ) ;?>
1627
                                </div>
1628
                            </div>
1629
                            <?php
1630
                        }
1631
                    } else {
1632
                        echo '<div class="alert alert-warning">'. __( 'No payment gateway active', 'invoicing' ) .'</div>';
1633
                    }
1634
1635
                    do_action( 'wpinv_payment_mode_after_gateways' );
1636
                    ?>
1637
                </div>
1638
            </div>
1639
            <?php do_action( 'wpinv_payment_mode_after_gateways_wrap' ); ?>
1640
    </div>
1641
    <?php
1642
    do_action('wpinv_payment_mode_bottom');
1643
}
1644
add_action( 'wpinv_payment_mode_select', 'wpinv_payment_mode_select' );
1645
1646
function wpinv_checkout_billing_info() {
1647
    if ( wpinv_is_checkout() ) {
1648
        $billing_details    = wpinv_checkout_billing_details();
1649
        $selected_country   = !empty( $billing_details['country'] ) ? $billing_details['country'] : wpinv_default_billing_country();
1650
        ?>
1651
        <div id="wpinv-fields" class="clearfix">
1652
            <div id="wpi-billing" class="wpi-billing clearfix panel panel-default">
1653
                <div class="panel-heading"><h3 class="panel-title"><?php _e( 'Billing Details', 'invoicing' );?></h3></div>
1654
                <div id="wpinv-fields-box" class="panel-body">
1655
                    <?php do_action( 'wpinv_checkout_billing_fields_first', $billing_details ); ?>
1656
                    <p class="wpi-cart-field wpi-col2 wpi-colf">
1657
                        <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>
1658
                        <?php
1659
                        echo wpinv_html_text( array(
1660
                                'id'            => 'wpinv_first_name',
1661
                                'name'          => 'wpinv_first_name',
1662
                                'value'         => $billing_details['first_name'],
1663
                                'class'         => 'wpi-input form-control',
1664
                                'placeholder'   => __( 'First name', 'invoicing' ),
1665
                                'required'      => (bool)wpinv_get_option( 'fname_mandatory' ),
1666
                            ) );
1667
                        ?>
1668
                    </p>
1669
                    <p class="wpi-cart-field wpi-col2 wpi-coll">
1670
                        <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>
1671
                        <?php
1672
                        echo wpinv_html_text( array(
1673
                                'id'            => 'wpinv_last_name',
1674
                                'name'          => 'wpinv_last_name',
1675
                                'value'         => $billing_details['last_name'],
1676
                                'class'         => 'wpi-input form-control',
1677
                                'placeholder'   => __( 'Last name', 'invoicing' ),
1678
                                'required'      => (bool)wpinv_get_option( 'lname_mandatory' ),
1679
                            ) );
1680
                        ?>
1681
                    </p>
1682
                    <p class="wpi-cart-field wpi-col2 wpi-colf">
1683
                        <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>
1684
                        <?php
1685
                        echo wpinv_html_text( array(
1686
                                'id'            => 'wpinv_address',
1687
                                'name'          => 'wpinv_address',
1688
                                'value'         => $billing_details['address'],
1689
                                'class'         => 'wpi-input form-control',
1690
                                'placeholder'   => __( 'Address', 'invoicing' ),
1691
                                'required'      => (bool)wpinv_get_option( 'address_mandatory' ),
1692
                            ) );
1693
                        ?>
1694
                    </p>
1695
                    <p class="wpi-cart-field wpi-col2 wpi-coll">
1696
                        <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>
1697
                        <?php
1698
                        echo wpinv_html_text( array(
1699
                                'id'            => 'wpinv_city',
1700
                                'name'          => 'wpinv_city',
1701
                                'value'         => $billing_details['city'],
1702
                                'class'         => 'wpi-input form-control',
1703
                                'placeholder'   => __( 'City', 'invoicing' ),
1704
                                'required'      => (bool)wpinv_get_option( 'city_mandatory' ),
1705
                            ) );
1706
                        ?>
1707
                    </p>
1708
                    <p id="wpinv_country_box" class="wpi-cart-field wpi-col2 wpi-colf">
1709
                        <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>
1710
                        <?php echo wpinv_html_select( array(
1711
                            'options'          => wpinv_get_country_list(),
1712
                            'name'             => 'wpinv_country',
1713
                            'id'               => 'wpinv_country',
1714
                            'selected'         => $selected_country,
1715
                            'show_option_all'  => false,
1716
                            'show_option_none' => false,
1717
                            'class'            => 'wpi-input form-control wpi_select2',
1718
                            'placeholder'      => __( 'Choose a country', 'invoicing' ),
1719
                            'required'         => (bool)wpinv_get_option( 'country_mandatory' ),
1720
                        ) ); ?>
1721
                    </p>
1722
                    <p id="wpinv_state_box" class="wpi-cart-field wpi-col2 wpi-coll">
1723
                        <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>
1724
                        <?php
1725
                        $states = wpinv_get_country_states( $selected_country );
1726
                        if( !empty( $states ) ) {
1727
                            echo wpinv_html_select( array(
1728
                                'options'          => $states,
1729
                                'name'             => 'wpinv_state',
1730
                                'id'               => 'wpinv_state',
1731
                                'selected'         => $billing_details['state'],
1732
                                'show_option_all'  => false,
1733
                                'show_option_none' => false,
1734
                                'class'            => 'wpi-input form-control wpi_select2',
1735
                                'placeholder'      => __( 'Choose a state', 'invoicing' ),
1736
                                'required'         => (bool)wpinv_get_option( 'state_mandatory' ),
1737
                            ) );
1738
                        } else {
1739
                            echo wpinv_html_text( array(
1740
                                'name'          => 'wpinv_state',
1741
                                'value'         => $billing_details['state'],
1742
                                'id'            => 'wpinv_state',
1743
                                'class'         => 'wpi-input form-control',
1744
                                'placeholder'   => __( 'State / Province', 'invoicing' ),
1745
                                'required'      => (bool)wpinv_get_option( 'state_mandatory' ),
1746
                            ) );
1747
                        }
1748
                        ?>
1749
                    </p>
1750
                    <p class="wpi-cart-field wpi-col2 wpi-colf">
1751
                        <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>
1752
                        <?php
1753
                        echo wpinv_html_text( array(
1754
                                'name'          => 'wpinv_zip',
1755
                                'value'         => $billing_details['zip'],
1756
                                'id'            => 'wpinv_zip',
1757
                                'class'         => 'wpi-input form-control',
1758
                                'placeholder'   => __( 'ZIP / Postcode', 'invoicing' ),
1759
                                'required'      => (bool)wpinv_get_option( 'zip_mandatory' ),
1760
                            ) );
1761
                        ?>
1762
                    </p>
1763
                    <p class="wpi-cart-field wpi-col2 wpi-coll">
1764
                        <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>
1765
                        <?php
1766
                        echo wpinv_html_text( array(
1767
                                'id'            => 'wpinv_phone',
1768
                                'name'          => 'wpinv_phone',
1769
                                'value'         => $billing_details['phone'],
1770
                                'class'         => 'wpi-input form-control',
1771
                                'placeholder'   => __( 'Phone', 'invoicing' ),
1772
                                'required'      => (bool)wpinv_get_option( 'phone_mandatory' ),
1773
                            ) );
1774
                        ?>
1775
                    </p>
1776
                    <?php do_action( 'wpinv_checkout_billing_fields_last', $billing_details ); ?>
1777
                    <div class="clearfix"></div>
1778
                </div>
1779
            </div>
1780
            <?php do_action( 'wpinv_after_billing_fields', $billing_details ); ?>
1781
        </div>
1782
        <?php
1783
    }
1784
}
1785
add_action( 'wpinv_checkout_billing_info', 'wpinv_checkout_billing_info' );
1786
1787
function wpinv_checkout_hidden_fields() {
1788
?>
1789
    <?php if ( is_user_logged_in() ) { ?>
1790
    <input type="hidden" name="wpinv_user_id" value="<?php echo get_current_user_id(); ?>"/>
1791
    <?php } ?>
1792
    <input type="hidden" name="wpi_action" value="payment" />
1793
<?php
1794
}
1795
1796
function wpinv_checkout_button_purchase() {
1797
    ob_start();
1798
?>
1799
    <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' ) ?>"/>
1800
<?php
1801
    return apply_filters( 'wpinv_checkout_button_purchase', ob_get_clean() );
1802
}
1803
1804
function wpinv_checkout_total() {
1805
    global $cart_total;
1806
?>
1807
<div id="wpinv_checkout_total" class="panel panel-info">
1808
    <div class="panel-body">
1809
    <?php
1810
    do_action( 'wpinv_purchase_form_before_checkout_total' );
1811
    ?>
1812
    <strong><?php _e( 'Invoice Total:', 'invoicing' ) ?></strong> <span class="wpinv-chdeckout-total"><?php echo $cart_total;?></span>
1813
    <?php
1814
    do_action( 'wpinv_purchase_form_after_checkout_total' );
1815
    ?>
1816
    </div>
1817
</div>
1818
<?php
1819
}
1820
add_action( 'wpinv_checkout_form_bottom', 'wpinv_checkout_total', 9998 );
1821
1822
function wpinv_checkout_accept_tandc() {
1823
    $page = wpinv_get_option( 'tandc_page' );
1824
    ?>
1825
    <div id="wpinv_checkout_tandc" class="panel panel-success">
1826
        <div class="panel-body">
1827
            <?php echo wpinv_get_policy_text(); ?>
1828
            <?php
1829
            if(isset($page) && (int)$page > 0 && apply_filters( 'wpinv_checkout_show_terms', true )){
1830
                $terms_link = esc_url( get_permalink( $page ) );
0 ignored issues
show
Bug introduced by
It seems like get_permalink($page) 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

1830
                $terms_link = esc_url( /** @scrutinizer ignore-type */ get_permalink( $page ) );
Loading history...
1831
                ?>
1832
                <label class="">
1833
                    <input type="checkbox" class="wpi-terms-checkbox" name="wpi_terms" id="wpi-terms" <?php checked( apply_filters( 'wpinv_terms_is_checked_default', isset( $_POST['wpi_terms'] ) ), true ); ?>> <span><?php printf( __( 'I&rsquo;ve read and accept the <a href="%s" target="_blank" class="wpi-terms-and-conditions-link">terms &amp; conditions</a>', 'invoicing' ), $terms_link ); ?></span> <span class="wpi-required">*</span>
1834
                </label>
1835
            <?php } ?>
1836
        </div>
1837
    </div>
1838
    <?php
1839
}
1840
add_action( 'wpinv_checkout_form_bottom', 'wpinv_checkout_accept_tandc', 9995 );
1841
1842
function wpinv_checkout_submit() {
1843
?>
1844
<div id="wpinv_purchase_submit" class="panel panel-success">
1845
    <div class="panel-body text-center">
1846
    <?php
1847
    do_action( 'wpinv_purchase_form_before_submit' );
1848
    wpinv_checkout_hidden_fields();
1849
    echo wpinv_checkout_button_purchase();
1850
    do_action( 'wpinv_purchase_form_after_submit' );
1851
    ?>
1852
    </div>
1853
</div>
1854
<?php
1855
}
1856
add_action( 'wpinv_checkout_form_bottom', 'wpinv_checkout_submit', 9999 );
1857
1858
function wpinv_receipt_billing_address( $invoice_id = 0 ) {
1859
    $invoice = wpinv_get_invoice( $invoice_id );
1860
1861
    if ( empty( $invoice ) ) {
1862
        return NULL;
1863
    }
1864
1865
    $billing_details = $invoice->get_user_info();
1866
    $address_row = wpinv_get_invoice_address_markup( $billing_details );
1867
1868
    ob_start();
1869
    ?>
1870
    <table class="table table-bordered table-sm wpi-billing-details">
1871
        <tbody>
1872
            <tr class="wpi-receipt-name">
1873
                <th class="text-left"><?php _e( 'Name', 'invoicing' ); ?></th>
1874
                <td><?php echo esc_html( trim( $billing_details['first_name'] . ' ' . $billing_details['last_name'] ) ) ;?></td>
1875
            </tr>
1876
            <tr class="wpi-receipt-email">
1877
                <th class="text-left"><?php _e( 'Email', 'invoicing' ); ?></th>
1878
                <td><?php echo $billing_details['email'] ;?></td>
1879
            </tr>
1880
            <tr class="wpi-receipt-address">
1881
                <th class="text-left"><?php _e( 'Address', 'invoicing' ); ?></th>
1882
                <td><?php echo $address_row ;?></td>
1883
            </tr>
1884
            <?php if ( $billing_details['phone'] ) { ?>
1885
            <tr class="wpi-receipt-phone">
1886
                <th class="text-left"><?php _e( 'Phone', 'invoicing' ); ?></th>
1887
                <td><?php echo esc_html( $billing_details['phone'] ) ;?></td>
1888
            </tr>
1889
            <?php } ?>
1890
        </tbody>
1891
    </table>
1892
    <?php
1893
    $output = ob_get_clean();
1894
    
1895
    $output = apply_filters( 'wpinv_receipt_billing_address', $output, $invoice_id );
1896
1897
    echo $output;
1898
}
1899
1900
function wpinv_filter_success_page_content( $content ) {
1901
    if ( isset( $_GET['payment-confirm'] ) && wpinv_is_success_page() ) {
1902
        if ( has_filter( 'wpinv_payment_confirm_' . sanitize_text_field( $_GET['payment-confirm'] ) ) ) {
1903
            $content = apply_filters( 'wpinv_payment_confirm_' . sanitize_text_field( $_GET['payment-confirm'] ), $content );
1904
        }
1905
    }
1906
1907
    return $content;
1908
}
1909
add_filter( 'the_content', 'wpinv_filter_success_page_content', 99999 );
1910
1911
function wpinv_receipt_actions( $invoice ) {
1912
    if ( !empty( $invoice ) ) {
1913
        $actions = array();
1914
1915
        if ( wpinv_user_can_view_invoice( $invoice->ID ) ) {
1916
            $actions['print']   = array(
1917
                'url'  => $invoice->get_view_url( true ),
1918
                'name' => __( 'Print Invoice', 'invoicing' ),
1919
                'class' => 'btn-primary',
1920
            );
1921
        }
1922
1923
        if ( is_user_logged_in() ) {
1924
            $actions['history'] = array(
1925
                'url'  => wpinv_get_history_page_uri(),
1926
                'name' => __( 'Invoice History', 'invoicing' ),
1927
                'class' => 'btn-warning',
1928
            );
1929
        }
1930
1931
        $actions = apply_filters( 'wpinv_invoice_receipt_actions', $actions, $invoice );
1932
1933
        if ( !empty( $actions ) ) {
1934
        ?>
1935
        <div class="wpinv-receipt-actions text-right">
1936
            <?php foreach ( $actions as $key => $action ) { $class = !empty($action['class']) ? sanitize_html_class( $action['class'] ) : ''; ?>
1937
            <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>
1938
            <?php } ?>
1939
        </div>
1940
        <?php
1941
        }
1942
    }
1943
}
1944
add_action( 'wpinv_receipt_start', 'wpinv_receipt_actions', -10, 1 );
1945
1946
function wpinv_invoice_link( $invoice_id ) {
1947
    $invoice = wpinv_get_invoice( $invoice_id );
1948
1949
    if ( empty( $invoice ) ) {
1950
        return NULL;
1951
    }
1952
1953
    $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

1953
    $invoice_link = '<a href="' . esc_url( /** @scrutinizer ignore-type */ $invoice->get_view_url() ) . '">' . $invoice->get_number() . '</a>';
Loading history...
1954
1955
    return apply_filters( 'wpinv_get_invoice_link', $invoice_link, $invoice );
1956
}
1957
1958
function wpinv_invoice_subscription_details( $invoice ) {
1959
    if ( !empty( $invoice ) && $invoice->is_recurring() && ! wpinv_is_subscription_payment( $invoice ) ) {
1960
        $subscription = wpinv_get_subscription( $invoice, true );
1961
1962
        if ( empty( $subscription ) ) {
1963
            return;
1964
        }
1965
1966
        $frequency = WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency($subscription->period, $subscription->frequency);
1967
        $billing = wpinv_price(wpinv_format_amount($subscription->recurring_amount), wpinv_get_invoice_currency_code($subscription->parent_payment_id)) . ' / ' . $frequency;
1968
        $initial = wpinv_price(wpinv_format_amount($subscription->initial_amount), wpinv_get_invoice_currency_code($subscription->parent_payment_id));
1969
1970
        $payments = $subscription->get_child_payments();
1971
        ?>
1972
        <div class="wpinv-subscriptions-details">
1973
            <h3 class="wpinv-subscriptions-t"><?php echo apply_filters( 'wpinv_subscription_details_title', __( 'Subscription Details', 'invoicing' ) ); ?></h3>
1974
            <table class="table">
1975
                <thead>
1976
                    <tr>
1977
                        <th><?php _e( 'Billing Cycle', 'invoicing' ) ;?></th>
1978
                        <th><?php _e( 'Start Date', 'invoicing' ) ;?></th>
1979
                        <th><?php _e( 'Expiration Date', 'invoicing' ) ;?></th>
1980
                        <th class="text-center"><?php _e( 'Times Billed', 'invoicing' ) ;?></th>
1981
                        <th class="text-center"><?php _e( 'Status', 'invoicing' ) ;?></th>
1982
                    </tr>
1983
                </thead>
1984
                <tbody>
1985
                    <tr>
1986
                        <td><?php printf(_x('%s then %s', 'Initial subscription amount then billing cycle and amount', 'invoicing'), $initial, $billing); ?></td>
1987
                        <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

1987
                        <td><?php echo date_i18n(/** @scrutinizer ignore-type */ get_option('date_format'), strtotime($subscription->created, current_time('timestamp'))); ?></td>
Loading history...
1988
                        <td><?php echo date_i18n(get_option('date_format'), strtotime($subscription->expiration, current_time('timestamp'))); ?></td>
1989
                        <td class="text-center"><?php echo $subscription->get_times_billed() . ' / ' . (($subscription->bill_times == 0) ? 'Until Cancelled' : $subscription->bill_times); ?></td>
1990
                        <td class="text-center wpi-sub-status"><?php echo $subscription->get_status_label(); ?></td>
1991
                    </tr>
1992
                </tbody>
1993
            </table>
1994
        </div>
1995
        <?php if ( !empty( $payments ) ) { ?>
1996
        <div class="wpinv-renewal-payments">
1997
            <h3 class="wpinv-renewals-t"><?php echo apply_filters( 'wpinv_renewal_payments_title', __( 'Renewal Payments', 'invoicing' ) ); ?></h3>
1998
            <table class="table">
1999
                <thead>
2000
                    <tr>
2001
                        <th>#</th>
2002
                        <th><?php _e( 'Invoice', 'invoicing' ) ;?></th>
2003
                        <th><?php _e( 'Date', 'invoicing' ) ;?></th>
2004
                        <th class="text-right"><?php _e( 'Amount', 'invoicing' ) ;?></th>
2005
                    </tr>
2006
                </thead>
2007
                <tbody>
2008
                    <?php
2009
                        $i = 1;
2010
                        foreach ( $payments as $payment ) {
2011
                            $invoice_id = $payment->ID;
2012
                    ?>
2013
                    <tr>
2014
                        <th scope="row"><?php echo $i;?></th>
2015
                        <td><?php echo wpinv_invoice_link( $invoice_id ) ;?></td>
2016
                        <td><?php echo wpinv_get_invoice_date( $invoice_id ); ?></td>
2017
                        <td class="text-right"><?php echo wpinv_payment_total( $invoice_id, true ); ?></td>
2018
                    </tr>
2019
                    <?php $i++; } ?>
2020
                </tbody>
2021
            </table>
2022
        </div>
2023
        <?php } ?>
2024
        <?php
2025
    }
2026
}
2027
2028
function wpinv_cart_total_label( $label, $invoice ) {
2029
    if ( empty( $invoice ) ) {
2030
        return $label;
2031
    }
2032
2033
    $prefix_label = '';
2034
    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...
2035
        $prefix_label   = '<span class="label label-primary label-recurring">' . __( 'Recurring Payment', 'invoicing' ) . '</span> ' . wpinv_subscription_payment_desc( $invoice );
2036
    } else if ( $invoice->is_renewal() ) {
2037
        $prefix_label   = '<span class="label label-primary label-renewal">' . __( 'Renewal Payment', 'invoicing' ) . '</span> ';        
2038
    }
2039
2040
    if ( $prefix_label != '' ) {
2041
        $label  = '<span class="wpinv-cart-sub-desc">' . $prefix_label . '</span> ' . $label;
2042
    }
2043
2044
    return $label;
2045
}
2046
add_filter( 'wpinv_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
2047
add_filter( 'wpinv_email_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
2048
add_filter( 'wpinv_print_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
2049
2050
add_action( 'wpinv_invoice_print_middle', 'wpinv_invoice_subscription_details', 10, 1 );
2051
2052
function wpinv_invoice_print_description( $invoice ) {
2053
    if ( empty( $invoice ) ) {
2054
        return NULL;
2055
    }
2056
    if ( $description = wpinv_get_invoice_description( $invoice->ID ) ) {
2057
        ?>
2058
        <div class="row wpinv-lower">
2059
            <div class="col-sm-12 wpinv-description">
2060
                <?php echo wpautop( $description ); ?>
2061
            </div>
2062
        </div>
2063
        <?php
2064
    }
2065
}
2066
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

2066
add_action( 'wpinv_invoice_print_middle', 'wpinv_invoice_print_description', /** @scrutinizer ignore-type */ 10.1, 1 );
Loading history...
2067
2068
function wpinv_invoice_print_payment_info( $invoice ) {
2069
    if ( empty( $invoice ) ) {
2070
        return NULL;
2071
    }
2072
2073
    if ( $payments_info = wpinv_display_payments_info( $invoice->ID, false ) ) {
2074
        ?>
2075
        <div class="row wpinv-payments">
2076
            <div class="col-sm-12">
2077
                <?php echo $payments_info; ?>
2078
            </div>
2079
        </div>
2080
        <?php 
2081
    }
2082
}
2083
// add_action( 'wpinv_invoice_print_after_line_items', 'wpinv_invoice_print_payment_info', 10, 1 );
2084
2085
function wpinv_get_invoice_note_line_item( $note, $echo = true ) {
2086
    if ( empty( $note ) ) {
2087
        return NULL;
2088
    }
2089
2090
    if ( is_int( $note ) ) {
2091
        $note = get_comment( $note );
2092
    }
2093
2094
    if ( !( is_object( $note ) && is_a( $note, 'WP_Comment' ) ) ) {
2095
        return NULL;
2096
    }
2097
2098
    $note_classes   = array( 'note' );
2099
    $note_classes[] = get_comment_meta( $note->comment_ID, '_wpi_customer_note', true ) ? 'customer-note' : '';
2100
    $note_classes[] = $note->comment_author === 'System' ? 'system-note' : '';
2101
    $note_classes   = apply_filters( 'wpinv_invoice_note_class', array_filter( $note_classes ), $note );
2102
    $note_classes   = !empty( $note_classes ) ? implode( ' ', $note_classes ) : '';
2103
2104
    ob_start();
2105
    ?>
2106
    <li rel="<?php echo absint( $note->comment_ID ) ; ?>" class="<?php echo esc_attr( $note_classes ); ?>">
2107
        <div class="note_content">
2108
            <?php echo wpautop( wptexturize( wp_kses_post( $note->comment_content ) ) ); ?>
2109
        </div>
2110
        <p class="meta">
2111
            <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

2111
            <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...
2112
            <?php if ( is_admin() && ( $note->comment_author !== 'System' || wpinv_current_user_can_manage_invoicing() ) ) { ?>
2113
                <a href="#" class="delete_note"><?php _e( 'Delete note', 'invoicing' ); ?></a>
2114
            <?php } ?>
2115
        </p>
2116
    </li>
2117
    <?php
2118
    $note_content = ob_get_clean();
2119
    $note_content = apply_filters( 'wpinv_get_invoice_note_line_item', $note_content, $note, $echo );
2120
2121
    if ( $echo ) {
2122
        echo $note_content;
2123
    } else {
2124
        return $note_content;
2125
    }
2126
}
2127
2128
function wpinv_invalid_invoice_content() {
2129
    global $post;
2130
2131
    $invoice = wpinv_get_invoice( $post->ID );
2132
2133
    $error = __( 'This invoice is only viewable by clicking on the invoice link that was sent to you via email.', 'invoicing' );
2134
    if ( !empty( $invoice->ID ) && $invoice->has_status( array_keys( wpinv_get_invoice_statuses() ) ) ) {
2135
        if ( is_user_logged_in() ) {
2136
            if ( wpinv_require_login_to_checkout() ) {
2137
                if ( isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
2138
                    $error = __( 'You are not allowed to view this invoice.', 'invoicing' );
2139
                }
2140
            }
2141
        } else {
2142
            if ( wpinv_require_login_to_checkout() ) {
2143
                if ( isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
2144
                    $error = __( 'You must be logged in to view this invoice.', 'invoicing' );
2145
                }
2146
            }
2147
        }
2148
    } else {
2149
        $error = __( 'This invoice is deleted or does not exist.', 'invoicing' );
2150
    }
2151
    ?>
2152
    <div class="row wpinv-row-invalid">
2153
        <div class="col-md-6 col-md-offset-3 wpinv-message error">
2154
            <h3><?php _e( 'Access Denied', 'invoicing' ); ?></h3>
2155
            <p class="wpinv-msg-text"><?php echo $error; ?></p>
2156
        </div>
2157
    </div>
2158
    <?php
2159
}
2160
add_action( 'wpinv_invalid_invoice_content', 'wpinv_invalid_invoice_content' );
2161
2162
add_action( 'wpinv_checkout_billing_fields_last', 'wpinv_force_company_name_field');
2163
function wpinv_force_company_name_field(){
2164
    $invoice = wpinv_get_invoice_cart();
2165
    $user_id = wpinv_get_user_id( $invoice->ID );
2166
    $company = empty( $user_id ) ? "" : get_user_meta( $user_id, '_wpinv_company', true );
2167
    if ( 1 == wpinv_get_option( 'force_show_company' ) && !wpinv_use_taxes() ) {
2168
        ?>
2169
        <p class="wpi-cart-field wpi-col2 wpi-colf">
2170
            <label for="wpinv_company" class="wpi-label"><?php _e('Company Name', 'invoicing'); ?></label>
2171
            <?php
2172
            echo wpinv_html_text(array(
2173
                'id' => 'wpinv_company',
2174
                'name' => 'wpinv_company',
2175
                'value' => $company,
2176
                'class' => 'wpi-input form-control',
2177
                'placeholder' => __('Company name', 'invoicing'),
2178
                'required'      => true,
2179
            ));
2180
            ?>
2181
        </p>
2182
        <?php
2183
    }
2184
}
2185
2186
/**
2187
 * Function to get privacy policy text.
2188
 *
2189
 * @since 1.0.13
2190
 * @return string
2191
 */
2192
function wpinv_get_policy_text() {
2193
    $privacy_page_id = get_option( 'wp_page_for_privacy_policy', 0 );
2194
2195
    $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]' ));
2196
2197
    if(!$privacy_page_id){
2198
        $privacy_page_id = wpinv_get_option( 'privacy_page', 0 );
2199
    }
2200
2201
    $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

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

2295
    /** @scrutinizer ignore-call */ 
2296
    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...
2296
    add_filter( 'template_include', 'wpinv_template', 999, 1 );
2297
}
2298
2299
/**
2300
 * Helper function to display a payment form on the frontend.
2301
 */
2302
function getpaid_display_payment_form( $form ) {
2303
    global $invoicing;
2304
2305
    // Ensure that it is published.
2306
	if ( 'publish' != get_post_status( $form ) ) {
2307
		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

2307
		return /** @scrutinizer ignore-call */ aui()->alert(
Loading history...
2308
			array(
2309
				'type'    => 'warning',
2310
				'content' => __( 'This payment form is no longer active', 'invoicing' ),
2311
			)
2312
		);
2313
	}
2314
2315
    // Get the form elements and items.
2316
    $form     = absint( $form );
2317
	$elements = $invoicing->form_elements->get_form_elements( $form );
2318
	$items    = $invoicing->form_elements->get_form_items( $form );
2319
2320
	ob_start();
2321
	echo "<form class='wpinv_payment_form'>";
2322
	do_action( 'wpinv_payment_form_top' );
2323
	echo "<input type='hidden' name='form_id' value='$form'/>";
2324
	wp_nonce_field( 'wpinv_payment_form', 'wpinv_payment_form' );
2325
	wp_nonce_field( 'vat_validation', '_wpi_nonce' );
2326
2327
	foreach ( $elements as $element ) {
2328
		do_action( 'wpinv_frontend_render_payment_form_element', $element, $items, $form );
2329
		do_action( "wpinv_frontend_render_payment_form_{$element['type']}", $element, $items, $form );
2330
	}
2331
2332
	echo "<div class='wpinv_payment_form_errors alert alert-danger d-none'></div>";
2333
	do_action( 'wpinv_payment_form_bottom' );
2334
	echo '</form>';
2335
2336
	$content = ob_get_clean();
2337
	return str_replace( 'sr-only', '', $content );
2338
}
2339
2340
/**
2341
 * Helper function to display a item payment form on the frontend.
2342
 */
2343
function getpaid_display_item_payment_form( $items ) {
2344
    global $invoicing;
2345
2346
    foreach ( array_keys( $items ) as $id ) {
2347
	    if ( 'publish' != get_post_status( $id ) ) {
2348
		    unset( $items[ $id ] );
2349
	    }
2350
    }
2351
2352
    if ( empty( $items ) ) {
2353
		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

2353
		return /** @scrutinizer ignore-call */ aui()->alert(
Loading history...
2354
			array(
2355
				'type'    => 'warning',
2356
				'content' => __( 'No published items found', 'invoicing' ),
2357
			)
2358
		);
2359
    }
2360
2361
    $item_key = getpaid_convert_items_to_string( $items );
2362
2363
    // Get the form elements and items.
2364
    $form     = wpinv_get_default_payment_form();
2365
	$elements = $invoicing->form_elements->get_form_elements( $form );
2366
	$items    = $invoicing->form_elements->convert_normal_items( $items );
2367
2368
	ob_start();
2369
	echo "<form class='wpinv_payment_form'>";
2370
	do_action( 'wpinv_payment_form_top' );
2371
    echo "<input type='hidden' name='form_id' value='$form'/>";
2372
    echo "<input type='hidden' name='form_items' value='$item_key'/>";
2373
	wp_nonce_field( 'wpinv_payment_form', 'wpinv_payment_form' );
2374
	wp_nonce_field( 'vat_validation', '_wpi_nonce' );
2375
2376
	foreach ( $elements as $element ) {
2377
		do_action( 'wpinv_frontend_render_payment_form_element', $element, $items, $form );
2378
		do_action( "wpinv_frontend_render_payment_form_{$element['type']}", $element, $items, $form );
2379
	}
2380
2381
	echo "<div class='wpinv_payment_form_errors alert alert-danger d-none'></div>";
2382
	do_action( 'wpinv_payment_form_bottom' );
2383
	echo '</form>';
2384
2385
	$content = ob_get_clean();
2386
	return str_replace( 'sr-only', '', $content );
2387
}
2388
2389
/**
2390
 * Helper function to display an invoice payment form on the frontend.
2391
 */
2392
function getpaid_display_invoice_payment_form( $invoice_id ) {
2393
    global $invoicing;
2394
2395
    $invoice = wpinv_get_invoice( $invoice_id );
2396
2397
    if ( empty( $invoice ) ) {
2398
		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

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