Passed
Push — main ( a0e9c5...805967 )
by Paul
07:42
created

Notification::recipients()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 25
ccs 0
cts 20
cp 0
rs 9.3222
c 0
b 0
f 0
cc 5
nc 12
nop 0
crap 30
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use GeminiLabs\SiteReviews\Database\OptionManager;
6
use GeminiLabs\SiteReviews\Helpers\Arr;
7
use GeminiLabs\SiteReviews\Modules\Html\TemplateTags;
8
use GeminiLabs\SiteReviews\Review;
9
10
class Notification
11
{
12
    /**
13
     * @var Review
14
     */
15
    protected $review;
16
17
    /**
18
     * @var array
19
     */
20
    protected $types;
21
22
    public function __construct()
23
    {
24
        $this->types = glsr_get_option('general.notifications', [], 'array');
25
    }
26
27
    public function send(Review $review): void
28
    {
29
        $this->review = $review;
30
        if (!empty(array_intersect(['admin', 'author', 'custom'], $this->types))) {
31
            $this->sendToEmail();
32
        }
33
        if (in_array('discord', $this->types)) {
34
            $this->sendToDiscord();
35
        }
36
        if (in_array('slack', $this->types)) {
37
            $this->sendToSlack();
38
        }
39
    }
40
41
    protected function buildEmail(): array
42
    {
43
        return [
44
            'to' => $this->recipients(),
45
            'subject' => $this->subject(true),
46
            'template' => 'default',
47
            'template-tags' => glsr(TemplateTags::class)->tags($this->review, [
48
                'include' => [
49
                    'approve_url',
50
                    'edit_url',
51
                    'review_assigned_links',
52
                    'review_assigned_posts',
53
                    'review_assigned_terms',
54
                    'review_assigned_users',
55
                    'review_author',
56
                    'review_categories',
57
                    'review_content',
58
                    'review_email',
59
                    'review_id',
60
                    'review_ip',
61
                    'review_link',
62
                    'review_rating',
63
                    'review_title',
64
                    'site_title',
65
                    'site_url',
66
                ],
67
            ]),
68
        ];
69
    }
70
71
    protected function recipients(): array
72
    {
73
        $emails = [];
74
        if (in_array('admin', $this->types)) {
75
            $emails[] = glsr(OptionManager::class)->getWP('admin_email');
76
        }
77
        if (in_array('author', $this->types)) {
78
            $posts = $this->review->assignedPosts();
79
            $userIds = wp_list_pluck($posts, 'post_author');
80
            if (!empty($userIds)) {
81
                $users = get_users(['fields' => ['user_email'], 'include' => $userIds]);
82
                $userEmails = wp_list_pluck($users, 'user_email');
83
                $emails = array_merge($emails, $userEmails);
84
            }
85
        }
86
        if (in_array('custom', $this->types)) {
87
            $customEmails = glsr_get_option('general.notification_email', '', 'string');
88
            $customEmails = str_replace([' ', ',', ';'], ',', $customEmails);
89
            $customEmails = explode(',', $customEmails);
90
            $emails = array_merge($emails, $customEmails);
91
        }
92
        $emails = glsr()->filterArray('notification/emails', $emails, $this->review);
93
        $emails = array_map([glsr(Sanitizer::class), 'sanitizeEmail'], $emails);
94
        $emails = Arr::reindex(Arr::unique($emails));
95
        return $emails;
96
    }
97
98
    protected function sendToDiscord(): void
99
    {
100
        $notification = glsr(Discord::class)->compose($this->review, [
101
            'assigned_links' => glsr(TemplateTags::class)->tagReviewAssignedLinks($this->review, '[%2$s](%1$s)'),
102
            'header' => $this->subject(),
103
        ]);
104
        $notification->send();
105
    }
106
107
    protected function sendToEmail(): void
108
    {
109
        $notification = glsr(Email::class)->compose($this->buildEmail(), [
110
            'review' => $this->review,
111
        ]);
112
        $notification->send();
113
    }
114
115
    protected function sendToSlack(): void
116
    {
117
        $notification = glsr(Slack::class)->compose($this->review, [
118
            'assigned_links' => glsr(TemplateTags::class)->tagReviewAssignedLinks($this->review, '<%s|%s>'),
119
            'header' => $this->subject(),
120
        ]);
121
        $notification->send();
122
    }
123
124
    protected function subject(bool $withPostAssignment = false): string
125
    {
126
        $siteTitle = wp_specialchars_decode(glsr(OptionManager::class)->getWP('blogname'), ENT_QUOTES);
127
        $title = sprintf(__('New %s-star review', 'site-reviews'), $this->review->rating);
128
        if ($withPostAssignment) {
129
            $postAssignments = glsr(TemplateTags::class)->tagReviewAssignedPosts($this->review);
130
            if (!empty($postAssignments)) {
131
                $title = sprintf(__('New %s-star review of %s', 'site-reviews'), $this->review->rating, $postAssignments);
132
            }
133
        }
134
        $title = sprintf('[%s] %s', $siteTitle, $title);
135
        return glsr()->filterString('notification/title', $title, $this->review);
136
    }
137
}
138