PluginProbe
ActivityPub / 5.7.0
ActivityPub v5.7.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 +652 -136 1.2.05.7.0 View file →
@@ -1,12 +1,21 @@
1 1 <?php
2 +/**
3 + * ActivityPub Class.
4 + *
5 + * @package Activitypub
6 + */
7 +
2 8 namespace Activitypub;
3 9
4 -use Activitypub\Signature;
5 -use Activitypub\Collection\Users;
10 +use Exception;
11 +use Activitypub\Collection\Actors;
12 +use Activitypub\Collection\Outbox;
13 +use Activitypub\Collection\Followers;
14 +use Activitypub\Collection\Extra_Fields;
6 15
7 16 /**
8 - * ActivityPub Class
17 + * ActivityPub Class.
9 18 *
10 19 * @author Matthias Pfefferle
11 20 */
12 21 class Activitypub {
@@ -13,15 +22,17 @@
13 22 /**
14 23 * Initialize the class, registering WordPress hooks.
15 24 */
16 25 public static function init() {
17 - \add_filter( 'template_include', array( self::class, 'render_json_template' ), 99 );
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( 'redirect_canonical', array( self::class, 'no_trailing_redirect' ), 10, 2 );
18 30 \add_filter( 'query_vars', array( self::class, 'add_query_vars' ) );
19 31 \add_filter( 'pre_get_avatar_data', array( self::class, 'pre_get_avatar_data' ), 11, 2 );
20 - \add_filter( 'get_comment_link', array( self::class, 'remote_comment_link' ), 11, 3 );
21 32
22 - // Add support for ActivityPub to custom post types
23 - $post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ) ? \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ) : array();
33 + // Add support for ActivityPub to custom post types.
34 + $post_types = \get_option( 'activitypub_support_post_types', array( 'post' ) );
24 35
25 36 foreach ( $post_types as $post_type ) {
26 37 \add_post_type_support( $post_type, 'activitypub' );
27 38 }
@@ -29,43 +40,89 @@
29 40 \add_action( 'wp_trash_post', array( self::class, 'trash_post' ), 1 );
30 41 \add_action( 'untrash_post', array( self::class, 'untrash_post' ), 1 );
31 42
32 43 \add_action( 'init', array( self::class, 'add_rewrite_rules' ), 11 );
44 + \add_action( 'init', array( self::class, 'theme_compat' ), 11 );
33 45
34 - \add_action( 'after_setup_theme', array( self::class, 'theme_compat' ), 99 );
46 + \add_action( 'user_register', array( self::class, 'user_register' ) );
35 47
36 - \add_action( 'in_plugin_update_message-' . ACTIVITYPUB_PLUGIN_BASENAME, array( self::class, 'plugin_update_message' ) );
48 + \add_filter( 'activitypub_get_actor_extra_fields', array( Extra_Fields::class, 'default_actor_extra_fields' ), 10, 2 );
49 +
50 + \add_action( 'updated_postmeta', array( self::class, 'updated_postmeta' ), 10, 4 );
51 + \add_action( 'added_post_meta', array( self::class, 'updated_postmeta' ), 10, 4 );
52 +
53 + \add_action( 'init', array( self::class, 'register_user_meta' ), 11 );
54 +
55 + // Register several post_types.
56 + self::register_post_types();
57 +
58 + self::register_oembed_providers();
59 + Embed::init();
37 60 }
38 61
39 62 /**
40 - * Activation Hook
41 - *
42 - * @return void
63 + * Activation Hook.
43 64 */
44 65 public static function activate() {
45 66 self::flush_rewrite_rules();
67 + Scheduler::register_schedules();
46 68
47 - Scheduler::register_schedules();
69 + \add_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ), 10, 3 );
70 + Migration::update_comment_counts();
48 71 }
49 72
50 73 /**
51 - * Deactivation Hook
52 - *
53 - * @return void
74 + * Deactivation Hook.
54 75 */
55 76 public static function deactivate() {
56 77 self::flush_rewrite_rules();
78 + Scheduler::deregister_schedules();
57 79
58 - Scheduler::deregister_schedules();
80 + \remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ) );
81 + Migration::update_comment_counts( 2000 );
59 82 }
60 83
61 84 /**
62 - * Uninstall Hook
63 - *
64 - * @return void
85 + * Uninstall Hook.
65 86 */
66 87 public static function uninstall() {
67 88 Scheduler::deregister_schedules();
89 +
90 + \remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ) );
91 + Migration::update_comment_counts( 2000 );
92 +
93 + \delete_option( 'activitypub_actor_mode' );
94 + \delete_option( 'activitypub_allow_likes' );
95 + \delete_option( 'activitypub_allow_replies' );
96 + \delete_option( 'activitypub_attribution_domains' );
97 + \delete_option( 'activitypub_authorized_fetch' );
98 + \delete_option( 'activitypub_application_user_private_key' );
99 + \delete_option( 'activitypub_application_user_public_key' );
100 + \delete_option( 'activitypub_blog_user_also_known_as' );
101 + \delete_option( 'activitypub_blog_user_moved_to' );
102 + \delete_option( 'activitypub_blog_user_private_key' );
103 + \delete_option( 'activitypub_blog_user_public_key' );
104 + \delete_option( 'activitypub_blog_description' );
105 + \delete_option( 'activitypub_blog_identifier' );
106 + \delete_option( 'activitypub_custom_post_content' );
107 + \delete_option( 'activitypub_db_version' );
108 + \delete_option( 'activitypub_default_extra_fields' );
109 + \delete_option( 'activitypub_enable_blog_user' );
110 + \delete_option( 'activitypub_enable_users' );
111 + \delete_option( 'activitypub_header_image' );
112 + \delete_option( 'activitypub_last_post_with_permalink_as_id' );
113 + \delete_option( 'activitypub_mailer_new_follower' );
114 + \delete_option( 'activitypub_mailer_new_dm' );
115 + \delete_option( 'activitypub_max_image_attachments' );
116 + \delete_option( 'activitypub_migration_lock' );
117 + \delete_option( 'activitypub_object_type' );
118 + \delete_option( 'activitypub_outbox_purge_days' );
119 + \delete_option( 'activitypub_shared_inbox' );
120 + \delete_option( 'activitypub_support_post_types' );
121 + \delete_option( 'activitypub_use_hashtags' );
122 + \delete_option( 'activitypub_use_opengraph' );
123 + \delete_option( 'activitypub_use_permalink_as_id_for_blog' );
124 + \delete_option( 'activitypub_vary_header' );
68 125 }
69 126
70 127 /**
71 128 * Return a AS2 JSON version of an author, post or page.
@@ -73,48 +130,214 @@
73 130 * @param string $template The path to the template object.
74 131 *
75 132 * @return string The new path to the JSON template.
76 133 */
77 - public static function render_json_template( $template ) {
78 - if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
134 + public static function render_activitypub_template( $template ) {
135 + if ( \wp_is_serving_rest_request() || \wp_doing_ajax() ) {
79 136 return $template;
80 137 }
81 138
139 + self::add_headers();
140 +
82 141 if ( ! is_activitypub_request() ) {
83 142 return $template;
84 143 }
85 144
86 - $json_template = false;
145 + $activitypub_template = false;
146 + $activitypub_object = Query::get_instance()->get_activitypub_object();
87 147
88 - // check if user can publish posts
89 - if ( \is_author() && is_wp_error( Users::get_by_id( \get_the_author_meta( 'ID' ) ) ) ) {
90 - return $template;
91 - }
148 + if ( $activitypub_object ) {
149 + if ( \get_query_var( 'preview' ) ) {
150 + \define( 'ACTIVITYPUB_PREVIEW', true );
92 151
93 - if ( \is_author() ) {
94 - $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/author-json.php';
95 - } elseif ( \is_singular() ) {
96 - $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/post-json.php';
97 - } elseif ( \is_home() ) {
98 - $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/blog-json.php';
152 + /**
153 + * Filter the template used for the ActivityPub preview.
154 + *
155 + * @param string $activitypub_template Absolute path to the template file.
156 + */
157 + $activitypub_template = apply_filters( 'activitypub_preview_template', ACTIVITYPUB_PLUGIN_DIR . '/templates/post-preview.php' );
158 + } else {
159 + $activitypub_template = ACTIVITYPUB_PLUGIN_DIR . 'templates/activitypub-json.php';
160 + }
99 161 }
100 162
101 - if ( ACTIVITYPUB_AUTHORIZED_FETCH ) {
163 + /*
164 + * Check if the request is authorized.
165 + *
166 + * @see https://www.w3.org/wiki/SocialCG/ActivityPub/Primer/Authentication_Authorization#Authorized_fetch
167 + * @see https://swicg.github.io/activitypub-http-signature/#authorized-fetch
168 + */
169 + if ( $activitypub_template && use_authorized_fetch() ) {
102 170 $verification = Signature::verify_http_signature( $_SERVER );
103 171 if ( \is_wp_error( $verification ) ) {
104 - // fallback as template_loader can't return http headers
172 + header( 'HTTP/1.1 401 Unauthorized' );
173 +
174 + // Fallback as template_loader can't return http headers.
105 175 return $template;
106 176 }
107 177 }
108 178
109 - return $json_template;
179 + if ( $activitypub_template ) {
180 + \set_query_var( 'is_404', false );
181 +
182 + // Check if header already sent.
183 + if ( ! \headers_sent() ) {
184 + // Send 200 status header.
185 + \status_header( 200 );
186 + }
187 +
188 + return $activitypub_template;
189 + }
190 +
191 + return $template;
110 192 }
111 193
112 194 /**
195 + * Add the 'self' link to the header.
196 + */
197 + public static function add_headers() {
198 + $id = Query::get_instance()->get_activitypub_object_id();
199 +
200 + if ( ! $id ) {
201 + return;
202 + }
203 +
204 + if ( ! headers_sent() ) {
205 + \header( 'Link: <' . esc_url( $id ) . '>; title="ActivityPub (JSON)"; rel="alternate"; type="application/activity+json"', false );
206 +
207 + if ( \get_option( 'activitypub_vary_header' ) ) {
208 + // Send Vary header for Accept header.
209 + \header( 'Vary: Accept', false );
210 + }
211 + }
212 +
213 + add_action(
214 + 'wp_head',
215 + function () use ( $id ) {
216 + echo PHP_EOL . '<link rel="alternate" title="ActivityPub (JSON)" type="application/activity+json" href="' . esc_url( $id ) . '" />' . PHP_EOL;
217 + }
218 + );
219 + }
220 +
221 + /**
222 + * Remove trailing slash from ActivityPub @username requests.
223 + *
224 + * @param string $redirect_url The URL to redirect to.
225 + * @param string $requested_url The requested URL.
226 + *
227 + * @return string $redirect_url The possibly-unslashed redirect URL.
228 + */
229 + public static function no_trailing_redirect( $redirect_url, $requested_url ) {
230 + if ( get_query_var( 'actor' ) ) {
231 + return $requested_url;
232 + }
233 +
234 + return $redirect_url;
235 + }
236 +
237 + /**
238 + * Add support for `p` and `author` query vars.
239 + *
240 + * @param string $redirect_url The URL to redirect to.
241 + * @param string $requested_url The requested URL.
242 + *
243 + * @return string $redirect_url
244 + */
245 + public static function redirect_canonical( $redirect_url, $requested_url ) {
246 + if ( ! is_activitypub_request() ) {
247 + return $redirect_url;
248 + }
249 +
250 + $query = \wp_parse_url( $requested_url, PHP_URL_QUERY );
251 +
252 + if ( ! $query ) {
253 + return $redirect_url;
254 + }
255 +
256 + $query_params = \wp_parse_args( $query );
257 + unset( $query_params['activitypub'] );
258 +
259 + if ( 1 !== count( $query_params ) ) {
260 + return $redirect_url;
261 + }
262 +
263 + if ( isset( $query_params['p'] ) ) {
264 + return null;
265 + }
266 +
267 + if ( isset( $query_params['author'] ) ) {
268 + return null;
269 + }
270 +
271 + return $requested_url;
272 + }
273 +
274 + /**
275 + * Custom redirects for ActivityPub requests.
276 + *
277 + * @return void
278 + */
279 + public static function template_redirect() {
280 + global $wp_query;
281 +
282 + $comment_id = get_query_var( 'c', null );
283 +
284 + // Check if it seems to be a comment.
285 + if ( $comment_id ) {
286 + $comment = get_comment( $comment_id );
287 +
288 + // Load a 404-page if `c` is set but not valid.
289 + if ( ! $comment ) {
290 + $wp_query->set_404();
291 + return;
292 + }
293 +
294 + // Stop if it's not an ActivityPub comment.
295 + if ( is_activitypub_request() && ! is_local_comment( $comment ) ) {
296 + return;
297 + }
298 +
299 + wp_safe_redirect( get_comment_link( $comment ) );
300 + exit;
301 + }
302 +
303 + $actor = get_query_var( 'actor', null );
304 + if ( $actor ) {
305 + $actor = Actors::get_by_username( $actor );
306 + if ( ! $actor || \is_wp_error( $actor ) ) {
307 + $wp_query->set_404();
308 + return;
309 + }
310 +
311 + if ( is_activitypub_request() ) {
312 + return;
313 + }
314 +
315 + if ( $actor->get__id() > 0 ) {
316 + $redirect_url = $actor->get_url();
317 + } else {
318 + $redirect_url = get_bloginfo( 'url' );
319 + }
320 +
321 + wp_safe_redirect( $redirect_url, 301 );
322 + exit;
323 + }
324 + }
325 +
326 + /**
113 327 * Add the 'activitypub' query variable so WordPress won't mangle it.
328 + *
329 + * @param array $vars The query variables.
330 + *
331 + * @return array The query variables.
114 332 */
115 333 public static function add_query_vars( $vars ) {
116 334 $vars[] = 'activitypub';
335 + $vars[] = 'preview';
336 + $vars[] = 'author';
337 + $vars[] = 'actor';
338 + $vars[] = 'c';
339 + $vars[] = 'p';
117 340
118 341 return $vars;
119 342 }
120 343
@@ -149,19 +372,21 @@
149 372 return \apply_filters( 'get_avatar_data', $args, $id_or_email );
150 373 }
151 374
152 375 // Check if comment has an avatar.
153 - $avatar = self::get_avatar_url( $id_or_email->comment_ID );
376 + $avatar = \get_comment_meta( $id_or_email->comment_ID, 'avatar_url', true );
154 377
155 378 if ( $avatar ) {
156 - if ( ! isset( $args['class'] ) || ! \is_array( $args['class'] ) ) {
157 - $args['class'] = array( 'u-photo' );
158 - } else {
159 - $args['class'][] = 'u-photo';
160 - $args['class'] = \array_unique( $args['class'] );
379 + if ( empty( $args['class'] ) ) {
380 + $args['class'] = array();
381 + } elseif ( \is_string( $args['class'] ) ) {
382 + $args['class'] = \explode( ' ', $args['class'] );
161 383 }
384 +
162 385 $args['url'] = $avatar;
163 386 $args['class'][] = 'avatar-activitypub';
387 + $args['class'][] = 'u-photo';
388 + $args['class'] = \array_unique( $args['class'] );
164 389 }
165 390
166 391 return $args;
167 392 }
@@ -166,48 +391,16 @@
166 391 return $args;
167 392 }
168 393
169 394 /**
170 - * Function to retrieve Avatar URL if stored in meta.
171 - *
172 - * @param int|WP_Comment $comment
173 - *
174 - * @return string $url
175 - */
176 - public static function get_avatar_url( $comment ) {
177 - if ( \is_numeric( $comment ) ) {
178 - $comment = \get_comment( $comment );
179 - }
180 - return \get_comment_meta( $comment->comment_ID, 'avatar_url', true );
181 - }
182 -
183 - /**
184 - * Link remote comments to source url.
185 - *
186 - * @param string $comment_link
187 - * @param object|WP_Comment $comment
188 - *
189 - * @return string $url
190 - */
191 - public static function remote_comment_link( $comment_link, $comment ) {
192 - $remote_comment_link = get_comment_meta( $comment->comment_ID, 'source_url', true );
193 - if ( $remote_comment_link ) {
194 - $comment_link = esc_url( $remote_comment_link );
195 - }
196 - return $comment_link;
197 - }
198 -
199 - /**
200 395 * Store permalink in meta, to send delete Activity.
201 396 *
202 397 * @param string $post_id The Post ID.
203 - *
204 - * @return void
205 398 */
206 399 public static function trash_post( $post_id ) {
207 400 \add_post_meta(
208 401 $post_id,
209 - 'activitypub_canonical_url',
402 + '_activitypub_canonical_url',
210 403 \get_permalink( $post_id ),
211 404 true
212 405 );
213 406 }
@@ -212,24 +405,24 @@
212 405 );
213 406 }
214 407
215 408 /**
216 - * Delete permalink from meta
409 + * Delete permalink from meta.
217 410 *
218 - * @param string $post_id The Post ID
219 - *
220 - * @return void
411 + * @param string $post_id The Post ID.
221 412 */
222 413 public static function untrash_post( $post_id ) {
223 - \delete_post_meta( $post_id, 'activitypub_canonical_url' );
414 + \delete_post_meta( $post_id, '_activitypub_canonical_url' );
224 415 }
225 416
226 417 /**
227 - * Add rewrite rules
418 + * Add rewrite rules.
228 419 */
229 420 public static function add_rewrite_rules() {
230 - // If another system needs to take precedence over the ActivityPub rewrite rules,
231 - // they can define their own and will manually call the appropriate functions as required.
421 + /*
422 + * If another system needs to take precedence over the ActivityPub rewrite rules,
423 + * they can define their own and will manually call the appropriate functions as required.
424 + */
232 425 if ( ACTIVITYPUB_DISABLE_REWRITES ) {
233 426 return;
234 427 }
235 428
@@ -243,29 +436,19 @@
243 436
244 437 if ( ! \class_exists( 'Nodeinfo_Endpoint' ) && true === (bool) \get_option( 'blog_public', 1 ) ) {
245 438 \add_rewrite_rule(
246 439 '^.well-known/nodeinfo',
247 - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo/discovery',
440 + 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo',
248 441 'top'
249 442 );
250 - \add_rewrite_rule(
251 - '^.well-known/x-nodeinfo2',
252 - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo2',
253 - 'top'
254 - );
255 443 }
256 444
257 - \add_rewrite_rule(
258 - '^@([\w\-\.]+)',
259 - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/users/$matches[1]',
260 - 'top'
261 - );
262 -
445 + \add_rewrite_rule( '^@([\w\-\.]+)\/?$', 'index.php?actor=$matches[1]', 'top' );
263 446 \add_rewrite_endpoint( 'activitypub', EP_AUTHORS | EP_PERMALINK | EP_PAGES );
264 447 }
265 448
266 449 /**
267 - * Flush rewrite rules;
450 + * Flush rewrite rules.
268 451 */
269 452 public static function flush_rewrite_rules() {
270 453 self::add_rewrite_rules();
271 454 \flush_rewrite_rules();
@@ -271,61 +454,394 @@
271 454 \flush_rewrite_rules();
272 455 }
273 456
274 457 /**
275 - * Theme compatibility stuff
276 - *
277 - * @return void
458 + * Theme compatibility stuff.
278 459 */
279 460 public static function theme_compat() {
280 - $site_icon = get_theme_support( 'custom-logo' );
461 + // We assume that you want to use Post-Formats when enabling the setting.
462 + if ( 'wordpress-post-format' === \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE ) ) {
463 + if ( ! get_theme_support( 'post-formats' ) ) {
464 + // Add support for the Aside, Gallery Post Formats...
465 + add_theme_support(
466 + 'post-formats',
467 + array(
468 + 'gallery',
469 + 'status',
470 + 'image',
471 + 'video',
472 + 'audio',
473 + )
474 + );
475 + }
476 + }
477 + }
281 478
282 - if ( ! $site_icon ) {
283 - // custom logo support
284 - add_theme_support(
285 - 'custom-logo',
286 - array(
287 - 'height' => 80,
288 - 'width' => 80,
289 - )
290 - );
291 - }
479 + /**
480 + * Register Custom Post Types.
481 + */
482 + private static function register_post_types() {
483 + \register_post_type(
484 + Followers::POST_TYPE,
485 + array(
486 + 'labels' => array(
487 + 'name' => _x( 'Followers', 'post_type plural name', 'activitypub' ),
488 + 'singular_name' => _x( 'Follower', 'post_type single name', 'activitypub' ),
489 + ),
490 + 'public' => false,
491 + 'hierarchical' => false,
492 + 'rewrite' => false,
493 + 'query_var' => false,
494 + 'delete_with_user' => false,
495 + 'can_export' => true,
496 + 'supports' => array(),
497 + )
498 + );
292 499
293 - $custom_header = get_theme_support( 'custom-header' );
500 + \register_post_meta(
501 + Followers::POST_TYPE,
502 + '_activitypub_inbox',
503 + array(
504 + 'type' => 'string',
505 + 'single' => true,
506 + 'sanitize_callback' => 'sanitize_url',
507 + )
508 + );
294 509
295 - if ( ! $custom_header ) {
296 - // This theme supports a custom header
297 - $custom_header_args = array(
298 - 'width' => 1250,
299 - 'height' => 600,
300 - 'header-text' => true,
301 - );
302 - add_theme_support( 'custom-header', $custom_header_args );
510 + \register_post_meta(
511 + Followers::POST_TYPE,
512 + '_activitypub_errors',
513 + array(
514 + 'type' => 'string',
515 + 'single' => false,
516 + 'sanitize_callback' => function ( $value ) {
517 + if ( ! is_string( $value ) ) {
518 + throw new Exception( 'Error message is no valid string' );
519 + }
520 +
521 + return esc_sql( $value );
522 + },
523 + )
524 + );
525 +
526 + \register_post_meta(
527 + Followers::POST_TYPE,
528 + '_activitypub_user_id',
529 + array(
530 + 'type' => 'string',
531 + 'single' => false,
532 + 'sanitize_callback' => function ( $value ) {
533 + return esc_sql( $value );
534 + },
535 + )
536 + );
537 +
538 + \register_post_meta(
539 + Followers::POST_TYPE,
540 + '_activitypub_actor_json',
541 + array(
542 + 'type' => 'string',
543 + 'single' => true,
544 + 'sanitize_callback' => function ( $value ) {
545 + return sanitize_text_field( $value );
546 + },
547 + )
548 + );
549 +
550 + // Register Outbox Post-Type.
551 + register_post_type(
552 + Outbox::POST_TYPE,
553 + array(
554 + 'labels' => array(
555 + 'name' => _x( 'Outbox', 'post_type plural name', 'activitypub' ),
556 + 'singular_name' => _x( 'Outbox Item', 'post_type single name', 'activitypub' ),
557 + ),
558 + 'capabilities' => array(
559 + 'create_posts' => false,
560 + ),
561 + 'map_meta_cap' => true,
562 + 'public' => false,
563 + 'show_in_rest' => true,
564 + 'rewrite' => false,
565 + 'query_var' => false,
566 + 'supports' => array( 'title', 'editor', 'author', 'custom-fields' ),
567 + 'delete_with_user' => true,
568 + 'can_export' => true,
569 + 'exclude_from_search' => true,
570 + )
571 + );
572 +
573 + /**
574 + * Register Activity Type meta for Outbox items.
575 + *
576 + * @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
577 + */
578 + \register_post_meta(
579 + Outbox::POST_TYPE,
580 + '_activitypub_activity_type',
581 + array(
582 + 'type' => 'string',
583 + 'description' => 'The type of the activity',
584 + 'single' => true,
585 + 'show_in_rest' => true,
586 + 'sanitize_callback' => function ( $value ) {
587 + $value = ucfirst( strtolower( $value ) );
588 + $schema = array(
589 + 'type' => 'string',
590 + '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' ),
591 + 'default' => 'Announce',
592 + );
593 +
594 + if ( is_wp_error( rest_validate_enum( $value, $schema, '' ) ) ) {
595 + return $schema['default'];
596 + }
597 +
598 + return $value;
599 + },
600 + )
601 + );
602 +
603 + \register_post_meta(
604 + Outbox::POST_TYPE,
605 + '_activitypub_activity_actor',
606 + array(
607 + 'type' => 'string',
608 + 'single' => true,
609 + 'show_in_rest' => true,
610 + 'sanitize_callback' => function ( $value ) {
611 + $schema = array(
612 + 'type' => 'string',
613 + 'enum' => array( 'application', 'blog', 'user' ),
614 + 'default' => 'user',
615 + );
616 +
617 + if ( is_wp_error( rest_validate_enum( $value, $schema, '' ) ) ) {
618 + return $schema['default'];
619 + }
620 +
621 + return $value;
622 + },
623 + )
624 + );
625 +
626 + \register_post_meta(
627 + Outbox::POST_TYPE,
628 + '_activitypub_outbox_offset',
629 + array(
630 + 'type' => 'integer',
631 + 'single' => true,
632 + 'description' => 'Keeps track of the followers offset when processing outbox items.',
633 + 'sanitize_callback' => 'absint',
634 + 'default' => 0,
635 + )
636 + );
637 +
638 + \register_post_meta(
639 + Outbox::POST_TYPE,
640 + '_activitypub_object_id',
641 + array(
642 + 'type' => 'string',
643 + 'single' => true,
644 + 'description' => 'The ID (ActivityPub URI) of the object that the outbox item is about.',
645 + 'sanitize_callback' => 'sanitize_url',
646 + )
647 + );
648 +
649 + \register_post_meta(
650 + Outbox::POST_TYPE,
651 + 'activitypub_content_visibility',
652 + array(
653 + 'type' => 'string',
654 + 'single' => true,
655 + 'show_in_rest' => true,
656 + 'sanitize_callback' => function ( $value ) {
657 + $schema = array(
658 + 'type' => 'string',
659 + 'enum' => array( ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC, ACTIVITYPUB_CONTENT_VISIBILITY_QUIET_PUBLIC, ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE, ACTIVITYPUB_CONTENT_VISIBILITY_LOCAL ),
660 + 'default' => ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC,
661 + );
662 +
663 + if ( is_wp_error( rest_validate_enum( $value, $schema, '' ) ) ) {
664 + return $schema['default'];
665 + }
666 +
667 + return $value;
668 + },
669 + )
670 + );
671 +
672 + // Both User and Blog Extra Fields types have the same args.
673 + $args = array(
674 + 'labels' => array(
675 + 'name' => _x( 'Extra fields', 'post_type plural name', 'activitypub' ),
676 + 'singular_name' => _x( 'Extra field', 'post_type single name', 'activitypub' ),
677 + 'add_new' => __( 'Add new', 'activitypub' ),
678 + 'add_new_item' => __( 'Add new extra field', 'activitypub' ),
679 + 'new_item' => __( 'New extra field', 'activitypub' ),
680 + 'edit_item' => __( 'Edit extra field', 'activitypub' ),
681 + 'view_item' => __( 'View extra field', 'activitypub' ),
682 + 'all_items' => __( 'All extra fields', 'activitypub' ),
683 + ),
684 + 'public' => false,
685 + 'hierarchical' => false,
686 + 'query_var' => false,
687 + 'has_archive' => false,
688 + 'publicly_queryable' => false,
689 + 'show_in_menu' => false,
690 + 'delete_with_user' => true,
691 + 'can_export' => true,
692 + 'exclude_from_search' => true,
693 + 'show_in_rest' => true,
694 + 'map_meta_cap' => true,
695 + 'show_ui' => true,
696 + 'supports' => array( 'title', 'editor', 'page-attributes' ),
697 + );
698 +
699 + \register_post_type( Extra_Fields::USER_POST_TYPE, $args );
700 + \register_post_type( Extra_Fields::BLOG_POST_TYPE, $args );
701 +
702 + /**
703 + * Fires after ActivityPub custom post types have been registered.
704 + */
705 + \do_action( 'activitypub_after_register_post_type' );
706 + }
707 +
708 + /**
709 + * Add the 'activitypub' capability to users who can publish posts.
710 + *
711 + * @param int $user_id User ID.
712 + */
713 + public static function user_register( $user_id ) {
714 + if ( \user_can( $user_id, 'publish_posts' ) ) {
715 + $user = \get_user_by( 'id', $user_id );
716 + $user->add_cap( 'activitypub' );
303 717 }
304 718 }
305 719
306 720 /**
307 - * Display plugin upgrade notice to users
721 + * Delete `activitypub_content_visibility` when updated to an empty value.
308 722 *
309 - * @param array $data The plugin data
310 - *
311 - * @return void
723 + * @param int $meta_id ID of updated metadata entry.
724 + * @param int $object_id Post ID.
725 + * @param string $meta_key Metadata key.
726 + * @param mixed $meta_value Metadata value. This will be a PHP-serialized string representation of the value
727 + * if the value is an array, an object, or itself a PHP-serialized string.
312 728 */
313 - public static function plugin_update_message( $data ) {
314 - if ( ! isset( $data['upgrade_notice'] ) ) {
315 - return;
729 + public static function updated_postmeta( $meta_id, $object_id, $meta_key, $meta_value ) {
730 + if ( 'activitypub_content_visibility' === $meta_key && empty( $meta_value ) ) {
731 + \delete_post_meta( $object_id, 'activitypub_content_visibility' );
316 732 }
733 + }
317 734
318 - printf(
319 - '<div class="update-message">%s</div>',
320 - wp_kses(
321 - wpautop( $data['upgrade_notice '] ),
322 - array(
323 - 'p' => array(),
324 - 'a' => array( 'href', 'title' ),
325 - 'strong' => array(),
326 - 'em' => array(),
327 - )
735 + /**
736 + * Register some Mastodon oEmbed providers.
737 + */
738 + public static function register_oembed_providers() {
739 + \wp_oembed_add_provider( '#https?://mastodon\.social/(@.+)/([0-9]+)#i', 'https://mastodon.social/api/oembed', true );
740 + \wp_oembed_add_provider( '#https?://mastodon\.online/(@.+)/([0-9]+)#i', 'https://mastodon.online/api/oembed', true );
741 + \wp_oembed_add_provider( '#https?://mastodon\.cloud/(@.+)/([0-9]+)#i', 'https://mastodon.cloud/api/oembed', true );
742 + \wp_oembed_add_provider( '#https?://mstdn\.social/(@.+)/([0-9]+)#i', 'https://mstdn.social/api/oembed', true );
743 + \wp_oembed_add_provider( '#https?://mastodon\.world/(@.+)/([0-9]+)#i', 'https://mastodon.world/api/oembed', true );
744 + \wp_oembed_add_provider( '#https?://mas\.to/(@.+)/([0-9]+)#i', 'https://mas.to/api/oembed', true );
745 + }
746 +
747 + /**
748 + * Register user meta.
749 + */
750 + public static function register_user_meta() {
751 + $blog_prefix = $GLOBALS['wpdb']->get_blog_prefix();
752 +
753 + \register_meta(
754 + 'user',
755 + $blog_prefix . 'activitypub_also_known_as',
756 + array(
757 + 'type' => 'array',
758 + 'description' => 'An array of URLs that the user is known by.',
759 + 'single' => true,
760 + 'default' => array(),
761 + 'sanitize_callback' => array( Sanitize::class, 'url_list' ),
762 + )
763 + );
764 +
765 + \register_meta(
766 + 'user',
767 + $blog_prefix . 'activitypub_old_host_data',
768 + array(
769 + 'description' => 'Actor object for the user on the old host.',
770 + 'single' => true,
771 + )
772 + );
773 +
774 + \register_meta(
775 + 'user',
776 + $blog_prefix . 'activitypub_moved_to',
777 + array(
778 + 'type' => 'string',
779 + 'description' => 'The new URL of the user.',
780 + 'single' => true,
781 + 'sanitize_callback' => 'sanitize_url',
782 + )
783 + );
784 +
785 + \register_meta(
786 + 'user',
787 + $blog_prefix . 'activitypub_description',
788 + array(
789 + 'type' => 'string',
790 + 'description' => 'The user’s description.',
791 + 'single' => true,
792 + 'default' => '',
793 + 'sanitize_callback' => function ( $value ) {
794 + return wp_kses( $value, 'user_description' );
795 + },
796 + )
797 + );
798 +
799 + \register_meta(
800 + 'user',
801 + $blog_prefix . 'activitypub_icon',
802 + array(
803 + 'type' => 'integer',
804 + 'description' => 'The attachment ID for user’s profile image.',
805 + 'single' => true,
806 + 'default' => 0,
807 + 'sanitize_callback' => 'absint',
808 + )
809 + );
810 +
811 + \register_meta(
812 + 'user',
813 + $blog_prefix . 'activitypub_header_image',
814 + array(
815 + 'type' => 'integer',
816 + 'description' => 'The attachment ID for the user’s header image.',
817 + 'single' => true,
818 + 'default' => 0,
819 + 'sanitize_callback' => 'absint',
820 + )
821 + );
822 +
823 + \register_meta(
824 + 'user',
825 + 'activitypub_show_welcome_tab',
826 + array(
827 + 'type' => 'integer',
828 + 'description' => 'Whether to show the welcome tab.',
829 + 'single' => true,
830 + 'default' => 1,
831 + 'sanitize_callback' => 'absint',
832 + )
833 + );
834 +
835 + \register_meta(
836 + 'user',
837 + 'activitypub_show_advanced_tab',
838 + array(
839 + 'type' => 'integer',
840 + 'description' => 'Whether to show the advanced tab.',
841 + 'single' => true,
842 + 'default' => 0,
843 + 'sanitize_callback' => 'absint',
328 844 )
329 845 );
330 846 }
331 847 }