Passed
Pull Request — master (#275)
by Brian
06:25
created

wpinv_get_checkout_fields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Contains functions related to Invoicing plugin.
4
 *
5
 * @since 1.0.0
6
 * @package Invoicing
7
 */
8
 
9
// MUST have WordPress.
10
if ( !defined( 'WPINC' ) ) {
11
    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
12
}
13
14
if ( !is_admin() ) {
15
    add_filter( 'template_include', 'wpinv_template', 10, 1 );
16
    add_action( 'wpinv_invoice_print_body_start', 'wpinv_display_invoice_top_bar' );
17
    add_action( 'wpinv_invoice_top_bar_left', 'wpinv_invoice_display_left_actions' );
18
    add_action( 'wpinv_invoice_top_bar_right', 'wpinv_invoice_display_right_actions' );
19
}
20
21
function wpinv_template_path() {
22
    return apply_filters( 'wpinv_template_path', wpinv_get_theme_template_dir_name() );
23
}
24
25
function wpinv_display_invoice_top_bar( $invoice ) {
26
    if ( empty( $invoice ) ) {
27
        return;
28
    }
29
    ?>
30
    <div class="row wpinv-top-bar no-print">
31
        <div class="container">
32
            <div class="col-xs-6">
33
                <?php do_action( 'wpinv_invoice_top_bar_left', $invoice );?>
34
            </div>
35
            <div class="col-xs-6 text-right">
36
                <?php do_action( 'wpinv_invoice_top_bar_right', $invoice );?>
37
            </div>
38
        </div>
39
    </div>
40
    <?php
41
}
42
43
function wpinv_invoice_display_left_actions( $invoice ) {
44
    if ( empty( $invoice ) ) {
45
        return; // Exit if invoice is not set.
46
    }
47
    
48
    if ( $invoice->post_type == 'wpi_invoice' ) {
49
        if ( $invoice->needs_payment() ) {
50
            ?> <a class="btn btn-success btn-sm" title="<?php esc_attr_e( 'Pay This Invoice', 'invoicing' ); ?>" href="<?php echo esc_url( $invoice->get_checkout_payment_url() ); ?>"><?php _e( 'Pay For Invoice', 'invoicing' ); ?></a><?php
51
        }
52
    }
53
    do_action('wpinv_invoice_display_left_actions', $invoice);
54
}
55
56
function wpinv_invoice_display_right_actions( $invoice ) {
57
    if ( empty( $invoice ) ) {
58
        return; // Exit if invoice is not set.
59
    }
60
61
    if ( $invoice->post_type == 'wpi_invoice' ) { ?>
62
        <a class="btn btn-primary btn-sm btn-print-invoice" onclick="window.print();" href="javascript:void(0)"><?php _e( 'Print Invoice', 'invoicing' ); ?></a>
63
        <?php if ( is_user_logged_in() ) { ?>
64
        &nbsp;&nbsp;<a class="btn btn-warning btn-sm btn-invoice-history" href="<?php echo esc_url( wpinv_get_history_page_uri() ); ?>"><?php _e( 'Invoice History', 'invoicing' ); ?></a>
0 ignored issues
show
Bug introduced by
It seems like wpinv_get_history_page_uri() 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

64
        &nbsp;&nbsp;<a class="btn btn-warning btn-sm btn-invoice-history" href="<?php echo esc_url( /** @scrutinizer ignore-type */ wpinv_get_history_page_uri() ); ?>"><?php _e( 'Invoice History', 'invoicing' ); ?></a>
Loading history...
65
        <?php }
66
    }
67
    do_action('wpinv_invoice_display_right_actions', $invoice);
68
}
69
70
function wpinv_before_invoice_content( $content ) {
71
    global $post;
72
73
    if ( !empty( $post ) && $post->post_type == 'wpi_invoice' && is_singular( 'wpi_invoice' ) && is_main_query() ) {
74
        ob_start();
75
        do_action( 'wpinv_before_invoice_content', $post->ID );
76
        $content = ob_get_clean() . $content;
77
    }
78
79
    return $content;
80
}
81
add_filter( 'the_content', 'wpinv_before_invoice_content' );
82
83
function wpinv_after_invoice_content( $content ) {
84
    global $post;
85
86
    if ( !empty( $post ) && $post->post_type == 'wpi_invoice' && is_singular( 'wpi_invoice' ) && is_main_query() ) {
87
        ob_start();
88
        do_action( 'wpinv_after_invoice_content', $post->ID );
89
        $content .= ob_get_clean();
90
    }
91
92
    return $content;
93
}
94
add_filter( 'the_content', 'wpinv_after_invoice_content' );
95
96
function wpinv_get_templates_dir() {
97
    return WPINV_PLUGIN_DIR . 'templates';
98
}
99
100
function wpinv_get_templates_url() {
101
    return WPINV_PLUGIN_URL . 'templates';
102
}
103
104
function wpinv_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
105
    if ( ! empty( $args ) && is_array( $args ) ) {
106
		extract( $args );
107
	}
108
109
	$located = wpinv_locate_template( $template_name, $template_path, $default_path );
110
	// Allow 3rd party plugin filter template file from their plugin.
111
	$located = apply_filters( 'wpinv_get_template', $located, $template_name, $args, $template_path, $default_path );
112
113
	if ( ! file_exists( $located ) ) {
114
        _doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $located ), '2.1' );
115
		return;
116
	}
117
118
	do_action( 'wpinv_before_template_part', $template_name, $template_path, $located, $args );
119
120
	include( $located );
121
122
	do_action( 'wpinv_after_template_part', $template_name, $template_path, $located, $args );
