diff --git a/includes/helpers/helpers-ical.php b/includes/helpers/helpers-ical.php index 3cb0ca1..e8feed3 100644 --- a/includes/helpers/helpers-ical.php +++ b/includes/helpers/helpers-ical.php @@ -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', @@ -158,7 +162,6 @@ function calendarp_ical_sync_events() { $event_data['to'] = strtotime( $to[0]['data'] ); } } - $events[] = $event_data; } diff --git a/includes/ical/class-calendar-plus-ical-sync.php b/includes/ical/class-calendar-plus-ical-sync.php index 5e29589..68a83a8 100644 --- a/includes/ical/class-calendar-plus-ical-sync.php +++ b/includes/ical/class-calendar-plus-ical-sync.php @@ -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. * @@ -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']; } /** @@ -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; @@ -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; } @@ -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; }