PluginProbe
NodeInfo(2) / trunk
NodeInfo(2) vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 2.0.0 2.1.0 2.1.1 2.2.0 2.3.0 2.3.1 3.0.0 3.1.0
nodeinfo / includes / controller / class-nodeinfo.php

class-nodeinfo.php in NodeInfo(2) trunk, at includes/controller/class-nodeinfo.php

197 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * NodeInfo REST Controller.
4 *
5 * @package Nodeinfo
6 */
7
8 namespace Nodeinfo\Controller;
9
10 /**
11 * NodeInfo REST Controller class.
12 *
13 * Handles NodeInfo discovery and versioned endpoints.
14 * Versions are registered dynamically via filters.
15 */
16 class Nodeinfo extends \WP_REST_Controller {
17
18 /**
19 * The namespace.
20 *
21 * @var string
22 */
23 protected $namespace = 'nodeinfo';
24
25 /**
26 * Register the routes.
27 */
28 public function register_routes() {
29 \register_rest_route(
30 $this->namespace,
31 '/discovery',
32 array(
33 array(
34 'methods' => \WP_REST_Server::READABLE,
35 'callback' => array( $this, 'get_discovery' ),
36 'permission_callback' => '__return_true',
37 ),
38 )
39 );
40
41 $versions = $this->get_versions();
42
43 if ( empty( $versions ) ) {
44 return;
45 }
46
47 \register_rest_route(
48 $this->namespace,
49 '/(?P<version>\d\.\d)',
50 array(
51 'args' => array(
52 'version' => array(
53 'description' => 'The NodeInfo schema version.',
54 'type' => 'string',
55 'enum' => $versions,
56 'required' => true,
57 ),
58 ),
59 array(
60 'methods' => \WP_REST_Server::READABLE,
61 'callback' => array( $this, 'get_item' ),
62 'permission_callback' => '__return_true',
63 ),
64 'schema' => array( $this, 'get_item_schema' ),
65 )
66 );
67 }
68
69 /**
70 * Gets registered NodeInfo versions.
71 *
72 * @return array List of version strings.
73 */
74 protected function get_versions() {
75 /**
76 * Filters the list of supported NodeInfo versions.
77 *
78 * @param array $versions List of version strings (e.g., '2.0', '2.1').
79 */
80 return \apply_filters( 'nodeinfo_versions', array() );
81 }
82
83 /**
84 * Retrieves the discovery document.
85 *
86 * @return \WP_REST_Response The response object.
87 */
88 public function get_discovery() {
89 $links = array();
90
91 /**
92 * Filters the NodeInfo discovery links.
93 *
94 * @param array $links The discovery links.
95 */
96 $links = \apply_filters( 'nodeinfo_discovery_links', $links );
97
98 $discovery = array( 'links' => $links );
99
100 /**
101 * Filters the NodeInfo discovery document.
102 *
103 * @param array $discovery The discovery document.
104 */
105 $discovery = \apply_filters( 'nodeinfo_discovery', $discovery );
106
107 $response = new \WP_REST_Response( $discovery );
108 $response->header( 'Content-Type', 'application/json; profile=http://nodeinfo.diaspora.software' );
109
110 return $response;
111 }
112
113 /**
114 * Retrieves NodeInfo for a specific version.
115 *
116 * @param \WP_REST_Request $request The request object.
117 * @return \WP_REST_Response The response object.
118 */
119 public function get_item( $request ) {
120 $version = $request->get_param( 'version' );
121
122 $nodeinfo = array(
123 'version' => $version,
124 'software' => \apply_filters( 'nodeinfo_data_software', array(), $version ),
125 'protocols' => \apply_filters( 'nodeinfo_data_protocols', array(), $version ),
126 'services' => \apply_filters(
127 'nodeinfo_data_services',
128 array(
129 'inbound' => array(),
130 'outbound' => array(),
131 ),
132 $version
133 ),
134 'openRegistrations' => (bool) \get_option( 'users_can_register', false ),
135 'usage' => \apply_filters( 'nodeinfo_data_usage', array(), $version ),
136 'metadata' => \apply_filters( 'nodeinfo_data_metadata', array(), $version ),
137 );
138
139 /**
140 * Filters the complete NodeInfo response.
141 *
142 * @param array $nodeinfo The NodeInfo data.
143 * @param string $version The NodeInfo version.
144 */
145 $nodeinfo = \apply_filters( 'nodeinfo_data', $nodeinfo, $version );
146
147 return new \WP_REST_Response( $nodeinfo );
148 }
149
150 /**
151 * Retrieves the NodeInfo schema.
152 *
153 * @return array The schema data.
154 */
155 public function get_item_schema() {
156 $schema = array(
157 '$schema' => 'http://json-schema.org/draft-04/schema#',
158 'title' => 'nodeinfo',
159 'type' => 'object',
160 'properties' => array(),
161 );
162
163 /**
164 * Filters the NodeInfo schema.
165 *
166 * @param array $schema The schema data.
167 */
168 return \apply_filters( 'nodeinfo_schema', $schema );
169 }
170
171 /**
172 * Adds NodeInfo discovery links to JRD documents.
173 *
174 * Translates the nodeinfo_discovery_links filter to JRD format
175 * for WebFinger and Host-Meta discovery.
176 *
177 * @param array $jrd The JRD document.
178 * @return array The modified JRD document.
179 */
180 public static function jrd( $jrd ) {
181 if ( ! isset( $jrd['links'] ) ) {
182 $jrd['links'] = array();
183 }
184
185 /**
186 * Filters the NodeInfo discovery links.
187 *
188 * @param array $links The discovery links.
189 */
190 $links = \apply_filters( 'nodeinfo_discovery_links', array() );
191
192 $jrd['links'] = \array_merge( $jrd['links'], $links );
193
194 return $jrd;
195 }
196 }
197