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