Passed
Push — master ( c5e685...72070d )
by Brian
15:09
created

getpaid_invoice_meta()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 5
c 1
b 1
f 0
dl 0
loc 14
rs 10
cc 2
nc 2
nop 1
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 getpaid_template()->templates_dir;
108
}
109
110
/**
111
 * Returns a url to the templates directory.
112
 * 
113
 * @return string
114
 */
115
function wpinv_get_templates_url() {
116
    return getpaid_template()->templates_url;
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
    return getpaid_template()->display_template( $template_name, $args, $template_path, $default_path );
0 ignored issues
show
Bug introduced by
Are you sure the usage of getpaid_template()->disp...te_path, $default_path) targeting GetPaid_Template::display_template() 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...
130
}
131
132
/**
133
 * Retrieves a given template's html code.
134
 * 
135
 * First checks if there is a template overide, if not it loads the default template.
136
 * 
137
 * @param string $template_name e.g payment-forms/cart.php The template to locate.
138
 * @param array $args An array of args to pass to the template.
139
 * @param string $template_path The templates directory relative to the theme's root dir. Defaults to 'invoicing'.
140
 * @param string $default_path The root path to the default template. Defaults to invoicing/templates
141
 */
142
function wpinv_get_template_html( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
143
	return getpaid_template()->get_template( $template_name, $args, $template_path, $default_path );
144
}
145
146
/**
147
 * Returns the default path from where to look for template overides.
148
 * 
149
 * @return string
150
 */
151
function wpinv_template_path() {
152
    return apply_filters( 'wpinv_template_path', wpinv_get_theme_template_dir_name() );
153
}
154
155
/**
156
 * Returns the directory containing the template overides.
157
 * 
158
 * @return string
159
 */
160
function wpinv_get_theme_template_dir_name() {
161
	return trailingslashit( apply_filters( 'wpinv_templates_dir', 'invoicing' ) );
162
}
163
164
/**
165
 * Locates a template path.
166
 * 
167
 * First checks if there is a template overide, if not it loads the default template.
168
 * 
169
 * @param string $template_name e.g payment-forms/cart.php The template to locate.
170
 * @param string $template_path The template path relative to the theme's root dir. Defaults to 'invoicing'.
171
 * @param string $default_path The root path to the default template. Defaults to invoicing/templates
172
 */
