Passed
Push — master ( 2f00ce...78f7a8 )
by Brian
04:58
created

wpinv_hidden_callback()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
c 0
b 0
f 0
nc 8
nop 1
dl 0
loc 19
rs 9.6111
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 $input;
270
    }
271
272
    wp_parse_str( $raw_referrer, $referrer );
273
274
	if ( empty( $referrer['tab'] ) ) {
275
        return $input;
276
	}
277
278
    $settings = wpinv_get_registered_settings();
279
    $tab      = isset( $referrer['tab'] ) ? $referrer['tab'] : 'general';
280
    $section  = isset( $referrer['section'] ) ? $referrer['section'] : 'main';
281
282
    $input = $input ? $input : array();
283
    $input = apply_filters( 'wpinv_settings_tab_' . $tab . '_sanitize', $input );
284
    $input = apply_filters( 'wpinv_settings_' . $tab . '-' . $section . '_sanitize', $input );
285
286
    // Loop through each setting being saved and pass it through a sanitization filter
287
    foreach ( $input as $key => $value ) {
288
289
        // Get the setting type (checkbox, select, etc)
290
        $type = isset( $settings[ $tab ][$section][ $key ]['type'] ) ? $settings[ $tab ][$section][ $key ]['type'] : false;
291
292
        if ( $type ) {
293
            // Field type specific filter
294
            $input[$key] = apply_filters( 'wpinv_settings_sanitize_' . $type, $value, $key );
295
        }
296
297
        // General filter
298
		$input[ $key ] = apply_filters( 'wpinv_settings_sanitize', $input[ $key ], $key );
299
300
		// Key specific filter.
301
		$input[ $key ] = apply_filters( "wpinv_settings_sanitize_$key", $input[ $key ] );
302
    }
303
304
    // Loop through the whitelist and unset any that are empty for the tab being saved
305
    $main_settings    = isset( $settings[ $tab ] ) ? $settings[ $tab ] : array(); // Check for extensions that aren't using new sections
306
    $section_settings = ! empty( $settings[ $tab ][ $section ] ) ? $settings[ $tab ][ $section ] : array();
307
308
    $found_settings   = array_merge( $main_settings, $section_settings );
309
310
    if ( ! empty( $found_settings ) ) {
311
        foreach ( $found_settings as $key => $value ) {
312
313
            // settings used to have numeric keys, now they have keys that match the option ID. This ensures both methods work
314
            if ( is_numeric( $key ) ) {
315
                $key = $value['id'];
316
            }
317
318
            if ( ! isset( $input[ $key ] ) && isset( $wpinv_options[ $key ] ) ) {
319
                unset( $wpinv_options[ $key ] );
320
            }
321
        }
322
    }
323
324
    // Merge our new settings with the existing
325
    $output = array_merge( $wpinv_options, $input );
326
327
    add_settings_error( 'wpinv-notices', '', __( 'Settings updated.', 'invoicing' ), 'updated' );
328
329
    return $output;
