@@ -15,6 +15,11 @@ class WpTestsStarter
15
15
16
16
private DbUrlParser $ dbUrlParser ;
17
17
18
+ /**
19
+ * @var callable[]
20
+ */
21
+ private static $ livePlugins = [];
22
+
18
23
/**
19
24
* @var callable[]
20
25
*/
@@ -56,6 +61,7 @@ public function bootstrap()
56
61
$ this ->defineConstants ();
57
62
$ this ->declareGlobals ();
58
63
$ this ->writeConfigFile ();
64
+ $ this ->installLivePlugin ();
59
65
60
66
$ wpBoostrapFile = $ this ->baseDir . '/tests/phpunit/includes/bootstrap.php ' ;
61
67
require_once $ wpBoostrapFile ;
@@ -188,11 +194,63 @@ public function addActivePlugin(string $plugin): self
188
194
return $ this ;
189
195
}
190
196
197
+ public function addLivePlugin (callable $ plugin ): self
198
+ {
199
+ self ::$ livePlugins [] = $ plugin ;
200
+
201
+ return $ this ;
202
+ }
203
+
191
204
public function useTablePrefix (string $ prefix ): self
192
205
{
193
206
return $ this ->useGlobalVar ('table_prefix ' , $ prefix );
194
207
}
195
208
209
+ public static function runLivePlugins (): void
210
+ {
211
+ foreach (self ::$ livePlugins as $ livePlugin ) {
212
+ $ livePlugin ();
213
+ }
214
+ }
215
+
216
+ private function installLivePlugin (): void
217
+ {
218
+ if (!self ::$ livePlugins ) {
219
+ return ;
220
+ }
221
+
222
+ $ pluginCode = <<<'PHP'
223
+ <?php
224
+
225
+ declare(strict_types=1);
226
+
227
+ /**
228
+ * Plugin Name: Wp Tests Starter live plugin
229
+ */
230
+ namespace Inpsyde\WpTestsStarter;
231
+
232
+ if (!class_exists(\Inpsyde\WpTestsStarter\WpTestsStarter::class)) {
233
+ return;
234
+ }
235
+
236
+ add_action(
237
+ 'muplugins_loaded',
238
+ [\Inpsyde\WpTestsStarter\WpTestsStarter::class, 'runLivePlugins']
239
+ );
240
+ PHP;
241
+
242
+ $ muPluginDir = $ this ->muPluginDir ();
243
+ if (!is_dir ($ muPluginDir )) {
244
+ mkdir ($ muPluginDir , 0755 , true );
245
+ }
246
+
247
+ $ pluginFile = $ muPluginDir . '/wp-tests-starter-live-plugin.php ' ;
248
+ file_put_contents (
249
+ $ pluginFile ,
250
+ $ pluginCode
251
+ );
252
+ }
253
+
196
254
private function defineConstants (): void
197
255
{
198
256
foreach ($ this ->constants as $ constant => $ value ) {
@@ -232,6 +290,27 @@ private function generateSalts()
232
290
}
233
291
}
234
292
293
+ private function muPluginDir (): ?string
294
+ {
295
+ if (defined ('WPMU_PLUGIN_DIR ' )) {
296
+ return (string ) WPMU_PLUGIN_DIR ;
297
+ }
298
+
299
+ if (array_key_exists ('WPMU_PLUGIN_DIR ' , $ this ->constants )) {
300
+ return (string ) $ this ->constants ['WPMU_PLUGIN_DIR ' ];
301
+ }
302
+
303
+ if (defined ('WP_CONTENT_DIR ' )) {
304
+ return WP_CONTENT_DIR . '/mu-plugins ' ;
305
+ }
306
+
307
+ if (array_key_exists ('WP_CONTENT_DIR ' , $ this ->constants )) {
308
+ return $ this ->constants ['WP_CONTENT_DIR ' ] . '/mu-plugins ' ;
309
+ }
310
+
311
+ return $ this ->baseDir . '/src/wp-content/mu-plugins ' ;
312
+ }
313
+
235
314
private function writeConfigFile ()
236
315
{
237
316
$ configFile = $ this ->getConfigFile ();
0 commit comments