@@ -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 */
0 commit comments