049046be70
* add collection endpoint * show featured posts * more consistant wording * backwards compatibility with php7.x * compatibility with php5.6 * use ACTIVITYPUB_AUTHORIZED_FETCH instead because the ACTIVITYPUB_SECURE_MODE could be misinterpreted with disabling the security mechanisms completely. * the blog user follows all authors of a blog if not in single_user mode * phpdoc * adding changes based on feedback from @jeherve * global namespace * better hashtag handling should also fix #373 #239 thanks @jeherve for help and feedback! * fix workflow
162 lines
3.8 KiB
PHP
162 lines
3.8 KiB
PHP
<?php
|
|
namespace Activitypub\Rest;
|
|
|
|
use WP_REST_Server;
|
|
use WP_REST_Response;
|
|
use Activitypub\Transformer\Post;
|
|
|
|
use function Activitypub\esc_hashtag;
|
|
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',
|
|
),
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 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' );
|
|
$number = 4;
|
|
|
|
$tags = \get_terms(
|
|
array(
|
|
'taxonomy' => 'post_tag',
|
|
'orderby' => 'count',
|
|
'order' => 'DESC',
|
|
'number' => $number,
|
|
)
|
|
);
|
|
|
|
if ( is_wp_error( $tags ) ) {
|
|
$tags = array();
|
|
}
|
|
|
|
$response = array(
|
|
'@context' => 'https://www.w3.org/ns/activitystreams',
|
|
array(
|
|
'Hashtah' => 'as:Hastag',
|
|
),
|
|
'id' => get_rest_url_by_path( sprintf( 'users/%d/collections/tags', $user_id ) ),
|
|
'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' );
|
|
|
|
$args = array(
|
|
'post__in' => \get_option( 'sticky_posts' ),
|
|
'ignore_sticky_posts' => 1,
|
|
);
|
|
|
|
if ( $user_id > 0 ) {
|
|
$args['author'] = $user_id;
|
|
}
|
|
|
|
$posts = \get_posts( $args );
|
|
|
|
$response = array(
|
|
'@context' => 'https://www.w3.org/ns/activitystreams',
|
|
array(
|
|
'ostatus' => 'http://ostatus.org#',
|
|
'atomUri' => 'ostatus:atomUri',
|
|
'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri',
|
|
'conversation' => 'ostatus:conversation',
|
|
'sensitive' => 'as:sensitive',
|
|
'toot' => 'http://joinmastodon.org/ns#',
|
|
'votersCount' => 'toot:votersCount',
|
|
),
|
|
'id' => get_rest_url_by_path( sprintf( 'users/%d/collections/featured', $user_id ) ),
|
|
'totalItems' => count( $posts ),
|
|
'items' => array(),
|
|
);
|
|
|
|
foreach ( $posts as $post ) {
|
|
$response['items'][] = Post::transform( $post )->to_object()->to_array();
|
|
}
|
|
|
|
return new WP_REST_Response( $response, 200 );
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
}
|