Skip to content

Commit

Permalink
Fix removing of calendar events if resource is temporarily unavailable (
Browse files Browse the repository at this point in the history
  • Loading branch information
albig authored Dec 6, 2024
1 parent 50bcccb commit c8906dc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
37 changes: 37 additions & 0 deletions functions/icalimport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand All @@ -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 );

Expand All @@ -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 );
}
}

Expand Down
2 changes: 1 addition & 1 deletion mkdocs/docs/development.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Themeentwicklung
# Theme-Entwicklung

## Sprachdateien
- Erzeuge mit `make make-pot` ein neues Template-File
Expand Down

0 comments on commit c8906dc

Please sign in to comment.