Backoffice
1 month ago
build_widget_data_attributes.php
1 month ago
get_widget_settings.php
1 month ago
load_script_file.php
1 month ago
load_script_template_file.php
1 month ago
load_translations.php
1 year ago
register_shortcode.php
1 month ago
sanitize_restaurant_id.php
1 month ago
sanitize_widget_settings.php
1 month ago
sanitize_widget_settings.php
87 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Zenchef\Widget\Widget; |
| 4 | |
| 5 | use function in_array; |
| 6 | use function max; |
| 7 | use function preg_match; |
| 8 | use function trim; |
| 9 | |
| 10 | const SUPPORTED_LANGUAGES = ['en', 'es', 'it', 'de', 'fr', 'pt', 'nl']; |
| 11 | const SUPPORTED_POSITIONS = ['left', 'center', 'right']; |
| 12 | |
| 13 | /** |
| 14 | * Normalises a language code: accepts only SDK-supported values, anything else |
| 15 | * is treated as "no preference" (empty). |
| 16 | * |
| 17 | * @param mixed $value |
| 18 | * @return string |
| 19 | */ |
| 20 | function sanitize_language($value) |
| 21 | { |
| 22 | $trimmed = trim((string) $value); |
| 23 | |
| 24 | if ($trimmed === '' || !in_array($trimmed, SUPPORTED_LANGUAGES, true)) { |
| 25 | return ''; |
| 26 | } |
| 27 | |
| 28 | return $trimmed; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Validates a 6-digit hex color. Empty or invalid returns ''. |
| 33 | * |
| 34 | * @param mixed $value |
| 35 | * @return string |
| 36 | */ |
| 37 | function sanitize_primary_color($value) |
| 38 | { |
| 39 | $trimmed = trim((string) $value); |
| 40 | |
| 41 | if ($trimmed === '' || preg_match('/^#[A-Fa-f0-9]{6}$/', $trimmed) !== 1) { |
| 42 | return ''; |
| 43 | } |
| 44 | |
| 45 | return $trimmed; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Whitelists position; defaults to 'right' when invalid (matches SDK default). |
| 50 | * |
| 51 | * @param mixed $value |
| 52 | * @return string |
| 53 | */ |
| 54 | function sanitize_position($value) |
| 55 | { |
| 56 | $trimmed = trim((string) $value); |
| 57 | |
| 58 | if (!in_array($trimmed, SUPPORTED_POSITIONS, true)) { |
| 59 | return 'right'; |
| 60 | } |
| 61 | |
| 62 | return $trimmed; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Coerces a boolean-ish value to '1' (true) or '0' (false). Unchecked checkboxes |
| 67 | * are absent from $_POST so missing/empty input becomes '0'. |
| 68 | * |
| 69 | * @param mixed $value |
| 70 | * @return string |
| 71 | */ |
| 72 | function sanitize_boolean($value) |
| 73 | { |
| 74 | return !empty($value) && $value !== '0' ? '1' : '0'; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Casts an open delay to a non-negative integer. |
| 79 | * |
| 80 | * @param mixed $value |
| 81 | * @return int |
| 82 | */ |
| 83 | function sanitize_open_delay($value) |
| 84 | { |
| 85 | return max(0, (int) $value); |
| 86 | } |
| 87 |