PluginProbe ʕ •ᴥ•ʔ
Rich Showcase for Google Reviews / 6.4
Rich Showcase for Google Reviews v6.4
6.9.8 6.9.7 6.9.6 trunk 1.4 1.42 1.43 1.44 1.45 1.46 1.47 1.48 1.49 1.5 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.7 1.6.8 1.6.9 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 1.8 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3 2.4 2.4.1 2.4.2 2.5 2.5.1 2.6 2.6.1 2.6.2 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.6.1 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.8.1 4.8.2 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.7.1 5.8 5.9 5.9.1 5.9.2 5.9.3 5.9.7 6.0 6.1 6.2 6.3 6.4 6.4.1 6.5 6.6 6.6.1 6.6.2 6.7 6.8 6.8.1 6.8.2 6.9 6.9.1 6.9.2 6.9.3 6.9.4 6.9.4.1 6.9.4.2 6.9.4.3 6.9.4.4 6.9.5
widget-google-reviews / includes / core / class-google-api-new.php
widget-google-reviews / includes / core Last commit date
class-connect-helper.php 1 year ago class-core.php 10 months ago class-database.php 10 months ago class-google-api-new.php 1 year ago class-google-api-old.php 11 months ago class-google-connect.php 11 months ago class-google-dao.php 10 months ago class-google-utils.php 1 year ago
class-google-api-new.php
155 lines
1 <?php
2
3 namespace WP_Rplg_Google_Reviews\Includes\Core;
4
5 class Google_Api_New {
6
7 const FIELDS = ['id', 'displayName', 'photos', 'googleMapsUri', 'websiteUri', 'formattedAddress', 'rating', 'userRatingCount', 'reviews'];
8
9 const PLACE_FIELDS = ['id', 'displayName', 'photos', 'googleMapsUri', 'websiteUri', 'rating', 'userRatingCount'];
10
11 const ISOTIME_9D_REGEXP = '/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.(\d{9})Z/';
12
13 private $dao;
14 private $helper;
15
16 public function __construct(Google_Dao $dao, Connect_Helper $helper) {
17 $this->dao = $dao;
18 $this->helper = $helper;
19 }
20
21 public function connect($pid, $lang, $key, $local_img = false) {
22 $url = $this->url($pid, $key, self::FIELDS, $lang);
23 $result = $this->call($url, $key, $local_img);
24
25 return $result;
26 }
27
28 public function place($pid, $lang, $key) {
29 $url = $this->url($pid, $key, self::PLACE_FIELDS, $lang);
30 $result = $this->call($url, $key, false, false);
31
32 return $result;
33 }
34
35 private function call($url, $key, $local_img = false, $db_save = true) {
36 $res = wp_remote_get($url);
37 $body = wp_remote_retrieve_body($res);
38 $json = json_decode($body);
39
40 if ($json && isset($json->rating)) {
41 $photo = $this->photo($json, $key);
42 $json->business_photo = $photo;
43
44 if ($db_save) {
45 $old_place = $this->convert($json);
46 $this->dao->save($old_place, $local_img);
47 }
48
49 $result = array(
50 'id' => $json->id,
51 'name' => $json->displayName->text,
52 'rating' => $json->rating,
53 'user_ratings_total' => $json->userRatingCount,
54 'photo' => strlen($json->business_photo) ? $json->business_photo : GRW_GOOGLE_BIZ,
55 'reviews' => isset($json->reviews) ? $json->reviews : null
56 );
57 $status = 'success';
58 } else {
59 if (isset($json->error)) {
60 $result = array('error_message' => $json->error);
61 } else {
62 $result = array('error_message' => 'The place you are trying to connect to does not have a rating yet.');
63 }
64 $status = 'failed';
65 }
66
67 return compact('status', 'result');
68 }
69
70 private function url($pid, $key, $fields = [], $lang = '') {
71 $url = GRW_GOOGLE_PLACE_API_NEW . $pid . '?fields=' . implode(',', $fields) . '&key=' . $key;
72 if (strlen($lang) > 0) {
73 $url = $url . '&languageCode=' . $lang;
74 }
75 return $url;
76 }
77
78 private function photo($json, $key) {
79 if (isset($json->photos) && count($json->photos) > 0) {
80 $url = add_query_arg(
81 array(
82 'maxWidthPx' => '300',
83 'maxHeightPx' => '300',
84 'key' => $key
85 ),
86 'https://places.googleapis.com/v1/' . $json->photos[0]->name . '/media'
87 );
88 return $this->helper->upload_image($url, $json->id);
89 }
90 return null;
91 }
92
93 private function convert($new_place) {
94 $old_place = [
95 'place_id' => $new_place->id,
96 'rating' => $new_place->rating,
97 'user_ratings_total' => isset($new_place->userRatingCount) ? $new_place->userRatingCount : 0,
98 'name' => $new_place->displayName->text,
99 'photo' => isset($new_place->business_photo) ? $new_place->business_photo : null,
100 'url' => isset($new_place->googleMapsUri) ? $new_place->googleMapsUri : null,
101 'website' => isset($new_place->websiteUri) ? $new_place->websiteUri : null,
102 'formatted_address' => isset($new_place->formattedAddress) ? $new_place->formattedAddress : null,
103 'icon' => null
104 ];
105
106 if (isset($new_place->reviews) && count($new_place->reviews) > 0) {
107 $old_reviews = [];
108 foreach ($new_place->reviews as $review) {
109 array_push($old_reviews, [
110 'rating' => $review->rating,
111 'text' => isset($review->text) ? $review->text->text : null,
112 'time' => $this->get_time($review),
113 'language' => $this->get_lang($review),
114 'author_name' => $this->get_author_name($review),
115 'author_url' => $this->get_author_url($review),
116 'profile_photo_url' => $this->get_author_img($review)
117 ]);
118 }
119 $old_place['reviews'] = $old_reviews;
120 }
121
122 return json_decode(json_encode($old_place));
123 }
124
125 // Special case for long (with 9 digits after T, for instance 2018-12-16T22:06:12.830950891Z) dates, unsupported in PHP < 8
126 private function get_time($review) {
127 $publishTime = $review->publishTime;
128 $matches = array();
129 preg_match(self::ISOTIME_9D_REGEXP, $publishTime, $matches, PREG_OFFSET_CAPTURE);
130 if (count($matches) > 1) {
131 $publishTime = str_replace($matches[1][0], substr($matches[1][0], 0, 6), $publishTime);
132 }
133 return date('U', strtotime($publishTime));
134 }
135
136 private function get_lang($review) {
137 if (isset($review->text) && isset($review->text->languageCode)) {
138 return ($review->text->languageCode == 'en-US' ? 'en' : $review->text->languageCode);
139 } else {
140 return null;
141 }
142 }
143
144 private function get_author_name($review) {
145 return isset($review->authorAttribution) && strlen($review->authorAttribution->displayName) > 0 ? $review->authorAttribution->displayName : null;
146 }
147
148 private function get_author_url($review) {
149 return isset($review->authorAttribution) && strlen($review->authorAttribution->uri) > 0 ? $review->authorAttribution->uri : null;
150 }
151
152 private function get_author_img($review) {
153 return isset($review->authorAttribution) && isset($review->authorAttribution->photoUri) ? $review->authorAttribution->photoUri : null;
154 }
155 }