Example #1
0
 /**
  * Builds the metabox for editing screen of the wpseo_locations Custom Post Type
  */
 function metabox_locations()
 {
     /** @var WPSEO_Local_Core */
     global $wpseo_local_core;
     $post_id = get_the_ID();
     echo '<div style="overflow: hidden;" id="wpseo-local-metabox">';
     // Noncename needed to verify where the data originated
     echo '<input type="hidden" name="locationsmeta_noncename" id="locationsmeta_noncename" value="' . wp_create_nonce(plugin_basename(__FILE__)) . '" />';
     // Copy from other locations field
     $locations = get_posts(array('post_type' => 'wpseo_locations', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'fields' => 'ids', 'exclude' => $post_id));
     if (count($locations) > 0) {
         echo '<p>';
         echo '<label class="textinput">' . __('Copy data from another location', 'yoast-local-seo') . ':</label>';
         echo '<select class="chzn-select" name="_wpseo_copy_from_location" id="wpseo_copy_from_location" style="width: 400px;" data-placeholder="' . __('Choose your location', 'yoast-local-seo') . '">';
         echo '<option value=""></option>';
         foreach ($locations as $location_id) {
             echo '<option value="' . $location_id . '">' . get_the_title($location_id) . '</option>';
         }
         echo '</select>';
         echo '</p>';
         echo '<p style="clear:both; margin-left: 150px;"><em><strong>' . __('Note', 'yoast-local-seo') . ':</strong> ' . __('selecting a location will overwrite all data below. If you accidently selected a location, just refresh the page and make sure you don\'t save it.', 'yoast-local-seo') . '</em></p><br>';
         wp_reset_postdata();
     }
     // Get the location data if its already been entered
     $business_type = get_post_meta($post_id, '_wpseo_business_type', true);
     $business_address = get_post_meta($post_id, '_wpseo_business_address', true);
     $business_city = get_post_meta($post_id, '_wpseo_business_city', true);
     $business_state = get_post_meta($post_id, '_wpseo_business_state', true);
     $business_zipcode = get_post_meta($post_id, '_wpseo_business_zipcode', true);
     $business_country = get_post_meta($post_id, '_wpseo_business_country', true);
     $business_phone = get_post_meta($post_id, '_wpseo_business_phone', true);
     $business_phone_2nd = get_post_meta($post_id, '_wpseo_business_phone_2nd', true);
     $business_fax = get_post_meta($post_id, '_wpseo_business_fax', true);
     $business_email = get_post_meta($post_id, '_wpseo_business_email', true);
     $notes_1 = get_post_meta($post_id, '_wpseo_business_notes_1', true);
     $notes_2 = get_post_meta($post_id, '_wpseo_business_notes_2', true);
     $notes_3 = get_post_meta($post_id, '_wpseo_business_notes_3', true);
     $business_url = get_post_meta($post_id, '_wpseo_business_url', true);
     $business_vat_id = get_post_meta($post_id, '_wpseo_business_vat_id', true);
     $business_tax_id = get_post_meta($post_id, '_wpseo_business_tax_id', true);
     $business_coc_id = get_post_meta($post_id, '_wpseo_business_coc_id', true);
     $coordinates_lat = get_post_meta($post_id, '_wpseo_coordinates_lat', true);
     $coordinates_long = get_post_meta($post_id, '_wpseo_coordinates_long', true);
     $is_postal_address = get_post_meta($post_id, '_wpseo_is_postal_address', true);
     $multiple_opening_hours = get_post_meta($post_id, '_wpseo_multiple_opening_hours', true);
     $multiple_opening_hours = $multiple_opening_hours == 'on';
     // Echo out the field
     echo '<p><label class="textinput" for="wpseo_business_type">Business type:</label>';
     echo '<select class="chzn-select" name="_wpseo_business_type" id="wpseo_business_type" style="width: 200px;" data-placeholder="' . __('Choose your business type', 'yoast-local-seo') . '">';
     echo '<option></option>';
     foreach ($wpseo_local_core->get_local_business_types() as $bt_option => $bt_label) {
         echo '<option ' . selected($business_type, $bt_option, false) . ' value="' . $bt_option . '">' . $bt_label . '</option>';
     }
     echo '</select></p>';
     echo '<p class="desc label">' . sprintf(__('If your business type is not listed, please read %sthe FAQ entry%s.', 'yoast-local-seo'), '<a href="http://kb.yoast.com/article/49-my-business-is-not-listed-can-you-add-it" target="_blank">', '</a>') . '</p><br class="clear">';
     echo '<p><label class="textinput" for="wpseo_business_address">' . __('Business address:', 'yoast-local-seo') . '</label>';
     echo '<input type="text" name="_wpseo_business_address" id="wpseo_business_address" value="' . $business_address . '" /></p>';
     echo '<p><label class="textinput" for="wpseo_business_city">' . __('Business city', 'yoast-local-seo') . ':</label>';
     echo '<input type="text" name="_wpseo_business_city" id="wpseo_business_city" value="' . $business_city . '" /></p>';
     echo '<p><label class="textinput" for="wpseo_business_state">' . __('Business state', 'yoast-local-seo') . ':</label>';
     echo '<input type="text" name="_wpseo_business_state" id="wpseo_business_state" value="' . $business_state . '" /></p>';
     echo '<p><label class="textinput" for="wpseo_business_zipcode">' . __('Business zipcode', 'yoast-local-seo') . ':</label>';
     echo '<input type="text" name="_wpseo_business_zipcode" id="wpseo_business_zipcode" value="' . $business_zipcode . '" /></p>';
     echo '<p><label class="textinput" for="wpseo_business_country">' . __('Business country', 'yoast-local-seo') . ':</label>';
     echo '<select class="chzn-select" name="_wpseo_business_country" id="wpseo_business_country" style="width: 200px; margin-top: 8px;" data-placeholder="' . __('Choose your country', 'yoast-local-seo') . '">';
     echo '<option></option>';
     $countries = WPSEO_Local_Frontend::get_country_array();
     foreach ($countries as $key => $val) {
         echo '<option value="' . $key . '"' . ($business_country == $key ? ' selected="selected"' : '') . '>' . $countries[$key] . '</option>';
     }
     echo '</select></p>';
     echo '<p><label class="textinput" for="wpseo_business_phone">' . __('Main phone number', 'yoast-local-seo') . ':</label>';
     echo '<input type="text" name="_wpseo_business_phone" id="wpseo_business_phone" value="' . $business_phone . '" /></p>';
     echo '<p><label class="textinput" for="wpseo_business_phone_2nd">' . __('Second phone number', 'yoast-local-seo') . ':</label>';
     echo '<input type="text" name="_wpseo_business_phone_2nd" id="wpseo_business_phone_2nd" value="' . $business_phone_2nd . '" /></p>';
     echo '<p><label class="textinput" for="wpseo_business_fax">' . __('Fax number', 'yoast-local-seo') . ':</label>';
     echo '<input type="text" name="_wpseo_business_fax" id="wpseo_business_fax" value="' . $business_fax . '" /></p>';
     echo '<p><label class="textinput" for="wpseo_business_email">' . __('Email address', 'yoast-local-seo') . ':</label>';
     echo '<input type="text" name="_wpseo_business_email" id="wpseo_business_email" value="' . $business_email . '" /></p>';
     echo '<p><label class="textinput" for="wpseo_business_url">' . __('URL', 'yoast-local-seo') . ':</label>';
     echo '<input type="text" name="_wpseo_business_url" id="wpseo_business_url" value="' . $business_url . '" /></p>';
     echo '<p><label class="textinput" for="wpseo_business_vat_id">' . __('VAT ID', 'yoast-local-seo') . ':</label>';
     echo '<input type="text" name="_wpseo_business_vat_id" id="wpseo_business_vat_id" value="' . $business_vat_id . '" /></p>';
     echo '<p><label class="textinput" for="wpseo_business_tax_id">' . __('Tax ID', 'yoast-local-seo') . ':</label>';
     echo '<input type="text" name="_wpseo_business_tax_id" id="wpseo_business_tax_id" value="' . $business_tax_id . '" /></p>';
     echo '<p><label class="textinput" for="wpseo_business_coc_id">' . __('Chamber of Commerce ID', 'yoast-local-seo') . ':</label>';
     echo '<input type="text" name="_wpseo_business_coc_id" id="wpseo_business_coc_id" value="' . $business_coc_id . '" /></p>';
     echo '<p><label class="textinput" for="wpseo_custom_notes_1">' . __('Notes', 'yoast-local-seo') . ':</label>';
     echo '<textarea name="_wpseo_business_notes_1" id="wpseo_business_notes_1" >' . $notes_1 . '</textarea></p>';
     echo '<p><label class="textinput" for="wpseo_business_notes_2">' . __('Notes', 'yoast-local-seo') . ':</label>';
     echo '<textarea name="_wpseo_business_notes_2" id="wpseo_business_notes_2" >' . $notes_2 . '</textarea></p>';
     echo '<p><label class="textinput" for="wpseo_business_notes_3">' . __('Notes', 'yoast-local-seo') . ':</label>';
     echo '<textarea name="_wpseo_business_notes_3" id="wpseo_business_notes_3" >' . $notes_3 . '</textarea></p>';
     echo '<p>' . __('You can enter the lat/long coordinates yourself. If you leave them empty they will be calculated automatically. If you want to re-calculate these fields, please make them blank before saving this location.', 'yoast-local-seo') . '</p>';
     echo '<p><label class="textinput" for="wpseo_coordinates_lat">' . __('Latitude', 'yoast-local-seo') . ':</label>';
     echo '<input type="text" name="_wpseo_coordinates_lat" id="wpseo_coordinates_lat" value="' . $coordinates_lat . '" /></p>';
     echo '<p><label class="textinput" for="wpseo_coordinates_long">' . __('Longitude', 'yoast-local-seo') . ':</label>';
     echo '<input type="text" name="_wpseo_coordinates_long" id="wpseo_coordinates_long" value="' . $coordinates_long . '" /></p>';
     echo '<p>' . __('If the marker is not in the right location for your store, you can drag the pin to the location where you want it.', 'yoast-local-seo') . '</p>';
     wpseo_local_show_map(array('id' => $post_id, 'echo' => true, 'show_route' => false, 'map_style' => 'roadmap', 'draggable' => true));
     echo '<p>';
     echo '<label class="textinput" for="wpseo_is_postal_address">' . __('This address is a postal address (not a physical location)', 'yoast-local-seo') . ':</label>';
     echo '<input type="checkbox" class="checkbox" name="_wpseo_is_postal_address" id="wpseo_is_postal_address" value="1" ' . checked($is_postal_address, 1, false) . ' />';
     echo '</p>';
     $hide_opening_hours = isset($this->options['hide_opening_hours']) && $this->options['hide_opening_hours'] == 'on';
     // Opening hours
     echo '<br class="clear">';
     echo '<div id="hide-opening-hours" style="display: ' . ($hide_opening_hours ? 'none' : 'block') . ';">';
     echo '<h4>' . __('Opening hours', 'yoast-local-seo') . '</h4>';
     echo '<div id="opening-hours-multiple">';
     echo '<label for="wpseo_multiple_opening_hours" class="textinput">' . __('I have two sets of opening hours per day', 'yoast-local-seo') . ':</label>';
     echo '<input class="checkbox" id="wpseo_multiple_opening_hours" type="checkbox" name="_wpseo_multiple_opening_hours" value="on" ' . checked(true, $multiple_opening_hours, false) . '> ';
     echo '</div>';
     echo '<br class="clear">';
     foreach ($wpseo_local_core->days as $key => $day) {
         $field_name = '_wpseo_opening_hours_' . $key;
         $value_from = get_post_meta($post_id, $field_name . '_from', true);
         if (!$value_from) {
             $value_from = '09:00';
         }
         $value_to = get_post_meta($post_id, $field_name . '_to', true);
         if (!$value_to) {
             $value_to = '17:00';
         }
         $value_second_from = get_post_meta($post_id, $field_name . '_second_from', true);
         if (!$value_second_from) {
             $value_second_from = '09:00';
         }
         $value_second_to = get_post_meta($post_id, $field_name . '_second_to', true);
         if (!$value_second_to) {
             $value_second_to = '17:00';
         }
         echo '<div class="clear opening-hours">';
         if (!isset($this->options['opening_hours_24h'])) {
             $this->options['opening_hours_24h'] = false;
         }
         echo '<label class="textinput">' . $day . ':</label>';
         echo '<select class="openinghours_from" style="width: 100px;" id="' . $field_name . '_from" name="' . $field_name . '_from">';
         echo wpseo_show_hour_options($this->options['opening_hours_24h'], $value_from);
         echo '</select><span id="' . $field_name . '_to_wrapper"> - ';
         echo '<select class="openinghours_to" style="width: 100px;" id="' . $field_name . '_to" name="' . $field_name . '_to">';
         echo wpseo_show_hour_options($this->options['opening_hours_24h'], $value_to);
         echo '</select></span>';
         echo '<div class="clear opening-hour-second ' . (!$multiple_opening_hours ? 'hidden' : '') . '">';
         echo '<div id="' . $field_name . '_second">';
         echo '<label class="textinput">&nbsp;</label>';
         echo '<select class="openinghours_from_second" style="width: 100px;" id="' . $field_name . '_second_from" name="' . $field_name . '_second_from">';
         echo wpseo_show_hour_options($this->options['opening_hours_24h'], $value_second_from);
         echo '</select><span id="' . $field_name . '_second_to_wrapper"> - ';
         echo '<select class="openinghours_to_second" style="width: 100px;" id="' . $field_name . '_second_to" name="' . $field_name . '_second_to">';
         echo wpseo_show_hour_options($this->options['opening_hours_24h'], $value_second_to);
         echo '</select></span>';
         echo '</div>';
         echo '</div>';
         echo '</div>';
     }
     echo '</div><!-- #hide-opening-hours -->';
     echo '<br class="clear" />';
     echo '</div>';
 }
