Skip to content

Filter post_preview_link for a viewable preview link#515

Merged
rebeccahum merged 1 commit into
masterfrom
rebecca/fix_513
Aug 6, 2019
Merged

Filter post_preview_link for a viewable preview link#515
rebeccahum merged 1 commit into
masterfrom
rebecca/fix_513

Conversation

@rebeccahum
Copy link
Copy Markdown
Contributor

This PR resolves #513. Since the check (https://github.com/WordPress/WordPress/blob/cf3fa9f7c8038447303eea1fbd3636c5fece28cd/wp-admin/includes/post.php#L1808) in wp_create_post_autosave() fails because an autosave is not present for unpublished posts saved with custom statuses, preview_nonce gets added onto the preview link. This also piggybacks off the logic in wp_create_autosave() to check if this is a personal preview: https://github.com/WordPress/WordPress/blob/cf3fa9f7c8038447303eea1fbd3636c5fece28cd/wp-admin/includes/post.php#L1791

Copy link
Copy Markdown
Contributor

@rinatkhaziev rinatkhaziev left a comment

Choose a reason for hiding this comment

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

:shipit:

@rebeccahum rebeccahum merged commit 929170e into master Aug 6, 2019
@rebeccahum rebeccahum deleted the rebecca/fix_513 branch August 6, 2019 21:34
@valdezm
Copy link
Copy Markdown

valdezm commented Nov 13, 2019

The personal preview conditional isn't working, our site no longer has preview capabilities since preview_nonce never appears. We have to revert this plugin back in order to have our Editors the ability to preview their work before publishing.

This block of code is unconditionally removing preview_nonce:
public function fix_preview_link_part_three( $preview_link, $query_args ) {
if ( $autosave = wp_get_post_autosave( $query_args->ID, $query_args->post_author ) ) {
foreach ( array_intersect( array_keys( _wp_post_revision_fields( $query_args ) ), array_keys( _wp_post_revision_fields( $autosave ) ) ) as $field ) {
if ( normalize_whitespace( $query_args->$field ) != normalize_whitespace( $autosave->$field ) ) {
// Pass through, it's a personal preview.
return $preview_link;
}
}
}
return remove_query_arg( [ 'preview_nonce' ], $preview_link );
}

Please advise

@rebeccahum
Copy link
Copy Markdown
Contributor Author

@valdezm Can you please provide steps on how to reproduce?

@valdezm
Copy link
Copy Markdown

valdezm commented Nov 13, 2019

I am waiting on authroization from the company to dig any deeper in to this. But since the update, the filter is unconditionally removing preview_nonce resulting in the Preview to no longer work.

I'll update in 24 hours, should know by then.

@justmahito
Copy link
Copy Markdown

Hi @rebeccahum, this fix breaks the preview functionality for the cases when the user is not the author of the post, but has appropriate permissions for editing others posts: the issue occurs because WP creates autosave for the current user and in your commit you check autosave for the post author and not for the current user.

Regards,
Maryia

@ashkanghadimi
Copy link
Copy Markdown

Hi
we have the same issue with our published post that preview is not working for theme. some of our posts have wp_nonce in preview link and those are showing the changes, but some of the others don't have wp_nonce and they are not showing the last changes in preview and we have to update (publish) to see the changes. It happens since some months ago with recent updates of edit-flow.

@rebeccahum
Copy link
Copy Markdown
Contributor Author

@ashkanghadimi Hi! Do you have any steps on reproducing this on a clean WP install?

@ashkanghadimi
Copy link
Copy Markdown

1-Make a post with sample text
2-publish it
3-make a change in published post and click on preview button.

You probably can not see the changes after publishing this post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Post Previews are broken on custom post statuses

5 participants