Fix #1220: support media lookup by filename or URL in MCP abilities - #1223
Fix #1220: support media lookup by filename or URL in MCP abilities#1223faisalahammad wants to merge 5 commits into
Conversation
When Imagify is installed via Composer as a dependency (e.g. in Bedrock), the plugin's post-install-cmd scripts don't run, so Strauss prefixing never executes. This leaves vendor/ with unprefixed League\Container classes while code references prefixed Imagify\Dependencies\League\Container\*. Fix: Add class_alias fallbacks in inc/main.php to map unprefixed classes to prefixed namespace when needed. Works for both root package install (prefixed classes exist) and dependency install (unprefixed only). Fixes wp-media#1073
…lities The three MCP media actions required a numeric media_id, but callers usually know an image by its filename or URL. Add a MediaResolver that resolves media_id, media_filename, or media_url to a single attachment ID, with a clear error when the match is missing or ambiguous. media_id is now optional in optimize-media, restore-media, and get-media-status.
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 17 |
| Duplication | 0 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
|
Thanks for taking this on @faisalahammad, and sorry for the slow reply. I am going to close this one in favour of #1225, which takes the same approach — optional Two things I could not carry over, for the record:
#1225 also drops the Really do appreciate the contribution — please keep them coming. |
Description
Fixes #1220
The three MCP media actions (
imagify/optimize-media,imagify/restore-media,imagify/get-media-status) required a numericmedia_id. Callers usually know an image by its filename or URL, so this adds two optional inputs,media_filenameandmedia_url. A new MediaResolver helper turns any of the three identifiers into a single attachment ID. When a filename matches more than one attachment, the action returns a clear error asking formedia_id.Type of change
Detailed scenario
What was tested
Automated unit tests cover the resolver and all three abilities. The resolver has 14 tests (45 assertions) covering numeric
media_id, URL resolution, filename basename match, ambiguous match, no match, and missing identifier. Each ability'sexecutegained tests for the filename path, the ambiguity error, and the missing-identifier error. Integration tests assert the new schema fields register on all three abilities. Static analysis (phpstan) is clean on the changed files and phpcs passes on the changed files.How to test
imagify/optimize-mediawithmedia_filenameset to a file in the Media Library (for examplehero.jpg).media_urlset to the attachment URL.media_id.Result: media resolves by filename or URL, and missing or ambiguous input fails with an explicit message.
Affected Features & Quality Assurance Scope
The three MCP media abilities are extended with new optional inputs. Existing
media_idbehavior is unchanged. The Media Library upload flow and manual optimization are not touched.Technical description
Documentation
classes/Abilities/MediaResolver.phpis a static helper with one method,resolve_id( array $args ). It checksmedia_idfirst, thenmedia_urlviaattachment_url_to_postid(), thenmedia_filenamevia aWP_Queryon the_wp_attached_filemeta with a basename match. The filename query is capped at five results so the ambiguity check stays bounded. First match wins; more than one match returns aWP_Errorwith a message asking formedia_id. Each ability calls the resolver at the top ofexecute()and folds the resolved ID into the existing flow.docs/api/mcp.mddocuments the new inputs and the resolution rule.New dependencies
None. Uses WordPress core functions (
attachment_url_to_postid,WP_Query) already available.Risks
The filename query uses a
LIKEon postmeta, which is an index hit. It runs only on a one-shot MCP call and is capped at five results, so the cost is bounded. Removingmedia_idfrom the required list means a caller can send no identifier; the resolver returns a clear missing-identifier error instead of failing silently.Mandatory Checklist
Code validation
Code style
Unticked items justification
Manual validation was limited to automated tests because the MCP server needs a running WordPress site with a valid API key. The resolver and each ability path are covered by unit tests, and schema registration is covered by integration tests.
Additional Checks