PluginProbe
ActivityPub / 0.7.3
ActivityPub v0.7.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-outbox.php

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

127 lines 3.3 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', '/users/(?P<id>\d+)/outbox', array(
25 array(
26 'methods' => \WP_REST_Server::READABLE,
27 'callback' => array( '\Activitypub\Rest\Outbox', 'user_outbox' ),
28 'args' => self::request_parameters(),
29 ),
30 )
31 );
32 }
33
34 /**
35 * Renders the user-outbox
36 *
37 * @param WP_REST_Request $request
38 * @return WP_REST_Response
39 */
40 public static function user_outbox( $request ) {
41 $user_id = $request->get_param( 'id' );
42 $author = get_user_by( 'ID', $user_id );
43
44 if ( ! $author ) {
45 return new \WP_Error( 'rest_invalid_param', __( 'User not found', 'activitypub' ), array(
46 'status' => 404,
47 'params' => array(
48 'user_id' => __( 'User not found', 'activitypub' ),
49 ),
50 ) );
51 }
52
53 $page = $request->get_param( 'page', 0 );
54
55 /*
56 * Action triggerd prior to the ActivityPub profile being created and sent to the client
57 */
58 do_action( 'activitypub_outbox_pre' );
59
60 $json = new \stdClass();
61
62 $json->{'@context'} = \Activitypub\get_context();
63 $json->id = home_url( add_query_arg( null, null ) );
64 $json->generator = 'http://wordpress.org/?v=' . get_bloginfo_rss( 'version' );
65 $json->actor = get_author_posts_url( $user_id );
66 $json->type = 'OrderedCollectionPage';
67 $json->partOf = get_rest_url( null, "/activitypub/1.0/users/$user_id/outbox" ); // phpcs:ignore
68
69 $count_posts = wp_count_posts();
70 $json->totalItems = intval( $count_posts->publish ); // phpcs:ignore
71
72 $posts = get_posts( array(
73 'posts_per_page' => 10,
74 'author' => $user_id,
75 'offset' => $page * 10,
76 ) );
77
78 $json->first = add_query_arg( 'page', 0, $json->partOf ); // phpcs:ignore
79 $json->last = add_query_arg( 'page', ( ceil ( $json->totalItems / 10 ) ) - 1, $json->partOf ); // phpcs:ignore
80
81 if ( ( ceil ( $json->totalItems / 10 ) ) - 1 > $page ) { // phpcs:ignore
82 $json->next = add_query_arg( 'page', ++$page, $json->partOf ); // phpcs:ignore
83 }
84
85 foreach ( $posts as $post ) {
86 $activitypub_post = new \Activitypub\Post( $post );
87 $activitypub_activity = new \Activitypub\Activity( 'Create', \Activitypub\Activity::TYPE_NONE );
88 $activitypub_activity->from_post( $activitypub_post->to_array() );
89 $json->orderedItems[] = $activitypub_activity->to_array(); // phpcs:ignore
90 }
91
92 // filter output
93 $json = apply_filters( 'activitypub_outbox_array', $json );
94
95 /*
96 * Action triggerd after the ActivityPub profile has been created and sent to the client
97 */
98 do_action( 'activitypub_outbox_post' );
99
100 $response = new \WP_REST_Response( $json, 200 );
101
102 $response->header( 'Content-Type', 'application/activity+json' );
103
104 return $response;
105 }
106
107 /**
108 * The supported parameters
109 *
110 * @return array list of parameters
111 */
112 public static function request_parameters() {
113 $params = array();
114
115 $params['page'] = array(
116 'type' => 'integer',
117 );
118
119 $params['id'] = array(
120 'required' => true,
121 'type' => 'integer',
122 );
123
124 return $params;
125 }
126 }
127