PluginProbe
ActivityPub / 7.8.2
ActivityPub v7.8.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 +283 -240 1.0.87.8.2 View file →
@@ -1,12 +1,19 @@
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 Activitypub\Collection\Followers;
11 +use Activitypub\Collection\Following;
12 +use Activitypub\Collection\Posts;
6 13
7 14 /**
8 - * ActivityPub Class
15 + * ActivityPub Class.
9 16 *
10 17 * @author Matthias Pfefferle
11 18 */
12 19 class Activitypub {
@@ -13,319 +20,355 @@
13 20 /**
14 21 * Initialize the class, registering WordPress hooks.
15 22 */
16 23 public static function init() {
17 - \add_filter( 'template_include', array( self::class, 'render_json_template' ), 99 );
18 - \add_filter( 'query_vars', array( self::class, 'add_query_vars' ) );
19 - \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 );
24 + \add_action( 'init', array( self::class, 'theme_compat' ), 11 );
25 + \add_action( 'init', array( self::class, 'register_user_meta' ), 11 );
21 26
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();
24 -
25 - foreach ( $post_types as $post_type ) {
26 - \add_post_type_support( $post_type, 'activitypub' );
27 - }
28 -
29 27 \add_action( 'wp_trash_post', array( self::class, 'trash_post' ), 1 );
30 28 \add_action( 'untrash_post', array( self::class, 'untrash_post' ), 1 );
31 29
32 - \add_action( 'init', array( self::class, 'add_rewrite_rules' ), 11 );
30 + \add_action( 'user_register', array( self::class, 'user_register' ) );
33 31
34 - \add_action( 'after_setup_theme', array( self::class, 'theme_compat' ), 99 );
35 -
36 - \add_action( 'in_plugin_update_message-' . ACTIVITYPUB_PLUGIN_BASENAME, array( self::class, 'plugin_update_message' ) );
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 );
37 34 }
38 35
39 36 /**
40 - * Activation Hook
37 + * Activation Hook.
41 38 *
42 - * @return void
39 + * @param bool $network_wide Whether to activate the plugin for all sites in the network or just the current site.
43 40 */
44 - public static function activate() {
41 + public static function activate( $network_wide ) {
45 42 self::flush_rewrite_rules();
43 + Scheduler::register_schedules();
46 44
47 - Scheduler::register_schedules();
45 + \add_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ), 10, 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 + }
48 56 }
49 57
50 58 /**
51 - * Deactivation Hook
59 + * Deactivation Hook.
52 60 *
53 - * @return void
61 + * @param bool $network_wide Whether to deactivate the plugin for all sites in the network or just the current site.
54 62 */
55 - public static function deactivate() {
63 + public static function deactivate( $network_wide ) {
56 64 self::flush_rewrite_rules();
65 + Scheduler::deregister_schedules();
57 66
58 - Scheduler::deregister_schedules();
67 + \remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ) );
68 + Migration::update_comment_counts( 2000 );
69 +
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();
76 + }
77 + }
59 78 }
60 79
61 80 /**
62 - * Uninstall Hook
63 - *
64 - * @return void
81 + * Uninstall Hook.
65 82 */
66 83 public static function uninstall() {
67 84 Scheduler::deregister_schedules();
85 +
86 + \remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ) );
87 + Migration::update_comment_counts( 2000 );
88 +
89 + Posts::delete_all();
90 +
91 + Options::delete();
68 92 }
69 93
70 94 /**
71 - * Return a AS2 JSON version of an author, post or page.
95 + * Store permalink in meta, to send delete Activity.
72 96 *
73 - * @param string $template The path to the template object.
97 + * @param string $post_id The Post ID.
98 + */
99 + public static function trash_post( $post_id ) {
100 + \add_post_meta(
101 + $post_id,
102 + '_activitypub_canonical_url',
103 + \get_permalink( $post_id ),
104 + true
105 + );
106 + }
107 +
108 + /**
109 + * Delete permalink from meta.
74 110 *
75 - * @return string The new path to the JSON template.
111 + * @param string $post_id The Post ID.
76 112 */
77 - public static function render_json_template( $template ) {
78 - if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
79 - return $template;
80 - }
81 -
82 - if ( ! is_activitypub_request() ) {
83 - return $template;
84 - }
85 -
86 - $json_template = false;
87 -
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';
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';
99 - }
100 -
101 - if ( ACTIVITYPUB_AUTHORIZED_FETCH ) {
102 - $verification = Signature::verify_http_signature( $_SERVER );
103 - if ( \is_wp_error( $verification ) ) {
104 - // fallback as template_loader can't return http headers
105 - return $template;
106 - }
107 - }
108 -
109 - return $json_template;
113 + public static function untrash_post( $post_id ) {
114 + \delete_post_meta( $post_id, '_activitypub_canonical_url' );
110 115 }
111 116
112 117 /**
113 - * Add the 'activitypub' query variable so WordPress won't mangle it.
118 + * Flush rewrite rules.
114 119 */
115 - public static function add_query_vars( $vars ) {
116 - $vars[] = 'activitypub';
117 -
118 - return $vars;
120 + public static function flush_rewrite_rules() {
121 + Router::add_rewrite_rules();
122 + \flush_rewrite_rules();
119 123 }
120 124
121 125 /**
122 - * Replaces the default avatar.
126 + * Add rewrite rules.
123 127 *
124 - * @param array $args Arguments passed to get_avatar_data(), after processing.
125 - * @param int|string|object $id_or_email A user ID, email address, or comment object.
126 - *
127 - * @return array $args
128 + * @deprecated 7.5.0 Use {@see Router::add_rewrite_rules()}.
128 129 */
129 - public static function pre_get_avatar_data( $args, $id_or_email ) {
130 - if (
131 - ! $id_or_email instanceof \WP_Comment ||
132 - ! isset( $id_or_email->comment_type ) ||
133 - $id_or_email->user_id
134 - ) {
135 - return $args;
136 - }
130 + public static function add_rewrite_rules() {
131 + _deprecated_function( __FUNCTION__, '7.5.0', '\Activitypub\Router::add_rewrite_rules()' );
137 132
138 - $allowed_comment_types = \apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
139 - if (
140 - ! empty( $id_or_email->comment_type ) &&
141 - ! \in_array(
142 - $id_or_email->comment_type,
143 - (array) $allowed_comment_types,
144 - true
145 - )
146 - ) {
147 - $args['url'] = false;
148 - /** This filter is documented in wp-includes/link-template.php */
149 - return \apply_filters( 'get_avatar_data', $args, $id_or_email );
150 - }
151 -
152 - // Check if comment has an avatar.
153 - $avatar = self::get_avatar_url( $id_or_email->comment_ID );
154 -
155 - 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'] );
161 - }
162 - $args['url'] = $avatar;
163 - $args['class'][] = 'avatar-activitypub';
164 - }
165 -
166 - return $args;
133 + Router::add_rewrite_rules();
167 134 }
168 135
169 136 /**
170 - * Function to retrieve Avatar URL if stored in meta.
171 - *
172 - * @param int|WP_Comment $comment
173 - *
174 - * @return string $url
137 + * Theme compatibility stuff.
175 138 */
176 - public static function get_avatar_url( $comment ) {
177 - if ( \is_numeric( $comment ) ) {
178 - $comment = \get_comment( $comment );
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 + }
179 155 }
180 - return \get_comment_meta( $comment->comment_ID, 'avatar_url', true );
181 156 }
182 157
183 158 /**
184 - * Link remote comments to source url.
159 + * Add the 'activitypub' capability to users who can publish posts.
185 160 *
186 - * @param string $comment_link
187 - * @param object|WP_Comment $comment
188 - *
189 - * @return string $url
161 + * @param int $user_id User ID.
190 162 */
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 );
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' );
195 167 }
196 - return $comment_link;
197 168 }
198 169
199 170 /**
200 - * Store permalink in meta, to send delete Activity.
201 - *
202 - * @param string $post_id The Post ID.
203 - *
204 - * @return void
171 + * Register user meta.
205 172 */
206 - public static function trash_post( $post_id ) {
207 - \add_post_meta(
208 - $post_id,
209 - 'activitypub_canonical_url',
210 - \get_permalink( $post_id ),
211 - true
173 + public static function register_user_meta() {
174 + $blog_prefix = $GLOBALS['wpdb']->get_blog_prefix();
175 +
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' ),
185 + )
212 186 );
213 - }
214 187
215 - /**
216 - * Delete permalink from meta
217 - *
218 - * @param string $post_id The Post ID
219 - *
220 - * @return void
221 - */
222 - public static function untrash_post( $post_id ) {
223 - \delete_post_meta( $post_id, 'activitypub_canonical_url' );
224 - }
188 + \register_meta(
189 + 'user',
190 + $blog_prefix . 'activitypub_hide_social_graph',
191 + 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,
198 + )
199 + );
225 200
226 - /**
227 - * Add rewrite rules
228 - */
229 - 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.
232 - if ( ACTIVITYPUB_DISABLE_REWRITES ) {
233 - return;
234 - }
201 + \register_meta(
202 + 'user',
203 + $blog_prefix . 'activitypub_old_host_data',
204 + array(
205 + 'description' => 'Actor object for the user on the old host.',
206 + 'single' => true,
207 + )
208 + );
235 209
236 - if ( ! \class_exists( 'Webfinger' ) ) {
237 - \add_rewrite_rule(
238 - '^.well-known/webfinger',
239 - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/webfinger',
240 - 'top'
241 - );
242 - }
210 + \register_meta(
211 + 'user',
212 + $blog_prefix . 'activitypub_moved_to',
213 + array(
214 + 'type' => 'string',
215 + 'description' => 'The new URL of the user.',
216 + 'single' => true,
217 + 'sanitize_callback' => 'sanitize_url',
218 + )
219 + );
243 220
244 - if ( ! \class_exists( 'Nodeinfo_Endpoint' ) && true === (bool) \get_option( 'blog_public', 1 ) ) {
245 - \add_rewrite_rule(
246 - '^.well-known/nodeinfo',
247 - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo/discovery',
248 - 'top'
249 - );
250 - \add_rewrite_rule(
251 - '^.well-known/x-nodeinfo2',
252 - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo2',
253 - 'top'
254 - );
255 - }
221 + \register_meta(
222 + 'user',
223 + $blog_prefix . 'activitypub_description',
224 + array(
225 + 'type' => 'string',
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 + );
256 234
257 - \add_rewrite_rule(
258 - '^@([\w\-\.]+)',
259 - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/users/$matches[1]',
260 - 'top'
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',
244 + )
261 245 );
262 246
263 - \add_rewrite_endpoint( 'activitypub', EP_AUTHORS | EP_PERMALINK | EP_PAGES );
264 - }
247 + \register_meta(
248 + 'user',
249 + $blog_prefix . 'activitypub_header_image',
250 + array(
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 + );
265 258
266 - /**
267 - * Flush rewrite rules;
268 - */
269 - public static function flush_rewrite_rules() {
270 - self::add_rewrite_rules();
271 - \flush_rewrite_rules();
272 - }
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' ) );
273 270
274 - /**
275 - * Theme compatibility stuff
276 - *
277 - * @return void
278 - */
279 - public static function theme_compat() {
280 - $site_icon = get_theme_support( 'custom-logo' );
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' ) );
281 282
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 - }
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' ) );
292 294
293 - $custom_header = get_theme_support( 'custom-header' );
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 + );
294 306
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 - }
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 ) );
342 + },
343 + )
344 + );
345 +
346 + \register_meta(
347 + 'user',
348 + 'activitypub_blocked_keywords',
349 + array(
350 + 'type' => 'array',
351 + 'description' => 'User-specific blocked ActivityPub keywords.',
352 + 'single' => true,
353 + 'default' => array(),
354 + 'sanitize_callback' => static function ( $value ) {
355 + return \array_map( 'sanitize_text_field', $value );
356 + },
357 + )
358 + );
304 359 }
305 360
306 361 /**
307 - * Display plugin upgrade notice to users
362 + * Set default values for user options.
308 363 *
309 - * @param array $data The plugin data
310 - *
311 - * @return void
364 + * @param bool|string $value Option value.
365 + * @return bool|string
312 366 */
313 - public static function plugin_update_message( $data ) {
314 - if ( ! isset( $data['upgrade_notice'] ) ) {
315 - return;
367 + public static function user_options_default( $value ) {
368 + if ( false === $value ) {
369 + return '1';
316 370 }
317 371
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 - )
328 - )
329 - );
372 + return $value;
330 373 }
331 374 }