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

Post Content/Title: Reflect changes when previewing post #37622

Merged
merged 3 commits into from
Dec 28, 2021

Conversation

Mamaduka
Copy link
Member

Description

Resolves #37463.

The preview filter applies content from autosave to the queries object (and global post). When we pass a post ID to the get_the_content, it uses an instance of the post where those changes aren't applied. Using null instead of $post_id in this scenario fixes the issue.

How has this been tested?

Using TT2 theme.

  1. Makes a change to a post without saving.
  2. Click Preview -> Preview in new tab
  3. Confirm that changes are reflected in the preview.

Test Query Loops

  1. Add Query block to a page (you can use one of the patterns).
  2. Disable "Inherit query from template"
  3. Replace Post Expert with Post Content.
  4. Check that post content is correctly displayed in the loop.

Additionally, check that main queries like index or archive page work as expected.

Screencast

CleanShot.2021-12-24.at.15.11.47.mp4

Types of changes

Bugfix

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).

@Mamaduka
Copy link
Member Author

@aristath, I wasn't able to provide a lot of context regarding this condition, so I'll post my best guess below. Would you please let me know if I'm correct?

if ( ! in_the_loop() && have_posts() ) {
the_post();
}

  1. The in_the_loop is used to check if the Post Template loop renders the block.
  2. If not, we try to set up post data when there are posts.

Is this correct?

@Mamaduka Mamaduka self-assigned this Dec 24, 2021
@Mamaduka Mamaduka added [Block] Post Content Affects the Post Content Block [Type] Bug An existing feature does not function as intended Backport to WP 6.6 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta labels Dec 24, 2021
@Mamaduka Mamaduka added this to 👀 Needs review in WordPress 5.9 Must-Haves via automation Dec 24, 2021
@adamsilverstein
Copy link
Member

Excellent work tracking this down @Mamaduka, thank you! I can confirm this PR fixes the preview of content for published posts.

One issue remains: changes to the post title are not reflected in the preview (which makes sense given your fix only applies to the_content).

To reproduce:

  1. make changes to a post title without saving.
  2. preview the post in a new tab

expected result: the updated title should be shown
actual result: the original post title is displayed.

I'm going to dig a bit more into the title issue: still this is a big improvement so we should land it either way before the next release

@adamsilverstein
Copy link
Member

@Mamaduka -

In 61c1402 (#37622) I changed get_the_title in packages/block-library/src/post-title/index.php to skip passing the post id, thus returning the global post object. In the case of previews, this will be correctly set to the preview post.

In my testing, changes to the post title are now correctly reflected in the post preview.

I'm not sure why this call would ever need to pass a post id, so I removed it. If I missed something here, please feel free to correct me!

Copy link
Member

@adamsilverstein adamsilverstein left a comment

Choose a reason for hiding this comment

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

Looks good!

@Mamaduka
Copy link
Member Author

Thanks for testing, @adamsilverstein.

I also realized that I could omit passing $post_id and get the same result. It wasn't working in my initial tests for some reason.

I decided to leave a modified version of my original comment just in case to avoid future regressions. Plus added new commit regarding in_the_loop() logic.

@Mamaduka Mamaduka changed the title Post Content: Reflect changes when previewing post Post Content/Title: Reflect changes when previewing post Dec 28, 2021
Copy link
Member

@walbo walbo left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Tested and the changes are now correct in the preview and in the saved post.

Should the $post_ID be removed from the Post Featured Image as well?

@Mamaduka
Copy link
Member Author

Should the $post_ID be removed from the Post Featured Image as well?

The _set_preview only updates title, content, and excerpt, plus thumbnail is stored in post meta, which is not covered by revisions.

I'm going to merge this PR (the failing test isn't related) and follow up with an excerpt block update.

@Mamaduka Mamaduka merged commit 884341d into trunk Dec 28, 2021
WordPress 5.9 Must-Haves automation moved this from 👀 Needs review to ✅ Done Dec 28, 2021
@Mamaduka Mamaduka deleted the fix/post-content-previews branch December 28, 2021 17:48
@github-actions github-actions bot added this to the Gutenberg 12.3 milestone Dec 28, 2021
@Mamaduka Mamaduka mentioned this pull request Dec 29, 2021
7 tasks
@adamsilverstein
Copy link
Member

Should the $post_ID be removed from the Post Featured Image as well?
great question @walbo!

The _set_preview only updates title, content, and excerpt, plus thumbnail is stored in post meta, which is not covered by revisions.

