public static function add(array $DATA_ = NULL)
 {
     $url = $DATA_['uri'];
     $name = @$DATA_['name'];
     $post_id = @$DATA_['post_id'];
     apply_filters('em_object_get_image_type', 'event', false, isset($this) ? $this : self);
     //Handle the attachment as a WP Post
     $attachment = '';
     $tmpfname = tempnam(ESS_IO::tmp(), "ESS_IMG_" . $post_id . "_");
     $h = fopen($tmpfname, "w");
     if ($h && strlen($url) > 5) {
         fwrite($h, file_get_contents($url));
         $image_ = getimagesize($tmpfname);
         switch ($image_['mime']) {
             case "image/gif":
                 $mime = "gif";
                 break;
             case "image/jpeg":
                 $mime = "jpg";
                 break;
             case "image/png":
                 $mime = "png";
                 break;
             case "image/bmp":
                 $mime = "bmp";
                 break;
         }
         $file = array('tmp_name' => $tmpfname, 'name' => basename($tmpfname), 'type' => $image_[2], 'mime' => $mime, 'width' => $image_[0], 'height' => $image_[1], 'size' => filesize($tmpfname), 'error' => 0);
         //var_dump( $file );
         if (file_exists($file['tmp_name']) && ESS_Images::image_validator($file)) {
             require_once ABSPATH . "wp-admin" . '/includes/file.php';
             require_once ABSPATH . "wp-admin" . '/includes/image.php';
             $attachment = ESS_Images::handle_upload($file);
             if ($attachment) {
                 //echo "DEBUG: <b>". __CLASS__.":".__LINE__."</b>";
                 //var_dump( $attachment );
                 $attachment_id = wp_insert_attachment(array('post_mime_type' => $image_['mime'], 'post_title' => $name, 'post_content' => '', 'post_status' => 'inherit'), $attachment['file'], $post_id);
                 $attachment_metadata = wp_generate_attachment_metadata($attachment_id, $attachment['file']);
                 wp_update_attachment_metadata($attachment_id, $attachment_metadata);
                 update_post_meta($post_id, '_thumbnail_id', $attachment_id);
                 update_post_meta($post_id, '_start_ts', date('U'));
                 update_post_meta($post_id, '_end_ts', intval(date('U') + 60 * 60 * 24 * 365));
                 @fclose($h);
                 if (file_exists($tmpfname)) {
                     @unlink($tmpfname);
                 }
                 return apply_filters('em_object_image_upload', $attachment_id, $this);
             }
         }
         @fclose($h);
         if (file_exists($tmpfname)) {
             @unlink($tmpfname);
         }
     }
     return apply_filters('em_object_image_upload', false, $this);
 }
 public static function output($event_id = NULL, $event_cat = NULL, $page = 1, $is_download = FALSE, $is_push = FALSE)
 {
     //ob_clean();
     //ini_set('zlib.output_handler','');
     $EM_Events = !empty($event_id) ? array(em_get_event($event_id)) : EM_Events::get(apply_filters('em_calendar_template_args', array('limit' => get_option('ess_feed_limit'), 'category' => $event_cat, 'page' => $page, 'owner' => FALSE, 'orderby' => 'event_start_date')));
     $limit = 0;
     //no limit for now, but this shall be eventually set in the wP settings page, option name dbem_ess_limit
     //var_dump( $EM_Events );die;
     //echo "count: ".count($EM_Events);
     //echo "limit: " . get_option('ess_feed_limit', $limit );
     //echo "event_id: ". $event_id;
     //die;
     $essFeed = new FeedWriter(strlen(get_option('ess_feed_language')) == 2 ? get_option('ess_feed_language', ESS_Database::DEFAULT_LANGUAGE) : ESS_Database::DEFAULT_LANGUAGE);
     $essFeed->DEBUG = FALSE;
     // ###  DEBUG  MODE
     $essFeed->setTitle(self::_g('ess_feed_title', __('ESS Feed generated by Wordpress', 'dbem')));
     $essFeed->setLink(str_replace('&push=1', '', str_replace('&download=1', '', FeedWriter::getCurrentURL())));
     $essFeed->setPublished(ESS_Timezone::get_date_GMT(get_option('em_last_modified', current_time('timestamp', TRUE))));
     $essFeed->setUpdated(ESS_Timezone::get_date_GMT(current_time('timestamp', TRUE)));
     $essFeed->setRights(self::_g('ess_feed_rights', 'Rights'));
     $count = 0;
     while (count($EM_Events) > 0) {
         foreach ($EM_Events as $ie => $EM_Event) {
             if (intval($EM_Event->event_id) <= 0) {
                 // event empty
                 break;
             }
             if (get_option('ess_feed_limit', $limit) != 0 && $count > get_option('ess_feed_limit', $limit)) {
                 //we've reached the max event per feed
                 break;
             }
             //dd( $EM_Event );
             $event_url = self::unhtmlentities(esc_html(urldecode($EM_Event->guid)));
             // --- Create a new event to the feed: <feed>
             $newEvent = $essFeed->newEventFeed();
             $newEvent->setTitle($EM_Event->output(get_option('dbem_rss_title_format', sprintf(__('Event Nb: %d', 'dbem'), $ie)), "rss"));
             $newEvent->setUri($event_url);
             $newEvent->setPublished(ESS_Timezone::get_date_GMT(strtotime($EM_Event->post_date)));
             $newEvent->setUpdated(ESS_Timezone::get_date_GMT(strtotime($EM_Event->post_modified)));
             $newEvent->setAccess(intval($EM_Event->event_private) === 1 ? EssDTD::ACCESS_PRIVATE : EssDTD::ACCESS_PUBLIC);
             // -- encode a unic ID base on the host name + event_id (e.g. www.sample.com:123)
             $newEvent->setId((strlen(@$_SERVER['SERVER_NAME']) > 0 ? $_SERVER['SERVER_NAME'] : @$_SERVER['HTTP_HOST']) . ":" . $EM_Event->event_id);
             $description = wpautop($EM_Event->post_content, TRUE);
             if (get_option('ess_backlink_enabled')) {
                 $feed_uri_host = parse_url($event_url, PHP_URL_HOST);
                 $description = "<h6>" . __("Source:", 'dbem') . " <a target=\"_blank\" title=\"" . __("Source:", 'dbem') . " " . $feed_uri_host . "\" href=\"" . $event_url . "\">" . $feed_uri_host . "</a></h6>" . $description;
             }
             //dd( $description );
             // Add custom attributes json encoded in HTML comments <!--||ATTR||xx{xx:'xx',xx...}||ATTR||-->
             if (@count(@$EM_Event->event_attributes) > 0 && @$EM_Event->event_attributes != NULL) {
                 $custom_att = $EM_Event->event_attributes;
                 //dd( $custom_att );
                 $description .= "<!--" . self::CUSTOM_ATTRIBUTE_SEPARATOR . json_encode(iconv(mb_detect_encoding($custom_att, mb_detect_order(), TRUE), "UTF-8", $custom_att), JSON_UNESCAPED_SLASHES) . self::CUSTOM_ATTRIBUTE_SEPARATOR . "-->";
             }
             $newEvent->setDescription($description);
             //dd( $description );
             // ====== TAGS ========
             $tags = get_the_terms($EM_Event->post_id, EM_TAXONOMY_TAG);
             //var_dump( $tags );
             if (count($tags) > 0 && $tags != NULL) {
                 $arr_ = array();
                 foreach ($tags as $tag) {
                     if (strlen($tag->name) > 1 && is_numeric($tag->name) == FALSE) {
                         array_push($arr_, $tag->name);
                     }
                 }
                 if (count($arr_) > 0) {
                     $newEvent->setTags($arr_);
                 }
             }
             // ====== CATEGORIES ========
             //dd( $EM_Event );
             // $categories_ = EM_Categories::get( $EM_Event->get_categories()->get_ids() ); // enumerate all the categories
             if (@count(@$EM_Event->categories) > 0 && @$EM_Event->categories != NULL) {
                 $cat_duplicates_ = array();
                 foreach ($EM_Event->categories as $cat) {
                     $category_type = get_option('ess_feed_category_type');
                     if (strlen($cat->name) > 0 && strlen($category_type) > 3) {
                         if (in_array(strtolower($cat->name), $cat_duplicates_) == FALSE) {
                             $newEvent->addCategory($category_type, array('name' => $cat->name, 'id' => $cat->id));
                         }
                         $cat_duplicates_[] = strtolower($cat->name);
                     }
                 }
             }
             // ====== DATES =========
             $event_start = NULL;
             if (isset($EM_Event->event_start_date) && isset($EM_Event->event_end_date)) {
                 $event_start = ESS_Timezone::get_date_GMT(strtotime($EM_Event->event_start_date . " " . $EM_Event->event_start_time));
                 $event_stop = ESS_Timezone::get_date_GMT(strtotime($EM_Event->event_end_date . " " . $EM_Event->event_end_time));
                 // -- STANDALONE -----
                 if ($EM_Event->recurrence <= 0) {
                     $duration_s = FeedValidator::getDateDiff('s', $event_start, $event_stop);
                     // number of seconds between two dates
                     $newEvent->addDate('standalone', 'hour', NULL, NULL, NULL, NULL, array('name' => __('Date', 'dbem'), 'start' => $event_start, 'duration' => $duration_s > 0 ? $duration_s / 60 / 60 : 0));
                 } else {
                     $interval = intval($EM_Event->recurrence_interval);
                     $u = $EM_Event->recurrence_freq;
                     $event_unit = $u == 'daily' ? 'day' : ($u == 'weekly' ? 'week' : ($u == 'monthly' ? 'month' : ($u == 'yearly' ? 'year' : 'hour')));
                     switch ($event_unit) {
                         default:
                         case 'day':
                             $limit = FeedValidator::getDateDiff('d', $event_start, $event_stop);
                             break;
                             // number of days
                         // number of days
                         case 'hour':
                             $limit = FeedValidator::getDateDiff('h', $event_start, $event_stop);
                             break;
                             // number of hours
                         // number of hours
                         case 'week':
                             $limit = FeedValidator::getDateDiff('ww', $event_start, $event_stop);
                             break;
                             // number of weeks
                         // number of weeks
                         case 'month':
                             $limit = FeedValidator::getDateDiff('m', $event_start, $event_stop);
                             break;
                             // number of months
                         // number of months
                         case 'year':
                             $limit = FeedValidator::getDateDiff('yyyy', $event_start, $event_stop);
                             break;
                             // number of years
                     }
                     $d = intval($EM_Event->recurrence_byday);
                     $selected_day = $event_unit == 'year' || $event_unit == 'month' || $event_unit == 'week' ? $d == 0 ? 'sunday' : ($d == 1 ? 'monday' : ($d == 2 ? 'tuesday' : ($d == 3 ? 'wednesday' : ($d == 4 ? 'thursday' : ($d == 5 ? 'friday' : ($d == 6 ? 'saturday' : '')))))) : '';
                     $w = intval($EM_Event->recurrence_byweekno);
                     $selected_week = $event_unit == 'month' ? $w == -1 ? 'last' : ($w == 1 ? 'first' : ($w == 2 ? 'second' : ($w == 3 ? 'third' : ($w == 4 ? 'fourth' : '')))) : '';
                     $newEvent->addDate('recurrent', $event_unit, $limit, $interval, $selected_day, $selected_week, array('name' => sprintf(__('Date: %s', 'dbem'), $event_start), 'start' => $event_start, 'duration' => 0));
                 }
             }
             // end date
             // ====== PLACES ========
             $places_ = $EM_Event->get_location();
             //var_dump( $places_ );
             if (is_object($places_) && strlen($places_->location_name) > 0) {
                 $newEvent->addPlace('fixed', null, array('name' => $places_->location_name == $places_->location_address ? sprintf(__('Place: %s', 'dbem'), $places_->location_name) : $places_->location_name, 'latitude' => isset($places_->location_latitude) ? round($places_->location_latitude * 10000) / 10000 : '', 'longitude' => isset($places_->location_longitude) ? round($places_->location_longitude * 10000) / 10000 : '', 'address' => strlen($places_->location_address) > 0 ? $places_->location_address : $places_->location_name, 'city' => $places_->location_town, 'zip' => $places_->location_postcode, 'state' => $places_->location_region, 'state_code' => $places_->location_state, 'country' => FeedValidator::$COUNTRIES_[strtoupper($places_->location_country)], 'country_code' => strtolower($places_->location_country) == 'xe' ? '' : $places_->location_country));
             }
             // end place
             // ====== PRICES =========
             if ($EM_Event->is_free() == TRUE) {
                 $newEvent->addPrice('standalone', 'free', NULL, NULL, NULL, NULL, NULL, array('name' => __('Free', 'dbem'), 'currency' => get_option('ess_feed_currency', ESS_Database::DEFAULT_CURRENCY), 'value' => 0));
             } else {
                 $prices_ = $EM_Event->get_bookings();
                 //dd($prices_);
                 if ($prices_) {
                     if (count($prices_->tickets->tickets) > 0 && $event_start != NULL && $prices_->tickets->tickets != NULL) {
                         if (isset($prices_->tickets)) {
                             if (is_array($prices_->tickets->tickets)) {
                                 foreach ($prices_->tickets->tickets as $i => $price) {
                                     if (is_object($price)) {
                                         $duration_s = $price->ticket_start && $price->ticket_end ? FeedValidator::getDateDiff('s', $price->ticket_start, $price->ticket_end) : 0;
                                         $ticket_start = isset($price->ticket_start) ? ESS_Timezone::get_date_GMT(strtotime($price->ticket_start)) : 0;
                                         $ticket_end = isset($price->ticket_end) ? ESS_Timezone::get_date_GMT(strtotime($price->ticket_end)) : 0;
                                         $p = intval($price->ticket_price);
                                         $e_start = strtotime($event_start);
                                         $t_end = strtotime($ticket_end);
                                         $t_mode = $p > 0 ? $e_start < $t_end || $ticket_end == 0 ? 'fixed' : 'prepaid' : 'free';
                                         $newEvent->addPrice('standalone', $t_mode, "hour", NULL, NULL, NULL, NULL, array('name' => strlen($price->ticket_name) > 0 ? $price->ticket_name : sprintf(__("Ticket Nb %s", 'dbem'), $i), 'currency' => get_option('ess_feed_currency', ESS_Database::DEFAULT_CURRENCY), 'value' => intval($price->ticket_price) > 0 ? $price->ticket_price : 0, 'start' => isset($price->ticket_start) ? $ticket_start : '', 'duration' => intval($duration_s) > 0 ? $duration_s / 60 / 60 : 0, 'uri' => $event_url . "#em-booking"));
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             // end prices
             // ====== PEOPLE ===========
             $people_ = $EM_Event->get_contact();
             //var_dump( $people_ );
             if ($people_ instanceof EM_Person) {
                 $owner_name = strlen(self::_g('ess_owner_firstname')) > 0 && strlen(self::_g('ess_owner_lastname')) > 0 ? trim(self::_g('ess_owner_firstname') . " " . self::_g('ess_owner_lastname')) : $people_->data->display_name;
                 if (strlen($owner_name) > 0 && get_option('ess_owner_activate')) {
                     $newEvent->addPeople('organizer', array('name' => $owner_name, 'firstname' => self::_g('ess_owner_firstname'), 'lastname' => self::_g('ess_owner_lastname'), 'organization' => self::_g('ess_owner_company'), 'logo' => '', 'icon' => '', 'uri' => self::_g('ess_owner_website'), 'address' => self::_g('ess_owner_address'), 'city' => self::_g('ess_owner_city'), 'zip' => self::_g('ess_owner_zip'), 'state' => self::_g('ess_owner_state'), 'state_code' => '', 'country' => FeedValidator::$COUNTRIES_[strtoupper(self::_g('ess_owner_country'))], 'country_code' => strtolower(self::_g('ess_owner_country')) == 'xe' ? 'GB' : self::_g('ess_owner_country'), 'email' => self::_g('ess_owner_email'), 'phone' => self::_g('ess_owner_phone')));
                 }
                 foreach (ESS_Database::$SOCIAL_PLATFORMS as $type => $socials_) {
                     foreach ($socials_ as $social) {
                         if (FeedValidator::isValidURL(get_option('ess_social_' . $social))) {
                             $newEvent->addPeople('social', array('name' => __(ucfirst($social), 'dbem'), 'uri' => self::_g('ess_social_' . $social)));
                         }
                     }
                 }
                 if (strlen(self::_g('ess_feed_title')) > 0 && FeedValidator::isValidURL(self::_g('ess_feed_website'))) {
                     $newEvent->addPeople('author', array('name' => self::_g('ess_feed_title'), 'uri' => self::_g('ess_feed_website')));
                 }
                 if (strlen($EM_Event->post_excerpt) > 0) {
                     $newEvent->addPeople('attendee', array('name' => __('Required'), 'minpeople' => 0, 'maxpeople' => 0, 'minage' => 0, 'restriction' => esc_html($EM_Event->post_excerpt)));
                 }
             }
             // ====== MEDIA ============
             // -- IMAGES -----
             if (get_option('ess_feed_export_images')) {
                 $IMG_ = array();
                 $media_url = $EM_Event->get_image_url();
                 if (FeedValidator::isValidURL($media_url)) {
                     array_push($IMG_, array('name' => __('Main Image', 'dbem'), 'uri' => $media_url));
                 }
                 $images_ = ESS_Images::get($EM_Event->post_id);
                 if (count($images_) > 0 && $images_ != NULL) {
                     foreach ($images_ as $i => $img_) {
                         array_push($IMG_, array('name' => strlen($img_['name']) > 0 ? $img_['name'] : sprintf(__('Image %d', 'dbem'), $i), 'uri' => $img_['uri']));
                     }
                 }
                 if (count($IMG_) > 0 && $IMG_ != NULL) {
                     $IMG_ = array_map("unserialize", array_unique(array_map("serialize", $IMG_)));
                     $duplicates_ = array();
                     foreach ($IMG_ as $i => $img_) {
                         if (!in_array($img_['uri'], $duplicates_)) {
                             $newEvent->addMedia('image', array('name' => strlen($img_['name']) > 0 ? sprintf(__('Image %d', 'dbem'), $i) : $img_['name'], 'uri' => $img_['uri']));
                         }
                         array_push($duplicates_, $img_['uri']);
                     }
                 }
             }
             // -- SOUNDS -----
             if (get_option('ess_feed_export_sounds')) {
                 // TODO:...
             }
             // -- VIDEOS -----
             if (get_option('ess_feed_export_videos')) {
                 // TODO:...
             }
             // -- WEBSITES -----
             if (FeedValidator::isValidURL(self::_g('ess_feed_website'))) {
                 $newEvent->addMedia('website', array('name' => __('The website', 'dbem'), 'uri' => self::_g('ess_feed_website')));
             }
             $essFeed->addItem($newEvent);
             $count++;
         }
         // -- We've reached the limit of event per feed, or showing one event only
         if (!empty($event_id) || intval($event_id) <= 0 || get_option('ess_feed_limit', $limit) != 0 && $count > get_option('ess_feed_limit', $limit) || $count <= get_option('ess_feed_limit', $limit) && intval($page) <= 1) {
             break;
         } else {
             //-- Go to the next page of results
             $page++;
             self::output($event_id, $page, $is_download, $is_push);
         }
     }
     $essFeed->IS_DOWNLOAD = $is_download === TRUE ? TRUE : FALSE;
     $essFeed->AUTO_PUSH = get_option('ess_feed_push', TRUE) && $is_push === TRUE ? TRUE : FALSE;
     //var_dump( $essFeed );
     $essFeed->genarateFeed();
 }
 public static function create_event_from_feed(array $FEED_ = NULL, $event_id = NULL)
 {
     global $ESS_Notices, $current_site;
     $EM_Event = NULL;
     if ($FEED_ != NULL) {
         //dd( $FEED_ );
         $EM_Event = new EM_Event(intval($event_id) > 0 ? $event_id : 0);
         // set eventID for update
         // -- Populate $_POST global var for EM functions
         if (ESS_Import::set_post_from_feed($FEED_)) {
             //dd($_POST);
             if ($EM_Event->can_manage('edit_events', 'edit_recurring_events', 'edit_others_events') && $EM_Event->get_post()) {
                 // -- temporarily remove the save listener to prevent multi-pushing to search engines
                 ESS_IO::set_save_filter(FALSE);
                 $EM_Location = NULL;
                 $EM_Categories = NULL;
                 $EM_Tickets = NULL;
                 $blog_id = isset($current_site) ? $current_site->blog_id : NULL;
                 // used as global by some functions (Cf: EM_location::save())
                 if (empty($event_id)) {
                     $EM_Event->force_status = intval(get_option('ess_syndication_status')) >= 1 ? ESS_Database::EVENT_STATUS_PUBLISH : ESS_Database::EVENT_STATUS_DRAFT;
                     $EM_Event->event_status = 1;
                     $EM_Event->previous_status = 1;
                 } else {
                     $EM_Event->event_id = $event_id;
                     // -- Remove old images in case of event's update
                     if (get_option('ess_feed_import_images') && intval($EM_Event->post_id) > 0) {
                         ESS_Images::delete($EM_Event->post_id);
                     }
                 }
                 $EM_Event->post_status = strtolower($_POST['event_access']) == 'private' ? 'private' : ESS_Database::EVENT_STATUS_PUBLISH;
                 // == GENERAL
                 if (strlen($_POST['content']) > 0) {
                     if (get_option('ess_backlink_enabled')) {
                         $feed_uri = $FEED_['generals']['uri'];
                         $feed_uri_host = parse_url($feed_uri, PHP_URL_HOST);
                         $_POST['content'] .= "<h6>" . __("Source:", 'dbem') . " <a title=\"" . __("Source:", 'dbem') . " " . $feed_uri_host . "\" href=\"" . $feed_uri . "\">" . parse_url($feed_uri, PHP_URL_HOST) . "</a></h6>";
                     }
                 }
                 // == PLACE / LOCATION
                 //dd( $_POST['location_name'] );
                 if ($_POST['no_location'] === FALSE && strlen($_POST['location_name']) > 0 && get_option('dbem_locations_enabled')) {
                     $EM_Location = new EM_Location();
                     if ($EM_Location->can_manage('publish_locations') && $EM_Location->get_post(FALSE)) {
                         //d( $EM_Location );
                         // -- Search if this location already exists in the database
                         $similar_ = ESS_Import::load_similar_location(array('location_name' => $EM_Location->location_name, 'location_address' => $EM_Location->location_address, 'location_town' => $EM_Location->location_town, 'location_state' => $EM_Location->location_state, 'location_postcode' => $EM_Location->location_postcode, 'location_country' => $EM_Location->location_country));
                         //dd( $similar_ );
                         // if the location already exists use it instead.
                         if (@count($similar_) > 0 && $similar_ != NULL) {
                             foreach ($similar_ as $key => $val) {
                                 $EM_Location->{$key} = $val;
                             }
                         } else {
                             $EM_Location->post_status = 'publish';
                             $EM_Location->location_status = 1;
                             $EM_Location->post_content = '';
                         }
                         // -- Search & defines latitude / longitude if not set
                         if (FeedValidator::isValidLatitude((string) $_POST['location_latitude']) == FALSE || FeedValidator::isValidLongitude((string) $_POST['location_longitude']) == FALSE) {
                             require_once EM_ESS_DIR . "/inc/libs/geocoder/GoogleGeocode.php";
                             $geocode_ = GoogleGeocode::getGeocodeFromAddress(trim($EM_Location->location_address . " " . $EM_Location->location_town . " " . $EM_Location->location_postcode . " " . $EM_Location->location_country));
                             $lat = (string) $geocode_['results'][0]['geometry']['location']['lat'];
                             $lng = (string) $geocode_['results'][0]['geometry']['location']['lng'];
                             //echo "latitude: " .  $lat . " ==> ".((FeedValidator::isValidLatitude(  $lat ))?'TRUE':'FALSE')."<br/>";
                             //echo "longitude: " . $lng . " ==> ".((FeedValidator::isValidLongitude( $lng ))?'TRUE':'FALSE')."<br/>";
                             if (FeedValidator::isValidLatitude($lat) && FeedValidator::isValidLongitude($lng)) {
                                 $EM_Location->location_latitude = $lat;
                                 $EM_Location->location_longitude = $lng;
                             }
                         }
                         if ($EM_Location->save() === FALSE) {
                             $ESS_Notices->add_error($EM_Location->get_errors());
                         }
                         $EM_Event->location_id = $EM_Location->location_id;
                     } else {
                         $ESS_Notices->add_error($EM_Location->get_errors());
                     }
                 }
                 // end add location
                 //dd( $EM_Location );
                 // == PRICE / TICKETS
                 //dd( $_POST['em_tickets'] );
                 if (@count($_POST['em_tickets']) > 0 && get_option('dbem_rsvp_enabled')) {
                     $EM_Tickets = new EM_Tickets($EM_Event);
                     $ticket_data = NULL;
                     // Create tickets only if they doesn't exists
                     if (@count($EM_Tickets->tickets) <= 0) {
                         foreach ($_POST['em_tickets'] as $ticket_data) {
                             $EM_Ticket = new EM_Ticket();
                             $EM_Ticket->get_post($ticket_data);
                             $EM_Tickets->tickets[] = $EM_Ticket;
                         }
                     }
                     if ($ticket_data != NULL) {
                         $EM_Event->event_rsvp = TRUE;
                         $EM_Event->event_rsvp_date = $ticket_data['event_rsvp_date'];
                         $EM_Event->event_rsvp_time = $ticket_data['event_rsvp_time'];
                         $EM_Event->event_spaces = $ticket_data['event_spaces'];
                         $EM_Event->rsvp_time = $ticket_data['event_rsvp_time'];
                     }
                 }
                 // end add tickets
                 //dd( $EM_Tickets );
                 // == CATEGORIES
                 //dd( $_POST['event_categories'] );
                 if (@count($_POST['event_categories']) > 0 && get_option('dbem_categories_enabled')) {
                     $EM_Categories = new EM_Categories();
                     if ($EM_Categories->can_manage('edit_event_categories')) {
                         $caregory_ids_ = array();
                         foreach ($_POST['event_categories'] as $category_name) {
                             $category_term = get_term_by('name', $category_name, EM_TAXONOMY_CATEGORY);
                             // DEBUG: 2014-01-30
                             // Fix a internationalization bug report: http://wordpress.org/support/topic/finding-event-category-by-slug-is-not-always-a-good-idea
                             //$category_slug = sanitize_title_with_dashes( $category_name );
                             //$category_term = get_term_by( 'slug', $category_slug, EM_TAXONOMY_CATEGORY );
                             if ($category_term === FALSE) {
                                 // Term (with category taxonomy) not created yet, let's create it
                                 //$term_array = wp_insert_term( $category_name, EM_TAXONOMY_CATEGORY, array( 'slug' => $category_slug ) );
                                 $term_array = wp_insert_term($category_name, EM_TAXONOMY_CATEGORY, array('name' => $category_name));
                                 if (intval($term_array['term_id']) > 0) {
                                     array_push($caregory_ids_, intval($term_array['term_id']));
                                 }
                             } else {
                                 if (intval($category_term->term_id) > 0) {
                                     array_push($caregory_ids_, intval($category_term->term_id));
                                 }
                             }
                         }
                         $_POST['event_categories'] = $caregory_ids_;
                         if ($EM_Categories->get_post() === FALSE) {
                             $ESS_Notices->add_error($EM_Categories->get_errors());
                         }
                     } else {
                         $ESS_Notices->add_error($EM_Categories->get_errors());
                     }
                 }
                 // end add categories
                 $EM_Event->categories = $EM_Categories;
                 //dd( $EM_Categories );
                 // == TAGS
                 //dd( $_POST['event_tags'] );
                 if (@count($_POST['event_tags']) > 0 && get_option('dbem_tags_enabled')) {
                     $EM_Tags = new EM_Tags();
                     $tag_ids_ = array();
                     foreach ($_POST['event_tags'] as $tag_name) {
                         $tag_slug = sanitize_title_with_dashes($tag_name);
                         $tag_term = get_term_by('slug', $tag_slug, EM_TAXONOMY_TAG);
                         if ($tag_term === FALSE) {
                             // -- Term (with tag taxonomy) not created yet, let's create it
                             $term_array = wp_insert_term($tag_name, EM_TAXONOMY_TAG, array('slug' => $tag_slug));
                             if (intval($term_array['term_id']) > 0) {
                                 array_push($tag_ids_, intval($term_array['term_id']));
                             }
                         } else {
                             if (intval($tag_term->term_id) > 0) {
                                 array_push($tag_ids_, intval($tag_term->term_id));
                             }
                         }
                     }
                     $_POST['event_tags'] = $tag_ids_;
                     if ($EM_Tags->get_post() === FALSE) {
                         $ESS_Notices->add_error($EM_Categories->get_errors());
                     }
                 }
                 // end add tags
                 //dd( $EM_Tags );
                 // == DATE (force date values)
                 if (strlen($_POST['event_start_date']) > 0) {
                     $EM_Event->event_start_date = $_POST['event_start_date'];
                     $EM_Event->start_date = $_POST['event_start_date'];
                     $EM_Event->event_start_time = $_POST['event_start_time'];
                     $EM_Event->start_time = $_POST['event_start_time'];
                     $EM_Event->event_end_date = $_POST['event_end_date'];
                     $EM_Event->end_date = $_POST['event_end_date'];
                     $EM_Event->event_end_time = $_POST['event_end_time'];
                     $EM_Event->end_time = $_POST['event_end_time'];
                     $EM_Event->start = strtotime($EM_Event->event_start_date . " " . $EM_Event->event_start_time);
                     $EM_Event->end = strtotime($EM_Event->event_end_date . " " . $EM_Event->event_end_time);
                     if ($EM_Event->end < date('U')) {
                         $ESS_Notices->add_info(__("An event imported is already finished: " . ESS_Elements::get_ahref($FEED_['generals']['uri']), 'dbem'));
                     }
                 }
                 // end add date
                 // == PEOPLE
                 $EM_Event->post_excerpt = strlen($_POST['event_excerpt']) > 0 ? $_POST['event_excerpt'] : '';
                 // -- update the information already set line 187
                 //dd( get_option( 'ess_syndication_status' ) );
                 $EM_Event->force_status = intval(get_option('ess_syndication_status')) >= 1 ? ESS_Database::EVENT_STATUS_PUBLISH : ESS_Database::EVENT_STATUS_DRAFT;
                 //dd( $EM_Event );
                 // == SAVE EVENT ======
                 $res = $EM_Event->save();
                 //dd( $res ); // return FALSE if two of some functions are not updated in EM_Events()
                 //echo "event post id: ". $EM_Event->post_id ."<br/>";
                 //echo "event event id: ". $EM_Event->event_id."<br/>";
                 if (intval($EM_Event->post_id) > 0) {
                     // == MEDIA ==========
                     if (@count($_POST['event_media'])) {
                         //var_dump( $_POST['event_media'] );
                         $media_attachement_ = array();
                         foreach ($_POST['event_media'] as $media_) {
                             if (ESS_IO::is_file_exists($media_['uri'])) {
                                 // Use the same 'manage' value to control the importation of 'images', 'sounds' or 'videos'.
                                 if ($EM_Event->can_manage('upload_event_images')) {
                                     // == IMAGES
                                     if (FeedValidator::getMediaType($media_['uri']) == 'image' && get_option('ess_feed_import_images')) {
                                         $attachment_id = ESS_Images::add(array('uri' => $media_['uri'], 'name' => $media_['name'], 'post_id' => $EM_Event->post_id));
                                         if ($attachment_id !== FALSE && intval($attachment_id) > 0) {
                                             array_push($media_attachement_, array('attachement_id' => $attachment_id, 'uri' => $media_['uri']));
                                         } else {
                                             $ESS_Notices->add_error(__("Impossible to upload the event's image: ", 'dbem') . ESS_Elements::get_ahref($media_['uri']));
                                         }
                                     }
                                     // end add images
                                     // == VIDEOS (TODO...)
                                     if (FeedValidator::getMediaType($media_['uri']) == 'video' && get_option('ess_feed_import_videos')) {
                                         if (ESS_Videos::add(array('uri' => $media_['uri'], 'name' => $media_['name'], 'post_id' => $EM_Event->post_id)) === FALSE) {
                                             $ESS_Notices->add_error(__("Impossible to upload the event's video: ", 'dbem') . ESS_Elements::get_ahref($media_['uri']));
                                         }
                                     }
                                     // end add videos
                                     // == SOUNDS (TODO...)
                                     if (FeedValidator::getMediaType($media_['uri']) == 'sound' && get_option('ess_feed_import_sounds')) {
                                         if (ESS_Sounds::add(array('uri' => $media_['uri'], 'name' => $media_['name'], 'post_id' => $EM_Event->post_id)) === FALSE) {
                                             $ESS_Notices->add_error(__("Impossible to upload the event's audio file: ", 'dbem') . ESS_Elements::get_ahref($media_['uri']));
                                         }
                                     }
                                     // end add sounds
                                 } else {
                                     $ESS_Notices->add_error($EM_Event->get_errors());
                                 }
                             } else {
                                 $ESS_Notices->add_info(sprintf(__("A media file defined in the ESS feed is not reachable: <a href='%s' target='_blank'>%s</a>", 'dbem'), $media_['uri'], $media_['uri']));
                             }
                         }
                         // -- Define image with the highest 'priority' as first attachement
                         $priority_test = 1;
                         foreach ($_POST['event_media'] as $media_) {
                             if (get_option('ess_feed_import_images') == FALSE) {
                                 break;
                             }
                             if (@$media_['priority'] == $priority_test) {
                                 if (FeedValidator::getMediaType($media_['uri']) == 'image') {
                                     foreach ($media_attachement_ as $ma_) {
                                         if ($ma_['uri'] == $media_['uri'] && $ma_['attachement_id'] > 0) {
                                             ESS_Images::delete($EM_Event->post_id, $ma_['attachement_id']);
                                             $err = ESS_Images::add(array('uri' => $media_['uri'], 'name' => $media_['name'], 'post_id' => $EM_Event->post_id));
                                         }
                                     }
                                 } else {
                                     $priority_test++;
                                 }
                             }
                         }
                         // -- Display all the media files thumbnail at the bottom of the event's description
                         $imgs_ = ESS_Images::get_thumbnails($EM_Event->post_id);
                         //var_dump( $imgs_ );die;
                         if (@count($imgs_) > 0) {
                             $EM_Event->post_content .= "<br/><hr/>";
                             foreach ($imgs_ as $img_) {
                                 if (FeedValidator::isValidURL($img_['url'])) {
                                     $EM_Event->post_content .= "<img src='" . $img_['url'] . "' width='" . $img_['width'] . "' height='" . $img_['height'] . "' style='display:inline;margin:5px;'/>";
                                 }
                             }
                             // -- UPDATE event's description with thumbnail images at the bottom
                             $res = $EM_Event->save();
                         }
                     }
                     // end add media
                     // == TICKETS ==========
                     if (@count($EM_Tickets->tickets) > 0 && get_option('dbem_rsvp_enabled')) {
                         $EM_Tickets->blog_id = $blog_id;
                         $EM_Tickets->event_id = $EM_Event->event_id;
                         if ($EM_Tickets->save() === FALSE) {
                             $ESS_Notices->add_error($EM_Tickets->get_errors());
                         }
                     }
                     // end assign event to categories
                     // == CATEGORIES ==========
                     if (@count($EM_Categories->categories) > 0 && get_option('dbem_categories_enabled')) {
                         $EM_Categories->blog_id = $blog_id;
                         $EM_Categories->event_id = $EM_Event->event_id;
                         if ($EM_Categories->save() === FALSE) {
                             $ESS_Notices->add_error($EM_Categories->get_errors());
                         }
                     }
                     // end assign event to categories
                     // == TAGS ==========
                     if (@count($EM_Tags->tags) > 0 && get_option('dbem_tags_enabled')) {
                         //var_dump( $EM_Tags->tags );
                         $EM_Tags->blog_id = $blog_id;
                         $EM_Tags->event_id = $EM_Event->event_id;
                         // this function doesn't seem to work...
                         if ($EM_Tags->save() === FALSE) {
                             $ESS_Notices->add_error($EM_Tags->get_errors());
                         }
                         $tags_ = array();
                         foreach ($EM_Tags->tags as $EM_Tag) {
                             if (strlen($EM_Tag->slug) > 0) {
                                 array_push($tags_, $EM_Tag->slug);
                             }
                         }
                         if (@count($tags_) > 0) {
                             wp_set_object_terms($EM_Event->post_id, $tags_, EM_TAXONOMY_TAG);
                         }
                     }
                     // end assign event to tags
                     // == LOCATION ==========
                     ESS_Database::clear_locations();
                     //dd( $EM_Event );
                     ESS_IO::set_save_filter(TRUE);
                 }
             } else {
                 $ESS_Notices->add_error($EM_Event->get_errors());
             }
         }
     }
     return $EM_Event;
 }