Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions includes/Checker/Runtime_Environment_Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

Expand All @@ -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 ) );
}

/**
Expand All @@ -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();
Expand All @@ -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;
}

Expand All @@ -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 ) );
}

/**
Expand Down
82 changes: 82 additions & 0 deletions tests/phpunit/tests/Checker/Runtime_Environment_Setup_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading