Compat: only disable Jetpack's image CDN via filter
This follows the discussion in #307. 1. Do not disable Jetpack's image CDN in ActivityPub requests by default. 2. Add a new filter, activitypub_images_use_jetpack_image_cdn, that site owners can use to disable Jetpack's Image CDN if they'd like to. 3. Extract image getting into its own method for improved readability.
This commit is contained in:
parent
475f4aaea0
commit
da63763ddc
1 changed files with 55 additions and 32 deletions
|
@ -305,15 +305,55 @@ 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 );
|
||||||
|
if ( $thumbnail ) {
|
||||||
|
$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 );
|
||||||
|
$image = array(
|
||||||
|
'type' => 'Image',
|
||||||
|
'url' => $thumbnail[0],
|
||||||
|
'mediaType' => $mimetype,
|
||||||
|
);
|
||||||
|
|
||||||
|
if ( $alt ) {
|
||||||
|
$image['name'] = $alt;
|
||||||
|
}
|
||||||
|
$images[] = $image;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->attachments = $images;
|
||||||
|
|
||||||
|
return $images;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If you use the Jetpack plugin and its Image CDN, aka Photon,
|
* Return details about an image attachment.
|
||||||
* 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.
|
* Can return a CDNized URL if Jetpack's image CDN is active.
|
||||||
|
* This can be disabled with a filter.
|
||||||
|
*
|
||||||
|
* @param int $id The attachment ID.
|
||||||
|
*
|
||||||
|
* @return array|false Array of image data, or boolean false if no image is available.
|
||||||
|
*/
|
||||||
|
public function get_image( $id ) {
|
||||||
|
/**
|
||||||
|
* Allow bypassing Jetpack's Image CDN when returning image URLs.
|
||||||
|
*
|
||||||
|
* @param bool $should_use_cdn Whether to use the Jetpack Image CDN. True by default.
|
||||||
|
*/
|
||||||
|
$should_use_cdn = apply_filters( 'activitypub_images_use_jetpack_image_cdn', true );
|
||||||
|
|
||||||
|
if ( $should_use_cdn ) {
|
||||||
|
// 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.
|
||||||
|
*
|
||||||
|
* Note: old versions of Jetpack used the Jetpack_Photon class to do this.
|
||||||
* New versions use the Image_CDN class.
|
* New versions use the Image_CDN class.
|
||||||
* Let's handle both.
|
* Let's handle both.
|
||||||
*/
|
*/
|
||||||
|
@ -332,24 +372,7 @@ class Post {
|
||||||
\add_filter( 'image_downsize', array( \Jetpack_Photon::instance(), 'filter_image_downsize' ), 10, 3 );
|
\add_filter( 'image_downsize', array( \Jetpack_Photon::instance(), 'filter_image_downsize' ), 10, 3 );
|
||||||
}
|
}
|
||||||
|
|
||||||
$mimetype = \get_post_mime_type( $id );
|
return $thumbnail;
|
||||||
|
|
||||||
if ( $thumbnail ) {
|
|
||||||
$image = array(
|
|
||||||
'type' => 'Image',
|
|
||||||
'url' => $thumbnail[0],
|
|
||||||
'mediaType' => $mimetype,
|
|
||||||
);
|
|
||||||
if ( $alt ) {
|
|
||||||
$image['name'] = $alt;
|
|
||||||
}
|
|
||||||
$images[] = $image;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->attachments = $images;
|
|
||||||
|
|
||||||
return $images;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue