PluginProbe
ActivityPub / 2.0.1
ActivityPub v2.0.1
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / rest / class-collection.php

class-collection.php in ActivityPub 2.0.1, at includes/rest/class-collection.php

223 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Activitypub\Rest;
3
4 use WP_Error;
5 use WP_REST_Server;
6 use WP_REST_Response;
7 use Activitypub\Transformer\Post;
8 use Activitypub\Activity\Activity;
9 use Activitypub\Collection\Users as User_Collection;
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/(?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/(?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' => Activity::CONTEXT,
106 'id' => get_rest_url_by_path( sprintf( 'users/%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' => Activity::CONTEXT,
164 'id' => get_rest_url_by_path( sprintf( 'users/%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 $response['orderedItems'][] = Post::transform( $post )->to_object()->to_array();
172 }
173
174 $rest_response = new WP_REST_Response( $response, 200 );
175 $rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) );
176
177 return $rest_response;
178 }
179
180 /**
181 * Moderators endpoint
182 *
183 * @param WP_REST_Request $request The request object.
184 *
185 * @return WP_REST_Response The response object.
186 */
187 public static function moderators_get( $request ) {
188 $response = array(
189 '@context' => Activity::CONTEXT,
190 'id' => get_rest_url_by_path( 'collections/moderators' ),
191 'type' => 'OrderedCollection',
192 'orderedItems' => array(),
193 );
194
195 $users = User_Collection::get_collection();
196
197 foreach ( $users as $user ) {
198 $response['orderedItems'][] = $user->get_url();
199 }
200
201 $rest_response = new WP_REST_Response( $response, 200 );
202 $rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) );
203
204 return $rest_response;
205 }
206
207 /**
208 * The supported parameters
209 *
210 * @return array list of parameters
211 */
212 public static function request_parameters() {
213 $params = array();
214
215 $params['user_id'] = array(
216 'required' => true,
217 'type' => 'string',
218 );
219
220 return $params;
221 }
222 }
223