2023-10-22 22:20:53 +00:00
|
|
|
<?php
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Collections REST-Class file.
|
|
|
|
*
|
|
|
|
* @package Activitypub
|
|
|
|
*/
|
|
|
|
|
2023-10-22 22:20:53 +00:00
|
|
|
namespace Activitypub\Rest;
|
|
|
|
|
|
|
|
use WP_REST_Server;
|
|
|
|
use WP_REST_Response;
|
2024-03-28 09:39:50 +00:00
|
|
|
use Activitypub\Activity\Actor;
|
|
|
|
use Activitypub\Activity\Base_Object;
|
2023-10-22 22:20:53 +00:00
|
|
|
use Activitypub\Collection\Users as User_Collection;
|
2024-10-09 12:44:17 +00:00
|
|
|
use Activitypub\Collection\Replies;
|
|
|
|
|
2024-06-27 12:10:38 +00:00
|
|
|
use Activitypub\Transformer\Factory;
|
2024-10-09 12:44:17 +00:00
|
|
|
use WP_Error;
|
2023-10-22 22:20:53 +00:00
|
|
|
|
|
|
|
use function Activitypub\esc_hashtag;
|
|
|
|
use function Activitypub\is_single_user;
|
|
|
|
use function Activitypub\get_rest_url_by_path;
|
|
|
|
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* ActivityPub Collections REST-Class.
|
2023-10-22 22:20:53 +00:00
|
|
|
*
|
|
|
|
* @author Matthias Pfefferle
|
|
|
|
*
|
|
|
|
* @see https://docs.joinmastodon.org/spec/activitypub/#featured
|
|
|
|
* @see https://docs.joinmastodon.org/spec/activitypub/#featuredTags
|
|
|
|
*/
|
|
|
|
class Collection {
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Initialize the class, registering WordPress hooks.
|
2023-10-22 22:20:53 +00:00
|
|
|
*/
|
|
|
|
public static function init() {
|
|
|
|
self::register_routes();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Register routes.
|
2023-10-22 22:20:53 +00:00
|
|
|
*/
|
|
|
|
public static function register_routes() {
|
|
|
|
\register_rest_route(
|
|
|
|
ACTIVITYPUB_REST_NAMESPACE,
|
2024-06-27 12:10:38 +00:00
|
|
|
'/(users|actors)/(?P<user_id>[\w\-\.]+)/collections/tags',
|
2023-10-22 22:20:53 +00:00
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'methods' => WP_REST_Server::READABLE,
|
|
|
|
'callback' => array( self::class, 'tags_get' ),
|
|
|
|
'args' => self::request_parameters(),
|
|
|
|
'permission_callback' => '__return_true',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
\register_rest_route(
|
|
|
|
ACTIVITYPUB_REST_NAMESPACE,
|
2024-06-27 12:10:38 +00:00
|
|
|
'/(users|actors)/(?P<user_id>[\w\-\.]+)/collections/featured',
|
2023-10-22 22:20:53 +00:00
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'methods' => WP_REST_Server::READABLE,
|
|
|
|
'callback' => array( self::class, 'featured_get' ),
|
|
|
|
'args' => self::request_parameters(),
|
|
|
|
'permission_callback' => '__return_true',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
\register_rest_route(
|
|
|
|
ACTIVITYPUB_REST_NAMESPACE,
|
|
|
|
'/collections/moderators',
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'methods' => WP_REST_Server::READABLE,
|
|
|
|
'callback' => array( self::class, 'moderators_get' ),
|
|
|
|
'permission_callback' => '__return_true',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
2024-10-09 12:44:17 +00:00
|
|
|
|
|
|
|
\register_rest_route(
|
|
|
|
ACTIVITYPUB_REST_NAMESPACE,
|
|
|
|
'/(?P<type>[\w\-\.]+)s/(?P<id>[\w\-\.]+)/replies',
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'methods' => WP_REST_Server::READABLE,
|
|
|
|
'callback' => array( self::class, 'replies_get' ),
|
|
|
|
'args' => self::request_parameters_for_replies(),
|
|
|
|
'permission_callback' => '__return_true',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The endpoint for replies collections.
|
|
|
|
*
|
|
|
|
* @param \WP_REST_Request $request The request object.
|
|
|
|
*
|
|
|
|
* @return WP_REST_Response|\WP_Error The response object or WP_Error.
|
|
|
|
*/
|
|
|
|
public static function replies_get( $request ) {
|
|
|
|
$type = $request->get_param( 'type' );
|
|
|
|
|
|
|
|
// Get the WordPress object of that "owns" the requested replies.
|
|
|
|
switch ( $type ) {
|
|
|
|
case 'comment':
|
|
|
|
$wp_object = \get_comment( $request->get_param( 'id' ) );
|
|
|
|
break;
|
|
|
|
case 'post':
|
|
|
|
default:
|
|
|
|
$wp_object = \get_post( $request->get_param( 'id' ) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! isset( $wp_object ) || is_wp_error( $wp_object ) ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'activitypub_replies_collection_does_not_exist',
|
|
|
|
\sprintf(
|
|
|
|
// translators: %s: The type (post, comment, etc.) for which no replies collection exists.
|
|
|
|
\__( 'No reply collection exists for the type %s.', 'activitypub' ),
|
|
|
|
$type
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$page = intval( $request->get_param( 'page' ) );
|
|
|
|
|
|
|
|
// If the request parameter page is present get the CollectionPage otherwise the replies collection.
|
|
|
|
if ( isset( $page ) ) {
|
|
|
|
$response = Replies::get_collection_page( $wp_object, $page );
|
|
|
|
} else {
|
|
|
|
$response = Replies::get_collection( $wp_object );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( is_wp_error( $response ) ) {
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add ActivityPub Context.
|
|
|
|
$response = array_merge(
|
|
|
|
array( '@context' => Base_Object::JSON_LD_CONTEXT ),
|
|
|
|
$response
|
|
|
|
);
|
|
|
|
|
|
|
|
return new WP_REST_Response( $response, 200 );
|
2023-10-22 22:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Featured Tags endpoint
|
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @param \WP_REST_Request $request The request object.
|
2023-10-22 22:20:53 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @return WP_REST_Response|\WP_Error The response object or WP_Error.
|
2023-10-22 22:20:53 +00:00
|
|
|
*/
|
|
|
|
public static function tags_get( $request ) {
|
|
|
|
$user_id = $request->get_param( 'user_id' );
|
|
|
|
$user = User_Collection::get_by_various( $user_id );
|
|
|
|
|
|
|
|
if ( is_wp_error( $user ) ) {
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
$number = 4;
|
|
|
|
|
|
|
|
$tags = \get_terms(
|
|
|
|
array(
|
|
|
|
'taxonomy' => 'post_tag',
|
|
|
|
'orderby' => 'count',
|
|
|
|
'order' => 'DESC',
|
|
|
|
'number' => $number,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( is_wp_error( $tags ) ) {
|
|
|
|
$tags = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = array(
|
2024-03-28 09:39:50 +00:00
|
|
|
'@context' => Base_Object::JSON_LD_CONTEXT,
|
2024-06-27 12:10:38 +00:00
|
|
|
'id' => get_rest_url_by_path( sprintf( 'actors/%d/collections/tags', $user->get__id() ) ),
|
2023-10-22 22:20:53 +00:00
|
|
|
'type' => 'Collection',
|
2023-12-08 23:23:11 +00:00
|
|
|
'totalItems' => is_countable( $tags ) ? count( $tags ) : 0,
|
2023-10-22 22:20:53 +00:00
|
|
|
'items' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $tags as $tag ) {
|
|
|
|
$response['items'][] = array(
|
|
|
|
'type' => 'Hashtag',
|
|
|
|
'href' => \esc_url( \get_tag_link( $tag ) ),
|
|
|
|
'name' => esc_hashtag( $tag->name ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-12-08 23:23:11 +00:00
|
|
|
$rest_response = new WP_REST_Response( $response, 200 );
|
|
|
|
$rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) );
|
|
|
|
|
|
|
|
return $rest_response;
|
2023-10-22 22:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Featured posts endpoint
|
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @param \WP_REST_Request $request The request object.
|
2023-10-22 22:20:53 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @return WP_REST_Response|\WP_Error The response object or WP_Error.
|
2023-10-22 22:20:53 +00:00
|
|
|
*/
|
|
|
|
public static function featured_get( $request ) {
|
|
|
|
$user_id = $request->get_param( 'user_id' );
|
|
|
|
$user = User_Collection::get_by_various( $user_id );
|
|
|
|
|
|
|
|
if ( is_wp_error( $user ) ) {
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sticky_posts = \get_option( 'sticky_posts' );
|
|
|
|
|
|
|
|
if ( ! is_single_user() && User_Collection::BLOG_USER_ID === $user->get__id() ) {
|
|
|
|
$posts = array();
|
|
|
|
} elseif ( $sticky_posts ) {
|
|
|
|
$args = array(
|
|
|
|
'post__in' => $sticky_posts,
|
|
|
|
'ignore_sticky_posts' => 1,
|
|
|
|
'orderby' => 'date',
|
|
|
|
'order' => 'DESC',
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $user->get__id() > 0 ) {
|
|
|
|
$args['author'] = $user->get__id();
|
|
|
|
}
|
|
|
|
|
|
|
|
$posts = \get_posts( $args );
|
|
|
|
} else {
|
|
|
|
$posts = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = array(
|
2024-03-28 09:39:50 +00:00
|
|
|
'@context' => Base_Object::JSON_LD_CONTEXT,
|
2024-06-27 12:10:38 +00:00
|
|
|
'id' => get_rest_url_by_path( sprintf( 'actors/%d/collections/featured', $user_id ) ),
|
2023-10-22 22:20:53 +00:00
|
|
|
'type' => 'OrderedCollection',
|
2023-12-08 23:23:11 +00:00
|
|
|
'totalItems' => is_countable( $posts ) ? count( $posts ) : 0,
|
2023-10-22 22:20:53 +00:00
|
|
|
'orderedItems' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $posts as $post ) {
|
2024-06-27 12:10:38 +00:00
|
|
|
$transformer = Factory::get_transformer( $post );
|
|
|
|
|
|
|
|
if ( \is_wp_error( $transformer ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response['orderedItems'][] = $transformer->to_object()->to_array( false );
|
2023-10-22 22:20:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-08 23:23:11 +00:00
|
|
|
$rest_response = new WP_REST_Response( $response, 200 );
|
|
|
|
$rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) );
|
|
|
|
|
|
|
|
return $rest_response;
|
2023-10-22 22:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Moderators endpoint.
|
2023-10-22 22:20:53 +00:00
|
|
|
*
|
|
|
|
* @return WP_REST_Response The response object.
|
|
|
|
*/
|
2024-10-09 12:44:17 +00:00
|
|
|
public static function moderators_get() {
|
2023-10-22 22:20:53 +00:00
|
|
|
$response = array(
|
2024-03-28 09:39:50 +00:00
|
|
|
'@context' => Actor::JSON_LD_CONTEXT,
|
2023-10-22 22:20:53 +00:00
|
|
|
'id' => get_rest_url_by_path( 'collections/moderators' ),
|
|
|
|
'type' => 'OrderedCollection',
|
|
|
|
'orderedItems' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
$users = User_Collection::get_collection();
|
|
|
|
|
|
|
|
foreach ( $users as $user ) {
|
|
|
|
$response['orderedItems'][] = $user->get_url();
|
|
|
|
}
|
|
|
|
|
2023-12-08 23:23:11 +00:00
|
|
|
$rest_response = new WP_REST_Response( $response, 200 );
|
|
|
|
$rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) );
|
|
|
|
|
|
|
|
return $rest_response;
|
2023-10-22 22:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* The supported parameters.
|
2023-10-22 22:20:53 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @return array List of parameters.
|
2023-10-22 22:20:53 +00:00
|
|
|
*/
|
|
|
|
public static function request_parameters() {
|
|
|
|
$params = array();
|
|
|
|
|
|
|
|
$params['user_id'] = array(
|
|
|
|
'required' => true,
|
2024-10-09 12:44:17 +00:00
|
|
|
'type' => 'string',
|
|
|
|
);
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The supported parameters.
|
|
|
|
*
|
|
|
|
* @return array list of parameters.
|
|
|
|
*/
|
|
|
|
public static function request_parameters_for_replies() {
|
|
|
|
$params = array();
|
|
|
|
|
|
|
|
$params['type'] = array(
|
|
|
|
'required' => true,
|
|
|
|
'type' => 'string',
|
|
|
|
'enum' => array( 'post', 'comment' ),
|
|
|
|
);
|
|
|
|
|
|
|
|
$params['id'] = array(
|
|
|
|
'required' => true,
|
|
|
|
'type' => 'string',
|
2023-10-22 22:20:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
}
|