PluginProbe
WP Debugging / 2.2.0
WP Debugging v2.2.0
2.12.6 2.12.5 trunk 2.10.0 2.10.1 2.10.2 2.11.0 2.11.1 2.11.10 2.11.11 2.11.12 2.11.13 2.11.14 2.11.15 2.11.16 2.11.17 2.11.18 2.11.19 2.11.2 2.11.20 2.11.21 2.11.22 2.11.23 2.11.24 2.11.3 All 59 releases
← All changes | src/Bootstrap.php +32 -133 trunk2.2.0 View file →
@@ -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 {
@@ -20,9 +15,9 @@
20 15 class Bootstrap {
21 16 /**
22 17 * Holds main plugin file.
23 18 *
24 - * @var string
19 + * @var $file
25 20 */
26 21 protected $file;
27 22
28 23 /**
@@ -27,9 +22,9 @@
27 22
28 23 /**
29 24 * Holds main plugin directory.
30 25 *
31 - * @var string
26 + * @var $dir
32 27 */
33 28 protected $dir;
34 29
35 30 /**
@@ -34,27 +29,13 @@
34 29
35 30 /**
36 31 * Holds plugin options.
37 32 *
38 - * @var array
33 + * @var $options
39 34 */
40 35 protected static $options;
41 36
42 37 /**
43 - * Holds `wp-config.php` file path.
44 - *
45 - * @var string
46 - */
47 - protected static $config_path;
48 -
49 - /**
50 - * Holds pre-defined constants for `wp-config.php`.
51 - *
52 - * @var array
53 - */
54 - protected $defined_constants = [ 'wp_debug_log', 'script_debug', 'savequeries', 'wp_debug', 'wp_debug_display', 'wp_disable_fatal_error_handler' ];
55 -
56 - /**
57 38 * Constructor.
58 39 *
59 40 * @param string $file Main plugin file.
60 41 * @return void
@@ -59,73 +40,27 @@
59 40 * @param string $file Main plugin file.
60 41 * @return void
61 42 */
62 43 public function __construct( $file ) {
63 - $this->file = $file;
64 - $this->dir = dirname( $file );
65 - self::$options = get_site_option( 'wp_debugging', [ 'wp_debug' => '1' ] );
66 - 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
44 + $this->file = $file;
45 + $this->dir = dirname( $file );
46 + self::$options = get_site_option( 'wp_debugging', [ 'wp_debug' => '1' ] );
47 + @ini_set( 'display_errors', 1 );
68 48 }
69 49
70 50 /**
71 - * Test for writable wp-config.php, exit with notice if not available.
51 + * Let's get going.
72 52 *
73 - * @return bool|void
53 + * @return void
74 54 */
75 - public function init() {
76 - register_activation_hook( $this->file, [ $this, 'activate' ] );
77 - register_deactivation_hook( $this->file, [ $this, 'deactivate' ] );
78 -
79 - // Load settings hooks and exit if not on WP Debugging settings page.
80 - // phpcs:ignore WordPress.Security.NonceVerification.Recommended
81 - if ( ! isset( $_REQUEST['page'] ) || 'wp-debugging' !== sanitize_key( wp_unslash( $_REQUEST['page'] ) ) ) {
82 - ( new Settings( self::$options, self::$config_path, $this->defined_constants ) )
83 - ->load_hooks();
84 - return;
85 - }
86 -
87 - if ( ! is_writable( self::$config_path ) ) {
88 - echo '<div class="error notice is-dismissible"><p>';
89 - 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' ) );
90 - echo '</p></div>';
91 -
92 - return false;
93 - }
94 -
55 + public function run() {
56 + require_once $this->dir . '/vendor/autoload.php';
95 57 $this->load_hooks();
58 + ( new Settings( self::$options ) )->load_hooks();
59 + \WP_Dependency_Installer::instance()->run( $this->dir );
96 60 }
97 61
98 62 /**
99 - * Get the `wp-config.php` file path.
100 - *
101 - * The config file may reside one level above ABSPATH but is not part of another installation.
102 - *
103 - * @see wp-load.php#L26-L42
104 - *
105 - * @return string $config_path
106 - */
107 - public function get_config_path() {
108 - $config_path = ABSPATH . 'wp-config.php';
109 -
110 - if ( ! file_exists( $config_path ) ) {
111 - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
112 - if ( @file_exists( dirname( ABSPATH ) . '/wp-config.php' ) && ! @file_exists( dirname( ABSPATH ) . '/wp-settings.php' ) ) {
113 - $config_path = dirname( ABSPATH ) . '/wp-config.php';
114 - }
115 - }
116 -
117 - /**
118 - * Filter the config file path.
119 - *
120 - * @since 2.3.0
121 - *
122 - * @param string $config_path
123 - */
124 - return apply_filters( 'wp_debugging_config_path', $config_path );
125 - }
126 -
127 - /**
128 63 * Load hooks.
129 64 *
130 65 * @return void
131 66 */
@@ -130,13 +65,11 @@
130 65 * @return void
131 66 */
132 67 public function load_hooks() {
133 68 add_action(
134 - 'plugins_loaded',
69 + 'init',
135 70 function () {
136 - ( new Settings( self::$options, self::$config_path, $this->defined_constants ) )
137 - ->load_hooks()
138 - ->process_filter_constants();
71 + load_plugin_textdomain( 'wp-debugging' );
139 72 }
140 73 );
141 74 add_filter(
142 75 'wp_dependency_timeout',
@@ -147,14 +80,11 @@
147 80 },
148 81 10,
149 82 2
150 83 );
151 - add_action(
152 - 'plugins_loaded',
153 - function () {
154 - \WP_Dependency_Installer::instance()->run( $this->dir );
155 - }
156 - );
84 +
85 + register_activation_hook( $this->file, [ $this, 'activate' ] );
86 + register_deactivation_hook( $this->file, [ $this, 'deactivate' ] );
157 87 }
158 88
159 89 /**
160 90 * Run on activation.
@@ -162,20 +92,19 @@
162 92 *
163 93 * @return void
164 94 */
165 95 public function activate() {
166 - $this->set_pre_activation_constants();
167 -
168 - // Need to remove user defined constants from filter.
169 - $user_defined = apply_filters( 'wp_debugging_add_constants', [] );
170 - foreach ( array_keys( $user_defined ) as $defined ) {
171 - unset( self::$options[ $defined ] );
96 + $config_transformer = new \WPConfigTransformer( ABSPATH . 'wp-config.php' );
97 + $constants = [ 'wp_debug_log', 'script_debug', 'savequeries' ];
98 + $constants = array_merge( array_keys( self::$options ), $constants );
99 + $config_args = [
100 + 'raw' => true,
101 + 'normalize' => true,
102 + ];
103 + foreach ( $constants as $constant ) {
104 + $value = 'wp_debug_display' === $constant ? 'false' : 'true';
105 + $config_transformer->update( 'constant', strtoupper( $constant ), $value, $config_args );
172 106 }
173 -
174 - $constants = [ 'wp_debug_log', 'script_debug', 'savequeries' ];
175 - $constants = array_flip( array_merge( array_keys( self::$options ), $constants ) );
176 -
177 - ( new Settings( self::$options, self::$config_path, $this->defined_constants ) )->add_constants( $constants );
178 107 }
179 108
180 109 /**
181 110 * Run on deactivation.
@@ -183,41 +112,11 @@
183 112 *
184 113 * @return void
185 114 */
186 115 public function deactivate() {
187 - $restore_constants = get_site_option( 'wp_debugging_restore' );
188 - $remove_user_added = array_diff( self::$options, array_flip( $this->defined_constants ) );
189 - $remove_constants = array_diff( array_flip( $this->defined_constants ), array_keys( (array) $restore_constants ) );
190 - $remove_constants = array_merge( $remove_constants, $remove_user_added );
191 -
192 - ( new Settings( self::$options, self::$config_path, $this->defined_constants ) )->remove_constants( $remove_constants );
193 -
194 - $this->restore_pre_activation_constants();
195 - }
196 -
197 - /**
198 - * Set pre-activation constant from `wp-config.php`.
199 - *
200 - * @return void
201 - */
202 - private function set_pre_activation_constants() {
203 - $config_transformer = new \WPConfigTransformer( self::$config_path );
204 - $predefined_constants = [];
205 - foreach ( $this->defined_constants as $defined_constant ) {
206 - if ( $config_transformer->exists( 'constant', strtoupper( $defined_constant ) ) ) {
207 - $value = $config_transformer->get_value( 'constant', strtoupper( $defined_constant ) );
208 - $predefined_constants[ $defined_constant ]['value'] = $value;
209 - }
116 + $config_transformer = new \WPConfigTransformer( ABSPATH . 'wp-config.php' );
117 + $constants = [ 'wp_debug_log', 'script_debug', 'savequeries', 'wp_debug', 'wp_debug_display', 'wp_disable_fatal_error_handler' ];
118 + foreach ( $constants as $constant ) {
119 + $config_transformer->remove( 'constant', strtoupper( $constant ) );
210 120 }
211 - update_site_option( 'wp_debugging_restore', $predefined_constants );
212 - }
213 -
214 - /**
215 - * Restore pre-activation constants to `wp-config.php`.
216 - *
217 - * @return void
218 - */
219 - private function restore_pre_activation_constants() {
220 - $restore_constants = get_site_option( 'wp_debugging_restore' );
221 - ( new Settings( self::$options, self::$config_path, $this->defined_constants ) )->add_constants( $restore_constants );
222 121 }
223 122 }