Passed
Push — master ( 651b7e...bf7bd5 )
by Brian
06:27
created

wpinv_update_options()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Contains settings related functions
4
 *
5
 * @package Invoicing
6
 * @since   1.0.0
7
 */
8
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * Retrieves all default settings.
13
 * 
14
 * @return array
15
 */
16
function wpinv_get_settings() {
17
    $defaults = array();
18
19
    foreach ( array_values( wpinv_get_registered_settings() ) as $tab_settings ) {
20
21
        foreach ( array_values( $tab_settings ) as $section_settings ) {
22
23
            foreach ( $section_settings as $key => $setting ) {
24
                if ( isset( $setting['std'] ) ) {
25
                    $defaults[ $key ] = $setting['std'];
26
                }
27
            }
28
29
        }
30
31
    }
32
33
    return $defaults;
34
35
}
36
37
/**
38
 * Retrieves all settings.
39
 * 
40
 * @return array
41
 */
42
function wpinv_get_options() {
43
    global $wpinv_options;
44
45
    // Try fetching the saved options.
46
    if ( ! is_array( $wpinv_options ) ) {
47
        $wpinv_options = get_option( 'wpinv_settings' );
48
    }
49
50
    // If that fails, don't fetch the default settings to prevent a loop.
51
    if ( ! is_array( $wpinv_options ) ) {
52
        $wpinv_options = array();
53
    }
54
55
    return $wpinv_options;
56
}
57
58
/**
59
 * Retrieves a single setting.
60
 * 
61
 * @param string $key the setting key.
62
 * @param mixed $default The default value to use if the setting has not been set.
63
 * @return array
64
 */
