| 1 |
<?php |
| 2 |
namespace Activitypub\Rest; |
| 3 |
|
| 4 |
use stdClass; |
| 5 |
use WP_Error; |
| 6 |
use WP_REST_Server; |
| 7 |
use WP_REST_Response; |
| 8 |
use Activitypub\Transformer\Post; |
| 9 |
use Activitypub\Activity\Activity; |
| 10 |
use Activitypub\Collection\Users as User_Collection; |
| 11 |
|
| 12 |
use function Activitypub\get_context; |
| 13 |
use function Activitypub\get_rest_url_by_path; |
| 14 |
|
| 15 |
/** |
| 16 |
* ActivityPub Outbox REST-Class |
| 17 |
* |
| 18 |
* @author Matthias Pfefferle |
| 19 |
* |
| 20 |
* @see https://www.w3.org/TR/activitypub/#outbox |
| 21 |
*/ |
| 22 |
class Outbox { |
| 23 |
/** |
| 24 |
* Initialize the class, registering WordPress hooks |
| 25 |
*/ |
| 26 |
public static function init() { |
| 27 |
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Register routes |
| 32 |
*/ |
| 33 |
public static function register_routes() { |
| 34 |
\register_rest_route( |
| 35 |
ACTIVITYPUB_REST_NAMESPACE, |
| 36 |
'/users/(?P<user_id>[\w\-\.]+)/outbox', |
| 37 |
array( |
| 38 |
array( |
| 39 |
'methods' => WP_REST_Server::READABLE, |
| 40 |
'callback' => array( self::class, 'user_outbox_get' ), |
| 41 |
'args' => self::request_parameters(), |
| 42 |
'permission_callback' => '__return_true', |
| 43 |
), |
| 44 |
) |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Renders the user-outbox |
| 50 |
* |
| 51 |
* @param WP_REST_Request $request |
| 52 |
* @return WP_REST_Response |
| 53 |
*/ |
| 54 |
public static function user_outbox_get( $request ) { |
| 55 |
$user_id = $request->get_param( 'user_id' ); |
| 56 |
$user = User_Collection::get_by_various( $user_id ); |
| 57 |
|
| 58 |
if ( is_wp_error( $user ) ) { |
| 59 |
return $user; |
| 60 |
} |
| 61 |
|
| 62 |
$post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ); |
| 63 |
|
| 64 |
$page = $request->get_param( 'page', 1 ); |
| 65 |
|
| 66 |
/* |
| 67 |
* Action triggerd prior to the ActivityPub profile being created and sent to the client |
| 68 |
*/ |
| 69 |
\do_action( 'activitypub_rest_outbox_pre' ); |
| 70 |
|
| 71 |
$json = new stdClass(); |
| 72 |
|
| 73 |
$json->{'@context'} = get_context(); |
| 74 |
$json->id = get_rest_url_by_path( sprintf( 'users/%d/outbox', $user_id ) ); |
| 75 |
$json->generator = 'http://wordpress.org/?v=' . \get_bloginfo_rss( 'version' ); |
| 76 |
$json->actor = $user->get_id(); |
| 77 |
$json->type = 'OrderedCollectionPage'; |
| 78 |
$json->partOf = get_rest_url_by_path( sprintf( 'users/%d/outbox', $user_id ) ); // phpcs:ignore |
| 79 |
$json->totalItems = 0; // phpcs:ignore |
| 80 |
|
| 81 |
foreach ( $post_types as $post_type ) { |
| 82 |
$count_posts = \wp_count_posts( $post_type ); |
| 83 |
$json->totalItems += \intval( $count_posts->publish ); // phpcs:ignore |
| 84 |
} |
| 85 |
|
| 86 |
$json->first = \add_query_arg( 'page', 1, $json->partOf ); // phpcs:ignore |
| 87 |
$json->last = \add_query_arg( 'page', \ceil ( $json->totalItems / 10 ), $json->partOf ); // phpcs:ignore |
| 88 |
|
| 89 |
if ( $page && ( ( \ceil ( $json->totalItems / 10 ) ) > $page ) ) { // phpcs:ignore |
| 90 |
$json->next = \add_query_arg( 'page', $page + 1, $json->partOf ); // phpcs:ignore |
| 91 |
} |
| 92 |
|
| 93 |
if ( $page && ( $page > 1 ) ) { // phpcs:ignore |
| 94 |
$json->prev = \add_query_arg( 'page', $page - 1, $json->partOf ); // phpcs:ignore |
| 95 |
} |
| 96 |
|
| 97 |
if ( $page ) { |
| 98 |
$posts = \get_posts( |
| 99 |
array( |
| 100 |
'posts_per_page' => 10, |
| 101 |
'author' => $user_id, |
| 102 |
'paged' => $page, |
| 103 |
'post_type' => $post_types, |
| 104 |
) |
| 105 |
); |
| 106 |
|
| 107 |
foreach ( $posts as $post ) { |
| 108 |
$post = Post::transform( $post )->to_object(); |
| 109 |
$activity = new Activity(); |
| 110 |
$activity->set_type( 'Create' ); |
| 111 |
$activity->set_context( null ); |
| 112 |
$activity->set_object( $post ); |
| 113 |
|
| 114 |
$json->orderedItems[] = $activity->to_array(); // phpcs:ignore |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
// filter output |
| 119 |
$json = \apply_filters( 'activitypub_rest_outbox_array', $json ); |
| 120 |
|
| 121 |
/* |
| 122 |
* Action triggerd after the ActivityPub profile has been created and sent to the client |
| 123 |
*/ |
| 124 |
\do_action( 'activitypub_outbox_post' ); |
| 125 |
|
| 126 |
$response = new WP_REST_Response( $json, 200 ); |
| 127 |
|
| 128 |
$response->header( 'Content-Type', 'application/activity+json' ); |
| 129 |
|
| 130 |
return $response; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* The supported parameters |
| 135 |
* |
| 136 |
* @return array list of parameters |
| 137 |
*/ |
| 138 |
public static function request_parameters() { |
| 139 |
$params = array(); |
| 140 |
|
| 141 |
$params['page'] = array( |
| 142 |
'type' => 'integer', |
| 143 |
'default' => 1, |
| 144 |
); |
| 145 |
|
| 146 |
$params['user_id'] = array( |
| 147 |
'required' => true, |
| 148 |
'type' => 'string', |
| 149 |
); |
| 150 |
|
| 151 |
return $params; |
| 152 |
} |
| 153 |
} |
| 154 |
|