1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* WP Walkscore API |
4
|
|
|
* |
5
|
|
|
* @package WP-Walkscore-API |
6
|
|
|
*/ |
7
|
|
|
/* |
8
|
|
|
* Plugin Name: WP WalkScore API |
9
|
|
|
* Plugin URI: https://github.com/wp-api-libraries/wp-walkscore-api |
10
|
|
|
* Description: Perform API requests to Walkscore in WordPress. |
11
|
|
|
* Author: WP API Libraries |
12
|
|
|
* Version: 1.0.0 |
13
|
|
|
* Author URI: https://wp-api-libraries.com |
14
|
|
|
* GitHub Plugin URI: https://github.com/wp-api-libraries/wp-walkscore-api |
15
|
|
|
* GitHub Branch: master |
16
|
|
|
*/ |
17
|
|
|
/* Exit if accessed directly. */ |
18
|
|
|
if ( ! defined( 'ABSPATH' ) ) { exit; } |
19
|
|
|
/* Check if class exists. */ |
20
|
|
|
if ( ! class_exists( 'WalkscoreAPI' ) ) { |
21
|
|
|
/** |
22
|
|
|
* Walkscore API Class. |
23
|
|
|
*/ |
24
|
|
|
class WalkscoreAPI { |
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* API Key. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
static private $wsapikey; |
31
|
|
|
/** |
32
|
|
|
* Return format. XML or JSON. |
33
|
|
|
* |
34
|
|
|
* @var [string |
35
|
|
|
*/ |
36
|
|
|
static private $output; |
37
|
|
|
/** |
38
|
|
|
* URL to the API. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $base_uri = 'http://api.walkscore.com'; |
43
|
|
|
/** |
44
|
|
|
* __construct function. |
45
|
|
|
* |
46
|
|
|
* @access public |
47
|
|
|
* @param mixed $wsapikey API Key. |
48
|
|
|
* @return void |
|
|
|
|
49
|
|
|
*/ |
50
|
|
|
public function __construct( $wsapikey, $output = 'json' ) { |
51
|
|
|
static::$wsapikey = $wsapikey; |
|
|
|
|
52
|
|
|
static::$output = $output; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
/** |
55
|
|
|
* Fetch the request from the API. |
56
|
|
|
* |
57
|
|
|
* @access private |
58
|
|
|
* @param mixed $request Request URL. |
59
|
|
|
* @return $body Body. |
|
|
|
|
60
|
|
|
*/ |
61
|
|
View Code Duplication |
private function fetch( $request ) { |
|
|
|
|
62
|
|
|
$response = wp_remote_get( $request ); |
63
|
|
|
$code = wp_remote_retrieve_response_code( $response ); |
64
|
|
|
if ( 200 !== $code ) { |
65
|
|
|
return new WP_Error( 'response-error', sprintf( __( 'Server response code: %d', 'text-domain' ), $code ) ); |
66
|
|
|
} |
67
|
|
|
$body = wp_remote_retrieve_body( $response ); |
68
|
|
|
return json_decode( $body ); |
69
|
|
|
} |
70
|
|
|
/** |
71
|
|
|
* Get Walkscore (https://www.walkscore.com/professional/api.php) |
72
|
|
|
* |
73
|
|
|
* @access public |
74
|
|
|
* @param mixed $wsapikey Your Walk Score API Key. Contact us to get one. Required. |
|
|
|
|
75
|
|
|
* @param mixed $latitude The latitude of the requested location. Required. |
76
|
|
|
* @param mixed $longitude The longitude of the requested location. Required. |
77
|
|
|
* @param mixed $address The URL encoded address. Required. |
78
|
|
|
* @param mixed $format Return results in XML or JSON (defaults to XML). |
|
|
|
|
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
function get_walkscore( $latitude, $longitude, $address ) { |
|
|
|
|
82
|
|
View Code Duplication |
if ( empty( $latitude ) || empty( $longitude ) || empty( $address ) ) { |
|
|
|
|
83
|
|
|
return new WP_Error( 'required-fields', __( 'Required fields are empty.', 'text-domain' ) ); |
84
|
|
|
} |
85
|
|
|
$request = $this->base_uri . '/score?format=' . static::$output . '&wsapikey=' . static::$wsapikey . '&lat=' . $latitude . '&lon=' . $longitude . '&address=' . $address; |
|
|
|
|
86
|
|
|
return $this->fetch( $request ); |
87
|
|
|
} |
88
|
|
|
/** |
89
|
|
|
* get_transit_score function. |
90
|
|
|
* |
91
|
|
|
* @access public |
92
|
|
|
* @param mixed $latitude |
93
|
|
|
* @param mixed $longitude |
94
|
|
|
* @param mixed $city |
95
|
|
|
* @param mixed $state |
96
|
|
|
* @param mixed $country |
97
|
|
|
* @param mixed $research |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
function get_transit_score( $latitude, $longitude, $city, $state, $country, $research ) { |
|
|
|
|
101
|
|
View Code Duplication |
if ( empty( $latitude ) || empty( $longitude ) || empty( $city ) || empty( $state ) ) { |
|
|
|
|
102
|
|
|
return new WP_Error( 'required-fields', __( 'Required fields are empty.', 'text-domain' ) ); |
103
|
|
|
} |
104
|
|
|
$request = $this->base_uri . '/transit/score/?wsapikey=' . static::$wsapikey . '&lat=' . $latitude . '&lon=' . $longitude . '&city=' . $city . '&state=' . $state . '&format=' . $output; |
|
|
|
|
105
|
|
|
return $this->fetch( $request ); |
106
|
|
|
} |
107
|
|
|
function get_transit_stop_search() { |
|
|
|
|
108
|
|
|
} |
109
|
|
|
function get_transit_network_search() { |
|
|
|
|
110
|
|
|
} |
111
|
|
|
function get_transit_stop_details() { |
|
|
|
|
112
|
|
|
} |
113
|
|
|
function get_transit_route_details() { |
|
|
|
|
114
|
|
|
} |
115
|
|
|
function get_transit_supported_cities() { |
|
|
|
|
116
|
|
|
} |
117
|
|
|
/** |
118
|
|
|
* Response code message |
119
|
|
|
* |
120
|
|
|
* @param [String] $code : Response code to get message from. |
|
|
|
|
121
|
|
|
* @return [String] : Message corresponding to response code sent in. |
|
|
|
|
122
|
|
|
*/ |
123
|
|
|
public function response_code_msg( $code = '' ) { |
124
|
|
|
switch ( $code ) { |
125
|
|
|
case 1: |
126
|
|
|
$msg = __( 'Walk Score successfully returned.', 'text-domain' ); |
127
|
|
|
break; |
128
|
|
|
case 2: |
129
|
|
|
$msg = __( 'Score is being calculated and is not currently available.', 'text-domain' ); |
130
|
|
|
break; |
131
|
|
|
case 30: |
132
|
|
|
$msg = __( 'Invalid latitude/longitude.', 'text-domain' ); |
133
|
|
|
break; |
134
|
|
|
case 31: |
135
|
|
|
$msg = __( 'Walk Score API internal error.', 'text-domain' ); |
136
|
|
|
break; |
137
|
|
|
case 40: |
138
|
|
|
$msg = __( 'Your WSAPIKEY is invalid.', 'text-domain' ); |
139
|
|
|
break; |
140
|
|
|
case 41: |
141
|
|
|
$msg = __( 'Your daily API quota has been exceeded.', 'text-domain' ); |
142
|
|
|
break; |
143
|
|
|
case 42: |
144
|
|
|
$msg = __( 'Your IP address has been blocked.', 'text-domain' ); |
145
|
|
|
break; |
146
|
|
|
default: |
147
|
|
|
$msg = __( 'Sorry, response code is unknown.', 'text-domain' ); |
148
|
|
|
break; |
149
|
|
|
} |
150
|
|
|
return $msg; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.