Skip to content

Commit 52b7b99

Browse files
committed
Fix. After test. Ignoring other parts of title fixed.
1 parent 1b7c483 commit 52b7b99

3 files changed

Lines changed: 60 additions & 46 deletions

File tree

lib/Cleantalk/Antispam/EmailEncoder/EmailEncoder.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,6 @@ public function modifyContent($content)
190190
return $content;
191191
}
192192

193-
if ($this->shortcodes->exclude->isContentExcluded($content)) {
194-
return $content;
195-
}
196-
197193
// modify content to prevent aria-label replaces by hiding it
198194
$content = $this->handleAriaLabelContent($content);
199195

lib/Cleantalk/Antispam/EmailEncoder/Shortcodes/SkipContentFromEncodeSC.php

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class SkipContentFromEncodeSC extends EmailEncoderShortCode
1818
/**
1919
* @var string The wrapper used to mark content for exclusion.
2020
*/
21-
private $exclusion_wrapper = '%%APBCT_SHORT_CODE_EXCLUDE_EE%%';
21+
private $exclusion_wrapper = '##SCE_%d##';
22+
private $replaces = array();
2223

2324
/**
2425
* Callback function for the shortcode.
@@ -32,9 +33,7 @@ class SkipContentFromEncodeSC extends EmailEncoderShortCode
3233
*/
3334
public function callback($_atts, $content, $_tag)
3435
{
35-
$wrapper = $this->exclusion_wrapper;
36-
$content = $wrapper . $content . $wrapper;
37-
return $content;
36+
return $this->processExclusions($content);
3837
}
3938

4039
/**
@@ -64,28 +63,47 @@ public function changeContentBeforeEncoderModify($content)
6463
*/
6564
public function changeContentAfterEncoderModify($content)
6665
{
67-
$content = str_replace(
68-
$this->exclusion_wrapper,
69-
'',
70-
$content
71-
);
72-
return $content;
66+
return $this->revertExclusions($content);
7367
}
7468

7569
/**
76-
* Checks if the content is excluded from encoding.
70+
* Apply exclusions to replace modified shortcodes with service symbols. Then collect all the performed
71+
* replacements to memory storage to being reverted after common modifying.
7772
*
78-
* Uses a regular expression to determine if the content is wrapped
79-
* in the exclusion wrapper.
80-
*
81-
* @param string $content The content to check.
82-
* @return false|int Returns 1 if the content is excluded, 0 otherwise.
73+
* @param string|null $content The content to check.
74+
* @return string Returns content with handled exclusions
8375
* @psalm-suppress PossiblyUnusedMethod
8476
*/
85-
public function isContentExcluded($content)
77+
public function processExclusions($content)
78+
{
79+
if (is_null($content)) {
80+
return (string)$content;
81+
}
82+
$index = count($this->replaces);
83+
$placeholder = sprintf($this->exclusion_wrapper, $index);
84+
$this->replaces[$index] = [
85+
'origin' => $content,
86+
'replace' => $placeholder
87+
];
88+
$wrappedContent = $placeholder;
89+
90+
return $wrappedContent;
91+
}
92+
93+
/**
94+
* Rollback al the replaces with modified shortcodes after common encoding.
95+
* @param string $content
96+
*
97+
* @return string
98+
*/
99+
public function revertExclusions($content)
86100
{
87-
$exclusion_regex = '/' . $this->exclusion_wrapper . '.*' . $this->exclusion_wrapper . '/';
88-
return preg_match($exclusion_regex, $content);
101+
foreach ($this->replaces as $_item => $data) {
102+
if (isset($data['replace'], $data['origin']) && is_string($data['replace']) && is_string($data['origin'])) {
103+
$content = str_replace($data['replace'], $data['origin'], $content);
104+
}
105+
}
106+
return $content;
89107
}
90108

