Skip to content

Commit e40872c

Browse files
pdclarkHere
authored andcommitted
Clean up attachments from incomplete Pods form uploads
Tag file uploads with pending meta on AJAX upload, remove the flag when the form is saved, and schedule a daily cron to delete attachments that remain flagged and are older than 12 hours. Fixes #7459
1 parent 27fb556 commit e40872c

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

classes/PodsInit.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,7 @@ public function setup_content_types( $force = false ) {
17681768

17691769
new PodsRESTFields( $post_type_name );
17701770
}
1771+
17711772
}//end foreach
17721773

17731774
foreach ( $existing_taxonomies as $taxonomy_name => $taxonomy_name_again ) {
@@ -1797,6 +1798,7 @@ public function setup_content_types( $force = false ) {
17971798

17981799
new PodsRESTFields( $taxonomy_name );
17991800
}
1801+
18001802
}//end foreach
18011803

18021804
if ( ! empty( PodsMeta::$user ) ) {
@@ -2217,6 +2219,8 @@ public function deactivate() {
22172219

22182220
delete_option( 'pods_callouts' );
22192221

2222+
wp_clear_scheduled_hook( 'pods_cleanup_pending_form_uploads' );
2223+
22202224
pods_api()->cache_flush_pods();
22212225

22222226
}
@@ -2551,6 +2555,13 @@ public function run() {
25512555
add_filter( 'post_updated_messages', [ $this, 'setup_updated_messages' ], 10, 1 );
25522556
add_action( 'delete_attachment', [ $this, 'delete_attachment' ] );
25532557

2558+
// Schedule daily cleanup of attachments uploaded to forms that were never completed.
2559+
if ( ! wp_next_scheduled( 'pods_cleanup_pending_form_uploads' ) ) {
2560+
wp_schedule_event( time(), 'daily', 'pods_cleanup_pending_form_uploads' );
2561+
}
2562+
2563+
add_action( 'pods_cleanup_pending_form_uploads', [ $this, 'cleanup_pending_form_uploads' ] );
2564+
25542565
// Register widgets
25552566
add_action( 'widgets_init', [ $this, 'register_widgets' ] );
25562567

@@ -2685,6 +2696,37 @@ public function delete_attachment( $_ID ) {
26852696
do_action( 'pods_init_delete_attachment', $_ID );
26862697
}
26872698

2699+
/**
2700+
* Delete attachments that were uploaded to Pods forms that were never completed.
2701+
*
2702+
* Skips recently uploaded attachments to avoid removing
2703+
* files for forms still being filled out.
2704+
*/
2705+
public function cleanup_pending_form_uploads() {
2706+
$time_threshold = time() - 12 * HOUR_IN_SECONDS;
2707+
2708+
$attachments = get_posts( [
2709+
'post_type' => 'attachment',
2710+
'meta_key' => '_pods_pending_form_upload',
2711+
'fields' => 'ids',
2712+
'nopaging' => true,
2713+
] );
2714+
2715+
if ( empty( $attachments ) ) {
2716+
return;
2717+
}
2718+
2719+
foreach ( $attachments as $attachment_id ) {
2720+
$uploaded_at = (int) get_post_meta( $attachment_id, '_pods_pending_form_upload', true );
2721+
2722+
if ( $uploaded_at > $time_threshold ) {
2723+
continue;
2724+
}
2725+
2726+
wp_delete_attachment( (int) $attachment_id, true );
2727+
}
2728+
}
2729+
26882730
/**
26892731
* Register widgets for Pods
26902732
*/

classes/fields/file.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,9 @@ public function save( $value, $id = null, $name = null, $options = null, $fields
766766
continue;
767767
}
768768

769+
// Form submitted: remove pending upload flag.
770+
delete_post_meta( $attachment_id, '_pods_pending_form_upload' );
771+
769772
// Check whether attachment is a valid image type.
770773
if ( null === $first_attachment_id && 'image/' === substr( $attachment->post_mime_type, 0, 6 ) ) {
771774
$first_attachment_id = $attachment_id;
@@ -1341,6 +1344,9 @@ public function admin_ajax_upload() {
13411344

13421345
pods_error( '<div style="color:#FF0000">Error: ' . implode( '</div><div>', $errors ) . '</div>' );
13431346
} else {
1347+
// Mark as pending so incomplete form uploads can be cleaned up later.
1348+
update_post_meta( $attachment_id, '_pods_pending_form_upload', time() );
1349+
13441350
$attachment = get_post( $attachment_id, ARRAY_A );
13451351

13461352
$attachment['filename'] = basename( $attachment['guid'] );

0 commit comments

Comments
 (0)