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

Last image in post content has incorrect src & srcset (substitutes from the first image) #245

Open
LeTraceurSnork opened this issue Mar 12, 2025 · 3 comments

Comments

@LeTraceurSnork
Copy link

LeTraceurSnork commented Mar 12, 2025

Describe the bug
Last image from post content (post_type=post) duplicates first image of the same post (if more than one image presents) when WooCommerce enabled alongside with ilab-media-tools plugin (WordPress store link)

Here's what I did: fresh WordPress, only WooCommerce and Media Tools installed. Twentytwentyfive theme, new post, 3 images (pic rel). While plugins is disabled, I get this:
Image

When any of two plugins enabled by its own, the picture is the same, but when I enable two of them simultaneously, I get this picture:
Image

To Reproduce

  1. Install fresh WordPress
  2. Install WooCommerce and Media Tools plugin
  3. Set Media Tools up with any data (important part is to turn Enable Cloud Storage checkbox on)
  4. Create a post with at least two images in its content
  5. Open the post and ensure of the bug

Expected behavior
Last image of the post renders as it is

Screenshots
If applicable, add screenshots to help explain your problem.

Server (please complete the following information)
Server Info: Apache/2.4.62 (Debian)
PHP Version: 8.1.31
PHP Post Max Size: 8 MB
PHP Time Limit: 30
PHP Max Input Vars: 1000
cURL Version: 7.88.1
OpenSSL/3.0.15
WordPress 6.7.1

How did you install the plugin?
Via composer from wpackagist

Additional context
Same issue in WooCommerce repo: woocommerce/woocommerce#56333
The bug appears only when Enable Cloud Storage checkbox is on, even if images are uploaded locally in wp-content/uploads and only when Replace URLs checkbox is on

@LeTraceurSnork
Copy link
Author

LeTraceurSnork commented Mar 13, 2025

Okay, finally figured io out
So, we have \Automattic\WooCommerce\Blocks\BlockTypesController::add_data_attributes() and its trim( $content ); line
And now we have \MediaCloud\Plugin\Tools\Storage\StorageContentHooks::fixGutenbergFigures() with its preg_matches. The problem is that preg_match is greedy by default and specifically preg_match('/class\s*=\s*(?:.*)wp-image-([0-9]+)/m', $figureMatch, $imageIdMatches) seems to match ALL the trimmed string, which would be all of the post content's HTML.

Here's a PR that fixes this behavior, please, accept it ASAP
#248

@LeTraceurSnork
Copy link
Author

@jawngee

@jawngee
Copy link
Contributor

jawngee commented Mar 17, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants