Skip to content

Commit 6c399ab

Browse files
undo revisions in the library and on clicking edit button
1 parent 5c0104b commit 6c399ab

File tree

3 files changed

+49
-25
lines changed

3 files changed

+49
-25
lines changed

classes/Visualizer/Module.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,4 +323,48 @@ private function _getHTML( $rows ) {
323323
);
324324
}
325325

326+
/**
327+
* Undo revisions for the chart, and if necessary, restore the earliest version.
328+
*
329+
* @return bool If any revisions were found.
330+
*/
331+
protected function undoRevisions( $chart_id, $restore = false ) {
332+
$revisions = wp_get_post_revisions( $chart_id, array( 'order' => 'ASC' ) );
333+
if ( $revisions ) {
334+
$revision_ids = array_keys( $revisions );
335+
336+
// when we restore, a new revision is likely to be created. so, let's disable revisions for the time being.
337+
add_filter( 'wp_revisions_to_keep', '__return_false' );
338+
339+
if ( $restore ) {
340+
// restore to the oldest one i.e. the first one.
341+
wp_restore_post_revision( $revision_ids[0] );
342+
}
343+
344+
// delete all revisions.
345+
foreach ( $revision_ids as $id ) {
346+
wp_delete_post_revision( $id );
347+
}
348+
349+
return true;
350+
}
351+
return false;
352+
}
353+
354+
/**
355+
* If existing revisions exist for the chart, restore the earliest version and then create a new revision to initiate editing.
356+
*/
357+
protected function handleExistingRevisions( $chart_id, $chart ) {
358+
// undo revisions.
359+
$revisions_found = $this->undoRevisions( $chart_id, true );
360+
// create revision for the edit action.
361+
wp_save_post_revision( $chart_id );
362+
363+
if ( $revisions_found ) {
364+
// fetch chart data again in case it was updated by an earlier revision.
365+
$chart = get_post( $chart_id );
366+
}
367+
return $chart;
368+
}
369+
326370
}

classes/Visualizer/Module/Admin.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,10 @@ public function renderLibraryPage() {
496496
$query = new WP_Query( $query_args );
497497
while ( $query->have_posts() ) {
498498
$chart = $query->next_post();
499+
500+
// if the user has updated a chart and instead of saving it, has closed the modal. If the user refreshes, they should get the original chart.
501+
$chart = $this->handleExistingRevisions( $chart->ID, $chart );
502+
499503
// fetch and update settings
500504
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
501505
unset( $settings['height'], $settings['width'] );

classes/Visualizer/Module/Chart.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ public function __construct( Visualizer_Plugin $plugin ) {
5757
$this->_addAjaxAction( Visualizer_Plugin::ACTION_EDIT_CHART, 'renderChartPages' );
5858
$this->_addAjaxAction( Visualizer_Plugin::ACTION_UPLOAD_DATA, 'uploadData' );
5959
$this->_addAjaxAction( Visualizer_Plugin::ACTION_CLONE_CHART, 'cloneChart' );
60-
// Added by Ash/Upwork
6160
$this->_addAjaxAction( Visualizer_Plugin::ACTION_EXPORT_DATA, 'exportData' );
62-
// Added by Ash/Upwork
6361
}
6462

6563
/**
@@ -281,7 +279,7 @@ public function renderChartPages() {
281279
$this->undoRevisions( $chart_id, false );
282280
} else {
283281
// if the edit button is clicked.
284-
$rev = wp_save_post_revision( $chart_id );
282+
$this->_chart = $this->handleExistingRevisions( $chart_id, $this->_chart );
285283
}
286284

287285
switch ( $tab ) {
@@ -613,26 +611,4 @@ private function _handleDataPage() {
613611
wp_iframe( array( $render, 'render' ) );
614612
}
615613

616-
/**
617-
* Undo revisions for the chart, and if necessary, restore the earliest version.
618-
*/
619-
private function undoRevisions( $chart_id, $restore = false ) {
620-
$revisions = wp_get_post_revisions( $chart_id, array( 'order' => 'ASC' ) );
621-
if ( $revisions ) {
622-
$revision_ids = array_keys( $revisions );
623-
624-
// when we restore, a new revision is likely to be created. so, let's disable revisions for the time being.
625-
add_filter( 'wp_revisions_to_keep', '__return_false' );
626-
627-
if ( $restore ) {
628-
// restore to the oldest one i.e. the first one.
629-
wp_restore_post_revision( $revision_ids[0] );
630-
}
631-
632-
// delete all revisions.
633-
foreach ( $revision_ids as $id ) {
634-
wp_delete_post_revision( $id );
635-
}
636-
}
637-
}
638614
}

0 commit comments

Comments
 (0)