diff --git a/includes/Checker/Runtime_Environment_Setup.php b/includes/Checker/Runtime_Environment_Setup.php index f01169364..5c9ec7301 100644 --- a/includes/Checker/Runtime_Environment_Setup.php +++ b/includes/Checker/Runtime_Environment_Setup.php @@ -24,10 +24,25 @@ final class Runtime_Environment_Setup { * * @global wpdb $wpdb WordPress database abstraction object. * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. + * + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function set_up() { global $wpdb, $wp_filesystem; + /** + * Fires before the runtime environment is set up. + * + * @since 2.0.0 + * + * @param array $context { + * Context for the hook. + * + * @type bool $early_exit Whether the method exited before completing all setup steps. + * } + */ + do_action( 'wp_plugin_check_before_runtime_setup', array( 'early_exit' => false ) ); + require_once ABSPATH . '/wp-admin/includes/upgrade.php'; // Get the existing site URL. @@ -82,6 +97,18 @@ static function () use ( $permalink_structure ) { // Return early if the plugin check object cache already exists. if ( defined( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION' ) && WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION ) { + /** + * Fires after the runtime environment is set up, including when it exits early. + * + * @since 2.0.0 + * + * @param array $context { + * Context for the hook. + * + * @type bool $early_exit Whether the method exited before completing all setup steps. + * } + */ + do_action( 'wp_plugin_check_after_runtime_setup', array( 'early_exit' => true ) ); return; } @@ -92,6 +119,19 @@ static function () use ( $permalink_structure ) { $wp_filesystem->copy( WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'drop-ins/object-cache.copy.php', WP_CONTENT_DIR . '/object-cache.php' ); } } + + /** + * Fires after the runtime environment is set up, including when it exits early. + * + * @since 2.0.0 + * + * @param array $context { + * Context for the hook. + * + * @type bool $early_exit Whether the method exited before completing all setup steps. + * } + */ + do_action( 'wp_plugin_check_after_runtime_setup', array( 'early_exit' => false ) ); } /** @@ -105,6 +145,19 @@ static function () use ( $permalink_structure ) { public function clean_up() { global $wpdb, $wp_filesystem; + /** + * Fires before the runtime environment is cleaned up. + * + * @since 2.0.0 + * + * @param array $context { + * Context for the hook. + * + * @type bool $early_exit Whether the method exited before completing all cleanup steps. + * } + */ + do_action( 'wp_plugin_check_before_runtime_cleanup', array( 'early_exit' => false ) ); + require_once ABSPATH . '/wp-admin/includes/upgrade.php'; $prefix_cleanup = $this->amend_db_base_prefix(); @@ -121,12 +174,36 @@ public function clean_up() { // Return early if the plugin check object cache does not exist. if ( ! defined( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION' ) || ! WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION ) { + /** + * Fires after the runtime environment is cleaned up, including when it exits early. + * + * @since 2.0.0 + * + * @param array $context { + * Context for the hook. + * + * @type bool $early_exit Whether the method exited before completing all cleanup steps. + * } + */ + do_action( 'wp_plugin_check_after_runtime_cleanup', array( 'early_exit' => true ) ); return; } // Remove the object-cache.php file. if ( $wp_filesystem || WP_Filesystem() ) { if ( ! $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ) { + /** + * Fires after the runtime environment is cleaned up, including when it exits early. + * + * @since 2.0.0 + * + * @param array $context { + * Context for the hook. + * + * @type bool $early_exit Whether the method exited before completing all cleanup steps. + * } + */ + do_action( 'wp_plugin_check_after_runtime_cleanup', array( 'early_exit' => true ) ); return; } @@ -138,6 +215,19 @@ public function clean_up() { $wp_filesystem->delete( WP_CONTENT_DIR . '/object-cache.php' ); } } + + /** + * Fires after the runtime environment is cleaned up, including when it exits early. + * + * @since 2.0.0 + * + * @param array $context { + * Context for the hook. + * + * @type bool $early_exit Whether the method exited before completing all cleanup steps. + * } + */ + do_action( 'wp_plugin_check_after_runtime_cleanup', array( 'early_exit' => false ) ); } /** diff --git a/tests/phpunit/tests/Checker/Runtime_Environment_Setup_Tests.php b/tests/phpunit/tests/Checker/Runtime_Environment_Setup_Tests.php index 6f78930fb..ff943e155 100644 --- a/tests/phpunit/tests/Checker/Runtime_Environment_Setup_Tests.php +++ b/tests/phpunit/tests/Checker/Runtime_Environment_Setup_Tests.php @@ -92,6 +92,88 @@ public function test_can_set_up_with_failing_filesystem() { $this->assertFalse( $runtime_setup->can_set_up() ); } + public function test_before_runtime_setup_action_fires() { + $this->set_up_mock_filesystem(); + + $fired = false; + $context = null; + add_action( + 'wp_plugin_check_before_runtime_setup', + function ( $ctx ) use ( &$fired, &$context ) { + $fired = true; + $context = $ctx; + } + ); + + $runtime_setup = new Runtime_Environment_Setup(); + $runtime_setup->set_up(); + + $this->assertTrue( $fired ); + $this->assertIsArray( $context ); + $this->assertArrayHasKey( 'early_exit', $context ); + $this->assertFalse( $context['early_exit'] ); + } + + public function test_after_runtime_setup_action_fires() { + $this->set_up_mock_filesystem(); + + $fired = false; + $context = null; + add_action( + 'wp_plugin_check_after_runtime_setup', + function ( $ctx ) use ( &$fired, &$context ) { + $fired = true; + $context = $ctx; + } + ); + + $runtime_setup = new Runtime_Environment_Setup(); + $runtime_setup->set_up(); + + $this->assertTrue( $fired ); + $this->assertIsArray( $context ); + $this->assertArrayHasKey( 'early_exit', $context ); + } + + public function test_before_runtime_cleanup_action_fires() { + $fired = false; + $context = null; + add_action( + 'wp_plugin_check_before_runtime_cleanup', + function ( $ctx ) use ( &$fired, &$context ) { + $fired = true; + $context = $ctx; + } + ); + + $runtime_setup = new Runtime_Environment_Setup(); + $runtime_setup->clean_up(); + + $this->assertTrue( $fired ); + $this->assertIsArray( $context ); + $this->assertArrayHasKey( 'early_exit', $context ); + $this->assertFalse( $context['early_exit'] ); + } + + public function test_after_runtime_cleanup_action_fires() { + $fired = false; + $context = null; + add_action( + 'wp_plugin_check_after_runtime_cleanup', + function ( $ctx ) use ( &$fired, &$context ) { + $fired = true; + $context = $ctx; + } + ); + + $runtime_setup = new Runtime_Environment_Setup(); + $runtime_setup->clean_up(); + + $this->assertTrue( $fired ); + $this->assertIsArray( $context ); + $this->assertArrayHasKey( 'early_exit', $context ); + } + public function test_clean_up() { global $wp_filesystem, $wpdb, $table_prefix;