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-old.php
107 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WP_Rplg_Google_Reviews\Includes\Core; |
| 4 | |
| 5 | class Google_Api_Old { |
| 6 | |
| 7 | private $dao; |
| 8 | private $helper; |
| 9 | |
| 10 | public function __construct(Google_Dao $dao, Connect_Helper $helper) { |
| 11 | $this->dao = $dao; |
| 12 | $this->helper = $helper; |
| 13 | } |
| 14 | |
| 15 | public function connect($pid, $lang, $key, $local_img = false) { |
| 16 | // First call with default sorting method |
| 17 | $url = $this->url($pid, $lang, $key); |
| 18 | $this->call($url, $key, $local_img); |
| 19 | |
| 20 | // Second call with default 'newest' sorting method |
| 21 | $result = $this->refresh($pid, $lang, $key, $local_img); |
| 22 | |
| 23 | return $result; |
| 24 | } |
| 25 | |
| 26 | public function refresh($pid, $lang, $key, $local_img = false) { |
| 27 | // Second call with default 'newest' sorting method |
| 28 | $url = $this->url($pid, $lang, $key, 'newest'); |
| 29 | $result = $this->call($url, $key, $local_img); |
| 30 | |
| 31 | return $result; |
| 32 | } |
| 33 | |
| 34 | public function place($pid, $lang, $key) { |
| 35 | $url = $this->url($pid, $lang, $key); |
| 36 | $result = $this->call($url, $key, false, false); |
| 37 | |
| 38 | return $result; |
| 39 | } |
| 40 | |
| 41 | public function call($url, $key = null, $local_img = false, $db_save = true) { |
| 42 | $res = wp_remote_get($url); |
| 43 | $body = wp_remote_retrieve_body($res); |
| 44 | $json = json_decode($body); |
| 45 | |
| 46 | if ($json && isset($json->result) && isset($json->result->rating)) { |
| 47 | $data = $json->result; |
| 48 | |
| 49 | if ($key) { |
| 50 | $photo = $this->photo($data, $key); |
| 51 | $data->business_photo = $photo; |
| 52 | } |
| 53 | |
| 54 | if ($db_save) { |
| 55 | $this->dao->save($data, $local_img); |
| 56 | } |
| 57 | |
| 58 | $result = array( |
| 59 | 'id' => $data->place_id, |
| 60 | 'name' => $data->name, |
| 61 | 'rating' => $data->rating, |
| 62 | 'user_ratings_total' => $data->user_ratings_total, |
| 63 | 'photo' => isset($data->business_photo) && strlen($data->business_photo) ? $data->business_photo : GRW_GOOGLE_BIZ, |
| 64 | 'reviews' => isset($data->reviews) ? $data->reviews : null |
| 65 | ); |
| 66 | if (isset($data->map_url) && strlen($data->map_url) > 0) { |
| 67 | $result['map_url'] = $data->map_url; |
| 68 | } |
| 69 | $status = 'success'; |
| 70 | } else { |
| 71 | if (isset($json->error_message)) { |
| 72 | $result = array('error_message' => $json->error_message); |
| 73 | } else { |
| 74 | $result = array('error_message' => 'The place you are trying to connect to does not have a rating yet.'); |
| 75 | } |
| 76 | $status = 'failed'; |
| 77 | } |
| 78 | return compact('status', 'result'); |
| 79 | } |
| 80 | |
| 81 | private function url($pid, $lang, $key = '', $reviews_sort = '') { |
| 82 | $url = GRW_GOOGLE_PLACE_API . 'details/json?placeid=' . $pid . '&key=' . $key; |
| 83 | if (strlen($lang) > 0) { |
| 84 | $url = $url . '&language=' . $lang; |
| 85 | } |
| 86 | if (strlen($reviews_sort) > 0) { |
| 87 | $url = $url . '&reviews_sort=' . $reviews_sort; |
| 88 | } |
| 89 | return $url; |
| 90 | } |
| 91 | |
| 92 | private function photo($json, $key) { |
| 93 | if (isset($json->photos) && count($json->photos) > 0) { |
| 94 | $url = add_query_arg( |
| 95 | array( |
| 96 | 'maxwidth' => '300', |
| 97 | 'maxheight' => '300', |
| 98 | 'photoreference' => $json->photos[0]->photo_reference, |
| 99 | 'key' => $key |
| 100 | ), |
| 101 | 'https://maps.googleapis.com/maps/api/place/photo' |
| 102 | ); |
| 103 | return $this->helper->upload_image($url, $json->place_id); |
| 104 | } |
| 105 | return null; |
| 106 | } |
| 107 | } |