Passed
Push — master ( 7078d4...295d40 )
by Paul
06:31
created

Notification::buildSlackNotification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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