Passed
Pull Request — master (#376)
by Brian
106:18
created

wpinv_get_published_items_for_dropdown()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 19
rs 9.9
cc 3
nc 2
nop 0
1
<?php
2
/**
3
 * Contains functions related to Invoicing plugin.
4
 *
5
 * @since 1.0.0
6
 * @package Invoicing
7
 */
8
 
9
// MUST have WordPress.
10
if ( !defined( 'WPINC' ) ) {
11
    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
12
}
13
14
if ( !is_admin() ) {
15
    add_filter( 'template_include', 'wpinv_template', 10, 1 );
16
    add_action( 'wpinv_invoice_print_body_start', 'wpinv_display_invoice_top_bar' );
17
    add_action( 'wpinv_invoice_top_bar_left', 'wpinv_invoice_display_left_actions' );
18
    add_action( 'wpinv_invoice_top_bar_right', 'wpinv_invoice_display_right_actions' );
19
}
20
21
function wpinv_template_path() {
22
    return apply_filters( 'wpinv_template_path', wpinv_get_theme_template_dir_name() );
23
}
24
25
function wpinv_display_invoice_top_bar( $invoice ) {
26
    if ( empty( $invoice ) ) {
27
        return;
28
    }
29
    ?>
30
    <div class="row wpinv-top-bar no-print">
31
        <div class="container">
32
            <div class="col-xs-6">
33
                <?php do_action( 'wpinv_invoice_top_bar_left', $invoice );?>
34
            </div>
35
            <div class="col-xs-6 text-right">
36
                <?php do_action( 'wpinv_invoice_top_bar_right', $invoice );?>
37
            </div>
38
        </div>
39
    </div>
40
    <?php
41
}
42
43
function wpinv_invoice_display_left_actions( $invoice ) {
44
    if ( empty( $invoice ) ) {
45
        return; // Exit if invoice is not set.
46
    }
47
    
48
    if ( $invoice->post_type == 'wpi_invoice' ) {
49
        if ( $invoice->needs_payment() ) {
50
            ?> <a class="btn btn-success btn-sm" title="<?php esc_attr_e( 'Pay This Invoice', 'invoicing' ); ?>" href="<?php echo esc_url( $invoice->get_checkout_payment_url() ); ?>"><?php _e( 'Pay For Invoice', 'invoicing' ); ?></a><?php
51
        }
52
    }
53
    do_action('wpinv_invoice_display_left_actions', $invoice);
54
}
55
56
function wpinv_invoice_display_right_actions( $invoice ) {
57
    if ( empty( $invoice ) ) {
58
        return; // Exit if invoice is not set.
59
    }
60
61
    if ( $invoice->post_type == 'wpi_invoice' ) { ?>
62
        <a class="btn btn-primary btn-sm btn-print-invoice" onclick="window.print();" href="javascript:void(0)"><?php _e( 'Print Invoice', 'invoicing' ); ?></a>
63
        <?php if ( is_user_logged_in() ) { ?>
64
        &nbsp;&nbsp;<a class="btn btn-warning btn-sm btn-invoice-history" href="<?php echo esc_url( wpinv_get_history_page_uri() ); ?>"><?php _e( 'Invoice History', 'invoicing' ); ?></a>
0 ignored issues
show
Bug introduced by
It seems like wpinv_get_history_page_uri() can also be of type false; however, parameter $url of esc_url() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

64
        &nbsp;&nbsp;<a class="btn btn-warning btn-sm btn-invoice-history" href="<?php echo esc_url( /** @scrutinizer ignore-type */ wpinv_get_history_page_uri() ); ?>"><?php _e( 'Invoice History', 'invoicing' ); ?></a>
Loading history...
65
        <?php }
66
    }
67
    do_action('wpinv_invoice_display_right_actions', $invoice);
68
}
69
70
function wpinv_before_invoice_content( $content ) {
71
    global $post;
72
73
    if ( !empty( $post ) && $post->post_type == 'wpi_invoice' && is_singular( 'wpi_invoice' ) && is_main_query() ) {
74
        ob_start();
75
        do_action( 'wpinv_before_invoice_content', $post->ID );
76
        $content = ob_get_clean() . $content;
77
    }
78
79
    return $content;
80
}
81
add_filter( 'the_content', 'wpinv_before_invoice_content' );
82
83
function wpinv_after_invoice_content( $content ) {
84
    global $post;
85
86
    if ( !empty( $post ) && $post->post_type == 'wpi_invoice' && is_singular( 'wpi_invoice' ) && is_main_query() ) {
87
        ob_start();
88
        do_action( 'wpinv_after_invoice_content', $post->ID );
89
        $content .= ob_get_clean();
90
    }
91
92
    return $content;
93
}
94
add_filter( 'the_content', 'wpinv_after_invoice_content' );
95
96
function wpinv_get_templates_dir() {
97
    return WPINV_PLUGIN_DIR . 'templates';
98
}
99
100
function wpinv_get_templates_url() {
101
    return WPINV_PLUGIN_URL . 'templates';
102
}
103
104
function wpinv_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
105
    if ( ! empty( $args ) && is_array( $args ) ) {
106
		extract( $args );
107
	}
108
109
	$located = wpinv_locate_template( $template_name, $template_path, $default_path );
110
	// Allow 3rd party plugin filter template file from their plugin.
111
	$located = apply_filters( 'wpinv_get_template', $located, $template_name, $args, $template_path, $default_path );
112
113
	if ( ! file_exists( $located ) ) {
114
        _doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $located ), '2.1' );
115
		return;
116
	}
117
118
	do_action( 'wpinv_before_template_part', $template_name, $template_path, $located, $args );
119
120
	include( $located );
121
122
	do_action( 'wpinv_after_template_part', $template_name, $template_path, $located, $args );
123
}
124
125
function wpinv_get_template_html( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
126
	ob_start();
127
	wpinv_get_template( $template_name, $args, $template_path, $default_path );
128
	return ob_get_clean();
129
}
130
131
function wpinv_locate_template( $template_name, $template_path = '', $default_path = '' ) {
132
    if ( ! $template_path ) {
133
        $template_path = wpinv_template_path();
134
    }
135
136
    if ( ! $default_path ) {
137
        $default_path = WPINV_PLUGIN_DIR . 'templates/';
138
    }
139
140
    // Look within passed path within the theme - this is priority.
141
    $template = locate_template(
142
        array(
143
            trailingslashit( $template_path ) . $template_name,
144
            $template_name
145
        )
146
    );
147
148
    // Get default templates/
149
    if ( !$template && $default_path ) {
150
        $template = trailingslashit( $default_path ) . $template_name;
151
    }
152
153
    // Return what we found.
154
    return apply_filters( 'wpinv_locate_template', $template, $template_name, $template_path );
155
}
156
157
function wpinv_get_template_part( $slug, $name = null, $load = true ) {
158
	do_action( 'get_template_part_' . $slug, $slug, $name );
159
160
	// Setup possible parts
161
	$templates = array();
162
	if ( isset( $name ) )
163
		$templates[] = $slug . '-' . $name . '.php';
164
	$templates[] = $slug . '.php';
165
166
	// Allow template parts to be filtered
167
	$templates = apply_filters( 'wpinv_get_template_part', $templates, $slug, $name );
168
169
	// Return the part that is found
170
	return wpinv_locate_tmpl( $templates, $load, false );
171
}
172
173
function wpinv_locate_tmpl( $template_names, $load = false, $require_once = true ) {
174
	// No file found yet
175
	$located = false;
176
177
	// Try to find a template file
178
	foreach ( (array)$template_names as $template_name ) {
179
180
		// Continue if template is empty
181
		if ( empty( $template_name ) )
182
			continue;
183
184
		// Trim off any slashes from the template name
185
		$template_name = ltrim( $template_name, '/' );
186
187
		// try locating this template file by looping through the template paths
188
		foreach( wpinv_get_theme_template_paths() as $template_path ) {
189
190
			if( file_exists( $template_path . $template_name ) ) {
191
				$located = $template_path . $template_name;
192
				break;
193
			}
194
		}
195
196
		if( !empty( $located ) ) {
197
			break;
198
		}
199
	}
200
201
	if ( ( true == $load ) && ! empty( $located ) )
202
		load_template( $located, $require_once );
203
204
	return $located;
205
}
206
207
function wpinv_get_theme_template_paths() {
208
	$template_dir = wpinv_get_theme_template_dir_name();
209
210
	$file_paths = array(
211
		1 => trailingslashit( get_stylesheet_directory() ) . $template_dir,
212
		10 => trailingslashit( get_template_directory() ) . $template_dir,
213
		100 => wpinv_get_templates_dir()
214
	);
215
216
	$file_paths = apply_filters( 'wpinv_template_paths', $file_paths );
217
218
	// sort the file paths based on priority
219
	ksort( $file_paths, SORT_NUMERIC );
220
221
	return array_map( 'trailingslashit', $file_paths );
222
}
223
224
function wpinv_get_theme_template_dir_name() {
225
	return trailingslashit( apply_filters( 'wpinv_templates_dir', 'invoicing' ) );
226
}
227
228
function wpinv_checkout_meta_tags() {
229
230
	$pages   = array();
231
	$pages[] = wpinv_get_option( 'success_page' );
232
	$pages[] = wpinv_get_option( 'failure_page' );
233
	$pages[] = wpinv_get_option( 'invoice_history_page' );
234
	$pages[] = wpinv_get_option( 'invoice_subscription_page' );
235
236
	if( !wpinv_is_checkout() && !is_page( $pages ) ) {
237
		return;
238
	}
239
240
	echo '<meta name="robots" content="noindex,nofollow" />' . "\n";
241
}
242
add_action( 'wp_head', 'wpinv_checkout_meta_tags' );
243
244
function wpinv_add_body_classes( $class ) {
245
	$classes = (array)$class;
246
247
	if( wpinv_is_checkout() ) {
248
		$classes[] = 'wpinv-checkout';
249
		$classes[] = 'wpinv-page';
250
	}
251
252
	if( wpinv_is_success_page() ) {
253
		$classes[] = 'wpinv-success';
254
		$classes[] = 'wpinv-page';
255
	}
256
257
	if( wpinv_is_failed_transaction_page() ) {
258
		$classes[] = 'wpinv-failed-transaction';
259
		$classes[] = 'wpinv-page';
260
	}
261
262
	if( wpinv_is_invoice_history_page() ) {
263
		$classes[] = 'wpinv-history';
264
		$classes[] = 'wpinv-page';
265
	}
266
267
	if( wpinv_is_subscriptions_history_page() ) {
268
		$classes[] = 'wpinv-subscription';
269
		$classes[] = 'wpinv-page';
270
	}
271
272
	if( wpinv_is_test_mode() ) {
273
		$classes[] = 'wpinv-test-mode';
274
		$classes[] = 'wpinv-page';
275
	}
276
277
	return array_unique( $classes );
278
}
279
add_filter( 'body_class', 'wpinv_add_body_classes' );
280
281
function wpinv_html_dropdown( $name = 'wpinv_discounts', $selected = 0, $status = '' ) {
282
    $args = array( 'nopaging' => true );
283
284
    if ( ! empty( $status ) )
285
        $args['post_status'] = $status;
286
287
    $discounts = wpinv_get_discounts( $args );
288
    $options   = array();
289
290
    if ( $discounts ) {
291
        foreach ( $discounts as $discount ) {
292
            $options[ absint( $discount->ID ) ] = esc_html( get_the_title( $discount->ID ) );
293
        }
294
    } else {
295
        $options[0] = __( 'No discounts found', 'invoicing' );
296
    }
297
298
    $output = wpinv_html_select( array(
299
        'name'             => $name,
300
        'selected'         => $selected,
301
        'options'          => $options,
302
        'show_option_all'  => false,
303
        'show_option_none' => false,
304
    ) );
305
306
    return $output;
307
}
308
309
function wpinv_html_year_dropdown( $name = 'year', $selected = 0, $years_before = 5, $years_after = 0 ) {
310
    $current     = date( 'Y' );
311
    $start_year  = $current - absint( $years_before );
312
    $end_year    = $current + absint( $years_after );
313
    $selected    = empty( $selected ) ? date( 'Y' ) : $selected;
314
    $options     = array();
315
316
    while ( $start_year <= $end_year ) {
317
        $options[ absint( $start_year ) ] = $start_year;
318
        $start_year++;
319
    }
320
321
    $output = wpinv_html_select( array(
322
        'name'             => $name,
323
        'selected'         => $selected,
324
        'options'          => $options,
325
        'show_option_all'  => false,
326
        'show_option_none' => false
327
    ) );
328
329
    return $output;
330
}
331
332
function wpinv_html_month_dropdown( $name = 'month', $selected = 0 ) {
333
334
    $options = array(
335
        '1'  => __( 'January', 'invoicing' ),
336
        '2'  => __( 'February', 'invoicing' ),
337
        '3'  => __( 'March', 'invoicing' ),
338
        '4'  => __( 'April', 'invoicing' ),
339
        '5'  => __( 'May', 'invoicing' ),
340
        '6'  => __( 'June', 'invoicing' ),
341
        '7'  => __( 'July', 'invoicing' ),
342
        '8'  => __( 'August', 'invoicing' ),
343
        '9'  => __( 'September', 'invoicing' ),
344
        '10' => __( 'October', 'invoicing' ),
345
        '11' => __( 'November', 'invoicing' ),
346
        '12' => __( 'December', 'invoicing' ),
347
    );
348
349
    // If no month is selected, default to the current month
350
    $selected = empty( $selected ) ? date( 'n' ) : $selected;
351
352
    $output = wpinv_html_select( array(
353
        'name'             => $name,
354
        'selected'         => $selected,
355
        'options'          => $options,
356
        'show_option_all'  => false,
357
        'show_option_none' => false
358
    ) );
359
360
    return $output;
361
}
362
363
function wpinv_html_select( $args = array() ) {
364
    $defaults = array(
365
        'options'          => array(),
366
        'name'             => null,
367
        'class'            => '',
368
        'id'               => '',
369
        'selected'         => 0,
370
        'placeholder'      => null,
371
        'multiple'         => false,
372
        'show_option_all'  => _x( 'All', 'all dropdown items', 'invoicing' ),
373
        'show_option_none' => _x( 'None', 'no dropdown items', 'invoicing' ),
374
        'data'             => array(),
375
        'onchange'         => null,
376
        'required'         => false,
377
        'disabled'         => false,
378
        'readonly'         => false,
379
    );
380
381
    $args = wp_parse_args( $args, $defaults );
0 ignored issues
show
Security Variable Injection introduced by
$args can contain request data and is used in variable name context(s) leading to a potential security vulnerability.

2 paths for user data to reach this point

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

Used in variable context

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

General Strategies to prevent injection

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

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

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

$sanitized = (integer) $tainted;
Loading history...
382
383
    $data_elements = '';
384
    foreach ( $args['data'] as $key => $value ) {
385
        $data_elements .= ' data-' . esc_attr( $key ) . '="' . esc_attr( $value ) . '"';
386
    }
387
388
    if( $args['multiple'] ) {
389
        $multiple = ' MULTIPLE';
390
    } else {
391
        $multiple = '';
392
    }
393
394
    if( $args['placeholder'] ) {
395
        $placeholder = $args['placeholder'];
396
    } else {
397
        $placeholder = '';
398
    }
399
    
400
    $options = '';
401
    if( !empty( $args['onchange'] ) ) {
402
        $options .= ' onchange="' . esc_attr( $args['onchange'] ) . '"';
403
    }
404
    
405
    if( !empty( $args['required'] ) ) {
406
        $options .= ' required="required"';
407
    }
408
    
409
    if( !empty( $args['disabled'] ) ) {
410
        $options .= ' disabled';
411
    }
412
    
413
    if( !empty( $args['readonly'] ) ) {
414
        $options .= ' readonly';
415
    }
416
417
    $class  = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
418
    $output = '<select name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( $args['id'] ) . '" class="wpinv-select ' . $class . '"' . $multiple . ' data-placeholder="' . $placeholder . '" ' . trim( $options ) . $data_elements . '>';
419
420
    if ( $args['show_option_all'] ) {
421
        if( $args['multiple'] ) {
422
            $selected = selected( true, in_array( 0, $args['selected'] ), false );
423
        } else {
424
            $selected = selected( $args['selected'], 0, false );
425
        }
426
        $output .= '<option value="all"' . $selected . '>' . esc_html( $args['show_option_all'] ) . '</option>';
427
    }
428
429
    if ( !empty( $args['options'] ) ) {
430
431
        if ( $args['show_option_none'] ) {
432
            if( $args['multiple'] ) {
433
                $selected = selected( true, in_array( "", $args['selected'] ), false );
434
            } else {
435
                $selected = selected( $args['selected'] === "", true, false );
436
            }
437
            $output .= '<option value=""' . $selected . '>' . esc_html( $args['show_option_none'] ) . '</option>';
438
        }
439
440
        foreach( $args['options'] as $key => $option ) {
441
442
            if( $args['multiple'] && is_array( $args['selected'] ) ) {
443
                $selected = selected( true, (bool)in_array( $key, $args['selected'] ), false );
444
            } else {
445
                $selected = selected( $args['selected'], $key, false );
446
            }
447
448
            $output .= '<option value="' . esc_attr( $key ) . '"' . $selected . '>' . esc_html( $option ) . '</option>';
449
        }
450
    }
451
452
    $output .= '</select>';
453
454
    return $output;
455
}
456
457
function wpinv_item_dropdown( $args = array() ) {
458
    $defaults = array(
459
        'name'              => 'wpi_item',
460
        'id'                => 'wpi_item',
461
        'class'             => '',
462
        'multiple'          => false,
463
        'selected'          => 0,
464
        'number'            => 100,
465
        'placeholder'       => __( 'Choose a item', 'invoicing' ),
466
        'data'              => array( 'search-type' => 'item' ),
467
        'show_option_all'   => false,
468
        'show_option_none'  => false,
469
        'show_recurring'    => false,
470
    );
471
472
    $args = wp_parse_args( $args, $defaults );
473
474
    $item_args = array(
475
        'post_type'      => 'wpi_item',
476
        'orderby'        => 'title',
477
        'order'          => 'ASC',
478
        'posts_per_page' => $args['number']
479
    );
480
481
    $item_args  = apply_filters( 'wpinv_item_dropdown_query_args', $item_args, $args, $defaults );
482
483
    $items      = get_posts( $item_args );
484
    $options    = array();
485
    if ( $items ) {
486
        foreach ( $items as $item ) {
487
            $title = esc_html( $item->post_title );
488
            
489
            if ( !empty( $args['show_recurring'] ) ) {
490
                $title .= wpinv_get_item_suffix( $item->ID, false );
491
            }
492
            
493
            $options[ absint( $item->ID ) ] = $title;
494
        }
495
    }
496
497
    // This ensures that any selected items are included in the drop down
498
    if( is_array( $args['selected'] ) ) {
499
        foreach( $args['selected'] as $item ) {
500
            if( ! in_array( $item, $options ) ) {
501
                $title = get_the_title( $item );
502
                if ( !empty( $args['show_recurring'] ) ) {
503
                    $title .= wpinv_get_item_suffix( $item, false );
504
                }
505
                $options[$item] = $title;
506
            }
507
        }
508
    } elseif ( is_numeric( $args['selected'] ) && $args['selected'] !== 0 ) {
509
        if ( ! in_array( $args['selected'], $options ) ) {
510
            $title = get_the_title( $args['selected'] );
511
            if ( !empty( $args['show_recurring'] ) ) {
512
                $title .= wpinv_get_item_suffix( $args['selected'], false );
513
            }
514
            $options[$args['selected']] = get_the_title( $args['selected'] );
515
        }
516
    }
517
518
    $output = wpinv_html_select( array(
519
        'name'             => $args['name'],
520
        'selected'         => $args['selected'],
521
        'id'               => $args['id'],
522
        'class'            => $args['class'],
523
        'options'          => $options,
524
        'multiple'         => $args['multiple'],
525
        'placeholder'      => $args['placeholder'],
526
        'show_option_all'  => $args['show_option_all'],
527
        'show_option_none' => $args['show_option_none'],
528
        'data'             => $args['data'],
529
    ) );
530
531
    return $output;
532
}
533
534
/**
535
 * Returns an array of published items.
536
 */