Example #2
0
    echo '</select>';
    echo '</div>';
    echo '</div>';
}
echo '</div><!-- #show-opening-hours -->';
echo '</div><!-- #hide-opening-hours -->';
echo '<h2>' . __('Store locator settings', 'yoast-local-seo') . '</h2>';
WPSEO_Local_Admin_Wrappers::textinput('sl_num_results', __('Number of results', 'yoast-local-seo'));
echo '<h2>' . __('Advanced settings', 'yoast-local-seo') . '</h2>';
WPSEO_Local_Admin_Wrappers::textinput('api_key', __('Google Maps API key<br>(not required)', 'yoast-local-seo'));
echo '<p class="desc label" style="border:none; margin-bottom: 0;">' . sprintf(__('When you have a lot of locations, you could hit the limits of the Geocoding API. In that case you can enter your Google Maps API key to prevent that. To obtain an API key, please read more about it at the %sGoogle Maps documentation%s (make sure the Geocoding API is enabled in the Services section).', 'yoast-local-seo'), '<a href="https://developers.google.com/maps/documentation/javascript/tutorial#api_key">', '</a>') . '</p>';
WPSEO_Local_Admin_Wrappers::select('unit_system', __('Unit System', 'yoast-local-seo'), array('METRIC' => __('Metric', 'yoast-local-seo'), 'IMPERIAL' => __('Imperial', 'yoast-local-seo')));
WPSEO_Local_Admin_Wrappers::select('map_view_style', __('Default map style', 'yoast-local-seo'), array('HYBRID' => __('Hybrid', 'yoast-local-seo'), 'SATELLITE' => __('Satellite', 'yoast-local-seo'), 'ROADMAP' => __('Roadmap', 'yoast-local-seo'), 'TERRAIN' => __('Terrain', 'yoast-local-seo')));
WPSEO_Local_Admin_Wrappers::select('address_format', __('Address format', 'yoast-local-seo'), array('address-state-postal' => '{address} {city}, {state} {zipcode} &nbsp;&nbsp;&nbsp;&nbsp; (New York, NY 12345 )', 'address-state-postal-comma' => '{address} {city}, {state}, {zipcode} &nbsp;&nbsp;&nbsp;&nbsp; (New York, NY, 12345 )', 'address-postal-city-state' => '{address} {zipcode} {city}, {state} &nbsp;&nbsp;&nbsp;&nbsp; (12345 New York, NY )', 'address-postal' => '{address} {city} {zipcode} &nbsp;&nbsp;&nbsp;&nbsp; (New York 12345 )', 'address-postal-comma' => '{address} {city}, {zipcode} &nbsp;&nbsp;&nbsp;&nbsp; (New York, 12345 )', 'address-city' => '{address} {city} &nbsp;&nbsp;&nbsp;&nbsp; (London)', 'postal-address' => '{zipcode} {state} {city} {address} &nbsp;&nbsp;&nbsp;&nbsp; (12345, NY, New York)'));
echo '<p class="desc label" style="border:none; margin-bottom: 0;">' . sprintf(__('A lot of countries have their own address format. Please choose one that matches yours. If you have something completely different, please let us know via %s.', 'yoast-local-seo'), '<a href="mailto:pluginsupport@yoast.com">pluginsupport@yoast.com</a>') . '</p>';
WPSEO_Local_Admin_Wrappers::select('default_country', __('Default country', 'yoast-local-seo'), WPSEO_Local_Frontend::get_country_array());
echo '<p class="desc label" style="border:none; margin-bottom: 0;">' . __('If you\'re having multiple locations and they\'re all in one country, you can select your default country here. This country will be used in the storelocator search to improve the search results.', 'yoast-local-seo') . '</p>';
WPSEO_Local_Admin_Wrappers::textinput('show_route_label', __('"Show route" label', 'yoast-local-seo'), '', array('placeholder' => __('Show route', 'yoast-local-seo')));
echo '<br class="clear">';
echo '<label class="textinput" for="custom_marker">' . __('Custom marker', 'yoast-local-seo') . ':</label>';
echo '<img src="' . (isset($options['custom_marker']) ? wp_get_attachment_url($options['custom_marker']) : '') . '" id="custom_marker" />';
WPSEO_Local_Admin_Wrappers::hidden('custom_marker');
echo '<button class="set_custom_images button">' . __('Set custom marker image', 'yoast-local-seo') . '</button>';
if (isset($options['custom_marker']) && '' != $options['custom_marker']) {
    echo '<br /><button id="remove_marker">' . __('Remove marker', 'yoast-local-seo') . '</button>';
    $wpseo_local_core->check_custom_marker_size($options['custom_marker']);
} else {
    echo '<p class="desc label" style="border:none; margin-bottom: 0;">' . __('The custom marker should be 100x100 px. If the image exceeds those dimensions it could (partially) cover the info popup.', 'yoast-local-seo') . '</p>';
}
echo '<br class="clear">';
WPSEO_Local_Admin_Wrappers::checkbox('hide_tools_section', '', __('Hide tools section', 'yoast-local-seo'));
Example #3
0
 /**
  * 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>';
             }
         }
     }
 }
Example #4
0
 /**
  * Return the country name based on country code
  *
  * @since 0.1
  *
  * @param string $country_code Two char country code
  *
  * @return string Country name
  */
 public static function get_country($country_code = '')
 {
     $countries = WPSEO_Local_Frontend::get_country_array();
     if ($country_code == '' || !array_key_exists($country_code, $countries)) {
         return false;
     }
     return $countries[$country_code];
 }
/**
 * 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;
}
Example #6
0
 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;
 }
Example #7
0
 /**
  * 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;
 }