Passed
Push — master ( 18d41c...f9af5f )
by Paul
13:52 queued 07:13
created

Sanitizer   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 233
Duplicated Lines 0 %

Test Coverage

Coverage 90.79%

Importance

Changes 7
Bugs 1 Features 2
Metric Value
wmc 31
eloc 59
c 7
b 1
f 2
dl 0
loc 233
ccs 69
cts 76
cp 0.9079
rs 9.92

20 Methods

Rating   Name   Duplication   Size   Complexity  
A sanitizeTextHtml() 0 10 1
A sanitizeUrl() 0 8 2
A sanitizeArrayInt() 0 3 1
A buildSanitizers() 0 9 3
A sanitizeSlug() 0 3 1
A sanitizeText() 0 3 1
A sanitizeTextMultiline() 0 3 1
A sanitizeArrayString() 0 3 1
A sanitizeBool() 0 3 1
A sanitizeArray() 0 3 1
A sanitizeKey() 0 3 1
A sanitizeEmail() 0 3 1
A run() 0 9 3
A __construct() 0 4 1
A sanitizeDate() 0 7 2
A sanitizeInt() 0 3 1
A sanitizeUserName() 0 8 3
A sanitizeUserEmail() 0 8 3
A sanitizeId() 0 8 2
A sanitizeName() 0 4 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use GeminiLabs\SiteReviews\Helper;
6
use GeminiLabs\SiteReviews\Helpers\Arr;
7
use GeminiLabs\SiteReviews\Helpers\Cast;
8
use GeminiLabs\SiteReviews\Helpers\Str;
9
10
class Sanitizer
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $sanitizers;
16
17
    /**
18
     * @var array
19
     */
20
    protected $values;
21
22 38
    public function __construct(array $values = [], array $sanitizers = [])
23
    {
24 38
        $this->sanitizers = $this->buildSanitizers(Arr::consolidate($sanitizers));
25 38
        $this->values = Arr::consolidate($values);
26 38
    }
27
28
    /**
29
     * @return array|bool|string
30
     */
31 38
    public function run()
32
    {
33 38
        $result = $this->values;
34 38
        foreach ($this->values as $key => $value) {
35 38
            if (array_key_exists($key, $this->sanitizers)) {
36 38
                $result[$key] = call_user_func([$this, $this->sanitizers[$key]], $value);
37
            }
38
        }
39 38
        return $result;
40
    }
41
42
    /**
43
     * @param mixed $value
44
     * @return array
45
     */
46 17
    public function sanitizeArray($value)
47
    {
48 17
        return Arr::consolidate($value);
49
    }
50
51
    /**
52
     * @param mixed $value
53
     * @return int[]
54
     */
55 25
    public function sanitizeArrayInt($value)
56
    {
57 25
        return Arr::uniqueInt(Cast::toArray($value));
58
    }
59
60
    /**
61
     * @param mixed $value
62
     * @return string[]
63
     */
64 1
    public function sanitizeArrayString($value)
65
    {
66 1
        return array_filter(Cast::toArray($value), 'is_string');
67
    }
68
69
    /**
70
     * @param mixed $value
71
     * @return bool
72
     */
73 16
    public function sanitizeBool($value)
74
    {
75 16
        return Cast::toBool($value);
76
    }
77
78
    /**
79
     * If date is invalid then return an empty string.
80
     * @param mixed $value
81
     * @param string $fallback
82
     * @return string
83
     */
84 24
    public function sanitizeDate($value, $fallback = '')
85
    {
86 24
        $date = strtotime(trim(Cast::toString($value)));
87 24
        if (false !== $date) {
88 1
            return wp_date('Y-m-d H:i:s', $date);
89
        }
90 24
        return $fallback;
91
    }
92
93
    /**
94
     * @param mixed $value
95
     * @return string
96
     */
97 25
    public function sanitizeEmail($value)
98
    {
99 25
        return sanitize_email(trim(Cast::toString($value)));
100
    }
101
102
    /**
103
     * @param mixed $value
104
     * @return string
105
     */
106 7
    public function sanitizeId($value)