65
function wpinv_get_option( $key = '', $default = false ) {
66
67
    $options = wpinv_get_options();
68
    $value   = isset( $options[ $key ] ) ? $options[ $key ] : $default;
69
    $value   = apply_filters( 'wpinv_get_option', $value, $key, $default );
70
71
    return apply_filters( 'wpinv_get_option_' . $key, $value, $key, $default );
0 ignored issues
show
Bug Best Practice introduced by
The expression return apply_filters('wp...$value, $key, $default) could also return false which is incompatible with the documented return type array. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
72
}
73
74
/**
75
 * Updates all settings.
76
 * 
77
 * @param array $options the new options.
78
 * @return bool
79
 */
80
function wpinv_update_options( $options ) {
81
    global $wpinv_options;
82
83
    // update the option.
84
    if ( is_array( $options ) && update_option( 'wpinv_settings', $options ) ) {
85
        $wpinv_options = $options;
86
        return true;
87
    }
88
89
    return false;
90
}
91
92
/**
93
 * Updates a single setting.
94
 * 
95
 * @param string $key the setting key.
96
 * @param mixed $value The setting value.
97
 * @return bool
98
 */
99
function wpinv_update_option( $key = '', $value = false ) {
100
101
    // If no key, exit.
102
    if ( empty( $key ) ) {
103
        return false;
104
    }
105
106
    // Maybe delete the option instead.
107
    if ( is_null( $value ) ) {
108
        return wpinv_delete_option( $key );
109
    }
110
111
    // Prepare the new options.
112
    $options         = wpinv_get_options();
113
    $options[ $key ] = apply_filters( 'wpinv_update_option', $value, $key );
114
115
    // Save the new options.
116
    return wpinv_update_options( $options );
117
118
}
119
120
/**
121
 * Deletes a single setting.
122
 * 
123
 * @param string $key the setting key.
124
 * @return bool
125
 */
126
function wpinv_delete_option( $key = '' ) {
127
128
    // If no key, exit
129
    if ( empty( $key ) ) {
130
        return false;
131
    }
132
133
    $options = wpinv_get_options();
134
135
    if ( isset( $options[ $key ] ) ) {
136
        unset( $options[ $key ] );
137
        return wpinv_update_options( $options );
138
    }
139
140
    return true;
141
142
}
143
144
/**
145
 * Register settings after admin inits.
146
 * 
147
 */
148
function wpinv_register_settings() {
149
150
    // Loop through all tabs.
151
    foreach ( wpinv_get_registered_settings() as $tab => $sections ) {
152
153
        // In each tab, loop through sections.
154
        foreach ( $sections as $section => $settings ) {
155
156
            // Check for backwards compatibility
157
            $section_tabs = wpinv_get_settings_tab_sections( $tab );
158
            if ( ! is_array( $section_tabs ) || ! array_key_exists( $section, $section_tabs ) ) {
159
                $section = 'main';
160
                $settings = $sections;
161
            }
162
163
            // Register the setting section.
164
            add_settings_section(
165
                'wpinv_settings_' . $tab . '_' . $section,
166
                __return_null(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of __return_null() is correct as it seems to always return null.

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

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

}

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

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

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

Loading history...
167
                '__return_false',
168
                'wpinv_settings_' . $tab . '_' . $section
169
            );
170
171
            foreach ( $settings as $option ) {
172
                if ( ! empty( $option['id'] ) ) {
173
                    wpinv_register_settings_option( $tab, $section, $option );
174
                }
175
            }
176
177
        }
178
    }
179
180
    // Creates our settings in the options table.
181
    register_setting( 'wpinv_settings', 'wpinv_settings', 'wpinv_settings_sanitize' );
0 ignored issues
show
Bug introduced by
'wpinv_settings_sanitize' of type string is incompatible with the type array expected by parameter $args of register_setting(). ( Ignorable by Annotation )

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

181
    register_setting( 'wpinv_settings', 'wpinv_settings', /** @scrutinizer ignore-type */ 'wpinv_settings_sanitize' );
Loading history...
182
}
183
add_action( 'admin_init', 'wpinv_register_settings' );
184
185
/**
186
 * Register a single settings option.
187
 * 
188
 * @param string $tab
189
 * @param string $section
190
 * @param string $option
191
 * 
192
 */
193
function wpinv_register_settings_option( $tab, $section, $option ) {
194
195
    $name    = isset( $option['name'] ) ? $option['name'] : '';
196
    $cb      = "wpinv_{$option['type']}_callback";
197
    $section = "wpinv_settings_{$tab}_$section";
198
199
    // Loop through all tabs.
200
    add_settings_field(
201
        'wpinv_settings[' . $option['id'] . ']',
202
        $name,
203
        function_exists( $cb ) ? $cb : 'wpinv_missing_callback',
204
        $section,
205
        $section,
206
        array(
207
            'section'     => $section,
208
            'id'          => isset( $option['id'] )          ? $option['id']          : null,
209
            'desc'        => isset( $option['desc'] )        ? $option['desc']        : '',
210
            'name'        => isset( $option['name'] )        ? $option['name']        : null,
211
            'size'        => isset( $option['size'] )        ? $option['size']        : null,
212
            'options'     => isset( $option['options'] )     ? $option['options']     : '',
213
            'selected'    => isset( $option['selected'] )    ? $option['selected']    : null,
214
            'std'         => isset( $option['std'] )         ? $option['std']         : '',
215
            'min'         => isset( $option['min'] )         ? $option['min']         : null,
216
            'max'         => isset( $option['max'] )         ? $option['max']         : null,
217
            'step'        => isset( $option['step'] )        ? $option['step']        : null,
218
            'placeholder' => isset( $option['placeholder'] ) ? $option['placeholder'] : null,
219
            'allow_blank' => isset( $option['allow_blank'] ) ? $option['allow_blank'] : true,
220
            'readonly'    => isset( $option['readonly'] )    ? $option['readonly']    : false,
221
            'faux'        => isset( $option['faux'] )        ? $option['faux']        : false,
222
            'onchange'    => isset( $option['onchange'] )   ? $option['onchange']     : '',
223
            'custom'      => isset( $option['custom'] )     ? $option['custom']       : '',
224
            'class'       => isset( $option['class'] )     ? $option['class']         : '',
225
            'cols'        => isset( $option['cols'] ) && (int) $option['cols'] > 0 ? (int) $option['cols'] : 50,
226
            'rows'        => isset( $option['rows'] ) && (int) $option['rows'] > 0 ? (int) $option['rows'] : 5,
227
        )
228
    );
229
230
}
231
232
/**
233
 * Returns an array of all registered settings.
234
 * 
235
 * @return array
236
 */
237
function wpinv_get_registered_settings() {
238
    return apply_filters( 'wpinv_registered_settings', wpinv_get_data( 'admin-settings' ) );
239
}
240
241
/**
242
 * Sanitizes settings before they are saved.
243
 * 
244
 * @return array
245
 */
246
function wpinv_settings_sanitize( $input = array() ) {
247
248
    $wpinv_options = wpinv_get_options();
249
250
    if ( empty( wp_get_raw_referer() ) ) {
251
        return $input;
252
    }
253
254
    wp_parse_str( wp_get_raw_referer(), $referrer );
0 ignored issues
show
Bug introduced by
It seems like wp_get_raw_referer() can also be of type false; however, parameter $string of wp_parse_str() 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

254
    wp_parse_str( /** @scrutinizer ignore-type */ wp_get_raw_referer(), $referrer );
Loading history...
255
256
    $settings = wpinv_get_registered_settings();
257
    $tab      = isset( $referrer['tab'] ) ? $referrer['tab'] : 'general';
258
    $section  = isset( $referrer['section'] ) ? $referrer['section'] : 'main';
259
260
    $input = $input ? $input : array();
261
    $input = apply_filters( 'wpinv_settings_tab_' . $tab . '_sanitize', $input );
262
    $input = apply_filters( 'wpinv_settings_' . $tab . '-' . $section . '_sanitize', $input );
263
264
    // Loop through each setting being saved and pass it through a sanitization filter
265
    foreach ( $input as $key => $value ) {
266
267
        // Get the setting type (checkbox, select, etc)
268
        $type = isset( $settings[ $tab ][$section][ $key ]['type'] ) ? $settings[ $tab ][$section][ $key ]['type'] : false;
269
270
        if ( $type ) {
271
            // Field type specific filter
272
            $input[$key] = apply_filters( 'wpinv_settings_sanitize_' . $type, $value, $key );
273
        }
274
275
        // General filter
276
        $input[ $key ] = apply_filters( 'wpinv_settings_sanitize', $input[ $key ], $key );
277
    }
278
279
    // Loop through the whitelist and unset any that are empty for the tab being saved
280
    $main_settings    = $section == 'main' ? $settings[ $tab ] : array(); // Check for extensions that aren't using new sections
281
    $section_settings = ! empty( $settings[ $tab ][ $section ] ) ? $settings[ $tab ][ $section ] : array();
282
283
    $found_settings = array_merge( $main_settings, $section_settings );
284
285
    if ( ! empty( $found_settings ) ) {
286
        foreach ( $found_settings as $key => $value ) {
287
288
            // settings used to have numeric keys, now they have keys that match the option ID. This ensures both methods work
289
            if ( is_numeric( $key ) ) {
290
                $key = $value['id'];
291
            }
292
293
            if ( ! isset( $input[ $key ] ) && isset( $wpinv_options[ $key ] ) ) {
294
                unset( $wpinv_options[ $key ] );
295
            }
296
        }
297
    }
298
299
    // Merge our new settings with the existing
300
    $output = array_merge( $wpinv_options, $input );
301
302
    add_settings_error( 'wpinv-notices', '', __( 'Settings updated.', 'invoicing' ), 'updated' );
303
304
    return $output;
305
}
306
307
function wpinv_settings_sanitize_misc_accounting( $input ) {
308
309
    if ( ! wpinv_current_user_can_manage_invoicing() ) {
310
        return $input;
311
    }
312
313
    if( ! empty( $input['enable_sequential'] ) && !wpinv_get_option( 'enable_sequential' ) ) {
314
        // Shows an admin notice about upgrading previous order numbers
315
        getpaid_session()->set( 'upgrade_sequential', '1' );
316
    }
317
318
    return $input;
319
}
320
add_filter( 'wpinv_settings_misc-accounting_sanitize', 'wpinv_settings_sanitize_misc_accounting' );
321
322
function wpinv_settings_sanitize_tax_rates( $input ) {
323
    if( ! wpinv_current_user_can_manage_invoicing() ) {
324
        return $input;
325
    }
326
327
    $new_rates = !empty( $_POST['tax_rates'] ) ? array_values( $_POST['tax_rates'] ) : array();
328
329
    $tax_rates = array();
330
331
    if ( !empty( $new_rates ) ) {
332
        foreach ( $new_rates as $rate ) {
333
            if ( isset( $rate['country'] ) && empty( $rate['country'] ) && empty( $rate['state'] ) ) {
334
                continue;
335
            }
336
            
337
            $rate['rate'] = wpinv_sanitize_amount( $rate['rate'], 4 );
338
            
339
            $tax_rates[] = $rate;
340
        }
341
    }
342
343
    update_option( 'wpinv_tax_rates', $tax_rates );
344
345
    return $input;
346
}
347
add_filter( 'wpinv_settings_taxes-rates_sanitize', 'wpinv_settings_sanitize_tax_rates' );
348
349
function wpinv_sanitize_text_field( $input ) {
350
    return trim( $input );
351
}
352
add_filter( 'wpinv_settings_sanitize_text', 'wpinv_sanitize_text_field' );
353
354
function wpinv_get_settings_tabs() {
355
    $tabs             = array();
356
    $tabs['general']  = __( 'General', 'invoicing' );
357
    $tabs['gateways'] = __( 'Payment Gateways', 'invoicing' );
358
    $tabs['taxes']    = __( 'Taxes', 'invoicing' );
359
    $tabs['emails']   = __( 'Emails', 'invoicing' );
360
    $tabs['privacy']  = __( 'Privacy', 'invoicing' );
361
    $tabs['misc']     = __( 'Misc', 'invoicing' );
362
    $tabs['tools']    = __( 'Tools', 'invoicing' );
363
364
    return apply_filters( 'wpinv_settings_tabs', $tabs );
365
}
366
367
function wpinv_get_settings_tab_sections( $tab = false ) {
368
    $tabs     = false;
369
    $sections = wpinv_get_registered_settings_sections();
370
371
    if( $tab && ! empty( $sections[ $tab ] ) ) {
372
        $tabs = $sections[ $tab ];
373
    } else if ( $tab ) {
374
        $tabs = false;
375
    }
376
377
    return $tabs;
378
}
379
380
function wpinv_get_registered_settings_sections() {
381
    static $sections = false;
382
383
    if ( false !== $sections ) {
384
        return $sections;
385
    }
386
387
    $sections = array(
388
        'general' => apply_filters( 'wpinv_settings_sections_general', array(
389
            'main' => __( 'General Settings', 'invoicing' ),
390
            'currency_section' => __( 'Currency Settings', 'invoicing' ),
391
            'labels' => __( 'Label Texts', 'invoicing' ),
392
        ) ),
393
        'gateways' => apply_filters( 'wpinv_settings_sections_gateways', array(
394
            'main' => __( 'Gateway Settings', 'invoicing' ),
395
        ) ),
396
        'taxes' => apply_filters( 'wpinv_settings_sections_taxes', array(
397
            'main' => __( 'Tax Settings', 'invoicing' ),
398
            'rates' => __( 'Tax Rates', 'invoicing' ),
399
        ) ),
400
        'emails' => apply_filters( 'wpinv_settings_sections_emails', array(
401
            'main' => __( 'Email Settings', 'invoicing' ),
402
        ) ),
403
        'privacy' => apply_filters( 'wpinv_settings_sections_privacy', array(
404
            'main' => __( 'Privacy policy', 'invoicing' ),
405
        ) ),
406
        'misc' => apply_filters( 'wpinv_settings_sections_misc', array(
407
            'main' => __( 'Miscellaneous', 'invoicing' ),
408
            'fields' => __( 'Fields Settings', 'invoicing' ),
409
            'custom-css' => __( 'Custom CSS', 'invoicing' ),
410
        ) ),
411
        'tools' => apply_filters( 'wpinv_settings_sections_tools', array(
412
            'main' => __( 'Diagnostic Tools', 'invoicing' ),
413
        ) ),
414
    );
415
416
    $sections = apply_filters( 'wpinv_settings_sections', $sections );
417
418
    return $sections;
419
}
420
421
function wpinv_get_pages( $with_slug = false, $default_label = NULL ) {
422
	$pages_options = array();
423
424
	if( $default_label !== NULL && $default_label !== false ) {
425
		$pages_options = array( '' => $default_label ); // Blank option
426
	}
427
428
	$pages = get_pages();
429
	if ( $pages ) {
430
		foreach ( $pages as $page ) {
431
			$title = $with_slug ? $page->post_title . ' (' . $page->post_name . ')' : $page->post_title;
432
            $pages_options[ $page->ID ] = $title;
433
		}
434
	}
435
436
	return $pages_options;
437
}
438
439
function wpinv_header_callback( $args ) {
440
	if ( !empty( $args['desc'] ) ) {
441
        echo $args['desc'];
442
    }
443
}
444
445
function wpinv_hidden_callback( $args ) {
446
	global $wpinv_options;
447
448
	if ( isset( $args['set_value'] ) ) {
449
		$value = $args['set_value'];
450
	} elseif ( isset( $wpinv_options[ $args['id'] ] ) ) {
451
		$value = $wpinv_options[ $args['id'] ];
452
	} else {
453
		$value = isset( $args['std'] ) ? $args['std'] : '';
454
	}
455
456
	if ( isset( $args['faux'] ) && true === $args['faux'] ) {
457
		$args['readonly'] = true;
458
		$value = isset( $args['std'] ) ? $args['std'] : '';
459
		$name  = '';
460
	} else {
461
		$name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"';
462
	}
463
464
	$html = '<input type="hidden" id="wpinv_settings[' . wpinv_sanitize_key( $args['id'] ) . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '" />';
465
    
466
	echo $html;
467
}
468
469
function wpinv_checkbox_callback( $args ) {
470
	global $wpinv_options;
471
    
472
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
473
474
	if ( isset( $args['faux'] ) && true === $args['faux'] ) {
475
		$name = '';
476
	} else {
477
		$name = 'name="wpinv_settings[' . $sanitize_id . ']"';
478
	}
479
480
	$checked = isset( $wpinv_options[ $args['id'] ] ) ? checked( 1, $wpinv_options[ $args['id'] ], false ) : '';
481
	$html = '<input type="checkbox" id="wpinv_settings[' . $sanitize_id . ']"' . $name . ' value="1" ' . $checked . '/>';
482
	$html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
483
484
	echo $html;
485
}
486
487
function wpinv_multicheck_callback( $args ) {
488
	global $wpinv_options;
489
490
	$sanitize_id = wpinv_sanitize_key( $args['id'] );
491
	$class = !empty( $args['class'] ) ? ' ' . esc_attr( $args['class'] ) : '';
492
493
	if ( ! empty( $args['options'] ) ) {
494
		echo '<div class="wpi-mcheck-rows wpi-mcheck-' . $sanitize_id . $class . '">';
495
        foreach( $args['options'] as $key => $option ):
496
			$sanitize_key = wpinv_sanitize_key( $key );
497
			if ( isset( $wpinv_options[$args['id']][$sanitize_key] ) ) { 
498
				$enabled = $sanitize_key;
499
			} else { 
500
				$enabled = NULL; 
501
			}
502
			echo '<div class="wpi-mcheck-row"><input name="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="' . esc_attr( $sanitize_key ) . '" ' . checked( $sanitize_key, $enabled, false ) . '/>&nbsp;';
503
			echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . wp_kses_post( $option ) . '</label></div>';
504
		endforeach;
505
		echo '</div>';
506
		echo '<p class="description">' . $args['desc'] . '</p>';
507
	}
508
}
509
510
function wpinv_payment_icons_callback( $args ) {
511
	global $wpinv_options;
512
    
513
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
514
515
	if ( ! empty( $args['options'] ) ) {
516
		foreach( $args['options'] as $key => $option ) {
517
            $sanitize_key = wpinv_sanitize_key( $key );
518
            
519
			if( isset( $wpinv_options[$args['id']][$key] ) ) {
520
				$enabled = $option;
521
			} else {
522
				$enabled = NULL;
523
			}
524
525
			echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" style="margin-right:10px;line-height:16px;height:16px;display:inline-block;">';
526
527
				echo '<input name="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="' . esc_attr( $option ) . '" ' . checked( $option, $enabled, false ) . '/>&nbsp;';
528
529
				if ( wpinv_string_is_image_url( $key ) ) {
530
					echo '<img class="payment-icon" src="' . esc_url( $key ) . '" style="width:32px;height:24px;position:relative;top:6px;margin-right:5px;"/>';
531
				} else {
532
					$card = strtolower( str_replace( ' ', '', $option ) );
533
534
					if ( has_filter( 'wpinv_accepted_payment_' . $card . '_image' ) ) {
535
						$image = apply_filters( 'wpinv_accepted_payment_' . $card . '_image', '' );
536
					} else {
537
						$image       = wpinv_locate_template( 'images' . DIRECTORY_SEPARATOR . 'icons' . DIRECTORY_SEPARATOR . $card . '.gif', false );
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $template_path of wpinv_locate_template(). ( Ignorable by Annotation )

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

537
						$image       = wpinv_locate_template( 'images' . DIRECTORY_SEPARATOR . 'icons' . DIRECTORY_SEPARATOR . $card . '.gif', /** @scrutinizer ignore-type */ false );
Loading history...
538
						$content_dir = WP_CONTENT_DIR;
539
540
						if ( function_exists( 'wp_normalize_path' ) ) {
541
							// Replaces backslashes with forward slashes for Windows systems
542
							$image = wp_normalize_path( $image );
543
							$content_dir = wp_normalize_path( $content_dir );
544
						}
545
546
						$image = str_replace( $content_dir, content_url(), $image );
547
					}
548
549
					echo '<img class="payment-icon" src="' . esc_url( $image ) . '" style="width:32px;height:24px;position:relative;top:6px;margin-right:5px;"/>';
550
				}
551
			echo $option . '</label>';
552
		}
553
		echo '<p class="description" style="margin-top:16px;">' . wp_kses_post( $args['desc'] ) . '</p>';
554
	}
555
}
556
557
function wpinv_radio_callback( $args ) {
558
	global $wpinv_options;
559
    
560
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
561
    
562
    foreach ( $args['options'] as $key => $option ) :
563
		$sanitize_key = wpinv_sanitize_key( $key );
564
        
565
        $checked = false;
566
567
		if ( isset( $wpinv_options[ $args['id'] ] ) && $wpinv_options[ $args['id'] ] == $key )
568
			$checked = true;
569
		elseif( isset( $args['std'] ) && $args['std'] == $key && ! isset( $wpinv_options[ $args['id'] ] ) )
570
			$checked = true;
571
572
		echo '<input name="wpinv_settings[' . $sanitize_id . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="radio" value="' . $sanitize_key . '" ' . checked(true, $checked, false) . '/>&nbsp;';
573
		echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . esc_html( $option ) . '</label><br/>';
574
	endforeach;
575
576
	echo '<p class="description">' . wp_kses_post( $args['desc'] ) . '</p>';
577
}
578
579
function wpinv_gateways_callback( $args ) {
580
	global $wpinv_options;
581
    
582
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
583
584
	foreach ( $args['options'] as $key => $option ) :
585
		$sanitize_key = wpinv_sanitize_key( $key );
586
        
587
        if ( isset( $wpinv_options['gateways'][ $key ] ) )
588
			$enabled = '1';
589
		else
590
			$enabled = null;
591
592
		echo '<input name="wpinv_settings[' . esc_attr( $args['id'] ) . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="1" ' . checked('1', $enabled, false) . '/>&nbsp;';
593
		echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . esc_html( $option['admin_label'] ) . '</label><br/>';
594
	endforeach;
595
}
596
597
function wpinv_gateway_select_callback($args) {
598
	global $wpinv_options;
599
    
600
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
601
    $class = !empty( $args['class'] ) ? ' ' . esc_attr( $args['class'] ) : '';
602
603
	echo '<select name="wpinv_settings[' . $sanitize_id . ']"" id="wpinv_settings[' . $sanitize_id . ']" class="'.$class.'" >';
604
605
	foreach ( $args['options'] as $key => $option ) :
606
		if ( isset( $args['selected'] ) && $args['selected'] !== null && $args['selected'] !== false ) {
607
            $selected = selected( $key, $args['selected'], false );
608
        } else {
609
            $selected = isset( $wpinv_options[ $args['id'] ] ) ? selected( $key, $wpinv_options[$args['id']], false ) : '';
610
        }
611
		echo '<option value="' . wpinv_sanitize_key( $key ) . '"' . $selected . '>' . esc_html( $option['admin_label'] ) . '</option>';
612
	endforeach;
613
614
	echo '</select>';
615
	echo '<label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
616
}
617
618
function wpinv_text_callback( $args ) {
619
	global $wpinv_options;
620
    
621
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
622
623
	if ( isset( $wpinv_options[ $args['id'] ] ) ) {
624
		$value = $wpinv_options[ $args['id'] ];
625
	} else {
626
		$value = isset( $args['std'] ) ? $args['std'] : '';
627
	}
628
629
	if ( isset( $args['faux'] ) && true === $args['faux'] ) {
630
		$args['readonly'] = true;
631
		$value = isset( $args['std'] ) ? $args['std'] : '';
632
		$name  = '';
633
	} else {
634
		$name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"';
635
	}
636
	$class = !empty( $args['class'] ) ? sanitize_html_class( $args['class'] ) : '';
637
638
	$readonly = $args['readonly'] === true ? ' readonly="readonly"' : '';
639
	$size     = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular';
640
	$html     = '<input type="text" class="' . sanitize_html_class( $size ) . '-text ' . $class . '" id="wpinv_settings[' . $sanitize_id . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '"' . $readonly . '/>';
641
	$html    .= '<label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
642
643
	echo $html;
644
}
645
646
function wpinv_number_callback( $args ) {
647
	global $wpinv_options;
648
    
649
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
650
651
	if ( isset( $wpinv_options[ $args['id'] ] ) ) {
652
		$value = $wpinv_options[ $args['id'] ];
653
	} else {
654
		$value = isset( $args['std'] ) ? $args['std'] : '';
655
	}
656
657
	if ( isset( $args['faux'] ) && true === $args['faux'] ) {
658
		$args['readonly'] = true;
659
		$value = isset( $args['std'] ) ? $args['std'] : '';
660
		$name  = '';
661
	} else {
662
		$name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"';
663
	}
664
665
	$max  = isset( $args['max'] ) ? $args['max'] : 999999;
666
	$min  = isset( $args['min'] ) ? $args['min'] : 0;
667
	$step = isset( $args['step'] ) ? $args['step'] : 1;
668
	$class = !empty( $args['class'] ) ? sanitize_html_class( $args['class'] ) : '';
669
670
	$size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular';
671
	$html = '<input type="number" step="' . esc_attr( $step ) . '" max="' . esc_attr( $max ) . '" min="' . esc_attr( $min ) . '" class="' . sanitize_html_class( $size ) . '-text ' . $class . '" id="wpinv_settings[' . $sanitize_id . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '"/>';
672
	$html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
673
674
	echo $html;
675
}
676
677
function wpinv_textarea_callback( $args ) {
678
	global $wpinv_options;
679
    
680
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
681
682
	if ( isset( $wpinv_options[ $args['id'] ] ) ) {
683
		$value = $wpinv_options[ $args['id'] ];
684
	} else {
685
		$value = isset( $args['std'] ) ? $args['std'] : '';
686
	}
687
    
688
    $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular';
689
    $class = ( isset( $args['class'] ) && ! is_null( $args['class'] ) ) ? $args['class'] : 'large-text';
690
691
	$html = '<textarea class="' . sanitize_html_class( $class ) . ' txtarea-' . sanitize_html_class( $size ) . ' wpi-' . esc_attr( sanitize_html_class( $sanitize_id ) ) . ' " cols="' . $args['cols'] . '" rows="' . $args['rows'] . '" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']">' . esc_textarea( stripslashes( $value ) ) . '</textarea>';
692
	$html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
693
694
	echo $html;
695
}
696
697
function wpinv_password_callback( $args ) {
698
	global $wpinv_options;
699
    
700
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
701
702
	if ( isset( $wpinv_options[ $args['id'] ] ) ) {
703
		$value = $wpinv_options[ $args['id'] ];
704
	} else {
705
		$value = isset( $args['std'] ) ? $args['std'] : '';
706
	}
707
708
	$size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular';
709
	$html = '<input type="password" class="' . sanitize_html_class( $size ) . '-text" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( $value ) . '"/>';
710
	$html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>';
711
712
	echo $html;
713
}
714
715
function wpinv_missing_callback($args) {
716
	printf(
717
		__( 'The callback function used for the %s setting is missing.', 'invoicing' ),
718
		'<strong>' . $args['id'] . '</strong>'
719
	);
720
}
721
722
function wpinv_select_callback($args) {
723
	global $wpinv_options;
724
    
725
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
726
727
	if ( isset( $wpinv_options[ $args['id'] ] ) ) {
728
		$value = $wpinv_options[ $args['id'] ];
729
	} else {
730
		$value = isset( $args['std'] ) ? $args['std'] : '';
731
	}
732
    
733
    if ( isset( $args['selected'] ) && $args['selected'] !== null && $args['selected'] !== false ) {
734
        $value = $args['selected'];
735
    }
736
737
	if ( isset( $args['placeholder'] ) ) {
738
		$placeholder = $args['placeholder'];
739
	} else {
740
		$placeholder = '';
741
	}
742
    
743
    if( !empty( $args['onchange'] ) ) {
744
        $onchange = ' onchange="' . esc_attr( $args['onchange'] ) . '"';
745
    } else {
746
        $onchange = '';
747
    }
748
749
    $class = !empty( $args['class'] ) ? ' ' . esc_attr( $args['class'] ) : '';
750
751
	$html = '<select id="wpinv_settings[' . $sanitize_id . ']" class="'.$class.'"  name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" data-placeholder="' . esc_html( $placeholder ) . '"' . $onchange . ' />';
752
753
	foreach ( $args['options'] as $option => $name ) {
754
		$selected = selected( $option, $value, false );
755
		$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $name ) . '</option>';
756
	}
