Sonith_Automatic_User_Deletion/sntg-automatic-user-deletion.php
2020-08-16 20:11:06 +02:00

68 lines
2.5 KiB
PHP

<?php
/*
* Plugin Name: Sonith's Automatic User Deletion
* Author: CRP92
* Description: The Plugin autmatically deletes users (except admins) that have not been logged in for more than one year and assigns the content to a default user (sntg-deleted-user)
*
* License: GPLv3 or later
* License URI: https://www.gnu.org/licenses/gpl-3.0.en.html
* Version: 0
* Text Domain: sntg-automatic-user-deletion
*/
include ('src/constants.php');
include ('src/login_tracking.php');
include ('src/cron_job.php');
/********** HELPER FUNCTIONS *********************/
if ( ! function_exists('write_log')) {
/** function for easily writing data to the log */
function write_log ( $log ) {
if ( is_array( $log ) || is_object( $log ) ) {
error_log( print_r( $log, true ) );
} else {
error_log( $log );
}
}
}
/********** END HELPER FUNCTIONS *****************/
register_activation_hook( __FILE__, 'sntg_automatic_user_deletion_activation' );
register_deactivation_hook( __FILE__, 'sntg_automatic_user_deletion_deactivation' );
add_action('admin_init', 'sntg_automatic_user_deletion_activate');
add_action('wp_login', 'sntg_aud_login_tracking');
add_action('sntg_automatic_user_deletion_cron', 'sntg_aud_cron_job');
function sntg_aud_activation(){
add_option( 'sntg_activated_plugin', 'sntg-automatic-user-deletion' );
}
/** registers new user with DELETED_USER_NAME "sntg-deleted-user" that stores all old data
* registers cron job ("sntg-check-inactive-users" - once a week)
* adds action to wp-login to set last login
*/
function sntg_automatic_user_deletion_activate(){
if (is_admin() && get_option ('sntg_activated_plugin') == 'sntg-automatic-user-deletion'){
//create new user for orphaned content
if (!username_exists(DELETED_USER_NAME)){
wp_create_user(DELETED_USER_NAME, wp_generate_password());
}
//schedule the checking function
if (!wp_next_scheduled ('sntg_automatic_user_deletion_cron')){
wp_schedule_event(time(), SCHEDULING_INTERVAL, 'sntg_automatic_user_deletion_cron');
}
}
if (username_exists(DELETED_USER_NAME) && wp_next_scheduled ('sntg_automatic_user_deletion_cron')){
delete_option( 'sntg_activated_plugin' );
}
}
function sntg_automatic_user_deletion_deactivation(){
write_log('hm...');
$timestamp = wp_next_scheduled( 'sntg_automatic_user_deletion_cron' );
wp_unschedule_event( $timestamp, 'sntg_automatic_user_deletion_cron', array() );
delete_option( 'sntg_deactivated_plugin' );
}