| 1 |
<?php |
| 2 |
namespace Activitypub\Rest; |
| 3 |
|
| 4 |
use WP_REST_Response; |
| 5 |
use Activitypub\Collection\Users as User_Collection; |
| 6 |
|
| 7 |
use function Activitypub\is_single_user; |
| 8 |
use function Activitypub\get_rest_url_by_path; |
| 9 |
use function Activitypub\get_masked_wp_version; |
| 10 |
|
| 11 |
/** |
| 12 |
* ActivityPub Following REST-Class |
| 13 |
* |
| 14 |
* @author Matthias Pfefferle |
| 15 |
* |
| 16 |
* @see https://www.w3.org/TR/activitypub/#following |
| 17 |
*/ |
| 18 |
class Following { |
| 19 |
/** |
| 20 |
* Initialize the class, registering WordPress hooks |
| 21 |
*/ |
| 22 |
public static function init() { |
| 23 |
self::register_routes(); |
| 24 |
|
| 25 |
\add_filter( 'activitypub_rest_following', array( self::class, 'default_following' ), 10, 2 ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Register routes |
| 30 |
*/ |
| 31 |
public static function register_routes() { |
| 32 |
\register_rest_route( |
| 33 |
ACTIVITYPUB_REST_NAMESPACE, |
| 34 |
'/(users|actors)/(?P<user_id>[\w\-\.]+)/following', |
| 35 |
array( |
| 36 |
array( |
| 37 |
'methods' => \WP_REST_Server::READABLE, |
| 38 |
'callback' => array( self::class, 'get' ), |
| 39 |
'args' => self::request_parameters(), |
| 40 |
'permission_callback' => '__return_true', |
| 41 |
), |
| 42 |
) |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Handle GET request |
| 48 |
* |
| 49 |
* @param WP_REST_Request $request |
| 50 |
* |
| 51 |
* @return WP_REST_Response |
| 52 |
*/ |
| 53 |
public static function get( $request ) { |
| 54 |
$user_id = $request->get_param( 'user_id' ); |
| 55 |
$user = User_Collection::get_by_various( $user_id ); |
| 56 |
|
| 57 |
if ( is_wp_error( $user ) ) { |
| 58 |
return $user; |
| 59 |
} |
| 60 |
|
| 61 |
/* |
| 62 |
* Action triggerd prior to the ActivityPub profile being created and sent to the client |
| 63 |
*/ |
| 64 |
\do_action( 'activitypub_rest_following_pre' ); |
| 65 |
|
| 66 |
$json = new \stdClass(); |
| 67 |
|
| 68 |
$json->{'@context'} = \Activitypub\get_context(); |
| 69 |
|
| 70 |
$json->id = get_rest_url_by_path( sprintf( 'actors/%d/following', $user->get__id() ) ); |
| 71 |
$json->generator = 'http://wordpress.org/?v=' . get_masked_wp_version(); |
| 72 |
$json->actor = $user->get_id(); |
| 73 |
$json->type = 'OrderedCollectionPage'; |
| 74 |
|
| 75 |
$json->partOf = get_rest_url_by_path( sprintf( 'actors/%d/following', $user->get__id() ) ); // phpcs:ignore |
| 76 |
|
| 77 |
$items = apply_filters( 'activitypub_rest_following', array(), $user ); // phpcs:ignore |
| 78 |
|
| 79 |
$json->totalItems = is_countable( $items ) ? count( $items ) : 0; // phpcs:ignore |
| 80 |
$json->orderedItems = $items; // phpcs:ignore |
| 81 |
|
| 82 |
$json->first = $json->partOf; // phpcs:ignore |
| 83 |
|
| 84 |
$rest_response = new WP_REST_Response( $json, 200 ); |
| 85 |
$rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) ); |
| 86 |
|
| 87 |
return $rest_response; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* The supported parameters |
| 92 |
* |
| 93 |
* @return array list of parameters |
| 94 |
*/ |
| 95 |
public static function request_parameters() { |
| 96 |
$params = array(); |
| 97 |
|
| 98 |
$params['page'] = array( |
| 99 |
'type' => 'integer', |
| 100 |
); |
| 101 |
|
| 102 |
$params['user_id'] = array( |
| 103 |
'required' => true, |
| 104 |
'type' => 'string', |
| 105 |
); |
| 106 |
|
| 107 |
return $params; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Add the Blog Authors to the following list of the Blog Actor |
| 112 |
* if Blog not in single mode. |
| 113 |
* |
| 114 |
* @param array $array The array of following urls. |
| 115 |
* @param User $user The user object. |
| 116 |
* |
| 117 |
* @return array The array of following urls. |
| 118 |
*/ |
| 119 |
public static function default_following( $array, $user ) { |
| 120 |
if ( 0 !== $user->get__id() || is_single_user() ) { |
| 121 |
return $array; |
| 122 |
} |
| 123 |
|
| 124 |
$users = User_Collection::get_collection(); |
| 125 |
|
| 126 |
foreach ( $users as $user ) { |
| 127 |
$array[] = $user->get_url(); |
| 128 |
} |
| 129 |
|
| 130 |
return $array; |
| 131 |
} |
| 132 |
} |
| 133 |
|