537
function wpinv_get_published_items_for_dropdown() {
538
539
    $items = get_posts(
540
        array(
541
            'post_type'      => 'wpi_item',
542
            'orderby'        => 'title',
543
            'order'          => 'ASC',
544
            'posts_per_page' => '-1'
545
        )
546
    );
547
548
    $options = array();
549
    if ( $items ) {
550
        foreach ( $items as $item ) {
551
            $options[ $item->ID ] = esc_html( $item->post_title ) . wpinv_get_item_suffix( $item->ID, false );
552
        }
553
    }
554
555
    return $options;
556
}
557
558
function wpinv_html_checkbox( $args = array() ) {
559
    $defaults = array(
560
        'name'     => null,
561
        'current'  => null,
562
        'class'    => 'wpinv-checkbox',
563
        'options'  => array(
564
            'disabled' => false,
565
            'readonly' => false
566
        )
567
    );
568
569
    $args = wp_parse_args( $args, $defaults );
570
571
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
572
    $options = '';
573
    if ( ! empty( $args['options']['disabled'] ) ) {
574
        $options .= ' disabled="disabled"';
575
    } elseif ( ! empty( $args['options']['readonly'] ) ) {
576
        $options .= ' readonly';
577
    }
578
579
    $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 ) . ' />';
580
581
    return $output;
582
}
583
584
function wpinv_html_text( $args = array() ) {
585
    // Backwards compatibility
586
    if ( func_num_args() > 1 ) {
587
        $args = func_get_args();
588
589
        $name  = $args[0];
590
        $value = isset( $args[1] ) ? $args[1] : '';
591
        $label = isset( $args[2] ) ? $args[2] : '';
592
        $desc  = isset( $args[3] ) ? $args[3] : '';
593
    }
594
595
    $defaults = array(
596
        'id'           => '',
597
        'name'         => isset( $name )  ? $name  : 'text',
598
        'value'        => isset( $value ) ? $value : null,
599
        'label'        => isset( $label ) ? $label : null,
600
        'desc'         => isset( $desc )  ? $desc  : null,
601
        'placeholder'  => '',
602
        'class'        => 'regular-text',
603
        'disabled'     => false,
604
        'readonly'     => false,
605
        'required'     => false,
606
        'autocomplete' => '',
607
        'data'         => false
608
    );
609
610
    $args = wp_parse_args( $args, $defaults );
611
612
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
613
    $options = '';
614
    if( $args['required'] ) {
615
        $options .= ' required="required"';
616
    }
617
    if( $args['readonly'] ) {
618
        $options .= ' readonly';
619
    }
620
    if( $args['readonly'] ) {
621
        $options .= ' readonly';
622
    }
623
624
    $data = '';
625
    if ( !empty( $args['data'] ) ) {
626
        foreach ( $args['data'] as $key => $value ) {
627
            $data .= 'data-' . wpinv_sanitize_key( $key ) . '="' . esc_attr( $value ) . '" ';
628
        }
629
    }
630
631
    $output = '<span id="wpinv-' . wpinv_sanitize_key( $args['name'] ) . '-wrap">';
632
    $output .= '<label class="wpinv-label" for="' . wpinv_sanitize_key( $args['id'] ) . '">' . esc_html( $args['label'] ) . '</label>';
633
    if ( ! empty( $args['desc'] ) ) {
634
        $output .= '<span class="wpinv-description">' . esc_html( $args['desc'] ) . '</span>';
635
    }
636
637
    $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 ) . '/>';
638
639
    $output .= '</span>';
640
641
    return $output;
