Passed
Push — master ( 5fd7f4...305ace )
by Brian
07:46
created

wpinv_settings_sanitize_input()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

546
						$image       = wpinv_locate_template( 'images' . DIRECTORY_SEPARATOR . 'icons' . DIRECTORY_SEPARATOR . $card . '.gif', /** @scrutinizer ignore-type */ false );
Loading history...
547
						$content_dir = WP_CONTENT_DIR;
548
549
						if ( function_exists( 'wp_normalize_path' ) ) {
550
							// Replaces backslashes with forward slashes for Windows systems
551
							$image = wp_normalize_path( $image );
552
							$content_dir = wp_normalize_path( $content_dir );
553
						}
554
555
						$image = str_replace( $content_dir, content_url(), $image );
556
					}
557
558
					echo '<img class="payment-icon" src="' . esc_url( $image ) . '" style="width:32px;height:24px;position:relative;top:6px;margin-right:5px;"/>';
559
				}
560
			echo $option . '</label>';
561
		}
562
		echo '<p class="description" style="margin-top:16px;">' . wp_kses_post( $args['desc'] ) . '</p>';
563
	}
564
}
565
566
/**
567
 * Displays a radio settings field.
568
 */
569
function wpinv_radio_callback( $args ) {
570
571
	$std = isset( $args['std'] ) ? $args['std'] : '';
572
	$std = wpinv_get_option( $args['id'], $std );
573
	?>
574
		<fieldset>
575
			<ul id="wpinv-settings-<?php echo esc_attr( $args['id'] ); ?>" style="margin-top: 0;">
576
				<?php foreach( $args['options'] as $key => $option ) : ?>
577
					<li>
578
						<label>
579
							<input name="wpinv_settings[<?php echo esc_attr( $args['id'] ); ?>]" <?php checked( $std, $key ); ?> value="<?php echo esc_attr( $key ); ?>" type="radio">
580
							<?php echo wp_kses_post( $option ); ?>
581
						</label>
582
					</li>
583
				<?php endforeach; ?>
584
			</ul>
585
		</fieldset>
586
	<?php
587
	getpaid_settings_description_callback( $args );
588
}
589
590
/**
591
 * Displays a description if available.
592
 */
593
function getpaid_settings_description_callback( $args ) {
594
595
	if ( ! empty( $args['desc'] ) ) {
596
		$description = wp_kses_post( $args['desc'] );
597
		echo "<p class='description'>$description</p>";
598
	}
599
600
}
601
602
/**
603
 * Displays a list of available gateways.
604
 */
605
function wpinv_gateways_callback() {
606
607
	?>
608
		</td>
609
	</tr>
610
	<tr class="bsui">
611
    	<td colspan="2" class="p-0">
612
			<?php include plugin_dir_path( __FILE__ ) . 'views/html-gateways-edit.php'; ?>
613
614
	<?php
615
}
616
617
function wpinv_gateway_select_callback($args) {
618
    
619
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
620
    $class = !empty( $args['class'] ) ? ' ' . esc_attr( $args['class'] ) : '';
621
	$std     = isset( $args['std'] ) ? $args['std'] : '';
622
	$value   = wpinv_get_option( $args['id'], $std );
623
624
	echo '<select name="wpinv_settings[' . $sanitize_id . ']"" id="wpinv_settings[' . $sanitize_id . ']" class="'.$class.'" >';
625
626
	foreach ( $args['options'] as $key => $option ) :
627
		if ( isset( $args['selected'] ) && $args['selected'] !== null && $args['selected'] !== false ) {
628
            $selected = selected( $key, $args['selected'], false );
629
        } else {
630
            $selected = selected( $key, $value, false );
631
        }
632
		echo '<option value="' . wpinv_sanitize_key( $key ) . '"' . $selected . '>' . esc_html( $option['admin_label'] ) . '</option>';
633
	endforeach;
634
635
	echo '</select>';
636
	echo '<label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
637
}
638
639
/**
640
 * Generates attributes.
641
 * 
642
 * @param array $args
643
 * @return string
644
 */
645
function wpinv_settings_attrs_helper( $args ) {
646
647
	$value        = isset( $args['std'] ) ? $args['std'] : '';
648
	$id           = esc_attr( $args['id'] );
649
	$placeholder  = esc_attr( $args['placeholder'] );
650
651
	if ( ! empty( $args['faux'] ) ) {
652
		$args['readonly'] = true;
653
		$name             = '';
654
	} else {
655
		$value  = wpinv_get_option( $args['id'], $value );
656
		$name   = "wpinv_settings[$id]";
657
	}
658
659
	$value    = is_scalar( $value ) ? esc_attr( $value ) : '';
660
	$class    = esc_attr( $args['class'] );
661
	$style    = esc_attr( $args['style'] );
662
	$readonly = empty( $args['readonly'] ) ? '' : 'readonly onclick="this.select()"';
663
664
	$onchange = '';
665
    if ( ! empty( $args['onchange'] ) ) {
666
        $onchange = ' onchange="' . esc_attr( $args['onchange'] ) . '"';
667
	}
668
669
	return "name='$name' id='wpinv-settings-$id' style='$style' value='$value' class='$class' placeholder='$placeholder' data-placeholder='$placeholder' $onchange $readonly";
670
}
671
672
/**
673
 * Displays a text input settings callback.
674
 */
675
function wpinv_text_callback( $args ) {
676
677
	$desc = wp_kses_post( $args['desc'] );
678
	$desc = empty( $desc ) ? '' : "<p class='description'>$desc</p>";
679
	$attr = wpinv_settings_attrs_helper( $args );
680
681
	?>
682
		<label style="width: 100%;">
683
			<input type="text" <?php echo $attr; ?>>
684
			<?php echo $desc; ?>
685
		</label>
686
	<?php
687
688
}
689
690
/**
691
 * Displays a number input settings callback.
692
 */
693
function wpinv_number_callback( $args ) {
694
695
	$desc = wp_kses_post( $args['desc'] );
696
	$desc = empty( $desc ) ? '' : "<p class='description'>$desc</p>";
697
	$attr = wpinv_settings_attrs_helper( $args );
698
	$max  = intval( $args['max'] );
699
	$min  = intval( $args['min'] );
700
	$step = floatval( $args['step'] );
701
702
	?>
703
		<label style="width: 100%;">
704
			<input type="number" step="<?php echo $step; ?>" max="<?php echo $max; ?>" min="<?php echo $min; ?>" <?php echo $attr; ?>>
705
			<?php echo $desc; ?>
706
		</label>
707
	<?php
708
709
}
710
711
function wpinv_textarea_callback( $args ) {
712
    
713
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
714
	$std     = isset( $args['std'] ) ? $args['std'] : '';
715
	$value   = wpinv_get_option( $args['id'], $std );
716
717
    $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular';
718
    $class = ( isset( $args['class'] ) && ! is_null( $args['class'] ) ) ? $args['class'] : 'large-text';
719
720
	$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>';
721
	$html .= '<br /><label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
722
723
	echo $html;
724
}
725
726
function wpinv_password_callback( $args ) {
727
    
728
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
729
	$std     = isset( $args['std'] ) ? $args['std'] : '';
730
	$value   = wpinv_get_option( $args['id'], $std );
731
732
	$size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular';
733
	$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 ) . '"/>';
734
	$html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>';
735
736
	echo $html;
737
}
738
739
function wpinv_missing_callback($args) {
740
	printf(
741
		__( 'The callback function used for the %s setting is missing.', 'invoicing' ),
742
		'<strong>' . $args['id'] . '</strong>'
743
	);
744
}
745
746
/**
747
 * Displays a number input settings callback.
748
 */
