Passed
Push — master ( 6929e7...c24352 )
by Brian
10:18 queued 05:36
created

wpinv_ip_geolocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
c 0
b 0
f 0
dl 0
loc 1
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Template functions.
4
 *
5
 */
6
7
defined( 'ABSPATH' ) || exit;
8
9
/**
10
 * Displays an invoice.
11
 * 
12
 * @param WPInv_Invoice $invoice.
13
 */
14
function getpaid_invoice( $invoice ) {
15
    if ( ! empty( $invoice ) ) {
16
        wpinv_get_template( 'invoice/invoice.php', compact( 'invoice' ) );
17
    }
18
}
19
add_action( 'getpaid_invoice', 'getpaid_invoice', 10 );
20
21
/**
22
 * Displays the invoice footer.
23
 */
24
function getpaid_invoice_footer( $invoice ) {
25
    if ( ! empty( $invoice ) ) {
26
        wpinv_get_template( 'invoice/footer.php', compact( 'invoice' ) );
27
    }
28
}
29
add_action( 'getpaid_invoice_footer', 'getpaid_invoice_footer', 10 );
30
31
/**
32
 * Displays the invoice top bar.
33
 */
34
function getpaid_invoice_header( $invoice ) {
35
    if ( ! empty( $invoice ) ) {
36
        wpinv_get_template( 'invoice/header.php', compact( 'invoice' ) );
37
    }
38
}
39
add_action( 'getpaid_invoice_header', 'getpaid_invoice_header', 10 );
40
41
/**
42
 * Displays actions on the left side of the header.
43
 */
44
function getpaid_invoice_header_left_actions( $invoice ) {
45
    if ( ! empty( $invoice ) ) {
46
        wpinv_get_template( 'invoice/header-left-actions.php', compact( 'invoice' ) );
47
    }
48
}
49
add_action( 'getpaid_invoice_header_left', 'getpaid_invoice_header_left_actions', 10 );
50
51
/**
52
 * Displays actions on the right side of the invoice top bar.
53
 */
54
function getpaid_invoice_header_right_actions( $invoice ) {
55
    if ( ! empty( $invoice ) ) {
56
        wpinv_get_template( 'invoice/header-right-actions.php', compact( 'invoice' ) );
57
    }
58
}
59
add_action( 'getpaid_invoice_header_right', 'getpaid_invoice_header_right_actions', 10 );
60
61
/**
62
 * Displays the invoice title, watermark, logo etc.
63
 */
64
function getpaid_invoice_details_top( $invoice ) {
65
    if ( ! empty( $invoice ) ) {
66
        wpinv_get_template( 'invoice/details-top.php', compact( 'invoice' ) );
67
    }
68
}
69
add_action( 'getpaid_invoice_details', 'getpaid_invoice_details_top', 10 );
70
71
/**
72
 * Displays the company logo.
73
 */
74
function getpaid_invoice_logo( $invoice ) {
75
    if ( ! empty( $invoice ) ) {
76
        wpinv_get_template( 'invoice/invoice-logo.php', compact( 'invoice' ) );
77
    }
78
}
79
add_action( 'getpaid_invoice_details_top_left', 'getpaid_invoice_logo' );
80
81
/**
82
 * Displays the type of invoice.
83
 */
84
function getpaid_invoice_type( $invoice ) {
85
    if ( ! empty( $invoice ) ) {
86
        wpinv_get_template( 'invoice/invoice-type.php', compact( 'invoice' ) );
87
    }
88
}
89
add_action( 'getpaid_invoice_details_top_right', 'getpaid_invoice_type' );
90
91
/**
92
 * Displays the invoice details.
93
 */
94
function getpaid_invoice_details_main( $invoice ) {
95
    if ( ! empty( $invoice ) ) {
96
        wpinv_get_template( 'invoice/details.php', compact( 'invoice' ) );
97
    }
98
}
99
add_action( 'getpaid_invoice_details', 'getpaid_invoice_details_main', 50 );
100
101
/**
102
 * Returns a path to the templates directory.
103
 * 
104
 * @return string
105
 */
106
function wpinv_get_templates_dir() {
107
    return WPINV_PLUGIN_DIR . 'templates';
108
}
109
110
/**
111
 * Returns a url to the templates directory.
112
 * 
113
 * @return string
114
 */
115
function wpinv_get_templates_url() {
116
    return WPINV_PLUGIN_URL . 'templates';
117
}
118
119
/**
120
 * Displays a template.
121
 * 
122
 * First checks if there is a template overide, if not it loads the default template.
123
 * 
124
 * @param string $template_name e.g payment-forms/cart.php The template to locate.
125
 * @param string $template_path The templates directory relative to the theme's root dir. Defaults to 'invoicing'.
126
 * @param string $default_path The root path to the default template. Defaults to invoicing/templates
127
 */
128
function wpinv_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
129
130
    // Make variables available to the template.
131
    if ( ! empty( $args ) && is_array( $args ) ) {
132
		extract( $args );
133
	}
134
135
    // Locate the template.
136
	$located = wpinv_locate_template( $template_name, $template_path, $default_path );
137
138
    // Abort if the file does not exist.
139
	if ( ! file_exists( $located ) ) {
140
        _doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $located ), '2.1' );
141
		return;
142
	}
143
144
    // Fires before loading a template.
145
	do_action( 'wpinv_before_template_part', $template_name, $template_path, $located, $args );
146
147
    // Load the template.
148
	include( $located );
149
150
    // Fires after loading a template.
151
	do_action( 'wpinv_after_template_part', $template_name, $template_path, $located, $args );
152
}
153
154
/**
155
 * Retrieves a given template's html code.
156
 * 
157
 * First checks if there is a template overide, if not it loads the default template.
158
 * 
159
 * @param string $template_name e.g payment-forms/cart.php The template to locate.
160
 * @param string $template_path The templates directory relative to the theme's root dir. Defaults to 'invoicing'.
161
 * @param string $default_path The root path to the default template. Defaults to invoicing/templates
162
 */
163
function wpinv_get_template_html( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
164
	ob_start();
165
	wpinv_get_template( $template_name, $args, $template_path, $default_path );
166
	return ob_get_clean();
167
}
168
169
/**
170
 * Returns the default path from where to look for template overides.
171
 * 
172
 * @return string
173
 */
174
function wpinv_template_path() {
175
    return apply_filters( 'wpinv_template_path', wpinv_get_theme_template_dir_name() );
176
}
177
178
/**
179
 * Returns the directory containing the template overides.
180
 * 
181
 * @return string
182
 */
183
function wpinv_get_theme_template_dir_name() {
184
	return trailingslashit( apply_filters( 'wpinv_templates_dir', 'invoicing' ) );
185
}
186
187
/**
188
 * Locates a template path.
189
 * 
190
 * First checks if there is a template overide, if not it loads the default template.
191
 * 
192
 * @param string $template_name e.g payment-forms/cart.php The template to locate.
193
 * @param string $template_path The template path relative to the theme's root dir. Defaults to 'invoicing'.
194
 * @param string $default_path The root path to the default template. Defaults to invoicing/templates
195
 */
