PluginProbe
ActivityPub / 2.6.0
ActivityPub v2.6.0
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
← All changes | includes/class-activitypub.php +415 -253 8.2.1 → 2.6.0 View file →
@@ -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,88 +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 - 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 + }
92 192
93 - 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;
94 224 }
95 225
96 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 + /**
97 241 * Store permalink in meta, to send delete Activity.
98 242 *
99 243 * @param string $post_id The Post ID.
244 + *
245 + * @return void
100 246 */
101 247 public static function trash_post( $post_id ) {
102 248 \add_post_meta(
103 249 $post_id,
104 - '_activitypub_canonical_url',
250 + 'activitypub_canonical_url',
105 251 \get_permalink( $post_id ),
106 252 true
107 253 );
108 254 }
@@ -107,40 +253,98 @@
107 253 );
108 254 }
109 255
110 256 /**
111 - * Delete permalink from meta.
257 + * Delete permalink from meta
112 258 *
113 - * @param string $post_id The Post ID.
259 + * @param string $post_id The Post ID
260 + *
261 + * @return void
114 262 */
115 263 public static function untrash_post( $post_id ) {
116 - \delete_post_meta( $post_id, '_activitypub_canonical_url' );
264 + \delete_post_meta( $post_id, 'activitypub_canonical_url' );
117 265 }
118 266
119 267 /**
120 - * Flush rewrite rules.
268 + * Add rewrite rules
121 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 + */
122 310 public static function flush_rewrite_rules() {
123 - Router::add_rewrite_rules();
311 + self::add_rewrite_rules();
124 312 \flush_rewrite_rules();
125 313 }
126 314
127 315 /**
128 - * Add rewrite rules.
316 + * Theme compatibility stuff
129 317 *
130 - * @deprecated 7.5.0 Use {@see Router::add_rewrite_rules()}.
318 + * @return void
131 319 */
132 - public static function add_rewrite_rules() {
133 - _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' );
134 322
135 - Router::add_rewrite_rules();
136 - }
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 + }
137 333
138 - /**
139 - * Theme compatibility stuff.
140 - */
141 - public static function theme_compat() {
142 - // 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
143 347 if ( 'wordpress-post-format' === \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE ) ) {
144 348 if ( ! get_theme_support( 'post-formats' ) ) {
145 349 // Add support for the Aside, Gallery Post Formats...
146 350 add_theme_support(
@@ -157,243 +361,201 @@
157 361 }
158 362 }
159 363
160 364 /**
161 - * Add the 'activitypub' capability to users who can publish posts.
365 + * Display plugin upgrade notice to users
162 366 *
163 - * @param int $user_id User ID.
367 + * @param array $data The plugin data
368 + *
369 + * @return void
164 370 */
165 - public static function user_register( $user_id ) {
166 - if ( \user_can( $user_id, 'publish_posts' ) ) {
167 - $user = \get_user_by( 'id', $user_id );
168 - $user->add_cap( 'activitypub' );
371 + public static function plugin_update_message( $data ) {
372 + if ( ! isset( $data['upgrade_notice'] ) ) {
373 + return;
169 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 + );
170 388 }
171 389
172 390 /**
173 - * Register user meta.
391 + * Register the "Followers" Taxonomy
392 + *
393 + * @return void
174 394 */
175 - public static function register_user_meta() {
176 - $blog_prefix = $GLOBALS['wpdb']->get_blog_prefix();
177 -
178 - \register_meta(
179 - 'user',
180 - $blog_prefix . 'activitypub_also_known_as',
395 + private static function register_post_types() {
396 + \register_post_type(
397 + Followers::POST_TYPE,
181 398 array(
182 - 'type' => 'array',
183 - 'description' => 'An array of URLs that the user is known by.',
184 - 'single' => true,
185 - 'default' => array(),
186 - '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(),
187 410 )
188 411 );
189 412
190 - \register_meta(
191 - 'user',
192 - $blog_prefix . 'activitypub_hide_social_graph',
413 + \register_post_meta(
414 + Followers::POST_TYPE,
415 + 'activitypub_inbox',
193 416 array(
194 - 'type' => 'integer',
195 - 'description' => 'Hide Followers and Followings on Profile.',
196 - 'single' => true,
197 - 'default' => 0,
198 - 'sanitize_callback' => 'absint',
199 - 'show_in_rest' => true,
200 - )
201 - );
202 -
203 - \register_meta(
204 - 'user',
205 - $blog_prefix . 'activitypub_old_host_data',
206 - array(
207 - 'description' => 'Actor object for the user on the old host.',
208 - 'single' => true,
209 - )
210 - );
211 -
212 - \register_meta(
213 - 'user',
214 - $blog_prefix . 'activitypub_moved_to',
215 - array(
216 417 'type' => 'string',
217 - 'description' => 'The new URL of the user.',
218 418 'single' => true,
219 419 'sanitize_callback' => 'sanitize_url',
220 420 )
221 421 );
222 422
223 - \register_meta(
224 - 'user',
225 - $blog_prefix . 'activitypub_description',
423 + \register_post_meta(
424 + Followers::POST_TYPE,
425 + 'activitypub_errors',
226 426 array(
227 427 'type' => 'string',
228 - 'description' => 'The user description.',
229 - 'single' => true,
230 - 'default' => '',
231 - 'sanitize_callback' => static function ( $value ) {
232 - 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 );
233 435 },
234 436 )
235 437 );
236 438
237 - \register_meta(
238 - 'user',
239 - $blog_prefix . 'activitypub_icon',
439 + \register_post_meta(
440 + Followers::POST_TYPE,
441 + 'activitypub_user_id',
240 442 array(
241 - 'type' => 'integer',
242 - 'description' => 'The attachment ID for user profile image.',
243 - 'single' => true,
244 - 'default' => 0,
245 - 'sanitize_callback' => 'absint',
443 + 'type' => 'string',
444 + 'single' => false,
445 + 'sanitize_callback' => function ( $value ) {
446 + return esc_sql( $value );
447 + },
246 448 )
247 449 );
248 450
249 - \register_meta(
250 - 'user',
251 - $blog_prefix . 'activitypub_header_image',
451 + \register_post_meta(
452 + Followers::POST_TYPE,
453 + 'activitypub_actor_json',
252 454 array(
253 - 'type' => 'integer',
254 - 'description' => 'The attachment ID for the user header image.',
455 + 'type' => 'string',
255 456 'single' => true,
256 - 'default' => 0,
257 - 'sanitize_callback' => 'absint',
457 + 'sanitize_callback' => function ( $value ) {
458 + return sanitize_text_field( $value );
459 + },
258 460 )
259 461 );
260 462
261 - \register_meta(
262 - 'user',
263 - $blog_prefix . 'activitypub_mailer_new_dm',
463 + \register_post_type(
464 + 'ap_extrafield',
264 465 array(
265 - 'type' => 'integer',
266 - 'description' => 'Send a notification when someone sends this user a direct message.',
267 - 'single' => true,
268 - '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' ),
269 489 )
270 490 );
271 - \add_filter( 'get_user_option_activitypub_mailer_new_dm', array( self::class, 'user_options_default' ) );
272 491
273 - \register_meta(
274 - 'user',
275 - $blog_prefix . 'activitypub_mailer_new_follower',
276 - array(
277 - 'type' => 'integer',
278 - 'description' => 'Send a notification when someone starts to follow this user.',
279 - 'single' => true,
280 - 'sanitize_callback' => 'absint',
281 - )
282 - );
283 - \add_filter( 'get_user_option_activitypub_mailer_new_follower', array( self::class, 'user_options_default' ) );
492 + \do_action( 'activitypub_after_register_post_type' );
493 + }
284 494
285 - \register_meta(
286 - 'user',
287 - $blog_prefix . 'activitypub_mailer_new_mention',
288 - array(
289 - 'type' => 'integer',
290 - 'description' => 'Send a notification when someone mentions this user.',
291 - 'single' => true,
292 - 'sanitize_callback' => 'absint',
293 - )
294 - );
295 - \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 + }
296 508
297 - \register_meta(
298 - 'user',
299 - $blog_prefix . 'activitypub_mailer_annual_report',
300 - array(
301 - 'type' => 'integer',
302 - 'description' => 'Send the annual Fediverse Year in Review email.',
303 - 'single' => true,
304 - 'sanitize_callback' => 'absint',
305 - )
306 - );
307 - \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 + }
308 521
309 - \register_meta(
310 - 'user',
311 - $blog_prefix . 'activitypub_mailer_monthly_report',
312 - array(
313 - 'type' => 'integer',
314 - 'description' => 'Send a monthly Fediverse stats report email.',
315 - 'single' => true,
316 - 'sanitize_callback' => 'absint',
317 - )
318 - );
522 + $already_migrated = \get_user_meta( $user_id, 'activitypub_default_extra_fields', true );
319 523
320 - \register_meta(
321 - 'user',
322 - 'activitypub_show_welcome_tab',
323 - array(
324 - 'type' => 'integer',
325 - 'description' => 'Whether to show the welcome tab.',
326 - 'single' => true,
327 - 'default' => 1,
328 - 'sanitize_callback' => 'absint',
329 - )
330 - );
524 + if ( $already_migrated ) {
525 + return $extra_fields;
526 + }
331 527
332 - \register_meta(
333 - 'user',
334 - 'activitypub_show_advanced_tab',
335 - array(
336 - 'type' => 'integer',
337 - 'description' => 'Whether to show the advanced tab.',
338 - 'single' => true,
339 - 'default' => 0,
340 - 'sanitize_callback' => 'absint',
341 - )
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 ),
342 532 );
343 533
344 - // Moderation user meta.
345 - \register_meta(
346 - 'user',
347 - 'activitypub_blocked_actors',
348 - array(
349 - 'type' => 'array',
350 - 'description' => 'User-specific blocked ActivityPub actors.',
351 - 'single' => true,
352 - 'default' => array(),
353 - 'sanitize_callback' => array( Sanitize::class, 'identifier_list' ),
354 - )
355 - );
534 + foreach ( $defaults as $title => $url ) {
535 + if ( ! $url ) {
536 + continue;
537 + }
356 538
357 - \register_meta(
358 - 'user',
359 - 'activitypub_blocked_domains',
360 - array(
361 - 'type' => 'array',
362 - 'description' => 'User-specific blocked ActivityPub domains.',
363 - 'single' => true,
364 - 'default' => array(),
365 - 'sanitize_callback' => static function ( $value ) {
366 - return \array_unique( \array_map( array( Sanitize::class, 'host_list' ), $value ) );
367 - },
368 - )
369 - );
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 + );
370 552
371 - \register_meta(
372 - 'user',
373 - 'activitypub_blocked_keywords',
374 - array(
375 - 'type' => 'array',
376 - 'description' => 'User-specific blocked ActivityPub keywords.',
377 - 'single' => true,
378 - 'default' => array(),
379 - 'sanitize_callback' => static function ( $value ) {
380 - return \array_map( 'sanitize_text_field', $value );
381 - },
382 - )
383 - );
384 - }
553 + $extra_field_id = wp_insert_post( $extra_field );
554 + $extra_fields[] = get_post( $extra_field_id );
555 + }
385 556
386 - /**
387 - * Set default values for user options.
388 - *
389 - * @param bool|string $value Option value.
390 - * @return bool|string
391 - */
392 - public static function user_options_default( $value ) {
393 - if ( false === $value ) {
394 - return '1';
395 - }
557 + \update_user_meta( $user_id, 'activitypub_default_extra_fields', true );
396 558
397 - return $value;
559 + return $extra_fields;
398 560 }
399 561 }