749
function wpinv_select_callback( $args ) {
750
751
	$desc   = wp_kses_post( $args['desc'] );
752
	$desc   = empty( $desc ) ? '' : "<p class='description'>$desc</p>";
753
	$attr   = wpinv_settings_attrs_helper( $args );
754
	$value  = isset( $args['std'] ) ? $args['std'] : '';
755
	$value  = wpinv_get_option( $args['id'], $value );
756
	$rand   = uniqid( 'random_id' );
757
758
	?>
759
		<label style="width: 100%;">
760
			<select <?php echo $attr; ?>>
761
				<?php foreach ( $args['options'] as $option => $name ) : ?>
762
					<option value="<?php echo esc_attr( $option ); ?>" <?php echo selected( $option, $value ); ?>><?php echo wpinv_clean( $name ); ?></option>
0 ignored issues
show
Bug introduced by
Are you sure wpinv_clean($name) of type array|string can be used in echo? ( Ignorable by Annotation )

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

762
					<option value="<?php echo esc_attr( $option ); ?>" <?php echo selected( $option, $value ); ?>><?php echo /** @scrutinizer ignore-type */ wpinv_clean( $name ); ?></option>
Loading history...
763
				<?php endforeach;?>
764
			</select>
765
766
			<?php if ( substr( $args['id'], -5 ) === '_page' && is_numeric( $value ) ) : ?>
767
				<a href="<?php echo get_edit_post_link( $value ); ?>" target="_blank" class="button getpaid-page-setting-edit"><?php _e( 'Edit Page', 'invoicing' ) ?></a>
0 ignored issues
show
Bug introduced by
$value of type string is incompatible with the type WP_Post|integer expected by parameter $id of get_edit_post_link(). ( Ignorable by Annotation )

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

767
				<a href="<?php echo get_edit_post_link( /** @scrutinizer ignore-type */ $value ); ?>" target="_blank" class="button getpaid-page-setting-edit"><?php _e( 'Edit Page', 'invoicing' ) ?></a>
Loading history...
768
			<?php endif; ?>
769
770
			<?php if ( substr( $args['id'], -5 ) === '_page' && ! empty( $args['default_content'] ) ) : ?>
771
				&nbsp;<a href="#TB_inline?&width=400&height=550&inlineId=<?php echo $rand; ?>" class="button thickbox getpaid-page-setting-view-default"><?php _e( 'View Default Content', 'invoicing' ) ?></a>
772
				<div id='<?php echo $rand; ?>' style='display:none;'>
773
					<div>
774
						<h3><?php _e( 'Original Content', 'invoicing' ) ?></h3>
775
						<textarea readonly onclick="this.select()" rows="8" style="width: 100%;"><?php echo gepaid_trim_lines( wp_kses_post( $args['default_content'] ) ); ?></textarea>
776
						<h3><?php _e( 'Current Content', 'invoicing' ) ?></h3>
777
						<textarea readonly onclick="this.select()" rows="8" style="width: 100%;"><?php $_post = get_post( $value ); echo empty( $_post ) ? '' : gepaid_trim_lines( wp_kses_post( $_post->post_content ) ); ?></textarea>
778
					</div>
779
				</div>
780
			<?php endif; ?>
781
782
			<?php echo $desc; ?>
783
		</label>
784
	<?php
785
786
}
787
788
function wpinv_color_select_callback( $args ) {
789
    
790
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
791
	$std     = isset( $args['std'] ) ? $args['std'] : '';
792
	$value   = wpinv_get_option( $args['id'], $std );
793
794
	$html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"/>';
795
796
	foreach ( $args['options'] as $option => $color ) {
797
		$selected = selected( $option, $value, false );
798
		$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $color['label'] ) . '</option>';
799
	}
800
801
	$html .= '</select>';
802
	$html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
803
804
	echo $html;
805
}
806
807
function wpinv_rich_editor_callback( $args ) {
808
	global $wp_version;
809
    
810
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
811
812
	$std     = isset( $args['std'] ) ? $args['std'] : '';
813
	$value   = wpinv_get_option( $args['id'], $std );
814
	
815
	if ( ! empty( $args['allow_blank'] ) && empty( $value ) ) {
816
		$value = $std;
817
	}
818
819
	$rows = isset( $args['size'] ) ? $args['size'] : 20;
820
821
	$html = '<div class="getpaid-settings-editor-input">';
822
	if ( $wp_version >= 3.3 && function_exists( 'wp_editor' ) ) {
823
		ob_start();
824
		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 ) );
825
		$html .= ob_get_clean();
826
	} else {
827
		$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>';
828
	}
829
830
	$html .= '</div><br/><label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>';
831
832
	echo $html;
833
}
834
835
function wpinv_upload_callback( $args ) {
836
    
837
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
838
839
	$std     = isset( $args['std'] ) ? $args['std'] : '';
840
	$value   = wpinv_get_option( $args['id'], $std );
841
842
	$size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular';
843
	$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 ) ) . '"/>';
