From 3deea94a8cbee4d0cbdf8f984184d90935cd687c Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Tue, 14 Apr 2015 10:31:32 +1200 Subject: [PATCH] API Restore ability to do frontend-moderation This feature is disabled by default, but can be turned on either for spam, unmoderated comments, or both. --- code/extensions/CommentsExtension.php | 17 ++++++++++--- docs/en/Configuration.md | 8 +++--- tests/CommentsTest.php | 35 +++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/code/extensions/CommentsExtension.php b/code/extensions/CommentsExtension.php index 08e9bf0..c668307 100644 --- a/code/extensions/CommentsExtension.php +++ b/code/extensions/CommentsExtension.php @@ -23,6 +23,8 @@ class CommentsExtension extends DataExtension { * comment_permalink_prefix: ID prefix for each comment * require_moderation: Require moderation for all comments * require_moderation_cms: Ignore other comment moderation config settings and set via CMS + * frontend_moderation: Display unmoderated comments in the frontend, if the user can moderate them. + * frontend_spam: Display spam comments in the frontend, if the user can moderate them. * html_allowed: Allow for sanitized HTML in comments * use_preview: Preview formatted comment (when allowing HTML) * @@ -49,6 +51,8 @@ class CommentsExtension extends DataExtension { 'require_moderation' => false, 'require_moderation_nonmembers' => false, 'require_moderation_cms' => false, + 'frontend_moderation' => false, + 'frontend_spam' => false, 'html_allowed' => false, 'html_allowed_elements' => array('a', 'img', 'i', 'b'), 'use_preview' => false, @@ -199,11 +203,18 @@ class CommentsExtension extends DataExtension { $order = $this->owner->getCommentsOption('order_comments_by'); $list = $this ->AllComments() - ->sort($order) - ->filter('IsSpam', 0); + ->sort($order); + + // Filter spam comments for non-administrators if configured + $showSpam = $this->owner->getCommentsOption('frontend_spam') && $this->owner->canModerateComments(); + if(!$showSpam) { + $list = $list->filter('IsSpam', 0); + } // Filter un-moderated comments for non-administrators if moderation is enabled - if($this->owner->ModerationRequired !== 'None') { + $showUnmoderated = ($this->owner->ModerationRequired === 'None') + || ($this->owner->getCommentsOption('frontend_moderation') && $this->owner->canModerateComments()); + if(!$showUnmoderated) { $list = $list->filter('Moderated', 1); } diff --git a/docs/en/Configuration.md b/docs/en/Configuration.md index cc0a3ea..b9c3639 100644 --- a/docs/en/Configuration.md +++ b/docs/en/Configuration.md @@ -24,7 +24,7 @@ SiteTree: extensions: - CommentsExtension comments: - enabled: true # Enables commenting to be disabled for a specific class (or subclass of a parent with commenting enabled) + enabled: true # Enables commenting to be disabled for a specific class (or subclass of a parent with commenting enabled) enabled_cms: false # The 'enabled' option will be set via the CMS instead of config require_login: false # boolean, whether a user needs to login require_login_cms: false # The 'require_login' option will be set via the CMS instead of config @@ -40,8 +40,10 @@ SiteTree: comments_holder_id: 'comments-holder' # id for the comments holder comment_permalink_prefix: 'comment-' # id prefix for each comment. If needed make this different require_moderation: false - require_moderation_nonmembers: false # requires moderation for comments posted by non-members. 'require_moderation' overrides this if set. - require_moderation_cms: false # If true, ignore above values and configure moderation requirements via the CMS only + require_moderation_nonmembers: false # requires moderation for comments posted by non-members. 'require_moderation' overrides this if set. + require_moderation_cms: false # If true, ignore above values and configure moderation requirements via the CMS only + frontend_moderation: false # Display unmoderated comments in the frontend, if the user can moderate them. + frontend_spam: false # Display spam comments in the frontend, if the user can moderate them. html_allowed: false # allow for sanitized HTML in comments html_allowed_elements: - a diff --git a/tests/CommentsTest.php b/tests/CommentsTest.php index 3255340..86469a7 100644 --- a/tests/CommentsTest.php +++ b/tests/CommentsTest.php @@ -57,6 +57,41 @@ class CommentsTest extends FunctionalTest { Config::inst()->update('CommentableItem', 'comments', array('require_moderation_nonmembers' => false)); $this->assertEquals(2, $item->Comments()->Count()); + + // With unmoderated comments set to display in frontend + Config::inst()->update('CommentableItem', 'comments', array( + 'require_moderation' => true, + 'frontend_moderation' => true + )); + $this->assertEquals(1, $item->Comments()->Count()); + + $this->logInWithPermission('ADMIN'); + $this->assertEquals(2, $item->Comments()->Count()); + + // With spam comments set to display in frontend + Config::inst()->update('CommentableItem', 'comments', array( + 'require_moderation' => true, + 'frontend_moderation' => false, + 'frontend_spam' => true, + )); + if($member = Member::currentUser()) $member->logOut(); + $this->assertEquals(1, $item->Comments()->Count()); + + $this->logInWithPermission('ADMIN'); + $this->assertEquals(2, $item->Comments()->Count()); + + + // With spam and unmoderated comments set to display in frontend + Config::inst()->update('CommentableItem', 'comments', array( + 'require_moderation' => true, + 'frontend_moderation' => true, + 'frontend_spam' => true, + )); + if($member = Member::currentUser()) $member->logOut(); + $this->assertEquals(1, $item->Comments()->Count()); + + $this->logInWithPermission('ADMIN'); + $this->assertEquals(4, $item->Comments()->Count()); } /**