ultimatemember / Extended

All custom extended features and codes
16 stars 14 forks source link

Deleting users with cronjob #88

Closed Banana828 closed 3 months ago

Banana828 commented 4 months ago

Hello,

I am trying to delete users that keep on the status "awaiting email confirmation". I installed the plugin, but I don't see it anywhere in my WordPress admin.

Then I found this piece of code:

if (!wp_next_scheduled('um_cron_delete_users')) {
    wp_schedule_event(time(), 'daily', 'um_cron_delete_users');
}
add_action('um_cron_delete_users', 'um_cron_delete_waitingforemail');
function um_cron_delete_waitingforemail()
{    
    $args = array(
        'fields'     => 'ID',
        'number'     => -1,
        'date_query' => array(
            array('after'  => '5 days ago midnight', 'inclusive' => true),
        ),
        'meta_query' => array(
            "relation" => "AND",
            array(
                "key" => "status",
                "value" => "awaiting_email_confirmation",
                "compare" => "="
            )
        )
    );

    $users = get_users($args);

    foreach ($users as $user_id) {
        um_fetch_user($user_id);
        UM()->user()->delete(false);
    }
}

And I installed WP Control. I can see the scheduled task, but when I say 'run now' nothing happens. I have a user registered on April 1st and the status is 'awaiting_email_confirmation'.

I created another scheduled event with some database action, really low-key. When it runs it should add something to a table. When I run that it works.

What am I missing?

champsupertramp commented 3 months ago

Hi @Banana828 - You have an issue with retrieving the user ID.

Change this:

foreach ($users as $user_id) {
   um_fetch_user($user_id);
   UM()->user()->delete(false);
}

to

foreach ( $users as $user ) {
    um_fetch_user( $user->ID );
    UM()->user()->delete();
}

You may also check our new implementation in this file: https://github.com/ultimatemember/Extended/blob/main/src/um-cron-delete/src/Core.php#L27-L59

Banana828 commented 3 months ago

Hi @champsupertramp

I changed the code as you suggested, but it still isn't working.

This is the user:

image

I have copied the code of the link you gave, but nothing seems to be happening. It should delete the user, but it doesn't.