From fcdd01bb95d29be93abc7b15964dacfad2034e04 Mon Sep 17 00:00:00 2001 From: Christoph Daum Date: Mon, 13 Apr 2026 23:09:08 +0200 Subject: [PATCH] fix: use TextDomain as slug for single-file plugins MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For plugins in subdirectories, the directory name is used as the slug — matching the wp.org convention. But single- file plugins like hello.php derive slug 'hello' from the filename, which doesn't match the wp.org slug 'hello-dolly'. For single-file plugins, prefer the TextDomain header as the slug, since it reliably matches the wp.org slug for virtually all plugins. Fixes #78 --- rt-plugin-report.php | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/rt-plugin-report.php b/rt-plugin-report.php index 8e1dd54..9d8da47 100644 --- a/rt-plugin-report.php +++ b/rt-plugin-report.php @@ -144,7 +144,7 @@ public function settings_page() { echo ''; foreach ( $plugins as $key => $plugin ) { - $slug = $this->get_plugin_slug( $key ); + $slug = $this->get_plugin_slug( $key, $plugin ); $cache_key = $this->create_cache_key( $slug ); $cache = get_site_transient( $cache_key ); if ( $cache ) { @@ -204,7 +204,7 @@ private function get_plugin_slugs() { $plugins = get_plugins(); $slugs = array(); foreach ( $plugins as $key => $plugin ) { - $slugs[] = $this->get_plugin_slug( $key ); + $slugs[] = $this->get_plugin_slug( $key, $plugin ); } return $slugs; } @@ -213,14 +213,20 @@ private function get_plugin_slugs() { /** * Convert a plugin's file path into its slug. * - * @param string $file Plugin file path. + * @param string $file Plugin file path. + * @param array $plugin_data Optional plugin header data from get_plugins(). */ - private function get_plugin_slug( $file ) { + private function get_plugin_slug( $file, $plugin_data = array() ) { if ( strpos( $file, '/' ) !== false ) { $parts = explode( '/', $file ); - } else { - $parts = explode( '.', $file ); + return sanitize_title( $parts[0] ); + } + // Single-file plugin (e.g. hello.php): prefer TextDomain as slug, + // since the filename alone often doesn't match the wp.org slug. + if ( ! empty( $plugin_data['TextDomain'] ) ) { + return sanitize_title( $plugin_data['TextDomain'] ); } + $parts = explode( '.', $file ); return sanitize_title( $parts[0] ); } @@ -293,7 +299,7 @@ private function assemble_plugin_report( $slug ) { // Get the locally available info, and add it to the report. foreach ( $plugins as $key => $plugin ) { - if ( $this->get_plugin_slug( $key ) === $slug ) { + if ( $this->get_plugin_slug( $key, $plugin ) === $slug ) { // Translate plugin data. $textdomain = $plugin['TextDomain']; @@ -742,7 +748,7 @@ private function clear_cache() { $plugins = get_plugins(); // Loop through the plugins array, and delete cache items. foreach ( $plugins as $key => $plugin ) { - $slug = $this->get_plugin_slug( $key ); + $slug = $this->get_plugin_slug( $key, $plugin ); $this->clear_cache_item( $slug ); } } @@ -765,9 +771,11 @@ private function clear_cache_item( $slug ) { public function upgrade_delete_cache_items( $upgrader, $data ) { // Check if plugins have been upgraded by WP. if ( isset( $data ) && isset( $data['plugins'] ) && is_array( $data['plugins'] ) ) { + $plugins = get_plugins(); // Loop through the plugins, and delete the associated cache items. foreach ( $data['plugins'] as $key => $value ) { - $slug = $this->get_plugin_slug( $value ); + $plugin_data = isset( $plugins[ $value ] ) ? $plugins[ $value ] : array(); + $slug = $this->get_plugin_slug( $value, $plugin_data ); $this->clear_cache_item( $slug ); } }