<?php
namespace Jet_Engine\Modules\Maps_Listings;
class Render extends \Jet_Engine_Render_Listing_Grid {
public function get_name() {
return 'jet-engine-maps-listing';
}
public function default_settings() {
return array(
'lisitng_id' => '',
'address_field' => '',
'add_lat_lng' => '',
'lat_lng_address_field' => '',
'auto_center' => true,
'max_zoom' => '',
'custom_center' => '',
'custom_zoom' => 11,
'zoom_control' => 'auto',
'zoom_controls' => 'true',
'fullscreen_control' => 'true',
'street_view_controls' => 'true',
'map_type_controls' => 'true',
'posts_query' => array(),
'meta_query_relation' => 'AND',
'tax_query_relation' => 'AND',
'hide_widget_if' => '',
'popup_width' => 320,
'popup_offset' => 40,
'marker_type' => 'image',
'marker_image' => null,
'marker_icon' => null,
'marker_label_type' => 'post_title',
'marker_label_field' => '',
'marker_label_field_custom' => '',
'marker_label_text' => '',
'marker_label_format_cb' => 0,
'marker_label_custom' => false,
'marker_label_custom_output' => '%s',
'marker_image_field' => '',
'marker_image_field_custom' => '',
'multiple_marker_types' => false,
'multiple_markers' => array(),
'marker_clustering' => 'true',
'popup_pin' => false,
'popup_preloader' => false,
);
}
/**
* Get posts
*
* @param array $settings
* @return array
*/
public function get_posts( $settings ) {
$args = $this->build_posts_query_args_array( $settings );
$query = new \WP_Query( $args );
$this->query_vars['page'] = $query->get( 'paged' ) ? $query->get( 'paged' ) : 1;
$this->query_vars['pages'] = $query->max_num_pages;
$this->query_vars['request'] = $args;
return $query->posts;
}
/**
* Returns encoded map data
*
* @param array $settings [description]
* @return [type] [description]
*/
public function get_map_data( $settings = array() ) {
$result = array(
'zoomControl' => isset( $settings['zoom_controls'] ) ? filter_var( $settings['zoom_controls'], FILTER_VALIDATE_BOOLEAN ) : true,
'fullscreenControl' => isset( $settings['fullscreen_control'] ) ? filter_var( $settings['fullscreen_control'], FILTER_VALIDATE_BOOLEAN ) : true,
'streetViewControl' => isset( $settings['street_view_controls'] ) ? filter_var( $settings['street_view_controls'], FILTER_VALIDATE_BOOLEAN ) : true,
'mapTypeControl' => isset( $settings['map_type_controls'] ) ? filter_var( $settings['map_type_controls'], FILTER_VALIDATE_BOOLEAN ) : true,
);
return htmlspecialchars( json_encode( $result ) );
}
/**
* Returns map markers list
*
* @param array $query [description]
* @param array $settings [description]
* @return [type] [description]
*/
public function get_map_markers( $query = array(), $settings = array(), $json = true ) {
$result = array();
$address_field = ! empty( $settings['address_field'] ) ? $settings['address_field'] : false;
$add_lat_lng = ! empty( $settings['add_lat_lng'] ) ? $settings['add_lat_lng'] : false;
$add_lat_lng = filter_var( $add_lat_lng, FILTER_VALIDATE_BOOLEAN );
$lat_lng_address = ! empty( $settings['lat_lng_address_field'] ) ? $settings['lat_lng_address_field'] : false;
if ( $address_field || ( $add_lat_lng && $lat_lng_address ) ) {
foreach ( $query as $post ) {
$address = false;
if ( $address_field ) {
$fields = explode( '+', $address_field );
if ( 1 === count( $fields ) ) {
$address = get_post_meta( $post->ID, $address_field, true );
} else {
$address = Module::instance()->lat_lng->get_address_from_fields_group( $post->ID, $fields );
}
}
if ( ! $address && $add_lat_lng ) {
$fields = explode( '+', $lat_lng_address );
if ( 1 === count( $fields ) ) {
$address = get_post_meta( $post->ID, $lat_lng_address, true );
if ( $address ) {
$address = explode( ',', $address );
if ( 2 !== count( $address ) ) {
$address = false;
} else {
$address = array(
'lat' => trim( $address[0] ),
'lng' => trim( $address[1] ),
);
}
}
} else {
$miss_fields = false;
$keys = array( 'lat', 'lng' );
$address = array();
for ( $i = 0; $i < 2; $i++ ) {
$field = isset( $fields[ $i ] ) ? $fields[ $i ] : false;
$key = $keys[ $i ];
if ( ! $field ) {
$miss_fields = true;
} else {
$address_val = get_post_meta( $post->ID, $field, true );
if ( ! $address_val ) {
$miss_fields = true;
}
$address[ $key ] = $address_val;
}
}
if ( $miss_fields ) {
$address = false;
continue;
}
}
}
if ( empty( $address ) ) {
continue;
}
$latlang = Module::instance()->lat_lng->get( $post->ID, $address );
if ( empty( $latlang ) ) {
continue;
}
$result[] = array(
'id' => $post->ID,
'latLang' => $latlang,
'label' => $this->get_marker_label( $post->ID, $settings ),
'custom_marker' => $this->get_custom_marker( $post->ID, $settings ),
);
}
}
$result = apply_filters( 'jet-engine/maps-listing/map-markers', $result );
if ( $json ) {
return htmlspecialchars( json_encode( $result ) );
} else {
return $result;
}
}
/**
* Returns custom marker HTML (for dynamic image source or if dynamic markers is configured) or false
*/
public function get_custom_marker( $post_id, $settings ) {
$conditional_marker = $this->get_conditional_marker( $post_id, $settings );
if ( $conditional_marker ) {
return $conditional_marker;
}
$type = ! empty( $settings['marker_type'] ) ? $settings['marker_type'] : 'image';
if ( 'dynamic_image' !== $type ) {
return false;
}
$field = ! empty( $settings['marker_image_field'] ) ? $settings['marker_image_field'] : false;
$field = ! empty( $settings['marker_image_field_custom'] ) ? $settings['marker_image_field_custom'] : $field;
if ( ! $field ) {
return false;
}
$image = get_post_meta( $post_id, $field, true );
$image_id = absint( $image );
if ( $image_id ) {
$image_cache_key = 'jet_engine_marker_' . $image_id;
$image_url = wp_cache_get( $image_cache_key );
if ( ! $image_url ) {
$image_url = wp_get_attachment_image_url( $image_id, 'full' );
wp_cache_set( $image_cache_key, $image_url );
}
} else {
$image_url = $image;
}
return sprintf( '<img src="%1$s" alt="" style="cursor: pointer;">', $image_url );
}
/**
* Check if we need apply to this marker condiional marker data
*/
public function get_conditional_marker( $post_id, $settings ) {
$multiple_marker_types = ! empty( $settings['multiple_marker_types'] ) ? $settings['multiple_marker_types'] : false;
$multiple_marker_types = filter_var( $multiple_marker_types, FILTER_VALIDATE_BOOLEAN );
if ( ! $multiple_marker_types ) {
return false;
}
$marker_types = ! empty( $settings['multiple_markers'] ) ? $settings['multiple_markers'] : array();
if ( empty( $marker_types ) ) {
return false;
}
foreach ( $marker_types as $marker ) {
$condition_met = false;
$apply_type = ! empty( $marker['apply_type'] ) ? $marker['apply_type'] : false;
switch ( $apply_type ) {
case 'meta_field':
$field = ! empty( $marker['field_name'] ) ? $marker['field_name'] : false;
$field = ! empty( $marker['field_name_custom'] ) ? $marker['field_name_custom'] : $field;
if ( $field ) {
$field_value = ! empty( $marker['field_value'] ) ? $marker['field_value'] : false;
$saved_value = get_post_meta( $post_id, $field, true );
if ( is_array( $saved_value ) && ! empty( $saved_value ) ) {
if ( in_array( 'true', $saved_value ) || in_array( 'false', $saved_value ) ) {
$condition_met = ( isset( $saved_value[ $field_value ] ) && true === filter_var( $saved_value[ $field_value ], FILTER_VALIDATE_BOOLEAN ) );
} else {
$condition_met = in_array( $field_value, $saved_value );
}
} else {
$condition_met = ( $field_value == $saved_value );
}
}
break;
case 'post_term':
$taxonomy = ! empty( $marker['tax_name'] ) ? $marker['tax_name'] : false;
$term = ! empty( $marker['term_name'] ) ? $marker['term_name'] : false;
if ( $taxonomy && $term ) {
$condition_met = has_term( $term, $taxonomy, $post_id );
}
break;
}
if ( $condition_met ) {
$result = $this->get_marker_data( $marker );
if ( $result && ! empty( $result['html'] ) ) {
return $result['html'];
} elseif ( $result && ! empty( $result['url'] ) ) {
return sprintf( '<img src="%1$s" alt="" style="cursor: pointer;">', $result['url'] );
}
}
}
return false;
}
/**
* Returns marker label
*
* @param [type] $post_id [description]
* @param array $settings [description]
* @return [type] [description]
*/
public function get_marker_label( $post_id, $settings = array() ) {
$type = ! empty( $settings['marker_type'] ) ? $settings['marker_type'] : 'image';
Loading ...