PluginProbe
404 Solution / 4.3.0
404 Solution v4.3.0
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / database / DatabaseConnectionManager.php

DatabaseConnectionManager.php in 404 Solution 4.3.0, at includes/database/DatabaseConnectionManager.php

102 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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