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