642
}
643
644
function wpinv_html_date_field( $args = array() ) {
645
    if( empty( $args['class'] ) ) {
646
        $args['class'] = 'wpiDatepicker';
647
    } elseif( ! strpos( $args['class'], 'wpiDatepicker' ) ) {
648
        $args['class'] .= ' wpiDatepicker';
649
    }
650
651
    return wpinv_html_text( $args );
652
}
653
654
function wpinv_html_textarea( $args = array() ) {
655
    $defaults = array(
656
        'name'        => 'textarea',
657
        'value'       => null,
658
        'label'       => null,
659
        'desc'        => null,
660
        'class'       => 'large-text',
661
        'disabled'    => false,
662
        'placeholder' => '',
663
    );
664
665
    $args = wp_parse_args( $args, $defaults );
666
667
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
668
    $disabled = '';
669
    if( $args['disabled'] ) {
670
        $disabled = ' disabled="disabled"';
671
    }
672
673
    $output = '<span id="wpinv-' . wpinv_sanitize_key( $args['name'] ) . '-wrap">';
674
    $output .= '<label class="wpinv-label" for="' . wpinv_sanitize_key( $args['name'] ) . '">' . esc_html( $args['label'] ) . '</label>';
675
    $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>';
676
677
    if ( ! empty( $args['desc'] ) ) {
678
        $output .= '<span class="wpinv-description">' . esc_html( $args['desc'] ) . '</span>';
679
    }
680
    $output .= '</span>';
681
682
    return $output;
683
}
684
685
function wpinv_html_ajax_user_search( $args = array() ) {
686
    $defaults = array(
687
        'name'        => 'user_id',
688
        'value'       => null,
689
        'placeholder' => __( 'Enter username', 'invoicing' ),
690
        'label'       => null,
691
        'desc'        => null,
692
        'class'       => '',
693
        'disabled'    => false,
694
        'autocomplete'=> 'off',
695
        'data'        => false
696
    );
697
698
    $args = wp_parse_args( $args, $defaults );
699
700
    $args['class'] = 'wpinv-ajax-user-search ' . $args['class'];
701
702
    $output  = '<span class="wpinv_user_search_wrap">';
703
        $output .= wpinv_html_text( $args );
704
        $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>';
705
    $output .= '</span>';
706
707
    return $output;
708
}
709
710
function wpinv_ip_geolocation() {
711
    global $wpinv_euvat;
712
    
713
    $ip         = !empty( $_GET['ip'] ) ? sanitize_text_field( $_GET['ip'] ) : '';    
714
    $content    = '';
715
    $iso        = '';
716
    $country    = '';
717
    $region     = '';
718
    $city       = '';
719
    $longitude  = '';
720
    $latitude   = '';
721
    $credit     = '';
722
    $address    = '';
723
    
724
    if ( wpinv_get_option( 'vat_ip_lookup' ) == 'geoip2' && $geoip2_city = $wpinv_euvat->geoip2_city_record( $ip ) ) {
725
        try {
726
            $iso        = $geoip2_city->country->isoCode;
727
            $country    = $geoip2_city->country->name;
728
            $region     = !empty( $geoip2_city->subdivisions ) && !empty( $geoip2_city->subdivisions[0]->name ) ? $geoip2_city->subdivisions[0]->name : '';
729
            $city       = $geoip2_city->city->name;
730
            $longitude  = $geoip2_city->location->longitude;
731
            $latitude   = $geoip2_city->location->latitude;
732
            $credit     = __( 'Geolocated using the information by MaxMind, available from <a href="http://www.maxmind.com" target="_blank">www.maxmind.com</a>', 'invoicing' );
733
        } catch( Exception $e ) { }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
734
    }
735
    
736
    if ( !( $iso && $longitude && $latitude ) && function_exists( 'simplexml_load_file' ) ) {
737
        try {
738
            $load_xml = simplexml_load_file( 'http://www.geoplugin.net/xml.gp?ip=' . $ip );
739
            
740
            if ( !empty( $load_xml ) && isset( $load_xml->geoplugin_countryCode ) && !empty( $load_xml->geoplugin_latitude ) && !empty( $load_xml->geoplugin_longitude ) ) {
741
                $iso        = $load_xml->geoplugin_countryCode;
742
                $country    = $load_xml->geoplugin_countryName;
743
                $region     = !empty( $load_xml->geoplugin_regionName ) ? $load_xml->geoplugin_regionName : '';
744
                $city       = !empty( $load_xml->geoplugin_city ) ? $load_xml->geoplugin_city : '';
745
                $longitude  = $load_xml->geoplugin_longitude;
746
                $latitude   = $load_xml->geoplugin_latitude;
747
                $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...
748
                $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;
749
            }
750
        } catch( Exception $e ) { }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
751
    }
752
    
753
    if ( $iso && $longitude && $latitude ) {
754
        if ( $city ) {
755
            $address .= $city . ', ';
756
        }
757
        
758
        if ( $region ) {
759
            $address .= $region . ', ';
760
        }
761
        
762
        $address .= $country . ' (' . $iso . ')';
763
        $content = '<p>'. sprintf( __( '<b>Address:</b> %s', 'invoicing' ), $address ) . '</p>';
764
        $content .= '<p>'. $credit . '</p>';
765
    } else {
766
        $content = '<p>'. sprintf( __( 'Unable to find geolocation for the IP address: %s', 'invoicing' ), $ip ) . '</p>';
767
    }
768
    ?>
769
<!DOCTYPE html>
770
<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>
771
<body>
772
    <?php if ( $latitude && $latitude ) { ?>
773
    <div id="map"></div>
774
        <script src="//cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.0-rc.1/leaflet.js"></script>
775
        <script type="text/javascript">
776
        var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
777
            osmAttrib = '&copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors',
778
            osm = L.tileLayer(osmUrl, {maxZoom: 18, attribution: osmAttrib}),
779
            latlng = new L.LatLng(<?php echo $latitude;?>, <?php echo $longitude;?>);
780
781
        var map = new L.Map('map', {center: latlng, zoom: 12, layers: [osm]});
782
783
        var marker = new L.Marker(latlng);
784
        map.addLayer(marker);
785
786
        marker.bindPopup("<p><?php esc_attr_e( $address );?></p>");
787
    </script>
788
    <?php } ?>
789
    <div style="height:100px"><?php echo $content; ?></div>
790
</body></html>
791
<?php
792
    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...
793
}
794
add_action( 'wp_ajax_wpinv_ip_geolocation', 'wpinv_ip_geolocation' );
795
add_action( 'wp_ajax_nopriv_wpinv_ip_geolocation', 'wpinv_ip_geolocation' );
796
797
// Set up the template for the invoice.
798
function wpinv_template( $template ) {
799
    global $post, $wp_query;
800
    
801
    if ( ( is_single() || is_404() ) && !empty( $post->ID ) && (get_post_type( $post->ID ) == 'wpi_invoice' or get_post_type( $post->ID ) == 'wpi_quote')) {
802
        if ( wpinv_user_can_view_invoice( $post->ID ) ) {
803
            $template = wpinv_get_template_part( 'wpinv-invoice-print', false, false );
804
        } else {
805
            $template = wpinv_get_template_part( 'wpinv-invalid-access', false, false );
806
        }
807
    }
808
809
    return $template;
810
}
811
812
function wpinv_get_business_address() {
813
    $business_address   = wpinv_store_address();
814
    $business_address   = !empty( $business_address ) ? wpautop( wp_kses_post( $business_address ) ) : '';
815
    
816
    /*
817
    $default_country    = wpinv_get_default_country();
818
    $default_state      = wpinv_get_default_state();
819
    
820
    $address_fields = array();
821
    if ( !empty( $default_state ) ) {
822
        $address_fields[] = wpinv_state_name( $default_state, $default_country );
823
    }
824
    
825
    if ( !empty( $default_country ) ) {
826
        $address_fields[] = wpinv_country_name( $default_country );
827
    }
828
    
829
    if ( !empty( $address_fields ) ) {
830
        $address_fields = implode( ", ", $address_fields );
831
                
832
        $business_address .= wpautop( wp_kses_post( $address_fields ) );
833
    }
834
    */
835
    
836
    $business_address = $business_address ? '<div class="address">' . $business_address . '</div>' : '';
837
    
838
    return apply_filters( 'wpinv_get_business_address', $business_address );
839
}
840
841
function wpinv_display_from_address() {
842
    global $wpinv_euvat;
843
    
844
    $from_name = $wpinv_euvat->get_company_name();
845
    if (empty($from_name)) {
846
        $from_name = wpinv_get_business_name();
847
    }
848
    ?><div class="from col-xs-2"><strong><?php _e( 'From:', 'invoicing' ) ?></strong></div>
849
    <div class="wrapper col-xs-10">
850
        <div class="name"><?php echo esc_html( $from_name ); ?></div>
851
        <?php if ( $address = wpinv_get_business_address() ) { ?>
852
        <div class="address"><?php echo wpautop( wp_kses_post( $address ) );?></div>
853
        <?php } ?>
854
        <?php if ( $email_from = wpinv_mail_get_from_address() ) { ?>
855
        <div class="email_from"><?php echo wp_sprintf( __( 'Email: %s', 'invoicing' ), $email_from );?></div>
856
        <?php } ?>
857
    </div>
858
    <?php
859
}
860
861
function wpinv_watermark( $id = 0 ) {
862
    $output = wpinv_get_watermark( $id );
863
    
864
    return apply_filters( 'wpinv_get_watermark', $output, $id );
865
}
866
867
function wpinv_get_watermark( $id ) {
868
    if ( !$id > 0 ) {
869
        return NULL;
870
    }
871
    $invoice = wpinv_get_invoice( $id );
872
    
873
    if ( !empty( $invoice ) && "wpi_invoice" === $invoice->post_type ) {
874
        if ( $invoice->is_paid() ) {
875
            return __( 'Paid', 'invoicing' );
876
        }
877
        if ( $invoice->is_refunded() ) {
878
            return __( 'Refunded', 'invoicing' );
879
        }
880
        if ( $invoice->has_status( array( 'wpi-cancelled' ) ) ) {
881
            return __( 'Cancelled', 'invoicing' );
882
        }
883
    }
884
    
885
    return NULL;
886
}
887
888
function wpinv_display_invoice_details( $invoice ) {
889
    global $wpinv_euvat;
890
    
891
    $invoice_id = $invoice->ID;
892
    $vat_name   = $wpinv_euvat->get_vat_name();
893
    $use_taxes  = wpinv_use_taxes();
894
    
895
    $invoice_status = wpinv_get_invoice_status( $invoice_id );
896
    ?>
897
    <table class="table table-bordered table-sm">
898
        <?php if ( $invoice_number = wpinv_get_invoice_number( $invoice_id ) ) { ?>
899
            <tr class="wpi-row-number">
900
                <th><?php echo apply_filters( 'wpinv_invoice_number_label', __( 'Invoice Number', 'invoicing' ), $invoice ); ?></th>
901
                <td><?php echo esc_html( $invoice_number ); ?></td>
902
            </tr>
903
        <?php } ?>
904
        <tr class="wpi-row-status">
905
            <th><?php echo apply_filters( 'wpinv_invoice_status_label', __( 'Invoice Status', 'invoicing' ), $invoice ); ?></th>
906
            <td><?php echo wpinv_invoice_status_label( $invoice_status, wpinv_get_invoice_status( $invoice_id, true ) ); ?></td>
907
        </tr>
908
        <?php if ( $invoice->is_renewal() ) { ?>
909
        <tr class="wpi-row-parent">
910
            <th><?php echo apply_filters( 'wpinv_invoice_parent_invoice_label', __( 'Parent Invoice', 'invoicing' ), $invoice ); ?></th>
911
            <td><?php echo wpinv_invoice_link( $invoice->parent_invoice ); ?></td>
912
        </tr>
913
        <?php } ?>
914
        <?php if ( ( $gateway_name = wpinv_get_payment_gateway_name( $invoice_id ) ) && ( $invoice->is_paid() || $invoice->is_refunded() ) ) { ?>
915
            <tr class="wpi-row-gateway">
916
                <th><?php echo apply_filters( 'wpinv_invoice_payment_method_label', __( 'Payment Method', 'invoicing' ), $invoice ); ?></th>
917
                <td><?php echo $gateway_name; ?></td>
918
            </tr>
919
        <?php } ?>
920
        <?php if ( $invoice_date = wpinv_get_invoice_date( $invoice_id ) ) { ?>
921
            <tr class="wpi-row-date">
922
                <th><?php echo apply_filters( 'wpinv_invoice_date_label', __( 'Invoice Date', 'invoicing' ), $invoice ); ?></th>
923
                <td><?php echo $invoice_date; ?></td>
924
            </tr>
925
        <?php } ?>
926
        <?php do_action( 'wpinv_display_details_before_due_date', $invoice_id ); ?>
927
        <?php if ( wpinv_get_option( 'overdue_active' ) && $invoice->needs_payment() && ( $due_date = $invoice->get_due_date( true ) ) ) { ?>
928
            <tr class="wpi-row-date">
929
                <th><?php echo apply_filters( 'wpinv_invoice_due_date_label', __( 'Due Date', 'invoicing' ), $invoice ); ?></th>
930
                <td><?php echo $due_date; ?></td>
931
            </tr>
932
        <?php } ?>
933
        <?php do_action( 'wpinv_display_details_after_due_date', $invoice_id ); ?>
934
        <?php if ( $owner_vat_number = $wpinv_euvat->get_vat_number() ) { ?>
935
            <tr class="wpi-row-ovatno">
936
                <th><?php echo apply_filters( 'wpinv_invoice_owner_vat_number_label', wp_sprintf( __( 'Owner %s Number', 'invoicing' ), $vat_name ), $invoice, $vat_name ); ?></th>
937
                <td><?php echo $owner_vat_number; ?></td>
938
            </tr>
939
        <?php } ?>
940
        <?php if ( $use_taxes && ( $user_vat_number = wpinv_get_invoice_vat_number( $invoice_id ) ) ) { ?>
941
            <tr class="wpi-row-uvatno">
942
                <th><?php echo apply_filters( 'wpinv_invoice_user_vat_number_label', wp_sprintf( __( 'Invoice %s Number', 'invoicing' ), $vat_name ), $invoice, $vat_name ); ?></th>
943
                <td><?php echo $user_vat_number; ?></td>
944
            </tr>
945
        <?php } ?>
946
        <tr class="table-active tr-total wpi-row-total">
947
            <th><strong><?php _e( 'Total Amount', 'invoicing' ) ?></strong></th>
948
            <td><strong><?php echo wpinv_payment_total( $invoice_id, true ); ?></strong></td>
949
        </tr>
950
        <?php if ( $subscription = wpinv_get_subscription( $invoice_id ) ) { ?>
951
        <tr class="table-active wpi-row-recurring-total">
952
            <th><?php echo apply_filters( 'wpinv_invoice_parent_invoice_label', __( 'Recurring Amount', 'invoicing' ), $invoice ); ?></th>
953
            <td><strong><?php echo wpinv_price( wpinv_format_amount( $subscription->recurring_amount ), $invoice->get_currency() ); ?></strong></td>
954
        </tr>
955
        <tr class="wpi-row-expires">
956
            <th><?php echo apply_filters( 'wpinv_invoice_parent_invoice_label', __( 'Renews On', 'invoicing' ), $invoice ); ?></th>
957
            <td><?php echo sanitize_text_field( $subscription->expiration ); ?></td>
958
        </tr>
959
        <?php } ?>
960
    </table>
961
<?php
962
}
963
964
/**
965
 * Retrieves the address markup to use on Invoices.
966
 * 
967
 * @since 1.0.13
968
 * @see `wpinv_get_full_address_format`
969
 * @see `wpinv_get_invoice_address_replacements`
970
 * @param array $billing_details customer's billing details
971
 * @param  string $separator How to separate address lines.
972
 * @return string
973
 */
