updated plugin ActivityPub version 8.3.0
This commit is contained in:
@ -7,7 +7,12 @@
|
||||
|
||||
namespace Activitypub\Scheduler;
|
||||
|
||||
use Activitypub\Activity\Activity;
|
||||
use Activitypub\Collection\Actors;
|
||||
|
||||
use function Activitypub\add_to_outbox;
|
||||
use function Activitypub\get_content_visibility;
|
||||
use function Activitypub\get_post_id;
|
||||
use function Activitypub\get_wp_object_state;
|
||||
use function Activitypub\is_post_disabled;
|
||||
|
||||
@ -20,23 +25,36 @@ class Post {
|
||||
*/
|
||||
public static function init() {
|
||||
// Post transitions.
|
||||
\add_action( 'wp_after_insert_post', array( self::class, 'schedule_post_activity' ), 33, 4 );
|
||||
\add_action( 'wp_after_insert_post', array( self::class, 'triage' ), 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' ) );
|
||||
|
||||
/*
|
||||
* Sticky post transitions (featured collection).
|
||||
*
|
||||
* Note: These hooks run in addition to the legacy sticky hooks in
|
||||
* Actor scheduler, which send an Update activity when a post becomes
|
||||
* sticky or is unstuck. This means a sticky/unsticky event will cause both:
|
||||
* - an Add/Remove activity for the Actor's featured collection (below), and
|
||||
* - an Update activity (from Actor scheduler).
|
||||
* The Update activity is kept for backwards compatibility.
|
||||
*/
|
||||
\add_action( 'post_stuck', array( self::class, 'schedule_featured_add' ) );
|
||||
\add_action( 'post_unstuck', array( self::class, 'schedule_featured_remove' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle post updates and determine the appropriate Activity type.
|
||||
* Triage post transitions 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 ) {
|
||||
public static function triage( $post_id, $post, $update, $post_before ) {
|
||||
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
|
||||
return;
|
||||
}
|
||||
@ -45,8 +63,18 @@ class Post {
|
||||
return;
|
||||
}
|
||||
|
||||
$object_status = get_wp_object_state( $post );
|
||||
|
||||
// If the post is already soft-deleted, do not create any more activities.
|
||||
if (
|
||||
ACTIVITYPUB_OBJECT_STATE_DELETED === $object_status &&
|
||||
in_array( get_content_visibility( $post ), array( ACTIVITYPUB_CONTENT_VISIBILITY_LOCAL, ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE ), true )
|
||||
) {
|
||||
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
|
||||
if ( isset( $_REQUEST['bulk_edit'] ) && ( ! isset( $_REQUEST['post_author'] ) || -1 === (int) $_REQUEST['post_author'] ) && -1 === (int) $_REQUEST['_status'] ) { // phpcs:ignore WordPress
|
||||
return;
|
||||
}
|
||||
|
||||
@ -55,15 +83,20 @@ class Post {
|
||||
|
||||
switch ( $new_status ) {
|
||||
case 'publish':
|
||||
$type = ( 'publish' === $old_status ) ? 'Update' : 'Create';
|
||||
if ( $update ) {
|
||||
$type = ( 'publish' === $old_status ) ? 'Update' : 'Create';
|
||||
} else {
|
||||
$type = 'Create';
|
||||
}
|
||||
break;
|
||||
|
||||
case 'draft':
|
||||
case 'pending':
|
||||
$type = ( 'publish' === $old_status ) ? 'Update' : false;
|
||||
break;
|
||||
|
||||
case 'trash':
|
||||
$type = 'federated' === get_wp_object_state( $post ) ? 'Delete' : false;
|
||||
$type = ACTIVITYPUB_OBJECT_STATE_FEDERATED === $object_status ? 'Delete' : false;
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -75,7 +108,24 @@ class Post {
|
||||
return;
|
||||
}
|
||||
|
||||
// Add the post to the outbox.
|
||||
/*
|
||||
* If the post was already federated and this is a Create, skip.
|
||||
* The outbox controller already added it to the outbox.
|
||||
*/
|
||||
if ( ACTIVITYPUB_OBJECT_STATE_FEDERATED === $object_status && 'Create' === $type ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If the post was never federated before, it should be a Create activity.
|
||||
if ( empty( $object_status ) && 'Update' === $type ) {
|
||||
$type = 'Create';
|
||||
}
|
||||
|
||||
// If the post was federated before but is now local or private, it should be a Delete activity.
|
||||
if ( ACTIVITYPUB_OBJECT_STATE_FEDERATED === $object_status && in_array( get_content_visibility( $post ), array( ACTIVITYPUB_CONTENT_VISIBILITY_LOCAL, ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE ), true ) ) {
|
||||
$type = 'Delete';
|
||||
}
|
||||
|
||||
add_to_outbox( $post, $type, $post->post_author );
|
||||
}
|
||||
|
||||
@ -93,21 +143,89 @@ class Post {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( is_post_disabled( $post_id ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$post = \get_post( $post_id );
|
||||
|
||||
if ( ! $post instanceof \WP_Post ) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch ( \current_action() ) {
|
||||
case 'add_attachment':
|
||||
// Add the post to the outbox.
|
||||
add_to_outbox( $post, 'Create', $post->post_author );
|
||||
$type = 'Create';
|
||||
break;
|
||||
case 'edit_attachment':
|
||||
// Update the post to the outbox.
|
||||
add_to_outbox( $post, 'Update', $post->post_author );
|
||||
$type = 'Update';
|
||||
break;
|
||||
case 'delete_attachment':
|
||||
// Delete the post from the outbox.
|
||||
add_to_outbox( $post, 'Delete', $post->post_author );
|
||||
$type = 'Delete';
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
add_to_outbox( $post, $type, $post->post_author );
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule an Add activity when a post is added to the featured collection.
|
||||
*
|
||||
* @param int $post_id The post ID.
|
||||
*/
|
||||
public static function schedule_featured_add( $post_id ) {
|
||||
self::schedule_featured_update( $post_id, 'Add' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule a Remove activity when a post is removed from the featured collection.
|
||||
*
|
||||
* @param int $post_id The post ID.
|
||||
*/
|
||||
public static function schedule_featured_remove( $post_id ) {
|
||||
self::schedule_featured_update( $post_id, 'Remove' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule an Add or Remove activity for the featured collection.
|
||||
*
|
||||
* When a post's sticky status changes, this sends an Add or Remove activity
|
||||
* to notify followers about the change to the actor's featured collection.
|
||||
*
|
||||
* @see https://github.com/Automattic/wordpress-activitypub/issues/2795
|
||||
*
|
||||
* @param int $post_id The post ID.
|
||||
* @param string $activity_type The activity type ('Add' or 'Remove').
|
||||
*/
|
||||
private static function schedule_featured_update( $post_id, $activity_type ) {
|
||||
if ( \defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$post = \get_post( $post_id );
|
||||
|
||||
if ( ! $post ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( is_post_disabled( $post ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$actor = Actors::get_by_id( $post->post_author );
|
||||
|
||||
if ( ! $actor || \is_wp_error( $actor ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$activity = new Activity();
|
||||
$activity->set_type( $activity_type );
|
||||
$activity->set_actor( $actor->get_id() );
|
||||
$activity->set_object( get_post_id( $post->ID ) );
|
||||
$activity->set_target( $actor->get_featured() );
|
||||
|
||||
add_to_outbox( $activity, null, $post->post_author );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user