PluginProbe
Code Profiler – WordPress Performance Profiling and Debugging Made Easy / 1.6.1
Code Profiler – WordPress Performance Profiling and Debugging Made Easy v1.6.1
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.1, at lib/class-troubleshooter.php

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