Passed
Pull Request — master (#444)
by Brian
10:32
created

getpaid_submit_field()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 3
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_year_dropdown( $name = 'year', $selected = 0, $years_before = 5, $years_after = 0 ) {
298
    $current     = date( 'Y' );
299
    $start_year  = $current - absint( $years_before );
300
    $end_year    = $current + absint( $years_after );
301
    $selected    = empty( $selected ) ? date( 'Y' ) : $selected;
302
    $options     = array();
303
304
    while ( $start_year <= $end_year ) {
305
        $options[ absint( $start_year ) ] = $start_year;
306
        $start_year++;
307
    }
308
309
    $output = wpinv_html_select( array(
310
        'name'             => $name,
311
        'selected'         => $selected,
312
        'options'          => $options,
313
        'show_option_all'  => false,
314
        'show_option_none' => false
315
    ) );
316
317
    return $output;
318
}
319
320
function wpinv_html_month_dropdown( $name = 'month', $selected = 0 ) {
321
322
    $options = array(
323
        '1'  => __( 'January', 'invoicing' ),
324
        '2'  => __( 'February', 'invoicing' ),
325
        '3'  => __( 'March', 'invoicing' ),
326
        '4'  => __( 'April', 'invoicing' ),
327
        '5'  => __( 'May', 'invoicing' ),
328
        '6'  => __( 'June', 'invoicing' ),
329
        '7'  => __( 'July', 'invoicing' ),
330
        '8'  => __( 'August', 'invoicing' ),
331
        '9'  => __( 'September', 'invoicing' ),
332
        '10' => __( 'October', 'invoicing' ),
333
        '11' => __( 'November', 'invoicing' ),
334
        '12' => __( 'December', 'invoicing' ),
335
    );
336
337
    // If no month is selected, default to the current month
338
    $selected = empty( $selected ) ? date( 'n' ) : $selected;
339
340
    $output = wpinv_html_select( array(
341
        'name'             => $name,
342
        'selected'         => $selected,
343
        'options'          => $options,
344
        'show_option_all'  => false,
345
        'show_option_none' => false
346
    ) );
347
348
    return $output;
349
}
350
351
function wpinv_html_select( $args = array() ) {
352
    $defaults = array(
353
        'options'          => array(),
354
        'name'             => null,
355
        'class'            => '',
356
        'id'               => '',
357
        'selected'         => 0,
358
        'placeholder'      => null,
359
        'multiple'         => false,
360
        'show_option_all'  => _x( 'All', 'all dropdown items', 'invoicing' ),
361
        'show_option_none' => _x( 'None', 'no dropdown items', 'invoicing' ),
362
        'data'             => array(),
363
        'onchange'         => null,
364
        'required'         => false,
365
        'disabled'         => false,
366
        'readonly'         => false,
367
    );
368
369
    $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['type'] is assigned to $type in includes/admin/class-getpaid-post-types-admin.php on line 585
  1. Read from $_GET, and $_GET['type'] is assigned to $type
    in includes/admin/class-getpaid-post-types-admin.php on line 585
  2. wpinv_html_select() is called
    in includes/admin/class-getpaid-post-types-admin.php on line 589
  3. Enters via parameter $args
    in includes/wpinv-template-functions.php on line 351
  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 529
  1. Read from $_GET, and $_GET['vat_rule'] is assigned to $vat_rule
    in includes/admin/class-getpaid-post-types-admin.php on line 529
  2. wpinv_html_select() is called
    in includes/admin/class-getpaid-post-types-admin.php on line 534
  3. Enters via parameter $args
    in includes/wpinv-template-functions.php on line 351
  3. Path: Read from $_GET, and $_GET['vat_class'] is assigned to $vat_class in includes/admin/class-getpaid-post-types-admin.php on line 560
  1. Read from $_GET, and $_GET['vat_class'] is assigned to $vat_class
    in includes/admin/class-getpaid-post-types-admin.php on line 560
  2. wpinv_html_select() is called
    in includes/admin/class-getpaid-post-types-admin.php on line 564
  3. Enters via parameter $args
    in includes/wpinv-template-functions.php on line 351

Used in variable context

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

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

1179
                            date_i18n( /** @scrutinizer ignore-type */ get_option( 'time_format' ), strtotime( $note->comment_date ) )
Loading history...
1180
                        );
1181
                    ?>
1182
                </em>
1183
            <?php endif; ?>
1184
1185
        </div>
1186
1187
        <?php if ( is_admin() ) : ?>
1188
1189
            <p class="meta px-4 py-2">
1190
                <abbr class="exact-date" title="<?php echo esc_attr( $note->comment_date ); ?>">
1191
                    <?php
1192
                        printf(
1193
                            __( '%1$s - %2$s at %3$s', 'invoicing' ),
1194
                            $note->comment_author,
1195
                            getpaid_format_date_value( $note->comment_date ),
1196
                            date_i18n( get_option( 'time_format' ), strtotime( $note->comment_date ) )
1197
                        );
1198
                    ?>
1199
                </abbr>&nbsp;&nbsp;
1200
                <?php if ( $note->comment_author !== 'System' && wpinv_current_user_can_manage_invoicing() ) { ?>
1201
                    <a href="#" class="delete_note"><?php _e( 'Delete note', 'invoicing' ); ?></a>
1202
                <?php } ?>
1203
            </p>
1204
1205
        <?php endif; ?>
1206
        
1207
    </li>
1208
    <?php
1209
    $note_content = ob_get_clean();
1210
    $note_content = apply_filters( 'wpinv_get_invoice_note_line_item', $note_content, $note, $echo );
1211
1212
    if ( $echo ) {
1213
        echo $note_content;
1214
    } else {
1215
        return $note_content;
1216
    }
1217
}
1218
1219
function wpinv_invalid_invoice_content() {
1220
    global $post;
1221
1222
    $invoice = wpinv_get_invoice( $post->ID );
1223
1224
    $error = __( 'This invoice is only viewable by clicking on the invoice link that was sent to you via email.', 'invoicing' );
1225
    if ( !empty( $invoice->get_id() ) && $invoice->has_status( array_keys( wpinv_get_invoice_statuses() ) ) ) {
1226
        if ( is_user_logged_in() ) {
1227
            if ( wpinv_require_login_to_checkout() ) {
1228
                if ( isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
1229
                    $error = __( 'You are not allowed to view this invoice.', 'invoicing' );
1230
                }
1231
            }
1232
        } else {
1233
            if ( wpinv_require_login_to_checkout() ) {
1234
                if ( isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
1235
                    $error = __( 'You must be logged in to view this invoice.', 'invoicing' );
1236
                }
1237
            }
1238
        }
1239
    } else {
1240
        $error = __( 'This invoice is deleted or does not exist.', 'invoicing' );
1241
    }
1242
    ?>
1243
    <div class="row wpinv-row-invalid">
1244
        <div class="col-md-6 col-md-offset-3 wpinv-message error">
1245
            <h3><?php _e( 'Access Denied', 'invoicing' ); ?></h3>
1246
            <p class="wpinv-msg-text"><?php echo $error; ?></p>
1247
        </div>
1248
    </div>
1249
    <?php
1250
}
1251
add_action( 'wpinv_invalid_invoice_content', 'wpinv_invalid_invoice_content' );
1252
1253
/**
1254
 * Function to get privacy policy text.
1255
 *
1256
 * @since 1.0.13
1257
 * @return string
1258
 */
