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

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

300 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 $home = home_url('/'); // Frontend
43 $site = site_url('/'); // Backend
44 // Get user login name
45 $current_user = wp_get_current_user();
46 // Profile's name
47 $profile = code_profiler_profile_name();
48
49 $cp_options = get_option( 'code-profiler' );
50 // Memorized options
51 if ( empty( $cp_options['mem_where'] ) ) {
52 $cp_options['mem_where'] = 'frontend';
53 }
54 if ( empty( $cp_options['mem_post'] ) ) {
55 $cp_options['mem_post'] = '';
56 }
57 if ( empty( $cp_options['mem_user'] ) ) {
58 $cp_options['mem_user'] = 'unauthenticated';
59 }
60 if ( $cp_options['mem_where'] == 'backend' ) {
61 $cp_options['mem_user'] = 'authenticated';
62 }
63 if ( empty( $cp_options['mem_method'] ) ) {
64 $cp_options['mem_method'] = 'get';
65 }
66 if ( empty( $cp_options['ua'] ) ) {
67 $cp_options['ua'] = 'Firefox';
68 }
69 $cookies = '';
70 $payload = '';
71 if ( isset( $cp_options['cookies'] ) ) {
72 $cookies = trim( json_decode( $cp_options['cookies'] ) );
73 }
74 if ( isset( $cp_options['payload'] ) ) {
75 $payload = trim( json_decode( $cp_options['payload'] ) );
76 }
77 ?>
78 <table class="form-table">
79 <tr>
80 <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>
81 <td>
82 <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>
83 &nbsp;&nbsp;&nbsp;&nbsp;
84 <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>
85 &nbsp;&nbsp;&nbsp;&nbsp;
86 <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>
87 </p>
88 <br />
89 <?php
90 if ( $cp_options['mem_where'] == 'frontend' ) {
91 echo '<p id="p-frontend">';
92 } else {
93 echo '<p id="p-frontend" style="display:none">';
94 }
95 ?>
96 <?php esc_html_e('Select a page:', 'code-profiler') ?>
97 <select name="frontend" id="id-frontend">
98 <?php echo code_profiler_fetch_pages( $home, $cp_options ); ?>
99 </select>
100 </p>
101 <?php
102 if ( $cp_options['mem_where'] == 'custom' ) {
103 $value = $cp_options['mem_post'];
104 echo '<p id="p-custom">';
105 } else {
106 $value = '';
107 echo '<p id="p-custom" style="display:none">';
108 }
109 ?>
110 <input name="custom" id="id-custom" type="text" value="<?php echo esc_attr( $value ) ?>" size="70" placeholder="<?php
111 echo esc_attr(
112 sprintf(
113 __('e.g., %s', 'code-profiler'),
114 "{$home}foo/bar/"
115 )
116 ) ?>" />
117 </p>
118 <?php
119 if ( $cp_options['mem_where'] == 'backend' ) {
120 echo '<p id="p-backend">';
121 } else {
122 echo '<p id="p-backend" style="display:none">';
123 }
124 if (! empty( $cp_options['mem_username'] ) ) {
125 $username = $cp_options['mem_username'];
126 } else {
127 $username = $current_user->user_login;
128 }
129 ?>
130 <?php esc_html_e('Select a page:', 'code-profiler') ?>
131 <select name="backend" id="id-backend"><?php echo code_profiler_fetch_admin_pages( $site, $cp_options ) ?></select>
132 </p>
133 </td>
134 </tr>
135 <tr>
136 <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>
137 <td>
138 <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>
139 <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>
140 <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>
141 </td>
142 </tr>
143 <tr>
144 <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>
145 <td>
146 <select name="ua" id="ua-id">
147 <?php
148 foreach( CODE_PROFILER_UA as $types => $types_array ) {
149 echo '<optgroup label="'. esc_attr( $types ) .'">';
150 foreach( $types_array as $name => $value ) {
151 echo '<option value="'. esc_attr( $name ) .'"'. selected( $cp_options['ua'], $name ) .'>'. esc_html( $name ) .'</option>';
152 }
153 echo '</optgroup>';
154 }
155 ?>
156 </select>
157 </td>
158 </tr>
159 <tr>
160 <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>
161 <td>
162 <p><label><input type="text" class="regular-text" name="profile" maxlength="100" value="<?php echo esc_attr( $profile ) ?>" id="profile-name" /></label></p>
163 <p class="description"><?php esc_html_e('Max 100 characters', 'code-profiler') ?></p>
164 </td>
165 </tr>
166 </table>
167 <?php
168 if (! empty( $cookies ) || $cp_options['mem_method'] == 'post' ) {
169 echo '<div id="cp-advanced-settings">';
170 $disabled_button = ' disabled';
171 } else {
172 echo '<div id="cp-advanced-settings" style="display:none">';
173 $disabled_button = '';
174 }
175 ?>
176 <table class="form-table">
177 <tr>
178 <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>
179 <td>
180 <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>
181 <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>
182 <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>
183 <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>
184 </td>
185 </tr>
186 <tr>
187 <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>
188 <td>
189 <p><textarea name="cp-cookies" id="cp-cookies" class="regular-text code" maxlength="4000" rows="6"><?php echo esc_textarea( $cookies ) ?></textarea></p>
190 <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>
191 </td>
192 </tr>
193 </table>
194 </div>
195 <?php wp_nonce_field('start_profiler_nonce', 'cp_nonce', 0); ?>
196 <br />
197 <div>
198 <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 ?> />
199 &nbsp;&nbsp;&nbsp;
200 <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') ?>" />
201 </div>
202 <div id="cp-progress-div" style="display:none">
203 <br />
204 <div class="cp-progress-bar"><span id="cp-span-progress" style="width:0%"></span></div>
205 <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>
206 </div>
207
208 <?php
209
210 // =====================================================================
211 // Fetch pages and posts.
212
213 function code_profiler_fetch_pages( $home, $cp_options ) {
214
215 $posts = '';
216
217 if ( empty( $cp_options['mem_post'] ) ) {
218 $cp_options['mem_post'] = $home;
219 }
220
221 if ( $cp_options['mem_post'] == $home ) {
222 $pages = '<option value="'. esc_attr( $home ) .'" title="'. esc_attr( $home ) .'" selected>'.
223 esc_html__('Homepage', 'code-profiler') .'</option>';
224 } else {
225 $pages = '<option value="'. esc_attr( $home ) .'" title="'. esc_attr( $home ) .'">'.
226 esc_html__('Homepage', 'code-profiler') .'</option>';
227 }
228 $args = array(
229 'posts_per_page' => -1,
230 'post_type' => array( 'page' ),
231 'post_status' => 'publish',
232 'orderby' => 'post_name',
233 'order' => 'ASC'
234 );
235 $query = new WP_Query( $args );
236
237 if ( $query ) {
238 $items = $query->posts;
239 foreach( $items as $item ) {
240 if ( empty( $item->post_title ) ) {
241 $item->post_title = __('Untitled');
242 }
243 $permalink = get_permalink( $item->ID );
244
245 if ( $cp_options['mem_post'] == $permalink ) {
246 $pages .= '<option value="'. esc_attr( $permalink ).'" title="'.
247 esc_attr( $permalink ) .'" selected>'. esc_html( $item->post_title ) .'</option>';
248 } else {
249 $pages .= '<option value="'. esc_attr( $permalink ).'" title="'.
250 esc_attr( $permalink ) .'">'. esc_html( $item->post_title ) .'</option>';
251 }
252 }
253 }
254
255 return sprintf( '<optgroup label="%s">%s</optgroup>',
256 esc_attr__('Pages', 'code-profiler'),
257 $pages
258 );
259 }
260
261 // =====================================================================
262 // Fetch admin pages (backend)
263
264 function code_profiler_fetch_admin_pages( $home, $cp_options ) {
265
266 $backend = '';
267
268 if ( empty( $cp_options['mem_post'] ) ) {
269 $cp_options['mem_post'] = $home;
270 }
271
272 if (! empty( $GLOBALS[ 'menu' ] ) ) {
273 foreach( $GLOBALS[ 'menu' ] as $menu => $value ) {
274 if (! empty( $value[0] ) && substr( $value[2], -4 ) == '.php' ) {
275 // E.g., "Comments", "Plugins" etc
276 $value[0] = trim( preg_replace( '/\s+<.+$/', '', $value[0] ) );
277
278 if ( $cp_options['mem_post'] == "{$home}wp-admin/{$value[2]}" ) {
279 $backend .= '<option value="'. esc_attr( "{$home}wp-admin/{$value[2]}" ) .'" title="'.
280 esc_attr( "{$home}wp-admin/{$value[2]}" ) .'" selected>'.
281 esc_html( $value[0] ) .'</option>';
282 } else {
283 $backend .= '<option value="'. esc_attr( "{$home}wp-admin/{$value[2]}" ) .'" title="'.
284 esc_attr( "{$home}wp-admin/{$value[2]}" ) .'">'.
285 esc_html( $value[0] ) .'</option>';
286 }
287 }
288 }
289 }
290 if ( empty( $backend ) ) {
291 $backend = '<option value="'. esc_attr( $home ) .'wp-admin/" title="'. esc_attr( $home ) .'wp-admin/">'.
292 esc_html__( 'Dashboard', 'code-profiler' ) .'</option>';
293 }
294
295 return $backend;
296 }
297
298 // =====================================================================
299 // EOF
300