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

Aperta Adatta ai principianti
#1,253 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
php, wordpress

Direzione di ricerca

Inizia in inc/tag_replacer.php intorno alla riga 304, dove il img_url grezzo viene passato a optml_tag_replace, poi leggi Optml_Lazyload_Replacer::get_svg_for() e le sue chiamate is_file() / attachment_url_to_post_id(). Riproduci abilitando Lazyload e il colore placeholder generico, quindi richiedendo /wp-json/wp/v2/posts/<id> da disconnesso mentre osservi le query su wp_postmeta. Fatto significa che il placeholder REST ha le dimensioni reali e nessuna ricerca _wp_attached_file viene eseguita sull'URL con escape JSON.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
PHP
Stelle
74
Fork
14
Merge medio
1g 2h
PR unite (30g)
35

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Codeinwp/optimole-wp

Tutte le issue di Codeinwp/optimole-wp

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.