Passed
Push — master ( dea42c...d59b87 )
by Brian
11:32
created

wpinv_tax_rate_callback()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 11
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 apply_filters( 'wpinv_registered_settings', wpinv_get_data( 'admin-settings' ) );
245
}
246
247
/**
248
 * Sanitizes settings before they are saved.
249
 * 
250
 * @return array
251
 */
252
function wpinv_settings_sanitize( $input = array() ) {
253
254
    $wpinv_options = wpinv_get_options();
255
256
    if ( empty( wp_get_raw_referer() ) ) {
257
        return $input;
258
    }
259
260
    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

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

550
						$image       = wpinv_locate_template( 'images' . DIRECTORY_SEPARATOR . 'icons' . DIRECTORY_SEPARATOR . $card . '.gif', /** @scrutinizer ignore-type */ false );
Loading history...
551
						$content_dir = WP_CONTENT_DIR;
552
553
						if ( function_exists( 'wp_normalize_path' ) ) {
554
							// Replaces backslashes with forward slashes for Windows systems
555
							$image = wp_normalize_path( $image );
556
							$content_dir = wp_normalize_path( $content_dir );
557
						}
558
559
						$image = str_replace( $content_dir, content_url(), $image );
560
					}
561
562
					echo '<img class="payment-icon" src="' . esc_url( $image ) . '" style="width:32px;height:24px;position:relative;top:6px;margin-right:5px;"/>';
563
				}
564
			echo $option . '</label>';
565
		}
566
		echo '<p class="description" style="margin-top:16px;">' . wp_kses_post( $args['desc'] ) . '</p>';
567
	}
568
}
569
570
/**
571
 * Displays a radio settings field.
572
 */
573
function wpinv_radio_callback( $args ) {
574
575
	$std = isset( $args['std'] ) ? $args['std'] : '';
576
	$std = wpinv_get_option( $args['id'], $std );
577
	?>
578
		<fieldset>
579
			<ul id="wpinv-settings-<?php echo esc_attr( $args['id'] ); ?>" style="margin-top: 0;">
580
				<?php foreach( $args['options'] as $key => $option ) : ?>
581
					<li>
582
						<label>
583
							<input name="wpinv_settings[<?php echo esc_attr( $args['id'] ); ?>]" <?php checked( $std, $key ); ?> value="<?php echo esc_attr( $key ); ?>" type="radio">
584
							<?php echo wp_kses_post( $option ); ?>
585
						</label>
586
					</li>
587
				<?php endforeach; ?>
588
			</ul>
589
		</fieldset>
590
	<?php
591
	getpaid_settings_description_callback( $args );
592
}
593
594
/**
595
 * Displays a description if available.
596
 */
597
function getpaid_settings_description_callback( $args ) {
598
599
	if ( ! empty( $args['desc'] ) ) {
600
		$description = wp_kses_post( $args['desc'] );
601
		echo "<p class='description'>$description</p>";
602
	}
603
604
}
605
606
function wpinv_gateways_callback( $args ) {
607
	global $wpinv_options;
608
    
609
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
610
611
	foreach ( $args['options'] as $key => $option ) :
612
		$sanitize_key = wpinv_sanitize_key( $key );
613
        
614
        if ( isset( $wpinv_options['gateways'][ $key ] ) )
615
			$enabled = '1';
616
		else
617
			$enabled = null;
618
619
		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;';
620
		echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . esc_html( $option['admin_label'] ) . '</label><br/>';
621
	endforeach;
622
}
623
624
function wpinv_gateway_select_callback($args) {
625
	global $wpinv_options;
626
    
627
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
628
    $class = !empty( $args['class'] ) ? ' ' . esc_attr( $args['class'] ) : '';
629
630
	echo '<select name="wpinv_settings[' . $sanitize_id . ']"" id="wpinv_settings[' . $sanitize_id . ']" class="'.$class.'" >';
631
632
	foreach ( $args['options'] as $key => $option ) :
633
		if ( isset( $args['selected'] ) && $args['selected'] !== null && $args['selected'] !== false ) {
634
            $selected = selected( $key, $args['selected'], false );
635
        } else {
636
            $selected = isset( $wpinv_options[ $args['id'] ] ) ? selected( $key, $wpinv_options[$args['id']], false ) : '';
637
        }
638
		echo '<option value="' . wpinv_sanitize_key( $key ) . '"' . $selected . '>' . esc_html( $option['admin_label'] ) . '</option>';
639
	endforeach;
640
641
	echo '</select>';
642
	echo '<label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
643
}
644
645
function wpinv_text_callback( $args ) {
646
	global $wpinv_options;
647
    
648
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
649
650
	if ( isset( $wpinv_options[ $args['id'] ] ) ) {
651
		$value = $wpinv_options[ $args['id'] ];
652
	} else {
653
		$value = isset( $args['std'] ) ? $args['std'] : '';
654
	}
655
656
	if ( isset( $args['faux'] ) && true === $args['faux'] ) {
657
		$args['readonly'] = true;
658
		$value = isset( $args['std'] ) ? $args['std'] : '';
659
		$name  = '';
660
	} else {
661
		$name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"';
662
	}
663
	$class = !empty( $args['class'] ) ? sanitize_html_class( $args['class'] ) : '';
664
665
	$readonly = $args['readonly'] === true ? ' readonly="readonly"' : '';
666
	$size     = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular';
667
	$html     = '<input type="text" class="' . sanitize_html_class( $size ) . '-text ' . $class . '" id="wpinv_settings[' . $sanitize_id . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '"' . $readonly . '/>';
668
	$html    .= '<br /><label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
669
670
	echo $html;
671
}
672
673
function wpinv_number_callback( $args ) {
674
	global $wpinv_options;
675
    
676
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
677
678
	if ( isset( $wpinv_options[ $args['id'] ] ) ) {
679
		$value = $wpinv_options[ $args['id'] ];
680
	} else {
681
		$value = isset( $args['std'] ) ? $args['std'] : '';
682
	}
683
684
	if ( isset( $args['faux'] ) && true === $args['faux'] ) {
685
		$args['readonly'] = true;
686
		$value = isset( $args['std'] ) ? $args['std'] : '';
687
		$name  = '';
688
	} else {
689
		$name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"';
690
	}
691
692
	$max  = isset( $args['max'] ) ? $args['max'] : 999999;
693
	$min  = isset( $args['min'] ) ? $args['min'] : 0;
694
	$step = isset( $args['step'] ) ? $args['step'] : 1;
695
	$class = !empty( $args['class'] ) ? sanitize_html_class( $args['class'] ) : '';
696
697
	$size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular';
698
	$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 ) ) . '"/>';
699
	$html .= '<br /><label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
700
701
	echo $html;
702
}
703
704
function wpinv_textarea_callback( $args ) {
705
	global $wpinv_options;
706
    
707
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
708
709
	if ( isset( $wpinv_options[ $args['id'] ] ) ) {
710
		$value = $wpinv_options[ $args['id'] ];
711
	} else {
712
		$value = isset( $args['std'] ) ? $args['std'] : '';
713
	}
714
    
715
    $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular';
716
    $class = ( isset( $args['class'] ) && ! is_null( $args['class'] ) ) ? $args['class'] : 'large-text';
717
718
	$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>';
719
	$html .= '<br /><label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
720
721
	echo $html;
722
}
723
724
function wpinv_password_callback( $args ) {
725
	global $wpinv_options;
726
    
727
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
728
729
	if ( isset( $wpinv_options[ $args['id'] ] ) ) {
730
		$value = $wpinv_options[ $args['id'] ];
731
	} else {
732
		$value = isset( $args['std'] ) ? $args['std'] : '';
733
	}
734
735
	$size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular';
736
	$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 ) . '"/>';
737
	$html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>';
738
739
	echo $html;
