From 840d1443270bc1d5e348c6e950d68381696cc150 Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Tue, 13 Dec 2022 11:34:07 +0100 Subject: [PATCH] Avoid replacing mentions inside links --- includes/class-mention.php | 14 ++++++++++++++ .../class-friends-feed-parser-activitypub.php | 14 ++++++++++++++ tests/test-class-activitypub-mention.php | 3 +++ 3 files changed, 31 insertions(+) diff --git a/includes/class-mention.php b/includes/class-mention.php index 4e305a8..0227d8a 100644 --- a/includes/class-mention.php +++ b/includes/class-mention.php @@ -23,8 +23,22 @@ class Mention { * @return string the filtered post-content */ public static function the_content( $the_content ) { + $protected_tags = array(); + $the_content = preg_replace_callback( + '#]+>.*?#i', + function( $m ) use ( &$protected_tags ) { + $c = count( $protected_tags ); + $protect = '!#!#PROTECT' . $c . '#!#!'; + $protected_tags[ $protect ] = $m[0]; + return $protect; + }, + $the_content + ); + $the_content = \preg_replace_callback( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/', array( '\Activitypub\Mention', 'replace_with_links' ), $the_content ); + $the_content = str_replace( array_keys( $protected_tags ), array_values( $protected_tags ), $the_content ); + return $the_content; } diff --git a/integration/class-friends-feed-parser-activitypub.php b/integration/class-friends-feed-parser-activitypub.php index 58717f3..594ae85 100644 --- a/integration/class-friends-feed-parser-activitypub.php +++ b/integration/class-friends-feed-parser-activitypub.php @@ -453,8 +453,22 @@ class Friends_Feed_Parser_ActivityPub extends \Friends\Feed_Parser { public function the_content( $the_content ) { + $protected_tags = array(); + $the_content = preg_replace_callback( + '#]+>.*?#i', + function( $m ) use ( &$protected_tags ) { + $c = count( $protected_tags ); + $protect = '!#!#PROTECT' . $c . '#!#!'; + $protected_tags[ $protect ] = $m[0]; + return $protect; + }, + $the_content + ); + $the_content = \preg_replace_callback( '/@(?:[a-zA-Z0-9_-]+)/', array( $this, 'replace_with_links' ), $the_content ); + $the_content = str_replace( array_keys( $protected_tags ), array_values( $protected_tags ), $the_content ); + return $the_content; } diff --git a/tests/test-class-activitypub-mention.php b/tests/test-class-activitypub-mention.php index 274f432..e777dff 100644 --- a/tests/test-class-activitypub-mention.php +++ b/tests/test-class-activitypub-mention.php @@ -21,6 +21,9 @@ class Test_Activitypub_Mention extends ActivityPub_TestCase_Cache_HTTP { return array( array( 'hallo @username@example.org test', 'hallo @username test' ), array( 'hallo @pfefferle@notiz.blog test', 'hallo @pfefferle test' ), + array( 'hallo @pfefferle@notiz.blog test', 'hallo @pfefferle@notiz.blog test' ), + array( 'hallo @pfefferle@notiz.blog test', 'hallo @pfefferle@notiz.blog test' ), + array( 'hallo @pfefferle@notiz.blog test', 'hallo @pfefferle@notiz.blog test' ), ); }