196
function wpinv_locate_template( $template_name, $template_path = '', $default_path = '' ) {
197
198
    // Load the defaults for the template path and default path.
199
    $template_path = empty( $template_path ) ? wpinv_template_path() : $template_path;
200
    $default_path  = empty( $default_path ) ? WPINV_PLUGIN_DIR . 'templates/' : $default_path;
201
202
    // Check if the template was overidden.
203
    $template = locate_template(
204
        array( trailingslashit( $template_path ) . $template_name )
205
    );
206
207
    // Maybe replace it with a default path.
208
    if ( empty( $template ) && ! empty( $default_path ) ) {
209
        $template = trailingslashit( $default_path ) . $template_name;
210
    }
211
212
    // Return what we found.
213
    return apply_filters( 'wpinv_locate_template', $template, $template_name, $template_path, $default_path );
214
}
215
216
function wpinv_get_template_part( $slug, $name = null, $load = true ) {
217
	do_action( 'get_template_part_' . $slug, $slug, $name );
218
219
	// Setup possible parts
220
	$templates = array();
221
	if ( isset( $name ) )
222
		$templates[] = $slug . '-' . $name . '.php';
223
	$templates[] = $slug . '.php';
224
225
	// Allow template parts to be filtered
226
	$templates = apply_filters( 'wpinv_get_template_part', $templates, $slug, $name );
227
228
	// Return the part that is found
229
	return wpinv_locate_tmpl( $templates, $load, false );
230
}
231
232
function wpinv_locate_tmpl( $template_names, $load = false, $require_once = true ) {
233
	// No file found yet
234
	$located = false;
235
236
	// Try to find a template file
237
	foreach ( (array)$template_names as $template_name ) {
238
239
		// Continue if template is empty
240
		if ( empty( $template_name ) )
241
			continue;
242
243
		// Trim off any slashes from the template name
244
		$template_name = ltrim( $template_name, '/' );
245
246
		// try locating this template file by looping through the template paths
247
		foreach( wpinv_get_theme_template_paths() as $template_path ) {
248
249
			if( file_exists( $template_path . $template_name ) ) {
250
				$located = $template_path . $template_name;
251
				break;
252
			}
253
		}
254
255
		if( !empty( $located ) ) {
256
			break;
257
		}
258
	}
259
260
	if ( ( true == $load ) && ! empty( $located ) )
261
		load_template( $located, $require_once );
262
263
	return $located;
264
}
265
266
function wpinv_get_theme_template_paths() {
267
	$template_dir = wpinv_get_theme_template_dir_name();
268
269
	$file_paths = array(
270
		1 => trailingslashit( get_stylesheet_directory() ) . $template_dir,
271
		10 => trailingslashit( get_template_directory() ) . $template_dir,
272
		100 => wpinv_get_templates_dir()
273
	);
274
275
	$file_paths = apply_filters( 'wpinv_template_paths', $file_paths );
276
277
	// sort the file paths based on priority
278
	ksort( $file_paths, SORT_NUMERIC );
279
280
	return array_map( 'trailingslashit', $file_paths );
281
}
282
283
function wpinv_checkout_meta_tags() {
284
285
	$pages   = array();
286
	$pages[] = wpinv_get_option( 'success_page' );
287
	$pages[] = wpinv_get_option( 'failure_page' );
288
	$pages[] = wpinv_get_option( 'invoice_history_page' );
289
	$pages[] = wpinv_get_option( 'invoice_subscription_page' );
290
291
	if( !wpinv_is_checkout() && !is_page( $pages ) ) {
292
		return;
293
	}
294
295
	echo '<meta name="robots" content="noindex,nofollow" />' . "\n";
296
}
297
add_action( 'wp_head', 'wpinv_checkout_meta_tags' );
298
299
function wpinv_add_body_classes( $class ) {
300
	$classes = (array)$class;
301
302
	if( wpinv_is_checkout() ) {
303
		$classes[] = 'wpinv-checkout';
304
		$classes[] = 'wpinv-page';
305
	}
306
307
	if( wpinv_is_success_page() ) {
308
		$classes[] = 'wpinv-success';
309
		$classes[] = 'wpinv-page';
310
	}
311
312
	if( wpinv_is_failed_transaction_page() ) {
313
		$classes[] = 'wpinv-failed-transaction';
314
		$classes[] = 'wpinv-page';
315
	}
316
317
	if( wpinv_is_invoice_history_page() ) {
318
		$classes[] = 'wpinv-history';
319
		$classes[] = 'wpinv-page';
320
	}
321
322
	if( wpinv_is_subscriptions_history_page() ) {
323
		$classes[] = 'wpinv-subscription';
324
		$classes[] = 'wpinv-page';
325
	}
326
327
	if( wpinv_is_test_mode() ) {
328
		$classes[] = 'wpinv-test-mode';
329
		$classes[] = 'wpinv-page';
330
	}
331
332
	return array_unique( $classes );
333
}
334
add_filter( 'body_class', 'wpinv_add_body_classes' );
335
336
function wpinv_html_dropdown( $name = 'wpinv_discounts', $selected = 0, $status = '' ) {
337
    $args = array( 'nopaging' => true );
338
339
    if ( ! empty( $status ) )
340
        $args['post_status'] = $status;
341
342
    $discounts = wpinv_get_discounts( $args );
343
    $options   = array();
344
345
    if ( $discounts ) {
346
        foreach ( $discounts as $discount ) {
347
            $options[ absint( $discount->ID ) ] = esc_html( get_the_title( $discount->ID ) );
348
        }
349
    } else {
350
        $options[0] = __( 'No discounts found', 'invoicing' );
351
    }
352
353
    $output = wpinv_html_select( array(
354
        'name'             => $name,
355
        'selected'         => $selected,
356
        'options'          => $options,
357
        'show_option_all'  => false,
358
        'show_option_none' => false,
359
    ) );
360
361
    return $output;
362
}
363
364
function wpinv_html_year_dropdown( $name = 'year', $selected = 0, $years_before = 5, $years_after = 0 ) {
365
    $current     = date( 'Y' );
366
    $start_year  = $current - absint( $years_before );
367
    $end_year    = $current + absint( $years_after );
368
    $selected    = empty( $selected ) ? date( 'Y' ) : $selected;
369
    $options     = array();
370
371
    while ( $start_year <= $end_year ) {
372
        $options[ absint( $start_year ) ] = $start_year;
373
        $start_year++;
374
    }
375
376
    $output = wpinv_html_select( array(
377
        'name'             => $name,
378
        'selected'         => $selected,
379
        'options'          => $options,
380
        'show_option_all'  => false,
381
        'show_option_none' => false
382
    ) );
383
384
    return $output;
385
}
386
387
function wpinv_html_month_dropdown( $name = 'month', $selected = 0 ) {
388
389
    $options = array(
390
        '1'  => __( 'January', 'invoicing' ),
391
        '2'  => __( 'February', 'invoicing' ),
392
        '3'  => __( 'March', 'invoicing' ),
393
        '4'  => __( 'April', 'invoicing' ),
394
        '5'  => __( 'May', 'invoicing' ),
395
        '6'  => __( 'June', 'invoicing' ),
396
        '7'  => __( 'July', 'invoicing' ),
397
        '8'  => __( 'August', 'invoicing' ),
398
        '9'  => __( 'September', 'invoicing' ),
399
        '10' => __( 'October', 'invoicing' ),
400
        '11' => __( 'November', 'invoicing' ),
401
        '12' => __( 'December', 'invoicing' ),
402
    );
403
404
    // If no month is selected, default to the current month
405
    $selected = empty( $selected ) ? date( 'n' ) : $selected;
406
407
    $output = wpinv_html_select( array(
408
        'name'             => $name,
409
        'selected'         => $selected,
410
        'options'          => $options,
411
        'show_option_all'  => false,
412
        'show_option_none' => false
413
    ) );
414
415
    return $output;
416
}
417
418
function wpinv_html_select( $args = array() ) {
419
    $defaults = array(
420
        'options'          => array(),
421
        'name'             => null,
422
        'class'            => '',
423
        'id'               => '',
424
        'selected'         => 0,
425
        'placeholder'      => null,
426
        'multiple'         => false,
427
        'show_option_all'  => _x( 'All', 'all dropdown items', 'invoicing' ),
428
        'show_option_none' => _x( 'None', 'no dropdown items', 'invoicing' ),
429
        'data'             => array(),
430
        'onchange'         => null,
431
        'required'         => false,
432
        'disabled'         => false,
433
        'readonly'         => false,
434
    );
435
436
    $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.

3 paths for user data to reach this point

  1. Path: Read from $_GET, and $_GET['vat_class'] is assigned to $vat_class in includes/admin/class-getpaid-post-types-admin.php on line 552
  1. Read from $_GET, and $_GET['vat_class'] is assigned to $vat_class
    in includes/admin/class-getpaid-post-types-admin.php on line 552
  2. wpinv_html_select() is called
    in includes/admin/class-getpaid-post-types-admin.php on line 556
  3. Enters via parameter $args
    in includes/wpinv-template-functions.php on line 418
  2. Path: Read from $_GET, and $_GET['vat_rule'] is assigned to $vat_rule in includes/admin/class-getpaid-post-types-admin.php on line 522
  1. Read from $_GET, and $_GET['vat_rule'] is assigned to $vat_rule
    in includes/admin/class-getpaid-post-types-admin.php on line 522
  2. wpinv_html_select() is called
    in includes/admin/class-getpaid-post-types-admin.php on line 527
  3. Enters via parameter $args
    in includes/wpinv-template-functions.php on line 418
  3. Path: Read from $_GET, and $_GET['type'] is assigned to $type in includes/admin/class-getpaid-post-types-admin.php on line 577
  1. Read from $_GET, and $_GET['type'] is assigned to $type
    in includes/admin/class-getpaid-post-types-admin.php on line 577
  2. wpinv_html_select() is called
    in includes/admin/class-getpaid-post-types-admin.php on line 581
  3. Enters via parameter $args
    in includes/wpinv-template-functions.php on line 418

Used in variable context

  1. wp_parse_args() is called
    in includes/wpinv-template-functions.php on line 423
  2. Enters via parameter $args
    in wordpress/wp-includes/functions.php on line 4413
  3. wp_parse_str() is called
    in wordpress/wp-includes/functions.php on line 4419
  4. Enters via parameter $string
    in wordpress/wp-includes/formatting.php on line 4938
  5. parse_str() is called
    in wordpress/wp-includes/formatting.php on line 4939

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...
437
438
    $data_elements = '';
439
    foreach ( $args['data'] as $key => $value ) {
440
        $data_elements .= ' data-' . esc_attr( $key ) . '="' . esc_attr( $value ) . '"';
441
    }
442
443
    if( $args['multiple'] ) {
444
        $multiple = ' MULTIPLE';
445
    } else {
446
        $multiple = '';
447
    }
448
449
    if( $args['placeholder'] ) {
450
        $placeholder = $args['placeholder'];
451
    } else {
452
        $placeholder = '';
453
    }
454
    
455
    $options = '';
456
    if( !empty( $args['onchange'] ) ) {
457
        $options .= ' onchange="' . esc_attr( $args['onchange'] ) . '"';
458
    }
459
    
460
    if( !empty( $args['required'] ) ) {
461
        $options .= ' required="required"';
462
    }
463
    
464
    if( !empty( $args['disabled'] ) ) {
465
        $options .= ' disabled';
466
    }
467
    
468
    if( !empty( $args['readonly'] ) ) {
469
        $options .= ' readonly';
470
    }
471
472
    $class  = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
473
    $output = '<select name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( $args['id'] ) . '" class="wpinv-select ' . $class . '"' . $multiple . ' data-placeholder="' . $placeholder . '" ' . trim( $options ) . $data_elements . '>';
474
475
    if ( $args['show_option_all'] ) {
476
        if( $args['multiple'] ) {
477
            $selected = selected( true, in_array( 0, $args['selected'] ), false );
478
        } else {
479
            $selected = selected( $args['selected'], 0, false );
480
        }
481
        $output .= '<option value="all"' . $selected . '>' . esc_html( $args['show_option_all'] ) . '</option>';
482
    }
483
484
    if ( !empty( $args['options'] ) ) {
485
486
        if ( $args['show_option_none'] ) {
487
            if( $args['multiple'] ) {
488
                $selected = selected( true, in_array( "", $args['selected'] ), false );
489
            } else {
490
                $selected = selected( $args['selected'] === "", true, false );
491
            }
492
            $output .= '<option value=""' . $selected . '>' . esc_html( $args['show_option_none'] ) . '</option>';
493
        }
494
495
        foreach( $args['options'] as $key => $option ) {
496
497
            if( $args['multiple'] && is_array( $args['selected'] ) ) {
498
                $selected = selected( true, (bool)in_array( $key, $args['selected'] ), false );
499
            } else {
500
                $selected = selected( $args['selected'], $key, false );
501
            }
502
503
            $output .= '<option value="' . esc_attr( $key ) . '"' . $selected . '>' . esc_html( $option ) . '</option>';
504
        }
505
    }
506
507
    $output .= '</select>';
508
509
    return $output;
510
}
511
512
function wpinv_item_dropdown( $args = array() ) {
513
    $defaults = array(
514
        'name'              => 'wpi_item',
515
        'id'                => 'wpi_item',
516
        'class'             => '',
517
        'multiple'          => false,
518
        'selected'          => 0,
519
        'number'            => 100,
520
        'placeholder'       => __( 'Choose a item', 'invoicing' ),
521
        'data'              => array( 'search-type' => 'item' ),
522
        'show_option_all'   => false,
523
        'show_option_none'  => false,
524
        'show_recurring'    => false,
525
    );
526
527
    $args = wp_parse_args( $args, $defaults );
528
529
    $item_args = array(
530
        'post_type'      => 'wpi_item',
531
        'orderby'        => 'title',
532
        'order'          => 'ASC',
533
        'posts_per_page' => $args['number']
534
    );
535
536
    $item_args  = apply_filters( 'wpinv_item_dropdown_query_args', $item_args, $args, $defaults );
537
538
    $items      = get_posts( $item_args );
539
    $options    = array();
540
    if ( $items ) {
541
        foreach ( $items as $item ) {
542
            $title = esc_html( $item->post_title );
543
            
544
            if ( !empty( $args['show_recurring'] ) ) {
545
                $title .= wpinv_get_item_suffix( $item->ID, false );
546
            }
547
            
548
            $options[ absint( $item->ID ) ] = $title;
549
        }
550
    }
551
552
    // This ensures that any selected items are included in the drop down
553
    if( is_array( $args['selected'] ) ) {
554
        foreach( $args['selected'] as $item ) {
555
            if( ! in_array( $item, $options ) ) {
556
                $title = get_the_title( $item );
557
                if ( !empty( $args['show_recurring'] ) ) {
558
                    $title .= wpinv_get_item_suffix( $item, false );
559
                }
560
                $options[$item] = $title;
561
            }
562
        }
563
    } elseif ( is_numeric( $args['selected'] ) && $args['selected'] !== 0 ) {
564
        if ( ! in_array( $args['selected'], $options ) ) {
565
            $title = get_the_title( $args['selected'] );
566
            if ( !empty( $args['show_recurring'] ) ) {
567
                $title .= wpinv_get_item_suffix( $args['selected'], false );
568
            }
569
            $options[$args['selected']] = get_the_title( $args['selected'] );
570
        }
571
    }
572
573
    $output = wpinv_html_select( array(
574
        'name'             => $args['name'],
575
        'selected'         => $args['selected'],
576
        'id'               => $args['id'],
577
        'class'            => $args['class'],
578
        'options'          => $options,
579
        'multiple'         => $args['multiple'],
580
        'placeholder'      => $args['placeholder'],
581
        'show_option_all'  => $args['show_option_all'],
582
        'show_option_none' => $args['show_option_none'],
583
        'data'             => $args['data'],
584
    ) );
585
586
    return $output;
587
}
588
589
/**
590
 * Returns an array of published items.
591
 */
