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

532 lines 23.4 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://nintechnet.com/codeprofiler/ |
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
43 /**
44 * Check and warn if the Xdebug extension is loaded.
45 */
46 if ( extension_loaded('xdebug') ) {
47 printf( CODE_PROFILER_WARNING_NOTICE,
48 esc_html__('Warning: the PHP Xdebug extension is loaded. Consider disabling it as it can drastically impact the performance and results of Code Profiler.', 'code-profiler')
49 );
50 }
51
52 // Clear the log if it's too big
53 code_profiler_clearlog();
54
55 // Clean-up temp files left in the profiles folder
56 code_profiler_cleantmpfiles();
57
58 /**
59 * Create temporary folder and file used to profile cron events.
60 */
61 $error = code_profiler_create_tmpfile();
62 if ( $error ) {
63 printf( CODE_PROFILER_ERROR_NOTICE, esc_html( $error ) );
64 }
65
66 $home = home_url('/'); // Frontend
67 $site = site_url('/'); // Backend
68
69 // Get user login name
70 $current_user = wp_get_current_user();
71
72 $cp_options = get_option('code-profiler');
73 /**
74 * Check if we've been asked to re-run a profile.
75 */
76 if (! empty( $_REQUEST['action'] ) && $_REQUEST['action'] == 'rerun' &&
77 ! empty( $_REQUEST['profiles'][0] ) ) {
78 /**
79 * Make sure the profile exists and get its full path.
80 */
81 $profile_path = code_profiler_get_profile_path( $_REQUEST['profiles'][0] );
82 if ( $profile_path !== false ) {
83
84 $cp_data = json_decode( file_get_contents("$profile_path.summary.profile"), true );
85 if (! empty( $cp_data['rerun'] ) ) {
86 $cp_options['mem'] = $cp_data['rerun'];
87 $rerun = true;
88 }
89 }
90 }
91 // Profile's name
92 if (! empty( $cp_options['mem']['profile'] ) ) {
93 $profile = $cp_options['mem']['profile'];
94 } else {
95 $profile = code_profiler_profile_name();
96 }
97 /**
98 * Memorized options from last run.
99 */
100 if ( empty( $cp_options['mem']['x_end'] ) ) {
101 $cp_options['mem']['x_end'] = 'frontend';
102 }
103 if ( empty( $cp_options['mem']['post'] ) ) {
104 $cp_options['mem']['post'] = '';
105 }
106 if ( empty( $cp_options['mem']['x_auth'] ) ) {
107 $cp_options['mem']['x_auth'] = 'unauthenticated';
108 }
109 if ( $cp_options['mem']['x_end'] == 'backend') {
110 $cp_options['mem']['x_auth'] = 'authenticated';
111 }
112 if ( empty( $cp_options['mem']['method'] ) ) {
113 $cp_options['mem']['method'] = 'get';
114 }
115 $default_theme = get_option('stylesheet');
116 if ( empty( $cp_options['mem']['theme'] ) ) {
117 $cp_options['mem']['theme'] = $default_theme;
118 }
119 if ( empty( $cp_options['mem']['user_agent'] ) ) {
120 $cp_options['mem']['user_agent'] = 'Firefox';
121 }
122 if ( isset( $cp_options['mem']['cookies'] ) ) {
123 // stripslashes is only needed for cookies
124 $cookies = trim( stripslashes( json_decode( $cp_options['mem']['cookies'] ) ) );
125 } else {
126 $cookies = '';
127 }
128 if ( empty( $cp_options['mem']['content_type'] ) ||
129 ! in_array( $cp_options['mem']['content_type'], [ 1, 2, 3 ] ) ) {
130
131 $cp_options['mem']['content_type'] = 1;
132 }
133 if ( isset( $cp_options['mem']['payload'] ) ) {
134 $payload = trim( json_decode( $cp_options['mem']['payload'] ) );
135 } else {
136 $payload = '';
137 }
138 if ( isset( $cp_options['mem']['custom_headers'] ) ) {
139 $custom_headers = trim( json_decode( $cp_options['mem']['custom_headers'] ) );
140 } else {
141 $custom_headers = '';
142 }
143 if ( isset( $cp_options['mem']['exclusions'] ) ) {
144 $exclusions = json_decode( $cp_options['mem']['exclusions'] );
145 } else {
146 $exclusions = [];
147 }
148
149 ?>
150
151 <table style="width:100%">
152 <tr>
153 <td>
154 <table class="form-table">
155 <tr>
156 <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, a custom URL or a cron event. Note that in case of a cron event, the "HTTP Method" option will be ignored by Code Profiler as it will always send a POST request with an empty payload.', 'code-profiler') ?>"></span></th>
157 <td>
158 <p><label><input type="radio" name="x_end" value="frontend" onclick="cpjs_front_or_backend(this.value);"<?php checked( $cp_options['mem']['x_end'], 'frontend') ?> /> <?php esc_html_e('Website frontend', 'code-profiler') ?></label>
159 &nbsp;&nbsp;&nbsp;&nbsp;
160 <label><input type="radio" name="x_end" value="custom" onclick="cpjs_front_or_backend(this.value);"<?php checked( $cp_options['mem']['x_end'], 'custom') ?> /> <?php esc_html_e('Custom post/URL', 'code-profiler') ?></label>
161 &nbsp;&nbsp;&nbsp;&nbsp;
162 <label><input type="radio" name="x_end" value="backend" onclick="cpjs_front_or_backend(this.value);"<?php checked( $cp_options['mem']['x_end'], 'backend') ?> /> <?php esc_html_e('Admin backend', 'code-profiler') ?></label>
163 &nbsp;&nbsp;&nbsp;&nbsp;
164 <label><input type="radio" name="x_end" value="wpcron" onclick="cpjs_front_or_backend(this.value);"<?php checked( $cp_options['mem']['x_end'], 'wpcron') ?> /> <?php esc_html_e('WP-Cron', 'code-profiler') ?></label>
165 </p>
166 <br />
167 <?php
168 if ( $cp_options['mem']['x_end'] == 'frontend') {
169 echo '<p id="p-frontend">';
170 } else {
171 echo '<p id="p-frontend" style="display:none">';
172 }
173 ?>
174 <?php esc_html_e('Select a page:', 'code-profiler') ?>
175 <select name="frontend" id="id-frontend">
176 <?php echo code_profiler_fetch_pages( $home, $cp_options ); ?>
177 </select>
178 </p>
179 <?php
180 if ( $cp_options['mem']['x_end'] == 'custom') {
181 $value = $cp_options['mem']['post'];
182 echo '<p id="p-custom">';
183 } else {
184 $value = '';
185 echo '<p id="p-custom" style="display:none">';
186 }
187 ?>
188 <input name="custom" id="id-custom" type="text" value="<?php echo esc_attr( $value ) ?>" size="70" placeholder="<?php
189 echo esc_attr(
190 sprintf(
191 __('e.g., %s', 'code-profiler'),
192 "{$home}foo/bar/"
193 )
194 ) ?>" />
195 </p>
196 <?php
197 if ( $cp_options['mem']['x_end'] == 'backend') {
198 echo '<p id="p-backend">';
199 } else {
200 echo '<p id="p-backend" style="display:none">';
201 }
202 if (! empty( $cp_options['mem']['username'] ) ) {
203 $username = $cp_options['mem']['username'];
204 } else {
205 $username = $current_user->user_login;
206 }
207 ?>
208 <?php esc_html_e('Select a page:', 'code-profiler') ?>
209 <select name="backend" id="id-backend"><?php echo code_profiler_fetch_admin_pages( $site, $cp_options ) ?></select>
210 </p>
211 <?php
212 /**
213 * Cron tasks.
214 */
215 if ( $cp_options['mem']['x_end'] == 'wpcron') {
216 echo '<p id="p-wpcron">';
217 } else {
218 echo '<p id="p-wpcron" style="display:none">';
219 }
220 ?>
221 <?php esc_html_e('Select a cron event:', 'code-profiler') ?>
222 <select name="wpcron" id="id-wpcron">
223 <?php echo code_profiler_fetch_crontasks( $cp_options['mem']['post'] ); ?>
224 </select>
225 </p>
226 </td>
227 </tr>
228 <tr>
229 <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, otherwise Code Profiler will return an error if the user isn\'t allowed to access it.', 'code-profiler') ?>"></span></th>
230 <td>
231 <p><label><input type="radio" onclick="cpjs_authenticated(0);" name="x_auth" value="unauthenticated" id="user-unauthenticated"<?php checked( $cp_options['mem']['x_auth'], 'unauthenticated'); disabled( $cp_options['mem']['x_end'], 'backend'); ?> /> <?php esc_html_e('Unauthenticated user', 'code-profiler') ?></label></p>
232 <p><label><input type="radio" onclick="cpjs_authenticated(1);" name="x_auth" value="authenticated" id="user-authenticated"<?php checked( $cp_options['mem']['x_auth'], 'authenticated') ?> /> <?php esc_html_e('Authenticated user', 'code-profiler') ?></label></p>
233 <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']['x_auth'], 'unauthenticated') ?> /></label></p>
234 </td>
235 </tr>
236 <tr>
237 <th scope="row"><?php esc_html_e('Theme', 'code-profiler') ?> <span class="code-profiler-tip" data-tip="<?php esc_attr_e('If you want to profile different themes, you can use this option so that you don\'t need to modify your WordPress settings. The change will not affect your visitors, but only the profiler when it is running.', 'code-profiler') ?>"></span></th>
238 <td>
239 <select name="theme" id="id-theme">
240 <?php
241 $themes = code_profiler_get_themes();
242 foreach( $themes as $slug => $name ) {
243 if ( $slug == $default_theme ) {
244 echo '<option value=""'. selected( $cp_options['mem']['theme'], $slug, false ) .'>'.
245 esc_html( $name['n'] ) .' '. __('(active theme)', 'code-profiler') .'</option>';
246 } else {
247 echo '<option value="'. esc_attr( $slug ) .'"'. selected( $cp_options['mem']['theme'], $slug, false ) .'>'.
248 esc_html( $name['n'] ) .'</option>';
249 }
250 }
251 ?>
252 </select>
253 </td>
254 </tr>
255 <tr>
256 <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>
257 <td>
258 <select name="user_agent" id="ua-id">
259 <?php
260 foreach( CODE_PROFILER_UA as $types => $types_array ) {
261 echo '<optgroup label="'. esc_attr( $types ) .'">';
262 foreach( $types_array as $name => $value ) {
263 echo '<option value="'. esc_attr( $name ) .'"'. selected( $cp_options['mem']['user_agent'], $name, false ) .'>'. esc_html( $name ) .'</option>';
264 }
265 echo '</optgroup>';
266 }
267 ?>
268 </select>
269 </td>
270 </tr>
271 <tr>
272 <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>
273 <td>
274 <p><label><input type="text" class="regular-text" name="profile" maxlength="100" value="<?php echo esc_attr( $profile ) ?>" id="profile-name" /></label></p>
275 <p class="description"><?php esc_html_e('Max 100 characters', 'code-profiler') ?></p>
276 </td>
277 </tr>
278 </table>
279 </td>
280 <td style="vertical-align:top;text-align: center">
281 <?php
282 /**
283 * Display any available discount coupon.
284 */
285 CodeProfiler_WPCron::display_coupon();
286 ?>
287 </td>
288 </tr>
289 </table>
290 <?php
291 if ( (! empty( $cookies ) || ! empty( $custom_headers ) ||
292 $cp_options['mem']['method'] == 'post' || ! empty( $exclusions ) ) &&
293 // We don't display the advanced section if the last profile was a cron event.
294 $cp_options['mem']['x_end'] != 'wpcron' ) {
295
296 echo '<div id="cp-advanced-settings">';
297 $disabled_button = ' disabled';
298 } else {
299 echo '<div id="cp-advanced-settings" style="display:none">';
300 $disabled_button = '';
301 }
302 ?>
303 <table class="form-table">
304 <tr>
305 <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>
306 <td>
307 <p>
308 <label>
309 <input onclick="cpjs_get_post(0);" type="radio" name="method" value="get" id="get-method"<?php checked( $cp_options['mem']['method'], 'get') ?> /> GET(<?php esc_html_e('default', 'code-profiler') ?>)
310 </label>
311 </p>
312 <p>
313 <label>
314 <input onclick="cpjs_get_post(1);" type="radio" name="method" value="post" id="post-method"<?php checked( $cp_options['mem']['method'], 'post') ?> /> POST
315 </label>
316 </p>
317 <p>
318 <?php esc_html_e('Content-type:', 'code-profiler') ?>
319 <select name="cp-content-type" id="id-content-type"<?php disabled( $cp_options['mem']['method'], 'get') ?> onChange="cpjs_content_type(this.value);">
320 <option value="1"<?php selected( $cp_options['mem']['content_type'], 1 )?>>application/x-www-form-urlencoded (<?php esc_html_e('formatted', 'code-profiler') ?>)</option>
321 <option value="3"<?php selected( $cp_options['mem']['content_type'], 3 )?>>application/x-www-form-urlencoded (<?php esc_html_e('raw', 'code-profiler') ?>)</option>
322 <option value="2"<?php selected( $cp_options['mem']['content_type'], 2 )?>>application/json</option>
323 </select>
324 </p>
325 <p>
326 <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>
327 </p>
328 <p class="description" id="ct-1"<?php echo code_profiler_showhide( $cp_options['mem']['content_type'], 1 ) ?>>
329 <?php printf( esc_html__('Optional POST payload 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/http_method-1.png', dirname( __FILE__ ) ) .'" target="_blank" rel="noopener noreferrer">', '</a>') ?>
330 </p>
331 <p class="description" id="ct-3"<?php echo code_profiler_showhide( $cp_options['mem']['content_type'], 3 ) ?>>
332 <?php printf( esc_html__('Optional raw POST payload, on a single line.', 'code-profiler'), '<code>name=value</code>') ?> <?php printf( esc_html__('%sView example%s', 'code-profiler'), '<a href="'. plugins_url('/static/help/http_method-3.png', dirname( __FILE__ ) ) .'" target="_blank" rel="noopener noreferrer">', '</a>') ?>
333 </p>
334 <p class="description" id="ct-2"<?php echo code_profiler_showhide( $cp_options['mem']['content_type'], 2 ) ?>>
335 <?php printf( esc_html__('Optional JSON-encoded payload, on a single line.', 'code-profiler'), '<code>name=value</code>') ?> <?php printf( esc_html__('%sView example%s', 'code-profiler'), '<a href="'. plugins_url('/static/help/http_method-2.png', dirname( __FILE__ ) ) .'" target="_blank" rel="noopener noreferrer">', '</a>') ?>
336 </p>
337 </td>
338 </tr>
339 <tr>
340 <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>
341 <td>
342 <p><textarea name="cp-cookies" id="cp-cookies" class="regular-text code" maxlength="4000" rows="6"><?php echo esc_textarea( $cookies ) ?></textarea></p>
343 <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>
344 </td>
345 </tr>
346 <tr>
347 <th scope="row" class="row-med"><?php esc_html_e('HTTP headers', 'code-profiler'); ?> <span class="code-profiler-tip" data-tip="<?php esc_attr_e('You can use this field to add custom HTTP headers or even override existing ones (e.g., host, user-agent, accept-language etc). HTTP header names are case-insensitive and Code Profiler will automatically convert them to lowercase. HTTP header values are case-sensitive and only ASCII printable characters are allowed.', 'code-profiler') ?>"></span></th>
348 <td>
349 <textarea autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" id="custom-headers" name="custom_headers" class="regular-text code" rows="6"><?php echo esc_textarea( $custom_headers ) ?></textarea>
350 <p class="description"><?php printf( esc_html__('Optional HTTP header 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/custom_headers.png', dirname( __FILE__ ) ) .'" target="_blank" rel="noopener noreferrer">', '</a>') ?></p>
351 </td>
352 </tr>
353 <tr>
354 <th scope="row" class="row-med"><?php esc_html_e('File and folder exclusions', 'code-profiler'); ?> <span class="code-profiler-tip" data-tip="<?php esc_attr_e('This option lets you exclude files and folders from the profiling process. It can be a full path, a file or a folder name, or any part of them (substring). Values are case-sensitive and only ASCII printable characters are allowed.', 'code-profiler') ?>"></span></th>
355 <td>
356 <textarea autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" id="exclusions" name="exclusions" class="regular-text code" rows="6"><?php
357 foreach( $exclusions as $item ) {
358 echo esc_textarea( $item ) ."\n";
359 }
360 ?></textarea>
361 <p class="description"><?php esc_html_e('One item per line.', 'code-profiler') ?> <?php printf( esc_html__('%sView example%s', 'code-profiler'), '<a href="'. plugins_url('/static/help/exclusions.png', dirname( __FILE__ ) ) .'" target="_blank" rel="noopener noreferrer">', '</a>') ?></p>
362 </td>
363 </tr>
364 </table>
365 </div>
366 <?php wp_nonce_field('start_profiler_nonce', 'cp_nonce', 0); ?>
367 <br />
368 <div>
369 <input type="button" class="button 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 ?> />
370 &nbsp;&nbsp;&nbsp;
371 <input type="button" class="button 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') ?>" />
372 </div>
373 <div id="cp-progress-div" style="display:none">
374 <br />
375 <div class="cp-progress-bar"><span id="cp-span-progress" style="width:0%"></span></div>
376 <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 the profiler...', 'code-profiler') ?></font>
377 </div>
378 <?php
379
380 /**
381 * Re-run a profile.
382 */
383 if (! empty( $rerun ) ) {
384 echo '<script>window.addEventListener("load", cpjs_start_profiler);</script>';
385 }
386
387 // =====================================================================
388 // Fetch pages and posts.
389
390 function code_profiler_fetch_pages( $home, $cp_options ) {
391
392 $posts = '';
393
394 if ( empty( $cp_options['mem']['post'] ) ) {
395 $cp_options['mem']['post'] = $home;
396 }
397
398 if ( $cp_options['mem']['post'] == $home ) {
399 $pages = sprintf(
400 '<option value="%1$s" title="%1$s" selected>%2$s</option>',
401 esc_attr( $home ),
402 esc_html__('Homepage', 'code-profiler')
403 );
404 } else {
405 $pages = sprintf(
406 '<option value="%1$s" title="%1$s">%2$s</option>',
407 esc_attr( $home ),
408 esc_html__('Homepage', 'code-profiler')
409 );
410 }
411 $args = [
412 'posts_per_page' => -1,
413 'post_type' => ['page'],
414 'post_status' => 'publish',
415 'orderby' => 'post_name',
416 'order' => 'ASC'
417 ];
418 $query = new WP_Query( $args );
419
420 if ( $query ) {
421 $items = $query->posts;
422 foreach( $items as $item ) {
423 if ( empty( $item->post_title ) ) {
424 $item->post_title = __('Untitled');
425 }
426 $permalink = get_permalink( $item->ID );
427
428 if ( $cp_options['mem']['post'] == $permalink ) {
429 $pages .= sprintf(
430 '<option value="%1$s" title="%1$s" selected>%2$s</option>',
431 esc_attr( $permalink ),
432 esc_html( $item->post_title )
433 );
434 } else {
435 $pages .= sprintf(
436 '<option value="%1$s" title="%1$s">%2$s</option>',
437 esc_attr( $permalink ),
438 esc_html( $item->post_title )
439 );
440 }
441 }
442 }
443
444 return sprintf('<optgroup label="%s">%s</optgroup>',
445 esc_attr__('Pages', 'code-profiler'),
446 $pages
447 );
448 }
449
450 // =====================================================================
451 // Fetch admin pages (backend)
452
453 function code_profiler_fetch_admin_pages( $home, $cp_options ) {
454
455 $backend = '';
456
457 if ( empty( $cp_options['mem']['post'] ) ) {
458 $cp_options['mem']['post'] = $home;
459 }
460
461 if (! empty( $GLOBALS[ 'menu' ] ) ) {
462 foreach( $GLOBALS[ 'menu' ] as $menu => $value ) {
463 if (! empty( $value[0] ) && substr( $value[2], -4 ) == '.php') {
464 // E.g., "Comments", "Plugins" etc
465 $value[0] = trim( preg_replace('/\s*<.+$/', '', $value[0] ) );
466
467 if ( $cp_options['mem']['post'] == "{$home}wp-admin/{$value[2]}" ) {
468 $backend .= sprintf(
469 '<option value="%1$s" title="%1$s" selected>%2$s</option>',
470 esc_attr("{$home}wp-admin/{$value[2]}"),
471 esc_html( $value[0] )
472 );
473 } else {
474 $backend .= sprintf(
475 '<option value="%1$s" title="%1$s">%2$s</option>',
476 esc_attr("{$home}wp-admin/{$value[2]}"),
477 esc_html( $value[0] )
478 );
479 }
480 }
481 }
482 }
483 if ( empty( $backend ) ) {
484 $backend = sprintf(
485 '<option value="%1$swp-admin/" title="%1$swp-admin/">%2$s</option>',
486 esc_attr( $home ),
487 esc_html__('Dashboard', 'code-profiler')
488 );
489
490 }
491 return $backend;
492 }
493
494 // =====================================================================
495 // Fetch scheduled tasks.
496
497 function code_profiler_fetch_crontasks( $mem ) {
498
499 $cron_list = '';
500
501 $wp_crons = code_profiler_get_crons();
502
503 if ( empty( $wp_crons ) ) {
504 $cron_list = sprintf(
505 '<option value="0">%s</option>',
506 esc_html__('No cron tasks found.', 'code-profiler')
507 );
508 return $cron_list;
509 }
510
511 foreach( $wp_crons as $cron ) {
512
513 if ( $mem == $cron ) {
514 $selected = ' selected';
515 } else {
516 $selected = '';
517 }
518
519 $cron_list .= sprintf(
520 "<option value='%s'%s>%s</option>",
521 esc_attr( $cron ),
522 $selected,
523 esc_html( $cron )
524 );
525 }
526
527 return $cron_list;
528 }
529
530 // =====================================================================
531 // EOF
532