| @@ -395,8 +395,22 @@ | ||
| 395 | 395 | ); |
| 396 | 396 | } |
| 397 | 397 | |
| 398 | 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 | + | |
| 412 | + /** | |
| 399 | 413 | * Undo revisions for the chart, and if necessary, restore the earliest version. |
| 400 | 414 | * |
| 401 | 415 | * @return bool If any revisions were found. |
| 402 | 416 | */ |
| @@ -401,9 +415,11 @@ | ||
| 401 | 415 | * @return bool If any revisions were found. |
| 402 | 416 | */ |
| 403 | 417 | public final function undoRevisions( $chart_id, $restore = false ) { |
| 404 | 418 | 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 | + } | |
| 406 | 422 | $revisions = wp_get_post_revisions( $chart_id, array( 'order' => 'ASC' ) ); |
| 407 | 423 | if ( count( $revisions ) > 1 ) { |
| 408 | 424 | $revision_ids = array_keys( $revisions ); |
| 409 | 425 | |
| @@ -409,9 +425,9 @@ | ||
| 409 | 425 | |
| 410 | 426 | do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'found %d revisions = %s', count( $revisions ), print_r( $revision_ids, true ) ), 'debug', __FILE__, __LINE__ ); |
| 411 | 427 | |
| 412 | 428 | // 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(); | |
| 414 | 430 | |
| 415 | 431 | if ( $restore ) { |
| 416 | 432 | // restore to the oldest one i.e. the first one. |
| 417 | 433 | wp_restore_post_revision( array_shift( $revision_ids ) ); |
| @@ -430,10 +446,13 @@ | ||
| 430 | 446 | /** |
| 431 | 447 | * If existing revisions exist for the chart, restore the earliest version and then create a new revision to initiate editing. |
| 432 | 448 | */ |
| 433 | 449 | public final function handleExistingRevisions( $chart_id, $chart ) { |
| 450 | + | |
| 434 | 451 | do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'handleExistingRevisions for %d', $chart_id ), 'debug', __FILE__, __LINE__ ); |
| 435 | - | |
| 452 | + if ( get_post_type( $chart_id ) !== Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 453 | + return $chart_id; | |
| 454 | + } | |
| 436 | 455 | // undo revisions. |
| 437 | 456 | $revisions_found = $this->undoRevisions( $chart_id, true ); |
| 438 | 457 | |
| 439 | 458 | // create revision for the edit action. |