| @@ -1,58 +1,99 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * NodeInfo integration file. | |
| 4 | + * | |
| 5 | + * @package Activitypub | |
| 6 | + */ | |
| 7 | + | |
| 2 | 8 | namespace Activitypub\Integration; |
| 3 | 9 | |
| 4 | -use function Activitypub\get_total_users; | |
| 10 | +use Activitypub\Webfinger; | |
| 11 | + | |
| 5 | 12 | use function Activitypub\get_active_users; |
| 6 | 13 | use function Activitypub\get_rest_url_by_path; |
| 14 | +use function Activitypub\get_total_users; | |
| 7 | 15 | |
| 8 | 16 | /** |
| 9 | - * Compatibility with the NodeInfo plugin | |
| 17 | + * Compatibility with the NodeInfo plugin. | |
| 10 | 18 | * |
| 11 | 19 | * @see https://wordpress.org/plugins/nodeinfo/ |
| 12 | 20 | */ |
| 13 | 21 | class Nodeinfo { |
| 14 | 22 | /** |
| 15 | - * Initialize the class, registering WordPress hooks | |
| 23 | + * Initialize the class, registering WordPress hooks. | |
| 16 | 24 | */ |
| 17 | 25 | public static function init() { |
| 18 | 26 | \add_filter( 'nodeinfo_data', array( self::class, 'add_nodeinfo_data' ), 10, 2 ); |
| 19 | - \add_filter( 'nodeinfo2_data', array( self::class, 'add_nodeinfo2_data' ), 10 ); | |
| 27 | + \add_filter( 'nodeinfo2_data', array( self::class, 'add_nodeinfo2_data' ) ); | |
| 20 | 28 | |
| 21 | - \add_filter( 'wellknown_nodeinfo_data', array( self::class, 'add_wellknown_nodeinfo_data' ), 10, 2 ); | |
| 29 | + \add_filter( 'wellknown_nodeinfo_data', array( self::class, 'add_wellknown_nodeinfo_data' ) ); | |
| 22 | 30 | } |
| 23 | 31 | |
| 24 | 32 | /** |
| 25 | - * Extend NodeInfo data | |
| 33 | + * Extend NodeInfo data. | |
| 26 | 34 | * |
| 27 | - * @param array $nodeinfo NodeInfo data | |
| 28 | - * @param string The NodeInfo Version | |
| 35 | + * @param array $nodeinfo NodeInfo data. | |
| 36 | + * @param string $version The NodeInfo Version. | |
| 29 | 37 | * |
| 30 | - * @return array The extended array | |
| 38 | + * @return array The extended array. | |
| 31 | 39 | */ |
| 32 | 40 | public static function add_nodeinfo_data( $nodeinfo, $version ) { |
| 33 | - if ( $version >= '2.0' ) { | |
| 34 | - $nodeinfo['protocols'][] = 'activitypub'; | |
| 35 | - } else { | |
| 36 | - $nodeinfo['protocols']['inbound'][] = 'activitypub'; | |
| 37 | - $nodeinfo['protocols']['outbound'][] = 'activitypub'; | |
| 41 | + $nodeinfo = wp_parse_args( | |
| 42 | + $nodeinfo, | |
| 43 | + array( | |
| 44 | + 'version' => $version, | |
| 45 | + 'software' => array(), | |
| 46 | + 'usage' => array( | |
| 47 | + 'users' => array( | |
| 48 | + 'total' => 0, | |
| 49 | + 'activeMonth' => 0, | |
| 50 | + 'activeHalfyear' => 0, | |
| 51 | + ), | |
| 52 | + ), | |
| 53 | + 'protocols' => array(), | |
| 54 | + 'services' => array( | |
| 55 | + 'inbound' => array(), | |
| 56 | + 'outbound' => array(), | |
| 57 | + ), | |
| 58 | + 'metadata' => array(), | |
| 59 | + ) | |
| 60 | + ); | |
| 61 | + | |
| 62 | + if ( \version_compare( $version, '2.1', '>=' ) ) { | |
| 63 | + $nodeinfo['software']['homepage'] = 'https://wordpress.org/plugins/activitypub/'; | |
| 64 | + $nodeinfo['software']['repository'] = 'https://github.com/Automattic/wordpress-activitypub'; | |
| 38 | 65 | } |
| 39 | 66 | |
| 67 | + $nodeinfo['protocols'][] = 'activitypub'; | |
| 68 | + | |
| 69 | + $nodeinfo['services']['inbound'] = array_merge( | |
| 70 | + $nodeinfo['services']['inbound'], | |
| 71 | + array( 'gnusocial' ) | |
| 72 | + ); | |
| 73 | + $nodeinfo['services']['outbound'] = array_merge( | |
| 74 | + $nodeinfo['services']['outbound'], | |
| 75 | + array( 'friendica', 'gnusocial', 'mediagoblin', 'wordpress' ) | |
| 76 | + ); | |
| 77 | + | |
| 40 | 78 | $nodeinfo['usage']['users'] = array( |
| 41 | 79 | 'total' => get_total_users(), |
| 42 | - 'activeMonth' => get_active_users( '1 month ago' ), | |
| 43 | - 'activeHalfyear' => get_active_users( '6 month ago' ), | |
| 80 | + 'activeMonth' => get_active_users(), | |
| 81 | + 'activeHalfyear' => get_active_users( 6 ), | |
| 44 | 82 | ); |
| 45 | 83 | |
| 84 | + $nodeinfo['metadata']['federation'] = array( 'enabled' => true ); | |
| 85 | + $nodeinfo['metadata']['staffAccounts'] = self::get_staff(); | |
| 86 | + | |
| 46 | 87 | return $nodeinfo; |
| 47 | 88 | } |
| 48 | 89 | |
| 49 | 90 | /** |
| 50 | - * Extend NodeInfo2 data | |
| 91 | + * Extend NodeInfo2 data. | |
| 51 | 92 | * |
| 52 | - * @param array $nodeinfo NodeInfo2 data | |
| 93 | + * @param array $nodeinfo NodeInfo2 data. | |
| 53 | 94 | * |
| 54 | - * @return array The extended array | |
| 95 | + * @return array The extended array. | |
| 55 | 96 | */ |
| 56 | 97 | public static function add_nodeinfo2_data( $nodeinfo ) { |
| 57 | 98 | $nodeinfo['protocols'][] = 'activitypub'; |
| 58 | 99 | |
| @@ -57,10 +98,10 @@ | ||
| 57 | 98 | $nodeinfo['protocols'][] = 'activitypub'; |
| 58 | 99 | |
| 59 | 100 | $nodeinfo['usage']['users'] = array( |
| 60 | 101 | 'total' => get_total_users(), |
| 61 | - 'activeMonth' => get_active_users( '1 month ago' ), | |
| 62 | - 'activeHalfyear' => get_active_users( '6 month ago' ), | |
| 102 | + 'activeMonth' => get_active_users(), | |
| 103 | + 'activeHalfyear' => get_active_users( 6 ), | |
| 63 | 104 | ); |
| 64 | 105 | |
| 65 | 106 | return $nodeinfo; |
| 66 | 107 | } |
| @@ -65,19 +106,40 @@ | ||
| 65 | 106 | return $nodeinfo; |
| 66 | 107 | } |
| 67 | 108 | |
| 68 | 109 | /** |
| 69 | - * Extend the well-known nodeinfo data | |
| 110 | + * Extend the well-known nodeinfo data. | |
| 70 | 111 | * |
| 71 | - * @param array $data The well-known nodeinfo data | |
| 112 | + * @param array $data The well-known nodeinfo data. | |
| 72 | 113 | * |
| 73 | - * @return array The extended array | |
| 114 | + * @return array The extended array. | |
| 74 | 115 | */ |
| 75 | 116 | public static function add_wellknown_nodeinfo_data( $data ) { |
| 76 | 117 | $data['links'][] = array( |
| 77 | - 'rel' => 'https://www.w3.org/ns/activitystreams#Application', | |
| 118 | + 'rel' => 'https://www.w3.org/ns/activitystreams#Application', | |
| 78 | 119 | 'href' => get_rest_url_by_path( 'application' ), |
| 79 | 120 | ); |
| 80 | 121 | |
| 81 | 122 | return $data; |
| 123 | + } | |
| 124 | + | |
| 125 | + /** | |
| 126 | + * Get all staff accounts (admin users with the "activitypub" capability) and return them in WebFinger resource format. | |
| 127 | + * | |
| 128 | + * @return array List of staff accounts in WebFinger resource format. | |
| 129 | + */ | |
| 130 | + private static function get_staff() { | |
| 131 | + // Get all admin users with the cap activitypub. | |
| 132 | + $admins = get_users( | |
| 133 | + array( | |
| 134 | + 'role' => 'administrator', | |
| 135 | + 'orderby' => 'ID', | |
| 136 | + 'order' => 'ASC', | |
| 137 | + 'cap' => 'activitypub', | |
| 138 | + 'fields' => 'ID', | |
| 139 | + ) | |
| 140 | + ); | |
| 141 | + $admins = array_map( array( Webfinger::class, 'get_user_resource' ), $admins ); | |
| 142 | + | |
| 143 | + return array_values( array_filter( $admins ) ); | |
| 82 | 144 | } |
| 83 | 145 | } |