lsx_sanitize_choices()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 6
nop 2
dl 0
loc 17
rs 9.6111
c 0
b 0
f 0
1
<?php
2
/**
3
 * LSX sanitize callbacks for the customizer options.
4
 *
5
 * @package    lsx
6
 * @subpackage sanitize
7
 */
8
9
if ( ! defined( 'ABSPATH' ) ) {
10
	exit;
11
}
12
13
if ( ! function_exists( 'lsx_sanitize_choices' ) ) :
14
15
	/**
16
	 * Sanitize a value from a list of allowed values.
17
	 *
18
	 * @package    lsx
19
	 * @subpackage sanitize
20
	 *
21
	 * @param mixed $value      The value to sanitize.
22
	 * @param mixed $setting    The setting for which the sanitizing is occurring.
23
	 * @return mixed                The sanitized value.
24
	 */
25
	function lsx_sanitize_choices( $value, $setting ) {
26
		if ( is_object( $setting ) ) {
27
			$setting = $setting->id;
28
		}
29
30
		$choices = lsx_customizer_sanitize_get_choices( $setting );
31
32
		if ( ! is_wp_error( $choices ) && ! empty( $choices ) ) {
33
			$allowed_choices = array_keys( $choices );
0 ignored issues
show
Bug introduced by
It seems like $choices can also be of type WP_Error; however, parameter $array of array_keys() does only seem to accept array, 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

33
			$allowed_choices = array_keys( /** @scrutinizer ignore-type */ $choices );
Loading history...
34
35
			if ( ! in_array( $value, $allowed_choices ) ) {
0 ignored issues
show
introduced by
Not using strict comparison for in_array; supply true for third argument.
Loading history...
36
				$value = lsx_customizer_sanitize_get_default( $setting );
37
			}
38
39
			return $value;
40
		} else {
41
			return $choices;
42
		}
43
	}
44
45
endif;
46
47
if ( ! function_exists( 'lsx_customizer_sanitize_get_choices' ) ) :
48
49
	/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$id" missing
Loading history...
50
	 * Helper function to return the choices for a field.
51
	 *
52
	 * @package    lsx
53
	 * @subpackage sanitize
54
	 *
55
	 * @param string
0 ignored issues
show
Coding Style introduced by
Missing parameter name
Loading history...
56
	 * @return mixed $field
57
	 */
58
	function lsx_customizer_sanitize_get_choices( $id ) {
59
		global $lsx_customizer;
60
61
		$can_validate = method_exists( 'WP_Customize_Setting', 'validate' );
62
		$field        = $lsx_customizer->get_control( $id );
63
64
		if ( ! isset( $field['choices'] ) ) {
65
			return $can_validate ? new WP_Error( 'notexists', esc_html__( 'Choice doesn\'t exist', 'lsx' ) ) : false;
66
		}
67
68
		return $field['choices'];
69
	}
70
71
endif;
72
73
if ( ! function_exists( 'lsx_customizer_sanitize_get_default' ) ) :
74
75
	/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$id" missing
Loading history...
76
	 * Helper function to return defaults.
77
	 *
78
	 * @package    lsx
79
	 * @subpackage sanitize
80
	 *
81
	 * @param string
0 ignored issues
show
Coding Style introduced by
Missing parameter name
Loading history...
82
	 * @return mixed $default
83
	 */
84
	function lsx_customizer_sanitize_get_default( $id ) {
85
		global $lsx_customizer;
86
		$setting = $lsx_customizer->get_setting( $id );
87
88
		if ( isset( $setting['default'] ) ) {
89
			return $setting['default'];
90
		}
91
92
		return false;
93
	}
94
95
endif;
96
97
if ( ! function_exists( 'lsx_sanitize_checkbox' ) ) :
98
99
	/**
100
	 * Sanitizes an single or multiple checkbox input.
101
	 *
102
	 * @package    lsx
103
	 * @subpackage sanitize
104
	 *
105
	 * @param array $input
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
106
	 * @return array $output
107
	 */
108
	function lsx_sanitize_checkbox( $input ) {
109
		$can_validate = method_exists( 'WP_Customize_Setting', 'validate' );
110
111
		if ( ! is_bool( $input ) ) {
0 ignored issues
show
introduced by
The condition is_bool($input) is always false.
Loading history...
112
			return $can_validate ? new WP_Error( 'notboolean', esc_html__( 'Not a boolean', 'lsx' ) ) : false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $can_validate ? n...olean', 'lsx')) : false returns the type WP_Error|false which is incompatible with the documented return type array.
Loading history...
113
		}
114
115
		return $input;
116
	}
117
118
endif;
119