From 29aae82f8fdfd45b4046d1fdd9ad5d291ca7b09e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20S=C3=B8derlind?= Date: Sun, 8 Nov 2026 18:18:05 +0100 Subject: [PATCH 1/2] Fix content image link and size handling Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- inc/class-tachyon.php | 24 ++++++++++++++++--- tests/tests/class-tests-linking.php | 35 ++++++++++++++++++++++++++++ tests/tests/class-tests-resizing.php | 27 +++++++++++++++++++++ 3 files changed, 83 insertions(+), 3 deletions(-) diff --git a/inc/class-tachyon.php b/inc/class-tachyon.php index 34ee767..74e9497 100644 --- a/inc/class-tachyon.php +++ b/inc/class-tachyon.php @@ -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'; @@ -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 ); } } } @@ -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. @@ -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 ); } @@ -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. * diff --git a/tests/tests/class-tests-linking.php b/tests/tests/class-tests-linking.php index 5ae316e..3587d96 100644 --- a/tests/tests/class-tests-linking.php +++ b/tests/tests/class-tests-linking.php @@ -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( + '', + $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. * diff --git a/tests/tests/class-tests-resizing.php b/tests/tests/class-tests-resizing.php index 1add856..1a0fe26 100644 --- a/tests/tests/class-tests-resizing.php +++ b/tests/tests/class-tests-resizing.php @@ -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( + '' . + '' + ); + + 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. * From 9c6c95144d5c9e95243ab27b2e252015681d1790 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20S=C3=B8derlind?= Date: Sun, 8 Nov 2026 18:41:49 +0100 Subject: [PATCH 2/2] Bump version to 0.11.2 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- CHANGELOG.md | 5 +++++ tachyon.php | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..0ba104d --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# Changelog + +## 0.11.2 - 2026-11-08 + +- Fix content image link rewriting and image-size detection. diff --git a/tachyon.php b/tachyon.php index 1f07098..0bdf4f3 100644 --- a/tachyon.php +++ b/tachyon.php @@ -1,7 +1,7 @@