| @@ -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 Bootstrap |
| 19 | 14 | */ |
| 20 | 15 | class Bootstrap { |
| @@ -63,32 +58,21 @@ | ||
| 63 | 58 | $this->file = $file; |
| 64 | 59 | $this->dir = dirname( $file ); |
| 65 | 60 | self::$options = get_site_option( 'wp_debugging', [ 'wp_debug' => '1' ] ); |
| 66 | 61 | self::$config_path = $this->get_config_path(); |
| 67 | - @ini_set( 'display_errors', 1 ); // phpcs:ignore WordPress.PHP.IniSet.display_errors_Blacklisted,WordPress.PHP.NoSilencedErrors.Discouraged,WordPress.PHP.IniSet.display_errors_Disallowed | |
| 62 | + @ini_set( 'display_errors', 1 ); | |
| 68 | 63 | } |
| 69 | 64 | |
| 70 | 65 | /** |
| 71 | - * Test for writable wp-config.php, exit with notice if not available. | |
| 66 | + * Let's get going. | |
| 72 | 67 | * |
| 73 | - * @return bool|void | |
| 68 | + * @return void | |
| 74 | 69 | */ |
| 75 | - public function init() { | |
| 76 | - if ( ! is_writable( self::$config_path ) ) { | |
| 77 | - echo '<div class="error notice is-dismissible"><p>'; | |
| 78 | - echo wp_kses_post( __( 'The <strong>WP Debugging</strong> plugin must have a <code>wp-config.php</code> file that is writable by the filesystem.', 'wp-debugging' ) ); | |
| 79 | - echo '</p></div>'; | |
| 80 | - | |
| 81 | - return false; | |
| 82 | - } | |
| 83 | - | |
| 70 | + public function run() { | |
| 71 | + require_once $this->dir . '/vendor/autoload.php'; | |
| 84 | 72 | $this->load_hooks(); |
| 85 | - add_action( | |
| 86 | - 'plugins_loaded', | |
| 87 | - function () { | |
| 88 | - \WP_Dependency_Installer::instance()->run( $this->dir ); | |
| 89 | - } | |
| 90 | - ); | |
| 73 | + ( new Settings( self::$options, self::$config_path ) )->load_hooks(); | |
| 74 | + \WP_Dependency_Installer::instance()->run( $this->dir ); | |
| 91 | 75 | } |
| 92 | 76 | |
| 93 | 77 | /** |
| 94 | 78 | * Get the `wp-config.php` file path. |
| @@ -102,9 +86,8 @@ | ||
| 102 | 86 | public function get_config_path() { |
| 103 | 87 | $config_path = ABSPATH . 'wp-config.php'; |
| 104 | 88 | |
| 105 | 89 | if ( ! file_exists( $config_path ) ) { |
| 106 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged | |
| 107 | 90 | if ( @file_exists( dirname( ABSPATH ) . '/wp-config.php' ) && ! @file_exists( dirname( ABSPATH ) . '/wp-settings.php' ) ) { |
| 108 | 91 | $config_path = dirname( ABSPATH ) . '/wp-config.php'; |
| 109 | 92 | } |
| 110 | 93 | } |
| @@ -125,16 +108,8 @@ | ||
| 125 | 108 | * @return void |
| 126 | 109 | */ |
| 127 | 110 | public function load_hooks() { |
| 128 | 111 | add_action( |
| 129 | - 'plugins_loaded', | |
| 130 | - function () { | |
| 131 | - ( new Settings( self::$options, self::$config_path, $this->defined_constants ) ) | |
| 132 | - ->load_hooks() | |
| 133 | - ->process_filter_constants(); | |
| 134 | - } | |
| 135 | - ); | |
| 136 | - add_action( | |
| 137 | 112 | 'init', |
| 138 | 113 | function () { |
| 139 | 114 | load_plugin_textdomain( 'wp-debugging' ); |
| 140 | 115 | } |
| @@ -160,20 +135,20 @@ | ||
| 160 | 135 | * |
| 161 | 136 | * @return void |
| 162 | 137 | */ |
| 163 | 138 | public function activate() { |
| 164 | - $this->set_pre_activation_constants(); | |
| 165 | - | |
| 166 | - // Need to remove user defined constants from filter. | |
| 167 | - $user_defined = apply_filters( 'wp_debugging_add_constants', [] ); | |
| 168 | - foreach ( array_keys( $user_defined ) as $defined ) { | |
| 169 | - unset( self::$options[ $defined ] ); | |
| 139 | + $config_transformer = new \WPConfigTransformer( self::$config_path ); | |
| 140 | + $constants = [ 'wp_debug_log', 'script_debug', 'savequeries' ]; | |
| 141 | + $constants = array_merge( array_keys( self::$options ), $constants ); | |
| 142 | + $config_args = [ | |
| 143 | + 'raw' => true, | |
| 144 | + 'normalize' => true, | |
| 145 | + ]; | |
| 146 | + $this->set_pre_activation_constants( $config_transformer ); | |
| 147 | + foreach ( $constants as $constant ) { | |
| 148 | + $value = 'wp_debug_display' === $constant ? 'false' : 'true'; | |
| 149 | + $config_transformer->update( 'constant', strtoupper( $constant ), $value, $config_args ); | |
| 170 | 150 | } |
| 171 | - | |
| 172 | - $constants = [ 'wp_debug_log', 'script_debug', 'savequeries' ]; | |
| 173 | - $constants = array_flip( array_merge( array_keys( self::$options ), $constants ) ); | |
| 174 | - | |
| 175 | - ( new Settings( self::$options, self::$config_path, $this->defined_constants ) )->add_constants( $constants ); | |
| 176 | 151 | } |
| 177 | 152 | |
| 178 | 153 | /** |
| 179 | 154 | * Run on deactivation. |
| @@ -181,30 +156,33 @@ | ||
| 181 | 156 | * |
| 182 | 157 | * @return void |
| 183 | 158 | */ |
| 184 | 159 | public function deactivate() { |
| 185 | - $restore_constants = get_site_option( 'wp_debugging_restore' ); | |
| 186 | - $remove_user_added = array_diff( self::$options, array_flip( $this->defined_constants ) ); | |
| 187 | - $remove_constants = array_diff( array_flip( $this->defined_constants ), array_keys( $restore_constants ) ); | |
| 188 | - $remove_constants = array_merge( $remove_constants, $remove_user_added ); | |
| 160 | + $restore_constants = get_site_option( 'wp_debugging_restore' ); | |
| 161 | + $remove_constants = array_diff( $this->defined_constants, array_keys( $restore_constants ) ); | |
| 162 | + $config_transformer = new \WPConfigTransformer( self::$config_path ); | |
| 189 | 163 | |
| 190 | - ( new Settings( self::$options, self::$config_path, $this->defined_constants ) )->remove_constants( $remove_constants ); | |
| 191 | - | |
| 192 | - $this->restore_pre_activation_constants(); | |
| 164 | + foreach ( $remove_constants as $constant ) { | |
| 165 | + $config_transformer->remove( 'constant', strtoupper( $constant ) ); | |
| 166 | + } | |
| 167 | + $this->restore_pre_activation_constants( $config_transformer ); | |
| 193 | 168 | } |
| 194 | 169 | |
| 195 | 170 | /** |
| 196 | 171 | * Set pre-activation constant from `wp-config.php`. |
| 197 | 172 | * |
| 173 | + * @param \WPConfigTransformer $config_transformer | |
| 174 | + * | |
| 198 | 175 | * @return void |
| 199 | 176 | */ |
| 200 | - private function set_pre_activation_constants() { | |
| 201 | - $config_transformer = new \WPConfigTransformer( self::$config_path ); | |
| 177 | + private function set_pre_activation_constants( \WPConfigTransformer $config_transformer ) { | |
| 202 | 178 | $predefined_constants = []; |
| 203 | 179 | foreach ( $this->defined_constants as $defined_constant ) { |
| 204 | 180 | if ( $config_transformer->exists( 'constant', strtoupper( $defined_constant ) ) ) { |
| 205 | 181 | $value = $config_transformer->get_value( 'constant', strtoupper( $defined_constant ) ); |
| 206 | - $predefined_constants[ $defined_constant ]['value'] = $value; | |
| 182 | + $value = trim( $value, '"\'' ); // Normalize quoted value. | |
| 183 | + | |
| 184 | + $predefined_constants[ $defined_constant ] = $value; | |
| 207 | 185 | } |
| 208 | 186 | } |
| 209 | 187 | update_site_option( 'wp_debugging_restore', $predefined_constants ); |
| 210 | 188 | } |
| @@ -211,11 +189,19 @@ | ||
| 211 | 189 | |
| 212 | 190 | /** |
| 213 | 191 | * Restore pre-activation constants to `wp-config.php`. |
| 214 | 192 | * |
| 193 | + * @param \WPConfigTransformer $config_transformer | |
| 194 | + * | |
| 215 | 195 | * @return void |
| 216 | 196 | */ |
| 217 | - private function restore_pre_activation_constants() { | |
| 197 | + private function restore_pre_activation_constants( \WPConfigTransformer $config_transformer ) { | |
| 218 | 198 | $restore_constants = get_site_option( 'wp_debugging_restore' ); |
| 219 | - ( new Settings( self::$options, self::$config_path, $this->defined_constants ) )->add_constants( $restore_constants ); | |
| 199 | + $config_args = [ | |
| 200 | + 'raw' => true, | |
| 201 | + 'normalize' => true, | |
| 202 | + ]; | |
| 203 | + foreach ( $restore_constants as $constant => $value ) { | |
| 204 | + $config_transformer->update( 'constant', strtoupper( $constant ), $value, $config_args ); | |
| 205 | + } | |
| 220 | 206 | } |
| 221 | 207 | } |