Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix removing of calendar events if resource is temporarily unavailable #789

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading