| 1 |
<?php |
| 2 |
/** |
| 3 |
* Redis REST Controller |
| 4 |
* |
| 5 |
* Handles GET /redis-status and POST /redis-flush endpoints. |
| 6 |
* |
| 7 |
* @package Upress\EzCache |
| 8 |
*/ |
| 9 |
namespace Upress\EzCache\Rest; |
| 10 |
|
| 11 |
use Upress\EzCache\RedisObjectCache; |
| 12 |
use Upress\EzCache\Settings; |
| 13 |
use Upress\EzCache\PremiumFeatures; |
| 14 |
use WP_REST_Request; |
| 15 |
|
| 16 |
class RedisController { |
| 17 |
|
| 18 |
/** |
| 19 |
* GET /redis-status |
| 20 |
* Returns Redis connection status, memory, hit rate, and key count. |
| 21 |
*/ |
| 22 |
public function status() { |
| 23 |
$status = RedisObjectCache::get_status(); |
| 24 |
return wp_send_json_success( $status ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* POST /redis-flush |
| 29 |
* Flushes all ezcache:* keys from Redis. |
| 30 |
*/ |
| 31 |
public function flush() { |
| 32 |
if ( ! PremiumFeatures::is_premium() ) { |
| 33 |
return wp_send_json_error( [ 'message' => 'Premium required' ], 403 ); |
| 34 |
} |
| 35 |
|
| 36 |
$result = RedisObjectCache::flush(); |
| 37 |
if ( $result ) { |
| 38 |
return wp_send_json_success( [ 'message' => 'Redis cache flushed' ] ); |
| 39 |
} |
| 40 |
return wp_send_json_error( [ 'message' => 'Failed to flush Redis — is it running?' ] ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* POST /redis-toggle |
| 45 |
* Enable or disable the Redis object cache (deploys / removes drop-in). |
| 46 |
*/ |
| 47 |
public function toggle( WP_REST_Request $request ) { |
| 48 |
if ( ! PremiumFeatures::is_premium() ) { |
| 49 |
return wp_send_json_error( [ 'message' => 'Premium required' ], 403 ); |
| 50 |
} |
| 51 |
|
| 52 |
$params = (array) $request->get_json_params(); |
| 53 |
$enable = ! empty( $params['enable'] ); |
| 54 |
$fullpage = ! empty( $params['enable_fullpage'] ); |
| 55 |
|
| 56 |
Settings::set_settings( [ |
| 57 |
'enable_redis_object_cache' => $enable, |
| 58 |
'enable_redis_fullpage' => $fullpage, |
| 59 |
] ); |
| 60 |
|
| 61 |
if ( $enable ) { |
| 62 |
$deployed = RedisObjectCache::maybe_deploy_dropin(); |
| 63 |
if ( ! $deployed && ! RedisObjectCache::is_our_dropin() ) { |
| 64 |
return wp_send_json_error( [ |
| 65 |
'message' => 'A foreign object-cache.php already exists. Remove it first.', |
| 66 |
] ); |
| 67 |
} |
| 68 |
} else { |
| 69 |
RedisObjectCache::remove_dropin(); |
| 70 |
} |
| 71 |
|
| 72 |
return wp_send_json_success( [ |
| 73 |
'enabled' => $enable, |
| 74 |
'status' => RedisObjectCache::get_status(), |
| 75 |
] ); |
| 76 |
} |
| 77 |
} |
| 78 |
|