Passed
Push — master ( c0eaca...2632ed )
by Brian
04:50
created

wpinv_checkout_form()   B

Complexity

Conditions 11
Paths 9

Size

Total Lines 94
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 43
nc 9
nop 0
dl 0
loc 94
rs 7.3166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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, 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
    getpaid_template()->display_template( $template_name, $args, $template_path, $default_path );
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 615
  1. Read from $_GET, and $_GET['type'] is assigned to $type
    in includes/admin/class-getpaid-post-types-admin.php on line 615
  2. wpinv_html_select() is called
    in includes/admin/class-getpaid-post-types-admin.php on line 619
  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 565
  1. Read from $_GET, and $_GET['vat_rule'] is assigned to $vat_rule
    in includes/admin/class-getpaid-post-types-admin.php on line 565
  2. wpinv_html_select() is called
    in includes/admin/class-getpaid-post-types-admin.php on line 570
  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 591
  1. Read from $_GET, and $_GET['vat_class'] is assigned to $vat_class
    in includes/admin/class-getpaid-post-types-admin.php on line 591
  2. wpinv_html_select() is called
    in includes/admin/class-getpaid-post-types-admin.php on line 595
  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_textarea( $args = array() ) {
654
    $defaults = array(
655
        'name'        => 'textarea',
656
        'value'       => null,
657
        'label'       => null,
658
        'desc'        => null,
659
        'class'       => 'large-text',
660
        'disabled'    => false,
661
        'placeholder' => '',
662
    );
663
664
    $args = wp_parse_args( $args, $defaults );
665
666
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
667
    $disabled = '';
668
    if( $args['disabled'] ) {
669
        $disabled = ' disabled="disabled"';
670
    }
671
672
    $output = '<span id="wpinv-' . wpinv_sanitize_key( $args['name'] ) . '-wrap">';
673
    $output .= '<label class="wpinv-label" for="' . wpinv_sanitize_key( $args['name'] ) . '">' . esc_html( $args['label'] ) . '</label>';
674
    $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>';
675
676
    if ( ! empty( $args['desc'] ) ) {
677
        $output .= '<span class="wpinv-description">' . esc_html( $args['desc'] ) . '</span>';
678
    }
679
    $output .= '</span>';
680
681
    return $output;
682
}
683
684
function wpinv_html_ajax_user_search( $args = array() ) {
685
    $defaults = array(
686
        'name'        => 'user_id',
687
        'value'       => null,
688
        'placeholder' => __( 'Enter username', 'invoicing' ),
689
        'label'       => null,
690
        'desc'        => null,
691
        'class'       => '',
692
        'disabled'    => false,
693
        'autocomplete'=> 'off',
694
        'data'        => false
695
    );
696
697
    $args = wp_parse_args( $args, $defaults );
698
699
    $args['class'] = 'wpinv-ajax-user-search ' . $args['class'];
700
701
    $output  = '<span class="wpinv_user_search_wrap">';
702
        $output .= wpinv_html_text( $args );
703
        $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>';
704
    $output .= '</span>';
705
706
    return $output;
707
}
708
709
/**
710
 * Use our template to display invoices.
711
 * 
712
 * @param string $template the template that is currently being used.
713
 */
714
function wpinv_template( $template ) {
715
    global $post;
716
717
    if ( ! is_admin() && ( is_single() || is_404() ) && ! empty( $post->ID ) && getpaid_is_invoice_post_type( get_post_type( $post->ID ) ) ) {
718
719
        // If the user can view this invoice, display it.
720
        if ( wpinv_user_can_view_invoice( $post->ID ) ) {
721
722
            return wpinv_get_template_part( 'wpinv-invoice-print', false, false );
723
724
        // Else display an error message.
725
        } else {
726
727
            return wpinv_get_template_part( 'wpinv-invalid-access', false, false );
728
729
        }
730
731
    }
732
733
    return $template;
734
}
735
add_filter( 'template_include', 'wpinv_template', 10, 1 );
736
737
function wpinv_get_business_address() {
738
    $business_address   = wpinv_store_address();
739
    $business_address   = !empty( $business_address ) ? wpautop( wp_kses_post( $business_address ) ) : '';
740
    
741
    $business_address = $business_address ? '<div class="address">' . $business_address . '</div>' : '';
742
    
743
    return apply_filters( 'wpinv_get_business_address', $business_address );
744
}
745
746
/**
747
 * Displays the company address.
748
 */
