diff --git a/includes/Checker/Preparations/Universal_Runtime_Preparation.php b/includes/Checker/Preparations/Universal_Runtime_Preparation.php new file mode 100644 index 000000000..cd4ab8793 --- /dev/null +++ b/includes/Checker/Preparations/Universal_Runtime_Preparation.php @@ -0,0 +1,71 @@ +check_context = $check_context; + } + + /** + * Runs preparation step for the environment by modifying the plugins and theme to use, + * and returns a closure as a cleanup function. + * + * This preparation needs to be called very early in the WordPress lifecycle, before + * plugins are loaded, e.g. from a drop-in like `object-cache.php`. + * + * @since n.e.x.t + * + * @return callable Cleanup function to revert changes made by theme and plugin preparation classes. + * + * @throws Exception Thrown when preparation fails. + */ + public function prepare() { + + $cleanup_functions = array(); + + $use_minimal_theme_preparation = new Use_Minimal_Theme_Preparation( 'wp-empty-theme', WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . '/test-content/themes' ); + $cleanup_functions[] = $use_minimal_theme_preparation->prepare(); + + $force_single_plugin_preparation = new Force_Single_Plugin_Preparation( $this->check_context->basename() ); + $cleanup_functions[] = $force_single_plugin_preparation->prepare(); + + // Return the cleanup function. + return function () use ( $cleanup_functions ) { + + foreach ( $cleanup_functions as $cleanup_function ) { + $cleanup_function(); + } + }; + } +} diff --git a/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php b/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php new file mode 100644 index 000000000..c805b0e21 --- /dev/null +++ b/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php @@ -0,0 +1,45 @@ +prepare(); + + $this->assertTrue( has_filter( 'option_active_plugins' ) ); + $this->assertTrue( has_filter( 'default_option_active_plugins' ) ); + $this->assertTrue( has_filter( 'stylesheet' ) ); + $this->assertTrue( has_filter( 'template' ) ); + $this->assertTrue( has_filter( 'pre_option_template' ) ); + $this->assertTrue( has_filter( 'pre_option_stylesheet' ) ); + $this->assertTrue( has_filter( 'pre_option_current_theme' ) ); + $this->assertTrue( has_filter( 'pre_option_template_root' ) ); + $this->assertTrue( has_filter( 'pre_option_stylesheet_root' ) ); + + $cleanup(); + + $this->assertFalse( has_filter( 'option_active_plugins' ) ); + $this->assertFalse( has_filter( 'default_option_active_plugins' ) ); + $this->assertFalse( has_filter( 'stylesheet' ) ); + $this->assertFalse( has_filter( 'template' ) ); + $this->assertFalse( has_filter( 'pre_option_template' ) ); + $this->assertFalse( has_filter( 'pre_option_stylesheet' ) ); + $this->assertFalse( has_filter( 'pre_option_current_theme' ) ); + $this->assertFalse( has_filter( 'pre_option_template_root' ) ); + $this->assertFalse( has_filter( 'pre_option_stylesheet_root' ) ); + } +}