Skip to content

Commit e3a886f

Browse files
committed
Fix excerpt output on search
1 parent bfbce87 commit e3a886f

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

wp-content/themes/humanity-theme/includes/features/search/post-excerpt.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,37 @@ function get_first_paragraph( string $html ): string {
1515
return '';
1616
}
1717

18-
$doc = new DOMDocument();
18+
$cache_key = md5( $html );
19+
$cached = wp_cache_get( $cache_key );
20+
21+
if ( $cached ) {
22+
return $cached;
23+
}
24+
25+
$doc = new DOMDocument( '1.0', 'utf-8' );
1926

2027
// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
2128
$doc->formatOutput = false;
2229
$doc->substituteEntities = false;
23-
// phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
30+
$doc->preserveWhiteSpace = true;
31+
$doc->validateOnParse = false;
32+
33+
// ensure string is utf8
34+
$encoded_content = mb_convert_encoding( $html, 'UTF-8' );
35+
// encode everything
36+
$encoded_content = htmlentities( $encoded_content, ENT_NOQUOTES, 'UTF-8' );
37+
// decode "standard" characters
38+
$encoded_content = htmlspecialchars_decode( $encoded_content, ENT_NOQUOTES );
39+
// convert left side of ISO-8859-1 to HTML numeric character reference
40+
// this was taken from PHP docs for mb_encode_numericentity vvvvvvvvvvvvvvvvvvvvvvvvv
41+
$encoded_content = mb_encode_numericentity( $encoded_content, [ 0x80, 0x10FFFF, 0, ~0 ], 'UTF-8' );
2442

2543
libxml_use_internal_errors( true );
2644
$doc->loadHTML(
27-
$html,
28-
LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOXMLDECL | LIBXML_NOERROR | LIBXML_NOWARNING
45+
'<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></head><body>' .
46+
$encoded_content .
47+
'</body>',
48+
LIBXML_HTML_NODEFDTD | LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NOENT
2949
);
3050
libxml_use_internal_errors( false );
3151

@@ -39,7 +59,10 @@ function get_first_paragraph( string $html ): string {
3959
continue;
4060
}
4161

42-
return wp_kses_post( sprintf( '<p>%s</p>', $innards ) );
62+
$output = sprintf( '<p>%s</p>', $innards );
63+
64+
wp_cache_add( $cache_key, $output );
65+
return wp_kses_post( $output );
4366
}
4467

4568
return '';

wp-content/themes/humanity-theme/patterns/search.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
<!-- wp:group {"tagName":"div","className":"post-excerpt wp-block-post-excerpt"} -->
112112
<div class="wp-block-group post-excerpt wp-block-post-excerpt">
113113
<!-- wp:paragraph {"className":"wp-block-post-excerpt__excerpt"} -->
114-
<p class="wp-block-paragraph wp-block-post-excerpt__excerpt"><?php echo wp_kses_post( get_the_excerpt() ); ?></p>
114+
<p class="wp-block-paragraph wp-block-post-excerpt__excerpt"><?php echo wp_kses_post( get_first_paragraph( get_the_content() ) ); ?></p>
115115
<!-- /wp:paragraph -->
116116
</div>
117117
<!-- /wp:group -->

0 commit comments

Comments
 (0)