Merge pull request #307 from jeherve/fix/photon-images-jetpack-compatibility

Compatibility: do not serve images with Jetpack CDN when active
This commit is contained in:
Matthias Pfefferle 2023-04-24 14:00:48 +02:00 committed by GitHub
commit 475f4aaea0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -306,7 +306,32 @@ 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.
*
* 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( '\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 );
if ( $thumbnail ) {