Changeset 2304807
- Timestamp:
- 05/14/2020 11:07:26 AM (6 years ago)
- Location:
- mobile-builder/trunk
- Files:
-
- 6 edited
-
admin/class-mobile-builder-admin.php (modified) (2 diffs)
-
api/class-mobile-builder-vendor.php (modified) (3 diffs)
-
helpers/mobile-builder-functions.php (modified) (1 diff)
-
includes/class-mobile-builder.php (modified) (1 diff)
-
product/class-mobile-builder-product.php (modified) (3 diffs)
-
public/class-mobile-builder-public.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
mobile-builder/trunk/admin/class-mobile-builder-admin.php
r2303204 r2304807 99 99 100 100 wp_localize_script( $this->plugin_name, 'wp_rnlab_configs', array( 101 'api_nonce' => wp_create_nonce( 'wp_rest' ), 102 'api_url' => rest_url( '' ), 103 'plugin_name' => $this->plugin_name, 101 'api_nonce' => wp_create_nonce( 'wp_rest' ), 102 'api_url' => rest_url( '' ), 103 'plugin_name' => $this->plugin_name, 104 'app' => 'foody', 105 'vendor' => 'wcfm', 104 106 ) 105 107 ); … … 176 178 global $wpdb; 177 179 $table_name = $wpdb->prefix . MOBILE_BUILDER_TABLE_NAME; 180 178 181 return $wpdb->get_results( "SELECT * FROM $table_name", OBJECT ); 179 182 } -
mobile-builder/trunk/api/class-mobile-builder-vendor.php
r2303204 r2304807 153 153 154 154 $vendor_stores[] = array_merge( $store, array( 155 'id' => $key, 155 156 'gravatar' => $gravatar_url, 156 157 'list_banner_url' => $list_banner_url, … … 160 161 'total_review_rating' => $store_user->get_total_review_rating(), 161 162 'total_review_count' => $store_user->get_total_review_count(), 163 'rating' => array( 164 'rating' => $store_user->get_total_review_rating(), 165 'count' => $store_user->get_total_review_count(), 166 'avg' => $store_user->get_avg_review_rating(), 167 ) 162 168 ) ); 163 169 } … … 238 244 239 245 /** 246 * 247 * Product distance 248 * 249 * @param $args 250 * @param $wp_query 251 * 252 * @return mixed 253 */ 254 public function mbd_product_distance( $args, $wp_query ) { 255 256 global $wpdb; 257 258 $lat = $_GET['lat']; 259 $lng = $_GET['lng']; 260 $distance = ! empty( $_GET['radius'] ) ? esc_sql( $_GET['radius'] ) : 50; 261 262 if ( $lat && $lng ) { 263 264 $earth_radius = 6371; 265 $units = 'km'; 266 $degree = 111.045; 267 268 // add units to locations data. 269 $args['fields'] .= ", '{$units}' AS units"; 270 271 $args['fields'] .= ", ROUND( {$earth_radius} * acos( cos( radians( {$lat} ) ) * cos( radians( gmw_locations.latitude ) ) * cos( radians( gmw_locations.longitude ) - radians( {$lng} ) ) + sin( radians( {$lat} ) ) * sin( radians( gmw_locations.latitude ) ) ),1 ) AS distance"; 272 $args['join'] .= " INNER JOIN {$wpdb->base_prefix}gmw_locations gmw_locations ON $wpdb->posts.ID = gmw_locations.object_id "; 273 274 // calculate the between point. 275 $bet_lat1 = $lat - ( $distance / $degree ); 276 $bet_lat2 = $lat + ( $distance / $degree ); 277 $bet_lng1 = $lng - ( $distance / ( $degree * cos( deg2rad( $lat ) ) ) ); 278 $bet_lng2 = $lng + ( $distance / ( $degree * cos( deg2rad( $lat ) ) ) ); 279 280 $args['where'] .= " AND gmw_locations.object_type = 'post'"; 281 $args['where'] .= " AND gmw_locations.latitude BETWEEN {$bet_lat1} AND {$bet_lat2}"; 282 //$args['where'] .= " AND gmw_locations.longitude BETWEEN {$bet_lng1} AND {$bet_lng2} "; 283 284 // filter locations based on the distance. 285 $args['having'] = "HAVING distance <= {$distance} OR distance IS NULL"; 286 287 $args['orderby'] .= ', distance ASC'; 288 289 } 290 291 return $args; 292 } 293 294 /** 240 295 * @param $args 241 296 * @param $wp_query -
mobile-builder/trunk/helpers/mobile-builder-functions.php
r2303204 r2304807 45 45 return $result; 46 46 } 47 48 /** 49 * 50 * Distance matrix 51 * 52 * @param $origin_string 53 * @param $destinations_string 54 * @param $key 55 * @param string $units 56 * 57 * @return mixed 58 */ 59 function mobile_builder_distance_matrix($origin_string, $destinations_string, $key, $units = 'metric') { 60 $google_map_api = 'https://maps.googleapis.com/maps/api'; 61 $url = "$google_map_api/distancematrix/json?units=$units&origins=$origin_string&destinations=$destinations_string&key=$key"; 62 return json_decode( mobile_builder_request( 'GET', $url ) )->rows; 63 } -
mobile-builder/trunk/includes/class-mobile-builder.php
r2303204 r2304807 214 214 $plugin_api = new Mobile_Builder_Vendor( $this->get_plugin_name(), $this->get_version() ); 215 215 $this->loader->add_action( 'rest_api_init', $plugin_api, 'add_api_routes', 10 ); 216 $this->loader->add_filter( 'posts_clauses', $plugin_api, 'mbd_product_list_geo_location_filter_post_clauses', 500, 2 );216 // $this->loader->add_filter( 'posts_clauses', $plugin_api, 'mbd_product_list_geo_location_filter_post_clauses', 500, 2 ); 217 217 $this->loader->add_filter( 'posts_clauses', $plugin_api, 'mbd_product_list_by_vendor', 501, 2 ); 218 $this->loader->add_filter( 'posts_clauses', $plugin_api, 'mbd_product_distance', 501, 2 ); 218 219 219 220 } -
mobile-builder/trunk/product/class-mobile-builder-product.php
r2303204 r2304807 65 65 $namespace = $this->plugin_name . '/v' . intval( $this->version ); 66 66 67 $products = new WC_REST_Products_Controller(); 68 67 69 register_rest_route( $namespace, 'rating-count', array( 68 70 'methods' => 'GET', … … 70 72 ) ); 71 73 74 register_rest_route( 'wc/v3', 'products-distance', array( 75 'methods' => 'GET', 76 'callback' => array( $this, 'get_items' ), 77 'permission_callback' => array( $products, 'get_items_permissions_check' ), 78 ) ); 79 80 } 81 82 /** 83 * 84 * Get products items 85 * 86 * @param $request 87 * 88 * @return array|WP_Error|WP_REST_Response 89 */ 90 public function get_items( $request ) { 91 global $wpdb; 92 93 $lat = $request->get_param( 'lat' ); 94 $lng = $request->get_param( 'lng' ); 95 96 $productsClass = new WC_REST_Products_Controller(); 97 $response = $productsClass->get_items( $request ); 98 99 if ( $lat && $lng ) { 100 101 $ids = array(); 102 foreach ( $response->data as $key => $value ) { 103 $ids[] = $value['id']; 104 } 105 106 // Get all locations 107 $table_name = $wpdb->prefix . 'gmw_locations'; 108 $query = "SELECT * FROM $table_name WHERE object_id IN (" . implode( ',', $ids ) . ")"; 109 $gmw_locations = $wpdb->get_results( $query, OBJECT ); 110 111 // Calculator the distance 112 $origins = []; 113 foreach ( $gmw_locations as $key => $value ) { 114 $origins[] = $value->latitude . ',' . $value->longitude; 115 } 116 117 $origin_string = implode( '|', $origins ); 118 $destinations_string = "$lat,$lng"; 119 $key = MBD_GOOGLE_API_KEY; 120 121 $distance_matrix = mobile_builder_distance_matrix( $origin_string, $destinations_string, $key ); 122 123 // map distance matrix to product 124 $data = []; 125 foreach ( $response->data as $key => $item ) { 126 $index = array_search( $item['id'], array_column($gmw_locations, 'object_id') ); 127 $item['distance_matrix'] = $distance_matrix[ $index ]; 128 $data[] = $item; 129 } 130 131 // $data[] = array( 132 // 'origin_string' => $origin_string, 133 // 'destinations_string' => $destinations_string, 134 // 'ids' => $ids, 135 // 'gmw_locations' => $gmw_locations 136 // ); 137 138 $response->data = $data; 139 } 140 141 return $response; 72 142 } 73 143 … … 152 222 * @since 1.0.0 153 223 */ 154 public function custom_change_product_response( $response ) { 224 public function custom_change_product_response( $response, $object, $request ) { 225 226 // echo $request->get_param('lng'); 227 // echo $request->get_param('lat'); die; 228 155 229 global $woocommerce_wpml; 156 230 -
mobile-builder/trunk/public/class-mobile-builder-public.php
r2303204 r2304807 733 733 ) ); 734 734 735 $gmw = get_option( 'gmw_options' ); 736 735 737 $templates = array(); 736 738 $templates_data = $admin->template_configs(); … … 754 756 'time_format' => get_option( 'time_format' ), 755 757 'configs' => maybe_unserialize( $configs ), 758 'default_location' => $gmw['post_types_settings'], 756 759 'templates' => $decode ? $templates : $templates_data, 757 760 );
Note: See TracChangeset
for help on using the changeset viewer.