public static function control_import_ESS()
 {
     $import_url = isset($_REQUEST['ess_feed_url']) ? urldecode(@$_REQUEST['ess_feed_url']) : '';
     $update_url = isset($_REQUEST['update_once']) ? urldecode(@$_REQUEST['update_once']) : '';
     $selected_event_id = isset($_REQUEST['selected_event_id']) ? intval(@$_REQUEST['selected_event_id']) : '';
     if ((strlen($import_url) <= 0 || $import_url == ESS_IO::HTTP) && (FeedValidator::isValidURL($update_url) == FALSE || $selected_event_id <= 0)) {
         return;
     }
     if (FeedValidator::isValidURL($update_url) && $selected_event_id > 0) {
         ESS_Import::save($update_url, @$_REQUEST['feed_mode_' . $selected_event_id]);
     } else {
         ESS_Import::save($import_url, @$_REQUEST['ess_feed_mode']);
     }
 }
 public static function update_feeds_daily()
 {
     $headers = 'MIME-Version: 1.0' . "\r\n";
     $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
     $feeds_ = ESS_Database::get(array('feed_mode' => ESS_Database::FEED_MODE_CRON));
     if (@count($feeds_) > 0 && $feeds_ != NULL) {
         foreach ($feeds_ as $feed) {
             ESS_Import::save($feed->feed_url, 'on');
             //mail( '*****@*****.**', 'DAILY count: '.@count( $feeds_ ), 'feed URL: '. $feed->feed_url . '<br><br>_POST:' . @htmlvardump( $_POST ) . '<br><br>POST_ : ' . @htmlvardump( ESS_Import::$POST_ ), $headers );
         }
     }
 }
 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;
 }