| @@ -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,52 +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 | 31 | * Constructor. |
| 51 | 32 | * |
| 52 | - * @param array $options Plugin options. | |
| 53 | - * @param string $config_path Path to config file. | |
| 54 | - * @param array $defined_constants Pre-defined constant group. | |
| 33 | + * @param array $options Plugin options. | |
| 34 | + * @param string $config_path Path to config file. | |
| 55 | 35 | * @return void |
| 56 | 36 | */ |
| 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 | - } | |
| 37 | + public function __construct( $options, $config_path ) { | |
| 38 | + self::$options = $options; | |
| 39 | + self::$config_path = $config_path; | |
| 74 | 40 | } |
| 75 | 41 | |
| 76 | 42 | /** |
| 77 | 43 | * Load hooks for settings. |
| 78 | 44 | * |
| 79 | - * @return self | |
| 45 | + * @return void | |
| 80 | 46 | */ |
| 81 | 47 | public function load_hooks() { |
| 82 | 48 | add_action( 'admin_init', [ $this, 'add_settings' ] ); |
| 83 | 49 | add_action( is_multisite() ? 'network_admin_menu' : 'admin_menu', [ $this, 'add_plugin_menu' ] ); |
| @@ -88,10 +54,8 @@ | ||
| 88 | 54 | ? 'network_admin_plugin_action_links_wp-debugging/wp-debugging.php' |
| 89 | 55 | : 'plugin_action_links_wp-debugging/wp-debugging.php', |
| 90 | 56 | [ $this, 'plugin_action_links' ] |
| 91 | 57 | ); |
| 92 | - | |
| 93 | - return $this; | |
| 94 | 58 | } |
| 95 | 59 | |
| 96 | 60 | /** |
| 97 | 61 | * Add plugin menu. |
| @@ -99,14 +63,14 @@ | ||
| 99 | 63 | * @return void |
| 100 | 64 | */ |
| 101 | 65 | public function add_plugin_menu() { |
| 102 | 66 | $parent = is_multisite() ? 'settings.php' : 'tools.php'; |
| 103 | - $capability = is_multisite() ? 'manage_network_options' : 'manage_options'; | |
| 67 | + $capability = is_multisite() ? 'manage_network' : 'manage_options'; | |
| 104 | 68 | |
| 105 | 69 | add_submenu_page( |
| 106 | 70 | $parent, |
| 107 | 71 | esc_html__( 'WP Debugging', 'wp-debugging' ), |
| 108 | - esc_html_x( 'WP Debugging', 'Menu item', 'wp-debugging' ), | |
| 72 | + esc_html__( 'WP Debugging', 'wp-debugging' ), | |
| 109 | 73 | $capability, |
| 110 | 74 | 'wp-debugging', |
| 111 | 75 | [ $this, 'create_settings_page' ] |
| 112 | 76 | ); |
| @@ -117,23 +81,15 @@ | ||
| 117 | 81 | * |
| 118 | 82 | * @return void |
| 119 | 83 | */ |
| 120 | 84 | 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 | 85 | if ( isset( $_POST['option_page'] ) && |
| 129 | 86 | 'wp_debugging' === $_POST['option_page'] |
| 130 | 87 | ) { |
| 131 | 88 | $options = isset( $_POST['wp-debugging'] ) |
| 132 | - ? array_map( 'sanitize_text_field', wp_unslash( $_POST['wp-debugging'] ) ) | |
| 89 | + ? $_POST['wp-debugging'] | |
| 133 | 90 | : []; |
| 134 | - | |
| 135 | - $options = $this->sanitize( $options ); | |
| 91 | + $options = self::sanitize( $options ); | |
| 136 | 92 | $this->update_constants( self::$options, $options ); |
| 137 | 93 | $filtered_options = array_filter( |
| 138 | 94 | self::$options, |
| 139 | 95 | function ( $e ) { |
| @@ -170,66 +126,20 @@ | ||
| 170 | 126 | * |
| 171 | 127 | * @uses https://github.com/wp-cli/wp-config-transformer |
| 172 | 128 | * |
| 173 | 129 | * @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 | 130 | * @return void |
| 201 | 131 | */ |
| 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 ); | |
| 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 ); | |
| 226 | 141 | } |
| 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 | 142 | } |
| 233 | 143 | |
| 234 | 144 | /** |
| 235 | 145 | * Remove constants from wp-config.php file. |
| @@ -238,18 +148,12 @@ | ||
| 238 | 148 | * |
| 239 | 149 | * @param array $remove Constants to remove from wp-config.php. |
| 240 | 150 | * @return void |
| 241 | 151 | */ |
| 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 ) ); | |
| 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 ) ); | |
| 252 | 156 | } |
| 253 | 157 | } |
| 254 | 158 | |
| 255 | 159 | /** |
| @@ -258,16 +162,13 @@ | ||
| 258 | 162 | * @return void |
| 259 | 163 | */ |
| 260 | 164 | private function redirect_on_save() { |
| 261 | 165 | $update = false; |
| 262 | - | |
| 263 | - // phpcs:disable WordPress.Security.NonceVerification.Missing | |
| 264 | 166 | if ( ( isset( $_POST['action'] ) && 'update' === $_POST['action'] ) && |
| 265 | 167 | ( isset( $_POST['option_page'] ) && 'wp_debugging' === $_POST['option_page'] ) |
| 266 | 168 | ) { |
| 267 | 169 | $update = true; |
| 268 | 170 | } |
| 269 | - // phpcs:enable | |
| 270 | 171 | |
| 271 | 172 | $redirect_url = is_multisite() ? network_admin_url( 'settings.php' ) : admin_url( 'tools.php' ); |
| 272 | 173 | |
| 273 | 174 | if ( $update ) { |
| @@ -288,17 +189,15 @@ | ||
| 288 | 189 | * |
| 289 | 190 | * @return void |
| 290 | 191 | */ |
| 291 | 192 | 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'] ) | |
| 193 | + if ( ( isset( $_GET['updated'] ) && true == $_GET['updated'] ) || | |
| 194 | + ( isset( $_GET['settings-updated'] ) && true == $_GET['settings-updated'] ) | |
| 295 | 195 | ) { |
| 296 | 196 | echo '<div class="updated"><p>'; |
| 297 | 197 | esc_html_e( 'Saved.', 'wp-debugging' ); |
| 298 | 198 | echo '</p></div>'; |
| 299 | 199 | } |
| 300 | - // phpcs:enable | |
| 301 | 200 | } |
| 302 | 201 | |
| 303 | 202 | /** |
| 304 | 203 | * Register settings. |
| @@ -342,20 +241,21 @@ | ||
| 342 | 241 | 'title' => esc_html__( 'Set WP_DEBUG_DISPLAY to false, default is true.', 'wp-debugging' ), |
| 343 | 242 | ] |
| 344 | 243 | ); |
| 345 | 244 | |
| 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 | - ); | |
| 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 | + } | |
| 358 | 258 | } |
| 359 | 259 | |
| 360 | 260 | /** |
| 361 | 261 | * Print settings section information. |
| @@ -373,32 +273,16 @@ | ||
| 373 | 273 | * |
| 374 | 274 | * @return void |
| 375 | 275 | */ |
| 376 | 276 | 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 | 277 | $constants = [ 'wp_debug_log', 'script_debug', 'savequeries' ]; |
| 387 | 278 | $constants = array_merge( array_keys( self::$options ), $constants ); |
| 388 | - $constants = array_diff( $constants, array_keys( $additional_constants ) ); | |
| 389 | - | |
| 390 | 279 | echo '<pre>'; |
| 391 | 280 | foreach ( $constants as $constant ) { |
| 392 | 281 | $value = 'wp_debug_display' === $constant ? 'false' : 'true'; |
| 393 | 282 | $constant = strtoupper( $constant ); |
| 394 | - echo wp_kses_post( "<code>define( '{$constant}', {$value} );</code><br>" ); | |
| 283 | + echo( wp_kses_post( "<code>define( '{$constant}', {$value} );</code><br>" ) ); | |
| 395 | 284 | } |
| 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 | 285 | echo '</pre>'; |
| 402 | 286 | } |
| 403 | 287 | |
| 404 | 288 | /** |
| @@ -414,9 +298,9 @@ | ||
| 414 | 298 | <div class="updated fade"> |
| 415 | 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> |
| 416 | 300 | </div> |
| 417 | 301 | <div> |
| 418 | - <form method="post" action="<?php echo esc_attr( $action ); ?>"> | |
| 302 | + <form method="post" action="<?php esc_attr_e( $action ); ?>"> | |
| 419 | 303 | <?php settings_fields( 'wp_debugging' ); ?> |
| 420 | 304 | <?php do_settings_sections( 'wp_debugging' ); ?> |
| 421 | 305 | <?php submit_button(); ?> |
| 422 | 306 | </form> |
| @@ -445,16 +329,16 @@ | ||
| 445 | 329 | |
| 446 | 330 | /** |
| 447 | 331 | * Get the settings option array and print one of its values. |
| 448 | 332 | * |
| 449 | - * @param array $args 'id' and 'title'. | |
| 333 | + * @param array $args 'id' and 'title' | |
| 450 | 334 | */ |
| 451 | 335 | public function checkbox_setting( $args ) { |
| 452 | 336 | $checked = isset( self::$options[ $args['id'] ] ) ? self::$options[ $args['id'] ] : null; |
| 453 | 337 | ?> |
| 454 | 338 | <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 ); ?> > | |
| 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 ); ?> > | |
| 457 | 341 | <?php esc_html_e( $args['title'] ); ?> |
| 458 | 342 | </label> |
| 459 | 343 | <?php |
| 460 | 344 | } |
| @@ -470,7 +354,7 @@ | ||
| 470 | 354 | public function plugin_action_links( $links ) { |
| 471 | 355 | $settings_page = is_multisite() ? 'settings.php' : 'tools.php'; |
| 472 | 356 | $link = [ '<a href="' . esc_url( network_admin_url( $settings_page ) ) . '?page=wp-debugging">' . esc_html__( 'Settings', 'wp-debugging' ) . '</a>' ]; |
| 473 | 357 | |
| 474 | - return array_merge( $link, $links ); | |
| 358 | + return array_merge( $links, $link ); | |
| 475 | 359 | } |
| 476 | 360 | } |