173
function wpinv_locate_template( $template_name, $template_path = '', $default_path = '' ) {
174
    return getpaid_template()->locate_template( $template_name, $template_path, $default_path );
175
}
176
177
function wpinv_get_template_part( $slug, $name = null, $load = true ) {
178
	do_action( 'get_template_part_' . $slug, $slug, $name );
179
180
	// Setup possible parts
181
	$templates = array();
182
	if ( isset( $name ) )
183
		$templates[] = $slug . '-' . $name . '.php';
184
	$templates[] = $slug . '.php';
185
186
	// Allow template parts to be filtered
187
	$templates = apply_filters( 'wpinv_get_template_part', $templates, $slug, $name );
188
189
	// Return the part that is found
190
	return wpinv_locate_tmpl( $templates, $load, false );
191
}
192
193
function wpinv_locate_tmpl( $template_names, $load = false, $require_once = true ) {
194
	// No file found yet
195
	$located = false;
196
197
	// Try to find a template file
198
	foreach ( (array)$template_names as $template_name ) {
199
200
		// Continue if template is empty
201
		if ( empty( $template_name ) )
202
			continue;
203
204
		// Trim off any slashes from the template name
205
		$template_name = ltrim( $template_name, '/' );
206
207
		// try locating this template file by looping through the template paths
208
		foreach( wpinv_get_theme_template_paths() as $template_path ) {
209
210
			if( file_exists( $template_path . $template_name ) ) {
211
				$located = $template_path . $template_name;
212
				break;
213
			}
214
		}
215
216
		if( !empty( $located ) ) {
217
			break;
218
		}
219
	}
220
221
	if ( ( true == $load ) && ! empty( $located ) )
222
		load_template( $located, $require_once );
223
224
	return $located;
225
}
226
227
function wpinv_get_theme_template_paths() {
228
	$template_dir = wpinv_get_theme_template_dir_name();
229
230
	$file_paths = array(
231
		1 => trailingslashit( get_stylesheet_directory() ) . $template_dir,
232
		10 => trailingslashit( get_template_directory() ) . $template_dir,
233
		100 => wpinv_get_templates_dir()
234
	);
235
236
	$file_paths = apply_filters( 'wpinv_template_paths', $file_paths );
237
238
	// sort the file paths based on priority
239
	ksort( $file_paths, SORT_NUMERIC );
240
241
	return array_map( 'trailingslashit', $file_paths );
242
}
243
244
function wpinv_checkout_meta_tags() {
245
246
	$pages   = array();
247
	$pages[] = wpinv_get_option( 'success_page' );
248
	$pages[] = wpinv_get_option( 'failure_page' );
249
	$pages[] = wpinv_get_option( 'invoice_history_page' );
250
	$pages[] = wpinv_get_option( 'invoice_subscription_page' );
251
252
	if( !wpinv_is_checkout() && !is_page( $pages ) ) {
253
		return;
254
	}
255
256
	echo '<meta name="robots" content="noindex,nofollow" />' . "\n";
257
}
258
add_action( 'wp_head', 'wpinv_checkout_meta_tags' );
259
260
function wpinv_add_body_classes( $class ) {
261
	$classes = (array)$class;
262
263
	if( wpinv_is_checkout() ) {
264
		$classes[] = 'wpinv-checkout';
265
		$classes[] = 'wpinv-page';
266
	}
267
268
	if( wpinv_is_success_page() ) {
269
		$classes[] = 'wpinv-success';
270
		$classes[] = 'wpinv-page';
271
	}
272
273
	if( wpinv_is_failed_transaction_page() ) {
274
		$classes[] = 'wpinv-failed-transaction';
275
		$classes[] = 'wpinv-page';
276
	}
277
278
	if( wpinv_is_invoice_history_page() ) {
279
		$classes[] = 'wpinv-history';
280
		$classes[] = 'wpinv-page';
281
	}
282
283
	if( wpinv_is_subscriptions_history_page() ) {
284
		$classes[] = 'wpinv-subscription';
285
		$classes[] = 'wpinv-page';
286
	}
287
288
	if( wpinv_is_test_mode() ) {
289
		$classes[] = 'wpinv-test-mode';
290
		$classes[] = 'wpinv-page';
291
	}
292
293
	return array_unique( $classes );
294
}
295
add_filter( 'body_class', 'wpinv_add_body_classes' );
296
297
function wpinv_html_dropdown( $name = 'wpinv_discounts', $selected = 0, $status = '' ) {
298
    $args = array( 'nopaging' => true );
299
300
    if ( ! empty( $status ) )
301
        $args['post_status'] = $status;
302
303
    $discounts = wpinv_get_discounts( $args );
304
    $options   = array();
305
306
    if ( $discounts ) {
307
        foreach ( $discounts as $discount ) {
308
            $options[ absint( $discount->ID ) ] = esc_html( get_the_title( $discount->ID ) );
309
        }
310
    } else {
311
        $options[0] = __( 'No discounts found', 'invoicing' );
312
    }
313
314
    $output = wpinv_html_select( array(
315
        'name'             => $name,
316
        'selected'         => $selected,
317
        'options'          => $options,
318
        'show_option_all'  => false,
319
        'show_option_none' => false,
320
    ) );
321
322
    return $output;
323
}
324
325
function wpinv_html_year_dropdown( $name = 'year', $selected = 0, $years_before = 5, $years_after = 0 ) {
326
    $current     = date( 'Y' );
327
    $start_year  = $current - absint( $years_before );
328
    $end_year    = $current + absint( $years_after );
329
    $selected    = empty( $selected ) ? date( 'Y' ) : $selected;
330
    $options     = array();
331
332
    while ( $start_year <= $end_year ) {
333
        $options[ absint( $start_year ) ] = $start_year;
334
        $start_year++;
335
    }
336
337
    $output = wpinv_html_select( array(
338
        'name'             => $name,
339
        'selected'         => $selected,
340
        'options'          => $options,
341
        'show_option_all'  => false,
342
        'show_option_none' => false
343
    ) );
344
345
    return $output;
346
}
347
348
function wpinv_html_month_dropdown( $name = 'month', $selected = 0 ) {
349
350
    $options = array(
351
        '1'  => __( 'January', 'invoicing' ),
352
        '2'  => __( 'February', 'invoicing' ),
353
        '3'  => __( 'March', 'invoicing' ),
354
        '4'  => __( 'April', 'invoicing' ),
355
        '5'  => __( 'May', 'invoicing' ),
356
        '6'  => __( 'June', 'invoicing' ),
357
        '7'  => __( 'July', 'invoicing' ),
358
        '8'  => __( 'August', 'invoicing' ),
359
        '9'  => __( 'September', 'invoicing' ),
360
        '10' => __( 'October', 'invoicing' ),
361
        '11' => __( 'November', 'invoicing' ),
362
        '12' => __( 'December', 'invoicing' ),
363
    );
364
365
    // If no month is selected, default to the current month
366
    $selected = empty( $selected ) ? date( 'n' ) : $selected;
367
368
    $output = wpinv_html_select( array(
369
        'name'             => $name,
370
        'selected'         => $selected,
371
        'options'          => $options,
372
        'show_option_all'  => false,
373
        'show_option_none' => false
374
    ) );
375
376
    return $output;
377
}
378
379
function wpinv_html_select( $args = array() ) {
380
    $defaults = array(
381
        'options'          => array(),
382
        'name'             => null,
383
        'class'            => '',
384
        'id'               => '',
385
        'selected'         => 0,
386
        'placeholder'      => null,
387
        'multiple'         => false,
388
        'show_option_all'  => _x( 'All', 'all dropdown items', 'invoicing' ),
389
        'show_option_none' => _x( 'None', 'no dropdown items', 'invoicing' ),
390
        'data'             => array(),
391
        'onchange'         => null,
392
        'required'         => false,
393
        'disabled'         => false,
394
        'readonly'         => false,
395
    );
396
397
    $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_rule'] is assigned to $vat_rule in includes/admin/class-getpaid-post-types-admin.php on line 528
  1. Read from $_GET, and $_GET['vat_rule'] is assigned to $vat_rule
    in includes/admin/class-getpaid-post-types-admin.php on line 528
  2. wpinv_html_select() is called
    in includes/admin/class-getpaid-post-types-admin.php on line 533
  3. Enters via parameter $args
    in includes/wpinv-template-functions.php on line 379
  2. Path: Read from $_GET, and $_GET['vat_class'] is assigned to $vat_class in includes/admin/class-getpaid-post-types-admin.php on line 559
  1. Read from $_GET, and $_GET['vat_class'] is assigned to $vat_class
    in includes/admin/class-getpaid-post-types-admin.php on line 559
  2. wpinv_html_select() is called
    in includes/admin/class-getpaid-post-types-admin.php on line 563
  3. Enters via parameter $args
    in includes/wpinv-template-functions.php on line 379
  3. Path: Read from $_GET, and $_GET['type'] is assigned to $type in includes/admin/class-getpaid-post-types-admin.php on line 584
  1. Read from $_GET, and $_GET['type'] is assigned to $type
    in includes/admin/class-getpaid-post-types-admin.php on line 584
  2. wpinv_html_select() is called
    in includes/admin/class-getpaid-post-types-admin.php on line 588
  3. Enters via parameter $args
    in includes/wpinv-template-functions.php on line 379

Used in variable context

  1. wp_parse_args() is called
    in includes/wpinv-template-functions.php on line 437
  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...
398
399
    $data_elements = '';
400
    foreach ( $args['data'] as $key => $value ) {
401
        $data_elements .= ' data-' . esc_attr( $key ) . '="' . esc_attr( $value ) . '"';
402
    }
403
404
    if( $args['multiple'] ) {
405
        $multiple = ' MULTIPLE';
406
    } else {
407
        $multiple = '';
408
    }
409
410
    if( $args['placeholder'] ) {
411
        $placeholder = $args['placeholder'];
412
    } else {
413
        $placeholder = '';
414
    }
415
    
416
    $options = '';
417
    if( !empty( $args['onchange'] ) ) {
418
        $options .= ' onchange="' . esc_attr( $args['onchange'] ) . '"';
419
    }
420
    
421
    if( !empty( $args['required'] ) ) {
422
        $options .= ' required="required"';
423
    }
424
    
425
    if( !empty( $args['disabled'] ) ) {
426
        $options .= ' disabled';
427
    }
428
    
429
    if( !empty( $args['readonly'] ) ) {
430
        $options .= ' readonly';
431
    }
432
433
    $class  = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
434
    $output = '<select name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( $args['id'] ) . '" class="wpinv-select ' . $class . '"' . $multiple . ' data-placeholder="' . $placeholder . '" ' . trim( $options ) . $data_elements . '>';
435
436
    if ( $args['show_option_all'] ) {
437
        if( $args['multiple'] ) {
438
            $selected = selected( true, in_array( 0, $args['selected'] ), false );
439
        } else {
440
            $selected = selected( $args['selected'], 0, false );
441
        }
442
        $output .= '<option value="all"' . $selected . '>' . esc_html( $args['show_option_all'] ) . '</option>';
443
    }
444
445
    if ( !empty( $args['options'] ) ) {
446
447
        if ( $args['show_option_none'] ) {
448
            if( $args['multiple'] ) {
449
                $selected = selected( true, in_array( "", $args['selected'] ), false );
450
            } else {
451
                $selected = selected( $args['selected'] === "", true, false );
452
            }
453
            $output .= '<option value=""' . $selected . '>' . esc_html( $args['show_option_none'] ) . '</option>';
454
        }
455
456
        foreach( $args['options'] as $key => $option ) {
457
458
            if( $args['multiple'] && is_array( $args['selected'] ) ) {
459
                $selected = selected( true, (bool)in_array( $key, $args['selected'] ), false );
460
            } else {
461
                $selected = selected( $args['selected'], $key, false );
462
            }
463
464
            $output .= '<option value="' . esc_attr( $key ) . '"' . $selected . '>' . esc_html( $option ) . '</option>';
465
        }
466
    }
467
468
    $output .= '</select>';
469
470
    return $output;
471
}
472
473
function wpinv_item_dropdown( $args = array() ) {
474
    $defaults = array(
475
        'name'              => 'wpi_item',
476
        'id'                => 'wpi_item',
477
        'class'             => '',
478
        'multiple'          => false,
479
        'selected'          => 0,
480
        'number'            => 100,
481
        'placeholder'       => __( 'Choose a item', 'invoicing' ),
482
        'data'              => array( 'search-type' => 'item' ),
483
        'show_option_all'   => false,
484
        'show_option_none'  => false,
485
        'show_recurring'    => false,
486
    );
487
488
    $args = wp_parse_args( $args, $defaults );
489
490
    $item_args = array(
491
        'post_type'      => 'wpi_item',
492
        'orderby'        => 'title',
493
        'order'          => 'ASC',
494
        'posts_per_page' => $args['number']
495
    );
496
497
    $item_args  = apply_filters( 'wpinv_item_dropdown_query_args', $item_args, $args, $defaults );
498
499
    $items      = get_posts( $item_args );
500
    $options    = array();
501
    if ( $items ) {
502
        foreach ( $items as $item ) {
503
            $title = esc_html( $item->post_title );
504
            
505
            if ( !empty( $args['show_recurring'] ) ) {
506
                $title .= wpinv_get_item_suffix( $item->ID, false );
507
            }
508
            
509
            $options[ absint( $item->ID ) ] = $title;
510
        }
511
    }
512
513
    // This ensures that any selected items are included in the drop down
514
    if( is_array( $args['selected'] ) ) {
515
        foreach( $args['selected'] as $item ) {
516
            if( ! in_array( $item, $options ) ) {
517
                $title = get_the_title( $item );
518
                if ( !empty( $args['show_recurring'] ) ) {
519
                    $title .= wpinv_get_item_suffix( $item, false );
520
                }
521
                $options[$item] = $title;
522
            }
523
        }
524
    } elseif ( is_numeric( $args['selected'] ) && $args['selected'] !== 0 ) {
525
        if ( ! in_array( $args['selected'], $options ) ) {
526
            $title = get_the_title( $args['selected'] );
527
            if ( !empty( $args['show_recurring'] ) ) {
528
                $title .= wpinv_get_item_suffix( $args['selected'], false );
529
            }
530
            $options[$args['selected']] = get_the_title( $args['selected'] );
531
        }
532
    }
533
534
    $output = wpinv_html_select( array(
535
        'name'             => $args['name'],
536
        'selected'         => $args['selected'],
537
        'id'               => $args['id'],
538
        'class'            => $args['class'],
539
        'options'          => $options,
540
        'multiple'         => $args['multiple'],
541
        'placeholder'      => $args['placeholder'],
542
        'show_option_all'  => $args['show_option_all'],
543
        'show_option_none' => $args['show_option_none'],
544
        'data'             => $args['data'],
545
    ) );
546
547
    return $output;
548
}
549
550
/**
551
 * Returns an array of published items.
552
 */
553
function wpinv_get_published_items_for_dropdown() {
554
555
    $items = get_posts(
556
        array(
557
            'post_type'      => 'wpi_item',
558
            'orderby'        => 'title',
559
            'order'          => 'ASC',
560
            'posts_per_page' => '-1'
561
        )
562
    );
563
564
    $options = array();
565
    if ( $items ) {
566
        foreach ( $items as $item ) {
567
            $options[ $item->ID ] = esc_html( $item->post_title ) . wpinv_get_item_suffix( $item->ID, false );
568
        }
569
    }
570
571
    return $options;
572
}
573
574
function wpinv_html_checkbox( $args = array() ) {
575
    $defaults = array(
576
        'name'     => null,
577
        'current'  => null,
578
        'class'    => 'wpinv-checkbox',
579
        'options'  => array(
580
            'disabled' => false,
581
            'readonly' => false
582
        )
583
    );
584
585
    $args = wp_parse_args( $args, $defaults );
586
587
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
588
    $options = '';
589
    if ( ! empty( $args['options']['disabled'] ) ) {
590
        $options .= ' disabled="disabled"';
591
    } elseif ( ! empty( $args['options']['readonly'] ) ) {
592
        $options .= ' readonly';
593
    }
594
595
    $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 ) . ' />';
596
597
    return $output;
598
}
599
600
/**
601
 * Displays a hidden field.
602
 */
603
function getpaid_hidden_field( $name, $value ) {
604
    $name  = sanitize_text_field( $name );
605
    $value = esc_attr( $value );
606
607
    echo "<input type='hidden' name='$name' value='$value' />";
608
}
609
610
function wpinv_html_text( $args = array() ) {
611
    // Backwards compatibility
612
    if ( func_num_args() > 1 ) {
613
        $args = func_get_args();
614
615
        $name  = $args[0];
616
        $value = isset( $args[1] ) ? $args[1] : '';
617
        $label = isset( $args[2] ) ? $args[2] : '';
618
        $desc  = isset( $args[3] ) ? $args[3] : '';
619
    }
620
621
    $defaults = array(
622
        'id'           => '',
623
        'name'         => isset( $name )  ? $name  : 'text',
624
        'value'        => isset( $value ) ? $value : null,
625
        'label'        => isset( $label ) ? $label : null,
626
        'desc'         => isset( $desc )  ? $desc  : null,
627
        'placeholder'  => '',
628
        'class'        => 'regular-text',
629
        'disabled'     => false,
630
        'readonly'     => false,
631
        'required'     => false,
632
        'autocomplete' => '',
633
        'data'         => false
634
    );
635
636
    $args = wp_parse_args( $args, $defaults );
637
638
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
639
    $options = '';
640
    if( $args['required'] ) {
641
        $options .= ' required="required"';
642
    }
643
    if( $args['readonly'] ) {
644
        $options .= ' readonly';
645
    }
646
    if( $args['readonly'] ) {
647
        $options .= ' readonly';
648
    }
649
650
    $data = '';
651
    if ( !empty( $args['data'] ) ) {
652
        foreach ( $args['data'] as $key => $value ) {
653
            $data .= 'data-' . wpinv_sanitize_key( $key ) . '="' . esc_attr( $value ) . '" ';
654
        }
655
    }
656
657
    $output = '<span id="wpinv-' . wpinv_sanitize_key( $args['name'] ) . '-wrap">';
658
    $output .= '<label class="wpinv-label" for="' . wpinv_sanitize_key( $args['id'] ) . '">' . esc_html( $args['label'] ) . '</label>';
659
    if ( ! empty( $args['desc'] ) ) {
660
        $output .= '<span class="wpinv-description">' . esc_html( $args['desc'] ) . '</span>';
661
    }
662
663
    $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 ) . '/>';
664
665
    $output .= '</span>';
666
667
    return $output;