592
function wpinv_get_published_items_for_dropdown() {
593
594
    $items = get_posts(
595
        array(
596
            'post_type'      => 'wpi_item',
597
            'orderby'        => 'title',
598
            'order'          => 'ASC',
599
            'posts_per_page' => '-1'
600
        )
601
    );
602
603
    $options = array();
604
    if ( $items ) {
605
        foreach ( $items as $item ) {
606
            $options[ $item->ID ] = esc_html( $item->post_title ) . wpinv_get_item_suffix( $item->ID, false );
607
        }
608
    }
609
610
    return $options;
611
}
612
613
function wpinv_html_checkbox( $args = array() ) {
614
    $defaults = array(
615
        'name'     => null,
616
        'current'  => null,
617
        'class'    => 'wpinv-checkbox',
618
        'options'  => array(
619
            'disabled' => false,
620
            'readonly' => false
621
        )
622
    );
623
624
    $args = wp_parse_args( $args, $defaults );
625
626
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
627
    $options = '';
628
    if ( ! empty( $args['options']['disabled'] ) ) {
629
        $options .= ' disabled="disabled"';
630
    } elseif ( ! empty( $args['options']['readonly'] ) ) {
631
        $options .= ' readonly';
632
    }
633
634
    $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 ) . ' />';
635
636
    return $output;
637
}
638
639
/**
640
 * Displays a hidden field.
641
 */
642
function getpaid_hidden_field( $name, $value ) {
643
    $name  = sanitize_text_field( $name );
644
    $value = esc_attr( $value );
645
646
    echo "<input type='hidden' name='$name' value='$value' />";
647
}
648
649
function wpinv_html_text( $args = array() ) {
650
    // Backwards compatibility
651
    if ( func_num_args() > 1 ) {
652
        $args = func_get_args();
653
654
        $name  = $args[0];
655
        $value = isset( $args[1] ) ? $args[1] : '';
656
        $label = isset( $args[2] ) ? $args[2] : '';
657
        $desc  = isset( $args[3] ) ? $args[3] : '';
658
    }
659
660
    $defaults = array(
661
        'id'           => '',
662
        'name'         => isset( $name )  ? $name  : 'text',
663
        'value'        => isset( $value ) ? $value : null,
664
        'label'        => isset( $label ) ? $label : null,
665
        'desc'         => isset( $desc )  ? $desc  : null,
666
        'placeholder'  => '',
667
        'class'        => 'regular-text',
668
        'disabled'     => false,
669
        'readonly'     => false,
670
        'required'     => false,
671
        'autocomplete' => '',
672
        'data'         => false
673
    );
674
675
    $args = wp_parse_args( $args, $defaults );
676
677
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
678
    $options = '';
679
    if( $args['required'] ) {
680
        $options .= ' required="required"';
681
    }
682
    if( $args['readonly'] ) {
683
        $options .= ' readonly';
684
    }
685
    if( $args['readonly'] ) {
686
        $options .= ' readonly';
687
    }
688
689
    $data = '';
690
    if ( !empty( $args['data'] ) ) {
691
        foreach ( $args['data'] as $key => $value ) {
692
            $data .= 'data-' . wpinv_sanitize_key( $key ) . '="' . esc_attr( $value ) . '" ';
693
        }
694
    }
695
696
    $output = '<span id="wpinv-' . wpinv_sanitize_key( $args['name'] ) . '-wrap">';
697
    $output .= '<label class="wpinv-label" for="' . wpinv_sanitize_key( $args['id'] ) . '">' . esc_html( $args['label'] ) . '</label>';
698
    if ( ! empty( $args['desc'] ) ) {
699
        $output .= '<span class="wpinv-description">' . esc_html( $args['desc'] ) . '</span>';
700
    }
701
702
    $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 ) . '/>';
703
704
    $output .= '</span>';
705
706
    return $output;
707
}
708
709
function wpinv_html_date_field( $args = array() ) {
710
    if( empty( $args['class'] ) ) {
711
        $args['class'] = 'wpiDatepicker';
712
    } elseif( ! strpos( $args['class'], 'wpiDatepicker' ) ) {
713
        $args['class'] .= ' wpiDatepicker';
714
    }
715
716
    return wpinv_html_text( $args );
717
}
718
719
function wpinv_html_textarea( $args = array() ) {
720
    $defaults = array(
721
        'name'        => 'textarea',
722
        'value'       => null,
723
        'label'       => null,
724
        'desc'        => null,
725
        'class'       => 'large-text',
726
        'disabled'    => false,
727
        'placeholder' => '',
728
    );
729
730
    $args = wp_parse_args( $args, $defaults );
731
732
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
733
    $disabled = '';
734
    if( $args['disabled'] ) {
735
        $disabled = ' disabled="disabled"';
736
    }
737
738
    $output = '<span id="wpinv-' . wpinv_sanitize_key( $args['name'] ) . '-wrap">';
739
    $output .= '<label class="wpinv-label" for="' . wpinv_sanitize_key( $args['name'] ) . '">' . esc_html( $args['label'] ) . '</label>';
740
    $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>';
741
742
    if ( ! empty( $args['desc'] ) ) {
743
        $output .= '<span class="wpinv-description">' . esc_html( $args['desc'] ) . '</span>';
744
    }
745
    $output .= '</span>';
746
747
    return $output;
748
}
749
750
function wpinv_html_ajax_user_search( $args = array() ) {
751
    $defaults = array(
752
        'name'        => 'user_id',
753
        'value'       => null,
754
        'placeholder' => __( 'Enter username', 'invoicing' ),
755
        'label'       => null,
756
        'desc'        => null,
757
        'class'       => '',
758
        'disabled'    => false,
759
        'autocomplete'=> 'off',
760
        'data'        => false
761
    );
762
763
    $args = wp_parse_args( $args, $defaults );
764
765
    $args['class'] = 'wpinv-ajax-user-search ' . $args['class'];
766
767
    $output  = '<span class="wpinv_user_search_wrap">';
768
        $output .= wpinv_html_text( $args );
769
        $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>';
770
    $output .= '</span>';
771
772
    return $output;
773
}
774
775
/**
776
 * @deprecated.
777
 */
