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