| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 3 |
|
| 4 |
if (!class_exists('WP_Optimize_Server_Information')) : |
| 5 |
|
| 6 |
class WP_Optimize_Server_Information { |
| 7 |
|
| 8 |
/** |
| 9 |
* Use WordPress core debug data helper |
| 10 |
* |
| 11 |
* @return array |
| 12 |
*/ |
| 13 |
public function get_wp_debug_data() { |
| 14 |
if (!class_exists('WP_Debug_Data')) { |
| 15 |
if (!is_readable(ABSPATH.'wp-admin/includes/class-wp-debug-data.php')) { |
| 16 |
return array( |
| 17 |
'data' => array( |
| 18 |
'label' => esc_html__('Please note that WordPress health information, including details about installed plugins and installation data, is only available from version 5.2 onwards.', 'wp-optimize'). ' ' . esc_html__('We recommend updating your WordPress version.', 'wp-optimize') |
| 19 |
) |
| 20 |
); |
| 21 |
} |
| 22 |
|
| 23 |
require_once(ABSPATH.'wp-admin/includes/class-wp-debug-data.php'); |
| 24 |
} |
| 25 |
|
| 26 |
return WP_Debug_Data::debug_data(); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Retrieve WPO cache size |
| 31 |
* |
| 32 |
* @return array |
| 33 |
*/ |
| 34 |
public function get_cache_size() { |
| 35 |
return WP_Optimize()->get_page_cache()->get_cache_size(); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Retrieve WPO generated minify files and size |
| 40 |
* |
| 41 |
* @return string |
| 42 |
*/ |
| 43 |
public function get_minify_size() { |
| 44 |
$wpo_minify_options = wp_optimize_minify_config()->get(); |
| 45 |
if (!$wpo_minify_options['enabled']) return ''; |
| 46 |
|
| 47 |
$cache_path = WP_Optimize_Minify_Cache_Functions::cache_path(); |
| 48 |
return WP_Optimize_Minify_Cache_Functions::get_cachestats($cache_path['cachedir']); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get logs and their sizes |
| 53 |
* |
| 54 |
* @return array |
| 55 |
*/ |
| 56 |
public function get_logs_summary() { |
| 57 |
$sizes = $this->get_logs(true); |
| 58 |
|
| 59 |
$result = array(); |
| 60 |
foreach ($sizes as $filename => $filesize) { |
| 61 |
$result[] = $filename . ': ' . size_format($filesize); |
| 62 |
} |
| 63 |
|
| 64 |
return $result; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Look around for WPO logs and fetch the content |
| 69 |
* |
| 70 |
* @param bool $paths_only Don't include the file contents in the response, just the file size |
| 71 |
* @return array |
| 72 |
*/ |
| 73 |
public function get_logs($paths_only = false) { |
| 74 |
$upload_base = WP_Optimize_Utils::get_log_folder_path(); |
| 75 |
|
| 76 |
$cache_path = WP_Optimize_Minify_Cache_Functions::cache_path(); |
| 77 |
|
| 78 |
$log_file_patterns = array( |
| 79 |
$upload_base => array( |
| 80 |
'.*\.log$' |
| 81 |
), |
| 82 |
$cache_path['cachedir'] => array( |
| 83 |
'.*\.json$' |
| 84 |
) |
| 85 |
); |
| 86 |
|
| 87 |
$log_data = array(); |
| 88 |
|
| 89 |
foreach ($log_file_patterns as $path => $patterns) { |
| 90 |
$files = $this->scandir($path); |
| 91 |
foreach ($files as $file_full_path) { |
| 92 |
foreach ($patterns as $pattern) { |
| 93 |
$file = basename($file_full_path); |
| 94 |
if (preg_match('|' . $pattern . '|', $file) && is_file($file_full_path)) { |
| 95 |
$log_data[$file] = (true === $paths_only) ? filesize($file_full_path) : file_get_contents($file_full_path); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
return $log_data; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Retrieve WPO cache configuration |
| 106 |
* |
| 107 |
* @return array |
| 108 |
*/ |
| 109 |
public function get_cache_config() { |
| 110 |
return WPO_Cache_Config::instance()->get(); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Read main .htaccess file |
| 115 |
* |
| 116 |
* @return string |
| 117 |
*/ |
| 118 |
public function get_webroot_htaccess() { |
| 119 |
$file = trailingslashit(ABSPATH) . '.htaccess'; |
| 120 |
if (!is_readable($file)) return ''; |
| 121 |
|
| 122 |
return trim(file_get_contents($file)); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Read uploads dir .htaccess file |
| 127 |
* |
| 128 |
* @return string |
| 129 |
*/ |
| 130 |
public function get_upload_dir_htaccess() { |
| 131 |
$uploads_dir = wp_get_upload_dir(); |
| 132 |
$file = trailingslashit($uploads_dir['basedir']) . '.htaccess'; |
| 133 |
|
| 134 |
if (!is_readable($file)) return ''; |
| 135 |
|
| 136 |
return trim(file_get_contents($file)); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Iterate on uploads dir and read directories permissions |
| 141 |
* |
| 142 |
* @return array |
| 143 |
*/ |
| 144 |
public function get_uploads_dir_permissions() { |
| 145 |
$uploads_dir = wp_get_upload_dir(); |
| 146 |
if (!isset($uploads_dir['basedir']) || !is_dir($uploads_dir['basedir'])) { |
| 147 |
return array(); |
| 148 |
} |
| 149 |
|
| 150 |
$return = array( |
| 151 |
'uploads' => decoct(fileperms($uploads_dir['basedir']) & 0777) |
| 152 |
); |
| 153 |
|
| 154 |
$uploads_content = $this->scandir($uploads_dir['basedir']); |
| 155 |
|
| 156 |
foreach ($uploads_content as $element) { |
| 157 |
if (is_dir($element)) { |
| 158 |
$return['uploads/' . basename($element)] = decoct(fileperms($element) & 0777); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
return $return; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Inspect directory with opendir, since glob might not be available in some systems |
| 167 |
* |
| 168 |
* @param string $path The actual path to scan |
| 169 |
* @return array |
| 170 |
*/ |
| 171 |
private function scandir($path) { |
| 172 |
$content = array(); |
| 173 |
|
| 174 |
clearstatcache(); |
| 175 |
if (is_dir($path.'/') && ($handle = @opendir($path.'/'))) { // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- suppress PHP warning in case of failure |
| 176 |
while (false !== ($file = readdir($handle))) { |
| 177 |
if ("." === $file || ".." === $file) continue; |
| 178 |
|
| 179 |
$content[] = trailingslashit($path) . $file; |
| 180 |
} |
| 181 |
closedir($handle); |
| 182 |
} |
| 183 |
|
| 184 |
return $content; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Retrieve status of WebP redirection |
| 189 |
* |
| 190 |
* @return bool|null |
| 191 |
*/ |
| 192 |
public function webp_redirection_status() { |
| 193 |
$webp = WP_Optimize_WebP::get_instance(); |
| 194 |
$enabled = $webp->is_webp_conversion_enabled(); |
| 195 |
if (!$enabled) return false; |
| 196 |
|
| 197 |
$is_possible = $webp->is_webp_redirection_possible(); |
| 198 |
|
| 199 |
return $is_possible ? true : null; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
endif; |
| 204 |
|