From: Anthony Talarico Date: Thu, 23 Feb 2017 16:31:05 +0000 (-0500) Subject: first commit of trail maps plugin X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/index.cgi?a=commitdiff_plain;h=HEAD;p=prog%2Ftrail-maps.git first commit of trail maps plugin --- 98845d1ac7c8d2aebc54c0c05f1ce533d8019f7f diff --git a/enqueue.php b/enqueue.php new file mode 100644 index 0000000..55c083a --- /dev/null +++ b/enqueue.php @@ -0,0 +1,27 @@ + + + +$a){ + $counties[] = array('name' => $a->attribute_label, 'id' => $a->attribute_id, 'slug' => $a->attribute_name); + $options = array('hide_empty' => false); + $city_data[] = get_terms('pa_'. $a->attribute_name, $options); + } + if($option === 'county'){ + return $counties; + }else if($option === 'city'){ + return $city_data; + } + } + + // get a default list of all of the categories by taxonomy name + function get_activities($tax){ + $taxonomy = $tax; + $orderby = 'name'; + $show_count = 0; // 1 for yes, 0 for no + $hierarchical = 1; // 1 for yes, 0 for no + $title = ''; + $empty = 0; + + $args = array( + 'taxonomy' => $taxonomy, + 'orderby' => $orderby, + 'show_count' => $show_count, + 'hierarchical' => $hierarchical, + 'title_li' => $title, + 'hide_empty' => $empty + ); + return $categories = get_categories( $args ); + } + + // get a list of products by product attributes or by category + function get_products($county = false, $taxonomy = false){ + if( $county !== false){ + $args = array ( + 'post_type' => 'product', + 'posts_per_page' => -1, + 'meta_query' => array( + array( + 'key' => '_product_attributes', + 'value' => 'pa_'. $county, + 'compare' => 'LIKE' + ), + ), + ); + } else if( $taxonomy !== false) { + $args = array ( + 'post_type' => 'product', + 'posts_per_page' => -1, + 'meta_query' => array( + array( + 'key' => '_product_attributes', + 'value' => $taxonomy, + 'compare' => 'LIKE' + ), + ), + ); + } + $products = new WP_Query( $args ); + $product_posts[] = $products->posts; + foreach($product_posts as $product){ + foreach($product as $prod){ + $product_info[$prod->post_name] = array('title' => $prod->post_title,'id' => $prod->ID) ; + } + } + foreach($product_info as $info){ + $term_list[] = wp_get_post_terms($info['id'],'product_cat',array('fields'=>'all')); + } + return $term_list; + } + + // generate the initial dropdown before any 'on change' events + // call this function in theme + function build_elements(){ + echo '
'; + + $categories = get_activities('product_cat'); + echo ""; + + $counties = get_county_cities('county'); + echo "'; + $city_data = get_county_cities('city'); + echo ""; + foreach($counties as $county){ + echo ''; + } + echo ''; + + echo ""; + + $taxonomy = 'product_cat'; + $orderby = 'name'; + $show_count = 0; // 1 for yes, 0 for no + $hierarchical = 1; // 1 for yes, 0 for no + $title = ''; + $empty = 0; + + $args = array( + 'taxonomy' => $taxonomy, + 'orderby' => $orderby, + 'show_count' => $show_count, + 'hierarchical' => $hierarchical, + 'title_li' => $title, + 'hide_empty' => $empty + ); + $categories = get_categories( $args ); + + echo ""; +} + +add_action('wp_head','ajaxurl'); +function ajaxurl() { +?> + + + false); + $city_data = get_terms('pa_'. $county, $options); + + foreach($city_data as $city){ + $cities[] = $city; + } + + $args = array ( + 'post_type' => 'product', + 'posts_per_page' => -1, + 'meta_query' => array( + array( + 'key' => '_product_attributes', + 'value' => 'pa_'. $county, + 'compare' => 'LIKE' + ), + ), + ); + $products = new WP_Query( $args ); + $product_posts[] = $products->posts; + foreach($product_posts as $product){ + foreach($product as $prod){ + $product_info[$prod->post_name] = array('title' => $prod->post_title,'id' => $prod->ID) ; + } + } + foreach($product_info as $info){ + $term_list[] = wp_get_post_terms($info['id'],'product_cat',array('fields'=>'all')); + } + + $activities = array(); + foreach($term_list as $term){ + foreach($term as $t){ + if( !in_array($t,$activities) ){ + $activities[$t->term_id] = array('slug' => $t->slug, 'name'=> $t->name); + } + } + } + + $trail_data = array( + 'cities' => $cities, + 'products' => $product_info, + 'activities' => $activities + ); + + } else if( isset($_REQUEST['city']) ){ + + } else if( isset( $_REQUEST['activity'] ) ){ + $activity= filter_var($_REQUEST['activity'], FILTER_SANITIZE_STRING); + $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => $activity, 'orderby' => 'rand' ); + $products = new WP_Query( $args ); + + $product_posts[] = $products->posts; + foreach($product_posts as $product){ + foreach($product as $prod){ + $product_info[$prod->post_name] = array('title' => $prod->post_title,'id' => $prod->ID); + $county_names[] = get_post_meta( $prod->ID , '_product_attributes' ); + } + } + foreach($county_names as $key=>$county){ + $county_data[] = $county->name; + } + $trail_data = array( + 'products' => $product_info, + 'counties' => $county_data + ); + + } + + echo json_encode($trail_data); + wp_die(); +} +add_action('wp_ajax_trailmaps', 'ajaxCall', 9999); +add_action('wp_ajax_nopriv_trailmaps', 'ajaxCall', 9999); diff --git a/main.js b/main.js new file mode 100644 index 0000000..febadc2 --- /dev/null +++ b/main.js @@ -0,0 +1,146 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +// Load foundation +jQuery(document).ready(function ($) { + var page = $('body'); + var activities = page.find('#trail-activities'); + var counties = page.find('#trail-counties'); + var cities = page.find('#trail-cities'); + var widget = page.find('#trail-search-widget'); + var selected_item; + + counties.on('change', function(){ + $('option:selected', this).attr('selected', 'selected'); + var county = this.value; + var selected = $('option:selected', this).val(); + var taxonomy = $('option:selected', this).attr('data-taxonomy'); + jQuery.ajax({ + type:"POST", + dataType: 'json', + url: ajaxurl, + data: { + action: "trailmaps", + county: county, + taxonomy: taxonomy + }, + success:function(data){ + var county_data = data; + $('option:selected', this).attr('selected', 'selected'); + option = {}; + $('#trail-activities option').not(':eq(0), :selected').remove(); + var selected = $('#trail-activities option:selected').val(); + $.each(county_data.activities, function(index, value) { + if(value.slug !== selected){ + var option = $('