PluginProbe
NodeInfo(2) / trunk
NodeInfo(2) vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 2.0.0 2.1.0 2.1.1 2.2.0 2.3.0 2.3.1 3.0.0 3.1.0
nodeinfo / includes / integration / class-nodeinfo22.php

class-nodeinfo22.php in NodeInfo(2) trunk, at includes/integration/class-nodeinfo22.php

325 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * NodeInfo 2.2 Integration.
4 *
5 * @package Nodeinfo
6 * @link https://nodeinfo.diaspora.software/protocol/2.2
7 */
8
9 namespace Nodeinfo\Integration;
10
11 use function Nodeinfo\get_active_users;
12 use function Nodeinfo\get_masked_version;
13
14 /**
15 * NodeInfo 2.2 Integration class.
16 */
17 class Nodeinfo22 {
18
19 /**
20 * The version identifier.
21 */
22 const VERSION = '2.2';
23
24 /**
25 * Initialize the integration.
26 */
27 public static function init() {
28 \add_filter( 'nodeinfo_versions', array( __CLASS__, 'register_version' ) );
29 \add_filter( 'nodeinfo_discovery_links', array( __CLASS__, 'discovery_link' ) );
30 \add_filter( 'nodeinfo_schema', array( __CLASS__, 'schema' ) );
31 \add_filter( 'nodeinfo_data_software', array( __CLASS__, 'software' ), 10, 2 );
32 \add_filter( 'nodeinfo_data_protocols', array( __CLASS__, 'protocols' ), 10, 2 );
33 \add_filter( 'nodeinfo_data_services', array( __CLASS__, 'services' ), 10, 2 );
34 \add_filter( 'nodeinfo_data_usage', array( __CLASS__, 'usage' ), 10, 2 );
35 \add_filter( 'nodeinfo_data_metadata', array( __CLASS__, 'metadata' ), 10, 2 );
36 \add_filter( 'nodeinfo_data', array( __CLASS__, 'add_instance' ), 10, 2 );
37 }
38
39 /**
40 * Registers the version.
41 *
42 * @param array $versions The versions array.
43 * @return array The modified versions array.
44 */
45 public static function register_version( $versions ) {
46 $versions[] = self::VERSION;
47 return $versions;
48 }
49
50 /**
51 * Adds the discovery link.
52 *
53 * @param array $links The discovery links.
54 * @return array The modified links.
55 */
56 public static function discovery_link( $links ) {
57 $links[] = array(
58 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/' . self::VERSION,
59 'href' => \get_rest_url( null, '/nodeinfo/' . self::VERSION ),
60 );
61 return $links;
62 }
63
64 /**
65 * Adds the schema for NodeInfo 2.2.
66 *
67 * @link https://github.com/jhass/nodeinfo/blob/main/schemas/2.2/schema.json
68 *
69 * @param array $schema The schema.
70 * @return array The modified schema.
71 */
72 public static function schema( $schema ) {
73 // NodeInfo 2.2 schema - adds instance, activeWeek, and nostr protocol.
74 $schema['properties'] = \array_merge(
75 $schema['properties'],
76 array(
77 'version' => array(
78 'description' => 'The NodeInfo schema version.',
79 'type' => 'string',
80 ),
81 'instance' => array(
82 'description' => 'Metadata about this specific instance.',
83 'type' => 'object',
84 'properties' => array(
85 'name' => array(
86 'type' => 'string',
87 'maxLength' => 500,
88 ),
89 'description' => array(
90 'type' => 'string',
91 'maxLength' => 5000,
92 ),
93 ),
94 ),
95 'software' => array(
96 'description' => 'Metadata about server software in use.',
97 'type' => 'object',
98 'properties' => array(
99 'name' => array(
100 'type' => 'string',
101 'pattern' => '^[a-z0-9-]+$',
102 ),
103 'version' => array( 'type' => 'string' ),
104 'repository' => array(
105 'type' => 'string',
106 'format' => 'uri',
107 ),
108 'homepage' => array(
109 'type' => 'string',
110 'format' => 'uri',
111 ),
112 ),
113 ),
114 'protocols' => array(
115 'description' => 'The protocols supported on this server.',
116 'type' => 'array',
117 'minItems' => 1,
118 'items' => array(
119 'type' => 'string',
120 'enum' => array( 'activitypub', 'buddycloud', 'dfrn', 'diaspora', 'libertree', 'nostr', 'ostatus', 'pumpio', 'tent', 'xmpp', 'zot' ),
121 ),
122 ),
123 'services' => array(
124 'description' => 'Third party sites this server can connect to.',
125 'type' => 'object',
126 'properties' => array(
127 'inbound' => array(
128 'type' => 'array',
129 'items' => array(
130 'type' => 'string',
131 'enum' => array( 'atom1.0', 'gnusocial', 'imap', 'pnut', 'pop3', 'pumpio', 'rss2.0', 'twitter' ),
132 ),
133 ),
134 'outbound' => array(
135 'type' => 'array',
136 'items' => array(
137 'type' => 'string',
138 'enum' => array( 'atom1.0', 'blogger', 'buddycloud', 'diaspora', 'dreamwidth', 'drupal', 'facebook', 'friendica', 'gnusocial', 'google', 'insanejournal', 'libertree', 'linkedin', 'livejournal', 'mediagoblin', 'myspace', 'pinterest', 'pnut', 'posterous', 'pumpio', 'redmatrix', 'rss2.0', 'smtp', 'tent', 'tumblr', 'twitter', 'wordpress', 'xmpp' ),
139 ),
140 ),
141 ),
142 ),
143 'openRegistrations' => array(
144 'description' => 'Whether this server allows open self-registration.',
145 'type' => 'boolean',
146 ),
147 'usage' => array(
148 'description' => 'Usage statistics for this server.',
149 'type' => 'object',
150 'properties' => array(
151 'users' => array(
152 'type' => 'object',
153 'properties' => array(
154 'total' => array(
155 'type' => 'integer',
156 'minimum' => 0,
157 ),
158 'activeMonth' => array(
159 'type' => 'integer',
160 'minimum' => 0,
161 ),
162 'activeHalfyear' => array(
163 'type' => 'integer',
164 'minimum' => 0,
165 ),
166 'activeWeek' => array(
167 'type' => 'integer',
168 'minimum' => 0,
169 ),
170 ),
171 ),
172 'localPosts' => array(
173 'type' => 'integer',
174 'minimum' => 0,
175 ),
176 'localComments' => array(
177 'type' => 'integer',
178 'minimum' => 0,
179 ),
180 ),
181 ),
182 'metadata' => array(
183 'description' => 'Free form key value pairs for software specific values.',
184 'type' => 'object',
185 ),
186 )
187 );
188
189 return $schema;
190 }
191
192 /**
193 * Adds software information.
194 *
195 * @param array $software The software data.
196 * @param string $version The NodeInfo version.
197 * @return array The modified software data.
198 */
199 public static function software( $software, $version ) {
200 if ( self::VERSION !== $version ) {
201 return $software;
202 }
203
204 // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText -- NodeInfo spec uses lowercase.
205 $software['name'] = 'wordpress';
206 $software['version'] = get_masked_version();
207 $software['repository'] = 'https://github.com/wordpress/wordpress';
208 $software['homepage'] = 'https://wordpress.org';
209
210 return $software;
211 }
212
213 /**
214 * Adds protocols.
215 *
216 * NodeInfo 2.0+ uses a flat array of protocol strings.
217 *
218 * @param array $protocols The protocols data.
219 * @param string $version The NodeInfo version.
220 * @return array The modified protocols data.
221 */
222 public static function protocols( $protocols, $version ) {
223 if ( self::VERSION !== $version ) {
224 return $protocols;
225 }
226
227 // Default protocols - can be extended via filter.
228 return \apply_filters( 'nodeinfo_protocols', array() );
229 }
230
231 /**
232 * Adds services.
233 *
234 * @param array $services The services data.
235 * @param string $version The NodeInfo version.
236 * @return array The modified services data.
237 */
238 public static function services( $services, $version ) {
239 if ( self::VERSION !== $version ) {
240 return $services;
241 }
242
243 $services['inbound'] = array( 'atom1.0', 'rss2.0', 'pop3' );
244 $services['outbound'] = array( 'atom1.0', 'rss2.0', 'wordpress', 'smtp' );
245
246 return $services;
247 }
248
249 /**
250 * Adds usage statistics.
251 *
252 * @param array $usage The usage data.
253 * @param string $version The NodeInfo version.
254 * @return array The modified usage data.
255 */
256 public static function usage( $usage, $version ) {
257 if ( self::VERSION !== $version ) {
258 return $usage;
259 }
260
261 $users = \get_users(
262 array(
263 'fields' => 'ID',
264 'capability__in' => array( 'publish_posts' ),
265 )
266 );
267
268 $user_count = \is_array( $users ) ? \count( $users ) : 1;
269
270 $posts = \wp_count_posts();
271 $comments = \wp_count_comments();
272
273 $usage['users'] = array(
274 'total' => $user_count,
275 'activeMonth' => get_active_users( '1 month ago' ),
276 'activeHalfyear' => get_active_users( '6 month ago' ),
277 'activeWeek' => get_active_users( '1 week ago' ),
278 );
279
280 $usage['localPosts'] = (int) $posts->publish;
281 $usage['localComments'] = (int) $comments->approved;
282
283 return $usage;
284 }
285
286 /**
287 * Adds metadata.
288 *
289 * @param array $metadata The metadata.
290 * @param string $version The NodeInfo version.
291 * @return array The modified metadata.
292 */
293 public static function metadata( $metadata, $version ) {
294 if ( self::VERSION !== $version ) {
295 return $metadata;
296 }
297
298 $metadata['nodeName'] = \get_bloginfo( 'name' );
299 $metadata['nodeDescription'] = \get_bloginfo( 'description' );
300 $metadata['nodeIcon'] = \get_site_icon_url();
301
302 return $metadata;
303 }
304
305 /**
306 * Adds instance information (new in 2.2).
307 *
308 * @param array $nodeinfo The NodeInfo data.
309 * @param string $version The NodeInfo version.
310 * @return array The modified NodeInfo data.
311 */
312 public static function add_instance( $nodeinfo, $version ) {
313 if ( self::VERSION !== $version ) {
314 return $nodeinfo;
315 }
316
317 $nodeinfo['instance'] = array(
318 'name' => \get_bloginfo( 'name' ),
319 'description' => \get_bloginfo( 'description' ),
320 );
321
322 return $nodeinfo;
323 }
324 }
325