Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,33 @@

Basic import and export function for [Events Manager](https://wordpress.org/plugins/events-manager/).

## Example Import CSV

A CSV imported into Wordpress should contain the following columns:

- uid - a unique number representing this event
- summary - title of the event
- dtstart - start date and time of the event (in format matching the `dtformat` column)
- dtend - end date and time of the event (in format matching the `dtformat` column)
- dtformat - format of the `dtstart` and `dtend` columns in the [PHP date() format](http://php.net/manual/en/function.date.php)
- categories - a comma-separated list of categories that the event should be added to
- post_content - description of the event
- location_name - name of the location (optional)
- location_address - street address of the location
- location_town - city or town portion of the location address
- location_state - state portion of the address
- location_postcode - zipcode portion of the address
- location_country - country portion of the address
- location_latitude - latitude for the location (optional)
- location_longitude - longitude for the location (optional)

Below is an example row:

```
"uid","summary","dtstart","dtend","dtformat","categories","post_content","location_name","location_address","location_town","location_state","location_postcode","location_country","location_latitude","location_longitude"
"233","My Example Event","2016-06-16 11:00:00","2016-06-16 21:00:00","Y-m-d H:i:s","My Category 1,My Category 2","This is a description of the event.","The White House","1600 Pennsylvania Avenue","Washington","DC","20500","US","38.89761","-77.03673"
```

## Note from author

Although I never officially released this plugin, it seems to have leaked out and become a part of quite a few websites. I figure that means I ought to get it up and onto [GitHub](https://github.com/webaware/events-manager-import-export) where people can find it and report bugs.
Expand Down
43 changes: 29 additions & 14 deletions includes/class.EM_ImpExpImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ protected function importEventsXCal($filepath) {
// must create a new event
$event = new EM_Event();
}
$event->post_id = $data['uid']; // post_id is now NOT NULL
$event->location_id = $location ? $location->location_id : 0;
$event->event_attributes['em_impexp_uid'] = $data['uid'];
$event->event_attributes['em_impexp_url'] = $data['url'];
Expand Down Expand Up @@ -374,6 +375,7 @@ protected function importEventsCSV($filepath) {
'summary' => isset($cols['summary']) ? $cols['summary'] : '',
'dtstart' => isset($cols['dtstart']) ? $cols['dtstart'] : '',
'dtend' => isset($cols['dtend']) ? $cols['dtend'] : '',
'dtformat' => isset($cols['dtformat']) ? $cols['dtformat'] : '',
'categories' => isset($cols['categories']) ? $cols['categories'] : '',
'freq' => isset($cols['freq']) ? $cols['freq'] : '',
'byday' => isset($cols['byday']) ? $cols['byday'] : '',
Expand Down Expand Up @@ -412,6 +414,12 @@ protected function importEventsCSV($filepath) {
// try to find location by name
$location = $this->getLocationByName($data['location_name']);
}
// make sure the existing location is the same one by comparing postcodes
if ($data['location_postcode'] && $location->location_postcode != $data['location_postcode']) {
// this location has the same location_name as the one we want to create, but
// is actually a different location (e.g. City Hall in City A vs City Hall in City B)
$location = false;
}
if (!$location) {
// must create a new location object
$location = new EM_Location();
Expand Down Expand Up @@ -445,29 +453,36 @@ protected function importEventsCSV($filepath) {
// must create a new event
$event = new EM_Event();
}
$event->post_id = $data['uid']; // post_id is now NOT NULL
$event->location_id = $location ? $location->location_id : 0;
$event->event_attributes['em_impexp_uid'] = $data['uid'];
$event->event_attributes['em_impexp_url'] = $data['url'];
$event->event_name = $data['summary'];
$event->post_content = $data['post_content'];
$event->post_excerpt = $data['post_excerpt'];
if (preg_match('@^\\d\\d/\\d\\d/\\d\\d\\d\\d$@', $data['dtstart'])) {
$data['dtstart'] .= ' 00:00:00';
$event->start = date_create_from_format('d/m/Y H:i:s', $data['dtstart'])->getTimestamp();
$event->event_start_date = date('Y-m-d', $event->start);
$event->event_start_time = date('H:i:s', $event->start);
$dtformat = 'd/m/Y H:i:s';
if (isset($data['dtformat']) && !empty($data['dtformat'])) {
$dtformat = $data['dtformat'];
}
if (preg_match('@^\\d\\d/\\d\\d/\\d\\d\\d\\d$@', $data['dtend'])) {
$data['dtend'] .= ' 00:00:00';
$event->end = date_create_from_format('d/m/Y H:i:s', $data['dtend'])->getTimestamp();
$event->event_end_date = date('Y-m-d', $event->end);
$event->event_end_time = date('H:i:s', $event->end);

# parse start time
$sevent = date_create_from_format($dtformat, $data['dtstart']);
if ($sevent === FALSE) {
die("invalid start date for " . $data['summary'] . ": dtformat is $dtformat and start date is " . $data['dtstart']);
}
else {
$event->end = $event->start;
$event->event_end_date = $event->event_start_date;
$event->event_end_time = $event->event_start_time;
$event->start = $sevent->getTimestamp();
$event->event_start_date = date('Y-m-d', $event->start);
$event->event_start_time = date('H:i:s', $event->start);

# parse end time
$eevent = date_create_from_format($dtformat, $data['dtend']);
if ($eevent === FALSE) {
die("invalid end date for " . $data['summary'] . ": dtformat is $dtformat and end date is " . $data['dtend']);
}
$event->end = $eevent->getTimestamp();
$event->event_end_date = date('Y-m-d', $event->end);
$event->event_end_time = date('H:i:s', $event->end);

$event->event_date_modified = current_time('mysql');
$event->event_all_day = ($event->event_start_time === '00:00:00' && $event->event_end_time === '00:00:00') ? 1 : 0;

Expand Down