/** * Make the API call to Google to geocode the address. * * @since 1.0.0 * @param integer $post_id store post ID * @param array $store_data The store data * @return void|array $geo_data The geocoded response */ public function get_latlng($post_id, $store_data) { $address = $store_data['address'] . ',' . $store_data['city'] . ',' . $store_data['zip'] . ',' . $store_data['country']; $url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address) . '&sensor=false' . wpsl_get_gmap_api_params(); $response = wp_remote_get($url); if (is_wp_error($response) || wp_remote_retrieve_response_code($response) != 200 || empty($response['body'])) { $msg = __('The Google Geocoding API failed to return valid data, please try again later.', 'wpsl'); $this->geocode_failed($msg, $post_id); } else { $geo_data = json_decode($response['body'], true); return $geo_data; } }
/** * Load the required JS scripts. * * @since 1.0.0 * @return void */ public function add_frontend_scripts() { global $wpsl_settings, $wpsl; // Only load the required js files on the store locator page or individual store pages. if (empty($this->load_scripts)) { return; } $min = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; $dropdown_defaults = $this->get_dropdown_defaults(); wp_enqueue_script('wpsl-gmap', 'https://maps.google.com/maps/api/js' . wpsl_get_gmap_api_params(), '', null, true); $base_settings = array('storeMarker' => $this->create_retina_filename($wpsl_settings['store_marker']), 'mapType' => $wpsl_settings['map_type'], 'mapTypeControl' => $wpsl_settings['type_control'], 'zoomLevel' => $wpsl_settings['zoom_level'], 'zoomLatlng' => $wpsl_settings['zoom_latlng'], 'autoZoomLevel' => $wpsl_settings['auto_zoom_level'], 'scrollWheel' => $wpsl_settings['scrollwheel'], 'controlPosition' => $wpsl_settings['control_position'], 'path' => WPSL_URL); $locator_map_settings = array('startMarker' => $this->create_retina_filename($wpsl_settings['start_marker']), 'markerClusters' => $wpsl_settings['marker_clusters'], 'streetView' => $wpsl_settings['streetview'], 'autoLocate' => $wpsl_settings['auto_locate'], 'autoLoad' => $wpsl_settings['autoload'], 'markerEffect' => $wpsl_settings['marker_effect'], 'markerStreetView' => $wpsl_settings['marker_streetview'], 'markerZoomTo' => $wpsl_settings['marker_zoom_to'], 'newWindow' => $wpsl_settings['new_window'], 'resetMap' => $wpsl_settings['reset_map'], 'directionRedirect' => $wpsl_settings['direction_redirect'], 'phoneUrl' => $wpsl_settings['phone_url'], 'storeUrl' => $wpsl_settings['store_url'], 'moreInfoLocation' => $wpsl_settings['more_info_location'], 'mouseFocus' => $wpsl_settings['mouse_focus'], 'templateId' => $wpsl_settings['template_id'], 'maxResults' => $dropdown_defaults['max_results'], 'searchRadius' => $dropdown_defaults['search_radius'], 'distanceUnit' => $wpsl_settings['distance_unit'], 'geoLocationTimout' => apply_filters('wpsl_geolocation_timeout', 5000), 'ajaxurl' => admin_url('admin-ajax.php'), 'mapControls' => $this->get_map_controls(), 'mapTabAnchor' => apply_filters('wpsl_map_tab_anchor', 'wpsl-map-tab')); // If the marker clusters are enabled, include the js file and marker settings. if ($wpsl_settings['marker_clusters']) { wp_enqueue_script('wpsl-cluster', WPSL_URL . 'js/markerclusterer' . $min . '.js', '', WPSL_VERSION_NUM, true); //not minified version is in the /js folder $base_settings['clusterZoom'] = $wpsl_settings['cluster_zoom']; $base_settings['clusterSize'] = $wpsl_settings['cluster_size']; } // Check if we need to include the infobox script and settings. if ($wpsl_settings['infowindow_style'] == 'infobox') { wp_enqueue_script('wpsl-infobox', WPSL_URL . 'js/infobox' . $min . '.js', array('wpsl-gmap'), WPSL_VERSION_NUM, true); // Not minified version is in the /js folder $base_settings['infoWindowStyle'] = $wpsl_settings['infowindow_style']; $base_settings = $this->get_infobox_settings($base_settings); } // Include the map style. if (!empty($wpsl_settings['map_style'])) { $base_settings['mapStyle'] = strip_tags(stripslashes(json_decode($wpsl_settings['map_style']))); } wp_enqueue_script('wpsl-js', WPSL_URL . 'js/wpsl-gmap' . $min . '.js', array('jquery'), WPSL_VERSION_NUM, true); wp_enqueue_script('underscore'); // Check if we need to include all the settings and labels or just a part of them. if (in_array('wpsl_store_locator', $this->load_scripts)) { $settings = wp_parse_args($base_settings, $locator_map_settings); $template = 'wpsl_store_locator'; $labels = array('preloader' => $wpsl->i18n->get_translation('preloader_label', __('Searching...', 'wpsl')), 'noResults' => $wpsl->i18n->get_translation('no_results_label', __('No results found', 'wpsl')), 'moreInfo' => $wpsl->i18n->get_translation('more_label', __('More info', 'wpsl')), 'generalError' => $wpsl->i18n->get_translation('error_label', __('Something went wrong, please try again!', 'wpsl')), 'queryLimit' => $wpsl->i18n->get_translation('limit_label', __('API usage limit reached', 'wpsl')), 'directions' => $wpsl->i18n->get_translation('directions_label', __('Directions', 'wpsl')), 'noDirectionsFound' => $wpsl->i18n->get_translation('no_directions_label', __('No route could be found between the origin and destination', 'wpsl')), 'startPoint' => $wpsl->i18n->get_translation('start_label', __('Start location', 'wpsl')), 'back' => $wpsl->i18n->get_translation('back_label', __('Back', 'wpsl')), 'streetView' => $wpsl->i18n->get_translation('street_view_label', __('Street view', 'wpsl')), 'zoomHere' => $wpsl->i18n->get_translation('zoom_here_label', __('Zoom here', 'wpsl'))); wp_localize_script('wpsl-js', 'wpslLabels', $labels); wp_localize_script('wpsl-js', 'wpslGeolocationErrors', $this->geolocation_errors()); } else { $template = ''; $settings = $base_settings; } wp_localize_script('wpsl-js', 'wpslSettings', $settings); wpsl_create_underscore_templates($template); if (!empty($this->store_map_data)) { $i = 0; foreach ($this->store_map_data as $map) { wp_localize_script('wpsl-js', 'wpslMap_' . $i, $map); $i++; } } }
/** * Make the API call to Google to geocode the address. * * @since 1.0.0 * @param array $store_data The store data * @return array|string $geo_response The response from the Google Geocode API, or the wp_remote_get error message. */ public function get_latlng($store_data) { $address = $this->create_geocode_address($store_data); $url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address) . wpsl_get_gmap_api_params(true); $response = wp_remote_get($url); if (is_wp_error($response)) { $geo_response = sprintf(__('Something went wrong connecting to the Google Geocode API: %s %s Please try again later.', 'wpsl'), $response->get_error_message(), '<br><br>'); } else { $geo_response = json_decode($response['body'], true); } return $geo_response; }