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