| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 3 |
|
| 4 |
if (!class_exists('WP_Optimize_Server_Compatibility')) : |
| 5 |
/** |
| 6 |
* Adds compatibility for Servers. |
| 7 |
*/ |
| 8 |
class WP_Optimize_Server_Compatibility { |
| 9 |
|
| 10 |
/** |
| 11 |
* Cached result of the Kinsta server detection. |
| 12 |
* |
| 13 |
* Null until first evaluation, then bool for subsequent calls. |
| 14 |
* |
| 15 |
* @var bool|null |
| 16 |
*/ |
| 17 |
private $is_kinsta_server = null; |
| 18 |
|
| 19 |
/** |
| 20 |
* Returns singleton instance |
| 21 |
* |
| 22 |
* @return WP_Optimize_Server_Compatibility |
| 23 |
*/ |
| 24 |
public static function get_instance() { |
| 25 |
static $instance = null; |
| 26 |
if (null === $instance) { |
| 27 |
$instance = new WP_Optimize_Server_Compatibility(); |
| 28 |
} |
| 29 |
return $instance; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Detects whether the server handles cache. e.g. Nginx cache |
| 34 |
* |
| 35 |
* @return bool |
| 36 |
*/ |
| 37 |
public function does_server_handle_cache(): bool { |
| 38 |
return $this->is_kinsta(); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Detects whether the server supports table optimization. |
| 43 |
* |
| 44 |
* Some servers prevent table optimization because the InnoDB engine |
| 45 |
* does not optimize tables — instead it drops and recreates them, |
| 46 |
* resulting in elevated disk write operations. |
| 47 |
* |
| 48 |
* @return bool |
| 49 |
*/ |
| 50 |
public function does_server_allow_table_optimization(): bool { |
| 51 |
return !$this->is_kinsta(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Detects whether the server supports local webp conversion tools |
| 56 |
* |
| 57 |
* @return bool |
| 58 |
*/ |
| 59 |
public function does_server_allow_local_webp_conversion(): bool { |
| 60 |
return !$this->is_kinsta(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Detects if the platform is Kinsta or not. |
| 65 |
* |
| 66 |
* Results are cached after the first call to ensure consistency |
| 67 |
* and avoid redundant filter applications within a single request. |
| 68 |
* |
| 69 |
* @return bool Returns true if it is a Kinsta platform, otherwise returns false |
| 70 |
*/ |
| 71 |
private function is_kinsta(): bool { |
| 72 |
if (null !== $this->is_kinsta_server) { |
| 73 |
return $this->is_kinsta_server; |
| 74 |
} |
| 75 |
|
| 76 |
$is_kinsta = isset($_SERVER['KINSTA_CACHE_ZONE']); |
| 77 |
|
| 78 |
/** |
| 79 |
* Filter whether the server is detected as Kinsta. |
| 80 |
* |
| 81 |
* Allows overriding the default detection logic in case |
| 82 |
* Kinsta changes environment variables or for edge cases. |
| 83 |
* |
| 84 |
* @param bool $is_kinsta Detected Kinsta status. |
| 85 |
*/ |
| 86 |
$this->is_kinsta_server = (bool) apply_filters('wpo_is_kinsta_server', $is_kinsta); |
| 87 |
|
| 88 |
return $this->is_kinsta_server; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Disables automatic table optimization when the server does not allow it. |
| 93 |
* |
| 94 |
* Checks the current server capabilities and, if table optimization is |
| 95 |
* unsupported, updates the "auto" option to disable it. This prevents |
| 96 |
* scheduled or automatic optimizations from running on incompatible hosts. |
| 97 |
* |
| 98 |
* @return void |
| 99 |
*/ |
| 100 |
public function maybe_disable_unsupported_table_optimization(): void { |
| 101 |
|
| 102 |
if ($this->does_server_allow_table_optimization()) { |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
$options = WP_Optimize()->get_options(); |
| 107 |
$auto_options = $options->get_option('auto'); |
| 108 |
|
| 109 |
if (empty($auto_options) || !is_array($auto_options)) { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
if (isset($auto_options['optimize']) && 'true' === $auto_options['optimize']) { |
| 114 |
$auto_options['optimize'] = 'false'; |
| 115 |
$options->update_option('auto', $auto_options); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
endif; |
| 120 |
|