2023-08-09 13:07:30 +02:00
|
|
|
<?php
|
|
|
|
namespace Activitypub\Rest;
|
|
|
|
|
2023-09-22 09:38:59 +02:00
|
|
|
use WP_Error;
|
2023-08-09 13:07:30 +02:00
|
|
|
use WP_REST_Server;
|
|
|
|
use WP_REST_Response;
|
|
|
|
use Activitypub\Transformer\Post;
|
2023-08-11 09:23:49 +02:00
|
|
|
use Activitypub\Activity\Activity;
|
2023-08-11 11:16:06 +02:00
|
|
|
use Activitypub\Collection\Users as User_Collection;
|
2023-08-09 13:07:30 +02:00
|
|
|
|
|
|
|
use function Activitypub\esc_hashtag;
|
2023-08-11 11:16:06 +02:00
|
|
|
use function Activitypub\is_single_user;
|
2023-08-09 13:07:30 +02:00
|
|
|
use function Activitypub\get_rest_url_by_path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ActivityPub Collections REST-Class
|
|
|
|
*
|
|
|
|
* @author Matthias Pfefferle
|
|
|
|
*
|
|
|
|
* @see https://docs.joinmastodon.org/spec/activitypub/#featured
|
|
|
|
* @see https://docs.joinmastodon.org/spec/activitypub/#featuredTags
|
|
|
|
*/
|
|
|
|
class Collection {
|
|
|
|
/**
|
|
|
|
* Initialize the class, registering WordPress hooks
|
|
|
|
*/
|
|
|
|
public static function init() {
|
|
|
|
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register routes
|
|
|
|
*/
|
|
|
|
public static function register_routes() {
|
|
|
|
\register_rest_route(
|
|
|
|
ACTIVITYPUB_REST_NAMESPACE,
|
|
|
|
'/users/(?P<user_id>[\w\-\.]+)/collections/tags',
|
|
|
|
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,
|
|
|
|
'/users/(?P<user_id>[\w\-\.]+)/collections/featured',
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'methods' => WP_REST_Server::READABLE,
|
|
|
|
'callback' => array( self::class, 'featured_get' ),
|
|
|
|
'args' => self::request_parameters(),
|
|
|
|
'permission_callback' => '__return_true',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
2023-09-22 09:38:59 +02:00
|
|
|
|
|
|
|
\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',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
2023-08-09 13:07:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Featured Tags endpoint
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request The request object.
|
|
|
|
*
|
|
|
|
* @return WP_REST_Response The response object.
|
|
|
|
*/
|
|
|
|
public static function tags_get( $request ) {
|
|
|
|
$user_id = $request->get_param( 'user_id' );
|
2023-08-11 11:16:06 +02:00
|
|
|
$user = User_Collection::get_by_various( $user_id );
|
|
|
|
|
|
|
|
if ( is_wp_error( $user ) ) {
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
$number = 4;
|
2023-08-09 13:07:30 +02:00
|
|
|
|
|
|
|
$tags = \get_terms(
|
|
|
|
array(
|
|
|
|
'taxonomy' => 'post_tag',
|
|
|
|
'orderby' => 'count',
|
|
|
|
'order' => 'DESC',
|
|
|
|
'number' => $number,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( is_wp_error( $tags ) ) {
|
|
|
|
$tags = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = array(
|
2023-08-11 09:23:49 +02:00
|
|
|
'@context' => Activity::CONTEXT,
|
2023-08-11 11:16:06 +02:00
|
|
|
'id' => get_rest_url_by_path( sprintf( 'users/%d/collections/tags', $user->get__id() ) ),
|
2023-08-11 09:23:49 +02:00
|
|
|
'type' => 'Collection',
|
2023-08-09 13:07:30 +02:00
|
|
|
'totalItems' => count( $tags ),
|
|
|
|
'items' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $tags as $tag ) {
|
|
|
|
$response['items'][] = array(
|
|
|
|
'type' => 'Hashtag',
|
|
|
|
'href' => \esc_url( \get_tag_link( $tag ) ),
|
|
|
|
'name' => esc_hashtag( $tag->name ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new WP_REST_Response( $response, 200 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Featured posts endpoint
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request The request object.
|
|
|
|
*
|
|
|
|
* @return WP_REST_Response The response object.
|
|
|
|
*/
|
|
|
|
public static function featured_get( $request ) {
|
|
|
|
$user_id = $request->get_param( 'user_id' );
|
2023-08-11 11:16:06 +02:00
|
|
|
$user = User_Collection::get_by_various( $user_id );
|
2023-08-09 13:07:30 +02:00
|
|
|
|
2023-08-11 11:16:06 +02:00
|
|
|
if ( is_wp_error( $user ) ) {
|
|
|
|
return $user;
|
2023-08-09 13:07:30 +02:00
|
|
|
}
|
|
|
|
|
2023-08-11 11:16:06 +02:00
|
|
|
$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();
|
|
|
|
}
|
2023-08-09 13:07:30 +02:00
|
|
|
|
|
|
|
$response = array(
|
2023-08-11 09:23:49 +02:00
|
|
|
'@context' => Activity::CONTEXT,
|
|
|
|
'id' => get_rest_url_by_path( sprintf( 'users/%d/collections/featured', $user_id ) ),
|
|
|
|
'type' => 'OrderedCollection',
|
|
|
|
'totalItems' => count( $posts ),
|
|
|
|
'orderedItems' => array(),
|
2023-08-09 13:07:30 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $posts as $post ) {
|
2023-08-11 09:23:49 +02:00
|
|
|
$response['orderedItems'][] = Post::transform( $post )->to_object()->to_array();
|
2023-08-09 13:07:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return new WP_REST_Response( $response, 200 );
|
|
|
|
}
|
|
|
|
|
2023-09-22 09:38:59 +02:00
|
|
|
/**
|
|
|
|
* Moderators endpoint
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request The request object.
|
|
|
|
*
|
|
|
|
* @return WP_REST_Response The response object.
|
|
|
|
*/
|
|
|
|
public static function moderators_get( $request ) {
|
|
|
|
$response = array(
|
|
|
|
'@context' => Activity::CONTEXT,
|
|
|
|
'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();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new WP_REST_Response( $response, 200 );
|
|
|
|
}
|
|
|
|
|
2023-08-09 13:07:30 +02:00
|
|
|
/**
|
|
|
|
* The supported parameters
|
|
|
|
*
|
|
|
|
* @return array list of parameters
|
|
|
|
*/
|
|
|
|
public static function request_parameters() {
|
|
|
|
$params = array();
|
|
|
|
|
|
|
|
$params['user_id'] = array(
|
|
|
|
'required' => true,
|
|
|
|
'type' => 'string',
|
|
|
|
);
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
}
|