diff options
Feature cars search (#91)
Merge of feature-cars-search branch to 'master'
3 files changed, 137 insertions, 0 deletions
| -rwxr-xr-x | wp-content/plugins/dx-vehicles/dx-vehicles.php | 3 | ||||
| -rw-r--r-- | wp-content/plugins/dx-vehicles/search-class.php | 106 | ||||
| -rwxr-xr-x | wp-content/plugins/dx-vehicles/taxonomies-hierarchical-class.php | 28 |
diff --git a/wp-content/plugins/dx-vehicles/dx-vehicles.php b/wp-content/plugins/dx-vehicles/dx-vehicles.php index d352e79..8ad9496 100755 --- a/wp-content/plugins/dx-vehicles/dx-vehicles.php +++ b/wp-content/plugins/dx-vehicles/dx-vehicles.php @@ -14,10 +14,13 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); include 'taxonomies-non-hierarchical-class.php'; include 'taxonomies-hierarchical-class.php'; include 'vehicles-class.php'; +include 'search-class.php'; $cars = new Vehicles; $tax = new Taxonomies; $tax1 = new TaxonomiesHierarchical; +$search = new Search; $cars->init(); $tax->init(); $tax1->init(); +$search->init(); diff --git a/wp-content/plugins/dx-vehicles/search-class.php b/wp-content/plugins/dx-vehicles/search-class.php new file mode 100644 index 0000000..43316e5 --- /dev/null +++ b/wp-content/plugins/dx-vehicles/search-class.php @@ -0,0 +1,106 @@ +<?php +class Search { + function init() { + add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ), 1 ); + + add_filter( 'query_vars', array( $this, 'register_query_vars' ) ); + } + + static $tax_vars = array( // tax_name (what is in the request) => tax_key (what is in the database, only hierarchical taxonomies are different from tax_name ) + 'brand' => 'vehicle-type', + 'category' => 'vehicle-category', + 'color' => 'vehicle-color', + 'conditoin' => 'vehicle-condition', + 'fuel' => 'vehicle-fuel', + 'gearbox' => 'vehicle-gearbox', + 'location' => 'vehicle-location', + 'model' => 'vehicle-type', + 'type' => 'vehicle-category', + ); + + static $meta_vars = array( // meta_name (name of post meta, stored in the database) => meta_keys (what is in the request) + 'vehicle-horsepower' => array( + 'start_horsepower', 'end_horsepower' + ), + 'vehicle-price' => array( + 'start_price', 'end_price' + ), + 'vehicle-range' => array( + 'start_range', 'end_range' + ), + 'vehicle-year' => array( + 'start_year', 'end_year' + ), + ); + + function register_query_vars( $vars ) { // didn't use array_merge because it caused some problems + foreach( $this::$tax_vars as $key => $value ) { + $vars[] = $key; + } + foreach( $this::$meta_vars as $key => $value ) { + foreach( $value as $name ) { + $vars[] = $name; + } + } + return $vars; + } + + function get_slug_taxonomy_array( $name, $terms ) { + $taxonomy_arr = array( + 'taxonomy' => $name, + 'field' => 'slug', + 'terms' => $terms, + ); + return $taxonomy_arr; + } + + function get_num_meta_array( $key, $value1, $value2 ) { + $result; + if( ! empty( $value1 ) && ! empty( $value2 ) ) { + $result = array( + 'key' => $key, + 'value' => array( $value1, $value2 ), + 'type' => 'NUMERIC', + 'compare' => 'BETWEEN', + ); + } else if( ! empty( $value1 ) ) { + $result = array( + 'key' => $key, + 'value' => $value1, + 'type' => 'NUMERIC', + 'compare' => '>=', + ); + } else { + $result = array( + 'key' => $key, + 'value' => $value2, + 'type' => 'NUMERIC', + 'compare' => '<=', + ); + } + return $result; + } + + function pre_get_posts( $query ) { + $query->set( 'post_type', 'vehicles' ); + + $tax_query = array(); + + foreach( $this::$tax_vars as $tax_name => $tax_key ) { + if( ! empty( get_query_var( $tax_name ) ) ) { + $tax_query[] = $this->get_slug_taxonomy_array( $tax_key, get_query_var( $tax_name ) ); + } + } + + $meta_query = array(); + + foreach( $this::$meta_vars as $meta_name => $meta_keys ) { + if( ! empty( get_query_var( $meta_keys[0] ) ) || ! empty( get_query_var( $meta_keys[1] ) ) ) { + $meta_query[] = $this->get_num_meta_array( $meta_name, get_query_var( $meta_keys[0] ), get_query_var( $meta_keys[1] ) ); + } + } + + $query->set( 'meta_query', $meta_query ); + $query->set( 'tax_query', $tax_query ); + } +} diff --git a/wp-content/plugins/dx-vehicles/taxonomies-hierarchical-class.php b/wp-content/plugins/dx-vehicles/taxonomies-hierarchical-class.php index a1825f4..82c1089 100755 --- a/wp-content/plugins/dx-vehicles/taxonomies-hierarchical-class.php +++ b/wp-content/plugins/dx-vehicles/taxonomies-hierarchical-class.php @@ -21,6 +21,23 @@ class TaxonomiesHierarchical { return $arr; } + public function labels_types() { + $arr = array ( + 'name' => _x( 'Types', 'taxonomy general name' ), + 'singular_name' => _x( 'Type', 'taxonomy singular name' ), + 'search_items' => __( 'Search Type', 'textdomain' ), + 'all_items' => __( 'All Types', 'textdomain' ), + 'parent_item' => __( 'Parent Type', 'textdomain' ), + 'parent_item_colon' => __( 'Parent Type:', 'textdomain' ), + 'edit_item' => __( 'Edit Type' ), + 'update_item' => __( 'Update Type' ), + 'add_new_item' => __( 'Add New Type' ), + 'new_item_name' => __( 'New Type Name' ), + 'menu_name' => __( 'Type' ), + ); + return $arr; + } + public function tax_create(){ register_taxonomy( 'vehicle-category', 'vehicles', array( 'hierarchical' => true, @@ -33,5 +50,16 @@ class TaxonomiesHierarchical { 'rewrite' => array( 'slug' => 'vehicle-category' ), ) ); + register_taxonomy( 'vehicle-type', 'vehicles', array( + 'hierarchical' => true, + 'labels' => $this->labels_types(), + 'show_ui' => true, + 'show_admin_column' => true, + 'show_in_rest' => true, + 'update_count_callback' => '_update_post_term_count', + 'query_var' => true, + 'rewrite' => array( 'slug' => 'car-type' ), + ) + ); } } |
