/**
  * Deletes the posts that are not referenced anymore in the user_events table.
  *
  * @param array $event_ids
  *
  * @return number the number of deleted post
  */
 private function delete_events_not_referenced_by_facebook_users(array $event_ids)
 {
     $count = 0;
     foreach ($event_ids as $event_id) {
         $facebook_event = Ai1ec_Facebook_Factory::get_facebook_event_instance();
         $facebook_event->set_id($event_id);
         $referenced = $facebook_event->check_if_someone_refers_the_event_in_the_user_events_table();
         if (!$referenced) {
             $wp_id = $facebook_event->get_wordpress_post_id_from_facebook_event_id();
             $deleted = wp_delete_post($wp_id);
             if ($deleted !== FALSE) {
                 $count++;
             }
         }
     }
     return $count;
 }
 /**
  * This function is called when the user deletes a calendar event and we must delete the linked facebook event
  *
  * @param Ai1ec_Event $event
  */
 public function handle_delete_event(Ai1ec_Event $event)
 {
     if (isset($event->facebook_eid) && (int) $event->facebook_eid !== 0 && $event->facebook_status !== self::FB_IMPORTED_EVENT) {
         if ($this->check_if_we_have_a_valid_access_token()) {
             $facebook_event = Ai1ec_Facebook_Factory::get_facebook_event_instance();
             $facebook_event->set_id($event->facebook_eid);
             $facebook = $this->facebook_instance_factory();
             $facebook_event->delete_from_facebook($facebook);
         } else {
             $message = array("label" => __('All-in-One Event Calendar Facebook Import Error', AI1EC_PLUGIN_NAME), "message" => __("We couldn't delete data from Facebook as we don't have a valid access token. Try to log out of Facebook from the <strong>Events</strong> &gt; <strong>Calendar Feeds</strong> screen, then log in again.", AI1EC_PLUGIN_NAME), "message_type" => "error");
             update_option(Ai1ecFacebookConnectorPlugin::FB_OPTION_CRON_NOTICE, $message);
         }
     }
 }
 /**
  * Execute the custom bulk action "Export to Facebook".
  *
  */
 public function facebook_custom_bulk_action()
 {
     // if our custom action is selected and something has been posted
     if ((isset($_GET['action']) && $_GET['action'] === 'export-facebook' || isset($_GET['action2']) && $_GET['action2'] === 'export-facebook') && isset($_GET['post'])) {
         // Check if we have a valid access token
         if ($this->facebook_plugin->check_if_we_have_a_valid_access_token()) {
             // Iterate on the posts
             foreach ($_GET['post'] as $post_id) {
                 $ai1ec_event = new Ai1ec_Event($post_id);
                 // MAke an extra check that the post can be exported
                 if ($ai1ec_event->facebook_status === '') {
                     // Get a facebook event instance
                     $facebook_event = Ai1ec_Facebook_Factory::get_facebook_event_instance();
                     // Prepare the event
                     $facebook_event->populate_event_from_ai1ec_event($ai1ec_event);
                     $facebook = $this->facebook_plugin->facebook_instance_factory();
                     // Save the event to facebook
                     $result = $facebook_event->save_to_facebook($facebook);
                     // If everything went as expected, update the calendar event
                     if (is_array($result) && !empty($result)) {
                         $ai1ec_event->facebook_eid = $result['id'];
                         $ai1ec_event->facebook_status = Ai1ecFacebookConnectorPlugin::FB_EXPORTED_EVENT;
                         $ai1ec_event->save(TRUE);
                     }
                 }
                 // Wait one second to avoid overloading Facebook
                 sleep(1);
             }
         } else {
             $message = array("label" => __('All-in-One Event Calendar Facebook Export Error', AI1EC_PLUGIN_NAME), "message" => __("We couldn't export data to Facebook as we don't have a valid access token. Try to log out of Facebook from the <strong>Events</strong> &gt; <strong>Calendar Feeds</strong> screen, then log in again.", AI1EC_PLUGIN_NAME), "message_type" => "error");
             update_option(Ai1ecFacebookConnectorPlugin::FB_OPTION_CRON_NOTICE, $message);
         }
     }
 }