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