-
-
Notifications
You must be signed in to change notification settings - Fork 515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Timber 2.x - ImageHelper::theme_url_to_dir() returns an invalid path when theme template is theme-name/theme #2453
Comments
…ig (by fixing ImageHelper::theme_url_to_dir) when there's a slash in theme's template Without this fix, if your theme's tree structure is: -- my-theme-name/ --- images/ ---- foo.jpg --- theme/ ---- functions.php ---- style.css You weren't able to use Twig's image filters (like |resize) on your theme's images, because ImageHelper::theme_url_to_dir() failed to replace theme uri in image uri. It was trying to replace my-theme-name/theme in passed uri, but my-theme-name/theme isn't the root directory of your theme, it must be my-theme-name. Fix timber#2453
A filter sounds like the way to go for me as well. |
Providing the relevant filters is the way to go for me as well. A pull request with the filters was already submitted in #2725. As far as I can see, adding filters will not be a breaking change, so we could safely postpone this for 2.1 (see #2741). Let’s leave this issue open until the pull request is merged. |
As discussed yesterday, this will be a minor change and thus something we can do in 2.0.1 |
PR merged |
First, thanks for your great work on this project :)
Expected behavior
Path returned by
ImageHelper::theme_url_to_dir()
(https://github.com/timber/timber/blob/master/lib/ImageHelper.php#L445) should be consistent (always returning a file path), regardless of theme files structure.In
ImageHelper::theme_url_to_dir()
, we should check ifget_stylesheet()
contains a / and only return what comes before the / when that's the case.That way, $site_root won't contain /theme and theme url will properly be removed from $src.
Actual behavior
My theme has the following structure :
-- my-theme-name/
--- theme/
---- functions.php
---- style.css
--- images/
---- my-image.png
So my theme template is my-theme-name/theme and that's the value returned by
get_stylesheet()
.The problem is that in
ImageHelper::theme_url_to_dir()
, we use that to build $site_root variable, and $site_root ends with /theme (because of get_stylesheet()). But in $src, there's no /theme (because images are stored in my-theme-name/images/), so the following code fails to replace $site_root from url:$tmp = str_replace($site_root, '', $src);
And I end up with errors in my logs whenever I try to use any Twig filter (like |webp) on a theme image:
[25-May-2021 14:20:37 UTC] [ Timber ] Error loading /[...]/themes/my-theme-name/themehttps://[...]/themes/my-theme-name/build/images/my-image.png
The /theme shouldn't be here.
Steps to reproduce behavior
ImageHelper::theme_url_to_dir()
fails to replace theme url from image $srcWhat version of WordPress, PHP and Timber are you using?
WordPress 5.7.2, PHP 7.4, Timber 2.0-dev.
How did you install Timber? (for example, from GitHub, Composer/Packagist, WP.org?)
Through Composer.
The text was updated successfully, but these errors were encountered: