first commit of trail maps plugin master
authorAnthony Talarico <talarico@gaslightmedia.com>
Thu, 23 Feb 2017 16:31:05 +0000 (11:31 -0500)
committerAnthony Talarico <talarico@gaslightmedia.com>
Thu, 23 Feb 2017 16:31:05 +0000 (11:31 -0500)
enqueue.php [new file with mode: 0644]
functions.php [new file with mode: 0644]
index.php [new file with mode: 0644]
index.php~ [new file with mode: 0644]
main.js [new file with mode: 0644]

diff --git a/enqueue.php b/enqueue.php
new file mode 100644 (file)
index 0000000..55c083a
--- /dev/null
@@ -0,0 +1,27 @@
+<?php
+
+/* 
+ * 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.
+ */
+add_action('wp_head','ajaxurl');
+function ajaxurl() {
+?>
+<script type="text/javascript">
+var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
+</script>
+
+<?php
+}
+function enqueue_script() {   
+    wp_enqueue_script('jquery');
+    wp_enqueue_script(
+       'my_custom_script',
+        plugin_dir_url( __FILE__ ) . 'main.js', 
+        'jquery',
+        '1.0',
+        true
+    );
+}
+add_action('wp_enqueue_scripts', 'enqueue_script');
diff --git a/functions.php b/functions.php
new file mode 100644 (file)
index 0000000..cf55a96
--- /dev/null
@@ -0,0 +1,116 @@
+<?php
+
+/* 
+ * 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.
+ */
+
+// get either the default list of the cities or counties, depending on the option paramenter
+    // 'county' will get the attributes, 'city' will get the attribute values
+    function get_county_cities($option){
+        $pa = wc_get_attribute_taxonomies();
+        foreach($pa as $attr=>$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 '<div id="trail-search-widget">';
+
+            $categories = get_activities('product_cat');
+            echo "<select id='trail-activities'>";
+            echo '<option data-taxonomy="default" value="default">Activity</option>';
+            foreach($categories as $cat){
+                if($cat->slug !== 'downloads'){
+                    echo '<option value="'. $cat->slug . '">' . $cat->name . '</option>';
+                }
+            }
+            echo "</select>";
+
+            $counties = get_county_cities('county');
+            echo "<select id='trail-counties'>";
+            echo '<option data-taxonomy="default" value="default">County</option>';
+            foreach($counties as $county){
+                echo '<option value="'.$county['slug'] .'">' . $county['name'] . '</option>';
+            }
+            echo '</select>';
+            $city_data = get_county_cities('city');
+            echo "<select id='trail-cities'>";
+            echo '<option data-taxonomy="default" value="default">City</option>';
+            foreach($city_data as $cd){
+                foreach($cd as $c){
+                     echo '<option data-taxonomy="' . $c->taxonomy . '"data-id="' . $c->term_id .'" value="'. $c->slug. '">' . $c->name . '</option>';
+                }
+            }
+        echo '</div>';
+    }
\ No newline at end of file
diff --git a/index.php b/index.php
new file mode 100644 (file)
index 0000000..ec40542
--- /dev/null
+++ b/index.php
@@ -0,0 +1,152 @@
+<?php
+/*
+Plugin Name: Trail Maps
+Description: Trail Maps
+Version: 1.0.0
+Author: Anthony Talarico (Gaslight Media)
+Text Domain: trail-maps
+*/
+    require_once('enqueue.php');
+    require_once('functions.php');
+
+    // begin the 'on change' event functions (county, city and activity) each one
+    // returns a variation of $trail_data, which is sent via ajax
+    function ajaxCall(){
+
+        // on county dropdown change //////////////////////////////////////////
+        if( isset($_REQUEST['county'] ) ){
+            $taxonomy  = filter_var($_REQUEST['taxonomy'], FILTER_SANITIZE_STRING);
+            $county    = filter_var($_REQUEST['county'], FILTER_SANITIZE_STRING);
+
+            if( $taxonomy === 'default'){
+//                $city_data = get_county_cities('city');
+//                foreach($city_data as $city){
+//                    foreach($city as $c){
+//                        $cities[$c->slug] = array('name' => $c->name, 'id' => $c->term_id, 'slug' => $c->slug, 'taxonomy' => $c->taxonomy);
+//                    }
+//                } 
+//                $activities = get_activities('product_cat');
+            } else {
+                $options   = array('hide_empty' => false);
+                $city_data = get_terms('pa_'. $county, $options);
+                
+                foreach($city_data as $city){
+                    $cities[$city->slug] = array('name' => $city->name, 'id' => $city->term_id, 'slug' => $city->slug, 'taxonomy' => $city->taxonomy);
+                } 
+                $term_list = get_products($county, false);
+                foreach($term_list as $term){
+                    foreach($term as $t){
+                        if( !in_array($t,$activities) && $t->slug !== 'downloads'){
+                            $activities[$t->term_id] = array('slug' => $t->slug, 'name'=> $t->name);
+                        }
+                    }
+                }
+            }
+
+            $trail_data = array(
+                'cities'        => $cities,
+                'activities'    => $activities
+            );
+
+        // on city dropdown change //////////////////////////////////////////
+        } else if( isset($_REQUEST['city']) ){
+            $city       = filter_var($_REQUEST['city'], FILTER_SANITIZE_STRING);
+            $taxonomy   = filter_var($_REQUEST['taxonomy'], FILTER_SANITIZE_STRING);
+            $activities = array();
+            
+            if($taxonomy === 'default'){
+//                $activities = get_activities('product_cat');
+//                $city_data = get_county_cities('city');
+//                 foreach($city_data as $cd){
+//                    foreach($cd as $c){
+//                       $cities[$c->slug] = array('name' => $c->name, 'id' => $c->term_id, 'slug' => $c->slug, 'taxonomy' => $c->taxonomy);
+//                    }
+//                 } 
+            } else {
+                $term_list = get_products(false, $taxonomy);
+                $activities = array();
+                foreach($term_list as $term){
+                    foreach($term as $t){
+                        if( !in_array($t,$activities) && $t->slug !== 'downloads'){
+                            $activities[$t->term_id] = array('slug' => $t->slug, 'name'=> $t->name);
+                        }
+                    }
+                }   
+            }
+
+            $trail_data = array(
+                'activities'    => $activities,
+                'cities'        => $cities,
+                'taxonomy'      => $taxonomy
+            );
+
+        // on activity dropdown change //////////////////////////////////////
+        } else if( isset( $_REQUEST['activity'] ) ){
+             $activity   = filter_var($_REQUEST['activity'], FILTER_SANITIZE_STRING);
+             $taxonomy   = filter_var($_REQUEST['taxonomy'], FILTER_SANITIZE_STRING);
+
+            if($taxonomy === 'default'){
+                 $activities = get_activities('product_cat');
+                 $city_data = get_county_cities('city');
+                 foreach($city_data as $cd){
+                    foreach($cd as $c){
+                       $cities[$c->slug] = array('name' => $c->name, 'id' => $c->term_id, 'slug' => $c->slug, 'taxonomy' => $c->taxonomy);
+                    }
+                 } 
+                 $county_info = get_county_cities('county');
+                 foreach($county_info as $county){
+                     $county_names[] = array('slug' => $county['slug'], 'name' => $county['name']);
+                 }
+
+            } else {
+                 $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);
+                        $counties[] = get_post_meta( $prod->ID , '_product_attributes' );
+                    }
+                }
+                $county_taxonomy = array();
+                foreach($counties as $county){
+                    foreach($county as $cty){
+                        foreach($cty as $c){
+                            if( !in_array($c['name'],$county_taxonomy) ){
+                                $county_taxonomy[] = $c['name'];
+                            }
+                        }
+                    }
+                }
+
+                foreach($county_taxonomy as $ct){
+                    $options = array('hide_empty' => false);
+                    $city_data[] = get_terms($ct, $options);
+                    $county_info[] = get_taxonomy($ct);
+                }
+
+                foreach($city_data as $city){
+                    foreach($city as $c){
+                        $cities[$c->slug] = array('name' => $c->name, 'id' => $c->term_id, 'slug' => $c->slug, 'taxonomy' => $c->taxonomy);
+                    }
+                }
+
+                foreach($county_info as $ci){
+                    $county_names[] = array('name' => $ci->label,'slug' => strtolower($ci->label));
+                } 
+            }
+
+            $trail_data = array(
+                'activities'    => $activities,
+                'counties'      => $county_names,
+                'cities'        => $cities
+            );
+        }
+
+        echo json_encode($trail_data);
+        wp_die();
+    }
+
+add_action('wp_ajax_trailmaps', 'ajaxCall');
+add_action('wp_ajax_nopriv_trailmaps', 'ajaxCall');
\ No newline at end of file
diff --git a/index.php~ b/index.php~
new file mode 100644 (file)
index 0000000..795900c
--- /dev/null
@@ -0,0 +1,155 @@
+<?php
+/*
+Plugin Name: Trail Maps
+Description: Trail Maps
+Version: 1.0.0
+Author: Anthony Talarico (Gaslight Media)
+Text Domain: trail-maps
+*/
+function enqueue_script() {   
+    wp_enqueue_script('jquery');
+    wp_enqueue_script(
+       'my_custom_script',
+        plugin_dir_url( __FILE__ ) . 'main.js', 
+        'jquery',
+        '1.0',
+        true
+    );
+}
+add_action('wp_enqueue_scripts', 'enqueue_script');
+
+function build_widget(){
+    $pa= wc_get_attribute_taxonomies();
+
+    foreach($pa as $attr=>$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);
+    }
+    echo "<select id='trail-counties'>";
+    foreach($counties as $county){
+        echo '<option value="'.$county['slug'] .'">' . $county['name'] . '</option>';
+    }
+    echo '</select>';
+
+    echo "<select id='trail-cities'>";
+    foreach($city_data as $cd){
+        foreach($cd as $c){
+             echo '<option value="'. $c->slug. '">' . $c->name . '</option>';
+        }
+    }
+    echo "</select>";
+
+     $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 "<select id='trail-activities'>";
+    foreach($categories as $cat){
+        echo '<option value="'. $cat->slug . '">' . $cat->name . '</option>';
+    }
+    echo "</select>";
+}
+
+add_action('wp_head','ajaxurl');
+function ajaxurl() {
+?>
+<script type="text/javascript">
+var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
+</script>
+
+<?php
+}
+function ajaxCall(){
+    global $wpdb;
+    global $product;
+    
+    if( isset($_REQUEST['county'] ) ){
+        
+        $county = filter_var($_REQUEST['county'], FILTER_SANITIZE_STRING);
+        $options = array('hide_empty' => 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 (file)
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 = $('<option>', {value: value.slug, text: value.name});
+                        option.attr('data-taxonomy', value.taxonomy);
+                        option.attr('data-id', value.id);
+                        activities.append(option);
+                    }
+                });
+                option = {};
+                cities.empty();
+                cities.append($('<option>', {value: 'default', text: 'City', 'data-taxonomy': 'default'}));
+                $.each(county_data.cities, function(index, value){
+                   var option = $('<option>', {value: value.slug, text: value.name});
+                   option.attr('data-taxonomy', value.taxonomy);
+                   option.attr('data-id', value.id);
+                   cities.append(option);
+                });
+            }
+        });
+    });
+    
+    // working here
+    cities.on('change', function(){
+        $('option:selected', this).attr('selected', 'selected');
+       var city     = this.value;
+       var taxonomy = $('option:selected', this).attr('data-taxonomy');
+        jQuery.ajax({
+            type:"POST",
+            dataType: 'json',
+            url: ajaxurl,
+            data: {
+                action: "trailmaps",
+                city: city,
+                taxonomy: taxonomy
+            },
+            success:function(data){
+                var city_data = data;
+              
+                $('#trail-activities option').not(':eq(0), :selected').remove();
+                var selected = $('#trail-activities option:selected').val();
+                $.each(city_data.activities, function(index, value) {
+                    if(value.slug !== selected){
+                        var option = $('<option>', {value: value.slug, text: value.name});
+                        option.attr('data-taxonomy', value.taxonomy);
+                        option.attr('data-id', value.id);
+                        activities.append(option);
+                    }
+                    
+                });
+
+            }
+        });
+    });
+    
+    activities.on('change', function(){
+       $('option:selected', this).attr('selected', 'selected');
+       var activity = this.value;
+       var taxonomy = $('option:selected', this).attr('data-taxonomy');
+        jQuery.ajax({
+            type:"POST",
+            dataType: 'json',
+            url: ajaxurl,
+            data: {
+                action: "trailmaps",
+                activity: activity,
+                taxonomy: taxonomy
+            },
+            success:function(data){
+               var activity_data = data;
+
+                option = {};
+                $('#trail-counties option').not(':eq(0), :selected').remove();
+                var selected = $('#trail-counties option:selected').val();
+//                counties.append($('<option>', {value: 'default', text: 'County', 'data-taxonomy': 'default'}));
+                $.each(activity_data.counties,function(index,value){
+                    if(value.slug !== selected){
+                        var option = $('<option>', {value: value.slug, text: value.name});
+                        option.attr('data-id', value.id);
+                        counties.append(option);
+                    }
+                });
+
+
+                option = {};
+                $('#trail-cities option').not(':eq(0), :selected').remove();
+                var selected = $('#trail-cities option:selected').val();
+//                cities.append($('<option>', {value: 'default', text: 'City', 'data-taxonomy': 'default'}));
+                $.each(activity_data.cities, function(index, value){
+                    if(value.slug !== selected){
+                        var option = $('<option>', {value: value.slug, text: value.name});
+                        option.attr('data-taxonomy', value.taxonomy);
+                        option.attr('data-id', value.id);
+                        cities.append(option);
+                    }
+                });
+
+                if( $('#trail-activities option:selected').val() === 'default'){
+                    option = {};
+                    activities.empty();
+                    activities.append($('<option>', {value: 'default', text: 'Activity', 'data-taxonomy': 'default'}));
+                    $.each(activity_data.activities, function(index, value) {
+                        var option = $('<option>', {value: value.slug, text: value.name});
+                        option.attr('data-taxonomy', value.taxonomy);
+                        option.attr('data-id', value.id);
+                        activities.append(option);
+                    });
+                }
+            }
+        });
+    });
+});
\ No newline at end of file