Passed
Push — master ( cb75a2...6929e7 )
by Brian
05:54 queued 11s
created

wpinv_receipt_actions()   B

Complexity

Conditions 8
Paths 17

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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

3 paths for user data to reach this point

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

Used in variable context

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

General Strategies to prevent injection

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

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

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

$sanitized = (integer) $tainted;
Loading history...
437
438
    $data_elements = '';
439
    foreach ( $args['data'] as $key => $value ) {
440
        $data_elements .= ' data-' . esc_attr( $key ) . '="' . esc_attr( $value ) . '"';
441
    }
442
443
    if( $args['multiple'] ) {
444
        $multiple = ' MULTIPLE';
445
    } else {
446
        $multiple = '';
447
    }
448
449
    if( $args['placeholder'] ) {
450
        $placeholder = $args['placeholder'];
451
    } else {
452
        $placeholder = '';
453
    }
454
    
455
    $options = '';
456
    if( !empty( $args['onchange'] ) ) {
457
        $options .= ' onchange="' . esc_attr( $args['onchange'] ) . '"';
458
    }
459
    
460
    if( !empty( $args['required'] ) ) {
461
        $options .= ' required="required"';
462
    }
463
    
464
    if( !empty( $args['disabled'] ) ) {
465
        $options .= ' disabled';
466
    }
467
    
468
    if( !empty( $args['readonly'] ) ) {
469
        $options .= ' readonly';
470
    }
471
472
    $class  = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
473
    $output = '<select name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( $args['id'] ) . '" class="wpinv-select ' . $class . '"' . $multiple . ' data-placeholder="' . $placeholder . '" ' . trim( $options ) . $data_elements . '>';
474
475
    if ( $args['show_option_all'] ) {
476
        if( $args['multiple'] ) {
477
            $selected = selected( true, in_array( 0, $args['selected'] ), false );
478
        } else {
479
            $selected = selected( $args['selected'], 0, false );
480
        }
481
        $output .= '<option value="all"' . $selected . '>' . esc_html( $args['show_option_all'] ) . '</option>';
482
    }
483
484
    if ( !empty( $args['options'] ) ) {
485
486
        if ( $args['show_option_none'] ) {
487
            if( $args['multiple'] ) {
488
                $selected = selected( true, in_array( "", $args['selected'] ), false );
489
            } else {
490
                $selected = selected( $args['selected'] === "", true, false );
491
            }
492
            $output .= '<option value=""' . $selected . '>' . esc_html( $args['show_option_none'] ) . '</option>';
493
        }
494
495
        foreach( $args['options'] as $key => $option ) {
496
497
            if( $args['multiple'] && is_array( $args['selected'] ) ) {
498
                $selected = selected( true, (bool)in_array( $key, $args['selected'] ), false );
499
            } else {
500
                $selected = selected( $args['selected'], $key, false );
501
            }
502
503
            $output .= '<option value="' . esc_attr( $key ) . '"' . $selected . '>' . esc_html( $option ) . '</option>';
504
        }
505
    }
506
507
    $output .= '</select>';
508
509
    return $output;
510
}
511
512
function wpinv_item_dropdown( $args = array() ) {
513
    $defaults = array(
514
        'name'              => 'wpi_item',
515
        'id'                => 'wpi_item',
516
        'class'             => '',
517
        'multiple'          => false,
518
        'selected'          => 0,
519
        'number'            => 100,
520
        'placeholder'       => __( 'Choose a item', 'invoicing' ),
521
        'data'              => array( 'search-type' => 'item' ),
522
        'show_option_all'   => false,
523
        'show_option_none'  => false,
524
        'show_recurring'    => false,
525
    );
526
527
    $args = wp_parse_args( $args, $defaults );
528
529
    $item_args = array(
530
        'post_type'      => 'wpi_item',
531
        'orderby'        => 'title',
532
        'order'          => 'ASC',
533
        'posts_per_page' => $args['number']
534
    );
535
536
    $item_args  = apply_filters( 'wpinv_item_dropdown_query_args', $item_args, $args, $defaults );
537
538
    $items      = get_posts( $item_args );
539
    $options    = array();
540
    if ( $items ) {
541
        foreach ( $items as $item ) {
542
            $title = esc_html( $item->post_title );
543
            
544
            if ( !empty( $args['show_recurring'] ) ) {
545
                $title .= wpinv_get_item_suffix( $item->ID, false );
546
            }
547
            
548
            $options[ absint( $item->ID ) ] = $title;
549
        }
550
    }
551
552
    // This ensures that any selected items are included in the drop down
553
    if( is_array( $args['selected'] ) ) {
554
        foreach( $args['selected'] as $item ) {
555
            if( ! in_array( $item, $options ) ) {
556
                $title = get_the_title( $item );
557
                if ( !empty( $args['show_recurring'] ) ) {
558
                    $title .= wpinv_get_item_suffix( $item, false );
559
                }
560
                $options[$item] = $title;
561
            }
562
        }
563
    } elseif ( is_numeric( $args['selected'] ) && $args['selected'] !== 0 ) {
564
        if ( ! in_array( $args['selected'], $options ) ) {
565
            $title = get_the_title( $args['selected'] );
566
            if ( !empty( $args['show_recurring'] ) ) {
567
                $title .= wpinv_get_item_suffix( $args['selected'], false );
568
            }
569
            $options[$args['selected']] = get_the_title( $args['selected'] );
570
        }
571
    }
572
573
    $output = wpinv_html_select( array(
574
        'name'             => $args['name'],
575
        'selected'         => $args['selected'],
576
        'id'               => $args['id'],
577
        'class'            => $args['class'],
578
        'options'          => $options,
579
        'multiple'         => $args['multiple'],
580
        'placeholder'      => $args['placeholder'],
581
        'show_option_all'  => $args['show_option_all'],
582
        'show_option_none' => $args['show_option_none'],
583
        'data'             => $args['data'],
584
    ) );
585
586
    return $output;
587
}
588
589
/**
590
 * Returns an array of published items.
591
 */
592
function wpinv_get_published_items_for_dropdown() {
593
594
    $items = get_posts(
595
        array(
596
            'post_type'      => 'wpi_item',
597
            'orderby'        => 'title',
598
            'order'          => 'ASC',
599
            'posts_per_page' => '-1'
600
        )
601
    );
602
603
    $options = array();
604
    if ( $items ) {
605
        foreach ( $items as $item ) {
606
            $options[ $item->ID ] = esc_html( $item->post_title ) . wpinv_get_item_suffix( $item->ID, false );
607
        }
608
    }
609
610
    return $options;
611
}
612
613
function wpinv_html_checkbox( $args = array() ) {
614
    $defaults = array(
615
        'name'     => null,
616
        'current'  => null,
617
        'class'    => 'wpinv-checkbox',
618
        'options'  => array(
619
            'disabled' => false,
620
            'readonly' => false
621
        )
622
    );
623
624
    $args = wp_parse_args( $args, $defaults );
625
626
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
627
    $options = '';
628
    if ( ! empty( $args['options']['disabled'] ) ) {
629
        $options .= ' disabled="disabled"';
630
    } elseif ( ! empty( $args['options']['readonly'] ) ) {
631
        $options .= ' readonly';
632
    }
633
634
    $output = '<input type="checkbox"' . $options . ' name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( $args['name'] ) . '" class="' . $class . ' ' . esc_attr( $args['name'] ) . '" ' . checked( 1, $args['current'], false ) . ' />';
635
636
    return $output;
637
}
638
639
/**
640
 * Displays a hidden field.
641
 */
