PluginProbe
ActivityPub / 5.3.1
ActivityPub v5.3.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-post.php

class-post.php in ActivityPub 5.3.1, at includes/rest/class-post.php

161 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ActivityPub Post REST Endpoints
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Rest;
9
10 use WP_REST_Server;
11 use WP_REST_Response;
12 use WP_Error;
13 use Activitypub\Comment;
14 use Activitypub\Activity\Base_Object;
15 use Activitypub\Collection\Replies;
16
17 use function Activitypub\get_rest_url_by_path;
18
19 /**
20 * Class Post
21 *
22 * @package Activitypub\Rest
23 */
24 class Post {
25
26 /**
27 * Initialize the class and register routes.
28 */
29 public static function init() {
30 self::register_routes();
31 }
32
33 /**
34 * Register routes.
35 */
36 public static function register_routes() {
37 register_rest_route(
38 ACTIVITYPUB_REST_NAMESPACE,
39 '/posts/(?P<id>\d+)/reactions',
40 array(
41 'methods' => WP_REST_Server::READABLE,
42 'callback' => array( static::class, 'get_reactions' ),
43 'permission_callback' => '__return_true',
44 'args' => array(
45 'id' => array(
46 'required' => true,
47 'type' => 'integer',
48 ),
49 ),
50 )
51 );
52
53 register_rest_route(
54 ACTIVITYPUB_REST_NAMESPACE,
55 '/posts/(?P<id>\d+)/context',
56 array(
57 'methods' => WP_REST_Server::READABLE,
58 'callback' => array( static::class, 'get_context' ),
59 'permission_callback' => '__return_true',
60 'args' => array(
61 'id' => array(
62 'required' => true,
63 'type' => 'integer',
64 ),
65 ),
66 )
67 );
68 }
69
70 /**
71 * Get reactions for a post.
72 *
73 * @param \WP_REST_Request $request The request.
74 *
75 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
76 */
77 public static function get_reactions( $request ) {
78 $post_id = $request->get_param( 'id' );
79 $post = get_post( $post_id );
80
81 if ( ! $post ) {
82 return new WP_Error( 'post_not_found', 'Post not found', array( 'status' => 404 ) );
83 }
84
85 $reactions = array();
86
87 foreach ( Comment::get_comment_types() as $type_object ) {
88 $comments = get_comments(
89 array(
90 'post_id' => $post_id,
91 'type' => $type_object['type'],
92 'status' => 'approve',
93 )
94 );
95
96 if ( empty( $comments ) ) {
97 continue;
98 }
99
100 $count = count( $comments );
101 // phpcs:disable WordPress.WP.I18n
102 $label = sprintf(
103 _n(
104 $type_object['count_single'],
105 $type_object['count_plural'],
106 $count,
107 'activitypub'
108 ),
109 number_format_i18n( $count )
110 );
111 // phpcs:enable WordPress.WP.I18n
112
113 $reactions[ $type_object['collection'] ] = array(
114 'label' => $label,
115 'items' => array_map(
116 function ( $comment ) {
117 return array(
118 'name' => $comment->comment_author,
119 'url' => $comment->comment_author_url,
120 'avatar' => get_comment_meta( $comment->comment_ID, 'avatar_url', true ),
121 );
122 },
123 $comments
124 ),
125 );
126 }
127
128 return new WP_REST_Response( $reactions );
129 }
130
131 /**
132 * Get the context for a post.
133 *
134 * @param \WP_REST_Request $request The request.
135 *
136 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
137 */
138 public static function get_context( $request ) {
139 $post_id = $request->get_param( 'id' );
140
141 $collection = Replies::get_context_collection( $post_id );
142
143 if ( false === $collection ) {
144 return new WP_Error( 'post_not_found', 'Post not found', array( 'status' => 404 ) );
145 }
146
147 $response = array_merge(
148 array(
149 '@context' => Base_Object::JSON_LD_CONTEXT,
150 'id' => get_rest_url_by_path( sprintf( 'posts/%d/context', $post_id ) ),
151 ),
152 $collection
153 );
154
155 $response = \rest_ensure_response( $response );
156 $response->header( 'Content-Type', 'application/activity+json; charset=' . \get_option( 'blog_charset' ) );
157
158 return $response;
159 }
160 }
161