Passed
Push — master ( b7f7ee...5ac66d )
by Paul
15:27 queued 07:36
created

Sanitizer::sanitizeTextHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 7
dl 0
loc 10
ccs 0
cts 7
cp 0
rs 10
c 1
b 0
f 1
cc 1
nc 1
nop 1
crap 2
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 30
    public function __construct(array $values = [], array $sanitizers = [])
23
    {
24 30
        $this->sanitizers = $this->buildSanitizers(Arr::consolidate($sanitizers));
25 30
        $this->values = Arr::consolidate($values);
26 30
    }
27
28
    /**
29
     * @return array|bool|string
30
     */
31 30
    public function run()
32
    {
33 30
        $result = $this->values;
34 30
        foreach ($this->values as $key => $value) {
35 30
            if (array_key_exists($key, $this->sanitizers)) {
36 30
                $result[$key] = call_user_func([$this, $this->sanitizers[$key]], $value);
37
            }
38
        }
39 30
        return $result;
40
    }
41
42
    /**
43
     * @param mixed $value
44
     * @return array
45
     */
46 16
    public function sanitizeArray($value)
47
    {
48 16
        return Arr::consolidate($value);
49
    }
50
51
    /**
52
     * @param mixed $value
53
     * @return int[]
54
     */
55 17
    public function sanitizeArrayInt($value)
56
    {
57 17
        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 15
    public function sanitizeBool($value)
74
    {
75 15
        return Cast::toBool($value);
76
    }
77
78
    /**
79
     * If date is invalid then return an empty string.
80
     * @param mixed $value
81
     * @return string
82
     */
83 15
    public function sanitizeDate($value)
84
    {
85 15
        $date = strtotime(Cast::toString($value));
86 15
        if (false !== $date) {
87 1
            return wp_date('Y-m-d H:i:s', $date);
88
        }
89 15
        return '';
90
    }
91
92
    /**
93
     * @param mixed $value
94
     * @return string
95
     */
96 16
    public function sanitizeEmail($value)
97
    {
98 16
        return sanitize_email(Cast::toString($value));
99
    }
100
101
    /**
102
     * @param mixed $value
103
     * @return string
104
     */
105 7
    public function sanitizeId($value)
106
    {
107 7
        require_once ABSPATH.WPINC.'/pluggable.php';
108 7
        $value = $this->sanitizeSlug($value);
109 7
        if (empty($value)) {
110 7
            $value = glsr()->prefix.substr(wp_hash(serialize($this->values), 'nonce'), -12, 8);
111
        }
112 7
        return $value;
113
    }
114
115
    /**
116
     * @param mixed $value
117
     * @return int
118
     */
119 15
    public function sanitizeInt($value)
120
    {
121 15
        return Cast::toInt($value);
122
    }
123
124
    /**
125
     * @param mixed $value
126
     * @return string
127
     */
128 10
    public function sanitizeKey($value)
129
    {
130 10
        return Str::snakeCase(sanitize_key($this->sanitizeText($value)));
131
    }
132
133
    /**
134
     * @param mixed $value
135
     * @return string
136
     */
137 8
    public function sanitizeSlug($value)
138
    {
139 8
        return sanitize_title($this->sanitizeText($value));
140
    }
141
142
    /**
143
     * @param mixed $value
144
     * @return string
145
     */
146 20
    public function sanitizeText($value)
147
    {
148 20
        return sanitize_text_field(Cast::toString($value));
149
    }
150
151
    /**
152
     * @param mixed $value
153
     * @return string
154
     */
155
    public function sanitizeTextHtml($value)
156
    {
157
        global $allowedposttags;
158
        $allowedHtml = [
159
            'a' => glsr_get($allowedposttags, 'a'),
160
            'em' => glsr_get($allowedposttags, 'em'),
161
            'strong' => glsr_get($allowedposttags, 'strong'),
162
        ];
163
        $allowedHtml = glsr()->filterString('sanitize/allowed-html-tags', $allowedHtml, $allowedposttags);
164
        return trim(wp_kses(Cast::toString($value), $allowedHtml));
165
    }
166
167
    /**
168
     * @param mixed $value
169
     * @return string
170
     */
171 15
    public function sanitizeTextMultiline($value)
172
    {
173 15
        return sanitize_textarea_field(Cast::toString($value));
174
    }
175
176
    /**
177
     * @param mixed $value
178
     * @return string
179
     */
180 15
    public function sanitizeUrl($value)
181
    {
182 15
        $url = Cast::toString($value);
183 15
        if (!Str::startsWith('http://, https://', $url)) {
184 15
            $url = Str::prefix($url, 'https://');
185
        }
186 15
        $url = wp_http_validate_url($url);
187 15
        return esc_url_raw(Cast::toString($url));
188
    }
189
190
    /**
191
     * @param mixed $value
192
     * @return string
193
     */
194 14
    public function sanitizeUserEmail($value)
195
    {
196 14
        $user = wp_get_current_user();
197 14
        $value = Cast::toString($value);
198 14
        if ($user->exists() && !glsr()->retrieveAs('bool', 'import', false)) {
199 2
            return Helper::ifEmpty($value, $user->user_email);
200
        }
201 14
        return sanitize_email($value);
202
    }
203
204
    /**
205
     * @param mixed $value
206
     * @return string
207
     */
208 14
    public function sanitizeUserName($value)
209
    {
210 14
        $user = wp_get_current_user();
211 14
        $value = Cast::toString($value);
212 14
        if ($user->exists() && !glsr()->retrieveAs('bool', 'import', false)) {
213 2
            return Helper::ifEmpty($value, $user->display_name);
214
        }
215 14
        return sanitize_text_field($value);
216
    }
217
218
    /**
219
     * @return array
220
     */
221 30
    protected function buildSanitizers(array $sanitizers)
222
    {
223 30
        foreach ($sanitizers as $key => &$type) {
224 30
            $method = Helper::buildMethodName($type, 'sanitize');
225 30
            $type = method_exists($this, $method)
226 30
                ? $method
227 30
                : 'sanitizeText';
228
        }
229 30
        return $sanitizers;
230
    }
231
}
232