/** * Handles the CSV import and saves the locations */ public function handle_csv_import() { /** * Set the max execution time of this script to 3600 seconds (1 hour) * @TODO: Devide the upload in batches of 10 locations per batch. */ ini_set('max_execution_time', 3600); $upload_dir = wp_upload_dir(); $wpseo_upload_dir = $upload_dir["basedir"] . '/wpseo/import/'; $options = get_option($this->option_name); $count = 0; $last_imported = 0; if (isset($_POST['csv-import']) && check_admin_referer('wpseo_local_import_nonce', 'wpseo_local_import_nonce_field')) { $csv_path = $wpseo_upload_dir . basename($_FILES['wpseo']['name']['csvuploadlocations']); if (!empty($_FILES['wpseo']) && !move_uploaded_file($_FILES['wpseo']['tmp_name']['csvuploadlocations'], $csv_path)) { echo '<p class="error">' . __('Sorry, there was an error while uploading the CSV file.<br>Please make sure the ' . $wpseo_upload_dir . ' directory is writable (chmod 777).', 'yoast-local-seo') . '</p>'; } else { $is_simplemap_import = !empty($_POST['is-simplemap-import']) && $_POST['is-simplemap-import'] == '1'; $separator = ","; if (!empty($_POST['csv_separator']) && $_POST['csv_separator'] == "semicolon" && false == $is_simplemap_import) { $separator = ";"; } // Get location data from CSV $column_names = array("name", "address", "city", "zipcode", "state", "country", "phone", "phone2nd", "description", "image", "category", "url", "business_type", "opening_hours_monday_from", "opening_hours_monday_to", "opening_hours_monday_second_from", "opening_hours_monday_second_to", "opening_hours_tuesday_from", "opening_hours_tuesday_to", "opening_hours_tuesday_second_from", "opening_hours_tuesday_second_to", "opening_hours_wednesday_from", "opening_hours_wednesday_to", "opening_hours_wednesday_second_from", "opening_hours_wednesday_second_to", "opening_hours_thursday_from", "opening_hours_thursday_to", "opening_hours_thursday_second_from", "opening_hours_thursday_second_to", "opening_hours_friday_from", "opening_hours_friday_to", "opening_hours_friday_second_from", "opening_hours_friday_second_to", "opening_hours_saturday_from", "opening_hours_saturday_to", "opening_hours_saturday_second_from", "opening_hours_saturday_second_to", "opening_hours_sunday_from", "opening_hours_sunday_to", "opening_hours_sunday_second_from", "opening_hours_sunday_second_to", "notes_1", "notes_2", "notes_3"); if ($is_simplemap_import) { $column_names = array("name", "address", "address2", "city", "state", "zipcode", "country", "phone", "email", "fax", "url", "description", "special", "lat", "long", "pubdate", "category", "tag"); } $handle = fopen($csv_path, "r"); $locations = array(); $row = 0; while (($csvdata = fgetcsv($handle, 1000, $separator)) !== FALSE) { if ($row > 0) { $tmp_location = array(); for ($i = 0; $i < count($column_names); $i++) { // Skip columns for simplemap import if ($is_simplemap_import && in_array($column_names[$i], array('address2', 'email', 'url', 'special', 'pubdate', 'tag'))) { continue; } if (isset($csvdata[$i])) { $tmp_location[$column_names[$i]] = addslashes($csvdata[$i]); } } array_push($locations, $tmp_location); } $row++; } fclose($handle); $debug = false; global $wpseo_local_core; $business_types = $wpseo_local_core->get_local_business_types(); array_walk($business_types, 'wpseo_local_sanitize_business_types'); // Create WordPress posts in custom post type $errors = array(); foreach ($locations as $location) { // Create standard post data $current_post['ID'] = ''; $current_post['post_title'] = isset($location["name"]) ? $location["name"] : ''; $current_post['post_content'] = isset($location["description"]) ? $location["description"] : ''; $current_post['post_status'] = "publish"; $current_post['post_date'] = date("Y-m-d H:i:s", time()); $current_post['post_type'] = 'wpseo_locations'; $post_id = wp_insert_post($current_post); if (!$debug) { if (empty($location['lat']) && empty($location['long'])) { $full_address = wpseo_local_get_address_format($location['address'], true, $location['zipcode'], $location['city'], $location['state'], true, false, false); if (!empty($location['country'])) { $full_address .= ', ' . WPSEO_Local_Frontend::get_country($location['country']); } $geo_data = wpseo_geocode_address($full_address); if (!is_wp_error($geo_data) && !empty($geo_data->results[0])) { $location['lat'] = $geo_data->results[0]->geometry->location->lat; $location['long'] = $geo_data->results[0]->geometry->location->lng; } else { $location['lat'] = ''; $location['long'] = ''; if ($geo_data->get_error_code() == 'wpseo-query-limit') { $errors[] = sprintf(__('The usage of the Google Maps API has exceeds their limits. Please consider entering an API key in the %soptions%s', 'yoast-local-seo'), '<a href="' . admin_url('admin.php?page=wpseo_local') . '">', '</a>'); if (!empty($last_imported)) { $errors[] = sprintf(__('The last successfully imported location is <a href="%s" title="%s">%s</a>', 'yoast-local-seo'), get_edit_post_link($last_imported), get_the_title($last_imported), get_the_title($last_imported)); } break; } else { $errors[] = sprintf(__('Location <em>' . esc_attr($location["name"]) . '</em> could not be geo-coded. %sEdit this location%s.', 'yoast-local-seo'), '<a href="' . admin_url('post.php?post=' . esc_attr($post_id) . '&action=edit') . '">', '</a>'); } } } // Insert custom fields for location details if (!empty($post_id)) { add_post_meta($post_id, "_wpseo_business_name", isset($location["name"]) ? $location["name"] : '', true); add_post_meta($post_id, '_wpseo_business_address', isset($location["address"]) ? $location["address"] : '', true); add_post_meta($post_id, '_wpseo_business_city', isset($location["city"]) ? $location["city"] : '', true); add_post_meta($post_id, '_wpseo_business_state', isset($location["state"]) ? $location["state"] : '', true); add_post_meta($post_id, '_wpseo_business_zipcode', isset($location["zipcode"]) ? $location["zipcode"] : '', true); add_post_meta($post_id, '_wpseo_business_country', isset($location["country"]) ? $location["country"] : '', true); add_post_meta($post_id, '_wpseo_business_phone', isset($location["phone"]) ? $location["phone"] : '', true); add_post_meta($post_id, '_wpseo_business_fax', isset($location["fax"]) ? $location["fax"] : '', true); if (isset($location["phone_2nd"])) { add_post_meta($post_id, '_wpseo_business_phone_2nd', $location["phone_2nd"], true); } if (isset($location["email"])) { add_post_meta($post_id, '_wpseo_business_email', $location["email"], true); } if (isset($location['category'])) { wp_set_object_terms($post_id, $location['category'], 'wpseo_locations_category'); } if (isset($location['business_type'])) { $business_type = $location['business_type']; if (false == in_array($business_type, array_keys($business_types))) { $business_type = array_search($business_type, $business_types); } add_post_meta($post_id, '_wpseo_business_type', $business_type, true); } if (isset($location['url'])) { add_post_meta($post_id, '_wpseo_business_url', $location['url'], true); } // Add notes for ($i = 0; $i < 3; $i++) { $n = $i + 1; if (!empty($location['notes_' . $n])) { add_post_meta($post_id, '_wpseo_business_notes_' . $n, $location['notes_' . $n]); } } add_post_meta($post_id, '_wpseo_coordinates_lat', $location["lat"], true); add_post_meta($post_id, '_wpseo_coordinates_long', $location["long"], true); $count++; $last_imported = $post_id; } // Add image as post thumbnail if (!empty($location['image'])) { $wpseo_local_core->insert_attachment($post_id, $location['image'], true); } // Opening hours foreach ($wpseo_local_core->days as $key => $day) { if (isset($location['opening_hours_' . $key . '_from']) && !empty($location['opening_hours_' . $key . '_from']) && isset($location['opening_hours_' . $key . '_to']) && !empty($location['opening_hours_' . $key . '_to'])) { if ('closed' == strtolower($location['opening_hours_' . $key . '_from']) || 'closed' == strtolower($location['opening_hours_' . $key . '_to'])) { add_post_meta($post_id, '_wpseo_opening_hours_' . $key . '_from', 'closed', true); } else { $time_from = strtotime($location['opening_hours_' . $key . '_from']); $time_to = strtotime($location['opening_hours_' . $key . '_to']); if (false !== $time_from && false !== $time_to) { add_post_meta($post_id, '_wpseo_opening_hours_' . $key . '_from', date('H:i', $time_from), true); add_post_meta($post_id, '_wpseo_opening_hours_' . $key . '_to', date('H:i', $time_to), true); } else { add_post_meta($post_id, '_wpseo_opening_hours_' . $key . '_from', 'closed', true); if (false === $time_from) { $errors[] = sprintf(__('%s is not a valid time notation', 'yoast-local-seo'), $location['opening_hours_' . $key . '_from']); } else { if (false === $time_to) { $errors[] = sprintf(__('%s is not a valid time notation', 'yoast-local-seo'), $location['opening_hours_' . $key . '_to']); } } } if (isset($location['opening_hours_' . $key . '_second_from']) && !empty($location['opening_hours_' . $key . '_second_from']) && isset($location['opening_hours_' . $key . '_second_to']) && !empty($location['opening_hours_' . $key . '_second_to'])) { $time_second_from = strtotime($location['opening_hours_' . $key . '_second_from']); $time_second_to = strtotime($location['opening_hours_' . $key . '_second_to']); if (false !== $time_second_from && false !== $time_second_to) { add_post_meta($post_id, '_wpseo_opening_hours_' . $key . '_second_from', date('H:i', $time_second_from), true); add_post_meta($post_id, '_wpseo_opening_hours_' . $key . '_second_to', date('H:i', $time_second_to), true); // Multiple openingtimes are set. Enable them in the backend. update_post_meta($post_id, '_wpseo_multiple_opening_hours', 'on', true); } else { add_post_meta($post_id, '_wpseo_opening_hours_' . $key . '_second_from', 'closed', true); if (false === $time_second_from) { $errors[] = sprintf(__('%s is not a valid time notation', 'yoast-local-seo'), $location['opening_hours_' . $key . '_second_from']); } else { if (false === $time_second_to) { $errors[] = sprintf(__('%s is not a valid time notation', 'yoast-local-seo'), $location['opening_hours_' . $key . '_second_to']); } } } } else { add_post_meta($post_id, '_wpseo_opening_hours_' . $key . '_second_from', 'closed', true); } } } else { add_post_meta($post_id, '_wpseo_opening_hours_' . $key . '_from', 'closed', true); } } } } $msg = ''; if ($count > 0) { $msg .= $count . ' locations found and successfully imported.<br/>'; } if (!empty($errors)) { $msg .= '<p>'; $msg .= '<strong>' . __('Some errors has occured', 'yoast-local-seo') . '</strong><br>'; foreach ($errors as $error) { $msg .= $error . '<br>'; } $msg .= '</p>'; } if ($msg != '') { echo '<div id="message" class="message updated" style="width:94%;"><p>' . $msg . '</p></div>'; } } } }
/** * Output opengraph location tags. * * @link https://developers.facebook.com/docs/reference/opengraph/object-type/business.business * @link https://developers.facebook.com/docs/reference/opengraph/object-type/restaurant.restaurant * * @since 1.0 */ function opengraph_location() { if (is_singular('wpseo_locations') || "on" == WPSEO_Meta::get_value('opengraph-local') && !wpseo_has_multiple_locations()) { $options = get_option('wpseo_local'); $hide_opening_hours = isset($options['hide_opening_hours']) && $options['hide_opening_hours'] == 'on'; $location_data = wpseo_get_location_details(get_the_ID()); echo '<meta property="place:location:latitude" content="' . $location_data['business_coords_lat'] . '"/>' . "\n"; echo '<meta property="place:location:longitude" content="' . $location_data['business_coords_long'] . '"/>' . "\n"; echo '<meta property="business:contact_data:street_address" content="' . $location_data['business_address'] . '"/>' . "\n"; echo '<meta property="business:contact_data:locality" content="' . $location_data['business_city'] . '"/>' . "\n"; echo '<meta property="business:contact_data:country" content="' . WPSEO_Local_Frontend::get_country($location_data['business_country']) . '"/>' . "\n"; echo '<meta property="business:contact_data:postal_code" content="' . $location_data['business_zipcode'] . '"/>' . "\n"; echo '<meta property="business:contact_data:street_address" content="' . $location_data['business_address'] . '"/>' . "\n"; echo '<meta property="business:contact_data:website" content="' . trailingslashit(wpseo_xml_sitemaps_base_url('')) . '"/>' . "\n"; if (!empty($location_data['business_state'])) { echo '<meta property="business:contact_data:region" content="' . $location_data['business_state'] . '"/>' . "\n"; } if (!empty($location_data['business_email'])) { echo '<meta property="business:contact_data:email" content="' . $location_data['business_email'] . '"/>' . "\n"; } if (!empty($location_data['business_phone'])) { echo '<meta property="business:contact_data:phone_number" content="' . $location_data['business_phone'] . '"/>' . "\n"; } if (!empty($location_data['business_fax'])) { echo '<meta property="business:contact_data:fax_number" content="' . $location_data['business_fax'] . '"/>' . "\n"; } // Opening Hours if (false == $hide_opening_hours) { $days = array('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'); foreach ($days as $day) { $field_name = '_wpseo_opening_hours_' . $day; $start = get_post_meta(get_the_ID(), $field_name . '_from', true); if (!$start || empty($start)) { continue; } $end = get_post_meta(get_the_ID(), $field_name . '_to', true); if ($start == 'closed') { $end = 'closed'; } echo '<meta property="business:hours:day" content="' . $day . '"/>' . "\n"; echo '<meta property="business:hours:start" content="' . $start . '"/>' . "\n"; echo '<meta property="business:hours:end" content="' . $end . '"/>' . "\n"; } } } }
public function get_results() { global $wpdb; $nr_results = !empty($this->options['sl_num_results']) ? $this->options['sl_num_results'] : 10; $metric = $this->options['unit_system'] == 'METRIC' ? 'km' : 'mi'; $radius = !empty($_REQUEST['wpseo-sl-radius']) ? $_REQUEST['wpseo-sl-radius'] : 99999; $sl_category_term = !empty($_REQUEST['wpseo-sl-category']) ? $_REQUEST['wpseo-sl-category'] : ''; $distances = array('in_radius' => 0, 'locations' => array()); $search_string = isset($_REQUEST['wpseo-sl-search']) ? esc_attr($_REQUEST['wpseo-sl-search']) : ''; if ($search_string == '') { return $distances; } // Add country name to search to improve search results $default_country = isset($this->options['default_country']) ? $this->options['default_country'] : ''; if ($default_country != '') { $search_string .= ' ' . WPSEO_Local_Frontend::get_country($default_country); } $response = wpseo_geocode_address($search_string); if (is_wp_error($response)) { return $response; } // Get lat/long if (empty($response->results[0])) { return new WP_Error('wpseo-get-results-error', __('No valid JSON response. We cannot complete the search.', 'yoast-local-seo')); } $result = $response->results[0]; $coordinates = !empty($result->geometry->location) ? $result->geometry->location : false; if (!$coordinates) { return new WP_Error('wpseo-get-results-error', __('No valid coordinates. We cannot complete the search.', 'yoast-local-seo')); } // Extend SQL with category filter $inner_join = ''; if ($sl_category_term != '') { $inner_join .= "\n\t\t\t\t\tINNER JOIN {$wpdb->term_relationships} AS term_rel ON p.ID = term_rel.object_id\n\t\t\t\t\tINNER JOIN {$wpdb->term_taxonomy} AS taxo ON term_rel.term_taxonomy_id = taxo.term_taxonomy_id \n\t\t\t\t\t\tAND taxo.taxonomy = 'wpseo_locations_category'\n\t\t\t\t\t\tAND taxo.term_id = {$sl_category_term}\n\t\t\t\t"; } // Get all coordinates from posts $sql = "SELECT p.ID, m1.meta_value as lat, m2.meta_value as lng \n\t\t\t\t\t\tFROM {$wpdb->posts} p \n\t\t\t\t\t\tINNER JOIN {$wpdb->postmeta} m1 ON p.ID = m1.post_id \n\t\t\t\t\t\tINNER JOIN {$wpdb->postmeta} m2 ON p.ID = m2.post_id\n\t\t\t\t\t\t{$inner_join}\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tp.post_type = 'wpseo_locations' AND\n\t\t\t\t\t\t \tp.post_status = 'publish' AND\n\t\t\t\t\t\t\tm1.meta_key = '_wpseo_coordinates_lat' AND\n\t\t\t\t\t\t\tm2.meta_key = '_wpseo_coordinates_long'\n\t\t\t\t\t\tGROUP BY p.ID"; $locations = $wpdb->get_results($sql); // Calculate distance $in_radius = array(); $out_of_radius = array(); if (0 == $wpdb->num_rows) { return $distances; } foreach ($locations as $location) { // Skip locations with empty lat/long coordinates if (empty($location->lat) || empty($location->lng)) { continue; } $distance = $this->get_distance($coordinates->lat, $coordinates->lng, $location->lat, $location->lng); $distance_key = round($distance[$metric], 4) * 10000; // Filter on radius if ($distance[$metric] > $radius) { $out_of_radius[$distance[$metric]] = $location->ID; } else { $in_radius[(string) $distance_key] = $location->ID; } } if (0 == count($in_radius)) { // No results were found inside the given radius ksort($out_of_radius, SORT_NUMERIC); $distances['locations'] = array_slice($out_of_radius, 0, 1, true); return $distances; } ksort($in_radius, SORT_NUMERIC); $in_radius = array_slice($in_radius, 0, $nr_results, true); $distances['in_radius'] = count($in_radius); $distances['locations'] = $in_radius; return $distances; }
/** * Maps shortcode handler * * @since 0.1 * * @param array $atts Array of shortcode parameters * * @return string */ function wpseo_local_show_map($atts) { global $map_counter, $wpseo_enqueue_geocoder, $wpseo_map; $options = get_option('wpseo_local'); $tax_query = array(); // Backwards compatibility for scrollable / zoomable functions if (is_array($atts) && !array_key_exists('zoomable', $atts)) { $atts['zoomable'] = isset($atts['scrollable']) ? $atts['scrollable'] : true; } $atts = wpseo_check_falses(shortcode_atts(array('id' => '', 'term_id' => '', 'center' => '', 'width' => 400, 'height' => 300, 'zoom' => -1, 'show_route' => true, 'show_state' => true, 'show_country' => false, 'show_url' => false, 'show_email' => false, 'map_style' => isset($options['map_view_style']) ? $options['map_view_style'] : 'ROADMAP', 'scrollable' => true, 'draggable' => true, 'show_route_label' => isset($options['show_route_label']) && !empty($options['show_route_label']) ? $options['show_route_label'] : __('Show route', 'yoast-local-seo'), 'from_sl' => false, 'echo' => false), $atts)); if (!isset($map_counter)) { $map_counter = 0; } else { $map_counter++; } $location_array = $lats = $longs = array(); $location_array_str = ''; $default_custom_marker = ''; if (isset($options['custom_marker']) && intval($options['custom_marker'])) { $default_custom_marker = wp_get_attachment_url($options['custom_marker']); } if (!wpseo_has_multiple_locations()) { $atts['id'] = ''; $location_array[] = array('location_name' => $options['location_name'], 'location_url' => wpseo_xml_sitemaps_base_url(''), 'location_email' => $options['location_email'], 'location_address' => $options['location_address'], 'location_city' => $options['location_city'], 'location_state' => $options['location_state'], 'location_zipcode' => $options['location_zipcode'], 'location_country' => $options['location_country'], 'location_phone' => $options['location_phone'], 'location_phone_2nd' => $options['location_phone_2nd'], 'coordinates_lat' => $options['location_coords_lat'], 'coordinates_long' => $options['location_coords_long'], 'custom_marker' => $default_custom_marker); } else { if (get_post_type() == 'wpseo_locations') { if (($atts['id'] == '' || $atts['id'] == 'current') && !is_post_type_archive()) { $atts['id'] = get_queried_object_id(); } if (is_post_type_archive() && ($atts['id'] == '' || $atts['id'] == 'current')) { return ''; } if ($atts['id'] == 'all' && $atts['term_id'] != '') { $tax_query[] = array('taxonomy' => 'wpseo_locations_category', 'field' => 'term_id', 'terms' => $atts['term_id']); } } else { if ($atts['id'] != 'all' && empty($atts['id'])) { return is_singular('wpseo_locations') ? __('Please provide a post ID when using this shortcode outside a Locations singular page', 'yoast-local-seo') : ''; } } $location_ids = explode(',', $atts['id']); if ($atts['id'] == 'all' || $atts['id'] != 'all' && count($location_ids) > 1) { $args = array('post_type' => 'wpseo_locations', 'posts_per_page' => $atts['id'] == 'all' ? -1 : count($location_ids), 'fields' => 'ids', 'meta_query' => array(array('key' => '_wpseo_business_address', 'value' => '', 'compare' => '!=')), 'tax_query' => $tax_query); if (count($location_ids) > 1) { $args['post__in'] = $location_ids; } $location_ids = get_posts($args); } foreach ($location_ids as $location_id) { $custom_marker = wpseo_local_get_custom_marker($location_id, 'wpseo_locations_category'); $tmp_array = array('location_name' => get_the_title($location_id), 'location_url' => get_post_meta($location_id, '_wpseo_business_url', true), 'location_email' => get_post_meta($location_id, '_wpseo_business_email', true), 'location_address' => get_post_meta($location_id, '_wpseo_business_address', true), 'location_city' => get_post_meta($location_id, '_wpseo_business_city', true), 'location_state' => get_post_meta($location_id, '_wpseo_business_state', true), 'location_zipcode' => get_post_meta($location_id, '_wpseo_business_zipcode', true), 'location_country' => get_post_meta($location_id, '_wpseo_business_country', true), 'location_phone' => get_post_meta($location_id, '_wpseo_business_phone', true), 'location_phone_2nd' => get_post_meta($location_id, '_wpseo_business_phone_2nd', true), 'coordinates_lat' => get_post_meta($location_id, '_wpseo_coordinates_lat', true), 'coordinates_long' => get_post_meta($location_id, '_wpseo_coordinates_long', true), 'custom_marker' => $custom_marker != '' ? $custom_marker : $default_custom_marker); if (empty($tmp_array['location_url'])) { $tmp_array['location_url'] = get_permalink($location_id); } $location_array[] = $tmp_array; } } $noscript_output = '<ul>'; foreach ($location_array as $key => $location) { if ($location['coordinates_lat'] != '' && $location['coordinates_long'] != '') { $location_array_str .= "location_data.push( {\n\t\t\t\t'name': '" . wpseo_cleanup_string($location["location_name"]) . "',\n\t\t\t\t'url': '" . wpseo_cleanup_string($location["location_url"]) . "',\n\t\t\t\t'zip_city': '" . wpseo_local_get_address_format(wpseo_cleanup_string($location["location_address"]), false, $location["location_zipcode"], $location["location_city"], $location["location_state"], $atts['show_state'], true) . "',\n\t\t\t\t'country': '" . WPSEO_Local_Frontend::get_country($location['location_country']) . "',\n\t\t\t\t'show_country': " . ($atts['show_country'] ? 'true' : 'false') . ",\n\t\t\t\t'url': '" . esc_url($location['location_url']) . "',\n\t\t\t\t'show_url': " . ($atts['show_url'] ? 'true' : 'false') . ",\n\t\t\t\t'email': '" . $location['location_email'] . "',\n\t\t\t\t'show_email': " . ($atts['show_email'] ? 'true' : 'false') . ",\n\t\t\t\t'phone': '" . wpseo_cleanup_string($location['location_phone']) . "',\n\t\t\t\t'phone_2nd': '" . wpseo_cleanup_string($location['location_phone_2nd']) . "',\n\t\t\t\t'lat': " . wpseo_cleanup_string($location['coordinates_lat']) . ",\n\t\t\t\t'long': " . wpseo_cleanup_string($location['coordinates_long']) . ",\n\t\t\t\t'custom_marker': '" . wpseo_cleanup_string($location['custom_marker']) . "'\n\t\t\t} );\n"; } $noscript_output .= '<li><a href="' . $location['location_url'] . '">' . $location['location_name'] . '</a></li>'; $noscript_output .= '<li><a href="mailto:' . $location['location_email'] . '">' . $location['location_email'] . '</a></li>'; $full_address = $location['location_address'] . ', ' . $location['location_city'] . (strtolower($location['location_country']) == 'us' ? ', ' . $location['location_state'] : '') . ', ' . $location['location_zipcode'] . ', ' . WPSEO_Local_Frontend::get_country($location['location_country']); $location_array[$key]['full_address'] = $full_address; $lats[] = $location['coordinates_lat']; $longs[] = $location['coordinates_long']; } $noscript_output .= '</ul>'; $map = ''; $wpseo_enqueue_geocoder = true; if (!is_array($lats) || empty($lats) || !is_array($longs) || empty($longs)) { return; } if ($atts['center'] === '') { $center_lat = min($lats) + (max($lats) - min($lats)) / 2; $center_long = min($longs) + (max($longs) - min($longs)) / 2; } else { $center_lat = get_post_meta($atts['center'], '_wpseo_coordinates_lat', true); $center_long = get_post_meta($atts['center'], '_wpseo_coordinates_long', true); } // Default to zoom 10 if there's only one location as a center + bounds would zoom in far too much. if (-1 == $atts['zoom'] && 1 === count($location_array)) { $atts['zoom'] = 10; } if ($location_array_str != '') { $wpseo_map .= '<script type="text/javascript"> var map_' . $map_counter . '; var directionsDisplay_' . $map_counter . '; function wpseo_map_init' . ($map_counter != 0 ? '_' . $map_counter : '') . '() { var location_data = new Array();' . PHP_EOL . $location_array_str . ' directionsDisplay_' . $map_counter . ' = wpseo_show_map( location_data, ' . $map_counter . ', directionsDisplay_' . $map_counter . ', ' . $center_lat . ', ' . $center_long . ', ' . $atts['zoom'] . ', "' . $atts['map_style'] . '", "' . $atts['show_route'] . '", "' . $atts['scrollable'] . '", "' . $atts['draggable'] . '" ); } if( window.addEventListener ) window.addEventListener( "load", wpseo_map_init' . ($map_counter != 0 ? '_' . $map_counter : '') . ', false ); else if(window.attachEvent ) window.attachEvent( "onload", wpseo_map_init' . ($map_counter != 0 ? '_' . $map_counter : '') . '); </script>' . PHP_EOL; // Override(reset) the setting for images inside the map $map .= '<div id="map_canvas' . ($map_counter != 0 ? '_' . $map_counter : '') . '" class="wpseo-map-canvas" style="max-width: 100%; width: ' . $atts['width'] . 'px; height: ' . $atts['height'] . 'px;">' . $noscript_output . '</div>'; $route_tag = apply_filters('wpseo_local_location_route_title_name', 'h3'); if ($atts['show_route'] && ($atts['id'] != 'all' && strpos($atts['id'], ',') === false || $atts['from_sl'])) { $map .= '<div id="wpseo-directions-wrapper"' . ($atts['from_sl'] ? ' style="display: none;"' : '') . '>'; $map .= '<' . esc_html($route_tag) . ' id="wpseo-directions" class="wpseo-directions-heading">' . __('Route', 'yoast-local-seo') . '</' . esc_html($route_tag) . '>'; $map .= '<form action="" method="post" class="wpseo-directions-form" id="wpseo-directions-form' . ($map_counter != 0 ? '_' . $map_counter : '') . '" onsubmit="wpseo_calculate_route( directionsDisplay_' . $map_counter . ', ' . $location_array[0]['coordinates_lat'] . ', ' . $location_array[0]['coordinates_long'] . ', ' . $map_counter . '); return false;">'; $map .= '<p>'; $map .= __('Your location', 'yoast-local-seo') . ': <input type="text" size="20" id="origin' . ($map_counter != 0 ? '_' . $map_counter : '') . '" value="' . (!empty($_REQUEST['wpseo-sl-search']) ? esc_attr($_REQUEST['wpseo-sl-search']) : '') . '" />'; $map .= '<input type="submit" class="wpseo-directions-submit" value="' . $atts['show_route_label'] . '">'; $map .= '<span id="wpseo-noroute" style="display: none;">' . __('No route could be calculated.', 'yoast-local-seo') . '</span>'; $map .= '</p>'; $map .= '</form>'; $map .= '<div id="directions' . ($map_counter != 0 ? '_' . $map_counter : '') . '"></div>'; $map .= '</div>'; } } if ($atts['echo']) { echo $map; } return $map; }
/** * Retrieves the lat/long coordinates from the Google Maps API * * @param Array $location_info Array with location info. Array structure: array( _wpseo_business_address, _wpseo_business_city, _wpseo_business_state, _wpseo_business_zipcode, _wpseo_business_country ) * @param bool $force_update Whether to force the update or not * @param int $post_id * * @return bool|array Returns coordinates in array ( Format: array( 'lat', 'long' ) ). False when call the Maps API did not succeed */ public function get_geo_data($location_info, $force_update = false, $post_id = 0) { $full_address = wpseo_local_get_address_format($location_info['_wpseo_business_address'], false, $location_info['_wpseo_business_zipcode'], $location_info['_wpseo_business_city'], $location_info['_wpseo_business_state'], true, false, false) . ', ' . WPSEO_Local_Frontend::get_country($location_info['_wpseo_business_country']); $coordinates = array(); if (($post_id === 0 || empty($post_id)) && isset($location_info['_wpseo_post_id'])) { $post_id = $location_info['_wpseo_post_id']; } if ($force_update || empty($location_info['_wpseo_coords']['lat']) || empty($location_info['_wpseo_coords']['long'])) { $results = wpseo_geocode_address($full_address); if (is_wp_error($results)) { return false; } if (isset($results->results[0]) && !empty($results->results[0])) { $coordinates['lat'] = $results->results[0]->geometry->location->lat; $coordinates['long'] = $results->results[0]->geometry->location->lng; if (wpseo_has_multiple_locations() && $post_id !== 0) { update_post_meta($post_id, '_wpseo_coordinates_lat', $coordinates['lat']); update_post_meta($post_id, '_wpseo_coordinates_long', $coordinates['long']); } else { $options = get_option('wpseo_local'); $options['location_coords_lat'] = $coordinates['lat']; $options['location_coords_long'] = $coordinates['long']; update_option('wpseo_local', $options); } } } else { $coordinates['lat'] = $location_info['_wpseo_coords']['lat']; $coordinates['long'] = $location_info['_wpseo_coords']['long']; } $return_array['coords'] = $coordinates; $return_array["full_address"] = $full_address; return $return_array; }