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
SiteUrlWatcher.php
76 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SmashBalloon\Reviews\Common\Services; |
| 4 | |
| 5 | use Smashballoon\Stubs\Services\ServiceProvider; |
| 6 | |
| 7 | /** |
| 8 | * Keeps `sbr_settings['website_url']` in sync with WordPress's own `home` / |
| 9 | * `siteurl` options when the admin legitimately changes the site URL. |
| 10 | * |
| 11 | * **Why this is necessary:** |
| 12 | * |
| 13 | * `SBRelay::detect_site_migration()` (SMASH-1281) compares `get_home_url()` |
| 14 | * against the URL stored at registration. Without this watcher, any admin- |
| 15 | * initiated URL change (HTTP→HTTPS rollout, `example.com` → `www.example.com`, |
| 16 | * domain rebrand) would look indistinguishable from a site migration and |
| 17 | * trigger a full state wipe — punishing customers for doing legitimate WP |
| 18 | * admin work. |
| 19 | * |
| 20 | * The distinction rides on HOW the URL changed: |
| 21 | * |
| 22 | * - Admin edits General Settings → `update_option('home', …)` fires → |
| 23 | * this watcher updates `sbr_settings['website_url']` in lock-step → |
| 24 | * the detect check sees matching URLs → no wipe. � |
| 25 | |
| 26 | * |
| 27 | * - External DB copy / WP Engine push → raw INSERT/UPDATE on `wp_options` |
| 28 | * by a different process → no WP hook fires → `sbr_settings['website_url']` |
| 29 | * stays at the original value → detect check sees a mismatch → wipe. � |
| 30 | |
| 31 | * |
| 32 | * That asymmetry is exactly the signal we want. |
| 33 | * |
| 34 | * @see SMASH-1281 |
| 35 | */ |
| 36 | class SiteUrlWatcher extends ServiceProvider |
| 37 | { |
| 38 | public function register() |
| 39 | { |
| 40 | if (!function_exists('add_action')) { |
| 41 | return; |
| 42 | } |
| 43 | add_action('update_option_home', [$this, 'on_site_url_changed'], 10, 3); |
| 44 | add_action('update_option_siteurl', [$this, 'on_site_url_changed'], 10, 3); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Fired when WP's `home` or `siteurl` option is legitimately updated. |
| 49 | * Refreshes `sbr_settings['website_url']` so the proactive migration |
| 50 | * detection doesn't spuriously fire on the next request. |
| 51 | * |
| 52 | * Only acts if the plugin has already registered (access_token present). |
| 53 | * For unregistered installs there's nothing to sync — the URL will be |
| 54 | * captured fresh on first registration. |
| 55 | * |
| 56 | * Signature matches WP's `update_option_{$option}` hook: ($old, $new, $option). |
| 57 | * |
| 58 | * @param mixed $old_value |
| 59 | * @param mixed $new_value |
| 60 | * @param string $option |
| 61 | */ |
| 62 | public function on_site_url_changed($old_value, $new_value, $option = '') |
| 63 | { |
| 64 | $settings = get_option('sbr_settings', []); |
| 65 | if (!is_array($settings) || empty($settings['access_token'])) { |
| 66 | return; |
| 67 | } |
| 68 | $home = get_home_url(); |
| 69 | if (empty($home)) { |
| 70 | return; |
| 71 | } |
| 72 | $settings['website_url'] = $home; |
| 73 | update_option('sbr_settings', $settings); |
| 74 | } |
| 75 | } |
| 76 |