Created
July 5, 2017 08:51
-
-
Save mustardBees/b4e05286b09c35896947787f4d302950 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Reverse geocode case studies | |
Plugin URI: https://www.philwylie.co.uk/ | |
Description: Reverse geocode case studies. | |
Version: 1.0.0 | |
Author: Phil Wylie | |
Author URI: https://www.philwylie.co.uk/ | |
License: GPL2 | |
*/ | |
function iweb_reverse_geocode_case_studies() { | |
if ( ! isset( $_GET['reverse-geocode-case-studies'] ) ) { | |
return; | |
} | |
$args = array( | |
'posts_per_page' => -1, | |
'post_type' => 'case_study', | |
'post_status' => 'any', | |
); | |
$case_studies = new WP_Query( $args ); | |
if ( $case_studies->have_posts() ) { | |
while ( $case_studies->have_posts() ) { | |
$case_studies->the_post(); | |
echo '<li style="margin: 0 0 5px;">'; | |
$location = get_post_meta( get_the_ID(), '_cmb_location', true ); | |
if ( is_array( $location ) && ! empty( array_filter( $location ) ) ) { | |
echo '<span style="background: green; display: inline-block; width: 120px; text-align: center; padding: 4px; margin: 0 5px; 0 0">has location</span>'; | |
$url = add_query_arg( 'latlng', sprintf( '%s,%s', $location['latitude'], $location['longitude'] ), 'https://maps.googleapis.com/maps/api/geocode/json' ); | |
$response = wp_remote_get( $url ); | |
$api_response = json_decode( wp_remote_retrieve_body( $response ), true ); | |
$country = ''; | |
if ( isset( $api_response['results'][0]['address_components'] ) ) { | |
foreach ( $api_response['results'][0]['address_components'] as $key => $address_component ) { | |
if ($address_component['types'][0] == "administrative_area_level_1") { | |
$country = $address_component['long_name']; | |
continue; | |
} | |
} | |
} | |
if ( ! empty( $country ) ) { | |
wp_set_object_terms( get_the_ID(), $country, 'case_study_country' ); | |
echo '<span style="background: green; display: inline-block; width: 120px; text-align: center; padding: 4px; margin: 0 5px; 0 0">' . $country . '</span>'; | |
} else { | |
echo '<span style="background: red; display: inline-block; width: 120px; text-align: center; padding: 4px; margin: 0 5px; 0 0">no country</span>'; | |
} | |
} else { | |
echo '<span style="background: red; display: inline-block; width: 120px; text-align: center; padding: 4px; margin: 0 5px; 0 0">no location</span>'; | |
} | |
echo 'Working on [' . get_the_ID() . '] ' . get_the_title(); | |
} | |
} | |
wp_reset_postdata(); | |
exit; | |
} | |
add_action( 'init', 'iweb_reverse_geocode_case_studies' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment