Skip to content

Commit 1a8c7fa

Browse files
authored
Add media attachment filter and integration tests (#2387)
1 parent 17cc80c commit 1a8c7fa

File tree

6 files changed

+519
-0
lines changed

6 files changed

+519
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: changed
3+
4+
Improved Classic Editor integration by adding better media handling and full test coverage for attachments, permissions, and metadata.

integration/class-classic-editor.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Classic_Editor {
1919
*/
2020
public static function init() {
2121
\add_filter( 'activitypub_attachments_media_markup', array( self::class, 'filter_attachments_media_markup' ), 10, 2 );
22+
\add_filter( 'activitypub_attachment_ids', array( self::class, 'filter_attached_media_ids' ), 10, 2 );
2223
\add_action( 'add_meta_boxes', array( self::class, 'add_meta_box' ) );
2324
\add_action( 'save_post', array( self::class, 'save_meta_data' ) );
2425
}
@@ -51,6 +52,51 @@ public static function filter_attachments_media_markup( $markup, $attachment_ids
5152
return '[gallery ids="' . implode( ',', $attachment_ids ) . '" link="none"]';
5253
}
5354

55+
/**
56+
* Filter to add attached media IDs from the post's media library.
57+
*
58+
* Returns additional image attachments from the post's media library,
59+
* respecting the maximum image attachment limit. Only returns new
60+
* attachments that can be added without exceeding the limit.
61+
*
62+
* @param array $attachments The current list of attachments.
63+
* @param \WP_Post $item The post item.
64+
*
65+
* @return array Array of new media IDs to add (as associative arrays with 'id' key).
66+
* Returns empty array if already at or over the limit.
67+
*/
68+
public static function filter_attached_media_ids( $attachments, $item ) {
69+
$max_media = \get_option( 'activitypub_max_image_attachments', \ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS );
70+
$actual_count = \max( 0, $max_media - \count( $attachments ) );
71+
72+
if ( $actual_count <= 0 ) {
73+
return $attachments;
74+
}
75+
76+
$query = new \WP_Query(
77+
array(
78+
'post_parent' => $item->ID,
79+
'post_status' => 'inherit',
80+
'post_type' => 'attachment',
81+
'post_mime_type' => 'image',
82+
'order' => 'ASC',
83+
'orderby' => 'menu_order ID',
84+
'fields' => 'ids',
85+
'posts_per_page' => $actual_count,
86+
)
87+
);
88+
89+
// Transform IDs into associative arrays.
90+
$media_ids = \array_map(
91+
function ( $id ) {
92+
return array( 'id' => $id );
93+
},
94+
$query->get_posts()
95+
);
96+
97+
return \array_merge( $attachments, $media_ids );
98+
}
99+
54100
/**
55101
* Add ActivityPub meta box to the post editor.
56102
*
8.38 KB
Binary file not shown.
3.49 KB
Loading
14.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)