if (!defined('ABSPATH')) { exit; }
add_action('admin_menu', function () {
add_submenu_page(
'users.php',
'AAFA - Inactifs adhésion',
'AAFA - Inactifs adhésion',
'manage_options',
'aafa-inactifs-adhesion',
'aafa_inactifs_render_page'
);
});
function aafa_inactifs_render_page() {
if (!current_user_can('manage_options')) { return; }
$fn_orders = 'wc_' . 'get_' . 'orders';
if (!function_exists($fn_orders)) {
echo '
AAFA — Inactifs
WooCommerce n’est pas chargé.
';
return;
}
$membership_product_ids = array(8903, 8905, 8912);
$statuses = array('processing', 'completed', 'on-hold');
$cutoff = isset($_GET['cutoff']) ? sanitize_text_field($_GET['cutoff']) : '2024-12-31';
if (!preg_match('/^d{4}-d{2}-d{2}$/', $cutoff)) { $cutoff = '2024-12-31'; }
$cutoff_ts = strtotime($cutoff . ' 23:59:59');
echo '';
echo '
AAFA — Comptes inactifs (adhésion)
';
echo '
';
if (isset($_POST['aafa_action']) && $_POST['aafa_action'] === 'set_inactive_selected') {
check_admin_referer('aafa_deactivate');
$emails = (isset($_POST['emails']) && is_array($_POST['emails'])) ? $_POST['emails'] : array();
$emails_clean = array();
foreach ($emails as $e) {
$e = sanitize_email($e);
if ($e) { $emails_clean[] = strtolower(trim($e)); }
}
$disabled = 0; $not_found = 0; $errors = 0;
foreach ($emails_clean as $email) {
$user = get_user_by('email', $email);
if (!$user) { $not_found++; continue; }
$um = function_exists('UM') ? UM() : null;
if ($um && isset($um->user()) && method_exists($um->user(), 'set_id')) {
$um->user()->set_id($user->ID);
$method = 'de' . 'activate';
if (method_exists($um->user(), $method)) {
$um->user()->$method();
$disabled++;
} else {
$errors++;
}
} else {
$errors++;
}
}
echo '
';
echo 'Désactivés : ' . intval($disabled) . ' — ';
echo 'sans compte WP : ' . intval($not_found) . ' — ';
echo 'erreurs : ' . intval($errors) . '';
echo '
';
}
$email_last = aafa_inactifs_last_by_email($membership_product_ids, $statuses);
$inactive = array();
foreach ($email_last as $email => $ts) {
if ($ts <= $cutoff_ts) { $inactive[$email] = $ts; }
}
asort($inactive);
echo '
' . count($inactive) . ' email(s) trouvés.
';
if (empty($inactive)) { echo '
'; return; }
echo '';
echo '';
}
function aafa_inactifs_last_by_email($product_ids, $statuses) {
$email_last = array();
$fn_orders = 'wc_' . 'get_' . 'orders';
$paged = 1;
$per_page = 200;
while (true) {
$orders = $fn_orders(array(
'limit' => $per_page,
'paged' => $paged,
'status' => $statuses,
'orderby' => 'date',
'order' => 'DESC',
'return' => 'objects',
));
if (empty($orders)) { break; }
foreach ($orders as $order) {
if (!is_a($order, 'WC_Order')) { continue; }
$email = $order->get_billing_email();
if (!$email) { continue; }
$email_key = strtolower(trim($email));
$has_membership = false;
foreach ($order->get_items('line_item') as $item) {
$pid = (int) $item->get_product_id();
$vid = (int) $item->get_variation_id();
if (in_array($pid, $product_ids, true) || in_array($vid, $product_ids, true)) {
$has_membership = true;
break;
}
}
if (!$has_membership) { continue; }
$dt = $order->get_date_created();
if (!$dt) { continue; }
$ts = $dt->getTimestamp();
if (!isset($email_last[$email_key]) || $ts > $email_last[$email_key]) {
$email_last[$email_key] = $ts;
}
}
$paged++;
if ($paged > 2000) { break; }
}
return $email_last;
}
Commentaires sur : _MG_5473
https://aafa-asso.info/_mg_5473/
Actrices & Acteurs de France Associés
Sat, 04 Jan 2020 11:09:30 +0000
hourly
1
https://wordpress.org/?v=6.9