Created
November 26, 2012 20:04
-
-
Save rfair404/4150286 to your computer and use it in GitHub Desktop.
Rip off the Agentpress Listings Widget to enable Keyword Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This widget presents a search widget which uses listings' taxonomy for search fields. | |
* | |
* @package AgentPressTwo | |
* @since 2.0 | |
* @author Russell Fair | |
*/ | |
class AgentPressTwo_Listings_Search_Widget extends WP_Widget { | |
function AgentPressTwo_Listings_Search_Widget() { | |
$widget_ops = array( 'classname' => 'property-search2', 'description' => __( 'Display property search dropdown', 'apl' ) ); | |
$control_ops = array( 'width' => 200, 'height' => 250, 'id_base' => 'property-search2' ); | |
$this->WP_Widget( 'property-search2', __( 'AgentPressTwo - Listing Search', 'apl' ), $widget_ops, $control_ops ); | |
} | |
function widget( $args, $instance ) { | |
$instance = wp_parse_args( (array) $instance, array( | |
'title' => '', | |
'search_text' => __('keyword', 'apl'), | |
'button_text' => __( 'Search Properties', 'apl' ) | |
) ); | |
global $_agentpress_taxonomies; | |
$listings_taxonomies = $_agentpress_taxonomies->get_taxonomies(); | |
extract( $args ); | |
echo $before_widget; | |
if ( $instance['title'] ) echo $before_title . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $after_title; | |
echo '<form role="search" method="get" id="searchform" action="' . home_url( '/' ) . '" ><input type="hidden" value="" name="s" /><input type="hidden" value="listing" name="post_type" />'; | |
$listings_taxonomies = array_reverse( $listings_taxonomies ); | |
foreach ( $listings_taxonomies as $tax => $data ) { | |
if ( ! isset( $instance[$tax] ) || ! $instance[$tax] ) | |
continue; | |
$terms = get_terms( $tax, array( 'orderby' => 'name', 'order' => 'DESC', 'number' => 100, 'hierarchical' => false ) ); | |
if ( empty( $terms ) ) | |
continue; | |
$current = ! empty( $wp_query->query_vars[$tax] ) ? $wp_query->query_vars[$tax] : ''; | |
echo "<select name='$tax' id='$tax' class='agentpress-taxonomy'>\n\t"; | |
echo '<option value="" ' . selected( $current == '', true, false ) . ">{$data['labels']['name']}</option>\n"; | |
foreach ( (array) $terms as $term ) | |
echo "\t<option value='{$term->slug}' " . selected( $current, $term->slug, false ) . ">{$term->name}</option>\n"; | |
echo '</select>'; | |
} | |
$onfocus = sprintf("if (this.value == '%s') {this.value = '';}", esc_attr( $instance['search_text'] ) ); | |
$onblur = sprintf("if (this.value == '') {this.value = '%s';}", esc_attr( $instance['search_text'] ) ); | |
echo '<input type="text" id="kwsearch" value="'.esc_attr( $instance['search_text'] ).'" name="s" class="search" onfocus="'.$onfocus.'" onblur="'.$onblur.'" />'; | |
echo '<input type="submit" id="searchsubmit" class="searchsubmit" value="'. esc_attr( $instance['button_text'] ) .'" />'; | |
echo '<script type="text/javascript">jQuery(function($){$("#searchform").submit(function(){if( ($("#kwsearch").attr("value")) == "'.esc_attr( $instance['search_text'] ).'") { $("#kwsearch").attr("value", ""); }})});</script>'; | |
//function cleanSearch(){alert($("#kwsearch").attr("value"));}cleanSearch(); | |
echo '<div class="clear"></div></form>'; | |
echo $after_widget; | |
} | |
function update( $new_instance, $old_instance ) { | |
return $new_instance; | |
} | |
function form( $instance ) { | |
$instance = wp_parse_args( (array) $instance, array( | |
'title' => '', | |
'search_text' => __('keyword', 'apl'), | |
'button_text' => __( 'Search Properties', 'apl' ) | |
) ); | |
global $_agentpress_taxonomies; | |
$listings_taxonomies = $_agentpress_taxonomies->get_taxonomies(); | |
$new_widget = empty( $instance ); | |
printf( '<p><label for="%s">%s</label><input type="text" id="%s" name="%s" value="%s" style="%s" /></p>', $this->get_field_id( 'title' ), __( 'Title:', 'apl' ), $this->get_field_id( 'title' ), $this->get_field_name( 'title' ), esc_attr( $instance['title'] ), 'width: 95%;' ); | |
?> | |
<h5><?php _e( 'Include these taxonomies in the search widget', 'apl' ); ?></h5> | |
<?php | |
foreach ( (array) $listings_taxonomies as $tax => $data ) { | |
$terms = get_terms( $tax ); | |
if ( empty( $terms ) ) | |
continue; | |
?> | |
<p><label><input id="<?php echo $this->get_field_id( $tax ); ?>" type="checkbox" name="<?php echo $this->get_field_name( $tax ); ?>" value="1" <?php checked( 1, $instance[$tax] || $new_widget ); ?>/> <?php echo $data['labels']['name']; ?></label></p> | |
<?php | |
} | |
printf( '<p><label for="%s">%s</label><input type="text" id="%s" name="%s" value="%s" style="%s" /></p>', $this->get_field_id( 'search_text' ), __( 'Search Text:', 'apl' ), $this->get_field_id( 'search_text' ), $this->get_field_name( 'search_text' ), esc_attr( $instance['search_text'] ), 'width: 95%;' ); | |
printf( '<p><label for="%s">%s</label><input type="text" id="%s" name="%s" value="%s" style="%s" /></p>', $this->get_field_id( 'button_text' ), __( 'Button Text:', 'apl' ), $this->get_field_id( 'button_text' ), $this->get_field_name( 'button_text' ), esc_attr( $instance['button_text'] ), 'width: 95%;' ); | |
} | |
} | |
<?php | |
/** | |
* Register Widgets that will be used in the AgentPress Listings plugin | |
* | |
* @since 0.1.0 | |
* @author Russell Fair | |
*/ | |
function agentpresstwo_register_widgets() { | |
$widgets = array( 'AgentPressTwo_Listings_Search_Widget' ); | |
foreach ( (array) $widgets as $widget ) { | |
register_widget( $widget ); | |
} | |
} | |
add_action( 'widgets_init', 'agentpresstwo_register_widgets' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment