admin = $admin;
$this->post_type_name = $post_type_name;
$this->category = $category;
}
/**
* @return Admin
*/
public function getAdmin() {
return $this->admin;
}
/**
* @return mixed
*/
public function getPostTypeName() {
return $this->post_type_name;
}
/**
* @return mixed
*/
public function getCategory() {
return $this->category;
}
/**
* @param array $categories
*
* @return array
*/
public function sanitizeCategoriesArray( array $categories ) {
$newCategories = array();
if(!empty($categories)) {
foreach ($categories as $cat) {
if(intval($cat)) {
array_push($newCategories, absint($cat));
}
}
}
return $newCategories;
}
/**
* @param $keyword
*
* @return array
*/
public function getPostIn( $keyword ) {
$query_title = get_posts(
array(
'post_type' => $this->post_type_name,
's' => $keyword,
'numberposts' => - 1
)
);
$query_meta = get_posts(
array(
'post_type' => $this->post_type_name,
'numberposts' => - 1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_assl_geo_citta',
'value' => $keyword,
'compare' => 'LIKE'
),
array(
'key' => '_assl_geo_cap',
'value' => $keyword,
'compare' => 'LIKE'
),
array(
'key' => '_assl_geo_nazione',
'value' => $keyword,
'compare' => 'LIKE'
),
array(
'key' => '_assl_geo_via',
'value' => $keyword,
'compare' => 'LIKE'
)
)
)
);
$merged = array_merge( $query_title, $query_meta );
$post_ids = array();
foreach ( $merged as $item ) {
$post_ids[] = $item->ID;
}
$post__in = array_unique( $post_ids );
return $post__in;
}
/**
* @return string
*/
public function getStoreCategories() {
$out = '';
$terms = get_terms( $this->category, array( 'parent' => 0 ) );
if(!empty($terms)) {
$out .= '
';
$out .= '
';
$out .= '';
}
return $out;
}
/**
* @param $post_id
*
* @return array
*/
public function getStoreGeolocationData( $post_id ) {
$id = $post_id;
$title = get_the_title( $post_id );
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'assl-store-thumb' );
$lat = get_post_meta( $post_id, '_assl_geo_lat', true );
$lng = get_post_meta( $post_id, '_assl_geo_lng', true );
$icon = $this->getCategoryIcon( $post_id );
$via = get_post_meta( $post_id, '_assl_geo_via', true );
$num = get_post_meta( $post_id, '_assl_geo_num', true );
$citta = get_post_meta( $post_id, '_assl_geo_citta', true );
$stato = get_post_meta( $post_id, '_assl_geo_stato', true );
$cap = get_post_meta( $post_id, '_assl_geo_cap', true );
$nazione = get_post_meta( $post_id, '_assl_geo_nazione', true );
$telefono = get_post_meta( $post_id, '_assl_geo_tel', true );
$telefono2= get_post_meta( $post_id, '_assl_geo_tel2', true );
$fax = get_post_meta( $post_id, '_assl_geo_fax', true );
$website = get_post_meta( $post_id, '_assl_geo_url', true );
$email = get_post_meta( $post_id, '_assl_geo_mail', true );
$email2 = get_post_meta( $post_id, '_assl_geo_mail2', true );
return array(
'id' => $id,
'title' => $title,
'image' => $image[0],
'lat' => floatval( $lat ),
'lng' => floatval( $lng ),
'icon' => $icon,
'via' => $via,
'num' => $num,
'citta' => $citta,
'stato' => $stato,
'cap' => $cap,
'nazione' => $nazione,
'telefono' => $telefono,
'telefono2'=> $telefono2,
'fax' => $fax,
'website' => $website,
'email' => antispambot($email),
'email2' => antispambot($email2)
);
}
/**
* @param $lat1
* @param $lng1
* @param $lat2
* @param $lng2
* @param $unit
*
* @return float|int
*/
public function distanceCalculator( $lat1, $lng1, $lat2, $lng2, $unit ) {
$theta = $lng1 - $lng2;
$dist = sin( deg2rad( $lat1 ) ) * sin( deg2rad( $lat2 ) ) + cos( deg2rad( $lat1 ) ) * cos( deg2rad( $lat2 ) ) * cos( deg2rad( $theta ) );
$dist = acos( $dist );
$dist = rad2deg( $dist );
$miles = $dist * 60 * 1.1515;
switch ( $unit ) {
case 'km':
return ( $miles * 1.609344 );
break;
case 'mi.':
return intval( ( $miles * 1.609344 * 1000 ) );
break;
default:
return $miles;
break;
}
}
/**
* @param $post_id
*
* @return bool|string
*/
public function getCategoryIcon( $post_id ) {
//Marker di default del plugin
$defaultMarker = ASSL__PLUGIN_URL . "images/assl_marker.png";
//Marker di default scelto dall'utente per tutte le categorie
$defaultMarkerCategory = $this->admin->getOption( 'assl_pin_category_image', 'assl_admin_map' );
//Imposto il default
$default = $defaultMarkerCategory && "" != $defaultMarkerCategory ? $defaultMarkerCategory : $defaultMarker;
//Prendo la prima categoria
$categoria = $this->getFirstCategory( $post_id );
if ( $categoria ) {
$term = get_term_meta( $categoria->term_id, 'assl_store_image_category', true );
//Compatibilità con la vecchia versione che salvava un'array
$id = is_array($term) ? $term['id'] : $term;
//Se è un id valido prendo l'url altrimenti false
$image = $id && intval($id) ? wp_get_attachment_image_src($id, 'full') : false;
//Se trovo l'immagine la usa altrimenti metto quella di default
$icon = $image && isset($image[0]) ? $image[0] : $default;
} else {
$icon = $default;
}
return $icon;
}
/**
* @param $post_id
*
* @return mixed
*/
public function getFirstCategory( $post_id ) {
$post_terms = get_the_terms( $post_id, $this->category );
if ( ! empty( $post_terms ) ) {
return $post_terms[0];
} else {
return false;
}
}
/**
* @param $post_id
* @param $item
* @param array $distanza
*
* @return string
*/
private function getStoreLoop( $post_id, $item, array $distanza ) {
$lista = '';
$lista .= '';
if ( has_post_thumbnail( $post_id ) ) :
$lista .= get_the_post_thumbnail( $post_id, 'assl-store-thumb' );
$lista .= '
';
endif;
$lista .= '
 . ')
';
$lista .= '
';
$lista .= '';
$lista .= '
' . get_the_title( $post_id ) . '
';
$poi_cat = $this->getFirstCategory( $post_id );
if ( $poi_cat ) {
$lista .= '
' . $poi_cat->name . '
';
}
$lista .= apply_filters( 'the_content', get_post_field( 'post_content', $post_id ) );
$lista .= '
';
$via = get_post_meta( $post_id, '_assl_geo_via', true );
$num = get_post_meta( $post_id, '_assl_geo_num', true );
$cap = get_post_meta( $post_id, '_assl_geo_cap', true );
$city = get_post_meta( $post_id, '_assl_geo_citta', true );
$tel = get_post_meta( $post_id, '_assl_geo_tel', true );
$tel2 = get_post_meta( $post_id, '_assl_geo_tel2', true );
$fax = get_post_meta( $post_id, '_assl_geo_fax', true );
$mail = get_post_meta( $post_id, '_assl_geo_mail', true );
$mail2= get_post_meta( $post_id, '_assl_geo_mail2', true );
$url = get_post_meta( $post_id, '_assl_geo_url', true );
$lista .= $via;
if ( ! empty( $via ) ) :
$lista .= ! empty( $num ) ? ', ' . $num . '
' : '
';
endif;
$lista .= ! empty( $cap ) ? $cap : '';
$lista .= ( ! empty( $cap ) && ! empty( $city ) ) ? ' - ' : '';
$lista .= ! empty( $city ) ? $city . '
' : '
';
$lista .= ! empty( $tel ) ? ' ' . $tel . '
' : '';
$lista .= ! empty( $tel2 ) ? ' ' . $tel2 . '
' : '';
$lista .= ! empty( $fax ) ? 'Fax: ' . $fax . '
' : '';
if ( ! empty( $mail ) ):
$lista .= ' '.antispambot($mail).'
';
endif;
if ( ! empty( $mail2 ) ):
$lista .= ' '.antispambot($mail2).'
';
endif;
if ( ! empty( $url ) ):
$lista .= ' ' . preg_replace('#^https?://#', '', $url) . '';
endif;
$lista .= '';
if ( ! empty( $distanza ) && isset( $distanza['distanza'] ) && isset( $distanza['unit'] ) ) {
$km = intval( $distanza['distanza'] );
$lista .= '
' . __( 'Far: ', ASSL__TEXTDOMAIN ) . ' ' . $km . ' ' . $distanza['unit'] . '
';
}
$lista .= '
';
$lista .= '';
$lista .= '';
return $lista;
}
/**
* @param $args
* @param bool $usergeo
*
* @return object
*/
public function getStores( $args, $usergeo = false ) {
//Preparo un'array per la mappa
$poi_maps = array();
//Dati per Lista html
$lista = '';
$q = new \WP_Query( $args );
if ( $q->have_posts() ) :
$i = 0;
while ( $q->have_posts() ):
$q->the_post();
$post_id = get_the_ID();
//Loop articoli con geolocalizzazione
if ( $usergeo ) :
$maxDistance = intval( $usergeo[0] );
$lat1 = floatval( $usergeo[1] );
$lng1 = floatval( $usergeo[2] );
//Latitudine e longitudine del post
$lat2 = floatval( get_post_meta( $post_id, '_assl_geo_lat', true ) );
$lng2 = floatval( get_post_meta( $post_id, '_assl_geo_lng', true ) );
$unit_type = $this->admin->getOption( 'assl_km_mi', 'assl_admin_map' );
$unit = isset( $unit_type ) ? $unit_type : "km";
if ( ( isset( $lat1 ) && isset( $lng1 ) ) && ( isset( $lat2 ) && isset( $lng2 ) ) ) {
$distanza = floatval( $this->distanceCalculator( $lat1, $lng1, $lat2, $lng2, $unit ) );
} else {
$distanza = false;
}
if ( $distanza && ( $distanza < floatval( $maxDistance ) ) ) :
//Inserisco i poi nell'array per la mappa
$poi_maps[] = $this->getStoreGeolocationData( $post_id );
//Continuo con l'html
$lista .= $this->getStoreLoop( $post_id, $i, array(
'distanza' => $distanza,
'unit' => $unit
) );
$i ++;
endif;
else :
//Inserisco i poi nell'array per la mappa
$poi_maps[] = $this->getStoreGeolocationData( $post_id );
//Continuo con l'html
$lista .= $this->getStoreLoop( $post_id, $i, array() );
$i ++;
endif;
endwhile;
wp_reset_query();
wp_reset_postdata();
else:
//Non trovo nulla
endif;
//Ritorno i dati
$results = array(
'lista' => $lista,
'mappa' => $poi_maps,
'totstores' => count( $poi_maps )
);
return (object) $results;
}
/**
* @param $filter
*
* @return object
*
*/
public function getFilters( $filter ) {
$obj = array();
switch ( $filter ) {
case 'type':
$labelOption = $this->admin->getOption( 'assl_map_filter_labelName_type', 'assl_admin_advanced' );
$labelName = ( isset( $labelOption ) && $labelOption != '' ) ? $labelOption : __( 'Type', ASSL__TEXTDOMAIN );
$obj['button'] = '' . $labelName . '';
$obj['html'] = '' . $this->getStoreCategories() . '
';
break;
case 'radius':
$unit_type = $this->admin->getOption( 'assl_km_mi', 'assl_admin_map' );
$unit = $unit_type == 'km' ? 'METRIC' : 'IMPERIAL';
//cerchio raggio
$strokeColor = $this->admin->getOption( 'assl_map_radius_stroke_color', 'assl_admin_map' );
$strokeOpacity = $this->admin->getOption( 'assl_map_radius_stroke_opacity', 'assl_admin_map' );
$strokeWeight = $this->admin->getOption( 'assl_map_radius_stroke_weight', 'assl_admin_map' );
$fillColor = $this->admin->getOption( 'assl_map_radius_fill_color', 'assl_admin_map' );
$fillOpacity = $this->admin->getOption( 'assl_map_radius_fill_opacity', 'assl_admin_map' );
$radiusSettings = array(
'strokecolor' => $strokeColor ? $strokeColor : '#000',
'strokeopacity' => $strokeOpacity ? $strokeOpacity : 0.5,
'strokeweight' => $strokeWeight ? $strokeWeight : 1,
'fillcolor' => $fillColor ? $fillColor : '#000',
'fillopacity' => $fillOpacity ? $fillOpacity : 0.1
);
$labelOption = $this->admin->getOption( 'assl_map_filter_labelName_radius', 'assl_admin_advanced' );
$labelName = ( isset( $labelOption ) && $labelOption != '' ) ? $labelOption : __( 'Radius', ASSL__TEXTDOMAIN );
$obj['button'] = '' . $labelName . '';
$radius = '';
$radius .= "";
$radius .= '
';
$radius .= '
';
$obj['html'] = $radius;
break;
case 'list':
$labelOption = $this->admin->getOption( 'assl_map_filter_labelName_list', 'assl_admin_advanced' );
$labelName = ( isset( $labelOption ) && $labelOption != '' ) ? $labelOption : __( 'List', ASSL__TEXTDOMAIN );
$obj['button'] = '' . $labelName . '';
$obj['html'] = '';
break;
case 'search':
$labelOption = $this->admin->getOption( 'assl_map_filter_labelName_search', 'assl_admin_advanced' );
$labelName = ( isset( $labelOption ) && $labelOption != '' ) ? $labelOption : __( 'Search', ASSL__TEXTDOMAIN );
$obj['button'] = '' . $labelName . '';
$obj['html'] = '';
break;
}
return (object) $obj;
}
}