PluginProbe
ActivityPub / 0.7.3
ActivityPub v0.7.3
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 +108 -338 8.3.00.7.3 View file →
@@ -1,400 +1,170 @@
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;
14 -
15 4 /**
16 - * ActivityPub Class.
5 + * ActivityPub Class
17 6 *
18 7 * @author Matthias Pfefferle
19 8 */
20 9 class Activitypub {
21 10 /**
22 - * Initialize the class, registering WordPress hooks.
11 + * Initialize the class, registering WordPress hooks
23 12 */
24 13 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 );
14 + add_filter( 'template_include', array( '\Activitypub\Activitypub', 'render_json_template' ), 99 );
15 + add_filter( 'query_vars', array( '\Activitypub\Activitypub', 'add_query_vars' ) );
16 + add_action( 'init', array( '\Activitypub\Activitypub', 'add_rewrite_endpoint' ) );
17 + add_filter( 'pre_get_avatar_data', array( '\Activitypub\Activitypub', 'pre_get_avatar_data' ), 11, 2 );
27 18
28 - \add_action( 'wp_trash_post', array( self::class, 'trash_post' ), 1 );
29 - \add_action( 'untrash_post', array( self::class, 'untrash_post' ), 1 );
19 + add_post_type_support( 'post', 'activitypub' );
20 + add_post_type_support( 'page', 'activitypub' );
30 21
31 - \add_action( 'user_register', array( self::class, 'user_register' ) );
32 -
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 );
22 + add_action( 'transition_post_status', array( '\Activitypub\Activitypub', 'schedule_post_activity' ), 10, 3 );
35 23 }
36 24
37 25 /**
38 - * Activation Hook.
26 + * Return a AS2 JSON version of an author, post or page
39 27 *
40 - * @param bool $network_wide Whether to activate the plugin for all sites in the network or just the current site.
28 + * @param string $template the path to the template object
29 + *
30 + * @return string the new path to the JSON template
41 31 */
42 - public static function activate( $network_wide ) {
43 - self::flush_rewrite_rules();
44 - Scheduler::register_schedules();
32 + public static function render_json_template( $template ) {
33 + if ( ! is_author() && ! is_singular() ) {
34 + return $template;
35 + }
45 36
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();
48 -
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 - }
37 + if ( is_author() ) {
38 + $json_template = dirname( __FILE__ ) . '/../templates/json-author.php';
39 + } elseif ( is_singular() ) {
40 + $json_template = dirname( __FILE__ ) . '/../templates/json-post.php';
56 41 }
57 - }
58 42
59 - /**
60 - * Deactivation Hook.
61 - *
62 - * @param bool $network_wide Whether to deactivate the plugin for all sites in the network or just the current site.
63 - */
64 - public static function deactivate( $network_wide ) {
65 - self::flush_rewrite_rules();
66 - Scheduler::deregister_schedules();
43 + global $wp_query;
67 44
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 );
45 + if ( isset( $wp_query->query_vars['activitypub'] ) ) {
46 + return $json_template;
47 + }
70 48
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();
77 - }
49 + if ( ! isset( $_SERVER['HTTP_ACCEPT'] ) ) {
50 + return $template;
78 51 }
79 - }
80 52
81 - /**
82 - * Uninstall Hook.
83 - */
84 - public static function uninstall() {
85 - Scheduler::deregister_schedules();
53 + // interpret accept header
54 + $pos = stripos( $_SERVER['HTTP_ACCEPT'], ';' );
55 + if ( $pos ) {
56 + $accept_header = substr( $_SERVER['HTTP_ACCEPT'], 0, $pos );
57 + } else {
58 + $accept_header = $_SERVER['HTTP_ACCEPT'];
59 + }
60 + // accept header as an array
61 + $accept = explode( ',', trim( $accept_header ) );
86 62
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 );
63 + if (
64 + ! in_array( 'application/activity+json', $accept, true ) &&
65 + ! in_array( 'application/ld+json', $accept, true ) &&
66 + ! in_array( 'application/json', $accept, true )
67 + ) {
68 + return $template;
69 + }
89 70
90 - Remote_Posts::delete_all();
91 - Tombstone::delete_all();
92 - Client::delete_all();
93 -
94 - Options::delete();
71 + return $json_template;
95 72 }
96 73
97 74 /**
98 - * Store permalink in meta, to send delete Activity.
99 - *
100 - * @param string $post_id The Post ID.
75 + * Add the 'photos' query variable so WordPress
76 + * won't mangle it.
101 77 */
102 - public static function trash_post( $post_id ) {
103 - \add_post_meta(
104 - $post_id,
105 - '_activitypub_canonical_url',
106 - \get_permalink( $post_id ),
107 - true
108 - );
109 - }
78 + public static function add_query_vars( $vars ) {
79 + $vars[] = 'activitypub';
110 80
111 - /**
112 - * Delete permalink from meta.
113 - *
114 - * @param string $post_id The Post ID.
115 - */
116 - public static function untrash_post( $post_id ) {
117 - \delete_post_meta( $post_id, '_activitypub_canonical_url' );
81 + return $vars;
118 82 }
119 83
120 84 /**
121 - * Flush rewrite rules.
85 + * Add our rewrite endpoint to permalinks and pages.
122 86 */
123 - public static function flush_rewrite_rules() {
124 - Router::add_rewrite_rules();
125 - \flush_rewrite_rules();
87 + public static function add_rewrite_endpoint() {
88 + add_rewrite_endpoint( 'activitypub', EP_AUTHORS | EP_PERMALINK | EP_PAGES );
126 89 }
127 90
128 91 /**
129 - * Add rewrite rules.
92 + * Marks the post as "no webmentions sent yet"
130 93 *
131 - * @deprecated 7.5.0 Use {@see Router::add_rewrite_rules()}.
94 + * @param int $post_id
132 95 */
133 - public static function add_rewrite_rules() {
134 - _deprecated_function( __FUNCTION__, '7.5.0', '\Activitypub\Router::add_rewrite_rules()' );
96 + public static function schedule_post_activity( $new_status, $old_status, $post ) {
97 + // do not send activities if post is password protected
98 + if ( post_password_required( $post ) ) {
99 + return;
100 + }
135 101
136 - Router::add_rewrite_rules();
137 - }
102 + // check if post-type supports ActivityPub
103 + $post_types = get_post_types_by_support( 'activitypub' );
104 + if ( ! in_array( $post->post_type, $post_types, true ) ) {
105 + return;
106 + }
138 107
139 - /**
140 - * Theme compatibility stuff.
141 - */
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 - }
108 + if ( 'publish' === $new_status && 'publish' !== $old_status ) {
109 + wp_schedule_single_event( time() + wp_rand( 0, 120 ), 'activitypub_send_post_activity', array( $post->ID ) );
110 + } elseif ( 'publish' === $new_status ) {
111 + wp_schedule_single_event( time() + wp_rand( 0, 120 ), 'activitypub_send_update_activity', array( $post->ID ) );
158 112 }
159 113 }
160 114
161 115 /**
162 - * Add the 'activitypub' capability to users who can publish posts.
116 + * Replaces the default avatar
163 117 *
164 - * @param int $user_id User ID.
118 + * @param array $args Arguments passed to get_avatar_data(), after processing.
119 + * @param int|string|object $id_or_email A user ID, email address, or comment object
120 + *
121 + * @return array $args
165 122 */
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' );
123 + public static function pre_get_avatar_data( $args, $id_or_email ) {
124 + if (
125 + ! $id_or_email instanceof \WP_Comment ||
126 + ! isset( $id_or_email->comment_type ) ||
127 + $id_or_email->user_id
128 + ) {
129 + return $args;
170 130 }
171 - }
172 131
173 - /**
174 - * Register user meta.
175 - */
176 - public static function register_user_meta() {
177 - $blog_prefix = $GLOBALS['wpdb']->get_blog_prefix();
132 + $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
133 + if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types, true ) ) {
134 + $args['url'] = false;
135 + /** This filter is documented in wp-includes/link-template.php */
136 + return apply_filters( 'get_avatar_data', $args, $id_or_email );
137 + }
178 138
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 - );
139 + // check if comment has an avatar
140 + $avatar = self::get_avatar_url( $id_or_email->comment_ID );
190 141
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 - );
142 + if ( $avatar ) {
143 + if ( ! isset( $args['class'] ) || ! is_array( $args['class'] ) ) {
144 + $args['class'] = array( 'u-photo' );
145 + } else {
146 + $args['class'][] = 'u-photo';
147 + $args['class'] = array_unique( $args['class'] );
148 + }
149 + $args['url'] = $avatar;
150 + $args['class'][] = 'avatar-activitypub';
151 + }
203 152
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 - );
153 + return $args;
385 154 }
386 155
387 156 /**
388 - * Set default values for user options.
157 + * Function to retrieve Avatar URL if stored in meta
389 158 *
390 - * @param bool|string $value Option value.
391 - * @return bool|string
159 + *
160 + * @param int|WP_Comment $comment
161 + *
162 + * @return string $url
392 163 */
393 - public static function user_options_default( $value ) {
394 - if ( false === $value ) {
395 - return '1';
164 + public static function get_avatar_url( $comment ) {
165 + if ( is_numeric( $comment ) ) {
166 + $comment = get_comment( $comment );
396 167 }
397 -
398 - return $value;
168 + return get_comment_meta( $comment->comment_ID, 'avatar_url', true );
399 169 }
400 170 }