| @@ -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,60 +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 | - * Holds nonce. | |
| 51 | - * | |
| 52 | - * @var $nonce | |
| 53 | - */ | |
| 54 | - protected static $nonce; | |
| 55 | - | |
| 56 | - /** | |
| 57 | 31 | * Constructor. |
| 58 | 32 | * |
| 59 | - * @param array $options Plugin options. | |
| 60 | - * @param string $config_path Path to config file. | |
| 61 | - * @param array $defined_constants Pre-defined constant group. | |
| 33 | + * @param array $options Plugin options. | |
| 34 | + * @param string $config_path Path to config file. | |
| 62 | 35 | * @return void |
| 63 | 36 | */ |
| 64 | - public function __construct( $options, $config_path, $defined_constants ) { | |
| 65 | - self::$options = $options; | |
| 66 | - self::$config_path = $config_path; | |
| 67 | - $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 | - } | |
| 37 | + public function __construct( $options, $config_path ) { | |
| 38 | + self::$options = $options; | |
| 39 | + self::$config_path = $config_path; | |
| 82 | 40 | } |
| 83 | 41 | |
| 84 | 42 | /** |
| 85 | 43 | * Load hooks for settings. |
| 86 | 44 | * |
| 87 | - * @return self | |
| 45 | + * @return void | |
| 88 | 46 | */ |
| 89 | 47 | public function load_hooks() { |
| 90 | 48 | add_action( 'admin_init', [ $this, 'add_settings' ] ); |
| 91 | 49 | add_action( is_multisite() ? 'network_admin_menu' : 'admin_menu', [ $this, 'add_plugin_menu' ] ); |
| @@ -96,10 +54,8 @@ | ||
| 96 | 54 | ? 'network_admin_plugin_action_links_wp-debugging/wp-debugging.php' |
| 97 | 55 | : 'plugin_action_links_wp-debugging/wp-debugging.php', |
| 98 | 56 | [ $this, 'plugin_action_links' ] |
| 99 | 57 | ); |
| 100 | - | |
| 101 | - return $this; | |
| 102 | 58 | } |
| 103 | 59 | |
| 104 | 60 | /** |
| 105 | 61 | * Add plugin menu. |
| @@ -107,14 +63,14 @@ | ||
| 107 | 63 | * @return void |
| 108 | 64 | */ |
| 109 | 65 | public function add_plugin_menu() { |
| 110 | 66 | $parent = is_multisite() ? 'settings.php' : 'tools.php'; |
| 111 | - $capability = is_multisite() ? 'manage_network_options' : 'manage_options'; | |
| 67 | + $capability = is_multisite() ? 'manage_network' : 'manage_options'; | |
| 112 | 68 | |
| 113 | 69 | add_submenu_page( |
| 114 | 70 | $parent, |
| 115 | 71 | esc_html__( 'WP Debugging', 'wp-debugging' ), |
| 116 | - esc_html_x( 'WP Debugging', 'Menu item', 'wp-debugging' ), | |
| 72 | + esc_html__( 'WP Debugging', 'wp-debugging' ), | |
| 117 | 73 | $capability, |
| 118 | 74 | 'wp-debugging', |
| 119 | 75 | [ $this, 'create_settings_page' ] |
| 120 | 76 | ); |
| @@ -125,21 +81,15 @@ | ||
| 125 | 81 | * |
| 126 | 82 | * @return void |
| 127 | 83 | */ |
| 128 | 84 | 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 | 85 | if ( isset( $_POST['option_page'] ) && |
| 135 | 86 | 'wp_debugging' === $_POST['option_page'] |
| 136 | 87 | ) { |
| 137 | 88 | $options = isset( $_POST['wp-debugging'] ) |
| 138 | - ? array_map( 'sanitize_text_field', wp_unslash( $_POST['wp-debugging'] ) ) | |
| 89 | + ? $_POST['wp-debugging'] | |
| 139 | 90 | : []; |
| 140 | - | |
| 141 | - $options = $this->sanitize( $options ); | |
| 91 | + $options = self::sanitize( $options ); | |
| 142 | 92 | $this->update_constants( self::$options, $options ); |
| 143 | 93 | $filtered_options = array_filter( |
| 144 | 94 | self::$options, |
| 145 | 95 | function ( $e ) { |
| @@ -176,66 +126,20 @@ | ||
| 176 | 126 | * |
| 177 | 127 | * @uses https://github.com/wp-cli/wp-config-transformer |
| 178 | 128 | * |
| 179 | 129 | * @param array $add Constants to add to wp-config.php. |
| 180 | - * @return array $added Array of added constants. | |
| 181 | - */ | |
| 182 | - 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 | - } | |
| 194 | - | |
| 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 | - } | |
| 201 | - } | |
| 202 | - | |
| 203 | - /** | |
| 204 | - * Process user defined constants added via filter. | |
| 205 | - * | |
| 206 | 130 | * @return void |
| 207 | 131 | */ |
| 208 | - public function process_filter_constants() { | |
| 209 | - /** | |
| 210 | - * Filter to add user define constants. | |
| 211 | - * | |
| 212 | - * @since 2.5.0 | |
| 213 | - * | |
| 214 | - * @param array Array of added constants. | |
| 215 | - * The format for adding constants is to return an array of arrays. | |
| 216 | - * Each array will have the constant as the key with an array of configuration data. | |
| 217 | - * array( | |
| 218 | - * 'my_constant' => | |
| 219 | - * array( | |
| 220 | - * 'value' => $value, @type string | |
| 221 | - * '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. | |
| 222 | - * ), | |
| 223 | - * ) | |
| 224 | - * Default is an empty array. | |
| 225 | - */ | |
| 226 | - $filter_constants = apply_filters( 'wp_debugging_add_constants', [] ); | |
| 227 | - $remove_user_defined = array_diff( self::$options, array_flip( $this->defined_constants ) ); | |
| 228 | - | |
| 229 | - // Remove and re-add user defined constants. Clean up for when filter removed or changed. | |
| 230 | - if ( ! empty( $remove_user_defined ) ) { | |
| 231 | - $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 ); | |
| 232 | 141 | } |
| 233 | - $added_constants = $this->add_constants( $filter_constants ); | |
| 234 | - | |
| 235 | - $options = array_diff( self::$options, $remove_user_defined ); | |
| 236 | - self::$options = array_merge( $options, $added_constants ); | |
| 237 | - update_site_option( 'wp_debugging', (array) self::$options ); | |
| 238 | 142 | } |
| 239 | 143 | |
| 240 | 144 | /** |
| 241 | 145 | * Remove constants from wp-config.php file. |
| @@ -244,18 +148,12 @@ | ||
| 244 | 148 | * |
| 245 | 149 | * @param array $remove Constants to remove from wp-config.php. |
| 246 | 150 | * @return void |
| 247 | 151 | */ |
| 248 | - 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 ) ); | |
| 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 ) ); | |
| 258 | 156 | } |
| 259 | 157 | } |
| 260 | 158 | |
| 261 | 159 | /** |
| @@ -264,12 +162,8 @@ | ||
| 264 | 162 | * @return void |
| 265 | 163 | */ |
| 266 | 164 | private function redirect_on_save() { |
| 267 | 165 | $update = false; |
| 268 | - if ( ! wp_verify_nonce( static::$nonce, 'wp-debugging' ) ) { | |
| 269 | - return; | |
| 270 | - } | |
| 271 | - | |
| 272 | 166 | if ( ( isset( $_POST['action'] ) && 'update' === $_POST['action'] ) && |
| 273 | 167 | ( isset( $_POST['option_page'] ) && 'wp_debugging' === $_POST['option_page'] ) |
| 274 | 168 | ) { |
| 275 | 169 | $update = true; |
| @@ -295,13 +189,10 @@ | ||
| 295 | 189 | * |
| 296 | 190 | * @return void |
| 297 | 191 | */ |
| 298 | 192 | 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'] ) | |
| 193 | + if ( ( isset( $_GET['updated'] ) && true == $_GET['updated'] ) || | |
| 194 | + ( isset( $_GET['settings-updated'] ) && true == $_GET['settings-updated'] ) | |
| 304 | 195 | ) { |
| 305 | 196 | echo '<div class="updated"><p>'; |
| 306 | 197 | esc_html_e( 'Saved.', 'wp-debugging' ); |
| 307 | 198 | echo '</p></div>'; |
| @@ -350,20 +241,21 @@ | ||
| 350 | 241 | 'title' => esc_html__( 'Set WP_DEBUG_DISPLAY to false, default is true.', 'wp-debugging' ), |
| 351 | 242 | ] |
| 352 | 243 | ); |
| 353 | 244 | |
| 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 | - ); | |
| 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 | + } | |
| 366 | 258 | } |
| 367 | 259 | |
| 368 | 260 | /** |
| 369 | 261 | * Print settings section information. |
| @@ -381,32 +273,16 @@ | ||
| 381 | 273 | * |
| 382 | 274 | * @return void |
| 383 | 275 | */ |
| 384 | 276 | private function print_constants() { |
| 385 | - $added_constants = apply_filters( 'wp_debugging_add_constants', [] ); | |
| 386 | - $additional_constants = []; | |
| 387 | - if ( $added_constants ) { | |
| 388 | - foreach ( $added_constants as $constant => $config ) { | |
| 389 | - $additional_constants[ $constant ] = $config['value']; | |
| 390 | - } | |
| 391 | - } | |
| 392 | - | |
| 393 | - // Strip user defined constants from $constants array. | |
| 394 | 277 | $constants = [ 'wp_debug_log', 'script_debug', 'savequeries' ]; |
| 395 | 278 | $constants = array_merge( array_keys( self::$options ), $constants ); |
| 396 | - $constants = array_diff( $constants, array_keys( $additional_constants ) ); | |
| 397 | - | |
| 398 | 279 | echo '<pre>'; |
| 399 | 280 | foreach ( $constants as $constant ) { |
| 400 | 281 | $value = 'wp_debug_display' === $constant ? 'false' : 'true'; |
| 401 | 282 | $constant = strtoupper( $constant ); |
| 402 | - echo wp_kses_post( "<code>define( '{$constant}', {$value} );</code><br>" ); | |
| 283 | + echo( wp_kses_post( "<code>define( '{$constant}', {$value} );</code><br>" ) ); | |
| 403 | 284 | } |
| 404 | - foreach ( $additional_constants as $constant => $value ) { | |
| 405 | - $value = in_array( $value, [ 'true', 'false' ], true ) ? $value : "'$value'"; | |
| 406 | - $constant = strtoupper( $constant ); | |
| 407 | - echo wp_kses_post( "<code>define( '{$constant}', {$value} );</code><br>" ); | |
| 408 | - } | |
| 409 | 285 | echo '</pre>'; |
| 410 | 286 | } |
| 411 | 287 | |
| 412 | 288 | /** |
| @@ -422,9 +298,9 @@ | ||
| 422 | 298 | <div class="updated fade"> |
| 423 | 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> |
| 424 | 300 | </div> |
| 425 | 301 | <div> |
| 426 | - <form method="post" action="<?php echo esc_attr( $action ); ?>"> | |
| 302 | + <form method="post" action="<?php esc_attr_e( $action ); ?>"> | |
| 427 | 303 | <?php settings_fields( 'wp_debugging' ); ?> |
| 428 | 304 | <?php do_settings_sections( 'wp_debugging' ); ?> |
| 429 | 305 | <?php submit_button(); ?> |
| 430 | 306 | </form> |
| @@ -453,16 +329,16 @@ | ||
| 453 | 329 | |
| 454 | 330 | /** |
| 455 | 331 | * Get the settings option array and print one of its values. |
| 456 | 332 | * |
| 457 | - * @param array $args 'id' and 'title'. | |
| 333 | + * @param array $args 'id' and 'title' | |
| 458 | 334 | */ |
| 459 | 335 | public function checkbox_setting( $args ) { |
| 460 | 336 | $checked = isset( self::$options[ $args['id'] ] ) ? self::$options[ $args['id'] ] : null; |
| 461 | 337 | ?> |
| 462 | 338 | <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 ); ?> > | |
| 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 ); ?> > | |
| 465 | 341 | <?php esc_html_e( $args['title'] ); ?> |
| 466 | 342 | </label> |
| 467 | 343 | <?php |
| 468 | 344 | } |
| @@ -478,7 +354,7 @@ | ||
| 478 | 354 | public function plugin_action_links( $links ) { |
| 479 | 355 | $settings_page = is_multisite() ? 'settings.php' : 'tools.php'; |
| 480 | 356 | $link = [ '<a href="' . esc_url( network_admin_url( $settings_page ) ) . '?page=wp-debugging">' . esc_html__( 'Settings', 'wp-debugging' ) . '</a>' ]; |
| 481 | 357 | |
| 482 | - return array_merge( $link, $links ); | |
| 358 | + return array_merge( $links, $link ); | |
| 483 | 359 | } |
| 484 | 360 | } |