Skip to content

Commit 136f132

Browse files
authored
Merge pull request #788 from lucatume/fix-issue-785
fix issue 785, pluggables
2 parents 909755e + a607a61 commit 136f132

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

src/Module/WPLoader.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,10 @@ private function isWordPressInstalled(): bool
12401240
*/
12411241
private function includeAllPlugins(array $plugins, bool $isMultisite): void
12421242
{
1243-
PreloadFilters::addFilter('plugins_loaded', function () use ($plugins, $isMultisite) {
1243+
$filter = function ($optionValue) use (&$filter, $plugins, $isMultisite) {
1244+
// Immediately remove this function from the filter to avoid infinite loops.
1245+
remove_filter('pre_option_active_plugins', $filter, PHP_INT_MIN);
1246+
12441247
$activePlugins = $isMultisite ? get_site_option('active_sitewide_plugins') : get_option('active_plugins');
12451248

12461249
if (!is_array($activePlugins)) {
@@ -1274,14 +1277,19 @@ private function includeAllPlugins(array $plugins, bool $isMultisite): void
12741277
}
12751278
}
12761279

1277-
12781280
// Update the active plugins to include all plugins, external or not.
12791281
if ($isMultisite) {
12801282
update_site_option('active_sitewide_plugins', $activePlugins);
12811283
} else {
12821284
update_option('active_plugins', array_values(array_unique($activePlugins)));
12831285
}
1284-
}, -100000);
1286+
1287+
// Return the value unchanged.
1288+
return array_values(array_unique($activePlugins));
1289+
};
1290+
1291+
// Use the filter as an action to install (in a different process) and then activate the plugins.
1292+
PreloadFilters::addFilter('pre_option_active_plugins', $filter, PHP_INT_MIN);
12851293
}
12861294

12871295
/**

tests/_data/plugins/mu-plugin-1/plugin.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,12 @@ public static function activate(): void
2121
}
2222
}
2323
register_activation_hook(__FILE__, [MUPlugin1::class, 'activate']);
24+
25+
// Override the get_avatar pluggable function to verify the pluggable override works.
26+
if (!function_exists('get_avatar')) {
27+
function get_avatar($id_or_email, $size = 96, $default_value = '', $alt = '', $args = null)
28+
{
29+
return "<img class='avatar' height=23 width=89 src='https://example.com/avatar.jpg'>";
30+
}
31+
}
32+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace wploadersuite;
4+
5+
use lucatume\WPBrowser\TestCase\WPTestCase;
6+
7+
class PluggablesTest extends WPTestCase
8+
{
9+
public function test_pluggable_function_is_loaded_from_plugin():void{
10+
$this->assertEquals(
11+
"<img class='avatar' height=23 width=89 src='https://example.com/avatar.jpg'>",
12+
get_avatar('[email protected]')
13+
);
14+
}
15+
}

0 commit comments

Comments
 (0)