PluginProbe
Code Profiler – WordPress Performance Profiling and Debugging Made Easy / 1.7.7
Code Profiler – WordPress Performance Profiling and Debugging Made Easy v1.7.7
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_settings.php

menu_settings.php in Code Profiler – WordPress Performance Profiling and Debugging Made Easy 1.7.7, at lib/menu_settings.php

436 lines 16.5 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') ) {
15 die('Forbidden');
16 }
17
18 // =====================================================================
19 // Display Summary tab.
20
21 echo code_profiler_display_tabs( 3 );
22
23 // Save settings?
24 if (! empty( $_POST['save-settings'] ) ) {
25 // Make sure we have security nonce
26 if ( empty( $_POST['cp-save-settings'] ) ||
27 ! wp_verify_nonce($_POST['cp-save-settings'], 'cp_settings') ) {
28
29 wp_nonce_ays('cp_settings');
30 }
31 $res = code_profiler_save_settings();
32 if ( $res === true ) {
33 printf(
34 CODE_PROFILER_UPDATE_NOTICE,
35 esc_html__('Your changes have been saved.', 'code-profiler')
36 );
37 }
38 else {
39 printf(
40 CODE_PROFILER_ERROR_NOTICE,
41 esc_html__('No changes were detected.', 'code-profiler')
42 );
43 }
44 }
45
46 // Fetch current options
47 $cp_options = get_option('code-profiler');
48
49 ?>
50 <form method="post">
51 <?php wp_nonce_field('cp_settings', 'cp-save-settings', 0); ?>
52 <table class="form-table">
53 <?php
54
55 // Paths
56 if ( empty( $cp_options['show_paths'] ) ||
57 ! in_array( $cp_options['show_paths'], ['absolute', 'relative' ] ) ) {
58
59 $cp_options['show_paths'] = 'relative';
60 }
61 ?>
62 <tr>
63 <th scope="row"><?php esc_html_e('Paths','code-profiler') ?></th>
64 <td>
65 <p><label><input type="radio" name="cp_options[show_paths]"<?php checked( $cp_options['show_paths'], 'absolute') ?> value="absolute" /><?php printf( esc_html__('Show absolute paths (e.g., %s)','code-profiler'), esc_html( ABSPATH ) .'wp-admin/index.php') ?></label></p>
66 <p><label><input type="radio" name="cp_options[show_paths]"<?php checked( $cp_options['show_paths'], 'relative') ?> value="relative" /><?php printf( esc_html__('Show relative paths (e.g., %s)','code-profiler'), 'wp-admin/index.php') ?></label></p>
67 </td>
68 </tr>
69
70 <?php
71 // Name vs slug
72 if ( empty( $cp_options['display_name'] ) ||
73 ! in_array( $cp_options['display_name'], ['full', 'slug' ] ) ) {
74
75 $cp_options['display_name'] = 'full';
76 }
77 // Truncate names
78 if ( empty( $cp_options['truncate_name'] ) ||
79 ! preg_match('/^\d+$/', ( $cp_options['truncate_name'] ) ) ) {
80
81 $cp_options['truncate_name'] = 30;
82 }
83 ?>
84 <tr>
85 <th scope="row"><?php esc_html_e('Plugins & Themes','code-profiler') ?></th>
86 <td>
87 <p><label><input type="radio" name="cp_options[display_name]"<?php checked( $cp_options['display_name'], 'full') ?> value="full" /><?php printf( esc_html__('Show real names (e.g., %s)','code-profiler'), 'Super Plugin Pro for WordPress') ?></label></p>
88 <p><label><input type="radio" name="cp_options[display_name]"<?php checked( $cp_options['display_name'], 'slug') ?> value="slug" /><?php printf( esc_html__('Show slugs (e.g., %s)','code-profiler'), 'super-plugin-pro') ?></label></p>
89 <br />
90 <p><label><?php
91 printf(
92 esc_html__('Truncate names to %s characters', 'code-profiler'),
93 '<input class="small-text" type="number" size="4" min="0" maxlength="100" name="cp_options[truncate_name]" value="'. (int) $cp_options['truncate_name'] .'" />'
94 );
95 ?></label></p>
96 <p class="description"><?php esc_html_e('A name longer than 30 characters will be difficult to read on most charts.', 'code-profiler') ?></p>
97 </td>
98 </tr>
99
100 <?php
101 // chart type
102 if ( empty( $cp_options['chart_type'] ) ||
103 ! in_array( $cp_options['chart_type'], ['x', 'y' ] ) ) {
104
105 $cp_options['chart_type'] = 'x';
106 }
107 // Max plugins to display
108 if ( empty( $cp_options['chart_max_plugins'] ) ||
109 ! preg_match('/^\d+$/', ( $cp_options['chart_max_plugins'] ) ) ) {
110
111 $cp_options['chart_max_plugins'] = 25;
112 }
113 // Empty values
114 if (! empty( $cp_options['hide_empty_value'] ) ) {
115 $cp_options['hide_empty_value'] = 1;
116 } else {
117 $cp_options['hide_empty_value'] = 0;
118 }
119 ?>
120 <tr>
121 <th scope="row"><?php esc_html_e('Tables & Charts','code-profiler') ?></th>
122 <td>
123 <p><label><input type="radio" name="cp_options[chart_type]"<?php checked( $cp_options['chart_type'], 'x') ?> value="x" /><?php esc_html_e('Display horizontal charts by default','code-profiler') ?></label></p>
124 <p><label><input type="radio" name="cp_options[chart_type]"<?php checked( $cp_options['chart_type'], 'y') ?> value="y" /><?php esc_html_e('Display vertical charts by default','code-profiler') ?></label></p>
125 <p class="description"><?php esc_html_e('You can switch between vertical and horizontal by clicking the corresponding button located below each chart.', 'code-profiler') ?></p>
126
127 <br />
128
129 <p><label><?php
130 printf(
131 esc_html__('Do not display more than %s plugins on a chart', 'code-profiler'),
132 '<input class="small-text" type="number" size="3" min="1" maxlength="100" name="cp_options[chart_max_plugins]" value="'. (int) $cp_options['chart_max_plugins'] .'" />'
133 );
134 ?></label></p>
135 <p class="description"><?php esc_html_e('More than 30 plugins will be difficult to read on most charts. Note that only plugins that have a negligible impact on your website\'s performance will be hidden.', 'code-profiler') ?></p>
136
137 <br />
138
139 <p><label><input type="checkbox" name="cp_options[hide_empty_value]"<?php checked( $cp_options['hide_empty_value'], '1') ?> /><?php esc_html_e('Hide items that have an empty value.','code-profiler') ?></label></p>
140
141 <p class="description"><?php esc_html_e('This options will hide items that have an empty value on all graphs and tables.', 'code-profiler') ?></p>
142
143 </td>
144 </tr>
145
146 <?php
147 // Composer warning
148 if (! empty( $cp_options['warn_composer'] ) ) {
149 $cp_options['warn_composer'] = 1;
150 } else {
151 $cp_options['warn_composer'] = 0;
152 }
153 // WP-CLI integration
154 if (! empty( $cp_options['enable_wpcli'] ) ) {
155 $cp_options['enable_wpcli'] = 1;
156 } else {
157 $cp_options['enable_wpcli'] = 0;
158 }
159 // Disable WP-CRON
160 if (! empty( $cp_options['disable_wpcron'] ) ) {
161 $cp_options['disable_wpcron'] = 1;
162 } else {
163 $cp_options['disable_wpcron'] = 0;
164 }
165
166 // HTTP code to reject
167 if (! empty( $cp_options['http_response'] ) ) {
168 if (! preg_match( "/{$cp_options['http_response']}/", '300') ) {
169 $http_response_300 = 0;
170 } else {
171 $http_response_300 = 1;
172 }
173 if (! preg_match( "/{$cp_options['http_response']}/", '400') ) {
174 $http_response_400 = 0;
175 } else {
176 $http_response_400 = 1;
177 }
178 if (! preg_match( "/{$cp_options['http_response']}/", '500') ) {
179 $http_response_500 = 0;
180 } else {
181 $http_response_500 = 1;
182 }
183 } else {
184 $http_response_300 = 0;
185 $http_response_400 = 0;
186 $http_response_500 = 0;
187 }
188 ?>
189 <tr>
190 <th scope="row"><?php esc_html_e('General Options', 'code-profiler') ?></th>
191 <td>
192 <p><label><input type="checkbox" name="cp_options[warn_composer]"<?php checked( $cp_options['warn_composer'], '1') ?> /><?php esc_html_e('Show a notice when several plugins are using Composer dependency manager.','code-profiler') ?></label></p>
193
194 <p class="description"><?php esc_html_e('Consult the FAQ tab for more details about this option.', 'code-profiler') ?></p>
195
196 <br />
197
198 <p><label><input type="checkbox" name="cp_options[enable_wpcli]"<?php checked( $cp_options['enable_wpcli'], '1') ?> /><?php esc_html_e('Enable WP-CLI integration.','code-profiler') ?></label></p>
199 <p class="description"><?php printf( esc_html__('Enter %s to display the available command line options.', 'code-profiler'), '<code>wp code-profiler help</code>') ?></p>
200
201 <br />
202
203 <p><label><input type="checkbox" name="cp_options[disable_wpcron]"<?php checked( $cp_options['disable_wpcron'], '1') ?> /><?php esc_html_e('Disable WP-Cron when running the profiler.','code-profiler') ?></label></p>
204
205 <p class="description"><?php esc_html_e('This option will prevent WP-Cron to run scheduled tasks in the background that could affect the results of the profiler.', 'code-profiler') ?></p>
206
207 <br />
208
209 <p><?php esc_html_e('Stop the profiler and throw an error if the server returns any of the following HTTP status codes:', 'code-profiler') ?></p>
210 <p><label><input type="checkbox" name="cp_options[http_response_300]" value="1"<?php checked( $http_response_300, '1') ?> /><?php esc_html_e('3xx redirection (301 Moved Permanently, 302 Found etc)','code-profiler') ?></label></p>
211 <p><label><input type="checkbox" name="cp_options[http_response_400]" value="1"<?php checked( $http_response_400, '1') ?> /><?php esc_html_e('4xx client errors (400 Bad Request, 403 Forbidden, 404 Not Found etc)','code-profiler') ?></label></p>
212 <p><label><input type="checkbox" name="cp_options[http_response_500]" value="1"<?php checked( $http_response_500, '1') ?> /><?php esc_html_e('5xx server errors (500 Internal Server Error, 503 Service Unavailable etc)','code-profiler') ?></label></p>
213 </td>
214 </tr>
215 </table>
216
217 <h3><?php esc_html_e('Profiler','code-profiler') ?></h3>
218 <table class="form-table">
219 <?php
220 // Accuracy
221 if ( empty( $cp_options['accuracy'] ) ||
222 ! preg_match('/^(?:1|5|10|15|20)$/D', $cp_options['accuracy'] ) ) {
223
224 $cp_options['accuracy'] = 1;
225 }
226 ?>
227 <tr>
228 <th scope="row"><?php esc_html_e('Accuracy & Precision', 'code-profiler') ?> <span class="code-profiler-tip" data-tip="<?php
229 esc_attr_e('When accuracy is set to the highest level, Code Profiler runs as a "tracing profiler", and when accuracy is set to a lower level, it runs as a "sampling profiler".', 'code-profiler');
230 echo '<br />';
231 esc_attr_e('Selecting a high accuracy level is preferred and will work on most sites, but the profiling process will take longer to execute as opposed to choosing a lower accuracy level. If you have a slow WordPress site with a lot of plugins installed and your server or reverse proxy is timing out when Code Profiler is running (e.g., "503 Service Unavailable" or "504 Gateway Timeout" error), try to lower the accuracy level in order to speed up the profiling process and avoid the server timeout.', 'code-profiler');
232 ?>"></span></th>
233 <td>
234 <p><label><?php esc_html_e('Select the accuracy and precision level of the profiler:', 'code-profiler') ?> &nbsp;<select name="cp_options[accuracy]">
235 <?php
236 foreach( CODE_PROFILER_ACCURACY as $key => $value ) {
237 echo '<option value="'. esc_attr( $key ) .'"'. selected( $cp_options['accuracy'], $key, false ) .'>'. esc_html( $value ) .'</option>';
238 }
239 ?></select></label></p>
240 </td>
241 </tr>
242 <?php
243 $recommended = code_profiler_suggested_memory();
244 // Buffer size
245 if ( empty( $cp_options['buffer'] ) ) {
246 $cp_options['buffer'] = 0;
247 } elseif (! preg_match('/^(?:[1-9]|10)$/', $cp_options['buffer'] ) ) {
248 $cp_options['buffer'] = (int) $recommended;
249 }
250 ?>
251 <tr>
252 <th scope="row"><?php esc_html_e('Buffer size', 'code-profiler') ?> <span class="code-profiler-tip" data-tip="<?php
253 esc_attr_e('When running, the profiler collects data and saves it to a memory buffer. When the buffer is full, the data is written to disk. If your site uses too much memory and PHP throws a memory error, you can try to lower that value.', 'code-profiler');
254 ?>"></span></th>
255 <td>
256 <p>
257 <label>
258 <select name="cp_options[buffer]">
259 <?php
260 echo '<option value="0"'. selected( $cp_options['buffer'], 0, false ) .'>'.
261 esc_html__('Automatic', 'code-profiler') .
262 '</option>';
263 for ( $i = 1; $i <= 10; $i++ ) {
264 echo '<option value="'. $i .'"'. selected( $cp_options['buffer'], $i, false ) .'>'.
265 sprintf(
266 /* Translators: 'MB' for MegaBytes */
267 esc_html__('%s MB', 'code-profiler')
268 , $i ) .
269 '</option>';
270 }
271 ?>
272 </select>
273 </label>
274 </p>
275 <p class="description"><?php
276 printf(
277 esc_html__('Recommended value based on your system configuration: %s MB', 'code-profiler'),
278 (int) $recommended
279 );
280 ?></p>
281 </td>
282 </tr>
283 <?php
284 if ( empty( $cp_options['php_error'] ) ) {
285 $cp_options['php_error'] = 0;
286 } else {
287 $cp_options['php_error'] = 1;
288 }
289 ?>
290 <tr>
291 <th scope="row"><?php esc_html_e('PHP Error Logging', 'code-profiler') ?> <span class="code-profiler-tip" data-tip="<?php
292 esc_attr_e('This option will enable PHP error logging when the profiler is running. The log will be saved by default to "/wp-content/debug.log", unless you configured PHP or WordPress to use a different path.', 'code-profiler');
293 ?>"></span>
294 </th>
295 <td>
296 <p><label><input type="checkbox" name="cp_options[php_error]"<?php checked( $cp_options['php_error'], '1') ?> /><?php esc_html_e('Enable PHP error logging.','code-profiler') ?></label></p>
297 </td>
298 </tr>
299 </table>
300
301 <p><input type="submit" name="save-settings" class="button button-primary" value="<?php esc_attr_e('Save Settings', 'code-profiler') ?>" /></p>
302
303 </form>
304 <?php
305
306 // ===================================================================== 2023-06-15
307 // Save and validate the settings.
308
309 function code_profiler_save_settings() {
310
311 $cp_options = get_option('code-profiler');
312
313 // Paths
314 if ( empty( $_POST['cp_options']['show_paths'] ) ||
315 ! in_array( $_POST['cp_options']['show_paths'], ['absolute', 'relative' ] ) ) {
316
317 $cp_options['show_paths'] = 'relative';
318 } else {
319 $cp_options['show_paths'] = sanitize_key( $_POST['cp_options']['show_paths'] );
320 }
321
322 // Name vs slug
323 if ( empty( $_POST['cp_options']['display_name'] ) ||
324 ! in_array( $_POST['cp_options']['display_name'], ['full', 'slug' ] ) ) {
325
326 $cp_options['display_name'] = 'full';
327 } else {
328 $cp_options['display_name'] = sanitize_key( $_POST['cp_options']['display_name'] );
329 }
330
331 // Truncate names
332 if ( empty( $_POST['cp_options']['truncate_name'] ) ||
333 ! preg_match('/^\d+$/', ( $_POST['cp_options']['truncate_name'] ) ) ) {
334
335 $cp_options['truncate_name'] = 30;
336 } else {
337 $cp_options['truncate_name'] = (int) $_POST['cp_options']['truncate_name'];
338 }
339
340 // chart type
341 if ( empty( $_POST['cp_options']['chart_type'] ) ||
342 ! in_array( $_POST['cp_options']['chart_type'], ['x', 'y' ] ) ) {
343
344 $cp_options['chart_type'] = 'x';
345 } else {
346 $cp_options['chart_type'] = sanitize_key( $_POST['cp_options']['chart_type'] );
347 }
348
349 // Max plugins to display
350 if ( empty( $_POST['cp_options']['chart_max_plugins'] ) ||
351 ! preg_match('/^\d+$/', ( $_POST['cp_options']['chart_max_plugins'] ) ) ) {
352
353 $cp_options['chart_max_plugins'] = 25;
354 } else {
355 $cp_options['chart_max_plugins'] = (int) $_POST['cp_options']['chart_max_plugins'];
356 }
357
358 // Empty values
359 if (! empty( $_POST['cp_options']['hide_empty_value'] ) ) {
360 $cp_options['hide_empty_value'] = 1;
361 } else {
362 $cp_options['hide_empty_value'] = 0;
363 }
364
365 // Composer warning
366 if (! empty( $_POST['cp_options']['warn_composer'] ) ) {
367 $cp_options['warn_composer'] = 1;
368 } else {
369 $cp_options['warn_composer'] = 0;
370 }
371
372 // WP-CLI integration
373 if (! empty( $_POST['cp_options']['enable_wpcli'] ) ) {
374 $cp_options['enable_wpcli'] = 1;
375 } else {
376 $cp_options['enable_wpcli'] = 0;
377 }
378
379 // WP-Cron
380 if (! empty( $_POST['cp_options']['disable_wpcron'] ) ) {
381 $cp_options['disable_wpcron'] = 1;
382 } else {
383 $cp_options['disable_wpcron'] = 0;
384 }
385
386 // HTTP status code
387 $code = '';
388 $cp_options['http_response'] = '';
389 if (! empty( $_POST['cp_options']['http_response_300'] ) ) {
390 $code .= '3|';
391 }
392 if (! empty( $_POST['cp_options']['http_response_400'] ) ) {
393 $code .= '4|';
394 }
395 if (! empty( $_POST['cp_options']['http_response_500'] ) ) {
396 $code .= '5';
397 }
398 $code = rtrim( $code, '|');
399 if (! empty( $code ) ) {
400 $cp_options['http_response'] = "^(?:$code)\d{2}$";
401 }
402
403 // Accuracy
404 if ( empty( $_POST['cp_options']['accuracy'] ) ||
405 ! preg_match('/^(?:1|5|10|15|20)$/D', $_POST['cp_options']['accuracy'] ) ) {
406
407 $cp_options['accuracy'] = 1;
408 } else {
409 $cp_options['accuracy'] = (int) $_POST['cp_options']['accuracy'];
410 }
411
412 // Buffer size
413 if ( empty( $_POST['cp_options']['buffer'] ) ) {
414 $cp_options['buffer'] = 0;
415 } elseif (! preg_match('/^(?:[1-9]|10)$/D', $_POST['cp_options']['buffer'] ) ) {
416 $cp_options['buffer'] = 10;
417 } else {
418 $cp_options['buffer'] = (int) $_POST['cp_options']['buffer'];
419 }
420
421 /**
422 * PHP error logging.
423 */
424 if (! empty( $_POST['cp_options']['php_error'] ) ) {
425 $cp_options['php_error'] = 1;
426 } else {
427 $cp_options['php_error'] = 0;
428 }
429
430 return update_option('code-profiler', $cp_options );
431
432 }
433
434 // =====================================================================
435 // EOF
436