974
function wpinv_get_invoice_address_markup( $billing_details, $separator = '<br/>' ) {
975
976
    // Retrieve the address markup...
977
    $country= empty( $billing_details['country'] ) ? '' : $billing_details['country'];
978
    $format = wpinv_get_full_address_format( $country );
979
980
    // ... and the replacements.
981
    $replacements = wpinv_get_invoice_address_replacements( $billing_details );
982
983
    $formatted_address = str_ireplace( array_keys( $replacements ), $replacements, $format );
984
    
985
	// Remove unavailable tags.
986
    $formatted_address = preg_replace( "/\{\{\w+\}\}/", '', $formatted_address );
987
988
    // Clean up white space.
989
	$formatted_address = preg_replace( '/  +/', ' ', trim( $formatted_address ) );
990
    $formatted_address = preg_replace( '/\n\n+/', "\n", $formatted_address );
991
    
992
    // Break newlines apart and remove empty lines/trim commas and white space.
993
	$formatted_address = array_filter( array_map( 'wpinv_trim_formatted_address_line', explode( "\n", $formatted_address ) ) );
994
995
    // Add html breaks.
996
	$formatted_address = implode( $separator, $formatted_address );
997
998
	// We're done!
999
	return $formatted_address;
1000
    
1001
}
1002
1003
function wpinv_display_to_address( $invoice_id = 0 ) {
1004
    $invoice = wpinv_get_invoice( $invoice_id );
1005
    
1006
    if ( empty( $invoice ) ) {
1007
        return NULL;
1008
    }
1009
    
1010
    $billing_details = $invoice->get_user_info();
1011
    $output = '<div class="to col-xs-2"><strong>' . __( 'To:', 'invoicing' ) . '</strong></div>';
1012
    $output .= '<div class="wrapper col-xs-10">';
1013
    
1014
    ob_start();
1015
    do_action( 'wpinv_display_to_address_top', $invoice );
1016
    $output .= ob_get_clean();
1017
    
1018
    $address_row = wpinv_get_invoice_address_markup( $billing_details );
1019
1020
    if ( $address_row ) {
1021
        $output .= '<div class="address">' . $address_row . '</div>';
1022
    }
1023
1024
    if ( $phone = $invoice->get_phone() ) {
1025
        $output .= '<div class="phone">' . wp_sprintf( __( 'Phone: %s', 'invoicing' ), esc_html( $phone ) ) . '</div>';
1026
    }
1027
    if ( $email = $invoice->get_email() ) {
1028
        $output .= '<div class="email">' . wp_sprintf( __( 'Email: %s' , 'invoicing'), esc_html( $email ) ) . '</div>';
1029
    }
1030
1031
    ob_start();
1032
    do_action( 'wpinv_display_to_address_bottom', $invoice );
1033
    $output .= ob_get_clean();
1034
    
1035
    $output .= '</div>';
1036
    $output = apply_filters( 'wpinv_display_to_address', $output, $invoice );
1037
1038
    echo $output;
1039
}
1040
1041
function wpinv_display_line_items( $invoice_id = 0 ) {
1042
    global $wpinv_euvat, $ajax_cart_details;
1043
    $invoice            = wpinv_get_invoice( $invoice_id );
1044
    $quantities_enabled = wpinv_item_quantities_enabled();
1045
    $use_taxes          = wpinv_use_taxes();
1046
    if ( !$use_taxes && (float)$invoice->get_tax() > 0 ) {
1047
        $use_taxes = true;
1048
    }
1049
    $zero_tax           = !(float)$invoice->get_tax() > 0 ? true : false;
1050
    $tax_label           = $use_taxes && $invoice->has_vat() ? $wpinv_euvat->get_vat_name() : __( 'Tax', 'invoicing' );
1051
    $tax_title          = !$zero_tax && $use_taxes ? ( wpinv_prices_include_tax() ? wp_sprintf( __( '(%s Incl.)', 'invoicing' ), $tax_label ) : wp_sprintf( __( '(%s Excl.)', 'invoicing' ), $tax_label ) ) : '';
1052
1053
    $cart_details       = $invoice->get_cart_details();
1054
    $ajax_cart_details  = $cart_details;
1055
    ob_start();
1056
    ?>
1057
    <table class="table table-sm table-bordered">
1058
        <thead>
1059
            <tr>
1060
                <th class="name"><strong><?php _e( "Item Name", "invoicing" );?></strong></th>
1061
                <th class="rate"><strong><?php _e( "Price", "invoicing" );?></strong></th>
1062
                <?php if ($quantities_enabled) { ?>
1063
                    <th class="qty"><strong><?php _e( "Qty", "invoicing" );?></strong></th>
1064
                <?php } ?>
1065
                <?php if ($use_taxes && !$zero_tax) { ?>
1066
                    <th class="tax"><strong><?php echo $tax_label . ' <span class="normal small">(%)</span>'; ?></strong></th>
1067
                <?php } ?>
1068
                <th class="total"><strong><?php echo __( "Item Total", "invoicing" ) . ' <span class="normal small">' . $tax_title . '<span>';?></strong></th>
1069
            </tr>
1070
        </thead>
1071
        <tbody>
1072
        <?php 
1073
            if ( !empty( $cart_details ) ) {
1074
                do_action( 'wpinv_display_line_items_start', $invoice );
1075
1076
                $count = 0;
1077
                $cols  = 3;
1078
                foreach ( $cart_details as $key => $cart_item ) {
1079
                    $item_id    = !empty($cart_item['id']) ? absint( $cart_item['id'] ) : '';
1080
                    $item_price = isset($cart_item["item_price"]) ? wpinv_round_amount( $cart_item["item_price"] ) : 0;
1081
                    $line_total = isset($cart_item["subtotal"]) ? wpinv_round_amount( $cart_item["subtotal"] ) : 0;
1082
                    $quantity   = !empty($cart_item['quantity']) && (int)$cart_item['quantity'] > 0 ? absint( $cart_item['quantity'] ) : 1;
1083
1084
                    $item       = $item_id ? new WPInv_Item( $item_id ) : NULL;
1085
                    $summary    = '';
1086
	                $item_name    = '';
1087
                    $cols       = 3;
1088
                    if ( !empty($item) ) {
1089
                        $item_name  = $item->get_name();
1090
                        $summary    = $item->get_summary();
1091
                    }
1092
                    $item_name  = !empty($cart_item['name']) ? $cart_item['name'] : $item_name;
1093
1094
                    $summary = apply_filters( 'wpinv_print_invoice_line_item_summary', $summary, $cart_item, $item, $invoice );
1095
1096
                    $item_tax       = '';
1097
                    $tax_rate       = '';
1098
                    if ( $use_taxes && $cart_item['tax'] > 0 && $cart_item['subtotal'] > 0 ) {
1099
                        $item_tax = wpinv_price( wpinv_format_amount( $cart_item['tax'] ), $invoice->get_currency() );
1100
                        $tax_rate = !empty( $cart_item['vat_rate'] ) ? $cart_item['vat_rate'] : ( $cart_item['tax'] / $cart_item['subtotal'] ) * 100;
1101
                        $tax_rate = $tax_rate > 0 ? (float)wpinv_round_amount( $tax_rate, 4 ) : '';
1102
                        $tax_rate = $tax_rate != '' ? ' <small class="tax-rate">(' . $tax_rate . '%)</small>' : '';
1103
                    }
1104
1105
                    $line_item_tax = $item_tax . $tax_rate;
1106
1107
                    if ( $line_item_tax === '' ) {
1108
                        $line_item_tax = 0; // Zero tax
1109
                    }
1110
1111
                    $action = apply_filters( 'wpinv_display_line_item_action', '', $cart_item, $invoice, $cols );
1112
1113
                    $line_item = '<tr class="row-' . ( ($count % 2 == 0) ? 'even' : 'odd' ) . ' wpinv-item">';
1114
                        $line_item .= '<td class="name">' . $action. esc_html__( $item_name, 'invoicing' ) . wpinv_get_item_suffix( $item );
1115
                        if ( $summary !== '' ) {
1116
                            $line_item .= '<br/><small class="meta">' . wpautop( wp_kses_post( $summary ) ) . '</small>';
1117
                        }
1118
                        $line_item .= '</td>';
1119
1120
                        $line_item .= '<td class="rate">' . esc_html__( wpinv_price( wpinv_format_amount( $item_price ), $invoice->get_currency() ) ) . '</td>';
1121
                        if ($quantities_enabled) {
1122
                            $cols++;
1123
                            $line_item .= '<td class="qty">' . $quantity . '</td>';
1124
                        }
1125
                        if ($use_taxes && !$zero_tax) {
1126
                            $cols++;
1127
                            $line_item .= '<td class="tax">' . $line_item_tax . '</td>';
1128
                        }
1129
                        $line_item .= '<td class="total">' . esc_html__( wpinv_price( wpinv_format_amount( $line_total ), $invoice->get_currency() ) ) . '</td>';
1130
                    $line_item .= '</tr>';
1131
1132
                    echo apply_filters( 'wpinv_display_line_item', $line_item, $cart_item, $invoice, $cols );
1133
1134
                    $count++;
1135
                }
1136
1137
                do_action( 'wpinv_display_before_subtotal', $invoice, $cols );
1138
                ?>
1139
                <tr class="row-sub-total row_odd">
1140
                    <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php echo apply_filters( 'wpinv_print_cart_subtotal_label', '<strong>' . __( 'Sub Total', 'invoicing' ) . ':</strong>', $invoice ); ?></td>
1141
                    <td class="total"><strong><?php _e( wpinv_subtotal( $invoice_id, true ) ) ?></strong></td>
1142
                </tr>
1143
                <?php
1144
                do_action( 'wpinv_display_after_subtotal', $invoice, $cols );
1145
                
1146
                if ( wpinv_discount( $invoice_id, false ) > 0 ) {
1147
                    do_action( 'wpinv_display_before_discount', $invoice, $cols );
1148
                    ?>
1149
                        <tr class="row-discount">
1150
                            <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php wpinv_get_discount_label( wpinv_discount_code( $invoice_id ) ); ?>:</td>
1151
                            <td class="total"><?php echo wpinv_discount( $invoice_id, true, true ); ?></td>
1152
                        </tr>
1153
                    <?php
1154
                    do_action( 'wpinv_display_after_discount', $invoice, $cols );
1155
                }
1156
1157
                if ( $use_taxes ) {
1158
                    do_action( 'wpinv_display_before_tax', $invoice, $cols );
1159
                    ?>
1160
                    <tr class="row-tax">
1161
                        <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php echo apply_filters( 'wpinv_print_cart_tax_label', '<strong>' . $tax_label . ':</strong>', $invoice ); ?></td>
1162
                        <td class="total"><?php _e( wpinv_tax( $invoice_id, true ) ) ?></td>
1163
                    </tr>
1164
                    <?php
1165
                    do_action( 'wpinv_display_after_tax', $invoice, $cols );
1166
                }
1167
1168
                do_action( 'wpinv_display_before_total', $invoice, $cols );
1169
                ?>
1170
                <tr class="table-active row-total">
1171
                    <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php echo apply_filters( 'wpinv_print_cart_total_label', '<strong>' . __( 'Total', 'invoicing' ) . ':</strong>', $invoice ); ?></td>
1172
                    <td class="total"><strong><?php _e( wpinv_payment_total( $invoice_id, true ) ) ?></strong></td>
1173
                </tr>
1174
                <?php
1175
                do_action( 'wpinv_display_after_total', $invoice, $cols );
1176
1177
                do_action( 'wpinv_display_line_end', $invoice, $cols );
1178
            }
1179
        ?>
1180
        </tbody>
1181
    </table>
1182
    <?php
1183
    echo ob_get_clean();
1184
}
1185
1186
/**
1187
 * @param WPInv_Invoice $invoice
1188
 */
