PluginProbe
Code Profiler – WordPress Performance Profiling and Debugging Made Easy / 1.6.8
Code Profiler – WordPress Performance Profiling and Debugging Made Easy v1.6.8
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
code-profiler / lib / menu_profiler.php

menu_profiler.php in Code Profiler – WordPress Performance Profiling and Debugging Made Easy 1.6.8, at lib/menu_profiler.php

385 lines 17.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 +=====================================================================+
4 | ____ _ ____ __ _ _ |
5 | / ___|___ __| | ___ | _ \ _ __ ___ / _(_) | ___ _ __ |
6 | | | / _ \ / _` |/ _ \ | |_) | '__/ _ \| |_| | |/ _ \ '__| |
7 | | |__| (_) | (_| | __/ | __/| | | (_) | _| | | __/ | |
8 | \____\___/ \__,_|\___| |_| |_| \___/|_| |_|_|\___|_| |
9 | |
10 | (c) Jerome Bruandet ~ https://code-profiler.com/ |
11 +=====================================================================+
12 */
13
14 if (! defined('ABSPATH') ) { die('Forbidden'); }
15
16 // =====================================================================
17 // Display Summary tab.
18
19 // Used to display profiler's error messages while running
20 echo '<div class="error notice is-dismissible" style="display:none" id="code-profiler-error"><p></p></div>';
21 echo code_profiler_display_tabs( 1 );
22 $error = 0;
23 if (! function_exists('register_tick_function') ) {
24 $error = 'register_tick_function';
25 } elseif (! function_exists('stream_wrapper_unregister') ) {
26 $error = 'stream_wrapper_unregister';
27 } elseif (! function_exists('stream_wrapper_register') ) {
28 $error = 'stream_wrapper_register';
29 } elseif (! function_exists('stream_wrapper_restore') ) {
30 $error = 'stream_wrapper_restore';
31 }
32 if (! empty( $error ) ) {
33 printf( CODE_PROFILER_ERROR_NOTICE, sprintf(
34 esc_html__('Your PHP configuration is missing the "%s" function. Code Profiler cannot run without it. Please contact your server administrator about this error.', 'code-profiler'), $error )
35 );
36 }
37 if (! is_writable( CODE_PROFILER_UPLOAD_DIR ) ) {
38 printf( CODE_PROFILER_ERROR_NOTICE, sprintf(
39 esc_html__('The following folder is not writable: %s. Please change its permissions or ownership so that Code Profiler can write to it.', 'code-profiler'), CODE_PROFILER_UPLOAD_DIR )
40 );
41 }
42
43 // Clear the log if it's too big
44 code_profiler_clearlog();
45
46 $home = home_url('/'); // Frontend
47 $site = site_url('/'); // Backend
48 // Get user login name
49 $current_user = wp_get_current_user();
50 // Profile's name
51 $profile = code_profiler_profile_name();
52
53 $cp_options = get_option('code-profiler');
54 // Memorized options
55 if ( empty( $cp_options['mem_where'] ) ) {
56 $cp_options['mem_where'] = 'frontend';
57 }
58 if ( empty( $cp_options['mem_post'] ) ) {
59 $cp_options['mem_post'] = '';
60 }
61 if ( empty( $cp_options['mem_user'] ) ) {
62 $cp_options['mem_user'] = 'unauthenticated';
63 }
64 if ( $cp_options['mem_where'] == 'backend') {
65 $cp_options['mem_user'] = 'authenticated';
66 }
67 if ( empty( $cp_options['mem_method'] ) ) {
68 $cp_options['mem_method'] = 'get';
69 }
70 $default_theme = get_option('stylesheet');
71 if ( empty( $cp_options['mem_theme'] ) ) {
72 $cp_options['mem_theme'] = $default_theme;
73 }
74 if ( empty( $cp_options['ua'] ) ) {
75 $cp_options['ua'] = 'Firefox';
76 }
77 $cookies = '';
78 $payload = '';
79 $custom_headers = '';
80 if ( isset( $cp_options['cookies'] ) ) {
81 // stripslashes is only needed for cookies
82 $cookies = trim( stripslashes( json_decode( $cp_options['cookies'] ) ) );
83 }
84 if ( empty( $cp_options['mem_content_type'] ) ||
85 ! in_array( $cp_options['mem_content_type'], [ 1, 2 ] ) ) {
86
87 $cp_options['mem_content_type'] = 1;
88 }
89 if ( isset( $cp_options['payload'] ) ) {
90 $payload = trim( json_decode( $cp_options['payload'] ) );
91 }
92 if ( isset( $cp_options['custom_headers'] ) ) {
93 $custom_headers = trim( json_decode( $cp_options['custom_headers'] ) );
94 }
95 ?>
96 <table class="form-table">
97 <tr>
98 <th scope="row"><?php esc_html_e('Page to profile', 'code-profiler') ?> <span class="code-profiler-tip" data-tip="<?php esc_attr_e('Select the page you want the profiler to analyze. It can be in the frontend of the site, in the admin backend or a custom URL.', 'code-profiler') ?>"></span></th>
99 <td>
100 <p><label><input type="radio" name="where" value="frontend" onclick="cpjs_front_or_backend(1);"<?php checked( $cp_options['mem_where'], 'frontend') ?> /> <?php esc_html_e('Website frontend', 'code-profiler') ?></label>
101 &nbsp;&nbsp;&nbsp;&nbsp;
102 <label><input type="radio" name="where" value="custom" onclick="cpjs_front_or_backend(3);"<?php checked( $cp_options['mem_where'], 'custom') ?> /> <?php esc_html_e('Custom post/URL', 'code-profiler') ?></label>
103 &nbsp;&nbsp;&nbsp;&nbsp;
104 <label><input type="radio" name="where" value="backend" onclick="cpjs_front_or_backend(2);"<?php checked( $cp_options['mem_where'], 'backend') ?> /> <?php esc_html_e('Admin backend', 'code-profiler') ?></label>
105 </p>
106 <br />
107 <?php
108 if ( $cp_options['mem_where'] == 'frontend') {
109 echo '<p id="p-frontend">';
110 } else {
111 echo '<p id="p-frontend" style="display:none">';
112 }
113 ?>
114 <?php esc_html_e('Select a page:', 'code-profiler') ?>
115 <select name="frontend" id="id-frontend">
116 <?php echo code_profiler_fetch_pages( $home, $cp_options ); ?>
117 </select>
118 </p>
119 <?php
120 if ( $cp_options['mem_where'] == 'custom') {
121 $value = $cp_options['mem_post'];
122 echo '<p id="p-custom">';
123 } else {
124 $value = '';
125 echo '<p id="p-custom" style="display:none">';
126 }
127 ?>
128 <input name="custom" id="id-custom" type="text" value="<?php echo esc_attr( $value ) ?>" size="70" placeholder="<?php
129 echo esc_attr(
130 sprintf(
131 __('e.g., %s', 'code-profiler'),
132 "{$home}foo/bar/"
133 )
134 ) ?>" />
135 </p>
136 <?php
137 if ( $cp_options['mem_where'] == 'backend') {
138 echo '<p id="p-backend">';
139 } else {
140 echo '<p id="p-backend" style="display:none">';
141 }
142 if (! empty( $cp_options['mem_username'] ) ) {
143 $username = $cp_options['mem_username'];
144 } else {
145 $username = $current_user->user_login;
146 }
147 ?>
148 <?php esc_html_e('Select a page:', 'code-profiler') ?>
149 <select name="backend" id="id-backend"><?php echo code_profiler_fetch_admin_pages( $site, $cp_options ) ?></select>
150 </p>
151 </td>
152 </tr>
153 <tr>
154 <th scope="row"><?php esc_html_e('Run profiler as', 'code-profiler') ?> <span class="code-profiler-tip" data-tip="<?php esc_attr_e('The profiler can access the requested page as an unauthenticated user (frontend only) or as an authenticated user (frontend and backend). You can enter the login name of any registered user, but make sure that the user has the required capability to access the page you are going to profile, otherwise Code Profiler will return an error if the user isn\'t allowed to access it.', 'code-profiler') ?>"></span></th>
155 <td>
156 <p><label><input type="radio" onclick="cpjs_authenticated(0);" name="user" value="unauthenticated" id="user-unauthenticated"<?php checked( $cp_options['mem_user'], 'unauthenticated'); disabled( $cp_options['mem_where'], 'backend'); ?> /> <?php esc_html_e('Unauthenticated user', 'code-profiler') ?></label></p>
157 <p><label><input type="radio" onclick="cpjs_authenticated(1);" name="user" value="authenticated" id="user-authenticated"<?php checked( $cp_options['mem_user'], 'authenticated') ?> /> <?php esc_html_e('Authenticated user', 'code-profiler') ?></label></p>
158 <p><label><?php esc_html_e('User:', 'code-profiler') ?> <input type="text" name="username" id="user-name" value ="<?php esc_attr_e( $username ) ?>"<?php disabled( $cp_options['mem_user'], 'unauthenticated') ?> /></label></p>
159 </td>
160 </tr>
161 <tr>
162 <th scope="row"><?php esc_html_e('Theme', 'code-profiler') ?> <span class="code-profiler-tip" data-tip="<?php esc_attr_e('If you want to profile different themes, you can use this option so that you don\'t need to modify your WordPress settings. The change will not affect your visitors, but only the profiler when it is running.', 'code-profiler') ?>"></span></th>
163 <td>
164 <select name="theme" id="id-theme">
165 <?php
166 $themes = code_profiler_get_themes();
167 foreach( $themes as $slug => $name ) {
168 if ( $slug == $default_theme ) {
169 echo '<option value=""'. selected( $cp_options['mem_theme'], $slug, false ) .'>'.
170 esc_html( $name['n'] ) .' '. __('(active theme)', 'code-profiler') .'</option>';
171 } else {
172 echo '<option value="'. esc_attr( $slug ) .'"'. selected( $cp_options['mem_theme'], $slug, false ) .'>'.
173 esc_html( $name['n'] ) .'</option>';
174 }
175 }
176 ?>
177 </select>
178 </td>
179 </tr>
180 <tr>
181 <th scope="row"><?php esc_html_e('User agent', 'code-profiler') ?> <span class="code-profiler-tip" data-tip="<?php esc_attr_e('Some themes and plugins may execute different code depending on the type of device used to visit the website (e.g., a desktop computer, a mobile phone, a search engine bot etc). This option lets you change the User-Agent request header used by Code Profiler.', 'code-profiler') ?>"></span></th>
182 <td>
183 <select name="ua" id="ua-id">
184 <?php
185 foreach( CODE_PROFILER_UA as $types => $types_array ) {
186 echo '<optgroup label="'. esc_attr( $types ) .'">';
187 foreach( $types_array as $name => $value ) {
188 echo '<option value="'. esc_attr( $name ) .'"'. selected( $cp_options['ua'], $name, false ) .'>'. esc_html( $name ) .'</option>';
189 }
190 echo '</optgroup>';
191 }
192 ?>
193 </select>
194 </td>
195 </tr>
196 <tr>
197 <th scope="row"><?php esc_html_e('Name of the profile', 'code-profiler') ?> <span class="code-profiler-tip" data-tip="<?php esc_attr_e('Enter the name for this profile. It will be saved to the "Profiles List" page, with all other profiles.', 'code-profiler') ?>"></span></th>
198 <td>
199 <p><label><input type="text" class="regular-text" name="profile" maxlength="100" value="<?php echo esc_attr( $profile ) ?>" id="profile-name" /></label></p>
200 <p class="description"><?php esc_html_e('Max 100 characters', 'code-profiler') ?></p>
201 </td>
202 </tr>
203 </table>
204 <?php
205 if (! empty( $cookies ) || ! empty( $custom_headers ) || $cp_options['mem_method'] == 'post') {
206 echo '<div id="cp-advanced-settings">';
207 $disabled_button = ' disabled';
208 } else {
209 echo '<div id="cp-advanced-settings" style="display:none">';
210 $disabled_button = '';
211 }
212 ?>
213 <table class="form-table">
214 <tr>
215 <th scope="row"><?php esc_html_e('HTTP Method', 'code-profiler') ?> <span class="code-profiler-tip" data-tip="<?php esc_attr_e('You can select which HTTP method will be used by the profiler: GET or POST. If you select POST, you can also send an optional payload.', 'code-profiler') ?>"></span></th>
216 <td>
217 <p>
218 <label>
219 <input onclick="cpjs_get_post(0);" type="radio" name="method" value="get" id="get-method"<?php checked( $cp_options['mem_method'], 'get') ?> /> GET(<?php esc_html_e('default', 'code-profiler') ?>)
220 </label>
221 </p>
222 <p>
223 <label>
224 <input onclick="cpjs_get_post(1);" type="radio" name="method" value="post" id="post-method"<?php checked( $cp_options['mem_method'], 'post') ?> /> POST
225 </label>
226 </p>
227 <p>
228 <?php esc_html_e('Content-type:', 'code-profiler') ?>
229 <select name="cp-content-type" id="id-content-type"<?php disabled( $cp_options['mem_method'], 'get') ?> onChange="cpjs_content_type(this.value);">
230 <option value="1"<?php selected( $cp_options['mem_content_type'], 1 )?>>application/x-www-form-urlencoded (<?php esc_html_e('default', 'code-profiler') ?>)</option>
231 <option value="2"<?php selected( $cp_options['mem_content_type'], 2 )?>>application/json</option>
232 </select>
233 </p>
234 <p>
235 <textarea name="post-value" id="post-value" class="regular-text code"<?php disabled( $cp_options['mem_method'], 'get') ?> maxlength="4000" rows="6"><?php echo esc_textarea( $payload ) ?></textarea>
236 </p>
237 <p class="description" id="ct-1"<?php echo code_profiler_hide( $cp_options['mem_content_type'], 2) ?>>
238 <?php printf( esc_html__('Optional POST payload in %s format, one item per line.', 'code-profiler'), '<code>name=value</code>') ?> <?php printf( esc_html__('%sView example%s', 'code-profiler'), '<a href="'. plugins_url('/static/help/http_method-1.png', dirname( __FILE__ ) ) .'" target="_blank" rel="noopener noreferrer">', '</a>') ?>
239 </p>
240 <p class="description" id="ct-2"<?php echo code_profiler_hide( $cp_options['mem_content_type'], 1) ?>>
241 <?php printf( esc_html__('Optional JSON-encoded payload.', 'code-profiler'), '<code>name=value</code>') ?> <?php printf( esc_html__('%sView example%s', 'code-profiler'), '<a href="'. plugins_url('/static/help/http_method-2.png', dirname( __FILE__ ) ) .'" target="_blank" rel="noopener noreferrer">', '</a>') ?>
242 </p>
243 </td>
244 </tr>
245 <tr>
246 <th scope="row"><?php esc_html_e('Cookie', 'code-profiler') ?> <span class="code-profiler-tip" data-tip="<?php esc_attr_e('Some websites may execute different code if the user has one or more specific cookies. You can use this field for that purpose.', 'code-profiler') ?>"></span></th>
247 <td>
248 <p><textarea name="cp-cookies" id="cp-cookies" class="regular-text code" maxlength="4000" rows="6"><?php echo esc_textarea( $cookies ) ?></textarea></p>
249 <p class="description"><?php printf( esc_html__('Optional cookie in %s format, one item per line.', 'code-profiler'), '<code>name=value</code>') ?> <?php printf( esc_html__('%sView example%s', 'code-profiler'), '<a href="'. plugins_url('/static/help/cookies.png', dirname( __FILE__ ) ) .'" target="_blank" rel="noopener noreferrer">', '</a>') ?></p>
250 </td>
251 </tr>
252 <tr>
253 <th scope="row" class="row-med"><?php esc_html_e('HTTP headers', 'code-profiler'); ?> <span class="code-profiler-tip" data-tip="<?php esc_attr_e('You can use this field to add custom HTTP headers or even override existing ones (e.g., host, user-agent, accept-language etc). HTTP header names are case-insensitive and Code Profiler will automatically convert them to lowercase. HTTP header values are case-sensitive and only ASCII printable characters are allowed.', 'code-profiler') ?>"></span></th>
254 <td>
255 <textarea autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" id="custom-headers" name="custom_headers" class="regular-text code" rows="6"><?php echo esc_textarea( $custom_headers ) ?></textarea>
256 <p class="description"><?php printf( esc_html__('Optional HTTP header in %s format, one item per line.', 'code-profiler'), '<code>name: value</code>') ?> <?php printf( esc_html__('%sView example%s', 'code-profiler'), '<a href="'. plugins_url('/static/help/custom_headers.png', dirname( __FILE__ ) ) .'" target="_blank" rel="noopener noreferrer">', '</a>') ?></p>
257 </td>
258 </tr>
259 </table>
260 </div>
261 <?php wp_nonce_field('start_profiler_nonce', 'cp_nonce', 0); ?>
262 <br />
263 <div>
264 <input type="button" class="button button-secondary" id="button-adv-settings" name="adv-settings" value="<?php esc_attr_e('Advanced Options', 'code-profiler') ?>" onclick="cpjs_show_adv_settings();"<?php echo $disabled_button ?> />
265 &nbsp;&nbsp;&nbsp;
266 <input type="button" class="button button-primary" id="start-profile" name="start-profile" onClick="cpjs_start_profiler();" value="<?php esc_attr_e('Start Profiling', 'code-profiler') ?> »" title="<?php esc_attr_e('Click to start profiling your code.', 'code-profiler') ?>" />
267 </div>
268 <div id="cp-progress-div" style="display:none">
269 <br />
270 <div class="cp-progress-bar"><span id="cp-span-progress" style="width:0%"></span></div>
271 <img style="vertical-align:middle;display:none" id="progress-gif" src="<?php echo plugins_url('/static/progress.gif', dirname (__FILE__ ) ) ?>" />&nbsp;&nbsp;<font id="progress-text" style="display:none"><?php esc_html_e('Starting the profiler...', 'code-profiler') ?></font>
272 </div>
273
274 <?php
275
276 // =====================================================================
277 // Fetch pages and posts.
278
279 function code_profiler_fetch_pages( $home, $cp_options ) {
280
281 $posts = '';
282
283 if ( empty( $cp_options['mem_post'] ) ) {
284 $cp_options['mem_post'] = $home;
285 }
286
287 if ( $cp_options['mem_post'] == $home ) {
288 $pages = sprintf(
289 '<option value="%1$s" title="%1$s" selected>%2$s</option>',
290 esc_attr( $home ),
291 esc_html__('Homepage', 'code-profiler')
292 );
293 } else {
294 $pages = sprintf(
295 '<option value="%1$s" title="%1$s">%2$s</option>',
296 esc_attr( $home ),
297 esc_html__('Homepage', 'code-profiler')
298 );
299 }
300 $args = [
301 'posts_per_page' => -1,
302 'post_type' => ['page'],
303 'post_status' => 'publish',
304 'orderby' => 'post_name',
305 'order' => 'ASC'
306 ];
307 $query = new WP_Query( $args );
308
309 if ( $query ) {
310 $items = $query->posts;
311 foreach( $items as $item ) {
312 if ( empty( $item->post_title ) ) {
313 $item->post_title = __('Untitled');
314 }
315 $permalink = get_permalink( $item->ID );
316
317 if ( $cp_options['mem_post'] == $permalink ) {
318 $pages .= sprintf(
319 '<option value="%1$s" title="%1$s" selected>%2$s</option>',
320 esc_attr( $permalink ),
321 esc_html( $item->post_title )
322 );
323 } else {
324 $pages .= sprintf(
325 '<option value="%1$s" title="%1$s">%2$s</option>',
326 esc_attr( $permalink ),
327 esc_html( $item->post_title )
328 );
329 }
330 }
331 }
332
333 return sprintf('<optgroup label="%s">%s</optgroup>',
334 esc_attr__('Pages', 'code-profiler'),
335 $pages
336 );
337 }
338
339 // =====================================================================
340 // Fetch admin pages (backend)
341
342 function code_profiler_fetch_admin_pages( $home, $cp_options ) {
343
344 $backend = '';
345
346 if ( empty( $cp_options['mem_post'] ) ) {
347 $cp_options['mem_post'] = $home;
348 }
349
350 if (! empty( $GLOBALS[ 'menu' ] ) ) {
351 foreach( $GLOBALS[ 'menu' ] as $menu => $value ) {
352 if (! empty( $value[0] ) && substr( $value[2], -4 ) == '.php') {
353 // E.g., "Comments", "Plugins" etc
354 $value[0] = trim( preg_replace('/\s*<.+$/', '', $value[0] ) );
355
356 if ( $cp_options['mem_post'] == "{$home}wp-admin/{$value[2]}" ) {
357 $backend .= sprintf(
358 '<option value="%1$s" title="%1$s" selected>%2$s</option>',
359 esc_attr("{$home}wp-admin/{$value[2]}"),
360 esc_html( $value[0] )
361 );
362 } else {
363 $backend .= sprintf(
364 '<option value="%1$s" title="%1$s">%2$s</option>',
365 esc_attr("{$home}wp-admin/{$value[2]}"),
366 esc_html( $value[0] )
367 );
368 }
369 }
370 }
371 }
372 if ( empty( $backend ) ) {
373 $backend = sprintf(
374 '<option value="%1$swp-admin/" title="%1$swp-admin/">%2$s</option>',
375 esc_attr( $home ),
376 esc_html__('Dashboard', 'code-profiler')
377 );
378
379 }
380 return $backend;
381 }
382
383 // =====================================================================
384 // EOF
385