642
function getpaid_hidden_field( $name, $value ) {
643
    $name  = sanitize_text_field( $name );
644
    $value = esc_attr( $value );
645
646
    echo "<input type='hidden' name='$name' value='$value' />";
647
}
648
649
function wpinv_html_text( $args = array() ) {
650
    // Backwards compatibility
651
    if ( func_num_args() > 1 ) {
652
        $args = func_get_args();
653
654
        $name  = $args[0];
655
        $value = isset( $args[1] ) ? $args[1] : '';
656
        $label = isset( $args[2] ) ? $args[2] : '';
657
        $desc  = isset( $args[3] ) ? $args[3] : '';
658
    }
659
660
    $defaults = array(
661
        'id'           => '',
662
        'name'         => isset( $name )  ? $name  : 'text',
663
        'value'        => isset( $value ) ? $value : null,
664
        'label'        => isset( $label ) ? $label : null,
665
        'desc'         => isset( $desc )  ? $desc  : null,
666
        'placeholder'  => '',
667
        'class'        => 'regular-text',
668
        'disabled'     => false,
669
        'readonly'     => false,
670
        'required'     => false,
671
        'autocomplete' => '',
672
        'data'         => false
673
    );
674
675
    $args = wp_parse_args( $args, $defaults );
676
677
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
678
    $options = '';
679
    if( $args['required'] ) {
680
        $options .= ' required="required"';
681
    }
682
    if( $args['readonly'] ) {
683
        $options .= ' readonly';
684
    }
685
    if( $args['readonly'] ) {
686
        $options .= ' readonly';
687
    }
688
689
    $data = '';
690
    if ( !empty( $args['data'] ) ) {
691
        foreach ( $args['data'] as $key => $value ) {
692
            $data .= 'data-' . wpinv_sanitize_key( $key ) . '="' . esc_attr( $value ) . '" ';
693
        }
694
    }
695
696
    $output = '<span id="wpinv-' . wpinv_sanitize_key( $args['name'] ) . '-wrap">';
697
    $output .= '<label class="wpinv-label" for="' . wpinv_sanitize_key( $args['id'] ) . '">' . esc_html( $args['label'] ) . '</label>';
698
    if ( ! empty( $args['desc'] ) ) {
699
        $output .= '<span class="wpinv-description">' . esc_html( $args['desc'] ) . '</span>';
700
    }
701
702
    $output .= '<input type="text" name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( $args['id'] )  . '" autocomplete="' . esc_attr( $args['autocomplete'] )  . '" value="' . esc_attr( $args['value'] ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" class="' . $class . '" ' . $data . ' ' . trim( $options ) . '/>';
703
704
    $output .= '</span>';
705
706
    return $output;
707
}
708
709
function wpinv_html_date_field( $args = array() ) {
710
    if( empty( $args['class'] ) ) {
711
        $args['class'] = 'wpiDatepicker';
712
    } elseif( ! strpos( $args['class'], 'wpiDatepicker' ) ) {
713
        $args['class'] .= ' wpiDatepicker';
714
    }
715
716
    return wpinv_html_text( $args );
717
}
718
719
function wpinv_html_textarea( $args = array() ) {
720
    $defaults = array(
721
        'name'        => 'textarea',
722
        'value'       => null,
723
        'label'       => null,
724
        'desc'        => null,
725
        'class'       => 'large-text',
726
        'disabled'    => false,
727
        'placeholder' => '',
728
    );
729
730
    $args = wp_parse_args( $args, $defaults );
731
732
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
733
    $disabled = '';
734
    if( $args['disabled'] ) {
735
        $disabled = ' disabled="disabled"';
736
    }
737
738
    $output = '<span id="wpinv-' . wpinv_sanitize_key( $args['name'] ) . '-wrap">';
739
    $output .= '<label class="wpinv-label" for="' . wpinv_sanitize_key( $args['name'] ) . '">' . esc_html( $args['label'] ) . '</label>';
740
    $output .= '<textarea name="' . esc_attr( $args['name'] ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" id="' . wpinv_sanitize_key( $args['name'] ) . '" class="' . $class . '"' . $disabled . '>' . esc_attr( $args['value'] ) . '</textarea>';
741
742
    if ( ! empty( $args['desc'] ) ) {
743
        $output .= '<span class="wpinv-description">' . esc_html( $args['desc'] ) . '</span>';
744
    }
745
    $output .= '</span>';
746
747
    return $output;
748
}
749
750
function wpinv_html_ajax_user_search( $args = array() ) {
751
    $defaults = array(
752
        'name'        => 'user_id',
753
        'value'       => null,
754
        'placeholder' => __( 'Enter username', 'invoicing' ),
755
        'label'       => null,
756
        'desc'        => null,
757
        'class'       => '',
758
        'disabled'    => false,
759
        'autocomplete'=> 'off',
760
        'data'        => false
761
    );
762
763
    $args = wp_parse_args( $args, $defaults );
764
765
    $args['class'] = 'wpinv-ajax-user-search ' . $args['class'];
766
767
    $output  = '<span class="wpinv_user_search_wrap">';
768
        $output .= wpinv_html_text( $args );
769
        $output .= '<span class="wpinv_user_search_results hidden"><a class="wpinv-ajax-user-cancel" title="' . __( 'Cancel', 'invoicing' ) . '" aria-label="' . __( 'Cancel', 'invoicing' ) . '" href="#">x</a><span></span></span>';
770
    $output .= '</span>';
771
772
    return $output;
773
}
774
775
function wpinv_ip_geolocation() {
776
    global $wpinv_euvat;
777
    
778
    $ip         = !empty( $_GET['ip'] ) ? sanitize_text_field( $_GET['ip'] ) : '';    
779
    $content    = '';
780
    $iso        = '';
781
    $country    = '';
782
    $region     = '';
783
    $city       = '';
784
    $longitude  = '';
785
    $latitude   = '';
786
    $credit     = '';
787
    $address    = '';
788
    
789
    if ( wpinv_get_option( 'vat_ip_lookup' ) == 'geoip2' && $geoip2_city = $wpinv_euvat->geoip2_city_record( $ip ) ) {
790
        try {
791
            $iso        = $geoip2_city->country->isoCode;
792
            $country    = $geoip2_city->country->name;
793
            $region     = !empty( $geoip2_city->subdivisions ) && !empty( $geoip2_city->subdivisions[0]->name ) ? $geoip2_city->subdivisions[0]->name : '';
794
            $city       = $geoip2_city->city->name;
795
            $longitude  = $geoip2_city->location->longitude;
796
            $latitude   = $geoip2_city->location->latitude;
797
            $credit     = __( 'Geolocated using the information by MaxMind, available from <a href="http://www.maxmind.com" target="_blank">www.maxmind.com</a>', 'invoicing' );
798
        } catch( Exception $e ) { }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
799
    }
800
    
801
    if ( !( $iso && $longitude && $latitude ) && function_exists( 'simplexml_load_file' ) ) {
802
        try {
803
            $load_xml = simplexml_load_file( 'http://www.geoplugin.net/xml.gp?ip=' . $ip );
804
            
805
            if ( !empty( $load_xml ) && isset( $load_xml->geoplugin_countryCode ) && !empty( $load_xml->geoplugin_latitude ) && !empty( $load_xml->geoplugin_longitude ) ) {
806
                $iso        = $load_xml->geoplugin_countryCode;
807
                $country    = $load_xml->geoplugin_countryName;
808
                $region     = !empty( $load_xml->geoplugin_regionName ) ? $load_xml->geoplugin_regionName : '';
809
                $city       = !empty( $load_xml->geoplugin_city ) ? $load_xml->geoplugin_city : '';
810
                $longitude  = $load_xml->geoplugin_longitude;
811
                $latitude   = $load_xml->geoplugin_latitude;
812
                $credit     = $load_xml->geoplugin_credit;
0 ignored issues
show
Unused Code introduced by
The assignment to $credit is dead and can be removed.
Loading history...
813
                $credit     = __( 'Geolocated using the information by geoPlugin, available from <a href="http://www.geoplugin.com" target="_blank">www.geoplugin.com</a>', 'invoicing' ) . '<br>' . $load_xml->geoplugin_credit;
814
            }
815
        } catch( Exception $e ) { }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
816
    }
817
    
818
    if ( $iso && $longitude && $latitude ) {
819
        if ( $city ) {
820
            $address .= $city . ', ';
821
        }
822
        
823
        if ( $region ) {
824
            $address .= $region . ', ';
825
        }
826
        
827
        $address .= $country . ' (' . $iso . ')';
828
        $content = '<p>'. sprintf( __( '<b>Address:</b> %s', 'invoicing' ), $address ) . '</p>';
829
        $content .= '<p>'. $credit . '</p>';
830
    } else {
831
        $content = '<p>'. sprintf( __( 'Unable to find geolocation for the IP address: %s', 'invoicing' ), $ip ) . '</p>';
832
    }
833
    ?>
834
<!DOCTYPE html>
835
<html><head><title><?php echo sprintf( __( 'IP: %s', 'invoicing' ), $ip );?></title><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.0-rc.1/leaflet.css" /><style>html,body{height:100%;margin:0;padding:0;width:100%}body{text-align:center;background:#fff;color:#222;font-size:small;}body,p{font-family: arial,sans-serif}#map{margin:auto;width:100%;height:calc(100% - 120px);min-height:240px}</style></head>
836
<body>
837
    <?php if ( $latitude && $latitude ) { ?>
838
    <div id="map"></div>
839
        <script src="//cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.0-rc.1/leaflet.js"></script>
840
        <script type="text/javascript">
841
        var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
842
            osmAttrib = '&copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors',
843
            osm = L.tileLayer(osmUrl, {maxZoom: 18, attribution: osmAttrib}),
844
            latlng = new L.LatLng(<?php echo $latitude;?>, <?php echo $longitude;?>);
845
846
        var map = new L.Map('map', {center: latlng, zoom: 12, layers: [osm]});
847
848
        var marker = new L.Marker(latlng);
849
        map.addLayer(marker);
850
851
        marker.bindPopup("<p><?php esc_attr_e( $address );?></p>");
852
    </script>
853
    <?php } ?>
854
    <div style="height:100px"><?php echo $content; ?></div>
855
</body></html>
856
<?php
857
    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
858
}
859
add_action( 'wp_ajax_wpinv_ip_geolocation', 'wpinv_ip_geolocation' );
860
add_action( 'wp_ajax_nopriv_wpinv_ip_geolocation', 'wpinv_ip_geolocation' );
861
862
/**
863
 * Use our template to display invoices.
864
 * 
865
 * @param string $template the template that is currently being used.
866
 */
867
function wpinv_template( $template ) {
868
    global $post;
869
870
    if ( ! is_admin() && ( is_single() || is_404() ) && ! empty( $post->ID ) && getpaid_is_invoice_post_type( get_post_type( $post->ID ) ) ) {
871
872
        // If the user can view this invoice, display it.
873
        if ( wpinv_user_can_view_invoice( $post->ID ) ) {
874
875
            return wpinv_get_template_part( 'wpinv-invoice-print', false, false );
876
877
        // Else display an error message.
878
        } else {
879
880
            return wpinv_get_template_part( 'wpinv-invalid-access', false, false );
881
882
        }
883
884
    }
885
886
    return $template;
887
}
888
add_filter( 'template_include', 'wpinv_template', 10, 1 );
889
890
function wpinv_get_business_address() {
891
    $business_address   = wpinv_store_address();
892
    $business_address   = !empty( $business_address ) ? wpautop( wp_kses_post( $business_address ) ) : '';
893
    
894
    $business_address = $business_address ? '<div class="address">' . $business_address . '</div>' : '';
895
    
896
    return apply_filters( 'wpinv_get_business_address', $business_address );
897
}
898
899
/**
900
 * Displays the company address.
901
 */
902
function wpinv_display_from_address() {
903
    wpinv_get_template( 'invoice/company-address.php' );
904
}
905
add_action( 'getpaid_invoice_details_left', 'wpinv_display_from_address', 10 );
906
907
function wpinv_watermark( $id = 0 ) {
908
    $output = wpinv_get_watermark( $id );
909
    return apply_filters( 'wpinv_get_watermark', $output, $id );
910
}
911
912
function wpinv_get_watermark( $id ) {
913
    if ( !$id > 0 ) {
914
        return NULL;
915
    }
916
917
    $invoice = wpinv_get_invoice( $id );
918
    
919
    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...
920
        if ( $invoice->is_paid() ) {
921
            return __( 'Paid', 'invoicing' );
922
        }
923
        if ( $invoice->is_refunded() ) {
924
            return __( 'Refunded', 'invoicing' );
925
        }
926
        if ( $invoice->has_status( array( 'wpi-cancelled' ) ) ) {
927
            return __( 'Cancelled', 'invoicing' );
928
        }
929
    }
930
    
931
    return NULL;
932
}
933
934
/**
935
 * @deprecated
936
 */
