/** * Execute changes made in ClassiPress 3.1.0. * Geocoding migration script. * * @since 3.1.0 */ function cp_update_advanced_search_db() { global $wpdb; $output = ''; $post_ids = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type = %s ORDER BY ID ASC", APP_POST_TYPE)); if ($post_ids) { echo scb_admin_notice(__('Geocoding ad listing addresses to make the advanced search radius feature work. This process queries Google Maps to get longitude and latitude coordinates based on each ad listings address. Please be patient as this may take a few minutes to complete.', APP_TD)); foreach ($post_ids as $post_id) { if (!cp_get_geocode($post_id)) { $result = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key IN ('cp_street','cp_city','cp_state','cp_zipcode','cp_country')", $post_id), OBJECT_K); $address = ''; foreach ($result as $cur) { if (!empty($cur->meta_key)) { $address .= "{$cur->meta_value}, "; } } $address = rtrim($address, ', '); if ($address) { $output .= sprintf('<p>' . __("Ad #%d - %s ", APP_TD), $post_id, $address); $geocode = json_decode(wp_remote_retrieve_body(wp_remote_get('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address) . '&sensor=false'))); if ('OK' == $geocode->status) { $output .= esc_html("({$geocode->results[0]->geometry->location->lat}, {$geocode->results[0]->geometry->location->lng})"); cp_update_geocode($post_id, '', $geocode->results[0]->geometry->location->lat, $geocode->results[0]->geometry->location->lng); $output .= ' » <font color="green">' . __('Geocoding complete.', APP_TD) . '</font>'; } else { $output .= ' » <font color="red">' . __('Geocoding failed - address not found.', APP_TD) . '</font>'; } $output .= '</p>'; } } } $output .= '<br /><strong>' . __(' Geocoding table updated.', APP_TD) . '</strong><br />'; $output .= '<small>' . __('Please note: Ads that failed during this process will not show up during a radius search since the address was invalid.', APP_TD) . '</small>'; update_option('cp_db_version', '1280'); echo scb_admin_notice($output); } // end if $post_ids update_option('cp_db_version', '1280'); }
function cp_add_geocode($post_id, $cat, $lat, $lng) { global $wpdb; if (!empty($cat)) { _deprecated_argument(__FUNCTION__, '3.3.2'); } $post_id = intval($post_id); $lat = floatval($lat); $lng = floatval($lng); if (cp_get_geocode($post_id)) { return false; } $wpdb->insert($wpdb->cp_ad_geocodes, array('post_id' => $post_id, 'lat' => $lat, 'lng' => $lng)); return true; }
function cp_add_geocode($post_id, $cat, $lat, $lng) { global $wpdb; $post_id = intval($post_id); $lat = floatval($lat); $lng = floatval($lng); if (cp_get_geocode($post_id, $cat)) { return false; } $wpdb->insert($wpdb->cp_ad_geocodes, array('post_id' => $post_id, 'category' => $cat, 'lat' => $lat, 'lng' => $lng)); return true; }
<div id="gmap" class="mapblock"> <?php // check to see if ad is legacy or not and then assemble the map address if (get_post_meta($post->ID, 'location', true)) { $make_address = get_post_meta($post->ID, 'location', true); } else { $make_address = get_post_meta($post->ID, 'cp_street', true) . ' ' . get_post_meta($post->ID, 'cp_city', true) . ' ' . get_post_meta($post->ID, 'cp_state', true) . ' ' . get_post_meta($post->ID, 'cp_zipcode', true); } $coordinates = cp_get_geocode($post->ID); ?> <script type="text/javascript">var address = "<?php echo esc_js($make_address); ?> ";</script> <?php cp_google_maps_js($coordinates); ?> <!-- google map div --> <div id="map"></div> </div> <?php // Google map on single page function cp_google_maps_js($coordinates) {
/** * ClassiPress 3.1.0 geocoding migration script * * @since 3.1.0 */ function cp_update_advanced_search_db() { global $wpdb, $app_current_db_version; // get the ClassiPress db version number $app_current_db_version = get_option('cp_db_version'); cp_create_geocode_table(); $post_ids = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type = '" . APP_POST_TYPE . "' ORDER BY ID ASC")); if ($post_ids) { ?> <div class="wrap"> <?php screen_icon('themes'); ?> <h2><?php _e('ClassiPress Update', 'appthemes'); ?> </h2> <p class="info"><?php _e('Geocoding ad listing addresses to enable the advanced search radius feature to work. This process queries Google Maps to get longitude and latitude coordinates based on each ad listings address. Please be patient as this may take a few minutes to complete.', 'appthemes'); ?> </p> <?php foreach ($post_ids as $post_id) { if (!cp_get_geocode($post_id)) { $result = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key IN ('cp_street','cp_city','cp_state','cp_zipcode','cp_country')", $post_id), OBJECT_K); $address = ''; foreach ($result as $cur) { if (!empty($cur->meta_key)) { $address .= "{$cur->meta_value}, "; } } $address = rtrim($address, ', '); if ($address) { printf('<p>' . __("Ad #%d - %s ", 'appthemes'), $post_id, $address); $geocode = json_decode(wp_remote_retrieve_body(wp_remote_get('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address) . '&sensor=false'))); if ('OK' == $geocode->status) { echo esc_html("({$geocode->results[0]->geometry->location->lat}, {$geocode->results[0]->geometry->location->lng})"); $category = get_the_terms($post_id, APP_TAX_CAT); cp_update_geocode($post_id, $category[0]->name, $geocode->results[0]->geometry->location->lat, $geocode->results[0]->geometry->location->lng); echo ' » <font color="green">' . __('Geocoding complete.', 'appthemes') . '</font>'; } else { echo ' » <font color="red">' . __('Geocoding failed - address not found.', 'appthemes') . '</font>'; } echo '</p>'; } } } echo '<br /><strong>' . __('Database update complete.', 'appthemes') . '</strong><br />'; echo '<small>' . __('Please note: Ads that failed during this process will not show up during a radius search since the address was invalid. Ignore this message for new installs.', 'appthemes') . '</small>'; echo '<br /><br /><a class="button" href="admin.php?page=admin-options.php">' . __('Continue to Your Dashboard', 'appthemes') . '</a>'; ?> </div> <?php update_option('cp_db_version', 1280); include ABSPATH . '/wp-admin/admin-footer.php'; exit; } // end if $post_ids update_option('cp_db_version', 1280); }