Skip to content

Commit cbfe6ea

Browse files
committed
Protect code HTML
1 parent b9f8294 commit cbfe6ea

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

includes/class-hashtag.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static function init() {
2020
/**
2121
* Filter to save #tags as real WordPress tags
2222
*
23-
* @param int $id the rev-id
23+
* @param int $id the rev-id
2424
* @param WP_Post $post the post
2525
*
2626
* @return
@@ -44,6 +44,16 @@ public static function insert_post( $id, $post ) {
4444
*/
4545
public static function the_content( $the_content ) {
4646
$protected_tags = array();
47+
$the_content = preg_replace_callback(
48+
'#<(code|textarea|style)\b[^>]*>.*?</\1[^>]*>#i',
49+
function( $m ) use ( &$protected_tags ) {
50+
$c = count( $protected_tags );
51+
$protect = '!#!#PROTECT' . $c . '#!#!';
52+
$protected_tags[ $protect ] = $m[0];
53+
return $protect;
54+
},
55+
$the_content
56+
);
4757
$the_content = preg_replace_callback(
4858
'#<[^>]+>#i',
4959
function( $m ) use ( &$protected_tags ) {

includes/class-mention.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ public static function init() {
2424
*/
2525
public static function the_content( $the_content ) {
2626
$protected_tags = array();
27+
$the_content = preg_replace_callback(
28+
'#<(code|textarea|style)\b[^>]*>.*?</\1[^>]*>#i',
29+
function( $m ) use ( &$protected_tags ) {
30+
$c = count( $protected_tags );
31+
$protect = '!#!#PROTECT' . $c . '#!#!';
32+
$protected_tags[ $protect ] = $m[0];
33+
return $protect;
34+
},
35+
$the_content
36+
);
2737
$the_content = preg_replace_callback(
2838
'#<a.*?href=[^>]+>.*?</a>#i',
2939
function( $m ) use ( &$protected_tags ) {
@@ -68,7 +78,7 @@ public static function replace_with_links( $result ) {
6878
/**
6979
* Extract the mentions from the post_content.
7080
*
71-
* @param array $mentions The already found mentions.
81+
* @param array $mentions The already found mentions.
7282
* @param string $post_content The post content.
7383
* @return mixed The discovered mentions.
7484
*/

tests/test-class-activitypub-hashtag.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Test_Activitypub_Hashtag extends WP_UnitTestCase {
55
*/
66
public function test_the_content( $content, $content_with_hashtag ) {
77
\wp_create_term( 'object', 'post_tag' );
8+
\wp_create_term( 'ccc', 'post_tag' );
89
$object = \get_term_by( 'name', 'object', 'post_tag' );
910
$link = \get_term_link( $object, 'post_tag' );
1011

@@ -14,6 +15,15 @@ public function test_the_content( $content, $content_with_hashtag ) {
1415
}
1516

1617
public function the_content_provider() {
18+
$code = '<code>text with some #object and <a> tag inside</code>';
19+
$style = <<<ENDSTYLE
20+
<style type="text/css">
21+
<![[
22+
color: #ccc;
23+
]]>
24+
</style>
25+
ENDSTYLE;
26+
$textarea = '<textarea name="test" rows="20">color: #ccc</textarea>';
1727
return array(
1828
array( 'test', 'test' ),
1929
array( '#test', '#test' ),
@@ -27,6 +37,9 @@ public function the_content_provider() {
2737
array( '<div>#object</div>', '<div>#object</div>' ),
2838
array( '<a>#object</a>', '<a>#object</a>' ),
2939
array( '<div style="color: #ccc;">object</a>', '<div style="color: #ccc;">object</a>' ),
40+
array( $code, $code ),
41+
array( $style, $style ),
42+
array( $textarea, $textarea ),
3043
);
3144
}
3245
}

tests/test-class-activitypub-mention.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
class Test_Activitypub_Mention extends ActivityPub_TestCase_Cache_HTTP {
33
public static $users = array(
44
'[email protected]' => array(
5-
'url' => 'https://example.org/users/username',
6-
'name' => 'username',
5+
'url' => 'https://example.org/users/username',
6+
'name' => 'username',
77
),
88
);
99
/**
@@ -18,12 +18,14 @@ public function test_the_content( $content, $content_with_mention ) {
1818
}
1919

2020
public function the_content_provider() {
21+
$code = 'hallo <code>@[email protected]</code> test';
2122
return array(
2223
array( 'hallo @[email protected] test', 'hallo <a rel="mention" class="u-url mention" href="https://example.org/users/username">@<span>username</span></a> test' ),
2324
array( 'hallo @[email protected] test', 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/author/matthias-pfefferle/">@<span>pfefferle</span></a> test' ),
2425
array( 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/author/matthias-pfefferle/">@<span>pfefferle</span>@notiz.blog</a> test', 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/author/matthias-pfefferle/">@<span>pfefferle</span>@notiz.blog</a> test' ),
2526
array( 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/author/matthias-pfefferle/">@[email protected]</a> test', 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/author/matthias-pfefferle/">@[email protected]</a> test' ),
2627
array( 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/@pfefferle/">@[email protected]</a> test', 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/@pfefferle/">@[email protected]</a> test' ),
28+
array( $code, $code ),
2729
);
2830
}
2931

0 commit comments

Comments
 (0)