add_settings_page.php
1 month ago
load_settings_page_assets.php
1 month ago
register_settings.php
1 month ago
remove_settings.php
1 year ago
register_settings.php
80 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Zenchef\Widget\Widget\Backoffice; |
| 4 | |
| 5 | use function _x; |
| 6 | use function add_settings_error; |
| 7 | use function get_option; |
| 8 | use function is_array; |
| 9 | use function register_setting; |
| 10 | use function Zenchef\Widget\Widget\sanitize_boolean; |
| 11 | use function Zenchef\Widget\Widget\sanitize_language; |
| 12 | use function Zenchef\Widget\Widget\sanitize_open_delay; |
| 13 | use function Zenchef\Widget\Widget\sanitize_position; |
| 14 | use function Zenchef\Widget\Widget\sanitize_primary_color; |
| 15 | use function Zenchef\Widget\Widget\sanitize_restaurant_id; |
| 16 | use const Zenchef\Widget\ROOT_PATH; |
| 17 | use const Zenchef\Widget\SETTINGS_GROUP_SLUG; |
| 18 | use const Zenchef\Widget\SETTINGS_OPTION_NAME; |
| 19 | |
| 20 | require_once ROOT_PATH . 'src/Widget/sanitize_restaurant_id.php'; |
| 21 | require_once ROOT_PATH . 'src/Widget/sanitize_widget_settings.php'; |
| 22 | |
| 23 | /** |
| 24 | * @return void |
| 25 | */ |
| 26 | function register_settings() |
| 27 | { |
| 28 | register_setting(SETTINGS_GROUP_SLUG, SETTINGS_OPTION_NAME, [ |
| 29 | 'sanitize_callback' => __NAMESPACE__ . '\\sanitize_settings', |
| 30 | ]); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Sanitization callback for the plugin's option array. |
| 35 | * |
| 36 | * @param mixed $input |
| 37 | * @return array |
| 38 | */ |
| 39 | function sanitize_settings($input) |
| 40 | { |
| 41 | $previous = get_option(SETTINGS_OPTION_NAME, []); |
| 42 | if (!is_array($previous)) { |
| 43 | $previous = []; |
| 44 | } |
| 45 | if (!is_array($input)) { |
| 46 | return $previous; |
| 47 | } |
| 48 | |
| 49 | $sanitized = $previous; |
| 50 | |
| 51 | $sanitized_restaurant_id = sanitize_restaurant_id($input['restaurant_id'] ?? ''); |
| 52 | if ($sanitized_restaurant_id === null) { |
| 53 | add_settings_error( |
| 54 | SETTINGS_OPTION_NAME, |
| 55 | 'invalid_restaurant_id', |
| 56 | _x( |
| 57 | 'Invalid restaurant ID. Please copy the ID exactly as it appears in your Zenchef dashboard.', |
| 58 | 'widget.backoffice.settings_page.invalid_restaurant_id_error', |
| 59 | 'zenchef-widget-integration' |
| 60 | ), |
| 61 | 'error' |
| 62 | ); |
| 63 | $sanitized['restaurant_id'] = $previous['restaurant_id'] ?? ''; |
| 64 | } else { |
| 65 | $sanitized['restaurant_id'] = $sanitized_restaurant_id; |
| 66 | } |
| 67 | |
| 68 | $sanitized['language'] = sanitize_language($input['language'] ?? ''); |
| 69 | $sanitized['primary_color'] = sanitize_primary_color($input['primary_color'] ?? ''); |
| 70 | $sanitized['position'] = sanitize_position($input['position'] ?? 'right'); |
| 71 | $sanitized['open_delay'] = sanitize_open_delay($input['open_delay'] ?? 3000); |
| 72 | $sanitized['use_default_color'] = sanitize_boolean($input['use_default_color'] ?? null); |
| 73 | $sanitized['auto_open'] = sanitize_boolean($input['auto_open'] ?? null); |
| 74 | $sanitized['hide_button'] = sanitize_boolean($input['hide_button'] ?? null); |
| 75 | $sanitized['disable_gtm'] = sanitize_boolean($input['disable_gtm'] ?? null); |
| 76 | $sanitized['disable_ga4'] = sanitize_boolean($input['disable_ga4'] ?? null); |
| 77 | |
| 78 | return $sanitized; |
| 79 | } |
| 80 |