Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [#20954] Scheduled posts display the correct url #21410

Merged
merged 1 commit into from
Apr 14, 2020

Conversation

kirilzh
Copy link
Contributor

@kirilzh kirilzh commented Apr 5, 2020

Description

Fix #20954
Currently links are set from link key from

api: /wp/v2/posts/:id 

{
   "id":79,
   "guid":{
      "rendered":"http:\/\/localhost:8888\/?p=79",
      "raw":"http:\/\/localhost:8888\/?p=79"
   },
   "slug":"potato-salad",
   "status":"future",
   "link":"http:\/\/localhost:8888\/?p=79",
   "title":{
      "raw":"potato salad",
      "rendered":"potato salad"
   },
   "permalink_template":"http:\/\/localhost:8888\/2020\/04\/17\/%postname%\/",
   "generated_slug":"potato-salad"
}

For scheduled posts this does not represent their respective live URLs. Hence, I changed the source post link. For scheduled posts that have postname in the URL the slug is injected. I chose to use permalink_template as it corresponds to the setting chosen by the user.

How has this been tested?

From Settings -> Permalinks -> checked all options under custom structure. When scheduling a post the permalink_template creates the real url and with %postname% as a template.

Screenshots

image

Types of changes

Non-breaking change. Bug fix.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.

@kirilzh kirilzh force-pushed the update/postpublish-link branch from f861c83 to 62003ed Compare April 7, 2020 13:49
@kirilzh
Copy link
Contributor Author

kirilzh commented Apr 7, 2020

Ready to be reviewed

@paaljoachim
Copy link
Contributor

paaljoachim commented Apr 7, 2020

It would be great to get this added to 5.4.1.
As it fixes scheduled posts so they show the correct URL depending on which permalink setting is used.

@youknowriad @jorgefilipecosta @aduth

@jorgefilipecosta jorgefilipecosta added [Type] Bug An existing feature does not function as intended Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta labels Apr 7, 2020
@pwkip
Copy link
Contributor

pwkip commented Apr 7, 2020

I tested this with normal posts and @kirilzh's fix seems to be working fine.

I was wondering if the "View post" links should also be updated accordingly..

Screenshot from 2020-04-07 22-05-15
Screenshot from 2020-04-07 22-04-51

These links are still pointing to /?p=[post-id]. But with the classic editor plugin enabled, this is also the case. So I guess there's no need to update these.

On the other hand... I think it's a valid idea to actually generate the final URL for scheduled posts as the link attribute in the REST API. So for example: instead of

api: /wp/v2/posts/:id 
{
   "id":79,
   "status":"future",
   "link":"http:\/\/localhost:8888\/?p=79",
   "generated_slug":"potato-salad"
}

Actually have this return

api: /wp/v2/posts/:id 
{
   "id":79,
   "status":"future",
   "link":"http:\/\/localhost:8888\/2020\/04\/17\/potato-salad\/",
   "generated_slug":"potato-salad"
}

@youknowriad youknowriad requested review from earnjam and a team April 8, 2020 09:42
@earnjam
Copy link
Contributor

earnjam commented Apr 8, 2020

We can't update the REST API link attribute, or change the href because the future URL isn't live/routable yet.

@pwkip
Copy link
Contributor

pwkip commented Apr 8, 2020

@earnjam I'm not 100% sure what you mean by "the URL isn't live/routable". I think the same is true for the temporary link, no?

After creating a scheduled post (with the classic editor), I can confirm the following:

I'd like to learn why, if these URLs both work in the same way and point towards the same (in some cases non-existent) resource, why we can't have the permalink set as link from the start? WordPress is even presenting the permalink to the user after creating a scheduled post.
Screenshot from 2020-04-08 20-55-03

Admin accesses the scheduled post via permalink:
Admin accesses the scheduled post via permalink

Admin accesses the scheduled post via temporary link:
Admin accesses the scheduled post via temporary link

Visitor tries to access the scheduled post via permalink:
Visitor tries to access the scheduled post via permalink

Visitor tries to access the scheduled post via temporary link:
Visitor tries to access the scheduled post via temporary link

So what am I missing?

Copy link
Contributor

@mcsf mcsf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this fix. It looks good, though I've requested a couple of changes.

Edit: I see now that a previous review points out the same thing.
One thing to note is that this won't fix the URL shown in the snackbar upon saving a scheduled post. It doesn't necessarily have to, but I think that merits discussion:

Captura de ecrã 2020-04-09, às 17 43 22

* @return {string} PostPublish URL.
*/

const createPostURL = ( post ) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though a bit long, something like getFuturePostUrl might be clearer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment on lines 29 to 30
* Returns real URL of a post. PermalinkTemplate holds all necessary
* information for the real URL.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would update the first sentence based on my suggestion for getFuturePostUrl. The second sentence is IMO unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed description to

* Returns URL for a future post.

@mcsf
Copy link
Contributor

mcsf commented Apr 9, 2020

@earnjam I'm not 100% sure what you mean by "the URL isn't live/routable". I think the same is true for the temporary link, no?

I think the distinction in question (correct me if I'm wrong) was about how, once a post is published, WordPress guarantees that a permalink will keep pointing to the post even if the permalink is changed. That is:

  1. Publish a post as /2020/04/09/abc
  2. Change the slug to def
  3. Both /2020/04/09/abc and /2020/04/09/def will point to the post

In contrast, when dealing with scheduled posts I am not sure what kind of guarantees, if any, the system can make about ensuring the longevity of these generated URLs.

@pwkip
Copy link
Contributor

pwkip commented Apr 9, 2020

I think the question that needs to be answered first is this:
Should WP ever generate a permalink to a post that is not published yet?

  • If yes: why not make this the default behaviour, and put the permalink at the source (i.e. the post.link in the REST API)
  • If not: is this PR a good idea then? Since it would encourage users to use a link that is not guaranteed to work after the post is published.

EDIT: I just think it's confusing to present the user with 2 URLs

  1. http://example.com/?p=35 (temporary link)
  2. http://example.com/2020/05/my-new-post (permalink)

I think we need to decide on which format is the recommended one for scheduled posts.

I personally like the permalink best, but I agree with @mcsf that the original link should keep working if the slug changes. I just checked this, and that doesn't seem to be the case now. So we might need to change that too if the permalink should become the standard link.

@earnjam
Copy link
Contributor

earnjam commented Apr 10, 2020

@earnjam I'm not 100% sure what you mean by "the URL isn't live/routable". I think the same is true for the temporary link, no?

Sorry, I was thinking along the lines of for non-authenticated users, but it doesn't matter for them, as they'll get a 401 when trying to make a request for a scheduled post from the REST API. Neither style link for scheduled posts work for non-authenticated users, and since they couldn't access the post on the endpoint anyway, they don't need to be considered as to which to return/show.

Scheduled are very similar to drafts in their visibility. The primary difference between the two is the presence of a future publish date, and that the slug for a scheduled post has already been passed through wp_unique_post_slug() to prevent conflicts. Drafts don't have their slugs firmed yet, so they aren't guaranteed to be unique. This means we can't make the href for drafts be the estimated pretty permalink because it might point to other content, or return 404 when the slug isn't actually saved yet and is just being estimated in the editor from the post title. That's something that I think warrants further conversation in another issue/PR, as it would be helpful to return unique slugs for drafts to the editor so users know whether they will encounter a slug conflict before they publish. But it could be confusing when it's not a functional URL. Either way it doesn't matter with regards to this PR.

As far as for the link attribute on the REST API for scheduled posts, I do think it is more representative of the shape of the post to show the pretty permalink than the plain permalink. Doing that would also make the editor code simpler here. However, as @mcsf noted, not creating a 301 redirect when the slug changes prior to publishing is something that should be considered if we're going to begin returning it while scheduled.

@pwkip
Copy link
Contributor

pwkip commented Apr 10, 2020

@mcsf , @earnjam, @kirilzh do you all agree that the pretty permalink should be set as the post.link in the REST API (including 301 redirects for changed slugs)?

If so, I can open a trac ticket for this.

@earnjam
Copy link
Contributor

earnjam commented Apr 10, 2020

I think open the Trac ticket for sure. That way we can center that discussion around the REST API change.

For the purposes of this PR, I think it's fine to display the pretty permalink for scheduled posts in the editor using the parts supplied in the response (permalink structure + post name). If the REST API is changed, then we can refactor to just use that value directly.

@pwkip
Copy link
Contributor

pwkip commented Apr 10, 2020

Trac ticket opened: https://core.trac.wordpress.org/ticket/49871

@kirilzh kirilzh force-pushed the update/postpublish-link branch 2 times, most recently from 357026d to b735635 Compare April 13, 2020 08:19
@kirilzh kirilzh force-pushed the update/postpublish-link branch from b735635 to b2ef2fc Compare April 13, 2020 08:29
@mcsf
Copy link
Contributor

mcsf commented Apr 13, 2020

@kirilzh: any changes you'd like to make, or is this ready to merge?

@kirilzh
Copy link
Contributor Author

kirilzh commented Apr 13, 2020

it's ready to be merged

@paaljoachim
Copy link
Contributor

paaljoachim commented Apr 13, 2020

A last test using gutenberg.run.

Initial test.
New post.
Add a title.
Some text in a paragraph block.
Set one week ahead to activate Schedule... button.

Post address:
http://hkd7wa1i.gutenberg.run/2020/04/20/a-test/

Going back to adjust the post.
Resaving. Then viewing post. This is the URL I see:
http://hkd7wa1i.gutenberg.run/?p=11

I was expecting to see the correct permalink structure also on adjusting and updating the scheduled post.

@mcsf
Copy link
Contributor

mcsf commented Apr 14, 2020

Going back to adjust the post.
Resaving. Then viewing post. This is the URL I see:
http://hkd7wa1i.gutenberg.run/?p=11

I was expecting to see the correct permalink structure also on adjusting and updating the scheduled post.

There's a lot of room for improvement in the overall experience, as also noted in #21410 (comment). The question is whether this small step of showing the future URL in the post-schedule view is worth it on its own. I lean towards "yes".

@paaljoachim
Copy link
Contributor

Having the initial scheduled post show a pretty permalink and have all following edited version of the same post show a plain permalink is not a great experience (and makes it feel like a bug).
I am hoping that we could get this issue fully solved, so we do not need to go back to it again.

Schedule a post forward in time and view it on the frontend and see the URL reflect the permalink option one has selected under the permalink settings. On first viewing of the post and on editing the post in other viewings after it.

Hopefully we can get some additional voices in here that have some knowledge to share.
Thanks.

@earnjam

@pwkip
Copy link
Contributor

pwkip commented Apr 14, 2020

I also hope we can move the trac ticket forward and change the scheduled post link to the permalink at get_post_permalink(). I'll do my best to follow up on it and create a patch. But I have a feeling it could take a while because this change might have an impact on other code and some third-party plugins.

For now I'm in for merging this PR, because I believe it's a step in the right direction.
Once it's merged, I think we should add a comment in the trac ticket that this PR should be reverted after the change is done in get_post_permalink().

Copy link
Member

@jorgefilipecosta jorgefilipecosta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my tests, this PR seems like an improvement so I think it would be good to merge. It is no perfect solution yet but it seems better than what we have now.

@mcsf
Copy link
Contributor

mcsf commented Apr 14, 2020

In my tests, this PR seems like an improvement so I think it would be good to merge. It is no perfect solution yet but it seems better than what we have now.

Thanks for weighing in. Yeah, I'd like to merge so that we can improve the other aspects of the UX. The decisions made in the REST API ticket will also have an impact on what our next steps are.

@mcsf mcsf merged commit 527e7f1 into WordPress:master Apr 14, 2020
@github-actions github-actions bot added this to the Gutenberg 8.0 milestone Apr 14, 2020
@jorgefilipecosta
Copy link
Member

Given the discussion we had, I guess it would be good to test this approach in the plugin for some time. I'm removing the "Backport to WP Core" label.

@jorgefilipecosta jorgefilipecosta removed the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Apr 21, 2020
@mcsf mcsf mentioned this pull request Sep 10, 2020
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scheduled post does not show correct url on preview
6 participants