Skip to content

Commit f082a71

Browse files
committed
Add new hooks for runtime setup and shutdown
1 parent 0107eca commit f082a71

2 files changed

Lines changed: 172 additions & 0 deletions

File tree

includes/Checker/Runtime_Environment_Setup.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,25 @@ final class Runtime_Environment_Setup {
2424
*
2525
* @global wpdb $wpdb WordPress database abstraction object.
2626
* @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
27+
*
28+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2729
*/
2830
public function set_up() {
2931
global $wpdb, $wp_filesystem;
3032

33+
/**
34+
* Fires before the runtime environment is set up.
35+
*
36+
* @since 2.0.0
37+
*
38+
* @param array $context {
39+
* Context for the hook.
40+
*
41+
* @type bool $early_exit Whether the method exited before completing all setup steps.
42+
* }
43+
*/
44+
do_action( 'wp_plugin_check_before_runtime_setup', array( 'early_exit' => false ) );
45+
3146
require_once ABSPATH . '/wp-admin/includes/upgrade.php';
3247

3348
// Get the existing site URL.
@@ -82,6 +97,18 @@ static function () use ( $permalink_structure ) {
8297

8398
// Return early if the plugin check object cache already exists.
8499
if ( defined( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION' ) && WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION ) {
100+
/**
101+
* Fires after the runtime environment is set up, including when it exits early.
102+
*
103+
* @since 2.0.0
104+
*
105+
* @param array $context {
106+
* Context for the hook.
107+
*
108+
* @type bool $early_exit Whether the method exited before completing all setup steps.
109+
* }
110+
*/
111+
do_action( 'wp_plugin_check_after_runtime_setup', array( 'early_exit' => true ) );
85112
return;
86113
}
87114

@@ -92,6 +119,19 @@ static function () use ( $permalink_structure ) {
92119
$wp_filesystem->copy( WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'drop-ins/object-cache.copy.php', WP_CONTENT_DIR . '/object-cache.php' );
93120
}
94121
}
122+
123+
/**
124+
* Fires after the runtime environment is set up, including when it exits early.
125+
*
126+
* @since 2.0.0
127+
*
128+
* @param array $context {
129+
* Context for the hook.
130+
*
131+
* @type bool $early_exit Whether the method exited before completing all setup steps.
132+
* }
133+
*/
134+
do_action( 'wp_plugin_check_after_runtime_setup', array( 'early_exit' => false ) );
95135
}
96136

97137
/**
@@ -105,6 +145,19 @@ static function () use ( $permalink_structure ) {
105145
public function clean_up() {
106146
global $wpdb, $wp_filesystem;
107147

148+
/**
149+
* Fires before the runtime environment is cleaned up.
150+
*
151+
* @since 2.0.0
152+
*
153+
* @param array $context {
154+
* Context for the hook.
155+
*
156+
* @type bool $early_exit Whether the method exited before completing all cleanup steps.
157+
* }
158+
*/
159+
do_action( 'wp_plugin_check_before_runtime_cleanup', array( 'early_exit' => false ) );
160+
108161
require_once ABSPATH . '/wp-admin/includes/upgrade.php';
109162

110163
$prefix_cleanup = $this->amend_db_base_prefix();
@@ -121,12 +174,36 @@ public function clean_up() {
121174

122175
// Return early if the plugin check object cache does not exist.
123176
if ( ! defined( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION' ) || ! WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION ) {
177+
/**
178+
* Fires after the runtime environment is cleaned up, including when it exits early.
179+
*
180+
* @since 2.0.0
181+
*
182+
* @param array $context {
183+
* Context for the hook.
184+
*
185+
* @type bool $early_exit Whether the method exited before completing all cleanup steps.
186+
* }
187+
*/
188+
do_action( 'wp_plugin_check_after_runtime_cleanup', array( 'early_exit' => true ) );
124189
return;
125190
}
126191

127192
// Remove the object-cache.php file.
128193
if ( $wp_filesystem || WP_Filesystem() ) {
129194
if ( ! $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
195+
/**
196+
* Fires after the runtime environment is cleaned up, including when it exits early.
197+
*
198+
* @since 2.0.0
199+
*
200+
* @param array $context {
201+
* Context for the hook.
202+
*
203+
* @type bool $early_exit Whether the method exited before completing all cleanup steps.
204+
* }
205+
*/
206+
do_action( 'wp_plugin_check_after_runtime_cleanup', array( 'early_exit' => true ) );
130207
return;
131208
}
132209

@@ -138,6 +215,19 @@ public function clean_up() {
138215
$wp_filesystem->delete( WP_CONTENT_DIR . '/object-cache.php' );
139216
}
140217
}
218+
219+
/**
220+
* Fires after the runtime environment is cleaned up, including when it exits early.
221+
*
222+
* @since 2.0.0
223+
*
224+
* @param array $context {
225+
* Context for the hook.
226+
*
227+
* @type bool $early_exit Whether the method exited before completing all cleanup steps.
228+
* }
229+
*/
230+
do_action( 'wp_plugin_check_after_runtime_cleanup', array( 'early_exit' => false ) );
141231
}
142232

143233
/**

tests/phpunit/tests/Checker/Runtime_Environment_Setup_Tests.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,88 @@ public function test_can_set_up_with_failing_filesystem() {
9292
$this->assertFalse( $runtime_setup->can_set_up() );
9393
}
9494

95+
public function test_before_runtime_setup_action_fires() {
96+
$this->set_up_mock_filesystem();
97+
98+
$fired = false;
99+
$context = null;
100+
add_action(
101+
'wp_plugin_check_before_runtime_setup',
102+
function ( $ctx ) use ( &$fired, &$context ) {
103+
$fired = true;
104+
$context = $ctx;
105+
}
106+
);
107+
108+
$runtime_setup = new Runtime_Environment_Setup();
109+
$runtime_setup->set_up();
110+
111+
$this->assertTrue( $fired );
112+
$this->assertIsArray( $context );
113+
$this->assertArrayHasKey( 'early_exit', $context );
114+
$this->assertFalse( $context['early_exit'] );
115+
}
116+
117+
public function test_after_runtime_setup_action_fires() {
118+
$this->set_up_mock_filesystem();
119+
120+
$fired = false;
121+
$context = null;
122+
add_action(
123+
'wp_plugin_check_after_runtime_setup',
124+
function ( $ctx ) use ( &$fired, &$context ) {
125+
$fired = true;
126+
$context = $ctx;
127+
}
128+
);
129+
130+
$runtime_setup = new Runtime_Environment_Setup();
131+
$runtime_setup->set_up();
132+
133+
$this->assertTrue( $fired );
134+
$this->assertIsArray( $context );
135+
$this->assertArrayHasKey( 'early_exit', $context );
136+
}
137+
138+
public function test_before_runtime_cleanup_action_fires() {
139+
$fired = false;
140+
$context = null;
141+
add_action(
142+
'wp_plugin_check_before_runtime_cleanup',
143+
function ( $ctx ) use ( &$fired, &$context ) {
144+
$fired = true;
145+
$context = $ctx;
146+
}
147+
);
148+
149+
$runtime_setup = new Runtime_Environment_Setup();
150+
$runtime_setup->clean_up();
151+
152+
$this->assertTrue( $fired );
153+
$this->assertIsArray( $context );
154+
$this->assertArrayHasKey( 'early_exit', $context );
155+
$this->assertFalse( $context['early_exit'] );
156+
}
157+
158+
public function test_after_runtime_cleanup_action_fires() {
159+
$fired = false;
160+
$context = null;
161+
add_action(
162+
'wp_plugin_check_after_runtime_cleanup',
163+
function ( $ctx ) use ( &$fired, &$context ) {
164+
$fired = true;
165+
$context = $ctx;
166+
}
167+
);
168+
169+
$runtime_setup = new Runtime_Environment_Setup();
170+
$runtime_setup->clean_up();
171+
172+
$this->assertTrue( $fired );
173+
$this->assertIsArray( $context );
174+
$this->assertArrayHasKey( 'early_exit', $context );
175+
}
176+
95177
public function test_clean_up() {
96178
global $wp_filesystem, $wpdb, $table_prefix;
97179

0 commit comments

Comments
 (0)