668
}
669
670
function wpinv_html_date_field( $args = array() ) {
671
    if( empty( $args['class'] ) ) {
672
        $args['class'] = 'wpiDatepicker';
673
    } elseif( ! strpos( $args['class'], 'wpiDatepicker' ) ) {
674
        $args['class'] .= ' wpiDatepicker';
675
    }
676
677
    return wpinv_html_text( $args );
678
}
679
680
function wpinv_html_textarea( $args = array() ) {
681
    $defaults = array(
682
        'name'        => 'textarea',
683
        'value'       => null,
684
        'label'       => null,
685
        'desc'        => null,
686
        'class'       => 'large-text',
687
        'disabled'    => false,
688
        'placeholder' => '',
689
    );
690
691
    $args = wp_parse_args( $args, $defaults );
692
693
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
694
    $disabled = '';
695
    if( $args['disabled'] ) {
696
        $disabled = ' disabled="disabled"';
697
    }
698
699
    $output = '<span id="wpinv-' . wpinv_sanitize_key( $args['name'] ) . '-wrap">';
700
    $output .= '<label class="wpinv-label" for="' . wpinv_sanitize_key( $args['name'] ) . '">' . esc_html( $args['label'] ) . '</label>';
701
    $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>';
702
703
    if ( ! empty( $args['desc'] ) ) {
704
        $output .= '<span class="wpinv-description">' . esc_html( $args['desc'] ) . '</span>';
705
    }
706
    $output .= '</span>';
707
708
    return $output;
709
}
710
711
function wpinv_html_ajax_user_search( $args = array() ) {
712
    $defaults = array(
713
        'name'        => 'user_id',
714
        'value'       => null,
715
        'placeholder' => __( 'Enter username', 'invoicing' ),
716
        'label'       => null,
717
        'desc'        => null,
718
        'class'       => '',
719
        'disabled'    => false,
720
        'autocomplete'=> 'off',
721
        'data'        => false
722
    );
723
724
    $args = wp_parse_args( $args, $defaults );
725
726
    $args['class'] = 'wpinv-ajax-user-search ' . $args['class'];
727
728
    $output  = '<span class="wpinv_user_search_wrap">';
729
        $output .= wpinv_html_text( $args );
730
        $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>';
731
    $output .= '</span>';
732
733
    return $output;
734
}
735
736
/**
737
 * @deprecated.
738
 */
739
function wpinv_ip_geolocation() {}
740
741
/**
742
 * Use our template to display invoices.
743
 * 
744
 * @param string $template the template that is currently being used.
745
 */
746
function wpinv_template( $template ) {
747
    global $post;
748
749
    if ( ! is_admin() && ( is_single() || is_404() ) && ! empty( $post->ID ) && getpaid_is_invoice_post_type( get_post_type( $post->ID ) ) ) {
750
751
        // If the user can view this invoice, display it.
752
        if ( wpinv_user_can_view_invoice( $post->ID ) ) {
753
754
            return wpinv_get_template_part( 'wpinv-invoice-print', false, false );
755
756
        // Else display an error message.
757
        } else {
758
759
            return wpinv_get_template_part( 'wpinv-invalid-access', false, false );
760
761
        }
762
763
    }
764
765
    return $template;
766
}
767
add_filter( 'template_include', 'wpinv_template', 10, 1 );
768
769
function wpinv_get_business_address() {
770
    $business_address   = wpinv_store_address();
771
    $business_address   = !empty( $business_address ) ? wpautop( wp_kses_post( $business_address ) ) : '';
772
    
773
    $business_address = $business_address ? '<div class="address">' . $business_address . '</div>' : '';
774
    
775
    return apply_filters( 'wpinv_get_business_address', $business_address );
776
}
777
778
/**
779
 * Displays the company address.
780
 */
781
function wpinv_display_from_address() {
782
    wpinv_get_template( 'invoice/company-address.php' );
783
}
784
add_action( 'getpaid_invoice_details_left', 'wpinv_display_from_address', 10 );
785
786
function wpinv_watermark( $id = 0 ) {
787
    $output = wpinv_get_watermark( $id );
788
    return apply_filters( 'wpinv_get_watermark', $output, $id );
789
}
790
791
function wpinv_get_watermark( $id ) {
792
    if ( !$id > 0 ) {
793
        return NULL;
794
    }
795
796
    $invoice = wpinv_get_invoice( $id );
797
    
798
    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...
799
        if ( $invoice->is_paid() ) {
800
            return __( 'Paid', 'invoicing' );
801
        }
802
        if ( $invoice->is_refunded() ) {
803
            return __( 'Refunded', 'invoicing' );
804
        }
805
        if ( $invoice->has_status( array( 'wpi-cancelled' ) ) ) {
806
            return __( 'Cancelled', 'invoicing' );
807
        }
808
    }
809
    
810
    return NULL;
811
}
812
813
/**
814
 * @deprecated
815
 */
816
function wpinv_display_invoice_details( $invoice ) {
817
    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...
818
}
819
820
/**
821
 * Displays invoice meta.
822
 */
823
function getpaid_invoice_meta( $invoice ) {
824
825
    $invoice = new WPInv_Invoice( $invoice );
826
827
    // Ensure that we have an invoice.
828
    if ( 0 == $invoice->get_id() ) {
829
        return;
830
    }
831
832
    // Get the invoice meta.
833
    $meta = getpaid_get_invoice_meta( $invoice );
834
835
    // Display the meta.
836
    wpinv_get_template( 'invoice/invoice-meta.php', compact( 'invoice', 'meta' ) );
837
838
}
839
add_action( 'getpaid_invoice_details_right', 'getpaid_invoice_meta', 10 );
840
841
/**
842
 * Retrieves the address markup to use on Invoices.
843
 * 
844
 * @since 1.0.13
845
 * @see `wpinv_get_full_address_format`
846
 * @see `wpinv_get_invoice_address_replacements`
847
 * @param array $billing_details customer's billing details
848
 * @param  string $separator How to separate address lines.
849
 * @return string
850
 */
851
function wpinv_get_invoice_address_markup( $billing_details, $separator = '<br/>' ) {
852
853
    // Retrieve the address markup...
854
    $country= empty( $billing_details['country'] ) ? '' : $billing_details['country'];
855
    $format = wpinv_get_full_address_format( $country );
856
857
    // ... and the replacements.
858
    $replacements = wpinv_get_invoice_address_replacements( $billing_details );
859
860
    $formatted_address = str_ireplace( array_keys( $replacements ), $replacements, $format );
861
    
862
	// Remove unavailable tags.
863
    $formatted_address = preg_replace( "/\{\{\w+\}\}/", '', $formatted_address );
864
865
    // Clean up white space.
866
	$formatted_address = preg_replace( '/  +/', ' ', trim( $formatted_address ) );
867
    $formatted_address = preg_replace( '/\n\n+/', "\n", $formatted_address );
868
    
869
    // Break newlines apart and remove empty lines/trim commas and white space.
870
	$formatted_address = array_filter( array_map( 'wpinv_trim_formatted_address_line', explode( "\n", $formatted_address ) ) );
871
872
    // Add html breaks.
873
	$formatted_address = implode( $separator, $formatted_address );
874
875
	// We're done!
876
	return $formatted_address;
877
    
878
}
879
880
/**
881
 * Displays the billing address.
882
 * 
883
 * @param WPInv_Invoice $invoice
884
 */
885
function wpinv_display_to_address( $invoice = 0 ) {
886
    if ( ! empty( $invoice ) ) {
887
        wpinv_get_template( 'invoice/billing-address.php', compact( 'invoice' ) );
888
    }
889
}
890
add_action( 'getpaid_invoice_details_left', 'wpinv_display_to_address', 40 );
891
892
893
/**
894
 * Displays invoice line items.
895
 */
896
function wpinv_display_line_items( $invoice_id = 0 ) {
897
898
    // Prepare the invoice.
899
    $invoice = new WPInv_Invoice( $invoice_id );
900
901
    // Abort if there is no invoice.
902
    if ( 0 == $invoice->get_id() ) {
903
        return;
904
    }
905
906
    // Line item columns.
907
    $columns = getpaid_invoice_item_columns( $invoice );
908
    $columns = apply_filters( 'getpaid_invoice_line_items_table_columns', $columns, $invoice );
909
910
    wpinv_get_template( 'invoice/line-items.php', compact( 'invoice', 'columns' ) );
911
}
912
add_action( 'getpaid_invoice_line_items', 'wpinv_display_line_items', 10 );
913
914
/**
915
 * Displays invoice notices on invoices.
916
 */
917
function wpinv_display_invoice_notice() {
918
919
    $label  = wpinv_get_option( 'vat_invoice_notice_label' );
920
    $notice = wpinv_get_option( 'vat_invoice_notice' );
921
922
    if ( empty( $label ) && empty( $notice ) ) {
923
        return;
924
    }
925
926
    echo '<div class="mt-4 mb-4 wpinv-vat-notice">';
927
928
    if ( ! empty( $label ) ) {
929
        $label = sanitize_text_field( $label );
930
        echo "<h5>$label</h5>";
931
    }
932
933
    if ( ! empty( $notice ) ) {
934
        echo '<small class="form-text text-muted">' . wpautop( wptexturize( $notice ) ) . '</small>';
935
    }
936
937
    echo '</div>';
938
}
939
add_action( 'getpaid_invoice_line_items', 'wpinv_display_invoice_notice', 100 );
940
941
/**
942
 * @param WPInv_Invoice $invoice
943
 */
944
function wpinv_display_invoice_notes( $invoice ) {
945
946
    // Retrieve the notes.
947
    $notes = wpinv_get_invoice_notes( $invoice->get_id(), 'customer' );
948
949
    // Abort if we have non.
950
    if ( empty( $notes ) ) {
951
        return;
952
    }
953
954
    // Echo the note.
955
    echo '<div class="getpaid-invoice-notes-wrapper border position-relative w-100 mb-4 p-0">';
956
    echo '<h3 class="getpaid-invoice-notes-title text-dark bg-light border-bottom m-0 d-block">' . __( 'Notes', 'invoicing' ) .'</h3>';
957
    echo '<ul class="getpaid-invoice-notes mt-4 p-0">';
958
959
    foreach( $notes as $note ) {
960
        wpinv_get_invoice_note_line_item( $note );
961
    }
962
963
    echo '</ul>';
964
    echo '</div>';
965
}
966
add_action( 'getpaid_invoice_line_items', 'wpinv_display_invoice_notes', 60 );
967
968
/**
969
 * Loads scripts on our invoice templates.
970
 */
971
function wpinv_display_style() {
972
973
    // Make sure that all scripts have been loaded.
974
    if ( ! did_action( 'wp_enqueue_scripts' ) ) {
975
        do_action( 'wp_enqueue_scripts' );
976
    }
977
978
    // Register the invoices style.
979
    wp_register_style( 'wpinv-single-style', WPINV_PLUGIN_URL . 'assets/css/invoice.css', array(), filemtime( WPINV_PLUGIN_DIR . 'assets/css/invoice.css' ) );
980
981
    // Load required styles
982
    wp_print_styles( 'open-sans' );
983
    wp_print_styles( 'wpinv-single-style' );
984
    wp_print_styles( 'ayecode-ui' );
985
986
    // Maybe load custom css.
987
    $custom_css = wpinv_get_option( 'template_custom_css' );
988
989
    if ( isset( $custom_css ) && ! empty( $custom_css ) ) {
990
        $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

990
        $custom_css     = wp_kses( $custom_css, /** @scrutinizer ignore-type */ array( '\'', '\"' ) );
Loading history...
991
        $custom_css     = str_replace( '&gt;', '>', $custom_css );
992
        echo '<style type="text/css">';
993
        echo $custom_css;
994
        echo '</style>';
995
    }
996
997
}
998
add_action( 'wpinv_invoice_print_head', 'wpinv_display_style' );
999
add_action( 'wpinv_invalid_invoice_head', 'wpinv_display_style' );
1000
1001
1002
/**
1003
 * Displays the checkout page.
1004
 */
1005
function wpinv_checkout_form() {
1006
    global $wpi_checkout_id;
1007
1008
    // Retrieve the current invoice.
1009
    $invoice_id = getpaid_get_current_invoice_id();
1010
1011
    if ( empty( $invoice_id ) ) {
1012
1013
        return aui()->alert(
1014
            array(
1015
                'type'    => 'warning',
1016
                'content' => __( 'Invalid invoice', 'invoicing' ),
1017
            )
1018
        );
1019
1020
    }
1021
1022
    // Can the user view this invoice?
1023
    if ( ! wpinv_user_can_view_invoice( $invoice_id ) ) {
1024
1025
        return aui()->alert(
1026
            array(
1027
                'type'    => 'warning',
1028
                'content' => __( 'You are not allowed to view this invoice', 'invoicing' ),
1029
            )
1030
        );
1031
1032
    }
1033
1034
    // Ensure that it is not yet paid for.
1035
    $invoice = new WPInv_Invoice( $invoice_id );
1036
1037
    // Maybe mark it as viewed.
1038
    getpaid_maybe_mark_invoice_as_viewed( $invoice );
1039
1040
    if ( $invoice->is_paid() ) {
1041
1042
        return aui()->alert(
1043
            array(
1044
                'type'    => 'success',
1045
                'content' => __( 'This invoice has already been paid.', 'invoicing' ),
1046
            )
1047
        );
1048
1049
    }
1050
1051
    // Set the global invoice id.
1052
    $wpi_checkout_id = $invoice_id;
1053
1054
    // We'll display this invoice via the default form.
1055
    $form = new GetPaid_Payment_Form( wpinv_get_default_payment_form() );
1056
1057
    if ( 0 == $form->get_id() ) {
1058
1059
        return aui()->alert(
1060
            array(
1061
                'type'    => 'warning',
1062
                'content' => __( 'Error loading the payment form', 'invoicing' ),
1063
            )
1064
        );
1065
1066
    }
1067
1068
    // Set the invoice.
1069
    $form->invoice = $invoice;
1070
    $form->set_items( $invoice->get_items() );
1071
1072
    // Generate the html.
1073
    return $form->get_html();
1074
1075
}
1076
1077
function wpinv_empty_cart_message() {
1078
	return apply_filters( 'wpinv_empty_cart_message', '<span class="wpinv_empty_cart">' . __( 'Your cart is empty.', 'invoicing' ) . '</span>' );
1079
}
1080
1081
/**
1082
 * Echoes the Empty Cart Message
1083
 *
1084
 * @since 1.0
1085
 * @return void
1086
 */
