PluginProbe
Code Profiler – WordPress Performance Profiling and Debugging Made Easy / 1.7.4
Code Profiler – WordPress Performance Profiling and Debugging Made Easy v1.7.4
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
← All changes | lib/class-cli.php +56 -130 trunk1.7.4 View file →
@@ -17,9 +17,8 @@
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 /**
@@ -30,32 +29,19 @@
30 29
31 30 $this->is_enabled();
32 31
33 32 $_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 -
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,12 +49,8 @@
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 55 _e('Enter your HTTP basic authentication password:', 'code-profiler');
74 56 echo ' ';
@@ -77,20 +59,18 @@
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 74 $message = __('Unknown error returned by AJAX', 'code-profiler');
95 75 WP_CLI::error( $message );
96 76 exit;
@@ -99,15 +79,13 @@
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 89 $message = __('Unknown error returned by AJAX', 'code-profiler');
112 90 WP_CLI::error( $message );
113 91 exit;
@@ -116,15 +94,13 @@
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
@@ -132,16 +108,12 @@
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 ) {
@@ -169,34 +141,16 @@
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 155 $cp_options = get_option('code-profiler');
202 156
@@ -209,58 +163,38 @@
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";
167 + // Display name, time and %
168 + if ( $cp_options['display_name'] == 'slug' ) {
169 + $name = $v[0];
170 + } else {
171 + $name = $v[2];
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 + }
215 179
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 -
180 + $time = number_format( $v[1], 3 );
181 + $percent = number_format( $v[1] / $total_time * 100 );
182 + $chars = number_format( $percent * 80 / $coeff );
183 + // We use `echo` instead of `WP_CLI::log` because the layout could
184 + // be all messed-up when some caching plugins such as LiteSpeed Cache
185 + // are activated.
186 + echo " $name | {$time}s | {$percent}%\n";
187 + if (! $percent ) {
188 + echo " \u{258C}\n\n";
224 189 } 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];
190 + $bar = '';
191 + for ( $i = 0; $i < $chars; $i++ ) {
192 + $bar .= ' ';
230 193 }
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 - }
194 + echo WP_CLI::colorize(" %8$bar%n\n\n");
254 195 }
255 196 }
256 - if ( $this->out == 'csv') {
257 - echo $buffer;
258 -
259 - } elseif ( $this->out == 'json' ) {
260 - echo json_encode( $buffer );
261 - }
262 -
263 197 exit;
264 198 }
265 199
266 200
@@ -339,25 +273,17 @@
339 273 " {$this->cmd_run} ". __('Run the profiler in the frontend', 'code-profiler') ."\n\n".
340 274 __('GLOBAL PARAMETERS', 'code-profiler') ."\n\n".
341 275 " --dest=<URL to profile> **". __('Pro version only', 'code-profiler') ."**\n".
342 276 " ". __('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".
277 + " --user=<id|login|email>\n".
346 278 " ". __('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" );
279 + " --u=<username>\n".
280 + " ". __('HTTP Basic authentication username. You will be prompted to enter your password.', 'code-profiler') ."\n\n");
351 281 exit;
352 282 }
353 283
354 284 }
355 285
356 -WP_CLI::add_command(
357 - 'code-profiler',
358 - 'CodeProfiler_CLI',
359 - ['shortdesc' => 'Profile your blog with Code Profiler.']
360 -);
286 +WP_CLI::add_command('code-profiler', 'CodeProfiler_CLI', ['shortdesc' => __('Profile your blog with Code Profiler.', 'code-profiler') ] );
361 287
362 288 // =====================================================================
363 289 // EOF