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

391 lines 15.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://code-profiler.com/ |
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'] ) || ! wp_verify_nonce($_POST['cp-save-settings'], 'cp_settings') ) {
27 wp_nonce_ays('cp_settings');
28 }
29 $res = code_profiler_save_settings();
30 if ( $res === true ) {
31 printf( CODE_PROFILER_UPDATE_NOTICE, esc_html__('Your changes have been saved.', 'code-profiler') );
32 }
33 else {
34 printf( CODE_PROFILER_ERROR_NOTICE, esc_html__('No changes were detected.', 'code-profiler') );
35 }
36 }
37
38 // Fetch current options
39 $cp_options = get_option('code-profiler');
40
41 ?>
42 <form method="post">
43 <?php wp_nonce_field('cp_settings', 'cp-save-settings', 0); ?>
44 <table class="form-table">
45 <?php
46
47 // Paths
48 if ( empty( $cp_options['show_paths'] ) || ! in_array( $cp_options['show_paths'], ['absolute', 'relative' ] ) ) {
49 $cp_options['show_paths'] = 'relative';
50 }
51 ?>
52 <tr>
53 <th scope="row"><?php esc_html_e('Paths','code-profiler') ?></th>
54 <td>
55 <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>
56 <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>
57 </td>
58 </tr>
59
60 <?php
61 // Name vs slug
62 if ( empty( $cp_options['display_name'] ) || ! in_array( $cp_options['display_name'], ['full', 'slug' ] ) ) {
63 $cp_options['display_name'] = 'full';
64 }
65 // Truncate names
66 if ( empty( $cp_options['truncate_name'] ) || ! preg_match('/^\d+$/', ( $cp_options['truncate_name'] ) ) ) {
67 $cp_options['truncate_name'] = 30;
68 }
69 ?>
70 <tr>
71 <th scope="row"><?php esc_html_e('Plugins & Themes','code-profiler') ?></th>
72 <td>
73 <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>
74 <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>
75 <br />
76 <p><label><?php
77 printf(
78 esc_html__('Truncate names to %s characters', 'code-profiler'),
79 '<input class="small-text" type="number" size="4" min="0" maxlength="100" name="cp_options[truncate_name]" value="'. (int) $cp_options['truncate_name'] .'" />'
80 );
81 ?></label></p>
82 <p class="description"><?php esc_html_e('A name longer than 30 characters will be difficult to read on most charts.', 'code-profiler') ?></p>
83 </td>
84 </tr>
85
86 <?php
87 // chart type
88 if ( empty( $cp_options['chart_type'] ) || ! in_array( $cp_options['chart_type'], ['x', 'y' ] ) ) {
89 $cp_options['chart_type'] = 'x';
90 }
91 // Max plugins to display
92 if ( empty( $cp_options['chart_max_plugins'] ) || ! preg_match('/^\d+$/', ( $cp_options['chart_max_plugins'] ) ) ) {
93 $cp_options['chart_max_plugins'] = 25;
94 }
95 // Empty values
96 if (! empty( $cp_options['hide_empty_value'] ) ) {
97 $cp_options['hide_empty_value'] = 1;
98 } else {
99 $cp_options['hide_empty_value'] = 0;
100 }
101 ?>
102 <tr>
103 <th scope="row"><?php esc_html_e('Tables & Charts','code-profiler') ?></th>
104 <td>
105 <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>
106 <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>
107 <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>
108
109 <br />
110
111 <p><label><?php
112 printf(
113 esc_html__('Do not display more than %s plugins on a chart', 'code-profiler'),
114 '<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'] .'" />'
115 );
116 ?></label></p>
117 <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>
118
119 <br />
120
121 <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>
122
123 <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>
124
125 </td>
126 </tr>
127
128 <?php
129 // Composer warning
130 if (! empty( $cp_options['warn_composer'] ) ) {
131 $cp_options['warn_composer'] = 1;
132 } else {
133 $cp_options['warn_composer'] = 0;
134 }
135 // WP-CLI integration
136 if (! empty( $cp_options['enable_wpcli'] ) ) {
137 $cp_options['enable_wpcli'] = 1;
138 } else {
139 $cp_options['enable_wpcli'] = 0;
140 }
141 // Disable WP-CRON
142 if (! empty( $cp_options['disable_wpcron'] ) ) {
143 $cp_options['disable_wpcron'] = 1;
144 } else {
145 $cp_options['disable_wpcron'] = 0;
146 }
147
148 // HTTP code to reject
149 if (! empty( $cp_options['http_response'] ) ) {
150 if (! preg_match( "/{$cp_options['http_response']}/", '300') ) {
151 $http_response_300 = 0;
152 } else {
153 $http_response_300 = 1;
154 }
155 if (! preg_match( "/{$cp_options['http_response']}/", '400') ) {
156 $http_response_400 = 0;
157 } else {
158 $http_response_400 = 1;
159 }
160 if (! preg_match( "/{$cp_options['http_response']}/", '500') ) {
161 $http_response_500 = 0;
162 } else {
163 $http_response_500 = 1;
164 }
165 } else {
166 $http_response_300 = 0;
167 $http_response_400 = 0;
168 $http_response_500 = 0;
169 }
170 ?>
171 <tr>
172 <th scope="row"><?php esc_html_e('General Options', 'code-profiler') ?></th>
173 <td>
174 <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>
175
176 <p class="description"><?php esc_html_e('Consult the FAQ tab for more details about this option.', 'code-profiler') ?></p>
177
178 <br />
179
180 <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>
181 <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>
182
183 <br />
184
185 <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>
186
187 <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>
188
189 <br />
190
191 <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>
192 <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>
193 <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>
194 <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>
195 </td>
196 </tr>
197 </table>
198
199 <h3><?php esc_html_e('Profiler','code-profiler') ?></h3>
200 <table class="form-table">
201 <?php
202 // Accuracy
203 if ( empty( $cp_options['accuracy'] ) || ! preg_match('/^(?:1|5|10|15|20)$/D', $cp_options['accuracy'] ) ) {
204 $cp_options['accuracy'] = 1;
205 }
206 ?>
207 <tr>
208 <th scope="row"><?php esc_html_e('Accuracy & Precision', 'code-profiler') ?> <span class="code-profiler-tip" data-tip="<?php
209 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');
210 echo '<br />';
211 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');
212 ?>"></span></th>
213 <td>
214 <p><label><?php esc_html_e('Select the accuracy and precision level of the profiler:', 'code-profiler') ?> &nbsp;<select name="cp_options[accuracy]">
215 <?php
216 foreach( CODE_PROFILER_ACCURACY as $key => $value ) {
217 echo '<option value="'. esc_attr( $key ) .'"'. selected( $cp_options['accuracy'], $key, false ) .'>'. esc_html( $value ) .'</option>';
218 }
219 ?></select></label></p>
220 </td>
221 </tr>
222 <?php
223 $recommended = code_profiler_suggested_memory();
224 // Buffer size
225 if ( empty( $cp_options['buffer'] ) ) {
226 $cp_options['buffer'] = 0;
227 } elseif (! preg_match('/^(?:[1-9]|10)$/', $cp_options['buffer'] ) ) {
228 $cp_options['buffer'] = (int) $recommended;
229 }
230 ?>
231 <tr>
232 <th scope="row"><?php esc_html_e('Buffer size', 'code-profiler') ?> <span class="code-profiler-tip" data-tip="<?php
233 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');
234 ?>"></span></th>
235 <td>
236 <p>
237 <label>
238 <select name="cp_options[buffer]">
239 <?php
240 echo '<option value="0"'. selected( $cp_options['buffer'], 0, false ) .'>'.
241 esc_html__('Automatic', 'code-profiler') .
242 '</option>';
243 for ( $i = 1; $i <= 10; $i++ ) {
244 echo '<option value="'. $i .'"'. selected( $cp_options['buffer'], $i, false ) .'>'.
245 sprintf(
246 /* Translators: 'MB' for MegaBytes */
247 esc_html__('%s MB', 'code-profiler')
248 , $i ) .
249 '</option>';
250 }
251 ?>
252 </select>
253 </label>
254 </p>
255 <p class="description"><?php
256 printf(
257 esc_html__('Recommended value based on your system configuration: %s MB', 'code-profiler'),
258 (int) $recommended
259 );
260 ?></p>
261 </td>
262 </tr>
263 </table>
264
265 <p><input type="submit" name="save-settings" class="button button-primary" value="<?php esc_attr_e('Save Settings', 'code-profiler') ?>" /></p>
266
267 </form>
268 <?php
269
270 // ===================================================================== 2023-06-15
271 // Save and validate the settings.
272
273 function code_profiler_save_settings() {
274
275 $cp_options = get_option('code-profiler');
276
277 // Paths
278 if ( empty( $_POST['cp_options']['show_paths'] ) ||
279 ! in_array( $_POST['cp_options']['show_paths'], ['absolute', 'relative' ] ) ) {
280
281 $cp_options['show_paths'] = 'relative';
282 } else {
283 $cp_options['show_paths'] = sanitize_key( $_POST['cp_options']['show_paths'] );
284 }
285
286 // Name vs slug
287 if ( empty( $_POST['cp_options']['display_name'] ) ||
288 ! in_array( $_POST['cp_options']['display_name'], ['full', 'slug' ] ) ) {
289
290 $cp_options['display_name'] = 'full';
291 } else {
292 $cp_options['display_name'] = sanitize_key( $_POST['cp_options']['display_name'] );
293 }
294
295 // Truncate names
296 if ( empty( $_POST['cp_options']['truncate_name'] ) ||
297 ! preg_match('/^\d+$/', ( $_POST['cp_options']['truncate_name'] ) ) ) {
298
299 $cp_options['truncate_name'] = 30;
300 } else {
301 $cp_options['truncate_name'] = (int) $_POST['cp_options']['truncate_name'];
302 }
303
304 // chart type
305 if ( empty( $_POST['cp_options']['chart_type'] ) ||
306 ! in_array( $_POST['cp_options']['chart_type'], ['x', 'y' ] ) ) {
307
308 $cp_options['chart_type'] = 'x';
309 } else {
310 $cp_options['chart_type'] = sanitize_key( $_POST['cp_options']['chart_type'] );
311 }
312
313 // Max plugins to display
314 if ( empty( $_POST['cp_options']['chart_max_plugins'] ) ||
315 ! preg_match('/^\d+$/', ( $_POST['cp_options']['chart_max_plugins'] ) ) ) {
316
317 $cp_options['chart_max_plugins'] = 25;
318 } else {
319 $cp_options['chart_max_plugins'] = (int) $_POST['cp_options']['chart_max_plugins'];
320 }
321
322 // Empty values
323 if (! empty( $_POST['cp_options']['hide_empty_value'] ) ) {
324 $cp_options['hide_empty_value'] = 1;
325 } else {
326 $cp_options['hide_empty_value'] = 0;
327 }
328
329 // Composer warning
330 if (! empty( $_POST['cp_options']['warn_composer'] ) ) {
331 $cp_options['warn_composer'] = 1;
332 } else {
333 $cp_options['warn_composer'] = 0;
334 }
335
336 // WP-CLI integration
337 if (! empty( $_POST['cp_options']['enable_wpcli'] ) ) {
338 $cp_options['enable_wpcli'] = 1;
339 } else {
340 $cp_options['enable_wpcli'] = 0;
341 }
342
343 // WP-Cron
344 if (! empty( $_POST['cp_options']['disable_wpcron'] ) ) {
345 $cp_options['disable_wpcron'] = 1;
346 } else {
347 $cp_options['disable_wpcron'] = 0;
348 }
349
350 // HTTP status code
351 $code = '';
352 $cp_options['http_response'] = '';
353 if (! empty( $_POST['cp_options']['http_response_300'] ) ) {
354 $code .= '3|';
355 }
356 if (! empty( $_POST['cp_options']['http_response_400'] ) ) {
357 $code .= '4|';
358 }
359 if (! empty( $_POST['cp_options']['http_response_500'] ) ) {
360 $code .= '5';
361 }
362 $code = rtrim( $code, '|');
363 if (! empty( $code ) ) {
364 $cp_options['http_response'] = "^(?:$code)\d{2}$";
365 }
366
367 // Accuracy
368 if ( empty( $_POST['cp_options']['accuracy'] ) ||
369 ! preg_match('/^(?:1|5|10|15|20)$/D', $_POST['cp_options']['accuracy'] ) ) {
370
371 $cp_options['accuracy'] = 1;
372 } else {
373 $cp_options['accuracy'] = (int) $_POST['cp_options']['accuracy'];
374 }
375
376 // Buffer size
377 if ( empty( $_POST['cp_options']['buffer'] ) ) {
378 $cp_options['buffer'] = 0;
379 } elseif (! preg_match('/^(?:[1-9]|10)$/D', $_POST['cp_options']['buffer'] ) ) {
380 $cp_options['buffer'] = 10;
381 } else {
382 $cp_options['buffer'] = (int) $_POST['cp_options']['buffer'];
383 }
384
385 return update_option('code-profiler', $cp_options );
386
387 }
388
389 // =====================================================================
390 // EOF
391