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