123
}
124
125
function wpinv_get_template_html( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
126
	ob_start();
127
	wpinv_get_template( $template_name, $args, $template_path, $default_path );
128
	return ob_get_clean();
129
}
130
131
function wpinv_locate_template( $template_name, $template_path = '', $default_path = '' ) {
132
    if ( ! $template_path ) {
133
        $template_path = wpinv_template_path();
134
    }
135
136
    if ( ! $default_path ) {
137
        $default_path = WPINV_PLUGIN_DIR . 'templates/';
138
    }
139
140
    // Look within passed path within the theme - this is priority.
141
    $template = locate_template(
142
        array(
143
            trailingslashit( $template_path ) . $template_name,
144
            $template_name
145
        )
146
    );
147
148
    // Get default templates/
149
    if ( !$template && $default_path ) {
150
        $template = trailingslashit( $default_path ) . $template_name;
151
    }
152
153
    // Return what we found.
154
    return apply_filters( 'wpinv_locate_template', $template, $template_name, $template_path );
155
}
156
157
function wpinv_get_template_part( $slug, $name = null, $load = true ) {
158
	do_action( 'get_template_part_' . $slug, $slug, $name );
159
160
	// Setup possible parts
161
	$templates = array();
162
	if ( isset( $name ) )
163
		$templates[] = $slug . '-' . $name . '.php';
164
	$templates[] = $slug . '.php';
165
166
	// Allow template parts to be filtered
167
	$templates = apply_filters( 'wpinv_get_template_part', $templates, $slug, $name );
168
169
	// Return the part that is found
170
	return wpinv_locate_tmpl( $templates, $load, false );
171
}
172
173
function wpinv_locate_tmpl( $template_names, $load = false, $require_once = true ) {
174
	// No file found yet
175
	$located = false;
176
177
	// Try to find a template file
178
	foreach ( (array)$template_names as $template_name ) {
179
180
		// Continue if template is empty
181
		if ( empty( $template_name ) )
182
			continue;
183
184
		// Trim off any slashes from the template name
185
		$template_name = ltrim( $template_name, '/' );
186
187
		// try locating this template file by looping through the template paths
188
		foreach( wpinv_get_theme_template_paths() as $template_path ) {
189
190
			if( file_exists( $template_path . $template_name ) ) {
191
				$located = $template_path . $template_name;
192
				break;
193
			}
194
		}
195
196
		if( !empty( $located ) ) {
197
			break;
198
		}
199
	}
200
201
	if ( ( true == $load ) && ! empty( $located ) )
202
		load_template( $located, $require_once );
203
204
	return $located;
205
}
206
207
function wpinv_get_theme_template_paths() {
208
	$template_dir = wpinv_get_theme_template_dir_name();
209
210
	$file_paths = array(
211
		1 => trailingslashit( get_stylesheet_directory() ) . $template_dir,
212
		10 => trailingslashit( get_template_directory() ) . $template_dir,
213
		100 => wpinv_get_templates_dir()
214
	);
215
216
	$file_paths = apply_filters( 'wpinv_template_paths', $file_paths );
217
218
	// sort the file paths based on priority
219
	ksort( $file_paths, SORT_NUMERIC );
220
221
	return array_map( 'trailingslashit', $file_paths );
222
}
223
224
function wpinv_get_theme_template_dir_name() {
225
	return trailingslashit( apply_filters( 'wpinv_templates_dir', 'invoicing' ) );
226
}
227
228
function wpinv_checkout_meta_tags() {
229
230
	$pages   = array();
231
	$pages[] = wpinv_get_option( 'success_page' );
232
	$pages[] = wpinv_get_option( 'failure_page' );
233
	$pages[] = wpinv_get_option( 'invoice_history_page' );
234
	$pages[] = wpinv_get_option( 'invoice_subscription_page' );
235
236
	if( !wpinv_is_checkout() && !is_page( $pages ) ) {
237
		return;
238
	}
239
240
	echo '<meta name="robots" content="noindex,nofollow" />' . "\n";
241
}
242
add_action( 'wp_head', 'wpinv_checkout_meta_tags' );
243
244
function wpinv_add_body_classes( $class ) {
245
	$classes = (array)$class;
246
247
	if( wpinv_is_checkout() ) {
248
		$classes[] = 'wpinv-checkout';
249
		$classes[] = 'wpinv-page';
250
	}
251
252
	if( wpinv_is_success_page() ) {
253
		$classes[] = 'wpinv-success';
254
		$classes[] = 'wpinv-page';
255
	}
256
257
	if( wpinv_is_failed_transaction_page() ) {
258
		$classes[] = 'wpinv-failed-transaction';
259
		$classes[] = 'wpinv-page';
260
	}
261
262
	if( wpinv_is_invoice_history_page() ) {
263
		$classes[] = 'wpinv-history';
264
		$classes[] = 'wpinv-page';
265
	}
266
267
	if( wpinv_is_subscriptions_history_page() ) {
268
		$classes[] = 'wpinv-subscription';
269
		$classes[] = 'wpinv-page';
270
	}
271
272
	if( wpinv_is_test_mode() ) {
273
		$classes[] = 'wpinv-test-mode';
274
		$classes[] = 'wpinv-page';
275
	}
276
277
	return array_unique( $classes );
278
}
279
add_filter( 'body_class', 'wpinv_add_body_classes' );
280
281
function wpinv_html_dropdown( $name = 'wpinv_discounts', $selected = 0, $status = '' ) {
282
    $args = array( 'nopaging' => true );
283
284
    if ( ! empty( $status ) )
285
        $args['post_status'] = $status;
286
287
    $discounts = wpinv_get_discounts( $args );
288
    $options   = array();
289
290
    if ( $discounts ) {
291
        foreach ( $discounts as $discount ) {
292
            $options[ absint( $discount->ID ) ] = esc_html( get_the_title( $discount->ID ) );
293
        }
294
    } else {
295
        $options[0] = __( 'No discounts found', 'invoicing' );
296
    }
297
298
    $output = wpinv_html_select( array(
299
        'name'             => $name,
300
        'selected'         => $selected,
301
        'options'          => $options,
302
        'show_option_all'  => false,
303
        'show_option_none' => false,
304
    ) );
305
306
    return $output;
307
}
308
309
function wpinv_html_year_dropdown( $name = 'year', $selected = 0, $years_before = 5, $years_after = 0 ) {
310
    $current     = date( 'Y' );
311
    $start_year  = $current - absint( $years_before );
312
    $end_year    = $current + absint( $years_after );
313
    $selected    = empty( $selected ) ? date( 'Y' ) : $selected;
314
    $options     = array();
315
316
    while ( $start_year <= $end_year ) {
317
        $options[ absint( $start_year ) ] = $start_year;
318
        $start_year++;
319
    }
320
321
    $output = wpinv_html_select( array(
322
        'name'             => $name,
323
        'selected'         => $selected,
324
        'options'          => $options,
325
        'show_option_all'  => false,
326
        'show_option_none' => false
327
    ) );
328
329
    return $output;
330
}
331
332
function wpinv_html_month_dropdown( $name = 'month', $selected = 0 ) {
333
334
    $options = array(
335
        '1'  => __( 'January', 'invoicing' ),
336
        '2'  => __( 'February', 'invoicing' ),
337
        '3'  => __( 'March', 'invoicing' ),
338
        '4'  => __( 'April', 'invoicing' ),
339
        '5'  => __( 'May', 'invoicing' ),
340
        '6'  => __( 'June', 'invoicing' ),
341
        '7'  => __( 'July', 'invoicing' ),
342
        '8'  => __( 'August', 'invoicing' ),
343
        '9'  => __( 'September', 'invoicing' ),
344
        '10' => __( 'October', 'invoicing' ),
345
        '11' => __( 'November', 'invoicing' ),
346
        '12' => __( 'December', 'invoicing' ),
347
    );
348
349
    // If no month is selected, default to the current month
350
    $selected = empty( $selected ) ? date( 'n' ) : $selected;
351
352
    $output = wpinv_html_select( array(
353
        'name'             => $name,
354
        'selected'         => $selected,
355
        'options'          => $options,
356
        'show_option_all'  => false,
357
        'show_option_none' => false
358
    ) );
359
360
    return $output;
361
}
362
363
function wpinv_html_select( $args = array() ) {
364
    $defaults = array(
365
        'options'          => array(),
366
        'name'             => null,
367
        'class'            => '',
368
        'id'               => '',
369
        'selected'         => 0,
370
        'placeholder'      => null,
371
        'multiple'         => false,
372
        'show_option_all'  => _x( 'All', 'all dropdown items', 'invoicing' ),
373
        'show_option_none' => _x( 'None', 'no dropdown items', 'invoicing' ),
374
        'data'             => array(),
375
        'onchange'         => null,
376
        'required'         => false,
377
        'disabled'         => false,
378
        'readonly'         => false,
379
    );
380
381
    $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.

2 paths for user data to reach this point

  1. Path: Read from $_GET in includes/admin/wpinv-admin-functions.php on line 363
  1. Read from $_GET
    in includes/admin/wpinv-admin-functions.php on line 363
  2. wpinv_html_select() is called
    in includes/admin/wpinv-admin-functions.php on line 359
  3. Enters via parameter $args
    in includes/wpinv-template-functions.php on line 363
  2. Path: Read from $_GET, and $_GET['vat_class'] is assigned to $class in includes/admin/wpinv-admin-functions.php on line 378
  1. Read from $_GET, and $_GET['vat_class'] is assigned to $class
    in includes/admin/wpinv-admin-functions.php on line 378
  2. wpinv_html_select() is called
    in includes/admin/wpinv-admin-functions.php on line 381
  3. Enters via parameter $args
    in includes/wpinv-template-functions.php on line 363

Used in variable context

  1. wp_parse_args() is called
    in includes/wpinv-template-functions.php on line 381
  2. Enters via parameter $args
    in wordpress/wp-includes/functions.php on line 4294
  3. wp_parse_str() is called
    in wordpress/wp-includes/functions.php on line 4300
  4. Enters via parameter $string
    in wordpress/wp-includes/formatting.php on line 4865
  5. parse_str() is called
    in wordpress/wp-includes/formatting.php on line 4866

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...
382
383
    if ( isset( $args['field_required'] ) ) {
384
        $args['required'] = $args['field_required'];
385
    }
386
387
    $data_elements = '';
388
    foreach ( $args['data'] as $key => $value ) {
389
        $data_elements .= ' data-' . esc_attr( $key ) . '="' . esc_attr( $value ) . '"';
390
    }
391
392
    if( $args['multiple'] ) {
393
        $multiple = ' MULTIPLE';
394
    } else {
395
        $multiple = '';
396
    }
397
398
    if( $args['placeholder'] ) {
399
        $placeholder = $args['placeholder'];
400
    } else {
401
        $placeholder = '';
402
    }
403
    
404
    $options = '';
405
    if( !empty( $args['onchange'] ) ) {
406
        $options .= ' onchange="' . esc_attr( $args['onchange'] ) . '"';
407
    }
408
    
409
    if( !empty( $args['required'] ) ) {
410
        $options .= ' required="required"';
411
    }
412
    
413
    if( !empty( $args['disabled'] ) ) {
414
        $options .= ' disabled';
415
    }
416
    
417
    if( !empty( $args['readonly'] ) ) {
418
        $options .= ' readonly';
419
    }
420
421
    $class  = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
422
    $output = '<select name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( $args['id'] ) . '" class="wpinv-select ' . $class . '"' . $multiple . ' data-placeholder="' . $placeholder . '" ' . trim( $options ) . $data_elements . '>';
423
424
    if ( $args['show_option_all'] ) {
425
        if( $args['multiple'] ) {
426
            $selected = selected( true, in_array( 0, $args['selected'] ), false );
427
        } else {
428
            $selected = selected( $args['selected'], 0, false );
429
        }
430
        $output .= '<option value="all"' . $selected . '>' . esc_html( $args['show_option_all'] ) . '</option>';
431
    }
432
433
    if ( !empty( $args['options'] ) ) {
434
435
        if ( $args['show_option_none'] ) {
436
            if( $args['multiple'] ) {
437
                $selected = selected( true, in_array( "", $args['selected'] ), false );
438
            } else {
439
                $selected = selected( $args['selected'] === "", true, false );
440
            }
441
            $output .= '<option value=""' . $selected . '>' . esc_html( $args['show_option_none'] ) . '</option>';
442
        }
443
444
        foreach( $args['options'] as $key => $option ) {
445
446
            if( $args['multiple'] && is_array( $args['selected'] ) ) {
447
                $selected = selected( true, (bool)in_array( $key, $args['selected'] ), false );
448
            } else {
449
                $selected = selected( $args['selected'], $key, false );
450
            }
451
452
            $output .= '<option value="' . esc_attr( $key ) . '"' . $selected . '>' . esc_html( $option ) . '</option>';
453
        }
454
    }
455
456
    $output .= '</select>';
457
458
    return $output;
459
}
460
461
function wpinv_html_state_select( $args = array() ) {
462
463
    $key = 'country';
464
    if ( ! empty( $args['key'] ) ) {
465
        $key = $args['key'];
466
    }
467
468
    $selected_country = empty( $args[ $key ] ) ? wpinv_default_billing_country() : $args[ $key ];
469
470
    if ( ! empty( $args['billing_details'] ) && ! empty( $args['billing_details']['country'] ) ) {
471
        $selected_country = $args['billing_details']['country'];
472
    }
473
474
    $states = wpinv_get_country_states( $selected_country );
475
476
    if( !empty( $states ) ) {
477
        return wpinv_html_select( array(
478
            'options'          => $states,
479
            'name'             => $args['name'],
480
            'id'               => $args['id'],
481
            'selected'         => $args['value'],
482
            'show_option_all'  => false,
483
            'show_option_none' => false,
484
            'class'            => 'wpi-input form-control wpi_select2',
485
            'placeholder'      => $args['placeholder'],
486
            'required'         => $args['field_required'],
487
        ) );
488
    }
489
490
    return wpinv_html_text( array(
491
            'name'          => 'wpinv_state',
492
            'value'         => $args['value'],
493
            'id'            => 'wpinv_state',
494
            'class'         => 'wpi-input form-control',
495
            'placeholder'   => $args['placeholder'],
496
            'required'      => $args['field_required'],
497
        ) );
498
499
}
500
501
function wpinv_item_dropdown( $args = array() ) {
502
    $defaults = array(
503
        'name'              => 'wpi_item',
504
        'id'                => 'wpi_item',
505
        'class'             => '',
506
        'multiple'          => false,
507
        'selected'          => 0,
508
        'number'            => 100,
509
        'placeholder'       => __( 'Choose a item', 'invoicing' ),
510
        'data'              => array( 'search-type' => 'item' ),
511
        'show_option_all'   => false,
512
        'show_option_none'  => false,
513
        'show_recurring'    => false,
514
    );
515
516
    $args = wp_parse_args( $args, $defaults );
517
518
    $item_args = array(
519
        'post_type'      => 'wpi_item',
520
        'orderby'        => 'title',
521
        'order'          => 'ASC',
522
        'posts_per_page' => $args['number']
523
    );
524
    
525
    $item_args  = apply_filters( 'wpinv_item_dropdown_query_args', $item_args, $args, $defaults );
526
527
    $items      = get_posts( $item_args );
528
    $options    = array();
529
    if ( $items ) {
530
        foreach ( $items as $item ) {
531
            $title = esc_html( $item->post_title );
532
            
533
            if ( !empty( $args['show_recurring'] ) ) {
534
                $title .= wpinv_get_item_suffix( $item->ID, false );
535
            }
536
            
537
            $options[ absint( $item->ID ) ] = $title;
538
        }
539
    }
540
541
    // This ensures that any selected items are included in the drop down
542
    if( is_array( $args['selected'] ) ) {
543
        foreach( $args['selected'] as $item ) {
544
            if( ! in_array( $item, $options ) ) {
545
                $title = get_the_title( $item );
546
                if ( !empty( $args['show_recurring'] ) ) {
547
                    $title .= wpinv_get_item_suffix( $item, false );
548
                }
549
                $options[$item] = $title;
550
            }
551
        }
552
    } elseif ( is_numeric( $args['selected'] ) && $args['selected'] !== 0 ) {
553
        if ( ! in_array( $args['selected'], $options ) ) {
554
            $title = get_the_title( $args['selected'] );
555
            if ( !empty( $args['show_recurring'] ) ) {
556
                $title .= wpinv_get_item_suffix( $args['selected'], false );
557
            }
558
            $options[$args['selected']] = get_the_title( $args['selected'] );
559
        }
560
    }
561
562
    $output = wpinv_html_select( array(
563
        'name'             => $args['name'],
564
        'selected'         => $args['selected'],
565
        'id'               => $args['id'],
566
        'class'            => $args['class'],
567
        'options'          => $options,
568
        'multiple'         => $args['multiple'],
569
        'placeholder'      => $args['placeholder'],
570
        'show_option_all'  => $args['show_option_all'],
571
        'show_option_none' => $args['show_option_none'],
572
        'data'             => $args['data'],
573
    ) );
574
575
    return $output;
576
}
577
578
function wpinv_html_checkbox( $args = array() ) {
579
    $defaults = array(
580
        'name'     => null,
581
        'current'  => null,
582
        'class'    => 'wpinv-checkbox',
583
        'options'  => array(
584
            'disabled' => false,
585
            'readonly' => false
586
        )
587
    );
588
589
    $args = wp_parse_args( $args, $defaults );
590
591
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
592
    $options = '';
593
    if ( ! empty( $args['options']['disabled'] ) ) {
594
        $options .= ' disabled="disabled"';
595
    } elseif ( ! empty( $args['options']['readonly'] ) ) {
596
        $options .= ' readonly';
597
    }
598
599
    $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 ) . ' />';
600
601
    return $output;
602
}
603
604
function wpinv_html_text( $args = array() ) {
605
    // Backwards compatibility
606
    if ( func_num_args() > 1 ) {
607
        $args = func_get_args();
608
609
        $name  = $args[0];
610
        $value = isset( $args[1] ) ? $args[1] : '';
611
        $label = isset( $args[2] ) ? $args[2] : '';
612
        $desc  = isset( $args[3] ) ? $args[3] : '';
613
    }
614
615
    $defaults = array(
616
        'id'           => '',
617
        'name'         => isset( $name )  ? $name  : 'text',
618
        'value'        => isset( $value ) ? $value : null,
619
        'label'        => isset( $label ) ? $label : null,
620
        'desc'         => isset( $desc )  ? $desc  : null,
621
        'placeholder'  => '',
622
        'class'        => 'regular-text',
623
        'disabled'     => false,
624
        'readonly'     => false,
625
        'required'     => false,
626
        'autocomplete' => '',
627
        'data'         => false
628
    );
629
630
    $args = wp_parse_args( $args, $defaults );
631
632
    if ( isset( $args['field_required'] ) ) {
633
        $args['required'] = $args['field_required'];
634
    }
635
636
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
637
    $options = '';
638
    if( $args['required'] ) {
639
        $options .= ' required="required"';
640
    }
641
    if( $args['readonly'] ) {
642
        $options .= ' readonly';
643
    }
644
    if( $args['readonly'] ) {
645
        $options .= ' readonly';
646
    }
647
648
    $data = '';
649
    if ( !empty( $args['data'] ) ) {
650
        foreach ( $args['data'] as $key => $value ) {
651
            $data .= 'data-' . wpinv_sanitize_key( $key ) . '="' . esc_attr( $value ) . '" ';
652
        }
653
    }
654
655
    $output = '<span id="wpinv-' . wpinv_sanitize_key( $args['name'] ) . '-wrap">';
656
    $output .= '<label class="wpinv-label" for="' . wpinv_sanitize_key( $args['id'] ) . '">' . esc_html( $args['label'] ) . '</label>';
657
    if ( ! empty( $args['desc'] ) ) {
658
        $output .= '<span class="wpinv-description">' . esc_html( $args['desc'] ) . '</span>';
659
    }
660
661
    $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 ) . '/>';
662
663
    $output .= '</span>';
664
665
    return $output;
