Skip to content

fix: missing remote attachments issue#243

Open
Kallyan01 wants to merge 2 commits into
mainfrom
fix/remote-attachment
Open

fix: missing remote attachments issue#243
Kallyan01 wants to merge 2 commits into
mainfrom
fix/remote-attachment

Conversation

@Kallyan01

@Kallyan01 Kallyan01 commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

What

Remote site post thumbnail issue on the search area.

Why

It's impacting search result functionality.

Related Issue(s):

Closes https://github.com/rtCamp/OnePress/issues/73

How

The thumbnails were not loading in the search results because remote posts were assigned negative post IDs. To address this, we intercepted those posts and generated the thumbnail data through filters, ensuring that thumbnails are displayed correctly in the search results.

AI Disclosure

Claude Opus 4.6 ( Research )

Testing Instructions

  • Set up both the Governing and Brand sites in a local environment using wp-env.
  • Upload a few media files to the Brand site and run the re-index process.
  • On the Governing site, search for the uploaded media file names.
  • Verify that the media results now display visible thumbnails, whereas previously the thumbnails were not appearing.

Screenshots

Before:
image

After:
image

Additional Info

Checklist

  • I have read the Contribution Guidelines.
  • I have read the Development Guidelines.
  • I have added necessary tests to cover my changes.
  • I have updated the project documentation as needed.
  • My code has detailed inline documentation.
  • My code is tested to the best of my abilities.
  • My code passes all lints, tests, and checks.
Open WordPress Playground Preview

@Kallyan01 Kallyan01 self-assigned this Jun 12, 2026
@Kallyan01 Kallyan01 added the Bug label Jun 12, 2026
@Kallyan01 Kallyan01 changed the title Fixed missing remote attachments issue fix:missing remote attachments issue Jun 12, 2026
@Kallyan01 Kallyan01 changed the title fix:missing remote attachments issue fix: missing remote attachments issue Jun 12, 2026
@Kallyan01 Kallyan01 requested a review from justlevine June 12, 2026 10:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes missing thumbnails for remote (Algolia-hydrated) attachment results in the search UI by carrying thumbnail metadata on mocked remote WP_Post objects and intercepting WordPress image/thumbnail rendering paths to use that metadata.

Changes:

  • Adds remote thumbnail plumbing by storing Algolia thumbnail record data on remote WP_Post objects.
  • Hooks into WordPress image/thumbnail/content rendering filters to output remote thumbnail URLs/HTML for negative-ID (remote) posts and attachments.
  • Updates PHPStan stubs to model the new dynamic onesearch_thumbnail property.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
phpstan/stubs/wordpress-extended.php Adds PHPStan typing for the new WP_Post::$onesearch_thumbnail dynamic property.
inc/Modules/Search/Search.php Adds filters + helper logic to render remote thumbnails correctly for search results containing remote/negative-ID posts.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +361 to +373
if ( ! empty( $thumbnail['url'] ) ) {
$block_content = (string) preg_replace(
'#(<img[^>]*\bsrc)="[^"]*"([^>]*>)#si',
'$1="' . esc_url( $thumbnail['url'] ) . '"$2',
$block_content,
1
);
$block_content = (string) preg_replace(
'#\s*(?:srcset|sizes)="[^"]*"#',
'',
$block_content
);
}
Comment on lines +576 to +581
$content = (string) preg_replace(
'#<p\s+class=["\']attachment["\']\s*>.*?</p>#s',
$attachment_html,
$content,
1
);
Comment on lines +416 to +421
return [
$thumbnail['url'],
$thumbnail['width'] ?? 0,
$thumbnail['height'] ?? 0,
false,
];
Comment on lines +468 to +470
if ( is_string( $attr ) && ! empty( $attr ) ) {
$attr_string = ' ' . ltrim( $attr );
}
Comment on lines +472 to +480
if ( is_array( $attr ) ) {
foreach ( $attr as $name => $value ) {
if ( 'class' === $name ) {
$attr_string .= sprintf( ' class="%s"', esc_attr( $value ) );
} else {
$attr_string .= sprintf( ' %s="%s"', esc_attr( $name ), esc_attr( $value ) );
}
}
}

@justlevine justlevine left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore the copilot noise for now: this is what I was concerned about:

Image

(Left is the local post, right is the remote)

It's possible that the preg_replace can be fixed to better support other block image attachments, and even switching to the more ideal HTML API could catch even more. But ultimately the use of replacing the image output is always going to be flawed, fragile, and susceptible to edge cases.

As mentioned both in the GH issue and on slack, unless you have a reason otherwise, I think the best way to address the bug will be to:

  1. Programmatically create a single fictitious attachment at plugin activation (and checking/recreating it when using) and assign that number as the post attachment id instead of a non-existent negative post id. It should be hidden from the UX.
  2. Use the attachment hooks to swap out the metadata on the attachment to the specific remote file, instead of trying to finagle the post directly.

This way any attachment-related functionality work natively the image routing is an isolated implementation detail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants