defined_constants = $defined_constants; self::$config_args = [ 'normalize' => true ]; if ( false === strpos( file_get_contents( self::$config_path ), "/* That's all, stop editing!" ) ) { if ( 1 === preg_match( '@\$table_prefix(.*;)@', file_get_contents( self::$config_path ), $matches ) ) { self::$config_args = array_merge( self::$config_args, [ 'anchor' => "$matches[0]", 'placement' => 'after', ] ); } } } /** * Load hooks for settings. * * @return self */ public function load_hooks() { add_action( 'admin_init', [ $this, 'add_settings' ] ); add_action( is_multisite() ? 'network_admin_menu' : 'admin_menu', [ $this, 'add_plugin_menu' ] ); add_action( 'network_admin_edit_wp_debugging', [ $this, 'update_settings' ] ); add_action( 'admin_init', [ $this, 'update_settings' ] ); add_filter( is_multisite() ? 'network_admin_plugin_action_links_wp-debugging/wp-debugging.php' : 'plugin_action_links_wp-debugging/wp-debugging.php', [ $this, 'plugin_action_links' ] ); return $this; } /** * Add plugin menu. * * @return void */ public function add_plugin_menu() { $parent = is_multisite() ? 'settings.php' : 'tools.php'; $capability = is_multisite() ? 'manage_network_options' : 'manage_options'; add_submenu_page( $parent, esc_html__( 'WP Debugging', 'wp-debugging' ), esc_html_x( 'WP Debugging', 'Menu item', 'wp-debugging' ), $capability, 'wp-debugging', [ $this, 'create_settings_page' ] ); } /** * Update settings on save. * * @return void */ public function update_settings() { // Exit if improper privileges. if ( ! current_user_can( 'manage_options' ) || ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'wp_debugging-options' ) ) ) { return; } if ( isset( $_POST['option_page'] ) && 'wp_debugging' === $_POST['option_page'] ) { $options = isset( $_POST['wp-debugging'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['wp-debugging'] ) ) : []; $options = $this->sanitize( $options ); $this->update_constants( self::$options, $options ); $filtered_options = array_filter( self::$options, static 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 $updated New value of $options. * @return void */ private function update_constants( $old, $updated ) { $remove = array_diff_assoc( $old, $updated ); $add = array_diff_assoc( $updated, $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 array $added Array of added constants. */ public function add_constants( $add ) { $added = []; try { $config_transformer = new \WPConfigTransformer( self::$config_path ); foreach ( $add as $constant => $config ) { $value = 'wp_debug_display' === $constant ? 'false' : 'true'; $value = isset( $config['value'] ) ? $config['value'] : $value; $raw = isset( $config['raw'] ) ? $config['raw'] : true; self::$config_args = array_merge( self::$config_args, [ 'raw' => $raw ] ); $config_transformer->update( 'constant', strtoupper( $constant ), $value, self::$config_args ); $added[ $constant ] = $value; } return $added; } catch ( \Exception $e ) { $messsage = 'Caught Exception: \Fragen\WP_Debugging\Settings::add_constants() - ' . $e->getMessage(); wp_die( esc_html( $messsage ) ); } } /** * Process user defined constants added via filter. * * @return void */ public function process_filter_constants() { /** * Filter to add user define constants. * * @since 2.5.0 * * @param array Array of added constants. * The format for adding constants is to return an array of arrays. * Each array will have the constant as the key with an array of configuration data. * array( * 'my_constant' => * array( * 'value' => $value, @type string * '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. * ), * ) * Default is an empty array. */ $filter_constants = apply_filters( 'wp_debugging_add_constants', [] ); $remove_user_defined = array_diff( self::$options, array_flip( $this->defined_constants ) ); // Remove and re-add user defined constants. Clean up for when filter removed or changed. if ( ! empty( $remove_user_defined ) ) { $this->remove_constants( $remove_user_defined ); } $added_constants = $this->add_constants( $filter_constants ); $options = array_diff( self::$options, $remove_user_defined ); self::$options = array_merge( $options, $added_constants ); update_site_option( 'wp_debugging', (array) self::$options ); } /** * 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 */ public function remove_constants( $remove ) { try { $config_transformer = new \WPConfigTransformer( self::$config_path ); foreach ( array_keys( $remove ) as $constant ) { $config_transformer->remove( 'constant', strtoupper( $constant ) ); } } catch ( \Exception $e ) { $messsage = 'Caught Exception: \Fragen\WP_Debugging\Settings::remove_constants() - ' . $e->getMessage(); wp_die( esc_html( $messsage ) ); } } /** * Redirect back to settings page on save. * * @return void */ private function redirect_on_save() { $update = false; // phpcs:disable WordPress.Security.NonceVerification.Missing if ( ( isset( $_POST['action'] ) && 'update' === $_POST['action'] ) && ( isset( $_POST['option_page'] ) && 'wp_debugging' === $_POST['option_page'] ) ) { $update = true; } // phpcs:enable $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() { // phpcs:disable WordPress.Security.NonceVerification.Recommended if ( ( isset( $_GET['updated'] ) && '1' === $_GET['updated'] ) || ( isset( $_GET['settings-updated'] ) && '1' === $_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} );
" );
}
foreach ( $additional_constants as $constant => $value ) {
$value = in_array( $value, [ 'true', 'false' ], true ) ? $value : "'$value'";
$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 as documented in Debugging in WordPress.', 'wp-debugging' ) ); ?>