Skip to content

Commit f99ca99

Browse files
gibrownclaude
andcommitted
Search/Sync: fix feature flag option read in Sync, add sanitize callbacks, fix SSE abort race
- Fix 1: Sync module now passes the WP option value as the filter default in both add_ai_answer_post_types() and add_ai_answer_post_meta(), so enabling via get_option() actually enables sync (was always passing false before). - Fix 2: Add test_ai_cpts_in_whitelist_when_option_set() to cover the common option-only enablement path that would have caught Fix 1. - Fix 3: Add sanitize_callback to _jstopic_keywords (sanitize_text_field) and _jstopic_url (esc_url_raw) register_post_meta() calls. - Fix 4: Capture local controller reference before the fetchEventSource call so the .catch() handler checks the correct (now-finished) AbortController signal rather than the replacement controller from any subsequent request. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent d7943e7 commit f99ca99

4 files changed

Lines changed: 26 additions & 12 deletions

File tree

projects/packages/search/src/class-ai-answers.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ public function register_post_types() {
6666
self::TOPIC_CPT,
6767
'_jstopic_keywords',
6868
array(
69-
'single' => true,
70-
'type' => 'string',
71-
'show_in_rest' => true,
72-
'auth_callback' => function () {
69+
'single' => true,
70+
'type' => 'string',
71+
'show_in_rest' => true,
72+
'sanitize_callback' => 'sanitize_text_field',
73+
'auth_callback' => function () {
7374
return current_user_can( 'edit_posts' );
7475
},
7576
)
@@ -79,10 +80,11 @@ public function register_post_types() {
7980
self::TOPIC_CPT,
8081
'_jstopic_url',
8182
array(
82-
'single' => true,
83-
'type' => 'string',
84-
'show_in_rest' => true,
85-
'auth_callback' => function () {
83+
'single' => true,
84+
'type' => 'string',
85+
'show_in_rest' => true,
86+
'sanitize_callback' => 'esc_url_raw',
87+
'auth_callback' => function () {
8688
return current_user_can( 'edit_posts' );
8789
},
8890
)

projects/packages/search/src/instant-search/components/search-app.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ class SearchApp extends Component {
266266
this.aiController.abort();
267267
}
268268
this.aiController = new AbortController();
269+
const controller = this.aiController; // local capture to avoid race in .catch()
269270

270271
this.setState( { aiStatus: 'loading', aiText: '', aiCitations: [] } );
271272

@@ -282,7 +283,7 @@ class SearchApp extends Component {
282283
filters: this.props.filters,
283284
locale: options.locale || 'en',
284285
} ),
285-
signal: this.aiController.signal,
286+
signal: controller.signal,
286287
onopen: async response => {
287288
if ( ! response.ok ) {
288289
throw new Error( `HTTP ${ response.status }` );
@@ -310,7 +311,7 @@ class SearchApp extends Component {
310311
throw new Error( 'SSE error' );
311312
},
312313
} ).catch( () => {
313-
if ( ! this.aiController?.signal?.aborted ) {
314+
if ( ! controller.signal.aborted ) {
314315
this.setState( { aiStatus: 'error' } );
315316
}
316317
} );

projects/packages/sync/src/modules/class-search.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,7 +1817,7 @@ public function add_search_options_whitelist( $list ) {
18171817
* @return array Updated whitelist.
18181818
*/
18191819
public function add_ai_answer_post_types( $list ) {
1820-
if ( ! apply_filters( 'jetpack_search_ai_answers_enabled', false ) ) {
1820+
if ( ! apply_filters( 'jetpack_search_ai_answers_enabled', (bool) get_option( 'jetpack_search_ai_answers_enabled', false ) ) ) {
18211821
return $list;
18221822
}
18231823
$list[] = 'jp_search_behavior';
@@ -1832,7 +1832,7 @@ public function add_ai_answer_post_types( $list ) {
18321832
* @return array Updated whitelist.
18331833
*/
18341834
public function add_ai_answer_post_meta( $list ) {
1835-
if ( ! apply_filters( 'jetpack_search_ai_answers_enabled', false ) ) {
1835+
if ( ! apply_filters( 'jetpack_search_ai_answers_enabled', (bool) get_option( 'jetpack_search_ai_answers_enabled', false ) ) ) {
18361836
return $list;
18371837
}
18381838
$list[] = '_jstopic_keywords';

projects/packages/sync/tests/php/modules/Search_Module_Test.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,15 @@ public function test_ai_topic_meta_in_whitelist_when_search_enabled() {
7272
$this->assertContains( '_jstopic_keywords', $list );
7373
$this->assertContains( '_jstopic_url', $list );
7474
}
75+
76+
/**
77+
* AI Answer CPTs should be in the whitelist when enabled via WP option only (no filter).
78+
*/
79+
public function test_ai_cpts_in_whitelist_when_option_set() {
80+
update_option( 'jetpack_search_ai_answers_enabled', 1 );
81+
$list = apply_filters( 'jetpack_sync_post_types_whitelist', array() );
82+
delete_option( 'jetpack_search_ai_answers_enabled' );
83+
$this->assertContains( 'jp_search_behavior', $list );
84+
$this->assertContains( 'jetpack_search_topic', $list );
85+
}
7586
}

0 commit comments

Comments
 (0)