Make WordPress Core

Opened 9 years ago

Last modified 4 weeks ago

#43792 new enhancement

get_comment_excerpt filter should tell if the comment was shorted or not

Reported by: mattkeys Owned by:
Priority: normal Milestone: Awaiting Review
Component: Comments Version:
Severity: normal Keywords: has-patch dev-feedback has-unit-tests
Cc: Focuses:

Description

The design I am currently working on has long comments shortened to 100 words, and a 'show more' / 'show less' link is added.

I only want to add the 'show more' text if the comment has been shortened. There is a filter before get_comment_excerpt() returns that I can use to add my link. However it does not pass along information about whether or not the comment was shortened.

As a workaround I can check if the comment ends in "…", but it would make sense to add a boolean value to the filter arguments to make this easier/cleaner.

Attachments (1)

43792.diff​ (1019 bytes ) - added by mattkeys 9 years ago.

Download all attachments as: .zip

Change History (12)

#1 @mattkeys
9 years ago

  • Keywords has-patch needs-testing dev-feedback added

#2 @joyously
9 years ago

Wouldn't the boolean you added need to be set to something?

And isn't the excerpt and the comment object passed already? So you can just compare the lengths?

#3 @mattkeys
9 years ago

Hi @joyously thanks for looking at this. $use_ellipsis is a boolean already set by the get_comment_excerpt() function that is used to determine if the comment needs to be shortened, I am just passing its value to the filter at the end of the function.

There are workarounds; I mentioned checking for the … at the end of the string as one possible workaround. Your idea of checking both string lengths could work as well. In my opinion both of these ideas are a bit cumbersome, especially if we already have a boolean value at our disposal, we just need to add it to the filter.

#4 @joyously
9 years ago

I understand what you're saying, but adding a parameter means that all the code using the filter will get an error since it will have the wrong number of parameters. So it's not a trivial thing to do, and you should work with the parameters that you already have if you can.

#5 @mattkeys
9 years ago

I don't believe that to be the case, but would be interested to hear more if you are able to reproduce what you are saying.

The argument I added is at the 'end' of the current list of arguments. Anyone who is currently using the filter like so:

add_filter( 'get_comment_excerpt', 'my_comment_filter, 10, 3 );

Will continue to get the first 3 arguments that their add_filter() call specifies. If they want to get the new 4th argument, they would need to update their code to be:

add_filter( 'get_comment_excerpt', 'my_comment_filter, 10, 4 );

I've tested both variations locally with no errors. Let me know if I am missing something.

Version 0, edited 9 years ago by mattkeys (next)

#6 @joyously
9 years ago

I'm sorry, I was mistaken about the parameters. I was thinking of it backwards.

This ticket was mentioned in ​Slack in #core by mattkeys. ​View the logs.


8 years ago

#8 @birgire
8 years ago

Related #44541 where it's suggested to use wp_trim_words() within get_comment_excerpt().

I wonder if this is the way forward, if the same should apply for post excerpts as well, through the wp_trim_words filter:

https://core.trac.wordpress.org/browser/tags/4.9.8/src/wp-includes/formatting.php#L3334

#9 @pento
8 years ago

  • Version trunk

#10 @huzaifaalmesbah
10 months ago

  • Keywords needs-refresh added; needs-testing removed

I attempted to apply the latest patch against current trunk (7.0-alpha-61215-src).

The patch does not apply cleanly:

  • src/wp-includes/comment-template.php (.rej created)

Since the patch cannot be applied, it cannot be tested. The patch appears outdated and needs to be refreshed against current trunk before further testing.

Removing needs-testing and adding needs-refresh.

This ticket was mentioned in ​PR #14794 on ​WordPress/wordpress-develop by ​@irozum.


4 weeks ago
#11

  • Keywords has-unit-tests added; needs-refresh removed

Adds a fourth $is_trimmed argument to the get_comment_excerpt filter so callbacks can tell whether the comment text was shortened to build the excerpt, without checking for a trailing … (which misfires on comments that genuinely end in an ellipsis).

Changes

  • get_comment_excerpt() derives the flag by running wp_trim_words() a second time with an empty $more and comparing the result to the real excerpt. This reuses wp_trim_words()'s own counting rules, including character-based locales, instead of duplicating them.
  • The hook docblock documents the new $is_trimmed parameter with @since 7.2.0.
  • Appending a filter argument is backward compatible: callbacks registered with fewer accepted args are unaffected.
  • This resolves the remaining ask from the dev-feedback discussion on top of the earlier switch to wp_trim_words() (the old patch no longer applies because the $use_ellipsis variable is gone).

Tests

  • New data-provider test covering a long comment, a short comment, a word count equal to and above the limit, and a short comment that ends in an ellipsis.

Known edge case

  • The extra wp_trim_words() call fires the wp_trim_words filter a second time. A third-party filter that discards $more and rebuilds the text could make the flag report false for a trimmed comment.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5.5
Used for: Implementation, tests, and PR description.

---

This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See ​GitHub Pull Requests for Code Review in the Core Handbook for details.

🤖 Generated with ​Claude Code

Note: See TracTickets for help on using tickets.