file = $file; $this->dir = dirname( $file ); self::$options = get_site_option( 'wp_debugging', [ 'wp_debug' => '1' ] ); self::$config_path = $this->get_config_path(); @ini_set( 'display_errors', 1 ); } /** * Let's get going. * * @return void */ public function run() { require_once $this->dir . '/vendor/autoload.php'; $this->load_hooks(); ( new Settings( self::$options, self::$config_path ) )->load_hooks(); \WP_Dependency_Installer::instance()->run( $this->dir ); } /** * Get the `wp-config.php` file path. * * The config file may reside one level above ABSPATH but is not part of another installation. * * @see wp-load.php#L26-L42 * * @return string $config_path */ public function get_config_path() { $config_path = ABSPATH . 'wp-config.php'; if ( ! file_exists( $config_path ) ) { if ( @file_exists( dirname( ABSPATH ) . '/wp-config.php' ) && ! @file_exists( dirname( ABSPATH ) . '/wp-settings.php' ) ) { $config_path = dirname( ABSPATH ) . '/wp-config.php'; } } /** * Filter the config file path. * * @since 2.3.0 * * @param string $config_path */ return apply_filters( 'wp_debugging_config_path', $config_path ); } /** * Load hooks. * * @return void */ public function load_hooks() { add_action( 'init', function () { load_plugin_textdomain( 'wp-debugging' ); } ); add_filter( 'wp_dependency_timeout', function ( $timeout, $source ) { $timeout = basename( $this->dir ) !== $source ? $timeout : 45; return $timeout; }, 10, 2 ); register_activation_hook( $this->file, [ $this, 'activate' ] ); register_deactivation_hook( $this->file, [ $this, 'deactivate' ] ); } /** * Run on activation. * Reloads constants to wp-config.php, including saved options. * * @return void */ public function activate() { $config_transformer = new \WPConfigTransformer( self::$config_path ); $constants = [ 'wp_debug_log', 'script_debug', 'savequeries' ]; $constants = array_merge( array_keys( self::$options ), $constants ); $config_args = [ 'raw' => true, 'normalize' => true, ]; $this->set_pre_activation_constants( $config_transformer ); foreach ( $constants as $constant ) { $value = 'wp_debug_display' === $constant ? 'false' : 'true'; $config_transformer->update( 'constant', strtoupper( $constant ), $value, $config_args ); } } /** * Run on deactivation. * Removes all added constants from wp-config.php. * * @return void */ public function deactivate() { $restore_constants = get_site_option( 'wp_debugging_restore' ); $remove_constants = array_diff( $this->defined_constants, array_keys( $restore_constants ) ); $config_transformer = new \WPConfigTransformer( self::$config_path ); foreach ( $remove_constants as $constant ) { $config_transformer->remove( 'constant', strtoupper( $constant ) ); } $this->restore_pre_activation_constants( $config_transformer ); } /** * Set pre-activation constant from `wp-config.php`. * * @param \WPConfigTransformer $config_transformer * * @return void */ private function set_pre_activation_constants( \WPConfigTransformer $config_transformer ) { $predefined_constants = []; foreach ( $this->defined_constants as $defined_constant ) { if ( $config_transformer->exists( 'constant', strtoupper( $defined_constant ) ) ) { $value = $config_transformer->get_value( 'constant', strtoupper( $defined_constant ) ); $value = trim( $value, '"\'' ); // Normalize quoted value. $predefined_constants[ $defined_constant ] = $value; } } update_site_option( 'wp_debugging_restore', $predefined_constants ); } /** * Restore pre-activation constants to `wp-config.php`. * * @param \WPConfigTransformer $config_transformer * * @return void */ private function restore_pre_activation_constants( \WPConfigTransformer $config_transformer ) { $restore_constants = get_site_option( 'wp_debugging_restore' ); $config_args = [ 'raw' => true, 'normalize' => true, ]; foreach ( $restore_constants as $constant => $value ) { $config_transformer->update( 'constant', strtoupper( $constant ), $value, $config_args ); } } }