Skip to content
Merged
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
37 changes: 33 additions & 4 deletions classes/Visualizer/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -962,16 +962,45 @@ static function ( $value ) {
}

/**
* Get chart image.
* Returns the name of the file that holds the saved image of a chart.
*
* @param object $chart Chart data.
* @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.
* 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.
*/
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;
Comment on lines +984 to +990

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked: Visualizer Pro does not read visualizer-chart-image or the chart-img setting in any file.

}

/**
* Get chart image.
*
* @param WP_Post|null $chart The chart post.
* @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 = '';
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' );
}
}
Expand Down
8 changes: 4 additions & 4 deletions classes/Visualizer/Module/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Comment on lines +1757 to 1761

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 37c1ded: test_image_save_deletes_the_edited_old_image_of_the_chart. It fails on development.

Comment on lines +1757 to 1761

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.


Expand Down
4 changes: 2 additions & 2 deletions classes/Visualizer/Module/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '>' . wp_get_attachment_image( $chart_image, 'full' ) . '</div>';
}
Expand Down Expand Up @@ -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,
);

Expand Down
6 changes: 0 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,6 @@ parameters:
count: 1
path: classes/Visualizer/Module.php

-
message: '#^Method Visualizer_Module\:\:_getImage\(\) should return string but returns array\<string, string\>\.$#'
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
Expand Down
97 changes: 97 additions & 0 deletions tests/test-chart-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -121,6 +136,88 @@ 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( '<img', $this->render_as_image( $source_id ) );
$this->assertStringNotContainsString( '<img', $this->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( '<img', $module->_getImage( get_post( $source_id ) )['csv'] );

// The source chart saves a new image. That deletes the image the copy points to.
$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( '<img', $this->render_as_image( $source_id ) );

// 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 ) );
$this->assertStringContainsString( '<img', $this->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( '<img', $this->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.
*
Expand Down
Loading