1087
function wpinv_empty_checkout_cart() {
1088
    echo aui()->alert(
1089
        array(
1090
            'type'    => 'warning',
1091
            'content' => wpinv_empty_cart_message(),
1092
        )
1093
    );
1094
}
1095
add_action( 'wpinv_cart_empty', 'wpinv_empty_checkout_cart' );
1096
1097
function wpinv_receipt_billing_address( $invoice_id = 0 ) {
1098
    $invoice = wpinv_get_invoice( $invoice_id );
1099
1100
    if ( empty( $invoice ) ) {
1101
        return NULL;
1102
    }
1103
1104
    $billing_details = $invoice->get_user_info();
1105
    $address_row = wpinv_get_invoice_address_markup( $billing_details );
1106
1107
    ob_start();
1108
    ?>
1109
    <table class="table table-bordered table-sm wpi-billing-details">
1110
        <tbody>
1111
            <tr class="wpi-receipt-name">
1112
                <th class="text-left"><?php _e( 'Name', 'invoicing' ); ?></th>
1113
                <td><?php echo esc_html( trim( $billing_details['first_name'] . ' ' . $billing_details['last_name'] ) ) ;?></td>
1114
            </tr>
1115
            <tr class="wpi-receipt-email">
1116
                <th class="text-left"><?php _e( 'Email', 'invoicing' ); ?></th>
1117
                <td><?php echo $billing_details['email'] ;?></td>
1118
            </tr>
1119
            <tr class="wpi-receipt-address">
1120
                <th class="text-left"><?php _e( 'Address', 'invoicing' ); ?></th>
1121
                <td><?php echo $address_row ;?></td>
1122
            </tr>
1123
            <?php if ( $billing_details['phone'] ) { ?>
1124
            <tr class="wpi-receipt-phone">
1125
                <th class="text-left"><?php _e( 'Phone', 'invoicing' ); ?></th>
1126
                <td><?php echo esc_html( $billing_details['phone'] ) ;?></td>
1127
            </tr>
1128
            <?php } ?>
1129
        </tbody>
1130
    </table>
1131
    <?php
1132
    $output = ob_get_clean();
1133
    
1134
    $output = apply_filters( 'wpinv_receipt_billing_address', $output, $invoice_id );
1135
1136
    echo $output;
1137
}
1138
1139
/**
1140
 * Filters the receipt page.
1141
 */
1142
function wpinv_filter_success_page_content( $content ) {
1143
1144
    // Ensure this is our page.
1145
    if ( isset( $_GET['payment-confirm'] ) && wpinv_is_success_page() ) {
1146
1147
        $gateway = sanitize_text_field( $_GET['payment-confirm'] );
1148
        return apply_filters( "wpinv_payment_confirm_$gateway", $content );
1149
1150
    }
1151
1152
    return $content;
1153
}
1154
add_filter( 'the_content', 'wpinv_filter_success_page_content', 99999 );
1155
1156
function wpinv_invoice_link( $invoice_id ) {
1157
    $invoice = wpinv_get_invoice( $invoice_id );
1158
1159
    if ( empty( $invoice ) ) {
1160
        return NULL;
1161
    }
1162
1163
    $invoice_link = '<a href="' . esc_url( $invoice->get_view_url() ) . '">' . $invoice->get_number() . '</a>';
1164
1165
    return apply_filters( 'wpinv_get_invoice_link', $invoice_link, $invoice );
1166
}
1167
1168
function wpinv_cart_total_label( $label, $invoice ) {
1169
    if ( empty( $invoice ) ) {
1170
        return $label;
1171
    }
1172
1173
    $prefix_label = '';
1174
    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...
1175
        $prefix_label   = '<span class="label label-primary label-recurring">' . __( 'Recurring Payment', 'invoicing' ) . '</span> ' . wpinv_subscription_payment_desc( $invoice );
1176
    } else if ( $invoice->is_renewal() ) {
1177
        $prefix_label   = '<span class="label label-primary label-renewal">' . __( 'Renewal Payment', 'invoicing' ) . '</span> ';        
1178
    }
1179
1180
    if ( $prefix_label != '' ) {
1181
        $label  = '<span class="wpinv-cart-sub-desc">' . $prefix_label . '</span> ' . $label;
1182
    }
1183
1184
    return $label;
1185
}
1186
add_filter( 'wpinv_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
1187
add_filter( 'wpinv_email_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
1188
add_filter( 'wpinv_print_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
1189
1190
function wpinv_get_invoice_note_line_item( $note, $echo = true ) {
1191
    if ( empty( $note ) ) {
1192
        return NULL;
1193
    }
1194
1195
    if ( is_int( $note ) ) {
1196
        $note = get_comment( $note );
1197
    }
1198
1199
    if ( !( is_object( $note ) && is_a( $note, 'WP_Comment' ) ) ) {
1200
        return NULL;
1201
    }
1202
1203
    $note_classes   = array( 'note' );
1204
    $note_classes[] = get_comment_meta( $note->comment_ID, '_wpi_customer_note', true ) ? 'customer-note' : '';
1205
    $note_classes[] = $note->comment_author === 'System' ? 'system-note' : '';
1206
    $note_classes   = apply_filters( 'wpinv_invoice_note_class', array_filter( $note_classes ), $note );
1207
    $note_classes   = !empty( $note_classes ) ? implode( ' ', $note_classes ) : '';
1208
1209
    ob_start();
1210
    ?>
1211
    <li rel="<?php echo absint( $note->comment_ID ) ; ?>" class="<?php echo esc_attr( $note_classes ); ?> mt-4 pl-3 pr-3">
1212
        <div class="note_content bg-light border position-relative p-4">
1213
1214
            <?php echo wpautop( wptexturize( wp_kses_post( $note->comment_content ) ) ); ?>
1215
1216
            <?php if ( ! is_admin() ) : ?>
1217
                <em class="meta position-absolute form-text">
1218
                    <?php
1219
                        printf(
1220
                            __( '%1$s - %2$s at %3$s', 'invoicing' ),
1221
                            $note->comment_author,
1222
                            getpaid_format_date_value( $note->comment_date ),
1223
                            date_i18n( get_option( 'time_format' ), strtotime( $note->comment_date ) )
0 ignored issues
show
Bug introduced by
It seems like get_option('time_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

1223
                            date_i18n( /** @scrutinizer ignore-type */ get_option( 'time_format' ), strtotime( $note->comment_date ) )
Loading history...
1224
                        );
1225
                    ?>
1226
                </em>
1227
            <?php endif; ?>
1228
1229
        </div>
1230
1231
        <?php if ( is_admin() ) : ?>
1232
1233
            <p class="meta px-4 py-2">
1234
                <abbr class="exact-date" title="<?php echo esc_attr( $note->comment_date ); ?>">
1235
                    <?php
1236
                        printf(
1237
                            __( '%1$s - %2$s at %3$s', 'invoicing' ),
1238
                            $note->comment_author,
1239
                            getpaid_format_date_value( $note->comment_date ),
1240
                            date_i18n( get_option( 'time_format' ), strtotime( $note->comment_date ) )
1241
                        );
1242
                    ?>
1243
                </abbr>&nbsp;&nbsp;
1244
                <?php if ( $note->comment_author !== 'System' && wpinv_current_user_can_manage_invoicing() ) { ?>
1245
                    <a href="#" class="delete_note"><?php _e( 'Delete note', 'invoicing' ); ?></a>
1246
                <?php } ?>
1247
            </p>
1248
1249
        <?php endif; ?>
1250
        
1251
    </li>
1252
    <?php
1253
    $note_content = ob_get_clean();
1254
    $note_content = apply_filters( 'wpinv_get_invoice_note_line_item', $note_content, $note, $echo );
1255
1256
    if ( $echo ) {
1257
        echo $note_content;
1258
    } else {
1259
        return $note_content;
1260
    }
1261
}
1262
1263
function wpinv_invalid_invoice_content() {
1264
    global $post;
1265
1266
    $invoice = wpinv_get_invoice( $post->ID );
1267
1268
    $error = __( 'This invoice is only viewable by clicking on the invoice link that was sent to you via email.', 'invoicing' );
1269
    if ( !empty( $invoice->get_id() ) && $invoice->has_status( array_keys( wpinv_get_invoice_statuses() ) ) ) {
1270
        if ( is_user_logged_in() ) {
1271
            if ( wpinv_require_login_to_checkout() ) {
1272
                if ( isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
1273
                    $error = __( 'You are not allowed to view this invoice.', 'invoicing' );
1274
                }
1275
            }
1276
        } else {
1277
            if ( wpinv_require_login_to_checkout() ) {
1278
                if ( isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
1279
                    $error = __( 'You must be logged in to view this invoice.', 'invoicing' );
1280
                }
1281
            }
1282
        }
1283
    } else {
1284
        $error = __( 'This invoice is deleted or does not exist.', 'invoicing' );
1285
    }
1286
    ?>
1287
    <div class="row wpinv-row-invalid">
1288
        <div class="col-md-6 col-md-offset-3 wpinv-message error">
1289
            <h3><?php _e( 'Access Denied', 'invoicing' ); ?></h3>
1290
            <p class="wpinv-msg-text"><?php echo $error; ?></p>
1291
        </div>
1292
    </div>
1293
    <?php
1294
}
1295
add_action( 'wpinv_invalid_invoice_content', 'wpinv_invalid_invoice_content' );
1296
1297
/**
1298
 * Function to get privacy policy text.
1299
 *
1300
 * @since 1.0.13
1301
 * @return string
1302
 */
1303
function wpinv_get_policy_text() {
1304
    $privacy_page_id = get_option( 'wp_page_for_privacy_policy', 0 );
1305
1306
    $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]' ));
1307
1308
    if(!$privacy_page_id){
1309
        $privacy_page_id = wpinv_get_option( 'privacy_page', 0 );
1310
    }
1311
1312
    $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

1312
    $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...
1313
1314
    $find_replace = array(
1315
        '[wpinv_privacy_policy]' => $privacy_link,
1316
    );
1317
1318
    $privacy_text = str_replace( array_keys( $find_replace ), array_values( $find_replace ), $text );
1319
1320
    return wp_kses_post(wpautop($privacy_text));
1321
}
1322
1323
function wpinv_oxygen_fix_conflict() {
1324
    global $ct_ignore_post_types;
1325
1326
    if ( ! is_array( $ct_ignore_post_types ) ) {
1327
        $ct_ignore_post_types = array();
1328
    }
1329
1330
    $post_types = array( 'wpi_discount', 'wpi_invoice', 'wpi_item' );
1331
1332
    foreach ( $post_types as $post_type ) {
1333
        $ct_ignore_post_types[] = $post_type;
1334
1335
        // Ignore post type
1336
        add_filter( 'pre_option_oxygen_vsb_ignore_post_type_' . $post_type, '__return_true', 999 );
1337
    }
1338
1339
    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

1339
    /** @scrutinizer ignore-call */ 
1340
    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...
1340
    add_filter( 'template_include', 'wpinv_template', 999, 1 );
1341
}
1342
1343
/**
1344
 * Helper function to display a payment form on the frontend.
1345
 * 
1346
 * @param GetPaid_Payment_Form $form
1347
 */
1348
function getpaid_display_payment_form( $form ) {
1349
1350
    if ( is_numeric( $form ) ) {
0 ignored issues
show
introduced by
The condition is_numeric($form) is always false.
Loading history...
1351
        $form = new GetPaid_Payment_Form( $form );
1352
    }
1353
1354
    $form->display();
1355
1356
}
1357
1358
/**
1359
 * Helper function to display a item payment form on the frontend.
1360
 */
1361
function getpaid_display_item_payment_form( $items ) {
1362
1363
    $form = new GetPaid_Payment_Form( wpinv_get_default_payment_form() );
1364
    $form->set_items( $items );
1365
1366
    if ( 0 == count( $form->get_items() ) ) {
1367
        echo aui()->alert(
1368
			array(
1369
				'type'    => 'warning',
1370
				'content' => __( 'No published items found', 'invoicing' ),
1371
			)
1372
        );
1373
        return;
1374
    }
1375
1376
    $form->display();
1377
}
1378
1379
/**
1380
 * Helper function to display an invoice payment form on the frontend.
1381
 */
1382
function getpaid_display_invoice_payment_form( $invoice_id ) {
1383
1384
    $invoice = wpinv_get_invoice( $invoice_id );
1385
1386
    if ( empty( $invoice ) ) {
1387
		echo aui()->alert(
1388
			array(
1389
				'type'    => 'warning',
1390
				'content' => __( 'Invoice not found', 'invoicing' ),
1391
			)
1392
        );
1393
        return;
1394
    }
1395
1396
    if ( $invoice->is_paid() ) {
1397
		echo aui()->alert(
1398
			array(
1399
				'type'    => 'warning',
1400
				'content' => __( 'Invoice has already been paid', 'invoicing' ),
1401
			)
1402
        );
1403
        return;
1404
    }
1405
1406
    $form = new GetPaid_Payment_Form( wpinv_get_default_payment_form() );
1407
    $form->set_items( $invoice->get_items() );
1408
1409
    $form->display();
1410
}
1411
1412
/**
1413
 * Helper function to convert item string to array.
1414
 */
1415
function getpaid_convert_items_to_array( $items ) {
1416
    $items    = array_filter( array_map( 'trim', explode( ',', $items ) ) );
1417
    $prepared = array();
1418
1419
    foreach ( $items as $item ) {
1420
        $data = array_map( 'trim', explode( '|', $item ) );
1421
1422
        if ( empty( $data[0] ) || ! is_numeric( $data[0] ) ) {
1423
            continue;
1424
        }
1425
1426
        $quantity = 1;
1427
        if ( isset( $data[1] ) && is_numeric( $data[1] ) ) {
1428
            $quantity = (int) $data[1];
1429
        }
1430
1431
        $prepared[ $data[0] ] = $quantity;
1432
1433
    }
1434
1435
    return $prepared;
1436
}
1437
1438
/**
1439
 * Helper function to convert item array to string.
1440
 */
1441
function getpaid_convert_items_to_string( $items ) {
1442
    $prepared = array();
1443
1444
    foreach ( $items as $item => $quantity ) {
1445
        $prepared[] = "$item|$quantity";
1446
    }
1447
    return implode( ',', $prepared );
1448
}
1449
1450
/**
1451
 * Helper function to display a payment item.
1452
 * 
1453
 * Provide a label and one of $form, $items or $invoice.
1454
 */
1455
function getpaid_get_payment_button( $label, $form = null, $items = null, $invoice = null ) {
1456
    $label = sanitize_text_field( $label );
1457
    $nonce = wp_create_nonce('getpaid_ajax_form');
1458
1459
    if ( ! empty( $form ) ) {
1460
        $form  = esc_attr( $form );
1461
        return "<button class='btn btn-primary getpaid-payment-button' type='button' data-nonce='$nonce' data-form='$form'>$label</button>"; 
1462
    }
1463
	
1464
	if ( ! empty( $items ) ) {
1465
        $items  = esc_attr( $items );
1466
        return "<button class='btn btn-primary getpaid-payment-button' type='button' data-nonce='$nonce' data-item='$items'>$label</button>"; 
1467
    }
1468
    
1469
    if ( ! empty( $invoice ) ) {
1470
        $invoice  = esc_attr( $invoice );
1471
        return "<button class='btn btn-primary getpaid-payment-button' type='button' data-nonce='$nonce' data-invoice='$invoice'>$label</button>"; 
1472
    }
1473
1474
}
1475
1476
/**
1477
 * Display invoice description before line items.
1478
 *
1479
 * @param WPInv_Invoice $invoice
1480
 */
1481
function getpaid_the_invoice_description( $invoice ) {
1482
    $description = $invoice->get_description();
1483
1484
    if ( empty( $description ) ) {
1485
        return;
1486
    }
1487
1488
    $description = wp_kses_post( $description );
1489
    echo "<small class='getpaid-invoice-description text-dark p-2 form-text'><em>$description</em></small>";
1490
}
1491
add_action( 'getpaid_invoice_line_items', 'getpaid_the_invoice_description', 100 );
1492
1493
/**
1494
 * Render element on a form.
1495
 *
1496
 * @param array $element
1497
 * @param GetPaid_Payment_Form $form
1498
 */
1499
function getpaid_payment_form_element( $element, $form ) {
1500
1501
    // Set up the args.
1502
    $element_type    = trim( $element['type'] );
1503
    $element['form'] = $form;
1504
    extract( $element );
1505
1506
    // Try to locate the appropriate template.
1507
    $located = wpinv_locate_template( "payment-forms/elements/$element_type.php" );
1508
    
1509
    // Abort if this is not our element.
1510
    if ( empty( $located ) || ! file_exists( $located ) ) {
1511
        return;
1512
    }
1513
1514
    // Generate the class and id of the element.
1515
    $wrapper_class = 'getpaid-payment-form-element-' . trim( esc_attr( $element_type ) );
1516
    $id            = isset( $id ) ? $id : uniqid( 'gp' );
1517
1518
    // Echo the opening wrapper.
1519
    echo "<div class='getpaid-payment-form-element $wrapper_class'>";
1520
1521
    // Fires before displaying a given element type's content.
1522
    do_action( "getpaid_before_payment_form_{$element_type}_element", $element, $form );
1523
1524
    // Include the template for the element.
1525
    include $located;
1526
1527
    // Fires after displaying a given element type's content.
1528
    do_action( "getpaid_payment_form_{$element_type}_element", $element, $form );
1529
1530
    // Echo the closing wrapper.
1531
    echo '</div>';
1532
}
1533
add_action( 'getpaid_payment_form_element', 'getpaid_payment_form_element', 10, 2 );
1534
1535
/**
1536
 * Render an element's edit page.
1537
 *
1538
 * @param WP_Post $post
1539
 */
1540
function getpaid_payment_form_edit_element_template( $post ) {
1541
1542
    // Retrieve all elements.
1543
    $all_elements = wp_list_pluck( wpinv_get_data( 'payment-form-elements' ), 'type' );
1544
1545
    foreach ( $all_elements as $element ) {
1546
1547
        // Try to locate the appropriate template.
1548
        $element = sanitize_key( $element );
1549
        $located = wpinv_locate_template( "payment-forms-admin/edit/$element.php" );
1550
1551
        // Continue if this is not our element.
1552
        if ( empty( $located ) || ! file_exists( $located ) ) {
1553
            continue;
1554
        }
1555
1556
        // Include the template for the element.
1557
        echo "<div v-if=\"active_form_element.type=='$element'\">";
1558
        include $located;
1559
        echo '</div>';
1560
    }
1561
1562
}
1563
add_action( 'getpaid_payment_form_edit_element_template', 'getpaid_payment_form_edit_element_template' );
1564
1565
/**
1566
 * Render an element's preview.
1567
 *
1568
 */
1569
function getpaid_payment_form_render_element_preview_template() {
1570
1571
    // Retrieve all elements.
1572
    $all_elements = wp_list_pluck( wpinv_get_data( 'payment-form-elements' ), 'type' );
1573
1574
    foreach ( $all_elements as $element ) {
1575
1576
        // Try to locate the appropriate template.
1577
        $element = sanitize_key( $element );
1578
        $located = wpinv_locate_template( "payment-forms-admin/previews/$element.php" );
1579
1580
        // Continue if this is not our element.
1581
        if ( empty( $located ) || ! file_exists( $located ) ) {
1582
            continue;
1583
        }
1584
1585
        // Include the template for the element.
1586
        echo "<div v-if=\"form_element.type=='$element'\">";
1587
        include $located;
1588
        echo '</div>';
1589
    }
1590
1591
}
1592
add_action( 'wpinv_payment_form_render_element_template', 'getpaid_payment_form_render_element_preview_template' );
1593
1594
/**
1595
 * Shows a list of gateways that support recurring payments.
1596
 */
1597
function wpinv_get_recurring_gateways_text() {
1598
    $gateways = array();
1599
1600
    foreach ( wpinv_get_payment_gateways() as $key => $gateway ) {
1601
        if ( wpinv_gateway_support_subscription( $key ) ) {
1602
            $gateways[] = sanitize_text_field( $gateway['admin_label'] );
1603
        }
1604
    }
1605
1606
    if ( empty( $gateways ) ) {
1607
        return "<span class='form-text text-danger'>" . __( 'No active gateways support subscription payments.', 'invoicing' ) ."</span>";
1608
    }
1609
1610
    return "<span class='form-text text-muted'>" . wp_sprintf( __( 'Subscription payments only supported by: %s', 'invoicing' ), implode( ', ', $gateways ) ) ."</span>";
1611
1612
}
1613
1614
/**
1615
 * Returns the template.
1616
 * 
1617
 * @return GetPaid_Template
1618
 */
1619
function getpaid_template() {
1620
    return getpaid()->get( 'template' );
1621
}
1622
1623
/**
1624
 * Displays pagination links.
1625
 * 
1626
 * @param array args
0 ignored issues
show
Bug introduced by
The type args was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
1627
 * @return string
1628
 */
1629
function getpaid_paginate_links( $args ) {
1630
1631
    $args['type']     = 'array';
1632
    $args['mid_size'] = 1;
1633
    $pages        = paginate_links( $args );
1634
1635
    if ( ! is_array( $pages ) ) {
1636
        return '';
1637
    }
1638
1639
    $_pages = array();
1640
    foreach ( $pages as $page ) {
1641
        $_pages[] = str_replace( 'page-numbers', 'page-link text-decoration-none', $page );
1642
    }
1643
1644
    $links  = "<nav>\n\t<ul class='pagination justify-content-end m-0'>\n\t\t<li class='page-item'>";
1645
    $links .= join( "</li>\n\t\t<li class='page-item'>", $_pages );
1646
    $links .= "</li>\n\t</ul>\n</nav>\n";
1647
1648
    return $links;
1649
}
1650