Skip to content
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

Closed
Moustachos opened this issue May 25, 2021 · 5 comments

Comments

@Moustachos
Copy link

Moustachos commented May 25, 2021

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 if get_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

  • create a new theme with above files structure (taken from Starter theme 2.x: https://github.com/timber/starter-theme/tree/2.x)
  • add a theme image in theme-name/images/
  • load this image inside a Twig file and try to apply any image filter causing Timber to create a new image (like |webp)
  • new image can't be created, because ImageHelper::theme_url_to_dir() fails to replace theme url from image $src
  • see errors in PHP log

What 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.

@Moustachos Moustachos changed the title ImageHelper::theme_url_to_dir() returns an invalid path when theme template is theme-name/controllers Timber 2.x - ImageHelper::theme_url_to_dir() returns an invalid path when theme template is theme-name/theme Jun 3, 2021
Moustachos added a commit to Moustachos/timber-forked that referenced this issue Jun 18, 2021
…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
@gchtr gchtr added the 2.0 label Jul 12, 2021
@gchtr gchtr added needs-info Need more info on this issue in order to properly investigate needs-investigation We've got all the info, now someone needs to look into this to figure out what's going on and removed needs-info Need more info on this issue in order to properly investigate labels Mar 28, 2022
@gchtr gchtr added has-patch 2.x Future and removed needs-investigation We've got all the info, now someone needs to look into this to figure out what's going on 2.0 labels Nov 10, 2022
@gchtr gchtr added 2.0 and removed 2.x Future labels Mar 6, 2023
@gchtr gchtr mentioned this issue May 17, 2023
30 tasks
@nlemoine
Copy link
Member

Sorry, I don't think we should support this non standard structure/edge case in core (already discussed in #2458).

The very best we could do is providing a filter so users could add their workaround.

@gchtr, @Levdbas what do you think?

@Levdbas
Copy link
Member

Levdbas commented May 26, 2023

A filter sounds like the way to go for me as well.

@gchtr
Copy link
Member

gchtr commented May 29, 2023

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.

@gchtr gchtr added 2.x Future and removed 2.0 labels May 29, 2023
@Levdbas Levdbas added this to the 2.0.1 milestone Dec 14, 2023
@Levdbas
Copy link
Member

Levdbas commented Dec 14, 2023

As discussed yesterday, this will be a minor change and thus something we can do in 2.0.1

@Levdbas Levdbas added 2.0 and removed 2.x Future labels Dec 14, 2023
@Levdbas
Copy link
Member

Levdbas commented Jan 26, 2024

PR merged

@Levdbas Levdbas closed this as completed Jan 26, 2024
@Levdbas Levdbas modified the milestones: 2.0.1, 2.1.0 Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants