2018-12-09 21:54:57 +01:00
|
|
|
<?php
|
2019-02-24 12:07:41 +01:00
|
|
|
namespace Activitypub\Rest;
|
2018-12-09 21:54:57 +01:00
|
|
|
|
2019-02-24 13:01:28 +01:00
|
|
|
/**
|
|
|
|
* ActivityPub NodeInfo REST-Class
|
|
|
|
*
|
|
|
|
* @author Matthias Pfefferle
|
|
|
|
*
|
|
|
|
* @see http://nodeinfo.diaspora.software/
|
|
|
|
*/
|
2019-02-24 12:07:41 +01:00
|
|
|
class Nodeinfo {
|
2019-02-24 13:01:28 +01:00
|
|
|
/**
|
|
|
|
* Initialize the class, registering WordPress hooks
|
|
|
|
*/
|
2019-02-24 12:07:41 +01:00
|
|
|
public static function init() {
|
2020-05-23 12:34:42 +02:00
|
|
|
\add_action( 'rest_api_init', array( '\Activitypub\Rest\Nodeinfo', 'register_routes' ) );
|
|
|
|
\add_filter( 'nodeinfo_data', array( '\Activitypub\Rest\Nodeinfo', 'add_nodeinfo_discovery' ), 10, 2 );
|
|
|
|
\add_filter( 'nodeinfo2_data', array( '\Activitypub\Rest\Nodeinfo', 'add_nodeinfo2_discovery' ), 10 );
|
2019-02-24 12:07:41 +01:00
|
|
|
}
|
2019-02-28 19:31:55 +01:00
|
|
|
|
2018-12-09 21:54:57 +01:00
|
|
|
/**
|
|
|
|
* Register routes
|
|
|
|
*/
|
|
|
|
public static function register_routes() {
|
2019-09-27 10:12:59 +02:00
|
|
|
\register_rest_route(
|
2018-12-09 21:54:57 +01:00
|
|
|
'activitypub/1.0', '/nodeinfo/discovery', array(
|
|
|
|
array(
|
2019-02-24 12:07:41 +01:00
|
|
|
'methods' => \WP_REST_Server::READABLE,
|
|
|
|
'callback' => array( '\Activitypub\Rest\Nodeinfo', 'discovery' ),
|
2018-12-09 21:54:57 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
\register_rest_route(
|
2018-12-09 21:54:57 +01:00
|
|
|
'activitypub/1.0', '/nodeinfo', array(
|
|
|
|
array(
|
2019-02-24 12:07:41 +01:00
|
|
|
'methods' => \WP_REST_Server::READABLE,
|
|
|
|
'callback' => array( '\Activitypub\Rest\Nodeinfo', 'nodeinfo' ),
|
2018-12-09 21:54:57 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
\register_rest_route(
|
2018-12-09 21:54:57 +01:00
|
|
|
'activitypub/1.0', '/nodeinfo2', array(
|
|
|
|
array(
|
2019-02-24 12:07:41 +01:00
|
|
|
'methods' => \WP_REST_Server::READABLE,
|
|
|
|
'callback' => array( '\Activitypub\Rest\Nodeinfo', 'nodeinfo2' ),
|
2018-12-09 21:54:57 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render NodeInfo file
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request
|
2019-02-24 12:07:41 +01:00
|
|
|
*
|
2018-12-09 21:54:57 +01:00
|
|
|
* @return WP_REST_Response
|
|
|
|
*/
|
|
|
|
public static function nodeinfo( $request ) {
|
|
|
|
$nodeinfo = array();
|
|
|
|
|
|
|
|
$nodeinfo['version'] = '2.0';
|
|
|
|
$nodeinfo['software'] = array(
|
|
|
|
'name' => 'wordpress',
|
2019-09-27 10:12:59 +02:00
|
|
|
'version' => \get_bloginfo( 'version' ),
|
2018-12-09 21:54:57 +01:00
|
|
|
);
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
$users = \count_users();
|
|
|
|
$posts = \wp_count_posts();
|
|
|
|
$comments = \wp_count_comments();
|
2018-12-09 21:54:57 +01:00
|
|
|
|
|
|
|
$nodeinfo['usage'] = array(
|
|
|
|
'users' => array(
|
|
|
|
'total' => (int) $users['total_users'],
|
|
|
|
),
|
|
|
|
'localPosts' => (int) $posts->publish,
|
|
|
|
'localComments' => (int) $comments->approved,
|
|
|
|
);
|
|
|
|
|
|
|
|
$nodeinfo['openRegistrations'] = false;
|
2019-02-24 12:07:41 +01:00
|
|
|
$nodeinfo['protocols'] = array( 'activitypub' );
|
2018-12-09 21:54:57 +01:00
|
|
|
|
|
|
|
$nodeinfo['services'] = array(
|
|
|
|
'inbound' => array(),
|
|
|
|
'outbound' => array(),
|
|
|
|
);
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
$nodeinfo['metadata'] = array(
|
|
|
|
'email' => \get_option( 'admin_email' ),
|
|
|
|
);
|
2018-12-09 21:54:57 +01:00
|
|
|
|
2019-02-24 12:07:41 +01:00
|
|
|
return new \WP_REST_Response( $nodeinfo, 200 );
|
2018-12-09 21:54:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render NodeInfo file
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request
|
2019-02-24 12:07:41 +01:00
|
|
|
*
|
2018-12-09 21:54:57 +01:00
|
|
|
* @return WP_REST_Response
|
|
|
|
*/
|
|
|
|
public static function nodeinfo2( $request ) {
|
|
|
|
$nodeinfo = array();
|
|
|
|
|
|
|
|
$nodeinfo['version'] = '1.0';
|
2019-11-24 21:25:25 +01:00
|
|
|
$nodeinfo['server'] = array(
|
2020-05-12 20:30:06 +02:00
|
|
|
'baseUrl' => \home_url( '/' ),
|
2019-09-27 10:12:59 +02:00
|
|
|
'name' => \get_bloginfo( 'name' ),
|
2018-12-09 21:54:57 +01:00
|
|
|
'software' => 'wordpress',
|
2019-09-27 10:12:59 +02:00
|
|
|
'version' => \get_bloginfo( 'version' ),
|
2018-12-09 21:54:57 +01:00
|
|
|
);
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
$users = \count_users();
|
|
|
|
$posts = \wp_count_posts();
|
|
|
|
$comments = \wp_count_comments();
|
2018-12-09 21:54:57 +01:00
|
|
|
|
|
|
|
$nodeinfo['usage'] = array(
|
|
|
|
'users' => array(
|
|
|
|
'total' => (int) $users['total_users'],
|
|
|
|
),
|
|
|
|
'localPosts' => (int) $posts->publish,
|
|
|
|
'localComments' => (int) $comments->approved,
|
|
|
|
);
|
|
|
|
|
|
|
|
$nodeinfo['openRegistrations'] = false;
|
2019-02-24 12:07:41 +01:00
|
|
|
$nodeinfo['protocols'] = array( 'activitypub' );
|
2018-12-09 21:54:57 +01:00
|
|
|
|
|
|
|
$nodeinfo['services'] = array(
|
|
|
|
'inbound' => array(),
|
|
|
|
'outbound' => array(),
|
|
|
|
);
|
|
|
|
|
2019-08-20 13:03:06 +02:00
|
|
|
$nodeinfo['metadata'] = array(
|
2019-09-27 10:12:59 +02:00
|
|
|
'email' => \get_option( 'admin_email' ),
|
2019-08-20 13:03:06 +02:00
|
|
|
);
|
2018-12-09 21:54:57 +01:00
|
|
|
|
2019-02-24 12:07:41 +01:00
|
|
|
return new \WP_REST_Response( $nodeinfo, 200 );
|
2018-12-09 21:54:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render NodeInfo discovery file
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request
|
2019-02-24 12:07:41 +01:00
|
|
|
*
|
2018-12-09 21:54:57 +01:00
|
|
|
* @return WP_REST_Response
|
|
|
|
*/
|
|
|
|
public static function discovery( $request ) {
|
|
|
|
$discovery = array();
|
|
|
|
$discovery['links'] = array(
|
|
|
|
array(
|
|
|
|
'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0',
|
2019-09-27 10:12:59 +02:00
|
|
|
'href' => \get_rest_url( null, 'activitypub/1.0/nodeinfo' ),
|
2018-12-09 21:54:57 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2019-02-24 12:07:41 +01:00
|
|
|
return new \WP_REST_Response( $discovery, 200 );
|
2018-12-09 21:54:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extend NodeInfo data
|
|
|
|
*
|
2019-02-24 12:07:41 +01:00
|
|
|
* @param array $nodeinfo NodeInfo data
|
|
|
|
* @param string The NodeInfo Version
|
|
|
|
*
|
|
|
|
* @return array The extended array
|
2018-12-09 21:54:57 +01:00
|
|
|
*/
|
|
|
|
public static function add_nodeinfo_discovery( $nodeinfo, $version ) {
|
2019-02-24 12:07:41 +01:00
|
|
|
if ( '2.0' === $version ) {
|
2018-12-09 21:54:57 +01:00
|
|
|
$nodeinfo['protocols'][] = 'activitypub';
|
|
|
|
} else {
|
2019-09-27 10:12:59 +02:00
|
|
|
$nodeinfo['protocols']['inbound'][] = 'activitypub';
|
2018-12-09 21:54:57 +01:00
|
|
|
$nodeinfo['protocols']['outbound'][] = 'activitypub';
|
|
|
|
}
|
2019-02-24 12:07:41 +01:00
|
|
|
|
2018-12-09 21:54:57 +01:00
|
|
|
return $nodeinfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extend NodeInfo2 data
|
|
|
|
*
|
2019-02-24 12:07:41 +01:00
|
|
|
* @param array $nodeinfo NodeInfo2 data
|
|
|
|
*
|
|
|
|
* @return array The extended array
|
2018-12-09 21:54:57 +01:00
|
|
|
*/
|
|
|
|
public static function add_nodeinfo2_discovery( $nodeinfo ) {
|
|
|
|
$nodeinfo['protocols'][] = 'activitypub';
|
2019-02-24 12:07:41 +01:00
|
|
|
|
2018-12-09 21:54:57 +01:00
|
|
|
return $nodeinfo;
|
|
|
|
}
|
|
|
|
}
|