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