107
    {
108 7
        require_once ABSPATH.WPINC.'/pluggable.php';
109 7
        $value = $this->sanitizeSlug($value);
110 7
        if (empty($value)) {
111 7
            $value = glsr()->prefix.substr(wp_hash(serialize($this->values), 'nonce'), -12, 8);
112
        }
113 7
        return $value;
114
    }
115
116
    /**
117
     * @param mixed $value
118
     * @return int
119
     */
120 16
    public function sanitizeInt($value)
121
    {
122 16
        return Cast::toInt($value);
123
    }
124
125
    /**
126
     * This allows lowercase alphannumeric and underscore characters
127
     * @param mixed $value
128
     * @return string
129
     */
130 23
    public function sanitizeKey($value)
131
    {
132 23
        return Str::snakeCase(sanitize_key($this->sanitizeText($value)));
133
    }
134
135
    /**
136
     * This allows lowercase alpha and underscore characters
137
     * @param mixed $value
138
     * @return string
139
     */
140 22
    public function sanitizeName($value)
141
    {
142 22
        $value = Str::snakeCase($this->sanitizeText($value));
143 22
        return preg_replace('/[^a-z_]/', '', $value);
144
    }
145
146
    /**
147
     * @param mixed $value
148
     * @return string
149
     */
150 8
    public function sanitizeSlug($value)
151
    {
152 8
        return sanitize_title($this->sanitizeText($value));
153
    }
154
155
    /**
156
     * @param mixed $value
157
     * @return string
158
     */
159 28
    public function sanitizeText($value)
160
    {
161 28
        return sanitize_text_field(trim(Cast::toString($value)));
162
    }
163
164
    /**
165
     * @param mixed $value
166
     * @return string
167
     */
168
    public function sanitizeTextHtml($value)
169
    {
170
        global $allowedposttags;
171
        $allowedHtml = [
172
            'a' => glsr_get($allowedposttags, 'a'),
173
            'em' => glsr_get($allowedposttags, 'em'),
174
            'strong' => glsr_get($allowedposttags, 'strong'),
175
        ];
176
        $allowedHtml = glsr()->filterString('sanitize/allowed-html-tags', $allowedHtml, $allowedposttags);
177
        return wp_kses(trim(Cast::toString($value)), $allowedHtml);
178
    }
179
180
    /**
181
     * @param mixed $value
182
     * @return string
183
     */
184 16
    public function sanitizeTextMultiline($value)
185
    {
186 16
        return sanitize_textarea_field(trim(Cast::toString($value)));
187
    }
188
189
    /**
190
     * @param mixed $value
191
     * @return string
192
     */
193 16
    public function sanitizeUrl($value)
194
    {
195 16
        $url = trim(Cast::toString($value));
196 16
        if (!Str::startsWith('http://, https://', $url)) {
197 16
            $url = Str::prefix($url, 'https://');
198
        }
199 16
        $url = wp_http_validate_url($url);
200 16
        return esc_url_raw(Cast::toString($url));
201
    }
202
203
    /**
204
     * @param mixed $value
205
     * @return string
206
     */
207 15
    public function sanitizeUserEmail($value)
208
    {
209 15
        $user = wp_get_current_user();
210 15
        $value = trim(Cast::toString($value));
211 15
        if ($user->exists() && !glsr()->retrieveAs('bool', 'import', false)) {
212 2
            return Helper::ifEmpty($value, $user->user_email);
213
        }
214 15
        return sanitize_email($value);
215
    }
216
217
    /**
218
     * @param mixed $value
219
     * @return string
220
     */
221 15
    public function sanitizeUserName($value)
222
    {
223 15
        $user = wp_get_current_user();
224 15
        $value = trim(Cast::toString($value));
225 15
        if ($user->exists() && !glsr()->retrieveAs('bool', 'import', false)) {
226 2
            return Helper::ifEmpty($value, $user->display_name);
227
        }
228 15
        return sanitize_text_field($value);
229
    }
230
231
    /**
232
     * @return array
233
     */
234 38
    protected function buildSanitizers(array $sanitizers)
235
    {
236 38
        foreach ($sanitizers as $key => &$type) {
237 38
            $method = Helper::buildMethodName($type, 'sanitize');
238 38
            $type = method_exists($this, $method)
239 38
                ? $method
240 38
                : 'sanitizeText';
241
        }
242 38
        return $sanitizers;
243
    }
244
}
245