666
}
667
668
function wpinv_html_date_field( $args = array() ) {
669
    if( empty( $args['class'] ) ) {
670
        $args['class'] = 'wpiDatepicker';
671
    } elseif( ! strpos( $args['class'], 'wpiDatepicker' ) ) {
672
        $args['class'] .= ' wpiDatepicker';
673
    }
674
675
    return wpinv_html_text( $args );
676
}
677
678
function wpinv_html_textarea( $args = array() ) {
679
    $defaults = array(
680
        'name'        => 'textarea',
681
        'value'       => null,
682
        'label'       => null,
683
        'desc'        => null,
684
        'class'       => 'large-text',
685
        'disabled'    => false
686
    );
687
688
    $args = wp_parse_args( $args, $defaults );
689
690
    $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
691
    $disabled = '';
692
    if( $args['disabled'] ) {
693
        $disabled = ' disabled="disabled"';
694
    }
695
696
    $output = '<span id="wpinv-' . wpinv_sanitize_key( $args['name'] ) . '-wrap">';
697
    $output .= '<label class="wpinv-label" for="' . wpinv_sanitize_key( $args['name'] ) . '">' . esc_html( $args['label'] ) . '</label>';
698
    $output .= '<textarea name="' . esc_attr( $args['name'] ) . '" id="' . wpinv_sanitize_key( $args['name'] ) . '" class="' . $class . '"' . $disabled . '>' . esc_attr( $args['value'] ) . '</textarea>';
699
700
    if ( ! empty( $args['desc'] ) ) {
701
        $output .= '<span class="wpinv-description">' . esc_html( $args['desc'] ) . '</span>';
702
    }
703
    $output .= '</span>';
704
705
    return $output;
706
}
707
708
function wpinv_html_ajax_user_search( $args = array() ) {
709
    $defaults = array(
710
        'name'        => 'user_id',
711
        'value'       => null,
712
        'placeholder' => __( 'Enter username', 'invoicing' ),
713
        'label'       => null,
714
        'desc'        => null,
715
        'class'       => '',
716
        'disabled'    => false,
717
        'autocomplete'=> 'off',
718
        'data'        => false
719
    );
720
721
    $args = wp_parse_args( $args, $defaults );
722
723
    $args['class'] = 'wpinv-ajax-user-search ' . $args['class'];
724
725
    $output  = '<span class="wpinv_user_search_wrap">';
726
        $output .= wpinv_html_text( $args );
727
        $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>';
728
    $output .= '</span>';
729
730
    return $output;
731
}
732
733
function wpinv_ip_geolocation() {
734
    global $wpinv_euvat;
735
    
736
    $ip         = !empty( $_GET['ip'] ) ? sanitize_text_field( $_GET['ip'] ) : '';    
737
    $content    = '';
738
    $iso        = '';
739
    $country    = '';
740
    $region     = '';
741
    $city       = '';
742
    $longitude  = '';
743
    $latitude   = '';
744
    $credit     = '';
745
    $address    = '';
746
    
747
    if ( wpinv_get_option( 'vat_ip_lookup' ) == 'geoip2' && $geoip2_city = $wpinv_euvat->geoip2_city_record( $ip ) ) {
748
        try {
749
            $iso        = $geoip2_city->country->isoCode;
750
            $country    = $geoip2_city->country->name;
751
            $region     = !empty( $geoip2_city->subdivisions ) && !empty( $geoip2_city->subdivisions[0]->name ) ? $geoip2_city->subdivisions[0]->name : '';
752
            $city       = $geoip2_city->city->name;
753
            $longitude  = $geoip2_city->location->longitude;
754
            $latitude   = $geoip2_city->location->latitude;
755
            $credit     = __( 'Geolocated using the information by MaxMind, available from <a href="http://www.maxmind.com" target="_blank">www.maxmind.com</a>', 'invoicing' );
756
        } catch( Exception $e ) { }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
757
    }
758
    
759
    if ( !( $iso && $longitude && $latitude ) && function_exists( 'simplexml_load_file' ) ) {
760
        try {
761
            $load_xml = simplexml_load_file( 'http://www.geoplugin.net/xml.gp?ip=' . $ip );
762
            
763
            if ( !empty( $load_xml ) && isset( $load_xml->geoplugin_countryCode ) && !empty( $load_xml->geoplugin_latitude ) && !empty( $load_xml->geoplugin_longitude ) ) {
764
                $iso        = $load_xml->geoplugin_countryCode;
765
                $country    = $load_xml->geoplugin_countryName;
766
                $region     = !empty( $load_xml->geoplugin_regionName ) ? $load_xml->geoplugin_regionName : '';
767
                $city       = !empty( $load_xml->geoplugin_city ) ? $load_xml->geoplugin_city : '';
768
                $longitude  = $load_xml->geoplugin_longitude;
769
                $latitude   = $load_xml->geoplugin_latitude;
770
                $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...
771
                $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;
772
            }
773
        } catch( Exception $e ) { }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
774
    }
775
    
776
    if ( $iso && $longitude && $latitude ) {
777
        if ( $city ) {
778
            $address .= $city . ', ';
779
        }
780
        
781
        if ( $region ) {
782
            $address .= $region . ', ';
783
        }
784
        
785
        $address .= $country . ' (' . $iso . ')';
786
        $content = '<p>'. sprintf( __( '<b>Address:</b> %s', 'invoicing' ), $address ) . '</p>';
787
        $content .= '<p>'. $credit . '</p>';
788
    } else {
789
        $content = '<p>'. sprintf( __( 'Unable to find geolocation for the IP address: %s', 'invoicing' ), $ip ) . '</p>';
790
    }
791
    ?>
792
<!DOCTYPE html>
793
<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>
794
<body>
795
    <?php if ( $latitude && $latitude ) { ?>
796
    <div id="map"></div>
797
        <script src="//cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.0-rc.1/leaflet.js"></script>
798
        <script type="text/javascript">
799
        var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
800
            osmAttrib = '&copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors',
801
            osm = L.tileLayer(osmUrl, {maxZoom: 18, attribution: osmAttrib}),
802
            latlng = new L.LatLng(<?php echo $latitude;?>, <?php echo $longitude;?>);
803
804
        var map = new L.Map('map', {center: latlng, zoom: 12, layers: [osm]});
805
806
        var marker = new L.Marker(latlng);
807
        map.addLayer(marker);
808
809
        marker.bindPopup("<p><?php esc_attr_e( $address );?></p>");
810
    </script>
811
    <?php } ?>
812
    <div style="height:100px"><?php echo $content; ?></div>
813
</body></html>
814
<?php
815
    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...
816
}
817
add_action( 'wp_ajax_wpinv_ip_geolocation', 'wpinv_ip_geolocation' );
818
add_action( 'wp_ajax_nopriv_wpinv_ip_geolocation', 'wpinv_ip_geolocation' );
819
820
// Set up the template for the invoice.
821
function wpinv_template( $template ) {
822
    global $post, $wp_query;
823
    
824
    if ( ( is_single() || is_404() ) && !empty( $post->ID ) && (get_post_type( $post->ID ) == 'wpi_invoice' or get_post_type( $post->ID ) == 'wpi_quote')) {
825
        if ( wpinv_user_can_view_invoice( $post->ID ) ) {
826
            $template = wpinv_get_template_part( 'wpinv-invoice-print', false, false );
827
        } else {
828
            $template = wpinv_get_template_part( 'wpinv-invalid-access', false, false );
829
        }
830
    }
831
832
    return $template;
833
}
834
835
function wpinv_get_business_address() {
836
    $business_address   = wpinv_store_address();
837
    $business_address   = !empty( $business_address ) ? wpautop( wp_kses_post( $business_address ) ) : '';
838
    
839
    /*
840
    $default_country    = wpinv_get_default_country();
841
    $default_state      = wpinv_get_default_state();
842
    
843
    $address_fields = array();
844
    if ( !empty( $default_state ) ) {
845
        $address_fields[] = wpinv_state_name( $default_state, $default_country );
846
    }
847
    
848
    if ( !empty( $default_country ) ) {
849
        $address_fields[] = wpinv_country_name( $default_country );
850
    }
851
    
852
    if ( !empty( $address_fields ) ) {
853
        $address_fields = implode( ", ", $address_fields );
854
                
855
        $business_address .= wpautop( wp_kses_post( $address_fields ) );
856
    }
857
    */
858
    
859
    $business_address = $business_address ? '<div class="address">' . $business_address . '</div>' : '';
860
    
861
    return apply_filters( 'wpinv_get_business_address', $business_address );
862
}
863
864
function wpinv_display_from_address() {
865
    global $wpinv_euvat;
866
    
867
    $from_name = $wpinv_euvat->get_company_name();
868
    if (empty($from_name)) {
869
        $from_name = wpinv_get_business_name();
870
    }
871
    ?><div class="from col-xs-2"><strong><?php _e( 'From:', 'invoicing' ) ?></strong></div>
872
    <div class="wrapper col-xs-10">
873
        <div class="name"><?php echo esc_html( $from_name ); ?></div>
874
        <?php if ( $address = wpinv_get_business_address() ) { ?>
875
        <div class="address"><?php echo wpautop( wp_kses_post( $address ) );?></div>
876
        <?php } ?>
877
        <?php if ( $email_from = wpinv_mail_get_from_address() ) { ?>
878
        <div class="email_from"><?php echo wp_sprintf( __( 'Email: %s', 'invoicing' ), $email_from );?></div>
879
        <?php } ?>
880
    </div>
881
    <?php
882
}
883
884
function wpinv_watermark( $id = 0 ) {
885
    $output = wpinv_get_watermark( $id );
886
    
887
    return apply_filters( 'wpinv_get_watermark', $output, $id );
888
}
889
890
function wpinv_get_watermark( $id ) {
891
    if ( !$id > 0 ) {
892
        return NULL;
893
    }
894
    $invoice = wpinv_get_invoice( $id );
895
    
896
    if ( !empty( $invoice ) && "wpi_invoice" === $invoice->post_type ) {
897
        if ( $invoice->is_paid() ) {
898
            return __( 'Paid', 'invoicing' );
899
        }
900
        if ( $invoice->is_refunded() ) {
901
            return __( 'Refunded', 'invoicing' );
902
        }
903
        if ( $invoice->has_status( array( 'wpi-cancelled' ) ) ) {
904
            return __( 'Cancelled', 'invoicing' );
905
        }
906
    }
907
    
908
    return NULL;
909
}
910
911
function wpinv_display_invoice_details( $invoice ) {
912
    global $wpinv_euvat;
913
    
914
    $invoice_id = $invoice->ID;
915
    $vat_name   = $wpinv_euvat->get_vat_name();
916
    $use_taxes  = wpinv_use_taxes();
917
    
918
    $invoice_status = wpinv_get_invoice_status( $invoice_id );
919
    ?>
920
    <table class="table table-bordered table-sm">
921
        <?php if ( $invoice_number = wpinv_get_invoice_number( $invoice_id ) ) { ?>
922
            <tr class="wpi-row-number">
923
                <th><?php echo apply_filters( 'wpinv_invoice_number_label', __( 'Invoice Number', 'invoicing' ), $invoice ); ?></th>
924
                <td><?php echo esc_html( $invoice_number ); ?></td>
925
            </tr>
926
        <?php } ?>
927
        <tr class="wpi-row-status">
928
            <th><?php echo apply_filters( 'wpinv_invoice_status_label', __( 'Invoice Status', 'invoicing' ), $invoice ); ?></th>
929
            <td><?php echo wpinv_invoice_status_label( $invoice_status, wpinv_get_invoice_status( $invoice_id, true ) ); ?></td>
930
        </tr>
931
        <?php if ( $invoice->is_renewal() ) { ?>
932
        <tr class="wpi-row-parent">
933
            <th><?php echo apply_filters( 'wpinv_invoice_parent_invoice_label', __( 'Parent Invoice', 'invoicing' ), $invoice ); ?></th>
934
            <td><?php echo wpinv_invoice_link( $invoice->parent_invoice ); ?></td>
935
        </tr>
936
        <?php } ?>
937
        <?php if ( ( $gateway_name = wpinv_get_payment_gateway_name( $invoice_id ) ) && ( $invoice->is_paid() || $invoice->is_refunded() ) ) { ?>
938
            <tr class="wpi-row-gateway">
939
                <th><?php echo apply_filters( 'wpinv_invoice_payment_method_label', __( 'Payment Method', 'invoicing' ), $invoice ); ?></th>
940
                <td><?php echo $gateway_name; ?></td>
941
            </tr>
942
        <?php } ?>
943
        <?php if ( $invoice_date = wpinv_get_invoice_date( $invoice_id ) ) { ?>
944
            <tr class="wpi-row-date">
945
                <th><?php echo apply_filters( 'wpinv_invoice_date_label', __( 'Invoice Date', 'invoicing' ), $invoice ); ?></th>
946
                <td><?php echo $invoice_date; ?></td>
947
            </tr>
948
        <?php } ?>
949
        <?php do_action( 'wpinv_display_details_before_due_date', $invoice_id ); ?>
950
        <?php if ( wpinv_get_option( 'overdue_active' ) && $invoice->needs_payment() && ( $due_date = $invoice->get_due_date( true ) ) ) { ?>
951
            <tr class="wpi-row-date">
952
                <th><?php echo apply_filters( 'wpinv_invoice_due_date_label', __( 'Due Date', 'invoicing' ), $invoice ); ?></th>
953
                <td><?php echo $due_date; ?></td>
954
            </tr>
955
        <?php } ?>
956
        <?php do_action( 'wpinv_display_details_after_due_date', $invoice_id ); ?>
957
        <?php if ( $owner_vat_number = $wpinv_euvat->get_vat_number() ) { ?>
958
            <tr class="wpi-row-ovatno">
959
                <th><?php echo apply_filters( 'wpinv_invoice_owner_vat_number_label', wp_sprintf( __( 'Owner %s Number', 'invoicing' ), $vat_name ), $invoice, $vat_name ); ?></th>
960
                <td><?php echo $owner_vat_number; ?></td>
961
            </tr>
962
        <?php } ?>
963
        <?php if ( $use_taxes && ( $user_vat_number = wpinv_get_invoice_vat_number( $invoice_id ) ) ) { ?>
964
            <tr class="wpi-row-uvatno">
965
                <th><?php echo apply_filters( 'wpinv_invoice_user_vat_number_label', wp_sprintf( __( 'Invoice %s Number', 'invoicing' ), $vat_name ), $invoice, $vat_name ); ?></th>
966
                <td><?php echo $user_vat_number; ?></td>
967
            </tr>
968
        <?php } ?>
969
        <tr class="table-active tr-total wpi-row-total">
970
            <th><strong><?php _e( 'Total Amount', 'invoicing' ) ?></strong></th>
971
            <td><strong><?php echo wpinv_payment_total( $invoice_id, true ); ?></strong></td>
972
        </tr>
973
    </table>
974
<?php
975
}
976
977
/**
978
 * Retrieves the address markup to use on Invoices.
979
 * 
980
 * @since 1.0.13
981
 * @see `wpinv_get_full_address_format`
982
 * @see `wpinv_get_invoice_address_replacements`
983
 * @param array $billing_details customer's billing details
984
 * @param  string $separator How to separate address lines.
985
 * @return string
986
 */
987
function wpinv_get_invoice_address_markup( $billing_details, $separator = '<br/>' ) {
988
989
    // Retrieve the address markup...
990
    $country= empty( $billing_details['country'] ) ? '' : $billing_details['country'];
991
    $format = wpinv_get_full_address_format( $country );
992
993
    // ... and the replacements.
994
    $replacements = wpinv_get_invoice_address_replacements( $billing_details );
995
996
    $formatted_address = str_ireplace( array_keys( $replacements ), $replacements, $format );
997
    
998
	// Remove unavailable tags.
999
    $formatted_address = preg_replace( "/\{\{\w+\}\}/", '', $formatted_address );
1000
1001
    // Clean up white space.
1002
	$formatted_address = preg_replace( '/  +/', ' ', trim( $formatted_address ) );
1003
    $formatted_address = preg_replace( '/\n\n+/', "\n", $formatted_address );
1004
    
1005
    // Break newlines apart and remove empty lines/trim commas and white space.
1006
	$formatted_address = array_filter( array_map( 'wpinv_trim_formatted_address_line', explode( "\n", $formatted_address ) ) );
1007
1008
    // Add html breaks.
1009
	$formatted_address = implode( $separator, $formatted_address );
1010
1011
	// We're done!
1012
	return $formatted_address;
1013
    
1014
}
1015
1016
function wpinv_display_to_address( $invoice_id = 0 ) {
1017
    $invoice = wpinv_get_invoice( $invoice_id );
1018
    
1019
    if ( empty( $invoice ) ) {
1020
        return NULL;
1021
    }
1022
    
1023
    $billing_details = $invoice->get_user_info();
1024
    $output = '<div class="to col-xs-2"><strong>' . __( 'To:', 'invoicing' ) . '</strong></div>';
1025
    $output .= '<div class="wrapper col-xs-10">';
1026
    
1027
    ob_start();
1028
    do_action( 'wpinv_display_to_address_top', $invoice );
1029
    $output .= ob_get_clean();
1030
    
1031
    $address_row = wpinv_get_invoice_address_markup( $billing_details );
1032
1033
    if ( $address_row ) {
1034
        $output .= '<div class="address">' . $address_row . '</div>';
1035
    }
1036
1037
    if ( $phone = $invoice->get_phone() ) {
1038
        $output .= '<div class="phone">' . wp_sprintf( __( 'Phone: %s', 'invoicing' ), esc_html( $phone ) ) . '</div>';
1039
    }
1040
    if ( $email = $invoice->get_email() ) {
1041
        $output .= '<div class="email">' . wp_sprintf( __( 'Email: %s' , 'invoicing'), esc_html( $email ) ) . '</div>';
1042
    }
1043
1044
    ob_start();
1045
    do_action( 'wpinv_display_to_address_bottom', $invoice );
1046
    $output .= ob_get_clean();
1047
    
1048
    $output .= '</div>';
1049
    $output = apply_filters( 'wpinv_display_to_address', $output, $invoice );
1050
1051
    echo $output;
1052
}
1053
1054
function wpinv_display_line_items( $invoice_id = 0 ) {
1055
    global $wpinv_euvat, $ajax_cart_details;
1056
    $invoice            = wpinv_get_invoice( $invoice_id );
1057
    $quantities_enabled = wpinv_item_quantities_enabled();
1058
    $use_taxes          = wpinv_use_taxes();
1059
    if ( !$use_taxes && (float)$invoice->get_tax() > 0 ) {
1060
        $use_taxes = true;
1061
    }
1062
    $zero_tax           = !(float)$invoice->get_tax() > 0 ? true : false;
1063
    $tax_label           = $use_taxes && $invoice->has_vat() ? $wpinv_euvat->get_vat_name() : __( 'Tax', 'invoicing' );
1064
    $tax_title          = !$zero_tax && $use_taxes ? ( wpinv_prices_include_tax() ? wp_sprintf( __( '(%s Incl.)', 'invoicing' ), $tax_label ) : wp_sprintf( __( '(%s Excl.)', 'invoicing' ), $tax_label ) ) : '';
1065
1066
    $cart_details       = $invoice->get_cart_details();
1067
    $ajax_cart_details  = $cart_details;
1068
    ob_start();
1069
    ?>
1070
    <table class="table table-sm table-bordered table-responsive">
1071
        <thead>
1072
            <tr>
1073
                <th class="name"><strong><?php _e( "Item Name", "invoicing" );?></strong></th>
1074
                <th class="rate"><strong><?php _e( "Price", "invoicing" );?></strong></th>
1075
                <?php if ($quantities_enabled) { ?>
1076
                    <th class="qty"><strong><?php _e( "Qty", "invoicing" );?></strong></th>
1077
                <?php } ?>
1078
                <?php if ($use_taxes && !$zero_tax) { ?>
1079
                    <th class="tax"><strong><?php echo $tax_label . ' <span class="normal small">(%)</span>'; ?></strong></th>
1080
                <?php } ?>
1081
                <th class="total"><strong><?php echo __( "Item Total", "invoicing" ) . ' <span class="normal small">' . $tax_title . '<span>';?></strong></th>
1082
            </tr>
1083
        </thead>
1084
        <tbody>
1085
        <?php 
1086
            if ( !empty( $cart_details ) ) {
1087
                do_action( 'wpinv_display_line_items_start', $invoice );
1088
1089
                $count = 0;
1090
                $cols  = 3;
1091
                foreach ( $cart_details as $key => $cart_item ) {
1092
                    $item_id    = !empty($cart_item['id']) ? absint( $cart_item['id'] ) : '';
1093
                    $item_price = isset($cart_item["item_price"]) ? wpinv_round_amount( $cart_item["item_price"] ) : 0;
1094
                    $line_total = isset($cart_item["subtotal"]) ? wpinv_round_amount( $cart_item["subtotal"] ) : 0;
1095
                    $quantity   = !empty($cart_item['quantity']) && (int)$cart_item['quantity'] > 0 ? absint( $cart_item['quantity'] ) : 1;
1096
1097
                    $item       = $item_id ? new WPInv_Item( $item_id ) : NULL;
1098
                    $summary    = '';
1099
	                $item_name    = '';
1100
                    $cols       = 3;
1101
                    if ( !empty($item) ) {
1102
                        $item_name  = $item->get_name();
1103
                        $summary    = $item->get_summary();
1104
                    }
1105
                    $item_name  = !empty($cart_item['name']) ? $cart_item['name'] : $item_name;
1106
1107
                    $summary = apply_filters( 'wpinv_print_invoice_line_item_summary', $summary, $cart_item, $item, $invoice );
1108
1109
                    $item_tax       = '';
1110
                    $tax_rate       = '';
1111
                    if ( $use_taxes && $cart_item['tax'] > 0 && $cart_item['subtotal'] > 0 ) {
1112
                        $item_tax = wpinv_price( wpinv_format_amount( $cart_item['tax'] ), $invoice->get_currency() );
1113
                        $tax_rate = !empty( $cart_item['vat_rate'] ) ? $cart_item['vat_rate'] : ( $cart_item['tax'] / $cart_item['subtotal'] ) * 100;
1114
                        $tax_rate = $tax_rate > 0 ? (float)wpinv_round_amount( $tax_rate, 4 ) : '';
1115
                        $tax_rate = $tax_rate != '' ? ' <small class="tax-rate">(' . $tax_rate . '%)</small>' : '';
1116
                    }
1117
1118
                    $line_item_tax = $item_tax . $tax_rate;
1119
1120
                    if ( $line_item_tax === '' ) {
1121
                        $line_item_tax = 0; // Zero tax
1122
                    }
1123
1124
                    $action = apply_filters( 'wpinv_display_line_item_action', '', $cart_item, $invoice, $cols );
1125
1126
                    $line_item = '<tr class="row-' . ( ($count % 2 == 0) ? 'even' : 'odd' ) . ' wpinv-item">';
1127
                        $line_item .= '<td class="name">' . $action. esc_html__( $item_name, 'invoicing' ) . wpinv_get_item_suffix( $item );
1128
                        if ( $summary !== '' ) {
1129
                            $line_item .= '<br/><small class="meta">' . wpautop( wp_kses_post( $summary ) ) . '</small>';
1130
                        }
1131
                        $line_item .= '</td>';
1132
1133
                        $line_item .= '<td class="rate">' . esc_html__( wpinv_price( wpinv_format_amount( $item_price ), $invoice->get_currency() ) ) . '</td>';
1134
                        if ($quantities_enabled) {
1135
                            $cols++;
1136
                            $line_item .= '<td class="qty">' . $quantity . '</td>';
1137
                        }
1138
                        if ($use_taxes && !$zero_tax) {
1139
                            $cols++;
1140
                            $line_item .= '<td class="tax">' . $line_item_tax . '</td>';
1141
                        }
1142
                        $line_item .= '<td class="total">' . esc_html__( wpinv_price( wpinv_format_amount( $line_total ), $invoice->get_currency() ) ) . '</td>';
1143
                    $line_item .= '</tr>';
1144
1145
                    echo apply_filters( 'wpinv_display_line_item', $line_item, $cart_item, $invoice, $cols );
1146
1147
                    $count++;
1148
                }
1149
1150
                do_action( 'wpinv_display_before_subtotal', $invoice, $cols );
1151
                ?>
1152
                <tr class="row-sub-total row_odd">
1153
                    <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php echo apply_filters( 'wpinv_print_cart_subtotal_label', '<strong>' . __( 'Sub Total', 'invoicing' ) . ':</strong>', $invoice ); ?></td>
1154
                    <td class="total"><strong><?php _e( wpinv_subtotal( $invoice_id, true ) ) ?></strong></td>
1155
                </tr>
1156
                <?php
1157
                do_action( 'wpinv_display_after_subtotal', $invoice, $cols );
1158
                
1159
                if ( wpinv_discount( $invoice_id, false ) > 0 ) {
1160
                    do_action( 'wpinv_display_before_discount', $invoice, $cols );
1161
                    ?>
1162
                        <tr class="row-discount">
1163
                            <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php wpinv_get_discount_label( wpinv_discount_code( $invoice_id ) ); ?>:</td>
1164
                            <td class="total"><?php echo wpinv_discount( $invoice_id, true, true ); ?></td>
1165
                        </tr>
1166
                    <?php
1167
                    do_action( 'wpinv_display_after_discount', $invoice, $cols );
1168
                }
1169
1170
                if ( $use_taxes ) {
1171
                    do_action( 'wpinv_display_before_tax', $invoice, $cols );
1172
                    ?>
1173
                    <tr class="row-tax">
1174
                        <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php echo apply_filters( 'wpinv_print_cart_tax_label', '<strong>' . $tax_label . ':</strong>', $invoice ); ?></td>
1175
                        <td class="total"><?php _e( wpinv_tax( $invoice_id, true ) ) ?></td>
1176
                    </tr>
1177
                    <?php
1178
                    do_action( 'wpinv_display_after_tax', $invoice, $cols );
1179
                }
1180
1181
                do_action( 'wpinv_display_before_total', $invoice, $cols );
1182
                ?>
1183
                <tr class="table-active row-total">
1184
                    <td class="rate" colspan="<?php echo ( $cols - 1 ); ?>"><?php echo apply_filters( 'wpinv_print_cart_total_label', '<strong>' . __( 'Total', 'invoicing' ) . ':</strong>', $invoice ); ?></td>
1185
                    <td class="total"><strong><?php _e( wpinv_payment_total( $invoice_id, true ) ) ?></strong></td>
1186
                </tr>
1187
                <?php
1188
                do_action( 'wpinv_display_after_total', $invoice, $cols );
1189
1190
                do_action( 'wpinv_display_line_end', $invoice, $cols );
1191
            }
1192
        ?>
1193
        </tbody>
1194
    </table>
1195
    <?php
1196
    echo ob_get_clean();
1197
}
1198
1199
function wpinv_display_invoice_totals( $invoice_id = 0 ) {
1200
    $use_taxes = wpinv_use_taxes();
1201
1202
    do_action( 'wpinv_before_display_totals_table', $invoice_id ); 
1203
    ?>
1204
    <table class="table table-sm table-bordered table-responsive">
1205
        <tbody>
1206
            <?php do_action( 'wpinv_before_display_totals' ); ?>
1207
            <tr class="row-sub-total">
1208
                <td class="rate"><strong><?php _e( 'Sub Total', 'invoicing' ); ?></strong></td>
1209
                <td class="total"><strong><?php _e( wpinv_subtotal( $invoice_id, true ) ) ?></strong></td>
1210
            </tr>
1211
            <?php do_action( 'wpinv_after_display_totals' ); ?>
1212
            <?php if ( wpinv_discount( $invoice_id, false ) > 0 ) { ?>
1213
                <tr class="row-discount">
1214
                    <td class="rate"><?php wpinv_get_discount_label( wpinv_discount_code( $invoice_id ) ); ?></td>
1215
                    <td class="total"><?php echo wpinv_discount( $invoice_id, true, true ); ?></td>
1216
                </tr>
1217
            <?php do_action( 'wpinv_after_display_discount' ); ?>
1218
            <?php } ?>
1219
            <?php if ( $use_taxes ) { ?>
1220
            <tr class="row-tax">
1221
                <td class="rate"><?php _e( 'Tax', 'invoicing' ); ?></td>
1222
                <td class="total"><?php _e( wpinv_tax( $invoice_id, true ) ) ?></td>
1223
            </tr>
1224
            <?php do_action( 'wpinv_after_display_tax' ); ?>
1225
            <?php } ?>
1226
            <?php if ( $fees = wpinv_get_fees( $invoice_id ) ) { ?>
1227
                <?php foreach ( $fees as $fee ) { ?>
1228
                    <tr class="row-fee">
1229
                        <td class="rate"><?php echo $fee['label']; ?></td>
1230
                        <td class="total"><?php echo $fee['amount_display']; ?></td>
1231
                    </tr>
1232
                <?php } ?>
1233
            <?php } ?>
1234
            <tr class="table-active row-total">
1235
                <td class="rate"><strong><?php _e( 'Total', 'invoicing' ) ?></strong></td>
1236
                <td class="total"><strong><?php _e( wpinv_payment_total( $invoice_id, true ) ) ?></strong></td>
1237
            </tr>
1238
            <?php do_action( 'wpinv_after_totals' ); ?>
1239
        </tbody>
1240
1241
    </table>
1242
1243
    <?php do_action( 'wpinv_after_totals_table' );
1244
}
1245
1246
function wpinv_display_payments_info( $invoice_id = 0, $echo = true ) {
1247
    $invoice = wpinv_get_invoice( $invoice_id );
1248
1249
    ob_start();
1250
    do_action( 'wpinv_before_display_payments_info', $invoice_id );
1251
    if ( ( $gateway_title = $invoice->get_gateway_title() ) || $invoice->is_paid() || $invoice->is_refunded() ) {
1252
        ?>
1253
        <div class="wpi-payment-info">
1254
            <p class="wpi-payment-gateway"><?php echo wp_sprintf( __( 'Payment via %s', 'invoicing' ), $gateway_title ? $gateway_title : __( 'Manually', 'invoicing' ) ); ?></p>
1255
            <?php if ( $gateway_title ) { ?>
1256
            <p class="wpi-payment-transid"><?php echo wp_sprintf( __( 'Transaction ID: %s', 'invoicing' ), $invoice->get_transaction_id() ); ?></p>
1257
            <?php } ?>
1258
        </div>
1259
        <?php
1260
    }
1261
    do_action( 'wpinv_after_display_payments_info', $invoice_id );
1262
    $outout = ob_get_clean();
1263
1264
    if ( $echo ) {
1265
        echo $outout;
1266
    } else {
1267
        return $outout;
1268
    }
1269
}
1270
1271
function wpinv_display_style( $invoice ) {
1272
    wp_register_style( 'wpinv-single-style', WPINV_PLUGIN_URL . 'assets/css/invoice.css', array(), WPINV_VERSION );
1273
1274
    wp_print_styles( 'open-sans' );
1275
    wp_print_styles( 'wpinv-single-style' );
1276
1277
    $custom_css = wpinv_get_option('template_custom_css');
1278
    if(isset($custom_css) && !empty($custom_css)){
1279
        $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

1279
        $custom_css     = wp_kses( $custom_css, /** @scrutinizer ignore-type */ array( '\'', '\"' ) );
Loading history...
1280
        $custom_css     = str_replace( '&gt;', '>', $custom_css );
1281
        echo '<style type="text/css">';
1282
        echo $custom_css;
1283
        echo '</style>';
1284
    }
1285
}
1286
add_action( 'wpinv_invoice_print_head', 'wpinv_display_style' );
1287
add_action( 'wpinv_invalid_invoice_head', 'wpinv_display_style' );
1288
1289
function wpinv_checkout_billing_details() {
1290
    $invoice_id = (int)wpinv_get_invoice_cart_id();
1291
    if (empty($invoice_id)) {
1292
        wpinv_error_log( 'Invoice id not found', 'ERROR', __FILE__, __LINE__ );
1293
        return null;
1294
    }
1295
1296
    $invoice = wpinv_get_invoice_cart( $invoice_id );
1297
    if (empty($invoice)) {
1298
        wpinv_error_log( 'Invoice not found', 'ERROR', __FILE__, __LINE__ );
1299
        return null;
1300
    }
1301
    $user_id        = $invoice->get_user_id();
1302
    $user_info      = $invoice->get_user_info();
1303
    $address_info   = wpinv_get_user_address( $user_id );
1304
1305
    if ( empty( $user_info['first_name'] ) && !empty( $user_info['first_name'] ) ) {
1306
        $user_info['first_name'] = $user_info['first_name'];
1307
        $user_info['last_name'] = $user_info['last_name'];
1308
    }
1309
1310
    if ( ( ( empty( $user_info['country'] ) && !empty( $address_info['country'] ) ) || ( empty( $user_info['state'] ) && !empty( $address_info['state'] ) && $user_info['country'] == $address_info['country'] ) ) ) {
1311
        $user_info['country']   = $address_info['country'];
1312
        $user_info['state']     = $address_info['state'];
1313
        $user_info['city']      = $address_info['city'];
1314
        $user_info['zip']       = $address_info['zip'];
1315
    }
1316
1317
    $address_fields = array(
1318
        'user_id',
1319
        'company',
1320
        'vat_number',
1321
        'email',
1322
        'phone',
1323
        'address'
1324
    );
1325
1326
    foreach ( $address_fields as $field ) {
1327
        if ( empty( $user_info[$field] ) ) {
1328
            $user_info[$field] = $address_info[$field];
1329
        }
1330
    }
1331
1332
    return apply_filters( 'wpinv_checkout_billing_details', $user_info, $invoice );
1333
}
1334
1335
function wpinv_admin_get_line_items($invoice = array()) {
1336
    $item_quantities    = wpinv_item_quantities_enabled();
1337
    $use_taxes          = wpinv_use_taxes();
1338
1339
    if ( empty( $invoice ) ) {
1340
        return NULL;
1341
    }
1342
1343
    $cart_items = $invoice->get_cart_details();
1344
    if ( empty( $cart_items ) ) {
1345
        return NULL;
1346
    }
1347
    ob_start();
1348
1349
    do_action( 'wpinv_admin_before_line_items', $cart_items, $invoice );
1350
1351
    $count = 0;
1352
    foreach ( $cart_items as $key => $cart_item ) {
1353
        $item_id    = $cart_item['id'];
1354
        $wpi_item   = $item_id > 0 ? new WPInv_Item( $item_id ) : NULL;
1355
1356
        if (empty($wpi_item)) {
1357
            continue;
1358
        }
1359
1360
        $item_price     = wpinv_price( wpinv_format_amount( $cart_item['item_price'] ), $invoice->get_currency() );
1361
        $quantity       = !empty( $cart_item['quantity'] ) && $cart_item['quantity'] > 0 ? $cart_item['quantity'] : 1;
1362
        $item_subtotal  = wpinv_price( wpinv_format_amount( $cart_item['subtotal'] ), $invoice->get_currency() );
1363
        $can_remove     = true;
1364
1365
        $summary = apply_filters( 'wpinv_admin_invoice_line_item_summary', '', $cart_item, $wpi_item, $invoice );
1366
1367
        $item_tax       = '';
1368
        $tax_rate       = '';
1369
        if ( $cart_item['tax'] > 0 && $cart_item['subtotal'] > 0 ) {
1370
            $item_tax = wpinv_price( wpinv_format_amount( $cart_item['tax'] ), $invoice->get_currency() );
1371
            $tax_rate = !empty( $cart_item['vat_rate'] ) ? $cart_item['vat_rate'] : ( $cart_item['tax'] / $cart_item['subtotal'] ) * 100;
1372
            $tax_rate = $tax_rate > 0 ? (float)wpinv_round_amount( $tax_rate, 4 ) : '';
1373
            $tax_rate = $tax_rate != '' ? ' <span class="tax-rate">(' . $tax_rate . '%)</span>' : '';
1374
        }
1375
        $line_item_tax = $item_tax . $tax_rate;
1376
1377
        if ( $line_item_tax === '' ) {
1378
            $line_item_tax = 0; // Zero tax
1379
        }
1380
1381
        $line_item = '<tr class="item item-' . ( ($count % 2 == 0) ? 'even' : 'odd' ) . '" data-item-id="' . $item_id . '">';
1382
            $line_item .= '<td class="id">' . $item_id . '</td>';
1383
            $line_item .= '<td class="title"><a href="' . get_edit_post_link( $item_id ) . '" target="_blank">' . $cart_item['name'] . '</a>' . wpinv_get_item_suffix( $wpi_item );
1384
            if ( $summary !== '' ) {
1385
                $line_item .= '<span class="meta">' . wpautop( wp_kses_post( $summary ) ) . '</span>';
1386
            }
1387
            $line_item .= '</td>';
1388
            $line_item .= '<td class="price">' . $item_price . '</td>';
1389
            
1390
            if ( $item_quantities ) {
1391
                if ( count( $cart_items ) == 1 && $quantity <= 1 ) {
1392
                    $can_remove = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $can_remove is dead and can be removed.
Loading history...
1393
                }
1394
                $line_item .= '<td class="qty" data-quantity="' . $quantity . '">&nbsp;&times;&nbsp;' . $quantity . '</td>';
1395
            } else {
1396
                if ( count( $cart_items ) == 1 ) {
1397
                    $can_remove = false;
1398
                }
1399
            }
1400
            $line_item .= '<td class="total">' . $item_subtotal . '</td>';
1401
            
1402
            if ( $use_taxes ) {
1403
                $line_item .= '<td class="tax">' . $line_item_tax . '</td>';
1404
            }
1405
            $line_item .= '<td class="action">';
1406
            if ( !$invoice->is_paid() && !$invoice->is_refunded() ) {
1407
                $line_item .= '<i class="fa fa-remove wpinv-item-remove"></i>';
1408
            }
1409
            $line_item .= '</td>';
1410
        $line_item .= '</tr>';
1411
1412
        echo apply_filters( 'wpinv_admin_line_item', $line_item, $cart_item, $invoice );
1413
1414
        $count++;
1415
    } 
1416
1417
    do_action( 'wpinv_admin_after_line_items', $cart_items, $invoice );
1418
1419
    return ob_get_clean();
1420
}
1421
1422
function wpinv_checkout_form() {
1423
    global $wpi_checkout_id;
1424
1425
    // Set current invoice id.
1426
    $wpi_checkout_id = wpinv_get_invoice_cart_id();
1427
1428
    //Maybe update the prices
1429
    if(! empty( $_GET['wpi_dynamic_item'] ) && ! empty( $_GET['wpi_dynamic_price'] ) ) {
1430
1431
        //If the invoice exists, update it with new pricing details
1432
        if (! empty( $wpi_checkout_id ) ) {
1433
1434
            $_invoice       = wpinv_get_invoice_cart();
1435
            $_cart_details  = $_invoice->get_cart_details();
1436
            $_dynamic_item  = sanitize_text_field( $_GET['wpi_dynamic_item'] );
1437
1438
            //First, fetch the item
1439
            $item    = new WPInv_Item( $_dynamic_item );
1440
    
1441
            //Next, ensure it supports dynamic pricing...
1442
            if( $item->supports_dynamic_pricing() && $item->get_is_dynamic_pricing() ) {
1443
                
1444
                //... and that the new price is not lower than the minimum price
1445
                $_dynamic_price = (float) wpinv_sanitize_amount( sanitize_text_field( $_GET['wpi_dynamic_price'] ) );
1446
                if( $_dynamic_price < $item->get_minimum_price() ) {
1447
                    $_dynamic_price = $item->get_minimum_price();
1448
                }
1449
1450
                //Finally, update our invoice with the new price
1451
                if ( !empty( $_cart_details ) ) {
1452
1453
                    foreach ( $_cart_details as $key => $item ) {
1454
                        if ( !empty( $item['id'] ) && $_dynamic_item == $item['id'] ) {
1455
                            $_cart_details[$key]['custom_price'] = $_dynamic_price;
1456
                            $_cart_details[$key]['item_price']   = $_dynamic_price;
1457
                        }
1458
                    }
1459
1460
                    $_meta = $_invoice->get_meta();
1461
                    $_meta['cart_details'] = $_cart_details;
1462
                    $_invoice->set( 'payment_meta', $_meta );
1463
                    $_invoice->set( 'cart_details', $_cart_details );
1464
                    $_invoice->recalculate_totals();
1465
1466
                }
1467
1468
            }
1469
            
1470
        }
1471
1472
    }
1473
1474
    $form_action  = esc_url( wpinv_get_checkout_uri() );
0 ignored issues
show
Bug introduced by
It seems like wpinv_get_checkout_uri() 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

1474
    $form_action  = esc_url( /** @scrutinizer ignore-type */ wpinv_get_checkout_uri() );
Loading history...
1475
1476
    ob_start();
1477
	    do_action( 'wpinv_checkout_content_before' );
1478
        echo '<div id="wpinv_checkout_wrap">';
1479
1480
        if ( wpinv_get_cart_contents() || wpinv_cart_has_fees() ) {
1481
            ?>
1482
            <div id="wpinv_checkout_form_wrap" class="wpinv_clearfix table-responsive">
1483
                <?php do_action( 'wpinv_before_checkout_form' ); ?>
1484
                <form id="wpinv_checkout_form" class="wpi-form" action="<?php echo $form_action; ?>" method="POST">
1485
                    <?php
1486
                    do_action( 'wpinv_checkout_form_top' );
1487
                    do_action( 'wpinv_checkout_billing_info' );
1488
                    do_action( 'wpinv_checkout_cart' );
1489
                    do_action( 'wpinv_payment_mode_select'  );
1490
                    do_action( 'wpinv_checkout_form_bottom' )
1491
                    ?>
1492
                </form>
1493
                <?php do_action( 'wpinv_after_purchase_form' ); ?>
1494
            </div><!--end #wpinv_checkout_form_wrap-->
1495
        <?php
1496
        } else {
1497
            do_action( 'wpinv_cart_empty' );
1498
        }
1499
        echo '</div><!--end #wpinv_checkout_wrap-->';
1500
	    do_action( 'wpinv_checkout_content_after' );
1501
    return ob_get_clean();
1502
}
1503
1504
function wpinv_checkout_cart( $cart_details = array(), $echo = true ) {
1505
    global $ajax_cart_details;
1506
    $ajax_cart_details = $cart_details;
1507
1508
    ob_start();
1509
    do_action( 'wpinv_before_checkout_cart' );
1510
    echo '<div id="wpinv_checkout_cart_form" method="post">';
1511
        echo '<div id="wpinv_checkout_cart_wrap">';
1512
            wpinv_get_template_part( 'wpinv-checkout-cart' );
1513
        echo '</div>';
1514
    echo '</div>';
1515
    do_action( 'wpinv_after_checkout_cart' );
1516
    $content = ob_get_clean();
1517
1518
    if ( $echo ) {
1519
        echo $content;
1520
    } else {
1521
        return $content;
1522
    }
1523
}
1524
add_action( 'wpinv_checkout_cart', 'wpinv_checkout_cart', 10 );
1525
1526
function wpinv_empty_cart_message() {
1527
	return apply_filters( 'wpinv_empty_cart_message', '<span class="wpinv_empty_cart">' . __( 'Your cart is empty.', 'invoicing' ) . '</span>' );
1528
}
1529
1530
/**
1531
 * Echoes the Empty Cart Message
1532
 *
1533
 * @since 1.0
1534
 * @return void
1535
 */
