| @@ -6,13 +6,13 @@ | ||
| 6 | 6 | | | | / _ \ / _` |/ _ \ | |_) | '__/ _ \| |_| | |/ _ \ '__| | |
| 7 | 7 | | | |__| (_) | (_| | __/ | __/| | | (_) | _| | | __/ | | |
| 8 | 8 | | \____\___/ \__,_|\___| |_| |_| \___/|_| |_|_|\___|_| | |
| 9 | 9 | | | |
| 10 | - | (c) Jerome Bruandet ~ https://nintechnet.com/codeprofiler/ | | |
| 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 | |
| 18 | 18 | class CodeProfiler_CLI extends WP_CLI_Command { |
| @@ -17,13 +17,12 @@ | ||
| 17 | 17 | |
| 18 | 18 | class CodeProfiler_CLI extends WP_CLI_Command { |
| 19 | 19 | |
| 20 | 20 | private $file; |
| 21 | - private $out = 'view'; | |
| 22 | 21 | private $cmd_view = 'wp code-profiler view'; |
| 23 | 22 | private $cmd_run = 'wp code-profiler run'; |
| 24 | 23 | |
| 25 | - /** | |
| 24 | + /* | |
| 26 | 25 | * Run the profiler. |
| 27 | 26 | * |
| 28 | 27 | */ |
| 29 | 28 | public function run( $args, $assoc_args ) { |
| @@ -29,33 +28,20 @@ | ||
| 29 | 28 | public function run( $args, $assoc_args ) { |
| 30 | 29 | |
| 31 | 30 | $this->is_enabled(); |
| 32 | 31 | |
| 33 | - $_POST['cp_nonce'] = wp_create_nonce('start_profiler_nonce'); | |
| 34 | - | |
| 35 | - $_POST['post'] = home_url( '/' ); | |
| 36 | - // Mark the connection as 'HTTPS' if needed. | |
| 37 | - if ( strtolower( parse_url( $_POST['post'], PHP_URL_SCHEME ) ) == 'https') { | |
| 38 | - $_SERVER['HTTPS'] = 'on'; | |
| 39 | - } | |
| 40 | - | |
| 41 | - if (! empty( $assoc_args['wpcron'] ) ) { | |
| 42 | - $_POST['x_end'] = 'wpcron'; | |
| 43 | - $_POST['post'] = $assoc_args['wpcron']; | |
| 44 | - | |
| 32 | + $_POST['cp_nonce'] = wp_create_nonce( 'start_profiler_nonce' ); | |
| 33 | + $_POST['post'] = home_url( '/' ); | |
| 34 | + $_POST['where'] = 'frontend'; | |
| 35 | + // Detect if we're authenticated or not | |
| 36 | + if ( is_user_logged_in() === true ) { | |
| 37 | + $_POST['user'] = 'authenticated'; | |
| 45 | 38 | } else { |
| 46 | - $_POST['x_end'] = 'frontend'; | |
| 47 | - // Detect if we're authenticated or not | |
| 48 | - if ( is_user_logged_in() === true ) { | |
| 49 | - $_POST['x_auth'] = 'authenticated'; | |
| 50 | - } else { | |
| 51 | - $_POST['x_auth'] = 'unauthenticated'; | |
| 52 | - } | |
| 39 | + $_POST['user'] = 'unauthenticated'; | |
| 53 | 40 | } |
| 41 | + $_POST['profile'] = 'WP-CLI_' . time(); | |
| 42 | + $_POST['ua'] = 'FireFox'; | |
| 54 | 43 | |
| 55 | - $_POST['profile'] = 'WP-CLI_' . time(); | |
| 56 | - $_POST['user_agent'] = 'Firefox'; | |
| 57 | - | |
| 58 | 44 | if (! empty( $assoc_args['dest'] ) ) { |
| 59 | 45 | $message = sprintf( |
| 60 | 46 | __('The "%s" option is only available in the pro version of Code Profiler.', 'code-profiler'), |
| 61 | 47 | '--dest' |
| @@ -63,15 +49,11 @@ | ||
| 63 | 49 | WP_CLI::error( $message ); |
| 64 | 50 | exit; |
| 65 | 51 | } |
| 66 | 52 | |
| 67 | - if (! empty( $assoc_args['out'] ) && in_array( $assoc_args['out'], ['json', 'csv'] ) ) { | |
| 68 | - $this->out = $assoc_args['out']; | |
| 69 | - } | |
| 70 | - | |
| 71 | 53 | // HTTP basic authentication |
| 72 | 54 | if (! empty( $assoc_args['u'] ) ) { |
| 73 | - _e('Enter your HTTP basic authentication password:', 'code-profiler'); | |
| 55 | + _e('Enter your HTTP basic authentication password:', 'code-profiler' ); | |
| 74 | 56 | echo ' '; |
| 75 | 57 | echo "\033[30;40m"; |
| 76 | 58 | $password = trim( stream_get_line( STDIN, 255, PHP_EOL) ); |
| 77 | 59 | echo "\033[0m"; |
| @@ -77,22 +59,20 @@ | ||
| 77 | 59 | echo "\033[0m"; |
| 78 | 60 | $_POST['Authorization'] = 'Basic '. base64_encode( "{$assoc_args['u']}:{$password}" ); |
| 79 | 61 | } |
| 80 | 62 | |
| 81 | - if ( $this->out == 'view') { | |
| 82 | - WP_CLI::log( sprintf( | |
| 83 | - __('Starting Code Profiler v%s (profile: %s)', 'code-profiler'), | |
| 84 | - CODE_PROFILER_VERSION, $_POST['profile'] ) . "\n" | |
| 85 | - ); | |
| 63 | + WP_CLI::log( sprintf( | |
| 64 | + __('Starting Code Profiler v%s (profile: %s)', 'code-profiler' ), | |
| 65 | + CODE_PROFILER_VERSION, $_POST['profile'] ) . "\n" | |
| 66 | + ); | |
| 86 | 67 | |
| 87 | - $progress = \WP_CLI\Utils\make_progress_bar( '', 3 ); | |
| 88 | - $progress->tick(); | |
| 89 | - } | |
| 68 | + $progress = \WP_CLI\Utils\make_progress_bar( '', 3 ); | |
| 69 | + $progress->tick(); | |
| 90 | 70 | |
| 91 | 71 | // Run the profiler |
| 92 | - $response = json_decode( CodeProfiler_helpers::codeprofiler_start_profiler(), true ); | |
| 72 | + $response = json_decode( codeprofiler_start_profiler(), true ); | |
| 93 | 73 | if ( $response === false ) { |
| 94 | - $message = __('Unknown error returned by AJAX', 'code-profiler'); | |
| 74 | + $message = __('Unknown error returned by AJAX', 'code-profiler' ); | |
| 95 | 75 | WP_CLI::error( $message ); |
| 96 | 76 | exit; |
| 97 | 77 | } |
| 98 | 78 | if ( $response['status'] == 'error' ) { |
| @@ -99,17 +79,15 @@ | ||
| 99 | 79 | WP_CLI::error( $response['message'] ); |
| 100 | 80 | exit; |
| 101 | 81 | } |
| 102 | 82 | |
| 103 | - if ( $this->out == 'view') { | |
| 104 | - $progress->tick(); | |
| 105 | - } | |
| 83 | + $progress->tick(); | |
| 106 | 84 | |
| 107 | 85 | // All good, run the parser |
| 108 | 86 | $_POST['microtime'] = $response['microtime']; |
| 109 | - $response = json_decode( CodeProfiler_helpers::codeprofiler_prepare_report(), true ); | |
| 87 | + $response = json_decode( codeprofiler_prepare_report(), true ); | |
| 110 | 88 | if ( $response === false ) { |
| 111 | - $message = __('Unknown error returned by AJAX', 'code-profiler'); | |
| 89 | + $message = __('Unknown error returned by AJAX', 'code-profiler' ); | |
| 112 | 90 | WP_CLI::error( $message ); |
| 113 | 91 | exit; |
| 114 | 92 | } |
| 115 | 93 | if ( $response['status'] == 'error' ) { |
| @@ -116,37 +94,31 @@ | ||
| 116 | 94 | WP_CLI::error( $response['message'] ); |
| 117 | 95 | exit; |
| 118 | 96 | } |
| 119 | 97 | |
| 120 | - if ( $this->out == 'view') { | |
| 121 | - $progress->tick(); | |
| 122 | - $progress->finish(); | |
| 123 | - } | |
| 98 | + $progress->tick(); | |
| 99 | + $progress->finish(); | |
| 124 | 100 | |
| 125 | 101 | // Run the parser and show the results |
| 126 | - $this->view( $args, $assoc_args, $response['cp_profile'] ); | |
| 102 | + $this->view( $response['cp_profile'] ); | |
| 127 | 103 | |
| 128 | 104 | exit; |
| 129 | 105 | } |
| 130 | 106 | |
| 131 | 107 | |
| 132 | - /** | |
| 108 | + /* | |
| 133 | 109 | * Display last created profile. |
| 134 | 110 | * |
| 135 | 111 | */ |
| 136 | - public function view( $args = [], $assoc_args = [], $profile = '') { | |
| 112 | + public function view( $profile = '' ) { | |
| 137 | 113 | |
| 138 | 114 | $this->is_enabled(); |
| 139 | 115 | |
| 140 | - if (! empty( $assoc_args['out'] ) && in_array( $assoc_args['out'], ['json', 'csv'] ) ) { | |
| 141 | - $this->out = $assoc_args['out']; | |
| 142 | - } | |
| 143 | - | |
| 144 | 116 | // The profile was just created (`wp code-profiler run`)... |
| 145 | 117 | if (! empty( $profile ) ) { |
| 146 | 118 | $this->file = code_profiler_get_profile_path( $profile ); |
| 147 | 119 | if ( $this->file === false ) { |
| 148 | - $message = __('Cannot find the profile file', 'code-profiler'); | |
| 120 | + $message = __('Cannot find the profile file', 'code-profiler' ); | |
| 149 | 121 | WP_CLI::error( $message ); |
| 150 | 122 | exit; |
| 151 | 123 | } |
| 152 | 124 | $this->file .= '.slugs.profile'; |
| @@ -155,9 +127,9 @@ | ||
| 155 | 127 | } else { |
| 156 | 128 | $this->file = $this->find_last_profile(); |
| 157 | 129 | if ( $this->file === false ) { |
| 158 | 130 | $message = sprintf( |
| 159 | - __('No profile found. Run the profiler at least once to create a profile: %s', 'code-profiler'), | |
| 131 | + __('No profile found. Run the profiler at least once to create a profile: %s', 'code-profiler' ), | |
| 160 | 132 | $this->cmd_run |
| 161 | 133 | ); |
| 162 | 134 | WP_CLI::error( $message ); |
| 163 | 135 | exit; |
| @@ -169,37 +141,19 @@ | ||
| 169 | 141 | '`'. CODE_PROFILER_UPLOAD_DIR .'/\d{10}\.\d+\.(.+?)\.slugs\.profile$`', |
| 170 | 142 | $this->file, |
| 171 | 143 | $match |
| 172 | 144 | ); |
| 145 | + $message = sprintf( __('Viewing: %s', 'code-profiler'), $match[1] ); | |
| 146 | + $date = date('Y/m/d \@ H:i:s', filemtime( $this->file ) ); | |
| 147 | + echo WP_CLI::colorize("\n%Y$message ~ $date%n\n\n"); | |
| 173 | 148 | |
| 174 | - $cp_slug = __('Slug', 'code-profiler'); | |
| 175 | - $cp_time = __('Execution time', 'code-profiler'); | |
| 176 | - $cp_name = __('Name', 'code-profiler'); | |
| 177 | - $cp_type = __('Type', 'code-profiler'); | |
| 149 | + // Display stats | |
| 150 | + $summary_file = str_replace( '.slugs.profile', '', $this->file ); | |
| 151 | + echo code_profiler_getsummarystats( $summary_file, 'text' ); | |
| 178 | 152 | |
| 179 | - if ( $this->out == 'view' ) { | |
| 180 | - $buffer = ''; | |
| 181 | - $message = sprintf( __('Viewing: %s', 'code-profiler'), $match[1] ); | |
| 182 | - $date = date('Y/m/d \@ H:i:s', filemtime( $this->file ) ); | |
| 183 | - echo WP_CLI::colorize("\n%Y$message ~ $date%n\n\n"); | |
| 184 | - // Display stats | |
| 185 | - $summary_file = str_replace('.slugs.profile', '', $this->file ); | |
| 186 | - echo code_profiler_getsummarystats( $summary_file, 'text'); | |
| 187 | - /** | |
| 188 | - * CSV output. | |
| 189 | - */ | |
| 190 | - } elseif ( $this->out == 'csv' ) { | |
| 191 | - $buffer = "$cp_slug,\"$cp_time\",$cp_name,$cp_type\n"; | |
| 192 | - /** | |
| 193 | - * JSON-encoded output. | |
| 194 | - */ | |
| 195 | - } else { | |
| 196 | - $buffer = []; | |
| 197 | - } | |
| 198 | - | |
| 199 | 153 | $slugs = $this->read_profile(); |
| 200 | 154 | |
| 201 | - $cp_options = get_option('code-profiler'); | |
| 155 | + $cp_options = get_option( 'code-profiler' ); | |
| 202 | 156 | |
| 203 | 157 | // Get the total time and the slowest item |
| 204 | 158 | $total_time = 0; |
| 205 | 159 | foreach( $slugs as $k => $v ) { |
| @@ -209,72 +163,44 @@ | ||
| 209 | 163 | $coeff = number_format( $slugs[0][1] / $total_time * 100 ); |
| 210 | 164 | |
| 211 | 165 | foreach( $slugs as $k => $v ) { |
| 212 | 166 | |
| 213 | - if ( $this->out == 'csv') { | |
| 214 | - $buffer .= "{$v[0]},{$v[1]},{$v[2]},{$v[3]}\n"; | |
| 215 | - | |
| 216 | - } elseif( $this->out == 'json') { | |
| 217 | - $buffer[] = [ | |
| 218 | - $cp_slug = $v[0], | |
| 219 | - $cp_time = $v[1], | |
| 220 | - $cp_name = $v[2], | |
| 221 | - $cp_type = $v[3] | |
| 222 | - ]; | |
| 223 | - | |
| 167 | + // Display name, time and % | |
| 168 | + if ( $cp_options['display_name'] == 'slug' ) { | |
| 169 | + $name = $v[0]; | |
| 224 | 170 | } else { |
| 225 | - // Display name, time and % | |
| 226 | - if ( isset( $cp_options['display_name'] ) && $cp_options['display_name'] == 'slug' ) { | |
| 227 | - $name = $v[0]; | |
| 228 | - } else { | |
| 229 | - $name = $v[2]; | |
| 171 | + $name = $v[2]; | |
| 172 | + } | |
| 173 | + $time = number_format( $v[1], 3 ); | |
| 174 | + $percent = number_format( $v[1] / $total_time * 100 ); | |
| 175 | + $chars = number_format( $percent * 80 / $coeff ); | |
| 176 | + // We use `echo` instead of `WP_CLI::log` because the layout could | |
| 177 | + // be all messed-up when some caching plugins such as LiteSpeed Cache | |
| 178 | + // are activated. | |
| 179 | + echo " $name | {$time}s | {$percent}%\n"; | |
| 180 | + if (! $percent ) { | |
| 181 | + echo " \u{258C}\n\n"; | |
| 182 | + } else { | |
| 183 | + $bar = ''; | |
| 184 | + for ( $i = 0; $i < $chars; $i++ ) { | |
| 185 | + $bar .= ' '; | |
| 230 | 186 | } |
| 231 | - // Inform if it's the theme or a mu-plugin | |
| 232 | - if ( $v[3] == 'theme') { | |
| 233 | - $name .= ' (theme)'; | |
| 234 | - } elseif ( $v[3] == 'mu-plugin') { | |
| 235 | - $name .= ' (mu-plugin)'; | |
| 236 | - } | |
| 237 | - | |
| 238 | - $time = number_format( $v[1], 3 ); | |
| 239 | - $percent = number_format( $v[1] / $total_time * 100 ); | |
| 240 | - $chars = number_format( $percent * 80 / $coeff ); | |
| 241 | - // We use `echo` instead of `WP_CLI::log` because the layout could | |
| 242 | - // be all messed-up when some caching plugins such as LiteSpeed Cache | |
| 243 | - // are activated. | |
| 244 | - echo " $name | {$time}s | {$percent}%\n"; | |
| 245 | - if (! $percent ) { | |
| 246 | - echo " \u{258C}\n\n"; | |
| 247 | - } else { | |
| 248 | - $bar = ''; | |
| 249 | - for ( $i = 0; $i < $chars; $i++ ) { | |
| 250 | - $bar .= ' '; | |
| 251 | - } | |
| 252 | - echo WP_CLI::colorize(" %8$bar%n\n\n"); | |
| 253 | - } | |
| 187 | + echo WP_CLI::colorize(" %8$bar%n\n\n"); | |
| 254 | 188 | } |
| 255 | 189 | } |
| 256 | - if ( $this->out == 'csv') { | |
| 257 | - echo $buffer; | |
| 258 | - | |
| 259 | - } elseif ( $this->out == 'json' ) { | |
| 260 | - echo json_encode( $buffer ); | |
| 261 | - } | |
| 262 | - | |
| 263 | 190 | exit; |
| 264 | 191 | } |
| 265 | 192 | |
| 266 | - | |
| 267 | - /** | |
| 193 | + /* | |
| 268 | 194 | * Retrieve the full path/name to the last created profile |
| 269 | 195 | * |
| 270 | 196 | */ |
| 271 | 197 | private function find_last_profile() { |
| 272 | 198 | |
| 273 | - $profiles = code_profiler_glob( CODE_PROFILER_UPLOAD_DIR, '\.slugs\.profile$', true ); | |
| 199 | + $profiles = glob( CODE_PROFILER_UPLOAD_DIR .'/*.slugs.profile' ); | |
| 274 | 200 | |
| 275 | 201 | array_multisort( |
| 276 | - array_map('filectime', $profiles ), | |
| 202 | + array_map( 'filectime', $profiles ), | |
| 277 | 203 | SORT_NUMERIC, |
| 278 | 204 | SORT_DESC, |
| 279 | 205 | $profiles |
| 280 | 206 | ); |
| @@ -286,15 +212,15 @@ | ||
| 286 | 212 | return false; |
| 287 | 213 | } |
| 288 | 214 | |
| 289 | 215 | |
| 290 | - /** | |
| 216 | + /* | |
| 291 | 217 | * Parse and return the profile's data |
| 292 | 218 | * |
| 293 | 219 | */ |
| 294 | 220 | private function read_profile() { |
| 295 | 221 | |
| 296 | - $profile = str_replace('.slugs.profile', '', $this->file ); | |
| 222 | + $profile = str_replace( '.slugs.profile', '', $this->file ); | |
| 297 | 223 | |
| 298 | 224 | $res = code_profiler_get_profile_data( $profile ); |
| 299 | 225 | if ( isset( $res['error'] ) ) { |
| 300 | 226 | WP_CLI::error( $res['error'] ); |
| @@ -308,16 +234,15 @@ | ||
| 308 | 234 | |
| 309 | 235 | return $res; |
| 310 | 236 | } |
| 311 | 237 | |
| 312 | - | |
| 313 | - /** | |
| 238 | + /* | |
| 314 | 239 | * Verify wether WP CLI integration is enabled or not |
| 315 | 240 | * |
| 316 | 241 | */ |
| 317 | 242 | private function is_enabled() { |
| 318 | 243 | |
| 319 | - $cp_options = get_option('code-profiler'); | |
| 244 | + $cp_options = get_option( 'code-profiler' ); | |
| 320 | 245 | if ( empty( $cp_options['enable_wpcli'] ) ) { |
| 321 | 246 | $message = __('WP-CLI integration is disabled. To enable it, log in to your admin dashboard and go to the Code Profiler settings page.', 'code-profiler'); |
| 322 | 247 | WP_CLI::error( $message ); |
| 323 | 248 | exit; |
| @@ -323,10 +248,9 @@ | ||
| 323 | 248 | exit; |
| 324 | 249 | } |
| 325 | 250 | } |
| 326 | 251 | |
| 327 | - | |
| 328 | - /** | |
| 252 | + /* | |
| 329 | 253 | * Display help screen and quit. |
| 330 | 254 | * |
| 331 | 255 | */ |
| 332 | 256 | public function help() { |
| @@ -332,32 +256,24 @@ | ||
| 332 | 256 | public function help() { |
| 333 | 257 | |
| 334 | 258 | $this->is_enabled(); |
| 335 | 259 | |
| 336 | - WP_CLI::log("\nCode Profiler v". CODE_PROFILER_VERSION . | |
| 337 | - " (c)". date('Y') ." Jerome Bruandet & NinTechNet Limited ~ https://nintechnet.com/codeprofiler/\n\n". | |
| 260 | + WP_CLI::log( "\nCode Profiler v". CODE_PROFILER_VERSION . | |
| 261 | + " (c)". date('Y') ." Jerome Bruandet & NinTechNet Limited ~ https://code-profiler.com/\n\n". | |
| 338 | 262 | " {$this->cmd_view} ". __('View last created profile', 'code-profiler') ."\n". |
| 339 | - " {$this->cmd_run} ". __('Run the profiler in the frontend', 'code-profiler') ."\n\n". | |
| 263 | + " {$this->cmd_run} ". __('Run the profiler on the frontend', 'code-profiler') ."\n\n". | |
| 340 | 264 | __('GLOBAL PARAMETERS', 'code-profiler') ."\n\n". |
| 341 | 265 | " --dest=<URL to profile> **". __('Pro version only', 'code-profiler') ."**\n". |
| 342 | - " ". __('Path to the WordPress page or post to profile. If missing, profile the frontend.', 'code-profiler') ."\n\n". | |
| 343 | - " --wpcron=<cron event> (optional)\n". | |
| 344 | - " ". __('WordPress cron event to profile.', 'code-profiler') ."\n\n". | |
| 345 | - " --user=<id|login|email> (optional)\n". | |
| 266 | + " ". __('Path to the WordPress page or post to profile. If missing, profile the frontend.', 'code-profiler-pro') ."\n\n". | |
| 267 | + " --user=<id|login|email>\n". | |
| 346 | 268 | " ". __('Run the profiler as the corresponding WordPress user. If missing, run as an unauthenticated user.', 'code-profiler') ."\n\n". |
| 347 | - " --u=<username> (optional)\n". | |
| 348 | - " ". __('HTTP Basic authentication username. You will be prompted to enter your password.', 'code-profiler') ."\n\n". | |
| 349 | - " --out=<json|csv> (optional)\n". | |
| 350 | - " ". __('Return the results in JSON-encoded or CSV format.', 'code-profiler') ."\n\n" ); | |
| 269 | + " --u=<username>\n". | |
| 270 | + " ". __('HTTP Basic authentication username. You will be prompted to enter your password.', 'code-profiler') ."\n\n" ); | |
| 351 | 271 | exit; |
| 352 | 272 | } |
| 353 | 273 | |
| 354 | 274 | } |
| 355 | 275 | |
| 356 | -WP_CLI::add_command( | |
| 357 | - 'code-profiler', | |
| 358 | - 'CodeProfiler_CLI', | |
| 359 | - ['shortdesc' => 'Profile your blog with Code Profiler.'] | |
| 360 | -); | |
| 276 | +WP_CLI::add_command( 'code-profiler', 'CodeProfiler_CLI', ['shortdesc' => __('Profile your blog with Code Profiler.', 'code-profiler') ] ); | |
| 361 | 277 | |
| 362 | 278 | // ===================================================================== |
| 363 | 279 | // EOF |