defined_constants = $defined_constants; } /** * 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. * * phpcs:disable WordPress.Security.NonceVerification.Missing * * @return void */ public function update_settings() { if ( isset( $_POST['option_page'] ) && 'wp_debugging' === $_POST['option_page'] ) { $options = isset( $_POST['wp-debugging'] ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized ? wp_unslash( $_POST['wp-debugging'] ) : []; // phpcs:enable $options = self::sanitize( $options ); $this->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 array $added Array of added constants. */ public function add_constants( $add ) { if ( ! \file_exists( self::$config_path ) || ! trim( \file_get_contents( self::$config_path ) ) ) { return []; } $added = []; $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; $config_args = [ 'raw' => $raw, 'normalize' => true, ]; $config_transformer->update( 'constant', strtoupper( $constant ), $value, $config_args ); $added[ $constant ] = $value; } return $added; } /** * Process user defined constants added via filter. * * @return void */ public function process_filter_constants() { if ( ! \file_exists( self::$config_path ) || ! trim( \file_get_contents( self::$config_path ) ) ) { return; } /** * 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 ) { if ( ! \file_exists( self::$config_path ) || ! trim( \file_get_contents( self::$config_path ) ) ) { return; } $config_transformer = new \WPConfigTransformer( self::$config_path ); foreach ( array_keys( $remove ) as $constant ) { $config_transformer->remove( 'constant', strtoupper( $constant ) ); } } /** * Redirect back to settings page on save. * * phpcs:disable WordPress.Security.NonceVerification.Missing * * @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; } // 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. * * phpcs:disable WordPress.PHP.StrictComparisons.LooseComparison * phpcs:disable WordPress.Security.NonceVerification.Recommended * * @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 '

'; } // phpcs:enable } /** * 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() { $added_constants = apply_filters( 'wp_debugging_add_constants', [] ); $additional_constants = []; if ( $added_constants ) { foreach ( $added_constants as $constant => $config ) { $additional_constants[ $constant ] = $config['value']; } } // Strip user defined constants from $constants array. $constants = [ 'wp_debug_log', 'script_debug', 'savequeries' ]; $constants = array_merge( array_keys( self::$options ), $constants ); $constants = array_diff( $constants, array_keys( $additional_constants ) ); 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 per Debugging in WordPress.', 'wp-debugging' ) ); ?>

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