Skip to content

Instantly share code, notes, and snippets.

@BFTrick
Last active June 5, 2025 17:53
Show Gist options
  • Save BFTrick/4a2df442b128423ac61cc9bbcf3f98ad to your computer and use it in GitHub Desktop.
Save BFTrick/4a2df442b128423ac61cc9bbcf3f98ad to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: WooCommerce API Key Warning
* Description: Warns administrators about WooCommerce API keys before user deletion
* Version: 1.0.0
* Author: Patrick Rauland
* Requires PHP: 8.2
* Requires Plugins: woocommerce
*
* @package WooCommerce_API_Key_Warning
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class WooCommerce_API_Key_Warning {
/**
* Constructor
*/
public function __construct() {
// Hook into the user deletion form
add_action( 'delete_user_form', array( $this, 'check_api_keys' ) );
}
/**
* Check for API keys and display warning if found
*
* @param WP_User $user The user being deleted
*/
public function check_api_keys( $user ) {
global $wpdb;
// Get user ID from query parameter
$user_id = isset( $_GET['user'] ) ? intval( $_GET['user'] ) : 0;
// Query for API keys associated with this user
$api_keys = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}woocommerce_api_keys WHERE user_id = %d",
$user_id
)
);
if ( ! empty( $api_keys ) ) {
?>
<div class="notice notice-warning">
<p>
<strong><?php esc_html_e( '⚠️ Warning:', 'woocommerce-api-key-warning' ); ?></strong>
<?php
printf(
/* translators: %d: number of API keys */
esc_html__( 'This user has %d WooCommerce REST API keys that will be deleted.', 'woocommerce-api-key-warning' ),
count( $api_keys )
);
?>
</p>
<p><?php esc_html_e( 'Deleting them may break external services that rely on these credentials.', 'woocommerce-api-key-warning' ); ?></p>
<table class="widefat">
<thead>
<tr>
<th><?php esc_html_e( 'Description', 'woocommerce-api-key-warning' ); ?></th>
<th><?php esc_html_e( 'Last Used', 'woocommerce-api-key-warning' ); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ( $api_keys as $key ) : ?>
<tr>
<td><?php echo esc_html( $key->description ); ?></td>
<td>
<?php
if ( ! empty( $key->last_access ) ) {
echo esc_html( date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $key->last_access ) ) );
} else {
esc_html_e( 'Never', 'woocommerce-api-key-warning' );
}
?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php
}
}
}
// Initialize the plugin
new WooCommerce_API_Key_Warning();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment