Changeset 4402 for sites/trunk/wordcamp.org/public_html/wp-content/plugins/wcpt/wcpt-wordcamp/wordcamp-admin.php
- Timestamp:
- 11/22/2016 07:56:45 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wcpt/wcpt-wordcamp/wordcamp-admin.php
r4396 r4402 168 168 $new_address = $_POST[ wcpt_key_to_str( 'Physical Address', 'wcpt_' ) ]; 169 169 if ( $new_address != get_post_meta( $post_id, 'Physical Address', true ) ) { 170 if ( $coordinates = $this->geocode_address( $new_address ) ) { 171 update_post_meta( $post_id, '_venue_coordinates', $coordinates ); 172 } else { 173 delete_post_meta( $post_id, '_venue_coordinates' ); 174 } 170 $this->update_venue_address( $post_id, $new_address ); 175 171 } 176 172 … … 241 237 242 238 /** 243 * Geocode the given address into a latitude and longitude pair. 244 * 239 * Store the individual components of a camp's venue address 240 * 241 * These are used for the maps on Central, stats, etc. 242 * 243 * @param int $post_id 245 244 * @param string $address 246 * 247 * @return mixed 248 * false if the geocode request failed 249 * array with latitude and longitude indexes if the request succeeded 250 */ 251 function geocode_address( $address ) { 252 if ( ! $address ) { 253 return false; 254 } 255 256 $coordinates = false; 257 $request_url = add_query_arg( 'address', urlencode( $address ), 'https://maps.googleapis.com/maps/api/geocode/json' ); 258 $geocode_response = json_decode( wp_remote_retrieve_body( wp_remote_get( $request_url ) ) ); 259 // todo use wcorg_redundant_remote_get 260 261 if ( ! empty( $geocode_response->results[0]->geometry->location->lat ) ) { 245 */ 246 function update_venue_address( $post_id, $address ) { 247 $request_url = add_query_arg( 248 'address', 249 urlencode( $address ), 250 'https://maps.googleapis.com/maps/api/geocode/json' 251 ); 252 253 $response = wcorg_redundant_remote_get( $request_url ); 254 255 // Don't delete the existing (and probably good) values if the request failed 256 if ( is_wp_error( $response ) ) { 257 return; 258 } 259 260 $meta_values = $this->parse_geocode_response( $response ); 261 262 foreach ( $meta_values as $key => $value ) { 263 if ( is_null( $value ) ) { 264 delete_post_meta( $post_id, $key ); 265 } else { 266 update_post_meta( $post_id, $key, $value ); 267 } 268 } 269 } 270 271 /** 272 * Parse the values we want out of the Geocode API response 273 * 274 * @see https://developers.google.com/maps/documentation/geocoding/intro#Types API response schema 275 * 276 * @param $response 277 * 278 * @return array 279 */ 280 protected function parse_geocode_response( $response ) { 281 $body = json_decode( wp_remote_retrieve_body( $response ) ); 282 $body = isset ( $body->results[0] ) ? $body->results[0] : null; 283 284 if ( isset( $body->geometry->location->lat ) ) { 262 285 $coordinates = array( 263 'latitude' => $ geocode_response->results[0]->geometry->location->lat,264 'longitude' => $ geocode_response->results[0]->geometry->location->lng286 'latitude' => $body->geometry->location->lat, 287 'longitude' => $body->geometry->location->lng 265 288 ); 266 289 } 267 290 268 return $coordinates; 291 if ( isset ( $body->address_components ) ) { 292 foreach ( $body->address_components as $component ) { 293 foreach ( $component->types as $type ) { 294 switch ( $type ) { 295 296 case 'locality': 297 case 'administrative_area_level_1': 298 case 'postal_code': 299 $$type = $component->long_name; 300 break; 301 302 case 'country': 303 $country_code = $component->short_name; // This is not guaranteed to be ISO 3166-1 alpha-2, but should match in most cases 304 $country_name = $component->long_name; 305 break; 306 307 } 308 } 309 } 310 } 311 312 $values = array( 313 '_venue_coordinates' => isset( $coordinates ) ? $coordinates : null, 314 '_venue_city' => isset( $locality ) ? $locality : null, 315 '_venue_state' => isset( $administrative_area_level_1 ) ? $administrative_area_level_1 : null, 316 '_venue_country_code' => isset( $country_code ) ? $country_code : null, 317 '_venue_country_name' => isset( $country_name ) ? $country_name : null, 318 '_venue_zip' => isset( $postal_code ) ? $postal_code : null, 319 ); 320 321 return $values; 269 322 } 270 323 … … 273 326 * 274 327 * Returns post meta key 328 * 329 * @param string $meta_group 275 330 * 276 331 * @return array
Note: See TracChangeset
for help on using the changeset viewer.