aaroneaton / better-yourls

Source code for the Better YOURLS WordPress plugin
21 stars 6 forks source link

Don't use create_yourls_url() as a getter method #6

Open dgw opened 8 years ago

dgw commented 8 years ago

Not all YOURLS installations are restricted to one shorturl -> longurl mapping. It's a setting, and some people do want the ability to create different short URLs pointing to the same long URL for use in different channels.

Using create_yourls_url() in getter methods like filter_get_shortlink() and filter_sharing_permalink() assumes that the YOURLS instance in use is configured to generate only one shortlink per longurl—which isn't necessarily true.

This looked like a good candidate for refactoring, to have only one method that deals with checking whether a shortlink exists for the post in question. First it would check the postmeta field, and then query the YOURLS API. Unfortunately it seems the YOURLS API doesn't have a method to fetch the shorturl for a given link without (attempting) to create one…

So maybe it's better to do the refactor, but instead make the getter check the postmeta always, and then call create_yourls_url() iff there is no shortlink stored in the database.

It's tough, because the YOURLS API really should allow asking for a shorturl without making one… Thoughts?

ChrisWiegman commented 8 years ago

Definitely doable. As per #5 let me play with it a bit. I designed this for against a very specific YOURLs install so I'll go back and see about some more dynamic use cases.