937
function wpinv_display_invoice_details( $invoice ) {
938
    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...
939
}
940
941
/**
942
 * Displays invoice meta.
943
 */
944
function getpaid_invoice_meta( $invoice ) {
945
946
    $invoice = new WPInv_Invoice( $invoice );
947
948
    // Ensure that we have an invoice.
949
    if ( 0 == $invoice->get_id() ) {
950
        return;
951
    }
952
953
    // Load the invoice meta.
954
    $meta    = array(
955
956
        'number' => array(
957
            'label' => sprintf(
958
                __( '%s Number', 'invoicing' ),
959
                ucfirst( $invoice->get_type() )
960
            ),
961
            'value' => sanitize_text_field( $invoice->get_number() ),
962
        ),
963
964
        'status' => array(
965
            'label' => sprintf(
966
                __( '%s Status', 'invoicing' ),
967
                ucfirst( $invoice->get_type() )
968
            ),
969
            'value' => sanitize_text_field( $invoice->get_status_nicename() ),
970
        ),
971
972
        'date' => array(
973
            'label' => sprintf(
974
                __( '%s Date', 'invoicing' ),
975
                ucfirst( $invoice->get_type() )
976
            ),
977
            'value' => getpaid_format_date( $invoice->get_created_date() ),
978
        ),
979
980
        'date_paid' => array(
981
            'label' => __( 'Paid On', 'invoicing' ),
982
            'value' => getpaid_format_date( $invoice->get_completed_date() ),
983
        ),
984
985
        'transaction_id' => array(
986
            'label' => __( 'Transaction ID', 'invoicing' ),
987
            'value' => sanitize_text_field( $invoice->get_transaction_id() ),
988
        ),
989
990
        'due_date'  => array(
991
            'label' => __( 'Due Date', 'invoicing' ),
992
            'value' => getpaid_format_date( $invoice->get_due_date() ),
993
        ),
994
995
        'vat_number' => array(
996
            'label' => sprintf(
997
                __( '%s Number', 'invoicing' ),
998
                $GLOBALS['wpinv_euvat']->get_vat_name()
999
            ),
1000
            'value' => sanitize_text_field( $invoice->get_vat_number() ),
1001
        ),
1002
1003
    );
1004
1005
    // If it is not paid, remove the date of payment.
1006
    if ( ! $invoice->is_paid() ) {
1007
        unset( $meta[ 'date_paid' ] );
1008
        unset( $meta[ 'transaction_id' ] );
1009
    }
1010
1011
    // Only display the due date if due dates are enabled.
1012
    if ( ! $invoice->needs_payment() || ! wpinv_get_option( 'overdue_active' ) ) {
1013
        unset( $meta[ 'due_date' ] );
1014
    }
1015
1016
    // Only display the vat number if taxes are enabled.
1017
    if ( ! wpinv_use_taxes() ) {
1018
        unset( $meta[ 'vat_number' ] );
1019
    }
1020
1021
    if ( $invoice->is_recurring() ) {
1022
1023
        // Link to the parent invoice.
1024
        if ( $invoice->is_renewal() ) {
1025
1026
            $meta[ 'parent' ] = array(
1027
1028
                'label' => sprintf(
1029
                    __( 'Parent %s', 'invoicing' ),
1030
                    ucfirst( $invoice->get_type() )
1031
                ),
1032
1033
                'value' => wpinv_invoice_link( $invoice->get_parent_id() ),
1034
1035
            );
1036
1037
        }
1038
1039
        $subscription = wpinv_get_subscription( $invoice );
1040
1041
        if ( ! empty ( $subscription ) ) {
1042
1043
            // Display the renewal date.
1044
            if ( $subscription->is_active() && 'cancelled' != $subscription->status ) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $subscription->is_active() targeting WPInv_Subscription::is_active() seems to always return null.

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

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

}

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

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

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

Loading history...
1045
1046
                $meta[ 'renewal_date' ] = array(
1047
1048
                    'label' => __( 'Renews On', 'invoicing' ),
1049
                    'value' => getpaid_format_date( $subscription->expiration ),
1050
        
1051
                );
1052
1053
            }
1054
1055
            if ( $invoice->is_parent() ) {
1056
1057
                // Display the recurring amount.
1058
                $meta[ 'recurring_total' ] = array(
1059
1060
                    'label' => __( 'Recurring Amount', 'invoicing' ),
1061
                    'value' => wpinv_price( wpinv_format_amount( $subscription->recurring_amount ), $invoice->get_currency() ),
1062
        
1063
                );
1064
1065
            }
1066
            
1067
        }
1068
    }
1069
1070
    // Add the invoice total to the meta.
1071
    $meta[ 'invoice_total' ] = array(
1072
1073
        'label' => __( 'Total Amount', 'invoicing' ),
1074
        'value' => wpinv_price( wpinv_format_amount( $invoice->get_total() ), $invoice->get_currency() ),
1075
1076
    );
1077
1078
    // Provide a way for third party plugins to filter the meta.
1079
    $meta = apply_filters( 'getpaid_invoice_meta_data', $meta, $invoice );
1080
1081
    wpinv_get_template( 'invoice/invoice-meta.php', compact( 'invoice', 'meta' ) );
1082
1083
}
1084
add_action( 'getpaid_invoice_details_right', 'getpaid_invoice_meta', 10 );
1085
1086
/**
1087
 * Retrieves the address markup to use on Invoices.
1088
 * 
1089
 * @since 1.0.13
1090
 * @see `wpinv_get_full_address_format`
1091
 * @see `wpinv_get_invoice_address_replacements`
1092
 * @param array $billing_details customer's billing details
1093
 * @param  string $separator How to separate address lines.
1094
 * @return string
1095
 */
1096
function wpinv_get_invoice_address_markup( $billing_details, $separator = '<br/>' ) {
1097
1098
    // Retrieve the address markup...
1099
    $country= empty( $billing_details['country'] ) ? '' : $billing_details['country'];
1100
    $format = wpinv_get_full_address_format( $country );
1101
1102
    // ... and the replacements.
1103
    $replacements = wpinv_get_invoice_address_replacements( $billing_details );
1104
1105
    $formatted_address = str_ireplace( array_keys( $replacements ), $replacements, $format );
1106
    
1107
	// Remove unavailable tags.
1108
    $formatted_address = preg_replace( "/\{\{\w+\}\}/", '', $formatted_address );
1109
1110
    // Clean up white space.
1111
	$formatted_address = preg_replace( '/  +/', ' ', trim( $formatted_address ) );
1112
    $formatted_address = preg_replace( '/\n\n+/', "\n", $formatted_address );
1113
    
1114
    // Break newlines apart and remove empty lines/trim commas and white space.
1115
	$formatted_address = array_filter( array_map( 'wpinv_trim_formatted_address_line', explode( "\n", $formatted_address ) ) );
1116
1117
    // Add html breaks.
1118
	$formatted_address = implode( $separator, $formatted_address );
1119
1120
	// We're done!
1121
	return $formatted_address;
1122
    
1123
}
1124
1125
/**
1126
 * Displays the billing address.
1127
 * 
1128
 * @param WPInv_Invoice $invoice
1129
 */
1130
function wpinv_display_to_address( $invoice = 0 ) {
1131
    if ( ! empty( $invoice ) ) {
1132
        wpinv_get_template( 'invoice/billing-address.php', compact( 'invoice' ) );
1133
    }
1134
}
1135
add_action( 'getpaid_invoice_details_left', 'wpinv_display_to_address', 40 );
1136
1137
1138
/**
1139
 * Displays invoice line items.
1140
 */
1141
function wpinv_display_line_items( $invoice_id = 0 ) {
1142
1143
    // Prepare the invoice.
1144
    $invoice = new WPInv_Invoice( $invoice_id );
1145
1146
    // Abort if there is no invoice.
1147
    if ( 0 == $invoice->get_id() ) {
1148
        return;
1149
    }
1150
1151
    // Line item columns.
1152
    $columns = getpaid_invoice_item_columns( $invoice );
1153
    $columns = apply_filters( 'getpaid_invoice_line_items_table_columns', $columns, $invoice );
1154
1155
    wpinv_get_template( 'invoice/line-items.php', compact( 'invoice', 'columns' ) );
1156
}
1157
add_action( 'getpaid_invoice_line_items', 'wpinv_display_line_items', 10 );
1158
1159
/**
1160
 * @param WPInv_Invoice $invoice
1161
 */
1162
function wpinv_display_invoice_notes( $invoice ) {
1163
1164
    // Retrieve the notes.
1165
    $notes = wpinv_get_invoice_notes( $invoice->get_id(), 'customer' );
1166
1167
    // Abort if we have non.
1168
    if ( empty( $notes ) ) {
1169
        return;
1170
    }
1171
1172
    // Echo the note.
1173
    echo '<div class="getpaid-invoice-notes-wrapper border position-relative w-100 mb-4 p-0">';
1174
    echo '<h3 class="getpaid-invoice-notes-title text-dark bg-light border-bottom m-0 d-block">' . __( 'Notes', 'invoicing' ) .'</h3>';
1175
    echo '<ul class="getpaid-invoice-notes mt-4 p-0">';
1176
1177
    foreach( $notes as $note ) {
1178
        wpinv_get_invoice_note_line_item( $note );
1179
    }
1180
1181
    echo '</ul>';
1182
    echo '</div>';
1183
}
1184
add_action( 'getpaid_invoice_line_items', 'wpinv_display_invoice_notes', 60 );
1185
1186
/**
1187
 * Loads scripts on our invoice templates.
1188
 */
1189
function wpinv_display_style() {
1190
1191
    // Make sure that all scripts have been loaded.
1192
    if ( ! did_action( 'wp_enqueue_scripts' ) ) {
1193
        do_action( 'wp_enqueue_scripts' );
1194
    }
1195
1196
    // Register the invoices style.
1197
    wp_register_style( 'wpinv-single-style', WPINV_PLUGIN_URL . 'assets/css/invoice.css', array(), filemtime( WPINV_PLUGIN_DIR . 'assets/css/invoice.css' ) );
1198
1199
    // Load required styles
1200
    wp_print_styles( 'open-sans' );
1201
    wp_print_styles( 'wpinv-single-style' );
1202
    wp_print_styles( 'ayecode-ui' );
1203
1204
    // Maybe load custom css.
1205
    $custom_css = wpinv_get_option( 'template_custom_css' );
1206
1207
    if ( isset( $custom_css ) && ! empty( $custom_css ) ) {
1208
        $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

1208
        $custom_css     = wp_kses( $custom_css, /** @scrutinizer ignore-type */ array( '\'', '\"' ) );
Loading history...
1209
        $custom_css     = str_replace( '&gt;', '>', $custom_css );
1210
        echo '<style type="text/css">';
1211
        echo $custom_css;
1212
        echo '</style>';
1213
    }
1214
1215
}
1216
add_action( 'wpinv_invoice_print_head', 'wpinv_display_style' );
1217
add_action( 'wpinv_invalid_invoice_head', 'wpinv_display_style' );
1218
1219
1220
/**
1221
 * Displays the checkout page.
1222
 */
1223
function wpinv_checkout_form() {
1224
    global $wpi_checkout_id;
1225
1226
    // Retrieve the current invoice.
1227
    $invoice_id = getpaid_get_current_invoice_id();
1228
1229
    if ( empty( $invoice_id ) ) {
1230
1231
        return aui()->alert(
1232
            array(
1233
                'type'    => 'warning',
1234
                'content' => __( 'Invalid invoice', 'invoicing' ),
1235
            )
1236
        );
1237
1238
    }
1239
1240
    // Can the user view this invoice?
1241
    if ( ! wpinv_user_can_view_invoice( $invoice_id ) ) {
1242
1243
        return aui()->alert(
1244
            array(
1245
                'type'    => 'warning',
1246
                'content' => __( 'You are not allowed to view this invoice', 'invoicing' ),
1247
            )
1248
        );
1249
1250
    }
1251
1252
    // Ensure that it is not yet paid for.
1253
    $invoice = new WPInv_Invoice( $invoice_id );
1254
1255
    // Maybe mark it as viewed.
1256
    getpaid_maybe_mark_invoice_as_viewed( $invoice );
1257
1258
    if ( $invoice->is_paid() ) {
1259
1260
        return aui()->alert(
1261
            array(
1262
                'type'    => 'success',
1263
                'content' => __( 'This invoice has already been paid.', 'invoicing' ),
1264
            )
1265
        );
1266
1267
    }
1268
1269
    // Set the global invoice id.
1270
    $wpi_checkout_id = $invoice_id;
1271
1272
    // We'll display this invoice via the default form.
1273
    $form = new GetPaid_Payment_Form( wpinv_get_default_payment_form() );
1274
1275
    if ( 0 == $form->get_id() ) {
1276
1277
        return aui()->alert(
1278
            array(
1279
                'type'    => 'warning',
1280
                'content' => __( 'Error loading the payment form', 'invoicing' ),
1281
            )
1282
        );
1283
1284
    }
1285
1286
    // Set the invoice.
1287
    $form->invoice = $invoice;
1288
    $form->set_items( $invoice->get_items() );
1289
1290
    // Generate the html.
1291
    return $form->get_html();
1292
1293
}
1294
1295
function wpinv_checkout_cart( $cart_details = array(), $echo = true ) {
1296
    global $ajax_cart_details;
1297
    $ajax_cart_details = $cart_details;
1298
1299
    ob_start();
1300
    do_action( 'wpinv_before_checkout_cart' );
1301
    echo '<div id="wpinv_checkout_cart_form" method="post">';
1302
        echo '<div id="wpinv_checkout_cart_wrap">';
1303
            wpinv_get_template_part( 'wpinv-checkout-cart' );
1304
        echo '</div>';
1305
    echo '</div>';
1306
    do_action( 'wpinv_after_checkout_cart' );
1307
    $content = ob_get_clean();
1308
1309
    if ( $echo ) {
1310
        echo $content;
1311
    } else {
1312
        return $content;
1313
    }
1314
}
1315
add_action( 'wpinv_checkout_cart', 'wpinv_checkout_cart', 10 );
1316
1317
function wpinv_empty_cart_message() {
1318
	return apply_filters( 'wpinv_empty_cart_message', '<span class="wpinv_empty_cart">' . __( 'Your cart is empty.', 'invoicing' ) . '</span>' );
1319
}
1320
1321
/**
1322
 * Echoes the Empty Cart Message
1323
 *
1324
 * @since 1.0
1325
 * @return void
1326
 */
