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

Modernize ep_highlight_excerpt to deal with block content #3867

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions includes/classes/Feature/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,24 +285,31 @@ public function allow_excerpt_html() {
* @return string $text the new excerpt
*/
public function ep_highlight_excerpt( $text, $post ) {

$settings = $this->get_settings();

// reproduces wp_trim_excerpt filter, preserving the excerpt_more and excerpt_length filters
if ( '' === $text ) {
$text = get_the_content( '', false, $post );

$text = strip_shortcodes( $text );
$text = excerpt_remove_blocks( $text );

$text = apply_filters( 'the_content', $text );
$text = str_replace( '\]\]\>', ']]>', $text );

$text = strip_tags( $text, '<' . esc_html( $settings['highlight_tag'] ) . '>' );

// use the defined length, if already applied...
$excerpt_length = apply_filters( 'excerpt_length', 55 );

// use defined excerpt_more filter if it is used
$excerpt_more = apply_filters( 'excerpt_more', $text );

$excerpt_more = $excerpt_more !== $text ? $excerpt_more : '[&hellip;]';

/**
* WordPress would handle this using `wp_trim_words` but that removes all tags,
* including the one used to highlight the search term.
*/
$words = explode( ' ', $text, $excerpt_length + 1 );
if ( count( $words ) > $excerpt_length ) {
array_pop( $words );
Expand Down
Loading