Skip to content

Instantly share code, notes, and snippets.

@TheManFromEarth1
Created October 22, 2020 15:03
Show Gist options
  • Save TheManFromEarth1/b2bee9016a371f04d7fc6138ca518ff9 to your computer and use it in GitHub Desktop.
Save TheManFromEarth1/b2bee9016a371f04d7fc6138ca518ff9 to your computer and use it in GitHub Desktop.
/**
* Function to get information for a single location
* @return JSON Array with information
*/
public function single_location()
{
if($this->check_user()){
global $wpdb;
$user_id = (int) sanitize_text_field($_POST['user_id']);
$orders_table_name = $wpdb->prefix . 'wpapp_smart_orders';
$query_orders = $wpdb->get_results( $wpdb->prepare( "SELECT location_type, location_id FROM $orders_table_name WHERE user_id = %d AND confirmed = 1", $user_id) );
$my_cities = array();
$my_regions = array();
if(!empty($query_orders)){
foreach ($query_orders as $query_order) {
if($query_order->location_type == 'city'){
$my_cities[] =(int)$query_order->location_id;
} elseif ($query_order->location_type == 'region') {
$my_regions[] =(int)$query_order->location_id;
}
}
}
if(isset($_POST['post_id']) && is_numeric($_POST['post_id'])){
$post_id = (int) sanitize_text_field($_POST['post_id']);
$post = get_post( $post_id );
$cities = get_the_terms($post_id, 'smart_location_cities');
if($cities){
foreach ($cities as $city) {
$city_id = icl_object_id($city->term_id, 'smart_location_cities', true, ICL_LANGUAGE_CODE);
}
}
$regions = get_the_terms($post_id, 'smart_location_regions');
if($regions){
foreach ($regions as $region) {
$region_id = icl_object_id($region->term_id, 'smart_location_regions', true, ICL_LANGUAGE_CODE);
}
}
if(in_array($city_id, $my_cities) || in_array($region_id, $my_regions)){
//getting Post_id on the primary language
$post_id_primary = icl_object_id($post_id, 'location', true, ICL_LANGUAGE_CODE);
$single_location['location_title'] = html_entity_decode($post->post_title);
$single_location['location_content'] = sanitize_text_field($post->post_content);
$single_location['location_additional_info'] = sanitize_text_field(get_post_meta($post_id, '_smart_info', true));
$single_location['location_email'] = sanitize_email(get_post_meta($post_id, '_smart_email', true));
$single_location['location_street'] = sanitize_text_field(get_post_meta($post_id, '_smart_street', true));
$single_location['location_phone'] = get_post_meta($post_id, '_smart_phone', true);
$single_location['location_url'] = esc_url_raw(get_post_meta($post_id, '_smart_url', true), array('http', 'https'));
$single_location['location_city'] = sanitize_text_field(get_post_meta($post_id, '_smart_city', true));
$single_location['location_bookingcom_url'] = sanitize_text_field(get_post_meta($post_id, '_smart_bookingcom', true));
$single_location['location_info'] = sanitize_text_field(get_post_meta($post_id, '_smart_info', true));
$single_location['location_cuisine'] = sanitize_text_field(get_post_meta($post_id, '_smart_cuisine', true));
$single_location['location_price'] = sanitize_text_field(get_post_meta($post_id, '_smart_price', true));
$single_location['location_rating'] = (float) get_post_meta ($post_id_primary,'ratings_average', true );
$single_location['location_photo_credits'] = sanitize_text_field(get_post_meta($post_id, '_smart_photocredits', true));
$loc_categories = get_the_terms( $post_id, 'smart_location_categories' );
$single_location['location_category'] = array();
if (!empty($loc_categories)) {
foreach($loc_categories as $Loc_category) {
if($Loc_category != null) {
$single_location['location_category'][] = html_entity_decode($Loc_category->name);
}
}
}
$galleries = get_post_meta($post_id, '_smart_photos', true);
if(is_array($galleries) && !empty($galleries) ){
$single_location['location_gallery'] = array();
foreach ($galleries as $gallery){
$single_location['location_gallery'][] = sanitize_text_field(wp_get_attachment_url( $gallery));
}
}
$favorite_locations = get_user_meta($user_id, 'smart_locations_favorites', false);
if(in_array($post_id_primary, $favorite_locations) ){
$single_location['favorite_location'] = true;
} else {
$single_location['favorite_location'] = false;
}
WpApp_Res::json( $single_location );
} else{
WpApp_Res::json(array(
'success' => 0,
'message' => 'You have to buy city or region'
));
}
} else {
WpApp_Res::json(array('success' => 0));
}
} else {
WpApp_Res::json(array(
'success' => 0,
'message' => "You are not Logged In!"
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment