|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * NodeInfo 2.2 Integration. |
| 4 | + * |
| 5 | + * @package Nodeinfo |
| 6 | + * @link https://nodeinfo.diaspora.software/protocol/2.2 |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Nodeinfo\Integration; |
| 10 | + |
| 11 | +use function Nodeinfo\get_active_users; |
| 12 | +use function Nodeinfo\get_masked_version; |
| 13 | + |
| 14 | +/** |
| 15 | + * NodeInfo 2.2 Integration class. |
| 16 | + */ |
| 17 | +class Nodeinfo22 { |
| 18 | + |
| 19 | + /** |
| 20 | + * The version identifier. |
| 21 | + */ |
| 22 | + const VERSION = '2.2'; |
| 23 | + |
| 24 | + /** |
| 25 | + * Initialize the integration. |
| 26 | + */ |
| 27 | + public static function init() { |
| 28 | + add_filter( 'nodeinfo_versions', array( __CLASS__, 'register_version' ) ); |
| 29 | + add_filter( 'nodeinfo_discovery_links', array( __CLASS__, 'discovery_link' ) ); |
| 30 | + add_filter( 'nodeinfo_schema', array( __CLASS__, 'schema' ) ); |
| 31 | + add_filter( 'nodeinfo_data_software', array( __CLASS__, 'software' ), 10, 2 ); |
| 32 | + add_filter( 'nodeinfo_data_services', array( __CLASS__, 'services' ), 10, 2 ); |
| 33 | + add_filter( 'nodeinfo_data_usage', array( __CLASS__, 'usage' ), 10, 2 ); |
| 34 | + add_filter( 'nodeinfo_data_metadata', array( __CLASS__, 'metadata' ), 10, 2 ); |
| 35 | + add_filter( 'nodeinfo_data', array( __CLASS__, 'add_instance' ), 10, 2 ); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Registers the version. |
| 40 | + * |
| 41 | + * @param array $versions The versions array. |
| 42 | + * @return array The modified versions array. |
| 43 | + */ |
| 44 | + public static function register_version( $versions ) { |
| 45 | + $versions[] = self::VERSION; |
| 46 | + return $versions; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Adds the discovery link. |
| 51 | + * |
| 52 | + * @param array $links The discovery links. |
| 53 | + * @return array The modified links. |
| 54 | + */ |
| 55 | + public static function discovery_link( $links ) { |
| 56 | + $links[] = array( |
| 57 | + 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/' . self::VERSION, |
| 58 | + 'href' => get_rest_url( null, '/nodeinfo/' . self::VERSION ), |
| 59 | + ); |
| 60 | + return $links; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Adds the schema for NodeInfo 2.2. |
| 65 | + * |
| 66 | + * @param array $schema The schema. |
| 67 | + * @return array The modified schema. |
| 68 | + */ |
| 69 | + public static function schema( $schema ) { |
| 70 | + // NodeInfo 2.2 schema - adds instance and activeWeek. |
| 71 | + $schema['properties'] = array_merge( |
| 72 | + $schema['properties'], |
| 73 | + array( |
| 74 | + 'version' => array( |
| 75 | + 'description' => 'The NodeInfo schema version.', |
| 76 | + 'type' => 'string', |
| 77 | + ), |
| 78 | + 'instance' => array( |
| 79 | + 'description' => 'Metadata about this specific instance.', |
| 80 | + 'type' => 'object', |
| 81 | + 'properties' => array( |
| 82 | + 'name' => array( 'type' => 'string' ), |
| 83 | + 'description' => array( 'type' => 'string' ), |
| 84 | + ), |
| 85 | + ), |
| 86 | + 'software' => array( |
| 87 | + 'description' => 'Metadata about server software in use.', |
| 88 | + 'type' => 'object', |
| 89 | + 'properties' => array( |
| 90 | + 'name' => array( 'type' => 'string' ), |
| 91 | + 'version' => array( 'type' => 'string' ), |
| 92 | + 'repository' => array( |
| 93 | + 'type' => 'string', |
| 94 | + 'format' => 'uri', |
| 95 | + ), |
| 96 | + 'homepage' => array( |
| 97 | + 'type' => 'string', |
| 98 | + 'format' => 'uri', |
| 99 | + ), |
| 100 | + ), |
| 101 | + ), |
| 102 | + 'protocols' => array( |
| 103 | + 'description' => 'The protocols supported on this server.', |
| 104 | + 'type' => 'array', |
| 105 | + 'items' => array( 'type' => 'string' ), |
| 106 | + ), |
| 107 | + 'services' => array( |
| 108 | + 'description' => 'Third party sites this server can connect to.', |
| 109 | + 'type' => 'object', |
| 110 | + 'properties' => array( |
| 111 | + 'inbound' => array( |
| 112 | + 'type' => 'array', |
| 113 | + 'items' => array( 'type' => 'string' ), |
| 114 | + ), |
| 115 | + 'outbound' => array( |
| 116 | + 'type' => 'array', |
| 117 | + 'items' => array( 'type' => 'string' ), |
| 118 | + ), |
| 119 | + ), |
| 120 | + ), |
| 121 | + 'openRegistrations' => array( |
| 122 | + 'description' => 'Whether this server allows open self-registration.', |
| 123 | + 'type' => 'boolean', |
| 124 | + ), |
| 125 | + 'usage' => array( |
| 126 | + 'description' => 'Usage statistics for this server.', |
| 127 | + 'type' => 'object', |
| 128 | + 'properties' => array( |
| 129 | + 'users' => array( |
| 130 | + 'type' => 'object', |
| 131 | + 'properties' => array( |
| 132 | + 'total' => array( 'type' => 'integer' ), |
| 133 | + 'activeMonth' => array( 'type' => 'integer' ), |
| 134 | + 'activeHalfyear' => array( 'type' => 'integer' ), |
| 135 | + 'activeWeek' => array( 'type' => 'integer' ), |
| 136 | + ), |
| 137 | + ), |
| 138 | + 'localPosts' => array( 'type' => 'integer' ), |
| 139 | + 'localComments' => array( 'type' => 'integer' ), |
| 140 | + ), |
| 141 | + ), |
| 142 | + 'metadata' => array( |
| 143 | + 'description' => 'Free form key value pairs for software specific values.', |
| 144 | + 'type' => 'object', |
| 145 | + ), |
| 146 | + ) |
| 147 | + ); |
| 148 | + |
| 149 | + return $schema; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Adds software information. |
| 154 | + * |
| 155 | + * @param array $software The software data. |
| 156 | + * @param string $version The NodeInfo version. |
| 157 | + * @return array The modified software data. |
| 158 | + */ |
| 159 | + public static function software( $software, $version ) { |
| 160 | + if ( self::VERSION !== $version ) { |
| 161 | + return $software; |
| 162 | + } |
| 163 | + |
| 164 | + // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText -- NodeInfo spec uses lowercase. |
| 165 | + $software['name'] = 'wordpress'; |
| 166 | + $software['version'] = get_masked_version(); |
| 167 | + $software['repository'] = 'https://github.com/wordpress/wordpress'; |
| 168 | + |
| 169 | + return $software; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Adds services. |
| 174 | + * |
| 175 | + * @param array $services The services data. |
| 176 | + * @param string $version The NodeInfo version. |
| 177 | + * @return array The modified services data. |
| 178 | + */ |
| 179 | + public static function services( $services, $version ) { |
| 180 | + if ( self::VERSION !== $version ) { |
| 181 | + return $services; |
| 182 | + } |
| 183 | + |
| 184 | + $services['inbound'] = array( 'atom1.0', 'rss2.0', 'pop3' ); |
| 185 | + $services['outbound'] = array( 'atom1.0', 'rss2.0', 'wordpress', 'smtp' ); |
| 186 | + |
| 187 | + return $services; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Adds usage statistics. |
| 192 | + * |
| 193 | + * @param array $usage The usage data. |
| 194 | + * @param string $version The NodeInfo version. |
| 195 | + * @return array The modified usage data. |
| 196 | + */ |
| 197 | + public static function usage( $usage, $version ) { |
| 198 | + if ( self::VERSION !== $version ) { |
| 199 | + return $usage; |
| 200 | + } |
| 201 | + |
| 202 | + $users = get_users( |
| 203 | + array( |
| 204 | + 'fields' => 'ID', |
| 205 | + 'capability__in' => array( 'publish_posts' ), |
| 206 | + ) |
| 207 | + ); |
| 208 | + |
| 209 | + $user_count = is_array( $users ) ? count( $users ) : 1; |
| 210 | + |
| 211 | + $posts = wp_count_posts(); |
| 212 | + $comments = wp_count_comments(); |
| 213 | + |
| 214 | + $usage['users'] = array( |
| 215 | + 'total' => $user_count, |
| 216 | + 'activeMonth' => get_active_users( '1 month ago' ), |
| 217 | + 'activeHalfyear' => get_active_users( '6 month ago' ), |
| 218 | + 'activeWeek' => get_active_users( '1 week ago' ), |
| 219 | + ); |
| 220 | + |
| 221 | + $usage['localPosts'] = (int) $posts->publish; |
| 222 | + $usage['localComments'] = (int) $comments->approved; |
| 223 | + |
| 224 | + return $usage; |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Adds metadata. |
| 229 | + * |
| 230 | + * @param array $metadata The metadata. |
| 231 | + * @param string $version The NodeInfo version. |
| 232 | + * @return array The modified metadata. |
| 233 | + */ |
| 234 | + public static function metadata( $metadata, $version ) { |
| 235 | + if ( self::VERSION !== $version ) { |
| 236 | + return $metadata; |
| 237 | + } |
| 238 | + |
| 239 | + $metadata['nodeName'] = get_bloginfo( 'name' ); |
| 240 | + $metadata['nodeDescription'] = get_bloginfo( 'description' ); |
| 241 | + $metadata['nodeIcon'] = get_site_icon_url(); |
| 242 | + |
| 243 | + return $metadata; |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * Adds instance information (new in 2.2). |
| 248 | + * |
| 249 | + * @param array $nodeinfo The NodeInfo data. |
| 250 | + * @param string $version The NodeInfo version. |
| 251 | + * @return array The modified NodeInfo data. |
| 252 | + */ |
| 253 | + public static function add_instance( $nodeinfo, $version ) { |
| 254 | + if ( self::VERSION !== $version ) { |
| 255 | + return $nodeinfo; |
| 256 | + } |
| 257 | + |
| 258 | + $nodeinfo['instance'] = array( |
| 259 | + 'name' => get_bloginfo( 'name' ), |
| 260 | + 'description' => get_bloginfo( 'description' ), |
| 261 | + ); |
| 262 | + |
| 263 | + return $nodeinfo; |
| 264 | + } |
| 265 | +} |
0 commit comments