Skip to content

Commit

Permalink
feat: optional dissalow discussion acces when logged out (#1087)
Browse files Browse the repository at this point in the history
* feat: optional dissalow discussion acces when logged out

* fix: update translations
  • Loading branch information
thorbrink authored Oct 22, 2024
1 parent 89f0a4a commit c6879ff
Show file tree
Hide file tree
Showing 10 changed files with 14,464 additions and 27,210 deletions.
9,903 changes: 4,944 additions & 4,959 deletions languages/municipio.pot

Large diffs are not rendered by default.

Binary file modified languages/sv_SE.mo
Binary file not shown.
31,535 changes: 9,284 additions & 22,251 deletions languages/sv_SE.po

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions library/AcfFields/json/options-comment-settings.json
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": ""
}]

53 changes: 53 additions & 0 deletions library/AcfFields/php/options-comment-settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

if (function_exists('acf_add_local_field_group')) {
acf_add_local_field_group(array(
'key' => 'group_67173bdc92fde',
'title' => __('Comment Settings', 'municipio'),
'fields' => array(
0 => array(
'key' => 'field_67173bdc8a308',
'label' => __('Hide discussion for logged out users', 'municipio'),
'name' => 'hide_discussion_for_logged_out_users',
'aria-label' => '',
'type' => 'true_false',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'message' => '',
'default_value' => 0,
'ui_on_text' => '',
'ui_off_text' => '',
'ui' => 1,
),
),
'location' => array(
0 => array(
0 => array(
'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' => '',
));
}
Expand Down
2 changes: 2 additions & 0 deletions library/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Municipio\BrandedEmails\ApplyMailHtmlTemplate;
use Municipio\BrandedEmails\HtmlTemplate\Config\HtmlTemplateConfigService;
use Municipio\BrandedEmails\HtmlTemplate\DefaultHtmlTemplate;
use Municipio\Comment\OptionalHideDiscussionWhenLoggedOut;
use Municipio\Config\Features\SchemaData\SchemaDataConfigInterface;
use Municipio\Content\ResourceFromApi\Api\ResourceFromApiRestController;
use Municipio\Content\ResourceFromApi\Modifiers\HooksAdder;
Expand Down Expand Up @@ -217,6 +218,7 @@ public function __construct(
new \Municipio\Comment\Likes();
new \Municipio\Comment\Filters();
new \Municipio\Comment\Form();
$this->hooksRegistrar->register(new OptionalHideDiscussionWhenLoggedOut($this->wpService, $this->acfService));

/**
* Admin
Expand Down
1 change: 1 addition & 0 deletions library/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
'options-menu-description' => 'group_650296216899c',
'options-menu-language' => 'group_6141cc9c72cc3',
'options-api-resources-apis' => 'group_653a1673dc501',
'options-comment-settings' => 'group_67173bdc92fde',
'options-customize-header' => 'group_5afa93c0a25e1',
'options-customize-footer' => 'group_5afa94c88e1aa',
'resource-fields' => 'group_653a509450198',
Expand Down
58 changes: 58 additions & 0 deletions library/Comment/OptionalHideDiscussionWhenLoggedOut.php
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 library/Comment/OptionalHideDiscussionWhenLoggedOut.test.php
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']);
}
}
11 changes: 11 additions & 0 deletions tests/phpunit/TestUtils/WpMockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use stdClass;
use WP_Comment_Query;
use WP_Error;
use WP_Post;
use WP_Term;
Expand Down Expand Up @@ -45,6 +46,16 @@ public static function createWpError(array $args = []): MockObject|WP_Error
return self::buildMockWithArgs('WP_Error', $args);
}

/**
* Create a mock WP_Error object.
*
* @param array $args
*/
public static function createWpCommentQuery(array $args = []): MockObject|WP_Comment_Query
{
return self::buildMockWithArgs('WP_Comment_Query', $args);
}

/**
* Create a mock wpdb object.
*
Expand Down

0 comments on commit c6879ff

Please sign in to comment.