Repository navigation
Fix Subscriber listing owners unable to delete their listing images - #583
Conversation
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Approve. Nothing blocking, no notes.
What I checked
- The change is one expression in
can_delete_listing_image(): a logged-in user passes withdelete_poston the image OR whenpost_authorof the attachment is their own ID. Thebelongs_to_listing()check above it is untouched, and the AJAX handler still requires the nonce,can_edit_listing_images()and the listing-parent match first. A guest never reaches the new clause (not logged in returns earlier), andget_current_user_id()is always above 0 there, so an author of 0 cannot match. - Your reasoning for not dropping
delete_postholds: the re-parented foreign image case from #538 is still denied, because that image's author is not the caller.
Live before/after (BD Playground env, throwaway mu-plugin that creates a Subscriber owner, a listing they own, one image they uploaded and one an admin uploaded, then calls the real private can_delete_listing_image() by reflection and the real ajax_listing_submit_image_delete() handler with a valid nonce):
| Case | master | PR |
|---|---|---|
| Subscriber owner, own upload | false |
true |
| Subscriber owner, admin's upload | false |
false (the known limit you documented) |
| Another Subscriber, owner's upload | false |
false |
| Real AJAX handler, owner, own upload | {"success":false,"error":"Unknown error"} |
{"success":true,"data":{"imageId":8}} |
CI: both Codeception jobs, PHPStan and PHPCS are green at this head. The new testSubscriberOwnerCanDeleteOwnListingImage is covered by those CI runs (I did not run PHPUnit separately).
Not run: a browser click-through of the front-end edit page (the handler and permission check were exercised directly, as above), and the review skills (a one-expression diff plus one test).
Fixes Strategy11/business-directory-premium#395
What was broken
A Subscriber who owns a listing could not delete its images on the front-end edit page.
can_delete_listing_image()requireddelete_poston the image, and Subscribers don't havedelete_posts. The AJAX call returnedsuccess:falseand nothing happened.What changed
A logged-in user can now delete a listing image if they have
delete_poston it or they are the image's author (they uploaded it). Guests and the other checks are unchanged.The issue suggests dropping the
delete_postcheck and relying onbelongs_to_listing(). I didn't, becausebelongs_to_listing()is also true for a foreign image that was re-parented to the listing.testReparentedForeignImageCannotBeDeletedAfterUpgrade(the #538 security fix) covers that case and still passes.Known limit: an image uploaded by someone else (for example an admin on the owner's behalf) still can't be deleted by a Subscriber owner.
Verified
testSubscriberOwnerCanDeleteOwnListingImage. CI red before the fix (1 failure of 36), green after (both Codeception jobs, PHPCS, PHPStan): https://github.com/Strategy11/business-directory-plugin/actions/runs/44137595437🤖 Generated with Claude Code