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-greatschools-api.php
284 lines (244 loc) · 7.28 KB
/
wp-greatschools-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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
/**
* WP GreatSchools API
*
* @package WP-GreatSchools-API
*/
/*
* Plugin Name: WP GreatSchools API
* Plugin URI: https://github.com/wp-api-libraries/wp-greatschools-api
* Description: Perform API requests to GreatSchools in WordPress.
* Author: WP API Libraries
* Version: 1.0.0
* Author URI: https://wp-api-libraries.com
* GitHub Plugin URI: https://github.com/wp-api-libraries/wp-greatschools-api
* GitHub Branch: master
*/
/* Exit if accessed directly */
if ( ! defined( 'ABSPATH' ) ) { exit; }
if ( ! class_exists( 'GreatSchoolsAPI' ) ) {
/**
* GreatSchools API Class.
*/
class GreatSchoolsAPI {
/**
* API Key.
*
* @var string
*/
static private $api_key;
/**
* Output Type.
*
* @var string
*/
static private $output;
/**
* BaseAPI Endpoint
*
* @var string
* @access public
*/
public $base_uri = 'http://api.greatschools.org/schools/';
/**
* __construct function.
*
* @access public
* @param mixed $api_key API Key
* @param string $output (default: 'json') Output.
* @return void
*/
public function __construct( $api_key, $output = 'json' ) {
static::$api_key = $api_key;
static::$output = $output;
}
/**
* 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 );
$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 $body;
}
/**
* get_schools function.
*
* @access public
* @param mixed $state State.
* @param mixed $city City.
* @return void
*/
public function get_schools( $state, $city ) {
$request = $this->base_uri . $state . '/' . $city . '?key=' . static::$api_key;
$xml = simplexml_load_file( $request );
$json = wp_json_encode( $xml );
$results = json_decode( $json, true );
return $results;
}
/**
* get_nearby_schools function.
*
* @access public
* @param mixed $state State.
* @param mixed $zip Zip.
* @param mixed $city City.
* @param mixed $address Address.
* @param mixed $latitude Latitude.
* @param mixed $longitude Longitude.
* @param mixed $school_type School Type.
* @param mixed $level_code Level Code.
* @param mixed $radius Radius.
* @param mixed $limit Limit.
* @return void
*/
public function get_nearby_schools( $state, $zip, $city, $address, $latitude, $longitude, $school_type, $level_code, $radius, $limit ) {
}
/**
* Returns a profile data for a specific school.
*
* @access public
* @param mixed $state State.
* @param mixed $school_id School ID.
* @return void
*/
public function get_school( $state, $school_id ) {
$request = $this->base_uri . $state . '/'. $school_id .'?key=' . static::$api_key;
$xml = simplexml_load_file( $request );
$json = wp_json_encode( $xml );
$results = json_decode( $json, true );
return $results;
}
/**
* Returns a list of schools based on a search string.
*
* @access public
* @param mixed $state State.
* @param mixed $query_string Query String.
* @return void
*/
public function search_for_schools( $state, $query_string, $level_code, $sort, $limit ) {
$request = $this->base_uri . '/search/schools?key=' . static::$api_key . '&state=' . $state . '&q='. $query_string .'&levelCode='. $level_code .'&sort='. $sort .'&limit=' . $limit;
$xml = simplexml_load_file( $request );
$json = wp_json_encode( $xml );
$results = json_decode( $json, true );
return $results;
}
/**
* Returns a list of the most recent reviews for a school or for any schools in a city.
*
* @access public
* @param mixed $state State.
* @param mixed $city City.
* @param mixed $school_id School ID.
* @param mixed $cutoffage Cut Off Age.
* @param mixed $limit Limit.
* @param mixed $topic_id Topic ID.
* @return void
*/
public function get_school_reviews( $state, $city, $school_id, $cutoffage, $limit, $topic_id ) {
$request = $this->base_uri . '/reviews/school/' . $state . '/'. $school_id .'?key=' . static::$api_key . '&limit='. $limit .'&cutoffAge='. $cutoffage .'&topicId=' . $topic_id;
$xml = simplexml_load_file( $request );
$json = wp_json_encode( $xml );
$results = json_decode( $json, true );
return $results;
}
/**
* Returns a list of topics available for topical reviews.
*
* @access public
* @return void
*/
public function get_review_topics() {
$request = $this->base_uri . '/reviews/reviewTopics?key=' . static::$api_key;
$xml = simplexml_load_file( $request );
$json = wp_json_encode( $xml );
$results = json_decode( $json, true );
return $results;
}
/**
* Returns test and rank data for a specific school.
*
* @access public
* @param mixed $state State.
* @param mixed $school_id School ID.
* @return void
*/
public function get_school_test_scores( $state, $school_id ) {
$request = $this->base_uri . '/school/tests/' . $state . '/'. $school_id .'?key=' . static::$api_key;
$xml = simplexml_load_file( $request );
$json = wp_json_encode( $xml );
$results = json_decode( $json, true );
return $results;
}
/**
* Returns census and profile data for a school.
*
* @access public
* @param mixed $state State.
* @param mixed $school_id School ID.
* @return void
*/
public function get_school_census_data( $state, $school_id ) {
$request = $this->base_uri . '/school/census/' . $state . '/'. $school_id .'?key=' . static::$api_key;
$xml = simplexml_load_file( $request );
$json = wp_json_encode( $xml );
$results = json_decode( $json, true );
return $results;
}
/**
* Returns information about a city.
*
* @access public
* @param mixed $state State.
* @param mixed $city City.
* @return void
*/
public function get_city_schools( $state, $city ) {
$request = $this->base_uri . '/cities/' . $state . '/'. $city .'?key=' . static::$api_key;
$xml = simplexml_load_file( $request );
$json = wp_json_encode( $xml );
$results = json_decode( $json, true );
return $results;
}
/**
* Returns a list of cities near another city.
*
* @access public
* @param mixed $state State.
* @param mixed $city City.
* @param mixed $radius Radius.
* @param mixed $sort Sort.
* @return void
*/
public function get_nearby_cities( $state, $city, $radius, $sort = 'rating' ) {
$request = $this->base_uri . '/cities/nearby/' . $state . '/'. $city .'?key=' . static::$api_key . '&radius='. $radius .'&sort=' . $sort;
$xml = simplexml_load_file( $request );
$json = wp_json_encode( $xml );
$results = json_decode( $json, true );
return $results;
}
/**
* get_districts function.
*
* @access public
* @param mixed $state State.
* @param mixed $city City.
* @return void
*/
public function get_districts( $state, $city ) {
$request = $this->base_uri . '/districts/' . $state . '/'. $city .'?key=' . static::$api_key;
$xml = simplexml_load_file( $request );
$json = wp_json_encode( $xml );
$results = json_decode( $json, true );
return $results;
}
}
}