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

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