Skip to content

Commit

Permalink
Fix plugin active state detection (#29)
Browse files Browse the repository at this point in the history
* Add PluginProperties::pluginMainFile method

* Update documentation

* Fix plugin active state detection
  • Loading branch information
shvlv authored May 24, 2023
1 parent bec3070 commit cc91569
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
13 changes: 9 additions & 4 deletions src/Properties/PluginProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class PluginProperties extends BaseProperties
*/
private $pluginMainFile;

/**
* @var string
*/
private $pluginBaseName;

/**
* @var bool|null
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions tests/unit/Properties/PluginPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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());
Expand Down

0 comments on commit cc91569

Please sign in to comment.