1536
function wpinv_empty_checkout_cart() {
1537
	echo wpinv_empty_cart_message();
1538
}
1539
add_action( 'wpinv_cart_empty', 'wpinv_empty_checkout_cart' );
1540
1541
function wpinv_update_cart_button() {
1542
    if ( !wpinv_item_quantities_enabled() )
1543
        return;
1544
?>
1545
    <input type="submit" name="wpinv_update_cart_submit" class="wpinv-submit wpinv-no-js button" value="<?php _e( 'Update Cart', 'invoicing' ); ?>"/>
1546
    <input type="hidden" name="wpi_action" value="update_cart"/>
1547
<?php
1548
}
1549
1550
function wpinv_checkout_cart_columns() {
1551
    $default = 3;
1552
    if ( wpinv_item_quantities_enabled() ) {
1553
        $default++;
1554
    }
1555
    
1556
    if ( wpinv_use_taxes() ) {
1557
        $default++;
1558
    }
1559
1560
    return apply_filters( 'wpinv_checkout_cart_columns', $default );
1561
}
1562
1563
function wpinv_display_cart_messages() {
1564
    global $wpi_session;
1565
1566
    $messages = $wpi_session->get( 'wpinv_cart_messages' );
1567
1568
    if ( $messages ) {
1569
        foreach ( $messages as $message_id => $message ) {
1570
            // Try and detect what type of message this is
1571
            if ( strpos( strtolower( $message ), 'error' ) ) {
1572
                $type = 'error';
1573
            } elseif ( strpos( strtolower( $message ), 'success' ) ) {
1574
                $type = 'success';
1575
            } else {
1576
                $type = 'info';
1577
            }
1578
1579
            $classes = apply_filters( 'wpinv_' . $type . '_class', array( 'wpinv_errors', 'wpinv-alert', 'wpinv-alert-' . $type ) );
1580
1581
            echo '<div class="' . implode( ' ', $classes ) . '">';
1582
                // Loop message codes and display messages
1583
                    echo '<p class="wpinv_error" id="wpinv_msg_' . $message_id . '">' . $message . '</p>';
1584
            echo '</div>';
1585
        }
1586
1587
        // Remove all of the cart saving messages
1588
        $wpi_session->set( 'wpinv_cart_messages', null );
1589
    }
1590
}
1591
add_action( 'wpinv_before_checkout_cart', 'wpinv_display_cart_messages' );
1592
1593
function wpinv_discount_field() {
1594
    if ( isset( $_GET['wpi-gateway'] ) && wpinv_is_ajax_disabled() ) {
1595
        return; // Only show before a payment method has been selected if ajax is disabled
1596
    }
1597
1598
    if ( !wpinv_is_checkout() ) {
1599
        return;
1600
    }
1601
1602
    if ( wpinv_has_active_discounts() && wpinv_get_cart_total() ) {
1603
    ?>
1604
    <div id="wpinv-discount-field" class="panel panel-default">
1605
        <div class="panel-body">
1606
            <p>
1607
                <label class="wpinv-label" for="wpinv_discount_code"><strong><?php _e( 'Discount', 'invoicing' ); ?></strong></label>
1608
                <span class="wpinv-description"><?php _e( 'Enter a discount code if you have one.', 'invoicing' ); ?></span>
1609
            </p>
1610
            <div class="form-group row">
1611
                <div class="col-sm-4">
1612
                    <input class="wpinv-input form-control" type="text" id="wpinv_discount_code" name="wpinv_discount_code" placeholder="<?php _e( 'Enter discount code', 'invoicing' ); ?>"/>
1613
                </div>
1614
                <div class="col-sm-3">
1615
                    <button id="wpi-apply-discount" type="button" class="btn btn-success btn-sm"><?php _e( 'Apply Discount', 'invoicing' ); ?></button>
1616
                </div>
1617
                <div style="clear:both"></div>
1618
                <div class="col-sm-12 wpinv-discount-msg">
1619
                    <div class="alert alert-success"><i class="fa fa-check-circle"></i><span class="wpi-msg"></span></div>
1620
                    <div class="alert alert-error"><i class="fa fa-warning"></i><span class="wpi-msg"></span></div>
1621
                </div>
1622
            </div>
1623
        </div>
1624
    </div>
1625
<?php
1626
    }
1627
}
1628
add_action( 'wpinv_after_checkout_cart', 'wpinv_discount_field', -10 );
1629
1630
function wpinv_agree_to_terms_js() {
1631
    if ( wpinv_get_option( 'show_agree_to_terms', false ) ) {
1632
?>
1633
<script type="text/javascript">
1634
    jQuery(document).ready(function($){
1635
        $( document.body ).on('click', '.wpinv_terms_links', function(e) {
1636
            //e.preventDefault();
1637
            $('#wpinv_terms').slideToggle();
1638
            $('.wpinv_terms_links').toggle();
1639
            return false;
1640
        });
1641
    });
1642
</script>
1643
<?php
1644
    }
1645
}
1646
add_action( 'wpinv_checkout_form_top', 'wpinv_agree_to_terms_js' );
1647
1648
function wpinv_payment_mode_select() {
1649
    $gateways = wpinv_get_enabled_payment_gateways( true );
1650
    $gateways = apply_filters( 'wpinv_payment_gateways_on_cart', $gateways );
1651
    $invoice = wpinv_get_invoice( 0, true );
1652
1653
    do_action('wpinv_payment_mode_top');
1654
    $invoice_id = (int)$invoice->ID;
1655
    $chosen_gateway = wpinv_get_chosen_gateway( $invoice_id );
1656
    ?>
1657
    <div id="wpinv_payment_mode_select" data-gateway="<?php echo $chosen_gateway; ?>" <?php echo ( $invoice->is_free() ? 'style="display:none;" data-free="1"' : '' ); ?>>
1658
            <?php do_action( 'wpinv_payment_mode_before_gateways_wrap' ); ?>
1659
            <div id="wpinv-payment-mode-wrap" class="panel panel-default">
1660
                <div class="panel-heading"><h3 class="panel-title"><?php _e( 'Select Payment Method', 'invoicing' ); ?></h3></div>
1661
                <div class="panel-body list-group wpi-payment_methods">
1662
                    <?php
1663
                    do_action( 'wpinv_payment_mode_before_gateways' );
1664
1665
                    if ( !empty( $gateways ) ) {
1666
                        foreach ( $gateways as $gateway_id => $gateway ) {
1667
                            $checked       = checked( $gateway_id, $chosen_gateway, false );
1668
                            $button_label  = wpinv_get_gateway_button_label( $gateway_id );
1669
                            $gateway_label = wpinv_get_gateway_checkout_label( $gateway_id );
1670
                            $description   = wpinv_get_gateway_description( $gateway_id );
1671
                            ?>
1672
                            <div class="list-group-item">
1673
                                <div class="radio">
1674
                                    <label><input type="radio" data-button-text="<?php echo esc_attr( $button_label );?>" value="<?php echo esc_attr( $gateway_id ) ;?>" <?php echo $checked ;?> id="wpi_gateway_<?php echo esc_attr( $gateway_id );?>" name="wpi-gateway" class="wpi-pmethod"><?php echo esc_html( $gateway_label ); ?></label>
1675
                                </div>
1676
                                <div style="display:none;" class="payment_box wpi_gateway_<?php echo esc_attr( $gateway_id );?>" role="alert">
1677
                                    <?php if ( !empty( $description ) ) { ?>
1678
                                        <div class="wpi-gateway-desc alert alert-info"><?php _e( $description, 'invoicing' ); ?></div>
1679
                                    <?php } ?>
1680
                                    <?php do_action( 'wpinv_' . $gateway_id . '_cc_form', $invoice_id ) ;?>
1681
                                </div>
1682
                            </div>
1683
                            <?php
1684
                        }
1685
                    } else {
1686
                        echo '<div class="alert alert-warning">'. __( 'No payment gateway active', 'invoicing' ) .'</div>';
1687
                    }
1688
1689
                    do_action( 'wpinv_payment_mode_after_gateways' );
1690
                    ?>
1691
                </div>
1692
            </div>
1693
            <?php do_action( 'wpinv_payment_mode_after_gateways_wrap' ); ?>
1694
    </div>
1695
    <?php
1696
    do_action('wpinv_payment_mode_bottom');
1697
}
1698
add_action( 'wpinv_payment_mode_select', 'wpinv_payment_mode_select' );
1699
1700
/**
1701
 * Sanitizes a checkout field
1702
 */
