PluginProbe
Code Profiler – WordPress Performance Profiling and Debugging Made Easy / 1.6.8
Code Profiler – WordPress Performance Profiling and Debugging Made Easy v1.6.8
1.9.5 1.9.4 1.9.3 trunk 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.6 1.6.1 1.6.10 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 All 40 releases
code-profiler / lib / class-troubleshooter.php

class-troubleshooter.php in Code Profiler – WordPress Performance Profiling and Debugging Made Easy 1.6.8, at lib/class-troubleshooter.php

352 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 +=====================================================================+
4 | ____ _ ____ __ _ _ |
5 | / ___|___ __| | ___ | _ \ _ __ ___ / _(_) | ___ _ __ |
6 | | | / _ \ / _` |/ _ \ | |_) | '__/ _ \| |_| | |/ _ \ '__| |
7 | | |__| (_) | (_| | __/ | __/| | | (_) | _| | | __/ | |
8 | \____\___/ \__,_|\___| |_| |_| \___/|_| |_|_|\___|_| |
9 | |
10 | (c) Jerome Bruandet ~ https://code-profiler.com/ |
11 +=====================================================================+
12 */
13
14 if (! defined('ABSPATH') ) {
15 die('Forbidden');
16 }
17
18 // =====================================================================
19
20 class CP_Troubleshooter {
21
22 /**
23 * Array to handle all results
24 */
25 private $buffer = [];
26
27 /**
28 * Initialize.
29 */
30 public function __construct() {
31
32 $this->get_system_info('sysinfo');
33 $this->cp_info('code-profiler');
34 $this->function_exists('code-profiler');
35 $this->check_wpdb('wpdb');
36 $this->check_cache('cache');
37 $this->check_proxy('proxy');
38 $this->get_all_plugins('plugins');
39 $this->get_theme('themes');
40 $this->get_cp_config();
41
42 echo esc_textarea( print_r( $this->buffer, true ) );
43 }
44
45 /**
46 * Retrieve system info:
47 * * PHP version
48 * * PHP SAPI
49 * * HTTP server
50 * * Opcode cache
51 * * PHP directives (time limit, memory available, temp dir etc)
52 * * PHP last error
53 * * WordPress
54 */
55 private function get_system_info( $key ) {
56
57 $this->buffer[ $key ]['OS'] = php_uname();
58
59 if (! empty( $_SERVER['SERVER_SOFTWARE'] ) ) {
60 $this->buffer[ $key ]['HTTP server'] = $_SERVER['SERVER_SOFTWARE'];
61 } else {
62 $this->buffer[ $key ]['HTTP server'] = 'N/A';
63 }
64
65 $this->buffer[ $key ]['PHP'] = strtoupper( PHP_SAPI ) .' '. PHP_VERSION;
66
67 if ( extension_loaded('Zend OPcache') ) {
68 $this->buffer[ $key ]['Opcode cache'] = sprintf(
69 'Zend OPcache (%s)', ini_get('opcache.enable') ? 'enabled':'disabled'
70 );
71 } elseif ( extension_loaded('wincache') ) {
72 $this->buffer[ $key ]['Opcode cache'] = sprintf(
73 'wincache (%s)', ini_get('wincache.fcenabled') ? 'enabled':'disabled'
74 );
75 }
76
77 $this->buffer[ $key ]['Memory limit'] = ini_get('memory_limit');
78 $this->buffer[ $key ]['Peak limit'] = number_format( memory_get_peak_usage() );
79 $this->buffer[ $key ]['Max execution time'] = ini_get('max_execution_time') .'s';
80 $this->buffer[ $key ]['Disabled functions'] = ini_get('disable_functions');
81 $this->buffer[ $key ]['Display errors'] = ini_get('display_errors');
82
83 $tmp = ini_get('sys_temp_dir');
84 if (! empty( $tmp ) ) {
85 if ( is_writable( $tmp ) ) {
86 $tmp .= ' (writable)';
87 } else {
88 $tmp .= ' (not writable!)';
89 }
90 }
91 $tmp = $this->shorten_path( $tmp );
92 $this->buffer[ $key ]['Temp directory'] = $tmp;
93 $this->buffer[ $key ]['Log errors'] = ini_get('log_errors');
94
95 $error_log = ini_get('error_log');
96 $filesize = 0;
97 if ( is_file( $error_log ) ) {
98 $filesize = filesize( $error_log );
99 }
100 $this->buffer[ $key ]['Error log'] = "$error_log (". number_format( $filesize ) ." bytes)";
101
102 $this->buffer[ $key ]['Last error'] = error_get_last();
103 if ( isset( $this->buffer[ $key ]['Last error']['file'] ) ) {
104 $this->buffer[ $key ]['Last error']['file'] = $this->shorten_path( $this->buffer[ $key ]['Last error']['file'] );
105 }
106
107 // WordPress
108 if ( defined('WP_MEMORY_LIMIT') ) {
109 $this->buffer[ $key ]['WordPress']['WP_MEMORY_LIMIT'] = WP_MEMORY_LIMIT;
110 } else {
111 $this->buffer[ $key ]['WordPress']['WP_MEMORY_LIMIT'] = 'N/A';
112 }
113 if ( defined('WP_MAX_MEMORY_LIMIT') ) {
114 $this->buffer[ $key ]['WordPress']['WP_MAX_MEMORY_LIMIT'] = WP_MAX_MEMORY_LIMIT;
115 } else {
116 $this->buffer[ $key ]['WordPress']['WP_MAX_MEMORY_LIMIT'] = 'N/A';
117 }
118 global $wp_version;
119 $this->buffer[ $key ]['WordPress']['version'] = $wp_version;
120 $wp_debug = '';
121 if ( defined('WP_DEBUG') ) {
122 $wp_debug = WP_DEBUG;
123 }
124 $this->buffer[ $key ]['WordPress']['WP_DEBUG'] = $wp_debug;
125 $wp_debug = '';
126 $filesize = 0;
127 if ( defined('WP_DEBUG_LOG') ) {
128 $wp_debug = WP_DEBUG_LOG;
129 }
130 if ( is_file( $wp_debug ) ) {
131 $filesize = filesize( $error_log );
132 $wp_debug = $this->shorten_path( $wp_debug ) ." (". number_format( $filesize ) ." bytes)";
133 }
134 $this->buffer[ $key ]['WordPress']['WP_DEBUG_LOG'] = $wp_debug;
135
136 }
137
138 /**
139 * Verify if some important PHP functions are available.
140 */
141 private function function_exists( $key ) {
142
143 $required_functions = [
144 'register_shutdown_function' => 'shutdown',
145 'register_tick_function' => 'tick',
146 'stream_wrapper_unregister' => 'unregister',
147 'stream_wrapper_register' => 'register',
148 'stream_wrapper_restore' => 'restore',
149 'hrtime' => 'hrtime'
150 ];
151
152 foreach ( $required_functions as $function => $value ) {
153 $this->buffer[ $key ]['function'][ $value ] = function_exists( $function );
154 }
155
156 $required_methods = [
157 'PhpToken' => 'tokenize'
158 ];
159 foreach ( $required_methods as $class => $value ) {
160 $this->buffer[ $key ]['method'][ $value ] = method_exists( $class, $value );
161 }
162 }
163
164
165 /**
166 * Return the version and type (free/pro) of Code Profiler.
167 */
168 private function cp_info( $key ) {
169
170 if ( defined('CODE_PROFILER_PRO_VERSION') ) {
171 $this->buffer[ $key ]['slug'] = 'code-profiler-pro';
172 $this->buffer[ $key ]['version'] = CODE_PROFILER_PRO_VERSION;
173
174 } elseif ( defined('CODE_PROFILER_VERSION') ) {
175 $this->buffer[ $key ]['slug'] = 'code-profiler';
176 $this->buffer[ $key ]['version'] = CODE_PROFILER_VERSION;
177
178 } else {
179 exit('Error: Code Profiler is not active. Please activate it and run this script again.');
180
181 }
182
183 // Data folder must be writable
184 $upload_dir = wp_upload_dir();
185 $dir = $upload_dir['basedir'] .'/'. $this->buffer[ $key ]['slug'];
186
187 $this->buffer[ $key ]['data_dir']['path'] = $this->shorten_path( $dir );
188 $this->buffer[ $key ]['data_dir']['exists'] = file_exists( $dir );
189 $this->buffer[ $key ]['data_dir']['writable'] = is_writable( $dir );
190 }
191
192
193 /**
194 * Check if there are subclasses of the wpdb class and
195 * look for wp-content/db.php
196 */
197 function check_wpdb( $key ) {
198
199 foreach( get_declared_classes() as $class ) {
200 $reflected = new ReflectionClass( $class );
201 if ( $reflected->isSubclassOf('wpdb') ) {
202 $script = $reflected->getFileName();
203 $this->buffer[ $key ]['extends'][$class] = $this->shorten_path( $script );
204 }
205 }
206 $db_php = $this->shorten_path( WP_CONTENT_DIR .'/db.php');
207 $this->buffer[ $key ][ $db_php ] = file_exists( WP_CONTENT_DIR .'/db.php');
208 }
209
210
211 /**
212 * Check for advanced cache
213 */
214 private function check_cache( $key ) {
215
216 $this->buffer[ $key ] = [];
217 if ( defined('WP_CACHE') && file_exists( WP_CONTENT_DIR . '/advanced-cache.php') ) {
218 $this->buffer[ $key ][ $this->shorten_path( WP_CONTENT_DIR . '/advanced-cache.php') ] = 1;
219 }
220 }
221
222
223 /**
224 * Check for reverse proxy or CDN
225 */
226 private function check_proxy( $key ) {
227
228 $this->buffer[ $key ] = [];
229 $proxies = [
230 'HTTP_X_FORWARDED_FOR',
231 'HTTP_CF_CONNECTING_IP',
232 'HTTP_INCAP_CLIENT_IP'
233 ];
234
235 foreach( $proxies as $proxy ) {
236 if (! empty( $_SERVER[ $proxy ] ) ) {
237 $this->buffer[ $key ][ $proxy ] = 1;
238 }
239 }
240
241 }
242
243
244 /**
245 * Retrieve the list of all plugins and sort them
246 * (active or disabled).
247 */
248 private function get_all_plugins( $key ) {
249
250 if (! function_exists('get_plugins') ) {
251 require_once ABSPATH .'wp-admin/includes/plugin.php';
252 }
253 $plugins = get_plugins();
254 foreach( $plugins as $k => $v ) {
255 if ( $slug = substr( $k, 0, strpos( $k, '/') ) ) {
256 $name = 'N/A'; $version = 'N/A'; $uri = 'N/A';
257 if (! empty( $v['Name'] ) ) {
258 $name = $v['Name'];
259 }
260 if (! empty( $v['Version'] ) ) {
261 $version = $v['Version'];
262 }
263 if (! empty( $v['PluginURI'] ) ) {
264 $uri = $v['PluginURI'];
265 }
266 if ( is_plugin_active( $k ) ) {
267 $this->buffer[ $key ]['active'][ $slug ] = "$name - $version - $uri";
268
269 } else {
270 $this->buffer[ $key ]['inactive'][ $slug ] = "$name - $version - $uri";
271 }
272 }
273 }
274
275 $mu_plugins = get_mu_plugins();
276 foreach( $mu_plugins as $k => $v ) {
277 $name = 'N/A'; $version = 'N/A'; $uri = 'N/A';
278 if (! empty( $v['Name'] ) ) {
279 $name = $v['Name'];
280 }
281 if (! empty( $v['Version'] ) ) {
282 $version = $v['Version'];
283 }
284 if (! empty( $v['PluginURI'] ) ) {
285 $uri = $v['PluginURI'];
286 }
287 $this->buffer[ $key ]['mu-plugins'][ $k ] = "$name - $version - $uri";
288 }
289
290 }
291
292
293 /**
294 * Retrieve the active theme.
295 */
296 private function get_theme( $key ) {
297
298 $themes = wp_get_themes();
299 $active = get_option('template');
300 foreach( $themes as $k => $v ) {
301 $name = 'N/A'; $version = 'N/A';
302 if ( $v->Name ) {
303 $name = $v->Name;
304 }
305 if ( $v->Version ) {
306 $version = $v->Version;
307 }
308 if ( $k == $active ) {
309 $this->buffer[ $key ]['active'][ $k ] = "$name - $version";
310 } else {
311 $this->buffer[ $key ]['inactive'][ $k ] = "$name - $version";
312 }
313 }
314 }
315
316
317 /**
318 * Retrieve Code Profiler's configuration
319 */
320 private function get_cp_config() {
321
322 $cp_options = get_option('code-profiler');
323
324 $list = [
325 'hide_empty_value',
326 'warn_composer',
327 'enable_wpcli',
328 'disable_wpcron',
329 // 'disable_db-php',
330 'http_response',
331 // 'backtrace_limit',
332 'accuracy',
333 'buffer'
334 ];
335 foreach( $list as $element ) {
336 $this->buffer['options'][ $element ] = isset( $cp_options[ $element ] )? $cp_options[ $element ]:0;
337 }
338 }
339
340 /**
341 * Remove the ABSPATH from a path
342 */
343 private function shorten_path( $path ) {
344
345 return str_replace( ABSPATH, '', $path );
346 }
347
348 }
349
350 // =====================================================================
351 // EOF
352