wordpress-activitypub/includes/class-activitypub.php

300 lines
10 KiB
PHP
Raw Normal View History

2018-08-18 12:35:39 +02:00
<?php
namespace Activitypub;
2018-09-30 22:51:22 +02:00
/**
* ActivityPub Class
*
* @author Matthias Pfefferle
*/
2018-09-05 22:03:57 +02:00
class Activitypub {
2019-02-24 13:01:28 +01:00
/**
2020-05-14 21:04:33 +02:00
* Initialize the class, registering WordPress hooks.
2019-02-24 13:01:28 +01:00
*/
public static function init() {
2019-09-27 10:12:59 +02:00
\add_filter( 'template_include', array( '\Activitypub\Activitypub', 'render_json_template' ), 99 );
\add_filter( 'query_vars', array( '\Activitypub\Activitypub', 'add_query_vars' ) );
\add_action( 'init', array( '\Activitypub\Activitypub', 'add_rewrite_endpoint' ) );
\add_filter( 'pre_get_avatar_data', array( '\Activitypub\Activitypub', 'pre_get_avatar_data' ), 11, 2 );
2019-02-24 12:27:26 +01:00
// Add support for ActivityPub to custom post types
$post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ) ? \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ) : array();
foreach ( $post_types as $post_type ) {
\add_post_type_support( $post_type, 'activitypub' );
}
2019-02-24 12:27:26 +01:00
2019-09-27 10:12:59 +02:00
\add_action( 'transition_post_status', array( '\Activitypub\Activitypub', 'schedule_post_activity' ), 10, 3 );
2021-02-18 07:12:32 +01:00
2022-04-15 09:17:00 +02:00
\add_filter( 'preprocess_comment', array( '\Activitypub\Activitypub', 'preprocess_comment' ) );
\add_filter( 'comment_post', array( '\Activitypub\Activitypub', 'postprocess_comment' ), 10, 3 );
2022-04-15 03:04:43 +02:00
\add_filter( 'wp_update_comment_data', array( '\Activitypub\Activitypub', 'comment_updated_published' ), 20, 3 );
2021-02-18 07:12:32 +01:00
\add_action( 'transition_comment_status', array( '\Activitypub\Activitypub', 'schedule_comment_activity' ), 20, 3 );
2022-04-15 03:04:43 +02:00
\add_action( 'edit_comment', array( '\Activitypub\Activitypub', 'edit_comment' ), 20, 2 );//schedule_admin_comment_activity
\add_filter( 'get_comment_text', array( '\Activitypub\Activitypub', 'comment_append_edit_datetime' ), 10, 3 );
2021-02-18 07:12:32 +01:00
}
2018-09-30 22:51:22 +02:00
/**
2020-05-14 21:04:33 +02:00
* Return a AS2 JSON version of an author, post or page.
2018-09-30 22:51:22 +02:00
*
2020-05-14 21:04:33 +02:00
* @param string $template The path to the template object.
2018-09-30 22:51:22 +02:00
*
2020-05-14 21:04:33 +02:00
* @return string The new path to the JSON template.
2018-09-30 22:51:22 +02:00
*/
public static function render_json_template( $template ) {
if ( ! \is_author() && ! \is_singular() && ! \is_home() && ! \Activitypub\is_ap_comment() && ! \Activitypub\is_ap_replies() ) {
2018-08-18 12:35:39 +02:00
return $template;
}
2019-09-27 10:12:59 +02:00
if ( \is_author() ) {
$json_template = \dirname( __FILE__ ) . '/../templates/author-json.php';
2022-04-15 03:04:43 +02:00
} elseif ( \Activitypub\is_ap_replies() ) {
$json_template = \dirname( __FILE__ ) . '/../templates/replies-json.php';
} elseif ( \Activitypub\is_ap_comment() ) {
$json_template = \dirname( __FILE__ ) . '/../templates/comment-json.php';
2019-09-27 10:12:59 +02:00
} elseif ( \is_singular() ) {
$json_template = \dirname( __FILE__ ) . '/../templates/post-json.php';
2021-01-08 17:43:35 +01:00
} elseif ( \is_home() ) {
$json_template = \dirname( __FILE__ ) . '/../templates/blog-json.php';
}
2018-08-18 12:35:39 +02:00
global $wp_query;
2019-07-26 17:06:53 +02:00
if ( isset( $wp_query->query_vars['activitypub'] ) ) {
2018-08-18 12:35:39 +02:00
return $json_template;
}
if ( ! isset( $_SERVER['HTTP_ACCEPT'] ) ) {
return $template;
}
2019-09-29 18:22:59 +02:00
$accept_header = $_SERVER['HTTP_ACCEPT'];
if (
\stristr( $accept_header, 'application/activity+json' ) ||
\stristr( $accept_header, 'application/ld+json' )
2019-09-29 18:22:59 +02:00
) {
2019-09-30 07:59:19 +02:00
return $json_template;
2018-08-18 12:35:39 +02:00
}
2019-09-29 18:22:59 +02:00
2020-05-14 21:04:33 +02:00
// Accept header as an array.
$accept = \explode( ',', \trim( $accept_header ) );
2018-08-18 12:35:39 +02:00
if (
2019-09-30 07:59:19 +02:00
\in_array( 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"', $accept, true ) ||
\in_array( 'application/activity+json', $accept, true ) ||
\in_array( 'application/ld+json', $accept, true ) ||
\in_array( 'application/json', $accept, true )
2018-08-18 12:35:39 +02:00
) {
2019-09-30 07:59:19 +02:00
return $json_template;
2018-08-18 12:35:39 +02:00
}
2019-09-30 07:59:19 +02:00
return $template;
2018-08-18 12:35:39 +02:00
}
/**
2020-05-14 21:04:33 +02:00
* Add the 'activitypub' query variable so WordPress won't mangle it.
2018-08-18 12:35:39 +02:00
*/
public static function add_query_vars( $vars ) {
2019-07-26 17:06:53 +02:00
$vars[] = 'activitypub';
2022-04-15 03:04:43 +02:00
$vars[] = 'ap_comment_id';//comment_id doesn't work, 'c' is probably too short and prone to collisions
//Collections review
$vars[] = 'replies';
$vars[] = 'collection_page';
$vars[] = 'only_other_accounts';
2018-09-24 20:47:15 +02:00
2018-08-18 12:35:39 +02:00
return $vars;
}
/**
* Add our rewrite endpoint to permalinks and pages.
*/
public static function add_rewrite_endpoint() {
2019-09-27 10:12:59 +02:00
\add_rewrite_endpoint( 'activitypub', EP_AUTHORS | EP_PERMALINK | EP_PAGES );
2018-08-18 12:35:39 +02:00
}
/**
2020-05-14 21:04:33 +02:00
* Schedule Activities.
*
2020-05-14 21:04:33 +02:00
* @param string $new_status New post status.
* @param string $old_status Old post status.
* @param WP_Post $post Post object.
*/
2019-02-17 21:10:53 +01:00
public static function schedule_post_activity( $new_status, $old_status, $post ) {
2020-05-14 21:04:33 +02:00
// Do not send activities if post is password protected.
2019-09-27 10:12:59 +02:00
if ( \post_password_required( $post ) ) {
return;
}
2020-05-14 21:04:33 +02:00
// Check if post-type supports ActivityPub.
2019-09-27 10:12:59 +02:00
$post_types = \get_post_types_by_support( 'activitypub' );
if ( ! \in_array( $post->post_type, $post_types, true ) ) {
return;
}
2020-05-14 21:37:59 +02:00
$activitypub_post = new \Activitypub\Model\Post( $post );
2019-02-17 21:10:53 +01:00
if ( 'publish' === $new_status && 'publish' !== $old_status ) {
2020-05-14 21:37:59 +02:00
\wp_schedule_single_event( \time(), 'activitypub_send_post_activity', array( $activitypub_post ) );
2022-04-15 03:04:43 +02:00
} elseif ( 'publish' === $new_status ) { //this triggers when restored post from trash, which may not be desired
2020-05-14 21:37:59 +02:00
\wp_schedule_single_event( \time(), 'activitypub_send_update_activity', array( $activitypub_post ) );
2019-09-30 07:59:19 +02:00
} elseif ( 'trash' === $new_status ) {
2020-05-14 21:37:59 +02:00
\wp_schedule_single_event( \time(), 'activitypub_send_delete_activity', array( $activitypub_post ) );
2019-02-17 21:10:53 +01:00
}
}
2019-02-20 21:05:29 +01:00
2022-04-15 03:04:43 +02:00
/**
2021-02-18 07:12:32 +01:00
* preprocess local comments for federated replies
*/
public static function preprocess_comment( $commentdata ) {
2022-04-15 03:04:43 +02:00
// only process replies from local actors
2022-04-15 09:17:00 +02:00
if ( ! empty( $commentdata['user_id'] ) ) {
2022-04-15 03:04:43 +02:00
$commentdata['comment_type'] = 'activitypub';
// transform webfinger mentions to links and add @mentions to cc
$tagged_content = \Activitypub\transform_tags( $commentdata['comment_content'] );
$commentdata['comment_content'] = $tagged_content['content'];
$commentdata['comment_meta']['mentions'] = $tagged_content['mentions'];
2021-02-18 07:12:32 +01:00
}
2022-04-15 03:04:43 +02:00
return $commentdata;
}
2021-02-18 07:12:32 +01:00
/**
2022-04-15 03:04:43 +02:00
* comment_post()
2021-02-18 07:12:32 +01:00
* postprocess_comment for federating replies and inbox-forwarding
*/
public static function postprocess_comment( $comment_id, $comment_approved, $commentdata ) {
//Admin users comments bypass transition_comment_status (auto approved)
2022-09-28 19:18:30 +02:00
$user = \get_userdata( $commentdata['user_id'] );
if ( 'activitypub' === $commentdata['comment_type'] ) {
if (
( 1 === $comment_approved ) &&
2021-02-18 07:12:32 +01:00
! empty( $commentdata['user_id'] ) &&
2022-09-28 19:18:30 +02:00
\in_array( 'administrator', $user->roles )
2022-04-15 09:17:00 +02:00
) {
// Only for Admins
2021-02-18 07:12:32 +01:00
$mentions = \get_comment_meta( $comment_id, 'mentions', true );
\wp_schedule_single_event( \time(), 'activitypub_send_comment_activity', array( $comment_id ) );
2022-04-15 09:17:00 +02:00
}
2021-02-18 07:12:32 +01:00
}
}
2022-04-15 03:04:43 +02:00
/**
* edit_comment()
2022-04-15 09:17:00 +02:00
*
2022-04-15 03:04:43 +02:00
* Fires immediately after a comment is updated in the database.
* Fires immediately before comment status transition hooks are fired. (useful only for admin)
*/
2022-09-28 19:18:30 +02:00
public static function edit_comment( $comment_id, $data ) {
2022-04-15 03:04:43 +02:00
if ( ! is_null( $data['user_id'] ) ) {
2022-09-28 19:18:30 +02:00
\wp_schedule_single_event( \time(), 'activitypub_send_update_comment_activity', array( $comment_id ) );
2022-04-15 03:04:43 +02:00
}
}
2021-02-18 07:12:32 +01:00
/**
* Schedule Activities
2022-04-15 09:17:00 +02:00
*
2022-04-15 03:04:43 +02:00
* transition_comment_status()
2021-02-18 07:12:32 +01:00
* @param int $comment
*/
public static function schedule_comment_activity( $new_status, $old_status, $activitypub_comment ) {
if ( 'approved' === $new_status && 'approved' !== $old_status ) {
//should only federate replies from local actors
//should only federate replies to federated actors
2022-04-15 09:17:00 +02:00
2021-02-18 07:12:32 +01:00
$ap_object = unserialize( \get_comment_meta( $activitypub_comment->comment_ID, 'ap_object', true ) );
if ( empty( $ap_object ) ) {
\wp_schedule_single_event( \time(), 'activitypub_send_comment_activity', array( $activitypub_comment->comment_ID ) );
} else {
$local_user = \get_author_posts_url( $ap_object['user_id'] );
2022-04-15 09:17:00 +02:00
if ( ! is_null( $local_user ) ) {
2021-02-18 07:12:32 +01:00
if ( in_array( $local_user, $ap_object['to'] )
|| in_array( $local_user, $ap_object['cc'] )
|| in_array( $local_user, $ap_object['audience'] )
|| in_array( $local_user, $ap_object['tag'] )
) {
2022-04-15 09:17:00 +02:00
//if inReplyTo, object, target and/or tag are (local-wp) objects
\wp_schedule_single_event( \time(), 'activitypub_inbox_forward_activity', array( $activitypub_comment->comment_ID ) );
2021-02-18 07:12:32 +01:00
}
2022-04-15 09:17:00 +02:00
}
2021-02-18 07:12:32 +01:00
}
} elseif ( 'trash' === $new_status ) {
2022-04-15 03:04:43 +02:00
\wp_schedule_single_event( \time(), 'activitypub_send_delete_comment_activity', array( $activitypub_comment ) );
} elseif ( $old_status === $new_status ) {
//TODO Test with non-admin user
\wp_schedule_single_event( \time(), 'activitypub_send_update_comment_activity', array( $activitypub_comment->comment_ID ) );
2022-09-28 19:18:30 +02:00
}
2021-02-18 07:12:32 +01:00
}
2022-04-15 09:17:00 +02:00
2022-04-15 03:04:43 +02:00
/**
* get_comment_text( $comment )
2022-04-15 09:17:00 +02:00
*
2022-04-15 03:04:43 +02:00
* Filters the comment content before it is updated in the database.
*/
public static function comment_append_edit_datetime( $comment_text, $comment, $args ) {
if ( 'activitypub' === $comment->comment_type ) {
$updated = \wp_date( 'Y-m-d H:i:s', \strtotime( \get_comment_meta( $comment->comment_ID, 'ap_last_modified', true ) ) );
2022-04-15 09:17:00 +02:00
if ( $updated ) {
2022-04-15 03:04:43 +02:00
$append_updated = "<div>(Last edited on <time class='modified' datetime='{$updated}'>$updated</time>)</div>";
$comment_text .= $append_updated;
}
}
return $comment_text;
}
2021-02-18 07:12:32 +01:00
2019-02-20 21:05:29 +01:00
/**
2020-05-14 21:04:33 +02:00
* Replaces the default avatar.
2019-02-20 21:05:29 +01:00
*
2020-05-14 21:04:33 +02:00
* @param array $args Arguments passed to get_avatar_data(), after processing.
* @param int|string|object $id_or_email A user ID, email address, or comment object.
2019-02-20 21:05:29 +01:00
*
* @return array $args
*/
public static function pre_get_avatar_data( $args, $id_or_email ) {
if (
! $id_or_email instanceof \WP_Comment ||
! isset( $id_or_email->comment_type ) ||
$id_or_email->user_id
) {
2019-02-20 21:05:29 +01:00
return $args;
}
2021-02-18 07:12:32 +01:00
$allowed_comment_types = \apply_filters( 'get_avatar_comment_types', array( 'comment', 'activitypub' ) );
2019-09-27 10:12:59 +02:00
if ( ! empty( $id_or_email->comment_type ) && ! \in_array( $id_or_email->comment_type, (array) $allowed_comment_types, true ) ) {
2019-02-20 21:05:29 +01:00
$args['url'] = false;
/** This filter is documented in wp-includes/link-template.php */
2019-09-27 10:12:59 +02:00
return \apply_filters( 'get_avatar_data', $args, $id_or_email );
2019-02-20 21:05:29 +01:00
}
2020-05-14 21:04:33 +02:00
// Check if comment has an avatar.
2019-02-20 21:05:29 +01:00
$avatar = self::get_avatar_url( $id_or_email->comment_ID );
if ( $avatar ) {
2019-09-27 10:12:59 +02:00
if ( ! isset( $args['class'] ) || ! \is_array( $args['class'] ) ) {
2019-02-20 21:05:29 +01:00
$args['class'] = array( 'u-photo' );
} else {
$args['class'][] = 'u-photo';
2019-09-27 10:12:59 +02:00
$args['class'] = \array_unique( $args['class'] );
2019-02-20 21:05:29 +01:00
}
$args['url'] = $avatar;
$args['class'][] = 'avatar-activitypub';
}
return $args;
}
/**
2020-05-14 21:04:33 +02:00
* Function to retrieve Avatar URL if stored in meta.
2019-02-20 21:05:29 +01:00
*
* @param int|WP_Comment $comment
*
* @return string $url
*/
public static function get_avatar_url( $comment ) {
2019-09-27 10:12:59 +02:00
if ( \is_numeric( $comment ) ) {
$comment = \get_comment( $comment );
2019-02-20 21:05:29 +01:00
}
2019-09-27 10:12:59 +02:00
return \get_comment_meta( $comment->comment_ID, 'avatar_url', true );
2019-02-20 21:05:29 +01:00
}
2018-08-18 12:35:39 +02:00
}