|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Modules\Email; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Review; |
|
8
|
|
|
use WP_Post; |
|
9
|
|
|
|
|
10
|
|
|
class Notification |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var bool |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $email; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var Review |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $review; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var bool |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $slack; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $types; |
|
31
|
|
|
|
|
32
|
1 |
|
public function __construct() |
|
33
|
|
|
{ |
|
34
|
1 |
|
$this->types = glsr( OptionManager::class )->get( 'settings.general.notification', [] ); |
|
35
|
1 |
|
$this->email = count( array_intersect( ['admin', 'author', 'custom'], $types )) > 0; |
|
|
|
|
|
|
36
|
|
|
$this->slack = in_array( 'slack', $types ); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return void |
|
41
|
|
|
*/ |
|
42
|
|
|
public function send( Review $review ) |
|
43
|
|
|
{ |
|
44
|
|
|
if( empty( $this->types ))return; |
|
45
|
|
|
$this->review = $review; |
|
46
|
|
|
$args = [ |
|
47
|
|
|
'link' => $this->getLink(), |
|
48
|
|
|
'title' => $this->getTitle(), |
|
49
|
|
|
]; |
|
50
|
|
|
if( $this->email ) { |
|
51
|
|
|
$this->sendToEmail( $args ); |
|
52
|
|
|
} |
|
53
|
|
|
if( $this->slack ) { |
|
54
|
|
|
$this->sendToSlack( $args ); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return Email |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function buildEmail( array $args ) |
|
62
|
|
|
{ |
|
63
|
|
|
return glsr( Email::class )->compose([ |
|
64
|
|
|
'to' => $this->getEmailAddresses(), |
|
65
|
|
|
'subject' => $args['title'], |
|
66
|
|
|
'template' => 'email-notification', |
|
67
|
|
|
'template-tags' => [ |
|
68
|
|
|
'review_author' => $this->review->author, |
|
69
|
|
|
'review_content' => $this->review->content, |
|
70
|
|
|
'review_email' => $this->review->email, |
|
71
|
|
|
'review_ip' => $this->review->ip_address, |
|
72
|
|
|
'review_link' => sprintf( '<a href="%1$s">%1$s</a>', $args['link'] ), |
|
73
|
|
|
'review_rating' => $this->review->rating, |
|
74
|
|
|
'review_title' => $this->review->title, |
|
75
|
|
|
], |
|
76
|
|
|
]); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return Email |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function buildSlackNotification( array $args ) |
|
83
|
|
|
{ |
|
84
|
|
|
return glsr( Slack::class )->compose( $review, [ |
|
|
|
|
|
|
85
|
|
|
'fallback' => $this->buildEmail( $args )->read( 'plaintext' ), |
|
86
|
|
|
'link' => sprintf( '<%s|%s>', $args['link'], __( 'View Review', 'site-reviews' )), |
|
87
|
|
|
'pretext' => $args['title'], |
|
88
|
|
|
]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function getEmailAddresses() |
|
95
|
|
|
{ |
|
96
|
|
|
$emails = []; |
|
97
|
|
|
if( in_array( 'admin', $this->types )) { |
|
98
|
|
|
$emails[] = get_option( 'admin_email' ); |
|
99
|
|
|
} |
|
100
|
|
|
if( in_array( 'author', $this->types )) { |
|
101
|
|
|
$assignedPost = get_post( intval( $this->review->assigned_to )); |
|
102
|
|
|
if( $assignedPost instanceof WP_Post ) { |
|
103
|
|
|
$emails[] = get_the_author_meta( 'user_email', $assignedPost->ID ); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
if( in_array( 'custom', $this->types )) { |
|
107
|
|
|
$customEmails = glsr( OptionManager::class )->get( 'settings.general.notification_email' ); |
|
108
|
|
|
$customEmails = str_replace( [' ', ',', ';'], ',', $customEmails ); |
|
109
|
|
|
$customEmails = explode( ',', $customEmails ); |
|
110
|
|
|
$emails = array_merge( $emails, $customEmails ); |
|
111
|
|
|
} |
|
112
|
|
|
$emails = array_filter( array_keys( array_flip( $emails ))); |
|
113
|
|
|
return apply_filters( 'site-reviews/notification/emails', $emails, $this->review ); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function getLink() |
|
120
|
|
|
{ |
|
121
|
|
|
return admin_url( 'post.php?post='.$this->review->ID.'&action=edit' ); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function getTitle() |
|
128
|
|
|
{ |
|
129
|
|
|
$assignedTitle = get_the_title( intval( $this->review->assigned_to )); |
|
130
|
|
|
$title = _nx( |
|
131
|
|
|
'New %s-star review', |
|
132
|
|
|
'New %s-star review of: %s', |
|
133
|
|
|
intval( empty( $assignedTitle )), |
|
134
|
|
|
'This string differs depending on whether or not the review has been assigned to a post.', |
|
135
|
|
|
'site-reviews' |
|
136
|
|
|
); |
|
137
|
|
|
$title = sprintf( '[%s] %s', |
|
138
|
|
|
wp_specialchars_decode( strval( get_option( 'blogname' )), ENT_QUOTES ), |
|
139
|
|
|
sprintf( $title, $this->review->rating, $assignedTitle ) |
|
140
|
|
|
); |
|
141
|
|
|
return apply_filters( 'site-reviews/notification/title', $title, $this->review ); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return void |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function sendToEmail( array $args ) |
|
148
|
|
|
{ |
|
149
|
|
|
$email = $this->buildEmail( $args ); |
|
150
|
|
|
if( empty( $email->to )) { |
|
151
|
|
|
glsr_log()->error( 'Email notification was not sent: missing email address' ); |
|
152
|
|
|
return; |
|
153
|
|
|
} |
|
154
|
|
|
if( $email->send() === false ) { |
|
155
|
|
|
glsr_log()->error( 'Email notification was not sent: wp_mail() failed' )->debug( $email ); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @return void |
|
161
|
|
|
*/ |
|
162
|
|
|
protected function sendToSlack( array $args ) |
|
163
|
|
|
{ |
|
164
|
|
|
$notification = $this->buildSlackNotification( $args ); |
|
165
|
|
|
$result = $notification->send(); |
|
166
|
|
|
if( is_wp_error( $result )) { |
|
167
|
|
|
$notification->review = null; |
|
|
|
|
|
|
168
|
|
|
glsr_log()->error( $result->get_error_message() )->debug( $notification ); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|