PluginProbe
WP Debugging / 2.2.0
WP Debugging v2.2.0
2.12.6 2.12.5 trunk 2.10.0 2.10.1 2.10.2 2.11.0 2.11.1 2.11.10 2.11.11 2.11.12 2.11.13 2.11.14 2.11.15 2.11.16 2.11.17 2.11.18 2.11.19 2.11.2 2.11.20 2.11.21 2.11.22 2.11.23 2.11.24 2.11.3 All 59 releases
← All changes | src/Settings.php +44 -177 2.11.02.2.0 View file →
@@ -8,13 +8,8 @@
8 8 */
9 9
10 10 namespace Fragen\WP_Debugging;
11 11
12 -// Exit if called directly.
13 -if ( ! defined( 'WPINC' ) ) {
14 - die;
15 -}
16 -
17 12 /**
18 13 * Class Settings
19 14 */
20 15 class Settings {
@@ -20,72 +15,26 @@
20 15 class Settings {
21 16 /**
22 17 * Hold plugin options.
23 18 *
24 - * @var array
19 + * @var array $options
25 20 */
26 21 protected static $options;
27 22
28 23 /**
29 - * Hold `wp-config.php` file path.
30 - *
31 - * @var string
32 - */
33 - protected static $config_path;
34 -
35 - /**
36 - * Holds pre-defined constants for `wp-config.php`.
37 - *
38 - * @var array
39 - */
40 - protected $defined_constants;
41 -
42 - /**
43 - * Holds config args for WPConfigTransformer.
44 - *
45 - * @var array
46 - */
47 - protected static $config_args;
48 -
49 - /**
50 - * Holds nonce.
51 - *
52 - * @var $nonce
53 - */
54 - protected static $nonce;
55 -
56 - /**
57 24 * Constructor.
58 25 *
59 - * @param array $options Plugin options.
60 - * @param string $config_path Path to config file.
61 - * @param array $defined_constants Pre-defined constant group.
26 + * @param array $options Plugin options.
62 27 * @return void
63 28 */
64 - public function __construct( $options, $config_path, $defined_constants ) {
65 - self::$options = $options;
66 - self::$config_path = $config_path;
67 - $this->defined_constants = $defined_constants;
68 - self::$config_args = [ 'normalize' => true ];
69 - static::$nonce = wp_create_nonce( 'wp-debugging' );
70 -
71 - if ( false === strpos( file_get_contents( self::$config_path ), "/* That's all, stop editing!" ) ) {
72 - if ( 1 === preg_match( '@\$table_prefix = (.*);@', file_get_contents( self::$config_path ), $matches ) ) {
73 - self::$config_args = array_merge(
74 - self::$config_args,
75 - [
76 - 'anchor' => "$matches[0]",
77 - 'placement' => 'after',
78 - ]
79 - );
80 - }
81 - }
29 + public function __construct( $options ) {
30 + self::$options = $options;
82 31 }
83 32
84 33 /**
85 34 * Load hooks for settings.
86 35 *
87 - * @return self
36 + * @return void
88 37 */
89 38 public function load_hooks() {
90 39 add_action( 'admin_init', [ $this, 'add_settings' ] );
91 40 add_action( is_multisite() ? 'network_admin_menu' : 'admin_menu', [ $this, 'add_plugin_menu' ] );
@@ -96,10 +45,8 @@
96 45 ? 'network_admin_plugin_action_links_wp-debugging/wp-debugging.php'
97 46 : 'plugin_action_links_wp-debugging/wp-debugging.php',
98 47 [ $this, 'plugin_action_links' ]
99 48 );
100 -
101 - return $this;
102 49 }
103 50
104 51 /**
105 52 * Add plugin menu.
@@ -107,14 +54,14 @@
107 54 * @return void
108 55 */
109 56 public function add_plugin_menu() {
110 57 $parent = is_multisite() ? 'settings.php' : 'tools.php';
111 - $capability = is_multisite() ? 'manage_network_options' : 'manage_options';
58 + $capability = is_multisite() ? 'manage_network' : 'manage_options';
112 59
113 60 add_submenu_page(
114 61 $parent,
115 62 esc_html__( 'WP Debugging', 'wp-debugging' ),
116 - esc_html_x( 'WP Debugging', 'Menu item', 'wp-debugging' ),
63 + esc_html__( 'WP Debugging', 'wp-debugging' ),
117 64 $capability,
118 65 'wp-debugging',
119 66 [ $this, 'create_settings_page' ]
120 67 );
@@ -125,21 +72,15 @@
125 72 *
126 73 * @return void
127 74 */
128 75 public function update_settings() {
129 - // Exit if improper privileges.
130 - if ( ! current_user_can( 'manage_options' ) || ! wp_verify_nonce( static::$nonce, 'wp-debugging' ) ) {
131 - return;
132 - }
133 -
134 76 if ( isset( $_POST['option_page'] ) &&
135 77 'wp_debugging' === $_POST['option_page']
136 78 ) {
137 79 $options = isset( $_POST['wp-debugging'] )
138 - ? array_map( 'sanitize_text_field', wp_unslash( $_POST['wp-debugging'] ) )
80 + ? $_POST['wp-debugging']
139 81 : [];
140 -
141 - $options = $this->sanitize( $options );
82 + $options = self::sanitize( $options );
142 83 $this->update_constants( self::$options, $options );
143 84 $filtered_options = array_filter(
144 85 self::$options,
145 86 function ( $e ) {
@@ -176,66 +117,20 @@
176 117 *
177 118 * @uses https://github.com/wp-cli/wp-config-transformer
178 119 *
179 120 * @param array $add Constants to add to wp-config.php.
180 - * @return array $added Array of added constants.
181 - */
182 - public function add_constants( $add ) {
183 - $added = [];
184 - try {
185 - $config_transformer = new \WPConfigTransformer( self::$config_path );
186 - foreach ( $add as $constant => $config ) {
187 - $value = 'wp_debug_display' === $constant ? 'false' : 'true';
188 - $value = isset( $config['value'] ) ? $config['value'] : $value;
189 - $raw = isset( $config['raw'] ) ? $config['raw'] : true;
190 - self::$config_args = array_merge( self::$config_args, [ 'raw' => $raw ] );
191 - $config_transformer->update( 'constant', strtoupper( $constant ), $value, self::$config_args );
192 - $added[ $constant ] = $value;
193 - }
194 -
195 - return $added;
196 - } catch ( \Exception $e ) {
197 - $messsage = 'Caught Exception: \Fragen\WP_Debugging\Settings::add_constants() - ' . $e->getMessage();
198 - // error_log( $messsage );
199 - wp_die( esc_html( $messsage ) );
200 - }
201 - }
202 -
203 - /**
204 - * Process user defined constants added via filter.
205 - *
206 121 * @return void
207 122 */
208 - public function process_filter_constants() {
209 - /**
210 - * Filter to add user define constants.
211 - *
212 - * @since 2.5.0
213 - *
214 - * @param array Array of added constants.
215 - * The format for adding constants is to return an array of arrays.
216 - * Each array will have the constant as the key with an array of configuration data.
217 - * array(
218 - * 'my_constant' =>
219 - * array(
220 - * 'value' => $value, @type string
221 - * 'raw' => $raw, // Optional. @type bool $raw is a boolean where false will return the value in quotes and true will return the raw value. Default is true.
222 - * ),
223 - * )
224 - * Default is an empty array.
225 - */
226 - $filter_constants = apply_filters( 'wp_debugging_add_constants', [] );
227 - $remove_user_defined = array_diff( self::$options, array_flip( $this->defined_constants ) );
228 -
229 - // Remove and re-add user defined constants. Clean up for when filter removed or changed.
230 - if ( ! empty( $remove_user_defined ) ) {
231 - $this->remove_constants( $remove_user_defined );
123 + private function add_constants( $add ) {
124 + $config_transformer = new \WPConfigTransformer( ABSPATH . 'wp-config.php' );
125 + $config_args = [
126 + 'raw' => true,
127 + 'normalize' => true,
128 + ];
129 + foreach ( array_keys( $add ) as $constant ) {
130 + $value = 'wp_debug_display' === $constant ? 'false' : 'true';
131 + $config_transformer->update( 'constant', strtoupper( $constant ), $value, $config_args );
232 132 }
233 - $added_constants = $this->add_constants( $filter_constants );
234 -
235 - $options = array_diff( self::$options, $remove_user_defined );
236 - self::$options = array_merge( $options, $added_constants );
237 - update_site_option( 'wp_debugging', (array) self::$options );
238 133 }
239 134
240 135 /**
241 136 * Remove constants from wp-config.php file.
@@ -244,18 +139,12 @@
244 139 *
245 140 * @param array $remove Constants to remove from wp-config.php.
246 141 * @return void
247 142 */
248 - public function remove_constants( $remove ) {
249 - try {
250 - $config_transformer = new \WPConfigTransformer( self::$config_path );
251 - foreach ( array_keys( $remove ) as $constant ) {
252 - $config_transformer->remove( 'constant', strtoupper( $constant ) );
253 - }
254 - } catch ( \Exception $e ) {
255 - $messsage = 'Caught Exception: \Fragen\WP_Debugging\Settings::remove_constants() - ' . $e->getMessage();
256 - // error_log( $messsage );
257 - wp_die( esc_html( $messsage ) );
143 + private function remove_constants( $remove ) {
144 + $config_transformer = new \WPConfigTransformer( ABSPATH . 'wp-config.php' );
145 + foreach ( array_keys( $remove ) as $constant ) {
146 + $config_transformer->remove( 'constant', strtoupper( $constant ) );
258 147 }
259 148 }
260 149
261 150 /**
@@ -264,12 +153,8 @@
264 153 * @return void
265 154 */
266 155 private function redirect_on_save() {
267 156 $update = false;
268 - if ( ! wp_verify_nonce( static::$nonce, 'wp-debugging' ) ) {
269 - return;
270 - }
271 -
272 157 if ( ( isset( $_POST['action'] ) && 'update' === $_POST['action'] ) &&
273 158 ( isset( $_POST['option_page'] ) && 'wp_debugging' === $_POST['option_page'] )
274 159 ) {
275 160 $update = true;
@@ -295,13 +180,10 @@
295 180 *
296 181 * @return void
297 182 */
298 183 private function saved_settings_notice() {
299 - if ( ! wp_verify_nonce( static::$nonce, 'wp-debugging' ) ) {
300 - return;
301 - }
302 - if ( ( isset( $_GET['updated'] ) && '1' === $_GET['updated'] ) ||
303 - ( isset( $_GET['settings-updated'] ) && '1' === $_GET['settings-updated'] )
184 + if ( ( isset( $_GET['updated'] ) && true == $_GET['updated'] ) ||
185 + ( isset( $_GET['settings-updated'] ) && true == $_GET['settings-updated'] )
304 186 ) {
305 187 echo '<div class="updated"><p>';
306 188 esc_html_e( 'Saved.', 'wp-debugging' );
307 189 echo '</p></div>';
@@ -350,20 +232,21 @@
350 232 'title' => esc_html__( 'Set WP_DEBUG_DISPLAY to false, default is true.', 'wp-debugging' ),
351 233 ]
352 234 );
353 235
354 - add_settings_field(
355 - 'wp_disable_fatal_error_handler',
356 - null,
357 - [ $this, 'checkbox_setting' ],
358 - 'wp_debugging',
359 - 'wp_debugging',
360 - [
361 - 'id' => 'wp_disable_fatal_error_handler',
362 - 'title' => esc_html__( 'Set WP_DISABLE_FATAL_ERROR_HANDLER to true.', 'wp-debugging' ),
363 - 'class' => version_compare( get_bloginfo( 'version' ), '5.2', '>=' ) ? '' : 'hidden',
364 - ]
365 - );
236 + if ( version_compare( get_bloginfo( 'version' ), '5.2-beta', '>=' ) ) {
237 + add_settings_field(
238 + 'wp_disable_fatal_error_handler',
239 + null,
240 + [ $this, 'checkbox_setting' ],
241 + 'wp_debugging',
242 + 'wp_debugging',
243 + [
244 + 'id' => 'wp_disable_fatal_error_handler',
245 + 'title' => esc_html__( 'Set WP_DISABLE_FATAL_ERROR_HANDLER to true.', 'wp-debugging' ),
246 + ]
247 + );
248 + }
366 249 }
367 250
368 251 /**
369 252 * Print settings section information.
@@ -381,32 +264,16 @@
381 264 *
382 265 * @return void
383 266 */
384 267 private function print_constants() {
385 - $added_constants = apply_filters( 'wp_debugging_add_constants', [] );
386 - $additional_constants = [];
387 - if ( $added_constants ) {
388 - foreach ( $added_constants as $constant => $config ) {
389 - $additional_constants[ $constant ] = $config['value'];
390 - }
391 - }
392 -
393 - // Strip user defined constants from $constants array.
394 268 $constants = [ 'wp_debug_log', 'script_debug', 'savequeries' ];
395 269 $constants = array_merge( array_keys( self::$options ), $constants );
396 - $constants = array_diff( $constants, array_keys( $additional_constants ) );
397 -
398 270 echo '<pre>';
399 271 foreach ( $constants as $constant ) {
400 272 $value = 'wp_debug_display' === $constant ? 'false' : 'true';
401 273 $constant = strtoupper( $constant );
402 - echo wp_kses_post( "<code>define( '{$constant}', {$value} );</code><br>" );
274 + echo( wp_kses_post( "<code>define( '{$constant}', {$value} );</code><br>" ) );
403 275 }
404 - foreach ( $additional_constants as $constant => $value ) {
405 - $value = in_array( $value, [ 'true', 'false' ], true ) ? $value : "'$value'";
406 - $constant = strtoupper( $constant );
407 - echo wp_kses_post( "<code>define( '{$constant}', {$value} );</code><br>" );
408 - }
409 276 echo '</pre>';
410 277 }
411 278
412 279 /**
@@ -422,9 +289,9 @@
422 289 <div class="updated fade">
423 290 <p><?php echo wp_kses_post( __( '<strong>Please note:</strong> Your <code>wp-config.php</code> file must be writable by the filesystem. Any errors will result in a PHP Exception being thrown. Debug constants per <a href="https://codex.wordpress.org/Debugging_in_WordPress">Debugging in WordPress</a>.', 'wp-debugging' ) ); ?></p>
424 291 </div>
425 292 <div>
426 - <form method="post" action="<?php echo esc_attr( $action ); ?>">
293 + <form method="post" action="<?php esc_attr_e( $action ); ?>">
427 294 <?php settings_fields( 'wp_debugging' ); ?>
428 295 <?php do_settings_sections( 'wp_debugging' ); ?>
429 296 <?php submit_button(); ?>
430 297 </form>
@@ -453,17 +320,17 @@
453 320
454 321 /**
455 322 * Get the settings option array and print one of its values.
456 323 *
457 - * @param array $args 'id' and 'title'.
324 + * @param array $args 'id' and 'title'
458 325 */
459 326 public function checkbox_setting( $args ) {
460 327 $checked = isset( self::$options[ $args['id'] ] ) ? self::$options[ $args['id'] ] : null;
461 328 ?>
462 329 <style> .form-table th { display:none; } </style>
463 - <label for="<?php echo esc_attr( $args['id'] ); ?>">
464 - <input type="checkbox" name="wp-debugging[<?php echo esc_attr( $args['id'] ); ?>]" value="1" <?php checked( '1', $checked ); ?> >
465 - <?php esc_html_e( $args['title'] ); ?>
330 + <label for="<?php esc_attr_e( $args['id'] ); ?>">
331 + <input type="checkbox" name="wp-debugging[<?php esc_attr_e( $args['id'] ); ?>]" value="1" <?php checked( '1', $checked ); ?> >
332 + <?php echo $args['title']; ?>
466 333 </label>
467 334 <?php
468 335 }
469 336
@@ -478,7 +345,7 @@
478 345 public function plugin_action_links( $links ) {
479 346 $settings_page = is_multisite() ? 'settings.php' : 'tools.php';
480 347 $link = [ '<a href="' . esc_url( network_admin_url( $settings_page ) ) . '?page=wp-debugging">' . esc_html__( 'Settings', 'wp-debugging' ) . '</a>' ];
481 348
482 - return array_merge( $link, $links );
349 + return array_merge( $links, $link );
483 350 }
484 351 }