From cc9156968ebc617fb4e9ba7c5d26a2ddc3b1dd2a Mon Sep 17 00:00:00 2001 From: Shelmuk Vladimir Date: Wed, 24 May 2023 15:32:55 +0300 Subject: [PATCH] Fix plugin active state detection (#29) * Add PluginProperties::pluginMainFile method * Update documentation * Fix plugin active state detection --- src/Properties/PluginProperties.php | 13 +++++++++---- tests/unit/Properties/PluginPropertiesTest.php | 10 ++++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Properties/PluginProperties.php b/src/Properties/PluginProperties.php index dfb70be..ad3abd7 100644 --- a/src/Properties/PluginProperties.php +++ b/src/Properties/PluginProperties.php @@ -44,6 +44,11 @@ class PluginProperties extends BaseProperties */ private $pluginMainFile; + /** + * @var string + */ + private $pluginBaseName; + /** * @var bool|null */ @@ -92,12 +97,12 @@ protected function __construct(string $pluginMainFile) $this->pluginMainFile = wp_normalize_path($pluginMainFile); - $baseName = plugin_basename($pluginMainFile); + $this->pluginBaseName = plugin_basename($pluginMainFile); $basePath = plugin_dir_path($pluginMainFile); $baseUrl = plugins_url('/', $pluginMainFile); parent::__construct( - $baseName, + $this->pluginBaseName, $basePath, $baseUrl, $properties @@ -131,7 +136,7 @@ public function isActive(): bool if (!function_exists('is_plugin_active')) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } - $this->isActive = is_plugin_active($this->pluginMainFile); + $this->isActive = is_plugin_active($this->pluginBaseName); } return $this->isActive; @@ -146,7 +151,7 @@ public function isNetworkActive(): bool if (!function_exists('is_plugin_active_for_network')) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } - $this->isNetworkActive = is_plugin_active_for_network($this->pluginMainFile); + $this->isNetworkActive = is_plugin_active_for_network($this->pluginBaseName); } return $this->isNetworkActive; diff --git a/tests/unit/Properties/PluginPropertiesTest.php b/tests/unit/Properties/PluginPropertiesTest.php index 0ec5b74..f7f5ccd 100644 --- a/tests/unit/Properties/PluginPropertiesTest.php +++ b/tests/unit/Properties/PluginPropertiesTest.php @@ -91,7 +91,10 @@ public function testIsActive(): void $testee = PluginProperties::new($pluginMainFile); - Functions\expect('is_plugin_active')->andReturn(true); + Functions\expect('is_plugin_active') + ->andReturnUsing(static function (string $baseName) use ($expectedBaseName): bool { + return $baseName === $expectedBaseName; + }); static::assertTrue($testee->isActive()); } @@ -111,7 +114,10 @@ public function testIsNetworkActive(): void Functions\expect('plugin_basename')->andReturn($expectedBaseName); Functions\expect('plugin_dir_path')->andReturn($expectedBasePath); - Functions\expect('is_plugin_active_for_network')->andReturn(true); + Functions\expect('is_plugin_active_for_network') + ->andReturnUsing(static function (string $baseName) use ($expectedBaseName): bool { + return $baseName === $expectedBaseName; + }); $testee = PluginProperties::new($pluginMainFile); static::assertTrue($testee->isNetworkActive());