Opened 7 years ago
Last modified 3 weeks ago
#45960 new defect (bug)
plugin_basename() fails on windows path
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Awaiting Review | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | General | Keywords: | reporter-feedback close |
| Focuses: | Cc: |
Description
I happened upon this little bug with plugin_basename(). Namely if a file path include the C: (Windows), it will fail. So here's a quick fix.
<?php if ( substr( strtoupper($file), 0, 2 ) === "C:" ) { $file = ltrim($file, 'C:'); $file = ltrim($file, 'c:'); }
If we add this as initial sterilization, it will work as expected. Otherwise, the wrong string is returned.
Change History (5)
#2
@
7 years ago
Agreed @elrae. According to the codex though, an absolute path is required. I wonder why it's not working with a windows path?
#3
@
7 years ago
I use Wordpress on Windows and never faced this problem. There even appears (https://github.com/WordPress/wordpress-develop/blob/5.0.1/tests/phpunit/tests/functions/pluginBasename.php#L39) to be a test with Windows path names.
In any case, instead of a substr()/ltrim(), a simple preg_replace should do a cleaner job.
preg_replace('|^(?:[A-z]:)|', '', $file);
Above pattern will behave exactly as your snippet in the issue description.
preg_replace('|^(?:[A-z]:)([\\/])|', '$1', $file);
This pattern will also look for a forward/backwards slash in the file name (C:\), and only remove the C: part, making this behave exactly as the previous regex, but has the additional validation for proceeding slash.
#4
@
7 years ago
- Keywords reporter-feedback added
- Version trunk deleted
It'd be good to know what Windows configuration is causing this. @dovyp, can you post more information about your setup?
#5
@
3 weeks ago
- Keywords close added
Hi All,
I've had a quick look into the Windows path problem within the plugin_basename() function, and it appears to have existed before WordPress 4.6. The following changeset changeset (37332) introduced a fix by placing wp_normalize_path() at the start of the function. This ensures Windows paths are correctly normalized before further processing, addressing the mixed path separator problems observed in Windows development environments.
Related: #29154
Since I haven't been able to reproduce this issue in the current release (6.8.3), I'm curious if @dovyp was using an earlier WordPress version? Is this still happening for you? If so, could you please provide more details about your setup? Would you be able to install the Test Reports plugin and generate a report? Sharing this would help the community assist you further in resolving this.
However as this ticket is so old I have added the close tag, but of course if there is more to be said here please feel free to remove it and continue the conversation. 😃
You could technically run WP on a drive other than C:\, it's just the most commonly used letter for a drive on Windows. It could be f:\, z:\ or any other A-Z really. The ideal solution would take into account any character.