749
function wpinv_display_from_address() {
750
    wpinv_get_template( 'invoice/company-address.php' );
751
}
752
add_action( 'getpaid_invoice_details_left', 'wpinv_display_from_address', 10 );
753
754
/**
755
 * Generates a watermark text for an invoice.
756
 * 
757
 * @param WPInv_Invoice $invoice
758
 * @return string
759
 */
760
function wpinv_watermark( $invoice ) {
761
    $watermark = wpinv_get_watermark( $invoice );
762
    return apply_filters( 'wpinv_get_watermark', $watermark, $invoice );
763
}
764
765
/**
766
 * Generates a watermark text for an invoice.
767
 * 
768
 * @param WPInv_Invoice $invoice
769
 * @return string
770
 */
771
function wpinv_get_watermark( $invoice ) {
772
    return $invoice->get_status_nicename();
773
}
774
775
/**
776
 * @deprecated
777
 */
778
function wpinv_display_invoice_details( $invoice ) {
779
    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...
780
}
781
782
/**
783
 * Displays invoice meta.
784
 */
785
function getpaid_invoice_meta( $invoice ) {
786
787
    $invoice = new WPInv_Invoice( $invoice );
788
789
    // Ensure that we have an invoice.
790
    if ( 0 == $invoice->get_id() ) {
791
        return;
792
    }
793
794
    // Get the invoice meta.
795
    $meta = getpaid_get_invoice_meta( $invoice );
796
797
    // Display the meta.
798
    wpinv_get_template( 'invoice/invoice-meta.php', compact( 'invoice', 'meta' ) );
799
800
}
801
add_action( 'getpaid_invoice_details_right', 'getpaid_invoice_meta', 10 );
802
803
/**
804
 * Retrieves the address markup to use on Invoices.
805
 * 
806
 * @since 1.0.13
807
 * @see `wpinv_get_full_address_format`
808
 * @see `wpinv_get_invoice_address_replacements`
809
 * @param array $billing_details customer's billing details
810
 * @param  string $separator How to separate address lines.
811
 * @return string
812
 */
813
function wpinv_get_invoice_address_markup( $billing_details, $separator = '<br/>' ) {
814
815
    // Retrieve the address markup...
816
    $country= empty( $billing_details['country'] ) ? '' : $billing_details['country'];
817
    $format = wpinv_get_full_address_format( $country );
818
819
    // ... and the replacements.
820
    $replacements = wpinv_get_invoice_address_replacements( $billing_details );
821
822
    $formatted_address = str_ireplace( array_keys( $replacements ), $replacements, $format );
823
    
824
	// Remove unavailable tags.
825
    $formatted_address = preg_replace( "/\{\{\w+\}\}/", '', $formatted_address );
826
827
    // Clean up white space.
828
	$formatted_address = preg_replace( '/  +/', ' ', trim( $formatted_address ) );
829
    $formatted_address = preg_replace( '/\n\n+/', "\n", $formatted_address );
830
    
831
    // Break newlines apart and remove empty lines/trim commas and white space.
832
	$formatted_address = array_filter( array_map( 'wpinv_trim_formatted_address_line', explode( "\n", $formatted_address ) ) );
833
834
    // Add html breaks.
835
	$formatted_address = implode( $separator, $formatted_address );
836
837
	// We're done!
838
	return $formatted_address;
839
    
840
}
841
842
/**
843
 * Displays the billing address.
844
 * 
845
 * @param WPInv_Invoice $invoice
846
 */
847
function wpinv_display_to_address( $invoice = 0 ) {
848
    if ( ! empty( $invoice ) ) {
849
        wpinv_get_template( 'invoice/billing-address.php', compact( 'invoice' ) );
850
    }
851
}
852
add_action( 'getpaid_invoice_details_left', 'wpinv_display_to_address', 40 );
853
854
855
/**
856
 * Displays invoice line items.
857
 */
858
function wpinv_display_line_items( $invoice_id = 0 ) {
859
860
    // Prepare the invoice.
861
    $invoice = new WPInv_Invoice( $invoice_id );
862
863
    // Abort if there is no invoice.
864
    if ( 0 == $invoice->get_id() ) {
865
        return;
866
    }
867
868
    // Line item columns.
869
    $columns = getpaid_invoice_item_columns( $invoice );
870
    $columns = apply_filters( 'getpaid_invoice_line_items_table_columns', $columns, $invoice );
871
872
    wpinv_get_template( 'invoice/line-items.php', compact( 'invoice', 'columns' ) );
873
}
874
add_action( 'getpaid_invoice_line_items', 'wpinv_display_line_items', 10 );
875
876
/**
877
 * Displays invoice notices on invoices.
878
 */