1259
function wpinv_get_policy_text() {
1260
    $privacy_page_id = get_option( 'wp_page_for_privacy_policy', 0 );
1261
1262
    $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]' ));
1263
1264
    if(!$privacy_page_id){
1265
        $privacy_page_id = wpinv_get_option( 'privacy_page', 0 );
1266
    }
1267
1268
    $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

1268
    $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...
1269
1270
    $find_replace = array(
1271
        '[wpinv_privacy_policy]' => $privacy_link,
1272
    );
1273
1274
    $privacy_text = str_replace( array_keys( $find_replace ), array_values( $find_replace ), $text );
1275
1276
    return wp_kses_post(wpautop($privacy_text));
1277
}
1278
1279
function wpinv_oxygen_fix_conflict() {
1280
    global $ct_ignore_post_types;
1281
1282
    if ( ! is_array( $ct_ignore_post_types ) ) {
1283
        $ct_ignore_post_types = array();
1284
    }
1285
1286
    $post_types = array( 'wpi_discount', 'wpi_invoice', 'wpi_item' );
1287
1288
    foreach ( $post_types as $post_type ) {
1289
        $ct_ignore_post_types[] = $post_type;
1290
1291
        // Ignore post type
1292
        add_filter( 'pre_option_oxygen_vsb_ignore_post_type_' . $post_type, '__return_true', 999 );
1293
    }
1294
1295
    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

1295
    /** @scrutinizer ignore-call */ 
1296
    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...
1296
    add_filter( 'template_include', 'wpinv_template', 999, 1 );
1297
}
1298
1299
/**
1300
 * Helper function to display a payment form on the frontend.
1301
 * 
1302
 * @param GetPaid_Payment_Form $form
1303
 */
