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

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

167 lines 4.0 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 array(
83 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0',
84 'href' => get_rest_url_by_path( '/nodeinfo/2.0' ),
85 ),
86 array(
87 'rel' => 'https://nodeinfo.diaspora.software/ns/schema/2.0',
88 'href' => get_rest_url_by_path( '/nodeinfo/2.0' ),
89 ),
90 array(
91 'rel' => 'https://www.w3.org/ns/activitystreams#Application',
92 'href' => get_rest_url_by_path( 'application' ),
93 ),
94 ),
95 );
96
97 return \rest_ensure_response( $response );
98 }
99
100 /**
101 * Retrieves the NodeInfo profile.
102 *
103 * @param \WP_REST_Request $request The request object.
104 * @return \WP_REST_Response Response object.
105 */
106 public function get_item( $request ) {
107 $version = $request->get_param( 'version' );
108
109 /**
110 * Fires before the NodeInfo data is created and sent to the client.
111 *
112 * @param string $version The NodeInfo version.
113 */
114 \do_action( 'activitypub_rest_nodeinfo_pre', $version );
115
116 switch ( $version ) {
117 case '2.0':
118 $response = $this->get_version_2_0();
119 break;
120
121 default:
122 $response = new \WP_Error( 'activitypub_rest_nodeinfo_invalid_version', 'Unsupported NodeInfo version.', array( 'status' => 405 ) );
123 break;
124 }
125
126 return \rest_ensure_response( $response );
127 }
128
129 /**
130 * Get the NodeInfo 2.0 data.
131 *
132 * @return array
133 */
134 public function get_version_2_0() {
135 $posts = \wp_count_posts();
136 $comments = \wp_count_comments();
137
138 return array(
139 'version' => '2.0',
140 'software' => array(
141 'name' => 'wordpress',
142 'version' => \get_bloginfo( 'version' ),
143 ),
144 'protocols' => array( 'activitypub' ),
145 'services' => array(
146 'inbound' => array(),
147 'outbound' => array(),
148 ),
149 'openRegistrations' => (bool) get_option( 'users_can_register' ),
150 'usage' => array(
151 'users' => array(
152 'total' => get_total_users(),
153 'activeHalfyear' => get_active_users( '6 month ago' ),
154 'activeMonth' => get_active_users( '1 month ago' ),
155 ),
156 'localPosts' => $posts->publish,
157 'localComments' => $comments->approved,
158 ),
159 'metadata' => array(
160 'nodeName' => \get_bloginfo( 'name' ),
161 'nodeDescription' => \get_bloginfo( 'description' ),
162 'nodeIcon' => \get_site_icon_url(),
163 ),
164 );
165 }
166 }
167