879
function wpinv_display_invoice_notice() {
880
881
    $label  = wpinv_get_option( 'vat_invoice_notice_label' );
882
    $notice = wpinv_get_option( 'vat_invoice_notice' );
883
884
    if ( empty( $label ) && empty( $notice ) ) {
885
        return;
886
    }
887
888
    echo '<div class="mt-4 mb-4 wpinv-vat-notice">';
889
890
    if ( ! empty( $label ) ) {
891
        $label = sanitize_text_field( $label );
892
        echo "<h5>$label</h5>";
893
    }
894
895
    if ( ! empty( $notice ) ) {
896
        echo '<small class="form-text text-muted">' . wpautop( wptexturize( $notice ) ) . '</small>';
897
    }
898
899
    echo '</div>';
900
}
901
add_action( 'getpaid_invoice_line_items', 'wpinv_display_invoice_notice', 100 );
902
903
/**
904
 * @param WPInv_Invoice $invoice
905
 */
906
function wpinv_display_invoice_notes( $invoice ) {
907
908
    // Retrieve the notes.
909
    $notes = wpinv_get_invoice_notes( $invoice->get_id(), 'customer' );
910
911
    // Abort if we have non.
912
    if ( empty( $notes ) ) {
913
        return;
914
    }
915
916
    // Echo the note.
917
    echo '<div class="getpaid-invoice-notes-wrapper position-relative my-4">';
918
    echo '<h2 class="getpaid-invoice-notes-title mb-1 p-0 h4">' . __( 'Notes', 'invoicing' ) .'</h2>';
919
    echo '<ul class="getpaid-invoice-notes text-break overflow-auto list-unstyled p-0 m-0">';
920
921
    foreach( $notes as $note ) {
922
        wpinv_get_invoice_note_line_item( $note );
923
    }
924
925
    echo '</ul>';
926
    echo '</div>';
927
}
928
add_action( 'getpaid_invoice_line_items', 'wpinv_display_invoice_notes', 60 );
929
930
/**
931
 * Loads scripts on our invoice templates.
932
 */
933
function wpinv_display_style() {
934
935
    // Make sure that all scripts have been loaded.
936
    if ( ! did_action( 'wp_enqueue_scripts' ) ) {
937
        do_action( 'wp_enqueue_scripts' );
938
    }
939
940
    // Register the invoices style.
941
    wp_register_style( 'wpinv-single-style', WPINV_PLUGIN_URL . 'assets/css/invoice.css', array(), filemtime( WPINV_PLUGIN_DIR . 'assets/css/invoice.css' ) );
942
943
    // Load required styles
944
    wp_print_styles( 'wpinv-single-style' );
945
    wp_print_styles( 'ayecode-ui' );
946
947
    // Maybe load custom css.
948
    $custom_css = wpinv_get_option( 'template_custom_css' );
949
950
    if ( isset( $custom_css ) && ! empty( $custom_css ) ) {
951
        $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

951
        $custom_css     = wp_kses( $custom_css, /** @scrutinizer ignore-type */ array( '\'', '\"' ) );
Loading history...
952
        $custom_css     = str_replace( '&gt;', '>', $custom_css );
953
        echo '<style type="text/css">';
954
        echo $custom_css;
955
        echo '</style>';
956
    }
957
958
}
959
add_action( 'wpinv_invoice_print_head', 'wpinv_display_style' );
960
add_action( 'wpinv_invalid_invoice_head', 'wpinv_display_style' );
961
962
963
/**
964
 * Displays the checkout page.
965
 */
