PluginProbe
ActivityPub / 3.2.5
ActivityPub v3.2.5
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 3.2.5, at includes/rest/class-nodeinfo.php

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