From 44cc4f56a326d8c03e3725a3b3369d4ada4b4c97 Mon Sep 17 00:00:00 2001 From: Terence Ong Date: Wed, 8 Apr 2026 11:02:28 +0700 Subject: [PATCH] fix: add second-pass nbsp cleanup directly in get_articles() The strip_source_suffix() fallbacks aren't catching all nbsp-separated publisher names. Adding a direct strrpos-based cleanup pass in the get_articles() method itself as a safety net. This operates on the already-decoded text returned by strip_source_suffix(). Co-Authored-By: Claude Opus 4.6 --- includes/class-peptide-news-rest-api.php | 30 ++++++++++++++----- .../includes/class-peptide-news-rest-api.php | 30 ++++++++++++++----- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/includes/class-peptide-news-rest-api.php b/includes/class-peptide-news-rest-api.php index 367bf7c..c426b9a 100644 --- a/includes/class-peptide-news-rest-api.php +++ b/includes/class-peptide-news-rest-api.php @@ -146,14 +146,30 @@ class Peptide_News_Rest_API { $offset ) ); - // Strip source name from titles and summaries (e.g. "Article Title - Toronto Star"). + // Strip source name from titles, excerpts, and summaries. + $nbsp = "\xC2\xA0"; // UTF-8 non-breaking space. foreach ( $articles as $article ) { - $article->title = $this->strip_source_suffix( $article->title, $article->source, $article->source_url ); - if ( ! empty( $article->ai_summary ) ) { - $article->ai_summary = $this->strip_source_suffix( $article->ai_summary, $article->source, $article->source_url ); - } - if ( ! empty( $article->excerpt ) ) { - $article->excerpt = $this->strip_source_suffix( $article->excerpt, $article->source, $article->source_url ); + $fields = array( 'title', 'excerpt', 'ai_summary' ); + foreach ( $fields as $field ) { + if ( empty( $article->$field ) ) { + continue; + } + // First pass: use strip_source_suffix for known patterns. + $article->$field = $this->strip_source_suffix( $article->$field, $article->source, $article->source_url ); + + // Second pass: catch any remaining nbsp-separated publisher names + // (e.g., "...text\xC2\xA0\xC2\xA0Publisher Name" from Google News RSS). + $text = $article->$field; + $pos = strrpos( $text, $nbsp . $nbsp ); + if ( false === $pos ) { + $pos = strrpos( $text, $nbsp ); + } + if ( false !== $pos && $pos > 10 ) { + $candidate = rtrim( substr( $text, 0, $pos ) ); + if ( strlen( $candidate ) > strlen( $text ) * 0.3 ) { + $article->$field = $candidate; + } + } } } diff --git a/peptide-news-plugin/includes/class-peptide-news-rest-api.php b/peptide-news-plugin/includes/class-peptide-news-rest-api.php index e4e3d97..0db7b59 100644 --- a/peptide-news-plugin/includes/class-peptide-news-rest-api.php +++ b/peptide-news-plugin/includes/class-peptide-news-rest-api.php @@ -143,14 +143,30 @@ class Peptide_News_Rest_API { $offset ) ); - // Strip source name from titles and summaries (e.g. "Article Title - Toronto Star"). + // Strip source name from titles, excerpts, and summaries. + $nbsp = "\xC2\xA0"; // UTF-8 non-breaking space. foreach ( $articles as $article ) { - $article->title = $this->strip_source_suffix( $article->title, $article->source, $article->source_url ); - if ( ! empty( $article->ai_summary ) ) { - $article->ai_summary = $this->strip_source_suffix( $article->ai_summary, $article->source, $article->source_url ); - } - if ( ! empty( $article->excerpt ) ) { - $article->excerpt = $this->strip_source_suffix( $article->excerpt, $article->source, $article->source_url ); + $fields = array( 'title', 'excerpt', 'ai_summary' ); + foreach ( $fields as $field ) { + if ( empty( $article->$field ) ) { + continue; + } + // First pass: use strip_source_suffix for known patterns. + $article->$field = $this->strip_source_suffix( $article->$field, $article->source, $article->source_url ); + + // Second pass: catch any remaining nbsp-separated publisher names + // (e.g., "...text\xC2\xA0\xC2\xA0Publisher Name" from Google News RSS). + $text = $article->$field; + $pos = strrpos( $text, $nbsp . $nbsp ); + if ( false === $pos ) { + $pos = strrpos( $text, $nbsp ); + } + if ( false !== $pos && $pos > 10 ) { + $candidate = rtrim( substr( $text, 0, $pos ) ); + if ( strlen( $candidate ) > strlen( $text ) * 0.3 ) { + $article->$field = $candidate; + } + } } }