91109
/**

tests/Antispam/testEmailEnocderShortcodeSkip.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,46 +22,46 @@ public function testCallbackWrapsContentInExclusionWrapper()
2222
$content = 'Test content';
2323
$result = $this->shortcode->callback([], $content, 'apbct_skip_encoding');
2424

25-
$this->assertEquals('%%APBCT_SHORT_CODE_EXCLUDE_EE%%Test content%%APBCT_SHORT_CODE_EXCLUDE_EE%%', $result);
25+
$this->assertEquals('##SCE_0##', $result);
2626
}
2727

2828
public function testChangeContentBeforeEncoderModifyExecutesCallback()
2929
{
3030
$content = 'Some content with [apbct_skip_encoding]Test content[/apbct_skip_encoding]';
3131
$result = $this->shortcode->changeContentBeforeEncoderModify($content);
3232

33-
$this->assertStringContainsString('%%APBCT_SHORT_CODE_EXCLUDE_EE%%Test content%%APBCT_SHORT_CODE_EXCLUDE_EE%%', $result);
33+
$this->assertStringContainsString('Some content with ##SCE_0##', $result);
3434
}
3535

36-
public function testChangeContentAfterEncoderModifyRemovesExclusionWrapper()
37-
{
38-
$content = '%%APBCT_SHORT_CODE_EXCLUDE_EE%%Test content%%APBCT_SHORT_CODE_EXCLUDE_EE%%';
39-
$result = $this->shortcode->changeContentAfterEncoderModify($content);
40-
41-
$this->assertEquals('Test content', $result);
42-
}
43-
44-
public function testIsContentExcludedReturnsTrueForExcludedContent()
36+
public function testClearTitleContentFromShortcodeConstruction()
4537
{
46-
$content = '%%APBCT_SHORT_CODE_EXCLUDE_EE%%Test content%%APBCT_SHORT_CODE_EXCLUDE_EE%%';
47-
$result = $this->shortcode->isContentExcluded($content);
38+
$content = 'Some content with [apbct_skip_encoding]Test content[/apbct_skip_encoding]';
39+
$result = $this->shortcode->clearTitleContentFromShortcodeConstruction($content);
4840

49-
$this->assertEquals(1, $result);
41+
$this->assertEquals('Some content with Test content', $result);
5042
}
5143

52-
public function testIsContentExcludedReturnsFalseForNonExcludedContent()
44+
public function testClearTitleContentFromShortcodeConstructionSkippingEmail()
5345
{
54-
$content = 'Test content';
55-
$result = $this->shortcode->isContentExcluded($content);
46+
$content = 'Hah, there is email example@exmple.com and some content with [apbct_skip_encoding]Test content[/apbct_skip_encoding]';
47+
$result = $this->shortcode->clearTitleContentFromShortcodeConstruction($content);
5648

57-
$this->assertEquals(0, $result);
49+
$this->assertEquals('Hah, there is email example@exmple.com and some content with Test content', $result);
5850
}
5951

60-
public function testClearTitleContentFromShortcodeConstruction()
52+
public function testTitleContentWithEmailSkippedAndUnskipped()
6153
{
62-
$content = 'Some content with [apbct_skip_encoding]Test content[/apbct_skip_encoding]';
63-
$result = $this->shortcode->clearTitleContentFromShortcodeConstruction($content);
64-
65-
$this->assertEquals('Some content with Test content', $result);
54+
$origin_content = 'Hah, there is email example@exmple.com and some content with [apbct_skip_encoding]Test content[/apbct_skip_encoding]';
55+
$content = $origin_content;
56+
//emulate hook before
57+
$content = $this->shortcode->changeContentBeforeEncoderModify($content);
58+
//do common modifying
59+
$content = \Cleantalk\ApbctWP\Antispam\EmailEncoder::getInstance()->modifyContent($content);
60+
//emulate hook after
61+
$content = $this->shortcode->changeContentAfterEncoderModify($content);
62+
63+
$this->assertStringNotContainsString( 'example@exmple.com', $content);
64+
$this->assertStringNotContainsString( 'apbct_skip_encoding', $content);
65+
$this->assertStringContainsString( 'with Test content', $content);
6666
}
6767
}

0 commit comments

Comments
 (0)