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