· 6 years ago · Oct 29, 2019, 07:14 PM
1<?php
2
3namespace Surefire\Microsites\Lib\Sfmu;
4
5/**
6 * Module Name: Redwood HMC WebPunch Reviews
7 * Description: All functionality for pulling reviews from WebPunch.
8 */
9class Redwood_HMC_Webpunch_Reviews
10{
11 static private $api_url = 'https://app.webpunch12.com/api/v1/';
12 static private $feedback_api_key = 'ab120948aa60faab5974';
13 static private $feedback_api_secret = 'a4ebbbd732278abd4e47';
14 static private $reviews_api_key = 'ab120948aa60faab5974';
15 static private $reviews_api_secret = 'a4ebbbd732278abd4e47';
16 static private $setting_name = 'redwood_hmc_webpunch_location';
17 static private $transient_name = 'redwood_hmc_webpunch_location_uuids';
18 static private $floor = 4.0;
19
20 public function __construct()
21 {
22
23 }
24
25 public static function reset_reviews($microsite)
26 {
27 self::remove_testimonials($microsite);
28 }
29
30 public static function do_webpunch_pull($microsite=null) {
31 global $wpdb;
32
33 $microsite = $microsite?:get_current_blog_id();
34
35 // Clean incorrect entry if there is any
36 $incorrect_entry = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_title = '(no title)'" );
37
38 foreach( $incorrect_entry as $wrong ) {
39 wp_delete_post($wrong->ID);
40 }
41
42 $cf_response;
43 $er_response;
44
45 self::remove_testimonials($microsite);
46
47 if ( $microsite === 1 ) {
48 $location_uuids = self::get_location_uuids();
49 $cf_response = self::get_customer_feedback();
50 $er_response = self::get_external_reviews();
51 self::write_customer_feedback($cf_response->customer_feedback, $location_uuids, $microsite);
52 self::write_external_reviews($er_response->reviews, $location_uuids, $microsite);
53 } else {
54 $location_id = get_blog_option($microsite, self::$setting_name);
55 $location_id = $location_id['webpunch_location_id'];
56
57 if (empty($location_id)) return;
58
59 $cf_response = self::get_customer_feedback($location_id);
60 $er_response = self::get_external_reviews($location_id);
61 //echo '<pre>'; dd($er_response->reviews);
62 self::write_customer_feedback($cf_response->customer_feedback, '', $microsite);
63 self::write_external_reviews($er_response->reviews, '', $microsite);
64 }
65 }
66
67 private static function write_customer_feedback($customer_feedback=array(), $location_uuids = false, $microsite) {
68 $customer_feedback = array_filter($customer_feedback, array('self', 'filter_by_five_star_rating'));
69 $customer_feedback = array_filter($customer_feedback, array('self', 'filter_empty_cf'));
70 foreach ($customer_feedback as $count => $review) {
71 if ( $count % 5 != 0 )
72 usleep(60);
73
74 $location = !empty($review->customer->state) ? $review->customer->city . ', ' . $review->customer->state : $review->customer->city;
75 $testimonial = array(
76 'quote' => $review->comments,
77 'author' => $review->customer->first_name . ' ' . $review->customer->last_initial,
78 'author_location' => $location,
79 'source' => 'nps',
80 'source_url' => '#',
81 'date' => date('Ymd', strtotime($review->last_response_date)),
82 'location_uuid' => $review->location_uuid,
83 );
84 // var_dump($testimonial);
85 self::create_testimonial($testimonial, $location_uuids, $microsite);
86 }
87 }
88
89 private static function write_external_reviews($external_reviews, $location_uuids = false, $microsite) {
90 $external_reviews = array_filter($external_reviews, array('self', 'filter_by_rating'));
91 $external_reviews = array_filter($external_reviews, array('self', 'filter_out_blacklist'));
92 $external_reviews = array_filter($external_reviews, array('self', 'filter_empty_er'));
93 $external_reviews = array_map(array('self', 'remove_text'), $external_reviews);
94
95 foreach ($external_reviews as $count => $review) {
96 if ( $count % 5 != 0 )
97 usleep(60);
98
99 $testimonial = array(
100 'quote' => $review->text,
101 'author' => $review->author,
102 'author_location' => '',
103 'source' => strtolower($review->site),
104 'source_url' => '#',
105 'uuid' => $review->uuid,
106 'date' => date('Ymd', strtotime($review->date)),
107 'location_uuid' => $review->location_uuid,
108 );
109 // var_dump($testimonial);
110 self::create_testimonial($testimonial, $location_uuids, $microsite);
111 }
112
113 }
114
115 private static function remove_testimonials($microsite)
116 {
117 global $wpdb;
118
119 switch_to_blog($microsite);
120
121 $table = $wpdb->prefix.'posts';
122
123 set_time_limit(0);
124
125 $wpdb->query(
126 $wpdb->prepare(
127 "
128 DELETE FROM {$table}
129 WHERE post_type = %s
130 ",
131 'testimonial'
132 )
133 );
134
135 /*$testimonials = get_posts(
136 array(
137 'post_type' => 'testimonial',
138 'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash'),
139 'posts_per_page' => -1
140 )
141 );
142
143 foreach( $testimonials as $testimonial ) {
144 set_time_limit(0);
145 wp_delete_post($testimonial->ID, true);
146 }*/
147
148 restore_current_blog();
149 }
150
151 private static function create_testimonial($testimonial, $location_uuids = false, $microsite) {
152 set_time_limit(0);
153
154 $uuid = isset($testimonial['uuid']) ? $testimonial['uuid'] : hash('md5', serialize($testimonial));
155
156 switch_to_blog($microsite);
157 $review_exists = get_posts(
158 array(
159 'post_type' => 'testimonial',
160 'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash'),
161 'meta_key' => 'testimonial_uuid',
162 'meta_value' => $uuid
163 )
164 );
165 restore_current_blog();
166
167 if ( count($review_exists) > 0 ) return;
168
169 switch_to_blog($microsite);
170 $post_id = wp_insert_post(array(
171 'post_status' => 'publish',
172 'post_type' => 'testimonial',
173 ));
174 if (!$post_id) self::log_request('customer_feedback', 'Error - could not create testimonial');
175
176 $updated_post = wp_update_post(array(
177 'ID' => $post_id,
178 'post_title' => "Testimonial #$post_id",
179 ));
180 if (!$updated_post) self::log_request('customer_feedback', 'Error - could not update testimonial ' . $post_id);
181 restore_current_blog();
182
183 if ( $microsite !== 1 ) {
184 switch_to_blog(1);
185 $source_field = acf_get_field('source', true);
186 restore_current_blog();
187 } else {
188 $source_field = acf_get_field('source', true);
189 }
190
191 if(!isset($source_field)|| empty($source_field)) return;
192
193 $choices = array_keys($source_field['choices']);
194
195 if (!in_array($testimonial['source'], $choices)) {
196 $testimonial['source'] = 'offline';
197 }
198
199 if ($location_uuids && isset($location_uuids[$testimonial['location_uuid']])) {
200 $testimonial['location_name'] = $location_uuids[$testimonial['location_uuid']];
201 }
202
203 unset($testimonial['location_uuid']);
204
205 switch_to_blog($microsite);
206 foreach ($testimonial as $key => $value) {
207 if (!add_post_meta($post_id, $key, $value)) self::log_request('customer_feedback', 'Error - could not update testimonial');
208 }
209
210 if (!add_post_meta($post_id, 'testimonial_uuid', $uuid)) self::log_request('customer_feedback', 'Error - could not update testimonial');
211 restore_current_blog();
212 }
213
214 private static function get_location_uuids() {
215 set_time_limit(0);
216
217 $location_uuids = array();
218
219 // Get all sites in the network
220 $sites = wp_get_sites(array(
221 'public' => 1,
222 'deleted' => 0,
223 'archived' => 0,
224 'limit' => 1000
225 ));
226 // var_dump($sites);
227
228 // Loop through sites and get location uuid WebPunch option
229 foreach ($sites as $site) {
230 // Skip main site
231 if ($site['blog_id'] == 1) continue;
232
233 // Get WebPunch location UUID option from $site
234 $option = get_blog_option($site['blog_id'], self::$setting_name);
235 $location_uuid = $option['webpunch_location_id'];
236
237 // Save blogname at $location_uuid
238 if (!empty($location_uuid)) {
239 $location_uuids[$location_uuid] = get_blog_option($site['blog_id'], 'blogname');
240 }
241
242 }
243
244 return $location_uuids;
245 }
246
247 private static function filter_by_five_star_rating($value) {
248 return $value->five_star_rating >= self::$floor;
249 }
250
251 private static function filter_by_rating($value) {
252 return $value->rating >= self::$floor;
253 }
254
255 private static function filter_out_blacklist($value) {
256 $blacklist = ['yellowbot'];
257 return !in_array(strtolower($value->site), $blacklist);
258 }
259
260 private static function filter_empty_cf($value) {
261 return !empty($value->comments);
262 }
263
264 private static function filter_empty_er($value) {
265 return !empty($value->text);
266 }
267
268 private static function remove_text($value) {
269 $value->text = trim($value->text);
270 $value->text = preg_replace('/\(more\)\s*$/i', '', $value->text);
271 $value->text = trim($value->text);
272 return $value;
273 }
274
275 private static function get_customer_feedback($location_id = 0) {
276 $token = base64_encode(self::$feedback_api_key . ':' . self::$feedback_api_secret);
277 $response = self::get_json_from_api('customer_feedback', $location_id, $token);
278 // var_dump($response);
279 return $response;
280 }
281
282 private static function get_external_reviews($location_id = 0) {
283 $token = base64_encode(self::$reviews_api_key . ':' . self::$reviews_api_secret);
284 $response = self::get_json_from_api('external_reviews', $location_id, $token);
285 // var_dump($response);
286 return $response;
287 }
288
289 private static function get_json_from_api($slug, $location_id = 0, $token = '') {
290 if (empty($slug) || !is_string($slug)) return false;
291
292 $type = $slug === "external_reviews" ? 'reviews' : 'customer_feedback';
293
294 $url = self::$api_url . $slug . '.json';
295 if ($location_id) $url .= '?location_uuid=' . $location_id;
296
297 $response = wp_remote_get($url, array(
298 'headers' => array(
299 'Authorization' => 'Basic ' . $token,
300 ),
301 ));
302
303 if ( is_wp_error( $response ) ) {
304 $error_message = $response->get_error_message();
305 self::log_request($slug, "Error getting $url - $error_message");
306 return false;
307 } else {
308 $json = json_decode($response['body']);
309
310 if ( absint($json->pagination->total_available) > 100 ) {
311 $number_of_requests = ceil($json->pagination->total_available / $json->pagination->results_per_page);
312 $page = 2;
313 while($number_of_requests > 0) {
314 $url = ( strpos($url, '?') === false ) ? $url . '?page=' . $page : $url . '&page=' . $page;
315
316 $response = wp_remote_get($url, array(
317 'headers' => array(
318 'Authorization' => 'Basic ' . $token,
319 ),
320 ));
321
322 if ( is_wp_error( $response ) ) {
323 $error_message = $response->get_error_message();
324 self::log_request($slug, "Error getting $url - $error_message");
325 return false;
326 } else {
327 $response = json_decode($response['body']);
328 $json->$type = array_merge($json->$type, $response->$type);
329 }
330 $number_of_requests--;
331 $page++;
332 }
333 }
334
335 self::log_request($slug, "Success getting $url");
336 return $json;
337 }
338 }
339
340 /**
341 * Log the review type and status from API calls.
342 *
343 * @param object $args
344 */
345 private static function log_request($type, $status)
346 {
347 return; // REMOVE when done
348 $message = date('Y-d-m H:i:s') . ' - Review type: ' . $type . '|Status: ' . $status;
349 Redwood_Core_Logger::log('webpunch', $message);
350 }
351
352}