Changeset 3375722
- Timestamp:
- 10/09/2025 12:56:13 PM (2 months ago)
- Location:
- code-profiler/trunk
- Files:
-
- 4 edited
-
index.php (modified) (2 diffs)
-
lib/class-cli.php (modified) (9 diffs)
-
lib/helper.php (modified) (1 diff)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
code-profiler/trunk/index.php
r3371018 r3375722 6 6 Author: Jerome Bruandet ~ NinTechNet Ltd. 7 7 Author URI: https://nintechnet.com/ 8 Version: 1.8 8 Version: 1.8.1 9 9 Network: true 10 10 License: GPLv3 or later … … 13 13 */ 14 14 15 define('CODE_PROFILER_VERSION', '1.8 ');15 define('CODE_PROFILER_VERSION', '1.8.1'); 16 16 /** 17 17 +=====================================================================+ -
code-profiler/trunk/lib/class-cli.php
r3371018 r3375722 19 19 20 20 private $file; 21 private $out = 'view'; 21 22 private $cmd_view = 'wp code-profiler view'; 22 23 private $cmd_run = 'wp code-profiler run'; … … 55 56 WP_CLI::error( $message ); 56 57 exit; 58 } 59 60 if (! empty( $assoc_args['out'] ) && in_array( $assoc_args['out'], ['json', 'csv'] ) ) { 61 $this->out = $assoc_args['out']; 57 62 } 58 63 … … 67 72 } 68 73 69 WP_CLI::log( sprintf( 70 __('Starting Code Profiler v%s (profile: %s)', 'code-profiler'), 71 CODE_PROFILER_VERSION, $_POST['profile'] ) . "\n" 72 ); 73 74 $progress = \WP_CLI\Utils\make_progress_bar( '', 3 ); 75 $progress->tick(); 74 if ( $this->out == 'view') { 75 WP_CLI::log( sprintf( 76 __('Starting Code Profiler v%s (profile: %s)', 'code-profiler'), 77 CODE_PROFILER_VERSION, $_POST['profile'] ) . "\n" 78 ); 79 80 $progress = \WP_CLI\Utils\make_progress_bar( '', 3 ); 81 $progress->tick(); 82 } 76 83 77 84 // Run the profiler … … 87 94 } 88 95 89 $progress->tick(); 96 if ( $this->out == 'view') { 97 $progress->tick(); 98 } 90 99 91 100 // All good, run the parser … … 102 111 } 103 112 104 $progress->tick(); 105 $progress->finish(); 113 if ( $this->out == 'view') { 114 $progress->tick(); 115 $progress->finish(); 116 } 106 117 107 118 // Run the parser and show the results 108 $this->view( $ response['cp_profile'] );119 $this->view( $args, $assoc_args, $response['cp_profile'] ); 109 120 110 121 exit; … … 116 127 * 117 128 */ 118 public function view( $ profile = '') {129 public function view( $args = [], $assoc_args = [], $profile = '') { 119 130 120 131 $this->is_enabled(); 132 133 if (! empty( $assoc_args['out'] ) && in_array( $assoc_args['out'], ['json', 'csv'] ) ) { 134 $this->out = $assoc_args['out']; 135 } 121 136 122 137 // The profile was just created (`wp code-profiler run`)... … … 149 164 $match 150 165 ); 151 $message = sprintf( __('Viewing: %s', 'code-profiler'), $match[1] ); 152 $date = date('Y/m/d \@ H:i:s', filemtime( $this->file ) ); 153 echo WP_CLI::colorize("\n%Y$message ~ $date%n\n\n"); 154 155 // Display stats 156 $summary_file = str_replace('.slugs.profile', '', $this->file ); 157 echo code_profiler_getsummarystats( $summary_file, 'text'); 166 167 $cp_slug = __('Slug', 'code-profiler'); 168 $cp_time = __('Execution time', 'code-profiler'); 169 $cp_name = __('Name', 'code-profiler'); 170 $cp_type = __('Type', 'code-profiler'); 171 172 if ( $this->out == 'view' ) { 173 $buffer = ''; 174 $message = sprintf( __('Viewing: %s', 'code-profiler'), $match[1] ); 175 $date = date('Y/m/d \@ H:i:s', filemtime( $this->file ) ); 176 echo WP_CLI::colorize("\n%Y$message ~ $date%n\n\n"); 177 // Display stats 178 $summary_file = str_replace('.slugs.profile', '', $this->file ); 179 echo code_profiler_getsummarystats( $summary_file, 'text'); 180 /** 181 * CSV output. 182 */ 183 } elseif ( $this->out == 'csv' ) { 184 $buffer = "$cp_slug,\"$cp_time\",$cp_name,$cp_type\n"; 185 /** 186 * JSON-encoded output. 187 */ 188 } else { 189 $buffer = []; 190 } 158 191 159 192 $slugs = $this->read_profile(); … … 171 204 foreach( $slugs as $k => $v ) { 172 205 173 // Display name, time and % 174 if ( isset( $cp_options['display_name'] ) && $cp_options['display_name'] == 'slug' ) { 175 $name = $v[0]; 206 if ( $this->out == 'csv') { 207 $buffer .= "{$v[0]},{$v[1]},{$v[2]},{$v[3]}\n"; 208 209 } elseif( $this->out == 'json') { 210 $buffer[] = [ 211 $cp_slug = $v[0], 212 $cp_time = $v[1], 213 $cp_name = $v[2], 214 $cp_type = $v[3] 215 ]; 216 176 217 } else { 177 $name = $v[2]; 218 // Display name, time and % 219 if ( isset( $cp_options['display_name'] ) && $cp_options['display_name'] == 'slug' ) { 220 $name = $v[0]; 221 } else { 222 $name = $v[2]; 223 } 224 // Inform if it's the theme or a mu-plugin 225 if ( $v[3] == 'theme') { 226 $name .= ' (theme)'; 227 } elseif ( $v[3] == 'mu-plugin') { 228 $name .= ' (mu-plugin)'; 229 } 230 231 $time = number_format( $v[1], 3 ); 232 $percent = number_format( $v[1] / $total_time * 100 ); 233 $chars = number_format( $percent * 80 / $coeff ); 234 // We use `echo` instead of `WP_CLI::log` because the layout could 235 // be all messed-up when some caching plugins such as LiteSpeed Cache 236 // are activated. 237 echo " $name | {$time}s | {$percent}%\n"; 238 if (! $percent ) { 239 echo " \u{258C}\n\n"; 240 } else { 241 $bar = ''; 242 for ( $i = 0; $i < $chars; $i++ ) { 243 $bar .= ' '; 244 } 245 echo WP_CLI::colorize(" %8$bar%n\n\n"); 246 } 178 247 } 179 // Inform if it's the theme or a mu-plugin 180 if ( $v[3] == 'theme') { 181 $name .= ' (theme)'; 182 } elseif ( $v[3] == 'mu-plugin') { 183 $name .= ' (mu-plugin)'; 184 } 185 186 $time = number_format( $v[1], 3 ); 187 $percent = number_format( $v[1] / $total_time * 100 ); 188 $chars = number_format( $percent * 80 / $coeff ); 189 // We use `echo` instead of `WP_CLI::log` because the layout could 190 // be all messed-up when some caching plugins such as LiteSpeed Cache 191 // are activated. 192 echo " $name | {$time}s | {$percent}%\n"; 193 if (! $percent ) { 194 echo " \u{258C}\n\n"; 195 } else { 196 $bar = ''; 197 for ( $i = 0; $i < $chars; $i++ ) { 198 $bar .= ' '; 199 } 200 echo WP_CLI::colorize(" %8$bar%n\n\n"); 201 } 202 } 248 } 249 if ( $this->out == 'csv') { 250 echo $buffer; 251 252 } elseif ( $this->out == 'json' ) { 253 echo json_encode( $buffer ); 254 } 255 203 256 exit; 204 257 } … … 281 334 " --dest=<URL to profile> **". __('Pro version only', 'code-profiler') ."**\n". 282 335 " ". __('Path to the WordPress page or post to profile. If missing, profile the frontend.', 'code-profiler') ."\n\n". 283 " --user=<id|login|email> \n".336 " --user=<id|login|email> (optional)\n". 284 337 " ". __('Run the profiler as the corresponding WordPress user. If missing, run as an unauthenticated user.', 'code-profiler') ."\n\n". 285 " --u=<username>\n". 286 " ". __('HTTP Basic authentication username. You will be prompted to enter your password.', 'code-profiler') ."\n\n"); 338 " --u=<username> (optional)\n". 339 " ". __('HTTP Basic authentication username. You will be prompted to enter your password.', 'code-profiler') ."\n\n". 340 " --out=<json|csv> (optional)\n". 341 " ". __('Return the results in JSON-encoded or CSV format.', 'code-profiler') ."\n\n" ); 287 342 exit; 288 343 } -
code-profiler/trunk/lib/helper.php
r3371018 r3375722 140 140 version_compare( $cp_options['version'], CODE_PROFILER_VERSION, '<') ) { 141 141 142 // Version 1.1 143 if ( version_compare( $cp_options['version'], '1.1', '<' ) ) { 144 if (! isset( $cp_options['enable_wpcli'] ) ) { 145 $cp_options['enable_wpcli'] = 1; 146 } 147 } 148 149 // Version 1.2 150 if ( version_compare( $cp_options['version'], '1.2', '<' ) ) { 151 if (! isset( $cp_options['disable_wpcron'] ) ) { 152 $cp_options['disable_wpcron'] = 1; 153 } 154 } 155 156 // Version 1.3.1 157 if ( version_compare( $cp_options['version'], '1.3.1', '<' ) ) { 158 if (! isset( $cp_options['http_response'] ) ) { 159 $cp_options['http_response'] = '^(?:3|4|5)\d{2}$'; 160 } 161 } 162 163 // Version 1.5 164 if ( version_compare( $cp_options['version'], '1.5', '<' ) ) { 165 if (! isset( $cp_options['accuracy'] ) ) { 166 $cp_options['accuracy'] = 1; 167 } 168 } 169 170 // Version 1.7.5 171 if ( version_compare( $cp_options['version'], '1.7.5', '<' ) ) { 172 if (! isset( $cp_options['php_error'] ) ) { 173 $cp_options['php_error'] = 1; 174 } 175 } 176 177 // Version 1.8 178 if ( version_compare( $cp_options['version'], '1.8', '<' ) ) { 179 if ( isset( $cp_options['mem_where'] ) ) { 180 $cp_options['mem']['x_end'] = $cp_options['mem_where']; 181 unset( $cp_options['mem_where'] ); 182 } 183 if ( isset( $cp_options['mem_post'] ) ) { 184 $cp_options['mem']['post'] = $cp_options['mem_post']; 185 unset( $cp_options['mem_post'] ); 186 } 187 if ( isset( $cp_options['mem_user'] ) ) { 188 $cp_options['mem']['x_auth'] = $cp_options['mem_user']; 189 unset( $cp_options['mem_user'] ); 190 } 191 if ( isset( $cp_options['mem_username'] ) ) { 192 $cp_options['mem']['username'] = $cp_options['mem_username']; 193 unset( $cp_options['mem_username'] ); 194 } 195 if ( isset( $cp_options['mem_method'] ) ) { 196 $cp_options['mem']['method'] = $cp_options['mem_method']; 197 unset( $cp_options['mem_method'] ); 198 } 199 if ( isset( $cp_options['mem_theme'] ) ) { 200 $cp_options['mem']['theme'] = $cp_options['mem_theme']; 201 unset( $cp_options['mem_theme'] ); 202 } 203 if ( isset( $cp_options['ua'] ) ) { 204 $cp_options['mem']['user_agent'] = $cp_options['ua']; 205 unset( $cp_options['ua'] ); 206 } 207 if ( isset( $cp_options['cookies'] ) ) { 208 $cp_options['mem']['cookies'] = $cp_options['cookies']; 209 unset( $cp_options['cookies'] ); 210 } 211 if ( isset( $cp_options['mem_content_type'] ) ) { 212 $cp_options['mem']['content_type'] = $cp_options['mem_content_type']; 213 unset( $cp_options['mem_content_type'] ); 214 } 215 if ( isset( $cp_options['payload'] ) ) { 216 $cp_options['mem']['payload'] = $cp_options['payload']; 217 unset( $cp_options['payload'] ); 218 } 219 if ( isset( $cp_options['custom_headers'] ) ) { 220 $cp_options['mem']['custom_headers'] = $cp_options['custom_headers']; 221 unset( $cp_options['custom_headers'] ); 222 } 223 if ( isset( $cp_options['exclusions'] ) ) { 224 $cp_options['mem']['exclusions'] = $cp_options['exclusions']; 225 unset( $cp_options['exclusions'] ); 226 } 227 } 228 229 // Version 1.8.1 230 if ( version_compare( $cp_options['version'], '1.8.1', '<' ) ) { 231 CodeProfiler_WPCron::install(); 232 } 233 234 // Adjust current version 142 235 $cp_options['version'] = CODE_PROFILER_VERSION; 143 144 // Version 1.1145 if (! isset( $cp_options['enable_wpcli'] ) ) {146 $cp_options['enable_wpcli'] = 1;147 }148 149 // Version 1.2150 if (! isset( $cp_options['disable_wpcron'] ) ) {151 $cp_options['disable_wpcron'] = 1;152 }153 154 // Version 1.3.1155 if (! isset( $cp_options['http_response'] ) ) {156 $cp_options['http_response'] = '^(?:3|4|5)\d{2}$';157 }158 159 // Version 1.5160 if (! isset( $cp_options['accuracy'] ) ) {161 $cp_options['accuracy'] = 1;162 }163 164 // Version 1.7.5165 if (! isset( $cp_options['php_error'] ) ) {166 $cp_options['php_error'] = 1;167 }168 169 // Version 1.8170 if ( isset( $cp_options['mem_where'] ) ) {171 $cp_options['mem']['x_end'] = $cp_options['mem_where'];172 unset( $cp_options['mem_where'] );173 }174 if ( isset( $cp_options['mem_post'] ) ) {175 $cp_options['mem']['post'] = $cp_options['mem_post'];176 unset( $cp_options['mem_post'] );177 }178 if ( isset( $cp_options['mem_user'] ) ) {179 $cp_options['mem']['x_auth'] = $cp_options['mem_user'];180 unset( $cp_options['mem_user'] );181 }182 if ( isset( $cp_options['mem_username'] ) ) {183 $cp_options['mem']['username'] = $cp_options['mem_username'];184 unset( $cp_options['mem_username'] );185 }186 if ( isset( $cp_options['mem_method'] ) ) {187 $cp_options['mem']['method'] = $cp_options['mem_method'];188 unset( $cp_options['mem_method'] );189 }190 if ( isset( $cp_options['mem_theme'] ) ) {191 $cp_options['mem']['theme'] = $cp_options['mem_theme'];192 unset( $cp_options['mem_theme'] );193 }194 if ( isset( $cp_options['ua'] ) ) {195 $cp_options['mem']['user_agent'] = $cp_options['ua'];196 unset( $cp_options['ua'] );197 }198 if ( isset( $cp_options['cookies'] ) ) {199 $cp_options['mem']['cookies'] = $cp_options['cookies'];200 unset( $cp_options['cookies'] );201 }202 if ( isset( $cp_options['mem_content_type'] ) ) {203 $cp_options['mem']['content_type'] = $cp_options['mem_content_type'];204 unset( $cp_options['mem_content_type'] );205 }206 if ( isset( $cp_options['payload'] ) ) {207 $cp_options['mem']['payload'] = $cp_options['payload'];208 unset( $cp_options['payload'] );209 }210 if ( isset( $cp_options['custom_headers'] ) ) {211 $cp_options['mem']['custom_headers'] = $cp_options['custom_headers'];212 unset( $cp_options['custom_headers'] );213 }214 if ( isset( $cp_options['exclusions'] ) ) {215 $cp_options['mem']['exclusions'] = $cp_options['exclusions'];216 unset( $cp_options['exclusions'] );217 }218 236 219 237 // Update version in the DB -
code-profiler/trunk/readme.txt
r3371018 r3375722 4 4 Requires at least: 5.0 5 5 Tested up to: 6.8 6 Stable tag: 1.8 6 Stable tag: 1.8.1 7 7 License: GPLv3 or later 8 8 Requires PHP: 7.1 … … 94 94 == Changelog == 95 95 96 = 1.8.1 (09 Octobre 2025) = 97 98 * When profiling with WP-CLI, the results can be returned in JSON-encoded or CSV format, using the new `--out=<json|csv>` parameter. The output can be redirected to a file, e.g., `wp code-profiler-pro run --out=csv > /tmp/results.csv`. 99 96 100 = 1.8 (1st October, 2025) = 97 101
Note: See TracChangeset
for help on using the changeset viewer.