1327
function wpinv_empty_checkout_cart() {
1328
    echo aui()->alert(
1329
        array(
1330
            'type'    => 'warning',
1331
            'content' => wpinv_empty_cart_message(),
1332
        )
1333
    );
1334
}
1335
add_action( 'wpinv_cart_empty', 'wpinv_empty_checkout_cart' );
1336
1337
function wpinv_checkout_cart_columns() {
1338
    $default = 3;
1339
    if ( wpinv_item_quantities_enabled() ) {
1340
        $default++;
1341
    }
1342
1343
    if ( wpinv_use_taxes() ) {
1344
        $default++;
1345
    }
1346
1347
    return apply_filters( 'wpinv_checkout_cart_columns', $default );
1348
}
1349
1350
function wpinv_receipt_billing_address( $invoice_id = 0 ) {
1351
    $invoice = wpinv_get_invoice( $invoice_id );
1352
1353
    if ( empty( $invoice ) ) {
1354
        return NULL;
1355
    }
1356
1357
    $billing_details = $invoice->get_user_info();
1358
    $address_row = wpinv_get_invoice_address_markup( $billing_details );
1359
1360
    ob_start();
1361
    ?>
1362
    <table class="table table-bordered table-sm wpi-billing-details">
1363
        <tbody>
1364
            <tr class="wpi-receipt-name">
1365
                <th class="text-left"><?php _e( 'Name', 'invoicing' ); ?></th>
1366
                <td><?php echo esc_html( trim( $billing_details['first_name'] . ' ' . $billing_details['last_name'] ) ) ;?></td>
1367
            </tr>
1368
            <tr class="wpi-receipt-email">
1369
                <th class="text-left"><?php _e( 'Email', 'invoicing' ); ?></th>
1370
                <td><?php echo $billing_details['email'] ;?></td>
1371
            </tr>
1372
            <tr class="wpi-receipt-address">
1373
                <th class="text-left"><?php _e( 'Address', 'invoicing' ); ?></th>
1374
                <td><?php echo $address_row ;?></td>
1375
            </tr>
1376
            <?php if ( $billing_details['phone'] ) { ?>
1377
            <tr class="wpi-receipt-phone">
1378
                <th class="text-left"><?php _e( 'Phone', 'invoicing' ); ?></th>
1379
                <td><?php echo esc_html( $billing_details['phone'] ) ;?></td>
1380
            </tr>
1381
            <?php } ?>
1382
        </tbody>
1383
    </table>
1384
    <?php
1385
    $output = ob_get_clean();
1386
    
1387
    $output = apply_filters( 'wpinv_receipt_billing_address', $output, $invoice_id );
1388
1389
    echo $output;
1390
}
1391
1392
/**
1393
 * Filters the receipt page.
1394
 */
