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
11 changes: 6 additions & 5 deletions classes/Visualizer/Module/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ public function renderChartPages() {
$_POST['save_chart_image'] = isset( $_POST['save_chart_image'] ) && 'yes' === $_POST['save_chart_image'] ? true : false;
$_POST['lazy_load_chart'] = isset( $_POST['lazy_load_chart'] ) && 'yes' === $_POST['lazy_load_chart'] ? true : false;

if ( isset( $_POST['chart-img'] ) && ! empty( $_POST['chart-img'] ) ) {
if ( ! empty( $_POST['chart-img'] ) && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'] ) ) {
$attachment_id = $this->save_chart_image( $_POST['chart-img'], $chart_id, $_POST['save_chart_image'] );
if ( $attachment_id ) {
update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, $attachment_id );
Expand Down Expand Up @@ -1752,9 +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 ) {
// Delete old chart image.
$old_attachment_id = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, true );
if ( $old_attachment_id ) {
$filename = 'visualization-' . $chart_id . '.png';

// Delete the old chart image, but only a file that this method made for 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 ) {
wp_delete_attachment( $old_attachment_id, true );
}

Expand All @@ -1773,7 +1775,6 @@ public function save_chart_image( $base64_img, $chart_id, $save_attachment = tru
if ( false === $decoded || 0 !== strncmp( $decoded, "\x89PNG\r\n\x1a\n", 8 ) ) {
return 0;
}
$filename = 'visualization-' . $chart_id . '.png';
$file_type = 'image/png';
$hashed_filename = $filename;

Expand Down
68 changes: 68 additions & 0 deletions tests/test-chart-image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Chart image tests.
*
* @package Visualizer
*/
class Test_Visualizer_Chart_Image extends WP_Ajax_UnitTestCase {

/**
* A 1x1 PNG as the editor sends it.
*/
const PNG = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==';

/**
* Create a chart that the current user owns.
*
* @return int
*/
private function create_own_chart() {
$this->_setRole( 'contributor' );

return self::factory()->post->create(
array(
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
'post_author' => get_current_user_id(),
'post_status' => 'draft',
)
);
}

/**
* An attachment that the plugin did not make for the chart is not deleted when the chart image changes.
*/
public function test_image_save_keeps_an_attachment_of_another_owner() {
$attachment_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/images/canola.jpg' );
$chart_id = $this->create_own_chart();
update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, $attachment_id );

$module = new Visualizer_Module_Chart( Visualizer_Plugin::instance() );
$module->save_chart_image( self::PNG, $chart_id, false );

$this->assertInstanceOf( 'WP_Post', get_post( $attachment_id ) );
}

/**
* A request without the editor nonce does not save a chart image.
*/
public function test_image_save_needs_the_editor_nonce() {
$chart_id = $this->create_own_chart();
$_GET = array( 'chart' => $chart_id );
$_POST = array(
'chart-img' => self::PNG,
'save_chart_image' => 'yes',
);

ob_start();
try {
$this->_handleAjax( 'visualizer-edit-chart' );
} catch ( WPAjaxDieContinueException $e ) {
// The handler ends the request.
} catch ( WPAjaxDieStopException $ee ) {
// The handler ends the request.
}
ob_end_clean();

$this->assertSame( '', get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, true ) );
}
}
Loading