| @@ -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,15 +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 | 38 | * Constructor. |
| 51 | 39 | * |
| 52 | 40 | * @param array $options Plugin options. |
| 53 | 41 | * @param string $config_path Path to config file. |
| @@ -57,21 +45,8 @@ | ||
| 57 | 45 | public function __construct( $options, $config_path, $defined_constants ) { |
| 58 | 46 | self::$options = $options; |
| 59 | 47 | self::$config_path = $config_path; |
| 60 | 48 | $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 | 49 | } |
| 75 | 50 | |
| 76 | 51 | /** |
| 77 | 52 | * Load hooks for settings. |
| @@ -114,30 +89,27 @@ | ||
| 114 | 89 | |
| 115 | 90 | /** |
| 116 | 91 | * Update settings on save. |
| 117 | 92 | * |
| 93 | + * phpcs:disable WordPress.Security.NonceVerification.Missing | |
| 94 | + * | |
| 118 | 95 | * @return void |
| 119 | 96 | */ |
| 120 | 97 | 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 | 98 | if ( isset( $_POST['option_page'] ) && |
| 129 | 99 | 'wp_debugging' === $_POST['option_page'] |
| 130 | 100 | ) { |
| 131 | 101 | $options = isset( $_POST['wp-debugging'] ) |
| 132 | - ? array_map( 'sanitize_text_field', wp_unslash( $_POST['wp-debugging'] ) ) | |
| 102 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 103 | + ? wp_unslash( $_POST['wp-debugging'] ) | |
| 133 | 104 | : []; |
| 105 | + // phpcs:enable | |
| 134 | 106 | |
| 135 | - $options = $this->sanitize( $options ); | |
| 107 | + $options = self::sanitize( $options ); | |
| 136 | 108 | $this->update_constants( self::$options, $options ); |
| 137 | 109 | $filtered_options = array_filter( |
| 138 | 110 | self::$options, |
| 139 | - static function ( $e ) { | |
| 111 | + function ( $e ) { | |
| 140 | 112 | return '1' !== $e; |
| 141 | 113 | } |
| 142 | 114 | ); |
| 143 | 115 | $options = array_merge( $filtered_options, $options ); |
| @@ -148,15 +120,15 @@ | ||
| 148 | 120 | |
| 149 | 121 | /** |
| 150 | 122 | * Update constants in wp-config.php. |
| 151 | 123 | * |
| 152 | - * @param array $old Current value of self::$options. | |
| 153 | - * @param mixed $updated New value of $options. | |
| 124 | + * @param array $old Current value of self::$options. | |
| 125 | + * @param mixed $new New value of $options. | |
| 154 | 126 | * @return void |
| 155 | 127 | */ |
| 156 | - private function update_constants( $old, $updated ) { | |
| 157 | - $remove = array_diff_assoc( $old, $updated ); | |
| 158 | - $add = array_diff_assoc( $updated, $old ); | |
| 128 | + private function update_constants( $old, $new ) { | |
| 129 | + $remove = array_diff_assoc( $old, $new ); | |
| 130 | + $add = array_diff_assoc( $new, $old ); | |
| 159 | 131 | |
| 160 | 132 | if ( ! empty( $add ) ) { |
| 161 | 133 | $this->add_constants( $add ); |
| 162 | 134 | } |
| @@ -173,25 +145,26 @@ | ||
| 173 | 145 | * @param array $add Constants to add to wp-config.php. |
| 174 | 146 | * @return array $added Array of added constants. |
| 175 | 147 | */ |
| 176 | 148 | 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 | - } | |
| 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 | + } | |
| 188 | 165 | |
| 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 | - } | |
| 166 | + return $added; | |
| 194 | 167 | } |
| 195 | 168 | |
| 196 | 169 | /** |
| 197 | 170 | * Process user defined constants added via filter. |
| @@ -198,8 +171,11 @@ | ||
| 198 | 171 | * |
| 199 | 172 | * @return void |
| 200 | 173 | */ |
| 201 | 174 | public function process_filter_constants() { |
| 175 | + if ( ! \file_exists( self::$config_path ) || ! trim( \file_get_contents( self::$config_path ) ) ) { | |
| 176 | + return; | |
| 177 | + } | |
| 202 | 178 | /** |
| 203 | 179 | * Filter to add user define constants. |
| 204 | 180 | * |
| 205 | 181 | * @since 2.5.0 |
| @@ -238,28 +214,26 @@ | ||
| 238 | 214 | * @param array $remove Constants to remove from wp-config.php. |
| 239 | 215 | * @return void |
| 240 | 216 | */ |
| 241 | 217 | 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 ) ); | |
| 218 | + if ( ! \file_exists( self::$config_path ) || ! trim( \file_get_contents( self::$config_path ) ) ) { | |
| 219 | + return; | |
| 250 | 220 | } |
| 221 | + $config_transformer = new \WPConfigTransformer( self::$config_path ); | |
| 222 | + foreach ( array_keys( $remove ) as $constant ) { | |
| 223 | + $config_transformer->remove( 'constant', strtoupper( $constant ) ); | |
| 224 | + } | |
| 251 | 225 | } |
| 252 | 226 | |
| 253 | 227 | /** |
| 254 | 228 | * Redirect back to settings page on save. |
| 255 | 229 | * |
| 230 | + * phpcs:disable WordPress.Security.NonceVerification.Missing | |
| 231 | + * | |
| 256 | 232 | * @return void |
| 257 | 233 | */ |
| 258 | 234 | private function redirect_on_save() { |
| 259 | 235 | $update = false; |
| 260 | - | |
| 261 | - // phpcs:disable WordPress.Security.NonceVerification.Missing | |
| 262 | 236 | if ( ( isset( $_POST['action'] ) && 'update' === $_POST['action'] ) && |
| 263 | 237 | ( isset( $_POST['option_page'] ) && 'wp_debugging' === $_POST['option_page'] ) |
| 264 | 238 | ) { |
| 265 | 239 | $update = true; |
| @@ -283,14 +257,16 @@ | ||
| 283 | 257 | |
| 284 | 258 | /** |
| 285 | 259 | * Add notice when settings are saved. |
| 286 | 260 | * |
| 261 | + * phpcs:disable WordPress.PHP.StrictComparisons.LooseComparison | |
| 262 | + * phpcs:disable WordPress.Security.NonceVerification.Recommended | |
| 263 | + * | |
| 287 | 264 | * @return void |
| 288 | 265 | */ |
| 289 | 266 | 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'] ) | |
| 267 | + if ( ( isset( $_GET['updated'] ) && true == $_GET['updated'] ) || | |
| 268 | + ( isset( $_GET['settings-updated'] ) && true == $_GET['settings-updated'] ) | |
| 293 | 269 | ) { |
| 294 | 270 | echo '<div class="updated"><p>'; |
| 295 | 271 | esc_html_e( 'Saved.', 'wp-debugging' ); |
| 296 | 272 | echo '</p></div>'; |
| @@ -340,20 +316,21 @@ | ||
| 340 | 316 | 'title' => esc_html__( 'Set WP_DEBUG_DISPLAY to false, default is true.', 'wp-debugging' ), |
| 341 | 317 | ] |
| 342 | 318 | ); |
| 343 | 319 | |
| 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 | - ); | |
| 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 | + } | |
| 356 | 333 | } |
| 357 | 334 | |
| 358 | 335 | /** |
| 359 | 336 | * Print settings section information. |
| @@ -409,12 +386,12 @@ | ||
| 409 | 386 | $action = is_multisite() ? 'edit.php?action=wp-debugging' : 'options.php'; ?> |
| 410 | 387 | <div class="wrap"> |
| 411 | 388 | <h1><?php esc_html_e( 'WP Debugging', 'wp-debugging' ); ?></h1> |
| 412 | 389 | <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> | |
| 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> | |
| 414 | 391 | </div> |
| 415 | 392 | <div> |
| 416 | - <form method="post" action="<?php echo esc_attr( $action ); ?>"> | |
| 393 | + <form method="post" action="<?php esc_attr_e( $action ); ?>"> | |
| 417 | 394 | <?php settings_fields( 'wp_debugging' ); ?> |
| 418 | 395 | <?php do_settings_sections( 'wp_debugging' ); ?> |
| 419 | 396 | <?php submit_button(); ?> |
| 420 | 397 | </form> |
| @@ -449,11 +426,11 @@ | ||
| 449 | 426 | public function checkbox_setting( $args ) { |
| 450 | 427 | $checked = isset( self::$options[ $args['id'] ] ) ? self::$options[ $args['id'] ] : null; |
| 451 | 428 | ?> |
| 452 | 429 | <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'] ); ?> | |
| 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 ); ?> > | |
| 432 | + <?php esc_html_e( $args['title'] ); ?> | |
| 456 | 433 | </label> |
| 457 | 434 | <?php |
| 458 | 435 | } |
| 459 | 436 | |