Skip to content

Commit fcdd01b

Browse files
committed
fix: use TextDomain as slug for single-file plugins
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
1 parent e0d7977 commit fcdd01b

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

rt-plugin-report.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function settings_page() {
144144
echo '<tbody>';
145145

146146
foreach ( $plugins as $key => $plugin ) {
147-
$slug = $this->get_plugin_slug( $key );
147+
$slug = $this->get_plugin_slug( $key, $plugin );
148148
$cache_key = $this->create_cache_key( $slug );
149149
$cache = get_site_transient( $cache_key );
150150
if ( $cache ) {
@@ -204,7 +204,7 @@ private function get_plugin_slugs() {
204204
$plugins = get_plugins();
205205
$slugs = array();
206206
foreach ( $plugins as $key => $plugin ) {
207-
$slugs[] = $this->get_plugin_slug( $key );
207+
$slugs[] = $this->get_plugin_slug( $key, $plugin );
208208
}
209209
return $slugs;
210210
}
@@ -213,14 +213,20 @@ private function get_plugin_slugs() {
213213
/**
214214
* Convert a plugin's file path into its slug.
215215
*
216-
* @param string $file Plugin file path.
216+
* @param string $file Plugin file path.
217+
* @param array $plugin_data Optional plugin header data from get_plugins().
217218
*/
218-
private function get_plugin_slug( $file ) {
219+
private function get_plugin_slug( $file, $plugin_data = array() ) {
219220
if ( strpos( $file, '/' ) !== false ) {
220221
$parts = explode( '/', $file );
221-
} else {
222-
$parts = explode( '.', $file );
222+
return sanitize_title( $parts[0] );
223+
}
224+
// Single-file plugin (e.g. hello.php): prefer TextDomain as slug,
225+
// since the filename alone often doesn't match the wp.org slug.
226+
if ( ! empty( $plugin_data['TextDomain'] ) ) {
227+
return sanitize_title( $plugin_data['TextDomain'] );
223228
}
229+
$parts = explode( '.', $file );
224230
return sanitize_title( $parts[0] );
225231
}
226232

@@ -293,7 +299,7 @@ private function assemble_plugin_report( $slug ) {
293299

294300
// Get the locally available info, and add it to the report.
295301
foreach ( $plugins as $key => $plugin ) {
296-
if ( $this->get_plugin_slug( $key ) === $slug ) {
302+
if ( $this->get_plugin_slug( $key, $plugin ) === $slug ) {
297303

298304
// Translate plugin data.
299305
$textdomain = $plugin['TextDomain'];
@@ -742,7 +748,7 @@ private function clear_cache() {
742748
$plugins = get_plugins();
743749
// Loop through the plugins array, and delete cache items.
744750
foreach ( $plugins as $key => $plugin ) {
745-
$slug = $this->get_plugin_slug( $key );
751+
$slug = $this->get_plugin_slug( $key, $plugin );
746752
$this->clear_cache_item( $slug );
747753
}
748754
}
@@ -765,9 +771,11 @@ private function clear_cache_item( $slug ) {
765771
public function upgrade_delete_cache_items( $upgrader, $data ) {
766772
// Check if plugins have been upgraded by WP.
767773
if ( isset( $data ) && isset( $data['plugins'] ) && is_array( $data['plugins'] ) ) {
774+
$plugins = get_plugins();
768775
// Loop through the plugins, and delete the associated cache items.
769776
foreach ( $data['plugins'] as $key => $value ) {
770-
$slug = $this->get_plugin_slug( $value );
777+
$plugin_data = isset( $plugins[ $value ] ) ? $plugins[ $value ] : array();
778+
$slug = $this->get_plugin_slug( $value, $plugin_data );
771779
$this->clear_cache_item( $slug );
772780
}
773781
}

0 commit comments

Comments
 (0)