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
5 changes: 4 additions & 1 deletion includes/helpers/helpers-ical.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ function calendarp_ical_sync_events() {
$feed['type'] = $feeds[ $i ]['type'] = 'ical';
}

if ( isset( $feed['last_sync'] ) ) {
$feeds[ $i ]['prev_sync_time'] = $feed['last_sync']['time'];
}

$feeds[ $i ]['last_sync'] = array(
'time' => current_time( 'timestamp' ),
'status' => 'incomplete',
Expand Down Expand Up @@ -158,7 +162,6 @@ function calendarp_ical_sync_events() {
$event_data['to'] = strtotime( $to[0]['data'] );
}
}

$events[] = $event_data;
}

Expand Down
36 changes: 32 additions & 4 deletions includes/ical/class-calendar-plus-ical-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class Calendar_Plus_iCal_Sync {
*/
protected $default_status;

/**
* Time of previous synchronization to check if post was modified
* @var int
*/
protected $prev_sync_time;

/**
* Calendar_Plus_iCal_Sync constructor.
*
Expand All @@ -47,14 +53,16 @@ public function __construct( $events, $args = [] ) {
$this->locations = calendarp_get_locations();

$args = wp_parse_args( $args, [
'author' => 0,
'category' => 0,
'status' => 'publish',
'author' => 0,
'category' => 0,
'status' => 'publish',
'prev_sync_time' => time(),
] );

$this->event_author = $args['author'] ? $args['author'] : get_current_user_id();
$this->event_category = $args['category'];
$this->default_status = $args['status'];
$this->prev_sync_time = $args['prev_sync_time'];
}

/**
Expand Down Expand Up @@ -94,6 +102,7 @@ protected function sync_event( $event_data ) {
'last_updated' => '',
'categories' => [],
] );
$event_data['hash'] = md5( serialize( $event_data ) );

if ( 'publish' === $event_data['post_status'] ) {
$event_data['post_status'] = $this->default_status;
Expand All @@ -115,7 +124,24 @@ protected function sync_event( $event_data ) {
return $event->ID;
}

if ( ! $event_data['last_updated'] || $event_data['last_updated'] === $event->get_meta( 'ical_last_updated' ) ) {
$event_post = $event->get_post();
// Skip if there are no changes
if (
! empty( $event_data['last_updated'] ) &&
$event_data['last_updated'] === $event->get_meta( 'ical_last_updated' )
) {
return false;
}
// Skip if there are no changes
$old_event_hash = $event->get_meta( 'ical_hash' );
if ( $event_data['hash'] === $event->get_meta( 'ical_hash' ) ) {
return false;
}

if (
empty( $old_event_hash ) &&
$event_post->post_modified !== $event_post->post_date
) {
return false;
}

Expand Down Expand Up @@ -168,6 +194,8 @@ protected function sync_event( $event_data ) {
update_post_meta( $post_id, '_ical_last_updated', $event_data['last_updated'] );
}

update_post_meta( $post_id, '_ical_hash', $event_data['hash'] );

return $post_id;
}

Expand Down