Admin
2 months ago
Builder
2 months ago
Customizer
2 months ago
Exceptions
2 months ago
Helpers
2 months ago
Integrations
2 months ago
Migrations
2 months ago
ReviewAlerts
2 months ago
Services
2 months ago
Settings
2 months ago
Support
2 months ago
Traits
2 months ago
Utils
2 months ago
AuthorizationStatusCheck.php
2 months ago
BusinessDataCache.php
2 months ago
Clear_Cache.php
2 months ago
Container.php
2 months ago
DisplayElements.php
2 months ago
Email_Notification.php
2 months ago
Error_Reporter.php
2 months ago
Feed.php
2 months ago
FeedCache.php
2 months ago
FeedCacheUpdater.php
2 months ago
FeedDisplay.php
2 months ago
Feed_Locator.php
2 months ago
Parser.php
2 months ago
PostAggregator.php
2 months ago
RemoteRequest.php
2 months ago
SBR_Education.php
2 months ago
SBR_Settings.php
2 months ago
ServiceContainer.php
2 months ago
SinglePostCache.php
2 months ago
TemplateRenderer.php
2 months ago
Tooltip_Wizard.php
2 months ago
Util.php
2 months ago
Clear_Cache.php
190 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Clear Cache data in the DB |
| 5 | */ |
| 6 | |
| 7 | namespace SmashBalloon\Reviews\Common; |
| 8 | |
| 9 | if (! defined('ABSPATH')) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | use SmashBalloon\Reviews\Pro\Services\BulkUpdate\Bulk_External_Reviews_Update; |
| 14 | use SmashBalloon\Reviews\Pro\Services\BulkUpdate\Bulk_Reviews_Update; |
| 15 | use SmashBalloon\Reviews\Pro\Services\BulkUpdate\Bulk_WooCommerce_Reviews_Update; |
| 16 | use Smashballoon\Stubs\Services\ServiceProvider; |
| 17 | |
| 18 | class Clear_Cache extends ServiceProvider |
| 19 | { |
| 20 | public function register() |
| 21 | { |
| 22 | add_action('wp_ajax_sbr_clear_post_header_cache', [ $this , 'sbr_clear_post_header_cache' ]); |
| 23 | add_action('wp_ajax_sbr_reset_posts', [ $this, 'sbr_reset_posts' ]); |
| 24 | add_action('wp_ajax_sbr_reset_local_images', [ $this, 'sbr_reset_local_images' ]); |
| 25 | } |
| 26 | |
| 27 | private static function clear_data($table_name, $column, $where_clause) |
| 28 | { |
| 29 | global $wpdb; |
| 30 | $table_full_name = $wpdb->prefix . $table_name; |
| 31 | |
| 32 | // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared -- Table name and column are hardcoded internal values |
| 33 | $sql = " |
| 34 | UPDATE $table_full_name |
| 35 | SET $column = '' |
| 36 | $where_clause"; |
| 37 | $wpdb->query($sql); |
| 38 | // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared |
| 39 | } |
| 40 | |
| 41 | private static function drop_table($table_name) |
| 42 | { |
| 43 | global $wpdb; |
| 44 | $table_full_name = $wpdb->prefix . $table_name; |
| 45 | |
| 46 | // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared -- Table name is hardcoded internal value |
| 47 | $sql = "DROP TABLE IF EXISTS $table_full_name"; |
| 48 | $wpdb->query($sql); |
| 49 | // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared |
| 50 | } |
| 51 | |
| 52 | public function sbr_clear_post_header_cache() |
| 53 | { |
| 54 | check_ajax_referer('sbr-admin', 'nonce'); |
| 55 | if (!sbr_current_user_can('manage_reviews_feed_options')) { |
| 56 | wp_send_json_error(); |
| 57 | } |
| 58 | |
| 59 | $this->clear_post_header_cache(); |
| 60 | |
| 61 | // Reset bulk update states and schedule re-fetching of reviews |
| 62 | $this->reset_bulk_update_states(); |
| 63 | |
| 64 | wp_send_json(['success' => true], 200); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Reset bulk update states for all providers |
| 69 | * |
| 70 | * This triggers a full resync of reviews from all sources: |
| 71 | * - Google + Yelp: Drop the per-source bulk-history option so the |
| 72 | * "Unable to retrieve reviews history" warning clears, and reschedule |
| 73 | * bulk-history for any source whose provider has an API key configured |
| 74 | * - External providers (Airbnb, Booking, AliExpress): Re-fetch from relay API |
| 75 | * - WooCommerce: Resync from wp_comments table |
| 76 | * |
| 77 | * WooCommerce is included to provide users a "nuclear option" for full refresh. |
| 78 | * For ongoing changes, event-driven hooks handle individual review updates. |
| 79 | * |
| 80 | * @see documentation/WOOCOMMERCE_EVENT_DRIVEN_CACHE_ARCHITECTURE.md |
| 81 | * |
| 82 | * @since 2.3.0 |
| 83 | */ |
| 84 | private function reset_bulk_update_states(): void |
| 85 | { |
| 86 | // Google + Yelp: drop the per-source bulk-history state so the source |
| 87 | // list warning clears, then reschedule bulk-history for any provider |
| 88 | // with an API key configured. Same gating as BulkHistoryRoutine — keyless |
| 89 | // customers stay keyless, but they no longer see a permanent warning |
| 90 | // rooted in a stale option (regression-pin: pre-fix Bulk_Reviews_Update |
| 91 | // could leave entries at {retry: true, is_done: false} indefinitely with |
| 92 | // no UI path to clear them). |
| 93 | if (Util::sbr_is_pro() && class_exists(Bulk_Reviews_Update::class)) { |
| 94 | delete_option('sbr_bulk_sources'); |
| 95 | // Clear any in-flight bulk-cron events before rescheduling so a |
| 96 | // repeated "Clear All Caches" click doesn't enqueue a backlog. |
| 97 | // schedule_task uses wp_schedule_single_event which dedupes within |
| 98 | // 10 minutes for identical args, but Clear All Caches is on a manual |
| 99 | // click cadence — defensive cleanup is the safer contract. |
| 100 | if (function_exists('wp_clear_scheduled_hook')) { |
| 101 | wp_clear_scheduled_hook('sbr_reviews_bulk_cron'); |
| 102 | } |
| 103 | Bulk_Reviews_Update::schedule_needed_sources_history(); |
| 104 | } |
| 105 | |
| 106 | // External providers (Airbnb, Booking, AliExpress): Reset and schedule background fetch |
| 107 | if (Util::sbr_is_pro() && class_exists(Bulk_External_Reviews_Update::class)) { |
| 108 | Bulk_External_Reviews_Update::reset_all_sources(true); |
| 109 | } |
| 110 | |
| 111 | // WooCommerce: Resync all reviews from wp_comments table |
| 112 | // This provides users a way to force a full refresh if needed |
| 113 | if (Util::sbr_is_pro() && class_exists(Bulk_WooCommerce_Reviews_Update::class)) { |
| 114 | Bulk_WooCommerce_Reviews_Update::reset_all_sources(true); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public function sbr_reset_posts() |
| 119 | { |
| 120 | check_ajax_referer('sbr-admin', 'nonce'); |
| 121 | |
| 122 | if (!sbr_current_user_can('manage_reviews_feed_options')) { |
| 123 | wp_send_json_error(); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | SinglePostCache::delete_resizing_table_and_images(); |
| 128 | SinglePostCache::create_resizing_table_and_uploads_folder(); |
| 129 | $this->sbr_clear_post_header_cache(); |
| 130 | wp_send_json(['success' => true], 200); |
| 131 | } |
| 132 | |
| 133 | public function clear_post_header_cache() |
| 134 | { |
| 135 | self::clear_data( |
| 136 | "sbr_feed_caches", |
| 137 | "cache_value", |
| 138 | "WHERE cache_key NOT IN ( 'posts_backup', 'header_backup' );" |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | public static function clear_feed_caches_by_id($feeds_ids) |
| 144 | { |
| 145 | global $wpdb; |
| 146 | $cache_table_name = $wpdb->prefix . 'sbr_feed_caches'; |
| 147 | $feeds_ids_string = "'" . implode('\', \'', $feeds_ids) . "'"; |
| 148 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is prefixed, feed_ids are sanitized internally |
| 149 | $wpdb->query( |
| 150 | $wpdb->prepare( |
| 151 | "UPDATE $cache_table_name |
| 152 | SET cache_value = '' |
| 153 | WHERE cache_key NOT IN ( 'posts_backup', 'header_backup' ) |
| 154 | AND feed_id IN ($feeds_ids_string) |
| 155 | " |
| 156 | ) |
| 157 | ); |
| 158 | // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 159 | } |
| 160 | |
| 161 | |
| 162 | public function reset_images() |
| 163 | { |
| 164 | global $wpdb; |
| 165 | $posts_table_name = $wpdb->prefix . 'sbr_reviews_posts'; |
| 166 | // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is prefixed constant |
| 167 | $wpdb->query("UPDATE $posts_table_name SET images_done = 0"); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | public function sbr_reset_local_images() |
| 172 | { |
| 173 | check_ajax_referer('sbr-admin', 'nonce'); |
| 174 | if (!sbr_current_user_can('manage_reviews_feed_options')) { |
| 175 | wp_send_json_error(); |
| 176 | } |
| 177 | |
| 178 | SinglePostCache::delete_local_images(); |
| 179 | $this->reset_images(); |
| 180 | $this->clear_post_header_cache(); |
| 181 | wp_send_json( |
| 182 | [ |
| 183 | 'success' => true, |
| 184 | 'message' => __('Local images cleared', 'reviews-feed') |
| 185 | ], |
| 186 | 200 |
| 187 | ); |
| 188 | } |
| 189 | } |
| 190 |