1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Application; |
6
|
|
|
use GeminiLabs\SiteReviews\Database; |
7
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
8
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
9
|
|
|
use GeminiLabs\SiteReviews\Review; |
10
|
|
|
|
11
|
|
|
class Trustalyze |
12
|
|
|
{ |
13
|
|
|
const API_URL = 'https://www.trustalyze.com/api/rbs/'; |
14
|
|
|
const WEB_URL = 'https://trustalyze.com/plans?ref=105'; |
15
|
|
|
|
16
|
|
|
public $message; |
17
|
|
|
public $response; |
18
|
|
|
public $success; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @return mixed |
22
|
|
|
*/ |
23
|
|
|
public function __get($key) |
24
|
|
|
{ |
25
|
|
|
return property_exists($this, $key) |
26
|
|
|
? $this->$key |
27
|
|
|
: Arr::get($this->response, $key, null); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return self |
32
|
|
|
*/ |
33
|
|
|
public function activateKey($apiKey = '', $email = '') |
34
|
|
|
{ |
35
|
|
|
$this->send('api_key_activation.php', [ |
36
|
|
|
'body' => [ |
37
|
|
|
'apikey' => $apiKey ?: 0, |
38
|
|
|
'domain' => get_home_url(), |
39
|
|
|
'email' => $email ?: 0, |
40
|
|
|
], |
41
|
|
|
]); |
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return self |
47
|
|
|
*/ |
48
|
|
|
public function reset() |
49
|
|
|
{ |
50
|
|
|
$this->message = ''; |
51
|
|
|
$this->response = []; |
52
|
|
|
$this->success = false; |
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return self |
58
|
|
|
*/ |
59
|
|
|
public function sendReview(Review $review) |
60
|
|
|
{ |
61
|
|
|
$this->send('index.php', [ |
62
|
|
|
'body' => $this->getBodyForReview($review), |
63
|
|
|
'timeout' => 120, |
64
|
|
|
]); |
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return self |
70
|
|
|
*/ |
71
|
|
|
public function sendReviewResponse(Review $review) |
72
|
|
|
{ |
73
|
|
|
$this->send('fetch_customer_reply.php', [ |
74
|
|
|
'body' => $this->getBodyForResponse($review), |
75
|
|
|
]); |
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
protected function getBodyForResponse(Review $review) |
83
|
|
|
{ |
84
|
|
|
$trustalyzeResponse = [ |
85
|
|
|
'reply' => Str::truncate($review->response, 300), |
86
|
|
|
'review_id' => glsr(Database::class)->get($review->ID, 'trustalyze'), // this is the trustalyze review ID |
87
|
|
|
'review_transaction_id' => $review->review_id, |
88
|
|
|
'type' => 'M', |
89
|
|
|
]; |
90
|
|
|
return apply_filters('site-reviews/trustalyze/response', $trustalyzeResponse, $review); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
protected function getBodyForReview(Review $review) |
97
|
|
|
{ |
98
|
|
|
$trustalyzeReview = [ |
99
|
|
|
'domain' => get_home_url(), |
100
|
|
|
'firstname' => Str::truncate(Str::convertName($review->author, 'first'), 25), |
101
|
|
|
'rate' => $review->rating, |
102
|
|
|
'review_transaction_id' => $review->review_id, |
103
|
|
|
'reviews' => Str::truncate($review->content, 280), |
104
|
|
|
'title' => Str::truncate($review->title, 35), |
105
|
|
|
'transaction' => Application::ID, // woocommerce field, not needed for Site Reviews |
106
|
|
|
]; |
107
|
|
|
return apply_filters('site-reviews/trustalyze/review', $trustalyzeReview, $review); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param \WP_Error|array $response |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
protected function handleResponse($response) |
115
|
|
|
{ |
116
|
|
|
if (is_wp_error($response)) { |
117
|
|
|
$this->message = $response->get_error_message(); |
118
|
|
|
} else { |
119
|
|
|
$responseBody = wp_remote_retrieve_body($response); |
120
|
|
|
$responseCode = wp_remote_retrieve_response_code($response); |
121
|
|
|
$responseData = (array) json_decode($responseBody, true); |
122
|
|
|
$this->response = array_shift($responseData); |
123
|
|
|
$this->message = Arr::get($this->response, 'msg'); |
124
|
|
|
$this->success = 'success' === Arr::get($this->response, 'result') || 'yes' === Arr::get($this->response, 'success'); // @todo remove this ugly hack! |
125
|
|
|
if (200 !== $responseCode) { |
126
|
|
|
glsr_log()->error('Bad response code ['.$responseCode.']'); |
127
|
|
|
} |
128
|
|
|
if (!$this->success) { |
129
|
|
|
glsr_log()->error($this->message); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $endpoint |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
|
|
protected function send($endpoint, array $args = []) |
139
|
|
|
{ |
140
|
|
|
$args = wp_parse_args($args, [ |
141
|
|
|
'body' => null, |
142
|
|
|
'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'], |
143
|
|
|
'redirection' => 5, |
144
|
|
|
'sslverify' => false, |
145
|
|
|
'timeout' => 5, |
146
|
|
|
]); |
147
|
|
|
$this->reset(); |
148
|
|
|
$this->handleResponse( |
149
|
|
|
wp_remote_post(trailingslashit(static::API_URL).$endpoint, $args) |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|