| 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 $file |
| 20 |
*/ |
| 21 |
protected $file; |
| 22 |
|
| 23 |
/** |
| 24 |
* Holds main plugin directory. |
| 25 |
* |
| 26 |
* @var $dir |
| 27 |
*/ |
| 28 |
protected $dir; |
| 29 |
|
| 30 |
/** |
| 31 |
* Holds plugin options. |
| 32 |
* |
| 33 |
* @var $options |
| 34 |
*/ |
| 35 |
protected static $options; |
| 36 |
|
| 37 |
/** |
| 38 |
* Constructor. |
| 39 |
* |
| 40 |
* @param string $file Main plugin file. |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public function __construct( $file ) { |
| 44 |
$this->file = $file; |
| 45 |
$this->dir = dirname( $file ); |
| 46 |
self::$options = get_site_option( 'wp_debugging', [ 'wp_debug' => '1' ] ); |
| 47 |
@ini_set( 'display_errors', 1 ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Let's get going. |
| 52 |
* |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public function run() { |
| 56 |
require_once $this->dir . '/vendor/autoload.php'; |
| 57 |
$this->load_hooks(); |
| 58 |
( new Settings( self::$options ) )->load_hooks(); |
| 59 |
\WP_Dependency_Installer::instance()->run( $this->dir ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Load hooks. |
| 64 |
* |
| 65 |
* @return void |
| 66 |
*/ |
| 67 |
public function load_hooks() { |
| 68 |
add_action( |
| 69 |
'init', |
| 70 |
function () { |
| 71 |
load_plugin_textdomain( 'wp-debugging' ); |
| 72 |
} |
| 73 |
); |
| 74 |
add_filter( |
| 75 |
'wp_dependency_timeout', |
| 76 |
function ( $timeout, $source ) { |
| 77 |
$timeout = basename( $this->dir ) !== $source ? $timeout : 45; |
| 78 |
|
| 79 |
return $timeout; |
| 80 |
}, |
| 81 |
10, |
| 82 |
2 |
| 83 |
); |
| 84 |
|
| 85 |
register_activation_hook( $this->file, [ $this, 'activate' ] ); |
| 86 |
register_deactivation_hook( $this->file, [ $this, 'deactivate' ] ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Run on activation. |
| 91 |
* Reloads constants to wp-config.php, including saved options. |
| 92 |
* |
| 93 |
* @return void |
| 94 |
*/ |
| 95 |
public function activate() { |
| 96 |
$config_transformer = new \WPConfigTransformer( ABSPATH . 'wp-config.php' ); |
| 97 |
$constants = [ 'wp_debug_log', 'script_debug', 'savequeries' ]; |
| 98 |
$constants = array_merge( array_keys( self::$options ), $constants ); |
| 99 |
$config_args = [ |
| 100 |
'raw' => true, |
| 101 |
'normalize' => true, |
| 102 |
]; |
| 103 |
foreach ( $constants as $constant ) { |
| 104 |
$value = 'wp_debug_display' === $constant ? 'false' : 'true'; |
| 105 |
$config_transformer->update( 'constant', strtoupper( $constant ), $value, $config_args ); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Run on deactivation. |
| 111 |
* Removes all added constants from wp-config.php. |
| 112 |
* |
| 113 |
* @return void |
| 114 |
*/ |
| 115 |
public function deactivate() { |
| 116 |
$config_transformer = new \WPConfigTransformer( ABSPATH . 'wp-config.php' ); |
| 117 |
$constants = [ 'wp_debug_log', 'script_debug', 'savequeries', 'wp_debug', 'wp_debug_display', 'wp_disable_fatal_error_handler' ]; |
| 118 |
foreach ( $constants as $constant ) { |
| 119 |
$config_transformer->remove( 'constant', strtoupper( $constant ) ); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|