PluginProbe
Code Profiler – WordPress Performance Profiling and Debugging Made Easy / trunk
Code Profiler – WordPress Performance Profiling and Debugging Made Easy vtrunk
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 trunk, at lib/menu_settings.php

417 lines 15.8 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 // WP-CLI integration
148 if (! empty( $cp_options['enable_wpcli'] ) ) {
149 $cp_options['enable_wpcli'] = 1;
150 } else {
151 $cp_options['enable_wpcli'] = 0;
152 }
153 // Disable WP-CRON
154 if (! empty( $cp_options['disable_wpcron'] ) ) {
155 $cp_options['disable_wpcron'] = 1;
156 } else {
157 $cp_options['disable_wpcron'] = 0;
158 }
159
160 // HTTP code to reject
161 if (! empty( $cp_options['http_response'] ) ) {
162 if (! preg_match( "/{$cp_options['http_response']}/", '300') ) {
163 $http_response_300 = 0;
164 } else {
165 $http_response_300 = 1;
166 }
167 if (! preg_match( "/{$cp_options['http_response']}/", '400') ) {
168 $http_response_400 = 0;
169 } else {
170 $http_response_400 = 1;
171 }
172 if (! preg_match( "/{$cp_options['http_response']}/", '500') ) {
173 $http_response_500 = 0;
174 } else {
175 $http_response_500 = 1;
176 }
177 } else {
178 $http_response_300 = 0;
179 $http_response_400 = 0;
180 $http_response_500 = 0;
181 }
182 ?>
183 <tr>
184 <th scope="row"><?php esc_html_e('General Options', 'code-profiler') ?></th>
185 <td>
186 <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>
187 <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>
188
189 <br />
190
191 <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>
192
193 <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>
194
195 <br />
196
197 <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>
198 <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>
199 <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>
200 <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>
201 </td>
202 </tr>
203 </table>
204
205 <h3><?php esc_html_e('Profiler','code-profiler') ?></h3>
206 <table class="form-table">
207 <?php
208 // Accuracy
209 if ( empty( $cp_options['accuracy'] ) ||
210 ! preg_match('/^(?:1|5|10|15|20)$/D', $cp_options['accuracy'] ) ) {
211
212 $cp_options['accuracy'] = 1;
213 }
214 ?>
215 <tr>
216 <th scope="row"><?php esc_html_e('Accuracy & Precision', 'code-profiler') ?> <span class="code-profiler-tip" data-tip="<?php
217 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');
218 echo '<br />';
219 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');
220 ?>"></span></th>
221 <td>
222 <p><label><?php esc_html_e('Select the accuracy and precision level of the profiler:', 'code-profiler') ?> &nbsp;<select name="cp_options[accuracy]">
223 <?php
224 foreach( CODE_PROFILER_ACCURACY as $key => $value ) {
225 echo '<option value="'. esc_attr( $key ) .'"'. selected( $cp_options['accuracy'], $key, false ) .'>'. esc_html( $value ) .'</option>';
226 }
227 ?></select></label></p>
228 </td>
229 </tr>
230 <?php
231 $recommended = code_profiler_suggested_memory();
232 // Buffer size
233 if ( empty( $cp_options['buffer'] ) ) {
234 $cp_options['buffer'] = 0;
235 } elseif (! preg_match('/^(?:[1-9]|10)$/', $cp_options['buffer'] ) ) {
236 $cp_options['buffer'] = (int) $recommended;
237 }
238 ?>
239 <tr>
240 <th scope="row"><?php esc_html_e('Buffer size', 'code-profiler') ?> <span class="code-profiler-tip" data-tip="<?php
241 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');
242 ?>"></span></th>
243 <td>
244 <p>
245 <label>
246 <select name="cp_options[buffer]">
247 <?php
248 echo '<option value="0"'. selected( $cp_options['buffer'], 0, false ) .'>'.
249 esc_html__('Automatic', 'code-profiler') .
250 '</option>';
251 for ( $i = 1; $i <= 10; $i++ ) {
252 echo '<option value="'. $i .'"'. selected( $cp_options['buffer'], $i, false ) .'>'.
253 sprintf(
254 /* Translators: 'MB' for MegaBytes */
255 esc_html__('%s MB', 'code-profiler')
256 , $i ) .
257 '</option>';
258 }
259 ?>
260 </select>
261 </label>
262 </p>
263 <p class="description"><?php
264 printf(
265 esc_html__('Recommended value based on your system configuration: %s MB', 'code-profiler'),
266 (int) $recommended
267 );
268 ?></p>
269 </td>
270 </tr>
271 <?php
272 if ( empty( $cp_options['php_error'] ) ) {
273 $cp_options['php_error'] = 0;
274 } else {
275 $cp_options['php_error'] = 1;
276 }
277 ?>
278 <tr>
279 <th scope="row"><?php esc_html_e('PHP Error Logging', 'code-profiler') ?> <span class="code-profiler-tip" data-tip="<?php
280 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');
281 ?>"></span>
282 </th>
283 <td>
284 <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>
285 </td>
286 </tr>
287 </table>
288
289 <p><input type="submit" name="save-settings" class="button button-primary" value="<?php esc_attr_e('Save Settings', 'code-profiler') ?>" /></p>
290
291 </form>
292 <?php
293
294 // ===================================================================== 2023-06-15
295 // Save and validate the settings.
296
297 function code_profiler_save_settings() {
298
299 $cp_options = get_option('code-profiler');
300
301 // Paths
302 if ( empty( $_POST['cp_options']['show_paths'] ) ||
303 ! in_array( $_POST['cp_options']['show_paths'], ['absolute', 'relative' ] ) ) {
304
305 $cp_options['show_paths'] = 'relative';
306 } else {
307 $cp_options['show_paths'] = sanitize_key( $_POST['cp_options']['show_paths'] );
308 }
309
310 // Name vs slug
311 if ( empty( $_POST['cp_options']['display_name'] ) ||
312 ! in_array( $_POST['cp_options']['display_name'], ['full', 'slug' ] ) ) {
313
314 $cp_options['display_name'] = 'full';
315 } else {
316 $cp_options['display_name'] = sanitize_key( $_POST['cp_options']['display_name'] );
317 }
318
319 // Truncate names
320 if ( empty( $_POST['cp_options']['truncate_name'] ) ||
321 ! preg_match('/^\d+$/', ( $_POST['cp_options']['truncate_name'] ) ) ) {
322
323 $cp_options['truncate_name'] = 30;
324 } else {
325 $cp_options['truncate_name'] = (int) $_POST['cp_options']['truncate_name'];
326 }
327
328 // chart type
329 if ( empty( $_POST['cp_options']['chart_type'] ) ||
330 ! in_array( $_POST['cp_options']['chart_type'], ['x', 'y' ] ) ) {
331
332 $cp_options['chart_type'] = 'x';
333 } else {
334 $cp_options['chart_type'] = sanitize_key( $_POST['cp_options']['chart_type'] );
335 }
336
337 // Max plugins to display
338 if ( empty( $_POST['cp_options']['chart_max_plugins'] ) ||
339 ! preg_match('/^\d+$/', ( $_POST['cp_options']['chart_max_plugins'] ) ) ) {
340
341 $cp_options['chart_max_plugins'] = 25;
342 } else {
343 $cp_options['chart_max_plugins'] = (int) $_POST['cp_options']['chart_max_plugins'];
344 }
345
346 // Empty values
347 if (! empty( $_POST['cp_options']['hide_empty_value'] ) ) {
348 $cp_options['hide_empty_value'] = 1;
349 } else {
350 $cp_options['hide_empty_value'] = 0;
351 }
352
353 // WP-CLI integration
354 if (! empty( $_POST['cp_options']['enable_wpcli'] ) ) {
355 $cp_options['enable_wpcli'] = 1;
356 } else {
357 $cp_options['enable_wpcli'] = 0;
358 }
359
360 // WP-Cron
361 if (! empty( $_POST['cp_options']['disable_wpcron'] ) ) {
362 $cp_options['disable_wpcron'] = 1;
363 } else {
364 $cp_options['disable_wpcron'] = 0;
365 }
366
367 // HTTP status code
368 $code = '';
369 $cp_options['http_response'] = '';
370 if (! empty( $_POST['cp_options']['http_response_300'] ) ) {
371 $code .= '3|';
372 }
373 if (! empty( $_POST['cp_options']['http_response_400'] ) ) {
374 $code .= '4|';
375 }
376 if (! empty( $_POST['cp_options']['http_response_500'] ) ) {
377 $code .= '5';
378 }
379 $code = rtrim( $code, '|');
380 if (! empty( $code ) ) {
381 $cp_options['http_response'] = "^(?:$code)\d{2}$";
382 }
383
384 // Accuracy
385 if ( empty( $_POST['cp_options']['accuracy'] ) ||
386 ! preg_match('/^(?:1|5|10|15|20)$/D', $_POST['cp_options']['accuracy'] ) ) {
387
388 $cp_options['accuracy'] = 1;
389 } else {
390 $cp_options['accuracy'] = (int) $_POST['cp_options']['accuracy'];
391 }
392
393 // Buffer size
394 if ( empty( $_POST['cp_options']['buffer'] ) ) {
395 $cp_options['buffer'] = 0;
396 } elseif (! preg_match('/^(?:[1-9]|10)$/D', $_POST['cp_options']['buffer'] ) ) {
397 $cp_options['buffer'] = 10;
398 } else {
399 $cp_options['buffer'] = (int) $_POST['cp_options']['buffer'];
400 }
401
402 /**
403 * PHP error logging.
404 */
405 if (! empty( $_POST['cp_options']['php_error'] ) ) {
406 $cp_options['php_error'] = 1;
407 } else {
408 $cp_options['php_error'] = 0;
409 }
410
411 return update_option('code-profiler', $cp_options );
412
413 }
414
415 // =====================================================================
416 // EOF
417