silverstripe/comment-notifications

为访客评论提供电子邮件通知

安装次数: 135,188

依赖项: 1

建议者: 0

安全性: 0

星标: 3

关注者: 9

分支: 7

开放问题: 0

类型:silverstripe-vendormodule

3.0.0 2023-04-27 02:54 UTC

README

CI

为当新访客评论发布时提供简单的电子邮件通知。

安装

composer require silverstripe/comment-notifications

配置

要将默认接收通知的电子邮件地址配置为,请在 mysite/_config.yml 中放置以下内容

SilverStripe\Control\Email\Email:
  admin_email: 'will@fullscreen.io'

查看 CommentNotifiable 类以获取您可以在项目中覆盖的选项列表。

配置接收者

要定义谁接收评论通知,请定义一个 updateNotificationRecipients 方法并修改电子邮件地址列表。

mysite/code/CommentNotificationExtension.php

<?php

use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Comments\Model\Comment;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Security\Group;

class CommentNotificationExtension extends DataExtension
{
    /**
     * @param array $existing
     * @param Comment $comment
     */
    public function updateNotificationRecipients(&$existing, $comment)
    {
        // send notification of the comment to all administrators in the CMS
        $admin = Group::get()->filter('Code', 'admin');

        foreach ($admin as $group) {
            foreach ($group->Members() as $member) {
                $existing[] = $member->Email;
            }
        }

        // or, notify the user who originally created the page
        $page = $comment->Parent();
        if ($page instanceof SiteTree) {
            /** @var ArrayList $pageVersion */
            $pageVersion = $page->allVersions('', '', 1); // get the original version
            if ($pageVersion && $pageVersion->count()) {
                $existing[] = $pageVersion->first()->Author()->Email;
            }
        }
    }
}

CommentNotificationExtension 应用到任何启用了评论的类(例如 SiteTree)

mysite/_config/extensions.yml

SilverStripe\CMS\Model\SiteTree:
  extensions:
    - CommentNotificationExtension