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 '
';
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' ) ); ?>