| 1 |
<?php |
| 2 |
|
| 3 |
namespace ReviewX\Rest\Controllers; |
| 4 |
|
| 5 |
use ReviewX\Services\CacheServices; |
| 6 |
use WP_REST_Request; |
| 7 |
use WP_REST_Response; |
| 8 |
use ReviewX\Utilities\Helper; |
| 9 |
class CacheController |
| 10 |
{ |
| 11 |
protected CacheServices $cacheService; |
| 12 |
public function __construct() |
| 13 |
{ |
| 14 |
$this->cacheService = new CacheServices(); |
| 15 |
} |
| 16 |
public function clearAdminCache(WP_REST_Request $request) : WP_REST_Response |
| 17 |
{ |
| 18 |
$this->cacheService->removeCache(); |
| 19 |
return Helper::rest(['success' => \true])->success('Admin API cache cleared successfully.'); |
| 20 |
} |
| 21 |
public function clearProductCache(WP_REST_Request $request) : WP_REST_Response |
| 22 |
{ |
| 23 |
$productId = $request->get_param('product_id'); |
| 24 |
if ($productId === 'all') { |
| 25 |
global $wpdb; |
| 26 |
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_rvx_%_latest_reviews%'"); |
| 27 |
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_timeout_rvx_%_latest_reviews%'"); |
| 28 |
return Helper::rest(['success' => \true])->success('All product caches cleared.'); |
| 29 |
} |
| 30 |
if ($productId) { |
| 31 |
$this->cacheService->removeProductCache($productId); |
| 32 |
return Helper::rest(['success' => \true])->success('Product cache cleared.'); |
| 33 |
} |
| 34 |
return Helper::rest(['success' => \false])->fails('Product ID is required.'); |
| 35 |
} |
| 36 |
public function clearShortcodeCache(WP_REST_Request $request) : WP_REST_Response |
| 37 |
{ |
| 38 |
$type = $request->get_param('type'); |
| 39 |
if ($type === 'all') { |
| 40 |
\delete_transient('rvx_review_shortcode'); |
| 41 |
\delete_transient('rvx_shortcode_transient'); |
| 42 |
\delete_transient('rvx_shortcode_all_reviews'); |
| 43 |
global $wpdb; |
| 44 |
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_rvx_google_reviews_cache_%'"); |
| 45 |
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_timeout_rvx_google_reviews_cache_%'"); |
| 46 |
return Helper::rest(['success' => \true])->success('All shortcode caches cleared.'); |
| 47 |
} |
| 48 |
if ($type) { |
| 49 |
if ($type === 'google_review_list') { |
| 50 |
global $wpdb; |
| 51 |
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_rvx_google_reviews_cache_%'"); |
| 52 |
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_timeout_rvx_google_reviews_cache_%'"); |
| 53 |
} else { |
| 54 |
\delete_transient('rvx_review_shortcode'); |
| 55 |
\delete_transient('rvx_shortcode_transient'); |
| 56 |
\delete_transient('rvx_shortcode_all_reviews'); |
| 57 |
} |
| 58 |
return Helper::rest(['success' => \true])->success('Specific shortcode cache cleared.'); |
| 59 |
} |
| 60 |
return Helper::rest(['success' => \false])->fails('Type is required.'); |
| 61 |
} |
| 62 |
} |
| 63 |
|