| 1 |
<?php |
| 2 |
|
| 3 |
namespace Cookiez\Modules\Settings\Classes; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class Sanitize_Content { |
| 10 |
|
| 11 |
public const ALLOWED_LANGUAGES = [ |
| 12 |
'ar', |
| 13 |
'cs', |
| 14 |
'da', |
| 15 |
'de', |
| 16 |
'el', |
| 17 |
'en', |
| 18 |
'es', |
| 19 |
'et', |
| 20 |
'fi', |
| 21 |
'fr', |
| 22 |
'he', |
| 23 |
'hi', |
| 24 |
'hr', |
| 25 |
'hu', |
| 26 |
'it', |
| 27 |
'ja', |
| 28 |
'ko', |
| 29 |
'nb', |
| 30 |
'nl', |
| 31 |
'nn', |
| 32 |
'pl', |
| 33 |
'pt', |
| 34 |
'ro', |
| 35 |
'ru', |
| 36 |
'sk', |
| 37 |
'sl', |
| 38 |
'sr', |
| 39 |
'sv', |
| 40 |
'th', |
| 41 |
'tr', |
| 42 |
'uk', |
| 43 |
'uz', |
| 44 |
'vi', |
| 45 |
'zh', |
| 46 |
]; |
| 47 |
|
| 48 |
private const CATEGORY_KEYS = [ |
| 49 |
'necessary', |
| 50 |
'functional', |
| 51 |
'analytics', |
| 52 |
'advertising', |
| 53 |
'unclassified', |
| 54 |
]; |
| 55 |
public const FALLBACK_LANGUAGE = 'en'; |
| 56 |
|
| 57 |
/** |
| 58 |
* Normalize a WordPress locale (e.g. `uk_UA`, `pt_BR`) to a supported short |
| 59 |
* language code matching the bundled translation JSON files and stored |
| 60 |
* content keys. Falls back to English when the locale cannot be mapped. |
| 61 |
* @param string[] $disabled_languages |
| 62 |
*/ |
| 63 |
public static function resolve_language( |
| 64 |
string $wp_locale, |
| 65 |
string $default_language = self::FALLBACK_LANGUAGE, |
| 66 |
array $disabled_languages = [] |
| 67 |
): string { |
| 68 |
if ( in_array( $wp_locale, self::ALLOWED_LANGUAGES, true ) ) { |
| 69 |
$resolved = $wp_locale; |
| 70 |
} else { |
| 71 |
$short = strtolower( strtok( $wp_locale, '_-' ) ); |
| 72 |
$resolved = in_array( $short, self::ALLOWED_LANGUAGES, true ) |
| 73 |
? $short |
| 74 |
: $default_language; |
| 75 |
} |
| 76 |
|
| 77 |
if ( in_array( $resolved, $disabled_languages, true ) ) { |
| 78 |
return $default_language; |
| 79 |
} |
| 80 |
|
| 81 |
return $resolved; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @param array<string, mixed> $raw |
| 86 |
* @return array<string, mixed> |
| 87 |
*/ |
| 88 |
public static function sanitize( array $raw ): array { |
| 89 |
$out = []; |
| 90 |
|
| 91 |
if ( isset( $raw['languages'] ) && is_array( $raw['languages'] ) ) { |
| 92 |
$out['languages'] = array_values( |
| 93 |
array_filter( |
| 94 |
array_map( 'sanitize_text_field', $raw['languages'] ), |
| 95 |
fn( $lang ) => in_array( $lang, self::ALLOWED_LANGUAGES, true ) |
| 96 |
) |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
if ( isset( $raw['disabledLanguages'] ) && is_array( $raw['disabledLanguages'] ) ) { |
| 101 |
$out['disabledLanguages'] = array_values( |
| 102 |
array_filter( |
| 103 |
array_map( 'sanitize_text_field', $raw['disabledLanguages'] ), |
| 104 |
fn( $lang ) => in_array( $lang, self::ALLOWED_LANGUAGES, true ) |
| 105 |
) |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
if ( isset( $raw['content'] ) && is_array( $raw['content'] ) ) { |
| 110 |
$out['content'] = []; |
| 111 |
foreach ( $raw['content'] as $lang => $lang_data ) { |
| 112 |
$lang = sanitize_text_field( (string) $lang ); |
| 113 |
if ( ! in_array( $lang, self::ALLOWED_LANGUAGES, true ) || ! is_array( $lang_data ) ) { |
| 114 |
continue; |
| 115 |
} |
| 116 |
$out['content'][ $lang ] = self::sanitize_locale_entry( $lang_data ); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return $out; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* @param array<string, mixed> $raw |
| 125 |
* @return array<string, mixed> |
| 126 |
*/ |
| 127 |
private static function sanitize_locale_entry( array $raw ): array { |
| 128 |
$out = []; |
| 129 |
if ( array_key_exists( 'name', $raw ) ) { |
| 130 |
$out['name'] = sanitize_text_field( (string) $raw['name'] ); |
| 131 |
} |
| 132 |
if ( isset( $raw['opt-in'] ) && is_array( $raw['opt-in'] ) ) { |
| 133 |
$out['opt-in'] = self::sanitize_opt_in_wire( $raw['opt-in'] ); |
| 134 |
} |
| 135 |
if ( isset( $raw['opt-out'] ) && is_array( $raw['opt-out'] ) ) { |
| 136 |
$out['opt-out'] = self::sanitize_opt_out_wire( $raw['opt-out'] ); |
| 137 |
} |
| 138 |
return $out; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* @param array<string, mixed> $raw |
| 143 |
* @return array<string, mixed> |
| 144 |
*/ |
| 145 |
private static function sanitize_opt_in_wire( array $raw ): array { |
| 146 |
$out = []; |
| 147 |
if ( array_key_exists( 'title', $raw ) ) { |
| 148 |
$out['title'] = sanitize_text_field( (string) $raw['title'] ); |
| 149 |
} |
| 150 |
if ( array_key_exists( 'cookie-message', $raw ) ) { |
| 151 |
$out['cookie-message'] = sanitize_text_field( (string) $raw['cookie-message'] ); |
| 152 |
} |
| 153 |
if ( isset( $raw['buttons'] ) && is_array( $raw['buttons'] ) ) { |
| 154 |
$out['buttons'] = self::sanitize_buttons( $raw['buttons'] ); |
| 155 |
} |
| 156 |
if ( isset( $raw['banner-options'] ) && is_array( $raw['banner-options'] ) ) { |
| 157 |
$out['banner-options'] = self::sanitize_banner_options( $raw['banner-options'] ); |
| 158 |
} |
| 159 |
if ( isset( $raw['preferences'] ) && is_array( $raw['preferences'] ) ) { |
| 160 |
$out['preferences'] = self::sanitize_preferences_wire( $raw['preferences'] ); |
| 161 |
} |
| 162 |
if ( isset( $raw['cookie-categories'] ) && is_array( $raw['cookie-categories'] ) ) { |
| 163 |
$out['cookie-categories'] = self::sanitize_cookie_categories_wire( $raw['cookie-categories'] ); |
| 164 |
} |
| 165 |
return $out; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* @param array<string, mixed> $raw |
| 170 |
* @return array<string, mixed> |
| 171 |
*/ |
| 172 |
private static function sanitize_opt_out_wire( array $raw ): array { |
| 173 |
$out = []; |
| 174 |
if ( array_key_exists( 'title', $raw ) ) { |
| 175 |
$out['title'] = sanitize_text_field( (string) $raw['title'] ); |
| 176 |
} |
| 177 |
if ( array_key_exists( 'cookie-message', $raw ) ) { |
| 178 |
$out['cookie-message'] = sanitize_text_field( (string) $raw['cookie-message'] ); |
| 179 |
} |
| 180 |
if ( isset( $raw['buttons'] ) && is_array( $raw['buttons'] ) ) { |
| 181 |
$out['buttons'] = self::sanitize_buttons( $raw['buttons'] ); |
| 182 |
} |
| 183 |
if ( isset( $raw['banner-options'] ) && is_array( $raw['banner-options'] ) ) { |
| 184 |
$out['banner-options'] = self::sanitize_banner_options( $raw['banner-options'] ); |
| 185 |
} |
| 186 |
if ( isset( $raw['opt-out-dialog'] ) && is_array( $raw['opt-out-dialog'] ) ) { |
| 187 |
$out['opt-out-dialog'] = self::sanitize_opt_out_dialog_wire( $raw['opt-out-dialog'] ); |
| 188 |
} |
| 189 |
return $out; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* @param array<string, mixed> $raw |
| 194 |
* @return array<string, mixed> |
| 195 |
*/ |
| 196 |
private static function sanitize_buttons( array $raw ): array { |
| 197 |
$out = []; |
| 198 |
foreach ( [ 'accept-all', 'deny', 'customize' ] as $key ) { |
| 199 |
if ( array_key_exists( $key, $raw ) ) { |
| 200 |
$out[ $key ] = sanitize_text_field( (string) $raw[ $key ] ); |
| 201 |
} |
| 202 |
} |
| 203 |
return $out; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* @param array<string, mixed> $raw |
| 208 |
* @return array<string, mixed> |
| 209 |
*/ |
| 210 |
private static function sanitize_banner_options( array $raw ): array { |
| 211 |
$out = []; |
| 212 |
$bools = [ 'cookie-policy-link', 'privacy-policy-link', 'do-not-sell' ]; |
| 213 |
foreach ( $bools as $key ) { |
| 214 |
if ( array_key_exists( $key, $raw ) ) { |
| 215 |
$out[ $key ] = (bool) $raw[ $key ]; |
| 216 |
} |
| 217 |
} |
| 218 |
$texts = [ 'cookie-policy-link-text', 'privacy-policy-link-text', 'do-not-sell-link-text' ]; |
| 219 |
foreach ( $texts as $key ) { |
| 220 |
if ( array_key_exists( $key, $raw ) ) { |
| 221 |
$out[ $key ] = sanitize_text_field( (string) $raw[ $key ] ); |
| 222 |
} |
| 223 |
} |
| 224 |
$urls = [ 'cookie-policy-url', 'privacy-policy-url' ]; |
| 225 |
foreach ( $urls as $key ) { |
| 226 |
if ( array_key_exists( $key, $raw ) ) { |
| 227 |
$out[ $key ] = esc_url_raw( (string) $raw[ $key ] ); |
| 228 |
} |
| 229 |
} |
| 230 |
return $out; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* @param array<string, mixed> $raw |
| 235 |
* @return array<string, mixed> |
| 236 |
*/ |
| 237 |
private static function sanitize_preferences_wire( array $raw ): array { |
| 238 |
$out = []; |
| 239 |
foreach ( [ 'header', 'privacy-overview', 'accept-button', 'deny-button', 'save-preferences' ] as $key ) { |
| 240 |
if ( array_key_exists( $key, $raw ) ) { |
| 241 |
$out[ $key ] = sanitize_text_field( (string) $raw[ $key ] ); |
| 242 |
} |
| 243 |
} |
| 244 |
return $out; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* @param array<string, mixed> $raw |
| 249 |
* @return array<string, mixed> |
| 250 |
*/ |
| 251 |
private static function sanitize_opt_out_dialog_wire( array $raw ): array { |
| 252 |
$out = []; |
| 253 |
foreach ( [ 'header', 'privacy-overview', 'consent-label', 'deny-button', 'save-preferences' ] as $key ) { |
| 254 |
if ( array_key_exists( $key, $raw ) ) { |
| 255 |
$out[ $key ] = sanitize_text_field( (string) $raw[ $key ] ); |
| 256 |
} |
| 257 |
} |
| 258 |
return $out; |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* @param array<string, mixed> $raw |
| 263 |
* @return array<string, mixed> |
| 264 |
*/ |
| 265 |
private static function sanitize_cookie_categories_wire( array $raw ): array { |
| 266 |
$out = []; |
| 267 |
foreach ( self::CATEGORY_KEYS as $key ) { |
| 268 |
if ( ! isset( $raw[ $key ] ) || ! is_array( $raw[ $key ] ) ) { |
| 269 |
continue; |
| 270 |
} |
| 271 |
$cat = $raw[ $key ]; |
| 272 |
$out[ $key ] = []; |
| 273 |
if ( array_key_exists( 'title', $cat ) ) { |
| 274 |
$out[ $key ]['title'] = sanitize_text_field( (string) $cat['title'] ); |
| 275 |
} |
| 276 |
if ( array_key_exists( 'description', $cat ) ) { |
| 277 |
$out[ $key ]['description'] = sanitize_text_field( (string) $cat['description'] ); |
| 278 |
} |
| 279 |
} |
| 280 |
return $out; |
| 281 |
} |
| 282 |
} |
| 283 |
|