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
|
|
|
|
2023-07-25 10:47:59 +02:00
|
|
|
use WP_REST_Response;
|
|
|
|
|
2023-11-07 10:27:20 +01:00
|
|
|
use function Activitypub\get_total_users;
|
|
|
|
use function Activitypub\get_active_users;
|
2023-05-12 22:31:53 +02:00
|
|
|
use function Activitypub\get_rest_url_by_path;
|
|
|
|
|
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() {
|
2023-10-04 18:15:53 +02:00
|
|
|
self::register_routes();
|
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(
|
2023-05-11 20:25:30 +02:00
|
|
|
ACTIVITYPUB_REST_NAMESPACE,
|
2022-01-27 13:09:11 +01:00
|
|
|
'/nodeinfo/discovery',
|
|
|
|
array(
|
2018-12-09 21:54:57 +01:00
|
|
|
array(
|
2020-09-18 16:36:09 +02:00
|
|
|
'methods' => \WP_REST_Server::READABLE,
|
2023-04-20 15:22:11 +02:00
|
|
|
'callback' => array( self::class, 'discovery' ),
|
2020-09-18 16:36:09 +02:00
|
|
|
'permission_callback' => '__return_true',
|
2018-12-09 21:54:57 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
\register_rest_route(
|
2023-05-11 20:25:30 +02:00
|
|
|
ACTIVITYPUB_REST_NAMESPACE,
|
2022-01-27 13:09:11 +01:00
|
|
|
'/nodeinfo',
|
|
|
|
array(
|
2018-12-09 21:54:57 +01:00
|
|
|
array(
|
2020-09-18 16:36:09 +02:00
|
|
|
'methods' => \WP_REST_Server::READABLE,
|
2023-04-20 15:22:11 +02:00
|
|
|
'callback' => array( self::class, 'nodeinfo' ),
|
2020-09-18 16:36:09 +02:00
|
|
|
'permission_callback' => '__return_true',
|
2018-12-09 21:54:57 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
\register_rest_route(
|
2023-05-11 20:25:30 +02:00
|
|
|
ACTIVITYPUB_REST_NAMESPACE,
|
2022-01-27 13:09:11 +01:00
|
|
|
'/nodeinfo2',
|
|
|
|
array(
|
2018-12-09 21:54:57 +01:00
|
|
|
array(
|
2020-09-18 16:36:09 +02:00
|
|
|
'methods' => \WP_REST_Server::READABLE,
|
2023-04-20 15:22:11 +02:00
|
|
|
'callback' => array( self::class, 'nodeinfo2' ),
|
2020-09-18 16:36:09 +02:00
|
|
|
'permission_callback' => '__return_true',
|
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 ) {
|
2023-07-25 10:47:59 +02:00
|
|
|
/*
|
|
|
|
* Action triggerd prior to the ActivityPub profile being created and sent to the client
|
|
|
|
*/
|
|
|
|
\do_action( 'activitypub_rest_nodeinfo_pre' );
|
|
|
|
|
2018-12-09 21:54:57 +01:00
|
|
|
$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
|
|
|
$posts = \wp_count_posts();
|
|
|
|
$comments = \wp_count_comments();
|
2018-12-09 21:54:57 +01:00
|
|
|
|
|
|
|
$nodeinfo['usage'] = array(
|
|
|
|
'users' => array(
|
2023-11-07 10:27:20 +01:00
|
|
|
'total' => get_total_users(),
|
|
|
|
'activeMonth' => get_active_users( '1 month ago' ),
|
|
|
|
'activeHalfyear' => get_active_users( '6 month ago' ),
|
2018-12-09 21:54:57 +01:00
|
|
|
),
|
|
|
|
'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(),
|
|
|
|
);
|
|
|
|
|
2023-07-25 10:47:59 +02: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 ) {
|
2023-07-25 10:47:59 +02:00
|
|
|
/*
|
|
|
|
* Action triggerd prior to the ActivityPub profile being created and sent to the client
|
|
|
|
*/
|
|
|
|
\do_action( 'activitypub_rest_nodeinfo2_pre' );
|
|
|
|
|
2018-12-09 21:54:57 +01:00
|
|
|
$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
|
|
|
$posts = \wp_count_posts();
|
|
|
|
$comments = \wp_count_comments();
|
2018-12-09 21:54:57 +01:00
|
|
|
|
|
|
|
$nodeinfo['usage'] = array(
|
|
|
|
'users' => array(
|
2023-11-07 10:27:20 +01:00
|
|
|
'total' => get_total_users(),
|
|
|
|
'activeMonth' => get_active_users( 1 ),
|
|
|
|
'activeHalfyear' => get_active_users( 6 ),
|
2018-12-09 21:54:57 +01:00
|
|
|
),
|
|
|
|
'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(),
|
|
|
|
);
|
|
|
|
|
2023-07-25 10:47:59 +02: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',
|
2023-05-12 21:58:50 +02:00
|
|
|
'href' => get_rest_url_by_path( '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
|
|
|
}
|
|
|
|
}
|