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
12 changes: 11 additions & 1 deletion includes/ical/class-calendar-plus-ical-parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function parse_events() {
? $mod_calendar_tz
: $calendar_tz;

$events[] = array(
$event_post_data = array(
'post_status' => strtoupper( $_event->status ) === 'CANCELLED' ? 'trash' : 'publish',
'post_content' => $content,
'post_title' => wp_kses( $_event->summary, wp_kses_allowed_html( 'post' ) ),
Expand All @@ -188,6 +188,16 @@ public function parse_events() {
'location' => $_event->location,
'categories' => isset( $_event->categories ) ? array_map( 'trim', explode( ',', $_event->categories ) ) : array(),
);
if( ! empty( $_event->attach_array ) ) {
$event_post_data['attachment'] = array(
'url' => $_event->attach_array[1],
'mime' => $_event->attach_array[0]['FMTTYPE']
);
} else {
$event_post_data['attachment'] = array();
}

$events[] = $event_post_data;
}

return $events;
Expand Down
64 changes: 64 additions & 0 deletions includes/ical/class-calendar-plus-ical-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ protected function sync_event( $event_data ) {
'location' => '',
'last_updated' => '',
'categories' => [],
'attachment' => [],
] );

if ( 'publish' === $event_data['post_status'] ) {
Expand Down Expand Up @@ -168,9 +169,72 @@ protected function sync_event( $event_data ) {
update_post_meta( $post_id, '_ical_last_updated', $event_data['last_updated'] );
}

if( $event_data['attachment'] ) {

$current_attachment = wp_get_attachment_url( $post_id );
if ( $current_attachment !== $event_data['attachment'] ) {

$attachment_id = $this->import_attachment( $event_data['attachment'] );
if ( $attachment_id ) {

set_post_thumbnail( $post_id, $attachment_id );
}
}
}

return $post_id;
}

/**
* Stores local copy of remote attachment
*
* @param array $attachment {
* Attachment data
* @type string $url Remote file url
* @type string $mime File MIME type
* }
* @return int Attachment ID
*/
private function import_attachment( $attachment ) {

$file = wp_remote_get( $attachment['url'] );
if( ! is_wp_error( $file ) ) {

$content = wp_remote_retrieve_body( $file );
$tmp_file_name = wp_tempnam();
file_put_contents( $tmp_file_name, $content );

$url = wp_parse_url( $attachment['url'] );
$attachment_file = array(
'name' => wp_basename( $url['path'] ),
'type' => $attachment['mime'],
'size' => filesize( $tmp_file_name ),
'tmp_name' => $tmp_file_name
);

$data = wp_handle_sideload( $attachment_file, array( 'test_form' => false ) );
if ( ! isset( $data['error'] ) ) {

$attachment = wp_insert_attachment(
array(
'guid' => $data['url'],
'post_title' => $attachment_file['name'],
'post_mime_type' => $attachment['mime']
),
$data['file']
);
$image_meta_data = wp_generate_attachment_metadata( $attachment, $data['file'] );

if( $image_meta_data ) {
wp_update_attachment_metadata( $attachment, $data );
}
return $attachment;
}
}

return 0;
}

/**
* Retrieve the location post for a given location name, using an existing location if available or creating a new one if necessary
*
Expand Down