966
function wpinv_checkout_form() {
967
    global $wpi_checkout_id;
968
969
    // Retrieve the current invoice.
970
    $invoice_id = getpaid_get_current_invoice_id();
971
972
    if ( empty( $invoice_id ) ) {
973
974
        return aui()->alert(
975
            array(
976
                'type'    => 'warning',
977
                'content' => __( 'Invalid invoice', 'invoicing' ),
978
            )
979
        );
980
981
    }
982
983
    // Can the user view this invoice?
984
    if ( ! wpinv_user_can_view_invoice( $invoice_id ) ) {
985
986
        return aui()->alert(
987
            array(
988
                'type'    => 'warning',
989
                'content' => __( 'You are not allowed to view this invoice', 'invoicing' ),
990
            )
991
        );
992
993
    }
994
995
    // Ensure that it is not yet paid for.
996
    $invoice = new WPInv_Invoice( $invoice_id );
997
998
    // Maybe mark it as viewed.
999
    getpaid_maybe_mark_invoice_as_viewed( $invoice );
1000
1001
    if ( $invoice->is_paid() ) {
1002
1003
        return aui()->alert(
1004
            array(
1005
                'type'    => 'success',
1006
                'content' => __( 'This invoice has already been paid.', 'invoicing' ),
1007
            )
1008
        );
1009
1010
    }
1011
1012
    // Set the global invoice id.
1013
    $wpi_checkout_id = $invoice_id;
1014
1015
    // Retrieve appropriate payment form.
1016
    $payment_form = new GetPaid_Payment_Form( $invoice->get_meta( 'force_payment_form' ) );
1017
    $payment_form = $payment_form->exists() ? $payment_form : new GetPaid_Payment_Form( wpinv_get_default_payment_form() );
1018
1019
    if ( ! $payment_form->exists() ) {
1020
1021
        return aui()->alert(
1022
            array(
1023
                'type'    => 'warning',
1024
                'content' => __( 'Error loading the payment form', 'invoicing' ),
1025
            )
1026
        );
1027
1028
    }
1029
1030
    // Set the invoice.
1031
    $payment_form->invoice = $invoice;
1032
1033
    if ( ! $payment_form->is_default() ) {
1034
1035
        $items    = array();
1036
        $item_ids = array();
1037
1038
        foreach ( $invoice->get_items() as $item ) {
1039
            if ( ! in_array( $item->get_id(), $item_ids ) ) {
1040
                $item_ids[] = $item->get_id();
1041
                $items[]    = $item;
1042
            }
1043
        }
1044
1045
        foreach ( $payment_form->get_items() as $item ) {
1046
            if ( ! in_array( $item->get_id(), $item_ids ) ) {
1047
                $item_ids[] = $item->get_id();
1048
                $items[]    = $item;
1049
            }
1050
        }
1051
1052
        $payment_form->set_items( $items );
1053
1054
    } else {
1055
        $payment_form->set_items( $invoice->get_items() );
1056
    }
1057
1058
    // Generate the html.
1059
    return $payment_form->get_html();
1060
1061
}
1062
1063
function wpinv_empty_cart_message() {
1064
	return apply_filters( 'wpinv_empty_cart_message', '<span class="wpinv_empty_cart">' . __( 'Your cart is empty.', 'invoicing' ) . '</span>' );
1065
}
1066
1067
/**
1068
 * Echoes the Empty Cart Message
1069
 *
1070
 * @since 1.0
1071
 * @return void
1072
 */
1073
function wpinv_empty_checkout_cart() {
1074
    echo aui()->alert(
1075
        array(
1076
            'type'    => 'warning',
1077
            'content' => wpinv_empty_cart_message(),
1078
        )
1079
    );
1080
}
1081
add_action( 'wpinv_cart_empty', 'wpinv_empty_checkout_cart' );
1082
1083
/**
1084
 * Filters the receipt page.
1085
 */
