PluginProbe
ActivityPub / 3.2.2
ActivityPub v3.2.2
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 +284 -60 1.2.03.2.2 View file →
@@ -1,10 +1,21 @@
1 1 <?php
2 2 namespace Activitypub;
3 3
4 +use Exception;
4 5 use Activitypub\Signature;
5 6 use Activitypub\Collection\Users;
7 +use Activitypub\Collection\Followers;
8 +use Activitypub\Collection\Extra_Fields;
6 9
10 +use function Activitypub\is_comment;
11 +use function Activitypub\sanitize_url;
12 +use function Activitypub\is_local_comment;
13 +use function Activitypub\site_supports_blocks;
14 +use function Activitypub\is_user_type_disabled;
15 +use function Activitypub\is_activitypub_request;
16 +use function Activitypub\should_comment_be_federated;
17 +
7 18 /**
8 19 * ActivityPub Class
9 20 *
10 21 * @author Matthias Pfefferle
@@ -14,14 +25,14 @@
14 25 * Initialize the class, registering WordPress hooks.
15 26 */
16 27 public static function init() {
17 28 \add_filter( 'template_include', array( self::class, 'render_json_template' ), 99 );
29 + \add_action( 'template_redirect', array( self::class, 'template_redirect' ) );
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 33 // 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();
34 + $post_types = \get_option( 'activitypub_support_post_types', array( 'post' ) ) ? \get_option( 'activitypub_support_post_types', array( 'post' ) ) : array();
24 35
25 36 foreach ( $post_types as $post_type ) {
26 37 \add_post_type_support( $post_type, 'activitypub' );
27 38 }
@@ -29,12 +40,22 @@
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 48 \add_action( 'in_plugin_update_message-' . ACTIVITYPUB_PLUGIN_BASENAME, array( self::class, 'plugin_update_message' ) );
49 +
50 + if ( site_supports_blocks() ) {
51 + \add_action( 'tool_box', array( self::class, 'tool_box' ) );
52 + }
53 +
54 + \add_filter( 'activitypub_get_actor_extra_fields', array( Extra_Fields::class, 'default_actor_extra_fields' ), 10, 2 );
55 +
56 + // register several post_types
57 + self::register_post_types();
37 58 }
38 59
39 60 /**
40 61 * Activation Hook
@@ -42,9 +63,8 @@
42 63 * @return void
43 64 */
44 65 public static function activate() {
45 66 self::flush_rewrite_rules();
46 -
47 67 Scheduler::register_schedules();
48 68 }
49 69
50 70 /**
@@ -53,9 +73,8 @@
53 73 * @return void
54 74 */
55 75 public static function deactivate() {
56 76 self::flush_rewrite_rules();
57 -
58 77 Scheduler::deregister_schedules();
59 78 }
60 79
61 80 /**
@@ -84,37 +103,133 @@
84 103 }
85 104
86 105 $json_template = false;
87 106
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 - }
92 -
93 - if ( \is_author() ) {
94 - $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/author-json.php';
107 + if ( \is_author() && ! is_user_disabled( \get_the_author_meta( 'ID' ) ) ) {
108 + $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/user-json.php';
109 + } elseif ( is_comment() ) {
110 + $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/comment-json.php';
95 111 } elseif ( \is_singular() ) {
96 112 $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/post-json.php';
97 - } elseif ( \is_home() ) {
113 + } elseif ( \is_home() && ! is_user_type_disabled( 'blog' ) ) {
98 114 $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/blog-json.php';
99 115 }
100 116
101 - if ( ACTIVITYPUB_AUTHORIZED_FETCH ) {
117 + /*
118 + * Check if the request is authorized.
119 + *
120 + * @see https://www.w3.org/wiki/SocialCG/ActivityPub/Primer/Authentication_Authorization#Authorized_fetch
121 + * @see https://swicg.github.io/activitypub-http-signature/#authorized-fetch
122 + */
123 + if ( $json_template && ACTIVITYPUB_AUTHORIZED_FETCH ) {
102 124 $verification = Signature::verify_http_signature( $_SERVER );
103 125 if ( \is_wp_error( $verification ) ) {
126 + header( 'HTTP/1.1 401 Unauthorized' );
127 +
104 128 // fallback as template_loader can't return http headers
105 129 return $template;
106 130 }
107 131 }
108 132
109 - return $json_template;
133 + if ( $json_template ) {
134 + return $json_template;
135 + }
136 +
137 + return $template;
110 138 }
111 139
112 140 /**
141 + * Add the 'self' link to the header.
142 + *
143 + * @see
144 + *
145 + * @return void
146 + */
147 + public static function add_headers() {
148 + // phpcs:ignore
149 + $request_uri = $_SERVER['REQUEST_URI'];
150 +
151 + if ( ! $request_uri ) {
152 + return;
153 + }
154 +
155 + // only add self link to author pages...
156 + if ( is_author() ) {
157 + if ( is_user_disabled( get_queried_object_id() ) ) {
158 + return;
159 + }
160 + } elseif ( is_singular() ) { // or posts/pages/custom-post-types...
161 + if ( ! \post_type_supports( \get_post_type(), 'activitypub' ) ) {
162 + return;
163 + }
164 + } else { // otherwise return
165 + return;
166 + }
167 +
168 + // add self link to html and http header
169 + $host = wp_parse_url( home_url() );
170 + $self_link = esc_url(
171 + apply_filters(
172 + 'self_link',
173 + set_url_scheme(
174 + // phpcs:ignore
175 + 'http://' . $host['host'] . wp_unslash( $request_uri )
176 + )
177 + )
178 + );
179 +
180 + if ( ! headers_sent() ) {
181 + header( 'Link: <' . $self_link . '>; rel="alternate"; type="application/activity+json"' );
182 + }
183 +
184 + add_action(
185 + 'wp_head',
186 + function () use ( $self_link ) {
187 + echo PHP_EOL . '<link rel="alternate" type="application/activity+json" href="' . esc_url( $self_link ) . '" />' . PHP_EOL;
188 + }
189 + );
190 + }
191 +
192 + /**
193 + * Custom redirects for ActivityPub requests.
194 + *
195 + * @return void
196 + */
197 + public static function template_redirect() {
198 + self::add_headers();
199 +
200 + $comment_id = get_query_var( 'c', null );
201 +
202 + // check if it seems to be a comment
203 + if ( ! $comment_id ) {
204 + return;
205 + }
206 +
207 + $comment = get_comment( $comment_id );
208 +
209 + // load a 404 page if `c` is set but not valid
210 + if ( ! $comment ) {
211 + global $wp_query;
212 + $wp_query->set_404();
213 + return;
214 + }
215 +
216 + // stop if it's not an ActivityPub comment
217 + if ( is_activitypub_request() && ! is_local_comment( $comment ) ) {
218 + return;
219 + }
220 +
221 + wp_safe_redirect( get_comment_link( $comment ) );
222 + exit;
223 + }
224 +
225 + /**
113 226 * Add the 'activitypub' query variable so WordPress won't mangle it.
114 227 */
115 228 public static function add_query_vars( $vars ) {
116 229 $vars[] = 'activitypub';
230 + $vars[] = 'c';
231 + $vars[] = 'p';
117 232
118 233 return $vars;
119 234 }
120 235
@@ -152,16 +267,18 @@
152 267 // Check if comment has an avatar.
153 268 $avatar = self::get_avatar_url( $id_or_email->comment_ID );
154 269
155 270 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'] );
271 + if ( empty( $args['class'] ) ) {
272 + $args['class'] = array();
273 + } elseif ( \is_string( $args['class'] ) ) {
274 + $args['class'] = \explode( ' ', $args['class'] );
161 275 }
276 +
162 277 $args['url'] = $avatar;
163 278 $args['class'][] = 'avatar-activitypub';
279 + $args['class'][] = 'u-photo';
280 + $args['class'] = \array_unique( $args['class'] );
164 281 }
165 282
166 283 return $args;
167 284 }
@@ -180,24 +297,8 @@
180 297 return \get_comment_meta( $comment->comment_ID, 'avatar_url', true );
181 298 }
182 299
183 300 /**
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 301 * Store permalink in meta, to send delete Activity.
201 302 *
202 303 * @param string $post_id The Post ID.
203 304 *
@@ -255,9 +356,9 @@
255 356 }
256 357
257 358 \add_rewrite_rule(
258 359 '^@([\w\-\.]+)',
259 - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/users/$matches[1]',
360 + 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/actors/$matches[1]',
260 361 'top'
261 362 );
262 363
263 364 \add_rewrite_endpoint( 'activitypub', EP_AUTHORS | EP_PERMALINK | EP_PAGES );
@@ -271,37 +372,40 @@
271 372 \flush_rewrite_rules();
272 373 }
273 374
274 375 /**
376 + * Adds metabox on wp-admin/tools.php
377 + *
378 + * @return void
379 + */
380 + public static function tool_box() {
381 + if ( \current_user_can( 'edit_posts' ) ) {
382 + \load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/toolbox.php' );
383 + }
384 + }
385 +
386 + /**
275 387 * Theme compatibility stuff
276 388 *
277 389 * @return void
278 390 */
279 391 public static function theme_compat() {
280 - $site_icon = get_theme_support( 'custom-logo' );
281 -
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 - );
392 + // We assume that you want to use Post-Formats when enabling the setting
393 + if ( 'wordpress-post-format' === \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE ) ) {
394 + if ( ! get_theme_support( 'post-formats' ) ) {
395 + // Add support for the Aside, Gallery Post Formats...
396 + add_theme_support(
397 + 'post-formats',
398 + array(
399 + 'gallery',
400 + 'status',
401 + 'image',
402 + 'video',
403 + 'audio',
404 + )
405 + );
406 + }
291 407 }
292 -
293 - $custom_header = get_theme_support( 'custom-header' );
294 -
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 );
303 - }
304 408 }
305 409
306 410 /**
307 411 * Display plugin upgrade notice to users
@@ -326,6 +430,126 @@
326 430 'em' => array(),
327 431 )
328 432 )
329 433 );
434 + }
435 +
436 + /**
437 + * Register the "Followers" Taxonomy
438 + *
439 + * @return void
440 + */
441 + private static function register_post_types() {
442 + \register_post_type(
443 + Followers::POST_TYPE,
444 + array(
445 + 'labels' => array(
446 + 'name' => _x( 'Followers', 'post_type plural name', 'activitypub' ),
447 + 'singular_name' => _x( 'Follower', 'post_type single name', 'activitypub' ),
448 + ),
449 + 'public' => false,
450 + 'hierarchical' => false,
451 + 'rewrite' => false,
452 + 'query_var' => false,
453 + 'delete_with_user' => false,
454 + 'can_export' => true,
455 + 'supports' => array(),
456 + )
457 + );
458 +
459 + \register_post_meta(
460 + Followers::POST_TYPE,
461 + 'activitypub_inbox',
462 + array(
463 + 'type' => 'string',
464 + 'single' => true,
465 + 'sanitize_callback' => 'sanitize_url',
466 + )
467 + );
468 +
469 + \register_post_meta(
470 + Followers::POST_TYPE,
471 + 'activitypub_errors',
472 + array(
473 + 'type' => 'string',
474 + 'single' => false,
475 + 'sanitize_callback' => function ( $value ) {
476 + if ( ! is_string( $value ) ) {
477 + throw new Exception( 'Error message is no valid string' );
478 + }
479 +
480 + return esc_sql( $value );
481 + },
482 + )
483 + );
484 +
485 + \register_post_meta(
486 + Followers::POST_TYPE,
487 + 'activitypub_user_id',
488 + array(
489 + 'type' => 'string',
490 + 'single' => false,
491 + 'sanitize_callback' => function ( $value ) {
492 + return esc_sql( $value );
493 + },
494 + )
495 + );
496 +
497 + \register_post_meta(
498 + Followers::POST_TYPE,
499 + 'activitypub_actor_json',
500 + array(
501 + 'type' => 'string',
502 + 'single' => true,
503 + 'sanitize_callback' => function ( $value ) {
504 + return sanitize_text_field( $value );
505 + },
506 + )
507 + );
508 +
509 + // Both User and Blog Extra Fields types have the same args.
510 + $args = array(
511 + 'labels' => array(
512 + 'name' => _x( 'Extra fields', 'post_type plural name', 'activitypub' ),
513 + 'singular_name' => _x( 'Extra field', 'post_type single name', 'activitypub' ),
514 + 'add_new' => __( 'Add new', 'activitypub' ),
515 + 'add_new_item' => __( 'Add new extra field', 'activitypub' ),
516 + 'new_item' => __( 'New extra field', 'activitypub' ),
517 + 'edit_item' => __( 'Edit extra field', 'activitypub' ),
518 + 'view_item' => __( 'View extra field', 'activitypub' ),
519 + 'all_items' => __( 'All extra fields', 'activitypub' ),
520 + ),
521 + 'public' => false,
522 + 'hierarchical' => false,
523 + 'query_var' => false,
524 + 'has_archive' => false,
525 + 'publicly_queryable' => false,
526 + 'show_in_menu' => false,
527 + 'delete_with_user' => true,
528 + 'can_export' => true,
529 + 'exclude_from_search' => true,
530 + 'show_in_rest' => true,
531 + 'map_meta_cap' => true,
532 + 'show_ui' => true,
533 + 'supports' => array( 'title', 'editor', 'page-attributes' ),
534 + );
535 +
536 + \register_post_type( Extra_Fields::USER_POST_TYPE, $args );
537 + \register_post_type( Extra_Fields::BLOG_POST_TYPE, $args );
538 +
539 + \do_action( 'activitypub_after_register_post_type' );
540 + }
541 +
542 + /**
543 + * Add the 'activitypub' capability to users who can publish posts.
544 + *
545 + * @param int $user_id User ID.
546 + *
547 + * @param array $userdata The raw array of data passed to wp_insert_user().
548 + */
549 + public static function user_register( $user_id ) {
550 + if ( \user_can( $user_id, 'publish_posts' ) ) {
551 + $user = \get_user_by( 'id', $user_id );
552 + $user->add_cap( 'activitypub' );
553 + }
330 554 }
331 555 }