PluginProbe
ActivityPub / 2.1.0
ActivityPub v2.1.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 2.1.0, at includes/rest/class-nodeinfo.php

187 lines 4.2 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 use WP_REST_Response;
5
6 use function Activitypub\get_total_users;
7 use function Activitypub\get_active_users;
8 use function Activitypub\get_rest_url_by_path;
9
10 /**
11 * ActivityPub NodeInfo REST-Class
12 *
13 * @author Matthias Pfefferle
14 *
15 * @see http://nodeinfo.diaspora.software/
16 */
17 class Nodeinfo {
18 /**
19 * Initialize the class, registering WordPress hooks
20 */
21 public static function init() {
22 self::register_routes();
23 }
24
25 /**
26 * Register routes
27 */
28 public static function register_routes() {
29 \register_rest_route(
30 ACTIVITYPUB_REST_NAMESPACE,
31 '/nodeinfo/discovery',
32 array(
33 array(
34 'methods' => \WP_REST_Server::READABLE,
35 'callback' => array( self::class, 'discovery' ),
36 'permission_callback' => '__return_true',
37 ),
38 )
39 );
40
41 \register_rest_route(
42 ACTIVITYPUB_REST_NAMESPACE,
43 '/nodeinfo',
44 array(
45 array(
46 'methods' => \WP_REST_Server::READABLE,
47 'callback' => array( self::class, 'nodeinfo' ),
48 'permission_callback' => '__return_true',
49 ),
50 )
51 );
52
53 \register_rest_route(
54 ACTIVITYPUB_REST_NAMESPACE,
55 '/nodeinfo2',
56 array(
57 array(
58 'methods' => \WP_REST_Server::READABLE,
59 'callback' => array( self::class, 'nodeinfo2' ),
60 'permission_callback' => '__return_true',
61 ),
62 )
63 );
64 }
65
66 /**
67 * Render NodeInfo file
68 *
69 * @param WP_REST_Request $request
70 *
71 * @return WP_REST_Response
72 */
73 public static function nodeinfo( $request ) {
74 /*
75 * Action triggerd prior to the ActivityPub profile being created and sent to the client
76 */
77 \do_action( 'activitypub_rest_nodeinfo_pre' );
78
79 $nodeinfo = array();
80
81 $nodeinfo['version'] = '2.0';
82 $nodeinfo['software'] = array(
83 'name' => 'wordpress',
84 'version' => \get_bloginfo( 'version' ),
85 );
86
87 $posts = \wp_count_posts();
88 $comments = \wp_count_comments();
89
90 $nodeinfo['usage'] = array(
91 'users' => array(
92 'total' => get_total_users(),
93 'activeMonth' => get_active_users( '1 month ago' ),
94 'activeHalfyear' => get_active_users( '6 month ago' ),
95 ),
96 'localPosts' => (int) $posts->publish,
97 'localComments' => (int) $comments->approved,
98 );
99
100 $nodeinfo['openRegistrations'] = false;
101 $nodeinfo['protocols'] = array( 'activitypub' );
102
103 $nodeinfo['services'] = array(
104 'inbound' => array(),
105 'outbound' => array(),
106 );
107
108 $nodeinfo['metadata'] = array(
109 'nodeName' => \get_bloginfo( 'name' ),
110 'nodeDescription' => \get_bloginfo( 'description' ),
111 'nodeIcon' => \get_site_icon_url(),
112 );
113
114 return new WP_REST_Response( $nodeinfo, 200 );
115 }
116
117 /**
118 * Render NodeInfo file
119 *
120 * @param WP_REST_Request $request
121 *
122 * @return WP_REST_Response
123 */
124 public static function nodeinfo2( $request ) {
125 /*
126 * Action triggerd prior to the ActivityPub profile being created and sent to the client
127 */
128 \do_action( 'activitypub_rest_nodeinfo2_pre' );
129
130 $nodeinfo = array();
131
132 $nodeinfo['version'] = '1.0';
133 $nodeinfo['server'] = array(
134 'baseUrl' => \home_url( '/' ),
135 'name' => \get_bloginfo( 'name' ),
136 'software' => 'wordpress',
137 'version' => \get_bloginfo( 'version' ),
138 );
139
140 $posts = \wp_count_posts();
141 $comments = \wp_count_comments();
142
143 $nodeinfo['usage'] = array(
144 'users' => array(
145 'total' => get_total_users(),
146 'activeMonth' => get_active_users( 1 ),
147 'activeHalfyear' => get_active_users( 6 ),
148 ),
149 'localPosts' => (int) $posts->publish,
150 'localComments' => (int) $comments->approved,
151 );
152
153 $nodeinfo['openRegistrations'] = false;
154 $nodeinfo['protocols'] = array( 'activitypub' );
155
156 $nodeinfo['services'] = array(
157 'inbound' => array(),
158 'outbound' => array(),
159 );
160
161 return new WP_REST_Response( $nodeinfo, 200 );
162 }
163
164 /**
165 * Render NodeInfo discovery file
166 *
167 * @param WP_REST_Request $request
168 *
169 * @return WP_REST_Response
170 */
171 public static function discovery( $request ) {
172 $discovery = array();
173 $discovery['links'] = array(
174 array(
175 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0',
176 'href' => get_rest_url_by_path( 'nodeinfo' ),
177 ),
178 array(
179 'rel' => 'https://www.w3.org/ns/activitystreams#Application',
180 'href' => get_rest_url_by_path( 'application' ),
181 ),
182 );
183
184 return new \WP_REST_Response( $discovery, 200 );
185 }
186 }
187