Plugin Directory

Changeset 2304807


Ignore:
Timestamp:
05/14/2020 11:07:26 AM (6 years ago)
Author:
rnlab
Message:

update code

Location:
mobile-builder/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • mobile-builder/trunk/admin/class-mobile-builder-admin.php

    r2303204 r2304807  
    9999
    100100        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',
    104106            )
    105107        );
     
    176178        global $wpdb;
    177179        $table_name = $wpdb->prefix . MOBILE_BUILDER_TABLE_NAME;
     180
    178181        return $wpdb->get_results( "SELECT * FROM $table_name", OBJECT );
    179182    }
  • mobile-builder/trunk/api/class-mobile-builder-vendor.php

    r2303204 r2304807  
    153153
    154154            $vendor_stores[] = array_merge( $store, array(
     155                'id'                  => $key,
    155156                'gravatar'            => $gravatar_url,
    156157                'list_banner_url'     => $list_banner_url,
     
    160161                'total_review_rating' => $store_user->get_total_review_rating(),
    161162                '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                )
    162168            ) );
    163169        }
     
    238244
    239245    /**
     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    /**
    240295     * @param $args
    241296     * @param $wp_query
  • mobile-builder/trunk/helpers/mobile-builder-functions.php

    r2303204 r2304807  
    4545    return $result;
    4646}
     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 */
     59function 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  
    214214        $plugin_api = new Mobile_Builder_Vendor( $this->get_plugin_name(), $this->get_version() );
    215215        $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 );
    217217        $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 );
    218219
    219220    }
  • mobile-builder/trunk/product/class-mobile-builder-product.php

    r2303204 r2304807  
    6565        $namespace = $this->plugin_name . '/v' . intval( $this->version );
    6666
     67        $products = new WC_REST_Products_Controller();
     68
    6769        register_rest_route( $namespace, 'rating-count', array(
    6870            'methods'  => 'GET',
     
    7072        ) );
    7173
     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;
    72142    }
    73143
     
    152222     * @since    1.0.0
    153223     */
    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
    155229        global $woocommerce_wpml;
    156230
  • mobile-builder/trunk/public/class-mobile-builder-public.php

    r2303204 r2304807  
    733733            ) );
    734734
     735            $gmw = get_option( 'gmw_options' );
     736
    735737            $templates      = array();
    736738            $templates_data = $admin->template_configs();
     
    754756                'time_format'           => get_option( 'time_format' ),
    755757                'configs'               => maybe_unserialize( $configs ),
     758                'default_location'      => $gmw['post_types_settings'],
    756759                'templates'             => $decode ? $templates : $templates_data,
    757760            );
Note: See TracChangeset for help on using the changeset viewer.