778
function wpinv_ip_geolocation() {}
779
780
/**
781
 * Use our template to display invoices.
782
 * 
783
 * @param string $template the template that is currently being used.
784
 */
785
function wpinv_template( $template ) {
786
    global $post;
787
788
    if ( ! is_admin() && ( is_single() || is_404() ) && ! empty( $post->ID ) && getpaid_is_invoice_post_type( get_post_type( $post->ID ) ) ) {
789
790
        // If the user can view this invoice, display it.
791
        if ( wpinv_user_can_view_invoice( $post->ID ) ) {
792
793
            return wpinv_get_template_part( 'wpinv-invoice-print', false, false );
794
795
        // Else display an error message.
796
        } else {
797
798
            return wpinv_get_template_part( 'wpinv-invalid-access', false, false );
799
800
        }
801
802
    }
803
804
    return $template;
805
}
806
add_filter( 'template_include', 'wpinv_template', 10, 1 );
807
808
function wpinv_get_business_address() {
809
    $business_address   = wpinv_store_address();
810
    $business_address   = !empty( $business_address ) ? wpautop( wp_kses_post( $business_address ) ) : '';
811
    
812
    $business_address = $business_address ? '<div class="address">' . $business_address . '</div>' : '';
813
    
814
    return apply_filters( 'wpinv_get_business_address', $business_address );
815
}
816
817
/**
818
 * Displays the company address.
819
 */
820
function wpinv_display_from_address() {
821
    wpinv_get_template( 'invoice/company-address.php' );
822
}
823
add_action( 'getpaid_invoice_details_left', 'wpinv_display_from_address', 10 );
824
825
function wpinv_watermark( $id = 0 ) {
826
    $output = wpinv_get_watermark( $id );
827
    return apply_filters( 'wpinv_get_watermark', $output, $id );
828
}
829
830
function wpinv_get_watermark( $id ) {
831
    if ( !$id > 0 ) {
832
        return NULL;
833
    }
834
835
    $invoice = wpinv_get_invoice( $id );
836
    
837
    if ( !empty( $invoice ) && "wpi_invoice" === $invoice->post_type ) {
0 ignored issues
show
Bug Best Practice introduced by
The property post_type does not exist on WPInv_Invoice. Since you implemented __get, consider adding a @property annotation.
Loading history...
838
        if ( $invoice->is_paid() ) {
839
            return __( 'Paid', 'invoicing' );
840
        }
841
        if ( $invoice->is_refunded() ) {
842
            return __( 'Refunded', 'invoicing' );
843
        }
844
        if ( $invoice->has_status( array( 'wpi-cancelled' ) ) ) {
845
            return __( 'Cancelled', 'invoicing' );
846
        }
847
    }
848
    
849
    return NULL;
850
}
851
852
/**
853
 * @deprecated
854
 */
855
function wpinv_display_invoice_details( $invoice ) {
856
    return getpaid_invoice_meta( $invoice );
0 ignored issues
show
Bug introduced by
Are you sure the usage of getpaid_invoice_meta($invoice) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
857
}
858
859
/**
860
 * Displays invoice meta.
861
 */
862
function getpaid_invoice_meta( $invoice ) {
863
864
    $invoice = new WPInv_Invoice( $invoice );
865
866
    // Ensure that we have an invoice.
867
    if ( 0 == $invoice->get_id() ) {
868
        return;
869
    }
870
871
    // Load the invoice meta.
872
    $meta    = array(
873
874
        'number' => array(
875
            'label' => sprintf(
876
                __( '%s Number', 'invoicing' ),
877
                ucfirst( $invoice->get_type() )
878
            ),
879
            'value' => sanitize_text_field( $invoice->get_number() ),
880
        ),
881
882
        'status' => array(
883
            'label' => sprintf(
884
                __( '%s Status', 'invoicing' ),
885
                ucfirst( $invoice->get_type() )
886
            ),
887
            'value' => sanitize_text_field( $invoice->get_status_nicename() ),
888
        ),
889
890
        'date' => array(
891
            'label' => sprintf(
892
                __( '%s Date', 'invoicing' ),
893
                ucfirst( $invoice->get_type() )
894
            ),
895
            'value' => getpaid_format_date( $invoice->get_created_date() ),
896
        ),
897
898
        'date_paid' => array(
899
            'label' => __( 'Paid On', 'invoicing' ),
900
            'value' => getpaid_format_date( $invoice->get_completed_date() ),
901
        ),
902
903
        'transaction_id' => array(
904
            'label' => __( 'Transaction ID', 'invoicing' ),
905
            'value' => sanitize_text_field( $invoice->get_transaction_id() ),
906
        ),
907
908
        'due_date'  => array(
909
            'label' => __( 'Due Date', 'invoicing' ),
910
            'value' => getpaid_format_date( $invoice->get_due_date() ),
911
        ),
912
913
        'vat_number' => array(
914
            'label' => sprintf(
915
                __( '%s Number', 'invoicing' ),
916
                getpaid_tax()->get_vat_name()
917
            ),
918
            'value' => sanitize_text_field( $invoice->get_vat_number() ),
919
        ),
920
921
    );
922
923
    // If it is not paid, remove the date of payment.
924
    if ( ! $invoice->is_paid() ) {
925
        unset( $meta[ 'date_paid' ] );
926
        unset( $meta[ 'transaction_id' ] );
927
    }
928
929
    // Only display the due date if due dates are enabled.
930
    if ( ! $invoice->needs_payment() || ! wpinv_get_option( 'overdue_active' ) ) {
931
        unset( $meta[ 'due_date' ] );
932
    }
933
934
    // Only display the vat number if taxes are enabled.
935
    if ( ! wpinv_use_taxes() ) {
936
        unset( $meta[ 'vat_number' ] );
937
    }
938
939
    if ( $invoice->is_recurring() ) {
940
941
        // Link to the parent invoice.
942
        if ( $invoice->is_renewal() ) {
943
944
            $meta[ 'parent' ] = array(
945
946
                'label' => sprintf(
947
                    __( 'Parent %s', 'invoicing' ),
948
                    ucfirst( $invoice->get_type() )
949
                ),
950
951
                'value' => wpinv_invoice_link( $invoice->get_parent_id() ),
952
953
            );
954
955
        }
956
957
        $subscription = wpinv_get_subscription( $invoice );
958
959
        if ( ! empty ( $subscription ) ) {
960
961
            // Display the renewal date.
962
            if ( $subscription->is_active() && 'cancelled' != $subscription->status ) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $subscription->is_active() targeting WPInv_Subscription::is_active() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
963
964
                $meta[ 'renewal_date' ] = array(
965
966
                    'label' => __( 'Renews On', 'invoicing' ),
967
                    'value' => getpaid_format_date( $subscription->expiration ),
968
        
969
                );
970
971
            }
972
973
            if ( $invoice->is_parent() ) {
974
975
                // Display the recurring amount.
976
                $meta[ 'recurring_total' ] = array(
977
978
                    'label' => __( 'Recurring Amount', 'invoicing' ),
979
                    'value' => wpinv_price( wpinv_format_amount( $subscription->recurring_amount ), $invoice->get_currency() ),
980
        
981
                );
982
983
            }
984
            
985
        }
986
    }
987
988
    // Add the invoice total to the meta.
989
    $meta[ 'invoice_total' ] = array(
990
991
        'label' => __( 'Total Amount', 'invoicing' ),
992
        'value' => wpinv_price( wpinv_format_amount( $invoice->get_total() ), $invoice->get_currency() ),
993
994
    );
995
996
    // Provide a way for third party plugins to filter the meta.
997
    $meta = apply_filters( 'getpaid_invoice_meta_data', $meta, $invoice );
998
999
    wpinv_get_template( 'invoice/invoice-meta.php', compact( 'invoice', 'meta' ) );
1000
1001
}
1002
add_action( 'getpaid_invoice_details_right', 'getpaid_invoice_meta', 10 );
1003
1004
/**
1005
 * Retrieves the address markup to use on Invoices.
1006
 * 
1007
 * @since 1.0.13
1008
 * @see `wpinv_get_full_address_format`
1009
 * @see `wpinv_get_invoice_address_replacements`
1010
 * @param array $billing_details customer's billing details
1011
 * @param  string $separator How to separate address lines.
1012
 * @return string
1013
 */
1014
function wpinv_get_invoice_address_markup( $billing_details, $separator = '<br/>' ) {
1015
1016
    // Retrieve the address markup...
1017
    $country= empty( $billing_details['country'] ) ? '' : $billing_details['country'];
1018
    $format = wpinv_get_full_address_format( $country );
1019
1020
    // ... and the replacements.
1021
    $replacements = wpinv_get_invoice_address_replacements( $billing_details );
1022
1023
    $formatted_address = str_ireplace( array_keys( $replacements ), $replacements, $format );
1024
    
1025
	// Remove unavailable tags.
1026
    $formatted_address = preg_replace( "/\{\{\w+\}\}/", '', $formatted_address );
1027
1028
    // Clean up white space.
1029
	$formatted_address = preg_replace( '/  +/', ' ', trim( $formatted_address ) );
1030
    $formatted_address = preg_replace( '/\n\n+/', "\n", $formatted_address );
1031
    
1032
    // Break newlines apart and remove empty lines/trim commas and white space.
1033
	$formatted_address = array_filter( array_map( 'wpinv_trim_formatted_address_line', explode( "\n", $formatted_address ) ) );
1034
1035
    // Add html breaks.
1036
	$formatted_address = implode( $separator, $formatted_address );
1037
1038
	// We're done!
1039
	return $formatted_address;
1040
    
1041
}
1042
1043
/**
1044
 * Displays the billing address.
1045
 * 
1046
 * @param WPInv_Invoice $invoice
1047
 */