740
}
741
742
function wpinv_missing_callback($args) {
743
	printf(
744
		__( 'The callback function used for the %s setting is missing.', 'invoicing' ),
745
		'<strong>' . $args['id'] . '</strong>'
746
	);
747
}
748
749
function wpinv_select_callback($args) {
750
	global $wpinv_options;
751
    
752
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
753
754
	if ( isset( $wpinv_options[ $args['id'] ] ) ) {
755
		$value = $wpinv_options[ $args['id'] ];
756
	} else {
757
		$value = isset( $args['std'] ) ? $args['std'] : '';
758
	}
759
    
760
    if ( isset( $args['selected'] ) && $args['selected'] !== null && $args['selected'] !== false ) {
761
        $value = $args['selected'];
762
    }
763
764
	if ( isset( $args['placeholder'] ) ) {
765
		$placeholder = $args['placeholder'];
766
	} else {
767
		$placeholder = '';
768
	}
769
    
770
    if( !empty( $args['onchange'] ) ) {
771
        $onchange = ' onchange="' . esc_attr( $args['onchange'] ) . '"';
772
    } else {
773
        $onchange = '';
774
    }
775
776
    $class = !empty( $args['class'] ) ? ' ' . esc_attr( $args['class'] ) : '';
777
778
	$html = '<select id="wpinv_settings[' . $sanitize_id . ']" class="'.$class.'"  name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" data-placeholder="' . esc_html( $placeholder ) . '"' . $onchange . ' />';
779
780
	foreach ( $args['options'] as $option => $name ) {
781
		$selected = selected( $option, $value, false );
782
		$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $name ) . '</option>';
783
	}
784
785
	$html .= '</select>';
786
	$html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>';
787
788
	echo $html;
789
}
790
791
function wpinv_color_select_callback( $args ) {
792
	global $wpinv_options;
793
    
794
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
795
796
	if ( isset( $wpinv_options[ $args['id'] ] ) ) {
797
		$value = $wpinv_options[ $args['id'] ];
798
	} else {
799
		$value = isset( $args['std'] ) ? $args['std'] : '';
800
	}
801
802
	$html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"/>';
803
804
	foreach ( $args['options'] as $option => $color ) {
805
		$selected = selected( $option, $value, false );
806
		$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $color['label'] ) . '</option>';
807
	}
808
809
	$html .= '</select>';
810
	$html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
811
812
	echo $html;
813
}
814
815
function wpinv_rich_editor_callback( $args ) {
816
	global $wpinv_options, $wp_version;
817
    
818
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
819
820
	if ( isset( $wpinv_options[ $args['id'] ] ) ) {
821
		$value = $wpinv_options[ $args['id'] ];
822
823
		if( empty( $args['allow_blank'] ) && empty( $value ) ) {
824
			$value = isset( $args['std'] ) ? $args['std'] : '';
825
		}
826
	} else {
827
		$value = isset( $args['std'] ) ? $args['std'] : '';
828
	}
829
830
	$rows = isset( $args['size'] ) ? $args['size'] : 20;
831
832
	$html = '<div class="getpaid-settings-editor-input">';
833
	if ( $wp_version >= 3.3 && function_exists( 'wp_editor' ) ) {
834
		ob_start();
835
		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 ) );
836
		$html .= ob_get_clean();
837
	} else {
838
		$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>';
839
	}
840
841
	$html .= '</div><br/><label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>';
842
843
	echo $html;
844
}
845
846
function wpinv_upload_callback( $args ) {
847
	global $wpinv_options;
848
    
849
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
850
851
	if ( isset( $wpinv_options[ $args['id'] ] ) ) {
852
		$value = $wpinv_options[$args['id']];
853
	} else {
854
		$value = isset($args['std']) ? $args['std'] : '';
855
	}
856
857
	$size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular';
858
	$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 ) ) . '"/>';
859
	$html .= '<span>&nbsp;<input type="button" class="wpinv_settings_upload_button button-secondary" value="' . __( 'Upload File', 'invoicing' ) . '"/></span>';
860
	$html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>';
861
862
	echo $html;