1086
function wpinv_filter_success_page_content( $content ) {
1087
1088
    // Ensure this is our page.
1089
    if ( isset( $_GET['payment-confirm'] ) && wpinv_is_success_page() ) {
1090
1091
        $gateway = sanitize_text_field( $_GET['payment-confirm'] );
1092
        return apply_filters( "wpinv_payment_confirm_$gateway", $content );
1093
1094
    }
1095
1096
    return $content;
1097
}
1098
add_filter( 'the_content', 'wpinv_filter_success_page_content', 99999 );
1099
1100
function wpinv_invoice_link( $invoice_id ) {
1101
    $invoice = wpinv_get_invoice( $invoice_id );
1102
1103
    if ( empty( $invoice ) ) {
1104
        return NULL;
1105
    }
1106
1107
    $invoice_link = '<a href="' . esc_url( $invoice->get_view_url() ) . '">' . $invoice->get_number() . '</a>';
1108
1109
    return apply_filters( 'wpinv_get_invoice_link', $invoice_link, $invoice );
1110
}
1111
1112
function wpinv_get_invoice_note_line_item( $note, $echo = true ) {
1113
    if ( empty( $note ) ) {
1114
        return NULL;
1115
    }
1116
1117
    if ( is_int( $note ) ) {
1118
        $note = get_comment( $note );
1119
    }
1120
1121
    if ( !( is_object( $note ) && is_a( $note, 'WP_Comment' ) ) ) {
1122
        return NULL;
1123
    }
1124
1125
    $note_classes   = array( 'note' );
1126
    $note_classes[] = get_comment_meta( $note->comment_ID, '_wpi_customer_note', true ) ? 'customer-note' : '';
0 ignored issues
show
Bug introduced by
$note->comment_ID of type string is incompatible with the type integer expected by parameter $comment_id of get_comment_meta(). ( Ignorable by Annotation )

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

1126
    $note_classes[] = get_comment_meta( /** @scrutinizer ignore-type */ $note->comment_ID, '_wpi_customer_note', true ) ? 'customer-note' : '';
Loading history...
1127
    $note_classes[] = $note->comment_author === 'System' ? 'system-note' : '';
1128
    $note_classes   = apply_filters( 'wpinv_invoice_note_class', array_filter( $note_classes ), $note );
1129
    $note_classes   = !empty( $note_classes ) ? implode( ' ', $note_classes ) : '';
1130
1131
    ob_start();
1132
    ?>
1133
    <li rel="<?php echo absint( $note->comment_ID ) ; ?>" class="<?php echo esc_attr( $note_classes ); ?> mb-2">
1134
        <div class="note_content">
1135
1136
            <?php echo wptexturize( wp_kses_post( $note->comment_content ) ); ?>
1137
1138
            <?php if ( ! is_admin() ) : ?>
1139
                <em class="small form-text text-muted mt-0">
1140
                    <?php
1141
                        printf(
1142
                            __( '%1$s - %2$s at %3$s', 'invoicing' ),
1143
                            $note->comment_author,
1144
                            getpaid_format_date_value( $note->comment_date ),
1145
                            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

1145
                            date_i18n( /** @scrutinizer ignore-type */ get_option( 'time_format' ), strtotime( $note->comment_date ) )
Loading history...
1146
                        );
1147
                    ?>
1148
                </em>
1149
            <?php endif; ?>
1150
1151
        </div>
1152
1153
        <?php if ( is_admin() ) : ?>
1154
1155
            <p class="meta px-4 py-2">
1156
                <abbr class="exact-date" title="<?php echo esc_attr( $note->comment_date ); ?>">
1157
                    <?php
1158
                        printf(
1159
                            __( '%1$s - %2$s at %3$s', 'invoicing' ),
1160
                            $note->comment_author,
1161
                            getpaid_format_date_value( $note->comment_date ),
1162
                            date_i18n( get_option( 'time_format' ), strtotime( $note->comment_date ) )
1163
                        );
1164
                    ?>
1165
                </abbr>&nbsp;&nbsp;
1166
                <?php if ( $note->comment_author !== 'System' && wpinv_current_user_can_manage_invoicing() ) { ?>
1167
                    <a href="#" class="delete_note"><?php _e( 'Delete note', 'invoicing' ); ?></a>
1168
                <?php } ?>
1169
            </p>
1170
1171
        <?php endif; ?>
1172
        
1173
    </li>
1174
    <?php
1175
    $note_content = ob_get_clean();
1176
    $note_content = apply_filters( 'wpinv_get_invoice_note_line_item', $note_content, $note, $echo );
1177
1178
    if ( $echo ) {
1179
        echo $note_content;
1180
    } else {
1181
        return $note_content;
1182
    }
1183
}
1184
1185
/**
1186
 * Function to get privacy policy text.
1187
 *
1188
 * @since 1.0.13
1189
 * @return string
1190
 */
1191
function wpinv_get_policy_text() {
1192
    $privacy_page_id = get_option( 'wp_page_for_privacy_policy', 0 );
1193
1194
    $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]' ));
1195
1196
    if(!$privacy_page_id){
1197
        $privacy_page_id = wpinv_get_option( 'privacy_page', 0 );
1198
    }
1199
1200
    $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

1200
    $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...
1201
1202
    $find_replace = array(
1203
        '[wpinv_privacy_policy]' => $privacy_link,
1204
    );
1205
1206
    $privacy_text = str_replace( array_keys( $find_replace ), array_values( $find_replace ), $text );
1207
1208
    return wp_kses_post(wpautop($privacy_text));
1209
}
1210
1211
function wpinv_oxygen_fix_conflict() {
1212
    global $ct_ignore_post_types;
1213
1214
    if ( ! is_array( $ct_ignore_post_types ) ) {
1215
        $ct_ignore_post_types = array();
1216
    }
1217
1218
    $post_types = array( 'wpi_discount', 'wpi_invoice', 'wpi_item', 'wpi_payment_form' );
1219
1220
    foreach ( $post_types as $post_type ) {
1221
        $ct_ignore_post_types[] = $post_type;
1222
1223
        // Ignore post type
1224
        add_filter( 'pre_option_oxygen_vsb_ignore_post_type_' . $post_type, '__return_true', 999 );
1225
    }
1226
1227
    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

1227
    /** @scrutinizer ignore-call */ 
1228
    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...
1228
    add_filter( 'template_include', 'wpinv_template', 999, 1 );
1229
}
1230
1231
/**
1232
 * Helper function to display a payment form on the frontend.
1233
 * 
1234
 * @param GetPaid_Payment_Form $form
1235
 */
