Skip to content

Commit a5ff896

Browse files
Merge pull request #710 from Codeinwp/fixes/revison-wipe
fixes for revison wipe
2 parents f17e259 + e96395e commit a5ff896

File tree

6 files changed

+34
-6
lines changed

6 files changed

+34
-6
lines changed

classes/Visualizer/Gutenberg/Block.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,9 @@ public function update_chart_data( $data ) {
520520
}
521521

522522
if ( $data['id'] && ! is_wp_error( $data['id'] ) ) {
523-
523+
if ( get_post_type( $data['id'] ) !== Visualizer_Plugin::CPT_VISUALIZER ) {
524+
return new WP_Error( 'invalid_post_type', 'Invalid post type.' );
525+
}
524526
$chart_type = sanitize_text_field( $data['visualizer-chart-type'] );
525527
$source_type = sanitize_text_field( $data['visualizer-source'] );
526528

classes/Visualizer/Module.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,22 +395,38 @@ private function _getHTML( $rows ) {
395395
);
396396
}
397397

398+
/**
399+
* Disable revisions temporarily for visualizer post type.
400+
*/
401+
protected final function disableRevisionsTemporarily() {
402+
add_filter(
403+
'wp_revisions_to_keep', function( $num, $post ) {
404+
if ( $post->post_type === Visualizer_Plugin::CPT_VISUALIZER ) {
405+
return 0;
406+
}
407+
return $num;
408+
}, 10, 2
409+
);
410+
}
411+
398412
/**
399413
* Undo revisions for the chart, and if necessary, restore the earliest version.
400414
*
401415
* @return bool If any revisions were found.
402416
*/
403417
public final function undoRevisions( $chart_id, $restore = false ) {
404418
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'undoRevisions for %d with%s restore', $chart_id, ( $restore ? '' : 'out' ) ), 'debug', __FILE__, __LINE__ );
405-
419+
if ( get_post_type( $chart_id ) !== Visualizer_Plugin::CPT_VISUALIZER ) {
420+
return false;
421+
}
406422
$revisions = wp_get_post_revisions( $chart_id, array( 'order' => 'ASC' ) );
407423
if ( count( $revisions ) > 1 ) {
408424
$revision_ids = array_keys( $revisions );
409425

410426
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'found %d revisions = %s', count( $revisions ), print_r( $revision_ids, true ) ), 'debug', __FILE__, __LINE__ );
411427

412428
// when we restore, a new revision is likely to be created. so, let's disable revisions for the time being.
413-
add_filter( 'wp_revisions_to_keep', '__return_false' );
429+
$this->disableRevisionsTemporarily();
414430

415431
if ( $restore ) {
416432
// restore to the oldest one i.e. the first one.
@@ -431,8 +447,11 @@ public final function undoRevisions( $chart_id, $restore = false ) {
431447
* If existing revisions exist for the chart, restore the earliest version and then create a new revision to initiate editing.
432448
*/
433449
public final function handleExistingRevisions( $chart_id, $chart ) {
434-
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'handleExistingRevisions for %d', $chart_id ), 'debug', __FILE__, __LINE__ );
435450

451+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'handleExistingRevisions for %d', $chart_id ), 'debug', __FILE__, __LINE__ );
452+
if ( get_post_type( $chart_id ) !== Visualizer_Plugin::CPT_VISUALIZER ) {
453+
return $chart_id;
454+
}
436455
// undo revisions.
437456
$revisions_found = $this->undoRevisions( $chart_id, true );
438457

classes/Visualizer/Module/Chart.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,8 @@ private function _handleDataAndSettingsPage() {
684684

685685
// ensure that a revision is not created. If a revision is created it will have the proper data and the parent of the revision will have default data.
686686
// we do not want any difference in data so disable revisions temporarily.
687-
add_filter( 'wp_revisions_to_keep', '__return_false' );
687+
$this->disableRevisionsTemporarily();
688+
688689
wp_update_post( $this->_chart->to_array() );
689690
}
690691
// save meta data only when it is NOT being canceled.

classes/Visualizer/Module/Setup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public function refresh_db_for_chart( $chart, $chart_id, $force = false ) {
298298

299299
$error = $source->get_error();
300300
if ( empty( $error ) ) {
301-
add_filter( 'wp_revisions_to_keep', '__return_false' );
301+
$this->disableRevisionsTemporarily();
302302
if ( $load_series ) {
303303
update_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
304304
}

tests/test-import.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ public function test_db_import() {
232232
* @access public
233233
*/
234234
public function fileProvider() {
235+
$this->assertFileExists( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples', 'Folder "samples" does not exist' );
236+
235237
$file = VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'bar.csv';
236238
list( $content, $series ) = $this->parseFile( $file );
237239

@@ -305,6 +307,8 @@ private function parseFile( $file, $multiplyValuesBy = 1 ) {
305307
* @access public
306308
*/
307309
public function urlProvider() {
310+
$this->assertFileExists( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples', 'Folder "samples" does not exist' );
311+
308312
$url = 'https://demo.themeisle.com/wp-content/plugins/visualizer/samples/bar.csv';
309313
$file = download_url( $url );
310314
list( $content, $series ) = $this->parseFile( $file );

tests/test-revisions.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ public function test_chart_edit_save( $file_orig, $file_new ) {
272272
* @access public
273273
*/
274274
public function fileProvider() {
275+
$this->assertFileExists( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples', 'Folder "samples" does not exist' );
276+
275277
return array(
276278
array( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'line.csv', VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'bar.csv' ),
277279
);

0 commit comments

Comments
 (0)