updated plugin ActivityPub version 3.3.3

This commit is contained in:
2024-10-09 12:44:17 +00:00
committed by Gitium
parent fb4b27bbc6
commit c54fa007bd
106 changed files with 7070 additions and 2918 deletions

View File

@ -1,20 +1,18 @@
<?php
/**
* ActivityPub Class.
*
* @package Activitypub
*/
namespace Activitypub;
use Exception;
use Activitypub\Signature;
use Activitypub\Collection\Users;
use Activitypub\Collection\Followers;
use function Activitypub\is_comment;
use function Activitypub\sanitize_url;
use function Activitypub\is_local_comment;
use function Activitypub\is_user_type_disabled;
use function Activitypub\is_activitypub_request;
use function Activitypub\should_comment_be_federated;
use Activitypub\Collection\Extra_Fields;
/**
* ActivityPub Class
* ActivityPub Class.
*
* @author Matthias Pfefferle
*/
@ -28,7 +26,7 @@ class Activitypub {
\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
// 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();
foreach ( $post_types as $post_type ) {
@ -45,16 +43,18 @@ class Activitypub {
\add_action( 'in_plugin_update_message-' . ACTIVITYPUB_PLUGIN_BASENAME, array( self::class, 'plugin_update_message' ) );
\add_filter( 'activitypub_get_actor_extra_fields', array( self::class, 'default_actor_extra_fields' ), 10, 2 );
if ( site_supports_blocks() ) {
\add_action( 'tool_box', array( self::class, 'tool_box' ) );
}
// register several post_types
\add_filter( 'activitypub_get_actor_extra_fields', array( Extra_Fields::class, 'default_actor_extra_fields' ), 10, 2 );
// Register several post_types.
self::register_post_types();
}
/**
* Activation Hook
*
* @return void
* Activation Hook.
*/
public static function activate() {
self::flush_rewrite_rules();
@ -62,9 +62,7 @@ class Activitypub {
}
/**
* Deactivation Hook
*
* @return void
* Deactivation Hook.
*/
public static function deactivate() {
self::flush_rewrite_rules();
@ -72,9 +70,7 @@ class Activitypub {
}
/**
* Uninstall Hook
*
* @return void
* Uninstall Hook.
*/
public static function uninstall() {
Scheduler::deregister_schedules();
@ -99,7 +95,7 @@ class Activitypub {
$json_template = false;
if ( \is_author() && ! is_user_disabled( \get_the_author_meta( 'ID' ) ) ) {
$json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/author-json.php';
$json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/user-json.php';
} elseif ( is_comment() ) {
$json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/comment-json.php';
} elseif ( \is_singular() ) {
@ -119,7 +115,7 @@ class Activitypub {
if ( \is_wp_error( $verification ) ) {
header( 'HTTP/1.1 401 Unauthorized' );
// fallback as template_loader can't return http headers
// Fallback as template_loader can't return http headers.
return $template;
}
}
@ -131,29 +127,78 @@ class Activitypub {
return $template;
}
/**
* Add the 'self' link to the header.
*/
public static function add_headers() {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput
$request_uri = $_SERVER['REQUEST_URI'];
if ( ! $request_uri ) {
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"' );
}
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;
}
);
}
/**
* Custom redirects for ActivityPub requests.
*
* @return void
*/
public static function template_redirect() {
self::add_headers();
$comment_id = get_query_var( 'c', null );
// check if it seems to be a comment
// Check if it seems to be a comment.
if ( ! $comment_id ) {
return;
}
$comment = get_comment( $comment_id );
// load a 404 page if `c` is set but not valid
// Load a 404 page if `c` is set but not valid.
if ( ! $comment ) {
global $wp_query;
$wp_query->set_404();
return;
}
// stop if it's not an ActivityPub comment
// Stop if it's not an ActivityPub comment.
if ( is_activitypub_request() && ! is_local_comment( $comment ) ) {
return;
}
@ -164,6 +209,10 @@ class Activitypub {
/**
* Add the 'activitypub' query variable so WordPress won't mangle it.
*
* @param array $vars The query variables.
*
* @return array The query variables.
*/
public static function add_query_vars( $vars ) {
$vars[] = 'activitypub';
@ -226,9 +275,9 @@ class Activitypub {
/**
* Function to retrieve Avatar URL if stored in meta.
*
* @param int|WP_Comment $comment
* @param int|\WP_Comment $comment The comment ID or object.
*
* @return string $url
* @return string The Avatar URL.
*/
public static function get_avatar_url( $comment ) {
if ( \is_numeric( $comment ) ) {
@ -241,8 +290,6 @@ class Activitypub {
* Store permalink in meta, to send delete Activity.
*
* @param string $post_id The Post ID.
*
* @return void
*/
public static function trash_post( $post_id ) {
\add_post_meta(
@ -254,22 +301,22 @@ class Activitypub {
}
/**
* Delete permalink from meta
* Delete permalink from meta.
*
* @param string $post_id The Post ID
*
* @return void
* @param string $post_id The Post ID.
*/
public static function untrash_post( $post_id ) {
\delete_post_meta( $post_id, 'activitypub_canonical_url' );
}
/**
* Add rewrite rules
* Add rewrite rules.
*/
public static function add_rewrite_rules() {
// If another system needs to take precedence over the ActivityPub rewrite rules,
// they can define their own and will manually call the appropriate functions as required.
/*
* If another system needs to take precedence over the ActivityPub rewrite rules,
* they can define their own and will manually call the appropriate functions as required.
*/
if ( ACTIVITYPUB_DISABLE_REWRITES ) {
return;
}
@ -305,7 +352,7 @@ class Activitypub {
}
/**
* Flush rewrite rules;
* Flush rewrite rules.
*/
public static function flush_rewrite_rules() {
self::add_rewrite_rules();
@ -313,37 +360,19 @@ class Activitypub {
}
/**
* Theme compatibility stuff
*
* @return void
* 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.
*/
public static function theme_compat() {
$site_icon = get_theme_support( 'custom-logo' );
if ( ! $site_icon ) {
// custom logo support
add_theme_support(
'custom-logo',
array(
'height' => 80,
'width' => 80,
)
);
}
$custom_header = get_theme_support( 'custom-header' );
if ( ! $custom_header ) {
// This theme supports a custom header
$custom_header_args = array(
'width' => 1250,
'height' => 600,
'header-text' => true,
);
add_theme_support( 'custom-header', $custom_header_args );
}
// We assume that you want to use Post-Formats when enabling the setting
// We assume that you want to use Post-Formats when enabling the setting.
if ( 'wordpress-post-format' === \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE ) ) {
if ( ! get_theme_support( 'post-formats' ) ) {
// Add support for the Aside, Gallery Post Formats...
@ -362,11 +391,9 @@ class Activitypub {
}
/**
* Display plugin upgrade notice to users
* Display plugin upgrade notice to users.
*
* @param array $data The plugin data
*
* @return void
* @param array $data The plugin data.
*/
public static function plugin_update_message( $data ) {
if ( ! isset( $data['upgrade_notice'] ) ) {
@ -388,9 +415,7 @@ class Activitypub {
}
/**
* Register the "Followers" Taxonomy
*
* @return void
* Register the "Followers" Taxonomy.
*/
private static function register_post_types() {
\register_post_type(
@ -460,44 +485,43 @@ class Activitypub {
)
);
\register_post_type(
'ap_extrafield',
array(
'labels' => array(
'name' => _x( 'Extra fields', 'post_type plural name', 'activitypub' ),
'singular_name' => _x( 'Extra field', 'post_type single name', 'activitypub' ),
'add_new' => __( 'Add new', 'activitypub' ),
'add_new_item' => __( 'Add new extra field', 'activitypub' ),
'new_item' => __( 'New extra field', 'activitypub' ),
'edit_item' => __( 'Edit extra field', 'activitypub' ),
'view_item' => __( 'View extra field', 'activitypub' ),
'all_items' => __( 'All extra fields', 'activitypub' ),
),
'public' => false,
'hierarchical' => false,
'query_var' => false,
'has_archive' => false,
'publicly_queryable' => false,
'show_in_menu' => false,
'delete_with_user' => true,
'can_export' => true,
'exclude_from_search' => true,
'show_in_rest' => true,
'map_meta_cap' => true,
'show_ui' => true,
'supports' => array( 'title', 'editor' ),
)
// Both User and Blog Extra Fields types have the same args.
$args = array(
'labels' => array(
'name' => _x( 'Extra fields', 'post_type plural name', 'activitypub' ),
'singular_name' => _x( 'Extra field', 'post_type single name', 'activitypub' ),
'add_new' => __( 'Add new', 'activitypub' ),
'add_new_item' => __( 'Add new extra field', 'activitypub' ),
'new_item' => __( 'New extra field', 'activitypub' ),
'edit_item' => __( 'Edit extra field', 'activitypub' ),
'view_item' => __( 'View extra field', 'activitypub' ),
'all_items' => __( 'All extra fields', 'activitypub' ),
),
'public' => false,
'hierarchical' => false,
'query_var' => false,
'has_archive' => false,
'publicly_queryable' => false,
'show_in_menu' => false,
'delete_with_user' => true,
'can_export' => true,
'exclude_from_search' => true,
'show_in_rest' => true,
'map_meta_cap' => true,
'show_ui' => true,
'supports' => array( 'title', 'editor', 'page-attributes' ),
);
\register_post_type( Extra_Fields::USER_POST_TYPE, $args );
\register_post_type( Extra_Fields::BLOG_POST_TYPE, $args );
\do_action( 'activitypub_after_register_post_type' );
}
/**
* Add the 'activitypub' capability to users who can publish posts.
*
* @param int $user_id User ID.
*
* @param array $userdata The raw array of data passed to wp_insert_user().
* @param int $user_id User ID.
*/
public static function user_register( $user_id ) {
if ( \user_can( $user_id, 'publish_posts' ) ) {
@ -505,57 +529,4 @@ class Activitypub {
$user->add_cap( 'activitypub' );
}
}
/**
* Add default extra fields to an actor.
*
* @param array $extra_fields The extra fields.
* @param int $user_id The User-ID.
*
* @return array The extra fields.
*/
public static function default_actor_extra_fields( $extra_fields, $user_id ) {
if ( $extra_fields || ! $user_id ) {
return $extra_fields;
}
$already_migrated = \get_user_meta( $user_id, 'activitypub_default_extra_fields', true );
if ( $already_migrated ) {
return $extra_fields;
}
$defaults = array(
\__( 'Blog', 'activitypub' ) => \home_url( '/' ),
\__( 'Profile', 'activitypub' ) => \get_author_posts_url( $user_id ),
\__( 'Homepage', 'activitypub' ) => \get_the_author_meta( 'user_url', $user_id ),
);
foreach ( $defaults as $title => $url ) {
if ( ! $url ) {
continue;
}
$extra_field = array(
'post_type' => 'ap_extrafield',
'post_title' => $title,
'post_status' => 'publish',
'post_author' => $user_id,
'post_content' => sprintf(
'<!-- wp:paragraph --><p><a rel="me" title="%s" target="_blank" href="%s">%s</a></p><!-- /wp:paragraph -->',
\esc_attr( $url ),
$url,
\wp_parse_url( $url, \PHP_URL_HOST )
),
'comment_status' => 'closed',
);
$extra_field_id = wp_insert_post( $extra_field );
$extra_fields[] = get_post( $extra_field_id );
}
\update_user_meta( $user_id, 'activitypub_default_extra_fields', true );
return $extra_fields;
}
}