Update/ai image filename context#471
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #471 +/- ##
==========================================
Coverage 69.16% 69.17%
Complexity 981 981
==========================================
Files 63 63
Lines 4648 4649 +1
==========================================
+ Hits 3215 3216 +1
Misses 1433 1433
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@dkotter You said "keep this focused on the client-side only for now", which I read as: no shared PHP helper, no schema widening. I'd also like to confirm whether that applies to a filter hook too. The original issue asked for a
What are your thoughts? |
Yeah, comment was mostly about adding a PHP helper. I'm totally fine adding a new filter though so feel free to move forward with that |
|
I reviewed this while comparing it against my duplicate PR #480. I think the centralized client-side approach here is the better direction and it matches the guidance from the issue thread more closely.\n\nOne thing I wanted to flag from that comparison: issue #402 explicitly asks for an prefix and a unique suffix/timestamp. This PR improves the context part well, but currently drops both of those pieces.\n\nI’m closing #480 so discussion can stay here. If it helps, I’m happy to help fold any useful part of that PR into this one instead of keeping two parallel approaches open. |
|
I reviewed this while comparing it against my duplicate PR #480. I think the centralized client-side approach here is the better direction and it matches the guidance from the issue thread more closely. One thing I wanted to flag from that comparison: issue #402 explicitly asks for an ai-generated-image- prefix and a unique suffix/timestamp. This PR improves the context part well, but currently drops both of those pieces. I’m closing #480 so discussion can stay here. If it helps, I’m happy to help fold any useful part of that PR into this one instead of keeping two parallel approaches open. |
While yes, this is mentioned in the attached Issue, I don't think that's a requirement and I actually don't think that's the right slug to use. Right now we use |
…d not the extension.
…s values that are sanitized
|
@dkotter the changes you requested are have been implemented. |
* Add the slugifyForFilename utility function * Use the slugifyForFilename utility function to set the filename. * Add wpai_generated_image_filename filter * Only pass the filename to the wpai_generated_image_filename filter and not the extension. Co-authored-by: saarnilauri <laurisaarni@git.wordpress.org> Co-authored-by: dkotter <dkotter@git.wordpress.org> Co-authored-by: dhrupo <dhrupo@git.wordpress.org> Co-authored-by: jeffpaul <jeffpaul@git.wordpress.org> Co-authored-by: hi0001234d <emptyopssphere@git.wordpress.org> Co-authored-by: kishan-ranawat <kishanranawat@git.wordpress.org>

What?
Closes #402
Derives a descriptive, slugified filename for AI-generated images from the context that
uploadImage()already has locally (alt text, which falls back to the prompt), so attachments land in the Media Library as e.g.getting-started-with-the-wordpress-block-editor.pnginstead ofai-generated-image-<timestamp>.png.Why?
All AI-generated images currently land in the Media Library as
ai-generated-image-<timestamp>.png. That filename:The issue asks for context-aware filenames derived from the post title or prompt. The plugin already computes the descriptive string we need (
alt_text, which is either the generated alt text or the prompt fallback) insideuploadImage(), one line above wheretitleis set from it. The filename can be derived from the same source with no change to the four call sites.How?
Implementation follows the direction from @dkotter in the issue thread:
cleanForSlugfrom@wordpress/urlfor normalization (lowercase, punctuation stripped, hyphen-separated).ai-generated-image-prefix — just the slug.filenameis omitted, and the existing PHP default (ai-generated-image-<timestamp>) kicks in. This satisfies the issue's fallback requirement without any PHP source changes.Changes:
src/utils/text.ts— addsslugifyForFilename( text, length = 75 ). RunscleanForSlug, truncates on a hyphen boundary (reusing the same ≥50%-of-length rule as the existingtrimText), returns an empty string when the input has no slug-worthy characters.src/features/image-generation/functions/upload-image.ts— derivesparams.filenamefromparams.alt_textvia the new helper. Omits the param when the slug is empty.tests/Integration/Includes/Abilities/Image_ImportTest.php— strengthenstest_execute_callback_uses_defaultsto assert theai-generated-image-<timestamp>format is preserved for the empty-slug fallback path.All four existing
uploadImage()call sites (GenerateFeaturedImage,GenerateImageStandalone,GenerateImageInlineModal,MediaLibraryImageEditor) are untouched — the filename is derived centrally.Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 4.7
Used for: Code exploration, implementation of the slug helper and wiring in
upload-image.ts, PHP test extension. All output was reviewed and edited before submission; manual testing in WordPress Playground (WP 7.0-RC2, PHP 8.3, Gutenberg 23.0.0) was performed by me.Testing Instructions
Requires an image-generation provider configured (e.g. OpenAI API key).
ai-generated-image-<timestamp>.png.A red bicycle leaning against a stone wall at sunset. Confirm the Media Library filename reflects the prompt/alt text.npm run test:phpnpm run test:e2eScreenshots
Changelog Entry