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 +253 -296 1.3.07.7.0 View file →
@@ -1,16 +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\sanitize_url;
10 -
11 13 /**
12 - * ActivityPub Class
14 + * ActivityPub Class.
13 15 *
14 16 * @author Matthias Pfefferle
15 17 */
16 18 class Activitypub {
@@ -17,190 +19,74 @@
17 19 /**
18 20 * Initialize the class, registering WordPress hooks.
19 21 */
20 22 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 );
23 + \add_action( 'init', array( self::class, 'theme_compat' ), 11 );
24 + \add_action( 'init', array( self::class, 'register_user_meta' ), 11 );
25 25
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 26 \add_action( 'wp_trash_post', array( self::class, 'trash_post' ), 1 );
34 27 \add_action( 'untrash_post', array( self::class, 'untrash_post' ), 1 );
35 28
36 - \add_action( 'init', array( self::class, 'add_rewrite_rules' ), 11 );
29 + \add_action( 'user_register', array( self::class, 'user_register' ) );
37 30
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();
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 );
44 33 }
45 34
46 35 /**
47 - * Activation Hook
36 + * Activation Hook.
48 37 *
49 - * @return void
38 + * @param bool $network_wide Whether to activate the plugin for all sites in the network or just the current site.
50 39 */
51 - public static function activate() {
40 + public static function activate( $network_wide ) {
52 41 self::flush_rewrite_rules();
53 -
54 42 Scheduler::register_schedules();
55 - }
56 43
57 - /**
58 - * Deactivation Hook
59 - *
60 - * @return void
61 - */
62 - public static function deactivate() {
63 - self::flush_rewrite_rules();
64 - Scheduler::deregister_schedules();
65 - }
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();
66 46
67 - /**
68 - * Uninstall Hook
69 - *
70 - * @return void
71 - */
72 - public static function uninstall() {
73 - Scheduler::deregister_schedules();
74 - }
75 -
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;
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();
112 53 }
113 54 }
114 -
115 - return $json_template;
116 55 }
117 56
118 57 /**
119 - * Add the 'activitypub' query variable so WordPress won't mangle it.
120 - */
121 - public static function add_query_vars( $vars ) {
122 - $vars[] = 'activitypub';
123 -
124 - return $vars;
125 - }
126 -
127 - /**
128 - * Replaces the default avatar.
58 + * Deactivation Hook.
129 59 *
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
60 + * @param bool $network_wide Whether to deactivate the plugin for all sites in the network or just the current site.
134 61 */
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 - }
62 + public static function deactivate( $network_wide ) {
63 + self::flush_rewrite_rules();
64 + Scheduler::deregister_schedules();
143 65
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 - }
66 + \remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ) );
67 + Migration::update_comment_counts( 2000 );
157 68
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'] );
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();
167 75 }
168 - $args['url'] = $avatar;
169 - $args['class'][] = 'avatar-activitypub';
170 76 }
171 -
172 - return $args;
173 77 }
174 78
175 79 /**
176 - * Function to retrieve Avatar URL if stored in meta.
177 - *
178 - * @param int|WP_Comment $comment
179 - *
180 - * @return string $url
80 + * Uninstall Hook.
181 81 */
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 - }
82 + public static function uninstall() {
83 + Scheduler::deregister_schedules();
188 84
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;
85 + \remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ) );
86 + Migration::update_comment_counts( 2000 );
87 +
88 + Options::delete();
203 89 }
204 90
205 91 /**
206 92 * Store permalink in meta, to send delete Activity.
@@ -205,15 +91,13 @@
205 91 /**
206 92 * Store permalink in meta, to send delete Activity.
207 93 *
208 94 * @param string $post_id The Post ID.
209 - *
210 - * @return void
211 95 */
212 96 public static function trash_post( $post_id ) {
213 97 \add_post_meta(
214 98 $post_id,
215 - 'activitypub_canonical_url',
99 + '_activitypub_canonical_url',
216 100 \get_permalink( $post_id ),
217 101 true
218 102 );
219 103 }
@@ -218,196 +102,269 @@
218 102 );
219 103 }
220 104
221 105 /**
222 - * Delete permalink from meta
106 + * Delete permalink from meta.
223 107 *
224 - * @param string $post_id The Post ID
225 - *
226 - * @return void
108 + * @param string $post_id The Post ID.
227 109 */
228 110 public static function untrash_post( $post_id ) {
229 - \delete_post_meta( $post_id, 'activitypub_canonical_url' );
111 + \delete_post_meta( $post_id, '_activitypub_canonical_url' );
230 112 }
231 113
232 114 /**
233 - * Add rewrite rules
115 + * Flush rewrite rules.
234 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 + */
235 127 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 - }
128 + _deprecated_function( __FUNCTION__, '7.5.0', '\Activitypub\Router::add_rewrite_rules()' );
241 129
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 );
130 + Router::add_rewrite_rules();
270 131 }
271 132
272 133 /**
273 - * Flush rewrite rules;
134 + * Theme compatibility stuff.
274 135 */
275 - public static function flush_rewrite_rules() {
276 - self::add_rewrite_rules();
277 - \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 + }
278 153 }
279 154
280 155 /**
281 - * Theme compatibility stuff
156 + * Add the 'activitypub' capability to users who can publish posts.
282 157 *
283 - * @return void
158 + * @param int $user_id User ID.
284 159 */
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 - );
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' );
297 164 }
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 165 }
311 166
312 167 /**
313 - * Display plugin upgrade notice to users
314 - *
315 - * @param array $data The plugin data
316 - *
317 - * @return void
168 + * Register user meta.
318 169 */
319 - public static function plugin_update_message( $data ) {
320 - if ( ! isset( $data['upgrade_notice'] ) ) {
321 - return;
322 - }
170 + public static function register_user_meta() {
171 + $blog_prefix = $GLOBALS['wpdb']->get_blog_prefix();
323 172
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 - )
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' ),
334 182 )
335 183 );
336 - }
337 184
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,
185 + \register_meta(
186 + 'user',
187 + $blog_prefix . 'activitypub_hide_social_graph',
346 188 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(),
189 + 'type' => 'integer',
190 + 'description' => 'Hide Followers and Followings on Profile.',
191 + 'single' => true,
192 + 'default' => 0,
193 + 'sanitize_callback' => 'absint',
358 194 )
359 195 );
360 196
361 - register_post_meta(
362 - Followers::POST_TYPE,
363 - 'activitypub_inbox',
197 + \register_meta(
198 + 'user',
199 + $blog_prefix . 'activitypub_old_host_data',
364 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(
365 210 'type' => 'string',
211 + 'description' => 'The new URL of the user.',
366 212 'single' => true,
367 213 'sanitize_callback' => 'sanitize_url',
368 214 )
369 215 );
370 216
371 - register_post_meta(
372 - Followers::POST_TYPE,
373 - 'activitypub_errors',
217 + \register_meta(
218 + 'user',
219 + $blog_prefix . 'activitypub_description',
374 220 array(
375 221 '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 - }
222 + 'description' => 'The user description.',
223 + 'single' => true,
224 + 'default' => '',
225 + 'sanitize_callback' => function ( $value ) {
226 + return wp_kses( $value, 'user_description' );
227 + },
228 + )
229 + );
381 230
382 - return esc_sql( $value );
383 - },
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',
384 240 )
385 241 );
386 242
387 - register_post_meta(
388 - Followers::POST_TYPE,
389 - 'activitypub_user_id',
243 + \register_meta(
244 + 'user',
245 + $blog_prefix . 'activitypub_header_image',
390 246 array(
391 - 'type' => 'string',
392 - 'single' => false,
393 - 'sanitize_callback' => function( $value ) {
394 - return esc_sql( $value );
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(),
336 + 'sanitize_callback' => function ( $value ) {
337 + return \array_unique( \array_map( array( Sanitize::class, 'host_list' ), $value ) );
395 338 },
396 339 )
397 340 );
398 341
399 - register_post_meta(
400 - Followers::POST_TYPE,
401 - 'activitypub_actor_json',
342 + \register_meta(
343 + 'user',
344 + 'activitypub_blocked_keywords',
402 345 array(
403 - 'type' => 'string',
346 + 'type' => 'array',
347 + 'description' => 'User-specific blocked ActivityPub keywords.',
404 348 'single' => true,
405 - 'sanitize_callback' => function( $value ) {
406 - return sanitize_text_field( $value );
349 + 'default' => array(),
350 + 'sanitize_callback' => function ( $value ) {
351 + return \array_map( 'sanitize_text_field', $value );
407 352 },
408 353 )
409 354 );
355 + }
410 356
411 - 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;
412 369 }
413 370 }