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-outbox.php

class-outbox.php in ActivityPub 2.1.0, at includes/rest/class-outbox.php

151 lines 3.9 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 stdClass;
5 use WP_Error;
6 use WP_REST_Server;
7 use WP_REST_Response;
8 use Activitypub\Transformer\Post;
9 use Activitypub\Activity\Activity;
10 use Activitypub\Collection\Users as User_Collection;
11
12 use function Activitypub\get_context;
13 use function Activitypub\get_rest_url_by_path;
14
15 /**
16 * ActivityPub Outbox REST-Class
17 *
18 * @author Matthias Pfefferle
19 *
20 * @see https://www.w3.org/TR/activitypub/#outbox
21 */
22 class Outbox {
23 /**
24 * Initialize the class, registering WordPress hooks
25 */
26 public static function init() {
27 self::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\-\.]+)/outbox',
37 array(
38 array(
39 'methods' => WP_REST_Server::READABLE,
40 'callback' => array( self::class, 'user_outbox_get' ),
41 'args' => self::request_parameters(),
42 'permission_callback' => '__return_true',
43 ),
44 )
45 );
46 }
47
48 /**
49 * Renders the user-outbox
50 *
51 * @param WP_REST_Request $request
52 * @return WP_REST_Response
53 */
54 public static function user_outbox_get( $request ) {
55 $user_id = $request->get_param( 'user_id' );
56 $user = User_Collection::get_by_various( $user_id );
57
58 if ( is_wp_error( $user ) ) {
59 return $user;
60 }
61
62 $post_types = \get_option( 'activitypub_support_post_types', array( 'post' ) );
63
64 $page = $request->get_param( 'page', 1 );
65
66 /*
67 * Action triggerd prior to the ActivityPub profile being created and sent to the client
68 */
69 \do_action( 'activitypub_rest_outbox_pre' );
70
71 $json = new stdClass();
72
73 $json->{'@context'} = get_context();
74 $json->id = get_rest_url_by_path( sprintf( 'users/%d/outbox', $user_id ) );
75 $json->generator = 'http://wordpress.org/?v=' . \get_bloginfo_rss( 'version' );
76 $json->actor = $user->get_id();
77 $json->type = 'OrderedCollectionPage';
78 $json->partOf = get_rest_url_by_path( sprintf( 'users/%d/outbox', $user_id ) ); // phpcs:ignore
79 $json->totalItems = 0; // phpcs:ignore
80
81 foreach ( $post_types as $post_type ) {
82 $count_posts = \wp_count_posts( $post_type );
83 $json->totalItems += \intval( $count_posts->publish ); // phpcs:ignore
84 }
85
86 $json->first = \add_query_arg( 'page', 1, $json->partOf ); // phpcs:ignore
87 $json->last = \add_query_arg( 'page', \ceil ( $json->totalItems / 10 ), $json->partOf ); // phpcs:ignore
88
89 if ( $page && ( ( \ceil ( $json->totalItems / 10 ) ) > $page ) ) { // phpcs:ignore
90 $json->next = \add_query_arg( 'page', $page + 1, $json->partOf ); // phpcs:ignore
91 }
92
93 if ( $page && ( $page > 1 ) ) { // phpcs:ignore
94 $json->prev = \add_query_arg( 'page', $page - 1, $json->partOf ); // phpcs:ignore
95 }
96
97 if ( $page ) {
98 $posts = \get_posts(
99 array(
100 'posts_per_page' => 10,
101 'author' => $user_id,
102 'paged' => $page,
103 'post_type' => $post_types,
104 )
105 );
106
107 foreach ( $posts as $post ) {
108 $post = Post::transform( $post )->to_object();
109 $activity = new Activity();
110 $activity->set_type( 'Create' );
111 $activity->set_object( $post );
112 $json->orderedItems[] = $activity->to_array( false ); // phpcs:ignore
113 }
114 }
115
116 // filter output
117 $json = \apply_filters( 'activitypub_rest_outbox_array', $json );
118
119 /*
120 * Action triggerd after the ActivityPub profile has been created and sent to the client
121 */
122 \do_action( 'activitypub_outbox_post' );
123
124 $rest_response = new WP_REST_Response( $json, 200 );
125 $rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) );
126
127 return $rest_response;
128 }
129
130 /**
131 * The supported parameters
132 *
133 * @return array list of parameters
134 */
135 public static function request_parameters() {
136 $params = array();
137
138 $params['page'] = array(
139 'type' => 'integer',
140 'default' => 1,
141 );
142
143 $params['user_id'] = array(
144 'required' => true,
145 'type' => 'string',
146 );
147
148 return $params;
149 }
150 }
151