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

222 lines 5.6 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 // Exit if called directly.
13 if ( ! defined( 'WPINC' ) ) {
14 die;
15 }
16
17 /**
18 * Class Bootstrap
19 */
20 class Bootstrap {
21 /**
22 * Holds main plugin file.
23 *
24 * @var string
25 */
26 protected $file;
27
28 /**
29 * Holds main plugin directory.
30 *
31 * @var string
32 */
33 protected $dir;
34
35 /**
36 * Holds plugin options.
37 *
38 * @var array
39 */
40 protected static $options;
41
42 /**
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 * Constructor.
58 *
59 * @param string $file Main plugin file.
60 * @return void
61 */
62 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
68 }
69
70 /**
71 * Test for writable wp-config.php, exit with notice if not available.
72 *
73 * @return bool|void
74 */
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
84 $this->load_hooks();
85 add_action(
86 'plugins_loaded',
87 function () {
88 \WP_Dependency_Installer::instance()->run( $this->dir );
89 }
90 );
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 // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
107 if ( @file_exists( dirname( ABSPATH ) . '/wp-config.php' ) && ! @file_exists( dirname( ABSPATH ) . '/wp-settings.php' ) ) {
108 $config_path = dirname( ABSPATH ) . '/wp-config.php';
109 }
110 }
111
112 /**
113 * Filter the config file path.
114 *
115 * @since 2.3.0
116 *
117 * @param string $config_path
118 */
119 return apply_filters( 'wp_debugging_config_path', $config_path );
120 }
121
122 /**
123 * Load hooks.
124 *
125 * @return void
126 */
127 public function load_hooks() {
128 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 'init',
138 function () {
139 load_plugin_textdomain( 'wp-debugging' );
140 }
141 );
142 add_filter(
143 'wp_dependency_timeout',
144 function ( $timeout, $source ) {
145 $timeout = basename( $this->dir ) !== $source ? $timeout : 45;
146
147 return $timeout;
148 },
149 10,
150 2
151 );
152
153 register_activation_hook( $this->file, [ $this, 'activate' ] );
154 register_deactivation_hook( $this->file, [ $this, 'deactivate' ] );
155 }
156
157 /**
158 * Run on activation.
159 * Reloads constants to wp-config.php, including saved options.
160 *
161 * @return void
162 */
163 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 ] );
170 }
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 }
177
178 /**
179 * Run on deactivation.
180 * Removes all added constants from wp-config.php.
181 *
182 * @return void
183 */
184 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( (array) $restore_constants ) );
188 $remove_constants = array_merge( $remove_constants, $remove_user_added );
189
190 ( new Settings( self::$options, self::$config_path, $this->defined_constants ) )->remove_constants( $remove_constants );
191
192 $this->restore_pre_activation_constants();
193 }
194
195 /**
196 * Set pre-activation constant from `wp-config.php`.
197 *
198 * @return void
199 */
200 private function set_pre_activation_constants() {
201 $config_transformer = new \WPConfigTransformer( self::$config_path );
202 $predefined_constants = [];
203 foreach ( $this->defined_constants as $defined_constant ) {
204 if ( $config_transformer->exists( 'constant', strtoupper( $defined_constant ) ) ) {
205 $value = $config_transformer->get_value( 'constant', strtoupper( $defined_constant ) );
206 $predefined_constants[ $defined_constant ]['value'] = $value;
207 }
208 }
209 update_site_option( 'wp_debugging_restore', $predefined_constants );
210 }
211
212 /**
213 * Restore pre-activation constants to `wp-config.php`.
214 *
215 * @return void
216 */
217 private function restore_pre_activation_constants() {
218 $restore_constants = get_site_option( 'wp_debugging_restore' );
219 ( new Settings( self::$options, self::$config_path, $this->defined_constants ) )->add_constants( $restore_constants );
220 }
221 }
222