Switch to general actions and filter

As a result, we will not modify the images within the ActivityPub plugin, but the hooks will allow third-parties to do it on their end.

See discussion: https://github.com/pfefferle/wordpress-activitypub/pull/309#issuecomment-1521488186
This commit is contained in:
Jeremy Herve 2023-04-26 10:45:35 +02:00
parent 55de37b05d
commit e16e119e6c
No known key found for this signature in database
GPG key ID: EF65CC7D9AC11920
2 changed files with 30 additions and 30 deletions

View file

@ -305,7 +305,22 @@ class Post {
// get URLs for each image // get URLs for each image
foreach ( $image_ids as $id ) { foreach ( $image_ids as $id ) {
$thumbnail = $this->get_image( $id ); $image_size = 'full';
/**
* Filter the image URL returned for each post.
*
* @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.
*/
$thumbnail = apply_filters(
'activitypub_get_image',
$this->get_image( $id, $image_size ),
$id,
$image_size
);
if ( $thumbnail ) { if ( $thumbnail ) {
$mimetype = \get_post_mime_type( $id ); $mimetype = \get_post_mime_type( $id );
$alt = \get_post_meta( $id, '_wp_attachment_image_alt', true ); $alt = \get_post_meta( $id, '_wp_attachment_image_alt', true );
@ -333,44 +348,29 @@ class Post {
* Can return a CDNized URL if Jetpack's image CDN is active. * Can return a CDNized URL if Jetpack's image CDN is active.
* This can be disabled with a filter. * This can be disabled with a filter.
* *
* @param int $id The attachment ID. * @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. * @return array|false Array of image data, or boolean false if no image is available.
*/ */
public function get_image( $id ) { public function get_image( $id, $image_size = 'full' ) {
/** /**
* Allow bypassing Jetpack's Image CDN when returning image URLs. * Hook into the image retrieval process. Before image retrieval.
* *
* @param bool $should_use_cdn Whether to use the Jetpack Image CDN. True by default. * @param int $id The attachment ID.
* @param string $image_size The image size to retrieve. Set to 'full' by default.
*/ */
$should_use_cdn = apply_filters( 'activitypub_images_use_jetpack_image_cdn', true ); do_action( 'activitypub_get_image_pre', $id, $image_size );
if ( $should_use_cdn ) { $thumbnail = \wp_get_attachment_image_src( $id, $image_size );
// Return the full URL, using a CDN URL if Jetpack's image CDN is active.
return \wp_get_attachment_image_src( $id, 'full' );
}
/* /**
* Disable Jetpacks image CDN image processing for this request. * Hook into the image retrieval process. After image retrieval.
* *
* Note: old versions of Jetpack used the Jetpack_Photon class to do this. * @param int $id The attachment ID.
* New versions use the Image_CDN class. * @param string $image_size The image size to retrieve. Set to 'full' by default.
* Let's handle both.
*/ */
if ( \class_exists( '\Automattic\Jetpack\Image_CDN\Image_CDN' ) ) { do_action( 'activitypub_get_image_pre', $id, $image_size );
\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 );
}
return $thumbnail; return $thumbnail;
} }

View file

@ -115,7 +115,7 @@ Project maintained on GitHub at [pfefferle/wordpress-activitypub](https://github
= Next = = Next =
* Compatibility: add filter to allow disabling Jetpack's image CDN when returning images in ActivityPub requests. * 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. * Compatibility: indicate that the plugin is compatible and has been tested with the latest version of WordPress, 6.2.
= 0.17.0 = = 0.17.0 =