@@ -65,11 +65,7 @@ public static function add( $activity, $recipients ) {
6565 }
6666
6767 self ::add_taxonomies ( $ post_id , $ activity_object );
68-
69- // Process attachments if present.
70- if ( ! empty ( $ activity_object ['attachment ' ] ) ) {
71- Attachments::import_post_files ( $ activity_object ['attachment ' ], $ post_id );
72- }
68+ self ::maybe_import_attachments ( $ activity_object , $ post_id );
7369
7470 return \get_post ( $ post_id );
7571 }
@@ -145,11 +141,9 @@ public static function update( $activity, $recipients ) {
145141
146142 self ::add_taxonomies ( $ post_id , $ activity ['object ' ] );
147143
148- // Process attachments if present.
149- if ( ! empty ( $ activity ['object ' ]['attachment ' ] ) ) {
150- Attachments::delete_ap_posts_directory ( $ post_id );
151- Attachments::import_post_files ( $ activity ['object ' ]['attachment ' ], $ post_id );
152- }
144+ // Always delete existing attachments on update in case filter value changed.
145+ Attachments::delete_ap_posts_directory ( $ post_id );
146+ self ::maybe_import_attachments ( $ activity ['object ' ], $ post_id );
153147
154148 return \get_post ( $ post_id );
155149 }
@@ -227,6 +221,38 @@ private static function add_taxonomies( $post_id, $activity_object ) {
227221 \wp_set_post_terms ( $ post_id , $ tags , 'ap_tag ' );
228222 }
229223
224+ /**
225+ * Maybe import attachments for an activity object.
226+ *
227+ * Checks if attachments should be stored locally via filter and imports them if enabled.
228+ *
229+ * @param array $activity_object The activity object data.
230+ * @param int $post_id The post ID.
231+ */
232+ private static function maybe_import_attachments ( $ activity_object , $ post_id ) {
233+ // Process attachments if present.
234+ if ( empty ( $ activity_object ['attachment ' ] ) ) {
235+ return ;
236+ }
237+
238+ /**
239+ * Filters whether to store attachments locally for incoming ActivityPub posts.
240+ *
241+ * Allows plugins or users to disable local storage of attachments from
242+ * incoming ActivityPub posts. When disabled, attachments won't be downloaded
243+ * and stored locally, which can be useful for users with limited webspace.
244+ *
245+ * @param bool $store_locally Whether to store attachments locally. Default true.
246+ * @param array $activity_object The ActivityPub activity object.
247+ * @param int $post_id The post ID.
248+ */
249+ $ store_locally = \apply_filters ( 'activitypub_store_attachments_locally ' , true , $ activity_object , $ post_id );
250+
251+ if ( $ store_locally ) {
252+ Attachments::import_post_files ( $ activity_object ['attachment ' ], $ post_id );
253+ }
254+ }
255+
230256 /**
231257 * Get posts by remote actor.
232258 *
0 commit comments