| @@ -1,20 +1,23 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * ActivityPub Class. | |
| 4 | - * | |
| 5 | - * @package Activitypub | |
| 6 | - */ | |
| 7 | - | |
| 8 | 2 | namespace Activitypub; |
| 9 | 3 | |
| 4 | +use Exception; | |
| 5 | +use Activitypub\Signature; | |
| 6 | +use Activitypub\Collection\Users; | |
| 10 | 7 | use Activitypub\Collection\Followers; |
| 11 | -use Activitypub\Collection\Following; | |
| 12 | -use Activitypub\Collection\Remote_Posts; | |
| 13 | -use Activitypub\OAuth\Client; | |
| 8 | +use Activitypub\Collection\Extra_Fields; | |
| 14 | 9 | |
| 10 | +use function Activitypub\is_comment; | |
| 11 | +use function Activitypub\sanitize_url; | |
| 12 | +use function Activitypub\is_local_comment; | |
| 13 | +use function Activitypub\site_supports_blocks; | |
| 14 | +use function Activitypub\is_user_type_disabled; | |
| 15 | +use function Activitypub\is_activitypub_request; | |
| 16 | +use function Activitypub\should_comment_be_federated; | |
| 17 | + | |
| 15 | 18 | /** |
| 16 | - * ActivityPub Class. | |
| 19 | + * ActivityPub Class | |
| 17 | 20 | * |
| 18 | 21 | * @author Matthias Pfefferle |
| 19 | 22 | */ |
| 20 | 23 | class Activitypub { |
| @@ -21,89 +24,291 @@ | ||
| 21 | 24 | /** |
| 22 | 25 | * Initialize the class, registering WordPress hooks. |
| 23 | 26 | */ |
| 24 | 27 | public static function init() { |
| 25 | - \add_action( 'init', array( self::class, 'theme_compat' ), 11 ); | |
| 26 | - \add_action( 'init', array( self::class, 'register_user_meta' ), 11 ); | |
| 28 | + \add_filter( 'template_include', array( self::class, 'render_json_template' ), 99 ); | |
| 29 | + \add_action( 'template_redirect', array( self::class, 'template_redirect' ) ); | |
| 30 | + \add_filter( 'query_vars', array( self::class, 'add_query_vars' ) ); | |
| 31 | + \add_filter( 'pre_get_avatar_data', array( self::class, 'pre_get_avatar_data' ), 11, 2 ); | |
| 27 | 32 | |
| 33 | + // Add support for ActivityPub to custom post types | |
| 34 | + $post_types = \get_option( 'activitypub_support_post_types', array( 'post' ) ) ? \get_option( 'activitypub_support_post_types', array( 'post' ) ) : array(); | |
| 35 | + | |
| 36 | + foreach ( $post_types as $post_type ) { | |
| 37 | + \add_post_type_support( $post_type, 'activitypub' ); | |
| 38 | + } | |
| 39 | + | |
| 28 | 40 | \add_action( 'wp_trash_post', array( self::class, 'trash_post' ), 1 ); |
| 29 | 41 | \add_action( 'untrash_post', array( self::class, 'untrash_post' ), 1 ); |
| 30 | 42 | |
| 43 | + \add_action( 'init', array( self::class, 'add_rewrite_rules' ), 11 ); | |
| 44 | + \add_action( 'init', array( self::class, 'theme_compat' ), 11 ); | |
| 45 | + | |
| 31 | 46 | \add_action( 'user_register', array( self::class, 'user_register' ) ); |
| 32 | 47 | |
| 33 | - \add_action( 'activitypub_add_user_block', array( Followers::class, 'remove_blocked_actors' ), 10, 3 ); | |
| 34 | - \add_action( 'activitypub_add_user_block', array( Following::class, 'remove_blocked_actors' ), 10, 3 ); | |
| 48 | + \add_action( 'in_plugin_update_message-' . ACTIVITYPUB_PLUGIN_BASENAME, array( self::class, 'plugin_update_message' ) ); | |
| 49 | + | |
| 50 | + if ( site_supports_blocks() ) { | |
| 51 | + \add_action( 'tool_box', array( self::class, 'tool_box' ) ); | |
| 52 | + } | |
| 53 | + | |
| 54 | + \add_filter( 'activitypub_get_actor_extra_fields', array( Extra_Fields::class, 'default_actor_extra_fields' ), 10, 2 ); | |
| 55 | + | |
| 56 | + // register several post_types | |
| 57 | + self::register_post_types(); | |
| 35 | 58 | } |
| 36 | 59 | |
| 37 | 60 | /** |
| 38 | - * Activation Hook. | |
| 61 | + * Activation Hook | |
| 39 | 62 | * |
| 40 | - * @param bool $network_wide Whether to activate the plugin for all sites in the network or just the current site. | |
| 63 | + * @return void | |
| 41 | 64 | */ |
| 42 | - public static function activate( $network_wide ) { | |
| 65 | + public static function activate() { | |
| 43 | 66 | self::flush_rewrite_rules(); |
| 44 | 67 | Scheduler::register_schedules(); |
| 68 | + } | |
| 45 | 69 | |
| 46 | - \add_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ), 5, 3 ); | |
| 47 | - Migration::update_comment_counts(); | |
| 70 | + /** | |
| 71 | + * Deactivation Hook | |
| 72 | + * | |
| 73 | + * @return void | |
| 74 | + */ | |
| 75 | + public static function deactivate() { | |
| 76 | + self::flush_rewrite_rules(); | |
| 77 | + Scheduler::deregister_schedules(); | |
| 78 | + } | |
| 48 | 79 | |
| 49 | - if ( \is_multisite() && $network_wide && ! \wp_is_large_network() ) { | |
| 50 | - $sites = \get_sites( array( 'fields' => 'ids' ) ); | |
| 51 | - foreach ( $sites as $site ) { | |
| 52 | - \switch_to_blog( $site ); | |
| 53 | - self::flush_rewrite_rules(); | |
| 54 | - \restore_current_blog(); | |
| 80 | + /** | |
| 81 | + * Uninstall Hook | |
| 82 | + * | |
| 83 | + * @return void | |
| 84 | + */ | |
| 85 | + public static function uninstall() { | |
| 86 | + Scheduler::deregister_schedules(); | |
| 87 | + } | |
| 88 | + | |
| 89 | + /** | |
| 90 | + * Return a AS2 JSON version of an author, post or page. | |
| 91 | + * | |
| 92 | + * @param string $template The path to the template object. | |
| 93 | + * | |
| 94 | + * @return string The new path to the JSON template. | |
| 95 | + */ | |
| 96 | + public static function render_json_template( $template ) { | |
| 97 | + if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { | |
| 98 | + return $template; | |
| 99 | + } | |
| 100 | + | |
| 101 | + if ( ! is_activitypub_request() ) { | |
| 102 | + return $template; | |
| 103 | + } | |
| 104 | + | |
| 105 | + $json_template = false; | |
| 106 | + | |
| 107 | + if ( \is_author() && ! is_user_disabled( \get_the_author_meta( 'ID' ) ) ) { | |
| 108 | + $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/user-json.php'; | |
| 109 | + } elseif ( is_comment() ) { | |
| 110 | + $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/comment-json.php'; | |
| 111 | + } elseif ( \is_singular() ) { | |
| 112 | + $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/post-json.php'; | |
| 113 | + } elseif ( \is_home() && ! is_user_type_disabled( 'blog' ) ) { | |
| 114 | + $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/blog-json.php'; | |
| 115 | + } | |
| 116 | + | |
| 117 | + /* | |
| 118 | + * Check if the request is authorized. | |
| 119 | + * | |
| 120 | + * @see https://www.w3.org/wiki/SocialCG/ActivityPub/Primer/Authentication_Authorization#Authorized_fetch | |
| 121 | + * @see https://swicg.github.io/activitypub-http-signature/#authorized-fetch | |
| 122 | + */ | |
| 123 | + if ( $json_template && ACTIVITYPUB_AUTHORIZED_FETCH ) { | |
| 124 | + $verification = Signature::verify_http_signature( $_SERVER ); | |
| 125 | + if ( \is_wp_error( $verification ) ) { | |
| 126 | + header( 'HTTP/1.1 401 Unauthorized' ); | |
| 127 | + | |
| 128 | + // fallback as template_loader can't return http headers | |
| 129 | + return $template; | |
| 55 | 130 | } |
| 56 | 131 | } |
| 132 | + | |
| 133 | + if ( $json_template ) { | |
| 134 | + return $json_template; | |
| 135 | + } | |
| 136 | + | |
| 137 | + return $template; | |
| 57 | 138 | } |
| 58 | 139 | |
| 59 | 140 | /** |
| 60 | - * Deactivation Hook. | |
| 141 | + * Add the 'self' link to the header. | |
| 61 | 142 | * |
| 62 | - * @param bool $network_wide Whether to deactivate the plugin for all sites in the network or just the current site. | |
| 143 | + * @see | |
| 144 | + * | |
| 145 | + * @return void | |
| 63 | 146 | */ |
| 64 | - public static function deactivate( $network_wide ) { | |
| 65 | - self::flush_rewrite_rules(); | |
| 66 | - Scheduler::deregister_schedules(); | |
| 147 | + public static function add_headers() { | |
| 148 | + // phpcs:ignore | |
| 149 | + $request_uri = $_SERVER['REQUEST_URI']; | |
| 67 | 150 | |
| 68 | - \remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ), 5 ); | |
| 69 | - Migration::update_comment_counts( 2000 ); | |
| 151 | + if ( ! $request_uri ) { | |
| 152 | + return; | |
| 153 | + } | |
| 70 | 154 | |
| 71 | - if ( \is_multisite() && $network_wide && ! \wp_is_large_network() ) { | |
| 72 | - $sites = \get_sites( array( 'fields' => 'ids' ) ); | |
| 73 | - foreach ( $sites as $site ) { | |
| 74 | - \switch_to_blog( $site ); | |
| 75 | - self::flush_rewrite_rules(); | |
| 76 | - \restore_current_blog(); | |
| 155 | + // only add self link to author pages... | |
| 156 | + if ( is_author() ) { | |
| 157 | + if ( is_user_disabled( get_queried_object_id() ) ) { | |
| 158 | + return; | |
| 77 | 159 | } |
| 160 | + } elseif ( is_singular() ) { // or posts/pages/custom-post-types... | |
| 161 | + if ( ! \post_type_supports( \get_post_type(), 'activitypub' ) ) { | |
| 162 | + return; | |
| 163 | + } | |
| 164 | + } else { // otherwise return | |
| 165 | + return; | |
| 78 | 166 | } |
| 167 | + | |
| 168 | + // add self link to html and http header | |
| 169 | + $host = wp_parse_url( home_url() ); | |
| 170 | + $self_link = esc_url( | |
| 171 | + apply_filters( | |
| 172 | + 'self_link', | |
| 173 | + set_url_scheme( | |
| 174 | + // phpcs:ignore | |
| 175 | + 'http://' . $host['host'] . wp_unslash( $request_uri ) | |
| 176 | + ) | |
| 177 | + ) | |
| 178 | + ); | |
| 179 | + | |
| 180 | + if ( ! headers_sent() ) { | |
| 181 | + header( 'Link: <' . $self_link . '>; rel="alternate"; type="application/activity+json"' ); | |
| 182 | + } | |
| 183 | + | |
| 184 | + add_action( | |
| 185 | + 'wp_head', | |
| 186 | + function () use ( $self_link ) { | |
| 187 | + echo PHP_EOL . '<link rel="alternate" type="application/activity+json" href="' . esc_url( $self_link ) . '" />' . PHP_EOL; | |
| 188 | + } | |
| 189 | + ); | |
| 79 | 190 | } |
| 80 | 191 | |
| 81 | 192 | /** |
| 82 | - * Uninstall Hook. | |
| 193 | + * Custom redirects for ActivityPub requests. | |
| 194 | + * | |
| 195 | + * @return void | |
| 83 | 196 | */ |
| 84 | - public static function uninstall() { | |
| 85 | - Scheduler::deregister_schedules(); | |
| 197 | + public static function template_redirect() { | |
| 198 | + self::add_headers(); | |
| 86 | 199 | |
| 87 | - \remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ), 5 ); | |
| 88 | - Migration::update_comment_counts( 2000 ); | |
| 200 | + $comment_id = get_query_var( 'c', null ); | |
| 89 | 201 | |
| 90 | - Remote_Posts::delete_all(); | |
| 91 | - Tombstone::delete_all(); | |
| 92 | - Client::delete_all(); | |
| 202 | + // check if it seems to be a comment | |
| 203 | + if ( ! $comment_id ) { | |
| 204 | + return; | |
| 205 | + } | |
| 93 | 206 | |
| 94 | - Options::delete(); | |
| 207 | + $comment = get_comment( $comment_id ); | |
| 208 | + | |
| 209 | + // load a 404 page if `c` is set but not valid | |
| 210 | + if ( ! $comment ) { | |
| 211 | + global $wp_query; | |
| 212 | + $wp_query->set_404(); | |
| 213 | + return; | |
| 214 | + } | |
| 215 | + | |
| 216 | + // stop if it's not an ActivityPub comment | |
| 217 | + if ( is_activitypub_request() && ! is_local_comment( $comment ) ) { | |
| 218 | + return; | |
| 219 | + } | |
| 220 | + | |
| 221 | + wp_safe_redirect( get_comment_link( $comment ) ); | |
| 222 | + exit; | |
| 95 | 223 | } |
| 96 | 224 | |
| 97 | 225 | /** |
| 226 | + * Add the 'activitypub' query variable so WordPress won't mangle it. | |
| 227 | + */ | |
| 228 | + public static function add_query_vars( $vars ) { | |
| 229 | + $vars[] = 'activitypub'; | |
| 230 | + $vars[] = 'c'; | |
| 231 | + $vars[] = 'p'; | |
| 232 | + | |
| 233 | + return $vars; | |
| 234 | + } | |
| 235 | + | |
| 236 | + /** | |
| 237 | + * Replaces the default avatar. | |
| 238 | + * | |
| 239 | + * @param array $args Arguments passed to get_avatar_data(), after processing. | |
| 240 | + * @param int|string|object $id_or_email A user ID, email address, or comment object. | |
| 241 | + * | |
| 242 | + * @return array $args | |
| 243 | + */ | |
| 244 | + public static function pre_get_avatar_data( $args, $id_or_email ) { | |
| 245 | + if ( | |
| 246 | + ! $id_or_email instanceof \WP_Comment || | |
| 247 | + ! isset( $id_or_email->comment_type ) || | |
| 248 | + $id_or_email->user_id | |
| 249 | + ) { | |
| 250 | + return $args; | |
| 251 | + } | |
| 252 | + | |
| 253 | + $allowed_comment_types = \apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); | |
| 254 | + if ( | |
| 255 | + ! empty( $id_or_email->comment_type ) && | |
| 256 | + ! \in_array( | |
| 257 | + $id_or_email->comment_type, | |
| 258 | + (array) $allowed_comment_types, | |
| 259 | + true | |
| 260 | + ) | |
| 261 | + ) { | |
| 262 | + $args['url'] = false; | |
| 263 | + /** This filter is documented in wp-includes/link-template.php */ | |
| 264 | + return \apply_filters( 'get_avatar_data', $args, $id_or_email ); | |
| 265 | + } | |
| 266 | + | |
| 267 | + // Check if comment has an avatar. | |
| 268 | + $avatar = self::get_avatar_url( $id_or_email->comment_ID ); | |
| 269 | + | |
| 270 | + if ( $avatar ) { | |
| 271 | + if ( empty( $args['class'] ) ) { | |
| 272 | + $args['class'] = array(); | |
| 273 | + } elseif ( \is_string( $args['class'] ) ) { | |
| 274 | + $args['class'] = \explode( ' ', $args['class'] ); | |
| 275 | + } | |
| 276 | + | |
| 277 | + $args['url'] = $avatar; | |
| 278 | + $args['class'][] = 'avatar-activitypub'; | |
| 279 | + $args['class'][] = 'u-photo'; | |
| 280 | + $args['class'] = \array_unique( $args['class'] ); | |
| 281 | + } | |
| 282 | + | |
| 283 | + return $args; | |
| 284 | + } | |
| 285 | + | |
| 286 | + /** | |
| 287 | + * Function to retrieve Avatar URL if stored in meta. | |
| 288 | + * | |
| 289 | + * @param int|WP_Comment $comment | |
| 290 | + * | |
| 291 | + * @return string $url | |
| 292 | + */ | |
| 293 | + public static function get_avatar_url( $comment ) { | |
| 294 | + if ( \is_numeric( $comment ) ) { | |
| 295 | + $comment = \get_comment( $comment ); | |
| 296 | + } | |
| 297 | + return \get_comment_meta( $comment->comment_ID, 'avatar_url', true ); | |
| 298 | + } | |
| 299 | + | |
| 300 | + /** | |
| 98 | 301 | * Store permalink in meta, to send delete Activity. |
| 99 | 302 | * |
| 100 | 303 | * @param string $post_id The Post ID. |
| 304 | + * | |
| 305 | + * @return void | |
| 101 | 306 | */ |
| 102 | 307 | public static function trash_post( $post_id ) { |
| 103 | 308 | \add_post_meta( |
| 104 | 309 | $post_id, |
| 105 | - '_activitypub_canonical_url', | |
| 310 | + 'activitypub_canonical_url', | |
| 106 | 311 | \get_permalink( $post_id ), |
| 107 | 312 | true |
| 108 | 313 | ); |
| 109 | 314 | } |
| @@ -108,40 +313,84 @@ | ||
| 108 | 313 | ); |
| 109 | 314 | } |
| 110 | 315 | |
| 111 | 316 | /** |
| 112 | - * Delete permalink from meta. | |
| 317 | + * Delete permalink from meta | |
| 113 | 318 | * |
| 114 | - * @param string $post_id The Post ID. | |
| 319 | + * @param string $post_id The Post ID | |
| 320 | + * | |
| 321 | + * @return void | |
| 115 | 322 | */ |
| 116 | 323 | public static function untrash_post( $post_id ) { |
| 117 | - \delete_post_meta( $post_id, '_activitypub_canonical_url' ); | |
| 324 | + \delete_post_meta( $post_id, 'activitypub_canonical_url' ); | |
| 118 | 325 | } |
| 119 | 326 | |
| 120 | 327 | /** |
| 121 | - * Flush rewrite rules. | |
| 328 | + * Add rewrite rules | |
| 122 | 329 | */ |
| 330 | + public static function add_rewrite_rules() { | |
| 331 | + // If another system needs to take precedence over the ActivityPub rewrite rules, | |
| 332 | + // they can define their own and will manually call the appropriate functions as required. | |
| 333 | + if ( ACTIVITYPUB_DISABLE_REWRITES ) { | |
| 334 | + return; | |
| 335 | + } | |
| 336 | + | |
| 337 | + if ( ! \class_exists( 'Webfinger' ) ) { | |
| 338 | + \add_rewrite_rule( | |
| 339 | + '^.well-known/webfinger', | |
| 340 | + 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/webfinger', | |
| 341 | + 'top' | |
| 342 | + ); | |
| 343 | + } | |
| 344 | + | |
| 345 | + if ( ! \class_exists( 'Nodeinfo_Endpoint' ) && true === (bool) \get_option( 'blog_public', 1 ) ) { | |
| 346 | + \add_rewrite_rule( | |
| 347 | + '^.well-known/nodeinfo', | |
| 348 | + 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo/discovery', | |
| 349 | + 'top' | |
| 350 | + ); | |
| 351 | + \add_rewrite_rule( | |
| 352 | + '^.well-known/x-nodeinfo2', | |
| 353 | + 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo2', | |
| 354 | + 'top' | |
| 355 | + ); | |
| 356 | + } | |
| 357 | + | |
| 358 | + \add_rewrite_rule( | |
| 359 | + '^@([\w\-\.]+)', | |
| 360 | + 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/actors/$matches[1]', | |
| 361 | + 'top' | |
| 362 | + ); | |
| 363 | + | |
| 364 | + \add_rewrite_endpoint( 'activitypub', EP_AUTHORS | EP_PERMALINK | EP_PAGES ); | |
| 365 | + } | |
| 366 | + | |
| 367 | + /** | |
| 368 | + * Flush rewrite rules; | |
| 369 | + */ | |
| 123 | 370 | public static function flush_rewrite_rules() { |
| 124 | - Router::add_rewrite_rules(); | |
| 371 | + self::add_rewrite_rules(); | |
| 125 | 372 | \flush_rewrite_rules(); |
| 126 | 373 | } |
| 127 | 374 | |
| 128 | 375 | /** |
| 129 | - * Add rewrite rules. | |
| 376 | + * Adds metabox on wp-admin/tools.php | |
| 130 | 377 | * |
| 131 | - * @deprecated 7.5.0 Use {@see Router::add_rewrite_rules()}. | |
| 378 | + * @return void | |
| 132 | 379 | */ |
| 133 | - public static function add_rewrite_rules() { | |
| 134 | - _deprecated_function( __FUNCTION__, '7.5.0', '\Activitypub\Router::add_rewrite_rules()' ); | |
| 135 | - | |
| 136 | - Router::add_rewrite_rules(); | |
| 380 | + public static function tool_box() { | |
| 381 | + if ( \current_user_can( 'edit_posts' ) ) { | |
| 382 | + \load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/toolbox.php' ); | |
| 383 | + } | |
| 137 | 384 | } |
| 138 | 385 | |
| 139 | 386 | /** |
| 140 | - * Theme compatibility stuff. | |
| 387 | + * Theme compatibility stuff | |
| 388 | + * | |
| 389 | + * @return void | |
| 141 | 390 | */ |
| 142 | 391 | public static function theme_compat() { |
| 143 | - // We assume that you want to use Post-Formats when enabling the setting. | |
| 392 | + // We assume that you want to use Post-Formats when enabling the setting | |
| 144 | 393 | if ( 'wordpress-post-format' === \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE ) ) { |
| 145 | 394 | if ( ! get_theme_support( 'post-formats' ) ) { |
| 146 | 395 | // Add support for the Aside, Gallery Post Formats... |
| 147 | 396 | add_theme_support( |
| @@ -158,243 +407,149 @@ | ||
| 158 | 407 | } |
| 159 | 408 | } |
| 160 | 409 | |
| 161 | 410 | /** |
| 162 | - * Add the 'activitypub' capability to users who can publish posts. | |
| 411 | + * Display plugin upgrade notice to users | |
| 163 | 412 | * |
| 164 | - * @param int $user_id User ID. | |
| 413 | + * @param array $data The plugin data | |
| 414 | + * | |
| 415 | + * @return void | |
| 165 | 416 | */ |
| 166 | - public static function user_register( $user_id ) { | |
| 167 | - if ( \user_can( $user_id, 'publish_posts' ) ) { | |
| 168 | - $user = \get_user_by( 'id', $user_id ); | |
| 169 | - $user->add_cap( 'activitypub' ); | |
| 417 | + public static function plugin_update_message( $data ) { | |
| 418 | + if ( ! isset( $data['upgrade_notice'] ) ) { | |
| 419 | + return; | |
| 170 | 420 | } |
| 421 | + | |
| 422 | + printf( | |
| 423 | + '<div class="update-message">%s</div>', | |
| 424 | + wp_kses( | |
| 425 | + wpautop( $data['upgrade_notice '] ), | |
| 426 | + array( | |
| 427 | + 'p' => array(), | |
| 428 | + 'a' => array( 'href', 'title' ), | |
| 429 | + 'strong' => array(), | |
| 430 | + 'em' => array(), | |
| 431 | + ) | |
| 432 | + ) | |
| 433 | + ); | |
| 171 | 434 | } |
| 172 | 435 | |
| 173 | 436 | /** |
| 174 | - * Register user meta. | |
| 437 | + * Register the "Followers" Taxonomy | |
| 438 | + * | |
| 439 | + * @return void | |
| 175 | 440 | */ |
| 176 | - public static function register_user_meta() { | |
| 177 | - $blog_prefix = $GLOBALS['wpdb']->get_blog_prefix(); | |
| 178 | - | |
| 179 | - \register_meta( | |
| 180 | - 'user', | |
| 181 | - $blog_prefix . 'activitypub_also_known_as', | |
| 441 | + private static function register_post_types() { | |
| 442 | + \register_post_type( | |
| 443 | + Followers::POST_TYPE, | |
| 182 | 444 | array( |
| 183 | - 'type' => 'array', | |
| 184 | - 'description' => 'An array of URLs that the user is known by.', | |
| 185 | - 'single' => true, | |
| 186 | - 'default' => array(), | |
| 187 | - 'sanitize_callback' => array( Sanitize::class, 'identifier_list' ), | |
| 445 | + 'labels' => array( | |
| 446 | + 'name' => _x( 'Followers', 'post_type plural name', 'activitypub' ), | |
| 447 | + 'singular_name' => _x( 'Follower', 'post_type single name', 'activitypub' ), | |
| 448 | + ), | |
| 449 | + 'public' => false, | |
| 450 | + 'hierarchical' => false, | |
| 451 | + 'rewrite' => false, | |
| 452 | + 'query_var' => false, | |
| 453 | + 'delete_with_user' => false, | |
| 454 | + 'can_export' => true, | |
| 455 | + 'supports' => array(), | |
| 188 | 456 | ) |
| 189 | 457 | ); |
| 190 | 458 | |
| 191 | - \register_meta( | |
| 192 | - 'user', | |
| 193 | - $blog_prefix . 'activitypub_hide_social_graph', | |
| 459 | + \register_post_meta( | |
| 460 | + Followers::POST_TYPE, | |
| 461 | + 'activitypub_inbox', | |
| 194 | 462 | array( |
| 195 | - 'type' => 'integer', | |
| 196 | - 'description' => 'Hide Followers and Followings on Profile.', | |
| 197 | - 'single' => true, | |
| 198 | - 'default' => 0, | |
| 199 | - 'sanitize_callback' => 'absint', | |
| 200 | - 'show_in_rest' => true, | |
| 201 | - ) | |
| 202 | - ); | |
| 203 | - | |
| 204 | - \register_meta( | |
| 205 | - 'user', | |
| 206 | - $blog_prefix . 'activitypub_old_host_data', | |
| 207 | - array( | |
| 208 | - 'description' => 'Actor object for the user on the old host.', | |
| 209 | - 'single' => true, | |
| 210 | - ) | |
| 211 | - ); | |
| 212 | - | |
| 213 | - \register_meta( | |
| 214 | - 'user', | |
| 215 | - $blog_prefix . 'activitypub_moved_to', | |
| 216 | - array( | |
| 217 | 463 | 'type' => 'string', |
| 218 | - 'description' => 'The new URL of the user.', | |
| 219 | 464 | 'single' => true, |
| 220 | 465 | 'sanitize_callback' => 'sanitize_url', |
| 221 | 466 | ) |
| 222 | 467 | ); |
| 223 | 468 | |
| 224 | - \register_meta( | |
| 225 | - 'user', | |
| 226 | - $blog_prefix . 'activitypub_description', | |
| 469 | + \register_post_meta( | |
| 470 | + Followers::POST_TYPE, | |
| 471 | + 'activitypub_errors', | |
| 227 | 472 | array( |
| 228 | 473 | 'type' => 'string', |
| 229 | - 'description' => 'The user description.', | |
| 230 | - 'single' => true, | |
| 231 | - 'default' => '', | |
| 232 | - 'sanitize_callback' => static function ( $value ) { | |
| 233 | - return wp_kses( $value, 'user_description' ); | |
| 474 | + 'single' => false, | |
| 475 | + 'sanitize_callback' => function ( $value ) { | |
| 476 | + if ( ! is_string( $value ) ) { | |
| 477 | + throw new Exception( 'Error message is no valid string' ); | |
| 478 | + } | |
| 479 | + | |
| 480 | + return esc_sql( $value ); | |
| 234 | 481 | }, |
| 235 | 482 | ) |
| 236 | 483 | ); |
| 237 | 484 | |
| 238 | - \register_meta( | |
| 239 | - 'user', | |
| 240 | - $blog_prefix . 'activitypub_icon', | |
| 485 | + \register_post_meta( | |
| 486 | + Followers::POST_TYPE, | |
| 487 | + 'activitypub_user_id', | |
| 241 | 488 | array( |
| 242 | - 'type' => 'integer', | |
| 243 | - 'description' => 'The attachment ID for user profile image.', | |
| 244 | - 'single' => true, | |
| 245 | - 'default' => 0, | |
| 246 | - 'sanitize_callback' => 'absint', | |
| 489 | + 'type' => 'string', | |
| 490 | + 'single' => false, | |
| 491 | + 'sanitize_callback' => function ( $value ) { | |
| 492 | + return esc_sql( $value ); | |
| 493 | + }, | |
| 247 | 494 | ) |
| 248 | 495 | ); |
| 249 | 496 | |
| 250 | - \register_meta( | |
| 251 | - 'user', | |
| 252 | - $blog_prefix . 'activitypub_header_image', | |
| 497 | + \register_post_meta( | |
| 498 | + Followers::POST_TYPE, | |
| 499 | + 'activitypub_actor_json', | |
| 253 | 500 | array( |
| 254 | - 'type' => 'integer', | |
| 255 | - 'description' => 'The attachment ID for the user header image.', | |
| 501 | + 'type' => 'string', | |
| 256 | 502 | 'single' => true, |
| 257 | - 'default' => 0, | |
| 258 | - 'sanitize_callback' => 'absint', | |
| 503 | + 'sanitize_callback' => function ( $value ) { | |
| 504 | + return sanitize_text_field( $value ); | |
| 505 | + }, | |
| 259 | 506 | ) |
| 260 | 507 | ); |
| 261 | 508 | |
| 262 | - \register_meta( | |
| 263 | - 'user', | |
| 264 | - $blog_prefix . 'activitypub_mailer_new_dm', | |
| 265 | - array( | |
| 266 | - 'type' => 'integer', | |
| 267 | - 'description' => 'Send a notification when someone sends this user a direct message.', | |
| 268 | - 'single' => true, | |
| 269 | - 'sanitize_callback' => 'absint', | |
| 270 | - ) | |
| 509 | + // Both User and Blog Extra Fields types have the same args. | |
| 510 | + $args = array( | |
| 511 | + 'labels' => array( | |
| 512 | + 'name' => _x( 'Extra fields', 'post_type plural name', 'activitypub' ), | |
| 513 | + 'singular_name' => _x( 'Extra field', 'post_type single name', 'activitypub' ), | |
| 514 | + 'add_new' => __( 'Add new', 'activitypub' ), | |
| 515 | + 'add_new_item' => __( 'Add new extra field', 'activitypub' ), | |
| 516 | + 'new_item' => __( 'New extra field', 'activitypub' ), | |
| 517 | + 'edit_item' => __( 'Edit extra field', 'activitypub' ), | |
| 518 | + 'view_item' => __( 'View extra field', 'activitypub' ), | |
| 519 | + 'all_items' => __( 'All extra fields', 'activitypub' ), | |
| 520 | + ), | |
| 521 | + 'public' => false, | |
| 522 | + 'hierarchical' => false, | |
| 523 | + 'query_var' => false, | |
| 524 | + 'has_archive' => false, | |
| 525 | + 'publicly_queryable' => false, | |
| 526 | + 'show_in_menu' => false, | |
| 527 | + 'delete_with_user' => true, | |
| 528 | + 'can_export' => true, | |
| 529 | + 'exclude_from_search' => true, | |
| 530 | + 'show_in_rest' => true, | |
| 531 | + 'map_meta_cap' => true, | |
| 532 | + 'show_ui' => true, | |
| 533 | + 'supports' => array( 'title', 'editor', 'page-attributes' ), | |
| 271 | 534 | ); |
| 272 | - \add_filter( 'get_user_option_activitypub_mailer_new_dm', array( self::class, 'user_options_default' ) ); | |
| 273 | 535 | |
| 274 | - \register_meta( | |
| 275 | - 'user', | |
| 276 | - $blog_prefix . 'activitypub_mailer_new_follower', | |
| 277 | - array( | |
| 278 | - 'type' => 'integer', | |
| 279 | - 'description' => 'Send a notification when someone starts to follow this user.', | |
| 280 | - 'single' => true, | |
| 281 | - 'sanitize_callback' => 'absint', | |
| 282 | - ) | |
| 283 | - ); | |
| 284 | - \add_filter( 'get_user_option_activitypub_mailer_new_follower', array( self::class, 'user_options_default' ) ); | |
| 536 | + \register_post_type( Extra_Fields::USER_POST_TYPE, $args ); | |
| 537 | + \register_post_type( Extra_Fields::BLOG_POST_TYPE, $args ); | |
| 285 | 538 | |
| 286 | - \register_meta( | |
| 287 | - 'user', | |
| 288 | - $blog_prefix . 'activitypub_mailer_new_mention', | |
| 289 | - array( | |
| 290 | - 'type' => 'integer', | |
| 291 | - 'description' => 'Send a notification when someone mentions this user.', | |
| 292 | - 'single' => true, | |
| 293 | - 'sanitize_callback' => 'absint', | |
| 294 | - ) | |
| 295 | - ); | |
| 296 | - \add_filter( 'get_user_option_activitypub_mailer_new_mention', array( self::class, 'user_options_default' ) ); | |
| 297 | - | |
| 298 | - \register_meta( | |
| 299 | - 'user', | |
| 300 | - $blog_prefix . 'activitypub_mailer_annual_report', | |
| 301 | - array( | |
| 302 | - 'type' => 'integer', | |
| 303 | - 'description' => 'Send the annual Fediverse Year in Review email.', | |
| 304 | - 'single' => true, | |
| 305 | - 'sanitize_callback' => 'absint', | |
| 306 | - ) | |
| 307 | - ); | |
| 308 | - \add_filter( 'get_user_option_activitypub_mailer_annual_report', array( self::class, 'user_options_default' ) ); | |
| 309 | - | |
| 310 | - \register_meta( | |
| 311 | - 'user', | |
| 312 | - $blog_prefix . 'activitypub_mailer_monthly_report', | |
| 313 | - array( | |
| 314 | - 'type' => 'integer', | |
| 315 | - 'description' => 'Send a monthly Fediverse stats report email.', | |
| 316 | - 'single' => true, | |
| 317 | - 'sanitize_callback' => 'absint', | |
| 318 | - ) | |
| 319 | - ); | |
| 320 | - | |
| 321 | - \register_meta( | |
| 322 | - 'user', | |
| 323 | - 'activitypub_show_welcome_tab', | |
| 324 | - array( | |
| 325 | - 'type' => 'integer', | |
| 326 | - 'description' => 'Whether to show the welcome tab.', | |
| 327 | - 'single' => true, | |
| 328 | - 'default' => 1, | |
| 329 | - 'sanitize_callback' => 'absint', | |
| 330 | - ) | |
| 331 | - ); | |
| 332 | - | |
| 333 | - \register_meta( | |
| 334 | - 'user', | |
| 335 | - 'activitypub_show_advanced_tab', | |
| 336 | - array( | |
| 337 | - 'type' => 'integer', | |
| 338 | - 'description' => 'Whether to show the advanced tab.', | |
| 339 | - 'single' => true, | |
| 340 | - 'default' => 0, | |
| 341 | - 'sanitize_callback' => 'absint', | |
| 342 | - ) | |
| 343 | - ); | |
| 344 | - | |
| 345 | - // Moderation user meta. | |
| 346 | - \register_meta( | |
| 347 | - 'user', | |
| 348 | - 'activitypub_blocked_actors', | |
| 349 | - array( | |
| 350 | - 'type' => 'array', | |
| 351 | - 'description' => 'User-specific blocked ActivityPub actors.', | |
| 352 | - 'single' => true, | |
| 353 | - 'default' => array(), | |
| 354 | - 'sanitize_callback' => array( Sanitize::class, 'identifier_list' ), | |
| 355 | - ) | |
| 356 | - ); | |
| 357 | - | |
| 358 | - \register_meta( | |
| 359 | - 'user', | |
| 360 | - 'activitypub_blocked_domains', | |
| 361 | - array( | |
| 362 | - 'type' => 'array', | |
| 363 | - 'description' => 'User-specific blocked ActivityPub domains.', | |
| 364 | - 'single' => true, | |
| 365 | - 'default' => array(), | |
| 366 | - 'sanitize_callback' => static function ( $value ) { | |
| 367 | - return \array_unique( \array_map( array( Sanitize::class, 'host_list' ), $value ) ); | |
| 368 | - }, | |
| 369 | - ) | |
| 370 | - ); | |
| 371 | - | |
| 372 | - \register_meta( | |
| 373 | - 'user', | |
| 374 | - 'activitypub_blocked_keywords', | |
| 375 | - array( | |
| 376 | - 'type' => 'array', | |
| 377 | - 'description' => 'User-specific blocked ActivityPub keywords.', | |
| 378 | - 'single' => true, | |
| 379 | - 'default' => array(), | |
| 380 | - 'sanitize_callback' => static function ( $value ) { | |
| 381 | - return \array_map( 'sanitize_text_field', $value ); | |
| 382 | - }, | |
| 383 | - ) | |
| 384 | - ); | |
| 539 | + \do_action( 'activitypub_after_register_post_type' ); | |
| 385 | 540 | } |
| 386 | 541 | |
| 387 | 542 | /** |
| 388 | - * Set default values for user options. | |
| 543 | + * Add the 'activitypub' capability to users who can publish posts. | |
| 389 | 544 | * |
| 390 | - * @param bool|string $value Option value. | |
| 391 | - * @return bool|string | |
| 545 | + * @param int $user_id User ID. | |
| 546 | + * | |
| 547 | + * @param array $userdata The raw array of data passed to wp_insert_user(). | |
| 392 | 548 | */ |
| 393 | - public static function user_options_default( $value ) { | |
| 394 | - if ( false === $value ) { | |
| 395 | - return '1'; | |
| 549 | + public static function user_register( $user_id ) { | |
| 550 | + if ( \user_can( $user_id, 'publish_posts' ) ) { | |
| 551 | + $user = \get_user_by( 'id', $user_id ); | |
| 552 | + $user->add_cap( 'activitypub' ); | |
| 396 | 553 | } |
| 397 | - | |
| 398 | - return $value; | |
| 399 | 554 | } |
| 400 | 555 | } |