/** * 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_do_update_geocode($meta_id, $post_id, $meta_key, $meta_value) { global $wpdb, $cp_options; if (!in_array($meta_key, array('cp_city', 'cp_country', 'cp_state', 'cp_street', 'cp_zipcode'))) { return; } // remove old geocode result cp_delete_geocode($post_id); $address = ''; $fields = $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); foreach ($fields as $field) { if (!empty($field->meta_value)) { $address .= $field->meta_value . ', '; } } $address = rtrim($address, ', '); if (empty($address)) { return; } $region = $cp_options->gmaps_region; $address = urlencode($address); $geocode = json_decode(wp_remote_retrieve_body(wp_remote_get("http://maps.googleapis.com/maps/api/geocode/json?address={$address}&sensor=false®ion={$region}"))); if ('OK' == $geocode->status) { cp_update_geocode($post_id, '', $geocode->results[0]->geometry->location->lat, $geocode->results[0]->geometry->location->lng); } }
function cp_do_update_geocode($meta_id, $post_id, $meta_key, $meta_value) { global $wpdb; if (in_array($meta_key, array('cp_city', 'cp_country', 'cp_state', 'cp_street', 'cp_zipcode'))) { $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) { $region = get_option('cp_gmaps_region', 'us'); $address = urlencode($address); $geocode = json_decode(wp_remote_retrieve_body(wp_remote_get("http://maps.googleapis.com/maps/api/geocode/json?address={$address}&sensor=false®ion={$region}"))); if ('OK' == $geocode->status) { $category = wp_get_post_terms($post_id, APP_TAX_CAT); $category_name = empty($category) ? '' : $category[0]->name; cp_update_geocode($post_id, $category_name, $geocode->results[0]->geometry->location->lat, $geocode->results[0]->geometry->location->lng); } } } }
/** * save new coordinates for the ad listing */ function appmaps_save_meta_box($post_id, $post) { global $wpdb, $appmaps_key; // make sure something has been submitted from our nonce if (!isset($_POST[$appmaps_key . '_wpnonce'])) { return $post_id; } // verify this came from the our screen and with proper authorization, // because save_post can be triggered at other times if (!wp_verify_nonce($_POST[$appmaps_key . '_wpnonce'], basename(__FILE__))) { return $post_id; } // verify if this is an auto save routine. // if it is our form and it has not been submitted, dont want to do anything if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; } // lastly check to make sure this user has permissions to save post fields if (!current_user_can('edit_post', $post_id)) { return $post_id; } if ($post->post_type == 'revision') { return $post_id; } // update coordinates if (!empty($_POST['appmaps_latitude']) && !empty($_POST['appmaps_longitude'])) { $category = get_the_terms($post_id, 'ad_cat'); if (!$category || is_wp_error($category)) { $category[0]->name = ''; } cp_update_geocode($post_id, $category[0]->name, $_POST['appmaps_latitude'], $_POST['appmaps_longitude']); } }
function cp_default_ad() { global $wpdb; $posts = get_posts(array('posts_per_page' => 1, 'post_type' => APP_POST_TYPE, 'no_found_rows' => true)); if (!empty($posts)) { return; } $cat = appthemes_maybe_insert_term('Misc', APP_TAX_CAT); $description = '<p>This is your first ClassiPress ad listing. It is a placeholder ad just so you can see how it works. Delete this before launching your new classified ads site.</p>Duis arcu turpis, varius nec sagittis id, ultricies ac arcu. Etiam sagittis rutrum nunc nec viverra. Etiam egestas congue mi vel sollicitudin.</p><p>Vivamus ac libero massa. Cras pellentesque volutpat dictum. Ut blandit dapibus augue, lobortis cursus mi blandit sed. Fusce vulputate hendrerit sapien id aliquet.</p>'; $default_ad = array('post_title' => 'My First Classified Ad', 'post_name' => 'my-first-classified-ad', 'post_content' => $description, 'post_status' => 'publish', 'post_type' => APP_POST_TYPE, 'post_author' => 1); // insert the default ad $post_id = wp_insert_post($default_ad); //set the custom post type categories wp_set_post_terms($post_id, $cat['term_id'], APP_TAX_CAT, false); //set the custom post type tags $new_tags = array('ad tag1', 'ad tag2', 'ad tag3'); wp_set_post_terms($post_id, $new_tags, APP_TAX_TAG, false); // set some default meta values $ad_expire_date = appthemes_mysql_date(current_time('mysql'), 30); $advals['cp_sys_expire_date'] = $ad_expire_date; $advals['cp_sys_ad_duration'] = '30'; $advals['cp_sys_ad_conf_id'] = '3624e0d2963459d2'; $advals['cp_sys_userIP'] = '153.247.194.375'; $advals['cp_daily_count'] = '0'; $advals['cp_total_count'] = '0'; $advals['cp_price'] = '250'; $advals['cp_street'] = '153 Townsend St'; $advals['cp_city'] = 'San Francisco'; $advals['cp_state'] = 'California'; $advals['cp_country'] = 'United States'; $advals['cp_zipcode'] = '94107'; $advals['cp_sys_total_ad_cost'] = '5.00'; // now add the custom fields into WP post meta fields foreach ($advals as $meta_key => $meta_value) { add_post_meta($post_id, $meta_key, $meta_value, true); } // set coordinates of new ad cp_update_geocode($post_id, '', '37.779633', '-122.391762'); }
/** * 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); }