Skip to content

Commit

Permalink
Merge pull request #143 from xylusthemes/added_geolocation_in_ical
Browse files Browse the repository at this point in the history
Enhanced the location data in the iCal method.
  • Loading branch information
Rajat1192 committed Apr 20, 2024
2 parents 60d94e1 + 3e9b576 commit bc225ec
Show file tree
Hide file tree
Showing 7 changed files with 291 additions and 98 deletions.
2 changes: 1 addition & 1 deletion assets/css/import-facebook-events-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ table.ife_form_table tr td:nth-child(1) {
width: 100%;
}

.ife_google_maps_api_key{
.ife_google_maps_api_key, .ife_google_geolocation_api_key{
width: 50%;
}

Expand Down
48 changes: 48 additions & 0 deletions assets/js/import-facebook-events-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,52 @@ jQuery(document).ready(function($){
ife_xhr.send();
}

const ife_ggl_apikey_input = document.querySelector('.ife_google_geolocation_api_key');
if ( ife_ggl_apikey_input ) {
ife_ggl_apikey_input.addEventListener('input', function() {
const ife_ggl_check_key = document.querySelector('.ife_ggl_check_key');
if (ife_ggl_apikey_input.value.trim() !== '') {
ife_ggl_check_key.style.display = 'contents';
} else {
ife_ggl_check_key.style.display = 'none';
}
});
}

const ife_ggl_checkkeylink = document.querySelector('.ife_ggl_check_key a');
if ( ife_ggl_checkkeylink ) {
ife_ggl_checkkeylink.addEventListener('click', function(event) {
event.preventDefault();
const ife_ggl_apikey = ife_ggl_apikey_input.value.trim();
if ( ife_ggl_apikey !== '' ) {
ife_check_geolocation_apikey(ife_ggl_apikey);
}
});
}

function ife_check_geolocation_apikey(ife_ggl_apikey) {
const ife_ggl_xhr = new XMLHttpRequest();
ife_ggl_xhr.open('GET', 'https://maps.googleapis.com/maps/api/geocode/json?address=kalupur+swamianarayan+mandir&key=' + encodeURIComponent(ife_ggl_apikey), true);
const ife_ggl_loader = document.getElementById('ife_ggl_loader');
ife_ggl_loader.style.display = 'inline-block';
ife_ggl_xhr.onreadystatechange = function() {
if ( ife_ggl_xhr.readyState === XMLHttpRequest.DONE ) {
ife_ggl_loader.style.display = 'none';
var responseObject = JSON.parse( ife_ggl_xhr.response );
console.log( responseObject );
if (ife_ggl_xhr.status === 200 && responseObject.status === "OK" ) {
var ife_gm_success_notice = jQuery("#ife_ggl_success_message");
ife_gm_success_notice.html('<span class="ife_gmap_success_message">Valid Google GeoLocation License Key</span>');
setTimeout(function(){ ife_gm_success_notice.empty(); }, 2000);
} else {
var ife_gm_error_notice = jQuery("#ife_ggl_error_message");
ife_gm_error_notice.html( '<span class="ife_gmap_error_message" >Inalid Google GeoLocation License Key</span>' );
setTimeout(function(){ ife_gm_error_notice.empty(); }, 2000);
}
}
};

ife_ggl_xhr.send();
}

});
85 changes: 69 additions & 16 deletions includes/class-import-facebook-events-ical_parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,22 +391,75 @@ public function get_location( $event, $event_venue ) {
$location = 'Online Event';
}

$event_location = array(
'ID' => strtolower( trim( stripslashes( $location ) ) ),
'name' => isset( $location ) ? stripslashes( $location ) : '',
'description' => '',
'address_1' => '',
'address_2' => '',
'city' => '',
'state' => '',
'country' => '',
'zip' => '',
'lat' => '',
'long' => '',
'full_address' => isset( $location ) ? stripslashes( $location ) : '',
'url' => '',
'image_url' => ''
);
//Get Geo Location API Key
$api_key = get_option( 'ife_google_geolocation_api_key', false );
if( !empty( $api_key ) && !empty( $location ) ){

$url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode( $location ) . '&key=' . $api_key;
$response_data = file_get_contents($url);
$data = json_decode($response_data);

// Extracting relevant data
$result = $data->results[0];
$address_components = $result->address_components;
$geometry = $result->geometry;

$street_number = $route = $city = $state = $country = $postal_code = '';
foreach ($address_components as $component) {
if (in_array('street_number', $component->types)) {
$street_number = $component->long_name;
} elseif (in_array('route', $component->types)) {
$route = $component->long_name;
} elseif (in_array('locality', $component->types)) {
$city = $component->long_name;
} elseif (in_array('administrative_area_level_1', $component->types)) {
$state = $component->long_name;
} elseif (in_array('country', $component->types)) {
$country = $component->long_name;
} elseif (in_array('postal_code', $component->types)) {
$postal_code = $component->long_name;
}
}

// Extracting geometry
$latitude = $geometry->location->lat;
$longitude = $geometry->location->lng;
$id = strtolower(str_replace(' ', '_', $location_name ) );

$event_location = array(
'ID' => $id,
'name' => isset( $location_name ) ? stripslashes( $location_name ) : '',
'description' => '',
'address_1' => $street_number . ' ' . $route,
'address_2' => '',
'city' => $city,
'state' => $state,
'country' => $country,
'zip' => $postal_code,
'lat' => $latitude,
'long' => $longitude,
'full_address' => $result->formatted_address,
'url' => '',
'image_url' => ''
);
}else{
$event_location = array(
'ID' => strtolower( trim( stripslashes( $location ) ) ),
'name' => isset( $location ) ? stripslashes( $location ) : '',
'description' => '',
'address_1' => '',
'address_2' => '',
'city' => '',
'state' => '',
'country' => '',
'zip' => '',
'lat' => $latitude,
'long' => $longitude,
'full_address' => isset( $location ) ? stripslashes( $location ) : '',
'url' => '',
'image_url' => ''
);
}
}

