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

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

185 lines 4.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 * 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 $predefined_constants = [];
141 $constants = [ 'wp_debug_log', 'script_debug', 'savequeries' ];
142 $constants = array_merge( array_keys( self::$options ), $constants );
143 $config_args = [
144 'raw' => true,
145 'normalize' => true,
146 ];
147 foreach ( $this->defined_constants as $defined_constant ) {
148 if ( $config_transformer->exists( 'constant', strtoupper( $defined_constant ) ) ) {
149 $value = $config_transformer->get_value( 'constant', strtoupper( $defined_constant ) );
150 $value = trim( $value, '"\'' ); // Normalize quoted value.
151
152 $predefined_constants[ $defined_constant ] = $value;
153 }
154 }
155 update_site_option( 'wp_debugging_restore', $predefined_constants );
156 foreach ( $constants as $constant ) {
157 $value = 'wp_debug_display' === $constant ? 'false' : 'true';
158 $config_transformer->update( 'constant', strtoupper( $constant ), $value, $config_args );
159 }
160 }
161
162 /**
163 * Run on deactivation.
164 * Removes all added constants from wp-config.php.
165 *
166 * @return void
167 */
168 public function deactivate() {
169 $restore_constants = get_site_option( 'wp_debugging_restore' );
170 $remove_constants = array_diff( $this->defined_constants, array_keys( $restore_constants ) );
171 $config_transformer = new \WPConfigTransformer( self::$config_path );
172 $config_args = [
173 'raw' => true,
174 'normalize' => true,
175 ];
176
177 foreach ( $remove_constants as $constant ) {
178 $config_transformer->remove( 'constant', strtoupper( $constant ) );
179 }
180 foreach ( $restore_constants as $constant => $value ) {
181 $config_transformer->update( 'constant', strtoupper( $constant ), $value, $config_args );
182 }
183 }
184 }
185