plugin = $plugin;
add_action( 'admin_menu', [ $this, 'add_menu' ] );
add_action( 'admin_post_woo_cleanup_save_settings', [ $this, 'save_settings' ] );
add_action( 'admin_post_woo_cleanup_manual_run', [ $this, 'handle_manual_run' ] );
add_action( 'admin_post_woo_cleanup_send_notice', [ $this, 'handle_send_notice' ] );
add_action( 'admin_post_woo_cleanup_delete_user', [ $this, 'handle_delete_user' ] );
add_action( 'admin_notices', [ $this, 'show_admin_notices' ] );
}
// =========================================================================
// Menu Registration
// =========================================================================
public function add_menu(): void {
add_submenu_page(
'woocommerce',
__( 'Account Cleanup', 'woo-cleanup' ),
__( 'Account Cleanup', 'woo-cleanup' ),
'manage_woocommerce',
'woo-cleanup',
[ $this, 'render_accounts_page' ]
);
add_submenu_page(
'woocommerce',
__( 'Cleanup Settings', 'woo-cleanup' ),
__( 'Cleanup Settings', 'woo-cleanup' ),
'manage_woocommerce',
'woo-cleanup-settings',
[ $this, 'render_settings_page' ]
);
}
// =========================================================================
// Accounts List Page
// =========================================================================
public function render_accounts_page(): void {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_die( esc_html__( 'You do not have permission to view this page.', 'woo-cleanup' ) );
}
$options = $this->plugin->get_options();
$grace_days = absint( $options['grace_days'] );
$active_tab = isset( $_GET['tab'] ) && $_GET['tab'] === 'notice_sent'
? 'notice_sent'
: 'pending_notice';
$pending_users = WooCleanup::get_flagged_users( 'pending_notice' );
$noticed_users = WooCleanup::get_flagged_users( 'notice_sent' );
$last_run = get_option( 'woo_cleanup_last_run' );
// Cron next run
$next_run = wp_next_scheduled( WooCleanup::CRON_HOOK );
$users_to_show = $active_tab === 'notice_sent' ? $noticed_users : $pending_users;
?>
output_admin_styles(); ?>
|
|
|
|
|
|
|
|
|
ID;
$inactive_since = (int) get_user_meta( $user_id, WooCleanup::META_INACTIVE_SINCE, true );
$notice_ts = (int) get_user_meta( $user_id, WooCleanup::META_NOTICE_SENT, true );
$delete_ts = $notice_ts + ( $grace_days * DAY_IN_SECONDS );
$days_left = max( 0, (int) ceil( ( $delete_ts - time() ) / DAY_IN_SECONDS ) );
$last_order = WooCleanup::get_last_order_date( $user_id );
$edit_url = get_edit_user_link( $user_id );
$row_class = ( $active_tab === 'notice_sent' && $days_left <= 2 ) ? 'woo-cleanup-row--urgent' : '';
?>
display_name ); ?>
#
|
user_email ); ?> |
user_registered ) ) ); ?> |
' . esc_html__( 'Never', 'woo-cleanup' ) . ''; ?> |
|
|
|
|
|
plugin->get_options();
?>
! empty( $raw['enabled'] ) ? 1 : 0,
'inactivity_months' => max( 1, absint( $raw['inactivity_months'] ?? 18 ) ),
'grace_days' => max( 1, absint( $raw['grace_days'] ?? 7 ) ),
'email_subject' => sanitize_text_field( $raw['email_subject'] ?? '' ),
'email_body' => wp_kses_post( $raw['email_body'] ?? '' ),
];
update_option( 'woo_cleanup_options', $options );
wp_safe_redirect( add_query_arg(
[ 'page' => 'woo-cleanup-settings', 'message' => 'saved' ],
admin_url( 'admin.php' )
) );
exit;
}
public function handle_manual_run(): void {
if ( ! current_user_can( 'manage_woocommerce' )
|| ! check_admin_referer( 'woo_cleanup_manual_run' ) ) {
wp_die( esc_html__( 'Security check failed.', 'woo-cleanup' ) );
}
$this->plugin->run_cleanup();
wp_safe_redirect( add_query_arg(
[ 'page' => 'woo-cleanup', 'message' => 'ran' ],
admin_url( 'admin.php' )
) );
exit;
}
public function handle_send_notice(): void {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_die( esc_html__( 'Security check failed.', 'woo-cleanup' ) );
}
$user_id = absint( $_POST['user_id'] ?? 0 );
if ( ! $user_id || ! check_admin_referer( 'woo_cleanup_send_notice_' . $user_id ) ) {
wp_die( esc_html__( 'Security check failed.', 'woo-cleanup' ) );
}
$options = $this->plugin->get_options();
$this->plugin->send_notice( $user_id, absint( $options['grace_days'] ), $options );
update_user_meta( $user_id, WooCleanup::META_NOTICE_SENT, time() );
wp_safe_redirect( add_query_arg(
[ 'page' => 'woo-cleanup', 'message' => 'noticed', 'uid' => $user_id ],
admin_url( 'admin.php' )
) );
exit;
}
public function handle_delete_user(): void {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_die( esc_html__( 'Security check failed.', 'woo-cleanup' ) );
}
$user_id = absint( $_POST['user_id'] ?? 0 );
if ( ! $user_id || ! check_admin_referer( 'woo_cleanup_delete_user_' . $user_id ) ) {
wp_die( esc_html__( 'Security check failed.', 'woo-cleanup' ) );
}
// Double-check: never delete admins via this form.
$user = get_userdata( $user_id );
if ( $user && in_array( 'administrator', (array) $user->roles, true ) ) {
wp_die( esc_html__( 'Cannot delete administrator accounts.', 'woo-cleanup' ) );
}
$this->plugin->delete_customer( $user_id );
wp_safe_redirect( add_query_arg(
[ 'page' => 'woo-cleanup', 'message' => 'deleted' ],
admin_url( 'admin.php' )
) );
exit;
}
// =========================================================================
// Admin Notices
// =========================================================================
public function show_admin_notices(): void {
$screen = get_current_screen();
if ( ! $screen || strpos( $screen->id, 'woo-cleanup' ) === false ) {
return;
}
$message = $_GET['message'] ?? '';
$uid = absint( $_GET['uid'] ?? 0 );
switch ( $message ) {
case 'saved':
echo ''
. esc_html__( 'Settings saved.', 'woo-cleanup' )
. '
';
break;
case 'ran':
echo ''
. esc_html__( 'Cleanup routine completed. The account list has been updated.', 'woo-cleanup' )
. '
';
break;
case 'noticed':
$user = $uid ? get_userdata( $uid ) : null;
echo ''
. esc_html( sprintf(
__( 'Notice email sent to %s.', 'woo-cleanup' ),
$user ? $user->display_name : "user #{$uid}"
) )
. '
';
break;
case 'deleted':
echo ''
. esc_html__( 'Account deleted.', 'woo-cleanup' )
. '
';
break;
}
}
// =========================================================================
// Inline Styles (small footprint; avoids an extra HTTP request)
// =========================================================================
private function output_admin_styles(): void {
?>