Skip to content

Commit c55746d

Browse files
Merge pull request #250 from contactashish13/issue-243
add revision support
2 parents 4ddd631 + 978cbfe commit c55746d

File tree

7 files changed

+383
-6
lines changed

7 files changed

+383
-6
lines changed

classes/Visualizer/Module.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public function __construct( Visualizer_Plugin $plugin ) {
6363

6464
$this->_wpdb = $wpdb;
6565
$this->_plugin = $plugin;
66+
67+
$this->_addFilter( Visualizer_Plugin::FILTER_UNDO_REVISIONS, 'undoRevisions', 10, 2 );
68+
$this->_addFilter( Visualizer_Plugin::FILTER_HANDLE_REVISIONS, 'handleExistingRevisions', 10, 2 );
69+
6670
}
6771

6872
/**
@@ -328,17 +332,21 @@ private function _getHTML( $rows ) {
328332
*
329333
* @return bool If any revisions were found.
330334
*/
331-
protected function undoRevisions( $chart_id, $restore = false ) {
335+
public final function undoRevisions( $chart_id, $restore = false ) {
336+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'undoRevisions for %d with%s restore', $chart_id, ( $restore ? '' : 'out' ) ), 'debug', __FILE__, __LINE__ );
337+
332338
$revisions = wp_get_post_revisions( $chart_id, array( 'order' => 'ASC' ) );
333-
if ( $revisions ) {
339+
if ( count( $revisions ) > 1 ) {
334340
$revision_ids = array_keys( $revisions );
335341

342+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'found %d revisions = %s', count( $revisions ), print_r( $revision_ids, true ) ), 'debug', __FILE__, __LINE__ );
343+
336344
// when we restore, a new revision is likely to be created. so, let's disable revisions for the time being.
337345
add_filter( 'wp_revisions_to_keep', '__return_false' );
338346

339347
if ( $restore ) {
340348
// restore to the oldest one i.e. the first one.
341-
wp_restore_post_revision( $revision_ids[0] );
349+
wp_restore_post_revision( array_shift( $revision_ids ) );
342350
}
343351

344352
// delete all revisions.
@@ -354,9 +362,12 @@ protected function undoRevisions( $chart_id, $restore = false ) {
354362
/**
355363
* If existing revisions exist for the chart, restore the earliest version and then create a new revision to initiate editing.
356364
*/
357-
protected function handleExistingRevisions( $chart_id, $chart ) {
365+
public final function handleExistingRevisions( $chart_id, $chart ) {
366+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'handleExistingRevisions for %d', $chart_id ), 'debug', __FILE__, __LINE__ );
367+
358368
// undo revisions.
359369
$revisions_found = $this->undoRevisions( $chart_id, true );
370+
360371
// create revision for the edit action.
361372
wp_save_post_revision( $chart_id );
362373

classes/Visualizer/Module/Chart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public function renderChartPages() {
267267
$tab = isset( $_GET['tab'] ) && ! empty( $_GET['tab'] ) ? $_GET['tab'] : 'visualizer';
268268

269269
// skip chart type pages only for existing charts.
270-
if ( VISUALIZER_SKIP_CHART_TYPE_PAGE && 'auto-draft' !== $this->_chart->post_status && 'visualizer' === $_GET['tab'] ) {
270+
if ( VISUALIZER_SKIP_CHART_TYPE_PAGE && 'auto-draft' !== $this->_chart->post_status && ( ! empty( $_GET['tab'] ) && 'visualizer' === $_GET['tab'] ) ) {
271271
$tab = 'settings';
272272
}
273273

classes/Visualizer/Module/Frontend.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ public function renderChart( $atts ) {
197197
return '';
198198
}
199199

200+
// in case revisions exist.
201+
if ( true === ( $revisions = $this->undoRevisions( $chart->ID, true ) ) ) {
202+
$chart = get_post( $chart->ID );
203+
}
204+
200205
$id = 'visualizer-' . $atts['id'];
201206
$defaultClass = 'visualizer-front';
202207
$class = apply_filters( Visualizer_Plugin::FILTER_CHART_WRAPPER_CLASS, $atts['class'], $atts['id'] );

classes/Visualizer/Plugin.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class Visualizer_Plugin {
6262
const FILTER_GET_CHART_SERIES = 'visualizer-get-chart-series';
6363
const FILTER_GET_CHART_DATA = 'visualizer-get-chart-data';
6464
const FILTER_GET_CHART_SETTINGS = 'visualizer-get-chart-settings';
65+
const FILTER_UNDO_REVISIONS = 'visualizer-undo-revisions';
66+
const FILTER_HANDLE_REVISIONS = 'visualizer-handle-revisions';
6567

6668
const CF_CHART_URL = 'visualizer-chart-url';
6769
const CF_CHART_SCHEDULE = 'visualizer-chart-schedule';
@@ -97,6 +99,9 @@ class Visualizer_Plugin {
9799
* @access private
98100
*/
99101
private function __construct() {
102+
if ( VISUALIZER_DEBUG ) {
103+
add_action( 'themeisle_log_event', array( $this, 'themeisle_log_event_debug' ), 10, 5 );
104+
}
100105
}
101106

102107
/**
@@ -184,4 +189,14 @@ public function setModule( $class ) {
184189
private function __clone() {
185190
}
186191

192+
/**
193+
* For local testing, overrides the 'themeisle_log_event' hook and redirects to error.log.
194+
*/
195+
final function themeisle_log_event_debug( $name, $message, $type, $file, $line ) {
196+
if ( Visualizer_Plugin::NAME !== $name ) {
197+
return;
198+
}
199+
error_log( sprintf( '%s (%s): %s in %s:%s', $name, $type, $message, $file, $line ) );
200+
}
201+
187202
}

classes/Visualizer/Render/Page/Data.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,10 @@ private function getPermissionsLink( $id ) {
314314
* @access private
315315
*/
316316
private function permissionsSidebar() {
317+
// ignore for unit tests because Travis throws the error "Indirect modification of overloaded property Visualizer_Render_Page_Data::$permissions has no effect".
318+
if ( defined( 'WP_TESTS_DOMAIN' ) ) {
319+
return;
320+
}
317321
Visualizer_Render_Sidebar::_renderGroupStart(
318322
esc_html__( 'Who can see this chart?', 'visualizer' ) . '<span
319323
class="dashicons dashicons-lock"></span>', '', apply_filters( 'visualizer_pro_upsell_class', 'only-pro-feature', 'chart-permissions' )

index.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,17 @@ function visualizer_launch() {
7575
if ( ! defined( 'VISUALIZER_CSV_ENCLOSURE' ) ) {
7676
define( 'VISUALIZER_CSV_ENCLOSURE', '"' );
7777
}
78+
if ( ! defined( 'VISUALIZER_DEBUG' ) ) {
79+
define( 'VISUALIZER_DEBUG', false );
80+
}
81+
7882
// instantiate the plugin
7983
$plugin = Visualizer_Plugin::instance();
8084
// set general modules
8185
$plugin->setModule( Visualizer_Module_Setup::NAME );
8286
$plugin->setModule( Visualizer_Module_Sources::NAME );
8387
$plugin->setModule( Visualizer_Module_Chart::NAME );
84-
if ( is_admin() ) {
88+
if ( is_admin() || defined( 'WP_TESTS_DOMAIN' ) ) {
8589
// set admin modules
8690
$plugin->setModule( Visualizer_Module_Admin::NAME );
8791
} else {

0 commit comments

Comments
 (0)