@Mamaduka while it is true we don't currently directly support meta revisioning, the specific case of the featured image for post previews was addressed previously in #9151 so we should endeavor to ensure that works correctly with block themes to avoid a regression.

@adamsilverstein
Copy link
Member

I tracked the featured image down to what looks like an existing core issue, although I don't quite understand why the issue is triggered by block themes.

In src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php::create_post_autosave when the autosave sent exactly matches the previous autosave, the controller deletes the old autosave revision, then returns an error "There is nothing to save. The autosave and the post content are the same."

image

Deleting the old autosave revision means that later when src/wp-includes/revision.php::_set_preview calls wp_get_post_autosave the autosave is missing, and the preview data swaps fail.

I suspect this may also be the source of the failure of the title and content to display the preview data, typically core would automatically substitute the autosave data in the preview context (even if you passed the post id in the calls to get the title/content)

I am going to follow up with a trac ticket to fix the autosave removal which I believe is a bug.

In the meantime you can test a fix in Gutenberg by adding back the filter that never gets added in core due to the bug:
add_filter( 'get_post_metadata', '_wp_preview_post_thumbnail_filter', 10, 3 );

@Mamaduka
Copy link
Member Author

Thanks for tracking this down, @adamsilverstein. I wasn't aware of the previous fix.

I suspect this may also be the source of the failure of the title and content to display the preview data, typically core would automatically substitute the autosave data in the preview context (even if you passed the post id in the calls to get the title/content)

I'm not getting any errors when previewing posts without a thumbnail set, so probably title and content issues were unrelated.

@adamsilverstein
Copy link
Member

@walbo & @Mamaduka - regarding featured images in previews, I have a fix proposed on WordPress/wordpress-develop#2092. If you test this, it should show the correct featured image when changing and previewing.

@walbo
Copy link
Member

walbo commented Dec 29, 2021

@walbo & @Mamaduka - regarding featured images in previews, I have a fix proposed on WordPress/wordpress-develop#2092. If you test this, it should show the correct featured image when changing and previewing.

Left a comment on the core ticket. Tested and looks good to me.

@Mamaduka
Copy link
Member Author

Thanks, @adamsilverstein. I also left the comment on the Trac ticket.

I think we should also backport the fix. The plugin has to support WP 5.8 with Block Themes.

@noisysocks noisysocks removed the Backport to WP 6.6 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Jan 4, 2022
noisysocks pushed a commit that referenced this pull request Jan 4, 2022
When inside the main loop, we want to use a queried object to apply `the_preview` for the current post.
We force this behavior by omitting the post ID argument from the `get_the_content` and `get_the_title.`

Co-authored-by: Adam Silverstein <[email protected]>
@webmandesign
Copy link
Contributor

Hi,

I've just came across this issue (checking @adamsilverstein's comment referrence) removing the postId context that was working with Gutenberg 16.2.1.

In my theme I am using Post Title block also for displaying blog page title. I've decided for this so a user doesn't have to edit corresponding template (part) in Site Editor when user just wants to change the blog page title.

For this I'm using render_block_context filter hook to set proper blog page ID as postId context for Post Title block when it is H1 and displayed on the blog page.

Removing the postId context renders the title incorrect and now there is no way for me to modify it in Post Title block.

Maybe I don't know all the reasoning, but in my opinion the context specific PHP functions should stay filterable/modifiable even in blocks.

@Mamaduka
Copy link
Member Author

Hi, @webmandesign

That behavior was a regression introduced in #48001, and it didn't make it a WP release. The expectation is that the Post Title will be used in Query Loop, where the loop block setups all required context and globals for the title block.

I recently saw a request for a similar case - #52668.

There's also a proposal to stop relying on post-globals and only use provided context - #45828.

@webmandesign
Copy link
Contributor

@Mamaduka Thanks for the info.
Well, are there some blocks we can use outside the Query Loop then?

With classic themes we could provide the context for the PHP functions.

@Mamaduka
Copy link
Member Author

The Query Title block works outside the loop, used for displaying titles for archive and search pages. It uses get_the_archive_title under the hood.

@likethegoddess
Copy link

Unfortunately, the Query Title block does not work for me outside the loop on the home page. I'm looking for something similar to the Query Title that uses single_post_title instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Post Content Affects the Post Content Block [Type] Bug An existing feature does not function as intended
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Previewing single post changes frontend doesn't work on block based themes
6 participants