PluginProbe
WP Debugging / 2.5.7
WP Debugging v2.5.7
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
wp-debugging / src / Bootstrap.php

Bootstrap.php in WP Debugging 2.5.7, at src/Bootstrap.php

217 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WP Debugging
4 *
5 * @package wp-debugging
6 * @author Andy Fragen
7 * @license MIT
8 */
9
10 namespace Fragen\WP_Debugging;
11
12 /**
13 * Class Bootstrap
14 */
15 class Bootstrap {
16 /**
17 * Holds main plugin file.
18 *
19 * @var string
20 */
21 protected $file;
22
23 /**
24 * Holds main plugin directory.
25 *
26 * @var string
27 */
28 protected $dir;
29
30 /**
31 * Holds plugin options.
32 *
33 * @var array
34 */
35 protected static $options;
36
37 /**
38 * Holds `wp-config.php` file path.
39 *
40 * @var string
41 */
42 protected static $config_path;
43
44 /**
45 * Holds pre-defined constants for `wp-config.php`.
46 *
47 * @var array
48 */
49 protected $defined_constants = [ 'wp_debug_log', 'script_debug', 'savequeries', 'wp_debug', 'wp_debug_display', 'wp_disable_fatal_error_handler' ];
50
51 /**
52 * Constructor.
53 *
54 * @param string $file Main plugin file.
55 * @return void
56 */
57 public function __construct( $file ) {
58 $this->file = $file;
59 $this->dir = dirname( $file );
60 self::$options = get_site_option( 'wp_debugging', [ 'wp_debug' => '1' ] );
61 self::$config_path = $this->get_config_path();
62 @ini_set( 'display_errors', 1 );
63 }
64
65 /**
66 * Test for writable wp-config.php, exit with notice if not available.
67 *
68 * @return bool|void
69 */
70 public function init() {
71 if ( ! is_writable( self::$config_path ) ) {
72 echo '<div class="error notice is-dismissible"><p>';
73 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' ) );
74 echo '</p></div>';
75
76 return false;
77 }
78 $this->run();
79 }
80
81 /**
82 * Let's get going.
83 *
84 * @return void
85 */
86 public function run() {
87 require_once $this->dir . '/vendor/autoload.php';
88 $this->load_hooks();
89 ( new Settings( self::$options, self::$config_path, $this->defined_constants ) )->load_hooks();
90 \WP_Dependency_Installer::instance()->run( $this->dir );
91 }
92
93 /**
94 * Get the `wp-config.php` file path.
95 *
96 * The config file may reside one level above ABSPATH but is not part of another installation.
97 *
98 * @see wp-load.php#L26-L42
99 *
100 * @return string $config_path
101 */
102 public function get_config_path() {
103 $config_path = ABSPATH . 'wp-config.php';
104
105 if ( ! file_exists( $config_path ) ) {
106 if ( @file_exists( dirname( ABSPATH ) . '/wp-config.php' ) && ! @file_exists( dirname( ABSPATH ) . '/wp-settings.php' ) ) {
107 $config_path = dirname( ABSPATH ) . '/wp-config.php';
108 }
109 }
110
111 /**
112 * Filter the config file path.
113 *
114 * @since 2.3.0
115 *
116 * @param string $config_path
117 */
118 return apply_filters( 'wp_debugging_config_path', $config_path );
119 }
120
121 /**
122 * Load hooks.
123 *
124 * @return void
125 */
126 public function load_hooks() {
127 add_action(
128 'init',
129 function () {
130 load_plugin_textdomain( 'wp-debugging' );
131 }
132 );
133 add_filter(
134 'wp_dependency_timeout',
135 function ( $timeout, $source ) {
136 $timeout = basename( $this->dir ) !== $source ? $timeout : 45;
137
138 return $timeout;
139 },
140 10,
141 2
142 );
143
144 if ( file_exists( self::$config_path ) ) {
145 register_activation_hook( $this->file, [ $this, 'activate' ] );
146 register_deactivation_hook( $this->file, [ $this, 'deactivate' ] );
147 }
148 }
149
150 /**
151 * Run on activation.
152 * Reloads constants to wp-config.php, including saved options.
153 *
154 * @return void
155 */
156 public function activate() {
157 $this->set_pre_activation_constants();
158
159 // Need to remove user defined constants from filter.
160 $user_defined = apply_filters( 'wp_debugging_add_constants', [] );
161 foreach ( array_keys( $user_defined ) as $defined ) {
162 unset( self::$options[ $defined ] );
163 }
164
165 $constants = [ 'wp_debug_log', 'script_debug', 'savequeries' ];
166 $constants = array_flip( array_merge( array_keys( self::$options ), $constants ) );
167
168 ( new Settings( self::$options, self::$config_path, $this->defined_constants ) )->add_constants( $constants );
169 }
170
171 /**
172 * Run on deactivation.
173 * Removes all added constants from wp-config.php.
174 *
175 * @return void
176 */
177 public function deactivate() {
178 $restore_constants = get_site_option( 'wp_debugging_restore' );
179 $remove_user_added = array_diff( self::$options, array_flip( $this->defined_constants ) );
180 $remove_constants = array_diff( array_flip( $this->defined_constants ), array_keys( $restore_constants ) );
181 $remove_constants = array_merge( $remove_constants, $remove_user_added );
182
183 ( new Settings( self::$options, self::$config_path, $this->defined_constants ) )->remove_constants( $remove_constants );
184
185 $this->restore_pre_activation_constants();
186 }
187
188 /**
189 * Set pre-activation constant from `wp-config.php`.
190 *
191 * @return void
192 */
193 private function set_pre_activation_constants() {
194 $config_transformer = new \WPConfigTransformer( self::$config_path );
195 $predefined_constants = [];
196 foreach ( $this->defined_constants as $defined_constant ) {
197 if ( $config_transformer->exists( 'constant', strtoupper( $defined_constant ) ) ) {
198 $value = $config_transformer->get_value( 'constant', strtoupper( $defined_constant ) );
199 $value = trim( $value, '"\'' ); // Normalize quoted value.
200
201 $predefined_constants[ $defined_constant ]['value'] = $value;
202 }
203 }
204 update_site_option( 'wp_debugging_restore', $predefined_constants );
205 }
206
207 /**
208 * Restore pre-activation constants to `wp-config.php`.
209 *
210 * @return void
211 */
212 private function restore_pre_activation_constants() {
213 $restore_constants = get_site_option( 'wp_debugging_restore' );
214 ( new Settings( self::$options, self::$config_path, $this->defined_constants ) )->add_constants( $restore_constants );
215 }
216 }
217