| 1 |
<?php |
| 2 |
namespace Activitypub\Rest; |
| 3 |
|
| 4 |
use WP_REST_Server; |
| 5 |
use WP_REST_Response; |
| 6 |
use Activitypub\Activity\Actor; |
| 7 |
use Activitypub\Activity\Base_Object; |
| 8 |
use Activitypub\Collection\Users as User_Collection; |
| 9 |
use Activitypub\Transformer\Factory; |
| 10 |
|
| 11 |
use function Activitypub\esc_hashtag; |
| 12 |
use function Activitypub\is_single_user; |
| 13 |
use function Activitypub\get_rest_url_by_path; |
| 14 |
|
| 15 |
/** |
| 16 |
* ActivityPub Collections REST-Class |
| 17 |
* |
| 18 |
* @author Matthias Pfefferle |
| 19 |
* |
| 20 |
* @see https://docs.joinmastodon.org/spec/activitypub/#featured |
| 21 |
* @see https://docs.joinmastodon.org/spec/activitypub/#featuredTags |
| 22 |
*/ |
| 23 |
class Collection { |
| 24 |
/** |
| 25 |
* Initialize the class, registering WordPress hooks |
| 26 |
*/ |
| 27 |
public static function init() { |
| 28 |
self::register_routes(); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Register routes |
| 33 |
*/ |
| 34 |
public static function register_routes() { |
| 35 |
\register_rest_route( |
| 36 |
ACTIVITYPUB_REST_NAMESPACE, |
| 37 |
'/(users|actors)/(?P<user_id>[\w\-\.]+)/collections/tags', |
| 38 |
array( |
| 39 |
array( |
| 40 |
'methods' => WP_REST_Server::READABLE, |
| 41 |
'callback' => array( self::class, 'tags_get' ), |
| 42 |
'args' => self::request_parameters(), |
| 43 |
'permission_callback' => '__return_true', |
| 44 |
), |
| 45 |
) |
| 46 |
); |
| 47 |
|
| 48 |
\register_rest_route( |
| 49 |
ACTIVITYPUB_REST_NAMESPACE, |
| 50 |
'/(users|actors)/(?P<user_id>[\w\-\.]+)/collections/featured', |
| 51 |
array( |
| 52 |
array( |
| 53 |
'methods' => WP_REST_Server::READABLE, |
| 54 |
'callback' => array( self::class, 'featured_get' ), |
| 55 |
'args' => self::request_parameters(), |
| 56 |
'permission_callback' => '__return_true', |
| 57 |
), |
| 58 |
) |
| 59 |
); |
| 60 |
|
| 61 |
\register_rest_route( |
| 62 |
ACTIVITYPUB_REST_NAMESPACE, |
| 63 |
'/collections/moderators', |
| 64 |
array( |
| 65 |
array( |
| 66 |
'methods' => WP_REST_Server::READABLE, |
| 67 |
'callback' => array( self::class, 'moderators_get' ), |
| 68 |
'permission_callback' => '__return_true', |
| 69 |
), |
| 70 |
) |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* The Featured Tags endpoint |
| 76 |
* |
| 77 |
* @param WP_REST_Request $request The request object. |
| 78 |
* |
| 79 |
* @return WP_REST_Response The response object. |
| 80 |
*/ |
| 81 |
public static function tags_get( $request ) { |
| 82 |
$user_id = $request->get_param( 'user_id' ); |
| 83 |
$user = User_Collection::get_by_various( $user_id ); |
| 84 |
|
| 85 |
if ( is_wp_error( $user ) ) { |
| 86 |
return $user; |
| 87 |
} |
| 88 |
|
| 89 |
$number = 4; |
| 90 |
|
| 91 |
$tags = \get_terms( |
| 92 |
array( |
| 93 |
'taxonomy' => 'post_tag', |
| 94 |
'orderby' => 'count', |
| 95 |
'order' => 'DESC', |
| 96 |
'number' => $number, |
| 97 |
) |
| 98 |
); |
| 99 |
|
| 100 |
if ( is_wp_error( $tags ) ) { |
| 101 |
$tags = array(); |
| 102 |
} |
| 103 |
|
| 104 |
$response = array( |
| 105 |
'@context' => Base_Object::JSON_LD_CONTEXT, |
| 106 |
'id' => get_rest_url_by_path( sprintf( 'actors/%d/collections/tags', $user->get__id() ) ), |
| 107 |
'type' => 'Collection', |
| 108 |
'totalItems' => is_countable( $tags ) ? count( $tags ) : 0, |
| 109 |
'items' => array(), |
| 110 |
); |
| 111 |
|
| 112 |
foreach ( $tags as $tag ) { |
| 113 |
$response['items'][] = array( |
| 114 |
'type' => 'Hashtag', |
| 115 |
'href' => \esc_url( \get_tag_link( $tag ) ), |
| 116 |
'name' => esc_hashtag( $tag->name ), |
| 117 |
); |
| 118 |
} |
| 119 |
|
| 120 |
$rest_response = new WP_REST_Response( $response, 200 ); |
| 121 |
$rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) ); |
| 122 |
|
| 123 |
return $rest_response; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Featured posts endpoint |
| 128 |
* |
| 129 |
* @param WP_REST_Request $request The request object. |
| 130 |
* |
| 131 |
* @return WP_REST_Response The response object. |
| 132 |
*/ |
| 133 |
public static function featured_get( $request ) { |
| 134 |
$user_id = $request->get_param( 'user_id' ); |
| 135 |
$user = User_Collection::get_by_various( $user_id ); |
| 136 |
|
| 137 |
if ( is_wp_error( $user ) ) { |
| 138 |
return $user; |
| 139 |
} |
| 140 |
|
| 141 |
$sticky_posts = \get_option( 'sticky_posts' ); |
| 142 |
|
| 143 |
if ( ! is_single_user() && User_Collection::BLOG_USER_ID === $user->get__id() ) { |
| 144 |
$posts = array(); |
| 145 |
} elseif ( $sticky_posts ) { |
| 146 |
$args = array( |
| 147 |
'post__in' => $sticky_posts, |
| 148 |
'ignore_sticky_posts' => 1, |
| 149 |
'orderby' => 'date', |
| 150 |
'order' => 'DESC', |
| 151 |
); |
| 152 |
|
| 153 |
if ( $user->get__id() > 0 ) { |
| 154 |
$args['author'] = $user->get__id(); |
| 155 |
} |
| 156 |
|
| 157 |
$posts = \get_posts( $args ); |
| 158 |
} else { |
| 159 |
$posts = array(); |
| 160 |
} |
| 161 |
|
| 162 |
$response = array( |
| 163 |
'@context' => Base_Object::JSON_LD_CONTEXT, |
| 164 |
'id' => get_rest_url_by_path( sprintf( 'actors/%d/collections/featured', $user_id ) ), |
| 165 |
'type' => 'OrderedCollection', |
| 166 |
'totalItems' => is_countable( $posts ) ? count( $posts ) : 0, |
| 167 |
'orderedItems' => array(), |
| 168 |
); |
| 169 |
|
| 170 |
foreach ( $posts as $post ) { |
| 171 |
$transformer = Factory::get_transformer( $post ); |
| 172 |
|
| 173 |
if ( \is_wp_error( $transformer ) ) { |
| 174 |
continue; |
| 175 |
} |
| 176 |
|
| 177 |
$response['orderedItems'][] = $transformer->to_object()->to_array( false ); |
| 178 |
} |
| 179 |
|
| 180 |
$rest_response = new WP_REST_Response( $response, 200 ); |
| 181 |
$rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) ); |
| 182 |
|
| 183 |
return $rest_response; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Moderators endpoint |
| 188 |
* |
| 189 |
* @param WP_REST_Request $request The request object. |
| 190 |
* |
| 191 |
* @return WP_REST_Response The response object. |
| 192 |
*/ |
| 193 |
public static function moderators_get( $request ) { |
| 194 |
$response = array( |
| 195 |
'@context' => Actor::JSON_LD_CONTEXT, |
| 196 |
'id' => get_rest_url_by_path( 'collections/moderators' ), |
| 197 |
'type' => 'OrderedCollection', |
| 198 |
'orderedItems' => array(), |
| 199 |
); |
| 200 |
|
| 201 |
$users = User_Collection::get_collection(); |
| 202 |
|
| 203 |
foreach ( $users as $user ) { |
| 204 |
$response['orderedItems'][] = $user->get_url(); |
| 205 |
} |
| 206 |
|
| 207 |
$rest_response = new WP_REST_Response( $response, 200 ); |
| 208 |
$rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) ); |
| 209 |
|
| 210 |
return $rest_response; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* The supported parameters |
| 215 |
* |
| 216 |
* @return array list of parameters |
| 217 |
*/ |
| 218 |
public static function request_parameters() { |
| 219 |
$params = array(); |
| 220 |
|
| 221 |
$params['user_id'] = array( |
| 222 |
'required' => true, |
| 223 |
'type' => 'string', |
| 224 |
); |
| 225 |
|
| 226 |
return $params; |
| 227 |
} |
| 228 |
} |
| 229 |
|