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-nodeinfo.php

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

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