1048
function wpinv_display_to_address( $invoice = 0 ) {
1049
    if ( ! empty( $invoice ) ) {
1050
        wpinv_get_template( 'invoice/billing-address.php', compact( 'invoice' ) );
1051
    }
1052
}
1053
add_action( 'getpaid_invoice_details_left', 'wpinv_display_to_address', 40 );
1054
1055
1056
/**
1057
 * Displays invoice line items.
1058
 */
1059
function wpinv_display_line_items( $invoice_id = 0 ) {
1060
1061
    // Prepare the invoice.
1062
    $invoice = new WPInv_Invoice( $invoice_id );
1063
1064
    // Abort if there is no invoice.
1065
    if ( 0 == $invoice->get_id() ) {
1066
        return;
1067
    }
1068
1069
    // Line item columns.
1070
    $columns = getpaid_invoice_item_columns( $invoice );
1071
    $columns = apply_filters( 'getpaid_invoice_line_items_table_columns', $columns, $invoice );
1072
1073
    wpinv_get_template( 'invoice/line-items.php', compact( 'invoice', 'columns' ) );
1074
}
1075
add_action( 'getpaid_invoice_line_items', 'wpinv_display_line_items', 10 );
1076
1077
/**
1078
 * Displays invoice notices on invoices.
1079
 */
1080
function wpinv_display_invoice_notice() {
1081
1082
    $label  = wpinv_get_option( 'vat_invoice_notice_label' );
1083
    $notice = wpinv_get_option( 'vat_invoice_notice' );
1084
1085
    if ( empty( $label ) && empty( $notice ) ) {
1086
        return;
1087
    }
1088
1089
    echo '<div class="mt-4 mb-4 wpinv-vat-notice">';
1090
1091
    if ( ! empty( $label ) ) {
1092
        $label = sanitize_text_field( $label );
1093
        echo "<h5>$label</h5>";
1094
    }
1095
1096
    if ( ! empty( $notice ) ) {
1097
        echo '<small class="form-text text-muted">' . wpautop( wptexturize( $notice ) ) . '</small>';
1098
    }
1099
1100
    echo '</div>';
1101
}
1102
add_action( 'getpaid_invoice_line_items', 'wpinv_display_invoice_notice', 100 );
1103
1104
/**
1105
 * @param WPInv_Invoice $invoice
1106
 */
1107
function wpinv_display_invoice_notes( $invoice ) {
1108
1109
    // Retrieve the notes.
1110
    $notes = wpinv_get_invoice_notes( $invoice->get_id(), 'customer' );
1111
1112
    // Abort if we have non.
1113
    if ( empty( $notes ) ) {
1114
        return;
1115
    }
1116
1117
    // Echo the note.
1118
    echo '<div class="getpaid-invoice-notes-wrapper border position-relative w-100 mb-4 p-0">';
1119
    echo '<h3 class="getpaid-invoice-notes-title text-dark bg-light border-bottom m-0 d-block">' . __( 'Notes', 'invoicing' ) .'</h3>';
1120
    echo '<ul class="getpaid-invoice-notes mt-4 p-0">';
1121
1122
    foreach( $notes as $note ) {
1123
        wpinv_get_invoice_note_line_item( $note );
1124
    }
1125
1126
    echo '</ul>';
1127
    echo '</div>';
1128
}
1129
add_action( 'getpaid_invoice_line_items', 'wpinv_display_invoice_notes', 60 );
1130
1131
/**
1132
 * Loads scripts on our invoice templates.
1133
 */
1134
function wpinv_display_style() {
1135
1136
    // Make sure that all scripts have been loaded.
1137
    if ( ! did_action( 'wp_enqueue_scripts' ) ) {
1138
        do_action( 'wp_enqueue_scripts' );
1139
    }
1140
1141
    // Register the invoices style.
1142
    wp_register_style( 'wpinv-single-style', WPINV_PLUGIN_URL . 'assets/css/invoice.css', array(), filemtime( WPINV_PLUGIN_DIR . 'assets/css/invoice.css' ) );
1143
1144
    // Load required styles
1145
    wp_print_styles( 'open-sans' );
1146
    wp_print_styles( 'wpinv-single-style' );
1147
    wp_print_styles( 'ayecode-ui' );
1148
1149
    // Maybe load custom css.
1150
    $custom_css = wpinv_get_option( 'template_custom_css' );
1151
1152
    if ( isset( $custom_css ) && ! empty( $custom_css ) ) {
1153
        $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

1153
        $custom_css     = wp_kses( $custom_css, /** @scrutinizer ignore-type */ array( '\'', '\"' ) );
Loading history...
1154
        $custom_css     = str_replace( '&gt;', '>', $custom_css );
1155
        echo '<style type="text/css">';
1156
        echo $custom_css;
1157
        echo '</style>';
1158
    }
1159
1160
}
1161
add_action( 'wpinv_invoice_print_head', 'wpinv_display_style' );
1162
add_action( 'wpinv_invalid_invoice_head', 'wpinv_display_style' );
1163
1164
1165
/**
1166
 * Displays the checkout page.
1167
 */
1168
function wpinv_checkout_form() {
1169
    global $wpi_checkout_id;
1170
1171
    // Retrieve the current invoice.
1172
    $invoice_id = getpaid_get_current_invoice_id();
1173
1174
    if ( empty( $invoice_id ) ) {
1175
1176
        return aui()->alert(
1177
            array(
1178
                'type'    => 'warning',
1179
                'content' => __( 'Invalid invoice', 'invoicing' ),
1180
            )
1181
        );
1182
1183
    }
1184
1185
    // Can the user view this invoice?
1186
    if ( ! wpinv_user_can_view_invoice( $invoice_id ) ) {
1187
1188
        return aui()->alert(
1189
            array(
1190
                'type'    => 'warning',
1191
                'content' => __( 'You are not allowed to view this invoice', 'invoicing' ),
1192
            )
1193
        );
1194
1195
    }
1196
1197
    // Ensure that it is not yet paid for.
1198
    $invoice = new WPInv_Invoice( $invoice_id );
1199
1200
    // Maybe mark it as viewed.
1201
    getpaid_maybe_mark_invoice_as_viewed( $invoice );
1202
1203
    if ( $invoice->is_paid() ) {
1204
1205
        return aui()->alert(
1206
            array(
1207
                'type'    => 'success',
1208
                'content' => __( 'This invoice has already been paid.', 'invoicing' ),
1209
            )
1210
        );
1211
1212
    }
1213
1214
    // Set the global invoice id.
1215
    $wpi_checkout_id = $invoice_id;
1216
1217
    // We'll display this invoice via the default form.
1218
    $form = new GetPaid_Payment_Form( wpinv_get_default_payment_form() );
1219
1220
    if ( 0 == $form->get_id() ) {
1221
1222
        return aui()->alert(
1223
            array(
1224
                'type'    => 'warning',
1225
                'content' => __( 'Error loading the payment form', 'invoicing' ),
1226
            )
1227
        );
1228
1229
    }
1230
1231
    // Set the invoice.
1232
    $form->invoice = $invoice;
1233
    $form->set_items( $invoice->get_items() );
1234
1235
    // Generate the html.
1236
    return $form->get_html();
1237
1238
}
1239
1240
function wpinv_checkout_cart( $cart_details = array(), $echo = true ) {
1241
    global $ajax_cart_details;
1242
    $ajax_cart_details = $cart_details;
1243
1244
    ob_start();
1245
    do_action( 'wpinv_before_checkout_cart' );
1246
    echo '<div id="wpinv_checkout_cart_form" method="post">';
1247
        echo '<div id="wpinv_checkout_cart_wrap">';
1248
            wpinv_get_template_part( 'wpinv-checkout-cart' );
1249
        echo '</div>';
1250
    echo '</div>';
1251
    do_action( 'wpinv_after_checkout_cart' );
1252
    $content = ob_get_clean();
1253
1254
    if ( $echo ) {
1255
        echo $content;
1256
    } else {
1257
        return $content;
1258
    }
1259
}
1260
add_action( 'wpinv_checkout_cart', 'wpinv_checkout_cart', 10 );
1261
1262
function wpinv_empty_cart_message() {
1263
	return apply_filters( 'wpinv_empty_cart_message', '<span class="wpinv_empty_cart">' . __( 'Your cart is empty.', 'invoicing' ) . '</span>' );
1264
}
1265
1266
/**
1267
 * Echoes the Empty Cart Message
1268
 *
1269
 * @since 1.0
1270
 * @return void
1271
 */
1272
function wpinv_empty_checkout_cart() {
1273
    echo aui()->alert(
1274
        array(
1275
            'type'    => 'warning',
1276
            'content' => wpinv_empty_cart_message(),
1277
        )
1278
    );
1279
}
1280
add_action( 'wpinv_cart_empty', 'wpinv_empty_checkout_cart' );
1281
1282
function wpinv_checkout_cart_columns() {
1283
    $default = 3;
1284
    if ( wpinv_item_quantities_enabled() ) {
1285
        $default++;
1286
    }
1287
1288
    if ( wpinv_use_taxes() ) {
1289
        $default++;
1290
    }
1291
1292
    return apply_filters( 'wpinv_checkout_cart_columns', $default );
1293
}
1294
1295
function wpinv_receipt_billing_address( $invoice_id = 0 ) {
1296
    $invoice = wpinv_get_invoice( $invoice_id );
1297
1298
    if ( empty( $invoice ) ) {
1299
        return NULL;
1300
    }
1301
1302
    $billing_details = $invoice->get_user_info();
1303
    $address_row = wpinv_get_invoice_address_markup( $billing_details );
1304
1305
    ob_start();
1306
    ?>
1307
    <table class="table table-bordered table-sm wpi-billing-details">
1308
        <tbody>
1309
            <tr class="wpi-receipt-name">
1310
                <th class="text-left"><?php _e( 'Name', 'invoicing' ); ?></th>
1311
                <td><?php echo esc_html( trim( $billing_details['first_name'] . ' ' . $billing_details['last_name'] ) ) ;?></td>
1312
            </tr>
1313
            <tr class="wpi-receipt-email">
1314
                <th class="text-left"><?php _e( 'Email', 'invoicing' ); ?></th>
1315
                <td><?php echo $billing_details['email'] ;?></td>
1316
            </tr>
1317
            <tr class="wpi-receipt-address">
1318
                <th class="text-left"><?php _e( 'Address', 'invoicing' ); ?></th>
1319
                <td><?php echo $address_row ;?></td>
1320
            </tr>
1321
            <?php if ( $billing_details['phone'] ) { ?>
1322
            <tr class="wpi-receipt-phone">
1323
                <th class="text-left"><?php _e( 'Phone', 'invoicing' ); ?></th>
1324
                <td><?php echo esc_html( $billing_details['phone'] ) ;?></td>
1325
            </tr>
1326
            <?php } ?>
1327
        </tbody>
1328
    </table>
1329
    <?php
1330
    $output = ob_get_clean();
1331
    
1332
    $output = apply_filters( 'wpinv_receipt_billing_address', $output, $invoice_id );
1333
1334
    echo $output;
1335
}
1336
1337
/**
1338
 * Filters the receipt page.
1339
 */
1340
function wpinv_filter_success_page_content( $content ) {
1341
1342
    // Ensure this is our page.
1343
    if ( isset( $_GET['payment-confirm'] ) && wpinv_is_success_page() ) {
1344
1345
        $gateway = sanitize_text_field( $_GET['payment-confirm'] );
1346
        return apply_filters( "wpinv_payment_confirm_$gateway", $content );
1347
1348
    }
1349
1350
    return $content;
1351
}
1352
add_filter( 'the_content', 'wpinv_filter_success_page_content', 99999 );
1353
1354
function wpinv_invoice_link( $invoice_id ) {
1355
    $invoice = wpinv_get_invoice( $invoice_id );
1356
1357
    if ( empty( $invoice ) ) {
1358
        return NULL;
1359
    }
1360
1361
    $invoice_link = '<a href="' . esc_url( $invoice->get_view_url() ) . '">' . $invoice->get_number() . '</a>';
1362
1363
    return apply_filters( 'wpinv_get_invoice_link', $invoice_link, $invoice );
1364
}
1365
1366
function wpinv_invoice_subscription_details( $invoice ) {
1367
    if ( !empty( $invoice ) && $invoice->is_recurring() && ! wpinv_is_subscription_payment( $invoice ) ) {
1368
        $subscription = wpinv_get_subscription( $invoice, true );
0 ignored issues
show
Unused Code introduced by
The call to wpinv_get_subscription() has too many arguments starting with true. ( Ignorable by Annotation )

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

1368
        $subscription = /** @scrutinizer ignore-call */ wpinv_get_subscription( $invoice, true );

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...
1369
1370
        if ( empty( $subscription ) ) {
1371
            return;
1372
        }
1373
1374
        $frequency = WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency($subscription->period, $subscription->frequency);
1375
        $billing = wpinv_price(wpinv_format_amount($subscription->recurring_amount), $invoice->get_currency() ) . ' / ' . $frequency;
1376
        $initial = wpinv_price(wpinv_format_amount($subscription->initial_amount), $invoice->get_currency() );
1377
1378
        $payments = $subscription->get_child_payments();
1379
        ?>
1380
        <div class="wpinv-subscriptions-details">
1381
            <h3 class="wpinv-subscriptions-t"><?php echo apply_filters( 'wpinv_subscription_details_title', __( 'Subscription Details', 'invoicing' ) ); ?></h3>
1382
            <table class="table">
1383
                <thead>
1384
                    <tr>
1385
                        <th><?php _e( 'Billing Cycle', 'invoicing' ) ;?></th>
1386
                        <th><?php _e( 'Start Date', 'invoicing' ) ;?></th>
1387
                        <th><?php _e( 'Expiration Date', 'invoicing' ) ;?></th>
1388
                        <th class="text-center"><?php _e( 'Times Billed', 'invoicing' ) ;?></th>
1389
                        <th class="text-center"><?php _e( 'Status', 'invoicing' ) ;?></th>
1390
                    </tr>
1391
                </thead>
1392
                <tbody>
1393
                    <tr>
1394
                        <td><?php printf(_x('%s then %s', 'Initial subscription amount then billing cycle and amount', 'invoicing'), $initial, $billing); ?></td>
1395
                        <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

1395
                        <td><?php echo date_i18n(/** @scrutinizer ignore-type */ get_option('date_format'), strtotime($subscription->created, current_time('timestamp'))); ?></td>
Loading history...
1396
                        <td><?php echo date_i18n(get_option('date_format'), strtotime($subscription->expiration, current_time('timestamp'))); ?></td>
1397
                        <td class="text-center"><?php echo $subscription->get_times_billed() . ' / ' . (($subscription->bill_times == 0) ? 'Until Cancelled' : $subscription->bill_times); ?></td>
1398
                        <td class="text-center wpi-sub-status"><?php echo $subscription->get_status_label(); ?></td>
1399
                    </tr>
1400
                </tbody>
1401
            </table>
1402
        </div>
1403
        <?php if ( !empty( $payments ) ) { ?>
1404
        <div class="wpinv-renewal-payments">
1405
            <h3 class="wpinv-renewals-t"><?php echo apply_filters( 'wpinv_renewal_payments_title', __( 'Renewal Payments', 'invoicing' ) ); ?></h3>
1406
            <table class="table">
1407
                <thead>
1408
                    <tr>
1409
                        <th>#</th>
1410
                        <th><?php _e( 'Invoice', 'invoicing' ) ;?></th>
1411
                        <th><?php _e( 'Date', 'invoicing' ) ;?></th>
1412
                        <th class="text-right"><?php _e( 'Amount', 'invoicing' ) ;?></th>
1413
                    </tr>
1414
                </thead>
1415
                <tbody>
1416
                    <?php
1417
                        $i = 1;
1418
                        foreach ( $payments as $payment ) {
1419
                            $invoice_id = $payment->ID;
1420
                    ?>
1421
                    <tr>
1422
                        <th scope="row"><?php echo $i;?></th>
1423
                        <td><?php echo wpinv_invoice_link( $invoice_id ) ;?></td>
1424
                        <td><?php echo$invoice->get_date_created(); ?></td>
1425
                        <td class="text-right"><?php echo wpinv_price( wpinv_format_amount( $invoice->get_total() ), $invoice->get_currency() ); ?></td>
1426
                    </tr>
1427
                    <?php $i++; } ?>
1428
                </tbody>
1429
            </table>
1430
        </div>
1431
        <?php } ?>
1432
        <?php
1433
    }
1434
}
1435
add_action( 'getpaid_invoice_line_items', 'wpinv_invoice_subscription_details', 20 );
1436
1437
function wpinv_cart_total_label( $label, $invoice ) {
1438
    if ( empty( $invoice ) ) {
1439
        return $label;
1440
    }
1441
1442
    $prefix_label = '';
1443
    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...
1444
        $prefix_label   = '<span class="label label-primary label-recurring">' . __( 'Recurring Payment', 'invoicing' ) . '</span> ' . wpinv_subscription_payment_desc( $invoice );
1445
    } else if ( $invoice->is_renewal() ) {
1446
        $prefix_label   = '<span class="label label-primary label-renewal">' . __( 'Renewal Payment', 'invoicing' ) . '</span> ';        
1447
    }
1448
1449
    if ( $prefix_label != '' ) {
1450
        $label  = '<span class="wpinv-cart-sub-desc">' . $prefix_label . '</span> ' . $label;
1451
    }
1452
1453
    return $label;
