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