| 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></p> |
| 49 |
<br /> |
| 50 |
<?php esc_html_e('Select a page:', 'code-profiler') ?> |
| 51 |
<select name="frontend" id="id-frontend"> |
| 52 |
<?php echo code_profiler_fetch_pages( $home ); ?> |
| 53 |
</select> |
| 54 |
<br /> |
| 55 |
<br /> |
| 56 |
<p><label><input type="radio" name="where" value="custom" onclick="cpjs_front_or_backend(3);" /> <?php esc_html_e('Custom post/URL', 'code-profiler') ?></label></p> |
| 57 |
<br /> |
| 58 |
<input name="custom" id="id-custom" disabled type="text" value="" size="70" placeholder="<?php |
| 59 |
echo esc_attr( |
| 60 |
sprintf( |
| 61 |
__('e.g., %s', 'code-profiler'), |
| 62 |
"{$home}foo/bar/" |
| 63 |
) |
| 64 |
) ?>" /> |
| 65 |
<br /> |
| 66 |
<br /> |
| 67 |
<p><label><input type="radio" name="where" value="backend" onclick="cpjs_front_or_backend(2);" /> <?php esc_html_e('Admin backend', 'code-profiler') ?></label></p> |
| 68 |
<br /> |
| 69 |
<?php esc_html_e('Select a page:', 'code-profiler') ?> |
| 70 |
<select name="backend" id="id-backend" disabled><?php echo code_profiler_fetch_admin_pages( $home ) ?></select> |
| 71 |
</td> |
| 72 |
</tr> |
| 73 |
<tr> |
| 74 |
<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> |
| 75 |
<td> |
| 76 |
<p><label><input type="radio" name="user" value="unauthenticated" id="user-unauthenticated" checked /> <?php esc_html_e('Unauthenticated user', 'code-profiler') ?></label></p> |
| 77 |
<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> |
| 78 |
</td> |
| 79 |
</tr> |
| 80 |
<tr> |
| 81 |
<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> |
| 82 |
<td> |
| 83 |
<select name="ua" id="ua-id"> |
| 84 |
<?php |
| 85 |
foreach( CODE_PROFILER_UA as $types => $types_array ) { |
| 86 |
echo '<optgroup label="'. esc_attr( $types ) .'">'; |
| 87 |
foreach( $types_array as $name => $value ) { |
| 88 |
echo '<option value="'. esc_attr( $name ) .'">'. esc_html( $name ) .'</option>'; |
| 89 |
} |
| 90 |
echo '</optgroup>'; |
| 91 |
} |
| 92 |
?> |
| 93 |
</select> |
| 94 |
</td> |
| 95 |
</tr> |
| 96 |
<tr> |
| 97 |
<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> |
| 98 |
<td> |
| 99 |
<p><label><input type="text" size="70" name="profile" maxlength="100" value="<?php echo esc_attr( $profile ) ?>" id="profile-name" /></label></p> |
| 100 |
<p class="description"><?php esc_html_e('Max 100 characters', 'code-profiler') ?></p> |
| 101 |
</td> |
| 102 |
</tr> |
| 103 |
</table> |
| 104 |
<?php wp_nonce_field('start_profiler_nonce', 'cp_nonce', 0); ?> |
| 105 |
<br /> |
| 106 |
<div> |
| 107 |
<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') ?>" /> |
| 108 |
</div> |
| 109 |
<div id="cp-progress-div" style="display:none"> |
| 110 |
<br /> |
| 111 |
<div class="cp-progress-bar"><span id="cp-span-progress" style="width:0%"></span></div> |
| 112 |
<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> |
| 113 |
</div> |
| 114 |
|
| 115 |
<?php |
| 116 |
|
| 117 |
// ===================================================================== |
| 118 |
// Fetch pages and posts. |
| 119 |
|
| 120 |
function code_profiler_fetch_pages( $home ) { |
| 121 |
|
| 122 |
$posts = ''; |
| 123 |
$pages = '<option value="'. esc_attr( $home ) .'" title="'. esc_attr( $home ) .'">'. |
| 124 |
esc_html__('Homepage', 'code-profiler') .'</option>'; |
| 125 |
$args = array( |
| 126 |
'posts_per_page' => -1, |
| 127 |
'post_type' => array( 'page' ), |
| 128 |
'post_status' => 'publish', |
| 129 |
'orderby' => 'post_name', |
| 130 |
'order' => 'ASC', |
| 131 |
); |
| 132 |
$query = new WP_Query( $args ); |
| 133 |
|
| 134 |
if ( $query ) { |
| 135 |
$items = $query->posts; |
| 136 |
foreach( $items as $item ) { |
| 137 |
if ( empty( $item->post_title ) ) { |
| 138 |
$item->post_title = __('Untitled'); |
| 139 |
} |
| 140 |
$permalink = get_permalink( $item->ID ); |
| 141 |
$pages .= '<option value="'. esc_attr( $permalink ).'" title="'. |
| 142 |
esc_attr( $permalink ) .'">'. esc_html( $item->post_title ) .'</option>'; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
return sprintf( '<optgroup label="%s">%s</optgroup>', |
| 147 |
esc_attr__('Pages', 'code-profiler'), |
| 148 |
$pages |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
// ===================================================================== |
| 153 |
// Fetch admin pages (backend) |
| 154 |
|
| 155 |
function code_profiler_fetch_admin_pages( $home ) { |
| 156 |
|
| 157 |
$backend = ''; |
| 158 |
if (! empty( $GLOBALS[ 'menu' ] ) ) { |
| 159 |
foreach( $GLOBALS[ 'menu' ] as $menu => $value ) { |
| 160 |
if (! empty( $value[0] ) && substr( $value[2], -4 ) == '.php' ) { |
| 161 |
// E.g., "Comments", "Plugins" etc |
| 162 |
$value[0] = trim( preg_replace( '/\s+<.+$/', '', $value[0] ) ); |
| 163 |
$backend .= '<option value="'. esc_attr( "{$home}wp-admin/{$value[2]}" ) .'" title="'. |
| 164 |
esc_attr( "{$home}wp-admin/{$value[2]}" ) .'">'. |
| 165 |
esc_html( $value[0] ) .'</option>'; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
if ( empty( $backend ) ) { |
| 170 |
$backend = '<option value="'. esc_attr( $home ) .'wp-admin/" title="'. esc_attr( $home ) .'wp-admin/">'. |
| 171 |
esc_html__( 'Dashboard' ) .'</option>'; |
| 172 |
} |
| 173 |
|
| 174 |
return $backend; |
| 175 |
} |
| 176 |
|
| 177 |
// ===================================================================== |
| 178 |
// EOF |
| 179 |
|