/**
  * Used by createEvent and updateEvent - saves all the various event meta
  *
  * @param int   $event_id The event ID we are modifying meta for.
  * @param array $data     The meta fields we want saved.
  * @param       WP_Post   The event itself.
  *
  * @return void
  */
 public static function saveEventMeta($event_id, $data, $event = null)
 {
     if (isset($data['EventAllDay']) && ($data['EventAllDay'] == 'yes' || $data['EventAllDay'] == true || !isset($data['EventStartDate']))) {
         $data['EventStartDate'] = tribe_event_beginning_of_day($data['EventStartDate']);
         $data['EventEndDate'] = tribe_event_end_of_day($data['EventEndDate']);
     } else {
         delete_post_meta($event_id, '_EventAllDay');
         if (isset($data['EventStartMeridian'])) {
             $data['EventStartDate'] = date(TribeDateUtils::DBDATETIMEFORMAT, strtotime($data['EventStartDate'] . " " . $data['EventStartHour'] . ":" . $data['EventStartMinute'] . ":00 " . $data['EventStartMeridian']));
             $data['EventEndDate'] = date(TribeDateUtils::DBDATETIMEFORMAT, strtotime($data['EventEndDate'] . " " . $data['EventEndHour'] . ":" . $data['EventEndMinute'] . ":00 " . $data['EventEndMeridian']));
         } else {
             $data['EventStartDate'] = date(TribeDateUtils::DBDATETIMEFORMAT, strtotime($data['EventStartDate'] . " " . $data['EventStartHour'] . ":" . $data['EventStartMinute'] . ":00"));
             $data['EventEndDate'] = date(TribeDateUtils::DBDATETIMEFORMAT, strtotime($data['EventEndDate'] . " " . $data['EventEndHour'] . ":" . $data['EventEndMinute'] . ":00"));
         }
     }
     if (empty($data['EventHideFromUpcoming'])) {
         delete_post_meta($event_id, '_EventHideFromUpcoming');
     }
     // sanity check that start date < end date
     $startTimestamp = strtotime($data['EventStartDate']);
     $endTimestamp = strtotime($data['EventEndDate']);
     if ($startTimestamp > $endTimestamp) {
         $data['EventEndDate'] = $data['EventStartDate'];
     }
     $data['EventDuration'] = strtotime($data['EventEndDate']) - $startTimestamp;
     update_post_meta($event_id, '_EventShowMapLink', isset($data['venue']['EventShowMapLink']));
     update_post_meta($event_id, '_EventShowMap', isset($data['venue']['EventShowMap']));
     if (isset($data['post_status'])) {
         $post_status = $data['post_status'];
     } else {
         $post_status = get_post_status($event_id);
     }
     if (isset($data["Organizer"])) {
         if (!empty($data["Organizer"]["OrganizerID"])) {
             $organizer_post_status = get_post($data["Organizer"]['OrganizerID'])->post_status;
         } else {
             $organizer_post_status = $post_status;
         }
         $data['EventOrganizerID'] = TribeEventsAPI::saveEventOrganizer($data["Organizer"], $event, $organizer_post_status);
     }
     if (isset($data["Venue"])) {
         if (!empty($data['Venue']["VenueID"])) {
             $venue_post_status = get_post($data['Venue']['VenueID'])->post_status;
         } else {
             $venue_post_status = $post_status;
         }
         $data['EventVenueID'] = TribeEventsAPI::saveEventVenue($data["Venue"], $event, $venue_post_status);
     }
     $cost = isset($data['EventCost']) ? $data['EventCost'] : '';
     $data['EventCost'] = $cost;
     do_action('tribe_events_event_save', $event_id);
     $cost = isset($data['EventCost']) ? $data['EventCost'] : '';
     $data['EventCost'] = $cost;
     //update meta fields
     foreach (TribeEvents::instance()->metaTags as $tag) {
         $htmlElement = ltrim($tag, '_');
         if (isset($data[$htmlElement]) && $tag != TribeEvents::EVENTSERROROPT) {
             if (is_string($data[$htmlElement])) {
                 $data[$htmlElement] = filter_var($data[$htmlElement], FILTER_SANITIZE_STRING);
             }
             update_post_meta($event_id, $tag, $data[$htmlElement]);
         }
     }
     // Set sticky state for calendar view.
     if (isset($data['EventShowInCalendar']) && $data['EventShowInCalendar'] == 'yes' && $event->menu_order != '-1') {
         $update_event = array('ID' => $event_id, 'menu_order' => '-1');
         wp_update_post($update_event);
     } elseif ((!isset($data['EventShowInCalendar']) || $data['EventShowInCalendar'] != 'yes') && $event->menu_order == '-1') {
         $update_event = array('ID' => $event_id, 'menu_order' => '0');
         wp_update_post($update_event);
     }
     do_action('tribe_events_update_meta', $event_id, $data);
 }
 /**
  * Used by createEvent and updateEvent - saves all the various event meta 
  */
 public static function saveEventMeta($event_id, $data, $event = null)
 {
     $tribe_ecp = TribeEvents::instance();
     if (isset($data['EventAllDay']) && ($data['EventAllDay'] == 'yes' || $data['EventAllDay'] == true || !isset($data['EventStartDate']))) {
         $data['EventStartDate'] = TribeDateUtils::beginningOfDay($data['EventStartDate']);
         $data['EventEndDate'] = TribeDateUtils::endOfDay($data['EventEndDate']);
     } else {
         delete_post_meta($event_id, '_EventAllDay');
         if (isset($data['EventStartMeridian'])) {
             $data['EventStartDate'] = date(TribeDateUtils::DBDATETIMEFORMAT, strtotime($data['EventStartDate'] . " " . $data['EventStartHour'] . ":" . $data['EventStartMinute'] . ":00 " . $data['EventStartMeridian']));
             $data['EventEndDate'] = date(TribeDateUtils::DBDATETIMEFORMAT, strtotime($data['EventEndDate'] . " " . $data['EventEndHour'] . ":" . $data['EventEndMinute'] . ":00 " . $data['EventEndMeridian']));
         } else {
             $data['EventStartDate'] = date(TribeDateUtils::DBDATETIMEFORMAT, strtotime($data['EventStartDate'] . " " . $data['EventStartHour'] . ":" . $data['EventStartMinute'] . ":00"));
             $data['EventEndDate'] = date(TribeDateUtils::DBDATETIMEFORMAT, strtotime($data['EventEndDate'] . " " . $data['EventEndHour'] . ":" . $data['EventEndMinute'] . ":00"));
         }
     }
     if (!isset($data['EventHideFromUpcoming']) || !$data['EventHideFromUpcoming']) {
         delete_post_meta($event_id, '_EventHideFromUpcoming');
     }
     // sanity check that start date < end date
     $startTimestamp = strtotime($data['EventStartDate']);
     $endTimestamp = strtotime($data['EventEndDate']);
     if ($startTimestamp > $endTimestamp) {
         $data['EventEndDate'] = $data['EventStartDate'];
     }
     if (!isset($data['EventShowMapLink'])) {
         update_post_meta($event_id, '_EventShowMapLink', 'false');
     }
     if (!isset($data['EventShowMap'])) {
         update_post_meta($event_id, '_EventShowMap', 'false');
     }
     if (isset($data['post_status'])) {
         $post_status = $data['post_status'];
     } else {
         //print_r($data);
         if (isset($data["Organizer"]["OrganizerID"])) {
             $post_status = get_post($data["Organizer"]['OrganizerID'])->post_status;
         }
         if (isset($data['Venue']["VenueID"])) {
             $post_status = get_post($data['Venue']['VenueID'])->post_status;
         }
     }
     if (isset($data["Organizer"])) {
         $data['EventOrganizerID'] = TribeEventsAPI::saveEventOrganizer($data["Organizer"], $event, $post_status);
     }
     if (isset($data["Venue"])) {
         $data['EventVenueID'] = TribeEventsAPI::saveEventVenue($data["Venue"], $event, $post_status);
     }
     $tribe_ecp->do_action('tribe_events_event_save', $event_id);
     //update meta fields
     foreach ($tribe_ecp->metaTags as $tag) {
         $htmlElement = ltrim($tag, '_');
         if (isset($data[$htmlElement]) && $tag != TribeEvents::EVENTSERROROPT) {
             if (is_string($data[$htmlElement])) {
                 $data[$htmlElement] = filter_var($data[$htmlElement], FILTER_SANITIZE_STRING);
             }
             update_post_meta($event_id, $tag, $data[$htmlElement]);
         }
     }
     $tribe_ecp->do_action('tribe_events_update_meta', $event_id, false, $data, $event);
 }
 /**
  * Used by createEvent and updateEvent - saves all the various event meta
  *
  * @param int   $event_id The event ID we are modifying meta for.
  * @param array $data     The meta fields we want saved.
  * @param       WP_Post   The event itself.
  *
  * @return void
  */
 public static function saveEventMeta($event_id, $data, $event = null)
 {
     $tec = TribeEvents::instance();
     if (isset($data['EventAllDay']) && ($data['EventAllDay'] == 'yes' || $data['EventAllDay'] == true || !isset($data['EventStartDate']))) {
         $data['EventStartDate'] = tribe_event_beginning_of_day($data['EventStartDate']);
         $data['EventEndDate'] = tribe_event_end_of_day($data['EventEndDate']);
     } else {
         delete_post_meta($event_id, '_EventAllDay');
         if (isset($data['EventStartMeridian'])) {
             $data['EventStartDate'] = date(TribeDateUtils::DBDATETIMEFORMAT, strtotime($data['EventStartDate'] . " " . $data['EventStartHour'] . ":" . $data['EventStartMinute'] . ":00 " . $data['EventStartMeridian']));
             $data['EventEndDate'] = date(TribeDateUtils::DBDATETIMEFORMAT, strtotime($data['EventEndDate'] . " " . $data['EventEndHour'] . ":" . $data['EventEndMinute'] . ":00 " . $data['EventEndMeridian']));
         } else {
             $data['EventStartDate'] = date(TribeDateUtils::DBDATETIMEFORMAT, strtotime($data['EventStartDate'] . " " . $data['EventStartHour'] . ":" . $data['EventStartMinute'] . ":00"));
             $data['EventEndDate'] = date(TribeDateUtils::DBDATETIMEFORMAT, strtotime($data['EventEndDate'] . " " . $data['EventEndHour'] . ":" . $data['EventEndMinute'] . ":00"));
         }
     }
     if (empty($data['EventHideFromUpcoming'])) {
         delete_post_meta($event_id, '_EventHideFromUpcoming');
     }
     // sanity check that start date < end date
     $startTimestamp = strtotime($data['EventStartDate']);
     $endTimestamp = strtotime($data['EventEndDate']);
     if ($startTimestamp > $endTimestamp) {
         $data['EventEndDate'] = $data['EventStartDate'];
     }
     $data['EventDuration'] = strtotime($data['EventEndDate']) - $startTimestamp;
     update_post_meta($event_id, '_EventShowMapLink', isset($data['venue']['EventShowMapLink']));
     update_post_meta($event_id, '_EventShowMap', isset($data['venue']['EventShowMap']));
     if (isset($data['post_status'])) {
         $post_status = $data['post_status'];
     } else {
         $post_status = get_post_status($event_id);
     }
     if (isset($data["Organizer"])) {
         if (!empty($data["Organizer"]["OrganizerID"])) {
             $organizer_post_status = get_post($data["Organizer"]['OrganizerID'])->post_status;
         } else {
             $organizer_post_status = $post_status;
         }
         $data['EventOrganizerID'] = TribeEventsAPI::saveEventOrganizer($data["Organizer"], $event, $organizer_post_status);
     }
     if (isset($data["Venue"])) {
         if (!empty($data['Venue']["VenueID"])) {
             $venue_post_status = get_post($data['Venue']['VenueID'])->post_status;
         } else {
             $venue_post_status = $post_status;
         }
         $data['EventVenueID'] = TribeEventsAPI::saveEventVenue($data["Venue"], $event, $venue_post_status);
     }
     // Ordinarily there is a single cost value for each event, but addons (ie, ticketing plugins) may need
     // to record a number of different pricepoints for the same event
     $event_cost = isset($data['EventCost']) ? (array) $data['EventCost'] : array();
     $data['EventCost'] = (array) apply_filters('tribe_events_event_costs', $event_cost, $event_id);
     do_action('tribe_events_event_save', $event_id);
     //update meta fields
     foreach ($tec->metaTags as $tag) {
         $htmlElement = ltrim($tag, '_');
         if (isset($data[$htmlElement]) && $tag != TribeEvents::EVENTSERROROPT) {
             if (is_string($data[$htmlElement])) {
                 $data[$htmlElement] = filter_var($data[$htmlElement], FILTER_SANITIZE_STRING);
             }
             // Fields with multiple values per key
             if (is_array($data[$htmlElement])) {
                 delete_post_meta($event_id, $tag);
                 foreach ($data[$htmlElement] as $value) {
                     add_post_meta($event_id, $tag, $value);
                 }
             } else {
                 update_post_meta($event_id, $tag, $data[$htmlElement]);
             }
         }
     }
     // Set sticky state for calendar view.
     if (isset($data['EventShowInCalendar']) && $data['EventShowInCalendar'] == 'yes' && $event->menu_order != '-1') {
         $update_event = array('ID' => $event_id, 'menu_order' => '-1');
         wp_update_post($update_event);
     } elseif ((!isset($data['EventShowInCalendar']) || $data['EventShowInCalendar'] != 'yes') && $event->menu_order == '-1') {
         $update_event = array('ID' => $event_id, 'menu_order' => '0');
         wp_update_post($update_event);
     }
     do_action('tribe_events_update_meta', $event_id, $data);
 }