-
Notifications
You must be signed in to change notification settings - Fork 9
Fix. ContactEncoder. Improve shortcode statement. #863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
187 changes: 187 additions & 0 deletions
187
lib/Cleantalk/ApbctWP/ContactsEncoder/Integrations/CEIntegrationCommentList.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| <?php | ||
|
|
||
| namespace Cleantalk\ApbctWP\ContactsEncoder\Integrations; | ||
|
|
||
| /** | ||
| * Skip Contacts Encoder shortcode handling inside rendered comment lists in buffer mode. | ||
| */ | ||
| class CEIntegrationCommentList | ||
| { | ||
| /** | ||
| * @var array<string, string> | ||
| */ | ||
| private $region_placeholders = array(); | ||
|
|
||
| /** | ||
| * @param string $content | ||
| * | ||
| * @return string | ||
| */ | ||
| public function protect($content) | ||
| { | ||
| if ( ! is_string($content) || $content === '' ) { | ||
| return $content; | ||
| } | ||
|
|
||
| if ( | ||
| stripos($content, 'comment-list') === false | ||
| && stripos($content, 'id="comments"') === false | ||
| && stripos($content, "id='comments'") === false | ||
| ) { | ||
| return $content; | ||
| } | ||
|
|
||
| $this->region_placeholders = array(); | ||
|
|
||
| $content = $this->protectElementsById($content, 'comments'); | ||
| $content = $this->protectElementsByClass($content, 'comment-list', 'ol'); | ||
| $content = $this->protectElementsByClass($content, 'comment-list', 'ul'); | ||
|
|
||
| return $content; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $content | ||
| * | ||
| * @return string | ||
| */ | ||
| public function restore($content) | ||
| { | ||
| if ( ! is_string($content) || $this->region_placeholders === array() ) { | ||
| return $content; | ||
| } | ||
|
|
||
| $content = strtr($content, $this->region_placeholders); | ||
| $this->region_placeholders = array(); | ||
|
|
||
| return $content; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $content | ||
| * @param string $id | ||
| * | ||
| * @return string | ||
| */ | ||
| private function protectElementsById($content, $id) | ||
| { | ||
| $pattern = '/<div\b(?=[^>]*\bid=(["\'])' . preg_quote($id, '/') . '\1)/i'; | ||
|
|
||
| return $this->protectByOpeningTagPattern($content, $pattern, 'div'); | ||
| } | ||
|
|
||
| /** | ||
| * @param string $content | ||
| * @param string $class | ||
| * @param string $tag_name | ||
| * | ||
| * @return string | ||
| */ | ||
| private function protectElementsByClass($content, $class, $tag_name) | ||
| { | ||
| $pattern = '/<' . $tag_name . '\b(?=[^>]*\bclass=(["\'])[^"\']*\b' | ||
| . preg_quote($class, '/') | ||
| . '\b[^"\']*\1)/i'; | ||
|
|
||
| return $this->protectByOpeningTagPattern($content, $pattern, $tag_name); | ||
| } | ||
|
|
||
| /** | ||
| * @param string $content | ||
| * @param string $open_pattern | ||
| * @param string $tag_name | ||
| * | ||
| * @return string | ||
| */ | ||
| private function protectByOpeningTagPattern($content, $open_pattern, $tag_name) | ||
| { | ||
| $offset = 0; | ||
|
|
||
| while ( preg_match($open_pattern, $content, $matches, PREG_OFFSET_CAPTURE, $offset) ) { | ||
| $start = $matches[0][1] ?? null; | ||
|
|
||
| if ( $start === null ) { | ||
| break; | ||
| } | ||
|
|
||
| $element = $this->extractBalancedElement($content, $start, $tag_name); | ||
|
|
||
| if ( $element === null ) { | ||
| $offset = $start + 1; | ||
| continue; | ||
| } | ||
|
|
||
| $placeholder = '%%APBCT_COMMENT_REGION_' . count($this->region_placeholders) . '%%'; | ||
| $this->region_placeholders[$placeholder] = $element; | ||
|
|
||
| $content = substr($content, 0, $start) . $placeholder . substr($content, $start + strlen($element)); | ||
| $offset = $start + strlen($placeholder); | ||
| } | ||
|
|
||
| return $content; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $content | ||
| * @param int $start | ||
| * @param string $tag_name | ||
| * | ||
| * @return string|null | ||
| */ | ||
| private function extractBalancedElement($content, $start, $tag_name) | ||
| { | ||
| $open_end = strpos($content, '>', $start); | ||
|
|
||
| if ( $open_end === false ) { | ||
| return null; | ||
| } | ||
|
|
||
| $depth = 1; | ||
| $pos = $open_end + 1; | ||
| $length = strlen($content); | ||
| $open_needle = '<' . $tag_name; | ||
| $close_needle = '</' . $tag_name; | ||
|
|
||
| while ( $pos < $length && $depth > 0 ) { | ||
| $next_open = stripos($content, $open_needle, $pos); | ||
| $next_close = stripos($content, $close_needle, $pos); | ||
|
|
||
| if ( $next_close === false ) { | ||
| return null; | ||
| } | ||
|
|
||
| if ( $next_open !== false && $next_open < $next_close && $this->isTagOpenAt($content, $next_open, $tag_name) ) { | ||
| $depth++; | ||
| $pos = $next_open + 1; | ||
| continue; | ||
| } | ||
|
|
||
| $depth--; | ||
| $close_end = strpos($content, '>', $next_close); | ||
|
|
||
| if ( $close_end === false ) { | ||
| return null; | ||
| } | ||
|
|
||
| $pos = $close_end + 1; | ||
|
|
||
| if ( $depth === 0 ) { | ||
| return substr($content, $start, $pos - $start); | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $content | ||
| * @param int $offset | ||
| * @param string $tag_name | ||
| * | ||
| * @return bool | ||
| */ | ||
| private function isTagOpenAt($content, $offset, $tag_name) | ||
| { | ||
| return (bool) preg_match('/^<' . preg_quote($tag_name, '/') . '\b/i', substr($content, $offset, strlen($tag_name) + 2)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
tests/ApbctWP/ContactsEncoder/Integrations/TestCEIntegrationCommentList.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| <?php | ||
|
|
||
| namespace ApbctWP\ContactsEncoder\Integrations; | ||
|
|
||
| use Cleantalk\ApbctWP\ContactsEncoder\Integrations\CEIntegrationCommentList; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class TestCEIntegrationCommentList extends TestCase | ||
| { | ||
| public function testProtectAndRestoreCommentListPreservesSiblingComments() | ||
| { | ||
| $html = '<div id="comments" class="comments-area">' | ||
| . '<ol class="comment-list">' | ||
| . '<li class="comment"><div class="comment-content">[apbct_encode_data]</div></li>' | ||
| . '<li class="comment"><div class="comment-content">Second comment text</div></li>' | ||
| . '<li class="comment"><div class="comment-content">[apbct_encode_data]z[/apbct_encode_data][/apbct_encode_data]</div></li>' | ||
| . '</ol>' | ||
| . '</div>'; | ||
|
|
||
| $integration = new CEIntegrationCommentList(); | ||
| $protected = $integration->protect($html); | ||
| $restored = $integration->restore($protected); | ||
|
|
||
| $this->assertStringNotContainsString('[apbct_encode_data]', $protected); | ||
| $this->assertStringContainsString('%%APBCT_COMMENT_REGION_0%%', $protected); | ||
| $this->assertSame($html, $restored); | ||
| } | ||
|
|
||
| public function testProtectStandaloneCommentListWithoutCommentsWrapper() | ||
| { | ||
| $html = '<ol class="comment-list">' | ||
| . '<li>one</li>' | ||
| . '<li>two</li>' | ||
| . '</ol>'; | ||
|
|
||
| $integration = new CEIntegrationCommentList(); | ||
| $protected = $integration->protect($html); | ||
|
|
||
| $this->assertStringContainsString('%%APBCT_COMMENT_REGION_0%%', $protected); | ||
| $this->assertSame($html, $integration->restore($protected)); | ||
| } | ||
|
|
||
| public function testProtectLeavesContentWithoutCommentListUntouched() | ||
| { | ||
| $html = '<article>[apbct_encode_data]post@example.com[/apbct_encode_data]</article>'; | ||
|
|
||
| $integration = new CEIntegrationCommentList(); | ||
|
|
||
| $this->assertSame($html, $integration->protect($html)); | ||
| } | ||
|
|
||
| public function testRestoreClearsPlaceholdersForSubsequentCalls() | ||
| { | ||
| $html = '<ol class="comment-list"><li>one</li></ol>'; | ||
|
|
||
| $integration = new CEIntegrationCommentList(); | ||
| $protected = $integration->protect($html); | ||
| $restored = $integration->restore($protected); | ||
|
|
||
| $this->assertSame($html, $restored); | ||
| $this->assertSame($protected, $integration->restore($protected)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.