@@ -134,6 +134,7 @@ function sunflower_icalimport( $url = false, $auto_categories = false ) {
134
134
}
135
135
136
136
update_post_meta ( $ id , '_sunflower_event_uid ' , $ uid );
137
+ update_post_meta ( $ id , '_sunflower_event_source ' , md5 ( $ url ) );
137
138
138
139
if ( isset ( $ event ->LOCATION ) ) { // phpcs:ignore
139
140
update_post_meta ( $ id , '_sunflower_event_location_name ' , (string ) $ event ->LOCATION ); // phpcs:ignore
@@ -216,6 +217,29 @@ function sunflower_get_events_having_uid() {
216
217
return $ ids ;
217
218
}
218
219
220
+ /**
221
+ * Get all post type "sunflower_event" from given source.
222
+ *
223
+ * @param int $source The hash value of the source URL.
224
+ */
225
+ function sunflower_get_event_by_source ( $ source ) {
226
+ return new WP_Query (
227
+ array (
228
+ 'nopaging ' => true ,
229
+ 'post_type ' => 'sunflower_event ' ,
230
+ 'meta_key ' => '_sunflower_event_source ' ,
231
+ 'orderby ' => 'meta_value ' ,
232
+ 'meta_query ' => array (
233
+ array (
234
+ 'key ' => '_sunflower_event_source ' ,
235
+ 'value ' => $ source ,
236
+ 'compare ' => '= ' ,
237
+ ),
238
+ ),
239
+ )
240
+ );
241
+ }
242
+
219
243
/**
220
244
* Run the import job.
221
245
*
@@ -236,6 +260,7 @@ function sunflower_import_icals( $force = false ) {
236
260
$ lines = explode ( "\n" , (string ) sunflower_get_setting ( 'sunflower_ical_urls ' ) );
237
261
238
262
$ ids_from_remote = array ();
263
+ $ ids_from_source = array ();
239
264
foreach ( $ lines as $ line ) {
240
265
$ info = explode ( '; ' , $ line );
241
266
@@ -246,9 +271,21 @@ function sunflower_import_icals( $force = false ) {
246
271
continue ;
247
272
}
248
273
274
+ // Get all already imported events of the given source.
275
+ $ events_of_source = sunflower_get_event_by_source ( md5 ( $ url ) );
276
+ $ ids_from_source = array ();
277
+
278
+ while ( $ events_of_source ->have_posts () ) {
279
+ $ events_of_source ->the_post ();
280
+ $ ids_from_source [] = get_the_ID ();
281
+ }
282
+
249
283
$ response = sunflower_icalimport ( $ url , $ auto_categories );
250
284
if ( ! empty ( $ response ) && is_array ( $ response ) && is_array ( $ response [0 ] ) ) {
251
285
$ ids_from_remote = array_merge ( $ ids_from_remote , $ response [0 ] );
286
+ } else {
287
+ // Keep known events as the response might only be temporarily unavailable.
288
+ $ ids_from_remote = array_merge ( $ ids_from_remote , $ ids_from_source );
252
289
}
253
290
}
254
291
0 commit comments