diff --git a/classes/Visualizer/Module/Chart.php b/classes/Visualizer/Module/Chart.php index 34d615ab..1d5e3446 100644 --- a/classes/Visualizer/Module/Chart.php +++ b/classes/Visualizer/Module/Chart.php @@ -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 ); @@ -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 ); } @@ -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; diff --git a/tests/test-chart-image.php b/tests/test-chart-image.php new file mode 100644 index 00000000..6b088bbf --- /dev/null +++ b/tests/test-chart-image.php @@ -0,0 +1,68 @@ +_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 ) ); + } +}