PluginProbe
NodeInfo(2) / 1.0.8
NodeInfo(2) v1.0.8
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 / class-nodeinfo-endpoint.php

class-nodeinfo-endpoint.php in NodeInfo(2) 1.0.8, at includes/class-nodeinfo-endpoint.php

208 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class handles the NodeInfo API rest endpoints
4 */
5 class Nodeinfo_Endpoint {
6
7 /**
8 * Register the Routes.
9 */
10 public static function register_routes() {
11 register_rest_route(
12 'nodeinfo',
13 '/discovery',
14 array(
15 array(
16 'methods' => WP_REST_Server::READABLE,
17 'callback' => array( 'Nodeinfo_Endpoint', 'render_discovery' ),
18 'permission_callback' => '__return_true',
19 ),
20 )
21 );
22
23 register_rest_route(
24 'nodeinfo',
25 '/(?P<version>[\.\d]+)',
26 array(
27 array(
28 'methods' => WP_REST_Server::READABLE,
29 'callback' => array( 'Nodeinfo_Endpoint', 'render_nodeinfo' ),
30 'permission_callback' => '__return_true',
31 'args' => array(
32 'version' => array(
33 'required' => true,
34 'type' => 'string',
35 'description' => __( 'The version of the NodeInfo scheme', 'nodeinfo' ),
36 'enum' => array(
37 '1.0',
38 '1.1',
39 '2.0',
40 '2.1',
41 ),
42 ),
43 ),
44 ),
45 )
46 );
47
48 register_rest_route(
49 'nodeinfo2',
50 '/(?P<version>[\.\d]+)',
51 array(
52 array(
53 'methods' => WP_REST_Server::READABLE,
54 'callback' => array( 'Nodeinfo_Endpoint', 'render_nodeinfo2' ),
55 'permission_callback' => '__return_true',
56 'args' => array(
57 'version' => array(
58 'required' => true,
59 'type' => 'string',
60 'description' => __( 'The version of the NodeInfo2 scheme', 'nodeinfo' ),
61 'enum' => array(
62 '1.0',
63 ),
64 ),
65 ),
66 ),
67 )
68 );
69
70 register_rest_route(
71 'serviceinfo',
72 '/(?P<version>[\.\d]+)',
73 array(
74 array(
75 'methods' => WP_REST_Server::READABLE,
76 'callback' => array( 'Nodeinfo_Endpoint', 'render_serviceinfo' ),
77 'permission_callback' => '__return_true',
78 'args' => array(
79 'version' => array(
80 'required' => true,
81 'type' => 'string',
82 'description' => __( 'The version of the ServiceInfo scheme', 'nodeinfo' ),
83 'enum' => array(
84 '1.0',
85 ),
86 ),
87 ),
88 ),
89 )
90 );
91 }
92
93 /**
94 * Render the discovery file.
95 *
96 * @param WP_REST_Request $request the request object
97 * @return WP_REST_Response the response object
98 */
99 public static function render_discovery( WP_REST_Request $request ) {
100 $discovery = array();
101 $discovery['links'] = array(
102 array(
103 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.1',
104 'href' => get_rest_url( null, '/nodeinfo/2.1' ),
105 ),
106 array(
107 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0',
108 'href' => get_rest_url( null, '/nodeinfo/2.0' ),
109 ),
110 array(
111 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/1.1',
112 'href' => get_rest_url( null, '/nodeinfo/1.1' ),
113 ),
114 array(
115 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/1.0',
116 'href' => get_rest_url( null, '/nodeinfo/1.0' ),
117 ),
118 );
119
120 $discovery = apply_filters( 'wellknown_nodeinfo_data', $discovery );
121
122 // Create the response object
123 $response = new WP_REST_Response( $discovery );
124 $response->header( 'Content-Type', 'application/json; profile=http://nodeinfo.diaspora.software' );
125
126 return $response;
127 }
128
129 /**
130 * Render the NodeInfo file.
131 *
132 * @param WP_REST_Request $request the request object
133 * @return WP_REST_Response the response object
134 */
135 public static function render_nodeinfo( WP_REST_Request $request ) {
136 require_once( 'class-nodeinfo.php' );
137
138 $nodeinfo = new Nodeinfo( $request->get_param( 'version' ) );
139
140 // Create the response object
141 return new WP_REST_Response( $nodeinfo->to_array() );
142 }
143
144 /**
145 * Render the NodeInfo2 file.
146 *
147 * @param WP_REST_Request $request the request object
148 * @return WP_REST_Response the response object
149 */
150 public static function render_nodeinfo2( WP_REST_Request $request ) {
151 require_once( 'class-nodeinfo2.php' );
152
153 $nodeinfo2 = new Nodeinfo2( $request->get_param( 'version' ) );
154
155 // Create the response object
156 return new WP_REST_Response( $nodeinfo2->to_array() );
157 }
158
159 /**
160 * Render the ServiceInfo file.
161 *
162 * @param WP_REST_Request $request the request object
163 * @return WP_REST_Response the response object
164 */
165 public static function render_serviceinfo( WP_REST_Request $request ) {
166 require_once( 'class-serviceinfo.php' );
167
168 $serviceinfo = new Serviceinfo( $request->get_param( 'version' ) );
169
170 // Create the response object
171 return new WP_REST_Response( $serviceinfo->to_array() );
172 }
173
174 /**
175 * Add Host-Meta and WebFinger discovery links
176 *
177 * @param array $jrd the JRD file used by Host-Meta and WebFinger
178 * @return array the extended JRD file
179 */
180 public static function render_jrd( $jrd ) {
181 $jrd['links'][] = array(
182 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0',
183 'href' => get_rest_url( null, '/nodeinfo/2.0' ),
184 );
185
186 $jrd['links'][] = array(
187 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/1.1',
188 'href' => get_rest_url( null, '/nodeinfo/1.1' ),
189 );
190
191 $jrd['links'][] = array(
192 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/1.0',
193 'href' => get_rest_url( null, '/nodeinfo/1.0' ),
194 );
195
196 $jrd['links'][] = array(
197 'rel' => 'https://feneas.org/ns/serviceinfo',
198 'type' => 'application/ld+json',
199 'href' => get_rest_url( null, '/serviceinfo/1.0' ),
200 'properties' => array(
201 'https://feneas.org/ns/serviceinfo#software.name' => get_bloginfo( 'name', 'esc_attr' ),
202 ),
203 );
204
205 return $jrd;
206 }
207 }
208