1236
function getpaid_display_payment_form( $form ) {
1237
1238
    if ( is_numeric( $form ) ) {
0 ignored issues
show
introduced by
The condition is_numeric($form) is always false.
Loading history...
1239
        $form = new GetPaid_Payment_Form( $form );
1240
    }
1241
1242
    $form->display();
1243
1244
}
1245
1246
/**
1247
 * Helper function to display a item payment form on the frontend.
1248
 */
1249
function getpaid_display_item_payment_form( $items ) {
1250
1251
    $form = new GetPaid_Payment_Form( wpinv_get_default_payment_form() );
1252
    $form->set_items( $items );
1253
1254
    if ( 0 == count( $form->get_items() ) ) {
1255
        echo aui()->alert(
1256
			array(
1257
				'type'    => 'warning',
1258
				'content' => __( 'No published items found', 'invoicing' ),
1259
			)
1260
        );
1261
        return;
1262
    }
1263
1264
    $form_items = esc_attr( getpaid_convert_items_to_string( $items ) );
1265
    $form_items = "<input type='hidden' name='getpaid-form-items' value='$form_items' />";
1266
    $form->display( $form_items );
1267
}
1268
1269
/**
1270
 * Helper function to display an invoice payment form on the frontend.
1271
 */
1272
function getpaid_display_invoice_payment_form( $invoice_id ) {
1273
1274
    $invoice = wpinv_get_invoice( $invoice_id );
1275
1276
    if ( empty( $invoice ) ) {
1277
		echo aui()->alert(
1278
			array(
1279
				'type'    => 'warning',
1280
				'content' => __( 'Invoice not found', 'invoicing' ),
1281
			)
1282
        );
1283
        return;
1284
    }
1285
1286
    if ( $invoice->is_paid() ) {
1287
		echo aui()->alert(
1288
			array(
1289
				'type'    => 'warning',
1290
				'content' => __( 'Invoice has already been paid', 'invoicing' ),
1291
			)
1292
        );
1293
        return;
1294
    }
1295
1296
    $form = new GetPaid_Payment_Form( wpinv_get_default_payment_form() );
1297
    $form->set_items( $invoice->get_items() );
1298
1299
    $form->display();
1300
}
1301
1302
/**
1303
 * Helper function to convert item string to array.
1304
 */
1305
function getpaid_convert_items_to_array( $items ) {
1306
    $items    = array_filter( array_map( 'trim', explode( ',', $items ) ) );
1307
    $prepared = array();
1308
1309
    foreach ( $items as $item ) {
1310
        $data = array_map( 'trim', explode( '|', $item ) );
1311
1312
        if ( empty( $data[0] ) || ! is_numeric( $data[0] ) ) {
1313
            continue;
1314
        }
1315
1316
        $quantity = 1;
1317
        if ( isset( $data[1] ) && is_numeric( $data[1] ) ) {
1318
            $quantity = (float) $data[1];
1319
        }
1320
1321
        $prepared[ $data[0] ] = $quantity;
1322
1323
    }
1324
1325
    return $prepared;
1326
}
1327
1328
/**
1329
 * Helper function to convert item array to string.
1330
 */
1331
function getpaid_convert_items_to_string( $items ) {
1332
    $prepared = array();
1333
1334
    foreach ( $items as $item => $quantity ) {
1335
        $prepared[] = "$item|$quantity";
1336
    }
1337
    return implode( ',', $prepared );
1338
}
1339
1340
/**
1341
 * Helper function to display a payment item.
1342
 * 
1343
 * Provide a label and one of $form, $items or $invoice.
1344
 */
1345
function getpaid_get_payment_button( $label, $form = null, $items = null, $invoice = null ) {
1346
    $label = sanitize_text_field( $label );
1347
1348
    if ( ! empty( $form ) ) {
1349
        $form  = esc_attr( $form );
1350
        return "<button class='btn btn-primary getpaid-payment-button' type='button' data-form='$form'>$label</button>"; 
1351
    }
1352
	
1353
	if ( ! empty( $items ) ) {
1354
        $items  = esc_attr( $items );
1355
        return "<button class='btn btn-primary getpaid-payment-button' type='button' data-item='$items'>$label</button>"; 
1356
    }
1357
    
1358
    if ( ! empty( $invoice ) ) {
1359
        $invoice  = esc_attr( $invoice );
1360
        return "<button class='btn btn-primary getpaid-payment-button' type='button' data-invoice='$invoice'>$label</button>"; 
1361
    }
1362
1363
}
1364
1365
/**
1366
 * Display invoice description before line items.
1367
 *
1368
 * @param WPInv_Invoice $invoice
1369
 */
