| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
global $wpdb; |
| 6 |
$garbage_collection_time = get_option('wppm_garbage_collection_time' ,''); |
| 7 |
if ( empty( $garbage_collection_time ) ) { |
| 8 |
update_option( 'wppm_garbage_collection_time', current_time( 'mysql' ) ); |
| 9 |
return; |
| 10 |
} |
| 11 |
$check_flag = false; |
| 12 |
$now = current_time( 'timestamp' ); |
| 13 |
$ago = strtotime($garbage_collection_time); |
| 14 |
$diff = $now - $ago; |
| 15 |
if($diff >= DAY_IN_SECONDS){ |
| 16 |
$check_flag = true; |
| 17 |
} |
| 18 |
if(!$check_flag){ |
| 19 |
return; |
| 20 |
} |
| 21 |
$attachments=$wpdb->get_results(("select id,name,date_created from {$wpdb->prefix}wppm_attachments where is_active=0")); |
| 22 |
if($attachments){ |
| 23 |
foreach($attachments as $attachment){ |
| 24 |
$created_time = $attachment->date_created; |
| 25 |
$upload_dir = wp_upload_dir(); |
| 26 |
$time = strtotime($created_time); |
| 27 |
$month = wp_date("m",$time); |
| 28 |
$year = wp_date("Y",$time); |
| 29 |
$filename = sanitize_file_name( $attachment->name ); |
| 30 |
$filepath = $upload_dir['basedir'] . '/wppm/'.'/'.$year.'/'.$month.'/'. $filename; |
| 31 |
$attachment_updated_time = strtotime($created_time); |
| 32 |
$diff = $now - $attachment_updated_time; |
| 33 |
if($diff > DAY_IN_SECONDS){ |
| 34 |
$attachment_id = absint( $attachment->id ); |
| 35 |
$wpdb->delete( |
| 36 |
$wpdb->prefix . 'wppm_attachments', |
| 37 |
array( 'id' => $attachment_id ), |
| 38 |
array( '%d' ) |
| 39 |
); |
| 40 |
if(file_exists($filepath)){ |
| 41 |
//unlink($filepath); |
| 42 |
wp_delete_file($filepath); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
update_option('wppm_garbage_collection_time', current_time( 'mysql' ) ); |
| 48 |
|
| 49 |
|