| 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 |
), |
| 59 |
) |
| 60 |
); |
| 61 |
|
| 62 |
// Block actor. |
| 63 |
\register_rest_route( |
| 64 |
$this->namespace, |
| 65 |
'/' . $this->rest_base . '/(?P<id>[\d]+)/block', |
| 66 |
array( |
| 67 |
'args' => array( |
| 68 |
'id' => array( |
| 69 |
'description' => 'The ID of the actor.', |
| 70 |
'type' => 'integer', |
| 71 |
'required' => true, |
| 72 |
'validate_callback' => array( $this, 'validate_actor_id' ), |
| 73 |
), |
| 74 |
), |
| 75 |
array( |
| 76 |
'methods' => \WP_REST_Server::CREATABLE, |
| 77 |
'callback' => array( $this, 'block_actor' ), |
| 78 |
'permission_callback' => array( $this, 'check_permission' ), |
| 79 |
'args' => array( |
| 80 |
'site_wide' => array( |
| 81 |
'description' => 'Whether to block site-wide (admin only).', |
| 82 |
'type' => 'boolean', |
| 83 |
'default' => false, |
| 84 |
), |
| 85 |
), |
| 86 |
), |
| 87 |
) |
| 88 |
); |
| 89 |
|
| 90 |
// Follow actor. |
| 91 |
\register_rest_route( |
| 92 |
$this->namespace, |
| 93 |
'/' . $this->rest_base . '/(?P<id>[\d]+)/follow', |
| 94 |
array( |
| 95 |
'args' => array( |
| 96 |
'id' => array( |
| 97 |
'description' => 'The ID of the actor.', |
| 98 |
'type' => 'integer', |
| 99 |
'required' => true, |
| 100 |
'validate_callback' => array( $this, 'validate_actor_id' ), |
| 101 |
), |
| 102 |
), |
| 103 |
array( |
| 104 |
'methods' => \WP_REST_Server::CREATABLE, |
| 105 |
'callback' => array( $this, 'follow_actor' ), |
| 106 |
'permission_callback' => array( $this, 'check_permission' ), |
| 107 |
), |
| 108 |
) |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Check if the current user has permission to perform actions. |
| 114 |
* |
| 115 |
* @return bool|\WP_Error True if the request has permission, WP_Error object otherwise. |
| 116 |
*/ |
| 117 |
public function check_permission() { |
| 118 |
if ( ! user_can_activitypub( \get_current_user_id() ) ) { |
| 119 |
return new \WP_Error( |
| 120 |
'rest_forbidden', |
| 121 |
\__( 'Sorry, you are not allowed to perform this action.', 'activitypub' ), |
| 122 |
array( 'status' => 403 ) |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
return true; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Validate actor ID. |
| 131 |
* |
| 132 |
* @param int $value The actor ID. |
| 133 |
* @return bool True if valid, false otherwise. |
| 134 |
*/ |
| 135 |
public function validate_actor_id( $value ) { |
| 136 |
$actor = \get_post( $value ); |
| 137 |
|
| 138 |
return $actor instanceof \WP_Post && Remote_Actors::POST_TYPE === $actor->post_type; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Remove follower relationship. |
| 143 |
* |
| 144 |
* @param \WP_REST_Request $request Full data about the request. |
| 145 |
* @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure. |
| 146 |
*/ |
| 147 |
public function unfollow_actor( $request ) { |
| 148 |
$actor_id = $request->get_param( 'id' ); |
| 149 |
$user_id = \get_current_user_id(); |
| 150 |
|
| 151 |
$result = Followers::remove( $actor_id, $user_id ); |
| 152 |
|
| 153 |
if ( ! $result ) { |
| 154 |
return new \WP_Error( |
| 155 |
'rest_follower_removal_failed', |
| 156 |
\__( 'Failed to remove follower.', 'activitypub' ), |
| 157 |
array( 'status' => 500 ) |
| 158 |
); |
| 159 |
} |
| 160 |
|
| 161 |
return new \WP_REST_Response( |
| 162 |
array( |
| 163 |
'success' => true, |
| 164 |
'message' => \__( 'Follower removed successfully.', 'activitypub' ), |
| 165 |
), |
| 166 |
200 |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Block an actor. |
| 172 |
* |
| 173 |
* @param \WP_REST_Request $request Full data about the request. |
| 174 |
* @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure. |
| 175 |
*/ |
| 176 |
public function block_actor( $request ) { |
| 177 |
$actor_id = $request->get_param( 'id' ); |
| 178 |
$site_wide = $request->get_param( 'site_wide' ); |
| 179 |
$user_id = \get_current_user_id(); |
| 180 |
|
| 181 |
$actor = Remote_Actors::get_actor( $actor_id ); |
| 182 |
if ( \is_wp_error( $actor ) ) { |
| 183 |
return $actor; |
| 184 |
} |
| 185 |
|
| 186 |
$actor_url = $actor->get_id(); |
| 187 |
|
| 188 |
// Add user-specific block. |
| 189 |
$user_block_success = Moderation::add_user_block( $user_id, 'actor', $actor_url ); |
| 190 |
|
| 191 |
// Add site-wide block if requested and user has permission. |
| 192 |
$site_block_success = true; |
| 193 |
if ( $site_wide && \current_user_can( 'manage_options' ) ) { |
| 194 |
$site_block_success = Moderation::add_site_block( 'actor', $actor_url ); |
| 195 |
} |
| 196 |
|
| 197 |
if ( ! $user_block_success || ! $site_block_success ) { |
| 198 |
return new \WP_Error( |
| 199 |
'rest_actor_block_failed', |
| 200 |
\__( 'Failed to block actor.', 'activitypub' ), |
| 201 |
array( 'status' => 500 ) |
| 202 |
); |
| 203 |
} |
| 204 |
|
| 205 |
// Remove follower relationship after blocking. |
| 206 |
Followers::remove( $actor_id, $user_id ); |
| 207 |
|
| 208 |
return new \WP_REST_Response( |
| 209 |
array( |
| 210 |
'success' => true, |
| 211 |
'message' => \__( 'Actor blocked successfully.', 'activitypub' ), |
| 212 |
), |
| 213 |
200 |
| 214 |
); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Follow an actor. |
| 219 |
* |
| 220 |
* @param \WP_REST_Request $request Full data about the request. |
| 221 |
* @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure. |
| 222 |
*/ |
| 223 |
public function follow_actor( $request ) { |
| 224 |
// Check if following UI is enabled. |
| 225 |
if ( '1' !== \get_option( 'activitypub_following_ui', '0' ) ) { |
| 226 |
return new \WP_Error( |
| 227 |
'rest_following_disabled', |
| 228 |
\__( 'Following feature is disabled.', 'activitypub' ), |
| 229 |
array( 'status' => 403 ) |
| 230 |
); |
| 231 |
} |
| 232 |
|
| 233 |
$actor_id = $request->get_param( 'id' ); |
| 234 |
$user_id = \get_current_user_id(); |
| 235 |
|
| 236 |
$result = Following::follow( $actor_id, $user_id ); |
| 237 |
|
| 238 |
if ( \is_wp_error( $result ) ) { |
| 239 |
return $result; |
| 240 |
} |
| 241 |
|
| 242 |
return new \WP_REST_Response( |
| 243 |
array( |
| 244 |
'success' => true, |
| 245 |
'message' => \__( 'Actor followed successfully.', 'activitypub' ), |
| 246 |
), |
| 247 |
200 |
| 248 |
); |
| 249 |
} |
| 250 |
} |
| 251 |
|