| 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 |
$this->run(); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Let's get going. |
| 83 |
* |
| 84 |
* @return void |
| 85 |
*/ |
| 86 |
public function run() { |
| 87 |
$this->load_hooks(); |
| 88 |
( new Settings( self::$options, self::$config_path, $this->defined_constants ) )->load_hooks(); |
| 89 |
\WP_Dependency_Installer::instance()->run( $this->dir ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Get the `wp-config.php` file path. |
| 94 |
* |
| 95 |
* The config file may reside one level above ABSPATH but is not part of another installation. |
| 96 |
* |
| 97 |
* @see wp-load.php#L26-L42 |
| 98 |
* |
| 99 |
* @return string $config_path |
| 100 |
*/ |
| 101 |
public function get_config_path() { |
| 102 |
$config_path = ABSPATH . 'wp-config.php'; |
| 103 |
|
| 104 |
if ( ! file_exists( $config_path ) ) { |
| 105 |
if ( @file_exists( dirname( ABSPATH ) . '/wp-config.php' ) && ! @file_exists( dirname( ABSPATH ) . '/wp-settings.php' ) ) { |
| 106 |
$config_path = dirname( ABSPATH ) . '/wp-config.php'; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Filter the config file path. |
| 112 |
* |
| 113 |
* @since 2.3.0 |
| 114 |
* |
| 115 |
* @param string $config_path |
| 116 |
*/ |
| 117 |
return apply_filters( 'wp_debugging_config_path', $config_path ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Load hooks. |
| 122 |
* |
| 123 |
* @return void |
| 124 |
*/ |
| 125 |
public function load_hooks() { |
| 126 |
add_action( |
| 127 |
'init', |
| 128 |
function () { |
| 129 |
load_plugin_textdomain( 'wp-debugging' ); |
| 130 |
} |
| 131 |
); |
| 132 |
add_filter( |
| 133 |
'wp_dependency_timeout', |
| 134 |
function ( $timeout, $source ) { |
| 135 |
$timeout = basename( $this->dir ) !== $source ? $timeout : 45; |
| 136 |
|
| 137 |
return $timeout; |
| 138 |
}, |
| 139 |
10, |
| 140 |
2 |
| 141 |
); |
| 142 |
|
| 143 |
if ( file_exists( self::$config_path ) ) { |
| 144 |
register_activation_hook( $this->file, [ $this, 'activate' ] ); |
| 145 |
register_deactivation_hook( $this->file, [ $this, 'deactivate' ] ); |
| 146 |
} |
| 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 |
$config_transformer = new \WPConfigTransformer( self::$config_path ); |
| 194 |
$predefined_constants = []; |
| 195 |
foreach ( $this->defined_constants as $defined_constant ) { |
| 196 |
if ( $config_transformer->exists( 'constant', strtoupper( $defined_constant ) ) ) { |
| 197 |
$value = $config_transformer->get_value( 'constant', strtoupper( $defined_constant ) ); |
| 198 |
$value = trim( $value, '"\'' ); // Normalize quoted value. |
| 199 |
|
| 200 |
$predefined_constants[ $defined_constant ]['value'] = $value; |
| 201 |
} |
| 202 |
} |
| 203 |
update_site_option( 'wp_debugging_restore', $predefined_constants ); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Restore pre-activation constants to `wp-config.php`. |
| 208 |
* |
| 209 |
* @return void |
| 210 |
*/ |
| 211 |
private function restore_pre_activation_constants() { |
| 212 |
$restore_constants = get_site_option( 'wp_debugging_restore' ); |
| 213 |
( new Settings( self::$options, self::$config_path, $this->defined_constants ) )->add_constants( $restore_constants ); |
| 214 |
} |
| 215 |
} |
| 216 |
|