PluginProbe
ActivityPub / 0.9.0
ActivityPub v0.9.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 0.9.0, at includes/rest/class-nodeinfo.php

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