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