Repository navigation
Show a saved chart image only for the chart it was saved for - #1429
Conversation
Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
There was a problem hiding this comment.
🟢 Approved
This approves the fix; one inline comment asks for a deletion test, and two notes ask a developer to check image ownership.
Validation details
- Files reviewed: 5/5 changed files. Question rows left unchecked: the precedent on deleting only the chart's own image, and the precedent on stale chart image meta.
- The new PHPUnit test passed at HEAD on PHP 8.3.37 with PHPUnit 9.6.39:
OK (1 test, 12 assertions). - The same test failed on the base tree at
tests/test-chart-image.php:137. The copy rendered the source imagevisualization-4.png. - Releases 3.7.0, 3.9.0 and 4.0.12 save chart images as
visualization-<id>.png, so the new name rule keeps their images. - No other plugin path shows the stored image without the new check.
Untested areas
- The precedent on deleting only the chart's own image: not settled.
- The precedent on stale chart image meta: not settled.
- Visualizer Pro callbacks of the AMP view filter: source not available.
- Plugins that rename uploaded files: not checked.
- Jest and Playwright suites: not run.
- The variant pass did not settle these places, where the fixed cause may still reach:
/repo/classes/Visualizer/Module/AMP.php:93-95— unverified: visualizer_amp_view receives the whole chart and can bypass guarded default output. Whether Pro callbacks read its raw image meta requires the unavailable Pro source./repo/classes/Visualizer/Module.php:987— unverified: Deleted-attachment behavior across every supported WordPress version was not established. WordPress 5.2 and 7.1 read wp_attached_file at /deps/wordpress/5.2/wp-includes/post.php:45…
🤖 Automated review · run code-review-agent_6b0fccf8a265f4.93774965.
🤖 Review agent — review posted ✅ on 24079515 · approved · 1 finding · 13 min
Run code-review-agent_6b0fccf8a265f4.93774965 · trail
| // 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 ); | ||
| } |
There was a problem hiding this comment.
tests save_chart_image() now deletes more old images than before. At the base, it deleted the old image only when the file was exactly visualization-<id>.png. Now get_chart_image_id() also accepts -1, -e<timestamp>, -scaled and -rotated names. A chart whose image a user edited in the Media Library now loses that edited attachment on the next save. No test covers this. The new suffix loop only renders the chart. Add a case to tests/test-chart-image.php: save an image and store its ID in CF_CHART_IMAGE. Rename its file with update_attached_file() to the -e1700000000000.png name. Save again, and assert get_post() of the old ID is null. That assertion fails on the base.
There was a problem hiding this comment.
Added in 37c1ded: test_image_save_deletes_the_edited_old_image_of_the_chart. It fails on development.
| // 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 ); | ||
| } |
There was a problem hiding this comment.
note When a chart saves a new image, the code now force-deletes any attachment in its visualizer-chart-image meta whose file is visualization-<id> with a -1, -e…, -scaled or -rotated suffix. Can that meta ever hold an attachment the chart does not own, for example a user upload with such a name set by an import? If only save_chart_image() writes the meta, this is safe. If another writer can set it, a user's media file is deleted.
There was a problem hiding this comment.
Only save_chart_image() writes the meta, through Chart.php in the settings save. The key has the visualizer- prefix, so protect_chart_meta() keeps it out of the Custom Fields panel, XML-RPC and the REST API. An import of a WordPress export can set it, and the file name check then decides.
| 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 . '(-(\d+|e\d+|scaled|rotated))*\.png\z/', basename( (string) get_attached_file( $attachment_id ) ) ) ) { | ||
| return $attachment_id; | ||
| } | ||
| return 0; |
There was a problem hiding this comment.
note The new ownership check covers the shortcode, the image export and the default AMP output. The visualizer_amp_view filter receives the chart before that check. If a Visualizer Pro callback reads the raw visualizer-chart-image meta, a chart copied by an older version still shows the image of its source chart there. If Pro reads the image through get_chart_image_id() or _getImage(), it is safe.
There was a problem hiding this comment.
Checked: Visualizer Pro does not read visualizer-chart-image or the chart-img setting in any file.
Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
A chart copied by an older version still stores the image ID of its source chart. When the source chart saved a new image, the copy showed an empty container with
use_image="yes". A chart now shows a saved image only when the image was saved for that chart; otherwise it shows the live chart.What changed
Shortcode with
use_image="yes"— shows the saved image only when its file is the image of this chart:visualization-<chart id>.png, also with a suffix that WordPress adds: for a name that is in use, or for an image that a user edits. A chart that points to the image of another chart, or to a deleted image, shows the live chart.Cached chart data — the image ID is read on each render and is no longer part of the cached chart data. A cached ID could outlive its image.
Image export —
_getImage()uses the same rule.Image save —
save_chart_image()finds the old image of the chart through the same new helper,Visualizer_Module::get_chart_image_id(). Its behavior does not change.Note
New copies do not get the image ID since #1425. This change covers copies that older versions made, and their restored revisions.
Note
Tracked in Codeinwp/visualizer-pro#668.
QA
A copy made by a current version has no image ID, so plant the state of an older copy with WP-CLI.
Go to
WP Admin → Visualizer → Chart Library. Create chart A. In the chart editor, open the Settings tab. In the section Save chart as an image inside Media Library, select Save inside media library? and save. Note the ID of chart A.Create chart B with other data and save it. Note its ID. Give chart B the image ID of chart A:
Add
[visualizer id="<B>" use_image="yes"]and[visualizer id="<A>" use_image="yes"]to a page and open the page.Expect: chart A shows as an image. Chart B shows as the live chart with its own data, not as the image of chart A.
Open chart A in the chart editor and save it again. Reload the page.
Expect: chart A shows its new image. Chart B still shows the live chart, not an empty area.
🤖 Generated with Claude Code