| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
class ABJ_404_Solution_DatabaseConnectionManager { |
| 8 |
|
| 9 |
/** @var ABJ_404_Solution_DatabaseCore */ |
| 10 |
private $core; |
| 11 |
|
| 12 |
/** @var ABJ_404_Solution_Logging */ |
| 13 |
private $logger; |
| 14 |
|
| 15 |
/** |
| 16 |
* @param ABJ_404_Solution_DatabaseCore $core |
| 17 |
* @param ABJ_404_Solution_Logging $logger |
| 18 |
*/ |
| 19 |
public function __construct(ABJ_404_Solution_DatabaseCore $core, $logger) { |
| 20 |
$this->core = $core; |
| 21 |
$this->logger = $logger; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Forward DatabaseCore infrastructure calls that remain owned by the core. |
| 26 |
* |
| 27 |
* @param string $name |
| 28 |
* @param array<int, mixed> $arguments |
| 29 |
* @return mixed |
| 30 |
*/ |
| 31 |
public function __call(string $name, array $arguments) { |
| 32 |
return $this->core->$name(...$arguments); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Probe wpdb's check_connection method defensively for custom wpdb |
| 37 |
* drop-ins (HyperDB, LudicrousDB, mu-cluster proxies) that may not |
| 38 |
* implement it. WordPress core has shipped this method since 4.1, |
| 39 |
* but a `wp-content/db.php` drop-in can replace `$wpdb` with a |
| 40 |
* subclass that omits it. Calling an undefined method on such a |
| 41 |
* subclass throws a fatal Error before we'd hit a try/catch. |
| 42 |
* |
| 43 |
* Returns true (assume connected) when the method is missing -- the |
| 44 |
* absence of a probe is not a connection failure, and the standard |
| 45 |
* wpdb default is also "no probe == connected". |
| 46 |
* |
| 47 |
* @param object $wpdb The current $wpdb instance (or subclass). |
| 48 |
* @param bool $allowReconnect Passed through to check_connection(). |
| 49 |
* @return bool True if connected (or unable to probe); false if probed and disconnected. |
| 50 |
*/ |
| 51 |
public function safeCheckConnection($wpdb, bool $allowReconnect = false): bool { |
| 52 |
if (!is_object($wpdb)) { |
| 53 |
return true; |
| 54 |
} |
| 55 |
if (!method_exists($wpdb, 'check_connection') && !is_callable(array($wpdb, 'check_connection'))) { |
| 56 |
return true; |
| 57 |
} |
| 58 |
return (bool) $wpdb->check_connection($allowReconnect); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Ensure database connection is active and reconnect if necessary. |
| 63 |
* |
| 64 |
* @return bool True if connection is active, false otherwise |
| 65 |
*/ |
| 66 |
public function ensureConnection() { |
| 67 |
global $wpdb; |
| 68 |
|
| 69 |
if (!isset($wpdb)) { |
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
try { |
| 74 |
$isConnected = $this->safeCheckConnection($wpdb, false); |
| 75 |
|
| 76 |
if (!$isConnected) { |
| 77 |
$this->logger->debugMessage("Database connection lost, attempting to reconnect..."); |
| 78 |
|
| 79 |
if (is_object($wpdb) && method_exists($wpdb, 'db_connect')) { |
| 80 |
$wpdb->db_connect(); |
| 81 |
} |
| 82 |
|
| 83 |
if ($this->safeCheckConnection($wpdb, false)) { |
| 84 |
$this->logger->debugMessage("Database reconnection successful"); |
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
$this->logger->errorMessage("Failed to reconnect to database"); |
| 89 |
return false; |
| 90 |
} |
| 91 |
} catch (Exception $e) { |
| 92 |
$this->logger->debugMessage("Connection check failed: " . $e->getMessage()); |
| 93 |
return true; |
| 94 |
} catch (Error $e) { |
| 95 |
$this->logger->debugMessage("Connection check not available: " . $e->getMessage()); |
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
return true; |
| 100 |
} |
| 101 |
} |
| 102 |
|