videopress_cleanup_media_library

Helper that will look for VideoPress media items that are more than 30 minutes old, that have not had anything attached to them by a wpcom upload and deletes the ghost attachment.

Description

(int) videopress_cleanup_media_library(); 

These happen primarily because of failed upload attempts.

Returns (int)

The number of items that were cleaned up.


Usage

  1. if ( !function_exists( 'videopress_cleanup_media_library' ) ) { 
  2. require_once '/modules/videopress/utility-functions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = videopress_cleanup_media_library(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/modules/videopress/utility-functions.php  
  1. function videopress_cleanup_media_library() { 
  2.  
  3. // Disable this job for now. 
  4. return 0; 
  5. $query_args = array( 
  6. 'post_type' => 'attachment',  
  7. 'post_status' => 'inherit',  
  8. 'post_mime_type' => 'video/videopress',  
  9. 'meta_query' => array( 
  10. array( 
  11. 'key' => 'videopress_status',  
  12. 'value' => 'new',  
  13. ),  
  14. ); 
  15.  
  16. $query = new WP_Query( $query_args ); 
  17.  
  18. $cleaned = 0; 
  19.  
  20. $now = current_time( 'timestamp' ); 
  21.  
  22. if ( $query->have_posts() ) { 
  23. foreach ( $query->posts as $post ) { 
  24. $post_time = strtotime( $post->post_date_gmt ); 
  25.  
  26. // If the post is older than 30 minutes, it is safe to delete it. 
  27. if ( $now - $post_time > MINUTE_IN_SECONDS * 30 ) { 
  28. // Force delete the attachment, because we don't want it appearing in the trash. 
  29. wp_delete_attachment( $post->ID, true ); 
  30.  
  31. $cleaned++; 
  32.  
  33. return $cleaned;