Skip to content

Instantly share code, notes, and snippets.

@robertdevore
Last active November 11, 2024 22:54
Show Gist options
  • Save robertdevore/a15d1ad387a5d89504bdaf5eb99c3964 to your computer and use it in GitHub Desktop.
Save robertdevore/a15d1ad387a5d89504bdaf5eb99c3964 to your computer and use it in GitHub Desktop.
<?php
/**
* The plugin bootstrap file
*
* @link https://robertdevore.com
* @since 1.0.0
* @package Customer_Cleanup
*
* @wordpress-plugin
*
* Plugin Name: Customer Cleanup
* Description: Deletes customer role users who have never made a purchase and have been registered for more than 30 days. Logs the cleanup process to a CSV file.
* Plugin URI: https://robertdevore.com/
* Version: 1.0.0
* Author: Robert DeVore
* Author URI: https://robertdevore.com/
* License: GPL-2.0+
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: customer-cleanup
* Domain Path: /languages
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Current plugin version.
*/
define( 'CUSTOMER_CLEANUP_PLUGIN_VERSION', '1.0.0' );
/**
* Schedule cron job on plugin activation
*
* @since 1.0.0
* @return void
*/
function customer_cleanup_activate() {
if ( ! wp_next_scheduled( 'customer_cleanup_cleanup_users' ) ) {
wp_schedule_event( time(), 'daily', 'customer_cleanup_cleanup_users' );
}
}
register_activation_hook( __FILE__, 'customer_cleanup_activate' );
/**
* Cleanup users
*
* @since 1.0.0
* @return void
*/
function customer_cleanup_cleanup_users() {
$args = array(
'role' => 'customer',
'fields' => 'ID',
);
$customer_users = get_users( $args );
$deleted_users = array();
foreach ( $customer_users as $customer_id ) {
$user_registered = get_user_meta( $customer_id, 'registered', true );
$registered_timestamp = strtotime( $user_registered );
$current_timestamp = current_time( 'timestamp' );
$days_registered = floor( ( $current_timestamp - $registered_timestamp ) / ( 60 * 60 * 24 ) );
if ( $days_registered > 30 ) {
$args = array(
'customer_id' => $customer_id,
'status' => 'completed',
);
$orders = wc_get_orders( $args );
// If the customer has no completed orders, delete the user and log the deletion.
if ( empty( $orders ) ) {
wp_delete_user( $customer_id );
$deleted_users[] = $customer_id;
}
}
}
if ( ! empty( $deleted_users ) ) {
$timestamp = date( 'YmdHis' );
$log_folder = WP_CONTENT_DIR . '/customercleanup/';
if ( ! file_exists( $log_folder ) ) {
wp_mkdir_p( $log_folder );
touch( $log_folder . 'index.php' );
file_put_contents( $log_folder . '.htaccess', 'Deny from all' );
}
$log_file = $log_folder . 'cleanup_log_' . $timestamp . '.csv';
$file_handle = fopen( $log_file, 'w' );
if ( $file_handle ) {
fputcsv( $file_handle, array( 'User ID' ) );
foreach ( $deleted_users as $user_id ) {
fputcsv( $file_handle, array( $user_id ) );
}
fclose( $file_handle );
}
}
}
add_action( 'customer_cleanup_cleanup_users', 'customer_cleanup_cleanup_users' );
/**
* Display message
*
* @since 1.0.0
* @return string
*/
function customer_cleanup_display_message() {
$customer_users_count = count( get_users( array( 'role' => 'customer' ) ) );
$deleted_users_count = $customer_users_count - count( $orders );
echo '<div class="notice notice-success is-dismissible">';
echo '<p>Deleted ' . $deleted_users_count . 'customer role users who have never made a purchase and have been registered for more than 30 days.</p>';
echo '</div>';
}
add_action( 'admin_notices', 'customer_cleanup_display_message' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment