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

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

287 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * NodeInfo 1.1 Integration.
4 *
5 * @package Nodeinfo
6 * @link https://nodeinfo.diaspora.software/protocol/1.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 1.1 Integration class.
16 */
17 class Nodeinfo11 {
18
19 /**
20 * The version identifier.
21 */
22 const VERSION = '1.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 1.1.
65 *
66 * @link https://github.com/jhass/nodeinfo/blob/main/schemas/1.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 1.1 schema - adds hubzilla to software enum and zot to protocols.
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 'enum' => array( 'diaspora', 'friendica', 'hubzilla', 'redmatrix' ),
87 ),
88 'version' => array( 'type' => 'string' ),
89 ),
90 ),
91 'protocols' => array(
92 'description' => 'The protocols supported on this server.',
93 'type' => 'object',
94 'properties' => array(
95 'inbound' => array(
96 'type' => 'array',
97 'items' => array(
98 'type' => 'string',
99 'enum' => array( 'buddycloud', 'diaspora', 'friendica', 'gnusocial', 'libertree', 'mediagoblin', 'pumpio', 'redmatrix', 'smtp', 'tent', 'zot' ),
100 ),
101 ),
102 'outbound' => array(
103 'type' => 'array',
104 'items' => array(
105 'type' => 'string',
106 'enum' => array( 'buddycloud', 'diaspora', 'friendica', 'gnusocial', 'libertree', 'mediagoblin', 'pumpio', 'redmatrix', 'smtp', 'tent', 'zot' ),
107 ),
108 ),
109 ),
110 ),
111 'services' => array(
112 'description' => 'Third party sites this server can connect to.',
113 'type' => 'object',
114 'properties' => array(
115 'inbound' => array(
116 'type' => 'array',
117 'items' => array(
118 'type' => 'string',
119 'enum' => array( 'appnet', 'gnusocial', 'pumpio' ),
120 ),
121 ),
122 'outbound' => array(
123 'type' => 'array',
124 'items' => array(
125 'type' => 'string',
126 'enum' => array( 'appnet', 'blogger', 'buddycloud', 'diaspora', 'dreamwidth', 'drupal', 'facebook', 'friendica', 'gnusocial', 'google', 'insanejournal', 'libertree', 'linkedin', 'livejournal', 'mediagoblin', 'myspace', 'pinterest', 'posterous', 'pumpio', 'redmatrix', 'smtp', 'tent', 'tumblr', 'twitter', 'wordpress', 'xmpp' ),
127 ),
128 ),
129 ),
130 ),
131 'openRegistrations' => array(
132 'description' => 'Whether this server allows open self-registration.',
133 'type' => 'boolean',
134 ),
135 'usage' => array(
136 'description' => 'Usage statistics for this server.',
137 'type' => 'object',
138 'properties' => array(
139 'users' => array(
140 'type' => 'object',
141 'properties' => array(
142 'total' => array(
143 'type' => 'integer',
144 'minimum' => 0,
145 ),
146 'activeMonth' => array(
147 'type' => 'integer',
148 'minimum' => 0,
149 ),
150 'activeHalfyear' => array(
151 'type' => 'integer',
152 'minimum' => 0,
153 ),
154 ),
155 ),
156 'localPosts' => array(
157 'type' => 'integer',
158 'minimum' => 0,
159 ),
160 'localComments' => array(
161 'type' => 'integer',
162 'minimum' => 0,
163 ),
164 ),
165 ),
166 'metadata' => array(
167 'description' => 'Free form key value pairs for software specific values.',
168 'type' => 'object',
169 ),
170 )
171 );
172
173 return $schema;
174 }
175
176 /**
177 * Adds software information.
178 *
179 * @param array $software The software data.
180 * @param string $version The NodeInfo version.
181 * @return array The modified software data.
182 */
183 public static function software( $software, $version ) {
184 if ( self::VERSION !== $version ) {
185 return $software;
186 }
187
188 // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText -- NodeInfo spec uses lowercase.
189 $software['name'] = 'wordpress';
190 $software['version'] = get_masked_version();
191
192 return $software;
193 }
194
195 /**
196 * Adds protocols.
197 *
198 * @param array $protocols The protocols data.
199 * @param string $version The NodeInfo version.
200 * @return array The modified protocols data.
201 */
202 public static function protocols( $protocols, $version ) {
203 if ( self::VERSION !== $version ) {
204 return $protocols;
205 }
206
207 // NodeInfo 1.1 uses inbound/outbound structure.
208 $protocols['inbound'] = array();
209 $protocols['outbound'] = array();
210
211 return $protocols;
212 }
213
214 /**
215 * Adds services.
216 *
217 * @param array $services The services data.
218 * @param string $version The NodeInfo version.
219 * @return array The modified services data.
220 */
221 public static function services( $services, $version ) {
222 if ( self::VERSION !== $version ) {
223 return $services;
224 }
225
226 $services['inbound'] = array();
227 $services['outbound'] = array( 'smtp' );
228
229 return $services;
230 }
231
232 /**
233 * Adds usage statistics.
234 *
235 * @param array $usage The usage data.
236 * @param string $version The NodeInfo version.
237 * @return array The modified usage data.
238 */
239 public static function usage( $usage, $version ) {
240 if ( self::VERSION !== $version ) {
241 return $usage;
242 }
243
244 $users = \get_users(
245 array(
246 'fields' => 'ID',
247 'capability__in' => array( 'publish_posts' ),
248 )
249 );
250
251 $user_count = \is_array( $users ) ? \count( $users ) : 1;
252
253 $posts = \wp_count_posts();
254 $comments = \wp_count_comments();
255
256 $usage['users'] = array(
257 'total' => $user_count,
258 'activeMonth' => get_active_users( '1 month ago' ),
259 'activeHalfyear' => get_active_users( '6 month ago' ),
260 );
261
262 $usage['localPosts'] = (int) $posts->publish;
263 $usage['localComments'] = (int) $comments->approved;
264
265 return $usage;
266 }
267
268 /**
269 * Adds metadata.
270 *
271 * @param array $metadata The metadata.
272 * @param string $version The NodeInfo version.
273 * @return array The modified metadata.
274 */
275 public static function metadata( $metadata, $version ) {
276 if ( self::VERSION !== $version ) {
277 return $metadata;
278 }
279
280 $metadata['nodeName'] = \get_bloginfo( 'name' );
281 $metadata['nodeDescription'] = \get_bloginfo( 'description' );
282 $metadata['nodeIcon'] = \get_site_icon_url();
283
284 return $metadata;
285 }
286 }
287