PluginProbe
NodeInfo(2) / 1.0.4
NodeInfo(2) v1.0.4
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.4, at includes/class-nodeinfo-endpoint.php

148 lines 3.7 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', '/discovery', array(
13 array(
14 'methods' => WP_REST_Server::READABLE,
15 'callback' => array( 'Nodeinfo_Endpoint', 'render_discovery' ),
16 )
17 )
18 );
19
20 register_rest_route(
21 'nodeinfo', '/(?P<version>[\.\d]+)', array(
22 array(
23 'methods' => WP_REST_Server::READABLE,
24 'callback' => array( 'Nodeinfo_Endpoint', 'render_nodeinfo' ),
25 'args' => array(
26 'version' => array(
27 'required' => true,
28 'type' => 'string',
29 'description' => __( 'The version of the NodeInfo scheme', 'nodeinfo' ),
30 'enum' => array(
31 '1.0',
32 '1.1',
33 '2.0',
34 ),
35 )
36 ),
37 )
38 )
39 );
40
41 register_rest_route(
42 'nodeinfo2', '/(?P<version>[\.\d]+)', array(
43 array(
44 'methods' => WP_REST_Server::READABLE,
45 'callback' => array( 'Nodeinfo_Endpoint', 'render_nodeinfo2' ),
46 'args' => array(
47 'version' => array(
48 'required' => true,
49 'type' => 'string',
50 'description' => __( 'The version of the NodeInfo2 scheme', 'nodeinfo' ),
51 'enum' => array(
52 '1.0',
53 ),
54 )
55 ),
56 )
57 )
58 );
59 }
60
61 /**
62 * Render the discovery file.
63 *
64 * @param WP_REST_Request $request the request object
65 * @return WP_REST_Response the response object
66 */
67 public static function render_discovery( WP_REST_Request $request ) {
68 $discovery = array();
69 $discovery['links'] = array(
70 array(
71 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0',
72 'href' => get_rest_url( null, '/nodeinfo/2.0' ),
73 ),
74 array(
75 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/1.1',
76 'href' => get_rest_url( null, '/nodeinfo/1.1' ),
77 ),
78 array(
79 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/1.0',
80 'href' => get_rest_url( null, '/nodeinfo/1.0' ),
81 ),
82 );
83
84 $discovery = apply_filters( 'wellknown_nodeinfo_data', $discovery );
85
86 // Create the response object
87 $response = new WP_REST_Response( $discovery );
88 $response->header( 'Content-Type', 'application/json; profile=http://nodeinfo.diaspora.software' );
89
90 return $response;
91 }
92
93 /**
94 * Render the NodeInfo 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_nodeinfo( WP_REST_Request $request ) {
100 require_once( 'class-nodeinfo.php' );
101
102 $nodeinfo = new Nodeinfo( $request->get_param( 'version' ) );
103
104 // Create the response object
105 return new WP_REST_Response( $nodeinfo->to_array() );
106 }
107
108 /**
109 * Render the NodeInfo2 file.
110 *
111 * @param WP_REST_Request $request the request object
112 * @return WP_REST_Response the response object
113 */
114 public static function render_nodeinfo2( WP_REST_Request $request ) {
115 require_once( 'class-nodeinfo2.php' );
116
117 $nodeinfo2 = new Nodeinfo2( $request->get_param( 'version' ) );
118
119 // Create the response object
120 return new WP_REST_Response( $nodeinfo2->to_array() );
121 }
122
123 /**
124 * Add Host-Meta and WebFinger discovery links
125 *
126 * @param array $jrd the JRD file used by Host-Meta and WebFinger
127 * @return array the extended JRD file
128 */
129 public static function render_jrd( $jrd ) {
130 $jrd['links'][] = array(
131 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0',
132 'href' => get_rest_url( null, '/nodeinfo/2.0' ),
133 );
134
135 $jrd['links'][] = array(
136 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/1.1',
137 'href' => get_rest_url( null, '/nodeinfo/1.1' ),
138 );
139
140 $jrd['links'][] = array(
141 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/1.0',
142 'href' => get_rest_url( null, '/nodeinfo/1.0' ),
143 );
144
145 return $jrd;
146 }
147 }
148