1304
function getpaid_display_payment_form( $form ) {
1305
1306
    if ( is_numeric( $form ) ) {
0 ignored issues
show
introduced by
The condition is_numeric($form) is always false.
Loading history...
1307
        $form = new GetPaid_Payment_Form( $form );
1308
    }
1309
1310
    $form->display();
1311
1312
}
1313
1314
/**
1315
 * Helper function to display a item payment form on the frontend.
1316
 */
1317
function getpaid_display_item_payment_form( $items ) {
1318
1319
    $form = new GetPaid_Payment_Form( wpinv_get_default_payment_form() );
1320
    $form->set_items( $items );
1321
1322
    if ( 0 == count( $form->get_items() ) ) {
1323
        echo aui()->alert(
1324
			array(
1325
				'type'    => 'warning',
1326
				'content' => __( 'No published items found', 'invoicing' ),
1327
			)
1328
        );
1329
        return;
1330
    }
1331
1332
    $form_items = esc_attr( getpaid_convert_items_to_string( $items ) );
1333
    $form_items = "<input type='hidden' name='getpaid-form-items' value='$form_items' />";
1334
    $form->display( $form_items );
1335
}
1336
1337
/**
1338
 * Helper function to display an invoice payment form on the frontend.
1339
 */
1340
function getpaid_display_invoice_payment_form( $invoice_id ) {
1341
1342
    $invoice = wpinv_get_invoice( $invoice_id );
1343
1344
    if ( empty( $invoice ) ) {
1345
		echo aui()->alert(
1346
			array(
1347
				'type'    => 'warning',
1348
				'content' => __( 'Invoice not found', 'invoicing' ),
1349
			)
1350
        );
1351
        return;
1352
    }
1353
1354
    if ( $invoice->is_paid() ) {
1355
		echo aui()->alert(
1356
			array(
1357
				'type'    => 'warning',
1358
				'content' => __( 'Invoice has already been paid', 'invoicing' ),
1359
			)
1360
        );
1361
        return;
1362
    }
1363
1364
    $form = new GetPaid_Payment_Form( wpinv_get_default_payment_form() );
1365
    $form->set_items( $invoice->get_items() );
1366
1367
    $form->display();
1368
}
1369
1370
/**
1371
 * Helper function to convert item string to array.
1372
 */
1373
function getpaid_convert_items_to_array( $items ) {
1374
    $items    = array_filter( array_map( 'trim', explode( ',', $items ) ) );
1375
    $prepared = array();
1376
1377
    foreach ( $items as $item ) {
1378
        $data = array_map( 'trim', explode( '|', $item ) );
1379
1380
        if ( empty( $data[0] ) || ! is_numeric( $data[0] ) ) {
1381
            continue;
1382
        }
1383
1384
        $quantity = 1;
1385
        if ( isset( $data[1] ) && is_numeric( $data[1] ) ) {
1386
            $quantity = (int) $data[1];
1387
        }
1388
1389
        $prepared[ $data[0] ] = $quantity;
1390
1391
    }
1392
1393
    return $prepared;
1394
}
1395
1396
/**
1397
 * Helper function to convert item array to string.
1398
 */
1399
function getpaid_convert_items_to_string( $items ) {
1400
    $prepared = array();
1401
1402
    foreach ( $items as $item => $quantity ) {
1403
        $prepared[] = "$item|$quantity";
1404
    }
1405
    return implode( ',', $prepared );
1406
}
1407
1408
/**
1409
 * Helper function to display a payment item.
1410
 * 
1411
 * Provide a label and one of $form, $items or $invoice.
1412
 */
1413
function getpaid_get_payment_button( $label, $form = null, $items = null, $invoice = null ) {
1414
    $label = sanitize_text_field( $label );
1415
1416
    if ( ! empty( $form ) ) {
1417
        $form  = esc_attr( $form );
1418
        return "<button class='btn btn-primary getpaid-payment-button' type='button' data-form='$form'>$label</button>"; 
1419
    }
1420
	
1421
	if ( ! empty( $items ) ) {
1422
        $items  = esc_attr( $items );
1423
        return "<button class='btn btn-primary getpaid-payment-button' type='button' data-item='$items'>$label</button>"; 
1424
    }
1425
    
1426
    if ( ! empty( $invoice ) ) {
1427
        $invoice  = esc_attr( $invoice );
1428
        return "<button class='btn btn-primary getpaid-payment-button' type='button' data-invoice='$invoice'>$label</button>"; 
1429
    }
1430
1431
}
1432
1433
/**
1434
 * Display invoice description before line items.
1435
 *
1436
 * @param WPInv_Invoice $invoice
1437
 */
1438
function getpaid_the_invoice_description( $invoice ) {
1439
    $description = $invoice->get_description();
1440
1441
    if ( empty( $description ) ) {
1442
        return;
1443
    }
1444
1445
    $description = wp_kses_post( $description );
1446
    echo "<small class='getpaid-invoice-description text-dark p-2 form-text'><em>$description</em></small>";
1447
}
1448
add_action( 'getpaid_invoice_line_items', 'getpaid_the_invoice_description', 100 );
1449
1450
/**
1451
 * Render element on a form.
1452
 *
1453
 * @param array $element
1454
 * @param GetPaid_Payment_Form $form
1455
 */
1456
function getpaid_payment_form_element( $element, $form ) {
1457
1458
    // Set up the args.
1459
    $element_type    = trim( $element['type'] );
1460
    $element['form'] = $form;
1461
    extract( $element );
1462
1463
    // Try to locate the appropriate template.
1464
    $located = wpinv_locate_template( "payment-forms/elements/$element_type.php" );
1465
    
1466
    // Abort if this is not our element.
1467
    if ( empty( $located ) || ! file_exists( $located ) ) {
1468
        return;
1469
    }
1470
1471
    // Generate the class and id of the element.
1472
    $wrapper_class = 'getpaid-payment-form-element-' . trim( esc_attr( $element_type ) );
1473
    $id            = isset( $id ) ? $id : uniqid( 'gp' );
1474
1475
    // Echo the opening wrapper.
1476
    echo "<div class='getpaid-payment-form-element $wrapper_class'>";
1477
1478
    // Fires before displaying a given element type's content.
1479
    do_action( "getpaid_before_payment_form_{$element_type}_element", $element, $form );
1480
1481
    // Include the template for the element.
1482
    include $located;
1483
1484
    // Fires after displaying a given element type's content.
1485
    do_action( "getpaid_payment_form_{$element_type}_element", $element, $form );
1486
1487
    // Echo the closing wrapper.
1488
    echo '</div>';
1489
}
1490
add_action( 'getpaid_payment_form_element', 'getpaid_payment_form_element', 10, 2 );
1491
1492
/**
1493
 * Render an element's edit page.
1494
 *
1495
 * @param WP_Post $post
1496
 */
1497
function getpaid_payment_form_edit_element_template( $post ) {
1498
1499
    // Retrieve all elements.
1500
    $all_elements = wp_list_pluck( wpinv_get_data( 'payment-form-elements' ), 'type' );
1501
1502
    foreach ( $all_elements as $element ) {
1503
1504
        // Try to locate the appropriate template.
1505
        $element = sanitize_key( $element );
1506
        $located = wpinv_locate_template( "payment-forms-admin/edit/$element.php" );
1507
1508
        // Continue if this is not our element.
1509
        if ( empty( $located ) || ! file_exists( $located ) ) {
1510
            continue;
1511
        }
1512
1513
        // Include the template for the element.
1514
        echo "<div v-if=\"active_form_element.type=='$element'\">";
1515
        include $located;
1516
        echo '</div>';
1517
    }
1518
1519
}
1520
add_action( 'getpaid_payment_form_edit_element_template', 'getpaid_payment_form_edit_element_template' );
1521
1522
/**
1523
 * Render an element's preview.
1524
 *
1525
 */
1526
function getpaid_payment_form_render_element_preview_template() {
1527
1528
    // Retrieve all elements.
1529
    $all_elements = wp_list_pluck( wpinv_get_data( 'payment-form-elements' ), 'type' );
1530
1531
    foreach ( $all_elements as $element ) {
1532
1533
        // Try to locate the appropriate template.
1534
        $element = sanitize_key( $element );
1535
        $located = wpinv_locate_template( "payment-forms-admin/previews/$element.php" );
1536
1537
        // Continue if this is not our element.
1538
        if ( empty( $located ) || ! file_exists( $located ) ) {
1539
            continue;
1540
        }
1541
1542
        // Include the template for the element.
1543
        echo "<div v-if=\"form_element.type=='$element'\">";
1544
        include $located;
1545
        echo '</div>';
1546
    }
1547
1548
}
1549
add_action( 'wpinv_payment_form_render_element_template', 'getpaid_payment_form_render_element_preview_template' );
1550
1551
/**
1552
 * Shows a list of gateways that support recurring payments.
1553
 */
