PluginProbe
ActivityPub / 3.2.5
ActivityPub v3.2.5
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 3.2.5, at includes/rest/class-outbox.php

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