PluginProbe
ActivityPub / 5.3.1
ActivityPub v5.3.1
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 +548 -205 8.2.15.3.1 View file →
@@ -6,12 +6,13 @@
6 6 */
7 7
8 8 namespace Activitypub;
9 9
10 +use Exception;
11 +use Activitypub\Transformer\Factory;
12 +use Activitypub\Collection\Outbox;
10 13 use Activitypub\Collection\Followers;
11 -use Activitypub\Collection\Following;
12 -use Activitypub\Collection\Remote_Posts;
13 -use Activitypub\OAuth\Client;
14 +use Activitypub\Collection\Extra_Fields;
14 15
15 16 /**
16 17 * ActivityPub Class.
17 18 *
@@ -21,62 +22,64 @@
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_activitypub_template' ), 99 );
27 + \add_action( 'template_redirect', array( self::class, 'template_redirect' ) );
28 + \add_filter( 'redirect_canonical', array( self::class, 'redirect_canonical' ), 10, 2 );
29 + \add_filter( 'query_vars', array( self::class, 'add_query_vars' ) );
30 + \add_filter( 'pre_get_avatar_data', array( self::class, 'pre_get_avatar_data' ), 11, 2 );
27 31
32 + // Add support for ActivityPub to custom post types.
33 + $post_types = \get_option( 'activitypub_support_post_types', array( 'post' ) );
34 +
35 + foreach ( $post_types as $post_type ) {
36 + \add_post_type_support( $post_type, 'activitypub' );
37 + }
38 +
28 39 \add_action( 'wp_trash_post', array( self::class, 'trash_post' ), 1 );
29 40 \add_action( 'untrash_post', array( self::class, 'untrash_post' ), 1 );
30 41
42 + \add_action( 'init', array( self::class, 'add_rewrite_rules' ), 11 );
43 + \add_action( 'init', array( self::class, 'theme_compat' ), 11 );
44 +
31 45 \add_action( 'user_register', array( self::class, 'user_register' ) );
32 46
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 );
47 + \add_action( 'in_plugin_update_message-' . ACTIVITYPUB_PLUGIN_BASENAME, array( self::class, 'plugin_update_message' ) );
48 +
49 + if ( site_supports_blocks() ) {
50 + \add_action( 'tool_box', array( self::class, 'tool_box' ) );
51 + }
52 +
53 + \add_filter( 'activitypub_get_actor_extra_fields', array( Extra_Fields::class, 'default_actor_extra_fields' ), 10, 2 );
54 +
55 + \add_action( 'updated_postmeta', array( self::class, 'updated_postmeta' ), 10, 4 );
56 + \add_action( 'added_post_meta', array( self::class, 'updated_postmeta' ), 10, 4 );
57 +
58 + // Register several post_types.
59 + self::register_post_types();
35 60 }
36 61
37 62 /**
38 63 * Activation Hook.
39 - *
40 - * @param bool $network_wide Whether to activate the plugin for all sites in the network or just the current site.
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();
45 68
46 - \add_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ), 5, 3 );
69 + \add_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ), 10, 3 );
47 70 Migration::update_comment_counts();
48 -
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();
55 - }
56 - }
57 71 }
58 72
59 73 /**
60 74 * Deactivation Hook.
61 - *
62 - * @param bool $network_wide Whether to deactivate the plugin for all sites in the network or just the current site.
63 75 */
64 - public static function deactivate( $network_wide ) {
76 + public static function deactivate() {
65 77 self::flush_rewrite_rules();
66 78 Scheduler::deregister_schedules();
67 79
68 - \remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ), 5 );
80 + \remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ) );
69 81 Migration::update_comment_counts( 2000 );
70 -
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 - }
78 - }
79 82 }
80 83
81 84 /**
82 85 * Uninstall Hook.
@@ -83,18 +86,283 @@
83 86 */
84 87 public static function uninstall() {
85 88 Scheduler::deregister_schedules();
86 89
87 - \remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ), 5 );
90 + \remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ) );
88 91 Migration::update_comment_counts( 2000 );
89 92
90 - Remote_Posts::delete_all();
91 - Client::delete_all();
93 + delete_option( 'activitypub_actor_mode' );
94 + delete_option( 'activitypub_attribution_domains' );
95 + delete_option( 'activitypub_authorized_fetch' );
96 + delete_option( 'activitypub_application_user_private_key' );
97 + delete_option( 'activitypub_application_user_public_key' );
98 + delete_option( 'activitypub_blog_user_private_key' );
99 + delete_option( 'activitypub_blog_user_public_key' );
100 + delete_option( 'activitypub_blog_description' );
101 + delete_option( 'activitypub_blog_identifier' );
102 + delete_option( 'activitypub_custom_post_content' );
103 + delete_option( 'activitypub_db_version' );
104 + delete_option( 'activitypub_default_extra_fields' );
105 + delete_option( 'activitypub_enable_blog_user' );
106 + delete_option( 'activitypub_enable_users' );
107 + delete_option( 'activitypub_header_image' );
108 + delete_option( 'activitypub_last_post_with_permalink_as_id' );
109 + delete_option( 'activitypub_mailer_new_follower' );
110 + delete_option( 'activitypub_mailer_new_dm' );
111 + delete_option( 'activitypub_max_image_attachments' );
112 + delete_option( 'activitypub_migration_lock' );
113 + delete_option( 'activitypub_object_type' );
114 + delete_option( 'activitypub_outbox_purge_days' );
115 + delete_option( 'activitypub_support_post_types' );
116 + delete_option( 'activitypub_use_hashtags' );
117 + delete_option( 'activitypub_use_opengraph' );
118 + delete_option( 'activitypub_use_permalink_as_id_for_blog' );
119 + }
92 120
93 - Options::delete();
121 + /**
122 + * Return a AS2 JSON version of an author, post or page.
123 + *
124 + * @param string $template The path to the template object.
125 + *
126 + * @return string The new path to the JSON template.
127 + */
128 + public static function render_activitypub_template( $template ) {
129 + if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
130 + return $template;
131 + }
132 +
133 + self::add_headers();
134 +
135 + if ( ! is_activitypub_request() ) {
136 + return $template;
137 + }
138 +
139 + $activitypub_template = false;
140 + $activitypub_object = Query::get_instance()->get_activitypub_object();
141 +
142 + if ( $activitypub_object ) {
143 + if ( \get_query_var( 'preview' ) ) {
144 + \define( 'ACTIVITYPUB_PREVIEW', true );
145 +
146 + /**
147 + * Filter the template used for the ActivityPub preview.
148 + *
149 + * @param string $activitypub_template Absolute path to the template file.
150 + */
151 + $activitypub_template = apply_filters( 'activitypub_preview_template', ACTIVITYPUB_PLUGIN_DIR . '/templates/post-preview.php' );
152 + } else {
153 + $activitypub_template = ACTIVITYPUB_PLUGIN_DIR . 'templates/activitypub-json.php';
154 + }
155 + }
156 +
157 + /*
158 + * Check if the request is authorized.
159 + *
160 + * @see https://www.w3.org/wiki/SocialCG/ActivityPub/Primer/Authentication_Authorization#Authorized_fetch
161 + * @see https://swicg.github.io/activitypub-http-signature/#authorized-fetch
162 + */
163 + if ( $activitypub_template && use_authorized_fetch() ) {
164 + $verification = Signature::verify_http_signature( $_SERVER );
165 + if ( \is_wp_error( $verification ) ) {
166 + header( 'HTTP/1.1 401 Unauthorized' );
167 +
168 + // Fallback as template_loader can't return http headers.
169 + return $template;
170 + }
171 + }
172 +
173 + if ( $activitypub_template ) {
174 + \set_query_var( 'is_404', false );
175 +
176 + // Check if header already sent.
177 + if ( ! \headers_sent() ) {
178 + // Send 200 status header.
179 + \status_header( 200 );
180 + }
181 +
182 + return $activitypub_template;
183 + }
184 +
185 + return $template;
94 186 }
95 187
96 188 /**
189 + * Add the 'self' link to the header.
190 + */
191 + public static function add_headers() {
192 + $id = Query::get_instance()->get_activitypub_object_id();
193 +
194 + if ( ! $id ) {
195 + return;
196 + }
197 +
198 + if ( ! headers_sent() ) {
199 + \header( 'Link: <' . esc_url( $id ) . '>; title="ActivityPub (JSON)"; rel="alternate"; type="application/activity+json"', false );
200 +
201 + if ( ACTIVITYPUB_SEND_VARY_HEADER ) {
202 + // Send Vary header for Accept header.
203 + \header( 'Vary: Accept', false );
204 + }
205 + }
206 +
207 + add_action(
208 + 'wp_head',
209 + function () use ( $id ) {
210 + echo PHP_EOL . '<link rel="alternate" title="ActivityPub (JSON)" type="application/activity+json" href="' . esc_url( $id ) . '" />' . PHP_EOL;
211 + }
212 + );
213 + }
214 +
215 + /**
216 + * Add support for `p` and `author` query vars.
217 + *
218 + * @param string $redirect_url The URL to redirect to.
219 + * @param string $requested_url The requested URL.
220 + *
221 + * @return string $redirect_url
222 + */
223 + public static function redirect_canonical( $redirect_url, $requested_url ) {
224 + if ( ! is_activitypub_request() ) {
225 + return $redirect_url;
226 + }
227 +
228 + $query = \wp_parse_url( $requested_url, PHP_URL_QUERY );
229 +
230 + if ( ! $query ) {
231 + return $redirect_url;
232 + }
233 +
234 + $query_params = \wp_parse_args( $query );
235 + unset( $query_params['activitypub'] );
236 +
237 + if ( 1 !== count( $query_params ) ) {
238 + return $redirect_url;
239 + }
240 +
241 + if ( isset( $query_params['p'] ) ) {
242 + return null;
243 + }
244 +
245 + if ( isset( $query_params['author'] ) ) {
246 + return null;
247 + }
248 +
249 + return $requested_url;
250 + }
251 +
252 + /**
253 + * Custom redirects for ActivityPub requests.
254 + *
255 + * @return void
256 + */
257 + public static function template_redirect() {
258 + $comment_id = get_query_var( 'c', null );
259 +
260 + // Check if it seems to be a comment.
261 + if ( ! $comment_id ) {
262 + return;
263 + }
264 +
265 + $comment = get_comment( $comment_id );
266 +
267 + // Load a 404 page if `c` is set but not valid.
268 + if ( ! $comment ) {
269 + global $wp_query;
270 + $wp_query->set_404();
271 + return;
272 + }
273 +
274 + // Stop if it's not an ActivityPub comment.
275 + if ( is_activitypub_request() && ! is_local_comment( $comment ) ) {
276 + return;
277 + }
278 +
279 + wp_safe_redirect( get_comment_link( $comment ) );
280 + exit;
281 + }
282 +
283 + /**
284 + * Add the 'activitypub' query variable so WordPress won't mangle it.
285 + *
286 + * @param array $vars The query variables.
287 + *
288 + * @return array The query variables.
289 + */
290 + public static function add_query_vars( $vars ) {
291 + $vars[] = 'activitypub';
292 + $vars[] = 'preview';
293 + $vars[] = 'author';
294 + $vars[] = 'c';
295 + $vars[] = 'p';
296 +
297 + return $vars;
298 + }
299 +
300 + /**
301 + * Replaces the default avatar.
302 + *
303 + * @param array $args Arguments passed to get_avatar_data(), after processing.
304 + * @param int|string|object $id_or_email A user ID, email address, or comment object.
305 + *
306 + * @return array $args
307 + */
308 + public static function pre_get_avatar_data( $args, $id_or_email ) {
309 + if (
310 + ! $id_or_email instanceof \WP_Comment ||
311 + ! isset( $id_or_email->comment_type ) ||
312 + $id_or_email->user_id
313 + ) {
314 + return $args;
315 + }
316 +
317 + $allowed_comment_types = \apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
318 + if (
319 + ! empty( $id_or_email->comment_type ) &&
320 + ! \in_array(
321 + $id_or_email->comment_type,
322 + (array) $allowed_comment_types,
323 + true
324 + )
325 + ) {
326 + $args['url'] = false;
327 + /** This filter is documented in wp-includes/link-template.php */
328 + return \apply_filters( 'get_avatar_data', $args, $id_or_email );
329 + }
330 +
331 + // Check if comment has an avatar.
332 + $avatar = self::get_avatar_url( $id_or_email->comment_ID );
333 +
334 + if ( $avatar ) {
335 + if ( empty( $args['class'] ) ) {
336 + $args['class'] = array();
337 + } elseif ( \is_string( $args['class'] ) ) {
338 + $args['class'] = \explode( ' ', $args['class'] );
339 + }
340 +
341 + $args['url'] = $avatar;
342 + $args['class'][] = 'avatar-activitypub';
343 + $args['class'][] = 'u-photo';
344 + $args['class'] = \array_unique( $args['class'] );
345 + }
346 +
347 + return $args;
348 + }
349 +
350 + /**
351 + * Function to retrieve Avatar URL if stored in meta.
352 + *
353 + * @param int|\WP_Comment $comment The comment ID or object.
354 + *
355 + * @return string The Avatar URL.
356 + */
357 + public static function get_avatar_url( $comment ) {
358 + if ( \is_numeric( $comment ) ) {
359 + $comment = \get_comment( $comment );
360 + }
361 + return \get_comment_meta( $comment->comment_ID, 'avatar_url', true );
362 + }
363 +
364 + /**
97 365 * Store permalink in meta, to send delete Activity.
98 366 *
99 367 * @param string $post_id The Post ID.
100 368 */
@@ -116,24 +384,59 @@
116 384 \delete_post_meta( $post_id, '_activitypub_canonical_url' );
117 385 }
118 386
119 387 /**
388 + * Add rewrite rules.
389 + */
390 + public static function add_rewrite_rules() {
391 + /*
392 + * If another system needs to take precedence over the ActivityPub rewrite rules,
393 + * they can define their own and will manually call the appropriate functions as required.
394 + */
395 + if ( ACTIVITYPUB_DISABLE_REWRITES ) {
396 + return;
397 + }
398 +
399 + if ( ! \class_exists( 'Webfinger' ) ) {
400 + \add_rewrite_rule(
401 + '^.well-known/webfinger',
402 + 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/webfinger',
403 + 'top'
404 + );
405 + }
406 +
407 + if ( ! \class_exists( 'Nodeinfo_Endpoint' ) && true === (bool) \get_option( 'blog_public', 1 ) ) {
408 + \add_rewrite_rule(
409 + '^.well-known/nodeinfo',
410 + 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo',
411 + 'top'
412 + );
413 + }
414 +
415 + \add_rewrite_rule(
416 + '^@([\w\-\.]+)$',
417 + 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/actors/$matches[1]',
418 + 'top'
419 + );
420 +
421 + \add_rewrite_endpoint( 'activitypub', EP_AUTHORS | EP_PERMALINK | EP_PAGES );
422 + }
423 +
424 + /**
120 425 * Flush rewrite rules.
121 426 */
122 427 public static function flush_rewrite_rules() {
123 - Router::add_rewrite_rules();
428 + self::add_rewrite_rules();
124 429 \flush_rewrite_rules();
125 430 }
126 431
127 432 /**
128 - * Add rewrite rules.
129 - *
130 - * @deprecated 7.5.0 Use {@see Router::add_rewrite_rules()}.
433 + * Adds metabox on wp-admin/tools.php.
131 434 */
132 - public static function add_rewrite_rules() {
133 - _deprecated_function( __FUNCTION__, '7.5.0', '\Activitypub\Router::add_rewrite_rules()' );
134 -
135 - Router::add_rewrite_rules();
435 + public static function tool_box() {
436 + if ( \current_user_can( 'edit_posts' ) ) {
437 + \load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/toolbox.php' );
438 + }
136 439 }
137 440
138 441 /**
139 442 * Theme compatibility stuff.
@@ -157,243 +460,283 @@
157 460 }
158 461 }
159 462
160 463 /**
161 - * Add the 'activitypub' capability to users who can publish posts.
464 + * Display plugin upgrade notice to users.
162 465 *
163 - * @param int $user_id User ID.
466 + * @param array $data The plugin data.
164 467 */
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' );
468 + public static function plugin_update_message( $data ) {
469 + if ( ! isset( $data['upgrade_notice'] ) ) {
470 + return;
169 471 }
472 +
473 + printf(
474 + '<div class="update-message">%s</div>',
475 + wp_kses(
476 + wpautop( $data['upgrade_notice '] ),
477 + array(
478 + 'p' => array(),
479 + 'a' => array( 'href', 'title' ),
480 + 'strong' => array(),
481 + 'em' => array(),
482 + )
483 + )
484 + );
170 485 }
171 486
172 487 /**
173 - * Register user meta.
488 + * Register Custom Post Types.
174 489 */
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',
490 + private static function register_post_types() {
491 + \register_post_type(
492 + Followers::POST_TYPE,
181 493 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' ),
494 + 'labels' => array(
495 + 'name' => _x( 'Followers', 'post_type plural name', 'activitypub' ),
496 + 'singular_name' => _x( 'Follower', 'post_type single name', 'activitypub' ),
497 + ),
498 + 'public' => false,
499 + 'hierarchical' => false,
500 + 'rewrite' => false,
501 + 'query_var' => false,
502 + 'delete_with_user' => false,
503 + 'can_export' => true,
504 + 'supports' => array(),
187 505 )
188 506 );
189 507
190 - \register_meta(
191 - 'user',
192 - $blog_prefix . 'activitypub_hide_social_graph',
508 + \register_post_meta(
509 + Followers::POST_TYPE,
510 + '_activitypub_inbox',
193 511 array(
194 - 'type' => 'integer',
195 - 'description' => 'Hide Followers and Followings on Profile.',
512 + 'type' => 'string',
196 513 'single' => true,
197 - 'default' => 0,
198 - 'sanitize_callback' => 'absint',
199 - 'show_in_rest' => true,
514 + 'sanitize_callback' => 'sanitize_url',
200 515 )
201 516 );
202 517
203 - \register_meta(
204 - 'user',
205 - $blog_prefix . 'activitypub_old_host_data',
518 + \register_post_meta(
519 + Followers::POST_TYPE,
520 + '_activitypub_errors',
206 521 array(
207 - 'description' => 'Actor object for the user on the old host.',
208 - 'single' => true,
522 + 'type' => 'string',
523 + 'single' => false,
524 + 'sanitize_callback' => function ( $value ) {
525 + if ( ! is_string( $value ) ) {
526 + throw new Exception( 'Error message is no valid string' );
527 + }
528 +
529 + return esc_sql( $value );
530 + },
209 531 )
210 532 );
211 533
212 - \register_meta(
213 - 'user',
214 - $blog_prefix . 'activitypub_moved_to',
534 + \register_post_meta(
535 + Followers::POST_TYPE,
536 + '_activitypub_user_id',
215 537 array(
216 538 'type' => 'string',
217 - 'description' => 'The new URL of the user.',
218 - 'single' => true,
219 - 'sanitize_callback' => 'sanitize_url',
539 + 'single' => false,
540 + 'sanitize_callback' => function ( $value ) {
541 + return esc_sql( $value );
542 + },
220 543 )
221 544 );
222 545
223 - \register_meta(
224 - 'user',
225 - $blog_prefix . 'activitypub_description',
546 + \register_post_meta(
547 + Followers::POST_TYPE,
548 + '_activitypub_actor_json',
226 549 array(
227 550 'type' => 'string',
228 - 'description' => 'The user description.',
229 551 'single' => true,
230 - 'default' => '',
231 - 'sanitize_callback' => static function ( $value ) {
232 - return wp_kses( $value, 'user_description' );
552 + 'sanitize_callback' => function ( $value ) {
553 + return sanitize_text_field( $value );
233 554 },
234 555 )
235 556 );
236 557
237 - \register_meta(
238 - 'user',
239 - $blog_prefix . 'activitypub_icon',
558 + // Register Outbox Post-Type.
559 + register_post_type(
560 + Outbox::POST_TYPE,
240 561 array(
241 - 'type' => 'integer',
242 - 'description' => 'The attachment ID for user profile image.',
243 - 'single' => true,
244 - 'default' => 0,
245 - 'sanitize_callback' => 'absint',
562 + 'labels' => array(
563 + 'name' => _x( 'Outbox', 'post_type plural name', 'activitypub' ),
564 + 'singular_name' => _x( 'Outbox Item', 'post_type single name', 'activitypub' ),
565 + ),
566 + 'capabilities' => array(
567 + 'create_posts' => false,
568 + ),
569 + 'map_meta_cap' => true,
570 + 'public' => false,
571 + 'show_in_rest' => true,
572 + 'rewrite' => false,
573 + 'query_var' => false,
574 + 'supports' => array( 'title', 'editor', 'author', 'custom-fields' ),
575 + 'delete_with_user' => true,
576 + 'can_export' => true,
577 + 'exclude_from_search' => true,
246 578 )
247 579 );
248 580
249 - \register_meta(
250 - 'user',
251 - $blog_prefix . 'activitypub_header_image',
581 + /**
582 + * Register Activity Type meta for Outbox items.
583 + *
584 + * @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
585 + */
586 + \register_post_meta(
587 + Outbox::POST_TYPE,
588 + '_activitypub_activity_type',
252 589 array(
253 - 'type' => 'integer',
254 - 'description' => 'The attachment ID for the user header image.',
590 + 'type' => 'string',
591 + 'description' => 'The type of the activity',
255 592 'single' => true,
256 - 'default' => 0,
257 - 'sanitize_callback' => 'absint',
258 - )
259 - );
593 + 'show_in_rest' => true,
594 + 'sanitize_callback' => function ( $value ) {
595 + $value = ucfirst( strtolower( $value ) );
596 + $schema = array(
597 + 'type' => 'string',
598 + 'enum' => array( 'Accept', 'Add', 'Announce', 'Arrive', 'Block', 'Create', 'Delete', 'Dislike', 'Flag', 'Follow', 'Ignore', 'Invite', 'Join', 'Leave', 'Like', 'Listen', 'Move', 'Offer', 'Question', 'Reject', 'Read', 'Remove', 'TentativeReject', 'TentativeAccept', 'Travel', 'Undo', 'Update', 'View' ),
599 + 'default' => 'Announce',
600 + );
260 601
261 - \register_meta(
262 - 'user',
263 - $blog_prefix . 'activitypub_mailer_new_dm',
264 - array(
265 - 'type' => 'integer',
266 - 'description' => 'Send a notification when someone sends this user a direct message.',
267 - 'single' => true,
268 - 'sanitize_callback' => 'absint',
602 + if ( is_wp_error( rest_validate_enum( $value, $schema, '' ) ) ) {
603 + return $schema['default'];
604 + }
605 +
606 + return $value;
607 + },
269 608 )
270 609 );
271 - \add_filter( 'get_user_option_activitypub_mailer_new_dm', array( self::class, 'user_options_default' ) );
272 610
273 - \register_meta(
274 - 'user',
275 - $blog_prefix . 'activitypub_mailer_new_follower',
611 + \register_post_meta(
612 + Outbox::POST_TYPE,
613 + '_activitypub_activity_actor',
276 614 array(
277 - 'type' => 'integer',
278 - 'description' => 'Send a notification when someone starts to follow this user.',
615 + 'type' => 'string',
279 616 'single' => true,
280 - 'sanitize_callback' => 'absint',
281 - )
282 - );
283 - \add_filter( 'get_user_option_activitypub_mailer_new_follower', array( self::class, 'user_options_default' ) );
617 + 'show_in_rest' => true,
618 + 'sanitize_callback' => function ( $value ) {
619 + $schema = array(
620 + 'type' => 'string',
621 + 'enum' => array( 'application', 'blog', 'user' ),
622 + 'default' => 'user',
623 + );
284 624
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' ) );
625 + if ( is_wp_error( rest_validate_enum( $value, $schema, '' ) ) ) {
626 + return $schema['default'];
627 + }
296 628
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',
629 + return $value;
630 + },
305 631 )
306 632 );
307 - \add_filter( 'get_user_option_activitypub_mailer_annual_report', array( self::class, 'user_options_default' ) );
308 633
309 - \register_meta(
310 - 'user',
311 - $blog_prefix . 'activitypub_mailer_monthly_report',
634 + \register_post_meta(
635 + Outbox::POST_TYPE,
636 + '_activitypub_outbox_offset',
312 637 array(
313 638 'type' => 'integer',
314 - 'description' => 'Send a monthly Fediverse stats report email.',
315 639 'single' => true,
640 + 'description' => 'Keeps track of the followers offset when processing outbox items.',
316 641 'sanitize_callback' => 'absint',
642 + 'default' => 0,
317 643 )
318 644 );
319 645
320 - \register_meta(
321 - 'user',
322 - 'activitypub_show_welcome_tab',
646 + \register_post_meta(
647 + Outbox::POST_TYPE,
648 + '_activitypub_object_id',
323 649 array(
324 - 'type' => 'integer',
325 - 'description' => 'Whether to show the welcome tab.',
650 + 'type' => 'string',
326 651 'single' => true,
327 - 'default' => 1,
328 - 'sanitize_callback' => 'absint',
652 + 'description' => 'The ID (ActivityPub URI) of the object that the outbox item is about.',
653 + 'sanitize_callback' => 'sanitize_url',
329 654 )
330 655 );
331 656
332 - \register_meta(
333 - 'user',
334 - 'activitypub_show_advanced_tab',
657 + \register_post_meta(
658 + Outbox::POST_TYPE,
659 + 'activitypub_content_visibility',
335 660 array(
336 - 'type' => 'integer',
337 - 'description' => 'Whether to show the advanced tab.',
661 + 'type' => 'string',
338 662 'single' => true,
339 - 'default' => 0,
340 - 'sanitize_callback' => 'absint',
341 - )
342 - );
663 + 'show_in_rest' => true,
664 + 'sanitize_callback' => function ( $value ) {
665 + $schema = array(
666 + 'type' => 'string',
667 + 'enum' => array( ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC, ACTIVITYPUB_CONTENT_VISIBILITY_QUIET_PUBLIC, ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE, ACTIVITYPUB_CONTENT_VISIBILITY_LOCAL ),
668 + 'default' => ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC,
669 + );
343 670
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 - );
671 + if ( is_wp_error( rest_validate_enum( $value, $schema, '' ) ) ) {
672 + return $schema['default'];
673 + }
356 674
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 ) );
675 + return $value;
367 676 },
368 677 )
369 678 );
370 679
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 - )
680 + // Both User and Blog Extra Fields types have the same args.
681 + $args = array(
682 + 'labels' => array(
683 + 'name' => _x( 'Extra fields', 'post_type plural name', 'activitypub' ),
684 + 'singular_name' => _x( 'Extra field', 'post_type single name', 'activitypub' ),
685 + 'add_new' => __( 'Add new', 'activitypub' ),
686 + 'add_new_item' => __( 'Add new extra field', 'activitypub' ),
687 + 'new_item' => __( 'New extra field', 'activitypub' ),
688 + 'edit_item' => __( 'Edit extra field', 'activitypub' ),
689 + 'view_item' => __( 'View extra field', 'activitypub' ),
690 + 'all_items' => __( 'All extra fields', 'activitypub' ),
691 + ),
692 + 'public' => false,
693 + 'hierarchical' => false,
694 + 'query_var' => false,
695 + 'has_archive' => false,
696 + 'publicly_queryable' => false,
697 + 'show_in_menu' => false,
698 + 'delete_with_user' => true,
699 + 'can_export' => true,
700 + 'exclude_from_search' => true,
701 + 'show_in_rest' => true,
702 + 'map_meta_cap' => true,
703 + 'show_ui' => true,
704 + 'supports' => array( 'title', 'editor', 'page-attributes' ),
383 705 );
706 +
707 + \register_post_type( Extra_Fields::USER_POST_TYPE, $args );
708 + \register_post_type( Extra_Fields::BLOG_POST_TYPE, $args );
709 +
710 + /**
711 + * Fires after ActivityPub custom post types have been registered.
712 + */
713 + \do_action( 'activitypub_after_register_post_type' );
384 714 }
385 715
386 716 /**
387 - * Set default values for user options.
717 + * Add the 'activitypub' capability to users who can publish posts.
388 718 *
389 - * @param bool|string $value Option value.
390 - * @return bool|string
719 + * @param int $user_id User ID.
391 720 */
392 - public static function user_options_default( $value ) {
393 - if ( false === $value ) {
394 - return '1';
721 + public static function user_register( $user_id ) {
722 + if ( \user_can( $user_id, 'publish_posts' ) ) {
723 + $user = \get_user_by( 'id', $user_id );
724 + $user->add_cap( 'activitypub' );
395 725 }
726 + }
396 727
397 - return $value;
728 + /**
729 + * Delete `activitypub_content_visibility` when updated to an empty value.
730 + *
731 + * @param int $meta_id ID of updated metadata entry.
732 + * @param int $object_id Post ID.
733 + * @param string $meta_key Metadata key.
734 + * @param mixed $meta_value Metadata value. This will be a PHP-serialized string representation of the value
735 + * if the value is an array, an object, or itself a PHP-serialized string.
736 + */
737 + public static function updated_postmeta( $meta_id, $object_id, $meta_key, $meta_value ) {
738 + if ( 'activitypub_content_visibility' === $meta_key && empty( $meta_value ) ) {
739 + \delete_post_meta( $object_id, 'activitypub_content_visibility' );
740 + }
398 741 }
399 742 }