Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Allow Jetpack CDN to Serve Images #1203

Open
wants to merge 9 commits into
base: release/2.7.0
Choose a base branch
from
7 changes: 6 additions & 1 deletion admin/apple-actions/index/class-export.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,18 @@ public function fetch_exporter() {
if ( empty( $cover_caption ) ) {
$cover_caption = wp_get_attachment_caption( $cover_meta_id );
}
$image_url = wp_get_attachment_image_url( $cover_meta_id, 'full' );
// If Jetpack Boost is active, serve the cover image from the CDN.
$image_url = apply_filters( 'jetpack_photon_url', $image_url ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
$post_thumb = [
'caption' => ! empty( $cover_caption ) ? $cover_caption : '',
'url' => wp_get_attachment_url( $cover_meta_id ),
'url' => $image_url ?? '',
];
} else {
$thumb_id = get_post_thumbnail_id( $this->id );
$post_thumb_url = wp_get_attachment_url( $thumb_id );
// If Jetpack Boost is active, serve the cover image from the CDN.
$post_thumb_url = apply_filters( 'jetpack_photon_url', $post_thumb_url ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
if ( empty( $cover_caption ) ) {
$cover_caption = wp_get_attachment_caption( $thumb_id );
}
Expand Down
9 changes: 1 addition & 8 deletions includes/apple-exporter/builders/class-components.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ private function add_thumbnail_if_needed( &$components ) {
}
}

// If the normalized URL for the first image is different than the URL for the featured image, use the featured image.
// If the normalized URL for the first image is different from the URL for the featured image, use the featured image.
$cover_config = $this->content_cover();
$cover_url = $this->get_image_full_size_url( isset( $cover_config['url'] ) ? $cover_config['url'] : $cover_config );
$normalized_url = $this->get_image_full_size_url( $original_url );
Expand Down Expand Up @@ -633,13 +633,6 @@ private function get_image_full_size_url( $url ) {
*/
$normalized_path = preg_replace( '/-(?:\d+x\d+|scaled|rotated)(\.[^.]+)$/', '$1', $url_parts['path'] );

// Remove the Jetpack CDN domain.
if ( preg_match( '/^i[0-9]\.wp\.com$/', $url_parts['host'] ) ) {
$path_parts = explode( '/', $normalized_path );
$url_parts['host'] = array_shift( $path_parts );
$normalized_path = implode( '/', $path_parts );
}

// Put Humpty Dumpty back together again.
return sprintf(
'%s://%s%s',
Expand Down
2 changes: 1 addition & 1 deletion tests/apple-exporter/builders/test-class-components.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function data_image_full_size_url() {
// Photon images should return the original.
[
'https://i1.wp.com/www.example.org/wp-content/uploads/2020/07/sample-image.jpg?w=234&crop=0%2C5px%2C100%2C134px&ssl=1',
'https://www.example.org/wp-content/uploads/2020/07/sample-image.jpg',
'https://i1.wp.com/www.example.org/wp-content/uploads/2020/07/sample-image.jpg',
],
];
}
Expand Down