updated plugin ActivityPub
version 5.8.0
This commit is contained in:
@ -8,6 +8,8 @@
|
||||
namespace Activitypub;
|
||||
|
||||
use Exception;
|
||||
use Activitypub\Collection\Actors;
|
||||
use Activitypub\Collection\Outbox;
|
||||
use Activitypub\Collection\Followers;
|
||||
use Activitypub\Collection\Extra_Fields;
|
||||
|
||||
@ -21,13 +23,15 @@ class Activitypub {
|
||||
* Initialize the class, registering WordPress hooks.
|
||||
*/
|
||||
public static function init() {
|
||||
\add_filter( 'template_include', array( self::class, 'render_json_template' ), 99 );
|
||||
\add_filter( 'template_include', array( self::class, 'render_activitypub_template' ), 99 );
|
||||
\add_action( 'template_redirect', array( self::class, 'template_redirect' ) );
|
||||
\add_filter( 'redirect_canonical', array( self::class, 'redirect_canonical' ), 10, 2 );
|
||||
\add_filter( 'redirect_canonical', array( self::class, 'no_trailing_redirect' ), 10, 2 );
|
||||
\add_filter( 'query_vars', array( self::class, 'add_query_vars' ) );
|
||||
\add_filter( 'pre_get_avatar_data', array( self::class, 'pre_get_avatar_data' ), 11, 2 );
|
||||
|
||||
// Add support for ActivityPub to custom post types.
|
||||
$post_types = \get_option( 'activitypub_support_post_types', array( 'post' ) ) ? \get_option( 'activitypub_support_post_types', array( 'post' ) ) : array();
|
||||
$post_types = \get_option( 'activitypub_support_post_types', array( 'post' ) );
|
||||
|
||||
foreach ( $post_types as $post_type ) {
|
||||
\add_post_type_support( $post_type, 'activitypub' );
|
||||
@ -41,16 +45,18 @@ class Activitypub {
|
||||
|
||||
\add_action( 'user_register', array( self::class, 'user_register' ) );
|
||||
|
||||
\add_action( 'in_plugin_update_message-' . ACTIVITYPUB_PLUGIN_BASENAME, array( self::class, 'plugin_update_message' ) );
|
||||
|
||||
if ( site_supports_blocks() ) {
|
||||
\add_action( 'tool_box', array( self::class, 'tool_box' ) );
|
||||
}
|
||||
|
||||
\add_filter( 'activitypub_get_actor_extra_fields', array( Extra_Fields::class, 'default_actor_extra_fields' ), 10, 2 );
|
||||
|
||||
\add_action( 'updated_postmeta', array( self::class, 'updated_postmeta' ), 10, 4 );
|
||||
\add_action( 'added_post_meta', array( self::class, 'updated_postmeta' ), 10, 4 );
|
||||
|
||||
\add_action( 'init', array( self::class, 'register_user_meta' ), 11 );
|
||||
|
||||
// Register several post_types.
|
||||
self::register_post_types();
|
||||
|
||||
self::register_oembed_providers();
|
||||
Embed::init();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -59,6 +65,9 @@ class Activitypub {
|
||||
public static function activate() {
|
||||
self::flush_rewrite_rules();
|
||||
Scheduler::register_schedules();
|
||||
|
||||
\add_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ), 10, 3 );
|
||||
Migration::update_comment_counts();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -67,6 +76,9 @@ class Activitypub {
|
||||
public static function deactivate() {
|
||||
self::flush_rewrite_rules();
|
||||
Scheduler::deregister_schedules();
|
||||
|
||||
\remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ) );
|
||||
Migration::update_comment_counts( 2000 );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -74,6 +86,43 @@ class Activitypub {
|
||||
*/
|
||||
public static function uninstall() {
|
||||
Scheduler::deregister_schedules();
|
||||
|
||||
\remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ) );
|
||||
Migration::update_comment_counts( 2000 );
|
||||
|
||||
\delete_option( 'activitypub_actor_mode' );
|
||||
\delete_option( 'activitypub_allow_likes' );
|
||||
\delete_option( 'activitypub_allow_replies' );
|
||||
\delete_option( 'activitypub_attribution_domains' );
|
||||
\delete_option( 'activitypub_authorized_fetch' );
|
||||
\delete_option( 'activitypub_application_user_private_key' );
|
||||
\delete_option( 'activitypub_application_user_public_key' );
|
||||
\delete_option( 'activitypub_blog_user_also_known_as' );
|
||||
\delete_option( 'activitypub_blog_user_mailer_new_dm' );
|
||||
\delete_option( 'activitypub_blog_user_mailer_new_follower' );
|
||||
\delete_option( 'activitypub_blog_user_mailer_new_mention' );
|
||||
\delete_option( 'activitypub_blog_user_moved_to' );
|
||||
\delete_option( 'activitypub_blog_user_private_key' );
|
||||
\delete_option( 'activitypub_blog_user_public_key' );
|
||||
\delete_option( 'activitypub_blog_description' );
|
||||
\delete_option( 'activitypub_blog_identifier' );
|
||||
\delete_option( 'activitypub_custom_post_content' );
|
||||
\delete_option( 'activitypub_db_version' );
|
||||
\delete_option( 'activitypub_default_extra_fields' );
|
||||
\delete_option( 'activitypub_enable_blog_user' );
|
||||
\delete_option( 'activitypub_enable_users' );
|
||||
\delete_option( 'activitypub_header_image' );
|
||||
\delete_option( 'activitypub_last_post_with_permalink_as_id' );
|
||||
\delete_option( 'activitypub_max_image_attachments' );
|
||||
\delete_option( 'activitypub_migration_lock' );
|
||||
\delete_option( 'activitypub_object_type' );
|
||||
\delete_option( 'activitypub_outbox_purge_days' );
|
||||
\delete_option( 'activitypub_shared_inbox' );
|
||||
\delete_option( 'activitypub_support_post_types' );
|
||||
\delete_option( 'activitypub_use_hashtags' );
|
||||
\delete_option( 'activitypub_use_opengraph' );
|
||||
\delete_option( 'activitypub_use_permalink_as_id_for_blog' );
|
||||
\delete_option( 'activitypub_vary_header' );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -83,25 +132,33 @@ class Activitypub {
|
||||
*
|
||||
* @return string The new path to the JSON template.
|
||||
*/
|
||||
public static function render_json_template( $template ) {
|
||||
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
|
||||
public static function render_activitypub_template( $template ) {
|
||||
if ( \wp_is_serving_rest_request() || \wp_doing_ajax() ) {
|
||||
return $template;
|
||||
}
|
||||
|
||||
self::add_headers();
|
||||
|
||||
if ( ! is_activitypub_request() ) {
|
||||
return $template;
|
||||
}
|
||||
|
||||
$json_template = false;
|
||||
$activitypub_template = false;
|
||||
$activitypub_object = Query::get_instance()->get_activitypub_object();
|
||||
|
||||
if ( \is_author() && ! is_user_disabled( \get_the_author_meta( 'ID' ) ) ) {
|
||||
$json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/user-json.php';
|
||||
} elseif ( is_comment() ) {
|
||||
$json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/comment-json.php';
|
||||
} elseif ( \is_singular() ) {
|
||||
$json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/post-json.php';
|
||||
} elseif ( \is_home() && ! is_user_type_disabled( 'blog' ) ) {
|
||||
$json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/blog-json.php';
|
||||
if ( $activitypub_object ) {
|
||||
if ( \get_query_var( 'preview' ) ) {
|
||||
\define( 'ACTIVITYPUB_PREVIEW', true );
|
||||
|
||||
/**
|
||||
* Filter the template used for the ActivityPub preview.
|
||||
*
|
||||
* @param string $activitypub_template Absolute path to the template file.
|
||||
*/
|
||||
$activitypub_template = apply_filters( 'activitypub_preview_template', ACTIVITYPUB_PLUGIN_DIR . '/templates/post-preview.php' );
|
||||
} else {
|
||||
$activitypub_template = ACTIVITYPUB_PLUGIN_DIR . 'templates/activitypub-json.php';
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@ -110,7 +167,7 @@ class Activitypub {
|
||||
* @see https://www.w3.org/wiki/SocialCG/ActivityPub/Primer/Authentication_Authorization#Authorized_fetch
|
||||
* @see https://swicg.github.io/activitypub-http-signature/#authorized-fetch
|
||||
*/
|
||||
if ( $json_template && ACTIVITYPUB_AUTHORIZED_FETCH ) {
|
||||
if ( $activitypub_template && use_authorized_fetch() ) {
|
||||
$verification = Signature::verify_http_signature( $_SERVER );
|
||||
if ( \is_wp_error( $verification ) ) {
|
||||
header( 'HTTP/1.1 401 Unauthorized' );
|
||||
@ -120,8 +177,16 @@ class Activitypub {
|
||||
}
|
||||
}
|
||||
|
||||
if ( $json_template ) {
|
||||
return $json_template;
|
||||
if ( $activitypub_template ) {
|
||||
\set_query_var( 'is_404', false );
|
||||
|
||||
// Check if header already sent.
|
||||
if ( ! \headers_sent() ) {
|
||||
// Send 200 status header.
|
||||
\status_header( 200 );
|
||||
}
|
||||
|
||||
return $activitypub_template;
|
||||
}
|
||||
|
||||
return $template;
|
||||
@ -131,80 +196,132 @@ class Activitypub {
|
||||
* Add the 'self' link to the header.
|
||||
*/
|
||||
public static function add_headers() {
|
||||
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput
|
||||
$request_uri = $_SERVER['REQUEST_URI'];
|
||||
$id = Query::get_instance()->get_activitypub_object_id();
|
||||
|
||||
if ( ! $request_uri ) {
|
||||
if ( ! $id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Only add self link to author pages...
|
||||
if ( is_author() ) {
|
||||
if ( is_user_disabled( get_queried_object_id() ) ) {
|
||||
return;
|
||||
}
|
||||
} elseif ( is_singular() ) { // or posts/pages/custom-post-types...
|
||||
if ( ! \post_type_supports( \get_post_type(), 'activitypub' ) ) {
|
||||
return;
|
||||
}
|
||||
} else { // otherwise return.
|
||||
return;
|
||||
}
|
||||
|
||||
// Add self link to html and http header.
|
||||
$host = wp_parse_url( home_url() );
|
||||
|
||||
/**
|
||||
* Filters the self link.
|
||||
*
|
||||
* @param string $self_link The self link.
|
||||
*/
|
||||
$self_link = apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . wp_unslash( $request_uri ) ) );
|
||||
$self_link = esc_url( $self_link );
|
||||
|
||||
if ( ! headers_sent() ) {
|
||||
header( 'Link: <' . $self_link . '>; rel="alternate"; type="application/activity+json"' );
|
||||
\header( 'Link: <' . esc_url( $id ) . '>; title="ActivityPub (JSON)"; rel="alternate"; type="application/activity+json"', false );
|
||||
|
||||
if ( \get_option( 'activitypub_vary_header' ) ) {
|
||||
// Send Vary header for Accept header.
|
||||
\header( 'Vary: Accept', false );
|
||||
}
|
||||
}
|
||||
|
||||
add_action(
|
||||
'wp_head',
|
||||
function () use ( $self_link ) {
|
||||
echo PHP_EOL . '<link rel="alternate" type="application/activity+json" href="' . esc_url( $self_link ) . '" />' . PHP_EOL;
|
||||
function () use ( $id ) {
|
||||
echo PHP_EOL . '<link rel="alternate" title="ActivityPub (JSON)" type="application/activity+json" href="' . esc_url( $id ) . '" />' . PHP_EOL;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove trailing slash from ActivityPub @username requests.
|
||||
*
|
||||
* @param string $redirect_url The URL to redirect to.
|
||||
* @param string $requested_url The requested URL.
|
||||
*
|
||||
* @return string $redirect_url The possibly-unslashed redirect URL.
|
||||
*/
|
||||
public static function no_trailing_redirect( $redirect_url, $requested_url ) {
|
||||
if ( get_query_var( 'actor' ) ) {
|
||||
return $requested_url;
|
||||
}
|
||||
|
||||
return $redirect_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add support for `p` and `author` query vars.
|
||||
*
|
||||
* @param string $redirect_url The URL to redirect to.
|
||||
* @param string $requested_url The requested URL.
|
||||
*
|
||||
* @return string $redirect_url
|
||||
*/
|
||||
public static function redirect_canonical( $redirect_url, $requested_url ) {
|
||||
if ( ! is_activitypub_request() ) {
|
||||
return $redirect_url;
|
||||
}
|
||||
|
||||
$query = \wp_parse_url( $requested_url, PHP_URL_QUERY );
|
||||
|
||||
if ( ! $query ) {
|
||||
return $redirect_url;
|
||||
}
|
||||
|
||||
$query_params = \wp_parse_args( $query );
|
||||
unset( $query_params['activitypub'] );
|
||||
|
||||
if ( 1 !== count( $query_params ) ) {
|
||||
return $redirect_url;
|
||||
}
|
||||
|
||||
if ( isset( $query_params['p'] ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ( isset( $query_params['author'] ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $requested_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom redirects for ActivityPub requests.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function template_redirect() {
|
||||
self::add_headers();
|
||||
global $wp_query;
|
||||
|
||||
$comment_id = get_query_var( 'c', null );
|
||||
|
||||
// Check if it seems to be a comment.
|
||||
if ( ! $comment_id ) {
|
||||
return;
|
||||
if ( $comment_id ) {
|
||||
$comment = get_comment( $comment_id );
|
||||
|
||||
// Load a 404-page if `c` is set but not valid.
|
||||
if ( ! $comment ) {
|
||||
$wp_query->set_404();
|
||||
return;
|
||||
}
|
||||
|
||||
// Stop if it's not an ActivityPub comment.
|
||||
if ( is_activitypub_request() && ! is_local_comment( $comment ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
wp_safe_redirect( get_comment_link( $comment ) );
|
||||
exit;
|
||||
}
|
||||
|
||||
$comment = get_comment( $comment_id );
|
||||
$actor = get_query_var( 'actor', null );
|
||||
if ( $actor ) {
|
||||
$actor = Actors::get_by_username( $actor );
|
||||
if ( ! $actor || \is_wp_error( $actor ) ) {
|
||||
$wp_query->set_404();
|
||||
return;
|
||||
}
|
||||
|
||||
// Load a 404 page if `c` is set but not valid.
|
||||
if ( ! $comment ) {
|
||||
global $wp_query;
|
||||
$wp_query->set_404();
|
||||
return;
|
||||
if ( is_activitypub_request() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $actor->get__id() > 0 ) {
|
||||
$redirect_url = $actor->get_url();
|
||||
} else {
|
||||
$redirect_url = get_bloginfo( 'url' );
|
||||
}
|
||||
|
||||
wp_safe_redirect( $redirect_url, 301 );
|
||||
exit;
|
||||
}
|
||||
|
||||
// Stop if it's not an ActivityPub comment.
|
||||
if ( is_activitypub_request() && ! is_local_comment( $comment ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
wp_safe_redirect( get_comment_link( $comment ) );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -216,6 +333,9 @@ class Activitypub {
|
||||
*/
|
||||
public static function add_query_vars( $vars ) {
|
||||
$vars[] = 'activitypub';
|
||||
$vars[] = 'preview';
|
||||
$vars[] = 'author';
|
||||
$vars[] = 'actor';
|
||||
$vars[] = 'c';
|
||||
$vars[] = 'p';
|
||||
|
||||
@ -254,7 +374,7 @@ class Activitypub {
|
||||
}
|
||||
|
||||
// Check if comment has an avatar.
|
||||
$avatar = self::get_avatar_url( $id_or_email->comment_ID );
|
||||
$avatar = \get_comment_meta( $id_or_email->comment_ID, 'avatar_url', true );
|
||||
|
||||
if ( $avatar ) {
|
||||
if ( empty( $args['class'] ) ) {
|
||||
@ -272,20 +392,6 @@ class Activitypub {
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to retrieve Avatar URL if stored in meta.
|
||||
*
|
||||
* @param int|\WP_Comment $comment The comment ID or object.
|
||||
*
|
||||
* @return string The Avatar URL.
|
||||
*/
|
||||
public static function get_avatar_url( $comment ) {
|
||||
if ( \is_numeric( $comment ) ) {
|
||||
$comment = \get_comment( $comment );
|
||||
}
|
||||
return \get_comment_meta( $comment->comment_ID, 'avatar_url', true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Store permalink in meta, to send delete Activity.
|
||||
*
|
||||
@ -294,7 +400,7 @@ class Activitypub {
|
||||
public static function trash_post( $post_id ) {
|
||||
\add_post_meta(
|
||||
$post_id,
|
||||
'activitypub_canonical_url',
|
||||
'_activitypub_canonical_url',
|
||||
\get_permalink( $post_id ),
|
||||
true
|
||||
);
|
||||
@ -306,7 +412,7 @@ class Activitypub {
|
||||
* @param string $post_id The Post ID.
|
||||
*/
|
||||
public static function untrash_post( $post_id ) {
|
||||
\delete_post_meta( $post_id, 'activitypub_canonical_url' );
|
||||
\delete_post_meta( $post_id, '_activitypub_canonical_url' );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -332,22 +438,12 @@ class Activitypub {
|
||||
if ( ! \class_exists( 'Nodeinfo_Endpoint' ) && true === (bool) \get_option( 'blog_public', 1 ) ) {
|
||||
\add_rewrite_rule(
|
||||
'^.well-known/nodeinfo',
|
||||
'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo/discovery',
|
||||
'top'
|
||||
);
|
||||
\add_rewrite_rule(
|
||||
'^.well-known/x-nodeinfo2',
|
||||
'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo2',
|
||||
'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo',
|
||||
'top'
|
||||
);
|
||||
}
|
||||
|
||||
\add_rewrite_rule(
|
||||
'^@([\w\-\.]+)',
|
||||
'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/actors/$matches[1]',
|
||||
'top'
|
||||
);
|
||||
|
||||
\add_rewrite_rule( '^@([\w\-\.]+)\/?$', 'index.php?actor=$matches[1]', 'top' );
|
||||
\add_rewrite_endpoint( 'activitypub', EP_AUTHORS | EP_PERMALINK | EP_PAGES );
|
||||
}
|
||||
|
||||
@ -359,15 +455,6 @@ class Activitypub {
|
||||
\flush_rewrite_rules();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds metabox on wp-admin/tools.php.
|
||||
*/
|
||||
public static function tool_box() {
|
||||
if ( \current_user_can( 'edit_posts' ) ) {
|
||||
\load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/toolbox.php' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Theme compatibility stuff.
|
||||
*/
|
||||
@ -391,31 +478,7 @@ class Activitypub {
|
||||
}
|
||||
|
||||
/**
|
||||
* Display plugin upgrade notice to users.
|
||||
*
|
||||
* @param array $data The plugin data.
|
||||
*/
|
||||
public static function plugin_update_message( $data ) {
|
||||
if ( ! isset( $data['upgrade_notice'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
printf(
|
||||
'<div class="update-message">%s</div>',
|
||||
wp_kses(
|
||||
wpautop( $data['upgrade_notice '] ),
|
||||
array(
|
||||
'p' => array(),
|
||||
'a' => array( 'href', 'title' ),
|
||||
'strong' => array(),
|
||||
'em' => array(),
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the "Followers" Taxonomy.
|
||||
* Register Custom Post Types.
|
||||
*/
|
||||
private static function register_post_types() {
|
||||
\register_post_type(
|
||||
@ -437,7 +500,7 @@ class Activitypub {
|
||||
|
||||
\register_post_meta(
|
||||
Followers::POST_TYPE,
|
||||
'activitypub_inbox',
|
||||
'_activitypub_inbox',
|
||||
array(
|
||||
'type' => 'string',
|
||||
'single' => true,
|
||||
@ -447,7 +510,7 @@ class Activitypub {
|
||||
|
||||
\register_post_meta(
|
||||
Followers::POST_TYPE,
|
||||
'activitypub_errors',
|
||||
'_activitypub_errors',
|
||||
array(
|
||||
'type' => 'string',
|
||||
'single' => false,
|
||||
@ -463,7 +526,7 @@ class Activitypub {
|
||||
|
||||
\register_post_meta(
|
||||
Followers::POST_TYPE,
|
||||
'activitypub_user_id',
|
||||
'_activitypub_user_id',
|
||||
array(
|
||||
'type' => 'string',
|
||||
'single' => false,
|
||||
@ -475,7 +538,7 @@ class Activitypub {
|
||||
|
||||
\register_post_meta(
|
||||
Followers::POST_TYPE,
|
||||
'activitypub_actor_json',
|
||||
'_activitypub_actor_json',
|
||||
array(
|
||||
'type' => 'string',
|
||||
'single' => true,
|
||||
@ -485,6 +548,128 @@ class Activitypub {
|
||||
)
|
||||
);
|
||||
|
||||
// Register Outbox Post-Type.
|
||||
register_post_type(
|
||||
Outbox::POST_TYPE,
|
||||
array(
|
||||
'labels' => array(
|
||||
'name' => _x( 'Outbox', 'post_type plural name', 'activitypub' ),
|
||||
'singular_name' => _x( 'Outbox Item', 'post_type single name', 'activitypub' ),
|
||||
),
|
||||
'capabilities' => array(
|
||||
'create_posts' => false,
|
||||
),
|
||||
'map_meta_cap' => true,
|
||||
'public' => false,
|
||||
'show_in_rest' => true,
|
||||
'rewrite' => false,
|
||||
'query_var' => false,
|
||||
'supports' => array( 'title', 'editor', 'author', 'custom-fields' ),
|
||||
'delete_with_user' => true,
|
||||
'can_export' => true,
|
||||
'exclude_from_search' => true,
|
||||
)
|
||||
);
|
||||
|
||||
/**
|
||||
* Register Activity Type meta for Outbox items.
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
|
||||
*/
|
||||
\register_post_meta(
|
||||
Outbox::POST_TYPE,
|
||||
'_activitypub_activity_type',
|
||||
array(
|
||||
'type' => 'string',
|
||||
'description' => 'The type of the activity',
|
||||
'single' => true,
|
||||
'show_in_rest' => true,
|
||||
'sanitize_callback' => function ( $value ) {
|
||||
$value = ucfirst( strtolower( $value ) );
|
||||
$schema = array(
|
||||
'type' => 'string',
|
||||
'enum' => array( 'Accept', 'Add', 'Announce', 'Arrive', 'Block', 'Create', 'Delete', 'Dislike', 'Flag', 'Follow', 'Ignore', 'Invite', 'Join', 'Leave', 'Like', 'Listen', 'Move', 'Offer', 'Question', 'Reject', 'Read', 'Remove', 'TentativeReject', 'TentativeAccept', 'Travel', 'Undo', 'Update', 'View' ),
|
||||
'default' => 'Announce',
|
||||
);
|
||||
|
||||
if ( is_wp_error( rest_validate_enum( $value, $schema, '' ) ) ) {
|
||||
return $schema['default'];
|
||||
}
|
||||
|
||||
return $value;
|
||||
},
|
||||
)
|
||||
);
|
||||
|
||||
\register_post_meta(
|
||||
Outbox::POST_TYPE,
|
||||
'_activitypub_activity_actor',
|
||||
array(
|
||||
'type' => 'string',
|
||||
'single' => true,
|
||||
'show_in_rest' => true,
|
||||
'sanitize_callback' => function ( $value ) {
|
||||
$schema = array(
|
||||
'type' => 'string',
|
||||
'enum' => array( 'application', 'blog', 'user' ),
|
||||
'default' => 'user',
|
||||
);
|
||||
|
||||
if ( is_wp_error( rest_validate_enum( $value, $schema, '' ) ) ) {
|
||||
return $schema['default'];
|
||||
}
|
||||
|
||||
return $value;
|
||||
},
|
||||
)
|
||||
);
|
||||
|
||||
\register_post_meta(
|
||||
Outbox::POST_TYPE,
|
||||
'_activitypub_outbox_offset',
|
||||
array(
|
||||
'type' => 'integer',
|
||||
'single' => true,
|
||||
'description' => 'Keeps track of the followers offset when processing outbox items.',
|
||||
'sanitize_callback' => 'absint',
|
||||
'default' => 0,
|
||||
)
|
||||
);
|
||||
|
||||
\register_post_meta(
|
||||
Outbox::POST_TYPE,
|
||||
'_activitypub_object_id',
|
||||
array(
|
||||
'type' => 'string',
|
||||
'single' => true,
|
||||
'description' => 'The ID (ActivityPub URI) of the object that the outbox item is about.',
|
||||
'sanitize_callback' => 'sanitize_url',
|
||||
)
|
||||
);
|
||||
|
||||
\register_post_meta(
|
||||
Outbox::POST_TYPE,
|
||||
'activitypub_content_visibility',
|
||||
array(
|
||||
'type' => 'string',
|
||||
'single' => true,
|
||||
'show_in_rest' => true,
|
||||
'sanitize_callback' => function ( $value ) {
|
||||
$schema = array(
|
||||
'type' => 'string',
|
||||
'enum' => array( ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC, ACTIVITYPUB_CONTENT_VISIBILITY_QUIET_PUBLIC, ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE, ACTIVITYPUB_CONTENT_VISIBILITY_LOCAL ),
|
||||
'default' => ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC,
|
||||
);
|
||||
|
||||
if ( is_wp_error( rest_validate_enum( $value, $schema, '' ) ) ) {
|
||||
return $schema['default'];
|
||||
}
|
||||
|
||||
return $value;
|
||||
},
|
||||
)
|
||||
);
|
||||
|
||||
// Both User and Blog Extra Fields types have the same args.
|
||||
$args = array(
|
||||
'labels' => array(
|
||||
@ -515,6 +700,9 @@ class Activitypub {
|
||||
\register_post_type( Extra_Fields::USER_POST_TYPE, $args );
|
||||
\register_post_type( Extra_Fields::BLOG_POST_TYPE, $args );
|
||||
|
||||
/**
|
||||
* Fires after ActivityPub custom post types have been registered.
|
||||
*/
|
||||
\do_action( 'activitypub_after_register_post_type' );
|
||||
}
|
||||
|
||||
@ -529,4 +717,182 @@ class Activitypub {
|
||||
$user->add_cap( 'activitypub' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete `activitypub_content_visibility` when updated to an empty value.
|
||||
*
|
||||
* @param int $meta_id ID of updated metadata entry.
|
||||
* @param int $object_id Post ID.
|
||||
* @param string $meta_key Metadata key.
|
||||
* @param mixed $meta_value Metadata value. This will be a PHP-serialized string representation of the value
|
||||
* if the value is an array, an object, or itself a PHP-serialized string.
|
||||
*/
|
||||
public static function updated_postmeta( $meta_id, $object_id, $meta_key, $meta_value ) {
|
||||
if ( 'activitypub_content_visibility' === $meta_key && empty( $meta_value ) ) {
|
||||
\delete_post_meta( $object_id, 'activitypub_content_visibility' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register some Mastodon oEmbed providers.
|
||||
*/
|
||||
public static function register_oembed_providers() {
|
||||
\wp_oembed_add_provider( '#https?://mastodon\.social/(@.+)/([0-9]+)#i', 'https://mastodon.social/api/oembed', true );
|
||||
\wp_oembed_add_provider( '#https?://mastodon\.online/(@.+)/([0-9]+)#i', 'https://mastodon.online/api/oembed', true );
|
||||
\wp_oembed_add_provider( '#https?://mastodon\.cloud/(@.+)/([0-9]+)#i', 'https://mastodon.cloud/api/oembed', true );
|
||||
\wp_oembed_add_provider( '#https?://mstdn\.social/(@.+)/([0-9]+)#i', 'https://mstdn.social/api/oembed', true );
|
||||
\wp_oembed_add_provider( '#https?://mastodon\.world/(@.+)/([0-9]+)#i', 'https://mastodon.world/api/oembed', true );
|
||||
\wp_oembed_add_provider( '#https?://mas\.to/(@.+)/([0-9]+)#i', 'https://mas.to/api/oembed', true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register user meta.
|
||||
*/
|
||||
public static function register_user_meta() {
|
||||
$blog_prefix = $GLOBALS['wpdb']->get_blog_prefix();
|
||||
|
||||
\register_meta(
|
||||
'user',
|
||||
$blog_prefix . 'activitypub_also_known_as',
|
||||
array(
|
||||
'type' => 'array',
|
||||
'description' => 'An array of URLs that the user is known by.',
|
||||
'single' => true,
|
||||
'default' => array(),
|
||||
'sanitize_callback' => array( Sanitize::class, 'url_list' ),
|
||||
)
|
||||
);
|
||||
|
||||
\register_meta(
|
||||
'user',
|
||||
$blog_prefix . 'activitypub_old_host_data',
|
||||
array(
|
||||
'description' => 'Actor object for the user on the old host.',
|
||||
'single' => true,
|
||||
)
|
||||
);
|
||||
|
||||
\register_meta(
|
||||
'user',
|
||||
$blog_prefix . 'activitypub_moved_to',
|
||||
array(
|
||||
'type' => 'string',
|
||||
'description' => 'The new URL of the user.',
|
||||
'single' => true,
|
||||
'sanitize_callback' => 'sanitize_url',
|
||||
)
|
||||
);
|
||||
|
||||
\register_meta(
|
||||
'user',
|
||||
$blog_prefix . 'activitypub_description',
|
||||
array(
|
||||
'type' => 'string',
|
||||
'description' => 'The user’s description.',
|
||||
'single' => true,
|
||||
'default' => '',
|
||||
'sanitize_callback' => function ( $value ) {
|
||||
return wp_kses( $value, 'user_description' );
|
||||
},
|
||||
)
|
||||
);
|
||||
|
||||
\register_meta(
|
||||
'user',
|
||||
$blog_prefix . 'activitypub_icon',
|
||||
array(
|
||||
'type' => 'integer',
|
||||
'description' => 'The attachment ID for user’s profile image.',
|
||||
'single' => true,
|
||||
'default' => 0,
|
||||
'sanitize_callback' => 'absint',
|
||||
)
|
||||
);
|
||||
|
||||
\register_meta(
|
||||
'user',
|
||||
$blog_prefix . 'activitypub_header_image',
|
||||
array(
|
||||
'type' => 'integer',
|
||||
'description' => 'The attachment ID for the user’s header image.',
|
||||
'single' => true,
|
||||
'default' => 0,
|
||||
'sanitize_callback' => 'absint',
|
||||
)
|
||||
);
|
||||
|
||||
\register_meta(
|
||||
'user',
|
||||
$blog_prefix . 'activitypub_mailer_new_dm',
|
||||
array(
|
||||
'type' => 'integer',
|
||||
'description' => 'Send a notification when someone sends this user a direct message.',
|
||||
'single' => true,
|
||||
'sanitize_callback' => 'absint',
|
||||
)
|
||||
);
|
||||
\add_filter( 'get_user_option_activitypub_mailer_new_dm', array( self::class, 'user_options_default' ) );
|
||||
|
||||
\register_meta(
|
||||
'user',
|
||||
$blog_prefix . 'activitypub_mailer_new_follower',
|
||||
array(
|
||||
'type' => 'integer',
|
||||
'description' => 'Send a notification when someone starts to follow this user.',
|
||||
'single' => true,
|
||||
'sanitize_callback' => 'absint',
|
||||
)
|
||||
);
|
||||
\add_filter( 'get_user_option_activitypub_mailer_new_follower', array( self::class, 'user_options_default' ) );
|
||||
|
||||
\register_meta(
|
||||
'user',
|
||||
$blog_prefix . 'activitypub_mailer_new_mention',
|
||||
array(
|
||||
'type' => 'integer',
|
||||
'description' => 'Send a notification when someone mentions this user.',
|
||||
'single' => true,
|
||||
'sanitize_callback' => 'absint',
|
||||
)
|
||||
);
|
||||
\add_filter( 'get_user_option_activitypub_mailer_new_mention', array( self::class, 'user_options_default' ) );
|
||||
|
||||
\register_meta(
|
||||
'user',
|
||||
'activitypub_show_welcome_tab',
|
||||
array(
|
||||
'type' => 'integer',
|
||||
'description' => 'Whether to show the welcome tab.',
|
||||
'single' => true,
|
||||
'default' => 1,
|
||||
'sanitize_callback' => 'absint',
|
||||
)
|
||||
);
|
||||
|
||||
\register_meta(
|
||||
'user',
|
||||
'activitypub_show_advanced_tab',
|
||||
array(
|
||||
'type' => 'integer',
|
||||
'description' => 'Whether to show the advanced tab.',
|
||||
'single' => true,
|
||||
'default' => 0,
|
||||
'sanitize_callback' => 'absint',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set default values for user options.
|
||||
*
|
||||
* @param bool|string $value Option value.
|
||||
* @return bool|string
|
||||
*/
|
||||
public static function user_options_default( $value ) {
|
||||
if ( false === $value ) {
|
||||
return '1';
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user