| @@ -62,8 +62,12 @@ | ||
| 62 | 62 | global $wpdb; |
| 63 | 63 | |
| 64 | 64 | $this->_wpdb = $wpdb; |
| 65 | 65 | $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 | + | |
| 66 | 70 | } |
| 67 | 71 | |
| 68 | 72 | /** |
| 69 | 73 | * Registers an action hook. |
| @@ -320,7 +324,75 @@ | ||
| 320 | 324 | </html>'; |
| 321 | 325 | return array( |
| 322 | 326 | 'csv' => $html, |
| 323 | 327 | ); |
| 328 | + } | |
| 329 | + | |
| 330 | + /** | |
| 331 | + * Undo revisions for the chart, and if necessary, restore the earliest version. | |
| 332 | + * | |
| 333 | + * @return bool If any revisions were found. | |
| 334 | + */ | |
| 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 | + | |
| 338 | + $revisions = wp_get_post_revisions( $chart_id, array( 'order' => 'ASC' ) ); | |
| 339 | + if ( count( $revisions ) > 1 ) { | |
| 340 | + $revision_ids = array_keys( $revisions ); | |
| 341 | + | |
| 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 | + | |
| 344 | + // when we restore, a new revision is likely to be created. so, let's disable revisions for the time being. | |
| 345 | + add_filter( 'wp_revisions_to_keep', '__return_false' ); | |
| 346 | + | |
| 347 | + if ( $restore ) { | |
| 348 | + // restore to the oldest one i.e. the first one. | |
| 349 | + wp_restore_post_revision( array_shift( $revision_ids ) ); | |
| 350 | + } | |
| 351 | + | |
| 352 | + // delete all revisions. | |
| 353 | + foreach ( $revision_ids as $id ) { | |
| 354 | + wp_delete_post_revision( $id ); | |
| 355 | + } | |
| 356 | + | |
| 357 | + return true; | |
| 358 | + } | |
| 359 | + return false; | |
| 360 | + } | |
| 361 | + | |
| 362 | + /** | |
| 363 | + * If existing revisions exist for the chart, restore the earliest version and then create a new revision to initiate editing. | |
| 364 | + */ | |
| 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 | + | |
| 368 | + // undo revisions. | |
| 369 | + $revisions_found = $this->undoRevisions( $chart_id, true ); | |
| 370 | + | |
| 371 | + // create revision for the edit action. | |
| 372 | + wp_save_post_revision( $chart_id ); | |
| 373 | + | |
| 374 | + if ( $revisions_found ) { | |
| 375 | + // fetch chart data again in case it was updated by an earlier revision. | |
| 376 | + $chart = get_post( $chart_id ); | |
| 377 | + } | |
| 378 | + return $chart; | |
| 379 | + } | |
| 380 | + | |
| 381 | + /** | |
| 382 | + * Returns the language of the locale. | |
| 383 | + * | |
| 384 | + * @access protected | |
| 385 | + */ | |
| 386 | + protected function get_language() { | |
| 387 | + $locale = get_locale(); | |
| 388 | + if ( empty( $locale ) ) { | |
| 389 | + return ''; | |
| 390 | + } | |
| 391 | + $array = explode( '_', $locale ); | |
| 392 | + if ( count( $array ) < 2 ) { | |
| 393 | + return ''; | |
| 394 | + } | |
| 395 | + return reset( $array ); | |
| 324 | 396 | } |
| 325 | 397 | |
| 326 | 398 | } |