Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions includes/collection/class-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ public static function add( $activity, $recipients ) {
}

self::add_taxonomies( $post_id, $activity_object );

// Process attachments if present.
if ( ! empty( $activity_object['attachment'] ) ) {
Attachments::import_post_files( $activity_object['attachment'], $post_id );
}
self::maybe_import_attachments( $activity_object, $post_id );

return \get_post( $post_id );
}
Expand Down Expand Up @@ -145,11 +141,9 @@ public static function update( $activity, $recipients ) {

self::add_taxonomies( $post_id, $activity['object'] );

// Process attachments if present.
if ( ! empty( $activity['object']['attachment'] ) ) {
Attachments::delete_ap_posts_directory( $post_id );
Attachments::import_post_files( $activity['object']['attachment'], $post_id );
}
// Always delete existing attachments on update in case filter value changed.
Attachments::delete_ap_posts_directory( $post_id );
self::maybe_import_attachments( $activity['object'], $post_id );

return \get_post( $post_id );
}
Expand Down Expand Up @@ -227,6 +221,38 @@ private static function add_taxonomies( $post_id, $activity_object ) {
\wp_set_post_terms( $post_id, $tags, 'ap_tag' );
}

/**
* Maybe import attachments for an activity object.
*
* Checks if attachments should be stored locally via filter and imports them if enabled.
*
* @param array $activity_object The activity object data.
* @param int $post_id The post ID.
*/
private static function maybe_import_attachments( $activity_object, $post_id ) {
// Process attachments if present.
if ( empty( $activity_object['attachment'] ) ) {
return;
}

/**
* Filters whether to store attachments locally for incoming ActivityPub posts.
*
* Allows plugins or users to disable local storage of attachments from
* incoming ActivityPub posts. When disabled, attachments won't be downloaded
* and stored locally, which can be useful for users with limited webspace.
*
* @param bool $store_locally Whether to store attachments locally. Default true.
* @param array $activity_object The ActivityPub activity object.
* @param int $post_id The post ID.
*/
$store_locally = \apply_filters( 'activitypub_store_attachments_locally', true, $activity_object, $post_id );

if ( $store_locally ) {
Attachments::import_post_files( $activity_object['attachment'], $post_id );
}
}

/**
* Get posts by remote actor.
*
Expand Down