Skip to content
This repository has been archived by the owner on Aug 14, 2023. It is now read-only.

Merge pull request #227 from EkoJr/issue-223 #223 #227

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 35 additions & 72 deletions classes/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,39 +42,11 @@ public function widget( $args, $instance ) {

$options = $simple_map->get_options();

$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'] );

// Ensures Variables are always Defined
$instance = $this->set_instance( $instance );
/*
* Most of these variables could be reduced, and use the instance
* array instead.
*
* Same with $this->form method.
*/
// Search Form Options.
$show_address = $instance['show_address'];
$show_city = $instance['show_city'];
$show_state = $instance['show_state'];
$show_zip = $instance['show_zip'];
$show_distance = $instance['show_distance'];

$default_lat = $instance['default_lat'];
$default_lng = $instance['default_lng'];
$simplemap_page = $instance['simplemap_page'];

// Set taxonomies to available equivalents.
$show = array();
$terms = array();
foreach ( $options['taxonomies'] as $taxonomy => $tax_info ) {
$key = strtolower( $tax_info['plural'] );
$show[ $taxonomy ] = ! empty( $instance[ 'show_' . $key ] ) ? 1 : 0;
$terms[ $taxonomy ] = ! empty( $instance[ $key ] ) ? $instance[ $key ] : '';
}

$available = $terms;


//TODO update this to modern Widget API code.
$title = apply_filters( 'widget_title', $instance['title'] );
echo $before_widget;
if ( $title ) {
echo '<span class="sm-search-widget-title">' . $before_title . $title . $after_title . '</span>'; // XSS ok.
Expand All @@ -94,7 +66,7 @@ public function widget( $args, $instance ) {
$action = site_url();
} else {
$method = 'post';
$action = get_permalink( absint( $simplemap_page ) );
$action = get_permalink( absint( $instance['simplemap_page'] ) );
}

$location_search = '<div id="location_widget_search" >';
Expand All @@ -103,19 +75,19 @@ public function widget( $args, $instance ) {

$location_search .= apply_filters( 'sm-location-search-widget-table-top', '' );

if ( $show_address ) {
if ( $instance['show_address'] ) {
$location_search .= '<tr><td class="location_search_widget_address_cell location_search_widget_cell">' . apply_filters( 'sm-search-label-street', __( 'Street', 'simplemap' ) ) . ':<br /><input type="text" id="location_search_widget_address_field" name="location_search_address" /></td></tr>';
}
if ( $show_city ) {
if ( $instance['show_city'] ) {
$location_search .= '<tr><td class="location_search_widget_city_cell location_search_widget_cell">' . apply_filters( 'sm-search-label-city', __( 'City', 'simplemap' ) ) . ':<br /><input type="text" id="location_search_widget_city_field" name="location_search_city" /></td></tr>';
}
if ( $show_state ) {
if ( $instance['show_state'] ) {
$location_search .= '<tr><td class="location_search_widget_state_cell location_search_widget_cell">' . apply_filters( 'sm-search-label-state', __( 'State', 'simplemap' ) ) . ':<br /><input type="text" id="location_search_widget_state_field" name="location_search_state" /></td></tr>';
}
if ( $show_zip ) {
if ( $instance['show_zip'] ) {
$location_search .= '<tr><td class="location_search_widget_zip_cell location_search_widget_cell">' . apply_filters( 'sm-search-label-zip', __( 'Zip', 'simplemap' ) ) . ':<br /><input type="text" id="location_search_widget_zip_field" name="location_search_zip" /></td></tr>';
}
if ( $show_distance ) {
if ( $instance['show_distance'] ) {
$location_search .= '<tr><td class="location_search_widget_distance_cell location_search_widget_cell">' . apply_filters( 'sm-search-label-distance', __( 'Select a distance', 'simplemap' ) ) . ':<br /><select id="location_search_widget_distance_field" name="location_search_distance" >';

foreach ( $simple_map->get_search_radii() as $value ) {
Expand All @@ -127,12 +99,14 @@ public function widget( $args, $instance ) {
}

foreach ( $options['taxonomies'] as $taxonomy => $tax_info ) {
$plural_key = strtolower( $tax_info['plural'] );
// Place available values in array.
$available_terms = explode( ',', $available[ $taxonomy ] );
$available_terms = explode( ',', $instance[ $plural_key ] );
$valid = array();

// Loop through all days and create array of available days.
if ( $all_terms = get_terms( $taxonomy ) ) {
$all_terms = get_terms( $taxonomy );
if ( $all_terms ) {
foreach ( $all_terms as $key => $value ) {
if ( '' === $available_terms[0] || in_array( $value->term_id, $available_terms ) ) {
$valid[] = $value->term_id;
Expand All @@ -141,13 +115,14 @@ public function widget( $args, $instance ) {
}

// Show day filters if allowed.
if ( $all_terms && ! empty( $show[ $taxonomy ] ) ) {
if ( $all_terms && $instance[ 'show_' . $plural_key ] ) {
$php_taxonomy = str_replace( '-', '_', $taxonomy );
$term_search = '<tr><td class="location_search_' . strtolower( $tax_info['singular'] ) . '_cell location_search_cell">' . apply_filters( $php_taxonomy . '-text', __( $tax_info['plural'], 'simplemap' ) ) . ':<br />';

// Print checkbox for each available day.
foreach ( $valid as $key => $termid ) {
if ( $term = get_term_by( 'id', $termid, $taxonomy ) ) {
$term = get_term_by( 'id', $termid, $taxonomy );
if ( $term ) {
$term_search .= '<label for="location_search_widget_' . strtolower( $tax_info['plural'] ) . '_field_' . esc_attr( $term->term_id ) . '" class="no-linebreak"><input type="checkbox" name="location_search_' . $php_taxonomy . '_' . esc_attr( $term->term_id ) . 'field" id="location_search_widget_' . strtolower( $tax_info['plural'] ) . '_field_' . esc_attr( $term->term_id ) . '" value="' . esc_attr( $term->term_id ) . '" /> ' . esc_attr( $term->name ) . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label> ';
}
}
Expand All @@ -159,22 +134,22 @@ public function widget( $args, $instance ) {
}

// Hidden field for available days; we'll need this in the event that nothing is selected.
$term_search .= '<input type="hidden" id="avail_' . strtolower( $tax_info['plural'] ) . '" value="' . esc_attr( $terms[ $taxonomy ] ) . '" />';
$term_search .= '<input type="hidden" id="avail_' . strtolower( $tax_info['plural'] ) . '" value="' . esc_attr( $instance[ $plural_key ] ) . '" />';

$term_search = apply_filters( 'sm-location-' . strtolower( $tax_info['singular'] ) . '-search-widget', $term_search );
$location_search .= $term_search;
}

// Default lat / lng from shortcode?
if ( ! $default_lat ) {
$default_lat = $options['default_lat'];
if ( ! $instance['default_lat'] ) {
$instance['default_lat'] = $options['default_lat'];
}
if ( ! $default_lng ) {
$default_lng = $options['default_lng'];
if ( ! $instance['default_lng'] ) {
$instance['default_lng'] = $options['default_lng'];
}

$location_search .= "<input type='hidden' id='location_search_widget_default_lat' value='" . $default_lat . "' />";
$location_search .= "<input type='hidden' id='location_search_widget_default_lng' value='" . $default_lng . "' />";
$location_search .= "<input type='hidden' id='location_search_widget_default_lat' value='" . $instance['default_lat'] . "' />";
$location_search .= "<input type='hidden' id='location_search_widget_default_lng' value='" . $instance['default_lng'] . "' />";

// Hidden value for limit.
$location_search .= "<input type='hidden' name='location_search_widget_limit' id='location_search_widget_limit' value='" . $limit_value . "' />";
Expand All @@ -183,7 +158,7 @@ public function widget( $args, $instance ) {
$location_search .= "<input type='hidden' id='location_is_search_widget_results' name='location_is_search_results' value='1' />";

// Hidden value referencing page_id.
$location_search .= "<input type='hidden' name='page_id' value='" . absint( $simplemap_page ) . "' />";
$location_search .= "<input type='hidden' name='page_id' value='" . $instance['simplemap_page'] . "' />";

$location_search .= apply_filters( 'sm-location-search-widget-before-submit', '' );

Expand Down Expand Up @@ -222,45 +197,35 @@ public function update( $new_instance, $old_instance ) {
*/
public function form( $instance ) {
$instance = $this->set_instance( $instance );

$title = esc_attr( $instance['title'] );
$show_address = $instance['show_address'];
$show_city = $instance['show_city'];
$show_state = $instance['show_state'];
$show_zip = $instance['show_zip'];
$show_distance = $instance['show_distance'];
$default_lat = ! empty( $instance['default_lat'] ) ? esc_attr( $instance['default_lat'] ) : 0;
$default_lng = ! empty( $instance['default_lng'] ) ? esc_attr( $instance['default_lng'] ) : 0;
$simplemap_page = $instance['simplemap_page'];
?>

<p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>"/>
name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>"/>
</p>

<p><input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_address' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'show_address' ) ); ?>"<?php checked( $show_address ); ?> />
name="<?php echo esc_attr( $this->get_field_name( 'show_address' ) ); ?>"<?php checked( $instance['show_address'] ); ?> />
<label
for="<?php echo esc_attr( $this->get_field_id( 'show_address' ) ); ?>"><?php _e( 'Show Address', 'simplemap' ); ?></label><br/>

<input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_city' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'show_city' ) ); ?>"<?php checked( $show_city ); ?> />
name="<?php echo esc_attr( $this->get_field_name( 'show_city' ) ); ?>"<?php checked( $instance['show_city'] ); ?> />
<label
for="<?php echo esc_attr( $this->get_field_id( 'show_city' ) ); ?>"><?php _e( 'Show City', 'simplemap' ); ?></label><br/>

<input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_state' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'show_state' ) ); ?>"<?php checked( $show_state ); ?> />
name="<?php echo esc_attr( $this->get_field_name( 'show_state' ) ); ?>"<?php checked( $instance['show_state'] ); ?> />
<label
for="<?php echo $this->get_field_id( 'show_state' ); ?>"><?php _e( 'Show State', 'simplemap' ); ?></label><br/>

<input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_zip' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'show_zip' ) ); ?>"<?php checked( $show_zip ); ?> />
name="<?php echo esc_attr( $this->get_field_name( 'show_zip' ) ); ?>"<?php checked( $instance['show_zip'] ); ?> />
<label
for="<?php echo $this->get_field_id( 'show_zip' ); ?>"><?php _e( 'Show Zip', 'simplemap' ); ?></label><br/>

<input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_distance' ) ); ?>"
name="<?php echo $this->get_field_name( 'show_distance' ); ?>"<?php checked( $show_distance ); ?> />
name="<?php echo $this->get_field_name( 'show_distance' ); ?>"<?php checked( $instance['show_distance'] ); ?> />
<label
for="<?php echo $this->get_field_id( 'show_distance' ); ?>"><?php _e( 'Show Distance', 'simplemap' ); ?></label><br/>

Expand All @@ -269,13 +234,11 @@ public function form( $instance ) {
$options = $simple_map->get_options();
foreach ( $options['taxonomies'] as $taxonomy => $tax_info ) {
$key = strtolower( $tax_info['plural'] );
$show_field = 'show_' . $key;
$show = isset( $instance[ $show_field ] ) ? (bool) $instance[ $show_field ] : false;
?>
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( $show_field ); ?>"
name="<?php echo $this->get_field_name( $show_field ); ?>"<?php checked( $show ); ?> />
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'show_' . $key ); ?>"
name="<?php echo $this->get_field_name( 'show_' . $key ); ?>"<?php checked( $instance[ 'show_' . $key ] ); ?> />
<label
for="<?php echo $this->get_field_id( $show_field ); ?>"><?php _e( 'Show ' . $tax_info['plural'], 'simplemap' ); ?></label>
for="<?php echo $this->get_field_id( 'show_' . $key ); ?>"><?php _e( 'Show ' . $tax_info['plural'], 'simplemap' ); ?></label>
<br/>

<?php
Expand All @@ -290,17 +253,17 @@ public function form( $instance ) {

/** TODO: The commented out code below isn't working yet. Implement it.
* <p><label for="<?php echo $this->get_field_id( 'default_lat' ); ?>"><?php _e( 'Default Lat:', 'simplemap' ); ?></label>
* <input class="widefat" id="<?php echo $this->get_field_id( 'default_lat' ); ?>" name="<?php echo $this->get_field_name( 'default_lat' ); ?>" type="text" value="<?php echo $default_lat; ?>" /></p>
* <input class="widefat" id="<?php echo $this->get_field_id( 'default_lat' ); ?>" name="<?php echo $this->get_field_name( 'default_lat' ); ?>" type="text" value="<?php echo esc_attr( $instance['default_lat'] ); ?>" /></p>
*
* <p><label for="<?php echo $this->get_field_id( 'default_lng' ); ?>"><?php _e( 'Default Lng:', 'simplemap' ); ?></label>
* <input class="widefat" id="<?php echo $this->get_field_id( 'default_lng' ); ?>" name="<?php echo $this->get_field_name( 'default_lng' ); ?>" type="text" value="<?php echo $default_lng; ?>" /></p>
* <input class="widefat" id="<?php echo $this->get_field_id( 'default_lng' ); ?>" name="<?php echo $this->get_field_name( 'default_lng' ); ?>" type="text" value="<?php echo esc_attr( $instance['default_lng'] ); ?>" /></p>
*/
?>
<p><label
for="<?php echo $this->get_field_id( 'simplemap_page' ); ?>">SimpleMap <?php _e( 'Page or Post ID:', 'simplemap' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'simplemap_page' ); ?>"
name="<?php echo $this->get_field_name( 'simplemap_page' ); ?>" type="text"
value="<?php echo $simplemap_page; ?>"/></p>
value="<?php echo $instance['simplemap_page']; ?>"/></p>

<?php
}
Expand Down