Skip to content

Commit ef0143d

Browse files
authoredMar 22, 2024··
PluginProperties: Add support for Requires Plugins header (#41)
1 parent df169cb commit ef0143d

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed
 

‎src/Properties/PluginProperties.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class PluginProperties extends BaseProperties
1717
* Custom properties for Plugins.
1818
*/
1919
public const PROP_NETWORK = 'network';
20+
public const PROP_REQUIRES_PLUGINS = 'requiresPlugins';
2021
/**
2122
* Available methods of Properties::__call()
2223
* from plugin headers.
@@ -37,6 +38,7 @@ class PluginProperties extends BaseProperties
3738

3839
// additional headers
3940
self::PROP_NETWORK => 'Network',
41+
self::PROP_REQUIRES_PLUGINS => 'RequiresPlugins',
4042
];
4143

4244
/**
@@ -84,7 +86,7 @@ protected function __construct(string $pluginMainFile)
8486
if (!function_exists('get_plugin_data')) {
8587
require_once ABSPATH . 'wp-admin/includes/plugin.php';
8688
}
87-
89+
8890
// $markup = false, to avoid an incorrect early wptexturize call. Also we probably don't want HTML here anyway
8991
// @see https://core.trac.wordpress.org/ticket/49965
9092
$pluginData = get_plugin_data($pluginMainFile, false);
@@ -129,6 +131,16 @@ public function network(): bool
129131
return (bool) $this->get(self::PROP_NETWORK, false);
130132
}
131133

134+
/**
135+
* @return array
136+
*/
137+
public function requiresPlugins(): array
138+
{
139+
$value = $this->get(self::PROP_REQUIRES_PLUGINS);
140+
141+
return $value && is_string($value) ? explode(',', $value) : [];
142+
}
143+
132144
/**
133145
* @return bool
134146
*/

‎tests/unit/Properties/PluginPropertiesTest.php

+55
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,61 @@ public function testBasic(): void
7474
static::assertSame($expectedPluginMainFile, $testee->pluginMainFile());
7575
}
7676

77+
/**
78+
* @param string $requiresPlugins
79+
* @param array $expected
80+
*
81+
* @test
82+
*
83+
* @runInSeparateProcess
84+
*
85+
* @dataProvider provideRequiresPluginsData
86+
*/
87+
public function testRequiresPlugins(string $requiresPlugins, array $expected): void
88+
{
89+
$pluginMainFile = '/app/wp-content/plugins/plugin-dir/plugin-name.php';
90+
$expectedBaseName = 'plugin-dir/plugin-name.php';
91+
92+
Functions\expect('get_plugin_data')->andReturn([
93+
'RequiresPlugins' => $requiresPlugins,
94+
]);
95+
Functions\when('plugins_url')->returnArg(1);
96+
Functions\expect('plugin_basename')->andReturn($expectedBaseName);
97+
Functions\when('plugin_dir_path')->returnArg(1);
98+
99+
Functions\expect('wp_normalize_path')->andReturnFirstArg();
100+
101+
$testee = PluginProperties::new($pluginMainFile);
102+
static::assertEquals($expected, $testee->requiresPlugins());
103+
}
104+
105+
/**
106+
* @return array[]
107+
*/
108+
public function provideRequiresPluginsData(): array
109+
{
110+
return [
111+
'no dependencies' => [
112+
'',
113+
[],
114+
],
115+
'one dependency' => [
116+
'dependency',
117+
[
118+
'dependency',
119+
],
120+
],
121+
'multiple dependencies' => [
122+
'dependency1,dependency2,dependency3',
123+
[
124+
'dependency1',
125+
'dependency2',
126+
'dependency3',
127+
],
128+
],
129+
];
130+
}
131+
77132
/**
78133
* @test
79134
*/

0 commit comments

Comments
 (0)
Please sign in to comment.