This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-streetadvisor-api.php
117 lines (95 loc) · 2.56 KB
/
wp-streetadvisor-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* WP StreetAdvisor API
*
* @package WP-StreetAdvisor-API
*/
/*
* Plugin Name: WP Street Advisor API
* Plugin URI: https://github.com/wp-api-libraries/wp-streetadvisor-api
* Description: Perform API requests to streetadvisor.com in WordPress.
* Author: WP API Libraries
* Author URI: https://wp-api-libraries.com
* Version: 1.0.0
* GitHub Plugin URI: https://github.com/wp-api-libraries/wp-streetadvisor-api
* GitHub Branch: master
*/
/* Exit if accessed directly */
if ( ! defined( 'ABSPATH' ) ) { exit; }
/* Check if Class Exists. */
if ( ! class_exists( 'StreetAdvisorAPI' ) ) {
/**
* StreetAdvisor API Class.
*/
class StreetAdvisorAPI {
/**
* API Key.
*
* @var string
*/
static private $api_key;
/**
* URL to the API.
*
* @var string
*/
private $base_uri = 'https://web-api-legacy.streetadvisor.com';
/**
* __construct function.
*
* @access public
* @param mixed $api_key API Key.
* @return void
*/
public function __construct( $api_key ) {
static::$api_key = $api_key;
$this->args['headers'] = array(
'Content-Type' => 'application/json',
'Authorization' => $api_key,
);
}
/**
* Fetch the request from the API.
*
* @access private
* @param mixed $request Request URL.
* @return $body Body.
*/
private function fetch( $request ) {
$response = wp_remote_get( $request, $this->args );
$code = wp_remote_retrieve_response_code( $response );
if ( 200 !== $code ) {
return new WP_Error( 'response-error', sprintf( __( 'Server response code: %d', 'text-domain' ), $code ) );
}
$body = wp_remote_retrieve_body( $response );
return json_decode( $body );
}
/**
* Get Location Data.
*
* @access public
* @param mixed $latitude Latitude.
* @param mixed $longitude Longitude.
* @param mixed $level Level.
* @return void
*/
public function get_location_data( $latitude, $longitude, $level ) {
$request = $this->base_uri . '/locations/overview/'.$latitude.'/'.$longitude.'/'.$level;
return $this->fetch( $request );
}
/**
* Get Location Reviews.
*
* @access public
* @param mixed $latitude Latitude.
* @param mixed $longitude Longitude.
* @param mixed $level Level.
* @param int $review_count (default: 5) Total # of Reviews to display.
* @return void
*/
public function get_location_reviews( $latitude, $longitude, $level, $review_count = '5' ) {
$request = $this->base_uri . '/reviews/'.$latitude.'/'.$longitude.'/'.$level . '?take='. $review_count;
return $this->fetch( $request );
}
}
}