2020-04-07 13:03:04 +00:00
|
|
|
<?php
|
|
|
|
namespace Activitypub\Rest;
|
|
|
|
|
2023-10-22 22:20:53 +00:00
|
|
|
use stdClass;
|
|
|
|
use WP_Error;
|
|
|
|
use WP_REST_Server;
|
|
|
|
use WP_REST_Response;
|
|
|
|
use Activitypub\Activity\Activity;
|
|
|
|
use Activitypub\Collection\Users as User_Collection;
|
2024-06-27 12:10:38 +00:00
|
|
|
use Activitypub\Transformer\Factory;
|
2023-10-22 22:20:53 +00:00
|
|
|
|
|
|
|
use function Activitypub\get_context;
|
|
|
|
use function Activitypub\get_rest_url_by_path;
|
2024-04-19 10:49:31 +00:00
|
|
|
use function Activitypub\get_masked_wp_version;
|
2023-10-22 22:20:53 +00:00
|
|
|
|
2020-04-07 13:03:04 +00:00
|
|
|
/**
|
|
|
|
* ActivityPub Outbox REST-Class
|
|
|
|
*
|
|
|
|
* @author Matthias Pfefferle
|
|
|
|
*
|
|
|
|
* @see https://www.w3.org/TR/activitypub/#outbox
|
|
|
|
*/
|
|
|
|
class Outbox {
|
|
|
|
/**
|
|
|
|
* Initialize the class, registering WordPress hooks
|
|
|
|
*/
|
|
|
|
public static function init() {
|
2023-10-22 22:20:53 +00:00
|
|
|
self::register_routes();
|
2020-04-07 13:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register routes
|
|
|
|
*/
|
|
|
|
public static function register_routes() {
|
|
|
|
\register_rest_route(
|
2023-10-22 22:20:53 +00:00
|
|
|
ACTIVITYPUB_REST_NAMESPACE,
|
2024-06-27 12:10:38 +00:00
|
|
|
'/(users|actors)/(?P<user_id>[\w\-\.]+)/outbox',
|
2022-08-19 16:15:51 +00:00
|
|
|
array(
|
2020-04-07 13:03:04 +00:00
|
|
|
array(
|
2023-10-22 22:20:53 +00:00
|
|
|
'methods' => WP_REST_Server::READABLE,
|
|
|
|
'callback' => array( self::class, 'user_outbox_get' ),
|
2020-12-25 19:23:08 +00:00
|
|
|
'args' => self::request_parameters(),
|
|
|
|
'permission_callback' => '__return_true',
|
2020-04-07 13:03:04 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the user-outbox
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request
|
|
|
|
* @return WP_REST_Response
|
|
|
|
*/
|
2021-07-25 23:24:56 +00:00
|
|
|
public static function user_outbox_get( $request ) {
|
|
|
|
$user_id = $request->get_param( 'user_id' );
|
2023-10-22 22:20:53 +00:00
|
|
|
$user = User_Collection::get_by_various( $user_id );
|
2020-04-07 13:03:04 +00:00
|
|
|
|
2023-10-22 22:20:53 +00:00
|
|
|
if ( is_wp_error( $user ) ) {
|
|
|
|
return $user;
|
2020-04-07 13:03:04 +00:00
|
|
|
}
|
|
|
|
|
2024-03-28 09:39:50 +00:00
|
|
|
$post_types = \get_option( 'activitypub_support_post_types', array( 'post' ) );
|
2023-10-22 22:20:53 +00:00
|
|
|
|
|
|
|
$page = $request->get_param( 'page', 1 );
|
2020-04-07 13:03:04 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Action triggerd prior to the ActivityPub profile being created and sent to the client
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
\do_action( 'activitypub_rest_outbox_pre' );
|
2020-04-07 13:03:04 +00:00
|
|
|
|
2023-10-22 22:20:53 +00:00
|
|
|
$json = new stdClass();
|
2020-04-07 13:03:04 +00:00
|
|
|
|
2023-10-22 22:20:53 +00:00
|
|
|
$json->{'@context'} = get_context();
|
2024-06-27 12:10:38 +00:00
|
|
|
$json->id = get_rest_url_by_path( sprintf( 'actors/%d/outbox', $user_id ) );
|
2024-04-19 10:49:31 +00:00
|
|
|
$json->generator = 'http://wordpress.org/?v=' . get_masked_wp_version();
|
2023-10-22 22:20:53 +00:00
|
|
|
$json->actor = $user->get_id();
|
2020-04-07 13:03:04 +00:00
|
|
|
$json->type = 'OrderedCollectionPage';
|
2024-06-27 12:10:38 +00:00
|
|
|
$json->partOf = get_rest_url_by_path( sprintf( 'actors/%d/outbox', $user_id ) ); // phpcs:ignore
|
2022-12-19 23:08:11 +00:00
|
|
|
$json->totalItems = 0; // phpcs:ignore
|
2020-04-07 13:03:04 +00:00
|
|
|
|
2024-04-19 10:49:31 +00:00
|
|
|
if ( $user_id > 0 ) {
|
|
|
|
$count_posts = \count_user_posts( $user_id, $post_types, true );
|
|
|
|
$json->totalItems = \intval( $count_posts ); // phpcs:ignore
|
|
|
|
} else {
|
|
|
|
foreach ( $post_types as $post_type ) {
|
|
|
|
$count_posts = \wp_count_posts( $post_type );
|
|
|
|
$json->totalItems += \intval( $count_posts->publish ); // phpcs:ignore
|
|
|
|
}
|
2022-12-19 23:08:11 +00:00
|
|
|
}
|
2020-04-07 13:03:04 +00:00
|
|
|
|
2021-07-25 23:24:56 +00:00
|
|
|
$json->first = \add_query_arg( 'page', 1, $json->partOf ); // phpcs:ignore
|
|
|
|
$json->last = \add_query_arg( 'page', \ceil ( $json->totalItems / 10 ), $json->partOf ); // phpcs:ignore
|
2020-04-07 13:03:04 +00:00
|
|
|
|
2021-07-25 23:24:56 +00:00
|
|
|
if ( $page && ( ( \ceil ( $json->totalItems / 10 ) ) > $page ) ) { // phpcs:ignore
|
|
|
|
$json->next = \add_query_arg( 'page', $page + 1, $json->partOf ); // phpcs:ignore
|
2020-04-07 13:03:04 +00:00
|
|
|
}
|
|
|
|
|
2023-10-22 22:20:53 +00:00
|
|
|
if ( $page && ( $page > 1 ) ) { // phpcs:ignore
|
|
|
|
$json->prev = \add_query_arg( 'page', $page - 1, $json->partOf ); // phpcs:ignore
|
|
|
|
}
|
|
|
|
|
2021-07-25 23:24:56 +00:00
|
|
|
if ( $page ) {
|
2022-08-19 16:15:51 +00:00
|
|
|
$posts = \get_posts(
|
|
|
|
array(
|
|
|
|
'posts_per_page' => 10,
|
2024-04-19 10:49:31 +00:00
|
|
|
'author' => $user_id > 0 ? $user_id : null,
|
2023-10-22 22:20:53 +00:00
|
|
|
'paged' => $page,
|
|
|
|
'post_type' => $post_types,
|
2022-08-19 16:15:51 +00:00
|
|
|
)
|
|
|
|
);
|
2021-07-25 23:24:56 +00:00
|
|
|
|
|
|
|
foreach ( $posts as $post ) {
|
2024-06-27 12:10:38 +00:00
|
|
|
$transformer = Factory::get_transformer( $post );
|
|
|
|
|
|
|
|
if ( \is_wp_error( $transformer ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$post = $transformer->to_object();
|
2023-10-22 22:20:53 +00:00
|
|
|
$activity = new Activity();
|
|
|
|
$activity->set_type( 'Create' );
|
|
|
|
$activity->set_object( $post );
|
2024-03-28 09:39:50 +00:00
|
|
|
$json->orderedItems[] = $activity->to_array( false ); // phpcs:ignore
|
2021-07-25 23:24:56 +00:00
|
|
|
}
|
2020-04-07 13:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// filter output
|
2023-10-22 22:20:53 +00:00
|
|
|
$json = \apply_filters( 'activitypub_rest_outbox_array', $json );
|
2020-04-07 13:03:04 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Action triggerd after the ActivityPub profile has been created and sent to the client
|
|
|
|
*/
|
|
|
|
\do_action( 'activitypub_outbox_post' );
|
|
|
|
|
2023-12-08 23:23:11 +00:00
|
|
|
$rest_response = new WP_REST_Response( $json, 200 );
|
|
|
|
$rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) );
|
2020-04-07 13:03:04 +00:00
|
|
|
|
2023-12-08 23:23:11 +00:00
|
|
|
return $rest_response;
|
2020-04-07 13:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The supported parameters
|
|
|
|
*
|
|
|
|
* @return array list of parameters
|
|
|
|
*/
|
|
|
|
public static function request_parameters() {
|
|
|
|
$params = array();
|
|
|
|
|
|
|
|
$params['page'] = array(
|
|
|
|
'type' => 'integer',
|
2023-10-22 22:20:53 +00:00
|
|
|
'default' => 1,
|
2020-04-07 13:03:04 +00:00
|
|
|
);
|
|
|
|
|
2021-07-25 23:24:56 +00:00
|
|
|
$params['user_id'] = array(
|
2020-04-07 13:03:04 +00:00
|
|
|
'required' => true,
|
2023-10-22 22:20:53 +00:00
|
|
|
'type' => 'string',
|
2020-04-07 13:03:04 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
}
|