From c291c81ecc3246c8d12b574bdd390569ce427d45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Sat, 4 Jan 2025 12:27:09 +0100 Subject: [PATCH] Refactoring * Delete unused function * Modularize logic --- .../collection/class-event-sources.php | 15 ++----- .../activitypub/model/class-event-source.php | 39 ++++++++++--------- 2 files changed, 24 insertions(+), 30 deletions(-) diff --git a/includes/activitypub/collection/class-event-sources.php b/includes/activitypub/collection/class-event-sources.php index bbdf5a0..3fa0ca6 100644 --- a/includes/activitypub/collection/class-event-sources.php +++ b/includes/activitypub/collection/class-event-sources.php @@ -313,22 +313,15 @@ class Event_Sources { return; } - self::delete_events_by_event_source( $post_id); + self::delete_events_by_event_source( $post_id ); - $thumbnail_id = get_post_thumbnail_id( $post_id ); + $deleted = $event_source->delete(); - if ( $thumbnail_id ) { - wp_delete_attachment( $thumbnail_id, true ); - } - - $result = wp_delete_post( $post_id, false ); - - // If the deletion was successful delete all transients regarding event sources. - if ( $result ) { + if ( $deleted ) { self::queue_unfollow_actor( $activitypub_id ); } - return $result; + return $deleted; } /** diff --git a/includes/activitypub/model/class-event-source.php b/includes/activitypub/model/class-event-source.php index 978aeee..251a9c7 100644 --- a/includes/activitypub/model/class-event-source.php +++ b/includes/activitypub/model/class-event-source.php @@ -30,7 +30,7 @@ class Event_Source extends Actor { const ACTIVITYPUB_USER_HANDLE_REGEXP = '(?:([A-Za-z0-9_.-]+)@((?:[A-Za-z0-9_-]+\.)+[A-Za-z]+))'; /** - * The complete remote ActivityPub profile of the Event Source. + * The WordPress Post ID which stores the event source. * * @var int */ @@ -101,24 +101,6 @@ class Event_Source extends Actor { return $actor['outbox']; } - /** - * Get the WordPress post which stores the Event Source by the ActivityPub actor id of the event source. - * - * @param string $actor_id The ActivityPub actor ID. - * @return ?WP_Post The WordPress post if the actor is found, null if not. - */ - private static function get_wp_post_by_activitypub_actor_id( $actor_id ) { - global $wpdb; - $post_id = $wpdb->get_var( - $wpdb->prepare( - "SELECT ID FROM $wpdb->posts WHERE guid=%s AND post_type=%s", - esc_sql( $actor_id ), - Event_Sources::POST_TYPE - ) - ); - return $post_id ? get_post( $post_id ) : null; - } - /** * Get the Event Source by the ActivityPub ID. * @@ -296,4 +278,23 @@ class Event_Source extends Actor { return $post_id; } + + /** + * Delete an Event Source and it's profile image. + */ + public function delete() { + $post_id = $this->get__id(); + + if ( ! $post_id ) { + return false; + } + + $thumbnail_id = get_post_thumbnail_id( $post_id ); + + if ( $thumbnail_id ) { + wp_delete_attachment( $thumbnail_id, true ); + } + + return wp_delete_post( $post_id, false ) ?? false; + } }