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

219 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 /**
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 ); // phpcs:ignore WordPress.PHP.IniSet.display_errors_Blacklisted
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 if ( ! \file_exists( self::$config_path ) || ! trim( \file_get_contents( self::$config_path ) ) ) {
79 return;
80 }
81
82 $this->load_hooks();
83 \WP_Dependency_Installer::instance()->run( $this->dir );
84 }
85
86 /**
87 * Get the `wp-config.php` file path.
88 *
89 * The config file may reside one level above ABSPATH but is not part of another installation.
90 *
91 * @see wp-load.php#L26-L42
92 *
93 * @return string $config_path
94 */
95 public function get_config_path() {
96 $config_path = ABSPATH . 'wp-config.php';
97
98 if ( ! file_exists( $config_path ) ) {
99 if ( @file_exists( dirname( ABSPATH ) . '/wp-config.php' ) && ! @file_exists( dirname( ABSPATH ) . '/wp-settings.php' ) ) {
100 $config_path = dirname( ABSPATH ) . '/wp-config.php';
101 }
102 }
103
104 /**
105 * Filter the config file path.
106 *
107 * @since 2.3.0
108 *
109 * @param string $config_path
110 */
111 return apply_filters( 'wp_debugging_config_path', $config_path );
112 }
113
114 /**
115 * Load hooks.
116 *
117 * @return void
118 */
119 public function load_hooks() {
120 add_action(
121 'init',
122 function() {
123 ( new Settings( self::$options, self::$config_path, $this->defined_constants ) )
124 ->load_hooks()
125 ->process_filter_constants();
126 }
127 );
128 add_action(
129 'init',
130 function () {
131 load_plugin_textdomain( 'wp-debugging' );
132 }
133 );
134 add_filter(
135 'wp_dependency_timeout',
136 function ( $timeout, $source ) {
137 $timeout = basename( $this->dir ) !== $source ? $timeout : 45;
138
139 return $timeout;
140 },
141 10,
142 2
143 );
144
145 register_activation_hook( $this->file, [ $this, 'activate' ] );
146 register_deactivation_hook( $this->file, [ $this, 'deactivate' ] );
147 }
148
149 /**
150 * Run on activation.
151 * Reloads constants to wp-config.php, including saved options.
152 *
153 * @return void
154 */
155 public function activate() {
156 $this->set_pre_activation_constants();
157
158 // Need to remove user defined constants from filter.
159 $user_defined = apply_filters( 'wp_debugging_add_constants', [] );
160 foreach ( array_keys( $user_defined ) as $defined ) {
161 unset( self::$options[ $defined ] );
162 }
163
164 $constants = [ 'wp_debug_log', 'script_debug', 'savequeries' ];
165 $constants = array_flip( array_merge( array_keys( self::$options ), $constants ) );
166
167 ( new Settings( self::$options, self::$config_path, $this->defined_constants ) )->add_constants( $constants );
168 }
169
170 /**
171 * Run on deactivation.
172 * Removes all added constants from wp-config.php.
173 *
174 * @return void
175 */
176 public function deactivate() {
177 $restore_constants = get_site_option( 'wp_debugging_restore' );
178 $remove_user_added = array_diff( self::$options, array_flip( $this->defined_constants ) );
179 $remove_constants = array_diff( array_flip( $this->defined_constants ), array_keys( $restore_constants ) );
180 $remove_constants = array_merge( $remove_constants, $remove_user_added );
181
182 ( new Settings( self::$options, self::$config_path, $this->defined_constants ) )->remove_constants( $remove_constants );
183
184 $this->restore_pre_activation_constants();
185 }
186
187 /**
188 * Set pre-activation constant from `wp-config.php`.
189 *
190 * @return void
191 */
192 private function set_pre_activation_constants() {
193 if ( ! \file_exists( self::$config_path ) || ! trim( \file_get_contents( self::$config_path ) ) ) {
194 return;
195 }
196 $config_transformer = new \WPConfigTransformer( self::$config_path );
197 $predefined_constants = [];
198 foreach ( $this->defined_constants as $defined_constant ) {
199 if ( $config_transformer->exists( 'constant', strtoupper( $defined_constant ) ) ) {
200 $value = $config_transformer->get_value( 'constant', strtoupper( $defined_constant ) );
201 $value = trim( $value, '"\'' ); // Normalize quoted value.
202
203 $predefined_constants[ $defined_constant ]['value'] = $value;
204 }
205 }
206 update_site_option( 'wp_debugging_restore', $predefined_constants );
207 }
208
209 /**
210 * Restore pre-activation constants to `wp-config.php`.
211 *
212 * @return void
213 */
214 private function restore_pre_activation_constants() {
215 $restore_constants = get_site_option( 'wp_debugging_restore' );
216 ( new Settings( self::$options, self::$config_path, $this->defined_constants ) )->add_constants( $restore_constants );
217 }
218 }
219