PluginProbe
ActivityPub / 2.1.0
ActivityPub v2.1.0
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.1.0, at includes/rest/class-collection.php

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