PluginProbe
ActivityPub / 1.0.3
ActivityPub v1.0.3
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 1.0.3, at includes/rest/class-collection.php

214 lines 5.0 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' => count( $tags ),
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 return new WP_REST_Response( $response, 200 );
121 }
122
123 /**
124 * Featured posts endpoint
125 *
126 * @param WP_REST_Request $request The request object.
127 *
128 * @return WP_REST_Response The response object.
129 */
130 public static function featured_get( $request ) {
131 $user_id = $request->get_param( 'user_id' );
132 $user = User_Collection::get_by_various( $user_id );
133
134 if ( is_wp_error( $user ) ) {
135 return $user;
136 }
137
138 $sticky_posts = \get_option( 'sticky_posts' );
139
140 if ( ! is_single_user() && User_Collection::BLOG_USER_ID === $user->get__id() ) {
141 $posts = array();
142 } elseif ( $sticky_posts ) {
143 $args = array(
144 'post__in' => $sticky_posts,
145 'ignore_sticky_posts' => 1,
146 'orderby' => 'date',
147 'order' => 'DESC',
148 );
149
150 if ( $user->get__id() > 0 ) {
151 $args['author'] = $user->get__id();
152 }
153
154 $posts = \get_posts( $args );
155 } else {
156 $posts = array();
157 }
158
159 $response = array(
160 '@context' => Activity::CONTEXT,
161 'id' => get_rest_url_by_path( sprintf( 'users/%d/collections/featured', $user_id ) ),
162 'type' => 'OrderedCollection',
163 'totalItems' => count( $posts ),
164 'orderedItems' => array(),
165 );
166
167 foreach ( $posts as $post ) {
168 $response['orderedItems'][] = Post::transform( $post )->to_object()->to_array();
169 }
170
171 return new WP_REST_Response( $response, 200 );
172 }
173
174 /**
175 * Moderators endpoint
176 *
177 * @param WP_REST_Request $request The request object.
178 *
179 * @return WP_REST_Response The response object.
180 */
181 public static function moderators_get( $request ) {
182 $response = array(
183 '@context' => Activity::CONTEXT,
184 'id' => get_rest_url_by_path( 'collections/moderators' ),
185 'type' => 'OrderedCollection',
186 'orderedItems' => array(),
187 );
188
189 $users = User_Collection::get_collection();
190
191 foreach ( $users as $user ) {
192 $response['orderedItems'][] = $user->get_url();
193 }
194
195 return new WP_REST_Response( $response, 200 );
196 }
197
198 /**
199 * The supported parameters
200 *
201 * @return array list of parameters
202 */
203 public static function request_parameters() {
204 $params = array();
205
206 $params['user_id'] = array(
207 'required' => true,
208 'type' => 'string',
209 );
210
211 return $params;
212 }
213 }
214