PluginProbe
Code Profiler – WordPress Performance Profiling and Debugging Made Easy / 1.6.1
Code Profiler – WordPress Performance Profiling and Debugging Made Easy v1.6.1
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.1, at lib/menu_profiler.php

324 lines 14.2 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 if ( empty( $cp_options['ua'] ) ) {
71 $cp_options['ua'] = 'Firefox';
72 }
73 $cookies = '';
74 $payload = '';
75 if ( isset( $cp_options['cookies'] ) ) {
76 $cookies = trim( json_decode( $cp_options['cookies'] ) );
77 }
78 if ( isset( $cp_options['payload'] ) ) {
79 $payload = trim( json_decode( $cp_options['payload'] ) );
80 }
81 ?>
82 <table class="form-table">
83 <tr>
84 <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>
85 <td>
86 <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>
87 &nbsp;&nbsp;&nbsp;&nbsp;
88 <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>
89 &nbsp;&nbsp;&nbsp;&nbsp;
90 <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>
91 </p>
92 <br />
93 <?php
94 if ( $cp_options['mem_where'] == 'frontend') {
95 echo '<p id="p-frontend">';
96 } else {
97 echo '<p id="p-frontend" style="display:none">';
98 }
99 ?>
100 <?php esc_html_e('Select a page:', 'code-profiler') ?>
101 <select name="frontend" id="id-frontend">
102 <?php echo code_profiler_fetch_pages( $home, $cp_options ); ?>
103 </select>
104 </p>
105 <?php
106 if ( $cp_options['mem_where'] == 'custom') {
107 $value = $cp_options['mem_post'];
108 echo '<p id="p-custom">';
109 } else {
110 $value = '';
111 echo '<p id="p-custom" style="display:none">';
112 }
113 ?>
114 <input name="custom" id="id-custom" type="text" value="<?php echo esc_attr( $value ) ?>" size="70" placeholder="<?php
115 echo esc_attr(
116 sprintf(
117 __('e.g., %s', 'code-profiler'),
118 "{$home}foo/bar/"
119 )
120 ) ?>" />
121 </p>
122 <?php
123 if ( $cp_options['mem_where'] == 'backend') {
124 echo '<p id="p-backend">';
125 } else {
126 echo '<p id="p-backend" style="display:none">';
127 }
128 if (! empty( $cp_options['mem_username'] ) ) {
129 $username = $cp_options['mem_username'];
130 } else {
131 $username = $current_user->user_login;
132 }
133 ?>
134 <?php esc_html_e('Select a page:', 'code-profiler') ?>
135 <select name="backend" id="id-backend"><?php echo code_profiler_fetch_admin_pages( $site, $cp_options ) ?></select>
136 </p>
137 </td>
138 </tr>
139 <tr>
140 <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 (e.g. an admin page in the backend), otherwise Code Profiler will return an error if the user isn\'t allowed to access it.', 'code-profiler') ?>"></span></th>
141 <td>
142 <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>
143 <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>
144 <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>
145 </td>
146 </tr>
147 <tr>
148 <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>
149 <td>
150 <select name="ua" id="ua-id">
151 <?php
152 foreach( CODE_PROFILER_UA as $types => $types_array ) {
153 echo '<optgroup label="'. esc_attr( $types ) .'">';
154 foreach( $types_array as $name => $value ) {
155 echo '<option value="'. esc_attr( $name ) .'"'. selected( $cp_options['ua'], $name ) .'>'. esc_html( $name ) .'</option>';
156 }
157 echo '</optgroup>';
158 }
159 ?>
160 </select>
161 </td>
162 </tr>
163 <tr>
164 <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>
165 <td>
166 <p><label><input type="text" class="regular-text" name="profile" maxlength="100" value="<?php echo esc_attr( $profile ) ?>" id="profile-name" /></label></p>
167 <p class="description"><?php esc_html_e('Max 100 characters', 'code-profiler') ?></p>
168 </td>
169 </tr>
170 </table>
171 <?php
172 if (! empty( $cookies ) || $cp_options['mem_method'] == 'post') {
173 echo '<div id="cp-advanced-settings">';
174 $disabled_button = ' disabled';
175 } else {
176 echo '<div id="cp-advanced-settings" style="display:none">';
177 $disabled_button = '';
178 }
179 ?>
180 <table class="form-table">
181 <tr>
182 <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>
183 <td>
184 <p><label><input onclick="cpjs_get_post(0);" type="radio" name="method" value="get" id="get-method"<?php checked( $cp_options['mem_method'], 'get') ?> /> <?php esc_html_e('GET (default)', 'code-profiler') ?></label></p>
185 <p><label><input onclick="cpjs_get_post(1);" type="radio" name="method" value="post" id="post-method"<?php checked( $cp_options['mem_method'], 'post') ?> /> POST</label></p>
186 <p><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></p>
187 <p class="description"><?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.png', dirname( __FILE__ ) ) .'" target="_blank" rel="noopener noreferrer">', '</a>') ?> </p>
188 </td>
189 </tr>
190 <tr>
191 <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>
192 <td>
193 <p><textarea name="cp-cookies" id="cp-cookies" class="regular-text code" maxlength="4000" rows="6"><?php echo esc_textarea( $cookies ) ?></textarea></p>
194 <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>
195 </td>
196 </tr>
197 </table>
198 </div>
199 <?php wp_nonce_field('start_profiler_nonce', 'cp_nonce', 0); ?>
200 <br />
201 <div>
202 <input type="button" class="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 ?> />
203 &nbsp;&nbsp;&nbsp;
204 <input type="button" class="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') ?>" />
205 </div>
206 <div id="cp-progress-div" style="display:none">
207 <br />
208 <div class="cp-progress-bar"><span id="cp-span-progress" style="width:0%"></span></div>
209 <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>
210 </div>
211
212 <?php
213
214 // =====================================================================
215 // Fetch pages and posts.
216
217 function code_profiler_fetch_pages( $home, $cp_options ) {
218
219 $posts = '';
220
221 if ( empty( $cp_options['mem_post'] ) ) {
222 $cp_options['mem_post'] = $home;
223 }
224
225 if ( $cp_options['mem_post'] == $home ) {
226 $pages = sprintf(
227 '<option value="%1$s" title="%1$s" selected>%2$s</option>',
228 esc_attr( $home ),
229 esc_html__('Homepage', 'code-profiler')
230 );
231 } else {
232 $pages = sprintf(
233 '<option value="%1$s" title="%1$s">%2$s</option>',
234 esc_attr( $home ),
235 esc_html__('Homepage', 'code-profiler')
236 );
237 }
238 $args = [
239 'posts_per_page' => -1,
240 'post_type' => ['page'],
241 'post_status' => 'publish',
242 'orderby' => 'post_name',
243 'order' => 'ASC'
244 ];
245 $query = new WP_Query( $args );
246
247 if ( $query ) {
248 $items = $query->posts;
249 foreach( $items as $item ) {
250 if ( empty( $item->post_title ) ) {
251 $item->post_title = __('Untitled');
252 }
253 $permalink = get_permalink( $item->ID );
254
255 if ( $cp_options['mem_post'] == $permalink ) {
256 $pages .= sprintf(
257 '<option value="%1$s" title="%1$s" selected>%2$s</option>',
258 esc_attr( $permalink ),
259 esc_html( $item->post_title )
260 );
261 } else {
262 $pages .= sprintf(
263 '<option value="%1$s" title="%1$s">%2$s</option>',
264 esc_attr( $permalink ),
265 esc_html( $item->post_title )
266 );
267 }
268 }
269 }
270
271 return sprintf('<optgroup label="%s">%s</optgroup>',
272 esc_attr__('Pages', 'code-profiler'),
273 $pages
274 );
275 }
276
277 // =====================================================================
278 // Fetch admin pages (backend)
279
280 function code_profiler_fetch_admin_pages( $home, $cp_options ) {
281
282 $backend = '';
283
284 if ( empty( $cp_options['mem_post'] ) ) {
285 $cp_options['mem_post'] = $home;
286 }
287
288 if (! empty( $GLOBALS[ 'menu' ] ) ) {
289 foreach( $GLOBALS[ 'menu' ] as $menu => $value ) {
290 if (! empty( $value[0] ) && substr( $value[2], -4 ) == '.php') {
291 // E.g., "Comments", "Plugins" etc
292 $value[0] = trim( preg_replace('/\s+<.+$/', '', $value[0] ) );
293
294 if ( $cp_options['mem_post'] == "{$home}wp-admin/{$value[2]}" ) {
295 $backend .= sprintf(
296 '<option value="%1$s" title="%1$s" selected>%2$s</option>',
297 esc_attr("{$home}wp-admin/{$value[2]}"),
298 esc_html( $value[0] )
299 );
300 } else {
301 $backend .= sprintf(
302 '<option value="%1$s" title="%1$s">%2$s</option>',
303 esc_attr("{$home}wp-admin/{$value[2]}"),
304 esc_html( $value[0] )
305 );
306 }
307 }
308 }
309 }
310 if ( empty( $backend ) ) {
311 $backend = sprintf(
312 '<option value="%1$swp-admin/" title="%1$swp-admin/">%2$s</option>',
313 esc_attr( $home ),
314 esc_html__('Dashboard', 'code-profiler')
315 );
316
317 }
318
319 return $backend;
320 }
321
322 // =====================================================================
323 // EOF
324