Make WordPress Core

Ticket #42670: 42670.diff

File 42670.diff, 2.1 KB (added by brianhenryie, 2 years ago)

plugin_basename – Break when matching on sorted array (symlinks)

  • src/wp-includes/plugin.php

     
    720720        foreach ( $wp_plugin_paths as $dir => $realdir ) {
    721721                if ( strpos( $file, $realdir ) === 0 ) {
    722722                        $file = $dir . substr( $file, strlen( $realdir ) );
     723                                                break;
    723724                }
    724725        }
    725726
  • tests/phpunit/tests/functions/pluginBasename.php

     
    6262                $basename = plugin_basename( '/Users/me/Dropbox/Development/Repositories/plugin/trunk/plugin.php' );
    6363                $this->assertSame( 'trunk/plugin.php', $basename );
    6464        }
     65
     66        /**
     67         * @ticket 42670
     68         */
     69        public function test_return_correct_basename_for_symlinked_plugins_when_wp_plugin_dir_is_a_subdir_of_a_plugin() {
     70
     71                /**
     72                 * A lookup array saved by WordPress for symlinked plugins, used only by `plugin_basename()`.
     73                 *
     74                 * @var array<string, string> $wp_plugin_paths An array of path:realpath where path is the path of the symlink in the WP_PLUGIN_DIR and realpath is the actual location in the filesystem that the symlink is pointing to.
     75                 * @see wp_register_plugin_realpath()
     76                 */
     77                global $wp_plugin_paths;
     78
     79                // e.g. `/Users/brianhenry/Sites/plugin-whose-realpath-contains-everything`.
     80                // aka project directory
     81                $wp_plugin_dir_grandparent_directory = dirname( WP_PLUGIN_DIR, 3 );
     82
     83                $wp_plugin_paths = array(
     84                        WP_PLUGIN_DIR . '/plugin-whose-realpath-contains-everything' => $wp_plugin_dir_grandparent_directory,
     85                        WP_PLUGIN_DIR . '/woocommerce' => $wp_plugin_dir_grandparent_directory . '/wp-content/plugins/woocommerce',
     86                );
     87
     88                $result = plugin_basename( $wp_plugin_dir_grandparent_directory . '/wp-content/plugins/woocommerce/woocommerce.php' );
     89
     90                // Actual   :'plugin-whose-realpath-contains-everything/wordpress/wp-content/plugins/woocommerce/woocommerce.php'.
     91                $this->assertEquals( 'woocommerce/woocommerce.php', $result );
     92        }
    6593}