Lazyload SVG placeholder runs uncached attachment lookups with JSON-escaped URLs on REST responses

Offen Anfängerfreundlich
#1,253 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
72/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
php, wordpress
Bereich
backend, performance

Rechercherichtung

Beginne in inc/tag_replacer.php um Zeile 304, wo die rohe img_url an optml_tag_replace übergeben wird, und lies dann Optml_Lazyload_Replacer::get_svg_for() sowie dessen Aufrufe is_file() / attachment_url_to_post_id(). Reproduziere, indem du Lazyload und die generische Platzhalterfarbe aktivierst und dann ausgeloggt /wp-json/wp/v2/posts/<id> anfragst, während du die wp_postmeta-Abfragen beobachtest. Fertig bedeutet, dass der REST-Platzhalter die echten Dimensionen hat und keine _wp_attached_file-Lookups gegen die JSON-escapte URL laufen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

customer report

Reported in: https://secure.helpscout.net/conversation/3524196151/501323

On REST requests, Optml_Manager::process_template_redirect_content() buffers the whole JSON body, and Optml_Tag_Replacer passes the raw escaped img_url (https:\/\/...) to the optml_tag_replace filter (inc/tag_replacer.php:304). Optml_Lazyload_Replacer::get_svg_for() then calls is_file() and attachment_url_to_post_id() with that escaped URL. The file check fails and all 4 attachment_url_to_postid() variants miss (the www variant is also a no-op, since :// is not present in an escaped URL). ID 0 is cached for 10 minutes, so the lookups repeat after that.

Steps to reproduce (4.2.20)
  1. Enable Lazyload and "Enable generic placeholder color".
  2. Add an attachment image without width/height attributes to a post.
  3. Request /wp-json/wp/v2/posts/<id> while logged out.
Expected

The placeholder uses the real dimensions, as it does on the front end.

Actual

A 100% x 100% SVG placeholder, plus 4 wp_postmeta queries per image matching the escaped URL:

SELECT post_id, meta_value FROM wp_postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = 'http:\\/\\/localhost:8955\\/wp-content\\/uploads\\/2026\\/11\\/chart.jpg'
SELECT post_id, meta_value FROM wp_postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = 'http:\\/\\/localhost:8955\\/wp-content\\/uploads\\/2026\\/11\\/chart-scaled.jpg'
(same two again for the www variant)
Request Placeholder _wp_attached_file queries
Front end 2200 x 360 (correct) 0
REST 100% x 100% (fallback) 4 per image
REST, generic placeholder disabled blurred preview 0

On the reporter's site (large wp_postmeta table, Memcached object cache) one REST request for a custom post type made 29 wp_postmeta queries taking 6.25 s of a 7.98 s transaction, traced in New Relic through Optml_Lazyload_Replacer::get_svg_for > attachment_url_to_post_id.

Workaround

Disable "Enable generic placeholder color" (Settings > Advanced > Lazyload).

Suggested fix

Unslash the URL before get_svg_for() and the attachment lookup. Use the wp-image-<ID> class when it is present, before any URL lookup.

Related: #1249

Vorherrschende Sprache
PHP
Sterne
74
Forks
14
Ø Merge
1 T. 2 Std.
Gemergte PRs (30 T.)
35

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus Codeinwp/optimole-wp

Alle Issues in Codeinwp/optimole-wp

Ähnliche Issues

Weitere Issues zu PHP

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.