# code-profiler/1.4.1/lib/menu_profiler.php

Code Profiler – WordPress Performance Profiling and Debugging Made Easy, version 1.4.1. 227 lines.

- Page: https://pluginprobe.com/plugins/code-profiler/1.4.1/code/lib/menu_profiler.php
- Raw: https://pluginprobe.com/plugins/code-profiler/1.4.1/raw/lib/menu_profiler.php
- Modified: 2022-04-26T05:47:30+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/code-profiler/1.4.1/code/lib/menu_profiler.php#L10-L20`.

```php
<?php
/*
 +=====================================================================+
 |    ____          _        ____             __ _ _                   |
 |   / ___|___   __| | ___  |  _ \ _ __ ___  / _(_) | ___ _ __         |
 |  | |   / _ \ / _` |/ _ \ | |_) | '__/ _ \| |_| | |/ _ \ '__|        |
 |  | |__| (_) | (_| |  __/ |  __/| | | (_) |  _| | |  __/ |           |
 |   \____\___/ \__,_|\___| |_|   |_|  \___/|_| |_|_|\___|_|           |
 |                                                                     |
 |  (c) Jerome Bruandet ~ https://code-profiler.com/                   |
 +=====================================================================+
*/

if (! defined( 'ABSPATH' ) ) { die( 'Forbidden' ); }

// =====================================================================
// Display Summary tab.

// Used to display profiler's error messages while running
echo '<div class="error notice is-dismissible" style="display:none" id="code-profiler-error"><p></p></div>';
echo code_profiler_display_tabs( 1 );
$error = 0;
if (! function_exists('register_tick_function') ) {
	$error = 'register_tick_function';
} elseif (! function_exists('stream_wrapper_unregister') ) {
	$error = 'stream_wrapper_unregister';
} elseif (! function_exists('stream_wrapper_register') ) {
	$error = 'stream_wrapper_register';
} elseif (! function_exists('stream_wrapper_restore') ) {
	$error = 'stream_wrapper_restore';
}
if (! empty( $error ) ) {
	printf( CODE_PROFILER_ERROR_NOTICE, sprintf(
		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 )
	);
}

$home = home_url( '/' );
// Get user login name
$current_user = wp_get_current_user();
// Profile's name
$profile = code_profiler_profile_name();
?>
<table class="form-table">
	<tr>
		<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>
		<td>
			<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>
			&nbsp;&nbsp;&nbsp;&nbsp;
			<label><input type="radio" name="where" value="custom" onclick="cpjs_front_or_backend(3);" /> <?php esc_html_e('Custom post/URL', 'code-profiler') ?></label>
			&nbsp;&nbsp;&nbsp;&nbsp;
			<label><input type="radio" name="where" value="backend" onclick="cpjs_front_or_backend(2);" /> <?php esc_html_e('Admin backend', 'code-profiler') ?></label>
			</p>
			<br />
			<p id="p-frontend">
				<?php esc_html_e('Select a page:', 'code-profiler') ?>
				<select name="frontend" id="id-frontend">
					<?php echo code_profiler_fetch_pages( $home ); ?>
				</select>
			</p>
			<p id="p-custom" style="display:none">
				<input name="custom" id="id-custom" disabled type="text" value="" size="70" placeholder="<?php
				echo esc_attr(
					sprintf(
						__('e.g., %s', 'code-profiler'),
						"{$home}foo/bar/"
					)
				) ?>" />
			</p>
			<p id="p-backend" style="display:none">
				<?php esc_html_e('Select a page:', 'code-profiler') ?>
				<select name="backend" id="id-backend" disabled><?php echo code_profiler_fetch_admin_pages( $home ) ?></select>
			</p>
		</td>
	</tr>
	<tr>
		<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>
		<td>
			<p><label><input type="radio" name="user" value="unauthenticated" id="user-unauthenticated" checked /> <?php esc_html_e('Unauthenticated user', 'code-profiler') ?></label></p>
			<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>
		</td>
	</tr>
	<tr>
		<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>
		<td>
			<select name="ua" id="ua-id">
			<?php
			foreach( CODE_PROFILER_UA as $types => $types_array ) {
				echo '<optgroup label="'. esc_attr( $types ) .'">';
				foreach( $types_array as $name => $value ) {
					echo '<option value="'. esc_attr( $name ) .'">'. esc_html( $name ) .'</option>';
				}
				echo '</optgroup>';
			}
			?>
			</select>
		</td>
	</tr>
	<tr>
		<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>
		<td>
			<p><label><input type="text" class="regular-text" name="profile" maxlength="100" value="<?php echo esc_attr( $profile ) ?>" id="profile-name" /></label></p>
			<p class="description"><?php esc_html_e('Max 100 characters', 'code-profiler') ?></p>
		</td>
	</tr>
</table>
<?php
$cookies = '';
$payload = '';
$cp_options = get_option( 'code-profiler' );
if ( isset( $cp_options['cookies'] ) ) {
	$cookies = trim( json_decode( $cp_options['cookies'] ) );
}
if ( isset( $cp_options['payload'] ) ) {
	$payload	= trim( json_decode( $cp_options['payload'] ) );
}
if ( isset( $cp_options['post'] ) ) {
	$method	= 'post';
} else {
	$method	= 'get';
}

if (! empty( $cookies ) || $method == 'post' ) {
	echo '<div id="cp-advanced-settings">';
	$disabled_button = ' disabled';
} else {
	echo '<div id="cp-advanced-settings" style="display:none">';
	$disabled_button = '';
}
?>
	<table class="form-table">
		<tr>
			<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>
			<td>
				<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>
				<p><label><input onclick="cpjs_get_post(1);" type="radio" name="method" value="post" id="post-method"<?php checked( $method, 'post' ) ?> /> POST</label></p>
				<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>
				<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>
			</td>
		</tr>
		<tr>
			<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>
			<td>
				<p><textarea name="cp-cookies" id="cp-cookies" class="regular-text code" maxlength="4000" rows="6"><?php echo esc_textarea( $cookies ) ?></textarea></p>
				<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>
			</td>
		</tr>
	</table>
</div>
<?php wp_nonce_field('start_profiler_nonce', 'cp_nonce', 0); ?>
<br />
<div>
	<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 ?> />
	&nbsp;&nbsp;&nbsp;
	<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') ?>" />
</div>
<div id="cp-progress-div" style="display:none">
	<br />
	<div class="cp-progress-bar"><span id="cp-span-progress" style="width:0%"></span></div>
	<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>
</div>

<?php

// =====================================================================
// Fetch pages and posts.

function code_profiler_fetch_pages( $home ) {

	$posts = '';
	$pages = '<option value="'. esc_attr( $home ) .'" title="'. esc_attr( $home ) .'">'.
				esc_html__('Homepage', 'code-profiler') .'</option>';
	$args = array(
		'posts_per_page' => -1,
		'post_type'      => array( 'page' ),
		'post_status'    => 'publish',
		'orderby'        => 'post_name',
		'order'          => 'ASC',
	);
	$query = new WP_Query( $args );

	if ( $query ) {
		$items = $query->posts;
		foreach( $items as $item ) {
			if ( empty( $item->post_title ) ) {
				$item->post_title = __('Untitled');
			}
			$permalink = get_permalink( $item->ID );
			$pages .= '<option value="'. esc_attr( $permalink ).'" title="'.
						esc_attr( $permalink ) .'">'.	esc_html( $item->post_title ) .'</option>';
		}
	}

	return sprintf( '<optgroup label="%s">%s</optgroup>',
		esc_attr__('Pages', 'code-profiler'),
		$pages
	);
}

// =====================================================================
// Fetch admin pages (backend)

function code_profiler_fetch_admin_pages( $home ) {

	$backend = '';
	if (! empty( $GLOBALS[ 'menu' ] ) ) {
		foreach( $GLOBALS[ 'menu' ] as $menu => $value ) {
			if (! empty( $value[0] ) && substr( $value[2], -4 ) == '.php' ) {
				// E.g., "Comments", "Plugins" etc
				$value[0] = trim( preg_replace( '/\s+<.+$/', '', $value[0] ) );
				$backend .= '<option value="'. esc_attr( "{$home}wp-admin/{$value[2]}" ) .'" title="'.
							esc_attr( "{$home}wp-admin/{$value[2]}" ) .'">'.
							esc_html( $value[0] ) .'</option>';
			}
		}
	}
	if ( empty( $backend ) ) {
		$backend = '<option value="'. esc_attr( $home ) .'wp-admin/" title="'. esc_attr( $home ) .'wp-admin/">'.
						esc_html__( 'Dashboard' ) .'</option>';
	}

	return $backend;
}

// =====================================================================
// EOF

```
