updated plugin ActivityPub version 5.8.0

This commit is contained in:
2025-04-29 21:19:06 +00:00
committed by Gitium
parent 19dfd317cc
commit fdfbf76539
166 changed files with 14119 additions and 7163 deletions

View File

@ -0,0 +1,146 @@
<?php
/**
* Actor scheduler class file.
*
* @package Activitypub
*/
namespace Activitypub\Scheduler;
use Activitypub\Collection\Actors;
use Activitypub\Collection\Extra_Fields;
use function Activitypub\add_to_outbox;
use function Activitypub\is_user_type_disabled;
/**
* Post scheduler class.
*/
class Actor {
/**
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
// Profile updates for blog options.
if ( ! is_user_type_disabled( 'blog' ) ) {
\add_action( 'update_option_site_icon', array( self::class, 'blog_user_update' ) );
\add_action( 'update_option_blogdescription', array( self::class, 'blog_user_update' ) );
\add_action( 'update_option_blogname', array( self::class, 'blog_user_update' ) );
\add_action( 'add_option_activitypub_header_image', array( self::class, 'blog_user_update' ) );
\add_action( 'update_option_activitypub_header_image', array( self::class, 'blog_user_update' ) );
\add_action( 'add_option_activitypub_blog_identifier', array( self::class, 'blog_user_update' ) );
\add_action( 'update_option_activitypub_blog_identifier', array( self::class, 'blog_user_update' ) );
\add_action( 'add_option_activitypub_blog_description', array( self::class, 'blog_user_update' ) );
\add_action( 'update_option_activitypub_blog_description', array( self::class, 'blog_user_update' ) );
\add_filter( 'pre_set_theme_mod_custom_logo', array( self::class, 'blog_user_update' ) );
\add_filter( 'pre_set_theme_mod_header_image', array( self::class, 'blog_user_update' ) );
}
// Profile updates for user options.
if ( ! is_user_type_disabled( 'user' ) ) {
\add_action( 'profile_update', array( self::class, 'user_update' ) );
\add_action( 'added_user_meta', array( self::class, 'user_meta_update' ), 10, 3 );
\add_action( 'updated_user_meta', array( self::class, 'user_meta_update' ), 10, 3 );
// @todo figure out a feasible way of updating the header image since it's not unique to any user.
}
\add_action( 'add_option_activitypub_actor_mode', array( self::class, 'blog_user_update' ) );
\add_action( 'update_option_activitypub_actor_mode', array( self::class, 'blog_user_update' ) );
\add_action( 'transition_post_status', array( self::class, 'schedule_post_activity' ), 33, 3 );
}
/**
* Send a profile update when relevant user meta is updated.
*
* @param int $meta_id Meta ID being updated.
* @param int $user_id User ID being updated.
* @param string $meta_key Meta key being updated.
*/
public static function user_meta_update( $meta_id, $user_id, $meta_key ) {
// Don't bother if the user can't publish.
if ( ! \user_can( $user_id, 'activitypub' ) ) {
return;
}
$blog_prefix = $GLOBALS['wpdb']->get_blog_prefix();
// The user meta fields that affect a profile.
$fields = array(
$blog_prefix . 'activitypub_description',
$blog_prefix . 'activitypub_header_image',
$blog_prefix . 'activitypub_icon',
'description',
'display_name',
'user_url',
);
if ( in_array( $meta_key, $fields, true ) ) {
self::schedule_profile_update( $user_id );
}
}
/**
* Send a profile update when a user is updated.
*
* @param int $user_id User ID being updated.
*/
public static function user_update( $user_id ) {
// Don't bother if the user can't publish.
if ( ! \user_can( $user_id, 'activitypub' ) ) {
return;
}
self::schedule_profile_update( $user_id );
}
/**
* Theme mods only have a dynamic filter so we fudge it like this.
*
* @param mixed $value Optional. The value to be updated. Default null.
*
* @return mixed
*/
public static function blog_user_update( $value = null ) {
self::schedule_profile_update( Actors::BLOG_USER_ID );
return $value;
}
/**
* Schedule Activities.
*
* @param string $new_status New post status.
* @param string $old_status Old post status.
* @param \WP_Post $post Post object.
*/
public static function schedule_post_activity( $new_status, $old_status, $post ) {
if ( $post instanceof \WP_Post ) {
if ( Extra_Fields::USER_POST_TYPE === $post->post_type ) {
self::schedule_profile_update( $post->post_author );
} elseif ( Extra_Fields::BLOG_POST_TYPE === $post->post_type ) {
self::schedule_profile_update( Actors::BLOG_USER_ID );
}
}
}
/**
* Send a profile update to all followers. Gets hooked into all relevant options/meta etc.
*
* @param int $user_id The user ID to update (Could be 0 for Blog-User).
*/
public static function schedule_profile_update( $user_id ) {
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
return;
}
$actor = Actors::get_by_id( $user_id );
if ( ! $actor || \is_wp_error( $actor ) ) {
return;
}
$actor->set_updated( gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, time() ) );
add_to_outbox( $actor, 'Update', $user_id );
}
}

View File

@ -0,0 +1,91 @@
<?php
/**
* Comment scheduler class file.
*
* @package Activitypub
*/
namespace Activitypub\Scheduler;
use function Activitypub\add_to_outbox;
use function Activitypub\should_comment_be_federated;
/**
* Post scheduler class.
*/
class Comment {
/**
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
if ( ACTIVITYPUB_DISABLE_OUTGOING_INTERACTIONS ) {
return;
}
// Comment transitions.
\add_action( 'transition_comment_status', array( self::class, 'schedule_comment_activity' ), 20, 3 );
\add_action( 'wp_insert_comment', array( self::class, 'schedule_comment_activity_on_insert' ), 10, 2 );
}
/**
* Schedule Comment Activities.
*
* @see transition_comment_status()
*
* @param string $new_status New comment status.
* @param string $old_status Old comment status.
* @param \WP_Comment $comment Comment object.
*/
public static function schedule_comment_activity( $new_status, $old_status, $comment ) {
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
return;
}
$comment = get_comment( $comment );
// Federate only comments that are written by a registered user.
if ( ! $comment || ! $comment->user_id ) {
return;
}
$type = false;
if (
'approved' === $new_status &&
'approved' !== $old_status
) {
$type = 'Create';
} elseif ( 'approved' === $new_status ) {
$type = 'Update';
\update_comment_meta( $comment->comment_ID, 'activitypub_comment_modified', time(), true );
} elseif (
'trash' === $new_status ||
'spam' === $new_status
) {
$type = 'Delete';
}
if ( empty( $type ) ) {
return;
}
// Check if comment should be federated or not.
if ( ! should_comment_be_federated( $comment ) ) {
return;
}
add_to_outbox( $comment, $type, $comment->user_id );
}
/**
* Schedule Comment Activities on insert.
*
* @param int $comment_id Comment ID.
* @param \WP_Comment $comment Comment object.
*/
public static function schedule_comment_activity_on_insert( $comment_id, $comment ) {
if ( 1 === (int) $comment->comment_approved ) {
self::schedule_comment_activity( 'approved', '', $comment );
}
}
}

View File

@ -0,0 +1,113 @@
<?php
/**
* Post scheduler class file.
*
* @package Activitypub
*/
namespace Activitypub\Scheduler;
use function Activitypub\add_to_outbox;
use function Activitypub\get_wp_object_state;
use function Activitypub\is_post_disabled;
/**
* Post scheduler class.
*/
class Post {
/**
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
// Post transitions.
\add_action( 'wp_after_insert_post', array( self::class, 'schedule_post_activity' ), 33, 4 );
// Attachment transitions.
\add_action( 'add_attachment', array( self::class, 'transition_attachment_status' ) );
\add_action( 'edit_attachment', array( self::class, 'transition_attachment_status' ) );
\add_action( 'delete_attachment', array( self::class, 'transition_attachment_status' ) );
}
/**
* Handle post updates and determine the appropriate Activity type.
*
* @param int $post_id Post ID.
* @param \WP_Post $post Post object.
* @param bool $update Whether this is an existing post being updated.
* @param \WP_Post $post_before Post object before the update.
*/
public static function schedule_post_activity( $post_id, $post, $update, $post_before ) {
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
return;
}
if ( is_post_disabled( $post ) ) {
return;
}
// Bail on bulk edits, unless post author or post status changed.
if ( isset( $_REQUEST['bulk_edit'] ) && -1 === (int) $_REQUEST['post_author'] && -1 === (int) $_REQUEST['_status'] ) { // phpcs:ignore WordPress
return;
}
$new_status = get_post_status( $post );
$old_status = $post_before ? get_post_status( $post_before ) : null;
switch ( $new_status ) {
case 'publish':
$type = ( 'publish' === $old_status ) ? 'Update' : 'Create';
break;
case 'draft':
$type = ( 'publish' === $old_status ) ? 'Update' : false;
break;
case 'trash':
$type = 'federated' === get_wp_object_state( $post ) ? 'Delete' : false;
break;
default:
$type = false;
}
// Do not send Activities if `$type` is not set or unknown.
if ( empty( $type ) ) {
return;
}
// Add the post to the outbox.
add_to_outbox( $post, $type, $post->post_author );
}
/**
* Schedules Activities for attachment transitions.
*
* @param int $post_id Attachment ID.
*/
public static function transition_attachment_status( $post_id ) {
if ( \defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
return;
}
if ( ! \post_type_supports( 'attachment', 'activitypub' ) ) {
return;
}
$post = \get_post( $post_id );
switch ( \current_action() ) {
case 'add_attachment':
// Add the post to the outbox.
add_to_outbox( $post, 'Create', $post->post_author );
break;
case 'edit_attachment':
// Update the post to the outbox.
add_to_outbox( $post, 'Update', $post->post_author );
break;
case 'delete_attachment':
// Delete the post from the outbox.
add_to_outbox( $post, 'Delete', $post->post_author );
break;
}
}
}