return $event_location;
Expand Down
63 changes: 58 additions & 5 deletions includes/class-import-facebook-events-ical_parser_aioec.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,8 @@ public function get_location( $event, $event_venue ) {
'full_address' => isset( $event_venue->location->street ) ? $event_venue->location->street : '',
'url' => '',
'image_url' => '',
);
return $event_location;
);

}else{
$geo = $event->getProperty( 'GEO' );
$latitude = isset( $geo['latitude'] ) ? (float)$geo['latitude'] : '';
Expand All @@ -414,7 +414,59 @@ public function get_location( $event, $event_venue ) {
if ( empty( $location ) ) {
return null;
}
if ( !empty( $location ) || !empty( $geo ) ) {

//Get Geo Location API Key
$api_key = get_option( 'ife_google_geolocation_api_key', false );
if( !empty( $api_key ) && !empty( $location ) ){

$url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode( $location ) . '&key=' . $api_key;
$response_data = file_get_contents($url);
$data = json_decode($response_data);

// Extracting relevant data
$result = $data->results[0];
$address_components = $result->address_components;
$geometry = $result->geometry;

$street_number = $route = $city = $state = $country = $postal_code = '';
foreach ($address_components as $component) {
if (in_array('street_number', $component->types)) {
$street_number = $component->long_name;
} elseif (in_array('route', $component->types)) {
$route = $component->long_name;
} elseif (in_array('locality', $component->types)) {
$city = $component->long_name;
} elseif (in_array('administrative_area_level_1', $component->types)) {
$state = $component->long_name;
} elseif (in_array('country', $component->types)) {
$country = $component->long_name;
} elseif (in_array('postal_code', $component->types)) {
$postal_code = $component->long_name;
}
}

// Extracting geometry
$latitude = $geometry->location->lat;
$longitude = $geometry->location->lng;
$id = strtolower(str_replace(' ', '_', $location_name ) );

$event_location = array(
'ID' => $id,
'name' => isset( $location_name ) ? stripslashes( $location_name ) : '',
'description' => '',
'address_1' => $street_number . ' ' . $route,
'address_2' => '',
'city' => $city,
'state' => $state,
'country' => $country,
'zip' => $postal_code,
'lat' => $latitude,
'long' => $longitude,
'full_address' => $result->formatted_address,
'url' => '',
'image_url' => ''
);
}else{
$event_location = array(
'ID' => strtolower( trim( stripslashes( $location ) ) ),
'name' => isset( $location ) ? stripslashes( $location ) : '',
Expand All @@ -427,13 +479,14 @@ public function get_location( $event, $event_venue ) {
'zip' => '',
'lat' => $latitude,
'long' => $longitude,
'full_address' => '',
'full_address' => isset( $location ) ? stripslashes( $location ) : '',
'url' => '',
'image_url' => ''
);
}
return $event_location;
}

return $event_location;
}

/**
Expand Down
8 changes: 5 additions & 3 deletions includes/class-import-facebook-events-manage-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ public function handle_gma_settings_submit() {
global $ife_errors, $ife_success_msg;
if ( isset( $_POST['ife_gma_action'] ) && 'ife_save_gma_settings' === sanitize_text_field( wp_unslash( $_POST['ife_gma_action'] ) ) && check_admin_referer( 'ife_gma_setting_form_nonce_action', 'ife_gma_setting_form_nonce' ) ) { // input var okay.
$gma_option = array();
$gma_option['ife_google_maps_api_key'] = isset( $_POST['ife_google_maps_api_key'] ) ? wp_unslash( $_POST['ife_google_maps_api_key'] ) : ''; // input var okay.
$is_update = update_option( 'ife_google_maps_api_key', $gma_option['ife_google_maps_api_key'] );
if ( $is_update ) {
$gma_option['ife_google_maps_api_key'] = isset( $_POST['ife_google_maps_api_key'] ) ? wp_unslash( $_POST['ife_google_maps_api_key'] ) : '';
$gma_option['ife_google_geolocation_api_key'] = isset( $_POST['ife_google_geolocation_api_key'] ) ? wp_unslash( $_POST['ife_google_geolocation_api_key'] ) : '';
$is_gm_update = update_option( 'ife_google_maps_api_key', $gma_option['ife_google_maps_api_key'] );
$is_ggl_update = update_option( 'ife_google_geolocation_api_key', $gma_option['ife_google_geolocation_api_key'] );
if ( $is_gm_update || $is_ggl_update ) {
$ife_success_msg[] = __( 'Google Maps API Key has been saved successfully.', 'import-facebook-events' );
} else {
$ife_errors[] = __( 'Something went wrong! please try again.', 'import-facebook-events' );
Expand Down

0 comments on commit bc225ec

Please sign in to comment.