PluginProbe
WP Debugging / 2.4.3
WP Debugging v2.4.3
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.4.3, at src/Bootstrap.php

208 lines 5.1 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 * Let's get going.
67 *
68 * @return void
69 */
70 public function run() {
71 require_once $this->dir . '/vendor/autoload.php';
72 $this->load_hooks();
73 ( new Settings( self::$options, self::$config_path ) )->load_hooks();
74 \WP_Dependency_Installer::instance()->run( $this->dir );
75 }
76
77 /**
78 * Get the `wp-config.php` file path.
79 *
80 * The config file may reside one level above ABSPATH but is not part of another installation.
81 *
82 * @see wp-load.php#L26-L42
83 *
84 * @return string $config_path
85 */
86 public function get_config_path() {
87 $config_path = ABSPATH . 'wp-config.php';
88
89 if ( ! file_exists( $config_path ) ) {
90 if ( @file_exists( dirname( ABSPATH ) . '/wp-config.php' ) && ! @file_exists( dirname( ABSPATH ) . '/wp-settings.php' ) ) {
91 $config_path = dirname( ABSPATH ) . '/wp-config.php';
92 }
93 }
94
95 /**
96 * Filter the config file path.
97 *
98 * @since 2.3.0
99 *
100 * @param string $config_path
101 */
102 return apply_filters( 'wp_debugging_config_path', $config_path );
103 }
104
105 /**
106 * Load hooks.
107 *
108 * @return void
109 */
110 public function load_hooks() {
111 add_action(
112 'init',
113 function () {
114 load_plugin_textdomain( 'wp-debugging' );
115 }
116 );
117 add_filter(
118 'wp_dependency_timeout',
119 function ( $timeout, $source ) {
120 $timeout = basename( $this->dir ) !== $source ? $timeout : 45;
121
122 return $timeout;
123 },
124 10,
125 2
126 );
127
128 register_activation_hook( $this->file, [ $this, 'activate' ] );
129 register_deactivation_hook( $this->file, [ $this, 'deactivate' ] );
130 }
131
132 /**
133 * Run on activation.
134 * Reloads constants to wp-config.php, including saved options.
135 *
136 * @return void
137 */
138 public function activate() {
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 );
150 }
151 }
152
153 /**
154 * Run on deactivation.
155 * Removes all added constants from wp-config.php.
156 *
157 * @return void
158 */
159 public function deactivate() {
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 );
163
164 foreach ( $remove_constants as $constant ) {
165 $config_transformer->remove( 'constant', strtoupper( $constant ) );
166 }
167 $this->restore_pre_activation_constants( $config_transformer );
168 }
169
170 /**
171 * Set pre-activation constant from `wp-config.php`.
172 *
173 * @param \WPConfigTransformer $config_transformer
174 *
175 * @return void
176 */
177 private function set_pre_activation_constants( \WPConfigTransformer $config_transformer ) {
178 $predefined_constants = [];
179 foreach ( $this->defined_constants as $defined_constant ) {
180 if ( $config_transformer->exists( 'constant', strtoupper( $defined_constant ) ) ) {
181 $value = $config_transformer->get_value( 'constant', strtoupper( $defined_constant ) );
182 $value = trim( $value, '"\'' ); // Normalize quoted value.
183
184 $predefined_constants[ $defined_constant ] = $value;
185 }
186 }
187 update_site_option( 'wp_debugging_restore', $predefined_constants );
188 }
189
190 /**
191 * Restore pre-activation constants to `wp-config.php`.
192 *
193 * @param \WPConfigTransformer $config_transformer
194 *
195 * @return void
196 */
197 private function restore_pre_activation_constants( \WPConfigTransformer $config_transformer ) {
198 $restore_constants = get_site_option( 'wp_debugging_restore' );
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 }
206 }
207 }
208