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