Use short brackets, injection where possible and type hint injector created objects

This commit is contained in:
Robbie Averill 2018-09-24 18:16:03 +02:00
parent c0ca79090b
commit ec60328a0c
5 changed files with 78 additions and 71 deletions

View File

@ -3,22 +3,19 @@
namespace SilverStripe\Comments\Admin; namespace SilverStripe\Comments\Admin;
use SilverStripe\Admin\LeftAndMain; use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Comments\Admin\CommentsGridField;
use SilverStripe\Comments\Model\Comment; use SilverStripe\Comments\Model\Comment;
use SilverStripe\Forms\Tab;
use SilverStripe\Forms\TabSet;
use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form; use SilverStripe\Forms\Form;
use SilverStripe\Security\PermissionProvider; use SilverStripe\Forms\Tab;
use SilverStripe\Forms\TabSet;
use SilverStripe\Security\Security; use SilverStripe\Security\Security;
use SilverStripe\View\SSViewer;
/** /**
* Comment administration system within the CMS * Comment administration system within the CMS
* *
* @package comments * @package comments
*/ */
class CommentAdmin extends LeftAndMain implements PermissionProvider class CommentAdmin extends LeftAndMain
{ {
private static $url_segment = 'comments'; private static $url_segment = 'comments';
@ -28,7 +25,7 @@ class CommentAdmin extends LeftAndMain implements PermissionProvider
private static $menu_icon_class = 'font-icon-comment'; private static $menu_icon_class = 'font-icon-comment';
private static $allowed_actions = array( private static $allowed_actions = [
'approvedmarked', 'approvedmarked',
'deleteall', 'deleteall',
'deletemarked', 'deletemarked',
@ -37,18 +34,18 @@ class CommentAdmin extends LeftAndMain implements PermissionProvider
'spammarked', 'spammarked',
'EditForm', 'EditForm',
'unmoderated' 'unmoderated'
); ];
private static $required_permission_codes = 'CMS_ACCESS_CommentAdmin'; private static $required_permission_codes = 'CMS_ACCESS_CommentAdmin';
public function providePermissions() public function providePermissions()
{ {
return array( return [
"CMS_ACCESS_CommentAdmin" => array( 'CMS_ACCESS_CommentAdmin' => [
'name' => _t(__CLASS__ . '.ADMIN_PERMISSION', "Access to 'Comments' section"), 'name' => _t(__CLASS__ . '.ADMIN_PERMISSION', "Access to 'Comments' section"),
'category' => _t('SilverStripe\\Security\\Permission.CMS_ACCESS_CATEGORY', 'CMS Access') 'category' => _t('SilverStripe\\Security\\Permission.CMS_ACCESS_CATEGORY', 'CMS Access')
) ],
); ];
} }
/** /**
@ -69,7 +66,7 @@ class CommentAdmin extends LeftAndMain implements PermissionProvider
$newComments = Comment::get()->filter('Moderated', 0); $newComments = Comment::get()->filter('Moderated', 0);
$newGrid = new CommentsGridField( $newGrid = CommentsGridField::create(
'NewComments', 'NewComments',
'', '',
$newComments, $newComments,
@ -78,7 +75,7 @@ class CommentAdmin extends LeftAndMain implements PermissionProvider
$approvedComments = Comment::get()->filter('Moderated', 1)->filter('IsSpam', 0); $approvedComments = Comment::get()->filter('Moderated', 1)->filter('IsSpam', 0);
$approvedGrid = new CommentsGridField( $approvedGrid = CommentsGridField::create(
'ApprovedComments', 'ApprovedComments',
'', '',
$approvedComments, $approvedComments,
@ -87,7 +84,7 @@ class CommentAdmin extends LeftAndMain implements PermissionProvider
$spamComments = Comment::get()->filter('Moderated', 1)->filter('IsSpam', 1); $spamComments = Comment::get()->filter('Moderated', 1)->filter('IsSpam', 1);
$spamGrid = new CommentsGridField( $spamGrid = CommentsGridField::create(
'SpamComments', 'SpamComments',
'', '',
$spamComments, $spamComments,
@ -140,7 +137,7 @@ class CommentAdmin extends LeftAndMain implements PermissionProvider
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); $form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
if ($form->Fields()->hasTabset()) { if ($form->Fields()->hasTabset()) {
$form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet'); $form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet');
$form->addExtraClass('center ss-tabset cms-tabset ' . $this->BaseCSSClasses()); $form->addExtraClass('center ss-tabset cms-tabset ' . $this->BaseCSSClasses());
} }

View File

@ -2,7 +2,6 @@
namespace SilverStripe\Comments\Admin; namespace SilverStripe\Comments\Admin;
use SilverStripe\Forms\FormField;
use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridField;
use SilverStripe\View\HTML; use SilverStripe\View\HTML;

View File

@ -26,7 +26,7 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act
*/ */
public function getColumnAttributes($gridField, $record, $columnName) public function getColumnAttributes($gridField, $record, $columnName)
{ {
return array('class' => 'col-buttons'); return ['class' => 'col-buttons'];
} }
/** /**
@ -34,8 +34,8 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act
*/ */
public function getColumnMetadata($gridField, $columnName) public function getColumnMetadata($gridField, $columnName)
{ {
if ($columnName == 'Actions') { if ($columnName === 'Actions') {
return array('title' => ''); return ['title' => ''];
} }
} }
@ -44,7 +44,7 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act
*/ */
public function getColumnsHandled($gridField) public function getColumnsHandled($gridField)
{ {
return array('Actions'); return ['Actions'];
} }
/** /**
@ -64,7 +64,7 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act
'CustomAction' . $record->ID . 'Spam', 'CustomAction' . $record->ID . 'Spam',
_t(__CLASS__ . '.SPAM', 'Spam'), _t(__CLASS__ . '.SPAM', 'Spam'),
'spam', 'spam',
array('RecordID' => $record->ID) ['RecordID' => $record->ID]
) )
->addExtraClass('btn btn-secondary grid-field__icon-action') ->addExtraClass('btn btn-secondary grid-field__icon-action')
->Field(); ->Field();
@ -76,7 +76,7 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act
'CustomAction' . $record->ID . 'Approve', 'CustomAction' . $record->ID . 'Approve',
_t(__CLASS__ . '.APPROVE', 'Approve'), _t(__CLASS__ . '.APPROVE', 'Approve'),
'approve', 'approve',
array('RecordID' => $record->ID) ['RecordID' => $record->ID]
) )
->addExtraClass('btn btn-secondary grid-field__icon-action') ->addExtraClass('btn btn-secondary grid-field__icon-action')
->Field(); ->Field();
@ -90,7 +90,7 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act
*/ */
public function getActions($gridField) public function getActions($gridField)
{ {
return array('spam', 'approve'); return ['spam', 'approve'];
} }
/** /**
@ -98,7 +98,8 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act
*/ */
public function handleAction(GridField $gridField, $actionName, $arguments, $data) public function handleAction(GridField $gridField, $actionName, $arguments, $data)
{ {
if ($actionName == 'spam') { if ($actionName === 'spam') {
/** @var Comment $comment */
$comment = Comment::get()->byID($arguments['RecordID']); $comment = Comment::get()->byID($arguments['RecordID']);
$comment->markSpam(); $comment->markSpam();
@ -109,7 +110,8 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act
); );
} }
if ($actionName == 'approve') { if ($actionName === 'approve') {
/** @var Comment $comment */
$comment = Comment::get()->byID($arguments['RecordID']); $comment = Comment::get()->byID($arguments['RecordID']);
$comment->markApproved(); $comment->markApproved();

View File

@ -20,8 +20,9 @@ class CommentsGridFieldConfig extends GridFieldConfig_RecordEditor
$this->addComponent(new CommentsGridFieldAction()); $this->addComponent(new CommentsGridFieldAction());
// Format column // Format column
/** @var GridFieldDataColumns $columns */
$columns = $this->getComponentByType(GridFieldDataColumns::class); $columns = $this->getComponentByType(GridFieldDataColumns::class);
$columns->setFieldFormatting(array( $columns->setFieldFormatting([
'ParentTitle' => function ($value, &$item) { 'ParentTitle' => function ($value, &$item) {
return sprintf( return sprintf(
'<a href="%s" class="cms-panel-link external-link action" target="_blank">%s</a>', '<a href="%s" class="cms-panel-link external-link action" target="_blank">%s</a>',
@ -29,10 +30,10 @@ class CommentsGridFieldConfig extends GridFieldConfig_RecordEditor
$item->obj('ParentTitle')->forTemplate() $item->obj('ParentTitle')->forTemplate()
); );
} }
)); ]);
// Add bulk option // Add bulk option
$manager = new BulkManager(null, false); $manager = BulkManager::create(null, false);
$spamAction = SpamHandler::create()->setLabel(_t(__CLASS__ . '.SPAM', 'Spam')); $spamAction = SpamHandler::create()->setLabel(_t(__CLASS__ . '.SPAM', 'Spam'));
$approveAction = ApproveHandler::create()->setLabel(_t(__CLASS__ . '.APPROVE', 'Approve')); $approveAction = ApproveHandler::create()->setLabel(_t(__CLASS__ . '.APPROVE', 'Approve'));

View File

@ -4,20 +4,21 @@ namespace SilverStripe\Comments\Controllers;
use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Comments\Extensions\CommentsExtension; use SilverStripe\Comments\Extensions\CommentsExtension;
use SilverStripe\Comments\Forms\CommentForm;
use SilverStripe\Comments\Model\Comment; use SilverStripe\Comments\Model\Comment;
use SilverStripe\Control\Director;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Control\Email\Email; use SilverStripe\Control\Director;
use SilverStripe\Control\HTTP; use SilverStripe\Control\HTTP;
use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Control\RSS\RSSFeed; use SilverStripe\Control\RSS\RSSFeed;
use SilverStripe\Control\Session;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\PaginatedList;
use SilverStripe\Security\Member;
use SilverStripe\Security\Security;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Comments\Forms\CommentForm; use SilverStripe\Forms\Form;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\ORM\PaginatedList;
use SilverStripe\Security\Security;
/** /**
* @package comments * @package comments
@ -27,7 +28,7 @@ class CommentingController extends Controller
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
private static $allowed_actions = array( private static $allowed_actions = [
'delete', 'delete',
'spam', 'spam',
'ham', 'ham',
@ -36,15 +37,15 @@ class CommentingController extends Controller
'CommentsForm', 'CommentsForm',
'reply', 'reply',
'doPostComment', 'doPostComment',
'doPreviewComment' 'doPreviewComment',
); ];
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
private static $url_handlers = array( private static $url_handlers = [
'reply/$ParentCommentID//$ID/$OtherID' => 'reply', 'reply/$ParentCommentID//$ID/$OtherID' => 'reply',
); ];
/** /**
* Fields required for this form * Fields required for this form
@ -52,11 +53,11 @@ class CommentingController extends Controller
* @var array * @var array
* @config * @config
*/ */
private static $required_fields = array( private static $required_fields = [
'Name', 'Name',
'Email', 'Email',
'Comment' 'Comment',
); ];
/** /**
* Parent class this commenting form is for * Parent class this commenting form is for
@ -70,21 +71,21 @@ class CommentingController extends Controller
* *
* @var DataObject * @var DataObject
*/ */
private $ownerRecord = null; private $ownerRecord;
/** /**
* Parent controller record * Parent controller record
* *
* @var Controller * @var Controller
*/ */
private $ownerController = null; private $ownerController;
/** /**
* Backup url to return to * Backup url to return to
* *
* @var string * @var string
*/ */
protected $fallbackReturnURL = null; protected $fallbackReturnURL;
/** /**
* Set the parent class name to use * Set the parent class name to use
@ -178,6 +179,7 @@ class CommentingController extends Controller
{ {
// If possible use the current record // If possible use the current record
if ($record = $this->getOwnerRecord()) { if ($record = $this->getOwnerRecord()) {
/** @var DataObject|CommentsExtension $record */
return $record->getCommentsOption($key); return $record->getCommentsOption($key);
} }
@ -198,6 +200,7 @@ class CommentingController extends Controller
public function getOptions() public function getOptions()
{ {
if ($record = $this->getOwnerRecord()) { if ($record = $this->getOwnerRecord()) {
/** @var DataObject|CommentsExtension $record */
return $record->getCommentsOptions(); return $record->getCommentsOptions();
} }
@ -226,7 +229,7 @@ class CommentingController extends Controller
/** /**
* Outputs the RSS feed of comments * Outputs the RSS feed of comments
* *
* @return HTMLText * @return DBHTMLText
*/ */
public function rss() public function rss()
{ {
@ -252,10 +255,10 @@ class CommentingController extends Controller
$class = SiteTree::class; $class = SiteTree::class;
} }
$comments = Comment::get()->filter(array( $comments = Comment::get()->filter([
'Moderated' => 1, 'Moderated' => 1,
'IsSpam' => 0, 'IsSpam' => 0,
)); ]);
// Check if class filter // Check if class filter
if ($class) { if ($class) {
@ -275,10 +278,10 @@ class CommentingController extends Controller
} }
$title = _t(__CLASS__ . '.RSSTITLE', "Comments RSS Feed"); $title = _t(__CLASS__ . '.RSSTITLE', "Comments RSS Feed");
$comments = new PaginatedList($comments, $request); $comments = PaginatedList::create($comments, $request);
$comments->setPageLength($this->getOption('comments_per_page')); $comments->setPageLength($this->getOption('comments_per_page'));
return new RSSFeed( return RSSFeed::create(
$comments, $comments,
$link, $link,
$title, $title,
@ -375,6 +378,9 @@ class CommentingController extends Controller
* Redirect back to referer if available, ensuring that only site URLs * Redirect back to referer if available, ensuring that only site URLs
* are allowed to avoid phishing. If it's an AJAX request render the * are allowed to avoid phishing. If it's an AJAX request render the
* comment in it's new state * comment in it's new state
*
* @param Comment $comment
* @return DBHTMLText|HTTPResponse|false
*/ */
private function renderChangedCommentState($comment) private function renderChangedCommentState($comment)
{ {
@ -383,21 +389,21 @@ class CommentingController extends Controller
// Render comment using AJAX // Render comment using AJAX
if ($this->request->isAjax()) { if ($this->request->isAjax()) {
return $comment->renderWith('Includes/CommentsInterface_singlecomment'); return $comment->renderWith('Includes/CommentsInterface_singlecomment');
} else {
// Redirect to either the comment or start of the page
if (empty($referer)) {
return $this->redirectBack();
} else {
// Redirect to the comment, but check for phishing
$url = $referer . '#comment-' . $comment->ID;
// absolute redirection URLs not located on this site may cause phishing
if (Director::is_site_url($url)) {
return $this->redirect($url);
} else {
return false;
}
}
} }
// Redirect to either the comment or start of the page
if (empty($referer)) {
return $this->redirectBack();
}
// Redirect to the comment, but check for phishing
$url = $referer . '#comment-' . $comment->ID;
// absolute redirection URLs not located on this site may cause phishing
if (Director::is_site_url($url)) {
return $this->redirect($url);
}
return false;
} }
/** /**
@ -411,6 +417,7 @@ class CommentingController extends Controller
$id = isset($this->urlParams['ID']) ? $this->urlParams['ID'] : false; $id = isset($this->urlParams['ID']) ? $this->urlParams['ID'] : false;
if ($id) { if ($id) {
/** @var Comment $comment */
$comment = Comment::get()->byId($id); $comment = Comment::get()->byId($id);
if ($comment) { if ($comment) {
$this->fallbackReturnURL = $comment->Link(); $this->fallbackReturnURL = $comment->Link();
@ -436,9 +443,9 @@ class CommentingController extends Controller
$form->addExtraClass('reply-form'); $form->addExtraClass('reply-form');
// Load parent into reply form // Load parent into reply form
$form->loadDataFrom(array( $form->loadDataFrom([
'ParentCommentID' => $comment->ID 'ParentCommentID' => $comment->ID
)); ]);
// Customise action // Customise action
$form->setFormAction($this->Link('reply', $comment->ID)); $form->setFormAction($this->Link('reply', $comment->ID));
@ -461,6 +468,7 @@ class CommentingController extends Controller
{ {
// Extract parent comment from reply and build this way // Extract parent comment from reply and build this way
if ($parentID = $request->param('ParentCommentID')) { if ($parentID = $request->param('ParentCommentID')) {
/** @var Comment $comment */
$comment = DataObject::get_by_id(Comment::class, $parentID, true); $comment = DataObject::get_by_id(Comment::class, $parentID, true);
if ($comment) { if ($comment) {
return $this->ReplyForm($comment); return $this->ReplyForm($comment);
@ -518,8 +526,8 @@ class CommentingController extends Controller
// absolute redirection URLs not located on this site may cause phishing // absolute redirection URLs not located on this site may cause phishing
if (Director::is_site_url($url)) { if (Director::is_site_url($url)) {
return $this->redirect($url); return $this->redirect($url);
} else {
return false;
} }
return false;
} }
} }