Upgrade
1 week ago
CLIService.php
1 week ago
FeedCacheUpdateService.php
1 week ago
MigrationReactivationNotice.php
1 week ago
SBR_Upgrader.php
1 week ago
SettingsManagerService.php
1 week ago
ShortcodeService.php
1 week ago
SiteUrlWatcher.php
1 week ago
SettingsManagerService.php
108 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 | // Legacy interim key; consent now lives in the 'usagetracking' key. |
| 58 | unset($updated_settings['smash_usage_tracking']); |
| 59 | |
| 60 | $result = update_option($this->settings_options, $updated_settings); |
| 61 | |
| 62 | // Schedule/unschedule the usage tracking cron based on consent. |
| 63 | if (array_key_exists('usagetracking', $updated_settings)) { |
| 64 | $scheduler = new \SmashBalloon\Reviews\Common\UsageTracking\Core\Scheduler(); |
| 65 | if (! empty($updated_settings['usagetracking'])) { |
| 66 | $scheduler->schedule(); |
| 67 | } else { |
| 68 | $scheduler->unschedule(); |
| 69 | // Opting out must not be weaker than uninstalling: drop the |
| 70 | // already-collected data and the site token, or they would be |
| 71 | // retained indefinitely and transmitted after a later re-opt-in. |
| 72 | \SmashBalloon\Reviews\Common\UsageTracking\SmashUsageTracking::purge_stored_data(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return $result; |
| 77 | } |
| 78 | |
| 79 | public function get_settings(): array |
| 80 | { |
| 81 | $settings = get_option($this->settings_options, []); |
| 82 | // Return type is `array` — coerce corrupted non-array state to [] to |
| 83 | // avoid a TypeError and let SMASH-1281's recovery flow re-register. |
| 84 | return is_array($settings) ? $settings : []; |
| 85 | } |
| 86 | |
| 87 | public function sanitize_settings($data) |
| 88 | { |
| 89 | $sanitized_and_sorted = []; |
| 90 | |
| 91 | foreach ($data as $key => $value) { |
| 92 | $data_type = SBR_Feed_Saver_Manager::get_data_type($key); |
| 93 | $sanitized_values = array(); |
| 94 | if (is_array($value)) { |
| 95 | $sanitized_values[] = $value; |
| 96 | } elseif (is_object($value)) { |
| 97 | $sanitized_values[] = $value; |
| 98 | } else { |
| 99 | $type = SBR_Feed_Saver_Manager::is_boolean($value) ? 'boolean' : $data_type['sanitization']; |
| 100 | $sanitized_values = SBR_Feed_Saver_Manager::sanitize($type, $value); |
| 101 | } |
| 102 | |
| 103 | $sanitized_and_sorted[$key] = $sanitized_values; |
| 104 | } |
| 105 | return $sanitized_and_sorted; |
| 106 | } |
| 107 | } |
| 108 |