| 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 |
* Returns singleton instance |
| 12 |
* |
| 13 |
* @return WP_Optimize_Server_Compatibility |
| 14 |
*/ |
| 15 |
public static function get_instance() { |
| 16 |
static $instance = null; |
| 17 |
if (null === $instance) { |
| 18 |
$instance = new WP_Optimize_Server_Compatibility(); |
| 19 |
} |
| 20 |
return $instance; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Detects whether the server handles cache. e.g. Nginx cache |
| 25 |
* |
| 26 |
* @return bool |
| 27 |
*/ |
| 28 |
public function does_server_handle_cache(): bool { |
| 29 |
return $this->is_kinsta(); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Detects whether the server supports table optimization. |
| 34 |
* |
| 35 |
* Some servers prevent table optimization |
| 36 |
* because InnoDB engine does not optimize table |
| 37 |
* instead it drops tables and recreates them, |
| 38 |
* which results in elevated disk write operations |
| 39 |
* |
| 40 |
* @return bool |
| 41 |
*/ |
| 42 |
public function does_server_allow_table_optimization(): bool { |
| 43 |
return !$this->is_kinsta(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Detects whether the server supports local webp conversion tools |
| 48 |
* |
| 49 |
* @return bool |
| 50 |
*/ |
| 51 |
public function does_server_allow_local_webp_conversion(): bool { |
| 52 |
return !$this->is_kinsta(); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Detects if the platform is Kinsta or not |
| 57 |
* |
| 58 |
* @return bool Returns true if it is a Kinsta platform, otherwise returns false |
| 59 |
*/ |
| 60 |
private function is_kinsta(): bool { |
| 61 |
$is_kinsta = isset($_SERVER['KINSTA_CACHE_ZONE']); |
| 62 |
|
| 63 |
/** |
| 64 |
* Filter whether the server is detected as Kinsta. |
| 65 |
* |
| 66 |
* Allows overriding the default detection logic in case |
| 67 |
* Kinsta changes environment variables or for edge cases. |
| 68 |
* |
| 69 |
* @param bool $is_kinsta Detected Kinsta status. |
| 70 |
*/ |
| 71 |
return (bool) apply_filters('wpo_is_kinsta_server', $is_kinsta); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Disable table optimization if the server does not allow it. |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function maybe_disable_unsupported_table_optimization(): void { |
| 80 |
|
| 81 |
if ($this->does_server_allow_table_optimization()) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
$options = WP_Optimize()->get_options(); |
| 86 |
$auto_options = $options->get_option('auto'); |
| 87 |
|
| 88 |
if (empty($auto_options) || !is_array($auto_options)) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
if (!empty($auto_options['optimize']) && 'true' === $auto_options['optimize']) { |
| 93 |
$auto_options['optimize'] = 'false'; |
| 94 |
$options->update_option('auto', $auto_options); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
endif; |
| 99 |
|