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

227 lines 10.9 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
38 $home = home_url( '/' );
39 // Get user login name
40 $current_user = wp_get_current_user();
41 // Profile's name
42 $profile = code_profiler_profile_name();
43 ?>
44 <table class="form-table">
45 <tr>
46 <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>
47 <td>
48 <p><label><input type="radio" name="where" value="frontend" onclick="cpjs_front_or_backend(1);" checked /> <?php esc_html_e('Website frontend', 'code-profiler') ?></label>
49 &nbsp;&nbsp;&nbsp;&nbsp;
50 <label><input type="radio" name="where" value="custom" onclick="cpjs_front_or_backend(3);" /> <?php esc_html_e('Custom post/URL', 'code-profiler') ?></label>
51 &nbsp;&nbsp;&nbsp;&nbsp;
52 <label><input type="radio" name="where" value="backend" onclick="cpjs_front_or_backend(2);" /> <?php esc_html_e('Admin backend', 'code-profiler') ?></label>
53 </p>
54 <br />
55 <p id="p-frontend">
56 <?php esc_html_e('Select a page:', 'code-profiler') ?>
57 <select name="frontend" id="id-frontend">
58 <?php echo code_profiler_fetch_pages( $home ); ?>
59 </select>
60 </p>
61 <p id="p-custom" style="display:none">
62 <input name="custom" id="id-custom" disabled type="text" value="" size="70" placeholder="<?php
63 echo esc_attr(
64 sprintf(
65 __('e.g., %s', 'code-profiler'),
66 "{$home}foo/bar/"
67 )
68 ) ?>" />
69 </p>
70 <p id="p-backend" style="display:none">
71 <?php esc_html_e('Select a page:', 'code-profiler') ?>
72 <select name="backend" id="id-backend" disabled><?php echo code_profiler_fetch_admin_pages( $home ) ?></select>
73 </p>
74 </td>
75 </tr>
76 <tr>
77 <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 you, the authenticated user.', 'code-profiler' ) ?>"></span></th>
78 <td>
79 <p><label><input type="radio" name="user" value="unauthenticated" id="user-unauthenticated" checked /> <?php esc_html_e('Unauthenticated user', 'code-profiler') ?></label></p>
80 <p><label><input type="radio" name="user" value="authenticated" id="user-authenticated" /> <?php printf( esc_html__('Authenticated user (%s)', 'code-profiler'), esc_html( $current_user->user_login ) ) ?></label></p>
81 </td>
82 </tr>
83 <tr>
84 <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>
85 <td>
86 <select name="ua" id="ua-id">
87 <?php
88 foreach( CODE_PROFILER_UA as $types => $types_array ) {
89 echo '<optgroup label="'. esc_attr( $types ) .'">';
90 foreach( $types_array as $name => $value ) {
91 echo '<option value="'. esc_attr( $name ) .'">'. esc_html( $name ) .'</option>';
92 }
93 echo '</optgroup>';
94 }
95 ?>
96 </select>
97 </td>
98 </tr>
99 <tr>
100 <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>
101 <td>
102 <p><label><input type="text" class="regular-text" name="profile" maxlength="100" value="<?php echo esc_attr( $profile ) ?>" id="profile-name" /></label></p>
103 <p class="description"><?php esc_html_e('Max 100 characters', 'code-profiler') ?></p>
104 </td>
105 </tr>
106 </table>
107 <?php
108 $cookies = '';
109 $payload = '';
110 $cp_options = get_option( 'code-profiler' );
111 if ( isset( $cp_options['cookies'] ) ) {
112 $cookies = trim( json_decode( $cp_options['cookies'] ) );
113 }
114 if ( isset( $cp_options['payload'] ) ) {
115 $payload = trim( json_decode( $cp_options['payload'] ) );
116 }
117 if ( isset( $cp_options['post'] ) ) {
118 $method = 'post';
119 } else {
120 $method = 'get';
121 }
122
123 if (! empty( $cookies ) || $method == 'post' ) {
124 echo '<div id="cp-advanced-settings">';
125 $disabled_button = ' disabled';
126 } else {
127 echo '<div id="cp-advanced-settings" style="display:none">';
128 $disabled_button = '';
129 }
130 ?>
131 <table class="form-table">
132 <tr>
133 <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>
134 <td>
135 <p><label><input onclick="cpjs_get_post(0);" type="radio" name="method" value="get" id="get-method"<?php checked( $method, 'get' ) ?> /> <?php esc_html_e('GET (default)', 'code-profiler') ?></label></p>
136 <p><label><input onclick="cpjs_get_post(1);" type="radio" name="method" value="post" id="post-method"<?php checked( $method, 'post' ) ?> /> POST</label></p>
137 <p><textarea name="post-value" id="post-value" class="regular-text code"<?php disabled( $method, 'get' ) ?> maxlength="4000" rows="6"><?php echo esc_textarea( $payload ) ?></textarea></p>
138 <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">', '</a>') ?> </p>
139 </td>
140 </tr>
141 <tr>
142 <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>
143 <td>
144 <p><textarea name="cp-cookies" id="cp-cookies" class="regular-text code" maxlength="4000" rows="6"><?php echo esc_textarea( $cookies ) ?></textarea></p>
145 <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">', '</a>') ?></p>
146 </td>
147 </tr>
148 </table>
149 </div>
150 <?php wp_nonce_field('start_profiler_nonce', 'cp_nonce', 0); ?>
151 <br />
152 <div>
153 <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 ?> />
154 &nbsp;&nbsp;&nbsp;
155 <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') ?>" />
156 </div>
157 <div id="cp-progress-div" style="display:none">
158 <br />
159 <div class="cp-progress-bar"><span id="cp-span-progress" style="width:0%"></span></div>
160 <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>
161 </div>
162
163 <?php
164
165 // =====================================================================
166 // Fetch pages and posts.
167
168 function code_profiler_fetch_pages( $home ) {
169
170 $posts = '';
171 $pages = '<option value="'. esc_attr( $home ) .'" title="'. esc_attr( $home ) .'">'.
172 esc_html__('Homepage', 'code-profiler') .'</option>';
173 $args = array(
174 'posts_per_page' => -1,
175 'post_type' => array( 'page' ),
176 'post_status' => 'publish',
177 'orderby' => 'post_name',
178 'order' => 'ASC',
179 );
180 $query = new WP_Query( $args );
181
182 if ( $query ) {
183 $items = $query->posts;
184 foreach( $items as $item ) {
185 if ( empty( $item->post_title ) ) {
186 $item->post_title = __('Untitled');
187 }
188 $permalink = get_permalink( $item->ID );
189 $pages .= '<option value="'. esc_attr( $permalink ).'" title="'.
190 esc_attr( $permalink ) .'">'. esc_html( $item->post_title ) .'</option>';
191 }
192 }
193
194 return sprintf( '<optgroup label="%s">%s</optgroup>',
195 esc_attr__('Pages', 'code-profiler'),
196 $pages
197 );
198 }
199
200 // =====================================================================
201 // Fetch admin pages (backend)
202
203 function code_profiler_fetch_admin_pages( $home ) {
204
205 $backend = '';
206 if (! empty( $GLOBALS[ 'menu' ] ) ) {
207 foreach( $GLOBALS[ 'menu' ] as $menu => $value ) {
208 if (! empty( $value[0] ) && substr( $value[2], -4 ) == '.php' ) {
209 // E.g., "Comments", "Plugins" etc
210 $value[0] = trim( preg_replace( '/\s+<.+$/', '', $value[0] ) );
211 $backend .= '<option value="'. esc_attr( "{$home}wp-admin/{$value[2]}" ) .'" title="'.
212 esc_attr( "{$home}wp-admin/{$value[2]}" ) .'">'.
213 esc_html( $value[0] ) .'</option>';
214 }
215 }
216 }
217 if ( empty( $backend ) ) {
218 $backend = '<option value="'. esc_attr( $home ) .'wp-admin/" title="'. esc_attr( $home ) .'wp-admin/">'.
219 esc_html__( 'Dashboard' ) .'</option>';
220 }
221
222 return $backend;
223 }
224
225 // =====================================================================
226 // EOF
227