/** * Save an object location from data posted by the location editor. * * @since 1.3 * @uses GeoMashupDB::set_object_location() * @uses GeoMashupDB::delete_location() * * @param string $object_name The name of the object being edited. * @param string $object_id The ID of the object being edited. * @return bool|WP_Error True or a WordPress error. */ public function save_posted_object_location($object_name, $object_id) { // Check the nonce if (empty($_POST['geo_mashup_nonce']) || !wp_verify_nonce($_POST['geo_mashup_nonce'], 'geo-mashup-edit')) { return new WP_Error('invalid_request', __('Object location not saved - invalid request.', 'GeoMashup')); } $action = $this->get_submit_action(); if ('save' == $action or 'geocode' == $action) { $date_string = $_POST['geo_mashup_date'] . ' ' . $_POST['geo_mashup_hour'] . ':' . $_POST['geo_mashup_minute'] . ':00'; $geo_date = date('Y-m-d H:i:s', strtotime($date_string)); $post_location = array(); // If PHP has added slashes, WP will do it again before saving $post_location['saved_name'] = stripslashes($_POST['geo_mashup_location_name']); if ('geocode' == $action) { $status = GeoMashupDB::geocode($_POST['geo_mashup_search'], $post_location); if ($status != 200) { $post_location = array(); } } else { if (!empty($_POST['geo_mashup_select'])) { $selected_items = explode('|', $_POST['geo_mashup_select']); $post_location = intval($selected_items[0]); } else { $post_location['id'] = $_POST['geo_mashup_location_id']; list($lat, $lng) = split(',', $_POST['geo_mashup_location']); $post_location['lat'] = trim($lat); $post_location['lng'] = trim($lng); $post_location['geoname'] = $_POST['geo_mashup_geoname']; $post_location['address'] = stripslashes($_POST['geo_mashup_address']); $post_location['postal_code'] = $_POST['geo_mashup_postal_code']; $post_location['country_code'] = $_POST['geo_mashup_country_code']; $post_location['admin_code'] = $_POST['geo_mashup_admin_code']; $post_location['sub_admin_code'] = $_POST['geo_mashup_sub_admin_code']; $post_location['locality_name'] = $_POST['geo_mashup_locality_name']; } } if (!empty($post_location)) { $error = GeoMashupDB::set_object_location($object_name, $object_id, $post_location, true, $geo_date); if (is_wp_error($error)) { return $error; } } } else { if ('delete' == $action) { $error = GeoMashupDB::delete_object_location($object_name, $object_id); if (is_wp_error($error)) { return $error; } } } // If geodata was manually updated but Geo Mashup location was not, // they may be out of sync now. Allowing that for now. return true; }
function test_object_location_crud() { $post_id = wp_insert_post(array('post_status' => 'publish', 'post_content' => rand_str(), 'post_title' => rand_str())); $location = $this->get_nv_test_location(); // create $location_id = GeoMashupDB::set_object_location('post', $post_id, $location, $do_lookups = false); $this->assertFalse(is_wp_error($location_id)); $this->assertTrue(is_numeric($location_id)); $this->assertTrue($location_id > 0); // read $out = GeoMashupDB::get_object_location('post', $post_id); $this->assertNotNull($out); $this->assertEquals($location->lat, $out->lat); $this->assertEquals($location->lng, $out->lng); // update $geo_date = '2013-01-04 00:00:00'; $update_id = GeoMashupDB::set_object_location('post', $post_id, $location_id, $do_lookups = false, $geo_date); $this->assertFalse(is_wp_error($update_id)); $out = GeoMashupDB::get_object_location('post', $post_id); $this->assertEquals($out->geo_date, $geo_date); // delete $rows_affected = GeoMashupDB::delete_object_location('post', $post_id); $this->assertEquals($rows_affected, 1); $out = GeoMashupDB::get_object_location('post', $post_id); $this->assertNull($out); }