PluginProbe
WP Debugging / 2.4.3
WP Debugging v2.4.3
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 +52 -166 trunk2.4.3 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 {
@@ -32,52 +27,23 @@
32 27 */
33 28 protected static $config_path;
34 29
35 30 /**
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 31 * Constructor.
51 32 *
52 - * @param array $options Plugin options.
53 - * @param string $config_path Path to config file.
54 - * @param array $defined_constants Pre-defined constant group.
33 + * @param array $options Plugin options.
34 + * @param string $config_path Path to config file.
55 35 * @return void
56 36 */
57 - public function __construct( $options, $config_path, $defined_constants ) {
58 - self::$options = $options;
59 - self::$config_path = $config_path;
60 - $this->defined_constants = $defined_constants;
61 - self::$config_args = [ 'normalize' => true ];
62 -
63 - if ( false === strpos( file_get_contents( self::$config_path ), "/* That's all, stop editing!" ) ) {
64 - if ( 1 === preg_match( '@\$table_prefix(.*;)@', file_get_contents( self::$config_path ), $matches ) ) {
65 - self::$config_args = array_merge(
66 - self::$config_args,
67 - [
68 - 'anchor' => "$matches[0]",
69 - 'placement' => 'after',
70 - ]
71 - );
72 - }
73 - }
37 + public function __construct( $options, $config_path ) {
38 + self::$options = $options;
39 + self::$config_path = $config_path;
74 40 }
75 41
76 42 /**
77 43 * Load hooks for settings.
78 44 *
79 - * @return self
45 + * @return void
80 46 */
81 47 public function load_hooks() {
82 48 add_action( 'admin_init', [ $this, 'add_settings' ] );
83 49 add_action( is_multisite() ? 'network_admin_menu' : 'admin_menu', [ $this, 'add_plugin_menu' ] );
@@ -88,10 +54,8 @@
88 54 ? 'network_admin_plugin_action_links_wp-debugging/wp-debugging.php'
89 55 : 'plugin_action_links_wp-debugging/wp-debugging.php',
90 56 [ $this, 'plugin_action_links' ]
91 57 );
92 -
93 - return $this;
94 58 }
95 59
96 60 /**
97 61 * Add plugin menu.
@@ -99,14 +63,14 @@
99 63 * @return void
100 64 */
101 65 public function add_plugin_menu() {
102 66 $parent = is_multisite() ? 'settings.php' : 'tools.php';
103 - $capability = is_multisite() ? 'manage_network_options' : 'manage_options';
67 + $capability = is_multisite() ? 'manage_network' : 'manage_options';
104 68
105 69 add_submenu_page(
106 70 $parent,
107 71 esc_html__( 'WP Debugging', 'wp-debugging' ),
108 - esc_html_x( 'WP Debugging', 'Menu item', 'wp-debugging' ),
72 + esc_html__( 'WP Debugging', 'wp-debugging' ),
109 73 $capability,
110 74 'wp-debugging',
111 75 [ $this, 'create_settings_page' ]
112 76 );
@@ -117,27 +81,19 @@
117 81 *
118 82 * @return void
119 83 */
120 84 public function update_settings() {
121 - // Exit if improper privileges.
122 - if ( ! current_user_can( 'manage_options' )
123 - || ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'wp_debugging-options' ) )
124 - ) {
125 - return;
126 - }
127 -
128 85 if ( isset( $_POST['option_page'] ) &&
129 86 'wp_debugging' === $_POST['option_page']
130 87 ) {
131 88 $options = isset( $_POST['wp-debugging'] )
132 - ? array_map( 'sanitize_text_field', wp_unslash( $_POST['wp-debugging'] ) )
89 + ? $_POST['wp-debugging']
133 90 : [];
134 -
135 - $options = $this->sanitize( $options );
91 + $options = self::sanitize( $options );
136 92 $this->update_constants( self::$options, $options );
137 93 $filtered_options = array_filter(
138 94 self::$options,
139 - static function ( $e ) {
95 + function ( $e ) {
140 96 return '1' !== $e;
141 97 }
142 98 );
143 99 $options = array_merge( $filtered_options, $options );
@@ -148,15 +104,15 @@
148 104
149 105 /**
150 106 * Update constants in wp-config.php.
151 107 *
152 - * @param array $old Current value of self::$options.
153 - * @param mixed $updated New value of $options.
108 + * @param array $old Current value of self::$options.
109 + * @param mixed $new New value of $options.
154 110 * @return void
155 111 */
156 - private function update_constants( $old, $updated ) {
157 - $remove = array_diff_assoc( $old, $updated );
158 - $add = array_diff_assoc( $updated, $old );
112 + private function update_constants( $old, $new ) {
113 + $remove = array_diff_assoc( $old, $new );
114 + $add = array_diff_assoc( $new, $old );
159 115
160 116 if ( ! empty( $add ) ) {
161 117 $this->add_constants( $add );
162 118 }
@@ -170,65 +126,20 @@
170 126 *
171 127 * @uses https://github.com/wp-cli/wp-config-transformer
172 128 *
173 129 * @param array $add Constants to add to wp-config.php.
174 - * @return array $added Array of added constants.
175 - */
176 - public function add_constants( $add ) {
177 - $added = [];
178 - try {
179 - $config_transformer = new \WPConfigTransformer( self::$config_path );
180 - foreach ( $add as $constant => $config ) {
181 - $value = 'wp_debug_display' === $constant ? 'false' : 'true';
182 - $value = isset( $config['value'] ) ? $config['value'] : $value;
183 - $raw = isset( $config['raw'] ) ? $config['raw'] : true;
184 - self::$config_args = array_merge( self::$config_args, [ 'raw' => $raw ] );
185 - $config_transformer->update( 'constant', strtoupper( $constant ), $value, self::$config_args );
186 - $added[ $constant ] = $value;
187 - }
188 -
189 - return $added;
190 - } catch ( \Exception $e ) {
191 - $messsage = 'Caught Exception: \Fragen\WP_Debugging\Settings::add_constants() - ' . $e->getMessage();
192 - wp_die( esc_html( $messsage ) );
193 - }
194 - }
195 -
196 - /**
197 - * Process user defined constants added via filter.
198 - *
199 130 * @return void
200 131 */
201 - public function process_filter_constants() {
202 - /**
203 - * Filter to add user define constants.
204 - *
205 - * @since 2.5.0
206 - *
207 - * @param array Array of added constants.
208 - * The format for adding constants is to return an array of arrays.
209 - * Each array will have the constant as the key with an array of configuration data.
210 - * array(
211 - * 'my_constant' =>
212 - * array(
213 - * 'value' => $value, @type string
214 - * '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.
215 - * ),
216 - * )
217 - * Default is an empty array.
218 - */
219 - $filter_constants = apply_filters( 'wp_debugging_add_constants', [] );
220 - $remove_user_defined = array_diff( self::$options, array_flip( $this->defined_constants ) );
221 -
222 - // Remove and re-add user defined constants. Clean up for when filter removed or changed.
223 - if ( ! empty( $remove_user_defined ) ) {
224 - $this->remove_constants( $remove_user_defined );
132 + private function add_constants( $add ) {
133 + $config_transformer = new \WPConfigTransformer( self::$config_path );
134 + $config_args = [
135 + 'raw' => true,
136 + 'normalize' => true,
137 + ];
138 + foreach ( array_keys( $add ) as $constant ) {
139 + $value = 'wp_debug_display' === $constant ? 'false' : 'true';
140 + $config_transformer->update( 'constant', strtoupper( $constant ), $value, $config_args );
225 141 }
226 - $added_constants = $this->add_constants( $filter_constants );
227 -
228 - $options = array_diff( self::$options, $remove_user_defined );
229 - self::$options = array_merge( $options, $added_constants );
230 - update_site_option( 'wp_debugging', (array) self::$options );
231 142 }
232 143
233 144 /**
234 145 * Remove constants from wp-config.php file.
@@ -237,17 +148,12 @@
237 148 *
238 149 * @param array $remove Constants to remove from wp-config.php.
239 150 * @return void
240 151 */
241 - public function remove_constants( $remove ) {
242 - try {
243 - $config_transformer = new \WPConfigTransformer( self::$config_path );
244 - foreach ( array_keys( $remove ) as $constant ) {
245 - $config_transformer->remove( 'constant', strtoupper( $constant ) );
246 - }
247 - } catch ( \Exception $e ) {
248 - $messsage = 'Caught Exception: \Fragen\WP_Debugging\Settings::remove_constants() - ' . $e->getMessage();
249 - wp_die( esc_html( $messsage ) );
152 + private function remove_constants( $remove ) {
153 + $config_transformer = new \WPConfigTransformer( self::$config_path );
154 + foreach ( array_keys( $remove ) as $constant ) {
155 + $config_transformer->remove( 'constant', strtoupper( $constant ) );
250 156 }
251 157 }
252 158
253 159 /**
@@ -256,16 +162,13 @@
256 162 * @return void
257 163 */
258 164 private function redirect_on_save() {
259 165 $update = false;
260 -
261 - // phpcs:disable WordPress.Security.NonceVerification.Missing
262 166 if ( ( isset( $_POST['action'] ) && 'update' === $_POST['action'] ) &&
263 167 ( isset( $_POST['option_page'] ) && 'wp_debugging' === $_POST['option_page'] )
264 168 ) {
265 169 $update = true;
266 170 }
267 - // phpcs:enable
268 171
269 172 $redirect_url = is_multisite() ? network_admin_url( 'settings.php' ) : admin_url( 'tools.php' );
270 173
271 174 if ( $update ) {
@@ -286,17 +189,15 @@
286 189 *
287 190 * @return void
288 191 */
289 192 private function saved_settings_notice() {
290 - // phpcs:disable WordPress.Security.NonceVerification.Recommended
291 - if ( ( isset( $_GET['updated'] ) && '1' === $_GET['updated'] ) ||
292 - ( isset( $_GET['settings-updated'] ) && '1' === $_GET['settings-updated'] )
193 + if ( ( isset( $_GET['updated'] ) && true == $_GET['updated'] ) ||
194 + ( isset( $_GET['settings-updated'] ) && true == $_GET['settings-updated'] )
293 195 ) {
294 196 echo '<div class="updated"><p>';
295 197 esc_html_e( 'Saved.', 'wp-debugging' );
296 198 echo '</p></div>';
297 199 }
298 - // phpcs:enable
299 200 }
300 201
301 202 /**
302 203 * Register settings.
@@ -340,20 +241,21 @@
340 241 'title' => esc_html__( 'Set WP_DEBUG_DISPLAY to false, default is true.', 'wp-debugging' ),
341 242 ]
342 243 );
343 244
344 - add_settings_field(
345 - 'wp_disable_fatal_error_handler',
346 - null,
347 - [ $this, 'checkbox_setting' ],
348 - 'wp_debugging',
349 - 'wp_debugging',
350 - [
351 - 'id' => 'wp_disable_fatal_error_handler',
352 - 'title' => esc_html__( 'Set WP_DISABLE_FATAL_ERROR_HANDLER to true.', 'wp-debugging' ),
353 - 'class' => version_compare( get_bloginfo( 'version' ), '5.2', '>=' ) ? '' : 'hidden',
354 - ]
355 - );
245 + if ( version_compare( get_bloginfo( 'version' ), '5.2-beta', '>=' ) ) {
246 + add_settings_field(
247 + 'wp_disable_fatal_error_handler',
248 + null,
249 + [ $this, 'checkbox_setting' ],
250 + 'wp_debugging',
251 + 'wp_debugging',
252 + [
253 + 'id' => 'wp_disable_fatal_error_handler',
254 + 'title' => esc_html__( 'Set WP_DISABLE_FATAL_ERROR_HANDLER to true.', 'wp-debugging' ),
255 + ]
256 + );
257 + }
356 258 }
357 259
358 260 /**
359 261 * Print settings section information.
@@ -371,32 +273,16 @@
371 273 *
372 274 * @return void
373 275 */
374 276 private function print_constants() {
375 - $added_constants = apply_filters( 'wp_debugging_add_constants', [] );
376 - $additional_constants = [];
377 - if ( $added_constants ) {
378 - foreach ( $added_constants as $constant => $config ) {
379 - $additional_constants[ $constant ] = $config['value'];
380 - }
381 - }
382 -
383 - // Strip user defined constants from $constants array.
384 277 $constants = [ 'wp_debug_log', 'script_debug', 'savequeries' ];
385 278 $constants = array_merge( array_keys( self::$options ), $constants );
386 - $constants = array_diff( $constants, array_keys( $additional_constants ) );
387 -
388 279 echo '<pre>';
389 280 foreach ( $constants as $constant ) {
390 281 $value = 'wp_debug_display' === $constant ? 'false' : 'true';
391 282 $constant = strtoupper( $constant );
392 - echo wp_kses_post( "<code>define( '{$constant}', {$value} );</code><br>" );
283 + echo( wp_kses_post( "<code>define( '{$constant}', {$value} );</code><br>" ) );
393 284 }
394 - foreach ( $additional_constants as $constant => $value ) {
395 - $value = in_array( $value, [ 'true', 'false' ], true ) ? $value : "'$value'";
396 - $constant = strtoupper( $constant );
397 - echo wp_kses_post( "<code>define( '{$constant}', {$value} );</code><br>" );
398 - }
399 285 echo '</pre>';
400 286 }
401 287
402 288 /**
@@ -409,12 +295,12 @@
409 295 $action = is_multisite() ? 'edit.php?action=wp-debugging' : 'options.php'; ?>
410 296 <div class="wrap">
411 297 <h1><?php esc_html_e( 'WP Debugging', 'wp-debugging' ); ?></h1>
412 298 <div class="updated fade">
413 - <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 as documented in <a href="https://wordpress.org/documentation/article/debugging-in-wordpress/">Debugging in WordPress</a>.', 'wp-debugging' ) ); ?></p>
299 + <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>
414 300 </div>
415 301 <div>
416 - <form method="post" action="<?php echo esc_attr( $action ); ?>">
302 + <form method="post" action="<?php esc_attr_e( $action ); ?>">
417 303 <?php settings_fields( 'wp_debugging' ); ?>
418 304 <?php do_settings_sections( 'wp_debugging' ); ?>
419 305 <?php submit_button(); ?>
420 306 </form>
@@ -443,17 +329,17 @@
443 329
444 330 /**
445 331 * Get the settings option array and print one of its values.
446 332 *
447 - * @param array $args 'id' and 'title'.
333 + * @param array $args 'id' and 'title'
448 334 */
449 335 public function checkbox_setting( $args ) {
450 336 $checked = isset( self::$options[ $args['id'] ] ) ? self::$options[ $args['id'] ] : null;
451 337 ?>
452 338 <style> .form-table th { display:none; } </style>
453 - <label for="<?php echo esc_attr( $args['id'] ); ?>">
454 - <input type="checkbox" name="wp-debugging[<?php echo esc_attr( $args['id'] ); ?>]" value="1" <?php checked( '1', $checked ); ?> >
455 - <?php echo esc_html( $args['title'] ); ?>
339 + <label for="<?php esc_attr_e( $args['id'] ); ?>">
340 + <input type="checkbox" name="wp-debugging[<?php esc_attr_e( $args['id'] ); ?>]" value="1" <?php checked( '1', $checked ); ?> >
341 + <?php esc_html_e( $args['title'] ); ?>
456 342 </label>
457 343 <?php
458 344 }
459 345
@@ -468,7 +354,7 @@
468 354 public function plugin_action_links( $links ) {
469 355 $settings_page = is_multisite() ? 'settings.php' : 'tools.php';
470 356 $link = [ '<a href="' . esc_url( network_admin_url( $settings_page ) ) . '?page=wp-debugging">' . esc_html__( 'Settings', 'wp-debugging' ) . '</a>' ];
471 357
472 - return array_merge( $link, $links );
358 + return array_merge( $links, $link );
473 359 }
474 360 }