Skip to content

Commit

Permalink
PluginProperties: Add support for Requires Plugins header (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
tfrommen authored Mar 22, 2024
1 parent df169cb commit ef0143d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Properties/PluginProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class PluginProperties extends BaseProperties
* Custom properties for Plugins.
*/
public const PROP_NETWORK = 'network';
public const PROP_REQUIRES_PLUGINS = 'requiresPlugins';
/**
* Available methods of Properties::__call()
* from plugin headers.
Expand All @@ -37,6 +38,7 @@ class PluginProperties extends BaseProperties

// additional headers
self::PROP_NETWORK => 'Network',
self::PROP_REQUIRES_PLUGINS => 'RequiresPlugins',
];

/**
Expand Down Expand Up @@ -84,7 +86,7 @@ protected function __construct(string $pluginMainFile)
if (!function_exists('get_plugin_data')) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}

// $markup = false, to avoid an incorrect early wptexturize call. Also we probably don't want HTML here anyway
// @see https://core.trac.wordpress.org/ticket/49965
$pluginData = get_plugin_data($pluginMainFile, false);
Expand Down Expand Up @@ -129,6 +131,16 @@ public function network(): bool
return (bool) $this->get(self::PROP_NETWORK, false);
}

/**
* @return array
*/
public function requiresPlugins(): array
{
$value = $this->get(self::PROP_REQUIRES_PLUGINS);

return $value && is_string($value) ? explode(',', $value) : [];
}

/**
* @return bool
*/
Expand Down
55 changes: 55 additions & 0 deletions tests/unit/Properties/PluginPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,61 @@ public function testBasic(): void
static::assertSame($expectedPluginMainFile, $testee->pluginMainFile());
}

/**
* @param string $requiresPlugins
* @param array $expected
*
* @test
*
* @runInSeparateProcess
*
* @dataProvider provideRequiresPluginsData
*/
public function testRequiresPlugins(string $requiresPlugins, array $expected): void
{
$pluginMainFile = '/app/wp-content/plugins/plugin-dir/plugin-name.php';
$expectedBaseName = 'plugin-dir/plugin-name.php';

Functions\expect('get_plugin_data')->andReturn([
'RequiresPlugins' => $requiresPlugins,
]);
Functions\when('plugins_url')->returnArg(1);
Functions\expect('plugin_basename')->andReturn($expectedBaseName);
Functions\when('plugin_dir_path')->returnArg(1);

Functions\expect('wp_normalize_path')->andReturnFirstArg();

$testee = PluginProperties::new($pluginMainFile);
static::assertEquals($expected, $testee->requiresPlugins());
}

/**
* @return array[]
*/
public function provideRequiresPluginsData(): array
{
return [
'no dependencies' => [
'',
[],
],
'one dependency' => [
'dependency',
[
'dependency',
],
],
'multiple dependencies' => [
'dependency1,dependency2,dependency3',
[
'dependency1',
'dependency2',
'dependency3',
],
],
];
}

/**
* @test
*/
Expand Down

0 comments on commit ef0143d

Please sign in to comment.