1703
function wpinv_sanitize_checkout_field_args( $args ) {
1704
1705
    $name     = ( empty( $args['name'] ) ) ? 'wpinv_' . wp_generate_password( 12, false ) : $args['name'];
1706
    $id       = ( empty( $args['id'] ) ) ? $name : $args['id'];
1707
1708
    $defaults = array(
1709
        'id'                     => $id, // element id
1710
        'name'                   => $name, // input element name
1711
        'key'                    => ( ! isset( $args['key'] ) ) ? str_ireplace( 'wpinv_', '', $name ) : $args['key'], // value key in $billing_details
1712
        'input_class'            => 'wpi-input form-control', // input element class
1713
        'wrapper_class'          => 'wpi-cart-field wpi-col2', // p element class
1714
        'label_class'            => 'wpi-label', // label class
1715
        'placeholder'            => '', // First name.
1716
        'field_label'            => '',
1717
        'field_description'      => '',
1718
        'field_required'         => false,
1719
        'field_required_msg'     => sprintf(
1720
            __( '%s is required', 'invoicing' ),
1721
            ( empty( $args['field_label'] ) ) ? $name : $args['field_label']
1722
        ),
1723
        'field_type'             => 'text',
1724
        'show_in'                => array( 'email', 'checkout', 'details' ),
1725
    );
1726
1727
    return wp_parse_args( $args, $defaults );
1728
1729
}
1730
1731
/**
1732
 * Returns default checkout fields.
1733
 */
1734
function wpinv_get_default_checkout_fields() {
1735
    return array_map( 'wpinv_sanitize_checkout_field_args', wpinv_get_data( 'default-checkout-fields' ) );
0 ignored issues
show
Bug introduced by
It seems like wpinv_get_data('default-checkout-fields') can also be of type true; however, parameter $arr1 of array_map() does only seem to accept array, 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

1735
    return array_map( 'wpinv_sanitize_checkout_field_args', /** @scrutinizer ignore-type */ wpinv_get_data( 'default-checkout-fields' ) );
Loading history...
1736
}
1737
1738
/**
1739
 * Returns checkout fields.
1740
 */
1741
function wpinv_get_checkout_fields() {
1742
    $checkout_fields = wpinv_get_option( 'checkout_fields', null );
1743
1744
    if ( ! is_array( $checkout_fields ) ) {
1745
        $checkout_fields = wpinv_get_default_checkout_fields();
1746
    }
1747
1748
    return array_map( 'wpinv_sanitize_checkout_field_args', $checkout_fields );
1749
}
1750
1751
/**
1752
 * Returns an array of enabled checkout fields.
1753
 */
1754
function wpinv_prepare_checkout_fields( array $billing_details = array() ) {
1755
1756
    if ( empty( $billing_details['country'] ) ) {
1757
        $billing_details['country'] = wpinv_default_billing_country();
1758
    }
1759
1760
    $fields       = wpinv_get_checkout_fields();
1761
    $fields       = apply_filters('wpinv_checkout_fields', $fields, $billing_details );
1762
    $field_types  = wpinv_get_data( 'field-types' );
1763
1764
    foreach ( $fields as $key => $field ) {
1765
1766
        // Is the field type registered?
1767
        if ( empty( $field['field_type'] ) || empty( $field_types[ $field['field_type'] ] ) ) {
1768
            unset( $fields[ $key ] );
1769
            continue;
1770
        }
1771
1772
        // Each field type has its own render callback.
1773
        $fields[ $key ]['render_cb'] = $field_types[ $field['field_type'] ]['render_cb'];
1774
1775
        // Add the current field value.
1776
        if ( isset( $field['key'] ) && isset( $billing_details[ $field['key'] ] ) ) {
1777
1778
            if ( 'select' == $field['field_type'] ) {
1779
                $fields[ $key ]['selected'] = $billing_details[ $field['key'] ];
1780
            } else {
1781
                $fields[ $key ]['value'] = $billing_details[ $field['key'] ];
1782
            }
1783
1784
        }
1785
1786
        $fields[ $key ]['billing_details'] = $billing_details;
1787
        $fields[ $key ]['class']           =  $field['input_class'];
1788
1789
    }
1790
1791
    return $fields;
1792
1793
}
1794
1795
function wpinv_checkout_billing_info() {
1796
    if ( wpinv_is_checkout() ) {
1797
1798
        // Prepare the billing details and checkout fields.
1799
        $billing_details = wpinv_checkout_billing_details();
1800
        $checkout_fields = wpinv_prepare_checkout_fields( $billing_details );
1801
1802
        ?>
1803
        <div id="wpinv-fields" class="clearfix">
1804
            <?php do_action( 'wpinv_before_billing_fields', $billing_details ); ?>
1805
            <div id="wpi-billing" class="wpi-billing clearfix panel panel-default">
1806
                <div class="panel-heading"><h3 class="panel-title"><?php _e( 'Billing Details', 'invoicing' );?></h3></div>
1807
                <div id="wpinv-fields-box" class="panel-body">
1808
1809
                    <?php 
1810
1811
                        do_action( 'wpinv_checkout_billing_fields_first', $billing_details );
1812
1813
                        $first_col = true;
1814
                        foreach ( $checkout_fields as $field_details ) {
1815
1816
                            $type = sanitize_html_class( $field_details['field_type'] );
1817
                            $name = sanitize_html_class( $field_details['name'] );
1818
1819
                            // Fire actions before a field is displayed.
1820
                            do_action( "wpinv_checkout_billing_fields_before_$name", $field_details, $billing_details );
1821
                            do_action( "wpinv_checkout_billing_fields_before_single_field", $name, $field_details, $billing_details );
1822
                            do_action( "wpinv_checkout_billing_fields_before_field_type_$type", $field_details, $billing_details );
1823
1824
                            // Display the opening wrapper.
1825
                            $wrapper_class  = esc_attr( $field_details['wrapper_class'] );
1826
                            $wrapper_class .=  empty( $first_col ) ? ' wpi-coll' : ' wpi-colf';
1827
                            $wrapper_id     = "wpinv_{$name}_box";
1828
                            echo "<p id='$wrapper_id' class='$wrapper_class'>";
1829
1830
                            // And (maybe) the label.
1831
                            $label = esc_html( $field_details['field_label'] );
1832
                            if ( ! empty( $label ) ) {
1833
1834
                                // Is this field required?
1835
                                if ( ! empty( $field_details['field_required'] ) ) {
1836
                                    $label .= '<span class="wpi-required">*</span>';
1837
                                }
1838
1839
                                $label_class = esc_attr( $field_details['label_class'] );
1840
                                $input_id    = esc_attr( $field_details['id'] );
1841
                                echo "<label for='$input_id' class='$label_class'>$label</label>";
1842
1843
                            }
1844
1845
                            // Finally, display the input.
1846
                            if ( function_exists( $field_details['render_cb'] ) ) {
1847
                                echo call_user_func( $field_details['render_cb'], $field_details );
1848
                            }
1849
1850
                            // Fire actions when displaying a field.
1851
                            do_action( "wpinv_checkout_billing_fields_$name", $field_details, $billing_details );
1852
                            do_action( "wpinv_checkout_billing_fields_single_field", $name, $field_details, $billing_details );
1853
                            do_action( "wpinv_checkout_billing_fields_field_type_$type", $field_details, $billing_details );
1854
1855
                            if ( ! empty( $field_details['field_description'] ) ) {
1856
                                echo "<div class='wpi-field-description'>{$field_details['field_description']}</div>";
1857
                            }
1858
1859
                            echo "</p>";
1860
1861
                            // Fire actions after a field is displayed.
1862
                            do_action( "wpinv_checkout_billing_fields_after_$name", $field_details, $billing_details );
1863
                            do_action( "wpinv_checkout_billing_fields_after_single_field", $name, $field_details, $billing_details );
1864
                            do_action( "wpinv_checkout_billing_fields_after_field_type_$type", $field_details, $billing_details );
1865
1866
                            $first_col = ! $first_col;
0 ignored issues
show
introduced by
The condition $first_col is always true.
Loading history...
1867
                        }
1868
1869
                        do_action( 'wpinv_checkout_billing_fields_last', $billing_details );
1870
1871
                    ?>
1872
1873
                    <div class="clearfix"></div>
1874
                </div>
1875
            </div>
1876
            <?php do_action( 'wpinv_after_billing_fields', $billing_details ); ?>
1877
        </div>
1878
        <?php
1879
    }
1880
}
1881
add_action( 'wpinv_checkout_billing_info', 'wpinv_checkout_billing_info' );
1882
1883
function wpinv_checkout_hidden_fields() {
1884
?>
1885
    <?php if ( is_user_logged_in() ) { ?>
1886
    <input type="hidden" name="wpinv_user_id" value="<?php echo get_current_user_id(); ?>"/>
1887
    <?php } ?>
1888
    <input type="hidden" name="wpi_action" value="payment" />
1889
<?php
1890
}
1891
1892
function wpinv_checkout_button_purchase() {
1893
    ob_start();
1894
?>
1895
    <input type="submit" class="btn btn-success wpinv-submit" id="wpinv-payment-button" data-value="<?php esc_attr_e( 'Proceed to Pay', 'invoicing' ) ?>" name="wpinv_payment" value="<?php esc_attr_e( 'Proceed to Pay', 'invoicing' ) ?>"/>
1896
<?php
1897
    return apply_filters( 'wpinv_checkout_button_purchase', ob_get_clean() );
1898
}
1899
1900
function wpinv_checkout_total() {
1901
    global $cart_total;
1902
?>
1903
<div id="wpinv_checkout_total" class="panel panel-info">
1904
    <div class="panel-body">
1905
    <?php
1906
    do_action( 'wpinv_purchase_form_before_checkout_total' );
1907
    ?>
1908
    <strong><?php _e( 'Invoice Total:', 'invoicing' ) ?></strong> <span class="wpinv-chdeckout-total"><?php echo $cart_total;?></span>
1909
    <?php
1910
    do_action( 'wpinv_purchase_form_after_checkout_total' );
1911
    ?>
1912
    </div>
1913
</div>
1914
<?php
1915
}
1916
add_action( 'wpinv_checkout_form_bottom', 'wpinv_checkout_total', 9998 );
1917
1918
function wpinv_checkout_accept_tandc() {
1919
    $page = wpinv_get_option( 'tandc_page' );
1920
    ?>
1921
    <div id="wpinv_checkout_tandc" class="panel panel-success">
1922
        <div class="panel-body">
1923
            <?php echo wpinv_get_policy_text(); ?>
1924
            <?php
1925
            if(isset($page) && (int)$page > 0 && apply_filters( 'wpinv_checkout_show_terms', true )){
1926
                $terms_link = esc_url( get_permalink( $page ) );
0 ignored issues
show
Bug introduced by
It seems like get_permalink($page) 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

1926
                $terms_link = esc_url( /** @scrutinizer ignore-type */ get_permalink( $page ) );
Loading history...
1927
                ?>
1928
                <label class="">
1929
                    <input type="checkbox" class="wpi-terms-checkbox" name="wpi_terms" id="wpi-terms" <?php checked( apply_filters( 'wpinv_terms_is_checked_default', isset( $_POST['wpi_terms'] ) ), true ); ?>> <span><?php printf( __( 'I&rsquo;ve read and accept the <a href="%s" target="_blank" class="wpi-terms-and-conditions-link">terms &amp; conditions</a>', 'invoicing' ), $terms_link ); ?></span> <span class="wpi-required">*</span>
1930
                </label>
1931
            <?php } ?>
1932
        </div>
1933
    </div>
1934
    <?php
1935
}
1936
add_action( 'wpinv_checkout_form_bottom', 'wpinv_checkout_accept_tandc', 9995 );
1937
1938
function wpinv_checkout_submit() {
1939
?>
1940
<div id="wpinv_purchase_submit" class="panel panel-success">
1941
    <div class="panel-body text-center">
1942
    <?php
1943
    do_action( 'wpinv_purchase_form_before_submit' );
1944
    wpinv_checkout_hidden_fields();
1945
    echo wpinv_checkout_button_purchase();
1946
    do_action( 'wpinv_purchase_form_after_submit' );
1947
    ?>
1948
    </div>
1949
</div>
1950
<?php
1951
}
1952
add_action( 'wpinv_checkout_form_bottom', 'wpinv_checkout_submit', 9999 );
1953
1954
function wpinv_receipt_billing_address( $invoice_id = 0 ) {
1955
    $invoice = wpinv_get_invoice( $invoice_id );
1956
1957
    if ( empty( $invoice ) ) {
1958
        return NULL;
1959
    }
1960
1961
    $billing_details = $invoice->get_user_info();
1962
    $address_row = wpinv_get_invoice_address_markup( $billing_details );
1963
1964
    ob_start();
1965
    ?>
1966
    <table class="table table-bordered table-sm wpi-billing-details">
1967
        <tbody>
1968
            <tr class="wpi-receipt-name">
1969
                <th class="text-left"><?php _e( 'Name', 'invoicing' ); ?></th>
1970
                <td><?php echo esc_html( trim( $billing_details['first_name'] . ' ' . $billing_details['last_name'] ) ) ;?></td>
1971
            </tr>
1972
            <tr class="wpi-receipt-email">
1973
                <th class="text-left"><?php _e( 'Email', 'invoicing' ); ?></th>
1974
                <td><?php echo $billing_details['email'] ;?></td>
1975
            </tr>
1976
            <tr class="wpi-receipt-address">
1977
                <th class="text-left"><?php _e( 'Address', 'invoicing' ); ?></th>
1978
                <td><?php echo $address_row ;?></td>
1979
            </tr>
1980
            <?php if ( $billing_details['phone'] ) { ?>
1981
            <tr class="wpi-receipt-phone">
1982
                <th class="text-left"><?php _e( 'Phone', 'invoicing' ); ?></th>
1983
                <td><?php echo esc_html( $billing_details['phone'] ) ;?></td>
1984
            </tr>
1985
            <?php } ?>
1986
        </tbody>
1987
    </table>
1988
    <?php
1989
    $output = ob_get_clean();
1990
    
1991
    $output = apply_filters( 'wpinv_receipt_billing_address', $output, $invoice_id );
1992
1993
    echo $output;
1994
}
1995
1996
function wpinv_filter_success_page_content( $content ) {
1997
    if ( isset( $_GET['payment-confirm'] ) && wpinv_is_success_page() ) {
1998
        if ( has_filter( 'wpinv_payment_confirm_' . sanitize_text_field( $_GET['payment-confirm'] ) ) ) {
1999
            $content = apply_filters( 'wpinv_payment_confirm_' . sanitize_text_field( $_GET['payment-confirm'] ), $content );
2000
        }
2001
    }
2002
2003
    return $content;
2004
}
2005
add_filter( 'the_content', 'wpinv_filter_success_page_content', 99999 );
2006
2007
function wpinv_receipt_actions( $invoice ) {
2008
    if ( !empty( $invoice ) ) {
2009
        $actions = array();
2010
2011
        if ( wpinv_user_can_view_invoice( $invoice->ID ) ) {
2012
            $actions['print']   = array(
2013
                'url'  => $invoice->get_view_url( true ),
2014
                'name' => __( 'Print Invoice', 'invoicing' ),
2015
                'class' => 'btn-primary',
2016
            );
2017
        }
2018
2019
        if ( is_user_logged_in() ) {
2020
            $actions['history'] = array(
2021
                'url'  => wpinv_get_history_page_uri(),
2022
                'name' => __( 'Invoice History', 'invoicing' ),
2023
                'class' => 'btn-warning',
2024
            );
2025
        }
2026
2027
        $actions = apply_filters( 'wpinv_invoice_receipt_actions', $actions, $invoice );
2028
2029
        if ( !empty( $actions ) ) {
2030
        ?>
2031
        <div class="wpinv-receipt-actions text-right">
2032
            <?php foreach ( $actions as $key => $action ) { $class = !empty($action['class']) ? sanitize_html_class( $action['class'] ) : ''; ?>
2033
            <a href="<?php echo esc_url( $action['url'] );?>" class="btn btn-sm <?php echo $class . ' ' . sanitize_html_class( $key );?>" <?php echo ( !empty($action['attrs']) ? $action['attrs'] : '' ) ;?>><?php echo esc_html( $action['name'] );?></a>
2034
            <?php } ?>
2035
        </div>
2036
        <?php
2037
        }
2038
    }
2039
}
2040
add_action( 'wpinv_receipt_start', 'wpinv_receipt_actions', -10, 1 );
2041
2042
function wpinv_invoice_link( $invoice_id ) {
2043
    $invoice = wpinv_get_invoice( $invoice_id );
2044
2045
    if ( empty( $invoice ) ) {
2046
        return NULL;
2047
    }
2048
2049
    $invoice_link = '<a href="' . esc_url( $invoice->get_view_url() ) . '">' . $invoice->get_number() . '</a>';
0 ignored issues
show
Bug introduced by
It seems like $invoice->get_view_url() 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

2049
    $invoice_link = '<a href="' . esc_url( /** @scrutinizer ignore-type */ $invoice->get_view_url() ) . '">' . $invoice->get_number() . '</a>';
Loading history...
2050
2051
    return apply_filters( 'wpinv_get_invoice_link', $invoice_link, $invoice );
2052
}
2053
2054
function wpinv_invoice_subscription_details( $invoice ) {
2055
    if ( !empty( $invoice ) && $invoice->is_recurring() && ! wpinv_is_subscription_payment( $invoice ) ) {
2056
        $subscription = wpinv_get_subscription( $invoice, true );
2057
2058
        if ( empty( $subscription ) ) {
2059
            return;
2060
        }
2061
2062
        $frequency = WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency($subscription->period, $subscription->frequency);
2063
        $billing = wpinv_price(wpinv_format_amount($subscription->recurring_amount), wpinv_get_invoice_currency_code($subscription->parent_payment_id)) . ' / ' . $frequency;
2064
        $initial = wpinv_price(wpinv_format_amount($subscription->initial_amount), wpinv_get_invoice_currency_code($subscription->parent_payment_id));
2065
2066
        $payments = $subscription->get_child_payments();
2067
        ?>
2068
        <div class="wpinv-subscriptions-details">
2069
            <h3 class="wpinv-subscriptions-t"><?php echo apply_filters( 'wpinv_subscription_details_title', __( 'Subscription Details', 'invoicing' ) ); ?></h3>
2070
            <table class="table">
2071
                <thead>
2072
                    <tr>
2073
                        <th><?php _e( 'Billing Cycle', 'invoicing' ) ;?></th>
2074
                        <th><?php _e( 'Start Date', 'invoicing' ) ;?></th>
2075
                        <th><?php _e( 'Expiration Date', 'invoicing' ) ;?></th>
2076
                        <th class="text-center"><?php _e( 'Times Billed', 'invoicing' ) ;?></th>
2077
                        <th class="text-center"><?php _e( 'Status', 'invoicing' ) ;?></th>
2078
                    </tr>
2079
                </thead>
2080
                <tbody>
2081
                    <tr>
2082
                        <td><?php printf(_x('%s then %s', 'Initial subscription amount then billing cycle and amount', 'invoicing'), $initial, $billing); ?></td>
2083
                        <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

2083
                        <td><?php echo date_i18n(/** @scrutinizer ignore-type */ get_option('date_format'), strtotime($subscription->created, current_time('timestamp'))); ?></td>
Loading history...
2084
                        <td><?php echo date_i18n(get_option('date_format'), strtotime($subscription->expiration, current_time('timestamp'))); ?></td>
2085
                        <td class="text-center"><?php echo $subscription->get_times_billed() . ' / ' . (($subscription->bill_times == 0) ? 'Until Cancelled' : $subscription->bill_times); ?></td>
2086
                        <td class="text-center wpi-sub-status"><?php echo $subscription->get_status_label(); ?></td>
2087
                    </tr>
2088
                </tbody>
2089
            </table>
2090
        </div>
2091
        <?php if ( !empty( $payments ) ) { ?>
2092
        <div class="wpinv-renewal-payments">
2093
            <h3 class="wpinv-renewals-t"><?php echo apply_filters( 'wpinv_renewal_payments_title', __( 'Renewal Payments', 'invoicing' ) ); ?></h3>
2094
            <table class="table">
2095
                <thead>
2096
                    <tr>
2097
                        <th>#</th>
2098
                        <th><?php _e( 'Invoice', 'invoicing' ) ;?></th>
2099
                        <th><?php _e( 'Date', 'invoicing' ) ;?></th>
2100
                        <th class="text-right"><?php _e( 'Amount', 'invoicing' ) ;?></th>
2101
                    </tr>
2102
                </thead>
2103
                <tbody>
2104
                    <?php
2105
                        $i = 1;
2106
                        foreach ( $payments as $payment ) {
2107
                            $invoice_id = $payment->ID;
2108
                    ?>
2109
                    <tr>
2110
                        <th scope="row"><?php echo $i;?></th>
2111
                        <td><?php echo wpinv_invoice_link( $invoice_id ) ;?></td>
2112
                        <td><?php echo wpinv_get_invoice_date( $invoice_id ); ?></td>
2113
                        <td class="text-right"><?php echo wpinv_payment_total( $invoice_id, true ); ?></td>
2114
                    </tr>
2115
                    <?php $i++; } ?>
2116
                </tbody>
2117
            </table>
2118
        </div>
2119
        <?php } ?>
2120
        <?php
2121
    }
2122
}
2123
2124
function wpinv_cart_total_label( $label, $invoice ) {
2125
    if ( empty( $invoice ) ) {
2126
        return $label;
2127
    }
2128
2129
    $prefix_label = '';
2130
    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...
2131
        $prefix_label   = '<span class="label label-primary label-recurring">' . __( 'Recurring Payment', 'invoicing' ) . '</span> ' . wpinv_subscription_payment_desc( $invoice );
2132
    } else if ( $invoice->is_renewal() ) {
2133
        $prefix_label   = '<span class="label label-primary label-renewal">' . __( 'Renewal Payment', 'invoicing' ) . '</span> ';        
2134
    }