1454
}
1455
add_filter( 'wpinv_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
1456
add_filter( 'wpinv_email_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
1457
add_filter( 'wpinv_print_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
1458
1459
function wpinv_get_invoice_note_line_item( $note, $echo = true ) {
1460
    if ( empty( $note ) ) {
1461
        return NULL;
1462
    }
1463
1464
    if ( is_int( $note ) ) {
1465
        $note = get_comment( $note );
1466
    }
1467
1468
    if ( !( is_object( $note ) && is_a( $note, 'WP_Comment' ) ) ) {
1469
        return NULL;
1470
    }
1471
1472
    $note_classes   = array( 'note' );
1473
    $note_classes[] = get_comment_meta( $note->comment_ID, '_wpi_customer_note', true ) ? 'customer-note' : '';
1474
    $note_classes[] = $note->comment_author === 'System' ? 'system-note' : '';
1475
    $note_classes   = apply_filters( 'wpinv_invoice_note_class', array_filter( $note_classes ), $note );
1476
    $note_classes   = !empty( $note_classes ) ? implode( ' ', $note_classes ) : '';
1477
1478
    ob_start();
1479
    ?>
1480
    <li rel="<?php echo absint( $note->comment_ID ) ; ?>" class="<?php echo esc_attr( $note_classes ); ?> mt-4 pl-3 pr-3">
1481
        <div class="note_content bg-light border position-relative p-4">
1482
1483
            <?php echo wpautop( wptexturize( wp_kses_post( $note->comment_content ) ) ); ?>
1484
1485
            <?php if ( ! is_admin() ) : ?>
1486
                <em class="meta position-absolute form-text">
1487
                    <?php
1488
                        printf(
1489
                            __( '%1$s - %2$s at %3$s', 'invoicing' ),
1490
                            $note->comment_author,
1491
                            date_i18n( get_option( 'date_format' ), strtotime( $note->comment_date ) ),
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

1491
                            date_i18n( /** @scrutinizer ignore-type */ get_option( 'date_format' ), strtotime( $note->comment_date ) ),
Loading history...
1492
                            date_i18n( get_option( 'time_format' ), strtotime( $note->comment_date ) )
1493
                        );
1494
                    ?>
1495
                </em>
1496
            <?php endif; ?>
1497
1498
        </div>
1499
1500
        <?php if ( is_admin() ) : ?>
1501
1502
            <p class="meta px-4 py-2">
1503
                <abbr class="exact-date" title="<?php echo esc_attr( $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;
1504
                <?php if ( $note->comment_author !== 'System' && wpinv_current_user_can_manage_invoicing() ) { ?>
1505
                    <a href="#" class="delete_note"><?php _e( 'Delete note', 'invoicing' ); ?></a>
1506
                <?php } ?>
1507
            </p>
1508
1509
        <?php endif; ?>
1510
        
1511
    </li>
1512
    <?php
1513
    $note_content = ob_get_clean();
1514
    $note_content = apply_filters( 'wpinv_get_invoice_note_line_item', $note_content, $note, $echo );
1515
1516
    if ( $echo ) {
1517
        echo $note_content;
1518
    } else {
1519
        return $note_content;
1520
    }
1521
}
1522
1523
function wpinv_invalid_invoice_content() {
1524
    global $post;
1525
1526
    $invoice = wpinv_get_invoice( $post->ID );
1527
1528
    $error = __( 'This invoice is only viewable by clicking on the invoice link that was sent to you via email.', 'invoicing' );
1529
    if ( !empty( $invoice->get_id() ) && $invoice->has_status( array_keys( wpinv_get_invoice_statuses() ) ) ) {
1530
        if ( is_user_logged_in() ) {
1531
            if ( wpinv_require_login_to_checkout() ) {
1532
                if ( isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
1533
                    $error = __( 'You are not allowed to view this invoice.', 'invoicing' );
1534
                }
1535
            }
1536
        } else {
1537
            if ( wpinv_require_login_to_checkout() ) {
1538
                if ( isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
1539
                    $error = __( 'You must be logged in to view this invoice.', 'invoicing' );
1540
                }
1541
            }
1542
        }
1543
    } else {
1544
        $error = __( 'This invoice is deleted or does not exist.', 'invoicing' );
1545
    }
1546
    ?>
1547
    <div class="row wpinv-row-invalid">
1548
        <div class="col-md-6 col-md-offset-3 wpinv-message error">
1549
            <h3><?php _e( 'Access Denied', 'invoicing' ); ?></h3>
1550
            <p class="wpinv-msg-text"><?php echo $error; ?></p>
1551
        </div>
1552
    </div>
1553
    <?php
1554
}
1555
add_action( 'wpinv_invalid_invoice_content', 'wpinv_invalid_invoice_content' );
1556
1557
/**
1558
 * Function to get privacy policy text.
1559
 *
1560
 * @since 1.0.13
1561
 * @return string
1562
 */
1563
function wpinv_get_policy_text() {
1564
    $privacy_page_id = get_option( 'wp_page_for_privacy_policy', 0 );
1565
1566
    $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]' ));
1567
1568
    if(!$privacy_page_id){
1569
        $privacy_page_id = wpinv_get_option( 'privacy_page', 0 );
1570
    }
1571
1572
    $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

1572
    $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...
1573
1574
    $find_replace = array(
1575
        '[wpinv_privacy_policy]' => $privacy_link,
1576
    );
1577
1578
    $privacy_text = str_replace( array_keys( $find_replace ), array_values( $find_replace ), $text );
1579
1580
    return wp_kses_post(wpautop($privacy_text));
1581
}
1582
1583
1584
/**
1585
 * Allows the user to set their own price for an invoice item
1586
 */
1587
function wpinv_checkout_cart_item_name_your_price( $cart_item, $key ) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

1587
function wpinv_checkout_cart_item_name_your_price( $cart_item, /** @scrutinizer ignore-unused */ $key ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1588
    
1589
    //Ensure we have an item id
1590
    if(! is_array( $cart_item ) || empty( $cart_item['id'] ) ) {
1591
        return;
1592
    }
1593
1594
    //Fetch the item
1595
    $item_id = $cart_item['id'];
1596
    $item    = new WPInv_Item( $item_id );
1597
    
1598
    if(! $item->supports_dynamic_pricing() || !$item->get_is_dynamic_pricing() ) {
1599
        return;
1600
    }
1601
1602
    //Fetch the dynamic pricing "strings"
1603
    $suggested_price_text = esc_html( wpinv_get_option( 'suggested_price_text', __( 'Suggested Price:', 'invoicing' ) ) );
1604
    $minimum_price_text   = esc_html( wpinv_get_option( 'minimum_price_text', __( 'Minimum Price:', 'invoicing' ) ) );
1605
    $name_your_price_text = esc_html( wpinv_get_option( 'name_your_price_text', __( 'Name Your Price', 'invoicing' ) ) );
1606
1607
    //Display a "name_your_price" button
1608
    echo " &mdash; <a href='#' class='wpinv-name-your-price-frontend small'>$name_your_price_text</a></div>";
1609
1610
    //Display a name_your_price form
1611
    echo '<div class="name-your-price-miniform">';
1612
    
1613
    //Maybe display the recommended price
1614
    if( $item->get_price() > 0 && !empty( $suggested_price_text ) ) {
1615
        $suggested_price = $item->get_the_price();
1616
        echo "<div>$suggested_price_text &mdash; $suggested_price</div>";
1617
    }
1618
1619
    //Display the update price form
1620
    $symbol         = wpinv_currency_symbol();
1621
    $position       = wpinv_currency_position();
1622
    $minimum        = esc_attr( $item->get_minimum_price() );
1623
    $price          = esc_attr( $cart_item['item_price'] );
1624
    $update         = esc_attr__( "Update", 'invoicing' );
1625
1626
    //Ensure it supports dynamic prici
1627
    if( $price < $minimum ) {
1628
        $price = $minimum;
1629
    }
1630
1631
    echo '<label>';
1632
    echo $position != 'right' ? $symbol . '&nbsp;' : '';
1633
    echo "<input type='number' min='$minimum' placeholder='$price' value='$price' class='wpi-field-price' />";
1634
    echo $position == 'right' ? '&nbsp;' . $symbol : '' ;
1635
    echo "</label>";
1636
    echo "<input type='hidden' value='$item_id' class='wpi-field-item' />";
1637
    echo "<a class='btn btn-success wpinv-submit wpinv-update-dynamic-price-frontend'>$update</a>";
1638
1639
    //Maybe display the minimum price
1640
    if( $item->get_minimum_price() > 0 && !empty( $minimum_price_text ) ) {
1641
        $minimum_price = wpinv_price( wpinv_format_amount( $item->get_minimum_price() ) );
1642
        echo "<div>$minimum_price_text &mdash; $minimum_price</div>";
1643
    }
1644
1645
    echo "</div>";
1646
1647
}
1648
add_action( 'wpinv_checkout_cart_item_price_after', 'wpinv_checkout_cart_item_name_your_price', 10, 2 );
1649
1650
function wpinv_oxygen_fix_conflict() {
1651
    global $ct_ignore_post_types;
1652
1653
    if ( ! is_array( $ct_ignore_post_types ) ) {
1654
        $ct_ignore_post_types = array();
1655
    }
1656
1657
    $post_types = array( 'wpi_discount', 'wpi_invoice', 'wpi_item' );
1658
1659
    foreach ( $post_types as $post_type ) {
1660
        $ct_ignore_post_types[] = $post_type;
1661
1662
        // Ignore post type
1663
        add_filter( 'pre_option_oxygen_vsb_ignore_post_type_' . $post_type, '__return_true', 999 );
1664
    }
1665
1666
    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

1666
    /** @scrutinizer ignore-call */ 
1667
    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...
1667
    add_filter( 'template_include', 'wpinv_template', 999, 1 );
1668
}
1669
1670
/**
1671
 * Helper function to display a payment form on the frontend.
1672
 * 
1673
 * @param GetPaid_Payment_Form $form
1674
 */
1675
function getpaid_display_payment_form( $form ) {
1676
1677
    if ( is_numeric( $form ) ) {
0 ignored issues
show
introduced by
The condition is_numeric($form) is always false.
Loading history...
1678
        $form = new GetPaid_Payment_Form( $form );
1679
    }
1680
1681
    $form->display();
1682
1683
}
1684
1685
/**
1686
 * Helper function to display a item payment form on the frontend.
1687
 */
1688
function getpaid_display_item_payment_form( $items ) {
1689
    global $invoicing;
1690
1691
    foreach ( array_keys( $items ) as $id ) {
1692
	    if ( 'publish' != get_post_status( $id ) ) {
1693
		    unset( $items[ $id ] );
1694
	    }
1695
    }
1696
1697
    if ( empty( $items ) ) {
1698
		return aui()->alert(
1699
			array(
1700
				'type'    => 'warning',
1701
				'content' => __( 'No published items found', 'invoicing' ),
1702
			)
1703
		);
1704
    }
1705
1706
    $item_key = getpaid_convert_items_to_string( $items );
1707
1708
    // Get the form elements and items.
1709
    $form     = wpinv_get_default_payment_form();
1710
	$elements = $invoicing->form_elements->get_form_elements( $form );
1711
	$items    = $invoicing->form_elements->convert_normal_items( $items );
1712
1713
	ob_start();
1714
	echo "<form class='wpinv_payment_form'>";
1715
	do_action( 'wpinv_payment_form_top' );
1716
    echo "<input type='hidden' name='form_id' value='$form'/>";
1717
    echo "<input type='hidden' name='form_items' value='$item_key'/>";
1718
	wp_nonce_field( 'wpinv_payment_form', 'wpinv_payment_form' );
1719
	wp_nonce_field( 'vat_validation', '_wpi_nonce' );
1720
1721
	foreach ( $elements as $element ) {
1722
		do_action( 'wpinv_frontend_render_payment_form_element', $element, $items, $form );
1723
		do_action( "wpinv_frontend_render_payment_form_{$element['type']}", $element, $items, $form );
1724
	}
1725
1726
	echo "<div class='wpinv_payment_form_errors alert alert-danger d-none'></div>";
1727
	do_action( 'wpinv_payment_form_bottom' );
1728
	echo '</form>';
1729
1730
	$content = ob_get_clean();
1731
	return str_replace( 'sr-only', '', $content );
1732
}
1733
1734
/**
1735
 * Helper function to display an invoice payment form on the frontend.
1736
 */
1737
function getpaid_display_invoice_payment_form( $invoice_id ) {
1738
    global $invoicing;
1739
1740
    $invoice = wpinv_get_invoice( $invoice_id );
1741
1742
    if ( empty( $invoice ) ) {
1743
		return aui()->alert(
1744
			array(
1745
				'type'    => 'warning',
1746
				'content' => __( 'Invoice not found', 'invoicing' ),
1747
			)
1748
		);
1749
    }
1750
1751
    if ( $invoice->is_paid() ) {
1752
		return aui()->alert(
1753
			array(
1754
				'type'    => 'warning',
1755
				'content' => __( 'Invoice has already been paid', 'invoicing' ),
1756
			)
1757
		);
1758
    }
1759
1760
    // Get the form elements and items.
1761
    $form     = wpinv_get_default_payment_form();
1762
	$elements = $invoicing->form_elements->get_form_elements( $form );
1763
	$items    = $invoicing->form_elements->convert_checkout_items( $invoice->cart_details, $invoice );
0 ignored issues
show
Bug Best Practice introduced by
The property cart_details does not exist on WPInv_Invoice. Since you implemented __get, consider adding a @property annotation.
Loading history...
1764
1765
	ob_start();
1766
	echo "<form class='wpinv_payment_form'>";
1767
	do_action( 'wpinv_payment_form_top' );
1768
    echo "<input type='hidden' name='form_id' value='$form'/>";
1769
    echo "<input type='hidden' name='invoice_id' value='$invoice_id'/>";
1770
	wp_nonce_field( 'wpinv_payment_form', 'wpinv_payment_form' );
1771
	wp_nonce_field( 'vat_validation', '_wpi_nonce' );
1772
1773
	foreach ( $elements as $element ) {
1774
		do_action( 'wpinv_frontend_render_payment_form_element', $element, $items, $form );
1775
		do_action( "wpinv_frontend_render_payment_form_{$element['type']}", $element, $items, $form );
1776
	}
1777
1778
	echo "<div class='wpinv_payment_form_errors alert alert-danger d-none'></div>";
1779
	do_action( 'wpinv_payment_form_bottom' );
1780
	echo '</form>';
1781
1782
	$content = ob_get_clean();
1783
	return str_replace( 'sr-only', '', $content );
1784
}
1785
1786
/**
1787
 * Helper function to convert item string to array.
1788
 */
1789
function getpaid_convert_items_to_array( $items ) {
1790
    $items    = array_filter( array_map( 'trim', explode( ',', $items ) ) );
1791
    $prepared = array();
1792
1793
    foreach ( $items as $item ) {
1794
        $data = array_map( 'trim', explode( '|', $item ) );
1795
1796
        if ( empty( $data[0] ) || ! is_numeric( $data[0] ) ) {
1797
            continue;
1798
        }
1799
1800
        $quantity = 1;
1801
        if ( isset( $data[1] ) && is_numeric( $data[1] ) ) {
1802
            $quantity = $data[1];
1803
        }
1804
1805
        $prepared[ $data[0] ] = $quantity;
1806
1807
    }
1808
1809
    return $prepared;
1810
}
1811
1812
/**
1813
 * Helper function to convert item array to string.
1814
 */
1815
function getpaid_convert_items_to_string( $items ) {
1816
    $prepared = array();
1817
1818
    foreach ( $items as $item => $quantity ) {
1819
        $prepared[] = "$item|$quantity";
1820
    }
1821
    return implode( ',', $prepared );
1822
}
1823
1824
/**
1825
 * Helper function to display a payment item.
1826
 * 
1827
 * Provide a label and one of $form, $items or $invoice.
1828
 */
1829
function getpaid_get_payment_button( $label, $form = null, $items = null, $invoice = null ) {
1830
    $label = sanitize_text_field( $label );
1831
    $nonce = wp_create_nonce('getpaid_ajax_form');
1832
1833
    if ( ! empty( $form ) ) {
1834
        $form  = esc_attr( $form );
1835
        return "<button class='btn btn-primary getpaid-payment-button' type='button' data-nonce='$nonce' data-form='$form'>$label</button>"; 
1836
    }
1837
	
1838
	if ( ! empty( $items ) ) {
1839
        $items  = esc_attr( $items );
1840
        return "<button class='btn btn-primary getpaid-payment-button' type='button' data-nonce='$nonce' data-item='$items'>$label</button>"; 
1841
    }
1842
    
1843
    if ( ! empty( $invoice ) ) {
1844
        $invoice  = esc_attr( $invoice );
1845
        return "<button class='btn btn-primary getpaid-payment-button' type='button' data-nonce='$nonce' data-invoice='$invoice'>$label</button>"; 
1846
    }
1847
1848
}
1849
1850
/**
1851
 * Display invoice description before line items.
1852
 *
1853
 * @param WPInv_Invoice $invoice
1854
 */
1855
function getpaid_the_invoice_description( $invoice ) {
1856
    $description = $invoice->get_description();
1857
1858
    if ( empty( $description ) ) {
1859
        return;
1860
    }
1861
1862
    $description = wp_kses_post( $description );
1863
    echo "<small class='getpaid-invoice-description text-dark p-2 form-text'><em>$description</em></small>";
1864
}
1865
add_action( 'getpaid_invoice_line_items', 'getpaid_the_invoice_description', 100 );
1866
1867
/**
1868
 * Render element on a form.
1869
 *
1870
 * @param array $element
1871
 * @param GetPaid_Payment_Form $form
1872
 */
1873
function getpaid_payment_form_element( $element, $form ) {
1874
1875
    // Set up the args.
1876
    $element_type    = trim( $element['type'] );
1877
    $element['form'] = $form;
1878
    extract( $element );
1879
1880
    // Try to locate the appropriate template.
1881
    $located = wpinv_locate_template( "payment-forms/elements/$element_type.php" );
1882
    
1883
    // Abort if this is not our element.
1884
    if ( empty( $located ) || ! file_exists( $located ) ) {
1885
        return;
1886
    }
1887
1888
    // Generate the class and id of the element.
1889
    $wrapper_class = 'getpaid-payment-form-element-' . trim( esc_attr( $element_type ) );
1890
    $id            = isset( $id ) ? $id : uniqid( 'gp' );
1891
1892
    // Echo the opening wrapper.
1893
    echo "<div class='getpaid-payment-form-element $wrapper_class'>";
1894
1895
    // Fires before displaying a given element type's content.
1896
    do_action( "getpaid_before_payment_form_{$element_type}_element", $element, $form );
1897
1898
    // Include the template for the element.
1899
    include $located;
1900
1901
    // Fires after displaying a given element type's content.
1902
    do_action( "getpaid_payment_form_{$element_type}_element", $element, $form );
1903
1904
    // Echo the closing wrapper.
1905
    echo '</div>';
1906
}
1907
add_action( 'getpaid_payment_form_element', 'getpaid_payment_form_element', 10, 2 );
1908
1909
/**
1910
 * Shows a list of gateways that support recurring payments.
1911
 */
1912
function wpinv_get_recurring_gateways_text() {
1913
    $gateways = array();
1914
1915
    foreach ( wpinv_get_payment_gateways() as $key => $gateway ) {
1916
        if ( wpinv_gateway_support_subscription( $key ) ) {
1917
            $gateways[] = sanitize_text_field( $gateway['admin_label'] );
1918
        }
1919
    }
1920
1921
    if ( empty( $gateways ) ) {
1922
        return "<span class='form-text text-danger'>" . __( 'No active gateways support subscription payments.', 'invoicing' ) ."</span>";
1923
    }
1924
1925
    return "<span class='form-text text-muted'>" . wp_sprintf( __( 'Subscription payments only supported by: %s', 'invoicing' ), implode( ', ', $gateways ) ) ."</span>";
1926
1927
}
1928