863
}
864
865
function wpinv_color_callback( $args ) {
866
	global $wpinv_options;
867
    
868
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
869
870
	if ( isset( $wpinv_options[ $args['id'] ] ) ) {
871
		$value = $wpinv_options[ $args['id'] ];
872
	} else {
873
		$value = isset( $args['std'] ) ? $args['std'] : '';
874
	}
875
876
	$default = isset( $args['std'] ) ? $args['std'] : '';
877
878
	$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 ) . '" />';
879
	$html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
880
881
	echo $html;
882
}
883
884
function wpinv_country_states_callback($args) {
885
	global $wpinv_options;
886
    
887
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
888
889
	if ( isset( $args['placeholder'] ) ) {
890
		$placeholder = $args['placeholder'];
891
	} else {
892
		$placeholder = '';
893
	}
894
895
	$states = wpinv_get_country_states();
896
897
	$class = empty( $states ) ? ' class="wpinv-no-states"' : ' class="wpi_select2"';
898
	$html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"' . $class . 'data-placeholder="' . esc_html( $placeholder ) . '"/>';
899
900
	foreach ( $states as $option => $name ) {
901
		$selected = isset( $wpinv_options[ $args['id'] ] ) ? selected( $option, $wpinv_options[$args['id']], false ) : '';
902
		$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $name ) . '</option>';
903
	}
904
905
	$html .= '</select>';
906
	$html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
907
908
	echo $html;
909
}
910
911
/**
912
 * Displays the tax rates edit table.
913
 */
914
function wpinv_tax_rates_callback() {
915
	
916
	?>
917
		</td>
918
	</tr>
919
	<tr class="bsui">
920
    	<td colspan="2" class="p-0">
921
			<?php include plugin_dir_path( __FILE__ ) . 'views/html-tax-rates-edit.php'; ?>
922
923
	<?php
924
925
}
926
927
/**
928
 * Displays a tax rate' edit row.
929
 */
