/**
  * Remove a Facebook graph object that the user had subscribed to. This also deletes the orphaned (not referred to by other subscribed users) events if the user choose to do so.
  */
 public function remove_subscribed_ajax()
 {
     // Create the object that will be returned.
     $response = array("errors" => FALSE, "id" => $_POST['ai1ec_post_id'], "logged" => $_POST['ai1ec_logged_user'], "type" => $_POST['type']);
     if (isset($_POST['ai1ec_post_id'])) {
         try {
             $fgoc = Ai1ec_Facebook_Factory::get_facebook_graph_object_collection((array) $_POST['ai1ec_post_id']);
             $remove_events = $_POST['ai1ec_remove_events'] === 'true' ? TRUE : FALSE;
             // Try to unsubscribe.
             $how_many = $fgoc->update_subscription_status(FALSE, $remove_events);
             // Get an object
             $fgo = Ai1ec_Facebook_Factory::get_facebook_graph_object($_POST['ai1ec_post_id']);
             // No exception, prepare the message to return
             $user_name = $fgo->get_user_name();
             $response['message'] = sprintf(__("You unsubscribed from %s. ", AI1EC_PLUGIN_NAME), $user_name);
             if ($how_many > 0) {
                 $response['message'] .= sprintf(_n("%d event was deleted", "%d events were deleted.", $how_many, AI1EC_PLUGIN_NAME), $how_many);
             }
             // IF we are removing the logged user, update the session variable.
             if ($_POST['ai1ec_logged_user'] === 'true') {
                 $current_user = $this->get_current_facebook_user_from_cache();
                 $current_user->set_subscribed(FALSE);
                 $current_user->set_tag('');
                 $current_user->set_category('');
                 $this->save_facebook_user_in_cache($current_user);
             }
             // Get the data for the update of the multiselect
             $response['html'] = $this->refresh_multiselect($_POST['type']);
         } catch (Ai1ec_Facebook_Db_Exception $e) {
             $response['errors'] = TRUE;
             $error_messages = $e->get_error_messages();
             $response['error_message'] = $error_messages[0];
         }
     }
     echo json_encode($response);
     die;
 }
 /**
  * Convert a Facebook event into a Ai1ec Event.
  *
  * @param array $facebook_event The event to convert
  *
  * @param int $user_timezone the timezone of the user which is used to convert time
  *
  * @param int $post_id the id of the wp_post if we are updating
  *
  * @return Ai1ec_Event|array an Ai1ec object if we are performing an insert or an array if we ar updating
  */
 private function convert_facebook_event_to_ai1ec_event(array $facebook_event, $user_timezone, $post_id = NULL)
 {
     global $ai1ec_events_helper;
     // Create a new calendar event.
     $event = new Ai1ec_Event();
     // Set start time and end time after converting it to GMT
     if (isset($facebook_event['start_time'])) {
         $event->start = Ai1ec_Facebook_Event::convert_facebook_time_to_gmt_timestamp($facebook_event['start_time'], $user_timezone);
     }
     if (isset($facebook_event['end_time']) && $facebook_event['end_time'] !== NULL) {
         $event->end = Ai1ec_Facebook_Event::convert_facebook_time_to_gmt_timestamp($facebook_event['end_time'], $user_timezone);
     }
     // Check if the coordinates are set.
     $coordinates_set = isset($facebook_event['venue']['longitude']) && isset($facebook_event['venue']['latitude']);
     // Save location details, defaulting to empty strings if unset.
     $event->venue = Ai1ec_Facebook_Data_Converter::return_empty_or_value_if_set($facebook_event, 'location');
     $event->address = Ai1ec_Facebook_Data_Converter::return_empty_or_value_if_set($facebook_event['venue'], 'street');
     $event->city = Ai1ec_Facebook_Data_Converter::return_empty_or_value_if_set($facebook_event['venue'], 'city');
     $event->province = Ai1ec_Facebook_Data_Converter::return_empty_or_value_if_set($facebook_event['venue'], 'state');
     $event->postal_code = Ai1ec_Facebook_Data_Converter::return_empty_or_value_if_set($facebook_event['venue'], 'zip');
     $event->country = Ai1ec_Facebook_Data_Converter::return_empty_or_value_if_set($facebook_event['venue'], 'country');
     // If location is not a proper Facebook venue, address information will
     // not be provided by the above fields. Instead, "location" stores the
     // address, likely with commas separating components. If there are no commas
     // then we keep it as the venue name.
     if (!$event->address) {
         if (false !== strpos($event->venue, ',')) {
             $event->address = $event->venue;
             $event->venue = '';
         }
     } else {
         // In Ai1ec, "address" property always stores the combination of all
         // address details, so concatenate them now. (City, province, postal code,
         // country are used only for data collection & their indexing potential.)
         $address = array($event->address, $event->city, $event->province, $event->postal_code, $event->country);
         $event->address = implode(', ', array_filter($address));
     }
     // We show the map if we have a valid address or we have coordinates. This is arbitrary.
     $event->show_map = $coordinates_set || $event->address;
     $data = Ai1ec_Facebook_Factory::get_facebook_graph_object($facebook_event['facebook_user'])->get_category_and_tag();
     if (!$data['map_display_enabled']) {
         $event->show_map = false;
     }
     $event->show_coordinates = $coordinates_set ? 1 : 0;
     if ($coordinates_set) {
         $event->longitude = $facebook_event['venue']['longitude'];
         $event->latitude = $facebook_event['venue']['latitude'];
     }
     // We save the user and eid.
     $event->facebook_user = $facebook_event['facebook_user'];
     $event->facebook_eid = $facebook_event['eid'];
     $event->facebook_status = Ai1ecFacebookConnectorPlugin::FB_IMPORTED_EVENT;
     // We create the post array. This will create / update the wp_post.
     $post = array();
     $post['post_content'] = $facebook_event['description'];
     // Prepend profile picture, left-floated, to post body.
     if ($facebook_event['has_profile_pic']) {
         list(, , , $attr) = getimagesize($facebook_event['pic_big']);
         $post['post_content'] = '<div class="ai1ec-event-avatar alignleft"><img src="' . esc_attr($facebook_event['pic_big']) . '" ' . $attr . ' /></div>' . $post['post_content'];
     }
     $post['post_title'] = $facebook_event['name'];
     $post['post_status'] = 'publish';
     $post['post_type'] = AI1EC_POST_TYPE;
     // Set categories, tags, and comment status.
     $fgo = Ai1ec_Facebook_Factory::get_facebook_graph_object($facebook_event['facebook_user']);
     $data = $fgo->get_category_and_tag();
     $event->categories = $data['category'];
     $event->tags = $data['tag'];
     $post['comment_status'] = $data['comments_enabled'] ? 'open' : 'closed';
     // If we are saving a new event, fill out event object and return it alone.
     if ($post_id === NULL) {
         $event->post = $post;
         return $event;
     } else {
         $post['ID'] = $post_id;
         $event->post_id = $post_id;
         $return_array = array("post" => $post, "event" => $event);
         return $return_array;
     }
 }