757
758
	$html .= '</select>';
759
	$html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>';
760
761
	echo $html;
762
}
763
764
function wpinv_color_select_callback( $args ) {
765
	global $wpinv_options;
766
    
767
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
768
769
	if ( isset( $wpinv_options[ $args['id'] ] ) ) {
770
		$value = $wpinv_options[ $args['id'] ];
771
	} else {
772
		$value = isset( $args['std'] ) ? $args['std'] : '';
773
	}
774
775
	$html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"/>';
776
777
	foreach ( $args['options'] as $option => $color ) {
778
		$selected = selected( $option, $value, false );
779
		$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $color['label'] ) . '</option>';
780
	}
781
782
	$html .= '</select>';
783
	$html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
784
785
	echo $html;
786
}
787
788
function wpinv_rich_editor_callback( $args ) {
789
	global $wpinv_options, $wp_version;
790
    
791
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
792
793
	if ( isset( $wpinv_options[ $args['id'] ] ) ) {
794
		$value = $wpinv_options[ $args['id'] ];
795
796
		if( empty( $args['allow_blank'] ) && empty( $value ) ) {
797
			$value = isset( $args['std'] ) ? $args['std'] : '';
798
		}
799
	} else {
800
		$value = isset( $args['std'] ) ? $args['std'] : '';
801
	}
802
803
	$rows = isset( $args['size'] ) ? $args['size'] : 20;
804
805
	if ( $wp_version >= 3.3 && function_exists( 'wp_editor' ) ) {
806
		ob_start();
807
		wp_editor( stripslashes( $value ), 'wpinv_settings_' . esc_attr( $args['id'] ), array( 'textarea_name' => 'wpinv_settings[' . esc_attr( $args['id'] ) . ']', 'textarea_rows' => absint( $rows ), 'media_buttons' => false ) );
808
		$html = ob_get_clean();
809
	} else {
810
		$html = '<textarea class="large-text" rows="10" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" class="wpi-' . esc_attr( sanitize_html_class( $args['id'] ) ) . '">' . esc_textarea( stripslashes( $value ) ) . '</textarea>';
811
	}
812
813
	$html .= '<br/><label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>';
814
815
	echo $html;
816
}
817
818
function wpinv_upload_callback( $args ) {
819
	global $wpinv_options;
820
    
821
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
822
823
	if ( isset( $wpinv_options[ $args['id'] ] ) ) {
824
		$value = $wpinv_options[$args['id']];
825
	} else {
826
		$value = isset($args['std']) ? $args['std'] : '';
827
	}
828
829
	$size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular';
830
	$html = '<input type="text" class="' . sanitize_html_class( $size ) . '-text" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( stripslashes( $value ) ) . '"/>';
831
	$html .= '<span>&nbsp;<input type="button" class="wpinv_settings_upload_button button-secondary" value="' . __( 'Upload File', 'invoicing' ) . '"/></span>';
832
	$html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>';
833
834
	echo $html;
835
}
836
837
function wpinv_color_callback( $args ) {
838
	global $wpinv_options;
839
    
840
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
841
842
	if ( isset( $wpinv_options[ $args['id'] ] ) ) {
843
		$value = $wpinv_options[ $args['id'] ];
844
	} else {
845
		$value = isset( $args['std'] ) ? $args['std'] : '';
846
	}
847
848
	$default = isset( $args['std'] ) ? $args['std'] : '';
849
850
	$html = '<input type="text" class="wpinv-color-picker" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( $value ) . '" data-default-color="' . esc_attr( $default ) . '" />';
851
	$html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
852
853
	echo $html;
854
}
855
856
function wpinv_country_states_callback($args) {
857
	global $wpinv_options;
858
    
859
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
860
861
	if ( isset( $args['placeholder'] ) ) {
862
		$placeholder = $args['placeholder'];
863
	} else {
864
		$placeholder = '';
865
	}
866
867
	$states = wpinv_get_country_states();
868
869
	$class = empty( $states ) ? ' class="wpinv-no-states"' : ' class="wpi_select2"';
870
	$html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"' . $class . 'data-placeholder="' . esc_html( $placeholder ) . '"/>';
871
872
	foreach ( $states as $option => $name ) {
873
		$selected = isset( $wpinv_options[ $args['id'] ] ) ? selected( $option, $wpinv_options[$args['id']], false ) : '';
874
		$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $name ) . '</option>';
875
	}
