From 3f4c44db05e7a6b8df24f7891a177b865972e4d8 Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Mon, 24 Apr 2023 09:15:48 +0200 Subject: [PATCH 1/2] Compatibility: do not serve images with Jetpack CDN when active When Jetpack's image CDN is active, core calls to retrieve images return an image served by the CDN. Since Fediverse instances usually fetch and cache the data themselves, we do not need to use the CDN for those images when returned by the ActivityPub plugin. In fact, we really do not want that to happen, as Fediverse instances may get errors when trying to fetch images from the CDN (they may get blocked / rate-limited / ...). Let's hook into Jetpack's CDN to avoid that. --- includes/model/class-post.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/includes/model/class-post.php b/includes/model/class-post.php index 470ad70..6f33cc3 100644 --- a/includes/model/class-post.php +++ b/includes/model/class-post.php @@ -306,7 +306,24 @@ class Post { // get URLs for each image foreach ( $image_ids as $id ) { $alt = \get_post_meta( $id, '_wp_attachment_image_alt', true ); + + /** + * 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. + */ + if ( 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( 'Jetpack_Photon' ) ) { + \add_filter( 'image_downsize', array( \Jetpack_Photon::instance(), 'filter_image_downsize' ), 10, 3 ); + } + $mimetype = \get_post_mime_type( $id ); if ( $thumbnail ) { From 56d2b7e8be6b6765138dbd6a4170a056c7e95794 Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Mon, 24 Apr 2023 09:49:05 +0200 Subject: [PATCH 2/2] Update to handle both old and new versions of Jetpack See https://github.com/Automattic/jetpack/pull/30050/ --- includes/model/class-post.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/includes/model/class-post.php b/includes/model/class-post.php index 6f33cc3..a69e139 100644 --- a/includes/model/class-post.php +++ b/includes/model/class-post.php @@ -312,15 +312,23 @@ class Post { * 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. + * + * Old versions of Jetpack used the Jetpack_Photon class to do this. + * New versions use the Image_CDN class. + * Let's handle both. */ - if ( class_exists( 'Jetpack_Photon' ) ) { + 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( 'Jetpack_Photon' ) ) { + 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 ); }