PluginProbe
WP Debugging / 2.7.2
WP Debugging v2.7.2
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 +57 -90 2.11.02.7.2 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 {
@@ -39,22 +34,8 @@
39 34 */
40 35 protected $defined_constants;
41 36
42 37 /**
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 38 * Constructor.
58 39 *
59 40 * @param array $options Plugin options.
60 41 * @param string $config_path Path to config file.
@@ -64,22 +45,8 @@
64 45 public function __construct( $options, $config_path, $defined_constants ) {
65 46 self::$options = $options;
66 47 self::$config_path = $config_path;
67 48 $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 - }
82 49 }
83 50
84 51 /**
85 52 * Load hooks for settings.
@@ -122,24 +89,23 @@
122 89
123 90 /**
124 91 * Update settings on save.
125 92 *
93 + * phpcs:disable WordPress.Security.NonceVerification.Missing
94 + *
126 95 * @return void
127 96 */
128 97 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 98 if ( isset( $_POST['option_page'] ) &&
135 99 'wp_debugging' === $_POST['option_page']
136 100 ) {
137 101 $options = isset( $_POST['wp-debugging'] )
138 - ? array_map( 'sanitize_text_field', wp_unslash( $_POST['wp-debugging'] ) )
102 + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
103 + ? wp_unslash( $_POST['wp-debugging'] )
139 104 : [];
105 + // phpcs:enable
140 106
141 - $options = $this->sanitize( $options );
107 + $options = self::sanitize( $options );
142 108 $this->update_constants( self::$options, $options );
143 109 $filtered_options = array_filter(
144 110 self::$options,
145 111 function ( $e ) {
@@ -179,26 +145,26 @@
179 145 * @param array $add Constants to add to wp-config.php.
180 146 * @return array $added Array of added constants.
181 147 */
182 148 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 - }
149 + if ( ! \file_exists( self::$config_path ) || ! trim( \file_get_contents( self::$config_path ) ) ) {
150 + return [];
151 + }
152 + $added = [];
153 + $config_transformer = new \WPConfigTransformer( self::$config_path );
154 + foreach ( $add as $constant => $config ) {
155 + $value = 'wp_debug_display' === $constant ? 'false' : 'true';
156 + $value = isset( $config['value'] ) ? $config['value'] : $value;
157 + $raw = isset( $config['raw'] ) ? $config['raw'] : true;
158 + $config_args = [
159 + 'raw' => $raw,
160 + 'normalize' => true,
161 + ];
162 + $config_transformer->update( 'constant', strtoupper( $constant ), $value, $config_args );
163 + $added[ $constant ] = $value;
164 + }
194 165
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 - }
166 + return $added;
201 167 }
202 168
203 169 /**
204 170 * Process user defined constants added via filter.
@@ -205,8 +171,11 @@
205 171 *
206 172 * @return void
207 173 */
208 174 public function process_filter_constants() {
175 + if ( ! \file_exists( self::$config_path ) || ! trim( \file_get_contents( self::$config_path ) ) ) {
176 + return;
177 + }
209 178 /**
210 179 * Filter to add user define constants.
211 180 *
212 181 * @since 2.5.0
@@ -245,36 +214,32 @@
245 214 * @param array $remove Constants to remove from wp-config.php.
246 215 * @return void
247 216 */
248 217 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 ) );
218 + if ( ! \file_exists( self::$config_path ) || ! trim( \file_get_contents( self::$config_path ) ) ) {
219 + return;
258 220 }
221 + $config_transformer = new \WPConfigTransformer( self::$config_path );
222 + foreach ( array_keys( $remove ) as $constant ) {
223 + $config_transformer->remove( 'constant', strtoupper( $constant ) );
224 + }
259 225 }
260 226
261 227 /**
262 228 * Redirect back to settings page on save.
263 229 *
230 + * phpcs:disable WordPress.Security.NonceVerification.Missing
231 + *
264 232 * @return void
265 233 */
266 234 private function redirect_on_save() {
267 235 $update = false;
268 - if ( ! wp_verify_nonce( static::$nonce, 'wp-debugging' ) ) {
269 - return;
270 - }
271 -
272 236 if ( ( isset( $_POST['action'] ) && 'update' === $_POST['action'] ) &&
273 237 ( isset( $_POST['option_page'] ) && 'wp_debugging' === $_POST['option_page'] )
274 238 ) {
275 239 $update = true;
276 240 }
241 + // phpcs:enable
277 242
278 243 $redirect_url = is_multisite() ? network_admin_url( 'settings.php' ) : admin_url( 'tools.php' );
279 244
280 245 if ( $update ) {
@@ -292,21 +257,22 @@
292 257
293 258 /**
294 259 * Add notice when settings are saved.
295 260 *
261 + * phpcs:disable WordPress.PHP.StrictComparisons.LooseComparison
262 + * phpcs:disable WordPress.Security.NonceVerification.Recommended
263 + *
296 264 * @return void
297 265 */
298 266 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'] )
267 + if ( ( isset( $_GET['updated'] ) && true == $_GET['updated'] ) ||
268 + ( isset( $_GET['settings-updated'] ) && true == $_GET['settings-updated'] )
304 269 ) {
305 270 echo '<div class="updated"><p>';
306 271 esc_html_e( 'Saved.', 'wp-debugging' );
307 272 echo '</p></div>';
308 273 }
274 + // phpcs:enable
309 275 }
310 276
311 277 /**
312 278 * Register settings.
@@ -350,20 +316,21 @@
350 316 'title' => esc_html__( 'Set WP_DEBUG_DISPLAY to false, default is true.', 'wp-debugging' ),
351 317 ]
352 318 );
353 319
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 - );
320 + if ( version_compare( get_bloginfo( 'version' ), '5.2-beta', '>=' ) ) {
321 + add_settings_field(
322 + 'wp_disable_fatal_error_handler',
323 + null,
324 + [ $this, 'checkbox_setting' ],
325 + 'wp_debugging',
326 + 'wp_debugging',
327 + [
328 + 'id' => 'wp_disable_fatal_error_handler',
329 + 'title' => esc_html__( 'Set WP_DISABLE_FATAL_ERROR_HANDLER to true.', 'wp-debugging' ),
330 + ]
331 + );
332 + }
366 333 }
367 334
368 335 /**
369 336 * Print settings section information.
@@ -422,9 +389,9 @@
422 389 <div class="updated fade">
423 390 <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 391 </div>
425 392 <div>
426 - <form method="post" action="<?php echo esc_attr( $action ); ?>">
393 + <form method="post" action="<?php esc_attr_e( $action ); ?>">
427 394 <?php settings_fields( 'wp_debugging' ); ?>
428 395 <?php do_settings_sections( 'wp_debugging' ); ?>
429 396 <?php submit_button(); ?>
430 397 </form>
@@ -459,10 +426,10 @@
459 426 public function checkbox_setting( $args ) {
460 427 $checked = isset( self::$options[ $args['id'] ] ) ? self::$options[ $args['id'] ] : null;
461 428 ?>
462 429 <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 ); ?> >
430 + <label for="<?php esc_attr_e( $args['id'] ); ?>">
431 + <input type="checkbox" name="wp-debugging[<?php esc_attr_e( $args['id'] ); ?>]" value="1" <?php checked( '1', $checked ); ?> >
465 432 <?php esc_html_e( $args['title'] ); ?>
466 433 </label>
467 434 <?php
468 435 }