Skip to content

Commit 5c0104b

Browse files
add revision support #243
1 parent 950f479 commit 5c0104b

File tree

6 files changed

+115
-0
lines changed

6 files changed

+115
-0
lines changed

classes/Visualizer/Module/Admin.php

Lines changed: 62 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
*

classes/Visualizer/Module/Chart.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,17 @@ public function renderChartPages() {
273273
$tab = 'settings';
274274
}
275275

276+
if ( isset( $_POST['cancel'] ) && 1 === intval( $_POST['cancel'] ) ) {
277+
// if the cancel button is clicked.
278+
$this->undoRevisions( $chart_id, true );
279+
} elseif ( isset( $_POST['save'] ) && 1 === intval( $_POST['save'] ) ) {
280+
// if the save button is clicked.
281+
$this->undoRevisions( $chart_id, false );
282+
} else {
283+
// if the edit button is clicked.
284+
$rev = wp_save_post_revision( $chart_id );
285+
}
286+
276287
switch ( $tab ) {
277288
case 'settings':
278289
$this->_handleDataAndSettingsPage();
@@ -356,6 +367,9 @@ private function _handleDataAndSettingsPage() {
356367
$render->button = filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART
357368
? esc_html__( 'Save Chart', 'visualizer' )
358369
: esc_html__( 'Create Chart', 'visualizer' );
370+
if ( filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART ) {
371+
$render->cancel_button = esc_html__( 'Cancel', 'visualizer' );
372+
}
359373
} else {
360374
$render->button = esc_attr__( 'Insert Chart', 'visualizer' );
361375
}
@@ -598,4 +612,27 @@ private function _handleDataPage() {
598612
$this->_addAction( 'admin_head', 'renderFlattrScript' );
599613
wp_iframe( array( $render, 'render' ) );
600614
}
615+
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+
}
601638
}

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)