| 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 |
|
| 71 |
/** |
| 72 |
* Render the discovery file. |
| 73 |
* |
| 74 |
* @param WP_REST_Request $request the request object |
| 75 |
* @return WP_REST_Response the response object |
| 76 |
*/ |
| 77 |
public static function render_discovery( WP_REST_Request $request ) { |
| 78 |
$discovery = array(); |
| 79 |
$discovery['links'] = array( |
| 80 |
array( |
| 81 |
'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.1', |
| 82 |
'href' => get_rest_url( null, '/nodeinfo/2.1' ), |
| 83 |
), |
| 84 |
array( |
| 85 |
'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0', |
| 86 |
'href' => get_rest_url( null, '/nodeinfo/2.0' ), |
| 87 |
), |
| 88 |
array( |
| 89 |
'rel' => 'http://nodeinfo.diaspora.software/ns/schema/1.1', |
| 90 |
'href' => get_rest_url( null, '/nodeinfo/1.1' ), |
| 91 |
), |
| 92 |
array( |
| 93 |
'rel' => 'http://nodeinfo.diaspora.software/ns/schema/1.0', |
| 94 |
'href' => get_rest_url( null, '/nodeinfo/1.0' ), |
| 95 |
), |
| 96 |
); |
| 97 |
|
| 98 |
$discovery = apply_filters( 'wellknown_nodeinfo_data', $discovery ); |
| 99 |
|
| 100 |
// Create the response object |
| 101 |
$response = new WP_REST_Response( $discovery ); |
| 102 |
$response->header( 'Content-Type', 'application/json; profile=http://nodeinfo.diaspora.software' ); |
| 103 |
|
| 104 |
return $response; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Render the NodeInfo file. |
| 109 |
* |
| 110 |
* @param WP_REST_Request $request the request object |
| 111 |
* @return WP_REST_Response the response object |
| 112 |
*/ |
| 113 |
public static function render_nodeinfo( WP_REST_Request $request ) { |
| 114 |
require_once( 'class-nodeinfo.php' ); |
| 115 |
|
| 116 |
$nodeinfo = new Nodeinfo( $request->get_param( 'version' ) ); |
| 117 |
|
| 118 |
// Create the response object |
| 119 |
return new WP_REST_Response( $nodeinfo->to_array() ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Render the NodeInfo2 file. |
| 124 |
* |
| 125 |
* @param WP_REST_Request $request the request object |
| 126 |
* @return WP_REST_Response the response object |
| 127 |
*/ |
| 128 |
public static function render_nodeinfo2( WP_REST_Request $request ) { |
| 129 |
require_once( 'class-nodeinfo2.php' ); |
| 130 |
|
| 131 |
$nodeinfo2 = new Nodeinfo2( $request->get_param( 'version' ) ); |
| 132 |
|
| 133 |
// Create the response object |
| 134 |
return new WP_REST_Response( $nodeinfo2->to_array() ); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Add Host-Meta and WebFinger discovery links |
| 139 |
* |
| 140 |
* @param array $jrd the JRD file used by Host-Meta and WebFinger |
| 141 |
* @return array the extended JRD file |
| 142 |
*/ |
| 143 |
public static function render_jrd( $jrd ) { |
| 144 |
$jrd['links'][] = array( |
| 145 |
'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.1', |
| 146 |
'href' => get_rest_url( null, '/nodeinfo/2.1' ), |
| 147 |
); |
| 148 |
|
| 149 |
$jrd['links'][] = array( |
| 150 |
'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0', |
| 151 |
'href' => get_rest_url( null, '/nodeinfo/2.0' ), |
| 152 |
); |
| 153 |
|
| 154 |
$jrd['links'][] = array( |
| 155 |
'rel' => 'http://nodeinfo.diaspora.software/ns/schema/1.1', |
| 156 |
'href' => get_rest_url( null, '/nodeinfo/1.1' ), |
| 157 |
); |
| 158 |
|
| 159 |
$jrd['links'][] = array( |
| 160 |
'rel' => 'http://nodeinfo.diaspora.software/ns/schema/1.0', |
| 161 |
'href' => get_rest_url( null, '/nodeinfo/1.0' ), |
| 162 |
); |
| 163 |
|
| 164 |
return $jrd; |
| 165 |
} |
| 166 |
} |
| 167 |
|