2024-12-25 22:54:43 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Undo handler file.
|
|
|
|
*
|
|
|
|
* @package Event_Bridge_For_ActivityPub
|
|
|
|
* @since 1.0.0
|
|
|
|
* @license AGPL-3.0-or-later */
|
|
|
|
|
|
|
|
namespace Event_Bridge_For_ActivityPub\ActivityPub\Handler;
|
|
|
|
|
|
|
|
use Activitypub\Collection\Actors;
|
|
|
|
use Event_Bridge_For_ActivityPub\Event_Sources;
|
|
|
|
use Event_Bridge_For_ActivityPub\ActivityPub\Collection\Event_Sources as Event_Sources_Collection;
|
|
|
|
|
|
|
|
use function Activitypub\object_to_uri;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle Uno requests.
|
|
|
|
*/
|
|
|
|
class Undo {
|
|
|
|
/**
|
|
|
|
* Initialize the class, registering the handler for incoming `Uno` activities to the ActivityPub plugin.
|
|
|
|
*/
|
|
|
|
public static function init() {
|
|
|
|
\add_action(
|
|
|
|
'activitypub_inbox_undo',
|
|
|
|
array( self::class, 'handle_undo' ),
|
|
|
|
15,
|
|
|
|
2
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle incoming "Undo" activities.
|
|
|
|
*
|
|
|
|
* @param array $activity The activity-object.
|
|
|
|
* @param int $user_id The id of the local blog-user.
|
|
|
|
*/
|
|
|
|
public static function handle_undo( $activity, $user_id ) {
|
|
|
|
// We only process activities that are target to the blog actor.
|
|
|
|
if ( Actors::BLOG_USER_ID !== $user_id ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-01-04 10:01:14 +01:00
|
|
|
// Check that we are actually following/or have a pending follow request for this actor.
|
2024-12-25 22:54:43 +01:00
|
|
|
if ( ! Event_Sources::actor_is_event_source( $activity['actor'] ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-01-04 12:17:58 +01:00
|
|
|
$accept_id = object_to_uri( $activity['object'] );
|
2024-12-25 22:54:43 +01:00
|
|
|
|
2025-01-04 12:17:58 +01:00
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$results = $wpdb->get_results(
|
|
|
|
$wpdb->prepare(
|
|
|
|
"SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s",
|
|
|
|
'_event_bridge_for_activitypub_accept_of_follow',
|
|
|
|
esc_sql( $accept_id )
|
|
|
|
)
|
2024-12-25 22:54:43 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// If no event source with that accept ID is found return.
|
2025-01-04 12:17:58 +01:00
|
|
|
if ( empty( $results ) || ! $results ) {
|
2024-12-25 22:54:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-01-04 12:17:58 +01:00
|
|
|
$post_id = reset( $results )->post_id;
|
2024-12-25 22:54:43 +01:00
|
|
|
|
|
|
|
\delete_post_meta( $post_id, '_event_bridge_for_activitypub_accept_of_follow' );
|
|
|
|
}
|
|
|
|
}
|