Example #1
0
 public static function action_wp_ajax_betareno_add_idea()
 {
     $response = array('code' => 200, 'message' => 'ok');
     $required_fields = array('api_key', 'what', 'who', 'longitude', 'latitude');
     foreach ($required_fields as $field) {
         if (empty($_POST[$field])) {
             $response['code'] = 400;
             $response['message'] = 'Missing required field "' . $field . '"';
             echo json_encode($response);
             exit;
         }
     }
     if (!self::verify_api_key($_POST['api_key'])) {
         $response['code'] = 403;
         $response['message'] = 'Invalid API key.' . $field . '"';
         echo json_encode($response);
         exit;
     }
     // Create the post
     $postdata = array('post_type' => 'idea', 'post_title' => $_POST['what'], 'post_status' => 'publish');
     $post_id = wp_insert_post($postdata, true);
     if (is_wp_error($post_id)) {
         $response['code'] = 500;
         $response['message'] = $post_id->get_error_message();
         echo json_encode($response);
         exit;
     }
     $post = get_post($post_id);
     // Set the actor
     $term_ids = wp_set_object_terms($post_id, $_POST['who'], 'actor');
     if (is_wp_error($term_ids)) {
         $response['code'] = 500;
         $response['message'] = $term_ids->get_error_message();
         echo json_encode($response);
         exit;
     }
     $actor = get_term($term_ids[0], 'actor');
     // Set the location
     $location_id = GeoMashupDB::set_object_location('post', $post_id, array('lat' => $_POST['latitude'], 'lng' => $_POST['longitude']));
     if (is_wp_error($location_id)) {
         $response['code'] = 500;
         $response['message'] = $location_id->get_error_message();
         echo json_encode($response);
         exit;
     }
     $location = GeoMashupDB::get_location($location_id);
     // Set the when
     $when = '';
     if (isset($_POST['when'])) {
         $time = strtotime($_POST['when']);
         if ($time) {
             // Use Reno's timezone
             $dtzone = new DateTimeZone('America/Los Angeles');
             $date = date('r', $time);
             $dtime = new DateTime($date);
             $dtime->setTimezone($dtzone);
             $when = $dtime->format('Y-m-d h:i:00 e');
             update_post_meta($post_id, 'when', $when);
         }
     }
     // Handle photos
     $before_photo_url = '';
     if (isset($_FILES['before_photo'])) {
         $before_photo_id = media_handle_upload('before_photo', $post_id, array('post_title' => 'Before'));
         if (!is_wp_error($before_photo_id)) {
             update_post_meta($before_photo_id, 'photo_type', 'before');
             list($before_photo_url, $width, $height) = wp_get_attachment_image_src($before_photo_id);
         }
     }
     $after_photo_url = '';
     if (isset($_FILES['after_photo'])) {
         $after_photo_id = media_handle_upload('after_photo', $post_id, array('post_title' => 'After'));
         if (!is_wp_error($after_photo_id)) {
             update_post_meta($after_photo_id, 'photo_type', 'after');
             list($after_photo_url, $width, $height) = wp_get_attachment_image_src($after_photo_id);
         }
     }
     // Respond with the new idea
     $response['idea'] = array('ID' => $post_id, 'what' => $post->post_title, 'who' => $actor->name, 'when' => $when, 'latitude' => $location->lat, 'longitude' => $location->lng, 'votes' => 1, 'before_photo_url' => $before_photo_url, 'after_photo_url' => $after_photo_url);
     echo json_encode($response);
     exit;
 }
 function test_location_crud()
 {
     // create
     $location = $this->get_nv_test_location();
     $id = GeoMashupDB::set_location($location, $do_lookups = false);
     $this->assertFalse(is_wp_error($id));
     $this->assertTrue(is_numeric($id));
     $this->assertTrue($id > 0);
     // read
     $out = GeoMashupDB::get_location($id);
     $this->assertEquals($location->lat, $out->lat);
     $this->assertEquals($location->lng, $out->lng);
     // read cache
     $lcache = wp_cache_get($id, 'geo_mashup_locations');
     $this->assertInstanceOf('stdClass', $lcache);
     $this->assertEquals($id, $lcache->id);
     // update
     $out->locality_name = rand_str();
     $update_id = GeoMashupDB::set_location($out, $do_lookups = false);
     $this->assertEquals($update_id, $id);
     $out2 = GeoMashupDB::get_location($id);
     $this->assertEquals($id, $out2->id);
     $this->assertEquals($out->locality_name, $out2->locality_name);
     // delete
     GeoMashupDB::delete_location($id);
     $out = GeoMashupDB::get_location($id);
     $this->assertNull($out);
 }