PluginProbe
ActivityPub / 7.7.0
ActivityPub v7.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 +250 -320 2.1.17.7.0 View file →
@@ -1,20 +1,18 @@
1 1 <?php
2 +/**
3 + * ActivityPub Class.
4 + *
5 + * @package Activitypub
6 + */
7 +
2 8 namespace Activitypub;
3 9
4 -use Exception;
5 -use Activitypub\Signature;
6 -use Activitypub\Collection\Users;
7 10 use Activitypub\Collection\Followers;
11 +use Activitypub\Collection\Following;
8 12
9 -use function Activitypub\is_comment;
10 -use function Activitypub\sanitize_url;
11 -use function Activitypub\is_local_comment;
12 -use function Activitypub\is_activitypub_request;
13 -use function Activitypub\should_comment_be_federated;
14 -
15 13 /**
16 - * ActivityPub Class
14 + * ActivityPub Class.
17 15 *
18 16 * @author Matthias Pfefferle
19 17 */
20 18 class Activitypub {
@@ -21,226 +19,85 @@
21 19 /**
22 20 * Initialize the class, registering WordPress hooks.
23 21 */
24 22 public static function init() {
25 - \add_filter( 'template_include', array( self::class, 'render_json_template' ), 99 );
26 - \add_action( 'template_redirect', array( self::class, 'template_redirect' ) );
27 - \add_filter( 'query_vars', array( self::class, 'add_query_vars' ) );
28 - \add_filter( 'pre_get_avatar_data', array( self::class, 'pre_get_avatar_data' ), 11, 2 );
23 + \add_action( 'init', array( self::class, 'theme_compat' ), 11 );
24 + \add_action( 'init', array( self::class, 'register_user_meta' ), 11 );
29 25
30 - // Add support for ActivityPub to custom post types
31 - $post_types = \get_option( 'activitypub_support_post_types', array( 'post' ) ) ? \get_option( 'activitypub_support_post_types', array( 'post' ) ) : array();
32 -
33 - foreach ( $post_types as $post_type ) {
34 - \add_post_type_support( $post_type, 'activitypub' );
35 - }
36 -
37 26 \add_action( 'wp_trash_post', array( self::class, 'trash_post' ), 1 );
38 27 \add_action( 'untrash_post', array( self::class, 'untrash_post' ), 1 );
39 28
40 - \add_action( 'init', array( self::class, 'add_rewrite_rules' ), 11 );
29 + \add_action( 'user_register', array( self::class, 'user_register' ) );
41 30
42 - \add_action( 'after_setup_theme', array( self::class, 'theme_compat' ), 99 );
43 -
44 - \add_action( 'in_plugin_update_message-' . ACTIVITYPUB_PLUGIN_BASENAME, array( self::class, 'plugin_update_message' ) );
45 -
46 - // register several post_types
47 - self::register_post_types();
31 + \add_action( 'activitypub_add_user_block', array( Followers::class, 'remove_blocked_actors' ), 10, 3 );
32 + \add_action( 'activitypub_add_user_block', array( Following::class, 'remove_blocked_actors' ), 10, 3 );
48 33 }
49 34
50 35 /**
51 - * Activation Hook
36 + * Activation Hook.
52 37 *
53 - * @return void
38 + * @param bool $network_wide Whether to activate the plugin for all sites in the network or just the current site.
54 39 */
55 - public static function activate() {
40 + public static function activate( $network_wide ) {
56 41 self::flush_rewrite_rules();
57 42 Scheduler::register_schedules();
58 - }
59 43
60 - /**
61 - * Deactivation Hook
62 - *
63 - * @return void
64 - */
65 - public static function deactivate() {
66 - self::flush_rewrite_rules();
67 - Scheduler::deregister_schedules();
68 - }
44 + \add_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ), 10, 3 );
45 + Migration::update_comment_counts();
69 46
70 - /**
71 - * Uninstall Hook
72 - *
73 - * @return void
74 - */
75 - public static function uninstall() {
76 - Scheduler::deregister_schedules();
77 - }
78 -
79 - /**
80 - * Return a AS2 JSON version of an author, post or page.
81 - *
82 - * @param string $template The path to the template object.
83 - *
84 - * @return string The new path to the JSON template.
85 - */
86 - public static function render_json_template( $template ) {
87 - if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
88 - return $template;
89 - }
90 -
91 - if ( ! is_activitypub_request() ) {
92 - return $template;
93 - }
94 -
95 - $json_template = false;
96 -
97 - // check if user can publish posts
98 - if ( \is_author() && is_wp_error( Users::get_by_id( \get_the_author_meta( 'ID' ) ) ) ) {
99 - return $template;
100 - }
101 -
102 - // check if blog-user is enabled
103 - if ( \is_home() && is_wp_error( Users::get_by_id( Users::BLOG_USER_ID ) ) ) {
104 - return $template;
105 - }
106 -
107 - if ( \is_author() ) {
108 - $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/author-json.php';
109 - } elseif ( is_comment() ) {
110 - $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/comment-json.php';
111 - } elseif ( \is_singular() ) {
112 - $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/post-json.php';
113 - } elseif ( \is_home() ) {
114 - $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/blog-json.php';
115 - }
116 -
117 - if ( ACTIVITYPUB_AUTHORIZED_FETCH ) {
118 - $verification = Signature::verify_http_signature( $_SERVER );
119 - if ( \is_wp_error( $verification ) ) {
120 - // fallback as template_loader can't return http headers
121 - return $template;
47 + if ( \is_multisite() && $network_wide && ! \wp_is_large_network() ) {
48 + $sites = \get_sites( array( 'fields' => 'ids' ) );
49 + foreach ( $sites as $site ) {
50 + \switch_to_blog( $site );
51 + self::flush_rewrite_rules();
52 + \restore_current_blog();
122 53 }
123 54 }
124 -
125 - return $json_template;
126 55 }
127 56
128 57 /**
129 - * Custom redirects for ActivityPub requests.
58 + * Deactivation Hook.
130 59 *
131 - * @return void
60 + * @param bool $network_wide Whether to deactivate the plugin for all sites in the network or just the current site.
132 61 */
133 - public static function template_redirect() {
134 - $comment_id = get_query_var( 'c', null );
62 + public static function deactivate( $network_wide ) {
63 + self::flush_rewrite_rules();
64 + Scheduler::deregister_schedules();
135 65
136 - // check if it seems to be a comment
137 - if ( ! $comment_id ) {
138 - return;
139 - }
66 + \remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ) );
67 + Migration::update_comment_counts( 2000 );
140 68
141 - $comment = get_comment( $comment_id );
142 -
143 - // load a 404 page if `c` is set but not valid
144 - if ( ! $comment ) {
145 - global $wp_query;
146 - $wp_query->set_404();
147 - return;
69 + if ( \is_multisite() && $network_wide && ! \wp_is_large_network() ) {
70 + $sites = \get_sites( array( 'fields' => 'ids' ) );
71 + foreach ( $sites as $site ) {
72 + \switch_to_blog( $site );
73 + self::flush_rewrite_rules();
74 + \restore_current_blog();
75 + }
148 76 }
149 -
150 - // stop if it's not an ActivityPub comment
151 - if ( is_activitypub_request() && ! is_local_comment( $comment ) ) {
152 - return;
153 - }
154 -
155 - wp_safe_redirect( get_comment_link( $comment ) );
156 - exit;
157 77 }
158 78
159 79 /**
160 - * Add the 'activitypub' query variable so WordPress won't mangle it.
80 + * Uninstall Hook.
161 81 */
162 - public static function add_query_vars( $vars ) {
163 - $vars[] = 'activitypub';
164 - $vars[] = 'c';
165 - $vars[] = 'p';
82 + public static function uninstall() {
83 + Scheduler::deregister_schedules();
166 84
167 - return $vars;
168 - }
85 + \remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ) );
86 + Migration::update_comment_counts( 2000 );
169 87
170 - /**
171 - * Replaces the default avatar.
172 - *
173 - * @param array $args Arguments passed to get_avatar_data(), after processing.
174 - * @param int|string|object $id_or_email A user ID, email address, or comment object.
175 - *
176 - * @return array $args
177 - */
178 - public static function pre_get_avatar_data( $args, $id_or_email ) {
179 - if (
180 - ! $id_or_email instanceof \WP_Comment ||
181 - ! isset( $id_or_email->comment_type ) ||
182 - $id_or_email->user_id
183 - ) {
184 - return $args;
185 - }
186 -
187 - $allowed_comment_types = \apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
188 - if (
189 - ! empty( $id_or_email->comment_type ) &&
190 - ! \in_array(
191 - $id_or_email->comment_type,
192 - (array) $allowed_comment_types,
193 - true
194 - )
195 - ) {
196 - $args['url'] = false;
197 - /** This filter is documented in wp-includes/link-template.php */
198 - return \apply_filters( 'get_avatar_data', $args, $id_or_email );
199 - }
200 -
201 - // Check if comment has an avatar.
202 - $avatar = self::get_avatar_url( $id_or_email->comment_ID );
203 -
204 - if ( $avatar ) {
205 - if ( ! isset( $args['class'] ) || ! \is_array( $args['class'] ) ) {
206 - $args['class'] = array( 'u-photo' );
207 - } else {
208 - $args['class'][] = 'u-photo';
209 - $args['class'] = \array_unique( $args['class'] );
210 - }
211 - $args['url'] = $avatar;
212 - $args['class'][] = 'avatar-activitypub';
213 - }
214 -
215 - return $args;
88 + Options::delete();
216 89 }
217 90
218 91 /**
219 - * Function to retrieve Avatar URL if stored in meta.
220 - *
221 - * @param int|WP_Comment $comment
222 - *
223 - * @return string $url
224 - */
225 - public static function get_avatar_url( $comment ) {
226 - if ( \is_numeric( $comment ) ) {
227 - $comment = \get_comment( $comment );
228 - }
229 - return \get_comment_meta( $comment->comment_ID, 'avatar_url', true );
230 - }
231 -
232 - /**
233 92 * Store permalink in meta, to send delete Activity.
234 93 *
235 94 * @param string $post_id The Post ID.
236 - *
237 - * @return void
238 95 */
239 96 public static function trash_post( $post_id ) {
240 97 \add_post_meta(
241 98 $post_id,
242 - 'activitypub_canonical_url',
99 + '_activitypub_canonical_url',
243 100 \get_permalink( $post_id ),
244 101 true
245 102 );
246 103 }
@@ -245,196 +102,269 @@
245 102 );
246 103 }
247 104
248 105 /**
249 - * Delete permalink from meta
106 + * Delete permalink from meta.
250 107 *
251 - * @param string $post_id The Post ID
252 - *
253 - * @return void
108 + * @param string $post_id The Post ID.
254 109 */
255 110 public static function untrash_post( $post_id ) {
256 - \delete_post_meta( $post_id, 'activitypub_canonical_url' );
111 + \delete_post_meta( $post_id, '_activitypub_canonical_url' );
257 112 }
258 113
259 114 /**
260 - * Add rewrite rules
115 + * Flush rewrite rules.
261 116 */
117 + public static function flush_rewrite_rules() {
118 + Router::add_rewrite_rules();
119 + \flush_rewrite_rules();
120 + }
121 +
122 + /**
123 + * Add rewrite rules.
124 + *
125 + * @deprecated 7.5.0 Use {@see Router::add_rewrite_rules()}.
126 + */
262 127 public static function add_rewrite_rules() {
263 - // If another system needs to take precedence over the ActivityPub rewrite rules,
264 - // they can define their own and will manually call the appropriate functions as required.
265 - if ( ACTIVITYPUB_DISABLE_REWRITES ) {
266 - return;
267 - }
128 + _deprecated_function( __FUNCTION__, '7.5.0', '\Activitypub\Router::add_rewrite_rules()' );
268 129
269 - if ( ! \class_exists( 'Webfinger' ) ) {
270 - \add_rewrite_rule(
271 - '^.well-known/webfinger',
272 - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/webfinger',
273 - 'top'
274 - );
275 - }
276 -
277 - if ( ! \class_exists( 'Nodeinfo_Endpoint' ) && true === (bool) \get_option( 'blog_public', 1 ) ) {
278 - \add_rewrite_rule(
279 - '^.well-known/nodeinfo',
280 - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo/discovery',
281 - 'top'
282 - );
283 - \add_rewrite_rule(
284 - '^.well-known/x-nodeinfo2',
285 - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo2',
286 - 'top'
287 - );
288 - }
289 -
290 - \add_rewrite_rule(
291 - '^@([\w\-\.]+)',
292 - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/users/$matches[1]',
293 - 'top'
294 - );
295 -
296 - \add_rewrite_endpoint( 'activitypub', EP_AUTHORS | EP_PERMALINK | EP_PAGES );
130 + Router::add_rewrite_rules();
297 131 }
298 132
299 133 /**
300 - * Flush rewrite rules;
134 + * Theme compatibility stuff.
301 135 */
302 - public static function flush_rewrite_rules() {
303 - self::add_rewrite_rules();
304 - \flush_rewrite_rules();
136 + public static function theme_compat() {
137 + // We assume that you want to use Post-Formats when enabling the setting.
138 + if ( 'wordpress-post-format' === \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE ) ) {
139 + if ( ! get_theme_support( 'post-formats' ) ) {
140 + // Add support for the Aside, Gallery Post Formats...
141 + add_theme_support(
142 + 'post-formats',
143 + array(
144 + 'gallery',
145 + 'status',
146 + 'image',
147 + 'video',
148 + 'audio',
149 + )
150 + );
151 + }
152 + }
305 153 }
306 154
307 155 /**
308 - * Theme compatibility stuff
156 + * Add the 'activitypub' capability to users who can publish posts.
309 157 *
310 - * @return void
158 + * @param int $user_id User ID.
311 159 */
312 - public static function theme_compat() {
313 - $site_icon = get_theme_support( 'custom-logo' );
314 -
315 - if ( ! $site_icon ) {
316 - // custom logo support
317 - add_theme_support(
318 - 'custom-logo',
319 - array(
320 - 'height' => 80,
321 - 'width' => 80,
322 - )
323 - );
160 + public static function user_register( $user_id ) {
161 + if ( \user_can( $user_id, 'publish_posts' ) ) {
162 + $user = \get_user_by( 'id', $user_id );
163 + $user->add_cap( 'activitypub' );
324 164 }
325 -
326 - $custom_header = get_theme_support( 'custom-header' );
327 -
328 - if ( ! $custom_header ) {
329 - // This theme supports a custom header
330 - $custom_header_args = array(
331 - 'width' => 1250,
332 - 'height' => 600,
333 - 'header-text' => true,
334 - );
335 - add_theme_support( 'custom-header', $custom_header_args );
336 - }
337 165 }
338 166
339 167 /**
340 - * Display plugin upgrade notice to users
341 - *
342 - * @param array $data The plugin data
343 - *
344 - * @return void
168 + * Register user meta.
345 169 */
346 - public static function plugin_update_message( $data ) {
347 - if ( ! isset( $data['upgrade_notice'] ) ) {
348 - return;
349 - }
170 + public static function register_user_meta() {
171 + $blog_prefix = $GLOBALS['wpdb']->get_blog_prefix();
350 172
351 - printf(
352 - '<div class="update-message">%s</div>',
353 - wp_kses(
354 - wpautop( $data['upgrade_notice '] ),
355 - array(
356 - 'p' => array(),
357 - 'a' => array( 'href', 'title' ),
358 - 'strong' => array(),
359 - 'em' => array(),
360 - )
173 + \register_meta(
174 + 'user',
175 + $blog_prefix . 'activitypub_also_known_as',
176 + array(
177 + 'type' => 'array',
178 + 'description' => 'An array of URLs that the user is known by.',
179 + 'single' => true,
180 + 'default' => array(),
181 + 'sanitize_callback' => array( Sanitize::class, 'identifier_list' ),
361 182 )
362 183 );
363 - }
364 184
365 - /**
366 - * Register the "Followers" Taxonomy
367 - *
368 - * @return void
369 - */
370 - private static function register_post_types() {
371 - \register_post_type(
372 - Followers::POST_TYPE,
185 + \register_meta(
186 + 'user',
187 + $blog_prefix . 'activitypub_hide_social_graph',
373 188 array(
374 - 'labels' => array(
375 - 'name' => _x( 'Followers', 'post_type plural name', 'activitypub' ),
376 - 'singular_name' => _x( 'Follower', 'post_type single name', 'activitypub' ),
377 - ),
378 - 'public' => false,
379 - 'hierarchical' => false,
380 - 'rewrite' => false,
381 - 'query_var' => false,
382 - 'delete_with_user' => false,
383 - 'can_export' => true,
384 - 'supports' => array(),
189 + 'type' => 'integer',
190 + 'description' => 'Hide Followers and Followings on Profile.',
191 + 'single' => true,
192 + 'default' => 0,
193 + 'sanitize_callback' => 'absint',
385 194 )
386 195 );
387 196
388 - \register_post_meta(
389 - Followers::POST_TYPE,
390 - 'activitypub_inbox',
197 + \register_meta(
198 + 'user',
199 + $blog_prefix . 'activitypub_old_host_data',
391 200 array(
201 + 'description' => 'Actor object for the user on the old host.',
202 + 'single' => true,
203 + )
204 + );
205 +
206 + \register_meta(
207 + 'user',
208 + $blog_prefix . 'activitypub_moved_to',
209 + array(
392 210 'type' => 'string',
211 + 'description' => 'The new URL of the user.',
393 212 'single' => true,
394 213 'sanitize_callback' => 'sanitize_url',
395 214 )
396 215 );
397 216
398 - \register_post_meta(
399 - Followers::POST_TYPE,
400 - 'activitypub_errors',
217 + \register_meta(
218 + 'user',
219 + $blog_prefix . 'activitypub_description',
401 220 array(
402 221 'type' => 'string',
403 - 'single' => false,
222 + 'description' => 'The user description.',
223 + 'single' => true,
224 + 'default' => '',
404 225 'sanitize_callback' => function ( $value ) {
405 - if ( ! is_string( $value ) ) {
406 - throw new Exception( 'Error message is no valid string' );
407 - }
226 + return wp_kses( $value, 'user_description' );
227 + },
228 + )
229 + );
408 230
409 - return esc_sql( $value );
410 - },
231 + \register_meta(
232 + 'user',
233 + $blog_prefix . 'activitypub_icon',
234 + array(
235 + 'type' => 'integer',
236 + 'description' => 'The attachment ID for user profile image.',
237 + 'single' => true,
238 + 'default' => 0,
239 + 'sanitize_callback' => 'absint',
411 240 )
412 241 );
413 242
414 - \register_post_meta(
415 - Followers::POST_TYPE,
416 - 'activitypub_user_id',
243 + \register_meta(
244 + 'user',
245 + $blog_prefix . 'activitypub_header_image',
417 246 array(
418 - 'type' => 'string',
419 - 'single' => false,
247 + 'type' => 'integer',
248 + 'description' => 'The attachment ID for the user header image.',
249 + 'single' => true,
250 + 'default' => 0,
251 + 'sanitize_callback' => 'absint',
252 + )
253 + );
254 +
255 + \register_meta(
256 + 'user',
257 + $blog_prefix . 'activitypub_mailer_new_dm',
258 + array(
259 + 'type' => 'integer',
260 + 'description' => 'Send a notification when someone sends this user a direct message.',
261 + 'single' => true,
262 + 'sanitize_callback' => 'absint',
263 + )
264 + );
265 + \add_filter( 'get_user_option_activitypub_mailer_new_dm', array( self::class, 'user_options_default' ) );
266 +
267 + \register_meta(
268 + 'user',
269 + $blog_prefix . 'activitypub_mailer_new_follower',
270 + array(
271 + 'type' => 'integer',
272 + 'description' => 'Send a notification when someone starts to follow this user.',
273 + 'single' => true,
274 + 'sanitize_callback' => 'absint',
275 + )
276 + );
277 + \add_filter( 'get_user_option_activitypub_mailer_new_follower', array( self::class, 'user_options_default' ) );
278 +
279 + \register_meta(
280 + 'user',
281 + $blog_prefix . 'activitypub_mailer_new_mention',
282 + array(
283 + 'type' => 'integer',
284 + 'description' => 'Send a notification when someone mentions this user.',
285 + 'single' => true,
286 + 'sanitize_callback' => 'absint',
287 + )
288 + );
289 + \add_filter( 'get_user_option_activitypub_mailer_new_mention', array( self::class, 'user_options_default' ) );
290 +
291 + \register_meta(
292 + 'user',
293 + 'activitypub_show_welcome_tab',
294 + array(
295 + 'type' => 'integer',
296 + 'description' => 'Whether to show the welcome tab.',
297 + 'single' => true,
298 + 'default' => 1,
299 + 'sanitize_callback' => 'absint',
300 + )
301 + );
302 +
303 + \register_meta(
304 + 'user',
305 + 'activitypub_show_advanced_tab',
306 + array(
307 + 'type' => 'integer',
308 + 'description' => 'Whether to show the advanced tab.',
309 + 'single' => true,
310 + 'default' => 0,
311 + 'sanitize_callback' => 'absint',
312 + )
313 + );
314 +
315 + // Moderation user meta.
316 + \register_meta(
317 + 'user',
318 + 'activitypub_blocked_actors',
319 + array(
320 + 'type' => 'array',
321 + 'description' => 'User-specific blocked ActivityPub actors.',
322 + 'single' => true,
323 + 'default' => array(),
324 + 'sanitize_callback' => array( Sanitize::class, 'identifier_list' ),
325 + )
326 + );
327 +
328 + \register_meta(
329 + 'user',
330 + 'activitypub_blocked_domains',
331 + array(
332 + 'type' => 'array',
333 + 'description' => 'User-specific blocked ActivityPub domains.',
334 + 'single' => true,
335 + 'default' => array(),
420 336 'sanitize_callback' => function ( $value ) {
421 - return esc_sql( $value );
337 + return \array_unique( \array_map( array( Sanitize::class, 'host_list' ), $value ) );
422 338 },
423 339 )
424 340 );
425 341
426 - \register_post_meta(
427 - Followers::POST_TYPE,
428 - 'activitypub_actor_json',
342 + \register_meta(
343 + 'user',
344 + 'activitypub_blocked_keywords',
429 345 array(
430 - 'type' => 'string',
346 + 'type' => 'array',
347 + 'description' => 'User-specific blocked ActivityPub keywords.',
431 348 'single' => true,
349 + 'default' => array(),
432 350 'sanitize_callback' => function ( $value ) {
433 - return sanitize_text_field( $value );
351 + return \array_map( 'sanitize_text_field', $value );
434 352 },
435 353 )
436 354 );
355 + }
437 356
438 - \do_action( 'activitypub_after_register_post_type' );
357 + /**
358 + * Set default values for user options.
359 + *
360 + * @param bool|string $value Option value.
361 + * @return bool|string
362 + */
363 + public static function user_options_default( $value ) {
364 + if ( false === $value ) {
365 + return '1';
366 + }
367 +
368 + return $value;
439 369 }
440 370 }