330
}
331
332
function wpinv_settings_sanitize_misc_accounting( $input ) {
333
334
    if ( ! wpinv_current_user_can_manage_invoicing() ) {
335
        return $input;
336
    }
337
338
    if( ! empty( $input['enable_sequential'] ) && !wpinv_get_option( 'enable_sequential' ) ) {
339
        // Shows an admin notice about upgrading previous order numbers
340
        getpaid_session()->set( 'upgrade_sequential', '1' );
341
    }
342
343
    return $input;
344
}
345
add_filter( 'wpinv_settings_misc-accounting_sanitize', 'wpinv_settings_sanitize_misc_accounting' );
346
347
function wpinv_settings_sanitize_tax_rates( $input ) {
348
    if( ! wpinv_current_user_can_manage_invoicing() ) {
349
        return $input;
350
    }
351
352
    $new_rates = ! empty( $_POST['tax_rates'] ) ? array_values( $_POST['tax_rates'] ) : array();
353
    $tax_rates = array();
354
355
    foreach ( $new_rates as $rate ) {
356
357
		$rate['rate']    = wpinv_sanitize_amount( $rate['rate'] );
358
		$rate['name']    = sanitize_text_field( $rate['name'] );
359
		$rate['state']   = sanitize_text_field( $rate['state'] );
360
		$rate['country'] = sanitize_text_field( $rate['country'] );
361
		$rate['global']  = empty( $rate['state'] );
362
		$tax_rates[]     = $rate;
363
364
	}
365
366
    update_option( 'wpinv_tax_rates', $tax_rates );
367
368
    return $input;
369
}
370
add_filter( 'wpinv_settings_taxes-rates_sanitize', 'wpinv_settings_sanitize_tax_rates' );
371
372
function wpinv_sanitize_text_field( $input ) {
373
    return trim( $input );
374
}
375
add_filter( 'wpinv_settings_sanitize_text', 'wpinv_sanitize_text_field' );
376
377
function wpinv_get_settings_tabs() {
378
    $tabs             = array();
379
    $tabs['general']  = __( 'General', 'invoicing' );
380
    $tabs['gateways'] = __( 'Payment Gateways', 'invoicing' );
381
    $tabs['taxes']    = __( 'Taxes', 'invoicing' );
382
	$tabs['emails']   = __( 'Emails', 'invoicing' );
383
384
	if ( count( getpaid_get_integration_settings() ) > 0 ) {
385
		$tabs['integrations'] = __( 'Integrations', 'invoicing' );
386
	}
387
388
    $tabs['privacy']  = __( 'Privacy', 'invoicing' );
389
    $tabs['misc']     = __( 'Misc', 'invoicing' );
390
    $tabs['tools']    = __( 'Tools', 'invoicing' );
391
392
    return apply_filters( 'wpinv_settings_tabs', $tabs );
393
}
394
395
function wpinv_get_settings_tab_sections( $tab = false ) {
396
    $tabs     = false;
397
    $sections = wpinv_get_registered_settings_sections();
398
399
    if( $tab && ! empty( $sections[ $tab ] ) ) {
400
        $tabs = $sections[ $tab ];
401
    }
402
403
    return $tabs;
404
}
405
406
function wpinv_get_registered_settings_sections() {
407
    static $sections = false;
408
409
    if ( false !== $sections ) {
410
        return $sections;
411
    }
412
413
    $sections = array(
414
        'general' => apply_filters( 'wpinv_settings_sections_general', array(
415
            'main' => __( 'General Settings', 'invoicing' ),
416
            'currency_section' => __( 'Currency Settings', 'invoicing' ),
417
            'labels' => __( 'Label Texts', 'invoicing' ),
418
        ) ),
419
        'gateways' => apply_filters( 'wpinv_settings_sections_gateways', array(
420
            'main' => __( 'Gateway Settings', 'invoicing' ),
421
        ) ),
422
        'taxes' => apply_filters( 'wpinv_settings_sections_taxes', array(
423
            'main'  => __( 'Tax Settings', 'invoicing' ),
424
			'rates' => __( 'Tax Rates', 'invoicing' ),
425
			'vat'   => __( 'EU VAT Settings', 'invoicing' )
426
        ) ),
427
        'emails' => apply_filters( 'wpinv_settings_sections_emails', array(
428
            'main' => __( 'Email Settings', 'invoicing' ),
429
		) ),
430
431
		'integrations' => wp_list_pluck( getpaid_get_integration_settings(), 'label', 'id' ),
432
433
        'privacy' => apply_filters( 'wpinv_settings_sections_privacy', array(
434
            'main' => __( 'Privacy policy', 'invoicing' ),
435
        ) ),
436
        'misc' => apply_filters( 'wpinv_settings_sections_misc', array(
437
            'main' => __( 'Miscellaneous', 'invoicing' ),
438
            'custom-css' => __( 'Custom CSS', 'invoicing' ),
439
        ) ),
440
        'tools' => apply_filters( 'wpinv_settings_sections_tools', array(
441
            'main' => __( 'Diagnostic Tools', 'invoicing' ),
442
        ) ),
443
    );
444
445
    $sections = apply_filters( 'wpinv_settings_sections', $sections );
446
447
    return $sections;
448
}
449
450
function wpinv_get_pages( $with_slug = false, $default_label = NULL ) {
451
	$pages_options = array();
452
453
	if( $default_label !== NULL && $default_label !== false ) {
454
		$pages_options = array( '' => $default_label ); // Blank option
455
	}
456
457
	$pages = get_pages();
458
	if ( $pages ) {
459
		foreach ( $pages as $page ) {
460
			$title = $with_slug ? $page->post_title . ' (' . $page->post_name . ')' : $page->post_title;
461
            $pages_options[ $page->ID ] = $title;
462
		}
463
	}
464
465
	return $pages_options;
466
}
467
468
function wpinv_header_callback( $args ) {
469
	if ( !empty( $args['desc'] ) ) {
470
        echo $args['desc'];
471
    }
472
}
473
474
function wpinv_hidden_callback( $args ) {
475
476
	$std     = isset( $args['std'] ) ? $args['std'] : '';
477
	$value   = wpinv_get_option( $args['id'], $std );
478
479
	if ( isset( $args['set_value'] ) ) {
480
		$value = $args['set_value'];
481
	}
482
483
	if ( isset( $args['faux'] ) && true === $args['faux'] ) {
484
		$args['readonly'] = true;
485
		$name  = '';
486
	} else {
487
		$name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"';
488
	}
489
490
	$html = '<input type="hidden" id="wpinv_settings[' . wpinv_sanitize_key( $args['id'] ) . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '" />';
491
    
492
	echo $html;
493
}
494
495
/**
496
 * Displays a checkbox settings callback.
497
 */
