|
| 1 | +<?php |
| 2 | + |
| 3 | +defined('ABSPATH') or die('No script kiddies please!'); |
| 4 | + |
| 5 | +/* |
| 6 | + This script checks for accounts who needs to be removed according to the new rejection policy |
| 7 | + - Applications which were not completed/submitted |
| 8 | + - Applications which failed to update their vouchers |
| 9 | + - Applications which were rejected by the Membership Committee (30 days after the rejection was sent) |
| 10 | +
|
| 11 | + This code may close an application! |
| 12 | + */ |
| 13 | + |
| 14 | +//////////////////////////////////////////////////////////////////////////////// |
| 15 | +// Defines |
| 16 | +//////////////////////////////////////////////////////////////////////////////// |
| 17 | + |
| 18 | +// Be careful changing this value, you may send a reminder sooner than expected |
| 19 | +// Also beware any knock-on effect on CCGN_CLOSE_UPDATE_VOUCHERS_AFTER_DAYS |
| 20 | +define('CCGN_REMOVE_APPLICATION_AFTER_DAYS', 30); |
| 21 | + |
| 22 | + |
| 23 | +//////////////////////////////////////////////////////////////////////////////// |
| 24 | +// Checking and sending |
| 25 | +//////////////////////////////////////////////////////////////////////////////// |
| 26 | + |
| 27 | +// remove accounts after retention period |
| 28 | + |
| 29 | +function ccgn_close_and_remove_retention_data_applicant($applicant_id) |
| 30 | +{ |
| 31 | + _ccgn_application_delete_entries_created_by($applicant_id); |
| 32 | + delete_user_meta($applicant_id, CCGN_APPLICATION_TYPE); |
| 33 | + delete_user_meta($applicant_id, CCGN_APPLICATION_STATE); |
| 34 | + delete_user_meta($applicant_id, CCGN_USER_IS_AUTOVOUCHED); |
| 35 | + |
| 36 | + $delete = wp_delete_user($applicant_id); |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +// Send reminders to those that need them |
| 41 | + |
| 42 | +function ccgn_check_accounts_to_be_removed() |
| 43 | +{ |
| 44 | + $states_to_delete = array( |
| 45 | + CCGN_APPLICATION_STATE_DELETE, |
| 46 | + CCGN_APPLICATION_STATE_DIDNT_UPDATE_VOUCHERS |
| 47 | + ); |
| 48 | + $now = new DateTime('now'); |
| 49 | + foreach ($states_to_delete as $state) { |
| 50 | + $applicants = ccgn_applicant_ids_with_state( $state ); |
| 51 | + foreach ($applicants as $applicant_id) { |
| 52 | + $status_date = get_user_meta($applicant_id, 'ccgn-application-state-date', true); |
| 53 | + $state_date = new DateTime($status_date); |
| 54 | + $days_in_state = $state_date->diff($now)->days; |
| 55 | + if ($days_in_state > CCGN_REMOVE_APPLICATION_AFTER_DAYS) { |
| 56 | + // Maybe it's a good Idea to log the application that have been deleted (just the name, according to date retention) |
| 57 | + ccgn_close_and_remove_retention_data_applicant($applicant_id); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +function ccgn_schedule_add_retention_data() |
| 64 | +{ |
| 65 | + if (!wp_next_scheduled('ccgn_check_accounts_to_be_removed')) { |
| 66 | + wp_schedule_event( |
| 67 | + time(), |
| 68 | + 'daily', |
| 69 | + 'ccgn_check_accounts_to_be_removed' |
| 70 | + ); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +function ccgn_schedule_remove_retention_data() |
| 75 | +{ |
| 76 | + wp_clear_scheduled_hook('ccgn_check_accounts_to_be_removed'); |
| 77 | +} |
0 commit comments