| 1 |
<?php |
| 2 |
namespace Activitypub\Rest; |
| 3 |
|
| 4 |
/** |
| 5 |
* ActivityPub NodeInfo REST-Class |
| 6 |
* |
| 7 |
* @author Matthias Pfefferle |
| 8 |
* |
| 9 |
* @see http://nodeinfo.diaspora.software/ |
| 10 |
*/ |
| 11 |
class Nodeinfo { |
| 12 |
/** |
| 13 |
* Initialize the class, registering WordPress hooks |
| 14 |
*/ |
| 15 |
public static function init() { |
| 16 |
add_action( 'rest_api_init', array( '\Activitypub\Rest\Nodeinfo', 'register_routes' ) ); |
| 17 |
add_filter( 'nodeinfo_data', array( '\Activitypub\Rest\Nodeinfo', 'add_nodeinfo_discovery' ), 10, 2 ); |
| 18 |
add_filter( 'nodeinfo2_data', array( '\Activitypub\Rest\Nodeinfo', 'add_nodeinfo2_discovery' ), 10 ); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Register routes |
| 23 |
*/ |
| 24 |
public static function register_routes() { |
| 25 |
register_rest_route( |
| 26 |
'activitypub/1.0', '/nodeinfo/discovery', array( |
| 27 |
array( |
| 28 |
'methods' => \WP_REST_Server::READABLE, |
| 29 |
'callback' => array( '\Activitypub\Rest\Nodeinfo', 'discovery' ), |
| 30 |
), |
| 31 |
) |
| 32 |
); |
| 33 |
|
| 34 |
register_rest_route( |
| 35 |
'activitypub/1.0', '/nodeinfo', array( |
| 36 |
array( |
| 37 |
'methods' => \WP_REST_Server::READABLE, |
| 38 |
'callback' => array( '\Activitypub\Rest\Nodeinfo', 'nodeinfo' ), |
| 39 |
), |
| 40 |
) |
| 41 |
); |
| 42 |
|
| 43 |
register_rest_route( |
| 44 |
'activitypub/1.0', '/nodeinfo2', array( |
| 45 |
array( |
| 46 |
'methods' => \WP_REST_Server::READABLE, |
| 47 |
'callback' => array( '\Activitypub\Rest\Nodeinfo', 'nodeinfo2' ), |
| 48 |
), |
| 49 |
) |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Render NodeInfo file |
| 55 |
* |
| 56 |
* @param WP_REST_Request $request |
| 57 |
* |
| 58 |
* @return WP_REST_Response |
| 59 |
*/ |
| 60 |
public static function nodeinfo( $request ) { |
| 61 |
$nodeinfo = array(); |
| 62 |
|
| 63 |
$nodeinfo['version'] = '2.0'; |
| 64 |
$nodeinfo['software'] = array( |
| 65 |
'name' => 'wordpress', |
| 66 |
'version' => get_bloginfo( 'version' ), |
| 67 |
); |
| 68 |
|
| 69 |
$users = count_users(); |
| 70 |
$posts = wp_count_posts(); |
| 71 |
$comments = wp_count_comments(); |
| 72 |
|
| 73 |
$nodeinfo['usage'] = array( |
| 74 |
'users' => array( |
| 75 |
'total' => (int) $users['total_users'], |
| 76 |
), |
| 77 |
'localPosts' => (int) $posts->publish, |
| 78 |
'localComments' => (int) $comments->approved, |
| 79 |
); |
| 80 |
|
| 81 |
$nodeinfo['openRegistrations'] = false; |
| 82 |
$nodeinfo['protocols'] = array( 'activitypub' ); |
| 83 |
|
| 84 |
$nodeinfo['services'] = array( |
| 85 |
'inbound' => array(), |
| 86 |
'outbound' => array(), |
| 87 |
); |
| 88 |
|
| 89 |
$nodeinfo['metadata'] = new \stdClass(); |
| 90 |
|
| 91 |
return new \WP_REST_Response( $nodeinfo, 200 ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Render NodeInfo file |
| 96 |
* |
| 97 |
* @param WP_REST_Request $request |
| 98 |
* |
| 99 |
* @return WP_REST_Response |
| 100 |
*/ |
| 101 |
public static function nodeinfo2( $request ) { |
| 102 |
$nodeinfo = array(); |
| 103 |
|
| 104 |
$nodeinfo['version'] = '1.0'; |
| 105 |
$nodeinfo['software'] = array( |
| 106 |
'baseUrl' => home_url( '/' ), |
| 107 |
'name' => get_bloginfo( 'name' ), |
| 108 |
'software' => 'wordpress', |
| 109 |
'version' => get_bloginfo( 'version' ), |
| 110 |
); |
| 111 |
|
| 112 |
$users = count_users(); |
| 113 |
$posts = wp_count_posts(); |
| 114 |
$comments = wp_count_comments(); |
| 115 |
|
| 116 |
$nodeinfo['usage'] = array( |
| 117 |
'users' => array( |
| 118 |
'total' => (int) $users['total_users'], |
| 119 |
), |
| 120 |
'localPosts' => (int) $posts->publish, |
| 121 |
'localComments' => (int) $comments->approved, |
| 122 |
); |
| 123 |
|
| 124 |
$nodeinfo['openRegistrations'] = false; |
| 125 |
$nodeinfo['protocols'] = array( 'activitypub' ); |
| 126 |
|
| 127 |
$nodeinfo['services'] = array( |
| 128 |
'inbound' => array(), |
| 129 |
'outbound' => array(), |
| 130 |
); |
| 131 |
|
| 132 |
$nodeinfo['metadata'] = new \stdClass(); |
| 133 |
|
| 134 |
return new \WP_REST_Response( $nodeinfo, 200 ); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Render NodeInfo discovery file |
| 139 |
* |
| 140 |
* @param WP_REST_Request $request |
| 141 |
* |
| 142 |
* @return WP_REST_Response |
| 143 |
*/ |
| 144 |
public static function discovery( $request ) { |
| 145 |
$discovery = array(); |
| 146 |
$discovery['links'] = array( |
| 147 |
array( |
| 148 |
'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0', |
| 149 |
'href' => get_rest_url( null, 'activitypub/1.0/nodeinfo' ), |
| 150 |
), |
| 151 |
); |
| 152 |
|
| 153 |
return new \WP_REST_Response( $discovery, 200 ); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Extend NodeInfo data |
| 158 |
* |
| 159 |
* @param array $nodeinfo NodeInfo data |
| 160 |
* @param string The NodeInfo Version |
| 161 |
* |
| 162 |
* @return array The extended array |
| 163 |
*/ |
| 164 |
public static function add_nodeinfo_discovery( $nodeinfo, $version ) { |
| 165 |
if ( '2.0' === $version ) { |
| 166 |
$nodeinfo['protocols'][] = 'activitypub'; |
| 167 |
} else { |
| 168 |
$nodeinfo['protocols']['inbound'][] = 'activitypub'; |
| 169 |
$nodeinfo['protocols']['outbound'][] = 'activitypub'; |
| 170 |
} |
| 171 |
|
| 172 |
return $nodeinfo; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Extend NodeInfo2 data |
| 177 |
* |
| 178 |
* @param array $nodeinfo NodeInfo2 data |
| 179 |
* |
| 180 |
* @return array The extended array |
| 181 |
*/ |
| 182 |
public static function add_nodeinfo2_discovery( $nodeinfo ) { |
| 183 |
$nodeinfo['protocols'][] = 'activitypub'; |
| 184 |
|
| 185 |
return $nodeinfo; |
| 186 |
} |
| 187 |
} |
| 188 |
|