From c8906dce8f5ab5ca85182ff239900ce178cc15b8 Mon Sep 17 00:00:00 2001 From: Alexander Bigga Date: Fri, 6 Dec 2024 12:58:59 +0100 Subject: [PATCH] Fix removing of calendar events if resource is temporarily unavailable (#789) --- functions/icalimport.php | 37 +++++++++++++++++++++++++++++++++++++ mkdocs/docs/development.md | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/functions/icalimport.php b/functions/icalimport.php index dd7772d1..f0cb7123 100644 --- a/functions/icalimport.php +++ b/functions/icalimport.php @@ -134,6 +134,7 @@ function sunflower_icalimport( $url = false, $auto_categories = false ) { } update_post_meta( $id, '_sunflower_event_uid', $uid ); + update_post_meta( $id, '_sunflower_event_source', md5( $url ) ); if ( isset( $event->LOCATION ) ) { // phpcs:ignore update_post_meta( $id, '_sunflower_event_location_name', (string) $event->LOCATION ); // phpcs:ignore @@ -216,6 +217,29 @@ function sunflower_get_events_having_uid() { return $ids; } +/** + * Get all post type "sunflower_event" from given source. + * + * @param int $source The hash value of the source URL. + */ +function sunflower_get_event_by_source( $source ) { + return new WP_Query( + array( + 'nopaging' => true, + 'post_type' => 'sunflower_event', + 'meta_key' => '_sunflower_event_source', + 'orderby' => 'meta_value', + 'meta_query' => array( + array( + 'key' => '_sunflower_event_source', + 'value' => $source, + 'compare' => '=', + ), + ), + ) + ); +} + /** * Run the import job. * @@ -236,6 +260,7 @@ function sunflower_import_icals( $force = false ) { $lines = explode( "\n", (string) sunflower_get_setting( 'sunflower_ical_urls' ) ); $ids_from_remote = array(); + $ids_from_source = array(); foreach ( $lines as $line ) { $info = explode( ';', $line ); @@ -246,9 +271,21 @@ function sunflower_import_icals( $force = false ) { continue; } + // Get all already imported events of the given source. + $events_of_source = sunflower_get_event_by_source( md5( $url ) ); + $ids_from_source = array(); + + while ( $events_of_source->have_posts() ) { + $events_of_source->the_post(); + $ids_from_source[] = get_the_ID(); + } + $response = sunflower_icalimport( $url, $auto_categories ); if ( ! empty( $response ) && is_array( $response ) && is_array( $response[0] ) ) { $ids_from_remote = array_merge( $ids_from_remote, $response[0] ); + } else { + // Keep known events as the response might only be temporarily unavailable. + $ids_from_remote = array_merge( $ids_from_remote, $ids_from_source ); } } diff --git a/mkdocs/docs/development.md b/mkdocs/docs/development.md index 09081fa6..02ef63c7 100644 --- a/mkdocs/docs/development.md +++ b/mkdocs/docs/development.md @@ -1,4 +1,4 @@ -# Themeentwicklung +# Theme-Entwicklung ## Sprachdateien - Erzeuge mit `make make-pot` ein neues Template-File