diff --git a/includes/class-hashtag.php b/includes/class-hashtag.php index 342320d..b14a8fa 100644 --- a/includes/class-hashtag.php +++ b/includes/class-hashtag.php @@ -20,7 +20,7 @@ class Hashtag { /** * Filter to save #tags as real WordPress tags * - * @param int $id the rev-id + * @param int $id the rev-id * @param WP_Post $post the post * * @return @@ -44,20 +44,31 @@ class Hashtag { */ public static function the_content( $the_content ) { $protected_tags = array(); + $protect = function( $m ) use ( &$protected_tags ) { + $c = count( $protected_tags ); + $protect = '!#!#PROTECT' . $c . '#!#!'; + $protected_tags[ $protect ] = $m[0]; + return $protect; + }; + $the_content = preg_replace_callback( + '##is', + $protect, + $the_content + ); + $the_content = preg_replace_callback( + '#<(pre|code|textarea|style)\b[^>]*>.*?]*>#is', + $protect, + $the_content + ); $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; - }, + $protect, $the_content ); $the_content = \preg_replace_callback( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', array( '\Activitypub\Hashtag', 'replace_with_links' ), $the_content ); - $the_content = str_replace( array_keys( $protected_tags ), array_values( $protected_tags ), $the_content ); + $the_content = str_replace( array_reverse( array_keys( $protected_tags ) ), array_reverse( array_values( $protected_tags ) ), $the_content ); return $the_content; } diff --git a/includes/class-mention.php b/includes/class-mention.php index 0227d8a..7c8672a 100644 --- a/includes/class-mention.php +++ b/includes/class-mention.php @@ -24,20 +24,31 @@ class Mention { */ public static function the_content( $the_content ) { $protected_tags = array(); + $protect = function( $m ) use ( &$protected_tags ) { + $c = count( $protected_tags ); + $protect = '!#!#PROTECT' . $c . '#!#!'; + $protected_tags[ $protect ] = $m[0]; + return $protect; + }; + $the_content = preg_replace_callback( + '##is', + $protect, + $the_content + ); + $the_content = preg_replace_callback( + '#<(pre|code|textarea|style)\b[^>]*>.*?]*>#is', + $protect, + $the_content + ); $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; - }, + $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 ); + $the_content = str_replace( array_reverse( array_keys( $protected_tags ) ), array_reverse( array_values( $protected_tags ) ), $the_content ); return $the_content; } @@ -68,7 +79,7 @@ class Mention { /** * Extract the mentions from the post_content. * - * @param array $mentions The already found mentions. + * @param array $mentions The already found mentions. * @param string $post_content The post content. * @return mixed The discovered mentions. */ diff --git a/tests/test-class-activitypub-hashtag.php b/tests/test-class-activitypub-hashtag.php index 5c207bd..51a532c 100644 --- a/tests/test-class-activitypub-hashtag.php +++ b/tests/test-class-activitypub-hashtag.php @@ -5,6 +5,8 @@ class Test_Activitypub_Hashtag extends WP_UnitTestCase { */ public function test_the_content( $content, $content_with_hashtag ) { \wp_create_term( 'object', 'post_tag' ); + \wp_create_term( 'touch', 'post_tag' ); + \wp_create_term( 'ccc', 'post_tag' ); $object = \get_term_by( 'name', 'object', 'post_tag' ); $link = \get_term_link( $object, 'post_tag' ); @@ -14,6 +16,21 @@ class Test_Activitypub_Hashtag extends WP_UnitTestCase { } public function the_content_provider() { + $code = 'text with some #object and tag inside'; + $style = << + + +ENDSTYLE; + $pre = << +Please don't #touch + this. + +ENDPRE; + $textarea = ''; return array( array( 'test', 'test' ), array( '#test', '#test' ), @@ -27,6 +44,10 @@ class Test_Activitypub_Hashtag extends WP_UnitTestCase { array( '
#object
', '
#object
' ), array( '
#object', '#object' ), array( '
object', '
object' ), + array( $code, $code ), + array( $style, $style ), + array( $textarea, $textarea ), + array( $pre, $pre ), ); } } diff --git a/tests/test-class-activitypub-mention.php b/tests/test-class-activitypub-mention.php index e777dff..6f6b9ff 100644 --- a/tests/test-class-activitypub-mention.php +++ b/tests/test-class-activitypub-mention.php @@ -2,8 +2,8 @@ class Test_Activitypub_Mention extends ActivityPub_TestCase_Cache_HTTP { public static $users = array( 'username@example.org' => array( - 'url' => 'https://example.org/users/username', - 'name' => 'username', + 'url' => 'https://example.org/users/username', + 'name' => 'username', ), ); /** @@ -18,12 +18,21 @@ class Test_Activitypub_Mention extends ActivityPub_TestCase_Cache_HTTP { } public function the_content_provider() { + $code = 'hallo @username@example.org test'; + $pre = << +Please don't mention @username@example.org + here. + +ENDPRE; 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' ), + array( $code, $code ), + array( $pre, $pre ), ); }