930
function wpinv_tax_rate_callback( $tax_rate, $key, $echo = true ) {
931
	ob_start();
932
933
	$key                      = sanitize_key( $key );
934
	$tax_rate['reduced_rate'] = empty( $tax_rate['reduced_rate'] ) ? 0 : $tax_rate['reduced_rate'];
935
	include plugin_dir_path( __FILE__ ) . 'views/html-tax-rate-edit.php';
936
937
	if ( $echo ) {
938
		echo ob_get_clean( $echo );
0 ignored issues
show
Unused Code introduced by
The call to ob_get_clean() has too many arguments starting with $echo. ( Ignorable by Annotation )

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

938
		echo /** @scrutinizer ignore-call */ ob_get_clean( $echo );

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

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

Loading history...
939
	} else {
940
		return ob_get_clean( $echo ); 
941
	}
942
943
}
944
945
function wpinv_tools_callback($args) {
946
    ob_start(); ?>
947
    </td><tr>
948
    <td colspan="2" class="wpinv_tools_tdbox">
949
    <?php if ( $args['desc'] ) { ?><p><?php echo $args['desc']; ?></p><?php } ?>
950
    <?php do_action( 'wpinv_tools_before' ); ?>
951
    <table id="wpinv_tools_table" class="wp-list-table widefat fixed posts">
952
        <thead>
953
            <tr>
954
                <th scope="col" class="wpinv-th-tool"><?php _e( 'Tool', 'invoicing' ); ?></th>
955
                <th scope="col" class="wpinv-th-desc"><?php _e( 'Description', 'invoicing' ); ?></th>
956
                <th scope="col" class="wpinv-th-action"><?php _e( 'Action', 'invoicing' ); ?></th>
957
            </tr>
958
        </thead>
959
            <?php do_action( 'wpinv_tools_row' ); ?>
960
        <tbody>
961
        </tbody>
962
    </table>
963
    <?php do_action( 'wpinv_tools_after' ); ?>
964
    <?php
965
    echo ob_get_clean();
966
}
967
968
function wpinv_descriptive_text_callback( $args ) {
969
	echo wp_kses_post( $args['desc'] );
970
}
971
972
function wpinv_hook_callback( $args ) {
973
	do_action( 'wpinv_' . $args['id'], $args );
974
}
975
976
function wpinv_set_settings_cap() {
977
	return wpinv_get_capability();
978
}
979
add_filter( 'option_page_capability_wpinv_settings', 'wpinv_set_settings_cap' );
980
981
function wpinv_settings_sanitize_input( $value, $key ) {
982
983
    if ( $key == 'tax_rate' ) {
984
        $value = wpinv_sanitize_amount( $value );
985
        $value = $value >= 100 ? 99 : $value;
986
    }
987
988
    return $value;
989
}
990
add_filter( 'wpinv_settings_sanitize', 'wpinv_settings_sanitize_input', 10, 2 );
991
992
function wpinv_on_update_settings( $old_value, $value, $option ) {
993
    $old = !empty( $old_value['remove_data_on_unistall'] ) ? 1 : '';
994
    $new = !empty( $value['remove_data_on_unistall'] ) ? 1 : '';
995
    
996
    if ( $old != $new ) {
997
        update_option( 'wpinv_remove_data_on_invoice_unistall', $new );
998
    }
999
}
1000
add_action( 'update_option_wpinv_settings', 'wpinv_on_update_settings', 10, 3 );
1001
add_action( 'wpinv_settings_tab_bottom_emails_new_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 );
1002
add_action( 'wpinv_settings_tab_bottom_emails_cancelled_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 );
1003
add_action( 'wpinv_settings_tab_bottom_emails_failed_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 );
1004
add_action( 'wpinv_settings_tab_bottom_emails_onhold_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 );
1005
add_action( 'wpinv_settings_tab_bottom_emails_processing_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 );
1006
add_action( 'wpinv_settings_tab_bottom_emails_completed_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 );
1007
add_action( 'wpinv_settings_tab_bottom_emails_refunded_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 );
1008
add_action( 'wpinv_settings_tab_bottom_emails_user_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 );
1009
add_action( 'wpinv_settings_tab_bottom_emails_user_note', 'wpinv_settings_tab_bottom_emails', 10, 2 );
1010
add_action( 'wpinv_settings_tab_bottom_emails_overdue', 'wpinv_settings_tab_bottom_emails', 10, 2 );
1011
1012
function wpinv_settings_tab_bottom_emails( $active_tab, $section ) {
1013
    ?>
1014
    <div class="wpinv-email-wc-row ">
1015
        <div class="wpinv-email-wc-td">
1016
            <h3 class="wpinv-email-wc-title"><?php echo apply_filters( 'wpinv_settings_email_wildcards_title', __( 'Wildcards For Emails', 'invoicing' ) ); ?></h3>
1017
            <p class="wpinv-email-wc-description">
1018
                <?php
1019
                $description = __( 'The following wildcards can be used in email subjects, heading and content:<br>
1020
                    <strong>{site_title} :</strong> Site Title<br>
1021
                    <strong>{name} :</strong> Customer\'s full name<br>
1022
                    <strong>{first_name} :</strong> Customer\'s first name<br>
1023
                    <strong>{last_name} :</strong> Customer\'s last name<br>
1024
                    <strong>{email} :</strong> Customer\'s email address<br>
1025
                    <strong>{invoice_number} :</strong> The invoice number<br>
1026
                    <strong>{invoice_total} :</strong> The invoice total<br>
1027
                    <strong>{invoice_link} :</strong> The invoice link<br>
1028
                    <strong>{invoice_pay_link} :</strong> The payment link<br>
1029
                    <strong>{invoice_date} :</strong> The date the invoice was created<br>
1030
                    <strong>{invoice_due_date} :</strong> The date the invoice is due<br>
1031
                    <strong>{date} :</strong> Today\'s date.<br>
1032
                    <strong>{is_was} :</strong> If due date of invoice is past, displays "was" otherwise displays "is"<br>
1033
                    <strong>{invoice_label} :</strong> Invoices/quotes singular name. Ex: Invoice/Quote<br>', 'invoicing' );
1034
                echo apply_filters('wpinv_settings_email_wildcards_description', $description, $active_tab, $section);
1035
                ?>
1036
            </p>
1037
        </div>
1038
    </div>
1039
    <?php
1040
}