first commit - seems to work
This commit is contained in:
commit
06480007e9
67
sntg-automatic-user-deletion.php
Normal file
67
sntg-automatic-user-deletion.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?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' );
|
||||
}
|
||||
7
src/constants.php
Normal file
7
src/constants.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
define ('SEC_TO_MONTH', 24*60*60);
|
||||
define ('USER_DELETION_DELAY', 12); //how many months does plugin wait before users are deleted?
|
||||
define ('DELETION_DELETION_DELAY',36); //how many months does the plugin wait before user deletion information is deleted
|
||||
define ('DELETED_USER_NAME', 'sntg-deleted-user'); //name of user that "collects" deleted users content (posts, ...)
|
||||
define ('SCHEDULING_INTERVAL', 'weekly'); //how often should the cron job run?
|
||||
define ('SUBJECT', 'Baldige Löschung Ihres Benutzerkontos'); //subject of the warning email the user gets
|
||||
122
src/cron_job.php
Normal file
122
src/cron_job.php
Normal file
@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
/** cron hooking function
|
||||
* deletes users that were prepared last week
|
||||
* prepares users for deletion next week
|
||||
* deletes deletion info that is older than DELETION_DELETION_DELAY (months)
|
||||
*/
|
||||
function sntg_aud_cron_job(){
|
||||
delete_prepared_users();
|
||||
prepare_users_for_deletion();
|
||||
delete_deletion_entries();
|
||||
}
|
||||
|
||||
/** delete users that were prepared the last time the job ran */
|
||||
function delete_prepared_users(){
|
||||
$users = get_users( array( 'fields' => array( 'ID' ) ) );
|
||||
foreach($users as $user){
|
||||
$usermeta = get_user_meta ( $user->ID, 'sntg_user_to_be_deleted');
|
||||
if (sizeof($usermeta) == 1){
|
||||
write_log("user " . $user->ID . " has to be deleted: " .$usermeta[0]);
|
||||
delete_user($user->ID);
|
||||
}
|
||||
else {
|
||||
echo var_dump($usermeta);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** add db entries for non-admin users (wp_user_level < 10)
|
||||
* that have not been logged in for longer than USER_DELETION_DELAY (->constants.php)
|
||||
*
|
||||
*/
|
||||
function prepare_users_for_deletion(){
|
||||
//write_log("prepare users for deletion");
|
||||
$users = get_users( array( 'fields' => array( 'ID' ) ) );
|
||||
foreach($users as $user){
|
||||
$wp_user_level = get_user_meta ( $user->ID, 'wp_user_level', true);
|
||||
$user_object = get_user_by('id', $user->ID);
|
||||
//only delete non admins
|
||||
if (($wp_user_level < 10) && ($user_object->user_login != DELETED_USER_NAME )){
|
||||
$last_login = get_user_meta ($user->ID, 'sntg_last_login', true);
|
||||
//if no entry exists, create one with date now
|
||||
if ($last_login == ''){
|
||||
update_user_meta($user->ID, 'sntg_last_login', time(), true);
|
||||
}
|
||||
//otherwise check if it was more then USER_DELETION_DELAY (one year) ago
|
||||
elseif ( ( (time() - $last_login) / SEC_TO_MONTH) > USER_DELETION_DELAY){
|
||||
update_user_meta($user->ID, 'sntg_user_to_be_deleted', time(), true);
|
||||
write_log("deletion mail sent: " . send_deletion_warning_mail($user->ID));
|
||||
write_log($user_object->user_login . " prepared for deletion");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** creates an db entry with info about deletion, reassigns users content to default user (DELETED_USER_NAME -> constants.php) and deletes the user */
|
||||
function delete_user($old_user_ID){
|
||||
$reassign_user = get_user_by('login', DELETED_USER_NAME);
|
||||
$reassign_user_id = $reassign_user->ID;
|
||||
write_log ("deleting user with id: " . $old_user_ID);
|
||||
create_deletion_entry($old_user_ID);
|
||||
require_once(ABSPATH.'wp-admin/includes/user.php');
|
||||
wp_delete_user($old_user_ID, $reassign_user_id);
|
||||
}
|
||||
|
||||
/** creates a post with info about deleted user */
|
||||
function create_deletion_entry($user_id){
|
||||
$user = get_user_by('id', $user_id);
|
||||
$info = "user id: " . $user_id . "; " .
|
||||
"email: " . $user->user_email . '; ' .
|
||||
"username: " . $user->user_login .";";
|
||||
wp_insert_post(array(
|
||||
'post_type' => 'sntg_ud_info',
|
||||
'post_content' => $info,
|
||||
'post_status' => 'publish'
|
||||
));
|
||||
}
|
||||
|
||||
/** sends an email (templates/info_mail.php) to user with subject SUBJECT (->constants.php) */
|
||||
function send_deletion_warning_mail($user_id){
|
||||
$user = get_user_by('id', $user_id);
|
||||
add_filter( 'wp_mail_content_type', function( $content_type ) {
|
||||
return 'text/html';
|
||||
});
|
||||
$subject = get_bloginfo ('name') . ': ' . SUBJECT; //set subject in constants.php
|
||||
|
||||
ob_start();
|
||||
include (ABSPATH . 'wp-content/plugins/sntg-automatic-user-deletion/templates/info_mail.php');
|
||||
$message = ob_get_clean();
|
||||
|
||||
wp_mail($user->user_email, $subject, $message);
|
||||
}
|
||||
|
||||
/** after DELETION_DELETION_DELAY, most of the info about deleted users (eg email address)
|
||||
* has to be deleted as well (DSGVO), but the info that a user was deleted has to be stored*/
|
||||
function delete_deletion_entries(){
|
||||
$delay_years = intdiv( DELETION_DELETION_DELAY, 12 );
|
||||
$delay_months = DELETION_DELETION_DELAY % 12;
|
||||
$entries_to_delete = get_posts(array(
|
||||
'post_type' => 'sntg_ud_info',
|
||||
'numberposts' => '-1',
|
||||
'date_query' => array(
|
||||
array(
|
||||
'before' => array(
|
||||
'year' => date('Y') - $delay_years,
|
||||
'month' => date('n') - $delay_months,
|
||||
),
|
||||
'inclusive' => true,
|
||||
)
|
||||
),
|
||||
));
|
||||
foreach ($entries_to_delete as $entry){
|
||||
$info = "deleted deletion info. post_id: " . $entry->ID . "; " . explode(";", $entry->post_content)[0];
|
||||
write_log($info);
|
||||
wp_insert_post(array(
|
||||
'post_type' => 'sntg_ud_deletion',
|
||||
'post_content' => $info,
|
||||
'post_status' => 'publish'
|
||||
));
|
||||
wp_delete_post ($entry->ID);
|
||||
}
|
||||
}
|
||||
11
src/login_tracking.php
Normal file
11
src/login_tracking.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
/** login tracking function */
|
||||
function sntg_aud_login_tracking(){
|
||||
$user_id = get_current_user_id();
|
||||
//update last login time
|
||||
write_log('sntg_automatic_user_deletion_login_tracking');
|
||||
update_user_meta($user_id, 'sntg_last_login', time());
|
||||
//remove deletion entry if it exists
|
||||
delete_user_meta($user_id, 'sntg_user_to_be_deleted');
|
||||
}
|
||||
13
templates/info_mail.php
Normal file
13
templates/info_mail.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
<h1>Hallo <?php echo $user->login ?></h1>
|
||||
|
||||
<p>Du hast dein Nutzerkonto auf <a href="<?php echo get_bloginfo('url') ?> "> <?php echo get_bloginfo ('name')?> </a> seit ca. einem Jahr nicht mehr genutzt. Aus Datenschutzgründen wird es daher in einer Woche gelöscht, falls du dich bis dahin nicht einloggst. <p>
|
||||
|
||||
<p>Liebe Grüße, <br> <?php echo get_bloginfo ('name')?> - <?php echo get_bloginfo ('description')?>
|
||||
Loading…
x
Reference in New Issue
Block a user