PluginProbe
ActivityPub / 7.0.0
ActivityPub v7.0.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-controller.php

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

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