Upgrade
1 month ago
CLIService.php
1 month ago
FeedCacheUpdateService.php
1 month ago
MigrationReactivationNotice.php
1 month ago
SBR_Upgrader.php
1 month ago
SettingsManagerService.php
1 month ago
ShortcodeService.php
1 month ago
SiteUrlWatcher.php
1 month ago
UsageTrackingService.php
1 month ago
SettingsManagerService.php
89 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Settings Manager Service |
| 5 | */ |
| 6 | |
| 7 | namespace SmashBalloon\Reviews\Common\Services; |
| 8 | |
| 9 | if (! defined('ABSPATH')) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | use SmashBalloon\Reviews\Common\Builder\SBR_Feed_Saver_Manager; |
| 14 | use SmashBalloon\Reviews\Common\Utils\AjaxUtil; |
| 15 | use Smashballoon\Stubs\Services\ServiceProvider; |
| 16 | |
| 17 | class SettingsManagerService extends ServiceProvider |
| 18 | { |
| 19 | private $settings_options = 'sbr_settings'; |
| 20 | |
| 21 | public function register(): void |
| 22 | { |
| 23 | add_action('wp_ajax_sbr_update_settings', [$this, 'ajax_update_settings']); |
| 24 | } |
| 25 | |
| 26 | public function ajax_update_settings(): void |
| 27 | { |
| 28 | check_ajax_referer('sbr-admin', 'nonce'); |
| 29 | |
| 30 | if (!sbr_current_user_can('manage_reviews_feed_options')) { |
| 31 | wp_send_json_error(); |
| 32 | } |
| 33 | |
| 34 | unset($_POST['nonce'], $_POST['action']); |
| 35 | |
| 36 | $settings = json_decode(stripslashes($_POST['settings']), true); |
| 37 | |
| 38 | $update_array = $this->sanitize_settings($settings) ; |
| 39 | |
| 40 | foreach ($settings['translations'] as $key => $value) { |
| 41 | $update_array['translations'][$key] = sanitize_text_field($value); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | if ($this->update_settings($update_array)) { |
| 46 | wp_send_json_success(); |
| 47 | } else { |
| 48 | wp_send_json_error(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | public function update_settings($settings): bool |
| 53 | { |
| 54 | $current_settings = $this->get_settings(); |
| 55 | $updated_settings = array_merge($current_settings, $settings); |
| 56 | |
| 57 | return update_option($this->settings_options, $updated_settings); |
| 58 | } |
| 59 | |
| 60 | public function get_settings(): array |
| 61 | { |
| 62 | $settings = get_option($this->settings_options, []); |
| 63 | // Return type is `array` — coerce corrupted non-array state to [] to |
| 64 | // avoid a TypeError and let SMASH-1281's recovery flow re-register. |
| 65 | return is_array($settings) ? $settings : []; |
| 66 | } |
| 67 | |
| 68 | public function sanitize_settings($data) |
| 69 | { |
| 70 | $sanitized_and_sorted = []; |
| 71 | |
| 72 | foreach ($data as $key => $value) { |
| 73 | $data_type = SBR_Feed_Saver_Manager::get_data_type($key); |
| 74 | $sanitized_values = array(); |
| 75 | if (is_array($value)) { |
| 76 | $sanitized_values[] = $value; |
| 77 | } elseif (is_object($value)) { |
| 78 | $sanitized_values[] = $value; |
| 79 | } else { |
| 80 | $type = SBR_Feed_Saver_Manager::is_boolean($value) ? 'boolean' : $data_type['sanitization']; |
| 81 | $sanitized_values = SBR_Feed_Saver_Manager::sanitize($type, $value); |
| 82 | } |
| 83 | |
| 84 | $sanitized_and_sorted[$key] = $sanitized_values; |
| 85 | } |
| 86 | return $sanitized_and_sorted; |
| 87 | } |
| 88 | } |
| 89 |