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 | } |