| @@ -61,12 +61,74 @@ | ||
| 61 | 61 | $this->_addFilter( 'visualizer_logger_data', 'getLoggerData' ); |
| 62 | 62 | $this->_addFilter( 'visualizer_get_chart_counts', 'getChartCountsByTypeAndMeta' ); |
| 63 | 63 | $this->_addFilter( 'visualizer_feedback_review_trigger', 'feedbackReviewTrigger' ); |
| 64 | 64 | |
| 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 | + | |
| 65 | 70 | $this->_addAction( 'admin_init', 'init' ); |
| 66 | 71 | } |
| 67 | 72 | |
| 68 | 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 | + | |
| 130 | + /** | |
| 69 | 131 | * Admin init. |
| 70 | 132 | * |
| 71 | 133 | * @access public |
| 72 | 134 | */ |
| @@ -440,8 +502,12 @@ | ||
| 440 | 502 | $charts = array(); |
| 441 | 503 | $query = new WP_Query( $query_args ); |
| 442 | 504 | while ( $query->have_posts() ) { |
| 443 | 505 | $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 | + | |
| 444 | 510 | // fetch and update settings |
| 445 | 511 | $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ); |
| 446 | 512 | unset( $settings['height'], $settings['width'] ); |
| 447 | 513 | $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| @@ -468,8 +534,9 @@ | ||
| 468 | 534 | 'create' => add_query_arg( |
| 469 | 535 | array( |
| 470 | 536 | 'action' => Visualizer_Plugin::ACTION_CREATE_CHART, |
| 471 | 537 | 'library' => 'yes', |
| 538 | + 'type' => isset( $_GET['type'] ) ? $_GET['type'] : '', | |
| 472 | 539 | ), $ajaxurl |
| 473 | 540 | ), |
| 474 | 541 | 'edit' => add_query_arg( |
| 475 | 542 | array( |