[wp-trac] [WordPress Trac] #42670: Symlinked plugin makes plugin_basename function return wrong basename

WordPress Trac noreply at wordpress.org
Sat May 21 00:36:50 UTC 2022


#42670: Symlinked plugin makes plugin_basename function return wrong basename
-------------------------------+------------------------------
 Reporter:  sergiienko         |       Owner:  (none)
     Type:  defect (bug)       |      Status:  new
 Priority:  normal             |   Milestone:  Awaiting Review
Component:  Plugins            |     Version:  4.9
 Severity:  normal             |  Resolution:
 Keywords:  reporter-feedback  |     Focuses:
-------------------------------+------------------------------

Comment (by brianhenryie):

 This issue is a continuation of [28441] where [@ocean90] introduced
 `arsort()` in patch [37983] to address the same problem.

 The reasoning behind using `arsort()` is sound, but the patch failed to
 `break` after matching and a scenario exists where the wrong basename can
 still be returned.

 Given a project structure:


 {{{
 /Users/brianhenry/Sites/my-plugin
 /Users/brianhenry/Sites/my-plugin/my-plugin.php (An empty plugin, only
 headers)
 /Users/brianhenry/Sites/my-plugin/wordpress (WordPress installed here)
 /Users/brianhenry/Sites/my-plugin/wordpress/wp-content (Symlink to
 /Users/brianhenry/Sites/my-plugin/wordpress/wp-content)
 /Users/brianhenry/Sites/my-plugin/wp-content
 /Users/brianhenry/Sites/my-plugin/wp-content/plugins/my-plugin (Symlink to
 /Users/brianhenry/Sites/my-plugin/)
 /Users/brianhenry/Sites/my-plugin/wp-content/plugins/woocommerce
 }}}


 Looking at `plugin_basename()`, after `arsort()` `$wp_plugins_path` will
 look like:

 {{{#!php
 array (
   '/Users/brianhenry/Sites/my-plugin/wordpress/wp-
 content/plugins/woocommerce' => '/Users/brianhenry/Sites/my-plugin/wp-
 content/plugins/woocommerce',
   '/Users/brianhenry/Sites/my-plugin/wordpress/wp-content/plugins/my-
 plugin' => '/Users/brianhenry/Sites/my-plugin',
 )
 }}}


 Running `plugin_basename( __FILE__ )` for WooCommerce means
 `plugin_basename( '/Users/brianhenry/Sites/my-plugin/wp-
 content/plugins/woocommerce/woocommerce.php' )`.

 The operation `if ( strpos( $file, $realdir ) === 0 )` (does `$file` begin
 with `$realdir`?) evaluates to true for both values in the array.

 Using `arsort()` is certainly key to solving this, but the `for` loop must
 `break` once it matches. i.e. use the most specific match (the longest),
 any subsequent matches will be less specific.

 This patch adds only `break;`.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/42670#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list