Skip to content

Commit c2b5615

Browse files
delete old auto-draft posts
1 parent e5b1803 commit c2b5615

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

classes/Visualizer/Module/Chart.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,28 @@ public function deleteChart() {
237237
exit;
238238
}
239239

240+
/**
241+
* Delete charts that are still in auto-draft mode.
242+
*/
243+
private function deleteOldCharts() {
244+
$query = new WP_Query(
245+
array(
246+
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
247+
'post_status' => 'auto-draft',
248+
'fields' => 'ids',
249+
'update_post_meta_cache' => false,
250+
'update_post_term_cache' => false,
251+
'posts_per_page' => 50,
252+
)
253+
);
254+
if ( $query->have_posts() ) {
255+
$ids = array();
256+
while ( $query->have_posts() ) {
257+
wp_delete_post( $query->next_post(), true );
258+
}
259+
}
260+
}
261+
240262
/**
241263
* Renders appropriate page for chart builder. Creates new auto draft chart
242264
* if no chart has been specified.
@@ -250,6 +272,7 @@ public function renderChartPages() {
250272
// check chart, if chart not exists, will create new one and redirects to the same page with proper chart id
251273
$chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : '';
252274
if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) {
275+
$this->deleteOldCharts();
253276
$default_type = isset( $_GET['type'] ) && ! empty( $_GET['type'] ) ? $_GET['type'] : 'line';
254277
$source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $default_type . '.csv' );
255278
$source->fetch();

0 commit comments

Comments
 (0)