| @@ -10,9 +10,9 @@ | ||
| 10 | 10 | | (c) Jerome Bruandet ~ https://code-profiler.com/ | |
| 11 | 11 | +=====================================================================+ |
| 12 | 12 | */ |
| 13 | 13 | |
| 14 | -if (! defined( 'ABSPATH' ) ) { die( 'Forbidden' ); } | |
| 14 | +if (! defined('ABSPATH') ) { die('Forbidden'); } | |
| 15 | 15 | |
| 16 | 16 | |
| 17 | 17 | class CodeProfiler_Report { |
| 18 | 18 | |
| @@ -36,9 +36,9 @@ | ||
| 36 | 36 | private $tmp_diskio; |
| 37 | 37 | private $tmp_ticks; |
| 38 | 38 | |
| 39 | 39 | |
| 40 | - /** | |
| 40 | + /******************************************************************** | |
| 41 | 41 | * Initialize |
| 42 | 42 | */ |
| 43 | 43 | public function __construct( $profile_name, $microtime ) { |
| 44 | 44 | |
| @@ -60,9 +60,9 @@ | ||
| 60 | 60 | $cp_error = 2; |
| 61 | 61 | } |
| 62 | 62 | if (! empty( $cp_error ) ) { |
| 63 | 63 | $error = sprintf( |
| 64 | - esc_html__('Cannot create the report: the profiler did not generate a data file (#%s). Make sure the following directory is writable: %s. If you are using a caching plugin or an opcode cache, try to disable it. You may also find more details about the error in the Log page.', 'code-profiler'), | |
| 64 | + esc_html__('Cannot create the report: the profiler did not generate a data file (#%s). Make sure the following directory is writable: %s. If you are using a caching plugin or an opcode cache, try to disable it. You may also find more details about the error in the "Log" tab.', 'code-profiler'), | |
| 65 | 65 | $cp_error, |
| 66 | 66 | CODE_PROFILER_UPLOAD_DIR .'/' |
| 67 | 67 | ); |
| 68 | 68 | $this->return_error( $error ); |
| @@ -73,14 +73,14 @@ | ||
| 73 | 73 | $this->parsed_data += filesize( $this->tmp_ticks ); |
| 74 | 74 | |
| 75 | 75 | $this->profile_name = $profile_name; |
| 76 | 76 | $this->plugins_dir = preg_quote( realpath( WP_PLUGIN_DIR ) ); |
| 77 | - $this->themes_dir = preg_quote( realpath( WP_CONTENT_DIR .'/themes' ) ); | |
| 77 | + $this->themes_dir = preg_quote( realpath( WP_CONTENT_DIR .'/themes') ); | |
| 78 | 78 | $this->mu_dir = preg_quote( realpath( WPMU_PLUGIN_DIR ) ); |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | |
| 82 | - /** | |
| 82 | + /******************************************************************** | |
| 83 | 83 | * Filter and save the profiler's data |
| 84 | 84 | */ |
| 85 | 85 | public function prepare_report() { |
| 86 | 86 | |
| @@ -91,12 +91,13 @@ | ||
| 91 | 91 | $this->save_diskio(); |
| 92 | 92 | $this->save_composer(); |
| 93 | 93 | |
| 94 | 94 | code_profiler_log_info( sprintf( |
| 95 | - __('Volume of PHP code analyzed and processed: %s MB (%s plugins and 1 theme) - Memory used: %s MB', 'code-profiler'), | |
| 95 | + __('Volume of code and data analyzed: %1$sMB (%2$s plugins and 1 theme) in %4$ss - Memory used: %3$sMB', 'code-profiler'), | |
| 96 | 96 | number_format( $this->parsed_data / 1024 / 1024, 2 ), |
| 97 | - $this->total_plugins, | |
| 98 | - number_format( memory_get_peak_usage( false ) / 1024 / 1024, 2 ) | |
| 97 | + (int) $this->total_plugins, | |
| 98 | + number_format( memory_get_peak_usage( false ) / 1024 / 1024, 2 ), | |
| 99 | + number_format( microtime( true ) - $this->microtime, 2 ) | |
| 99 | 100 | )); |
| 100 | 101 | |
| 101 | 102 | return $this->microtime; |
| 102 | 103 | } |
| @@ -101,9 +102,9 @@ | ||
| 101 | 102 | return $this->microtime; |
| 102 | 103 | } |
| 103 | 104 | |
| 104 | 105 | |
| 105 | - /** | |
| 106 | + /******************************************************************** | |
| 106 | 107 | * Return a json-encoded error for AJAX, write to log and quit. |
| 107 | 108 | */ |
| 108 | 109 | private function return_error( $error ) { |
| 109 | 110 | |
| @@ -113,9 +114,9 @@ | ||
| 113 | 114 | wp_send_json( $response ); |
| 114 | 115 | } |
| 115 | 116 | |
| 116 | 117 | |
| 117 | - /** | |
| 118 | + /******************************************************************** | |
| 118 | 119 | * Save all data to disk |
| 119 | 120 | */ |
| 120 | 121 | private function save_data() { |
| 121 | 122 | |
| @@ -154,9 +155,9 @@ | ||
| 154 | 155 | } |
| 155 | 156 | |
| 156 | 157 | $error = ''; |
| 157 | 158 | if ( empty( $slugs_buffer ) ) { |
| 158 | - $error = esc_html__( 'Data is empty: no plugins or themes found', 'code-profiler' ); | |
| 159 | + $error = esc_html__('Data is empty: no plugins or themes found', 'code-profiler'); | |
| 159 | 160 | } |
| 160 | 161 | if ( $error ) { |
| 161 | 162 | $this->return_error( $error ); |
| 162 | 163 | } |
| @@ -189,14 +190,16 @@ | ||
| 189 | 190 | |
| 190 | 191 | } |
| 191 | 192 | |
| 192 | 193 | |
| 193 | - /** | |
| 194 | + /******************************************************************** | |
| 194 | 195 | * Convert microtime (PHP<7.3) or hrtime (PHP>=7.3) to seconds |
| 195 | 196 | */ |
| 196 | 197 | private function convert_2_seconds( $time ) { |
| 197 | 198 | |
| 198 | - if ( empty( $time ) ) { return '0'; } | |
| 199 | + if ( $time < 0 || empty( $time ) ) { | |
| 200 | + return '0'; | |
| 201 | + } | |
| 199 | 202 | if ( strpos( $time, '.' ) !== false ) { |
| 200 | 203 | // Looks like microtime |
| 201 | 204 | $time = number_format( $time, 6 ); |
| 202 | 205 | } else { |
| @@ -206,14 +209,14 @@ | ||
| 206 | 209 | return $time; |
| 207 | 210 | } |
| 208 | 211 | |
| 209 | 212 | |
| 210 | - /** | |
| 213 | + /******************************************************************** | |
| 211 | 214 | * Check if a slug is from a plugin or theme. |
| 212 | 215 | */ |
| 213 | 216 | private function plugin_or_theme( $script ) { |
| 214 | 217 | |
| 215 | - $res = [ 'theme' => '', 'plugin' => '', 'script' => '' ]; | |
| 218 | + $res = ['theme' => '', 'plugin' => '', 'script' => '']; | |
| 216 | 219 | |
| 217 | 220 | if ( preg_match("`^{$this->plugins_dir}[\\\/](?:(.+?)[\\\/]|([^\\\/]+\.php)$)`", $script, $slug ) ) { |
| 218 | 221 | if (! empty( $slug[1] ) ) { |
| 219 | 222 | $res['plugin'] = $slug[1]; |
| @@ -234,9 +237,9 @@ | ||
| 234 | 237 | return $res; |
| 235 | 238 | } |
| 236 | 239 | |
| 237 | 240 | |
| 238 | - /** | |
| 241 | + /******************************************************************** | |
| 239 | 242 | * Retrieve the full name for all plugins and themes. |
| 240 | 243 | */ |
| 241 | 244 | private function get_plugins_theme_name() { |
| 242 | 245 | |
| @@ -272,14 +275,14 @@ | ||
| 272 | 275 | } |
| 273 | 276 | } |
| 274 | 277 | |
| 275 | 278 | |
| 276 | - /** | |
| 279 | + /******************************************************************** | |
| 277 | 280 | * Parse, filter and sort the data |
| 278 | 281 | */ |
| 279 | 282 | private function parse_ticks() { |
| 280 | 283 | |
| 281 | - $fh = fopen( $this->tmp_ticks, 'rb' ); | |
| 284 | + $fh = fopen( $this->tmp_ticks, 'rb'); | |
| 282 | 285 | if ( $fh === false ) { |
| 283 | 286 | $error = sprintf( |
| 284 | 287 | esc_html__('Cannot open file for reading: %s', 'code-profiler'), |
| 285 | 288 | $this->tmp_ticks |
| @@ -389,9 +392,9 @@ | ||
| 389 | 392 | fclose( $fh ); |
| 390 | 393 | unlink( $this->tmp_ticks ); |
| 391 | 394 | } |
| 392 | 395 | |
| 393 | - /** | |
| 396 | + /******************************************************************** | |
| 394 | 397 | * Save IO stats |
| 395 | 398 | */ |
| 396 | 399 | private function save_iostats() { |
| 397 | 400 | |
| @@ -419,12 +422,12 @@ | ||
| 419 | 422 | unlink( $this->tmp_iostats ); |
| 420 | 423 | } |
| 421 | 424 | |
| 422 | 425 | |
| 423 | - /** | |
| 426 | + /******************************************************************** | |
| 424 | 427 | * Save Read/Write stats |
| 425 | 428 | */ |
| 426 | - function save_diskio() { | |
| 429 | + private function save_diskio() { | |
| 427 | 430 | |
| 428 | 431 | rename( |
| 429 | 432 | $this->tmp_diskio, |
| 430 | 433 | CODE_PROFILER_UPLOAD_DIR ."/{$this->microtime}.{$this->profile_name}.diskio.profile" |
| @@ -431,12 +434,12 @@ | ||
| 431 | 434 | ); |
| 432 | 435 | } |
| 433 | 436 | |
| 434 | 437 | |
| 435 | - /** | |
| 438 | + /******************************************************************** | |
| 436 | 439 | * Save list of plugins using composer |
| 437 | 440 | */ |
| 438 | - function save_composer() { | |
| 441 | + private function save_composer() { | |
| 439 | 442 | |
| 440 | 443 | if (! empty( $this->composer ) ) { |
| 441 | 444 | // Try to get the plugin's name |
| 442 | 445 | foreach( $this->composer as $slug => $v ) { |