1395
function wpinv_filter_success_page_content( $content ) {
1396
1397
    // Ensure this is our page.
1398
    if ( isset( $_GET['payment-confirm'] ) && wpinv_is_success_page() ) {
1399
1400
        $gateway = sanitize_text_field( $_GET['payment-confirm'] );
1401
        return apply_filters( "wpinv_payment_confirm_$gateway", $content );
1402
1403
    }
1404
1405
    return $content;
1406
}
1407
add_filter( 'the_content', 'wpinv_filter_success_page_content', 99999 );
1408
1409
function wpinv_invoice_link( $invoice_id ) {
1410
    $invoice = wpinv_get_invoice( $invoice_id );
1411
1412
    if ( empty( $invoice ) ) {
1413
        return NULL;
1414
    }
1415
1416
    $invoice_link = '<a href="' . esc_url( $invoice->get_view_url() ) . '">' . $invoice->get_number() . '</a>';
1417
1418
    return apply_filters( 'wpinv_get_invoice_link', $invoice_link, $invoice );
1419
}
1420
1421
function wpinv_invoice_subscription_details( $invoice ) {
1422
    if ( !empty( $invoice ) && $invoice->is_recurring() && ! wpinv_is_subscription_payment( $invoice ) ) {
1423
        $subscription = wpinv_get_subscription( $invoice, true );
0 ignored issues
show
Unused Code introduced by
The call to wpinv_get_subscription() has too many arguments starting with true. ( Ignorable by Annotation )

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

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

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
1424
1425
        if ( empty( $subscription ) ) {
1426
            return;
1427
        }
1428
1429
        $frequency = WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency($subscription->period, $subscription->frequency);
1430
        $billing = wpinv_price(wpinv_format_amount($subscription->recurring_amount), $invoice->get_currency() ) . ' / ' . $frequency;
1431
        $initial = wpinv_price(wpinv_format_amount($subscription->initial_amount), $invoice->get_currency() );
1432
1433
        $payments = $subscription->get_child_payments();
1434
        ?>
1435
        <div class="wpinv-subscriptions-details">
1436
            <h3 class="wpinv-subscriptions-t"><?php echo apply_filters( 'wpinv_subscription_details_title', __( 'Subscription Details', 'invoicing' ) ); ?></h3>
1437
            <table class="table">
1438
                <thead>
1439
                    <tr>
1440
                        <th><?php _e( 'Billing Cycle', 'invoicing' ) ;?></th>
1441
                        <th><?php _e( 'Start Date', 'invoicing' ) ;?></th>
1442
                        <th><?php _e( 'Expiration Date', 'invoicing' ) ;?></th>
1443
                        <th class="text-center"><?php _e( 'Times Billed', 'invoicing' ) ;?></th>
1444
                        <th class="text-center"><?php _e( 'Status', 'invoicing' ) ;?></th>
1445
                    </tr>
1446
                </thead>
1447
                <tbody>
1448
                    <tr>
1449
                        <td><?php printf(_x('%s then %s', 'Initial subscription amount then billing cycle and amount', 'invoicing'), $initial, $billing); ?></td>
1450
                        <td><?php echo date_i18n(get_option('date_format'), strtotime($subscription->created, current_time('timestamp'))); ?></td>
0 ignored issues
show
Bug introduced by
It seems like get_option('date_format') can also be of type false; however, parameter $format of date_i18n() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

1450
                        <td><?php echo date_i18n(/** @scrutinizer ignore-type */ get_option('date_format'), strtotime($subscription->created, current_time('timestamp'))); ?></td>
Loading history...
1451
                        <td><?php echo date_i18n(get_option('date_format'), strtotime($subscription->expiration, current_time('timestamp'))); ?></td>
1452
                        <td class="text-center"><?php echo $subscription->get_times_billed() . ' / ' . (($subscription->bill_times == 0) ? 'Until Cancelled' : $subscription->bill_times); ?></td>
1453
                        <td class="text-center wpi-sub-status"><?php echo $subscription->get_status_label(); ?></td>
1454
                    </tr>
1455
                </tbody>
1456
            </table>
1457
        </div>
1458
        <?php if ( !empty( $payments ) ) { ?>
1459
        <div class="wpinv-renewal-payments">
1460
            <h3 class="wpinv-renewals-t"><?php echo apply_filters( 'wpinv_renewal_payments_title', __( 'Renewal Payments', 'invoicing' ) ); ?></h3>
1461
            <table class="table">
1462
                <thead>
1463
                    <tr>
1464
                        <th>#</th>
1465
                        <th><?php _e( 'Invoice', 'invoicing' ) ;?></th>
1466
                        <th><?php _e( 'Date', 'invoicing' ) ;?></th>
1467
                        <th class="text-right"><?php _e( 'Amount', 'invoicing' ) ;?></th>
1468
                    </tr>
1469
                </thead>
1470
                <tbody>
1471
                    <?php
1472
                        $i = 1;
1473
                        foreach ( $payments as $payment ) {
1474
                            $invoice_id = $payment->ID;
1475
                    ?>
1476
                    <tr>
1477
                        <th scope="row"><?php echo $i;?></th>
1478
                        <td><?php echo wpinv_invoice_link( $invoice_id ) ;?></td>
1479
                        <td><?php echo$invoice->get_date_created(); ?></td>
1480
                        <td class="text-right"><?php echo wpinv_price( wpinv_format_amount( $invoice->get_total() ), $invoice->get_currency() ); ?></td>
1481
                    </tr>
1482
                    <?php $i++; } ?>
1483
                </tbody>
1484
            </table>
1485
        </div>
1486
        <?php } ?>
1487
        <?php
1488
    }
1489
}
1490
add_action( 'getpaid_invoice_line_items', 'wpinv_invoice_subscription_details', 20 );
1491
1492
function wpinv_cart_total_label( $label, $invoice ) {
1493
    if ( empty( $invoice ) ) {
1494
        return $label;
1495
    }
1496
1497
    $prefix_label = '';
1498
    if ( $invoice->is_parent() && $item_id = $invoice->get_recurring() ) {
0 ignored issues
show
Unused Code introduced by
The assignment to $item_id is dead and can be removed.
Loading history...
1499
        $prefix_label   = '<span class="label label-primary label-recurring">' . __( 'Recurring Payment', 'invoicing' ) . '</span> ' . wpinv_subscription_payment_desc( $invoice );
1500
    } else if ( $invoice->is_renewal() ) {
1501
        $prefix_label   = '<span class="label label-primary label-renewal">' . __( 'Renewal Payment', 'invoicing' ) . '</span> ';        
1502
    }
1503
1504
    if ( $prefix_label != '' ) {
1505
        $label  = '<span class="wpinv-cart-sub-desc">' . $prefix_label . '</span> ' . $label;
1506
    }
1507
1508
    return $label;
1509
}
1510
add_filter( 'wpinv_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
1511
add_filter( 'wpinv_email_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
1512
add_filter( 'wpinv_print_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
1513
1514
function wpinv_get_invoice_note_line_item( $note, $echo = true ) {
1515
    if ( empty( $note ) ) {
1516
        return NULL;
1517
    }
1518
1519
    if ( is_int( $note ) ) {
1520
        $note = get_comment( $note );
1521
    }
1522
1523
    if ( !( is_object( $note ) && is_a( $note, 'WP_Comment' ) ) ) {
1524
        return NULL;
1525
    }
1526
1527
    $note_classes   = array( 'note' );
1528
    $note_classes[] = get_comment_meta( $note->comment_ID, '_wpi_customer_note', true ) ? 'customer-note' : '';
1529
    $note_classes[] = $note->comment_author === 'System' ? 'system-note' : '';
1530
    $note_classes   = apply_filters( 'wpinv_invoice_note_class', array_filter( $note_classes ), $note );
1531
    $note_classes   = !empty( $note_classes ) ? implode( ' ', $note_classes ) : '';
1532
1533
    ob_start();
1534
    ?>
1535
    <li rel="<?php echo absint( $note->comment_ID ) ; ?>" class="<?php echo esc_attr( $note_classes ); ?> mt-4 pl-3 pr-3">
1536
        <div class="note_content bg-light border position-relative p-4">
1537
1538
            <?php echo wpautop( wptexturize( wp_kses_post( $note->comment_content ) ) ); ?>
1539
1540
            <?php if ( ! is_admin() ) : ?>
1541
                <em class="meta position-absolute form-text">
1542
                    <?php
1543
                        printf(
1544
                            __( '%1$s - %2$s at %3$s', 'invoicing' ),
1545
                            $note->comment_author,
1546
                            date_i18n( get_option( 'date_format' ), strtotime( $note->comment_date ) ),
0 ignored issues
show
Bug introduced by
It seems like get_option('date_format') can also be of type false; however, parameter $format of date_i18n() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

1546
                            date_i18n( /** @scrutinizer ignore-type */ get_option( 'date_format' ), strtotime( $note->comment_date ) ),
Loading history...
1547
                            date_i18n( get_option( 'time_format' ), strtotime( $note->comment_date ) )
1548
                        );
1549
                    ?>
1550
                </em>
1551
            <?php endif; ?>
1552
1553
        </div>
1554
1555
        <?php if ( is_admin() ) : ?>
1556
1557
            <p class="meta px-4 py-2">
1558
                <abbr class="exact-date" title="<?php echo esc_attr( $note->comment_date ); ?>"><?php printf( __( '%1$s - %2$s at %3$s', 'invoicing' ), $note->comment_author, date_i18n( get_option( 'date_format' ), strtotime( $note->comment_date ) ), date_i18n( get_option( 'time_format' ), strtotime( $note->comment_date ) ) ); ?></abbr>&nbsp;&nbsp;
1559
                <?php if ( $note->comment_author !== 'System' && wpinv_current_user_can_manage_invoicing() ) { ?>
1560
                    <a href="#" class="delete_note"><?php _e( 'Delete note', 'invoicing' ); ?></a>
1561
                <?php } ?>
1562
            </p>
1563
1564
        <?php endif; ?>
1565
        
1566
    </li>
1567
    <?php
1568
    $note_content = ob_get_clean();
1569
    $note_content = apply_filters( 'wpinv_get_invoice_note_line_item', $note_content, $note, $echo );
1570
1571
    if ( $echo ) {
1572
        echo $note_content;
1573
    } else {
1574
        return $note_content;
1575
    }
1576
}
1577
1578
function wpinv_invalid_invoice_content() {
1579
    global $post;
1580
1581
    $invoice = wpinv_get_invoice( $post->ID );
1582
1583
    $error = __( 'This invoice is only viewable by clicking on the invoice link that was sent to you via email.', 'invoicing' );
1584
    if ( !empty( $invoice->get_id() ) && $invoice->has_status( array_keys( wpinv_get_invoice_statuses() ) ) ) {
1585
        if ( is_user_logged_in() ) {
1586
            if ( wpinv_require_login_to_checkout() ) {
1587
                if ( isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
1588
                    $error = __( 'You are not allowed to view this invoice.', 'invoicing' );
1589
                }
1590
            }
1591
        } else {
1592
            if ( wpinv_require_login_to_checkout() ) {
1593
                if ( isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
1594
                    $error = __( 'You must be logged in to view this invoice.', 'invoicing' );
1595
                }
1596
            }
1597
        }
1598
    } else {
1599
        $error = __( 'This invoice is deleted or does not exist.', 'invoicing' );
1600
    }
1601
    ?>
1602
    <div class="row wpinv-row-invalid">
1603
        <div class="col-md-6 col-md-offset-3 wpinv-message error">
1604
            <h3><?php _e( 'Access Denied', 'invoicing' ); ?></h3>
1605
            <p class="wpinv-msg-text"><?php echo $error; ?></p>
1606
        </div>
1607
    </div>
1608
    <?php
1609
}
1610
add_action( 'wpinv_invalid_invoice_content', 'wpinv_invalid_invoice_content' );
1611
1612
/**
1613
 * Function to get privacy policy text.
1614
 *
1615
 * @since 1.0.13
1616
 * @return string
1617
 */
1618
function wpinv_get_policy_text() {
1619
    $privacy_page_id = get_option( 'wp_page_for_privacy_policy', 0 );
1620
1621
    $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]' ));
1622
1623
    if(!$privacy_page_id){
1624
        $privacy_page_id = wpinv_get_option( 'privacy_page', 0 );
1625
    }
1626
1627
    $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

1627
    $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...
1628
1629
    $find_replace = array(
1630
        '[wpinv_privacy_policy]' => $privacy_link,
1631
    );
1632
1633
    $privacy_text = str_replace( array_keys( $find_replace ), array_values( $find_replace ), $text );
1634
1635
    return wp_kses_post(wpautop($privacy_text));
1636
}
1637
1638
1639
/**
1640
 * Allows the user to set their own price for an invoice item
1641
 */
1642
function wpinv_checkout_cart_item_name_your_price( $cart_item, $key ) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

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

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

Loading history...
1643
    
1644
    //Ensure we have an item id
1645
    if(! is_array( $cart_item ) || empty( $cart_item['id'] ) ) {
1646
        return;
1647
    }
1648
1649
    //Fetch the item
1650
    $item_id = $cart_item['id'];
1651
    $item    = new WPInv_Item( $item_id );
1652
    
1653
    if(! $item->supports_dynamic_pricing() || !$item->get_is_dynamic_pricing() ) {
1654
        return;
1655
    }
1656
1657
    //Fetch the dynamic pricing "strings"
1658
    $suggested_price_text = esc_html( wpinv_get_option( 'suggested_price_text', __( 'Suggested Price:', 'invoicing' ) ) );
1659
    $minimum_price_text   = esc_html( wpinv_get_option( 'minimum_price_text', __( 'Minimum Price:', 'invoicing' ) ) );
1660
    $name_your_price_text = esc_html( wpinv_get_option( 'name_your_price_text', __( 'Name Your Price', 'invoicing' ) ) );
1661
1662
    //Display a "name_your_price" button
1663
    echo " &mdash; <a href='#' class='wpinv-name-your-price-frontend small'>$name_your_price_text</a></div>";
1664
1665
    //Display a name_your_price form
1666
    echo '<div class="name-your-price-miniform">';
1667
    
1668
    //Maybe display the recommended price
1669
    if( $item->get_price() > 0 && !empty( $suggested_price_text ) ) {
1670
        $suggested_price = $item->get_the_price();
1671
        echo "<div>$suggested_price_text &mdash; $suggested_price</div>";
1672
    }
1673
1674
    //Display the update price form
1675
    $symbol         = wpinv_currency_symbol();
1676
    $position       = wpinv_currency_position();
1677
    $minimum        = esc_attr( $item->get_minimum_price() );
1678
    $price          = esc_attr( $cart_item['item_price'] );
1679
    $update         = esc_attr__( "Update", 'invoicing' );
1680
1681
    //Ensure it supports dynamic prici
1682
    if( $price < $minimum ) {
1683
        $price = $minimum;
1684
    }
1685
1686
    echo '<label>';
1687
    echo $position != 'right' ? $symbol . '&nbsp;' : '';
1688
    echo "<input type='number' min='$minimum' placeholder='$price' value='$price' class='wpi-field-price' />";
1689
    echo $position == 'right' ? '&nbsp;' . $symbol : '' ;
1690
    echo "</label>";
1691
    echo "<input type='hidden' value='$item_id' class='wpi-field-item' />";
1692
    echo "<a class='btn btn-success wpinv-submit wpinv-update-dynamic-price-frontend'>$update</a>";
1693
1694
    //Maybe display the minimum price
1695
    if( $item->get_minimum_price() > 0 && !empty( $minimum_price_text ) ) {
1696
        $minimum_price = wpinv_price( wpinv_format_amount( $item->get_minimum_price() ) );
1697
        echo "<div>$minimum_price_text &mdash; $minimum_price</div>";
1698
    }
1699
1700
    echo "</div>";
1701
1702
}
1703
add_action( 'wpinv_checkout_cart_item_price_after', 'wpinv_checkout_cart_item_name_your_price', 10, 2 );
1704
1705
function wpinv_oxygen_fix_conflict() {
1706
    global $ct_ignore_post_types;
1707
1708
    if ( ! is_array( $ct_ignore_post_types ) ) {
1709
        $ct_ignore_post_types = array();
1710
    }
1711
1712
    $post_types = array( 'wpi_discount', 'wpi_invoice', 'wpi_item' );
1713
1714
    foreach ( $post_types as $post_type ) {
1715
        $ct_ignore_post_types[] = $post_type;
1716
1717
        // Ignore post type
1718
        add_filter( 'pre_option_oxygen_vsb_ignore_post_type_' . $post_type, '__return_true', 999 );
1719
    }
1720
1721
    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

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