2135
2136
    if ( $prefix_label != '' ) {
2137
        $label  = '<span class="wpinv-cart-sub-desc">' . $prefix_label . '</span> ' . $label;
2138
    }
2139
2140
    return $label;
2141
}
2142
add_filter( 'wpinv_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
2143
add_filter( 'wpinv_email_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
2144
add_filter( 'wpinv_print_cart_total_label', 'wpinv_cart_total_label', 10, 2 );
2145
2146
add_action( 'wpinv_invoice_print_middle', 'wpinv_invoice_subscription_details', 10, 1 );
2147
2148
function wpinv_invoice_print_description( $invoice ) {
2149
    if ( empty( $invoice ) ) {
2150
        return NULL;
2151
    }
2152
    if ( $description = wpinv_get_invoice_description( $invoice->ID ) ) {
2153
        ?>
2154
        <div class="row wpinv-lower">
2155
            <div class="col-sm-12 wpinv-description">
2156
                <?php echo wpautop( $description ); ?>
2157
            </div>
2158
        </div>
2159
        <?php
2160
    }
2161
}
2162
add_action( 'wpinv_invoice_print_middle', 'wpinv_invoice_print_description', 10.1, 1 );
0 ignored issues
show
Bug introduced by
10.1 of type double is incompatible with the type integer expected by parameter $priority of add_action(). ( Ignorable by Annotation )

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

2162
add_action( 'wpinv_invoice_print_middle', 'wpinv_invoice_print_description', /** @scrutinizer ignore-type */ 10.1, 1 );
Loading history...
2163
2164
function wpinv_invoice_print_payment_info( $invoice ) {
2165
    if ( empty( $invoice ) ) {
2166
        return NULL;
2167
    }
2168
2169
    if ( $payments_info = wpinv_display_payments_info( $invoice->ID, false ) ) {
2170
        ?>
2171
        <div class="row wpinv-payments">
2172
            <div class="col-sm-12">
2173
                <?php echo $payments_info; ?>
2174
            </div>
2175
        </div>
2176
        <?php 
2177
    }
2178
}
2179
// add_action( 'wpinv_invoice_print_after_line_items', 'wpinv_invoice_print_payment_info', 10, 1 );
2180
2181
function wpinv_get_invoice_note_line_item( $note, $echo = true ) {
2182
    if ( empty( $note ) ) {
2183
        return NULL;
2184
    }
2185
2186
    if ( is_int( $note ) ) {
2187
        $note = get_comment( $note );
2188
    }
2189
2190
    if ( !( is_object( $note ) && is_a( $note, 'WP_Comment' ) ) ) {
2191
        return NULL;
2192
    }
2193
2194
    $note_classes   = array( 'note' );
2195
    $note_classes[] = get_comment_meta( $note->comment_ID, '_wpi_customer_note', true ) ? 'customer-note' : '';
2196
    $note_classes[] = $note->comment_author === 'System' ? 'system-note' : '';
2197
    $note_classes   = apply_filters( 'wpinv_invoice_note_class', array_filter( $note_classes ), $note );
2198
    $note_classes   = !empty( $note_classes ) ? implode( ' ', $note_classes ) : '';
2199
2200
    ob_start();
2201
    ?>
2202
    <li rel="<?php echo absint( $note->comment_ID ) ; ?>" class="<?php echo esc_attr( $note_classes ); ?>">
2203
        <div class="note_content">
2204
            <?php echo wpautop( wptexturize( wp_kses_post( $note->comment_content ) ) ); ?>
2205
        </div>
2206
        <p class="meta">
2207
            <abbr class="exact-date" title="<?php echo $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;
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

2207
            <abbr class="exact-date" title="<?php echo $note->comment_date; ?>"><?php printf( __( '%1$s - %2$s at %3$s', 'invoicing' ), $note->comment_author, date_i18n( /** @scrutinizer ignore-type */ get_option( 'date_format' ), strtotime( $note->comment_date ) ), date_i18n( get_option( 'time_format' ), strtotime( $note->comment_date ) ) ); ?></abbr>&nbsp;&nbsp;
Loading history...
2208
            <?php if ( $note->comment_author !== 'System' || wpinv_current_user_can_manage_invoicing() ) { ?>
2209
                <a href="#" class="delete_note"><?php _e( 'Delete note', 'invoicing' ); ?></a>
2210
            <?php } ?>
2211
        </p>
2212
    </li>
2213
    <?php
2214
    $note_content = ob_get_clean();
2215
    $note_content = apply_filters( 'wpinv_get_invoice_note_line_item', $note_content, $note, $echo );
2216
2217
    if ( $echo ) {
2218
        echo $note_content;
2219
    } else {
2220
        return $note_content;
2221
    }
2222
}
2223
2224
function wpinv_invalid_invoice_content() {
2225
    global $post;
2226
2227
    $invoice = wpinv_get_invoice( $post->ID );
2228
2229
    $error = __( 'This invoice is only viewable by clicking on the invoice link that was sent to you via email.', 'invoicing' );
2230
    if ( !empty( $invoice->ID ) && $invoice->has_status( array_keys( wpinv_get_invoice_statuses() ) ) ) {
2231
        if ( is_user_logged_in() ) {
2232
            if ( wpinv_require_login_to_checkout() ) {
2233
                if ( isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
2234
                    $error = __( 'You are not allowed to view this invoice.', 'invoicing' );
2235
                }
2236
            }
2237
        } else {
2238
            if ( wpinv_require_login_to_checkout() ) {
2239
                if ( isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
2240
                    $error = __( 'You must be logged in to view this invoice.', 'invoicing' );
2241
                }
2242
            }
2243
        }
2244
    } else {
2245
        $error = __( 'This invoice is deleted or does not exist.', 'invoicing' );
2246
    }
2247
    ?>
2248
    <div class="row wpinv-row-invalid">
2249
        <div class="col-md-6 col-md-offset-3 wpinv-message error">
2250
            <h3><?php _e( 'Access Denied', 'invoicing' ); ?></h3>
2251
            <p class="wpinv-msg-text"><?php echo $error; ?></p>
2252
        </div>
2253
    </div>
2254
    <?php
2255
}
2256
add_action( 'wpinv_invalid_invoice_content', 'wpinv_invalid_invoice_content' );
2257
2258
add_action( 'wpinv_checkout_billing_fields_last', 'wpinv_force_company_name_field');
2259
function wpinv_force_company_name_field(){
2260
    $invoice = wpinv_get_invoice_cart();
2261
    $user_id = wpinv_get_user_id( $invoice->ID );
2262
    $company = empty( $user_id ) ? "" : get_user_meta( $user_id, '_wpinv_company', true );
2263
    if ( 1 == wpinv_get_option( 'force_show_company' ) && !wpinv_use_taxes() ) {
2264
        ?>
2265
        <p class="wpi-cart-field wpi-col2 wpi-colf">
2266
            <label for="wpinv_company" class="wpi-label"><?php _e('Company Name', 'invoicing'); ?></label>
2267
            <?php
2268
            echo wpinv_html_text(array(
2269
                'id' => 'wpinv_company',
2270
                'name' => 'wpinv_company',
2271
                'value' => $company,
2272
                'class' => 'wpi-input form-control',
2273
                'placeholder' => __('Company name', 'invoicing'),
2274
            ));
2275
            ?>
2276
        </p>
2277
        <?php
2278
    }
2279
}
2280
2281
/**
2282
 * Function to get privacy policy text.
2283
 *
2284
 * @since 1.0.13
2285
 * @return string
2286
 */
2287
function wpinv_get_policy_text() {
2288
    $privacy_page_id = get_option( 'wp_page_for_privacy_policy', 0 );
2289
2290
    $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]' ));
2291
2292
    if(!$privacy_page_id){
2293
        $privacy_page_id = wpinv_get_option( 'privacy_page', 0 );
2294
    }
2295
2296
    $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

2296
    $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...
2297
2298
    $find_replace = array(
2299
        '[wpinv_privacy_policy]' => $privacy_link,
2300
    );
2301
2302
    $privacy_text = str_replace( array_keys( $find_replace ), array_values( $find_replace ), $text );
2303
2304
    return wp_kses_post(wpautop($privacy_text));
2305
}
2306
2307
2308
/**
2309
 * Allows the user to set their own price for an invoice item
2310
 */
2311
function wpinv_checkout_cart_item_name_your_price( $cart_item, $key ) {
2312
    
2313
    //Ensure we have an item id
2314
    if(! is_array( $cart_item ) || empty( $cart_item['id'] ) ) {
2315
        return;
2316
    }
2317
2318
    //Fetch the item
2319
    $item_id = $cart_item['id'];
2320
    $item    = new WPInv_Item( $item_id );
2321
    
2322
    if(! $item->supports_dynamic_pricing() || !$item->get_is_dynamic_pricing() ) {
2323
        return;
2324
    }
2325
2326
    //Fetch the dynamic pricing "strings"
2327
    $suggested_price_text = esc_html( wpinv_get_option( 'suggested_price_text', __( 'Suggested Price:', 'invoicing' ) ) );
2328
    $minimum_price_text   = esc_html( wpinv_get_option( 'minimum_price_text', __( 'Minimum Price:', 'invoicing' ) ) );
2329
    $name_your_price_text = esc_html( wpinv_get_option( 'name_your_price_text', __( 'Name Your Price', 'invoicing' ) ) );
2330
2331
    //Display a "name_your_price" button
2332
    echo " &mdash; <a href='#' class='wpinv-name-your-price-frontend small'>$name_your_price_text</a></div>";
2333
2334
    //Display a name_your_price form
2335
    echo '<div class="name-your-price-miniform">';
2336
    
2337
    //Maybe display the recommended price
2338
    if( $item->get_price() > 0 && !empty( $suggested_price_text ) ) {
2339
        $suggested_price = $item->get_the_price();
2340
        echo "<div>$suggested_price_text &mdash; $suggested_price</div>";
2341
    }
2342
2343
    //Display the update price form
2344
    $symbol         = wpinv_currency_symbol();
2345
    $position       = wpinv_currency_position();
2346
    $minimum        = esc_attr( $item->get_minimum_price() );
2347
    $price          = esc_attr( $cart_item['item_price'] );
2348
    $update         = esc_attr__( "Update", 'invoicing' );
2349
2350
    //Ensure it supports dynamic prici
2351
    if( $price < $minimum ) {
2352
        $price = $minimum;
2353
    }
2354
2355
    echo '<label>';
2356
    echo $position != 'right' ? $symbol . '&nbsp;' : '';
2357
    echo "<input type='number' min='$minimum' placeholder='$price' value='$price' class='wpi-field-price' />";
2358
    echo $position == 'right' ? '&nbsp;' . $symbol : '' ;
2359
    echo "</label>";
2360
    echo "<input type='hidden' value='$item_id' class='wpi-field-item' />";
2361
    echo "<a class='btn btn-success wpinv-submit wpinv-update-dynamic-price-frontend'>$update</a>";
2362
2363
    //Maybe display the minimum price
2364
    if( $item->get_minimum_price() > 0 && !empty( $minimum_price_text ) ) {
2365
        $minimum_price = wpinv_price( wpinv_format_amount( $item->get_minimum_price() ) );
2366
        echo "<div>$minimum_price_text &mdash; $minimum_price</div>";
2367
    }
2368
2369
    echo "</div>";
2370
2371
}
2372
add_action( 'wpinv_checkout_cart_item_price_after', 'wpinv_checkout_cart_item_name_your_price', 10, 2 );
2373
2374
function wpinv_oxygen_fix_conflict() {
2375
    global $ct_ignore_post_types;
2376
2377
    if ( ! is_array( $ct_ignore_post_types ) ) {
2378
        $ct_ignore_post_types = array();
2379
    }
2380
2381
    $post_types = array( 'wpi_discount', 'wpi_invoice', 'wpi_item' );
2382
2383
    foreach ( $post_types as $post_type ) {
2384
        $ct_ignore_post_types[] = $post_type;
2385
2386
        // Ignore post type
2387
        add_filter( 'pre_option_oxygen_vsb_ignore_post_type_' . $post_type, '__return_true', 999 );
2388
    }
2389
2390
    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

2390
    /** @scrutinizer ignore-call */ 
2391
    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...
2391
    add_filter( 'template_include', 'wpinv_template', 999, 1 );
2392
}