PluginProbe
ActivityPub / 9.2.1
ActivityPub v9.2.1
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / handler / outbox / class-delete.php

class-delete.php in ActivityPub 9.2.1, at includes/handler/outbox/class-delete.php

131 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Outbox Delete handler file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Handler\Outbox;
9
10 use Activitypub\Collection\Remote_Posts;
11
12 use function Activitypub\object_to_uri;
13 use function Activitypub\url_to_commentid;
14
15 /**
16 * Handle outgoing Delete activities.
17 */
18 class Delete {
19 /**
20 * Initialize the class, registering WordPress hooks.
21 */
22 public static function init() {
23 \add_filter( 'activitypub_outbox_delete', array( self::class, 'handle_delete' ), 10, 2 );
24 }
25
26 /**
27 * Handle outgoing "Delete" activities from local actors.
28 *
29 * Deletes a WordPress post or comment.
30 *
31 * @param array $data The activity data array.
32 * @param int $user_id The user ID.
33 *
34 * @return \WP_Post|\WP_Comment|false The deleted object, or false on failure.
35 */
36 public static function handle_delete( $data, $user_id = null ) {
37 $object_id = object_to_uri( $data['object'] ?? '' );
38
39 if ( empty( $object_id ) ) {
40 return false;
41 }
42
43 // Try to delete a comment first, then fall back to a post.
44 $result = self::maybe_delete_comment( $object_id, $user_id );
45
46 if ( ! $result ) {
47 $result = self::maybe_delete_post( $object_id, $user_id );
48 }
49
50 if ( $result ) {
51 /**
52 * Fires after content has been deleted via an outgoing Delete activity.
53 *
54 * @param \WP_Post|\WP_Comment $result The deleted object.
55 * @param array $data The activity data.
56 * @param int $user_id The user ID.
57 */
58 \do_action( 'activitypub_outbox_handled_delete', $result, $data, $user_id );
59 }
60
61 return $result;
62 }
63
64 /**
65 * Try to delete a comment by its ActivityPub ID.
66 *
67 * @param string $object_id The ActivityPub object ID (URL).
68 * @param int $user_id The user ID.
69 *
70 * @return \WP_Comment|false The deleted comment, or false on failure.
71 */
72 private static function maybe_delete_comment( $object_id, $user_id ) {
73 $comment_id = url_to_commentid( $object_id );
74
75 if ( ! $comment_id ) {
76 return false;
77 }
78
79 $comment = \get_comment( $comment_id );
80
81 if ( ! $comment ) {
82 return false;
83 }
84
85 // Verify the user owns this comment.
86 if ( (int) $comment->user_id !== $user_id && $user_id > 0 ) {
87 return false;
88 }
89
90 if ( \wp_trash_comment( $comment ) ) {
91 return $comment;
92 }
93
94 return false;
95 }
96
97 /**
98 * Try to delete a post by its ActivityPub ID.
99 *
100 * @param string $object_id The ActivityPub object ID (URL).
101 * @param int $user_id The user ID.
102 *
103 * @return \WP_Post|false The deleted post, or false on failure.
104 */
105 private static function maybe_delete_post( $object_id, $user_id ) {
106 // Try to find a local post by permalink.
107 $post_id = \url_to_postid( $object_id );
108 $post = $post_id ? \get_post( $post_id ) : null;
109
110 // Fall back to Posts collection for remote posts (ap_post type).
111 if ( ! $post instanceof \WP_Post ) {
112 $post = Remote_Posts::get_by_guid( $object_id );
113 }
114
115 if ( ! $post instanceof \WP_Post ) {
116 return false;
117 }
118
119 // Verify the user owns this post.
120 if ( (int) $post->post_author !== $user_id && $user_id > 0 ) {
121 return false;
122 }
123
124 if ( \wp_trash_post( $post->ID ) ) {
125 return $post;
126 }
127
128 return false;
129 }
130 }
131