Passed
Push — master ( 71fe18...341f99 )
by Paul
05:12
created

Slack::buildContentField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 6
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\Defaults\SlackDefaults;
7
use GeminiLabs\SiteReviews\Modules\Rating;
8
use GeminiLabs\SiteReviews\Review;
9
use WP_Error;
10
11
class Slack
12
{
13
	/**
14
	 * @var string
15
	 */
16
	public $endpoint;
17
18
	/**
19
	 * @var array
20
	 */
21
	public $notification;
22
23
	/**
24
	 * @var Review
25
	 */
26
	public $review;
27
28
	public function __construct()
29
	{
30
		$this->endpoint = glsr( OptionManager::class )->get( 'settings.general.notification_slack' );
31
	}
32
33
	/**
34
	 * @return Slack
35
	 */
36
	public function compose( Review $review, array $notification )
37
	{
38
		if( empty( $this->endpoint ))return;
39
		$args = shortcode_atts( glsr( SlackDefaults::class )->defaults(), $notification );
40
		$this->review = $review;
41
		$notification = [
42
			'icon_url' => $args['icon_url'],
43
			'username' => $args['username'],
44
			'attachments' => [[
45
				'actions' => $this->buildAction( $args ),
46
				'pretext' => $args['pretext'],
47
				'color' => $args['color'],
48
				'fallback' => $args['fallback'],
49
				'fields' => $this->buildFields(),
50
			]],
51
		];
52
		$this->notification = apply_filters( 'site-reviews/slack/compose', $notification, $this );
53
		return $this;
54
	}
55
56
	/**
57
	 * @return WP_Error|array
58
	 */
59
	public function send()
60
	{
61
		if( empty( $this->endpoint )) {
62
			return new WP_Error( 'slack', 'Slack notification was not sent: missing endpoint' );
63
		}
64
		return wp_remote_post( $this->endpoint, [
65
			'blocking' => false,
66
			'body' => json_encode( $this->notification ),
67
			'headers' => ['Content-Type' => 'application/json'],
68
			'httpversion' => '1.0',
69
			'method' => 'POST',
70
			'redirection' => 5,
71
			'sslverify' => false,
72
			'timeout' => 45,
73
		]);
74
	}
75
76
	/**
77
	 * @return array
78
	 */
79
	protected function buildAction( array $args )
80
	{
81
		return [[
82
			'text' => $args['button_text'],
83
			'type' => 'button',
84
			'url' => $args['button_url'],
85
		]];
86
	}
87
88
	/**
89
	 * @return array
90
	 */
91
	protected function buildAuthorField()
92
	{
93
		$email = !empty( $this->review->email )
94
			? '<'.$this->review->email.'>'
95
			: '';
96
		$author = trim( rtrim( $this->review->author ).' '.$email );
97
		return ['value' => implode( ' - ', array_filter( [$author, $this->review->ip_address] ))];
98
	}
99
100
	/**
101
	 * @return array
102
	 */
103
	protected function buildContentField()
104
	{
105
		return !empty( $this->review->content )
106
			? ['value' => $this->review->content]
107
			: [];
108
	}
109
110
	/**
111
	 * @return array
112
	 */
113
	protected function buildFields()
114
	{
115
		$fields = [
116
			$this->buildStarsField(),
117
			$this->buildTitleField(),
118
			$this->buildContentField(),
119
			$this->buildAuthorField(),
120
		];
121
		return array_filter( $fields );
122
	}
123
124
	/**
125
	 * @return array
126
	 */
127
	protected function buildStarsField()
128
	{
129
		$solidStars = str_repeat( '★', $this->review->rating );
130
		$emptyStars = str_repeat( '☆', max( 0, Rating::MAX_RATING - $this->review->rating ));
131
		$stars = $solidStars.$emptyStars;
132
		$stars = apply_filters( 'site-reviews/slack/stars', $stars, $this->review->rating, Rating::MAX_RATING );
133
		return ['title' => $stars];
134
	}
135
136
	/**
137
	 * @return array
138
	 */
139
	protected function buildTitleField()
140
	{
141
		return !empty( $this->review->title )
142
			? ['title' => $this->review->title]
143
			: [];
144
	}
145
}
146