diff --git a/includes/model/class-post.php b/includes/model/class-post.php index db56a45..2b6c84c 100644 --- a/includes/model/class-post.php +++ b/includes/model/class-post.php @@ -305,41 +305,31 @@ class Post { // get URLs for each image foreach ( $image_ids as $id ) { - $alt = \get_post_meta( $id, '_wp_attachment_image_alt', true ); + $image_size = 'full'; /** - * If you use the Jetpack plugin and its Image CDN, aka Photon, - * the image strings returned will use the Photon URL. - * We don't want that since Fediverse instances already do caching on their end. - * Let the CDN only be used for visitors of the site. + * Filter the image URL returned for each post. * - * Old versions of Jetpack used the Jetpack_Photon class to do this. - * New versions use the Image_CDN class. - * Let's handle both. + * @param array|false $thumbnail The image URL, or false if no image is available. + * @param int $id The attachment ID. + * @param string $image_size The image size to retrieve. Set to 'full' by default. */ - if ( \class_exists( '\Automattic\Jetpack\Image_CDN\Image_CDN' ) ) { - \remove_filter( 'image_downsize', array( \Automattic\Jetpack\Image_CDN\Image_CDN::instance(), 'filter_image_downsize' ) ); - } elseif ( \class_exists( 'Jetpack_Photon' ) ) { - \remove_filter( 'image_downsize', array( \Jetpack_Photon::instance(), 'filter_image_downsize' ) ); - } - - $thumbnail = \wp_get_attachment_image_src( $id, 'full' ); - - // Re-enable Photon now that the image URL has been built. - if ( \class_exists( '\Automattic\Jetpack\Image_CDN\Image_CDN' ) ) { - \add_filter( 'image_downsize', array( \Automattic\Jetpack\Image_CDN\Image_CDN::instance(), 'filter_image_downsize' ), 10, 3 ); - } elseif ( \class_exists( 'Jetpack_Photon' ) ) { - \add_filter( 'image_downsize', array( \Jetpack_Photon::instance(), 'filter_image_downsize' ), 10, 3 ); - } - - $mimetype = \get_post_mime_type( $id ); + $thumbnail = apply_filters( + 'activitypub_get_image', + $this->get_image( $id, $image_size ), + $id, + $image_size + ); if ( $thumbnail ) { - $image = array( - 'type' => 'Image', - 'url' => $thumbnail[0], + $mimetype = \get_post_mime_type( $id ); + $alt = \get_post_meta( $id, '_wp_attachment_image_alt', true ); + $image = array( + 'type' => 'Image', + 'url' => $thumbnail[0], 'mediaType' => $mimetype, ); + if ( $alt ) { $image['name'] = $alt; } @@ -352,6 +342,36 @@ class Post { return $images; } + /** + * Return details about an image attachment. + * + * @param int $id The attachment ID. + * @param string $image_size The image size to retrieve. Set to 'full' by default. + * + * @return array|false Array of image data, or boolean false if no image is available. + */ + public function get_image( $id, $image_size = 'full' ) { + /** + * Hook into the image retrieval process. Before image retrieval. + * + * @param int $id The attachment ID. + * @param string $image_size The image size to retrieve. Set to 'full' by default. + */ + do_action( 'activitypub_get_image_pre', $id, $image_size ); + + $thumbnail = \wp_get_attachment_image_src( $id, $image_size ); + + /** + * Hook into the image retrieval process. After image retrieval. + * + * @param int $id The attachment ID. + * @param string $image_size The image size to retrieve. Set to 'full' by default. + */ + do_action( 'activitypub_get_image_pre', $id, $image_size ); + + return $thumbnail; + } + /** * Returns a list of Tags, used in the Post * diff --git a/readme.txt b/readme.txt index 3f9f7f0..0004235 100644 --- a/readme.txt +++ b/readme.txt @@ -115,6 +115,7 @@ Project maintained on GitHub at [automattic/wordpress-activitypub](https://githu = Next = +* Compatibility: add hooks to allow modifying images returned in ActivityPub requests. * Compatibility: indicate that the plugin is compatible and has been tested with the latest version of WordPress, 6.2. = 0.17.0 =