aaroneaton / better-yourls

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

Better control over when shorturls are created #7

Open dgw opened 8 years ago

dgw commented 8 years ago

I get that a big selling point, as it were, of this plugin is that it has only two options. But I'd like to propose adding an option for when to create shortlinks:

Those are just my ideas for the set of options, and I'd love to discuss alternatives. I'm not always the best at coming up with these things. But I do think that on publish should be the default, so the creation is delayed until the last possible moment, to give users the chance to think of a custom keyword if they want to. I really like the idea of having a "Create YOURLS link" button in the metabox to specifically create the shorturl independent of saving or publishing, too.

The reasoning goes like this: Adding a plugin like this to a site with lots of historical posts, like mine, means that a huge number of posts probably won't have shorturls stored. In that case, when one starts editing old posts to fix the odd broken link, embed, or what-have-you, suddenly shorturls get generated. They probably won't get used in my case, because once posts are announced to social media I rarely use the shortlink again. So they're unnecessary clutter in both the WordPress and YOURLS databases.

Additionally, often I don't think about the shortlink until after I've already saved a draft or two. By then, the plugin's already created one with a YOURLS-chosen keyword and I've lost the opportunity to pick a custom one—unless I want to edit it in YOURLS and then manually edit the WordPress database. (The plugin's postmeta field doesn't seem to be visible from anywhere that allows editing, so ssh + mysql is the only way I've found.) And the default YOURLS API doesn't offer a way for the plugin to change the keyword automatically—nor would one want to, necessarily, because the shortlink might have been used inadvertently and editing the keyword would then break it.

ChrisWiegman commented 8 years ago

This is actually something I've been debating. An "advanced" set of options that isn't displayed in less the user wants to.

It sounds like you have a pretty fancy use case. Can we talk a bit more about your workflow? I'm all for your suggested enhancements but I want to implement in such a manner that they're as flexible as possible for other workflows I haven't taken into consideration.

dgw commented 8 years ago

Mostly, I like to assign a custom keyword to posts before they're published. So if the module creates a shorturl on save (which I've noticed it doing on edits to already published posts) then that's the shortlink and it can't be edited without the aforementioned database monkeying. Not the end of the world, but it'd be nice if another plugin auto-editing a post to (e.g.) fix a link wouldn't cause a shorturl to be created.

Not sure how to differentiate between updating a published post and publishing a post for the first time, though. When I've looked at the code for other plugins that do things on publish, they do things like add a postmeta field indicating that they've already processed that post. So it must be, at least somewhat, a non-trivial problem.

The bit about not using the shortlinks after the posts are announced is a bit of a red herring. I'm not hiding them from the HTML output or anything; I just usually don't personally do anything with them.

dgw commented 8 years ago

I noticed today that this plugin also creates shortlinks for pages. Should I make a separate issue for choosing what post types get shorturls?

dgw commented 8 years ago

Two more things I noticed:

  1. There's a shortlink in YOURLS for /?post_type=wpcf7_contact_form, which isn't even a post.
  2. It's not possible to come up with and save a keyword when drafting a post; filling out the keyword and then saving a draft instead of publishing simply discards the keyword.
ChrisWiegman commented 8 years ago

Time hasn't been my friend lately but I have pushed a branch (https://github.com/ChrisWiegman/Better-YOURLS/tree/feature/fine-grained-control) to allow better control over when short links are created. Here's how it works:

1.) It adds a new setting for post types to choose post types to exclude from shortlink creation 2.) It adds the filter 'better_urls_shortlink' when the short link is created to allow for finer grain control. This filters the link and passes the calling hook (save post, etc) as well as the post id to help. 3.) There is a meta box on permitted post types, in the sidebar on the dashboard, to create a custom short link slug. I think this addresses some of what you're looking for.

In all cases, please take a look and let me know what you think.

dgw commented 8 years ago

This is much better in the current release version. All that's left is generating shortlinks when updating old posts that didn't have them, but that's kind of wanted behavior per #5. (Yes, it took me that long to realize I'd contradicted myself in different issues.)