1554
function wpinv_get_recurring_gateways_text() {
1555
    $gateways = array();
1556
1557
    foreach ( wpinv_get_payment_gateways() as $key => $gateway ) {
1558
        if ( wpinv_gateway_support_subscription( $key ) ) {
1559
            $gateways[] = sanitize_text_field( $gateway['admin_label'] );
1560
        }
1561
    }
1562
1563
    if ( empty( $gateways ) ) {
1564
        return "<span class='form-text text-danger'>" . __( 'No active gateways support subscription payments.', 'invoicing' ) ."</span>";
1565
    }
1566
1567
    return "<span class='form-text text-muted'>" . wp_sprintf( __( 'Subscription payments only supported by: %s', 'invoicing' ), implode( ', ', $gateways ) ) ."</span>";
1568
1569
}
1570
1571
/**
1572
 * Returns the template.
1573
 * 
1574
 * @return GetPaid_Template
1575
 */
1576
function getpaid_template() {
1577
    return getpaid()->get( 'template' );
1578
}
1579
1580
/**
1581
 * Displays pagination links.
1582
 * 
1583
 * @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...
1584
 * @return string
1585
 */
1586
function getpaid_paginate_links( $args ) {
1587
1588
    $args['type']     = 'array';
1589
    $args['mid_size'] = 1;
1590
    $pages        = paginate_links( $args );
1591
1592
    if ( ! is_array( $pages ) ) {
1593
        return '';
1594
    }
1595
1596
    $_pages = array();
1597
    foreach ( $pages as $page ) {
1598
        $_pages[] = str_replace( 'page-numbers', 'page-link text-decoration-none', $page );
1599
    }
1600
1601
    $links  = "<nav>\n\t<ul class='pagination justify-content-end m-0'>\n\t\t<li class='page-item'>";
1602
    $links .= join( "</li>\n\t\t<li class='page-item'>", $_pages );
1603
    $links .= "</li>\n\t</ul>\n</nav>\n";
1604
1605
    return $links;
1606
}
1607
1608
/**
1609
 * Displays the states select markup.
1610
 * 
1611
 * @param string country
1612
 * @param string state
0 ignored issues
show
Bug introduced by
The type state 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...
1613
 * @return string
1614
 */
1615
function getpaid_get_states_select_markup( $country, $state, $placeholder, $label, $help_text, $required = false, $wrapper_class = 'col-12', $field_name = 'wpinv_state' ) {
1616
1617
    $states = wpinv_get_country_states( $country );
1618
    $uniqid = uniqid( '_' );
1619
1620
    if ( ! empty( $states ) ) {
1621
1622
        return aui()->select( array(
1623
            'options'          => $states,
1624
            'name'             => esc_attr( $field_name ),
1625
            'id'               => sanitize_html_class( $field_name ) . $uniqid,
1626
            'value'            => sanitize_text_field( $state ),
1627
            'placeholder'      => $placeholder,
1628
            'required'         => $required,
1629
            'label'            => wp_kses_post( $label ),
1630
            'label_type'       => 'vertical',
1631
            'help_text'        => $help_text,
1632
            'class'            => 'getpaid-address-field wpinv_state',
1633
            'wrap_class'       => "$wrapper_class getpaid-address-field-wrapper__state",
1634
            'label_class'      => 'getpaid-address-field-label getpaid-address-field-label__state',
1635
        ));
1636
1637
    }
1638
1639
    return aui()->input(
1640
        array(
1641
            'name'        => esc_attr( $field_name ),
1642
            'id'          => sanitize_html_class( $field_name ) . $uniqid,
1643
            'placeholder' => $placeholder,
1644
            'required'    => $required,
1645
            'label'       => wp_kses_post( $label ),
1646
            'label_type'  => 'vertical',
1647
            'help_text'   => $help_text,
1648
            'value'       => sanitize_text_field( $state ),
1649
            'class'       => 'getpaid-address-field wpinv_state',
1650
            'wrap_class'  => "$wrapper_class getpaid-address-field-wrapper__state",
1651
            'label_class' => 'getpaid-address-field-label getpaid-address-field-label__state',
1652
        )
1653
    );
1654
1655
}
1656
1657
/**
1658
 * Retrieves an element's grid width.
1659
 * 
1660
 * @param array $element
1661
 * @return string
1662
 */
1663
function getpaid_get_form_element_grid_class( $element ) {
1664
1665
    $class = "col-12";
1666
    $width = empty( $element['grid_width'] ) ? 'full' : $element['grid_width'];
1667
1668
    if ( $width == 'half' ) {
1669
        $class = "col-12 col-md-6";
1670
    }
1671
1672
    if ( $width == 'third' ) {
1673
        $class = "col-12 col-md-4";
1674
    }
1675
1676
    return $class;
1677
}
1678