grunion_delete_old_spam

Deletes old spam feedbacks to keep the posts table size under control.

Description

grunion_delete_old_spam(); 

Usage

  1. if ( !function_exists( 'grunion_delete_old_spam' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'jetpack-by-wordpress-com/modules/contact-form/grunion-contact-form.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = grunion_delete_old_spam(); 
  7.  

Defined (1)

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

/modules/contact-form/grunion-contact-form.php  
  1. function grunion_delete_old_spam() { 
  2. global $wpdb; 
  3.  
  4. $grunion_delete_limit = 100; 
  5.  
  6. $now_gmt = current_time( 'mysql', 1 ); 
  7. $sql = $wpdb->prepare( " 
  8. SELECT `ID` 
  9. FROM $wpdb->posts 
  10. WHERE DATE_SUB( %s, INTERVAL 15 DAY ) > `post_date_gmt` 
  11. AND `post_type` = 'feedback' 
  12. AND `post_status` = 'spam' 
  13. LIMIT %d 
  14. ", $now_gmt, $grunion_delete_limit ); 
  15. $post_ids = $wpdb->get_col( $sql ); 
  16.  
  17. foreach ( (array) $post_ids as $post_id ) { 
  18. # force a full delete, skip the trash 
  19. wp_delete_post( $post_id, TRUE ); 
  20.  
  21. # Arbitrary check points for running OPTIMIZE 
  22. # nothing special about 5000 or 11 
  23. # just trying to periodically recover deleted rows 
  24. $random_num = mt_rand( 1, 5000 ); 
  25. if ( 
  26. /** 
  27. * Filter how often the module run OPTIMIZE TABLE on the core WP tables. 
  28. * @module contact-form 
  29. * @since 1.3.1 
  30. * @param int $random_num Random number. 
  31. */ 
  32. apply_filters( 'grunion_optimize_table', ( $random_num == 11 ) ) 
  33. ) { 
  34. $wpdb->query( "OPTIMIZE TABLE $wpdb->posts" ); 
  35.  
  36. # if we hit the max then schedule another run 
  37. if ( count( $post_ids ) >= $grunion_delete_limit ) { 
  38. wp_schedule_single_event( time() + 700, 'grunion_scheduled_delete' );