844
	$html .= '<span>&nbsp;<input type="button" class="wpinv_settings_upload_button button-secondary" value="' . __( 'Upload File', 'invoicing' ) . '"/></span>';
845
	$html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>';
846
847
	echo $html;
848
}
849
850
function wpinv_color_callback( $args ) {
851
852
	$std         = isset( $args['std'] ) ? $args['std'] : '';
853
	$value       = wpinv_get_option( $args['id'], $std );
854
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
855
856
	$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( $std ) . '" />';
857
	$html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
858
859
	echo $html;
860
}
861
862
function wpinv_country_states_callback($args) {
863
864
	$std     = isset( $args['std'] ) ? $args['std'] : '';
865
	$value   = wpinv_get_option( $args['id'], $std );
866
867
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
868
869
	if ( isset( $args['placeholder'] ) ) {
870
		$placeholder = $args['placeholder'];
871
	} else {
872
		$placeholder = '';
873
	}
874
875
	$states = wpinv_get_country_states();
876
877
	$class = empty( $states ) ? ' class="wpinv-no-states"' : ' class="wpi_select2"';
878
	$html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"' . $class . 'data-placeholder="' . esc_html( $placeholder ) . '"/>';
879
880
	foreach ( $states as $option => $name ) {
881
		$selected = selected( $option, $value, false );
882
		$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $name ) . '</option>';
883
	}
884
885
	$html .= '</select>';
886
	$html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
887
888
	echo $html;
889
}
890
891
/**
892
 * Displays the tax rates edit table.
893
 */
894
function wpinv_tax_rates_callback() {
895
	
896
	?>
897
		</td>
898
	</tr>
899
	<tr class="bsui">
900
    	<td colspan="2" class="p-0">
901
			<?php include plugin_dir_path( __FILE__ ) . 'views/html-tax-rates-edit.php'; ?>
902
903
	<?php
904
905
}
906
907
/**
908
 * Displays a tax rate' edit row.
909
 */
