-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: optional dissalow discussion acces when logged out (#1087)
* feat: optional dissalow discussion acces when logged out * fix: update translations
- Loading branch information
Showing
10 changed files
with
14,464 additions
and
27,210 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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,50 @@ | ||
[{ | ||
"key": "group_67173bdc92fde", | ||
"title": "Comment Settings", | ||
"fields": [ | ||
{ | ||
"key": "field_67173bdc8a308", | ||
"label": "Hide discussion for logged out users", | ||
"name": "hide_discussion_for_logged_out_users", | ||
"aria-label": "", | ||
"type": "true_false", | ||
"instructions": "", | ||
"required": 0, | ||
"conditional_logic": 0, | ||
"wrapper": { | ||
"width": "", | ||
"class": "", | ||
"id": "" | ||
}, | ||
"message": "", | ||
"default_value": 0, | ||
"ui_on_text": "", | ||
"ui_off_text": "", | ||
"ui": 1 | ||
} | ||
], | ||
"location": [ | ||
[ | ||
{ | ||
"param": "options_page", | ||
"operator": "==", | ||
"value": "acf-options-theme-options" | ||
} | ||
] | ||
], | ||
"menu_order": 0, | ||
"position": "normal", | ||
"style": "default", | ||
"label_placement": "left", | ||
"instruction_placement": "label", | ||
"hide_on_screen": "", | ||
"active": true, | ||
"description": "", | ||
"show_in_rest": 0, | ||
"acfe_display_title": "", | ||
"acfe_autosync": "", | ||
"acfe_form": 0, | ||
"acfe_meta": "", | ||
"acfe_note": "" | ||
}] | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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,58 @@ | ||
<?php | ||
|
||
namespace Municipio\Comment; | ||
|
||
use AcfService\Contracts\GetField; | ||
use Municipio\HooksRegistrar\Hookable; | ||
use WP_Comment_Query; | ||
use WpService\Contracts\{AddAction,IsUserLoggedIn}; | ||
|
||
/** | ||
* Hide discussion from logged out users. | ||
* Check if option is set to hide discussion from logged out users and hide discussion if user is not logged in. | ||
* Runs on the `pre_get_comments` action. | ||
*/ | ||
class OptionalHideDiscussionWhenLoggedOut implements Hookable | ||
{ | ||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct(private AddAction&IsUserLoggedIn $wpService, private GetField $acfService) | ||
{ | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function addHooks(): void | ||
{ | ||
$this->wpService->addAction('pre_get_comments', [$this, 'hideDiscussionFromLoggedOutUser'], 10, 1); | ||
} | ||
|
||
/** | ||
* Hide discussion for logged out users if option is set. | ||
* | ||
* @param WP_Comment_Query $query | ||
*/ | ||
public function hideDiscussionFromLoggedOutUser(WP_Comment_Query $query): void | ||
{ | ||
$hideForLoggedOutUsers = $this->acfService->getField('hide_discussion_for_logged_out_users', 'option'); | ||
|
||
if (!$this->wpService->isUserLoggedIn() && $hideForLoggedOutUsers === true) { | ||
$query = $this->getDisabledCommentQuery($query); | ||
} | ||
} | ||
|
||
/** | ||
* Disable comments by setting post__in to an empty array. | ||
* This will make the query return no comments. | ||
* | ||
* @param WP_Comment_Query $query | ||
* @return WP_Comment_Query | ||
*/ | ||
private function getDisabledCommentQuery(WP_Comment_Query $query): WP_Comment_Query | ||
{ | ||
$query->query_vars['post__in'] = [0]; | ||
return $query; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
library/Comment/OptionalHideDiscussionWhenLoggedOut.test.php
This file contains 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,61 @@ | ||
<?php | ||
|
||
namespace Municipio\Comment; | ||
|
||
use AcfService\AcfService; | ||
use AcfService\Implementations\FakeAcfService; | ||
use Municipio\TestUtils\WpMockFactory; | ||
use PHPUnit\Framework\TestCase; | ||
use WpService\Implementations\FakeWpService; | ||
use WpService\WpService; | ||
|
||
class OptionalHideDiscussionWhenLoggedOutTest extends TestCase | ||
{ | ||
private WpService $wpService; | ||
private AcfService $acfService; | ||
|
||
private function setUpServices(bool $userLoggedIn = false, mixed $optionValue = null): void | ||
{ | ||
$this->wpService = new FakeWpService(['isUserLoggedIn' => fn () => $userLoggedIn, 'addAction' => true]); | ||
$this->acfService = new FakeAcfService(['getField' => fn () => $optionValue]); | ||
} | ||
|
||
public function testAddHooks(): void | ||
{ | ||
$this->setUpServices(); | ||
$sut = new OptionalHideDiscussionWhenLoggedOut($this->wpService, $this->acfService); | ||
$sut->addHooks(); | ||
|
||
$this->assertEquals('pre_get_comments', $this->wpService->methodCalls['addAction'][0][0]); | ||
} | ||
|
||
public function testNotHiddenWhenLoggedIn(): void | ||
{ | ||
$this->setUpServices(true, true); | ||
$commentQuery = WpMockFactory::createWpCommentQuery(['query_vars' => []]); | ||
$sut = new OptionalHideDiscussionWhenLoggedOut($this->wpService, $this->acfService); | ||
$sut->hideDiscussionFromLoggedOutUser($commentQuery); | ||
|
||
$this->assertArrayNotHasKey('post__in', $commentQuery->query_vars); | ||
} | ||
|
||
public function testNotHiddenWhenLoggedOutAndOptionDisabled(): void | ||
{ | ||
$this->setUpServices(false, false); | ||
$commentQuery = WpMockFactory::createWpCommentQuery(['query_vars' => []]); | ||
$sut = new OptionalHideDiscussionWhenLoggedOut($this->wpService, $this->acfService); | ||
$sut->hideDiscussionFromLoggedOutUser($commentQuery); | ||
|
||
$this->assertArrayNotHasKey('post__in', $commentQuery->query_vars); | ||
} | ||
|
||
public function testHiddenWhenLoggedOutAndOptionEnabled(): void | ||
{ | ||
$this->setUpServices(false, true); | ||
$commentQuery = WpMockFactory::createWpCommentQuery(['query_vars' => []]); | ||
$sut = new OptionalHideDiscussionWhenLoggedOut($this->wpService, $this->acfService); | ||
$sut->hideDiscussionFromLoggedOutUser($commentQuery); | ||
|
||
$this->assertEquals([0], $commentQuery->query_vars['post__in']); | ||
} | ||
} |
This file contains 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