Date: Tue, 6 May 2025 15:38:38 +0100
Subject: [PATCH 36/42] Highlight content returned without p tags
When the API sends highlighted data back, it's returned without the expected tags that would wrap content.
Let's add those back.
---
.../search/src/inline-search/class-search-highlighter.php | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/projects/packages/search/src/inline-search/class-search-highlighter.php b/projects/packages/search/src/inline-search/class-search-highlighter.php
index 2081f242fc55e..954e2f5d3619a 100644
--- a/projects/packages/search/src/inline-search/class-search-highlighter.php
+++ b/projects/packages/search/src/inline-search/class-search-highlighter.php
@@ -136,7 +136,8 @@ public function filter_highlighted_content( $content ) {
}
if ( ! empty( $this->highlighted_content[ $post_id ]['content'] ) ) {
- return $this->highlighted_content[ $post_id ]['content'];
+ // Apply wpautop to maintain paragraph formatting
+ return wpautop( $this->highlighted_content[ $post_id ]['content'] );
}
return $content;
From 5f74b7922d4092bd8b868375ec34751ec03e5a5f Mon Sep 17 00:00:00 2001
From: Katja Paavola
Date: Thu, 8 May 2025 11:54:13 +0100
Subject: [PATCH 37/42] Cleanup related refactor
---
.../src/inline-search/class-inline-search.php | 34 +++++------
.../class-search-highlighter.php | 61 ++++++-------------
2 files changed, 33 insertions(+), 62 deletions(-)
diff --git a/projects/packages/search/src/inline-search/class-inline-search.php b/projects/packages/search/src/inline-search/class-inline-search.php
index 4eb95e5734e01..7b912542142ef 100644
--- a/projects/packages/search/src/inline-search/class-inline-search.php
+++ b/projects/packages/search/src/inline-search/class-inline-search.php
@@ -26,6 +26,14 @@ class Inline_Search extends Classic_Search {
*/
private $highlighter;
+ /**
+ * The search correction instance.
+ *
+ * @var Inline_Search_Correction|null
+ * @since $$next-version$$
+ */
+ private $correction;
+
/**
* Stores the list of post IDs that are actual search results.
*
@@ -122,7 +130,7 @@ public function filter__posts_pre_query( $posts, $query ) {
}
// Process the search results to extract post IDs and highlighted content.
- $this->process_search_results( $query );
+ $this->process_search_results();
// Create a WP_Query to fetch the actual posts.
$posts_query = $this->create_posts_query( $query );
@@ -458,38 +466,26 @@ public function get_search_result(
/**
* Process search results to extract post IDs and highlighted content.
- *
- * @param \WP_Query $query The original WP_Query.
*/
- private function process_search_results( $query ) {
- $post_ids = array();
- $search_term = $query->get( 's' );
- $corrected_search_term = '';
- $highlighted_results = array();
-
- // Store corrected query if available
- if ( ! empty( $this->search_result['corrected_query'] ) ) {
- $corrected_search_term = $this->search_result['corrected_query'];
- }
+ private function process_search_results() {
+ $post_ids = array();
+ $highlighted_results = array();
foreach ( $this->search_result['results'] as $result ) {
$post_id = (int) ( $result['fields']['post_id'] ?? 0 );
$post_ids[] = $post_id;
- // Store the highlight data keyed by post ID for later use
+ // Collect highlight data for processing
if ( ! empty( $result['highlight'] ) ) {
$highlighted_results[ $post_id ] = $result['highlight'];
}
}
$this->search_result_ids = $post_ids;
+ $this->highlighter = new Search_Highlighter( $post_ids );
- // Initialize the highlighter with search data and highlight results
- $this->highlighter = new Search_Highlighter( $search_term, $corrected_search_term, $post_ids );
-
- // Process results if we have highlight data
if ( ! empty( $highlighted_results ) ) {
- // Process the API highlighted results to prepare them for the highlighter
+ // Format highlight data for the highlighter
$processed_results = array();
foreach ( $highlighted_results as $post_id => $highlight_data ) {
$processed_results[] = array(
diff --git a/projects/packages/search/src/inline-search/class-search-highlighter.php b/projects/packages/search/src/inline-search/class-search-highlighter.php
index 954e2f5d3619a..a13046fbf6414 100644
--- a/projects/packages/search/src/inline-search/class-search-highlighter.php
+++ b/projects/packages/search/src/inline-search/class-search-highlighter.php
@@ -18,20 +18,6 @@ class Search_Highlighter {
*/
private $highlighted_content = array();
- /**
- * Stores the search term used in the query.
- *
- * @var string
- */
- private $search_term;
-
- /**
- * Stores the corrected search term if provided by the API.
- *
- * @var string
- */
- private $corrected_search_term;
-
/**
* Stores the list of post IDs that are actual search results.
*
@@ -42,18 +28,14 @@ class Search_Highlighter {
/**
* Constructor
*
- * @param string $search_term The original search term.
- * @param string $corrected_search_term The corrected search term (if any).
- * @param array $search_result_ids Array of post IDs from search results.
- * @param array $results Optional. The search result data from the API to process immediately.
+ * @param array $search_result_ids Array of post IDs from search results.
+ * @param array $results Optional. The search result data from the API to process immediately.
*/
- public function __construct( $search_term = '', $corrected_search_term = '', $search_result_ids = array(), $results = null ) {
- $this->search_term = $search_term;
- $this->corrected_search_term = $corrected_search_term;
- $this->search_result_ids = $search_result_ids;
- $this->highlighted_content = array();
+ public function __construct( $search_result_ids = array(), $results = null ) {
+ $this->search_result_ids = $search_result_ids;
+ $this->highlighted_content = array();
- // Process results immediately if provided
+ // Process API results immediately if provided
if ( $results !== null ) {
$this->process_results( $results );
}
@@ -87,16 +69,12 @@ public function process_results( $results ) {
}
/**
- * Update search terms and result IDs.
+ * Update search result IDs.
*
- * @param string $search_term The original search term.
- * @param string $corrected_search_term The corrected search term (if any).
- * @param array $search_result_ids Array of post IDs from search results.
+ * @param array $search_result_ids Array of post IDs from search results.
*/
- public function update_search_data( $search_term, $corrected_search_term = '', $search_result_ids = array() ) {
- $this->search_term = $search_term;
- $this->corrected_search_term = $corrected_search_term;
- $this->search_result_ids = $search_result_ids;
+ public function update_search_data( $search_result_ids = array() ) {
+ $this->search_result_ids = $search_result_ids;
}
/**
@@ -107,12 +85,10 @@ public function update_search_data( $search_term, $corrected_search_term = '', $
* @return string The filtered title.
*/
public function filter_highlighted_title( $title, $post_id ) {
- // Only process if this is one of our search results
if ( ! $this->is_search_result( $post_id ) ) {
return $title;
}
- // Check if we have a highlighted title from the API
if ( ! empty( $this->highlighted_content[ $post_id ]['title'] ) ) {
return $this->highlighted_content[ $post_id ]['title'];
}
@@ -127,16 +103,14 @@ public function filter_highlighted_title( $title, $post_id ) {
* @return string The filtered content.
*/
public function filter_highlighted_content( $content ) {
- // Get current post ID
$post_id = get_the_ID();
- // Only process if this is one of our search results
if ( ! $this->is_search_result( $post_id ) ) {
return $content;
}
if ( ! empty( $this->highlighted_content[ $post_id ]['content'] ) ) {
- // Apply wpautop to maintain paragraph formatting
+ // Apply wpautop to maintain paragraph formatting.
return wpautop( $this->highlighted_content[ $post_id ]['content'] );
}
@@ -150,14 +124,12 @@ public function filter_highlighted_content( $content ) {
* @return string The filtered comment text.
*/
public function filter_highlighted_comment( $comment_text ) {
- // Only process if this is one of our search results and we're in a search context
if ( ! is_search() || ! in_the_loop() ) {
return $comment_text;
}
$post_id = get_the_ID();
- // Check if this post is a search result and we have highlighted comments for it
if ( ! $this->is_search_result( $post_id ) || empty( $this->highlighted_content[ $post_id ]['comments'] ) ) {
return $comment_text;
}
@@ -176,7 +148,6 @@ private function process_result_highlighting( $result, $post_id ) {
return;
}
- // Check for data in various highlight field formats.
$title = $this->extract_highlight_field( $result, 'title' );
$content = $this->extract_highlight_field( $result, 'content' );
$comments = $this->extract_highlight_field( $result, 'comments' );
@@ -196,10 +167,14 @@ private function process_result_highlighting( $result, $post_id ) {
* @return string The extracted highlighted field.
*/
private function extract_highlight_field( $result, $field ) {
- // Try all possible field variants in order of likelihood
+ // Try exact match first
+ if ( isset( $result['highlight'][ $field ] ) && is_array( $result['highlight'][ $field ] ) && ! empty( $result['highlight'][ $field ] ) ) {
+ return $result['highlight'][ $field ][0];
+ }
+
+ // Try field variants with suffixes (e.g., 'title.default')
foreach ( $result['highlight'] as $key => $value ) {
- // Check if this key is for our requested field (exact match or with suffix)
- if ( $key === $field || strpos( $key, $field . '.' ) === 0 ) {
+ if ( strpos( $key, $field . '.' ) === 0 ) {
if ( is_array( $value ) && ! empty( $value ) ) {
return $value[0];
}
From 619c72a96a2870cab0734cb36755ffc84856c37d Mon Sep 17 00:00:00 2001
From: Katja Paavola
Date: Thu, 8 May 2025 14:50:49 +0100
Subject: [PATCH 38/42] changelog
---
projects/packages/search/changelog/add-inline-search-branding | 4 ++++
projects/plugins/jetpack/changelog/add-inline-search-branding | 4 ++++
projects/plugins/search/changelog/add-inline-search-branding | 4 ++++
3 files changed, 12 insertions(+)
create mode 100644 projects/packages/search/changelog/add-inline-search-branding
create mode 100644 projects/plugins/jetpack/changelog/add-inline-search-branding
create mode 100644 projects/plugins/search/changelog/add-inline-search-branding
diff --git a/projects/packages/search/changelog/add-inline-search-branding b/projects/packages/search/changelog/add-inline-search-branding
new file mode 100644
index 0000000000000..3630b8beef243
--- /dev/null
+++ b/projects/packages/search/changelog/add-inline-search-branding
@@ -0,0 +1,4 @@
+Significance: minor
+Type: added
+
+Inline Search: add Jetpack Branding to search results.
diff --git a/projects/plugins/jetpack/changelog/add-inline-search-branding b/projects/plugins/jetpack/changelog/add-inline-search-branding
new file mode 100644
index 0000000000000..90ef0e14a70cf
--- /dev/null
+++ b/projects/plugins/jetpack/changelog/add-inline-search-branding
@@ -0,0 +1,4 @@
+Significance: minor
+Type: enhancement
+
+add-inline-search-branding
diff --git a/projects/plugins/search/changelog/add-inline-search-branding b/projects/plugins/search/changelog/add-inline-search-branding
new file mode 100644
index 0000000000000..3630b8beef243
--- /dev/null
+++ b/projects/plugins/search/changelog/add-inline-search-branding
@@ -0,0 +1,4 @@
+Significance: minor
+Type: added
+
+Inline Search: add Jetpack Branding to search results.
From b624e6ec61d4e83b64bbbd7453289deeea7cf3b3 Mon Sep 17 00:00:00 2001
From: Katja Paavola
Date: Thu, 8 May 2025 17:11:24 +0100
Subject: [PATCH 39/42] Have the Search Correction class use our abstracted
class methods
---
.../class-inline-search-correction.php | 70 ++-----------------
1 file changed, 7 insertions(+), 63 deletions(-)
diff --git a/projects/packages/search/src/inline-search/class-inline-search-correction.php b/projects/packages/search/src/inline-search/class-inline-search-correction.php
index b76049f609d64..2d3d74bea4de1 100644
--- a/projects/packages/search/src/inline-search/class-inline-search-correction.php
+++ b/projects/packages/search/src/inline-search/class-inline-search-correction.php
@@ -7,21 +7,19 @@
namespace Automattic\Jetpack\Search;
-use Automattic\Jetpack\Assets;
-
/**
* Class for handling search correction display
*
* @since $$next-version$$
*/
-class Inline_Search_Correction {
+class Inline_Search_Correction extends Inline_Search_Component {
/**
* Setup hooks for displaying corrected query notice.
*
* @param \WP_Query $query The current query.
*/
public function setup_corrected_query_hooks( $query ) {
- if ( ! $query->is_search() || ! $query->is_main_query() ) {
+ if ( ! $this->is_valid_search_query( $query ) ) {
return;
}
@@ -42,8 +40,8 @@ public function enqueue_styles() {
return;
}
- $handle = 'jetpack-search-inline-corrected-query';
- $this->register_corrected_query_style( $handle );
+ $handle = self::SCRIPT_HANDLE . '-corrected-query';
+ $this->register_component_style( $handle, 'corrected-query.css' );
}
/**
@@ -57,7 +55,7 @@ public function register_corrected_query_script() {
return;
}
- $handle = 'jetpack-search-inline-corrected-query';
+ $handle = self::SCRIPT_HANDLE . '-corrected-query';
// Don't localize if already localized to prevent duplication
if ( wp_script_is( $handle, 'data' ) ) {
@@ -67,19 +65,10 @@ public function register_corrected_query_script() {
}
}
- Assets::register_script(
- $handle,
- 'build/inline-search/jp-search-inline.js',
- Package::get_installed_path() . '/src',
- array(
- 'in_footer' => true,
- 'textdomain' => 'jetpack-search-pkg',
- 'enqueue' => true,
- )
- );
+ $this->register_inline_search_script();
wp_localize_script(
- $handle,
+ self::SCRIPT_HANDLE,
'JetpackSearchCorrectedQuery',
array(
'html' => $corrected_query_html,
@@ -91,41 +80,6 @@ public function register_corrected_query_script() {
);
}
- /**
- * Register and enqueue theme-specific styles for corrected query.
- *
- * @since $$next-version$$
- * @param string $handle The script handle to use for the stylesheet.
- */
- private function register_corrected_query_style( $handle ) {
- $css_path = 'build/inline-search/';
- $css_file = 'corrected-query.css';
- $full_css_path = $css_path . $css_file;
- $package_path = Package::get_installed_path();
- $css_full_path = $package_path . '/' . $full_css_path;
-
- // Verify the CSS file exists before trying to enqueue it
- if ( ! file_exists( $css_full_path ) ) {
- return;
- }
-
- // We need to use plugins_url for reliable URL generation
- $file_url = plugins_url(
- $full_css_path,
- $package_path . '/package.json'
- );
-
- // Use the file's modification time for more precise cache busting
- $file_version = file_exists( $css_full_path ) ? filemtime( $css_full_path ) : Package::VERSION;
-
- wp_enqueue_style(
- $handle,
- $file_url,
- array(),
- $file_version // Use file modification time for cache busting
- );
- }
-
/**
* Replaces the search query with the corrected query in the title.
*
@@ -191,14 +145,4 @@ private function get_corrected_query_html() {
$message
);
}
-
- /**
- * Get the search result from the Inline_Search instance.
- *
- * @return array|\WP_Error|null The search result or null if not available.
- */
- private function get_search_result() {
- $inline_search = Inline_Search::instance();
- return $inline_search->get_search_result();
- }
}
From 595b5c3a750434d224c50d7721b1174ec4b82413 Mon Sep 17 00:00:00 2001
From: Katja Paavola
Date: Thu, 8 May 2025 17:15:08 +0100
Subject: [PATCH 40/42] Fix linter notices
---
.../packages/search/src/inline-search/styles/colophon.scss | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/projects/packages/search/src/inline-search/styles/colophon.scss b/projects/packages/search/src/inline-search/styles/colophon.scss
index d6289533c3937..ae822c9667000 100644
--- a/projects/packages/search/src/inline-search/styles/colophon.scss
+++ b/projects/packages/search/src/inline-search/styles/colophon.scss
@@ -65,7 +65,9 @@ $colophon-logo-margin-right: 4px;
.wp-theme-varia-wpcom,
.wp-theme-pubvaria {
+
@media only screen and (min-width: 768px) {
+
.jetpack-search-inline-colophon {
margin: 0 auto;
max-width: calc(782px - 32px);
@@ -88,6 +90,7 @@ $colophon-logo-margin-right: 4px;
}
}
}
+
.wp-theme-twentynineteen,
.wp-theme-pubtwentynineteen {
@@ -115,16 +118,18 @@ $colophon-logo-margin-right: 4px;
}
.wp-theme-generatepress {
+
.jetpack-search-inline-colophon {
display: block;
margin-top: 2rem;
clear: both;
}
}
+
.wp-theme-hevor {
+
.jetpack-search-inline-colophon {
justify-content: flex-start;
max-width: var(--wp--style--global--wide-size);
}
}
-
From 0da3a8709328330d00fa972b2ffe91915b4c9c3f Mon Sep 17 00:00:00 2001
From: Katja Paavola
Date: Fri, 9 May 2025 15:59:45 +0100
Subject: [PATCH 41/42] Rename highlighter class to reflect Inline Search
---
...ch-highlighter.php => class-inline-search-highlighter.php} | 2 +-
.../packages/search/src/inline-search/class-inline-search.php | 4 ++--
2 files changed, 3 insertions(+), 3 deletions(-)
rename projects/packages/search/src/inline-search/{class-search-highlighter.php => class-inline-search-highlighter.php} (99%)
diff --git a/projects/packages/search/src/inline-search/class-search-highlighter.php b/projects/packages/search/src/inline-search/class-inline-search-highlighter.php
similarity index 99%
rename from projects/packages/search/src/inline-search/class-search-highlighter.php
rename to projects/packages/search/src/inline-search/class-inline-search-highlighter.php
index a13046fbf6414..489ade9fae6aa 100644
--- a/projects/packages/search/src/inline-search/class-search-highlighter.php
+++ b/projects/packages/search/src/inline-search/class-inline-search-highlighter.php
@@ -10,7 +10,7 @@
/**
* Search Highlighter class
*/
-class Search_Highlighter {
+class Inline_Search_Highlighter {
/**
* Stores highlighted content from search results.
*
diff --git a/projects/packages/search/src/inline-search/class-inline-search.php b/projects/packages/search/src/inline-search/class-inline-search.php
index 409f5a005b980..9d1dd132987d8 100644
--- a/projects/packages/search/src/inline-search/class-inline-search.php
+++ b/projects/packages/search/src/inline-search/class-inline-search.php
@@ -21,7 +21,7 @@ class Inline_Search extends Classic_Search {
/**
* The Search Highlighter instance.
*
- * @var Search_Highlighter|null
+ * @var Inline_Search_Highlighter|null
* @since $$next-version$$
*/
private $highlighter;
@@ -495,7 +495,7 @@ private function process_search_results() {
}
$this->search_result_ids = $post_ids;
- $this->highlighter = new Search_Highlighter( $post_ids );
+ $this->highlighter = new Inline_Search_Highlighter( $post_ids );
if ( ! empty( $highlighted_results ) ) {
// Format highlight data for the highlighter
From f49a2e633943018a9c7bb30f9717b8fe1503e635 Mon Sep 17 00:00:00 2001
From: Katja Paavola
Date: Fri, 9 May 2025 16:02:44 +0100
Subject: [PATCH 42/42] Fix tests
---
.../search/tests/php/Inline_Search_Test.php | 45 ++++++++++---------
1 file changed, 24 insertions(+), 21 deletions(-)
diff --git a/projects/packages/search/tests/php/Inline_Search_Test.php b/projects/packages/search/tests/php/Inline_Search_Test.php
index fd89e47854152..ff59916896ece 100644
--- a/projects/packages/search/tests/php/Inline_Search_Test.php
+++ b/projects/packages/search/tests/php/Inline_Search_Test.php
@@ -104,13 +104,13 @@ public static function data_provider(): array {
'post_type' => 'any',
),
'expected_api_args' => array(
- 'size' => 5,
- 'from' => 0,
- 'fields' => array( 'post_id' ),
- 'query' => 'hello_world',
- 'sort' => 'score_recency',
- 'langs' => array( 'en_US' ),
- 'filter' => array(
+ 'size' => '5',
+ 'from' => '0',
+ 'fields' => array( 'post_id' ),
+ 'query' => 'hello_world',
+ 'sort' => 'score_recency',
+ 'langs' => array( 'en_US' ),
+ 'filter' => array(
'bool' => array(
'must' => array(
array(
@@ -121,6 +121,7 @@ public static function data_provider(): array {
),
),
),
+ 'highlight' => array( 'fields' => array( 'post_title', 'post_content' ) ),
),
),
'only_posts' => array(
@@ -130,13 +131,13 @@ public static function data_provider(): array {
'post_type' => 'post',
),
'expected_api_args' => array(
- 'size' => 5,
- 'from' => 0,
- 'fields' => array( 'post_id' ),
- 'query' => 'only search posts',
- 'sort' => 'score_recency',
- 'langs' => array( 'en_US' ),
- 'filter' => array(
+ 'size' => '5',
+ 'from' => '0',
+ 'fields' => array( 'post_id' ),
+ 'query' => 'only search posts',
+ 'sort' => 'score_recency',
+ 'langs' => array( 'en_US' ),
+ 'filter' => array(
'bool' => array(
'must' => array(
array(
@@ -147,6 +148,7 @@ public static function data_provider(): array {
),
),
),
+ 'highlight' => array( 'fields' => array( 'post_title', 'post_content' ) ),
),
),
'sort_by_date_asc' => array(
@@ -158,13 +160,13 @@ public static function data_provider(): array {
'orderby' => 'date',
),
'expected_api_args' => array(
- 'size' => 5,
- 'from' => 0,
- 'fields' => array( 'post_id' ),
- 'query' => 'search by date descending',
- 'sort' => 'date_asc',
- 'langs' => array( 'en_US' ),
- 'filter' => array(
+ 'size' => '5',
+ 'from' => '0',
+ 'fields' => array( 'post_id' ),
+ 'query' => 'search by date descending',
+ 'sort' => 'date_asc',
+ 'langs' => array( 'en_US' ),
+ 'filter' => array(
'bool' => array(
'must' => array(
array(
@@ -175,6 +177,7 @@ public static function data_provider(): array {
),
),
),
+ 'highlight' => array( 'fields' => array( 'post_title', 'post_content' ) ),
),
),
);