From 33cbc2f77bf2a2f78d76eab4f785c089020a35d2 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Thu, 20 Oct 2022 17:32:20 +0000 Subject: [PATCH 1/3] Add function to determine "local" slug --- wp-core-misc.php | 73 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 65 insertions(+), 8 deletions(-) diff --git a/wp-core-misc.php b/wp-core-misc.php index c48fc7ca9..33cde7cba 100644 --- a/wp-core-misc.php +++ b/wp-core-misc.php @@ -225,6 +225,66 @@ function vipgoci_wpcore_misc_get_addon_headers_and_type( } } +/** + * Determine "local" slug used when querying the WordPress.org API. + * + * @param string $addon_type Addon type, plugin or theme. + * @param string $full_path Full path to plugin or theme in git repository. + * @param string $relative_path Relative path to plugin or theme. + * + * @return string "local" slug. + */ +function vipgoci_wpcore_misc_determine_local_slug( + string $addon_type, + string $full_path, + string $relative_path +) :string { + if ( VIPGOCI_ADDON_THEME === $addon_type ) { + // Special case for themes. + $key = basename( dirname( $full_path ) ); + } elseif ( + ( VIPGOCI_ADDON_PLUGIN === $addon_type ) && + ( true === str_contains( $relative_path, '/' ) ) + ) { + $relative_path_dirname = dirname( $relative_path ); + + $relative_path_dirname_arr = array_reverse( + explode( '/', $relative_path_dirname ) + ); + + $relative_path_dirname_dircount = count( + $relative_path_dirname_arr + ); + + /* + * Only return "local" slug including directory if there are one or more + * directories in path, but not when last directory name includes certain + * names that will result in bogus results. + */ + if ( + ( $relative_path_dirname_dircount >= 1 ) && + ( false === in_array( + strtolower( $relative_path_dirname_arr[0] ), + array( + 'plugins', + 'themes', + 'library', + ), + true + ) ) + ) { + $key = $relative_path_dirname_arr[0] . '/' . + basename( $relative_path ); + } else { + $key = basename( $relative_path ); + } + } else { + $key = basename( $relative_path ); + } + + return $addon_type . '-' . $key; +} + /** * Get list of plugins or themes found in $path, return as array of * key-value pairs. @@ -325,14 +385,11 @@ function vipgoci_wpcore_misc_scan_directory_for_addons( /* * Calculate 'local slug'. */ - if ( VIPGOCI_ADDON_THEME === $addon_data['type'] ) { - // Special case for themes. - $wp_addon_key = $addon_data['type'] . '-' . basename( dirname( $tmp_path ) ); - } elseif ( str_contains( $addon_file, '/' ) ) { - $wp_addon_key = $addon_data['type'] . '-' . dirname( $addon_file ) . '/' . basename( $addon_file ); - } else { - $wp_addon_key = $addon_data['type'] . '-' . basename( $addon_file ); - } + $wp_addon_key = vipgoci_wpcore_misc_determine_local_slug( + $addon_data['type'], + $tmp_path, + $addon_file + ); $wp_addons[ $wp_addon_key ] = $addon_data; } From 75eb07cabec06cca5b875306e72c21b0d36b87de Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Wed, 9 Nov 2022 19:48:36 +0000 Subject: [PATCH 2/3] Add test for vipgoci_wpcore_misc_determine_local_slug() --- .../unit/WpCoreMiscDetermineLocalSlugTest.php | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 tests/unit/WpCoreMiscDetermineLocalSlugTest.php diff --git a/tests/unit/WpCoreMiscDetermineLocalSlugTest.php b/tests/unit/WpCoreMiscDetermineLocalSlugTest.php new file mode 100644 index 000000000..60a998760 --- /dev/null +++ b/tests/unit/WpCoreMiscDetermineLocalSlugTest.php @@ -0,0 +1,93 @@ +repo_path . '/themes/test-theme1/style.css' ), + array( $slug_prefix . 'theme-test-theme2', VIPGOCI_ADDON_THEME, $this->repo_path . '/test-group/themes/test-theme2/style.css' ), + array( $slug_prefix . 'theme-test-theme3', VIPGOCI_ADDON_THEME, $this->repo_path . '/plugins/test-plugin/test-theme3/style.css' ), + + array( $slug_prefix . 'plugin-hello-dolly/hello1.php', VIPGOCI_ADDON_PLUGIN, $this->repo_path . '/plugins/hello-dolly/hello1.php' ), + array( $slug_prefix . 'plugin-hello-dolly/hello2.php', VIPGOCI_ADDON_PLUGIN, $this->repo_path . '/themes/test-theme4/plugins/hello-dolly/hello2.php' ), + array( $slug_prefix . 'plugin-hello3.php', VIPGOCI_ADDON_PLUGIN, $this->repo_path . '/themes/test-theme5/plugins/hello3.php' ), + array( $slug_prefix . 'plugin-hello4.php', VIPGOCI_ADDON_PLUGIN, $this->repo_path . '/hello4.php' ), + ); + } + + /** + * Test common usage of the function. + * + * @param string $expected_local_slug Expected "local" slug. + * @param string $input_addon_type Add-on type. + * @param string $input_full_path Full path to file. + * + * @dataProvider dataDetermineLocalSlug + * + * @covers ::vipgoci_wpcore_misc_determine_local_slug + * + * @return void + */ + public function testDetermineLocalSlug( + string $expected_local_slug, + string $input_addon_type, + string $input_full_path, + ): void { + $input_relative_path = str_replace( + $this->repo_path . '/', + '', + $input_full_path + ); + + $this->assertSame( + $expected_local_slug, + vipgoci_wpcore_misc_determine_local_slug( + $input_addon_type, + $input_full_path, + $input_relative_path + ) + ); + } +} From 647568aedd1296b988cd3ec73ae82571d6fbf285 Mon Sep 17 00:00:00 2001 From: "Gudmundur D. Haraldsson" Date: Thu, 10 Nov 2022 15:27:46 +0000 Subject: [PATCH 3/3] Update WpCoreMiscDetermineLocalSlugTest.php --- tests/unit/WpCoreMiscDetermineLocalSlugTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/WpCoreMiscDetermineLocalSlugTest.php b/tests/unit/WpCoreMiscDetermineLocalSlugTest.php index 60a998760..b69577044 100644 --- a/tests/unit/WpCoreMiscDetermineLocalSlugTest.php +++ b/tests/unit/WpCoreMiscDetermineLocalSlugTest.php @@ -26,7 +26,7 @@ final class WpCoreMiscDetermineLocalSlugTest extends TestCase { private string $repo_path = '/tmp/git-repo-123'; /** - * Setup function. Require file. + * Setup function. Require files. * * @return void */