PluginProbe
NodeInfo(2) / 3.1.0
NodeInfo(2) v3.1.0
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-nodeinfo21.php

class-nodeinfo21.php in NodeInfo(2) 3.1.0, at includes/integration/class-nodeinfo21.php

285 lines 7.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.1 Integration.
4 *
5 * @package Nodeinfo
6 * @link https://nodeinfo.diaspora.software/protocol/2.1
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.1 Integration class.
16 */
17 class Nodeinfo21 {
18
19 /**
20 * The version identifier.
21 */
22 const VERSION = '2.1';
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 }
37
38 /**
39 * Registers the version.
40 *
41 * @param array $versions The versions array.
42 * @return array The modified versions array.
43 */
44 public static function register_version( $versions ) {
45 $versions[] = self::VERSION;
46 return $versions;
47 }
48
49 /**
50 * Adds the discovery link.
51 *
52 * @param array $links The discovery links.
53 * @return array The modified links.
54 */
55 public static function discovery_link( $links ) {
56 $links[] = array(
57 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/' . self::VERSION,
58 'href' => \get_rest_url( null, '/nodeinfo/' . self::VERSION ),
59 );
60 return $links;
61 }
62
63 /**
64 * Adds the schema for NodeInfo 2.1.
65 *
66 * @link https://github.com/jhass/nodeinfo/blob/main/schemas/2.1/schema.json
67 *
68 * @param array $schema The schema.
69 * @return array The modified schema.
70 */
71 public static function schema( $schema ) {
72 // NodeInfo 2.1 schema - adds repository and homepage to software.
73 $schema['properties'] = \array_merge(
74 $schema['properties'],
75 array(
76 'version' => array(
77 'description' => 'The NodeInfo schema version.',
78 'type' => 'string',
79 ),
80 'software' => array(
81 'description' => 'Metadata about server software in use.',
82 'type' => 'object',
83 'properties' => array(
84 'name' => array(
85 'type' => 'string',
86 'pattern' => '^[a-z0-9-]+$',
87 ),
88 'version' => array( 'type' => 'string' ),
89 'repository' => array(
90 'type' => 'string',
91 'format' => 'uri',
92 ),
93 'homepage' => array(
94 'type' => 'string',
95 'format' => 'uri',
96 ),
97 ),
98 ),
99 'protocols' => array(
100 'description' => 'The protocols supported on this server.',
101 'type' => 'array',
102 'minItems' => 1,
103 'items' => array(
104 'type' => 'string',
105 'enum' => array( 'activitypub', 'buddycloud', 'dfrn', 'diaspora', 'libertree', 'ostatus', 'pumpio', 'tent', 'xmpp', 'zot' ),
106 ),
107 ),
108 'services' => array(
109 'description' => 'Third party sites this server can connect to.',
110 'type' => 'object',
111 'properties' => array(
112 'inbound' => array(
113 'type' => 'array',
114 'items' => array(
115 'type' => 'string',
116 'enum' => array( 'atom1.0', 'gnusocial', 'imap', 'pnut', 'pop3', 'pumpio', 'rss2.0', 'twitter' ),
117 ),
118 ),
119 'outbound' => array(
120 'type' => 'array',
121 'items' => array(
122 'type' => 'string',
123 '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' ),
124 ),
125 ),
126 ),
127 ),
128 'openRegistrations' => array(
129 'description' => 'Whether this server allows open self-registration.',
130 'type' => 'boolean',
131 ),
132 'usage' => array(
133 'description' => 'Usage statistics for this server.',
134 'type' => 'object',
135 'properties' => array(
136 'users' => array(
137 'type' => 'object',
138 'properties' => array(
139 'total' => array(
140 'type' => 'integer',
141 'minimum' => 0,
142 ),
143 'activeMonth' => array(
144 'type' => 'integer',
145 'minimum' => 0,
146 ),
147 'activeHalfyear' => array(
148 'type' => 'integer',
149 'minimum' => 0,
150 ),
151 ),
152 ),
153 'localPosts' => array(
154 'type' => 'integer',
155 'minimum' => 0,
156 ),
157 'localComments' => array(
158 'type' => 'integer',
159 'minimum' => 0,
160 ),
161 ),
162 ),
163 'metadata' => array(
164 'description' => 'Free form key value pairs for software specific values.',
165 'type' => 'object',
166 ),
167 )
168 );
169
170 return $schema;
171 }
172
173 /**
174 * Adds software information.
175 *
176 * @param array $software The software data.
177 * @param string $version The NodeInfo version.
178 * @return array The modified software data.
179 */
180 public static function software( $software, $version ) {
181 if ( self::VERSION !== $version ) {
182 return $software;
183 }
184
185 // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText -- NodeInfo spec uses lowercase.
186 $software['name'] = 'wordpress';
187 $software['version'] = get_masked_version();
188 $software['repository'] = 'https://github.com/wordpress/wordpress';
189 $software['homepage'] = 'https://wordpress.org';
190
191 return $software;
192 }
193
194 /**
195 * Adds protocols.
196 *
197 * NodeInfo 2.0+ uses a flat array of protocol strings.
198 *
199 * @param array $protocols The protocols data.
200 * @param string $version The NodeInfo version.
201 * @return array The modified protocols data.
202 */
203 public static function protocols( $protocols, $version ) {
204 if ( self::VERSION !== $version ) {
205 return $protocols;
206 }
207
208 // Default protocols - can be extended via filter.
209 return \apply_filters( 'nodeinfo_protocols', array() );
210 }
211
212 /**
213 * Adds services.
214 *
215 * @param array $services The services data.
216 * @param string $version The NodeInfo version.
217 * @return array The modified services data.
218 */
219 public static function services( $services, $version ) {
220 if ( self::VERSION !== $version ) {
221 return $services;
222 }
223
224 $services['inbound'] = array( 'atom1.0', 'rss2.0', 'pop3' );
225 $services['outbound'] = array( 'atom1.0', 'rss2.0', 'wordpress', 'smtp' );
226
227 return $services;
228 }
229
230 /**
231 * Adds usage statistics.
232 *
233 * @param array $usage The usage data.
234 * @param string $version The NodeInfo version.
235 * @return array The modified usage data.
236 */
237 public static function usage( $usage, $version ) {
238 if ( self::VERSION !== $version ) {
239 return $usage;
240 }
241
242 $users = \get_users(
243 array(
244 'fields' => 'ID',
245 'capability__in' => array( 'publish_posts' ),
246 )
247 );
248
249 $user_count = \is_array( $users ) ? \count( $users ) : 1;
250
251 $posts = \wp_count_posts();
252 $comments = \wp_count_comments();
253
254 $usage['users'] = array(
255 'total' => $user_count,
256 'activeMonth' => get_active_users( '1 month ago' ),
257 'activeHalfyear' => get_active_users( '6 month ago' ),
258 );
259
260 $usage['localPosts'] = (int) $posts->publish;
261 $usage['localComments'] = (int) $comments->approved;
262
263 return $usage;
264 }
265
266 /**
267 * Adds metadata.
268 *
269 * @param array $metadata The metadata.
270 * @param string $version The NodeInfo version.
271 * @return array The modified metadata.
272 */
273 public static function metadata( $metadata, $version ) {
274 if ( self::VERSION !== $version ) {
275 return $metadata;
276 }
277
278 $metadata['nodeName'] = \get_bloginfo( 'name' );
279 $metadata['nodeDescription'] = \get_bloginfo( 'description' );
280 $metadata['nodeIcon'] = \get_site_icon_url();
281
282 return $metadata;
283 }
284 }
285