PluginProbe
ActivityPub / 5.3.1
ActivityPub v5.3.1
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-controller.php

class-nodeinfo-controller.php in ActivityPub 5.3.1, at includes/rest/class-nodeinfo-controller.php

172 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * NodeInfo controller file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Rest;
9
10 use function Activitypub\get_total_users;
11 use function Activitypub\get_active_users;
12 use function Activitypub\get_rest_url_by_path;
13
14 /**
15 * ActivityPub NodeInfo Controller.
16 *
17 * @author Matthias Pfefferle
18 *
19 * @see https://nodeinfo.diaspora.software/
20 */
21 class Nodeinfo_Controller extends \WP_REST_Controller {
22 /**
23 * The namespace of this controller's route.
24 *
25 * @var string
26 */
27 protected $namespace = ACTIVITYPUB_REST_NAMESPACE;
28
29 /**
30 * The REST base for this controller's route.
31 *
32 * @var string
33 */
34 protected $rest_base = 'nodeinfo';
35
36 /**
37 * Register routes.
38 */
39 public function register_routes() {
40 \register_rest_route(
41 $this->namespace,
42 '/' . $this->rest_base,
43 array(
44 array(
45 'methods' => \WP_REST_Server::READABLE,
46 'callback' => array( $this, 'get_items' ),
47 'permission_callback' => '__return_true',
48 ),
49 )
50 );
51
52 \register_rest_route(
53 $this->namespace,
54 '/' . $this->rest_base . '/(?P<version>\d\.\d)',
55 array(
56 'args' => array(
57 'version' => array(
58 'description' => 'The version of the NodeInfo schema.',
59 'type' => 'string',
60 'required' => true,
61 ),
62 ),
63 array(
64 'methods' => \WP_REST_Server::READABLE,
65 'callback' => array( $this, 'get_item' ),
66 'permission_callback' => '__return_true',
67 ),
68 )
69 );
70 }
71
72 /**
73 * Retrieves the NodeInfo discovery profile.
74 *
75 * @param \WP_REST_Request $request The request object.
76 *
77 * @return \WP_REST_Response Response object.
78 */
79 public function get_items( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
80 $response = array(
81 'links' => array(
82
83 /*
84 * Needs http protocol for spec compliance.
85 * @ticket https://github.com/Automattic/wordpress-activitypub/pull/1275
86 */
87 array(
88 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0',
89 'href' => get_rest_url_by_path( '/nodeinfo/2.0' ),
90 ),
91 array(
92 'rel' => 'https://nodeinfo.diaspora.software/ns/schema/2.0',
93 'href' => get_rest_url_by_path( '/nodeinfo/2.0' ),
94 ),
95 array(
96 'rel' => 'https://www.w3.org/ns/activitystreams#Application',
97 'href' => get_rest_url_by_path( 'application' ),
98 ),
99 ),
100 );
101
102 return \rest_ensure_response( $response );
103 }
104
105 /**
106 * Retrieves the NodeInfo profile.
107 *
108 * @param \WP_REST_Request $request The request object.
109 * @return \WP_REST_Response Response object.
110 */
111 public function get_item( $request ) {
112 $version = $request->get_param( 'version' );
113
114 /**
115 * Fires before the NodeInfo data is created and sent to the client.
116 *
117 * @param string $version The NodeInfo version.
118 */
119 \do_action( 'activitypub_rest_nodeinfo_pre', $version );
120
121 switch ( $version ) {
122 case '2.0':
123 $response = $this->get_version_2_0();
124 break;
125
126 default:
127 $response = new \WP_Error( 'activitypub_rest_nodeinfo_invalid_version', 'Unsupported NodeInfo version.', array( 'status' => 405 ) );
128 break;
129 }
130
131 return \rest_ensure_response( $response );
132 }
133
134 /**
135 * Get the NodeInfo 2.0 data.
136 *
137 * @return array
138 */
139 public function get_version_2_0() {
140 $posts = \wp_count_posts();
141 $comments = \wp_count_comments();
142
143 return array(
144 'version' => '2.0',
145 'software' => array(
146 'name' => 'wordpress',
147 'version' => \get_bloginfo( 'version' ),
148 ),
149 'protocols' => array( 'activitypub' ),
150 'services' => array(
151 'inbound' => array(),
152 'outbound' => array(),
153 ),
154 'openRegistrations' => (bool) get_option( 'users_can_register' ),
155 'usage' => array(
156 'users' => array(
157 'total' => get_total_users(),
158 'activeHalfyear' => get_active_users( '6 month ago' ),
159 'activeMonth' => get_active_users( '1 month ago' ),
160 ),
161 'localPosts' => $posts->publish,
162 'localComments' => $comments->approved,
163 ),
164 'metadata' => array(
165 'nodeName' => \get_bloginfo( 'name' ),
166 'nodeDescription' => \get_bloginfo( 'description' ),
167 'nodeIcon' => \get_site_icon_url(),
168 ),
169 );
170 }
171 }
172