From ef0143d58e4dd358f7cc3c8d917b18d07cf6e13d Mon Sep 17 00:00:00 2001 From: Thorsten Frommen Date: Fri, 22 Mar 2024 08:01:53 +0100 Subject: [PATCH] PluginProperties: Add support for Requires Plugins header (#41) --- src/Properties/PluginProperties.php | 14 ++++- .../unit/Properties/PluginPropertiesTest.php | 55 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/Properties/PluginProperties.php b/src/Properties/PluginProperties.php index 45d05ae..050abd3 100644 --- a/src/Properties/PluginProperties.php +++ b/src/Properties/PluginProperties.php @@ -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. @@ -37,6 +38,7 @@ class PluginProperties extends BaseProperties // additional headers self::PROP_NETWORK => 'Network', + self::PROP_REQUIRES_PLUGINS => 'RequiresPlugins', ]; /** @@ -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); @@ -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 */ diff --git a/tests/unit/Properties/PluginPropertiesTest.php b/tests/unit/Properties/PluginPropertiesTest.php index f7f5ccd..a9f5fce 100644 --- a/tests/unit/Properties/PluginPropertiesTest.php +++ b/tests/unit/Properties/PluginPropertiesTest.php @@ -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 */