Passed
Push — master ( d100e4...32435e )
by Brian
11:14
created

getpaid_get_integration_settings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
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 mixed
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 );
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
	if ( isset( $option['desc'] ) && ! empty( $option['help-tip'] ) ) {
200
		$tip   = esc_attr( $option['desc'] );
201
		$name .= "<span class='dashicons dashicons-editor-help wpi-help-tip' title='$tip'></span>";
202
		unset( $option['desc'] );
203
	}
204
205
    // Loop through all tabs.
206
    add_settings_field(
207
        'wpinv_settings[' . $option['id'] . ']',
208
        $name,
209
        function_exists( $cb ) ? $cb : 'wpinv_missing_callback',
210
        $section,
211
        $section,
212
        array(
213
            'section'     => $section,
214
            'id'          => isset( $option['id'] )          ? $option['id']          : null,
215
            'desc'        => isset( $option['desc'] )        ? $option['desc']        : '',
216
            'name'        => $name,
217
            'size'        => isset( $option['size'] )        ? $option['size']        : null,
218
            'options'     => isset( $option['options'] )     ? $option['options']     : '',
219
            'selected'    => isset( $option['selected'] )    ? $option['selected']    : null,
220
            'std'         => isset( $option['std'] )         ? $option['std']         : '',
221
            'min'         => isset( $option['min'] )         ? $option['min']         : null,
222
            'max'         => isset( $option['max'] )         ? $option['max']         : null,
223
            'step'        => isset( $option['step'] )        ? $option['step']        : null,
224
            'placeholder' => isset( $option['placeholder'] ) ? $option['placeholder'] : null,
225
            'allow_blank' => isset( $option['allow_blank'] ) ? $option['allow_blank'] : true,
226
            'readonly'    => isset( $option['readonly'] )    ? $option['readonly']    : false,
227
            'faux'        => isset( $option['faux'] )        ? $option['faux']        : false,
228
            'onchange'    => isset( $option['onchange'] )   ? $option['onchange']     : '',
229
            'custom'      => isset( $option['custom'] )     ? $option['custom']       : '',
230
            'class'       => isset( $option['class'] )     ? $option['class']         : '',
231
            'cols'        => isset( $option['cols'] ) && (int) $option['cols'] > 0 ? (int) $option['cols'] : 50,
232
            'rows'        => isset( $option['rows'] ) && (int) $option['rows'] > 0 ? (int) $option['rows'] : 5,
233
        )
234
    );
235
236
}
237
238
/**
239
 * Returns an array of all registered settings.
240
 * 
241
 * @return array
242
 */
243
function wpinv_get_registered_settings() {
244
	return array_filter( apply_filters( 'wpinv_registered_settings', wpinv_get_data( 'admin-settings' ) ) );
245
}
246
247
/**
248
 * Returns an array of all integration settings.
249
 * 
250
 * @return array
251
 */
252
function getpaid_get_integration_settings() {
253
    return apply_filters( 'getpaid_integration_settings', array() );
254
}
255
256
/**
257
 * Sanitizes settings before they are saved.
258
 * 
259
 * @return array
260
 */
261
function wpinv_settings_sanitize( $input = array() ) {
262
263
    $wpinv_options = wpinv_get_options();
264
265
    if ( empty( wp_get_raw_referer() ) ) {
266
        return $input;
267
    }
268
269
    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

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

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