laipower/wp-content/upgrade-temp-backup/plugins/activitypub/includes/rest/class-nodeinfo.php

195 lines
4.0 KiB
PHP
Raw Normal View History

2023-10-22 22:21:44 +00:00
<?php
namespace Activitypub\Rest;
2023-12-08 23:23:36 +00:00
use WP_REST_Response;
use function Activitypub\get_rest_url_by_path;
2023-10-22 22:21:44 +00:00
/**
* ActivityPub NodeInfo REST-Class
*
* @author Matthias Pfefferle
*
* @see http://nodeinfo.diaspora.software/
*/
class Nodeinfo {
/**
* Initialize the class, registering WordPress hooks
*/
public static function init() {
2023-12-08 23:23:36 +00:00
self::register_routes();
2023-10-22 22:21:44 +00:00
}
/**
* Register routes
*/
public static function register_routes() {
\register_rest_route(
2023-12-08 23:23:36 +00:00
ACTIVITYPUB_REST_NAMESPACE,
2023-10-22 22:21:44 +00:00
'/nodeinfo/discovery',
array(
array(
'methods' => \WP_REST_Server::READABLE,
2023-12-08 23:23:36 +00:00
'callback' => array( self::class, 'discovery' ),
2023-10-22 22:21:44 +00:00
'permission_callback' => '__return_true',
),
)
);
\register_rest_route(
2023-12-08 23:23:36 +00:00
ACTIVITYPUB_REST_NAMESPACE,
2023-10-22 22:21:44 +00:00
'/nodeinfo',
array(
array(
'methods' => \WP_REST_Server::READABLE,
2023-12-08 23:23:36 +00:00
'callback' => array( self::class, 'nodeinfo' ),
2023-10-22 22:21:44 +00:00
'permission_callback' => '__return_true',
),
)
);
\register_rest_route(
2023-12-08 23:23:36 +00:00
ACTIVITYPUB_REST_NAMESPACE,
2023-10-22 22:21:44 +00:00
'/nodeinfo2',
array(
array(
'methods' => \WP_REST_Server::READABLE,
2023-12-08 23:23:36 +00:00
'callback' => array( self::class, 'nodeinfo2' ),
2023-10-22 22:21:44 +00:00
'permission_callback' => '__return_true',
),
)
);
}
/**
* Render NodeInfo file
*
* @param WP_REST_Request $request
*
* @return WP_REST_Response
*/
public static function nodeinfo( $request ) {
2023-12-08 23:23:36 +00:00
/*
* Action triggerd prior to the ActivityPub profile being created and sent to the client
*/
\do_action( 'activitypub_rest_nodeinfo_pre' );
2023-10-22 22:21:44 +00:00
$nodeinfo = array();
$nodeinfo['version'] = '2.0';
$nodeinfo['software'] = array(
'name' => 'wordpress',
'version' => \get_bloginfo( 'version' ),
);
2023-12-08 23:23:36 +00:00
$users = \get_users(
array(
'capability__in' => array( 'publish_posts' ),
)
);
if ( is_array( $users ) ) {
$users = count( $users );
} else {
$users = 1;
}
2023-10-22 22:21:44 +00:00
$posts = \wp_count_posts();
$comments = \wp_count_comments();
$nodeinfo['usage'] = array(
'users' => array(
2023-12-08 23:23:36 +00:00
'total' => $users,
2023-10-22 22:21:44 +00:00
),
'localPosts' => (int) $posts->publish,
'localComments' => (int) $comments->approved,
);
$nodeinfo['openRegistrations'] = false;
$nodeinfo['protocols'] = array( 'activitypub' );
$nodeinfo['services'] = array(
'inbound' => array(),
'outbound' => array(),
);
2023-12-08 23:23:36 +00:00
return new WP_REST_Response( $nodeinfo, 200 );
2023-10-22 22:21:44 +00:00
}
/**
* Render NodeInfo file
*
* @param WP_REST_Request $request
*
* @return WP_REST_Response
*/
public static function nodeinfo2( $request ) {
2023-12-08 23:23:36 +00:00
/*
* Action triggerd prior to the ActivityPub profile being created and sent to the client
*/
\do_action( 'activitypub_rest_nodeinfo2_pre' );
2023-10-22 22:21:44 +00:00
$nodeinfo = array();
$nodeinfo['version'] = '1.0';
$nodeinfo['server'] = array(
'baseUrl' => \home_url( '/' ),
'name' => \get_bloginfo( 'name' ),
'software' => 'wordpress',
'version' => \get_bloginfo( 'version' ),
);
$users = \get_users(
array(
'capability__in' => array( 'publish_posts' ),
)
);
if ( is_array( $users ) ) {
$users = count( $users );
} else {
$users = 1;
}
$posts = \wp_count_posts();
$comments = \wp_count_comments();
$nodeinfo['usage'] = array(
'users' => array(
'total' => (int) $users,
),
'localPosts' => (int) $posts->publish,
'localComments' => (int) $comments->approved,
);
$nodeinfo['openRegistrations'] = false;
$nodeinfo['protocols'] = array( 'activitypub' );
$nodeinfo['services'] = array(
'inbound' => array(),
'outbound' => array(),
);
2023-12-08 23:23:36 +00:00
return new WP_REST_Response( $nodeinfo, 200 );
2023-10-22 22:21:44 +00:00
}
/**
* Render NodeInfo discovery file
*
* @param WP_REST_Request $request
*
* @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-12-08 23:23:36 +00:00
'href' => get_rest_url_by_path( 'nodeinfo' ),
2023-10-22 22:21:44 +00:00
),
);
return new \WP_REST_Response( $discovery, 200 );
}
}