| 1 |
<?php |
| 2 |
|
| 3 |
namespace ReviewX\Rest\Controllers; |
| 4 |
|
| 5 |
\defined("ABSPATH") || exit; |
| 6 |
use Exception; |
| 7 |
use WP_REST_Response; |
| 8 |
use ReviewX\Services\PingService; |
| 9 |
use ReviewX\Utilities\Helper; |
| 10 |
class PingController |
| 11 |
{ |
| 12 |
protected PingService $pingService; |
| 13 |
public function __construct() |
| 14 |
{ |
| 15 |
$this->pingService = new PingService(); |
| 16 |
} |
| 17 |
/** |
| 18 |
* Ping from sass and (cached for 2 hours) return site info. |
| 19 |
* |
| 20 |
* @return WP_REST_Response |
| 21 |
*/ |
| 22 |
public function ping() : WP_REST_Response |
| 23 |
{ |
| 24 |
// Cache time-to-live: 2 hours |
| 25 |
$cache_duration = 3600 * 2; |
| 26 |
try { |
| 27 |
// Try to get cache |
| 28 |
$data = \get_transient('rvx_ping_cache'); |
| 29 |
if (\false === $data) { |
| 30 |
// Cache miss: fetch fresh data, store and return |
| 31 |
$data = $this->pingService->ping(); |
| 32 |
\set_transient('rvx_ping_cache', $data, $cache_duration); |
| 33 |
} |
| 34 |
return Helper::rvxApi($data)->success(\__('Plugin Active', 'reviewx'), 200); |
| 35 |
} catch (Exception $e) { |
| 36 |
return Helper::rvxApi()->fails(\__('Plugin deactivated or uninstalled', 'reviewx'), 404); |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|