PluginProbe
ActivityPub / 1.0.0
ActivityPub v1.0.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-nodeinfo.php

class-nodeinfo.php in ActivityPub 1.0.0, at includes/rest/class-nodeinfo.php

229 lines 4.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 WP_REST_Response;
5
6 use function Activitypub\get_rest_url_by_path;
7
8 /**
9 * ActivityPub NodeInfo REST-Class
10 *
11 * @author Matthias Pfefferle
12 *
13 * @see http://nodeinfo.diaspora.software/
14 */
15 class Nodeinfo {
16 /**
17 * Initialize the class, registering WordPress hooks
18 */
19 public static function init() {
20 \add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
21 \add_filter( 'nodeinfo_data', array( self::class, 'add_nodeinfo_discovery' ), 10, 2 );
22 \add_filter( 'nodeinfo2_data', array( self::class, 'add_nodeinfo2_discovery' ), 10 );
23 }
24
25 /**
26 * Register routes
27 */
28 public static function register_routes() {
29 \register_rest_route(
30 ACTIVITYPUB_REST_NAMESPACE,
31 '/nodeinfo/discovery',
32 array(
33 array(
34 'methods' => \WP_REST_Server::READABLE,
35 'callback' => array( self::class, 'discovery' ),
36 'permission_callback' => '__return_true',
37 ),
38 )
39 );
40
41 \register_rest_route(
42 ACTIVITYPUB_REST_NAMESPACE,
43 '/nodeinfo',
44 array(
45 array(
46 'methods' => \WP_REST_Server::READABLE,
47 'callback' => array( self::class, 'nodeinfo' ),
48 'permission_callback' => '__return_true',
49 ),
50 )
51 );
52
53 \register_rest_route(
54 ACTIVITYPUB_REST_NAMESPACE,
55 '/nodeinfo2',
56 array(
57 array(
58 'methods' => \WP_REST_Server::READABLE,
59 'callback' => array( self::class, 'nodeinfo2' ),
60 'permission_callback' => '__return_true',
61 ),
62 )
63 );
64 }
65
66 /**
67 * Render NodeInfo file
68 *
69 * @param WP_REST_Request $request
70 *
71 * @return WP_REST_Response
72 */
73 public static function nodeinfo( $request ) {
74 /*
75 * Action triggerd prior to the ActivityPub profile being created and sent to the client
76 */
77 \do_action( 'activitypub_rest_nodeinfo_pre' );
78
79 $nodeinfo = array();
80
81 $nodeinfo['version'] = '2.0';
82 $nodeinfo['software'] = array(
83 'name' => 'wordpress',
84 'version' => \get_bloginfo( 'version' ),
85 );
86
87 $users = \get_users(
88 array(
89 'capability__in' => array( 'publish_posts' ),
90 )
91 );
92
93 if ( is_array( $users ) ) {
94 $users = count( $users );
95 } else {
96 $users = 1;
97 }
98
99 $posts = \wp_count_posts();
100 $comments = \wp_count_comments();
101
102 $nodeinfo['usage'] = array(
103 'users' => array(
104 'total' => $users,
105 ),
106 'localPosts' => (int) $posts->publish,
107 'localComments' => (int) $comments->approved,
108 );
109
110 $nodeinfo['openRegistrations'] = false;
111 $nodeinfo['protocols'] = array( 'activitypub' );
112
113 $nodeinfo['services'] = array(
114 'inbound' => array(),
115 'outbound' => array(),
116 );
117
118 return new WP_REST_Response( $nodeinfo, 200 );
119 }
120
121 /**
122 * Render NodeInfo file
123 *
124 * @param WP_REST_Request $request
125 *
126 * @return WP_REST_Response
127 */
128 public static function nodeinfo2( $request ) {
129 /*
130 * Action triggerd prior to the ActivityPub profile being created and sent to the client
131 */
132 \do_action( 'activitypub_rest_nodeinfo2_pre' );
133
134 $nodeinfo = array();
135
136 $nodeinfo['version'] = '1.0';
137 $nodeinfo['server'] = array(
138 'baseUrl' => \home_url( '/' ),
139 'name' => \get_bloginfo( 'name' ),
140 'software' => 'wordpress',
141 'version' => \get_bloginfo( 'version' ),
142 );
143
144 $users = \get_users(
145 array(
146 'capability__in' => array( 'publish_posts' ),
147 )
148 );
149
150 if ( is_array( $users ) ) {
151 $users = count( $users );
152 } else {
153 $users = 1;
154 }
155
156 $posts = \wp_count_posts();
157 $comments = \wp_count_comments();
158
159 $nodeinfo['usage'] = array(
160 'users' => array(
161 'total' => (int) $users,
162 ),
163 'localPosts' => (int) $posts->publish,
164 'localComments' => (int) $comments->approved,
165 );
166
167 $nodeinfo['openRegistrations'] = false;
168 $nodeinfo['protocols'] = array( 'activitypub' );
169
170 $nodeinfo['services'] = array(
171 'inbound' => array(),
172 'outbound' => array(),
173 );
174
175 return new WP_REST_Response( $nodeinfo, 200 );
176 }
177
178 /**
179 * Render NodeInfo discovery file
180 *
181 * @param WP_REST_Request $request
182 *
183 * @return WP_REST_Response
184 */
185 public static function discovery( $request ) {
186 $discovery = array();
187 $discovery['links'] = array(
188 array(
189 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0',
190 'href' => get_rest_url_by_path( 'nodeinfo' ),
191 ),
192 );
193
194 return new \WP_REST_Response( $discovery, 200 );
195 }
196
197 /**
198 * Extend NodeInfo data
199 *
200 * @param array $nodeinfo NodeInfo data
201 * @param string The NodeInfo Version
202 *
203 * @return array The extended array
204 */
205 public static function add_nodeinfo_discovery( $nodeinfo, $version ) {
206 if ( '2.0' === $version ) {
207 $nodeinfo['protocols'][] = 'activitypub';
208 } else {
209 $nodeinfo['protocols']['inbound'][] = 'activitypub';
210 $nodeinfo['protocols']['outbound'][] = 'activitypub';
211 }
212
213 return $nodeinfo;
214 }
215
216 /**
217 * Extend NodeInfo2 data
218 *
219 * @param array $nodeinfo NodeInfo2 data
220 *
221 * @return array The extended array
222 */
223 public static function add_nodeinfo2_discovery( $nodeinfo ) {
224 $nodeinfo['protocols'][] = 'activitypub';
225
226 return $nodeinfo;
227 }
228 }
229