910
function wpinv_tax_rate_callback( $tax_rate, $key, $echo = true ) {
911
	ob_start();
912
913
	$key                      = sanitize_key( $key );
914
	$tax_rate['reduced_rate'] = empty( $tax_rate['reduced_rate'] ) ? 0 : $tax_rate['reduced_rate'];
915
	include plugin_dir_path( __FILE__ ) . 'views/html-tax-rate-edit.php';
916
917
	if ( $echo ) {
918
		echo ob_get_clean();
919
	} else {
920
		return ob_get_clean(); 
921
	}
922
923
}
924
925
926
function wpinv_tools_callback($args) {
927
    ob_start(); ?>
928
    </td><tr>
929
    <td colspan="2" class="wpinv_tools_tdbox">
930
    <?php if ( $args['desc'] ) { ?><p><?php echo $args['desc']; ?></p><?php } ?>
931
    <?php do_action( 'wpinv_tools_before' ); ?>
932
    <table id="wpinv_tools_table" class="wp-list-table widefat fixed posts">
933
        <thead>
934
            <tr>
935
                <th scope="col" class="wpinv-th-tool"><?php _e( 'Tool', 'invoicing' ); ?></th>
936
                <th scope="col" class="wpinv-th-desc"><?php _e( 'Description', 'invoicing' ); ?></th>
937
                <th scope="col" class="wpinv-th-action"><?php _e( 'Action', 'invoicing' ); ?></th>
938
            </tr>
939
        </thead>
940
941
        <tbody>
942
			<tr>
943
                <td><?php _e( 'Check Pages', 'invoicing' );?></td>
944
                <td>
945
                    <small><?php _e( 'Creates any missing GetPaid pages.', 'invoicing' ); ?></small>
946
                </td>
947
                <td>
948
					<a href="<?php
949
						echo esc_url(
950
							wp_nonce_url(
951
								add_query_arg( 'getpaid-admin-action', 'create_missing_pages' ),
952
								'getpaid-nonce',
953
								'getpaid-nonce'
954
							)
955
						);
956
					?>" class="button button-primary"><?php _e('Run', 'invoicing');?></a>
957
                </td>
958
            </tr>
959
			<tr>
960
                <td><?php _e( 'Create Database Tables', 'invoicing' );?></td>
961
                <td>
962
                    <small><?php _e( 'Run this tool to create any missing database tables.', 'invoicing' ); ?></small>
963
                </td>
964
                <td>
965
					<a href="<?php
966
						echo esc_url(
967
							wp_nonce_url(
968
								add_query_arg( 'getpaid-admin-action', 'create_missing_tables' ),
969
								'getpaid-nonce',
970
								'getpaid-nonce'
971
							)
972
						);
973
					?>" class="button button-primary"><?php _e('Run', 'invoicing');?></a>
974
                </td>
975
            </tr>
976
			<tr>
977
                <td><?php _e( 'Migrate old invoices', 'invoicing' );?></td>
978
                <td>
979
                    <small><?php _e( 'If your old invoices were not migrated after updating from Invoicing to GetPaid, you can use this tool to migrate them.', 'invoicing' ); ?></small>
980
                </td>
981
                <td>
982
					<a href="<?php
983
						echo esc_url(
984
							wp_nonce_url(
985
								add_query_arg( 'getpaid-admin-action', 'migrate_old_invoices' ),
986
								'getpaid-nonce',
987
								'getpaid-nonce'
988
							)
989
						);
990
					?>" class="button button-primary"><?php _e('Run', 'invoicing');?></a>
991
                </td>
992
            </tr>
993
994
			<tr>
995
                <td><?php _e( 'Recalculate Discounts', 'invoicing' );?></td>
996
                <td>
997
                    <small><?php _e( 'Recalculate discounts for existing invoices that have discount codes but are not discounted.', 'invoicing' ); ?></small>
998
                </td>
999
                <td>
1000
					<a href="<?php
1001
						echo esc_url(
1002
							wp_nonce_url(
1003
								add_query_arg( 'getpaid-admin-action', 'recalculate_discounts' ),
1004
								'getpaid-nonce',
1005
								'getpaid-nonce'
1006
							)
1007
						);
1008
					?>" class="button button-primary"><?php _e( 'Run', 'invoicing' );?></a>
1009
                </td>
1010
            </tr>
1011
1012
			<?php do_action( 'wpinv_tools_row' ); ?>
1013
        </tbody>
1014
    </table>
1015
    <?php do_action( 'wpinv_tools_after' ); ?>
1016
    <?php
1017
    echo ob_get_clean();
1018
}
1019
1020
1021
function wpinv_descriptive_text_callback( $args ) {
1022
	echo wp_kses_post( $args['desc'] );
1023
}
1024
1025
function wpinv_raw_html_callback( $args ) {
1026
	echo $args['desc'];
1027
}
1028
1029
function wpinv_hook_callback( $args ) {
1030
	do_action( 'wpinv_' . $args['id'], $args );
1031
}
1032
1033
function wpinv_set_settings_cap() {
1034
	return wpinv_get_capability();
1035
}
1036
add_filter( 'option_page_capability_wpinv_settings', 'wpinv_set_settings_cap' );
1037
1038
1039
function wpinv_on_update_settings( $old_value, $value, $option ) {
1040
    $old = !empty( $old_value['remove_data_on_unistall'] ) ? 1 : '';
1041
    $new = !empty( $value['remove_data_on_unistall'] ) ? 1 : '';
1042
    
1043
    if ( $old != $new ) {
1044
        update_option( 'wpinv_remove_data_on_invoice_unistall', $new );
1045
    }
1046
}
1047
add_action( 'update_option_wpinv_settings', 'wpinv_on_update_settings', 10, 3 );
1048
1049
/**
1050
 * Returns the merge tags help text.
1051
 *
1052
 * @since    2.1.8
1053
 * 
1054
 * @return string
1055
 */
1056
function wpinv_get_merge_tags_help_text( $subscription = false ) {
1057
1058
	$url  = $subscription ? 'https://gist.github.com/picocodes/3d213982d57c34edf7a46fd3f0e8583e' : 'https://gist.github.com/picocodes/43bdc4d4bbba844534b2722e2af0b58f';
1059
	$link = sprintf(
1060
		'<strong><a href="%s" target="_blank">%s</a></strong>',
1061
		$url,
1062
		esc_html__( 'View available merge tags.', 'wpinv-quotes' )
1063
	);
1064
1065
	$description = esc_html__( 'The content of the email (Merge Tags and HTML are allowed).', 'invoicing' );
1066
1067
	return "$description $link";
1068
1069
}
1070