| @@ -6,13 +6,13 @@ | ||
| 6 | 6 | | | | / _ \ / _` |/ _ \ | |_) | '__/ _ \| |_| | |/ _ \ '__| | |
| 7 | 7 | | | |__| (_) | (_| | __/ | __/| | | (_) | _| | | __/ | | |
| 8 | 8 | | \____\___/ \__,_|\___| |_| |_| \___/|_| |_|_|\___|_| | |
| 9 | 9 | | | |
| 10 | - | (c) Jerome Bruandet ~ https://code-profiler.com/ | | |
| 10 | + | (c) Jerome Bruandet ~ https://nintechnet.com/codeprofiler/ | | |
| 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 { |
| @@ -20,9 +20,9 @@ | ||
| 20 | 20 | private $file; |
| 21 | 21 | private $cmd_view = 'wp code-profiler view'; |
| 22 | 22 | private $cmd_run = 'wp code-profiler run'; |
| 23 | 23 | |
| 24 | - /* | |
| 24 | + /** | |
| 25 | 25 | * Run the profiler. |
| 26 | 26 | * |
| 27 | 27 | */ |
| 28 | 28 | public function run( $args, $assoc_args ) { |
| @@ -28,9 +28,9 @@ | ||
| 28 | 28 | public function run( $args, $assoc_args ) { |
| 29 | 29 | |
| 30 | 30 | $this->is_enabled(); |
| 31 | 31 | |
| 32 | - $_POST['cp_nonce'] = wp_create_nonce( 'start_profiler_nonce' ); | |
| 32 | + $_POST['cp_nonce'] = wp_create_nonce('start_profiler_nonce'); | |
| 33 | 33 | $_POST['post'] = home_url( '/' ); |
| 34 | 34 | $_POST['where'] = 'frontend'; |
| 35 | 35 | // Detect if we're authenticated or not |
| 36 | 36 | if ( is_user_logged_in() === true ) { |
| @@ -38,9 +38,9 @@ | ||
| 38 | 38 | } else { |
| 39 | 39 | $_POST['user'] = 'unauthenticated'; |
| 40 | 40 | } |
| 41 | 41 | $_POST['profile'] = 'WP-CLI_' . time(); |
| 42 | - $_POST['ua'] = 'FireFox'; | |
| 42 | + $_POST['ua'] = 'Firefox'; | |
| 43 | 43 | |
| 44 | 44 | if (! empty( $assoc_args['dest'] ) ) { |
| 45 | 45 | $message = sprintf( |
| 46 | 46 | __('The "%s" option is only available in the pro version of Code Profiler.', 'code-profiler'), |
| @@ -51,9 +51,9 @@ | ||
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | // HTTP basic authentication |
| 54 | 54 | if (! empty( $assoc_args['u'] ) ) { |
| 55 | - _e('Enter your HTTP basic authentication password:', 'code-profiler' ); | |
| 55 | + _e('Enter your HTTP basic authentication password:', 'code-profiler'); | |
| 56 | 56 | echo ' '; |
| 57 | 57 | echo "\033[30;40m"; |
| 58 | 58 | $password = trim( stream_get_line( STDIN, 255, PHP_EOL) ); |
| 59 | 59 | echo "\033[0m"; |
| @@ -60,9 +60,9 @@ | ||
| 60 | 60 | $_POST['Authorization'] = 'Basic '. base64_encode( "{$assoc_args['u']}:{$password}" ); |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | WP_CLI::log( sprintf( |
| 64 | - __('Starting Code Profiler v%s (profile: %s)', 'code-profiler' ), | |
| 64 | + __('Starting Code Profiler v%s (profile: %s)', 'code-profiler'), | |
| 65 | 65 | CODE_PROFILER_VERSION, $_POST['profile'] ) . "\n" |
| 66 | 66 | ); |
| 67 | 67 | |
| 68 | 68 | $progress = \WP_CLI\Utils\make_progress_bar( '', 3 ); |
| @@ -70,9 +70,9 @@ | ||
| 70 | 70 | |
| 71 | 71 | // Run the profiler |
| 72 | 72 | $response = json_decode( codeprofiler_start_profiler(), true ); |
| 73 | 73 | if ( $response === false ) { |
| 74 | - $message = __('Unknown error returned by AJAX', 'code-profiler' ); | |
| 74 | + $message = __('Unknown error returned by AJAX', 'code-profiler'); | |
| 75 | 75 | WP_CLI::error( $message ); |
| 76 | 76 | exit; |
| 77 | 77 | } |
| 78 | 78 | if ( $response['status'] == 'error' ) { |
| @@ -85,9 +85,9 @@ | ||
| 85 | 85 | // All good, run the parser |
| 86 | 86 | $_POST['microtime'] = $response['microtime']; |
| 87 | 87 | $response = json_decode( codeprofiler_prepare_report(), true ); |
| 88 | 88 | if ( $response === false ) { |
| 89 | - $message = __('Unknown error returned by AJAX', 'code-profiler' ); | |
| 89 | + $message = __('Unknown error returned by AJAX', 'code-profiler'); | |
| 90 | 90 | WP_CLI::error( $message ); |
| 91 | 91 | exit; |
| 92 | 92 | } |
| 93 | 93 | if ( $response['status'] == 'error' ) { |
| @@ -104,13 +104,13 @@ | ||
| 104 | 104 | exit; |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | |
| 108 | - /* | |
| 108 | + /** | |
| 109 | 109 | * Display last created profile. |
| 110 | 110 | * |
| 111 | 111 | */ |
| 112 | - public function view( $profile = '' ) { | |
| 112 | + public function view( $profile = '') { | |
| 113 | 113 | |
| 114 | 114 | $this->is_enabled(); |
| 115 | 115 | |
| 116 | 116 | // The profile was just created (`wp code-profiler run`)... |
| @@ -116,9 +116,9 @@ | ||
| 116 | 116 | // The profile was just created (`wp code-profiler run`)... |
| 117 | 117 | if (! empty( $profile ) ) { |
| 118 | 118 | $this->file = code_profiler_get_profile_path( $profile ); |
| 119 | 119 | if ( $this->file === false ) { |
| 120 | - $message = __('Cannot find the profile file', 'code-profiler' ); | |
| 120 | + $message = __('Cannot find the profile file', 'code-profiler'); | |
| 121 | 121 | WP_CLI::error( $message ); |
| 122 | 122 | exit; |
| 123 | 123 | } |
| 124 | 124 | $this->file .= '.slugs.profile'; |
| @@ -127,9 +127,9 @@ | ||
| 127 | 127 | } else { |
| 128 | 128 | $this->file = $this->find_last_profile(); |
| 129 | 129 | if ( $this->file === false ) { |
| 130 | 130 | $message = sprintf( |
| 131 | - __('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'), | |
| 132 | 132 | $this->cmd_run |
| 133 | 133 | ); |
| 134 | 134 | WP_CLI::error( $message ); |
| 135 | 135 | exit; |
| @@ -146,14 +146,14 @@ | ||
| 146 | 146 | $date = date('Y/m/d \@ H:i:s', filemtime( $this->file ) ); |
| 147 | 147 | echo WP_CLI::colorize("\n%Y$message ~ $date%n\n\n"); |
| 148 | 148 | |
| 149 | 149 | // Display stats |
| 150 | - $summary_file = str_replace( '.slugs.profile', '', $this->file ); | |
| 151 | - echo code_profiler_getsummarystats( $summary_file, 'text' ); | |
| 150 | + $summary_file = str_replace('.slugs.profile', '', $this->file ); | |
| 151 | + echo code_profiler_getsummarystats( $summary_file, 'text'); | |
| 152 | 152 | |
| 153 | 153 | $slugs = $this->read_profile(); |
| 154 | 154 | |
| 155 | - $cp_options = get_option( 'code-profiler' ); | |
| 155 | + $cp_options = get_option('code-profiler'); | |
| 156 | 156 | |
| 157 | 157 | // Get the total time and the slowest item |
| 158 | 158 | $total_time = 0; |
| 159 | 159 | foreach( $slugs as $k => $v ) { |
| @@ -169,8 +169,15 @@ | ||
| 169 | 169 | $name = $v[0]; |
| 170 | 170 | } else { |
| 171 | 171 | $name = $v[2]; |
| 172 | 172 | } |
| 173 | + // Inform if it's the theme or a mu-plugin | |
| 174 | + if ( $v[3] == 'theme') { | |
| 175 | + $name .= ' (theme)'; | |
| 176 | + } elseif ( $v[3] == 'mu-plugin') { | |
| 177 | + $name .= ' (mu-plugin)'; | |
| 178 | + } | |
| 179 | + | |
| 173 | 180 | $time = number_format( $v[1], 3 ); |
| 174 | 181 | $percent = number_format( $v[1] / $total_time * 100 ); |
| 175 | 182 | $chars = number_format( $percent * 80 / $coeff ); |
| 176 | 183 | // We use `echo` instead of `WP_CLI::log` because the layout could |
| @@ -189,18 +196,19 @@ | ||
| 189 | 196 | } |
| 190 | 197 | exit; |
| 191 | 198 | } |
| 192 | 199 | |
| 193 | - /* | |
| 200 | + | |
| 201 | + /** | |
| 194 | 202 | * Retrieve the full path/name to the last created profile |
| 195 | 203 | * |
| 196 | 204 | */ |
| 197 | 205 | private function find_last_profile() { |
| 198 | 206 | |
| 199 | - $profiles = glob( CODE_PROFILER_UPLOAD_DIR .'/*.slugs.profile' ); | |
| 207 | + $profiles = code_profiler_glob( CODE_PROFILER_UPLOAD_DIR, '\.slugs\.profile$', true ); | |
| 200 | 208 | |
| 201 | 209 | array_multisort( |
| 202 | - array_map( 'filectime', $profiles ), | |
| 210 | + array_map('filectime', $profiles ), | |
| 203 | 211 | SORT_NUMERIC, |
| 204 | 212 | SORT_DESC, |
| 205 | 213 | $profiles |
| 206 | 214 | ); |
| @@ -212,15 +220,15 @@ | ||
| 212 | 220 | return false; |
| 213 | 221 | } |
| 214 | 222 | |
| 215 | 223 | |
| 216 | - /* | |
| 224 | + /** | |
| 217 | 225 | * Parse and return the profile's data |
| 218 | 226 | * |
| 219 | 227 | */ |
| 220 | 228 | private function read_profile() { |
| 221 | 229 | |
| 222 | - $profile = str_replace( '.slugs.profile', '', $this->file ); | |
| 230 | + $profile = str_replace('.slugs.profile', '', $this->file ); | |
| 223 | 231 | |
| 224 | 232 | $res = code_profiler_get_profile_data( $profile ); |
| 225 | 233 | if ( isset( $res['error'] ) ) { |
| 226 | 234 | WP_CLI::error( $res['error'] ); |
| @@ -234,15 +242,16 @@ | ||
| 234 | 242 | |
| 235 | 243 | return $res; |
| 236 | 244 | } |
| 237 | 245 | |
| 238 | - /* | |
| 246 | + | |
| 247 | + /** | |
| 239 | 248 | * Verify wether WP CLI integration is enabled or not |
| 240 | 249 | * |
| 241 | 250 | */ |
| 242 | 251 | private function is_enabled() { |
| 243 | 252 | |
| 244 | - $cp_options = get_option( 'code-profiler' ); | |
| 253 | + $cp_options = get_option('code-profiler'); | |
| 245 | 254 | if ( empty( $cp_options['enable_wpcli'] ) ) { |
| 246 | 255 | $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'); |
| 247 | 256 | WP_CLI::error( $message ); |
| 248 | 257 | exit; |
| @@ -248,9 +257,10 @@ | ||
| 248 | 257 | exit; |
| 249 | 258 | } |
| 250 | 259 | } |
| 251 | 260 | |
| 252 | - /* | |
| 261 | + | |
| 262 | + /** | |
| 253 | 263 | * Display help screen and quit. |
| 254 | 264 | * |
| 255 | 265 | */ |
| 256 | 266 | public function help() { |
| @@ -256,24 +266,24 @@ | ||
| 256 | 266 | public function help() { |
| 257 | 267 | |
| 258 | 268 | $this->is_enabled(); |
| 259 | 269 | |
| 260 | - WP_CLI::log( "\nCode Profiler v". CODE_PROFILER_VERSION . | |
| 261 | - " (c)". date('Y') ." Jerome Bruandet & NinTechNet Limited ~ https://code-profiler.com/\n\n". | |
| 270 | + WP_CLI::log("\nCode Profiler v". CODE_PROFILER_VERSION . | |
| 271 | + " (c)". date('Y') ." Jerome Bruandet & NinTechNet Limited ~ https://nintechnet.com/codeprofiler/\n\n". | |
| 262 | 272 | " {$this->cmd_view} ". __('View last created profile', 'code-profiler') ."\n". |
| 263 | - " {$this->cmd_run} ". __('Run the profiler on the frontend', 'code-profiler') ."\n\n". | |
| 273 | + " {$this->cmd_run} ". __('Run the profiler in the frontend', 'code-profiler') ."\n\n". | |
| 264 | 274 | __('GLOBAL PARAMETERS', 'code-profiler') ."\n\n". |
| 265 | 275 | " --dest=<URL to profile> **". __('Pro version only', 'code-profiler') ."**\n". |
| 266 | - " ". __('Path to the WordPress page or post to profile. If missing, profile the frontend.', 'code-profiler-pro') ."\n\n". | |
| 276 | + " ". __('Path to the WordPress page or post to profile. If missing, profile the frontend.', 'code-profiler') ."\n\n". | |
| 267 | 277 | " --user=<id|login|email>\n". |
| 268 | 278 | " ". __('Run the profiler as the corresponding WordPress user. If missing, run as an unauthenticated user.', 'code-profiler') ."\n\n". |
| 269 | 279 | " --u=<username>\n". |
| 270 | - " ". __('HTTP Basic authentication username. You will be prompted to enter your password.', 'code-profiler') ."\n\n" ); | |
| 280 | + " ". __('HTTP Basic authentication username. You will be prompted to enter your password.', 'code-profiler') ."\n\n"); | |
| 271 | 281 | exit; |
| 272 | 282 | } |
| 273 | 283 | |
| 274 | 284 | } |
| 275 | 285 | |
| 276 | -WP_CLI::add_command( 'code-profiler', 'CodeProfiler_CLI', ['shortdesc' => __('Profile your blog with Code Profiler.', 'code-profiler') ] ); | |
| 286 | +WP_CLI::add_command('code-profiler', 'CodeProfiler_CLI', ['shortdesc' => __('Profile your blog with Code Profiler.', 'code-profiler') ] ); | |
| 277 | 287 | |
| 278 | 288 | // ===================================================================== |
| 279 | 289 | // EOF |