498
function wpinv_checkbox_callback( $args ) {
499
500
	$std = isset( $args['std'] ) ? $args['std'] : '';
501
	$std = wpinv_get_option( $args['id'], $std );
502
	$id  = esc_attr( $args['id'] );
503
504
	getpaid_hidden_field( "wpinv_settings[$id]", '0' );
505
	?>
506
		<fieldset>
507
			<label>
508
				<input id="wpinv-settings-<?php echo $id; ?>" name="wpinv_settings[<?php echo $id; ?>]" <?php checked( empty( $std ), false ); ?> value="1" type="checkbox">
509
				<?php echo wp_kses_post( $args['desc'] ); ?>
510
			</label>
511
		</fieldset>
512
	<?php
513
}
514
515
function wpinv_multicheck_callback( $args ) {
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   = wpinv_get_option( $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
    
543
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
544
	$value   = wpinv_get_option( $args['id'], false);
545
546
	if ( ! empty( $args['options'] ) ) {
547
		foreach( $args['options'] as $key => $option ) {
548
            $sanitize_key = wpinv_sanitize_key( $key );
549
            
550
			if( empty( $value ) ) {
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
/**
625
 * Displays a list of available gateways.
626
 */
627
function wpinv_gateways_callback() {
628
629
	?>
630
		</td>
631
	</tr>
632
	<tr class="bsui">
633
    	<td colspan="2" class="p-0">
634
			<?php include plugin_dir_path( __FILE__ ) . 'views/html-gateways-edit.php'; ?>
635
636
	<?php
637
}
638
639
function wpinv_gateway_select_callback($args) {
640
    
641
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
642
    $class = !empty( $args['class'] ) ? ' ' . esc_attr( $args['class'] ) : '';
643
	$std     = isset( $args['std'] ) ? $args['std'] : '';
644
	$value   = wpinv_get_option( $args['id'], $std );
645
646
	echo '<select name="wpinv_settings[' . $sanitize_id . ']"" id="wpinv_settings[' . $sanitize_id . ']" class="'.$class.'" >';
647
648
	foreach ( $args['options'] as $key => $option ) :
649
		if ( isset( $args['selected'] ) && $args['selected'] !== null && $args['selected'] !== false ) {
650
            $selected = selected( $key, $args['selected'], false );
651
        } else {
652
            $selected = selected( $key, $value, false );
653
        }
654
		echo '<option value="' . wpinv_sanitize_key( $key ) . '"' . $selected . '>' . esc_html( $option['admin_label'] ) . '</option>';
655
	endforeach;
656
657
	echo '</select>';
658
	echo '<label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
659
}
660
661
/**
662
 * Generates attributes.
663
 * 
664
 * @param array $args
665
 * @return string
666
 */
667
function wpinv_settings_attrs_helper( $args ) {
668
669
	$value        = isset( $args['std'] ) ? $args['std'] : '';
670
	$id           = esc_attr( $args['id'] );
671
	$placeholder  = esc_attr( $args['placeholder'] );
672
673
	if ( ! empty( $args['faux'] ) ) {
674
		$args['readonly'] = true;
675
		$name             = '';
676
	} else {
677
		$value  = wpinv_get_option( $args['id'], $value );
678
		$name   = "wpinv_settings[$id]";
679
	}
680
681
	$value    = is_scalar( $value ) ? esc_attr( $value ) : '';
682
	$class    = esc_attr( $args['class'] );
683
	$style    = esc_attr( $args['style'] );
684
	$readonly = empty( $args['readonly'] ) ? '' : 'readonly onclick="this.select()"';
685
686
	$onchange = '';
687
    if ( ! empty( $args['onchange'] ) ) {
688
        $onchange = ' onchange="' . esc_attr( $args['onchange'] ) . '"';
689
	}
690
691
	return "name='$name' id='wpinv-settings-$id' style='$style' value='$value' class='$class' placeholder='$placeholder' data-placeholder='$placeholder' $onchange $readonly";
692
}
693
694
/**
695
 * Displays a text input settings callback.
696
 */
697
function wpinv_text_callback( $args ) {
698
699
	$desc = wp_kses_post( $args['desc'] );
700
	$desc = empty( $desc ) ? '' : "<p class='description'>$desc</p>";
701
	$attr = wpinv_settings_attrs_helper( $args );
702
703
	?>
704
		<label style="width: 100%;">
705
			<input type="text" <?php echo $attr; ?>>
706
			<?php echo $desc; ?>
707
		</label>
708
	<?php
709
710
}
711
712
/**
713
 * Displays a number input settings callback.
714
 */
715
function wpinv_number_callback( $args ) {
716
717
	$desc = wp_kses_post( $args['desc'] );
718
	$desc = empty( $desc ) ? '' : "<p class='description'>$desc</p>";
719
	$attr = wpinv_settings_attrs_helper( $args );
720
	$max  = intval( $args['max'] );
721
	$min  = intval( $args['min'] );
722
	$step = floatval( $args['step'] );
723
724
	?>
725
		<label style="width: 100%;">
726
			<input type="number" step="<?php echo $step; ?>" max="<?php echo $max; ?>" min="<?php echo $min; ?>" <?php echo $attr; ?>>
727
			<?php echo $desc; ?>
728
		</label>
729
	<?php
730
731
}
732
733
function wpinv_textarea_callback( $args ) {
734
    
735
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
736
	$std     = isset( $args['std'] ) ? $args['std'] : '';
737
	$value   = wpinv_get_option( $args['id'], $std );
738
739
    $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular';
740
    $class = ( isset( $args['class'] ) && ! is_null( $args['class'] ) ) ? $args['class'] : 'large-text';
741
742
	$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>';
743
	$html .= '<br /><label for="wpinv_settings[' . $sanitize_id . ']"> '  . wp_kses_post( $args['desc'] ) . '</label>';
744
745
	echo $html;
746
}
747
748
function wpinv_password_callback( $args ) {
749
    
750
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
751
	$std     = isset( $args['std'] ) ? $args['std'] : '';
752
	$value   = wpinv_get_option( $args['id'], $std );
753
754
	$size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular';
755
	$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 ) . '"/>';
756
	$html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>';
757
758
	echo $html;
759
}
760
761
function wpinv_missing_callback($args) {
762
	printf(
763
		__( 'The callback function used for the %s setting is missing.', 'invoicing' ),
764
		'<strong>' . $args['id'] . '</strong>'
765
	);
766
}
767
768
/**
769
 * Displays a number input settings callback.
770
 */
771
function wpinv_select_callback( $args ) {
772
773
	$desc   = wp_kses_post( $args['desc'] );
774
	$desc   = empty( $desc ) ? '' : "<p class='description'>$desc</p>";
775
	$attr   = wpinv_settings_attrs_helper( $args );
776
	$value  = isset( $args['std'] ) ? $args['std'] : '';
777
	$value  = wpinv_get_option( $args['id'], $value );
778
	$rand   = uniqid( 'random_id' );
779
780
	?>
781
		<label style="width: 100%;">
782
			<select <?php echo $attr; ?>>
783
				<?php foreach ( $args['options'] as $option => $name ) : ?>
784
					<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

784
					<option value="<?php echo esc_attr( $option ); ?>" <?php echo selected( $option, $value ); ?>><?php echo /** @scrutinizer ignore-type */ wpinv_clean( $name ); ?></option>
Loading history...
785
				<?php endforeach;?>
786
			</select>
787
788
			<?php if ( substr( $args['id'], -5 ) === '_page' && is_numeric( $value ) ) : ?>
789
				<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

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