1189
function wpinv_display_invoice_notes( $invoice ) {
1190
1191
    $notes = wpinv_get_invoice_notes( $invoice->ID, 'customer' );
1192
1193
    if ( empty( $notes ) ) {
1194
        return;
1195
    }
1196
1197
    echo '<div class="wpi_invoice_notes_container">';
1198
    echo '<h2>' . __( 'Invoice Notes', 'invoicing' ) .'</h2>';
1199
    echo '<ul class="wpi_invoice_notes">';
1200
1201
    foreach( $notes as $note ) {
1202
        wpinv_get_invoice_note_line_item( $note );
1203
    }
1204
1205
    echo '</ul>';
1206
    echo '</div>';
1207
}
1208
add_action( 'wpinv_invoice_print_after_line_items', 'wpinv_display_invoice_notes' );
1209
1210
function wpinv_display_invoice_totals( $invoice_id = 0 ) {
1211
    $use_taxes = wpinv_use_taxes();
1212
1213
    do_action( 'wpinv_before_display_totals_table', $invoice_id ); 
1214
    ?>
1215
    <table class="table table-sm table-bordered table-responsive">
1216
        <tbody>
1217
            <?php do_action( 'wpinv_before_display_totals' ); ?>
1218
            <tr class="row-sub-total">
1219
                <td class="rate"><strong><?php _e( 'Sub Total', 'invoicing' ); ?></strong></td>
1220
                <td class="total"><strong><?php _e( wpinv_subtotal( $invoice_id, true ) ) ?></strong></td>
1221
            </tr>
1222
            <?php do_action( 'wpinv_after_display_totals' ); ?>
1223
            <?php if ( wpinv_discount( $invoice_id, false ) > 0 ) { ?>
1224
                <tr class="row-discount">
1225
                    <td class="rate"><?php wpinv_get_discount_label( wpinv_discount_code( $invoice_id ) ); ?></td>
1226
                    <td class="total"><?php echo wpinv_discount( $invoice_id, true, true ); ?></td>
1227
                </tr>
1228
            <?php do_action( 'wpinv_after_display_discount' ); ?>
1229
            <?php } ?>
1230
            <?php if ( $use_taxes ) { ?>
1231
            <tr class="row-tax">
1232
                <td class="rate"><?php _e( 'Tax', 'invoicing' ); ?></td>
1233
                <td class="total"><?php _e( wpinv_tax( $invoice_id, true ) ) ?></td>
1234
            </tr>
1235
            <?php do_action( 'wpinv_after_display_tax' ); ?>
1236
            <?php } ?>
1237
            <?php if ( $fees = wpinv_get_fees( $invoice_id ) ) { ?>
1238
                <?php foreach ( $fees as $fee ) { ?>
1239
                    <tr class="row-fee">
1240
                        <td class="rate"><?php echo $fee['label']; ?></td>
1241
                        <td class="total"><?php echo $fee['amount_display']; ?></td>
1242
                    </tr>
1243
                <?php } ?>
1244
            <?php } ?>
1245
            <tr class="table-active row-total">
1246
                <td class="rate"><strong><?php _e( 'Total', 'invoicing' ) ?></strong></td>
1247
                <td class="total"><strong><?php _e( wpinv_payment_total( $invoice_id, true ) ) ?></strong></td>
1248
            </tr>
1249
            <?php do_action( 'wpinv_after_totals' ); ?>
1250
        </tbody>
1251
1252
    </table>
1253
1254
    <?php do_action( 'wpinv_after_totals_table' );
1255
}
1256
1257
function wpinv_display_payments_info( $invoice_id = 0, $echo = true ) {
1258
    $invoice = wpinv_get_invoice( $invoice_id );
1259
1260
    ob_start();
1261
    do_action( 'wpinv_before_display_payments_info', $invoice_id );
1262
    if ( ( $gateway_title = $invoice->get_gateway_title() ) || $invoice->is_paid() || $invoice->is_refunded() ) {
1263
        ?>
1264
        <div class="wpi-payment-info">
1265
            <p class="wpi-payment-gateway"><?php echo wp_sprintf( __( 'Payment via %s', 'invoicing' ), $gateway_title ? $gateway_title : __( 'Manually', 'invoicing' ) ); ?></p>
1266
            <?php if ( $gateway_title ) { ?>
1267
            <p class="wpi-payment-transid"><?php echo wp_sprintf( __( 'Transaction ID: %s', 'invoicing' ), $invoice->get_transaction_id() ); ?></p>
1268
            <?php } ?>
1269
        </div>
1270
        <?php
1271
    }
1272
    do_action( 'wpinv_after_display_payments_info', $invoice_id );
1273
    $outout = ob_get_clean();
1274
1275
    if ( $echo ) {
1276
        echo $outout;
1277
    } else {
1278
        return $outout;
1279
    }
1280
}
1281
1282
function wpinv_display_style( $invoice ) {
1283
    wp_register_style( 'wpinv-single-style', WPINV_PLUGIN_URL . 'assets/css/invoice.css', array(), WPINV_VERSION );
1284
1285
    wp_print_styles( 'open-sans' );
1286
    wp_print_styles( 'wpinv-single-style' );
1287
1288
    $custom_css = wpinv_get_option('template_custom_css');
1289
    if(isset($custom_css) && !empty($custom_css)){
1290
        $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

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

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

1969
    $invoice_link = '<a href="' . esc_url( /** @scrutinizer ignore-type */ $invoice->get_view_url() ) . '">' . $invoice->get_number() . '</a>';
Loading history...
1970
1971
    return apply_filters( 'wpinv_get_invoice_link', $invoice_link, $invoice );
1972
}
1973
1974
function wpinv_invoice_subscription_details( $invoice ) {
1975
    if ( !empty( $invoice ) && $invoice->is_recurring() && ! wpinv_is_subscription_payment( $invoice ) ) {
1976
        $subscription = wpinv_get_subscription( $invoice, true );
1977
1978
        if ( empty( $subscription ) ) {
1979
            return;
1980
        }
1981
1982
        $frequency = WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency($subscription->period, $subscription->frequency);
0 ignored issues
show
Bug Best Practice introduced by
The property frequency does not exist on WPInv_Subscription. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
It seems like $subscription->frequency can also be of type WP_Error; however, parameter $frequency_count of WPInv_Subscriptions::wpi...ubscription_frequency() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

1982
        $frequency = WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency($subscription->period, /** @scrutinizer ignore-type */ $subscription->frequency);
Loading history...
1983
        $billing = wpinv_price(wpinv_format_amount($subscription->recurring_amount), wpinv_get_invoice_currency_code($subscription->parent_payment_id)) . ' / ' . $frequency;
1984
        $initial = wpinv_price(wpinv_format_amount($subscription->initial_amount), wpinv_get_invoice_currency_code($subscription->parent_payment_id));
1985
1986
        $payments = $subscription->get_child_payments();
1987
        ?>
1988
        <div class="wpinv-subscriptions-details">
1989
            <h3 class="wpinv-subscriptions-t"><?php echo apply_filters( 'wpinv_subscription_details_title', __( 'Subscription Details', 'invoicing' ) ); ?></h3>
1990
            <table class="table">
1991
                <thead>
1992
                    <tr>
1993
                        <th><?php _e( 'Billing Cycle', 'invoicing' ) ;?></th>
1994
                        <th><?php _e( 'Start Date', 'invoicing' ) ;?></th>
1995
                        <th><?php _e( 'Expiration Date', 'invoicing' ) ;?></th>
1996
                        <th class="text-center"><?php _e( 'Times Billed', 'invoicing' ) ;?></th>
1997
                        <th class="text-center"><?php _e( 'Status', 'invoicing' ) ;?></th>
1998
                    </tr>
1999
                </thead>
2000
                <tbody>
2001
                    <tr>
2002
                        <td><?php printf(_x('%s then %s', 'Initial subscription amount then billing cycle and amount', 'invoicing'), $initial, $billing); ?></td>
2003
                        <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

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

2082
add_action( 'wpinv_invoice_print_middle', 'wpinv_invoice_print_description', /** @scrutinizer ignore-type */ 10.1, 1 );
Loading history...
2083
2084
function wpinv_invoice_print_payment_info( $invoice ) {
2085
    if ( empty( $invoice ) ) {
2086
        return NULL;
2087
    }
2088
2089
    if ( $payments_info = wpinv_display_payments_info( $invoice->ID, false ) ) {
2090
        ?>
2091
        <div class="row wpinv-payments">
2092
            <div class="col-sm-12">
2093
                <?php echo $payments_info; ?>
2094
            </div>
2095
        </div>
2096
        <?php 
2097
    }
2098
}
2099
// add_action( 'wpinv_invoice_print_after_line_items', 'wpinv_invoice_print_payment_info', 10, 1 );
2100
2101
function wpinv_get_invoice_note_line_item( $note, $echo = true ) {
2102
    if ( empty( $note ) ) {
2103
        return NULL;
2104
    }
2105
2106
    if ( is_int( $note ) ) {
2107
        $note = get_comment( $note );
2108
    }
2109
2110
    if ( !( is_object( $note ) && is_a( $note, 'WP_Comment' ) ) ) {
2111
        return NULL;
2112
    }
2113
2114
    $note_classes   = array( 'note' );
2115
    $note_classes[] = get_comment_meta( $note->comment_ID, '_wpi_customer_note', true ) ? 'customer-note' : '';
2116
    $note_classes[] = $note->comment_author === 'System' ? 'system-note' : '';
2117
    $note_classes   = apply_filters( 'wpinv_invoice_note_class', array_filter( $note_classes ), $note );
2118
    $note_classes   = !empty( $note_classes ) ? implode( ' ', $note_classes ) : '';
2119
2120
    ob_start();
2121
    ?>
2122
    <li rel="<?php echo absint( $note->comment_ID ) ; ?>" class="<?php echo esc_attr( $note_classes ); ?>">
2123
        <div class="note_content">
2124
            <?php echo wpautop( wptexturize( wp_kses_post( $note->comment_content ) ) ); ?>
2125
        </div>
2126
        <p class="meta">
2127
            <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

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

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

2311
    /** @scrutinizer ignore-call */ 
2312
    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...
2312
    add_filter( 'template_include', 'wpinv_template', 999, 1 );
2313
}
2314
2315
/**
2316
 * Helper function to display a payment form on the frontend.
2317
 */
2318
function getpaid_display_payment_form( $form ) {
2319
    global $invoicing;
2320
2321
    // Ensure that it is published.
2322
	if ( 'publish' != get_post_status( $form ) ) {
2323
		return aui()->alert(
2324
			array(
2325
				'type'    => 'warning',
2326
				'content' => __( 'This payment form is no longer active', 'invoicing' ),
2327
			)
2328
		);
2329
	}
2330
2331
    // Get the form.
2332
    $form = new GetPaid_Payment_Form( $form );
2333
    $html = wpinv_get_template_html( 'payment-forms/form.php', compact( 'form' ) );
2334
    return str_replace( 'sr-only', '', $html );
2335
2336
}
2337
2338
/**
2339
 * Helper function to display a item payment form on the frontend.
2340
 */
2341
function getpaid_display_item_payment_form( $items ) {
2342
    global $invoicing;
2343
2344
    foreach ( array_keys( $items ) as $id ) {
2345
	    if ( 'publish' != get_post_status( $id ) ) {
2346
		    unset( $items[ $id ] );
2347
	    }
2348
    }
2349
2350
    if ( empty( $items ) ) {
2351
		return aui()->alert(
2352
			array(
2353
				'type'    => 'warning',
2354
				'content' => __( 'No published items found', 'invoicing' ),
2355
			)
2356
		);
2357
    }
2358
2359
    $item_key = getpaid_convert_items_to_string( $items );
2360
2361
    // Get the form elements and items.
2362
    $form     = wpinv_get_default_payment_form();
2363
	$elements = $invoicing->form_elements->get_form_elements( $form );
2364
	$items    = $invoicing->form_elements->convert_normal_items( $items );
2365
2366
	ob_start();
2367
	echo "<form class='wpinv_payment_form'>";
2368
	do_action( 'wpinv_payment_form_top' );
2369
    echo "<input type='hidden' name='form_id' value='$form'/>";
2370
    echo "<input type='hidden' name='form_items' value='$item_key'/>";
2371
	wp_nonce_field( 'wpinv_payment_form', 'wpinv_payment_form' );
2372
	wp_nonce_field( 'vat_validation', '_wpi_nonce' );
2373
2374
	foreach ( $elements as $element ) {
2375
		do_action( 'wpinv_frontend_render_payment_form_element', $element, $items, $form );
2376
		do_action( "wpinv_frontend_render_payment_form_{$element['type']}", $element, $items, $form );
2377
	}
2378
2379
	echo "<div class='wpinv_payment_form_errors alert alert-danger d-none'></div>";
2380
	do_action( 'wpinv_payment_form_bottom' );
2381
	echo '</form>';
2382
2383
	$content = ob_get_clean();
2384
	return str_replace( 'sr-only', '', $content );
2385
}
2386
2387
/**
2388
 * Helper function to display an invoice payment form on the frontend.
2389
 */
2390
function getpaid_display_invoice_payment_form( $invoice_id ) {
2391
    global $invoicing;
2392
2393
    $invoice = wpinv_get_invoice( $invoice_id );
2394
2395
    if ( empty( $invoice ) ) {
2396
		return aui()->alert(
2397
			array(
2398
				'type'    => 'warning',
2399
				'content' => __( 'Invoice not found', 'invoicing' ),
2400
			)
2401
		);
2402
    }
2403
2404
    if ( $invoice->is_paid() ) {
2405
		return aui()->alert(
2406
			array(
2407
				'type'    => 'warning',
2408
				'content' => __( 'Invoice has already been paid', 'invoicing' ),
2409
			)
2410
		);
2411
    }
2412
2413
    // Get the form elements and items.
2414
    $form     = wpinv_get_default_payment_form();
2415
	$elements = $invoicing->form_elements->get_form_elements( $form );
2416
	$items    = $invoicing->form_elements->convert_checkout_items( $invoice->cart_details, $invoice );
2417
2418
	ob_start();
2419
	echo "<form class='wpinv_payment_form'>";
2420
	do_action( 'wpinv_payment_form_top' );
2421
    echo "<input type='hidden' name='form_id' value='$form'/>";
2422
    echo "<input type='hidden' name='invoice_id' value='$invoice_id'/>";
2423
	wp_nonce_field( 'wpinv_payment_form', 'wpinv_payment_form' );
2424
	wp_nonce_field( 'vat_validation', '_wpi_nonce' );
2425
2426
	foreach ( $elements as $element ) {
2427
		do_action( 'wpinv_frontend_render_payment_form_element', $element, $items, $form );
2428
		do_action( "wpinv_frontend_render_payment_form_{$element['type']}", $element, $items, $form );
2429
	}
2430
2431
	echo "<div class='wpinv_payment_form_errors alert alert-danger d-none'></div>";
2432
	do_action( 'wpinv_payment_form_bottom' );
2433
	echo '</form>';
2434
2435
	$content = ob_get_clean();
2436
	return str_replace( 'sr-only', '', $content );
2437
}
2438
2439
/**
2440
 * Helper function to convert item string to array.
2441
 */
2442
function getpaid_convert_items_to_array( $items ) {
2443
    $items    = array_filter( array_map( 'trim', explode( ',', $items ) ) );
2444
    $prepared = array();
2445
2446
    foreach ( $items as $item ) {
2447
        $data = array_map( 'trim', explode( '|', $item ) );
2448
2449
        if ( empty( $data[0] ) || ! is_numeric( $data[0] ) ) {
2450
            continue;
2451
        }
2452
2453
        $quantity = 1;
2454
        if ( isset( $data[1] ) && is_numeric( $data[1] ) ) {
2455
            $quantity = $data[1];
2456
        }
2457
2458
        $prepared[ $data[0] ] = $quantity;
2459
2460
    }
2461
2462
    return $prepared;
2463
}
2464
2465
/**
2466
 * Helper function to convert item array to string.
2467
 */
2468
function getpaid_convert_items_to_string( $items ) {
2469
    $prepared = array();
2470
2471
    foreach ( $items as $item => $quantity ) {
2472
        $prepared[] = "$item|$quantity";
2473
    }
2474
    return implode( ',', $prepared );
2475
}
2476
2477
/**
2478
 * Helper function to display a payment item.
2479
 * 
2480
 * Provide a label and one of $form, $items or $invoice.
2481
 */
2482
function getpaid_get_payment_button( $label, $form = null, $items = null, $invoice = null ) {
2483
    $label = sanitize_text_field( $label );
2484
    $nonce = wp_create_nonce('getpaid_ajax_form');
2485
2486
    if ( ! empty( $form ) ) {
2487
        $form  = esc_attr( $form );
2488
        return "<button class='btn btn-primary getpaid-payment-button' type='button' data-nonce='$nonce' data-form='$form'>$label</button>"; 
2489
    }
2490
	
2491
	if ( ! empty( $items ) ) {
2492
        $items  = esc_attr( $items );
2493
        return "<button class='btn btn-primary getpaid-payment-button' type='button' data-nonce='$nonce' data-item='$items'>$label</button>"; 
2494
    }
2495
    
2496
    if ( ! empty( $invoice ) ) {
2497
        $invoice  = esc_attr( $invoice );
2498
        return "<button class='btn btn-primary getpaid-payment-button' type='button' data-nonce='$nonce' data-invoice='$invoice'>$label</button>"; 
2499
    }
2500
2501
}
2502
2503
/**
2504
 * Display invoice description before line items.
2505
 *
2506
 * @param WPInv_Invoice $invoice
2507
 */
2508
function getpaid_the_invoice_description( $invoice ) {
2509
    if ( empty( $invoice->description ) ) {
2510
        return;
2511
    }
2512
2513
    $description = wp_kses_post( $invoice->description );
2514
    echo "<div style='color: #616161; font-size: 90%; margin-bottom: 20px;'><em>$description</em></div>";
2515
}
2516
add_action( 'wpinv_invoice_print_before_line_items', 'getpaid_the_invoice_description' );
2517
2518
/**
2519
 * Render element on a form.
2520
 *
2521
 * @param array $element
2522
 * @param GetPaid_Payment_Form $form
2523
 */
2524
function getpaid_payment_form_element( $element, $form ) {
2525
2526
    // Set up the args.
2527
    $element_type    = trim( $element['type'] );
2528
    $element['form'] = $form;
2529
    extract( $element );
2530
2531
    // Try to locate the appropriate template.
2532
    $located = wpinv_locate_template( "payment-forms/elements/$element_type.php" );
2533
    
2534
    // Abort if this is not our element.
2535
    if ( empty( $located ) || ! file_exists( $located ) ) {
2536
        return;
2537
    }
2538
2539
    // Generate the class and id of the element.
2540
    $wrapper_class = 'getpaid-payment-form-element-' . trim( esc_attr( $element_type ) );
2541
    $id            = isset( $id ) ? $id : uniqid( 'gp' );
2542
2543
    // Echo the opening wrapper.
2544
    echo "<div class='getpaid-payment-form-element $wrapper_class'>";
2545
2546
    // Fires before displaying a given element type's content.
2547
    do_action( "getpaid_before_payment_form_{$element_type}_element", $element, $form );
2548
2549
    // Include the template for the element.
2550
    include $located;
2551
2552
    // Fires after displaying a given element type's content.
2553
    do_action( "getpaid_payment_form_{$element_type}_element", $element, $form );
2554
2555
    // Echo the closing wrapper.
2556
    echo '</div>';
2557
}
2558
add_action( 'getpaid_payment_form_element', 'getpaid_payment_form_element', 10, 2 );
2559