1370
function getpaid_the_invoice_description( $invoice ) {
1371
    $description = $invoice->get_description();
1372
1373
    if ( empty( $description ) ) {
1374
        return;
1375
    }
1376
1377
    $description = wp_kses_post( $description );
1378
    echo "<small class='getpaid-invoice-description text-dark p-2 form-text' style='margin-bottom: 20px; border-left: 2px solid #2196F3;'><em>$description</em></small>";
1379
}
1380
add_action( 'getpaid_invoice_line_items', 'getpaid_the_invoice_description', 100 );
1381
1382
/**
1383
 * Render element on a form.
1384
 *
1385
 * @param array $element
1386
 * @param GetPaid_Payment_Form $form
1387
 */
1388
function getpaid_payment_form_element( $element, $form ) {
1389
1390
    // Set up the args.
1391
    $element_type    = trim( $element['type'] );
1392
    $element['form'] = $form;
1393
    extract( $element );
1394
1395
    // Try to locate the appropriate template.
1396
    $located = wpinv_locate_template( "payment-forms/elements/$element_type.php" );
1397
    
1398
    // Abort if this is not our element.
1399
    if ( empty( $located ) || ! file_exists( $located ) ) {
1400
        return;
1401
    }
1402
1403
    // Generate the class and id of the element.
1404
    $wrapper_class = 'getpaid-payment-form-element-' . trim( esc_attr( $element_type ) );
1405
    $id            = isset( $id ) ? $id : uniqid( 'gp' );
1406
1407
    // Echo the opening wrapper.
1408
    echo "<div class='getpaid-payment-form-element $wrapper_class'>";
1409
1410
    // Fires before displaying a given element type's content.
1411
    do_action( "getpaid_before_payment_form_{$element_type}_element", $element, $form );
1412
1413
    // Include the template for the element.
1414
    include $located;
1415
1416
    // Fires after displaying a given element type's content.
1417
    do_action( "getpaid_payment_form_{$element_type}_element", $element, $form );
1418
1419
    // Echo the closing wrapper.
1420
    echo '</div>';
1421
}
1422
add_action( 'getpaid_payment_form_element', 'getpaid_payment_form_element', 10, 2 );
1423
1424
/**
1425
 * Render an element's edit page.
1426
 *
1427
 * @param WP_Post $post
1428
 */
1429
function getpaid_payment_form_edit_element_template( $post ) {
1430
1431
    // Retrieve all elements.
1432
    $all_elements = wp_list_pluck( wpinv_get_data( 'payment-form-elements' ), 'type' );
1433
1434
    foreach ( $all_elements as $element ) {
1435
1436
        // Try to locate the appropriate template.
1437
        $element = sanitize_key( $element );
1438
        $located = wpinv_locate_template( "payment-forms-admin/edit/$element.php" );
1439
1440
        // Continue if this is not our element.
1441
        if ( empty( $located ) || ! file_exists( $located ) ) {
1442
            continue;
1443
        }
1444
1445
        // Include the template for the element.
1446
        echo "<div v-if=\"active_form_element.type=='$element'\">";
1447
        include $located;
1448
        echo '</div>';
1449
    }
1450
1451
}
1452
add_action( 'getpaid_payment_form_edit_element_template', 'getpaid_payment_form_edit_element_template' );
1453
1454
/**
1455
 * Render an element's preview.
1456
 *
1457
 */
1458
function getpaid_payment_form_render_element_preview_template() {
1459
1460
    // Retrieve all elements.
1461
    $all_elements = wp_list_pluck( wpinv_get_data( 'payment-form-elements' ), 'type' );
1462
1463
    foreach ( $all_elements as $element ) {
1464
1465
        // Try to locate the appropriate template.
1466
        $element = sanitize_key( $element );
1467
        $located = wpinv_locate_template( "payment-forms-admin/previews/$element.php" );
1468
1469
        // Continue if this is not our element.
1470
        if ( empty( $located ) || ! file_exists( $located ) ) {
1471
            continue;
1472
        }
1473
1474
        // Include the template for the element.
1475
        echo "<div v-if=\"form_element.type=='$element'\">";
1476
        include $located;
1477
        echo '</div>';
1478
    }
1479
1480
}
1481
add_action( 'wpinv_payment_form_render_element_template', 'getpaid_payment_form_render_element_preview_template' );
1482
1483
/**
1484
 * Shows a list of gateways that support recurring payments.
1485
 */
1486
function wpinv_get_recurring_gateways_text() {
1487
    $gateways = array();
1488
1489
    foreach ( wpinv_get_payment_gateways() as $key => $gateway ) {
1490
        if ( wpinv_gateway_support_subscription( $key ) ) {
1491
            $gateways[] = sanitize_text_field( $gateway['admin_label'] );
1492
        }
1493
    }
1494
1495
    if ( empty( $gateways ) ) {
1496
        return "<span class='form-text text-danger'>" . __( 'No active gateways support subscription payments.', 'invoicing' ) ."</span>";
1497
    }
1498
1499
    return "<span class='form-text text-muted'>" . wp_sprintf( __( 'Subscription payments only supported by: %s', 'invoicing' ), implode( ', ', $gateways ) ) ."</span>";
1500
1501
}
1502
1503
/**
1504
 * Returns the template.
1505
 * 
1506
 * @return GetPaid_Template
1507
 */
1508
function getpaid_template() {
1509
    return getpaid()->get( 'template' );
1510
}
1511
1512
/**
1513
 * Displays pagination links.
1514
 * 
1515
 * @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...
1516
 * @return string
1517
 */
1518
function getpaid_paginate_links( $args ) {
1519
    return str_replace( 'page-link dots', 'page-link text-dark', aui()->pagination( $args ) );
1520
}
1521
1522
/**
1523
 * Displays the states select markup.
1524
 * 
1525
 * @param string country
1526
 * @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...
1527
 * @return string
1528
 */
1529
function getpaid_get_states_select_markup( $country, $state, $placeholder, $label, $help_text, $required = false, $wrapper_class = 'col-12', $field_name = 'wpinv_state' ) {
1530
1531
    $states = wpinv_get_country_states( $country );
1532
    $uniqid = uniqid( '_' );
1533
1534
    if ( ! empty( $states ) ) {
1535
1536
        return aui()->select( array(
1537
            'options'          => $states,
1538
            'name'             => esc_attr( $field_name ),
1539
            'id'               => sanitize_html_class( $field_name ) . $uniqid,
1540
            'value'            => sanitize_text_field( $state ),
1541
            'placeholder'      => $placeholder,
1542
            'required'         => $required,
1543
            'label'            => wp_kses_post( $label ),
1544
            'label_type'       => 'vertical',
1545
            'help_text'        => $help_text,
1546
            'class'            => 'getpaid-address-field wpinv_state',
1547
            'wrap_class'       => "$wrapper_class getpaid-address-field-wrapper__state",
1548
            'label_class'      => 'getpaid-address-field-label getpaid-address-field-label__state',
1549
            'extra_attributes' => array(
1550
                'autocomplete' => "address-level1",
1551
            ),
1552
        ));
1553
1554
    }
1555
1556
    return aui()->input(
1557
        array(
1558
            'name'        => esc_attr( $field_name ),
1559
            'id'          => sanitize_html_class( $field_name ) . $uniqid,
1560
            'placeholder' => $placeholder,
1561
            'required'    => $required,
1562
            'label'       => wp_kses_post( $label ),
1563
            'label_type'  => 'vertical',
1564
            'help_text'   => $help_text,
1565
            'value'       => sanitize_text_field( $state ),
1566
            'class'       => 'getpaid-address-field wpinv_state',
1567
            'wrap_class'  => "$wrapper_class getpaid-address-field-wrapper__state",
1568
            'label_class' => 'getpaid-address-field-label getpaid-address-field-label__state',
1569
            'extra_attributes' => array(
1570
                'autocomplete' => "address-level1",
1571
            ),
1572
        )
1573
    );
1574
1575
}
1576
1577
/**
1578
 * Retrieves an element's grid width.
1579
 * 
1580
 * @param array $element
1581
 * @return string
1582
 */
1583
function getpaid_get_form_element_grid_class( $element ) {
1584
1585
    $class = "col-12";
1586
    $width = empty( $element['grid_width'] ) ? 'full' : $element['grid_width'];
1587
1588
    if ( $width == 'half' ) {
1589
        $class = "col-12 col-md-6";
1590
    }
1591
1592
    if ( $width == 'third' ) {
1593
        $class = "col-12 col-md-4";
1594
    }
1595
1596
    return $class;
1597
}
1598