PluginProbe
ActivityPub / 9.0.2
ActivityPub v9.0.2
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 / rest / admin / class-actions-controller.php

class-actions-controller.php in ActivityPub 9.0.2, at includes/rest/admin/class-actions-controller.php

254 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Admin Actions REST Controller
4 *
5 * Handles administrative actions for followers/actors management.
6 *
7 * @package Activitypub
8 */
9
10 namespace Activitypub\Rest\Admin;
11
12 use Activitypub\Collection\Followers;
13 use Activitypub\Collection\Following;
14 use Activitypub\Collection\Remote_Actors;
15 use Activitypub\Moderation;
16
17 use function Activitypub\user_can_activitypub;
18
19 /**
20 * Admin Actions REST Controller Class.
21 */
22 class Actions_Controller extends \WP_REST_Controller {
23 /**
24 * The namespace of this controller's route.
25 *
26 * @var string
27 */
28 protected $namespace = ACTIVITYPUB_REST_NAMESPACE;
29
30 /**
31 * The base of this controller's route.
32 *
33 * @var string
34 */
35 protected $rest_base = 'admin/actors';
36
37 /**
38 * Register routes.
39 */
40 public function register_routes() {
41 // Delete follower relationship.
42 \register_rest_route(
43 $this->namespace,
44 '/' . $this->rest_base . '/(?P<id>[\d]+)/unfollow',
45 array(
46 'args' => array(
47 'id' => array(
48 'description' => 'The ID of the actor.',
49 'type' => 'integer',
50 'required' => true,
51 'validate_callback' => array( $this, 'validate_actor_id' ),
52 ),
53 ),
54 array(
55 'methods' => \WP_REST_Server::DELETABLE,
56 'callback' => array( $this, 'unfollow_actor' ),
57 'permission_callback' => array( $this, 'check_permission' ),
58 'show_in_index' => false,
59 ),
60 )
61 );
62
63 // Block actor.
64 \register_rest_route(
65 $this->namespace,
66 '/' . $this->rest_base . '/(?P<id>[\d]+)/block',
67 array(
68 'args' => array(
69 'id' => array(
70 'description' => 'The ID of the actor.',
71 'type' => 'integer',
72 'required' => true,
73 'validate_callback' => array( $this, 'validate_actor_id' ),
74 ),
75 ),
76 array(
77 'methods' => \WP_REST_Server::CREATABLE,
78 'callback' => array( $this, 'block_actor' ),
79 'permission_callback' => array( $this, 'check_permission' ),
80 'show_in_index' => false,
81 'args' => array(
82 'site_wide' => array(
83 'description' => 'Whether to block site-wide (admin only).',
84 'type' => 'boolean',
85 'default' => false,
86 ),
87 ),
88 ),
89 )
90 );
91
92 // Follow actor.
93 \register_rest_route(
94 $this->namespace,
95 '/' . $this->rest_base . '/(?P<id>[\d]+)/follow',
96 array(
97 'args' => array(
98 'id' => array(
99 'description' => 'The ID of the actor.',
100 'type' => 'integer',
101 'required' => true,
102 'validate_callback' => array( $this, 'validate_actor_id' ),
103 ),
104 ),
105 array(
106 'methods' => \WP_REST_Server::CREATABLE,
107 'callback' => array( $this, 'follow_actor' ),
108 'permission_callback' => array( $this, 'check_permission' ),
109 'show_in_index' => false,
110 ),
111 )
112 );
113 }
114
115 /**
116 * Check if the current user has permission to perform actions.
117 *
118 * @return bool|\WP_Error True if the request has permission, WP_Error object otherwise.
119 */
120 public function check_permission() {
121 if ( ! user_can_activitypub( \get_current_user_id() ) ) {
122 return new \WP_Error(
123 'rest_forbidden',
124 \__( 'Sorry, you are not allowed to perform this action.', 'activitypub' ),
125 array( 'status' => 403 )
126 );
127 }
128
129 return true;
130 }
131
132 /**
133 * Validate actor ID.
134 *
135 * @param int $value The actor ID.
136 * @return bool True if valid, false otherwise.
137 */
138 public function validate_actor_id( $value ) {
139 $actor = \get_post( $value );
140
141 return $actor instanceof \WP_Post && Remote_Actors::POST_TYPE === $actor->post_type;
142 }
143
144 /**
145 * Remove follower relationship.
146 *
147 * @param \WP_REST_Request $request Full data about the request.
148 * @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure.
149 */
150 public function unfollow_actor( $request ) {
151 $actor_id = $request->get_param( 'id' );
152 $user_id = \get_current_user_id();
153
154 $result = Followers::remove( $actor_id, $user_id );
155
156 if ( ! $result ) {
157 return new \WP_Error(
158 'rest_follower_removal_failed',
159 \__( 'Failed to remove follower.', 'activitypub' ),
160 array( 'status' => 500 )
161 );
162 }
163
164 return new \WP_REST_Response(
165 array(
166 'success' => true,
167 'message' => \__( 'Follower removed successfully.', 'activitypub' ),
168 ),
169 200
170 );
171 }
172
173 /**
174 * Block an actor.
175 *
176 * @param \WP_REST_Request $request Full data about the request.
177 * @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure.
178 */
179 public function block_actor( $request ) {
180 $actor_id = $request->get_param( 'id' );
181 $site_wide = $request->get_param( 'site_wide' );
182 $user_id = \get_current_user_id();
183
184 $actor = Remote_Actors::get_actor( $actor_id );
185 if ( \is_wp_error( $actor ) ) {
186 return $actor;
187 }
188
189 $actor_url = $actor->get_id();
190
191 // Add user-specific block.
192 $user_block_success = Moderation::add_user_block( $user_id, 'actor', $actor_url );
193
194 // Add site-wide block if requested and user has permission.
195 $site_block_success = true;
196 if ( $site_wide && \current_user_can( 'manage_options' ) ) {
197 $site_block_success = Moderation::add_site_block( 'actor', $actor_url );
198 }
199
200 if ( ! $user_block_success || ! $site_block_success ) {
201 return new \WP_Error(
202 'rest_actor_block_failed',
203 \__( 'Failed to block actor.', 'activitypub' ),
204 array( 'status' => 500 )
205 );
206 }
207
208 // Remove follower relationship after blocking.
209 Followers::remove( $actor_id, $user_id );
210
211 return new \WP_REST_Response(
212 array(
213 'success' => true,
214 'message' => \__( 'Actor blocked successfully.', 'activitypub' ),
215 ),
216 200
217 );
218 }
219
220 /**
221 * Follow an actor.
222 *
223 * @param \WP_REST_Request $request Full data about the request.
224 * @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure.
225 */
226 public function follow_actor( $request ) {
227 // Check if following UI is enabled.
228 if ( '1' !== \get_option( 'activitypub_following_ui', '0' ) ) {
229 return new \WP_Error(
230 'rest_following_disabled',
231 \__( 'Following feature is disabled.', 'activitypub' ),
232 array( 'status' => 403 )
233 );
234 }
235
236 $actor_id = $request->get_param( 'id' );
237 $user_id = \get_current_user_id();
238
239 $result = Following::follow( $actor_id, $user_id );
240
241 if ( \is_wp_error( $result ) ) {
242 return $result;
243 }
244
245 return new \WP_REST_Response(
246 array(
247 'success' => true,
248 'message' => \__( 'Actor followed successfully.', 'activitypub' ),
249 ),
250 200
251 );
252 }
253 }
254