update_constants( self::$options, $options ); $filtered_options = array_filter( self::$options, function ( $e ) { return '1' !== $e; } ); $options = array_merge( $filtered_options, $options ); update_site_option( 'wp_debugging', (array) $options ); $this->redirect_on_save(); } } /** * Update constants in wp-config.php. * * @param array $old Current value of self::$options. * @param mixed $new New value of $options. * @return void */ private function update_constants( $old, $new ) { $remove = array_diff_assoc( $old, $new ); $add = array_diff_assoc( $new, $old ); if ( ! empty( $add ) ) { $this->add_constants( $add ); } if ( ! empty( $remove ) ) { $this->remove_constants( $remove ); } } /** * Add constants to wp-config.php file. * * @uses https://github.com/wp-cli/wp-config-transformer * * @param array $add Constants to add to wp-config.php. * @return void */ private function add_constants( $add ) { $config_transformer = new \WPConfigTransformer( ABSPATH . 'wp-config.php' ); $config_args = [ 'raw' => true, 'normalize' => true, ]; foreach ( array_keys( $add ) as $constant ) { $value = 'wp_debug_display' === $constant ? 'false' : 'true'; $config_transformer->update( 'constant', strtoupper( $constant ), $value, $config_args ); } } /** * Remove constants from wp-config.php file. * * @uses https://github.com/wp-cli/wp-config-transformer * * @param array $remove Constants to remove from wp-config.php. * @return void */ private function remove_constants( $remove ) { $config_transformer = new \WPConfigTransformer( ABSPATH . 'wp-config.php' ); foreach ( array_keys( $remove ) as $constant ) { $config_transformer->remove( 'constant', strtoupper( $constant ) ); } } /** * Redirect back to settings page on save. * * @return void */ private function redirect_on_save() { $update = false; if ( ( isset( $_POST['action'] ) && 'update' === $_POST['action'] ) && ( isset( $_POST['option_page'] ) && 'wp_debugging' === $_POST['option_page'] ) ) { $update = true; } $redirect_url = is_multisite() ? network_admin_url( 'settings.php' ) : admin_url( 'tools.php' ); if ( $update ) { $location = add_query_arg( [ 'page' => 'wp-debugging', 'updated' => $update, ], $redirect_url ); wp_safe_redirect( $location ); exit; } } /** * Add notice when settings are saved. * * @return void */ private function saved_settings_notice() { if ( ( isset( $_GET['updated'] ) && true == $_GET['updated'] ) || ( isset( $_GET['settings-updated'] ) && true == $_GET['settings-updated'] ) ) { echo '

'; esc_html_e( 'Saved.', 'wp-debugging' ); echo '

'; } } /** * Register settings. * * @return void */ public function add_settings() { register_setting( 'wp_debugging', 'wp_debugging', [ $this, 'sanitize' ] ); add_settings_section( 'wp_debugging', esc_html__( 'Debugging Constants', 'wp-debugging' ), [ $this, 'print_settings_section' ], 'wp_debugging' ); add_settings_field( 'wp_debug', null, [ $this, 'checkbox_setting' ], 'wp_debugging', 'wp_debugging', [ 'id' => 'wp_debug', 'title' => esc_html__( 'Set WP_DEBUG to true.', 'wp-debugging' ), ] ); add_settings_field( 'wp_debug_display', null, [ $this, 'checkbox_setting' ], 'wp_debugging', 'wp_debugging', [ 'id' => 'wp_debug_display', 'title' => esc_html__( 'Set WP_DEBUG_DISPLAY to false, default is true.', 'wp-debugging' ), ] ); if ( version_compare( get_bloginfo( 'version' ), '5.2-beta', '>=' ) ) { add_settings_field( 'wp_disable_fatal_error_handler', null, [ $this, 'checkbox_setting' ], 'wp_debugging', 'wp_debugging', [ 'id' => 'wp_disable_fatal_error_handler', 'title' => esc_html__( 'Set WP_DISABLE_FATAL_ERROR_HANDLER to true.', 'wp-debugging' ), ] ); } } /** * Print settings section information. * * @return void */ public function print_settings_section() { esc_html_e( 'The following constants are set with plugin activation and removed with plugin deactivation.', 'wp-debugging' ); $this->print_constants(); esc_html_e( 'Select the debugging constants.', 'wp-debugging' ); } /** * Print current constants. * * @return void */ private function print_constants() { $constants = [ 'wp_debug_log', 'script_debug', 'savequeries' ]; $constants = array_merge( array_keys( self::$options ), $constants ); echo '
';
		foreach ( $constants as $constant ) {
			$value    = 'wp_debug_display' === $constant ? 'false' : 'true';
			$constant = strtoupper( $constant );
			echo( wp_kses_post( "define( '{$constant}', {$value} );
" ) ); } echo '
'; } /** * Create settings page. * * @return void */ public function create_settings_page() { $this->saved_settings_notice(); $action = is_multisite() ? 'edit.php?action=wp-debugging' : 'options.php'; ?>

Please note: Your wp-config.php file must be writable by the filesystem. Any errors will result in a PHP Exception being thrown. Debug constants per Debugging in WordPress.', 'wp-debugging' ) ); ?>

' . esc_html__( 'Settings', 'wp-debugging' ) . '' ]; return array_merge( $links, $link ); } }