Skip to content

Commit c8906dc

Browse files
authored
Fix removing of calendar events if resource is temporarily unavailable (#789)
1 parent 50bcccb commit c8906dc

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

functions/icalimport.php

+37
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ function sunflower_icalimport( $url = false, $auto_categories = false ) {
134134
}
135135

136136
update_post_meta( $id, '_sunflower_event_uid', $uid );
137+
update_post_meta( $id, '_sunflower_event_source', md5( $url ) );
137138

138139
if ( isset( $event->LOCATION ) ) { // phpcs:ignore
139140
update_post_meta( $id, '_sunflower_event_location_name', (string) $event->LOCATION ); // phpcs:ignore
@@ -216,6 +217,29 @@ function sunflower_get_events_having_uid() {
216217
return $ids;
217218
}
218219

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+
219243
/**
220244
* Run the import job.
221245
*
@@ -236,6 +260,7 @@ function sunflower_import_icals( $force = false ) {
236260
$lines = explode( "\n", (string) sunflower_get_setting( 'sunflower_ical_urls' ) );
237261

238262
$ids_from_remote = array();
263+
$ids_from_source = array();
239264
foreach ( $lines as $line ) {
240265
$info = explode( ';', $line );
241266

@@ -246,9 +271,21 @@ function sunflower_import_icals( $force = false ) {
246271
continue;
247272
}
248273

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+
249283
$response = sunflower_icalimport( $url, $auto_categories );
250284
if ( ! empty( $response ) && is_array( $response ) && is_array( $response[0] ) ) {
251285
$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 );
252289
}
253290
}
254291

mkdocs/docs/development.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Themeentwicklung
1+
# Theme-Entwicklung
22

33
## Sprachdateien
44
- Erzeuge mit `make make-pot` ein neues Template-File

0 commit comments

Comments
 (0)