Skip to content

Commit 08e4a2a

Browse files
authored
Merge pull request #246 from contactashish13/issue-243
add revision support
2 parents 38a1441 + 578a7e2 commit 08e4a2a

File tree

7 files changed

+142
-2
lines changed

7 files changed

+142
-2
lines changed

classes/Visualizer/Module.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,50 @@ 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
/**
327371
* Returns the language of the locale.
328372
*
@@ -339,4 +383,5 @@ protected function get_language() {
339383
}
340384
return reset( $array );
341385
}
386+
342387
}

classes/Visualizer/Module/Admin.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,71 @@ public function __construct( Visualizer_Plugin $plugin ) {
6262
$this->_addFilter( 'visualizer_get_chart_counts', 'getChartCountsByTypeAndMeta' );
6363
$this->_addFilter( 'visualizer_feedback_review_trigger', 'feedbackReviewTrigger' );
6464

65+
// revision support.
66+
$this->_addFilter( 'wp_revisions_to_keep', 'limitRevisions', null, 10, 2 );
67+
$this->_addAction( '_wp_put_post_revision', 'addRevision', null, 10, 1 );
68+
$this->_addAction( 'wp_restore_post_revision', 'restoreRevision', null, 10, 2 );
69+
6570
$this->_addAction( 'admin_init', 'init' );
6671
}
6772

73+
/**
74+
* No limits on revisions.
75+
*/
76+
public function limitRevisions( $num, $post ) {
77+
if ( Visualizer_Plugin::CPT_VISUALIZER === $post->post_type ) {
78+
return -1;
79+
}
80+
return $num;
81+
}
82+
83+
/**
84+
* Add a revision.
85+
*/
86+
public function addRevision( $revision_id ) {
87+
$parent_id = wp_is_post_revision( $revision_id );
88+
if ( Visualizer_Plugin::CPT_VISUALIZER === get_post_type( $parent_id ) ) {
89+
// add the meta data to this revision.
90+
$meta = get_post_meta( $parent_id, '', true );
91+
92+
if ( $meta ) {
93+
foreach ( $meta as $key => $value ) {
94+
if ( 0 === strpos( $key, 'visualizer' ) ) {
95+
add_metadata( 'post', $revision_id, $key, maybe_unserialize( $value[0] ) );
96+
}
97+
}
98+
}
99+
}
100+
}
101+
102+
/**
103+
* Restore a revision.
104+
*/
105+
public function restoreRevision( $post_id, $revision_id ) {
106+
if ( Visualizer_Plugin::CPT_VISUALIZER === get_post_type( $post_id ) ) {
107+
// get the meta information from the revision.
108+
$meta = get_metadata( 'post', $revision_id, '', true );
109+
110+
// delete all meta information from the post before adding.
111+
$post_meta = get_post_meta( $post_id, '', true );
112+
if ( $post_meta ) {
113+
foreach ( $meta as $key => $value ) {
114+
if ( 0 === strpos( $key, 'visualizer' ) ) {
115+
delete_post_meta( $post_id, $key );
116+
}
117+
}
118+
}
119+
120+
if ( $meta ) {
121+
foreach ( $meta as $key => $value ) {
122+
if ( 0 === strpos( $key, 'visualizer' ) ) {
123+
add_post_meta( $post_id, $key, maybe_unserialize( $value[0] ) );
124+
}
125+
}
126+
}
127+
}
128+
}
129+
68130
/**
69131
* Admin init.
70132
*
@@ -441,6 +503,10 @@ public function renderLibraryPage() {
441503
$query = new WP_Query( $query_args );
442504
while ( $query->have_posts() ) {
443505
$chart = $query->next_post();
506+
507+
// 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.
508+
$chart = $this->handleExistingRevisions( $chart->ID, $chart );
509+
444510
// fetch and update settings
445511
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
446512
unset( $settings['height'], $settings['width'] );

classes/Visualizer/Module/Chart.php

Lines changed: 15 additions & 2 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
/**
@@ -273,6 +271,17 @@ public function renderChartPages() {
273271
$tab = 'settings';
274272
}
275273

274+
if ( isset( $_POST['cancel'] ) && 1 === intval( $_POST['cancel'] ) ) {
275+
// if the cancel button is clicked.
276+
$this->undoRevisions( $chart_id, true );
277+
} elseif ( isset( $_POST['save'] ) && 1 === intval( $_POST['save'] ) ) {
278+
// if the save button is clicked.
279+
$this->undoRevisions( $chart_id, false );
280+
} else {
281+
// if the edit button is clicked.
282+
$this->_chart = $this->handleExistingRevisions( $chart_id, $this->_chart );
283+
}
284+
276285
switch ( $tab ) {
277286
case 'settings':
278287
$this->_handleDataAndSettingsPage();
@@ -357,6 +366,9 @@ private function _handleDataAndSettingsPage() {
357366
$render->button = filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART
358367
? esc_html__( 'Save Chart', 'visualizer' )
359368
: esc_html__( 'Create Chart', 'visualizer' );
369+
if ( filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART ) {
370+
$render->cancel_button = esc_html__( 'Cancel', 'visualizer' );
371+
}
360372
} else {
361373
$render->button = esc_attr__( 'Insert Chart', 'visualizer' );
362374
}
@@ -599,4 +611,5 @@ private function _handleDataPage() {
599611
$this->_addAction( 'admin_head', 'renderFlattrScript' );
600612
wp_iframe( array( $render, 'render' ) );
601613
}
614+
602615
}

classes/Visualizer/Module/Setup.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public function setupCustomPostTypes() {
110110
Visualizer_Plugin::CPT_VISUALIZER, array(
111111
'label' => 'Visualizer Charts',
112112
'public' => false,
113+
'supports' => array( 'revisions' ),
113114
)
114115
);
115116
}

classes/Visualizer/Render/Page/Data.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ class="dashicons dashicons-lock"></span></h2>
254254
<form id="settings-form" action="<?php echo add_query_arg( 'nonce', wp_create_nonce() ); ?>"
255255
method="post">
256256
<?php echo $this->sidebar; ?>
257+
<input type="hidden" name="save" value="1">
258+
</form>
259+
<form id="cancel-form" action="<?php echo add_query_arg( 'nonce', wp_create_nonce() ); ?>" method="post">
260+
<input type="hidden" name="cancel" value="1">
257261
</form>
258262
</ul>
259263
</li>
@@ -415,6 +419,9 @@ protected function _renderToolbar() {
415419
echo '</div>';
416420
}
417421
echo '<input type="submit" id="settings-button" class="button button-primary button-large push-right" value="', $this->button, '">';
422+
if ( isset( $this->cancel_button ) ) {
423+
echo '<input type="submit" id="cancel-button" class="button button-secondary button-large push-right" value="', $this->cancel_button, '">';
424+
}
418425
}
419426

420427
}

css/library.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,7 @@ input:checked + .visualizer-slider:before {
287287
font-weight: bold;
288288
text-align: center;
289289
}
290+
291+
button.media-modal-close {
292+
display: none !important;
293+
}

js/frame.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@
7272
return false;
7373
});
7474

75+
$('#cancel-button').click(function () {
76+
$('#cancel-form').submit();
77+
});
78+
7579
});
7680

7781
function init_permissions(){

0 commit comments

Comments
 (0)