Skip to content

Inline Search: Return when the_title filter is called with the incorrect number of parameters #43511

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

Merged
merged 11 commits into from
May 27, 2025
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Protect against improper calls to the_title() filter.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public function __construct( array $search_result_ids = array(), ?array $results
* Set up the WordPress filters for highlighting.
*/
public function setup(): void {
// phpcs:ignore Squiz.PHP.CommentedOutCode.Found -- Temporarily disabled due to incompatibility with P2 theme
// add_filter( 'the_title', array( $this, 'filter_highlighted_title' ), 10, 2 );
add_filter( 'the_title', array( $this, 'filter_highlighted_title' ), 10, 2 );
add_filter( 'the_excerpt', array( $this, 'filter_highlighted_excerpt' ) );
add_filter( 'render_block_core/post-excerpt', array( $this, 'filter_render_highlighted_block' ), 10, 3 );
}
Expand All @@ -72,12 +71,18 @@ public function process_results( array $results ): void {
/**
* Filter the post title to show highlighted version.
*
* @param string $title The post title.
* @param int $post_id The post ID.
* @param string $title The post title.
* @param int|null $post_id Optional. The post ID. Default null.
*
* @return string The filtered title.
*/
public function filter_highlighted_title( string $title, int $post_id ): string {
public function filter_highlighted_title( string $title, ?int $post_id = null ): string {
// o2 is currently rendering <mark> tags in post titles, so we need to return the original.
$body_class = get_body_class();
if ( is_array( $body_class ) && in_array( 'o2', $body_class, true ) ) {
return $title;
}

Comment on lines +81 to +85
Copy link
Contributor

@valterlorran valterlorran May 21, 2025

Choose a reason for hiding this comment

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

Nit: Since get_body_class() ultimately returns array_unique, we might be able to simplify this check.

		if ( in_array( 'o2', $body_class, true ) ) {
			return $title;
		}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a good point, and I've given it some thought. I'm going to keep it as is, just in case there's a something out there filtering body_class and not returning an array. I appreciate that this is probably overly defensive, but also this PR exists, so there we are. :)

if ( ! $this->is_search_result( $post_id ) ) {
return $title;
}
Expand Down Expand Up @@ -163,11 +168,16 @@ private function extract_highlight_field( array $result, string $field ): string
/**
* Check if the current post is a search result from our API
*
* @param int $post_id The post ID to check.
* @param int|null $post_id The post ID to check.
*
* @return bool Whether the post is a search result.
*/
public function is_search_result( int $post_id ): bool {
public function is_search_result( ?int $post_id ): bool {
// o2 is initially returning null due to mishandling of the_title() filter.
if ( null === $post_id ) {
return false;
}

return is_search() && in_the_loop() && ! empty( $this->search_result_ids ) && in_array( $post_id, $this->search_result_ids, true );
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: bugfix

Protect against improper calls to the_title() filter.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Protect against improper calls to the_title() filter.
Loading