| @@ -39,15 +39,8 @@ | ||
| 39 | 39 | */ |
| 40 | 40 | protected $defined_constants; |
| 41 | 41 | |
| 42 | 42 | /** |
| 43 | - * Holds config args for WPConfigTransformer. | |
| 44 | - * | |
| 45 | - * @var array | |
| 46 | - */ | |
| 47 | - protected static $config_args; | |
| 48 | - | |
| 49 | - /** | |
| 50 | 43 | * Constructor. |
| 51 | 44 | * |
| 52 | 45 | * @param array $options Plugin options. |
| 53 | 46 | * @param string $config_path Path to config file. |
| @@ -57,21 +50,8 @@ | ||
| 57 | 50 | public function __construct( $options, $config_path, $defined_constants ) { |
| 58 | 51 | self::$options = $options; |
| 59 | 52 | self::$config_path = $config_path; |
| 60 | 53 | $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 | - } | |
| 74 | 54 | } |
| 75 | 55 | |
| 76 | 56 | /** |
| 77 | 57 | * Load hooks for settings. |
| @@ -114,30 +94,27 @@ | ||
| 114 | 94 | |
| 115 | 95 | /** |
| 116 | 96 | * Update settings on save. |
| 117 | 97 | * |
| 98 | + * phpcs:disable WordPress.Security.NonceVerification.Missing | |
| 99 | + * | |
| 118 | 100 | * @return void |
| 119 | 101 | */ |
| 120 | 102 | 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 | 103 | if ( isset( $_POST['option_page'] ) && |
| 129 | 104 | 'wp_debugging' === $_POST['option_page'] |
| 130 | 105 | ) { |
| 131 | 106 | $options = isset( $_POST['wp-debugging'] ) |
| 132 | - ? array_map( 'sanitize_text_field', wp_unslash( $_POST['wp-debugging'] ) ) | |
| 107 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 108 | + ? wp_unslash( $_POST['wp-debugging'] ) | |
| 133 | 109 | : []; |
| 110 | + // phpcs:enable | |
| 134 | 111 | |
| 135 | - $options = $this->sanitize( $options ); | |
| 112 | + $options = self::sanitize( $options ); | |
| 136 | 113 | $this->update_constants( self::$options, $options ); |
| 137 | 114 | $filtered_options = array_filter( |
| 138 | 115 | self::$options, |
| 139 | - static function ( $e ) { | |
| 116 | + function ( $e ) { | |
| 140 | 117 | return '1' !== $e; |
| 141 | 118 | } |
| 142 | 119 | ); |
| 143 | 120 | $options = array_merge( $filtered_options, $options ); |
| @@ -148,15 +125,15 @@ | ||
| 148 | 125 | |
| 149 | 126 | /** |
| 150 | 127 | * Update constants in wp-config.php. |
| 151 | 128 | * |
| 152 | - * @param array $old Current value of self::$options. | |
| 153 | - * @param mixed $updated New value of $options. | |
| 129 | + * @param array $old Current value of self::$options. | |
| 130 | + * @param mixed $new New value of $options. | |
| 154 | 131 | * @return void |
| 155 | 132 | */ |
| 156 | - private function update_constants( $old, $updated ) { | |
| 157 | - $remove = array_diff_assoc( $old, $updated ); | |
| 158 | - $add = array_diff_assoc( $updated, $old ); | |
| 133 | + private function update_constants( $old, $new ) { | |
| 134 | + $remove = array_diff_assoc( $old, $new ); | |
| 135 | + $add = array_diff_assoc( $new, $old ); | |
| 159 | 136 | |
| 160 | 137 | if ( ! empty( $add ) ) { |
| 161 | 138 | $this->add_constants( $add ); |
| 162 | 139 | } |
| @@ -177,13 +154,16 @@ | ||
| 177 | 154 | $added = []; |
| 178 | 155 | try { |
| 179 | 156 | $config_transformer = new \WPConfigTransformer( self::$config_path ); |
| 180 | 157 | 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 ); | |
| 158 | + $value = 'wp_debug_display' === $constant ? 'false' : 'true'; | |
| 159 | + $value = isset( $config['value'] ) ? $config['value'] : $value; | |
| 160 | + $raw = isset( $config['raw'] ) ? $config['raw'] : true; | |
| 161 | + $config_args = [ | |
| 162 | + 'raw' => $raw, | |
| 163 | + 'normalize' => true, | |
| 164 | + ]; | |
| 165 | + $config_transformer->update( 'constant', strtoupper( $constant ), $value, $config_args ); | |
| 186 | 166 | $added[ $constant ] = $value; |
| 187 | 167 | } |
| 188 | 168 | |
| 189 | 169 | return $added; |
| @@ -254,14 +234,14 @@ | ||
| 254 | 234 | |
| 255 | 235 | /** |
| 256 | 236 | * Redirect back to settings page on save. |
| 257 | 237 | * |
| 238 | + * phpcs:disable WordPress.Security.NonceVerification.Missing | |
| 239 | + * | |
| 258 | 240 | * @return void |
| 259 | 241 | */ |
| 260 | 242 | private function redirect_on_save() { |
| 261 | 243 | $update = false; |
| 262 | - | |
| 263 | - // phpcs:disable WordPress.Security.NonceVerification.Missing | |
| 264 | 244 | if ( ( isset( $_POST['action'] ) && 'update' === $_POST['action'] ) && |
| 265 | 245 | ( isset( $_POST['option_page'] ) && 'wp_debugging' === $_POST['option_page'] ) |
| 266 | 246 | ) { |
| 267 | 247 | $update = true; |
| @@ -285,14 +265,16 @@ | ||
| 285 | 265 | |
| 286 | 266 | /** |
| 287 | 267 | * Add notice when settings are saved. |
| 288 | 268 | * |
| 269 | + * phpcs:disable WordPress.PHP.StrictComparisons.LooseComparison | |
| 270 | + * phpcs:disable WordPress.Security.NonceVerification.Recommended | |
| 271 | + * | |
| 289 | 272 | * @return void |
| 290 | 273 | */ |
| 291 | 274 | 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'] ) | |
| 275 | + if ( ( isset( $_GET['updated'] ) && true == $_GET['updated'] ) || | |
| 276 | + ( isset( $_GET['settings-updated'] ) && true == $_GET['settings-updated'] ) | |
| 295 | 277 | ) { |
| 296 | 278 | echo '<div class="updated"><p>'; |
| 297 | 279 | esc_html_e( 'Saved.', 'wp-debugging' ); |
| 298 | 280 | echo '</p></div>'; |
| @@ -342,20 +324,21 @@ | ||
| 342 | 324 | 'title' => esc_html__( 'Set WP_DEBUG_DISPLAY to false, default is true.', 'wp-debugging' ), |
| 343 | 325 | ] |
| 344 | 326 | ); |
| 345 | 327 | |
| 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 | - ); | |
| 328 | + if ( version_compare( get_bloginfo( 'version' ), '5.2-beta', '>=' ) ) { | |
| 329 | + add_settings_field( | |
| 330 | + 'wp_disable_fatal_error_handler', | |
| 331 | + null, | |
| 332 | + [ $this, 'checkbox_setting' ], | |
| 333 | + 'wp_debugging', | |
| 334 | + 'wp_debugging', | |
| 335 | + [ | |
| 336 | + 'id' => 'wp_disable_fatal_error_handler', | |
| 337 | + 'title' => esc_html__( 'Set WP_DISABLE_FATAL_ERROR_HANDLER to true.', 'wp-debugging' ), | |
| 338 | + ] | |
| 339 | + ); | |
| 340 | + } | |
| 358 | 341 | } |
| 359 | 342 | |
| 360 | 343 | /** |
| 361 | 344 | * Print settings section information. |
| @@ -411,12 +394,12 @@ | ||
| 411 | 394 | $action = is_multisite() ? 'edit.php?action=wp-debugging' : 'options.php'; ?> |
| 412 | 395 | <div class="wrap"> |
| 413 | 396 | <h1><?php esc_html_e( 'WP Debugging', 'wp-debugging' ); ?></h1> |
| 414 | 397 | <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> | |
| 398 | + <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 | 399 | </div> |
| 417 | 400 | <div> |
| 418 | - <form method="post" action="<?php echo esc_attr( $action ); ?>"> | |
| 401 | + <form method="post" action="<?php esc_attr_e( $action ); ?>"> | |
| 419 | 402 | <?php settings_fields( 'wp_debugging' ); ?> |
| 420 | 403 | <?php do_settings_sections( 'wp_debugging' ); ?> |
| 421 | 404 | <?php submit_button(); ?> |
| 422 | 405 | </form> |
| @@ -451,10 +434,10 @@ | ||
| 451 | 434 | public function checkbox_setting( $args ) { |
| 452 | 435 | $checked = isset( self::$options[ $args['id'] ] ) ? self::$options[ $args['id'] ] : null; |
| 453 | 436 | ?> |
| 454 | 437 | <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 ); ?> > | |
| 438 | + <label for="<?php esc_attr_e( $args['id'] ); ?>"> | |
| 439 | + <input type="checkbox" name="wp-debugging[<?php esc_attr_e( $args['id'] ); ?>]" value="1" <?php checked( '1', $checked ); ?> > | |
| 457 | 440 | <?php esc_html_e( $args['title'] ); ?> |
| 458 | 441 | </label> |
| 459 | 442 | <?php |
| 460 | 443 | } |