PluginProbe
ActivityPub / 0.4.3
ActivityPub v0.4.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 +41 -433 2.0.10.4.3 View file →
@@ -1,16 +1,5 @@
1 1 <?php
2 -namespace Activitypub;
3 -
4 -use Exception;
5 -use Activitypub\Signature;
6 -use Activitypub\Collection\Users;
7 -use Activitypub\Collection\Followers;
8 -
9 -use function Activitypub\sanitize_url;
10 -use function Activitypub\is_comment;
11 -use function Activitypub\is_activitypub_request;
12 -
13 2 /**
14 3 * ActivityPub Class
15 4 *
16 5 * @author Matthias Pfefferle
@@ -16,107 +5,51 @@
16 5 * @author Matthias Pfefferle
17 6 */
18 7 class Activitypub {
19 8 /**
20 - * Initialize the class, registering WordPress hooks.
21 - */
22 - public static function init() {
23 - \add_filter( 'template_include', array( self::class, 'render_json_template' ), 99 );
24 - \add_action( 'template_redirect', array( self::class, 'template_redirect' ) );
25 - \add_filter( 'query_vars', array( self::class, 'add_query_vars' ) );
26 - \add_filter( 'pre_get_avatar_data', array( self::class, 'pre_get_avatar_data' ), 11, 2 );
27 - \add_filter( 'get_comment_link', array( self::class, 'remote_comment_link' ), 11, 3 );
28 -
29 - // Add support for ActivityPub to custom post types
30 - $post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ) ? \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ) : array();
31 -
32 - foreach ( $post_types as $post_type ) {
33 - \add_post_type_support( $post_type, 'activitypub' );
34 - }
35 -
36 - \add_action( 'wp_trash_post', array( self::class, 'trash_post' ), 1 );
37 - \add_action( 'untrash_post', array( self::class, 'untrash_post' ), 1 );
38 -
39 - \add_action( 'init', array( self::class, 'add_rewrite_rules' ), 11 );
40 -
41 - \add_action( 'after_setup_theme', array( self::class, 'theme_compat' ), 99 );
42 -
43 - \add_action( 'in_plugin_update_message-' . ACTIVITYPUB_PLUGIN_BASENAME, array( self::class, 'plugin_update_message' ) );
44 -
45 - \add_filter( 'comment_class', array( self::class, 'comment_class' ), 10, 3 );
46 -
47 - // register several post_types
48 - self::register_post_types();
49 - }
50 -
51 - /**
52 - * Activation Hook
9 + * Return a AS2 JSON version of an author, post or page
53 10 *
54 - * @return void
55 - */
56 - public static function activate() {
57 - self::flush_rewrite_rules();
58 - Scheduler::register_schedules();
59 - }
60 -
61 - /**
62 - * Deactivation Hook
11 + * @param string $template the path to the template object
63 12 *
64 - * @return void
13 + * @return string the new path to the JSON template
65 14 */
66 - public static function deactivate() {
67 - self::flush_rewrite_rules();
68 - Scheduler::deregister_schedules();
69 - }
70 -
71 - /**
72 - * Uninstall Hook
73 - *
74 - * @return void
75 - */
76 - public static function uninstall() {
77 - Scheduler::deregister_schedules();
78 - }
79 -
80 - /**
81 - * Return a AS2 JSON version of an author, post or page.
82 - *
83 - * @param string $template The path to the template object.
84 - *
85 - * @return string The new path to the JSON template.
86 - */
87 15 public static function render_json_template( $template ) {
88 - if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
16 + if ( ! is_author() && ! is_singular() ) {
89 17 return $template;
90 18 }
91 19
92 - if ( ! is_activitypub_request() ) {
93 - return $template;
20 + if ( is_author() ) {
21 + $json_template = dirname( __FILE__ ) . '/../templates/json-author.php';
22 + } elseif ( is_singular() ) {
23 + $json_template = dirname( __FILE__ ) . '/../templates/json-post.php';
94 24 }
95 25
96 - $json_template = false;
26 + global $wp_query;
97 27
98 - // check if user can publish posts
99 - if ( \is_author() && is_wp_error( Users::get_by_id( \get_the_author_meta( 'ID' ) ) ) ) {
28 + if ( isset( $wp_query->query_vars['as2'] ) ) {
29 + return $json_template;
30 + }
31 +
32 + if ( ! isset( $_SERVER['HTTP_ACCEPT'] ) ) {
100 33 return $template;
101 34 }
102 35
103 - if ( \is_author() ) {
104 - $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/author-json.php';
105 - } elseif ( is_comment() ) {
106 - $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/comment-json.php';
107 - } elseif ( \is_singular() ) {
108 - $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/post-json.php';
109 - } elseif ( \is_home() ) {
110 - $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/blog-json.php';
36 + // interpret accept header
37 + $pos = stripos( $_SERVER['HTTP_ACCEPT'], ';' );
38 + if ( $pos ) {
39 + $accept_header = substr( $_SERVER['HTTP_ACCEPT'], 0, $pos );
40 + } else {
41 + $accept_header = $_SERVER['HTTP_ACCEPT'];
111 42 }
43 + // accept header as an array
44 + $accept = explode( ',', trim( $accept_header ) );
112 45
113 - if ( ACTIVITYPUB_AUTHORIZED_FETCH ) {
114 - $verification = Signature::verify_http_signature( $_SERVER );
115 - if ( \is_wp_error( $verification ) ) {
116 - // fallback as template_loader can't return http headers
117 - return $template;
118 - }
46 + if (
47 + ! in_array( 'application/activity+json', $accept, true ) &&
48 + ! in_array( 'application/ld+json', $accept, true ) &&
49 + ! in_array( 'application/json', $accept, true )
50 + ) {
51 + return $template;
119 52 }
120 53
121 54 return $json_template;
122 55 }
@@ -121,358 +54,33 @@
121 54 return $json_template;
122 55 }
123 56
124 57 /**
125 - * Custom redirects for ActivityPub requests.
126 - *
127 - * @return void
58 + * Add the 'photos' query variable so WordPress
59 + * won't mangle it.
128 60 */
129 - public static function template_redirect() {
130 - $comment_id = get_query_var( 'c', null );
131 -
132 - // check if it seems to be a comment
133 - if ( ! $comment_id ) {
134 - return;
135 - }
136 -
137 - $comment = get_comment( $comment_id );
138 -
139 - // load a 404 page if `c` is set but not valid
140 - if ( ! $comment ) {
141 - global $wp_query;
142 - $wp_query->set_404();
143 - return;
144 - }
145 -
146 - // stop if it's not an ActivityPub comment
147 - if ( is_activitypub_request() && $comment->user_id ) {
148 - return;
149 - }
150 -
151 - wp_safe_redirect( get_comment_link( $comment ) );
152 - exit;
153 - }
154 -
155 - /**
156 - * Add the 'activitypub' query variable so WordPress won't mangle it.
157 - */
158 61 public static function add_query_vars( $vars ) {
159 - $vars[] = 'activitypub';
160 - $vars[] = 'c';
161 - $vars[] = 'p';
62 + $vars[] = 'as2';
162 63
163 64 return $vars;
164 65 }
165 66
166 67 /**
167 - * Replaces the default avatar.
168 - *
169 - * @param array $args Arguments passed to get_avatar_data(), after processing.
170 - * @param int|string|object $id_or_email A user ID, email address, or comment object.
171 - *
172 - * @return array $args
68 + * Add our rewrite endpoint to permalinks and pages.
173 69 */
174 - public static function pre_get_avatar_data( $args, $id_or_email ) {
175 - if (
176 - ! $id_or_email instanceof \WP_Comment ||
177 - ! isset( $id_or_email->comment_type ) ||
178 - $id_or_email->user_id
179 - ) {
180 - return $args;
181 - }
182 -
183 - $allowed_comment_types = \apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
184 - if (
185 - ! empty( $id_or_email->comment_type ) &&
186 - ! \in_array(
187 - $id_or_email->comment_type,
188 - (array) $allowed_comment_types,
189 - true
190 - )
191 - ) {
192 - $args['url'] = false;
193 - /** This filter is documented in wp-includes/link-template.php */
194 - return \apply_filters( 'get_avatar_data', $args, $id_or_email );
195 - }
196 -
197 - // Check if comment has an avatar.
198 - $avatar = self::get_avatar_url( $id_or_email->comment_ID );
199 -
200 - if ( $avatar ) {
201 - if ( ! isset( $args['class'] ) || ! \is_array( $args['class'] ) ) {
202 - $args['class'] = array( 'u-photo' );
203 - } else {
204 - $args['class'][] = 'u-photo';
205 - $args['class'] = \array_unique( $args['class'] );
206 - }
207 - $args['url'] = $avatar;
208 - $args['class'][] = 'avatar-activitypub';
209 - }
210 -
211 - return $args;
70 + public static function add_rewrite_endpoint() {
71 + add_rewrite_endpoint( 'as2', EP_AUTHORS | EP_PERMALINK | EP_PAGES );
212 72 }
213 73
214 74 /**
215 - * Function to retrieve Avatar URL if stored in meta.
75 + * Marks the post as "no webmentions sent yet"
216 76 *
217 - * @param int|WP_Comment $comment
218 - *
219 - * @return string $url
77 + * @param int $post_id
220 78 */
221 - public static function get_avatar_url( $comment ) {
222 - if ( \is_numeric( $comment ) ) {
223 - $comment = \get_comment( $comment );
79 + public static function schedule_post_activity( $new_status, $old_status, $post ) {
80 + if ( 'publish' === $new_status && 'publish' !== $old_status ) {
81 + wp_schedule_single_event( time() + wp_rand( 0, 120 ), 'activitypub_send_post_activity', array( $post->ID ) );
82 + } elseif ( 'publish' === $new_status ) {
83 + wp_schedule_single_event( time() + wp_rand( 0, 120 ), 'activitypub_send_update_activity', array( $post->ID ) );
224 84 }
225 - return \get_comment_meta( $comment->comment_ID, 'avatar_url', true );
226 - }
227 -
228 - /**
229 - * Link remote comments to source url.
230 - *
231 - * @param string $comment_link
232 - * @param object|WP_Comment $comment
233 - *
234 - * @return string $url
235 - */
236 - public static function remote_comment_link( $comment_link, $comment ) {
237 - if ( ! $comment || is_admin() ) {
238 - return $comment_link;
239 - }
240 -
241 - $comment_meta = \get_comment_meta( $comment->comment_ID );
242 -
243 - if ( ! empty( $comment_meta['source_url'][0] ) ) {
244 - return $comment_meta['source_url'][0];
245 - } elseif ( ! empty( $comment_meta['source_id'][0] ) ) {
246 - return $comment_meta['source_id'][0];
247 - }
248 -
249 - return $comment_link;
250 - }
251 -
252 - /**
253 - * Store permalink in meta, to send delete Activity.
254 - *
255 - * @param string $post_id The Post ID.
256 - *
257 - * @return void
258 - */
259 - public static function trash_post( $post_id ) {
260 - \add_post_meta(
261 - $post_id,
262 - 'activitypub_canonical_url',
263 - \get_permalink( $post_id ),
264 - true
265 - );
266 - }
267 -
268 - /**
269 - * Delete permalink from meta
270 - *
271 - * @param string $post_id The Post ID
272 - *
273 - * @return void
274 - */
275 - public static function untrash_post( $post_id ) {
276 - \delete_post_meta( $post_id, 'activitypub_canonical_url' );
277 - }
278 -
279 - /**
280 - * Add rewrite rules
281 - */
282 - public static function add_rewrite_rules() {
283 - // If another system needs to take precedence over the ActivityPub rewrite rules,
284 - // they can define their own and will manually call the appropriate functions as required.
285 - if ( ACTIVITYPUB_DISABLE_REWRITES ) {
286 - return;
287 - }
288 -
289 - if ( ! \class_exists( 'Webfinger' ) ) {
290 - \add_rewrite_rule(
291 - '^.well-known/webfinger',
292 - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/webfinger',
293 - 'top'
294 - );
295 - }
296 -
297 - if ( ! \class_exists( 'Nodeinfo_Endpoint' ) && true === (bool) \get_option( 'blog_public', 1 ) ) {
298 - \add_rewrite_rule(
299 - '^.well-known/nodeinfo',
300 - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo/discovery',
301 - 'top'
302 - );
303 - \add_rewrite_rule(
304 - '^.well-known/x-nodeinfo2',
305 - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo2',
306 - 'top'
307 - );
308 - }
309 -
310 - \add_rewrite_rule(
311 - '^@([\w\-\.]+)',
312 - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/users/$matches[1]',
313 - 'top'
314 - );
315 -
316 - \add_rewrite_endpoint( 'activitypub', EP_AUTHORS | EP_PERMALINK | EP_PAGES );
317 - }
318 -
319 - /**
320 - * Flush rewrite rules;
321 - */
322 - public static function flush_rewrite_rules() {
323 - self::add_rewrite_rules();
324 - \flush_rewrite_rules();
325 - }
326 -
327 - /**
328 - * Theme compatibility stuff
329 - *
330 - * @return void
331 - */
332 - public static function theme_compat() {
333 - $site_icon = get_theme_support( 'custom-logo' );
334 -
335 - if ( ! $site_icon ) {
336 - // custom logo support
337 - add_theme_support(
338 - 'custom-logo',
339 - array(
340 - 'height' => 80,
341 - 'width' => 80,
342 - )
343 - );
344 - }
345 -
346 - $custom_header = get_theme_support( 'custom-header' );
347 -
348 - if ( ! $custom_header ) {
349 - // This theme supports a custom header
350 - $custom_header_args = array(
351 - 'width' => 1250,
352 - 'height' => 600,
353 - 'header-text' => true,
354 - );
355 - add_theme_support( 'custom-header', $custom_header_args );
356 - }
357 - }
358 -
359 - /**
360 - * Display plugin upgrade notice to users
361 - *
362 - * @param array $data The plugin data
363 - *
364 - * @return void
365 - */
366 - public static function plugin_update_message( $data ) {
367 - if ( ! isset( $data['upgrade_notice'] ) ) {
368 - return;
369 - }
370 -
371 - printf(
372 - '<div class="update-message">%s</div>',
373 - wp_kses(
374 - wpautop( $data['upgrade_notice '] ),
375 - array(
376 - 'p' => array(),
377 - 'a' => array( 'href', 'title' ),
378 - 'strong' => array(),
379 - 'em' => array(),
380 - )
381 - )
382 - );
383 - }
384 -
385 - /**
386 - * Register the "Followers" Taxonomy
387 - *
388 - * @return void
389 - */
390 - private static function register_post_types() {
391 - \register_post_type(
392 - Followers::POST_TYPE,
393 - array(
394 - 'labels' => array(
395 - 'name' => _x( 'Followers', 'post_type plural name', 'activitypub' ),
396 - 'singular_name' => _x( 'Follower', 'post_type single name', 'activitypub' ),
397 - ),
398 - 'public' => false,
399 - 'hierarchical' => false,
400 - 'rewrite' => false,
401 - 'query_var' => false,
402 - 'delete_with_user' => false,
403 - 'can_export' => true,
404 - 'supports' => array(),
405 - )
406 - );
407 -
408 - \register_post_meta(
409 - Followers::POST_TYPE,
410 - 'activitypub_inbox',
411 - array(
412 - 'type' => 'string',
413 - 'single' => true,
414 - 'sanitize_callback' => 'sanitize_url',
415 - )
416 - );
417 -
418 - \register_post_meta(
419 - Followers::POST_TYPE,
420 - 'activitypub_errors',
421 - array(
422 - 'type' => 'string',
423 - 'single' => false,
424 - 'sanitize_callback' => function ( $value ) {
425 - if ( ! is_string( $value ) ) {
426 - throw new Exception( 'Error message is no valid string' );
427 - }
428 -
429 - return esc_sql( $value );
430 - },
431 - )
432 - );
433 -
434 - \register_post_meta(
435 - Followers::POST_TYPE,
436 - 'activitypub_user_id',
437 - array(
438 - 'type' => 'string',
439 - 'single' => false,
440 - 'sanitize_callback' => function ( $value ) {
441 - return esc_sql( $value );
442 - },
443 - )
444 - );
445 -
446 - \register_post_meta(
447 - Followers::POST_TYPE,
448 - 'activitypub_actor_json',
449 - array(
450 - 'type' => 'string',
451 - 'single' => true,
452 - 'sanitize_callback' => function ( $value ) {
453 - return sanitize_text_field( $value );
454 - },
455 - )
456 - );
457 -
458 - \do_action( 'activitypub_after_register_post_type' );
459 - }
460 -
461 - /**
462 - * Filters the CSS classes to add an ActivityPub class.
463 - *
464 - * @param string[] $classes An array of comment classes.
465 - * @param string[] $css_class An array of additional classes added to the list.
466 - * @param string $comment_id The comment ID as a numeric string.
467 - *
468 - * @return string[] An array of classes.
469 - */
470 - public static function comment_class( $classes, $css_class, $comment_id ) {
471 - // check if ActivityPub comment
472 - if ( 'activitypub' === get_comment_meta( $comment_id, 'protocol', true ) ) {
473 - $classes[] = 'activitypub-comment';
474 - }
475 -
476 - return $classes;
477 85 }
478 86 }