99use function Activitypub \esc_hashtag ;
1010use function Activitypub \is_single_user ;
1111use function Activitypub \get_rest_url_by_path ;
12+ use function Activitypub \site_supports_blocks ;
1213
1314/**
1415 * WordPress Post Transformer
@@ -166,6 +167,66 @@ protected function get_attributed_to() {
166167 return Users::get_by_id ( $ this ->wp_post ->post_author )->get_url ();
167168 }
168169
170+ /**
171+ * Returns the Image Attachments for this Post, parsed from blocks.
172+ * @param int $max_images The maximum number of images to return.
173+ * @param array $image_ids The image IDs to append new IDs to.
174+ *
175+ * @return array The image IDs.
176+ */
177+ protected function get_block_image_ids ( $ max_images , $ image_ids = [] ) {
178+ $ blocks = \parse_blocks ( $ this ->wp_post ->post_content );
179+ return self ::get_image_ids_from_blocks ( $ blocks , $ image_ids , $ max_images );
180+ }
181+
182+ /**
183+ * Recursively get image IDs from blocks.
184+ * @param array $blocks The blocks to search for image IDs
185+ * @param array $image_ids The image IDs to append new IDs to
186+ * @param int $max_images The maximum number of images to return.
187+ *
188+ * @return array The image IDs.
189+ */
190+ protected static function get_image_ids_from_blocks ( $ blocks , $ image_ids , $ max_images ) {
191+ foreach ( $ blocks as $ block ) {
192+ // recurse into inner blocks
193+ if ( ! empty ( $ block ['innerBlocks ' ] ) ) {
194+ $ image_ids = self ::get_image_ids_from_blocks ( $ block ['innerBlocks ' ], $ image_ids , $ max_images );
195+ }
196+
197+ switch ( $ block ['blockName ' ] ) {
198+ case 'core/image ' :
199+ case 'core/cover ' :
200+ if ( ! empty ( $ block ['attrs ' ]['id ' ] ) ) {
201+ $ image_ids [] = $ block ['attrs ' ]['id ' ];
202+ }
203+ break ;
204+ case 'jetpack/slideshow ' :
205+ case 'jetpack/tiled-gallery ' :
206+ if ( ! empty ( $ block ['attrs ' ]['ids ' ] ) ) {
207+ $ image_ids = array_merge ( $ image_ids , $ block ['attrs ' ]['ids ' ] );
208+ }
209+ break ;
210+ case 'jetpack/image-compare ' :
211+ if ( ! empty ( $ block ['attrs ' ]['beforeImageId ' ] ) ) {
212+ $ image_ids [] = $ block ['attrs ' ]['beforeImageId ' ];
213+ }
214+ if ( ! empty ( $ block ['attrs ' ]['afterImageId ' ] ) ) {
215+ $ image_ids [] = $ block ['attrs ' ]['afterImageId ' ];
216+ }
217+ break ;
218+ }
219+
220+ // we could be at or over max, stop unneeded work
221+ if ( count ( $ image_ids ) >= $ max_images ) {
222+ break ;
223+ }
224+ }
225+
226+ // still need to slice it because one gallery could knock us over the limit
227+ return \array_slice ( $ image_ids , 0 , $ max_images );
228+ }
229+
169230 /**
170231 * Generates all Image Attachments for a Post.
171232 *
@@ -192,21 +253,26 @@ protected function get_attachments() {
192253 }
193254
194255 if ( $ max_images > 0 ) {
195- // then list any image attachments
196- $ query = new \WP_Query (
197- array (
198- 'post_parent ' => $ id ,
199- 'post_status ' => 'inherit ' ,
200- 'post_type ' => 'attachment ' ,
201- 'post_mime_type ' => 'image ' ,
202- 'order ' => 'ASC ' ,
203- 'orderby ' => 'menu_order ID ' ,
204- 'posts_per_page ' => $ max_images ,
205- )
206- );
207- foreach ( $ query ->get_posts () as $ attachment ) {
208- if ( ! \in_array ( $ attachment ->ID , $ image_ids , true ) ) {
209- $ image_ids [] = $ attachment ->ID ;
256+ // first try to get images that are actually in the post content
257+ if ( site_supports_blocks () && \has_blocks ( $ this ->wp_post ->post_content ) ) {
258+ $ image_ids = $ this ->get_block_image_ids ( $ max_images , $ image_ids );
259+ } else {
260+ // fallback to images attached to the post
261+ $ query = new \WP_Query (
262+ array (
263+ 'post_parent ' => $ id ,
264+ 'post_status ' => 'inherit ' ,
265+ 'post_type ' => 'attachment ' ,
266+ 'post_mime_type ' => 'image ' ,
267+ 'order ' => 'ASC ' ,
268+ 'orderby ' => 'menu_order ID ' ,
269+ 'posts_per_page ' => $ max_images ,
270+ )
271+ );
272+ foreach ( $ query ->get_posts () as $ attachment ) {
273+ if ( ! \in_array ( $ attachment ->ID , $ image_ids , true ) ) {
274+ $ image_ids [] = $ attachment ->ID ;
275+ }
210276 }
211277 }
212278 }
0 commit comments