From 35d8e82316cb9898fa33406bbb06c8d1dec455d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Tue, 1 Dec 2026 22:18:10 +0200 Subject: [PATCH 1/5] fix: show a saved chart image only for the chart it was saved for Co-Authored-By: Claude Mythos 5.2 --- classes/Visualizer/Module.php | 30 ++++++++++- classes/Visualizer/Module/Chart.php | 8 +-- classes/Visualizer/Module/Frontend.php | 4 +- tests/test-chart-image.php | 69 ++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 8 deletions(-) diff --git a/classes/Visualizer/Module.php b/classes/Visualizer/Module.php index d00b2517..3d883beb 100644 --- a/classes/Visualizer/Module.php +++ b/classes/Visualizer/Module.php @@ -961,6 +961,32 @@ static function ( $value ) { ); } + /** + * Returns the name of the file that holds the saved image of a chart. + * + * @param int $chart_id The chart ID. + * @return string + */ + protected static function chart_image_filename( $chart_id ) { + return 'visualization-' . $chart_id . '.png'; + } + + /** + * Returns the ID of the image that was saved for a chart. + * + * A chart copied by an older version can point to the image of its source chart, also after that image is deleted. + * + * @param int $chart_id The chart ID. + * @return int The attachment ID, or 0 when the chart has no image of its own. + */ + public static function get_chart_image_id( $chart_id ) { + $attachment_id = (int) get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, true ); + if ( $attachment_id && basename( (string) get_attached_file( $attachment_id ) ) === self::chart_image_filename( $chart_id ) ) { + return $attachment_id; + } + return 0; + } + /** * Get chart image. * @@ -970,8 +996,8 @@ static function ( $value ) { public function _getImage( $chart = null ) { $image = ''; if ( $chart ) { - $chart_image = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_IMAGE, true ); - if ( ! empty( $chart_image ) ) { + $chart_image = self::get_chart_image_id( $chart->ID ); + if ( $chart_image ) { $image = wp_get_attachment_image( $chart_image, 'full' ); } } diff --git a/classes/Visualizer/Module/Chart.php b/classes/Visualizer/Module/Chart.php index 390400e8..785ac96d 100644 --- a/classes/Visualizer/Module/Chart.php +++ b/classes/Visualizer/Module/Chart.php @@ -1752,11 +1752,11 @@ public function saveFilter() { * @return int Attachment ID, or 0 when no attachment was saved. */ public function save_chart_image( $base64_img, $chart_id, $save_attachment = true ) { - $filename = 'visualization-' . $chart_id . '.png'; + $filename = self::chart_image_filename( $chart_id ); - // Delete the old chart image, but only a file with the name that this method gives to the image of this chart. - $old_attachment_id = (int) get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, true ); - if ( $old_attachment_id && basename( (string) get_attached_file( $old_attachment_id ) ) === $filename ) { + // Delete the old chart image, but only the image of this chart. + $old_attachment_id = self::get_chart_image_id( $chart_id ); + if ( $old_attachment_id ) { wp_delete_attachment( $old_attachment_id, true ); } diff --git a/classes/Visualizer/Module/Frontend.php b/classes/Visualizer/Module/Frontend.php index 4dd63de8..96b9c998 100644 --- a/classes/Visualizer/Module/Frontend.php +++ b/classes/Visualizer/Module/Frontend.php @@ -423,7 +423,8 @@ public function renderChart( $atts ) { } if ( 'yes' === $atts['use_image'] ) { - $chart_image = $chart_data['chart_image']; + // Read the image on each render: the cached chart data can outlive the image. + $chart_image = self::get_chart_image_id( $chart->ID ); if ( $chart_image ) { return '
getHtmlAttributes( $attributes ) . '>' . wp_get_attachment_image( $chart_image, 'full' ) . '
'; } @@ -766,7 +767,6 @@ private function getChartData( $cache_key = '', $chart_id = 0 ) { 'type' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ), 'settings' => $settings, 'series' => $series, - 'chart_image' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_IMAGE, true ), 'is_woocommerce_report' => $is_woocommerce_report, ); diff --git a/tests/test-chart-image.php b/tests/test-chart-image.php index c413be2d..09d083ad 100644 --- a/tests/test-chart-image.php +++ b/tests/test-chart-image.php @@ -121,6 +121,75 @@ public function test_translation_copy_does_not_take_the_chart_image() { $this->assertCopyHasNoImage( $chart_id ); } + /** + * A chart copied by an older version points to the image of its source chart. Its shortcode and its image + * export show no image of another chart, also when the chart data is cached and after the image is deleted. + */ + public function test_chart_does_not_show_the_image_of_another_chart() { + $module = new Visualizer_Module_Chart( Visualizer_Plugin::instance() ); + $source_id = $this->create_published_chart(); + $image_id = $module->save_chart_image( self::PNG, $source_id ); + update_post_meta( $source_id, Visualizer_Plugin::CF_CHART_IMAGE, $image_id ); + $copy_id = $this->create_published_chart(); + update_post_meta( $copy_id, Visualizer_Plugin::CF_CHART_IMAGE, $image_id ); + + $this->assertStringContainsString( 'render_as_image( $source_id ) ); + $this->assertStringNotContainsString( 'render_as_image( $copy_id ) ); + $this->assertStringContainsString( 'visualizer-action-print', $this->render_as_image( $copy_id ), 'The cached chart data gives the live chart too.' ); + $this->assertSame( array( 'csv' => '' ), $module->_getImage( get_post( $copy_id ) ) ); + + // The source chart saves a new image. That deletes the image the copy points to. + update_post_meta( $source_id, Visualizer_Plugin::CF_CHART_IMAGE, $module->save_chart_image( self::PNG, $source_id ) ); + + $this->assertNull( get_post( $image_id ) ); + $this->assertStringContainsString( 'visualizer-action-print', $this->render_as_image( $copy_id ) ); + $this->assertStringContainsString( 'render_as_image( $source_id ) ); + } + + /** + * Create a published line chart with data. Its live render has the print action, its saved image has not. + * + * @return int + */ + private function create_published_chart() { + $chart_id = self::factory()->post->create( + array( + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, + 'post_status' => 'publish', + 'post_content' => serialize( array( array( 'a', 1 ) ) ), + ) + ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, 'line' ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, 'ChartJS' ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, array( 'actions' => array( 'print' ) ) ); + update_post_meta( + $chart_id, + Visualizer_Plugin::CF_SERIES, + array( + array( + 'label' => 'x', + 'type' => 'string', + ), + array( + 'label' => 'y', + 'type' => 'number', + ), + ) + ); + + return $chart_id; + } + + /** + * Render the shortcode of a chart that asks for the saved image. + * + * @param int $chart_id Chart ID. + * @return string + */ + private function render_as_image( $chart_id ) { + return do_shortcode( '[visualizer id="' . $chart_id . '" use_image="yes"]' ); + } + /** * Create a chart of the current user that has a saved image. * From 73e94c5c447dab10756284458aa1972a0b726cde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Wed, 2 Dec 2026 02:14:17 +0200 Subject: [PATCH 2/5] fix: keep the chart image after WordPress renames its edited file Co-Authored-By: Claude Mythos 5.2 --- classes/Visualizer/Module.php | 8 +++++--- phpstan-baseline.neon | 6 ------ tests/test-chart-image.php | 8 +++++++- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/classes/Visualizer/Module.php b/classes/Visualizer/Module.php index 3d883beb..21588661 100644 --- a/classes/Visualizer/Module.php +++ b/classes/Visualizer/Module.php @@ -975,13 +975,15 @@ protected static function chart_image_filename( $chart_id ) { * Returns the ID of the image that was saved for a chart. * * A chart copied by an older version can point to the image of its source chart, also after that image is deleted. + * The file of an image keeps the name it was saved with; WordPress adds a suffix when a user edits the image. * * @param int $chart_id The chart ID. * @return int The attachment ID, or 0 when the chart has no image of its own. */ public static function get_chart_image_id( $chart_id ) { $attachment_id = (int) get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, true ); - if ( $attachment_id && basename( (string) get_attached_file( $attachment_id ) ) === self::chart_image_filename( $chart_id ) ) { + $saved_name = preg_quote( pathinfo( self::chart_image_filename( $chart_id ), PATHINFO_FILENAME ), '/' ); + if ( $attachment_id && preg_match( '/^' . $saved_name . '(-[^.]+)?\.png$/', basename( (string) get_attached_file( $attachment_id ) ) ) ) { return $attachment_id; } return 0; @@ -990,8 +992,8 @@ public static function get_chart_image_id( $chart_id ) { /** * Get chart image. * - * @param object $chart Chart data. - * @return string + * @param WP_Post|null $chart The chart post. + * @return array{csv: string} The image tag of the saved chart image, or an empty string. */ public function _getImage( $chart = null ) { $image = ''; diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 5c58b35b..9c656353 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -156,12 +156,6 @@ parameters: count: 1 path: classes/Visualizer/Module.php - - - message: '#^Method Visualizer_Module\:\:_getImage\(\) should return string but returns array\\.$#' - identifier: return.type - count: 1 - path: classes/Visualizer/Module.php - - message: '#^Method Visualizer_Module\:\:can_show_feature\(\) has no return type specified\.$#' identifier: missingType.return diff --git a/tests/test-chart-image.php b/tests/test-chart-image.php index 09d083ad..d29e9921 100644 --- a/tests/test-chart-image.php +++ b/tests/test-chart-image.php @@ -137,13 +137,19 @@ public function test_chart_does_not_show_the_image_of_another_chart() { $this->assertStringNotContainsString( 'render_as_image( $copy_id ) ); $this->assertStringContainsString( 'visualizer-action-print', $this->render_as_image( $copy_id ), 'The cached chart data gives the live chart too.' ); $this->assertSame( array( 'csv' => '' ), $module->_getImage( get_post( $copy_id ) ) ); + $this->assertStringContainsString( '_getImage( get_post( $source_id ) )['csv'] ); // The source chart saves a new image. That deletes the image the copy points to. - update_post_meta( $source_id, Visualizer_Plugin::CF_CHART_IMAGE, $module->save_chart_image( self::PNG, $source_id ) ); + $new_image_id = $module->save_chart_image( self::PNG, $source_id ); + update_post_meta( $source_id, Visualizer_Plugin::CF_CHART_IMAGE, $new_image_id ); $this->assertNull( get_post( $image_id ) ); $this->assertStringContainsString( 'visualizer-action-print', $this->render_as_image( $copy_id ) ); $this->assertStringContainsString( 'render_as_image( $source_id ) ); + + // WordPress adds a suffix to the file name when a user edits the image. The chart keeps that image. + update_attached_file( $new_image_id, str_replace( '.png', '-e1700000000000.png', (string) get_attached_file( $new_image_id ) ) ); + $this->assertStringContainsString( 'render_as_image( $source_id ) ); } /** From 19b1284f8e638d11133e7002ee4ddb27bd5a4a87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Wed, 2 Dec 2026 07:06:22 +0200 Subject: [PATCH 3/5] fix: accept only the image name suffixes that WordPress adds Co-Authored-By: Claude Mythos 5.2 --- classes/Visualizer/Module.php | 7 ++++--- tests/test-chart-image.php | 11 +++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/classes/Visualizer/Module.php b/classes/Visualizer/Module.php index 21588661..b11ce96f 100644 --- a/classes/Visualizer/Module.php +++ b/classes/Visualizer/Module.php @@ -975,7 +975,8 @@ protected static function chart_image_filename( $chart_id ) { * Returns the ID of the image that was saved for a chart. * * A chart copied by an older version can point to the image of its source chart, also after that image is deleted. - * The file of an image keeps the name it was saved with; WordPress adds a suffix when a user edits the image. + * The file of an image keeps the name it was saved with. WordPress adds a suffix for a name that is in use, for an + * image that a user edits, and for a large image that it scales or rotates. * * @param int $chart_id The chart ID. * @return int The attachment ID, or 0 when the chart has no image of its own. @@ -983,7 +984,7 @@ protected static function chart_image_filename( $chart_id ) { public static function get_chart_image_id( $chart_id ) { $attachment_id = (int) get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, true ); $saved_name = preg_quote( pathinfo( self::chart_image_filename( $chart_id ), PATHINFO_FILENAME ), '/' ); - if ( $attachment_id && preg_match( '/^' . $saved_name . '(-[^.]+)?\.png$/', basename( (string) get_attached_file( $attachment_id ) ) ) ) { + if ( $attachment_id && preg_match( '/^' . $saved_name . '(-(\d+|e\d+|scaled|rotated))*\.png\z/', basename( (string) get_attached_file( $attachment_id ) ) ) ) { return $attachment_id; } return 0; @@ -993,7 +994,7 @@ public static function get_chart_image_id( $chart_id ) { * Get chart image. * * @param WP_Post|null $chart The chart post. - * @return array{csv: string} The image tag of the saved chart image, or an empty string. + * @return array{csv: string} The `csv` value is the image tag of the saved chart image, or an empty string. */ public function _getImage( $chart = null ) { $image = ''; diff --git a/tests/test-chart-image.php b/tests/test-chart-image.php index d29e9921..1953ca3c 100644 --- a/tests/test-chart-image.php +++ b/tests/test-chart-image.php @@ -148,8 +148,15 @@ public function test_chart_does_not_show_the_image_of_another_chart() { $this->assertStringContainsString( 'render_as_image( $source_id ) ); // WordPress adds a suffix to the file name when a user edits the image. The chart keeps that image. - update_attached_file( $new_image_id, str_replace( '.png', '-e1700000000000.png', (string) get_attached_file( $new_image_id ) ) ); - $this->assertStringContainsString( 'render_as_image( $source_id ) ); + $saved_file = (string) get_attached_file( $new_image_id ); + foreach ( array( '-e1700000000000', '-1', '-1-scaled' ) as $suffix ) { + update_attached_file( $new_image_id, str_replace( '.png', $suffix . '.png', $saved_file ) ); + $this->assertStringContainsString( 'render_as_image( $source_id ), $suffix ); + } + + // Another suffix is not a name that WordPress gives to the saved image. + update_attached_file( $new_image_id, str_replace( '.png', '-backup.png', $saved_file ) ); + $this->assertStringNotContainsString( 'render_as_image( $source_id ) ); } /** From 240795153b67d2ff0dc480de873cc96883f70124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Wed, 2 Dec 2026 07:09:23 +0200 Subject: [PATCH 4/5] test: describe every accepted suffix Co-Authored-By: Claude Mythos 5.2 --- tests/test-chart-image.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-chart-image.php b/tests/test-chart-image.php index 1953ca3c..2abc97a8 100644 --- a/tests/test-chart-image.php +++ b/tests/test-chart-image.php @@ -147,7 +147,7 @@ public function test_chart_does_not_show_the_image_of_another_chart() { $this->assertStringContainsString( 'visualizer-action-print', $this->render_as_image( $copy_id ) ); $this->assertStringContainsString( 'render_as_image( $source_id ) ); - // WordPress adds a suffix to the file name when a user edits the image. The chart keeps that image. + // WordPress adds a suffix to the file name for an edited image, a name in use and a scaled image. The chart keeps that image. $saved_file = (string) get_attached_file( $new_image_id ); foreach ( array( '-e1700000000000', '-1', '-1-scaled' ) as $suffix ) { update_attached_file( $new_image_id, str_replace( '.png', $suffix . '.png', $saved_file ) ); From 37c1dedf1a8b7cadaf739c18b8b67efbd2170772 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Wed, 2 Dec 2026 10:25:20 +0200 Subject: [PATCH 5/5] test: cover the deletion of an edited chart image Co-Authored-By: Claude Mythos 5.2 --- tests/test-chart-image.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test-chart-image.php b/tests/test-chart-image.php index 2abc97a8..cb24dab8 100644 --- a/tests/test-chart-image.php +++ b/tests/test-chart-image.php @@ -59,6 +59,21 @@ public function test_image_save_deletes_the_old_image_of_the_chart() { $this->assertInstanceOf( 'WP_Post', get_post( $new_id ) ); } + /** + * A new image of a chart also replaces the old image after a user edited it, which gives its file a suffix. + */ + public function test_image_save_deletes_the_edited_old_image_of_the_chart() { + $chart_id = $this->create_own_chart(); + $module = new Visualizer_Module_Chart( Visualizer_Plugin::instance() ); + $old_id = $module->save_chart_image( self::PNG, $chart_id ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, $old_id ); + update_attached_file( $old_id, str_replace( '.png', '-e1700000000000.png', (string) get_attached_file( $old_id ) ) ); + + $module->save_chart_image( self::PNG, $chart_id ); + + $this->assertNull( get_post( $old_id ) ); + } + /** * A chart that points to the image of another chart does not delete that image. */