Completed
Push — master ( 9b8253...a465f2 )
by Brian
29s queued 14s
created

getpaid_get_form_element_grid_class()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 14
rs 10
cc 4
nc 8
nop 1
1
<?php
2
/**
3
 * Template functions.
4
 *
5
 */
6
7
defined( 'ABSPATH' ) || exit;
8
9
/**
10
 * Displays an invoice.
11
 * 
12
 * @param WPInv_Invoice $invoice.
13
 */
14
function getpaid_invoice( $invoice ) {
15
    if ( ! empty( $invoice ) ) {
16
        wpinv_get_template( 'invoice/invoice.php', compact( 'invoice' ) );
17
    }
18
}
19
add_action( 'getpaid_invoice', 'getpaid_invoice', 10 );
20
21
/**
22
 * Displays the invoice footer.
23
 */
24
function getpaid_invoice_footer( $invoice ) {
25
    if ( ! empty( $invoice ) ) {
26
        wpinv_get_template( 'invoice/footer.php', compact( 'invoice' ) );
27
    }
28
}
29
add_action( 'getpaid_invoice_footer', 'getpaid_invoice_footer', 10 );
30
31
/**
32
 * Displays the invoice top bar.
33
 */
34
function getpaid_invoice_header( $invoice ) {
35
    if ( ! empty( $invoice ) ) {
36
        wpinv_get_template( 'invoice/header.php', compact( 'invoice' ) );
37
    }
38
}
39
add_action( 'getpaid_invoice_header', 'getpaid_invoice_header', 10 );
40
41
/**
42
 * Displays actions on the left side of the header.
43
 */
44
function getpaid_invoice_header_left_actions( $invoice ) {
45
    if ( ! empty( $invoice ) ) {
46
        wpinv_get_template( 'invoice/header-left-actions.php', compact( 'invoice' ) );
47
    }
48
}
49
add_action( 'getpaid_invoice_header_left', 'getpaid_invoice_header_left_actions', 10 );
50
51
/**
52
 * Displays actions on the right side of the invoice top bar.
53
 */
54
function getpaid_invoice_header_right_actions( $invoice ) {
55
    if ( ! empty( $invoice ) ) {
56
        wpinv_get_template( 'invoice/header-right-actions.php', compact( 'invoice' ) );
57
    }
58
}
59
add_action( 'getpaid_invoice_header_right', 'getpaid_invoice_header_right_actions', 10 );
60
61
/**
62
 * Displays the invoice title, watermark, logo etc.
63
 */
64
function getpaid_invoice_details_top( $invoice ) {
65
    if ( ! empty( $invoice ) ) {
66
        wpinv_get_template( 'invoice/details-top.php', compact( 'invoice' ) );
67
    }
68
}
69
add_action( 'getpaid_invoice_details', 'getpaid_invoice_details_top', 10 );
70
71
/**
72
 * Displays the company logo.
73
 */
74
function getpaid_invoice_logo( $invoice ) {
75
    if ( ! empty( $invoice ) ) {
76
        wpinv_get_template( 'invoice/invoice-logo.php', compact( 'invoice' ) );
77
    }
78
}
79
add_action( 'getpaid_invoice_details_top_left', 'getpaid_invoice_logo' );
80
81
/**
82
 * Displays the type of invoice.
83
 */
84
function getpaid_invoice_type( $invoice ) {
85
    if ( ! empty( $invoice ) ) {
86
        wpinv_get_template( 'invoice/invoice-type.php', compact( 'invoice' ) );
87
    }
88
}
89
add_action( 'getpaid_invoice_details_top_right', 'getpaid_invoice_type' );
90
91
/**
92
 * Displays the invoice details.
93
 */
94
function getpaid_invoice_details_main( $invoice ) {
95
    if ( ! empty( $invoice ) ) {
96
        wpinv_get_template( 'invoice/details.php', compact( 'invoice' ) );
97
    }
98
}
99
add_action( 'getpaid_invoice_details', 'getpaid_invoice_details_main', 50 );
100
101
/**
102
 * Returns a path to the templates directory.
103
 * 
104
 * @return string
105
 */
106
function wpinv_get_templates_dir() {
107
    return getpaid_template()->templates_dir;
108
}
109
110
/**
111
 * Returns a url to the templates directory.
112
 * 
113
 * @return string
114
 */
115
function wpinv_get_templates_url() {
116
    return getpaid_template()->templates_url;
117
}
118
119
/**
120
 * Displays a template.
121
 * 
122
 * First checks if there is a template overide, if not it loads the default template.
123
 * 
124
 * @param string $template_name e.g payment-forms/cart.php The template to locate.
125
 * @param string $template_path The templates directory relative to the theme's root dir. Defaults to 'invoicing'.
126
 * @param string $default_path The root path to the default template. Defaults to invoicing/templates
127
 */
128
function wpinv_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
129
    return getpaid_template()->display_template( $template_name, $args, $template_path, $default_path );
0 ignored issues
show
Bug introduced by
Are you sure the usage of getpaid_template()->disp...te_path, $default_path) targeting GetPaid_Template::display_template() seems to always return null.

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

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

}

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

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

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

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

3 paths for user data to reach this point

  1. Path: Read from $_GET, and $_GET['vat_rule'] is assigned to $vat_rule in includes/admin/class-getpaid-post-types-admin.php on line 528
  1. Read from $_GET, and $_GET['vat_rule'] is assigned to $vat_rule
    in includes/admin/class-getpaid-post-types-admin.php on line 528
  2. wpinv_html_select() is called
    in includes/admin/class-getpaid-post-types-admin.php on line 533
  3. Enters via parameter $args
    in includes/wpinv-template-functions.php on line 379
  2. Path: Read from $_GET, and $_GET['type'] is assigned to $type in includes/admin/class-getpaid-post-types-admin.php on line 584
  1. Read from $_GET, and $_GET['type'] is assigned to $type
    in includes/admin/class-getpaid-post-types-admin.php on line 584
  2. wpinv_html_select() is called
    in includes/admin/class-getpaid-post-types-admin.php on line 588
  3. Enters via parameter $args
    in includes/wpinv-template-functions.php on line 379
  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 559
  1. Read from $_GET, and $_GET['vat_class'] is assigned to $vat_class
    in includes/admin/class-getpaid-post-types-admin.php on line 559
  2. wpinv_html_select() is called
    in includes/admin/class-getpaid-post-types-admin.php on line 563
  3. Enters via parameter $args
    in includes/wpinv-template-functions.php on line 379

Used in variable context

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

General Strategies to prevent injection

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

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

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

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

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

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

}

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

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

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

Loading history...
818
}
819
820
/**
821
 * Displays invoice meta.
822
 */
823
function getpaid_invoice_meta( $invoice ) {
824
825
    $invoice = new WPInv_Invoice( $invoice );
826
827
    // Ensure that we have an invoice.
828
    if ( 0 == $invoice->get_id() ) {
829
        return;
830
    }
831
832
    // Get the invoice meta.
833
    $meta = getpaid_get_invoice_meta( $invoice );
834
835
    // Display the meta.
836
    wpinv_get_template( 'invoice/invoice-meta.php', compact( 'invoice', 'meta' ) );
837
838
}
839
add_action( 'getpaid_invoice_details_right', 'getpaid_invoice_meta', 10 );
840
841
/**
842
 * Retrieves the address markup to use on Invoices.
843
 * 
844
 * @since 1.0.13
845
 * @see `wpinv_get_full_address_format`
846
 * @see `wpinv_get_invoice_address_replacements`
847
 * @param array $billing_details customer's billing details
848
 * @param  string $separator How to separate address lines.
849
 * @return string
850
 */
851
function wpinv_get_invoice_address_markup( $billing_details, $separator = '<br/>' ) {
852
853
    // Retrieve the address markup...
854
    $country= empty( $billing_details['country'] ) ? '' : $billing_details['country'];
855
    $format = wpinv_get_full_address_format( $country );
856
857
    // ... and the replacements.
858
    $replacements = wpinv_get_invoice_address_replacements( $billing_details );
859
860
    $formatted_address = str_ireplace( array_keys( $replacements ), $replacements, $format );
861
    
862
	// Remove unavailable tags.
863
    $formatted_address = preg_replace( "/\{\{\w+\}\}/", '', $formatted_address );
864
865
    // Clean up white space.
866
	$formatted_address = preg_replace( '/  +/', ' ', trim( $formatted_address ) );
867
    $formatted_address = preg_replace( '/\n\n+/', "\n", $formatted_address );
868
    
869
    // Break newlines apart and remove empty lines/trim commas and white space.
870
	$formatted_address = array_filter( array_map( 'wpinv_trim_formatted_address_line', explode( "\n", $formatted_address ) ) );
871
872
    // Add html breaks.
873
	$formatted_address = implode( $separator, $formatted_address );
874
875
	// We're done!
876
	return $formatted_address;
877
    
878
}
879
880
/**
881
 * Displays the billing address.
882
 * 
883
 * @param WPInv_Invoice $invoice
884
 */
885
function wpinv_display_to_address( $invoice = 0 ) {
886
    if ( ! empty( $invoice ) ) {
887
        wpinv_get_template( 'invoice/billing-address.php', compact( 'invoice' ) );
888
    }
889
}
890
add_action( 'getpaid_invoice_details_left', 'wpinv_display_to_address', 40 );
891
892
893
/**
894
 * Displays invoice line items.
895
 */
896
function wpinv_display_line_items( $invoice_id = 0 ) {
897
898
    // Prepare the invoice.
899
    $invoice = new WPInv_Invoice( $invoice_id );
900
901
    // Abort if there is no invoice.
902
    if ( 0 == $invoice->get_id() ) {
903
        return;
904
    }
905
906
    // Line item columns.
907
    $columns = getpaid_invoice_item_columns( $invoice );
908
    $columns = apply_filters( 'getpaid_invoice_line_items_table_columns', $columns, $invoice );
909
910
    wpinv_get_template( 'invoice/line-items.php', compact( 'invoice', 'columns' ) );
911
}
912
add_action( 'getpaid_invoice_line_items', 'wpinv_display_line_items', 10 );
913
914
/**
915
 * Displays invoice notices on invoices.
916
 */
917
function wpinv_display_invoice_notice() {
918
919
    $label  = wpinv_get_option( 'vat_invoice_notice_label' );
920
    $notice = wpinv_get_option( 'vat_invoice_notice' );
921
922
    if ( empty( $label ) && empty( $notice ) ) {
923
        return;
924
    }
925
926
    echo '<div class="mt-4 mb-4 wpinv-vat-notice">';
927
928
    if ( ! empty( $label ) ) {
929
        $label = sanitize_text_field( $label );
930
        echo "<h5>$label</h5>";
931
    }
932
933
    if ( ! empty( $notice ) ) {
934
        echo '<small class="form-text text-muted">' . wpautop( wptexturize( $notice ) ) . '</small>';
935
    }
936
937
    echo '</div>';
938
}
939
add_action( 'getpaid_invoice_line_items', 'wpinv_display_invoice_notice', 100 );
940
941
/**
942
 * @param WPInv_Invoice $invoice
943
 */
944
function wpinv_display_invoice_notes( $invoice ) {
945
946
    // Retrieve the notes.
947
    $notes = wpinv_get_invoice_notes( $invoice->get_id(), 'customer' );
948
949
    // Abort if we have non.
950
    if ( empty( $notes ) ) {
951
        return;
952
    }
953
954
    // Echo the note.
955
    echo '<div class="getpaid-invoice-notes-wrapper border position-relative w-100 mb-4 p-0">';
956
    echo '<h3 class="getpaid-invoice-notes-title text-dark bg-light border-bottom m-0 d-block">' . __( 'Notes', 'invoicing' ) .'</h3>';
957
    echo '<ul class="getpaid-invoice-notes mt-4 p-0">';
958
959
    foreach( $notes as $note ) {
960
        wpinv_get_invoice_note_line_item( $note );
961
    }
962
963
    echo '</ul>';
964
    echo '</div>';
965
}
966
add_action( 'getpaid_invoice_line_items', 'wpinv_display_invoice_notes', 60 );
967
968
/**
969
 * Loads scripts on our invoice templates.
970
 */
971
function wpinv_display_style() {
972
973
    // Make sure that all scripts have been loaded.
974
    if ( ! did_action( 'wp_enqueue_scripts' ) ) {
975
        do_action( 'wp_enqueue_scripts' );
976
    }
977
978
    // Register the invoices style.
979
    wp_register_style( 'wpinv-single-style', WPINV_PLUGIN_URL . 'assets/css/invoice.css', array(), filemtime( WPINV_PLUGIN_DIR . 'assets/css/invoice.css' ) );
980
981
    // Load required styles
982
    wp_print_styles( 'open-sans' );
983
    wp_print_styles( 'wpinv-single-style' );
984
    wp_print_styles( 'ayecode-ui' );
985
986
    // Maybe load custom css.
987
    $custom_css = wpinv_get_option( 'template_custom_css' );
988
989
    if ( isset( $custom_css ) && ! empty( $custom_css ) ) {
990
        $custom_css     = wp_kses( $custom_css, array( '\'', '\"' ) );
0 ignored issues
show
Bug introduced by
array(''', '\"') of type array<integer,string> is incompatible with the type array<mixed,array>|string expected by parameter $allowed_html of wp_kses(). ( Ignorable by Annotation )

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

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

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

1290
    $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...
1291
1292
    $find_replace = array(
1293
        '[wpinv_privacy_policy]' => $privacy_link,
1294
    );
1295
1296
    $privacy_text = str_replace( array_keys( $find_replace ), array_values( $find_replace ), $text );
1297
1298
    return wp_kses_post(wpautop($privacy_text));
1299
}
1300
1301
function wpinv_oxygen_fix_conflict() {
1302
    global $ct_ignore_post_types;
1303
1304
    if ( ! is_array( $ct_ignore_post_types ) ) {
1305
        $ct_ignore_post_types = array();
1306
    }
1307
1308
    $post_types = array( 'wpi_discount', 'wpi_invoice', 'wpi_item' );
1309
1310
    foreach ( $post_types as $post_type ) {
1311
        $ct_ignore_post_types[] = $post_type;
1312
1313
        // Ignore post type
1314
        add_filter( 'pre_option_oxygen_vsb_ignore_post_type_' . $post_type, '__return_true', 999 );
1315
    }
1316
1317
    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

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