diff options
| Commit | 8afcd640327873a1c8bd6162785c050920566cf8 (patch) | |
|---|---|---|
| Author | 2020-07-15 09:33:12 +0300 | |
| Committer | 2020-07-15 09:33:12 +0300 | |
| Files | 7da1e3c64d1d115ceb51517b7931e4a7e6673178 /wp-content/plugins/dx-vehicles | |
| tues-interns-2020-feature/cookie-notice.tar tues-interns-2020-feature/cookie-notice.tar.gz tues-interns-2020-feature/cookie-notice.zip | ||
| Parent | 4d6f27167e27628c0a7ebc2b40eb01fd5b16597f (diff) | |
| Parent | 38f71eb84200aab19ae9f6e27102d05a10a6d0a1 (diff) | |
Sync feature/cookie-notice with the latest master and run Gulp for assets feature/cookie-notice
6 files changed, 449 insertions, 0 deletions
diff --git a/wp-content/plugins/dx-vehicles/dx-vehicles.php b/wp-content/plugins/dx-vehicles/dx-vehicles.php new file mode 100755 index 0000000..8ad9496 --- /dev/null +++ b/wp-content/plugins/dx-vehicles/dx-vehicles.php @@ -0,0 +1,26 @@ +<?php +/** + * Plugin Name: DX Vehicles + * Plugin URI: https://devrix.com + * Description: Plugin for handling vehicles + * Version: 0.2 + * Author: DevriX-interns-2020 + * Author URI: https://devrix.com + * Text Domain: dx-vehicles + */ + +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 new file mode 100755 index 0000000..82c1089 --- /dev/null +++ b/wp-content/plugins/dx-vehicles/taxonomies-hierarchical-class.php @@ -0,0 +1,65 @@ +<?php + +class TaxonomiesHierarchical { + function init() { + add_action( 'init', array( $this, 'tax_create' ) , 999 ); + } + public function labels() { + $arr = array ( + 'name' => _x( 'Categories', 'taxonomy general name' ), + 'singular_name' => _x( 'Category', 'taxonomy singular name' ), + 'search_items' => __( 'Search Category', 'textdomain' ), + 'all_items' => __( 'All Categories', 'textdomain' ), + 'parent_item' => __( 'Parent Category', 'textdomain' ), + 'parent_item_colon' => __( 'Parent Category:', 'textdomain' ), + 'edit_item' => __( 'Edit Category' ), + 'update_item' => __( 'Update Category' ), + 'add_new_item' => __( 'Add New Category' ), + 'new_item_name' => __( 'New Category Name' ), + 'menu_name' => __( 'Category' ), + ); + 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, + 'labels' => $this->labels(), + 'show_ui' => true, + 'show_admin_column' => true, + 'show_in_rest' => true, + 'update_count_callback' => '_update_post_term_count', + 'query_var' => true, + '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' ), + ) + ); + } +} diff --git a/wp-content/plugins/dx-vehicles/taxonomies-non-hierarchical-class.php b/wp-content/plugins/dx-vehicles/taxonomies-non-hierarchical-class.php new file mode 100755 index 0000000..9ea1330 --- /dev/null +++ b/wp-content/plugins/dx-vehicles/taxonomies-non-hierarchical-class.php @@ -0,0 +1,93 @@ +<?php + +class Taxonomies { + function init() { + add_action( 'init', array( $this, 'tax_create' ) , 999 ); + } + + public function labels($name) { + $arr = array ( + 'name' => _x( $name.'s', 'taxonomy general name' ), + 'singular_name' => _x( $name, 'taxonomy singular name' ), + 'search_items' => __( 'Search '.$name.'s' ), + 'all_items' => __( 'All '.$name.'s' ), + 'parent_item' => null, + 'parent_item_colon' => null, + 'edit_item' => __( 'Edit '.$name ), + 'update_item' => __( 'Update '.$name ), + 'add_new_item' => __( 'Add New '.$name ), + 'new_item_name' => __( 'New '.$name.' Name' ), + 'menu_name' => __( $name.'s' ), + ); + return $arr; + } + + public function tax_create(){ + register_taxonomy( 'vehicle-location', 'vehicles', array( + 'hierarchical' => false, + 'labels' => $this->labels('Location'), + 'show_ui' => true, + 'show_admin_column' => true, + 'show_in_rest' => true, + 'update_count_callback' => '_update_post_term_count', + 'query_var' => true, + 'rewrite' => array( 'slug' => 'location' ), + ) + ); + register_taxonomy( 'vehicle-color', 'vehicles', array( + 'hierarchical' => false, + 'labels' => $this->labels('Color'), + 'show_ui' => true, + 'show_admin_column' => true, + 'show_in_rest' => true, + 'update_count_callback' => '_update_post_term_count', + 'query_var' => true, + 'rewrite' => array( 'slug' => 'color' ), + ) + ); + register_taxonomy( 'vehicle-condition', 'vehicles', array( + 'hierarchical' => false, + 'labels' => $this->labels('Condition'), + 'show_ui' => true, + 'show_admin_column' => true, + 'show_in_rest' => true, + 'update_count_callback' => '_update_post_term_count', + 'query_var' => true, + 'rewrite' => array( 'slug' => 'condition' ), + ) + ); + register_taxonomy( 'vehicle-gearbox', 'vehicles', array( + 'hierarchical' => false, + 'labels' => $this->labels('Gearbox'), + 'show_ui' => true, + 'show_admin_column' => true, + 'show_in_rest' => true, + 'update_count_callback' => '_update_post_term_count', + 'query_var' => true, + 'rewrite' => array( 'slug' => 'gearbox' ), + ) + ); + register_taxonomy( 'vehicle-fuel', 'vehicles', array( + 'hierarchical' => false, + 'labels' => $this->labels('Fuel'), + 'show_ui' => true, + 'show_admin_column' => true, + 'show_in_rest' => true, + 'update_count_callback' => '_update_post_term_count', + 'query_var' => true, + 'rewrite' => array( 'slug' => 'fuel' ), + ) + ); + register_taxonomy( 'vehicle-type', 'vehicles', array( + 'hierarchical' => false, + 'labels' => $this->labels('Type'), + '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' ), + ) + ); + } +} diff --git a/wp-content/plugins/dx-vehicles/vehicle-info-display-callback.php b/wp-content/plugins/dx-vehicles/vehicle-info-display-callback.php new file mode 100755 index 0000000..02d3308 --- /dev/null +++ b/wp-content/plugins/dx-vehicles/vehicle-info-display-callback.php @@ -0,0 +1,35 @@ +<table> + <tr> + <td><?php _e( 'Year:' ) ?></td> + <td><input type="text" name="vehicle-year" value="<?php echo( sanitize_text_field( get_post_meta( get_the_ID(), __( 'vehicle-year' ), true ) ) ) ?>"></td> + </tr> + <tr> + <td><?php _e( 'Millage:' ) ?></td> + <td><input type="text" name="vehicle-millage" value="<?php echo( sanitize_text_field( get_post_meta( get_the_ID(), __( 'vehicle-millage' ), true ) ) ) ?>"></td> + </tr> + <tr> + <td><?php _e( 'Horsepower:' ) ?></td> + <td><input type="text" name="vehicle-horsepower" value="<?php echo( sanitize_text_field( get_post_meta( get_the_ID(), __( 'vehicle-horsepower' ), true ) ) ) ?>"></td> + </tr> + <tr> + <td><?php _e( 'Range:' ) ?></td> + <td><input type="text" name="vehicle-range" value="<?php echo( sanitize_text_field( get_post_meta( get_the_ID(), __( 'vehicle-range' ), true ) ) ) ?>"></td> + </tr> + <tr> + <td><?php _e( 'Price:' ) ?></td> + <td><input type="text" name="vehicle-price" value="<?php echo( sanitize_text_field( get_post_meta( get_the_ID(), __( 'vehicle-price' ), true ) ) ) ?>"></td> + </tr> + <tr> + <td><?php _e( 'Photo:' ) ?></td> + <td><input type="file" name="vehicle-images[]" value="" multiple></td> + </tr> + <tr> + <td><?php _e( 'Uploaded photos<br>(might need page refresh):' ) ?></td> + <?php + $images = ( array )json_decode( get_post_meta( get_the_ID(), __( 'vehicle-images' ), true ) ); + foreach ( $images as $image ) { + echo( '<td> <img src="' . $image . '" alt=""></td>' ); + } + ?> + </tr> +</table> diff --git a/wp-content/plugins/dx-vehicles/vehicles-class.php b/wp-content/plugins/dx-vehicles/vehicles-class.php new file mode 100755 index 0000000..3a0b127 --- /dev/null +++ b/wp-content/plugins/dx-vehicles/vehicles-class.php @@ -0,0 +1,124 @@ +<?php +class Vehicles { + function init() { + add_action( 'init', array( $this, 'cpt_create' ) ); + add_action( 'add_meta_boxes', array( $this, 'register_meta_boxes' ) ); + add_action( 'save_post', array( $this, 'vehicle_info_save_meta' ), 1, 2 ); + } + + public function labels() { + $arr = array( + 'labels' => array( + 'name' => __( 'Vehicles' ), + 'singular_name' => __( 'Vehicle' ), + ), + 'public' => true, + 'has_archive' => true, + 'rewrite' => array( 'slug' => 'vehicles' ), + 'show_in_rest' => true, + 'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ), + ); + return $arr; + } + + function cpt_create( $post ) { + register_post_type( 'vehicles', $this->labels() ); + } + + function register_meta_boxes() { + add_meta_box( 'vehicle-info', __( 'Vehicle Information' ), array( $this, 'vehicle_info_display_callback' ), 'vehicles' ); + } + + function vehicle_info_display_callback( $post ) { + include 'vehicle-info-display-callback.php'; + } + + function remove_message( $location ) { + return remove_query_arg( 'message', $location ); + } + + function allowed_image_types() { + $mime_types = array( + 'jpg|jpeg|jpe' => 'image/jpeg', + 'gif' => 'image/gif', + 'png' => 'image/png', + 'bmp' => 'image/bmp', + 'tiff|tif' => 'image/tiff', + ); + return $mime_types; + } + + function get_enc_uploaded_img_urls( $images ) { + $uploaded_urls = array(); + for ( $i = 0; $i < sizeof( $images['name'] ); $i++ ) { + // Upload each valid file, throw error if file is not valid + if ( wp_check_filetype( $images['name'][ $i ], $this->allowed_image_types() )['ext'] ) { + $file = array( + 'name' => $images['name'][ $i ], + 'type' => $images['type'][ $i ], + 'tmp_name' => $images['tmp_name'][ $i ], + 'error' => $images['error'][ $i ], + 'size' => $images['size'][ $i ] + ); + // Note: images will not upload if you don't set 777 permission to wp-content folder and it's contents + $uploaded = wp_handle_upload( $file, array( 'test_form' => false ) ); + array_push( $uploaded_urls, $uploaded['url'] ); + } else { + wp_die( 'Invalid image' ); + } + } + return json_encode( $uploaded_urls ); + } + + function vehicle_info_save_meta( $post_id, $post ) { + // Return if the user doesn't have edit permissions. + if ( ! current_user_can( 'edit_post', $post_id ) ) { + return $post_id; + } + + // Now that we're authenticated, time to save the data. + // This sanitizes the data from the field and saves it into an array $events_meta. + $events_meta = array( + 'vehicle-year' => sanitize_text_field( isset( $_POST['vehicle-year'] ) ? $_POST['vehicle-year'] : null ), + 'vehicle-millage' => sanitize_text_field( isset( $_POST['vehicle-millage'] ) ? $_POST['vehicle-millage'] : null ), + 'vehicle-horsepower' => sanitize_text_field( isset( $_POST['vehicle-horsepower'] ) ? $_POST['vehicle-horsepower'] : null ), + 'vehicle-range' => sanitize_text_field( isset( $_POST['vehicle-range'] ) ? $_POST['vehicle-range'] : null ), + 'vehicle-price' => sanitize_text_field( isset( $_POST['vehicle-price'] ) ? $_POST['vehicle-price'] : null ), + 'vehicle-images' => isset( $_FILES['vehicle-images'] ) ? $this->get_enc_uploaded_img_urls( $_FILES['vehicle-images'] ): null, + ); + + // Cycle through the $events_meta array. + // Note, in this example we just have one item, but this is helpful if you have multiple. + foreach ( $events_meta as $key => $value ) { + if ( $value == null ) { + continue; + } + + switch ( $key ) { + case 'vehicle-year': + if( strcmp( "1769", $value ) > 0 || strcmp( "2020", $value ) < 0 ) wp_die( 'Invalid year' ); + break; + case 'vehicle-millage': + if( strcmp( "0", $value ) > 0 ) wp_die( 'Invalid millage.' ); + break; + case 'vehicle-horsepower': + if( strcmp( "0", $value ) > 0 ) wp_die( 'Invalid horsepower.' ); + break; + case 'vehicle-range': + if( strcmp( "0", $value ) > 0 ) wp_die( 'Invalid range.' ); + break; + case 'vehicle-price': + if( strcmp( "0", $value ) > 0 ) wp_die( 'Invalid price.' ); + break; + } + + if ( get_post_meta( $post_id, $key, false ) ) { + // If the custom field already has a value, update it. + update_post_meta( $post_id, $key, $value ); + } else { + // If the custom field doesn't have a value, add it. + add_post_meta( $post_id, $key, $value ); + } + } + } +} |
