From d38e7c5b67bcb159c68a15bbec9494c8116aaf43 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Sun, 28 Oct 2018 21:31:19 +0000 Subject: [PATCH 1/7] FIX Replace Convert JSON methods with json_* methods, deprecated from SilverStripe 4.4 --- src/Admin/CommentsGridFieldBulkAction/CommentHandler.php | 2 +- src/Admin/CommentsGridFieldBulkAction/Handler.php | 4 ++-- src/Forms/CommentForm.php | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Admin/CommentsGridFieldBulkAction/CommentHandler.php b/src/Admin/CommentsGridFieldBulkAction/CommentHandler.php index b22d04f..f2e1739 100644 --- a/src/Admin/CommentsGridFieldBulkAction/CommentHandler.php +++ b/src/Admin/CommentsGridFieldBulkAction/CommentHandler.php @@ -27,7 +27,7 @@ abstract class CommentHandler extends Handler $this->updateComment($comment); } - $response = new HTTPResponse(Convert::raw2json([ + $response = new HTTPResponse(json_encode([ 'done' => true, 'records' => $ids, ])); diff --git a/src/Admin/CommentsGridFieldBulkAction/Handler.php b/src/Admin/CommentsGridFieldBulkAction/Handler.php index d47be6b..ced63e4 100644 --- a/src/Admin/CommentsGridFieldBulkAction/Handler.php +++ b/src/Admin/CommentsGridFieldBulkAction/Handler.php @@ -37,7 +37,7 @@ class Handler extends GridFieldBulkActionHandler $record->markSpam(); } - $response = new HTTPResponse(Convert::raw2json(array( + $response = new HTTPResponse(json_encode(array( 'done' => true, 'records' => $ids ))); @@ -60,7 +60,7 @@ class Handler extends GridFieldBulkActionHandler $record->markApproved(); } - $response = new HTTPResponse(Convert::raw2json(array( + $response = new HTTPResponse(json_encode(array( 'done' => true, 'records' => $ids ))); diff --git a/src/Forms/CommentForm.php b/src/Forms/CommentForm.php index 04c1a68..c8639f5 100644 --- a/src/Forms/CommentForm.php +++ b/src/Forms/CommentForm.php @@ -146,7 +146,7 @@ class CommentForm extends Form // load user data from previous form request back into form. if (array_key_exists('UserData', $data)) { - $formData = Convert::json2array($data['UserData']); + $formData = json_decode($data['UserData'], true); $this->loadDataFrom([ 'Name' => isset($formData['Name']) ? $formData['Name'] : '', @@ -199,7 +199,7 @@ class CommentForm extends Form // cache users data $form->setSessionData([ - 'UserData' => Convert::raw2json($data), + 'UserData' => json_encode($data), 'Comment' => $data['Comment'] ]); @@ -270,7 +270,7 @@ class CommentForm extends Form // cache users data (name, email, etc to prepopulate on other forms). $form->setSessionData([ - 'UserData' => Convert::raw2json($data), + 'UserData' => json_encode($data), ]); // Find parent link From 8c08f4337693557dad68d92b0197a4101dbec4c9 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Tue, 27 Nov 2018 13:19:55 +0100 Subject: [PATCH 2/7] FIX RSS feed for comments now render correctly The comment DataList filter applies ParentClass = $className - when this is the base class, it fails to recognise comments for a BlogPost. This change uses the late class name instead. --- src/Extensions/CommentsExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Extensions/CommentsExtension.php b/src/Extensions/CommentsExtension.php index ce0ec93..892d190 100644 --- a/src/Extensions/CommentsExtension.php +++ b/src/Extensions/CommentsExtension.php @@ -441,7 +441,7 @@ class CommentsExtension extends DataExtension { return Controller::join_links( $this->getCommentRSSLink(), - str_replace('\\', '-', $this->owner->baseClass()), + str_replace('\\', '-', get_class($this->owner)), $this->owner->ID ); } From e101b747782de711dc0e2db892cc96daae735d70 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Tue, 27 Nov 2018 13:26:42 +0100 Subject: [PATCH 3/7] FIX Capitalise comment moderation action labels and update i18n syntax in templates --- lang/en.yml | 8 ++++---- templates/Includes/CommentsInterface_singlecomment.ss | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lang/en.yml b/lang/en.yml index b059e8d..04eef78 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -18,10 +18,10 @@ en: CommentsInterface_pendingcomment_ss: AWAITINGMODERATION: 'Your comment has been submitted and is now awaiting moderation.' CommentsInterface_singlecomment_ss: - APPROVE: 'approve it' - ISNTSPAM: 'not spam' - ISSPAM: 'spam it' - REMCOM: 'reject it' + APPROVE: 'Approve it' + ISNTSPAM: 'Not spam' + ISSPAM: 'Spam it' + REMCOM: 'Reject it' REPLYTO: 'Reply to' CommentsInterface_ss: AWAITINGMODERATION: 'Your comment has been submitted and is now awaiting moderation.' diff --git a/templates/Includes/CommentsInterface_singlecomment.ss b/templates/Includes/CommentsInterface_singlecomment.ss index aab7204..73f644f 100755 --- a/templates/Includes/CommentsInterface_singlecomment.ss +++ b/templates/Includes/CommentsInterface_singlecomment.ss @@ -20,21 +20,21 @@ From dc1f8622e0144852554f1be0f6a405676708c1ed Mon Sep 17 00:00:00 2001 From: Dylan Wagstaff Date: Fri, 11 Jan 2019 11:48:51 +1300 Subject: [PATCH 4/7] FIX show name of members in summary tables Anonymous comments (posted by the public at large) must have a name and an email address associated with them. On the other hand, a logged in user will have the `Member` record details used for this information, via the Author relationship. However the summary fields do not allow for this, and only reference Name and Email on the Comment model directly, so any comment posted by a logged in member has no data for name and email displayed in the various GridFields in the CMS for administering comments (either per page, or in the global ModelAdmin). To recitfy this we can change the summary fields to use getter methods that will return the Comment model info, or fall back to the Author associated Member record if Name and Email are unset on the Comment. --- src/Model/Comment.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Model/Comment.php b/src/Model/Comment.php index 0dd6eec..59ea8c7 100755 --- a/src/Model/Comment.php +++ b/src/Model/Comment.php @@ -125,8 +125,8 @@ class Comment extends DataObject * {@inheritDoc} */ private static $summary_fields = array( - 'Name' => 'Submitted By', - 'Email' => 'Email', + 'getAuthorName' => 'Submitted By', + 'getAuthorEmail' => 'Email', 'Comment.LimitWordCount' => 'Comment', 'Created' => 'Date Posted', 'Parent.Title' => 'Post', @@ -450,6 +450,20 @@ class Comment extends DataObject } } + /** + * Return the comment authors email address + * + * @return string + */ + public function getAuthorEmail() + { + if ($this->Email) { + return $this->Email; + } elseif ($author = $this->Author()) { + return $author->Email; + } + } + /** * Generate a secure admin-action link authorised for the specified member * From a57c83c1211621685b136962f53cbaa8c708a881 Mon Sep 17 00:00:00 2001 From: Guy Marriott Date: Fri, 11 Jan 2019 12:22:31 +1300 Subject: [PATCH 5/7] Removing superflous i18n test and updating label test to only test labels specifically added --- tests/CommentsTest.php | 35 +---------------------------------- 1 file changed, 1 insertion(+), 34 deletions(-) diff --git a/tests/CommentsTest.php b/tests/CommentsTest.php index a319a90..4ef6cda 100644 --- a/tests/CommentsTest.php +++ b/tests/CommentsTest.php @@ -605,52 +605,19 @@ class CommentsTest extends FunctionalTest */ public function testFieldLabels() { - $locale = i18n::get_locale(); - i18n::set_locale('fr'); - /** @var Comment $comment */ $comment = $this->objFromFixture(Comment::class, 'firstComA'); - $labels = $comment->FieldLabels(); - $expected = array( - 'Name' => 'Nom de l\'Auteur', - 'Comment' => 'Commentaire', - 'Email' => 'Email', - 'URL' => 'URL', - 'Moderated' => 'Modéré?', - 'IsSpam' => 'Spam?', - 'AllowHtml' => 'Allow Html', - 'SecretToken' => 'Secret Token', - 'Depth' => 'Depth', - 'Author' => 'Author Member', - 'ParentComment' => 'Parent Comment', - 'ChildComments' => 'Child Comments', - 'ParentTitle' => 'Parent', - 'Created' => 'Date de publication', - 'Parent' => 'Parent' - ); - i18n::set_locale($locale); - foreach ($expected as $key => $value) { - $this->assertEquals($value, $labels[$key]); - } - $labels = $comment->FieldLabels(); $expected = array( 'Name' => 'Author Name', 'Comment' => 'Comment', 'Email' => 'Email', 'URL' => 'URL', - 'Moderated' => 'Moderated?', 'IsSpam' => 'Spam?', - 'AllowHtml' => 'Allow Html', - 'SecretToken' => 'Secret Token', - 'Depth' => 'Depth', - 'Author' => 'Author Member', - 'ParentComment' => 'Parent Comment', - 'ChildComments' => 'Child Comments', + 'Moderated' => 'Moderated?', 'ParentTitle' => 'Parent', 'Created' => 'Date posted', - 'Parent' => 'Parent' ); foreach ($expected as $key => $value) { $this->assertEquals($value, $labels[$key]); From 7691237fe5854af04a2047ba538b2285733ebc3b Mon Sep 17 00:00:00 2001 From: Guy Marriott Date: Fri, 11 Jan 2019 12:25:20 +1300 Subject: [PATCH 6/7] FIX Correcting inconsistent capitalisation of label --- lang/en.yml | 2 +- src/Model/Comment.php | 2 +- tests/CommentsTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lang/en.yml b/lang/en.yml index 04eef78..82c7592 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -74,7 +74,7 @@ en: EMAIL: Email ISSPAM: 'Spam?' MODERATED: 'Moderated?' - NAME: 'Author Name' + NAME: 'Author name' 'ON': 'on' OPTIONS: Options OPTION_DESCRIPTION: 'Unmoderated and spam comments will not be displayed until approved' diff --git a/src/Model/Comment.php b/src/Model/Comment.php index 0dd6eec..faa2b17 100755 --- a/src/Model/Comment.php +++ b/src/Model/Comment.php @@ -219,7 +219,7 @@ class Comment extends DataObject { $labels = parent::fieldLabels($includerelations); - $labels['Name'] = _t(__CLASS__ . '.NAME', 'Author Name'); + $labels['Name'] = _t(__CLASS__ . '.NAME', 'Author name'); $labels['Comment'] = _t(__CLASS__ . '.COMMENT', 'Comment'); $labels['Email'] = _t(__CLASS__ . '.EMAIL', 'Email'); $labels['URL'] = _t(__CLASS__ . '.URL', 'URL'); diff --git a/tests/CommentsTest.php b/tests/CommentsTest.php index 4ef6cda..9f9836e 100644 --- a/tests/CommentsTest.php +++ b/tests/CommentsTest.php @@ -610,7 +610,7 @@ class CommentsTest extends FunctionalTest $labels = $comment->FieldLabels(); $expected = array( - 'Name' => 'Author Name', + 'Name' => 'Author name', 'Comment' => 'Comment', 'Email' => 'Email', 'URL' => 'URL', From 3812057b0075cf37fccc5111fa35b5a754aa6031 Mon Sep 17 00:00:00 2001 From: Dylan Wagstaff Date: Fri, 11 Jan 2019 14:37:24 +1300 Subject: [PATCH 7/7] Add ability for commenters to enter a URL without a protocol When posting a comment on the a page with this module applied, there is an optional input for the commenter to give their URL,presumably their website. However this input currently validates (via JavaScript) to allow URLs only iff they have a protocol. Common use cases when someone is asked for their website in my experience is to then receive a URL without a protocol, confounded in that most web browsers will accept this form and automatically add the http protocol, where a webserver may then redirect to https by default. This means that all the magic happens behind the scenes and most folks don't particularly care to think about protocols when entering web addresses. Yest this input will only validate true and allow comment submission if they do. So now it will allow a protocol-less entry into the URL box. --- javascript/CommentsInterface.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/javascript/CommentsInterface.js b/javascript/CommentsInterface.js index 231266c..d27ab84 100755 --- a/javascript/CommentsInterface.js +++ b/javascript/CommentsInterface.js @@ -2,7 +2,16 @@ * @package comments */ (function($) { + // The above closure encapsulates the $ variable away from the global scope + // and the one below is the `$(document).ready(...)` shorthand. $(function() { + // Override the default URL validator in order to extend it to allow protocol-less URLs + $.validator.methods.url = function( value, element ) { + // This line is copied directly from the jQuery.validation source (version 1.19.0) + // the only change is a single question mark added here ---------v + return this.optional( element ) || /^(?:(?:(?:https?|ftp):)?\/\/)?(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})).?)(?::\d{2,5})?(?:[/?#]\S*)?$/i.test( value ); + } + /** * Enable form validation */