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

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