Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## 0.11.2 - 2026-11-08

- Fix content image link rewriting and image-size detection.
24 changes: 21 additions & 3 deletions inc/class-tachyon.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ public static function filter_the_content( $content ) {
}

foreach ( $images[0] as $index => $tag ) {
$filter = [];

// Default to resize, though fit may be used in certain cases where a dimension cannot be ascertained.
$transform = 'resize';

Expand Down Expand Up @@ -422,7 +424,8 @@ public static function filter_the_content( $content ) {
$sizes = wp_list_filter( $image_sizes, $filter, 'OR' );
}
if ( ! empty( $sizes ) ) {
$size = reset( $sizes );
reset( $sizes );
$size = key( $sizes );
}
}
}
Expand Down Expand Up @@ -461,7 +464,7 @@ public static function filter_the_content( $content ) {

// If present, replace the link href with a Tachyoned URL for the full-size image.
if ( ! empty( $images['link_url'][ $index ] ) && self::validate_image_url( $images['link_url'][ $index ] ) ) {
$new_tag = preg_replace( '#(href=["|\'])' . $images['link_url'][ $index ] . '(["|\'])#i', '\1' . tachyon_url( $images['link_url'][ $index ] ) . '\2', $new_tag, 1 );
$new_tag = self::replace_link_url( $new_tag, $images['link_url'][ $index ] );
}

// Supplant the original source value with our Tachyon URL.
Expand Down Expand Up @@ -491,7 +494,7 @@ public static function filter_the_content( $content ) {
$content = str_replace( $tag, $new_tag, $content );
}
} elseif ( preg_match( '#^http(s)?://i[\d]{1}.wp.com#', $src ) && ! empty( $images['link_url'][ $index ] ) && self::validate_image_url( $images['link_url'][ $index ] ) ) {
$new_tag = preg_replace( '#(href=["|\'])' . $images['link_url'][ $index ] . '(["|\'])#i', '\1' . tachyon_url( $images['link_url'][ $index ] ) . '\2', $tag, 1 );
$new_tag = self::replace_link_url( $tag, $images['link_url'][ $index ] );

$content = str_replace( $tag, $new_tag, $content );
}
Expand All @@ -501,6 +504,21 @@ public static function filter_the_content( $content ) {
return $content;
}

/**
* Replace an image link URL with its Tachyon equivalent.
*
* @param string $tag Image tag or linked image markup.
* @param string $link_url Original link URL.
* @return string
*/
private static function replace_link_url( $tag, $link_url ) {
$pattern = '#(href=["|\'])' . preg_quote( $link_url, '#' ) . '(["|\'])#i';

return preg_replace_callback( $pattern, function ( $matches ) use ( $link_url ) {
return $matches[1] . tachyon_url( $link_url ) . $matches[2];
}, $tag, 1 );
}

/**
* Ensure galleries use Tachyon.
*
Expand Down
2 changes: 1 addition & 1 deletion tachyon.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Plugin Name: Tachyon
* Version: 0.11.1
* Version: 0.11.2
* Description: A standalone tachyon proof of concept
* Author: Joe Hoyle | Human Made | Automattic Inc
*
Expand Down
35 changes: 35 additions & 0 deletions tests/tests/class-tests-linking.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,41 @@ function test_content_filtering( $file, $content, $valid_link_urls, $valid_src_u
$this->assertContains( $actual_href, $valid_link_urls, 'The link is expected to be ' . implode( ' or ', $valid_link_urls ) );
}

/**
* Ensure link URLs containing regular-expression delimiters are matched literally.
*
* @dataProvider data_special_character_link_sources
*
* @param string $source Image source URL.
* @return void
*/
function test_content_filtering_escapes_special_characters_in_link_url( $source ) {
$base_url = wp_upload_dir()['baseurl'];
$link_url = $base_url . '/tachyon.jpg?variant=1#section-$1';
$source = str_replace( '%%BASE_URL%%', $base_url, $source );
$content = sprintf(
'<a href="%1$s"><img src="%2$s" /></a>',
$link_url,
$source
);

$filtered_content = Tachyon::filter_the_content( $content );

$this->assertSame( tachyon_url( $link_url ), $this->get_href_from_html( $filtered_content ) );
}

/**
* Provide local and Photon image sources for link rewriting.
*
* @return array
*/
function data_special_character_link_sources() {
return [
[ '%%BASE_URL%%/tachyon.jpg' ],
[ 'https://i0.wp.com/example.com/tachyon.jpg' ],
];
}

/**
* Data provider for test_content_filtering.
*
Expand Down
27 changes: 27 additions & 0 deletions tests/tests/class-tests-resizing.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,33 @@ function test_content_filtering( $file, $content, $valid_urls ) {
$this->assertContains( $actual_src, $valid_urls, 'The resized image is expected to be ' . implode( ' or ', $valid_urls ) );
}

/**
* Ensure size matching state does not carry over to the next image.
*
* @return void
*/
function test_image_size_resolution_does_not_leak_between_images() {
$base_url = wp_upload_dir()['baseurl'];
$sizes = [];
$size_filter = function ( $args, $image ) use ( &$sizes ) {
$sizes[] = $image['size'];
return $args;
};

add_filter( 'tachyon_post_image_args', $size_filter, 10, 2 );

Tachyon::filter_the_content(
'<img src="' . $base_url . '/tachyon.jpg" width="150" height="150" />' .
'<img src="' . $base_url . '/tachyon-large.jpg" />'
);

remove_filter( 'tachyon_post_image_args', $size_filter, 10 );

$this->assertCount( 2, $sizes );
$this->assertSame( 'thumb', $sizes[0] );
$this->assertSame( [ false, false ], $sizes[1] );
}

/**
* Data provider for test_content_filtering.
*
Expand Down