876
877
	$html .= '</select>';
878
	$html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
879
880
	echo $html;
881
}
882
883
function wpinv_tax_rates_callback($args) {
884
	global $wpinv_options;
885
	$rates = wpinv_get_tax_rates();
886
	ob_start(); ?>
887
    </td><tr>
888
    <td colspan="2" class="wpinv_tax_tdbox">
889
	<p><?php echo $args['desc']; ?></p>
890
	<table id="wpinv_tax_rates" class="wp-list-table widefat fixed posts">
891
		<thead>
892
			<tr>
893
				<th scope="col" class="wpinv_tax_country"><?php _e( 'Country', 'invoicing' ); ?></th>
894
				<th scope="col" class="wpinv_tax_state"><?php _e( 'State / Province', 'invoicing' ); ?></th>
895
                <th scope="col" class="wpinv_tax_global" title="<?php esc_attr_e( 'Apply rate to whole country, regardless of state / province', 'invoicing' ); ?>"><?php _e( 'Country Wide', 'invoicing' ); ?></th>
896
                <th scope="col" class="wpinv_tax_rate"><?php _e( 'Rate %', 'invoicing' ); ?></th> 
897
                <th scope="col" class="wpinv_tax_name"><?php _e( 'Tax Name', 'invoicing' ); ?></th>
898
				<th scope="col" class="wpinv_tax_action"><?php _e( 'Remove', 'invoicing' ); ?></th>
899
			</tr>
900
		</thead>
901
        <tbody>
902
		<?php if( !empty( $rates ) ) : ?>
903
			<?php foreach( $rates as $key => $rate ) : ?>
904
            <?php 
905
            $sanitized_key = wpinv_sanitize_key( $key );
906
            ?>
907
			<tr>
908
				<td class="wpinv_tax_country">
909
					<?php
910
					echo wpinv_html_select( array(
911
						'options'          => wpinv_get_country_list( true ),
912
						'name'             => 'tax_rates[' . $sanitized_key . '][country]',
913
                        'id'               => 'tax_rates[' . $sanitized_key . '][country]',
914
						'selected'         => $rate['country'],
915
						'show_option_all'  => false,
916
						'show_option_none' => false,
917
						'class'            => 'wpinv-tax-country wpi_select2',
918
						'placeholder'      => __( 'Choose a country', 'invoicing' )
919
					) );
920
					?>
921
				</td>
922
				<td class="wpinv_tax_state">
923
					<?php
924
					$states = wpinv_get_country_states( $rate['country'] );
925
					if( !empty( $states ) ) {
926
						echo wpinv_html_select( array(
927
							'options'          => array_merge( array( '' => '' ), $states ),
928
							'name'             => 'tax_rates[' . $sanitized_key . '][state]',
929
                            'id'               => 'tax_rates[' . $sanitized_key . '][state]',
930
							'selected'         => $rate['state'],
931
							'show_option_all'  => false,
932
							'show_option_none' => false,
933
                            'class'            => 'wpi_select2',
934
							'placeholder'      => __( 'Choose a state', 'invoicing' )
935
						) );
936
					} else {
937
						echo wpinv_html_text( array(
938
							'name'  => 'tax_rates[' . $sanitized_key . '][state]', $rate['state'],
939
							'value' => ! empty( $rate['state'] ) ? $rate['state'] : '',
940
                            'id'    => 'tax_rates[' . $sanitized_key . '][state]',
941
						) );
942
					}
943
					?>
944
				</td>
945
				<td class="wpinv_tax_global">
946
					<input type="checkbox" name="tax_rates[<?php echo $sanitized_key; ?>][global]" id="tax_rates[<?php echo $sanitized_key; ?>][global]" value="1"<?php checked( true, ! empty( $rate['global'] ) ); ?>/>
947
					<label for="tax_rates[<?php echo $sanitized_key; ?>][global]"><?php _e( 'Apply to whole country', 'invoicing' ); ?></label>
948
				</td>
949
				<td class="wpinv_tax_rate"><input type="number" class="small-text" step="any" min="0" max="99" name="tax_rates[<?php echo $sanitized_key; ?>][rate]" value="<?php echo esc_html( $rate['rate'] ); ?>"/></td>
950
                <td class="wpinv_tax_name"><input type="text" class="regular-text" name="tax_rates[<?php echo $sanitized_key; ?>][name]" value="<?php echo esc_html( $rate['name'] ); ?>"/></td>
951
				<td class="wpinv_tax_action"><span class="wpinv_remove_tax_rate button-secondary"><?php _e( 'Remove Rate', 'invoicing' ); ?></span></td>
952
			</tr>
953
			<?php endforeach; ?>
954
		<?php else : ?>
955
			<tr>
956
				<td class="wpinv_tax_country">
957
					<?php
958
					echo wpinv_html_select( array(
959
						'options'          => wpinv_get_country_list( true ),
960
						'name'             => 'tax_rates[0][country]',
961
						'show_option_all'  => false,
962
						'show_option_none' => false,
963
						'class'            => 'wpinv-tax-country wpi_select2',
964
						'placeholder'      => __( 'Choose a country', 'invoicing' )
965
					) ); ?>
966
				</td>
967
				<td class="wpinv_tax_state">
968
					<?php echo wpinv_html_text( array(
969
						'name' => 'tax_rates[0][state]'
970
					) ); ?>
971
				</td>
972
				<td class="wpinv_tax_global">
973
					<input type="checkbox" name="tax_rates[0][global]" id="tax_rates[0][global]" value="1"/>
974
					<label for="tax_rates[0][global]"><?php _e( 'Apply to whole country', 'invoicing' ); ?></label>
975
				</td>
976
				<td class="wpinv_tax_rate"><input type="number" class="small-text" step="any" min="0" max="99" name="tax_rates[0][rate]" placeholder="<?php echo (float)wpinv_get_option( 'tax_rate', 0 ) ;?>" value="<?php echo (float)wpinv_get_option( 'tax_rate', 0 ) ;?>"/></td>
977
                <td class="wpinv_tax_name"><input type="text" class="regular-text" name="tax_rates[0][name]" /></td>
978
				<td><span class="wpinv_remove_tax_rate button-secondary"><?php _e( 'Remove Rate', 'invoicing' ); ?></span></td>
979
			</tr>
980
		<?php endif; ?>
981
        </tbody>
982
        <tfoot><tr><td colspan="5"></td><td class="wpinv_tax_action"><span class="button-secondary" id="wpinv_add_tax_rate"><?php _e( 'Add Tax Rate', 'invoicing' ); ?></span></td></tr></tfoot>
983
	</table>
984
	<?php
985
	echo ob_get_clean();
986
}
987
988
function wpinv_tools_callback($args) {
989
    global $wpinv_options;
990
    ob_start(); ?>
991
    </td><tr>
992
    <td colspan="2" class="wpinv_tools_tdbox">
993
    <?php if ( $args['desc'] ) { ?><p><?php echo $args['desc']; ?></p><?php } ?>
994
    <?php do_action( 'wpinv_tools_before' ); ?>
995
    <table id="wpinv_tools_table" class="wp-list-table widefat fixed posts">
996
        <thead>
997
            <tr>
998
                <th scope="col" class="wpinv-th-tool"><?php _e( 'Tool', 'invoicing' ); ?></th>
999
                <th scope="col" class="wpinv-th-desc"><?php _e( 'Description', 'invoicing' ); ?></th>
1000
                <th scope="col" class="wpinv-th-action"><?php _e( 'Action', 'invoicing' ); ?></th>
1001
            </tr>
1002
        </thead>
1003
            <?php do_action( 'wpinv_tools_row' ); ?>
1004
        <tbody>
1005
        </tbody>
1006
    </table>
1007
    <?php do_action( 'wpinv_tools_after' ); ?>
1008
    <?php
1009
    echo ob_get_clean();
1010
}
1011
1012
function wpinv_descriptive_text_callback( $args ) {
1013
	echo wp_kses_post( $args['desc'] );
1014
}
1015
1016
function wpinv_hook_callback( $args ) {
1017
	do_action( 'wpinv_' . $args['id'], $args );
1018
}
1019
1020
function wpinv_set_settings_cap() {
1021
	return wpinv_get_capability();
1022
}
1023
add_filter( 'option_page_capability_wpinv_settings', 'wpinv_set_settings_cap' );
1024
1025
function wpinv_settings_sanitize_input( $value, $key ) {
1026
    if ( $key == 'tax_rate' || $key == 'eu_fallback_rate' ) {
1027
        $value = wpinv_sanitize_amount( $value, 4 );
1028
        $value = $value >= 100 ? 99 : $value;
1029
    }
1030
        
1031
    return $value;
1032
}
1033
add_filter( 'wpinv_settings_sanitize', 'wpinv_settings_sanitize_input', 10, 2 );
1034
1035
function wpinv_on_update_settings( $old_value, $value, $option ) {
1036
    $old = !empty( $old_value['remove_data_on_unistall'] ) ? 1 : '';
1037
    $new = !empty( $value['remove_data_on_unistall'] ) ? 1 : '';
1038
    
1039
    if ( $old != $new ) {
1040
        update_option( 'wpinv_remove_data_on_invoice_unistall', $new );
1041
    }
1042
}
1043
add_action( 'update_option_wpinv_settings', 'wpinv_on_update_settings', 10, 3 );
1044
add_action( 'wpinv_settings_tab_bottom_emails_new_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 );
1045
add_action( 'wpinv_settings_tab_bottom_emails_cancelled_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 );
1046
add_action( 'wpinv_settings_tab_bottom_emails_failed_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 );
1047
add_action( 'wpinv_settings_tab_bottom_emails_onhold_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 );
1048
add_action( 'wpinv_settings_tab_bottom_emails_processing_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 );
1049
add_action( 'wpinv_settings_tab_bottom_emails_completed_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 );
1050
add_action( 'wpinv_settings_tab_bottom_emails_refunded_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 );
1051
add_action( 'wpinv_settings_tab_bottom_emails_user_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 );
1052
add_action( 'wpinv_settings_tab_bottom_emails_user_note', 'wpinv_settings_tab_bottom_emails', 10, 2 );
1053
add_action( 'wpinv_settings_tab_bottom_emails_overdue', 'wpinv_settings_tab_bottom_emails', 10, 2 );
1054
1055
function wpinv_settings_tab_bottom_emails( $active_tab, $section ) {
1056
    ?>
1057
    <div class="wpinv-email-wc-row ">
1058
        <div class="wpinv-email-wc-td">
1059
            <h3 class="wpinv-email-wc-title"><?php echo apply_filters( 'wpinv_settings_email_wildcards_title', __( 'Wildcards For Emails', 'invoicing' ) ); ?></h3>
1060
            <p class="wpinv-email-wc-description">
1061
                <?php
1062
                $description = __( 'The following wildcards can be used in email subjects, heading and content:<br>
1063
                    <strong>{site_title} :</strong> Site Title<br>
1064
                    <strong>{name} :</strong> Customer\'s full name<br>
1065
                    <strong>{first_name} :</strong> Customer\'s first name<br>
1066
                    <strong>{last_name} :</strong> Customer\'s last name<br>
1067
                    <strong>{email} :</strong> Customer\'s email address<br>
1068
                    <strong>{invoice_number} :</strong> The invoice number<br>
1069
                    <strong>{invoice_total} :</strong> The invoice total<br>
1070
                    <strong>{invoice_link} :</strong> The invoice link<br>
1071
                    <strong>{invoice_pay_link} :</strong> The payment link<br>
1072
                    <strong>{invoice_date} :</strong> The date the invoice was created<br>
1073
                    <strong>{invoice_due_date} :</strong> The date the invoice is due<br>
1074
                    <strong>{date} :</strong> Today\'s date.<br>
1075
                    <strong>{is_was} :</strong> If due date of invoice is past, displays "was" otherwise displays "is"<br>
1076
                    <strong>{invoice_label} :</strong> Invoices/quotes singular name. Ex: Invoice/Quote<br>', 'invoicing' );
1077
                echo apply_filters('wpinv_settings_email_wildcards_description', $description, $active_tab, $section);
1078
                ?>
1079
            </p>
1080
        </div>
1081
    </div>
1082
    <?php
1083
}