Add function to delete all cached event of an event source
All checks were successful
PHP Code Checker / PHP Code Checker (pull_request) Successful in 47s
PHPUnit / PHPUnit – PHP 7.4 (pull_request) Successful in 1m4s
PHPUnit / PHPUnit – PHP 8.0 (pull_request) Successful in 1m6s
PHPUnit / PHPUnit – PHP 8.1 (pull_request) Successful in 1m3s
PHPUnit / PHPUnit – PHP 8.2 (pull_request) Successful in 1m5s
PHPUnit / PHPUnit – PHP 8.3 (pull_request) Successful in 1m8s
PHPUnit / PHPUnit – PHP 8.4 (pull_request) Successful in 1m5s

This commit is contained in:
André Menrath 2025-01-02 20:04:16 +01:00
parent 5f41ccdc42
commit d603cea442

View file

@ -208,6 +208,53 @@ class Event_Sources {
delete_transient( 'event_bridge_for_activitypub_event_sources_ids' );
}
/**
* Delete all posts of an event source.
*
* @param string $event_source_id The ActivityPub ID of the event source.
* @return void
*/
public static function delete_events_by_event_source( $event_source_id ) {
$args = array(
'author' => 0, // Currently we do not use a special user or special users.
'posts_per_page' => -1, // Retrieve all matching posts.
'meta_key' => '_event_bridge_for_activitypub_event_source',
'meta_query' => array(
array(
'key' => '_event_bridge_for_activitypub_event_source',
'value' => $event_source_id,
'compare' => '=',
),
),
);
$query = new \WP_Query( $args );
// If no matching posts are found, return early.
if ( ! $query->have_posts() ) {
return;
}
// Loop through the posts and delete them permanently.
foreach ( $query->posts as $post ) {
// Check if the post has a thumbnail.
$thumbnail_id = get_post_thumbnail_id( $post->ID );
if ( $thumbnail_id ) {
// Remove the thumbnail from the post.
delete_post_thumbnail( $post->ID );
// Delete the attachment (and its files) from the media library.
wp_delete_attachment( $thumbnail_id, true );
}
\wp_delete_post( $post->ID, true );
}
// Clean up the query.
\wp_reset_postdata();
}
/**
* Remove an Event Source (=Followed ActivityPub actor).
*
@ -228,13 +275,15 @@ class Event_Sources {
return;
}
self::delete_events_by_event_source( $actor->get_id() );
$thumbnail_id = get_post_thumbnail_id( $post_id );
if ( $thumbnail_id ) {
wp_delete_attachment( $thumbnail_id, true );
}
$result = wp_delete_post( $post_id, true );
$result = wp_delete_post( $post_id, false );
// If the deletion was successful delete all transients regarding event sources.
if ( $result ) {