PluginProbe
ActivityPub / 0.14.1
ActivityPub v0.14.1
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 0.14.1, at includes/rest/class-outbox.php

146 lines 3.8 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 /**
5 * ActivityPub Outbox REST-Class
6 *
7 * @author Matthias Pfefferle
8 *
9 * @see https://www.w3.org/TR/activitypub/#outbox
10 */
11 class Outbox {
12 /**
13 * Initialize the class, registering WordPress hooks
14 */
15 public static function init() {
16 \add_action( 'rest_api_init', array( '\Activitypub\Rest\Outbox', 'register_routes' ) );
17 }
18
19 /**
20 * Register routes
21 */
22 public static function register_routes() {
23 \register_rest_route(
24 'activitypub/1.0',
25 '/users/(?P<user_id>\d+)/outbox',
26 array(
27 array(
28 'methods' => \WP_REST_Server::READABLE,
29 'callback' => array( '\Activitypub\Rest\Outbox', 'user_outbox_get' ),
30 'args' => self::request_parameters(),
31 'permission_callback' => '__return_true',
32 ),
33 )
34 );
35 }
36
37 /**
38 * Renders the user-outbox
39 *
40 * @param WP_REST_Request $request
41 * @return WP_REST_Response
42 */
43 public static function user_outbox_get( $request ) {
44 $user_id = $request->get_param( 'user_id' );
45 $author = \get_user_by( 'ID', $user_id );
46 $post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) );
47
48 if ( ! $author ) {
49 return new \WP_Error(
50 'rest_invalid_param',
51 \__( 'User not found', 'activitypub' ),
52 array(
53 'status' => 404,
54 'params' => array(
55 'user_id' => \__( 'User not found', 'activitypub' ),
56 ),
57 )
58 );
59 }
60
61 $page = $request->get_param( 'page', 0 );
62
63 /*
64 * Action triggerd prior to the ActivityPub profile being created and sent to the client
65 */
66 \do_action( 'activitypub_outbox_pre' );
67
68 $json = new \stdClass();
69
70 $json->{'@context'} = \Activitypub\get_context();
71 $json->id = \home_url( \add_query_arg( null, null ) );
72 $json->generator = 'http://wordpress.org/?v=' . \get_bloginfo_rss( 'version' );
73 $json->actor = \get_author_posts_url( $user_id );
74 $json->type = 'OrderedCollectionPage';
75 $json->partOf = \get_rest_url( null, "/activitypub/1.0/users/$user_id/outbox" ); // phpcs:ignore
76 $json->totalItems = 0; // phpcs:ignore
77
78 // phpcs:ignore
79 $json->totalItems = 0;
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 ) {
94 $posts = \get_posts(
95 array(
96 'posts_per_page' => 10,
97 'author' => $user_id,
98 'offset' => ( $page - 1 ) * 10,
99 'post_type' => $post_types,
100 )
101 );
102
103 foreach ( $posts as $post ) {
104 $activitypub_post = new \Activitypub\Model\Post( $post );
105 $activitypub_activity = new \Activitypub\Model\Activity( 'Create', \Activitypub\Model\Activity::TYPE_NONE );
106 $activitypub_activity->from_post( $activitypub_post->to_array() );
107 $json->orderedItems[] = $activitypub_activity->to_array(); // phpcs:ignore
108 }
109 }
110
111 // filter output
112 $json = \apply_filters( 'activitypub_outbox_array', $json );
113
114 /*
115 * Action triggerd after the ActivityPub profile has been created and sent to the client
116 */
117 \do_action( 'activitypub_outbox_post' );
118
119 $response = new \WP_REST_Response( $json, 200 );
120
121 $response->header( 'Content-Type', 'application/activity+json' );
122
123 return $response;
124 }
125
126 /**
127 * The supported parameters
128 *
129 * @return array list of parameters
130 */
131 public static function request_parameters() {
132 $params = array();
133
134 $params['page'] = array(
135 'type' => 'integer',
136 );
137
138 $params['user_id'] = array(
139 'required' => true,
140 'type' => 'integer',
141 );
142
143 return $params;
144 }
145 }
146