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

[Block] Display paragraph breaks in comment contents block. #40667

Merged
merged 7 commits into from
Apr 28, 2022

Conversation

peterwilsoncc
Copy link
Contributor

What?

Fixes #40248.

Apply comment_text filter in comment template to ensure paragraph breaks, smilies, links, etc are displayed.

Why?

Ensure comments in a block theme are displayed with the original paragraph breaks included during authoring. Presently the content is shown as one blob which can become difficult to read.

How?

Traditional WP themes use comment_template() to echo the comment content.

function comment_text( $comment_ID = 0, $args = array() ) {
	$comment = get_comment( $comment_ID );

	$comment_text = get_comment_text( $comment, $args );
	/**
	 * Filters the text of a comment to be displayed.
	 *
	 * @since 1.2.0
	 *
	 * @see Walker_Comment::comment()
	 *
	 * @param string          $comment_text Text of the current comment.
	 * @param WP_Comment|null $comment      The comment object. Null if not found.
	 * @param array           $args         An array of arguments.
	 */
	echo apply_filters( 'comment_text', $comment_text, $comment, $args );
}

The final echo statement rules out it's use for rendering the template block but applying the same filter ensures parity in display between the comment loop and comment template.

Testing Instructions

  1. Test using WP 5.9 (trunk will use the previously merged functions)
  2. Enable 2022 with a block theme using the comment query loop
  3. Checkout gutenberg trunk
  4. Leave a comment with two or more paragraphs
  5. View the comment on the front end, observe it's one blob of text.
  6. Switch to this branch
  7. Reload the front end and your paragraphs will be visible once again.

Screenshots or screencast

Before
Screen Shot 2022-04-28 at 12 13 00 pm

After
Screen Shot 2022-04-28 at 12 13 29 pm

@peterwilsoncc peterwilsoncc added the [Block] Comments Affects the Comments Block - formerly known as Comments Query Loop label Apr 28, 2022
Comment on lines +141 to +142
str_replace( array( "\n", "\t" ), '', '<ol ><li id="comment-' . self::$comment_ids[0] . '" class="comment even thread-even depth-1"><div class="wp-block-comment-author-name"><a rel="external nofollow ugc" href="http://example.com/author-url/" target="_self" >Test</a></div><div class="wp-block-comment-content"><p>Hello world</p></div></li></ol>' ),
str_replace( array( "\n", "\t" ), '', gutenberg_render_block_core_comment_template( null, null, $block ) )
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Real talk: Even though I wrote it, I don't know how I feel about this. autop puts in some weird line breaks that I want to ignore but a line break is white space rather than an empty string.

@paaljoachim
Copy link
Contributor

paaljoachim commented Apr 28, 2022

I assume we should backport the fix to WP 6.0 beta.

@paaljoachim paaljoachim added 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 Apr 28, 2022
@cbravobernal cbravobernal added the [Type] Bug An existing feature does not function as intended label Apr 28, 2022
Copy link
Contributor

@cbravobernal cbravobernal left a comment

Choose a reason for hiding this comment

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

Worked like a charm!

@cbravobernal cbravobernal merged commit 806a742 into trunk Apr 28, 2022
@cbravobernal cbravobernal deleted the fix/40248-comment-paragraphs branch April 28, 2022 08:28
@github-actions github-actions bot added this to the Gutenberg 13.2 milestone Apr 28, 2022
@adamziel
Copy link
Contributor

adamziel commented Apr 29, 2022

@c4rl0sbr4v0 @peterwilsoncc @ockham @paaljoachim a corresponding PR against wordpress-develop will be needed to include these unit tests in WordPress 6.0. Is it something you want? If so, would you please create one?

adamziel pushed a commit that referenced this pull request Apr 29, 2022
* Apply `comment_text` filter in comment template.

* I think this is how I am supposed to test this.

* Fix tests paragraphs break/I broke.

* CS Fix.

* Tidy assertions even more.

* discard discardWhiteSpace — it was a bad idea.

* Thinking face.
@adamziel
Copy link
Contributor

I cherry picked this change into wp/6.0 branch to be included in WordPress 6.0 Beta 4 on Monday: 379461d

@adamziel adamziel 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 Apr 29, 2022
@ockham
Copy link
Contributor

ockham commented Apr 29, 2022

@c4rl0sbr4v0 @peterwilsoncc @ockham @paaljoachim a corresponding PR against wordpress-develop will be needed to include these unit tests in WordPress 6.0. Is it something you want? If so, would you please create one?

@DAreRodz kindly took care of this: WordPress/wordpress-develop#2649

@gziolo
Copy link
Member

gziolo commented Apr 29, 2022

Awesome, I guess we will have merge those changes into WordPress/wordpress-develop#2651 to get green checks on CI 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Comments Affects the Comments Block - formerly known as Comments Query Loop [Type] Bug An existing feature does not function as intended
6 participants