How to add a rewrite endpoint to unattached media?
EP_ATTACHMENT will insert rules for things attached to posts (and custom post types), but not unattached media. You can make this work by adding it to EP_PAGES instead- add_rewrite_endpoint( ‘foo’, EP_PAGES );