4 ? 3 : 1; return str_repeat('*', strlen($digits) - $keep) . substr($digits, -$keep); } /** * Nuova idempotency key per un invio form: resta nel payload in coda, quindi è identica a ogni retry. */ function adv_click_ingest_new_idempotency_key(): string { try { $rand = bin2hex(random_bytes(12)); } catch (Throwable) { $rand = md5(uniqid((string) mt_rand(), true)); } return 'wp-' . $rand; } /** * Link alla scheda lead nel CRM, costruito dall'origine dell'URL ingest. */ function adv_click_ingest_crm_lead_url(string $ingestUrl, ?int $leadId): string { if ($leadId === null || $leadId <= 0) { return ''; } $parts = parse_url($ingestUrl); if (!is_array($parts) || empty($parts['host'])) { return ''; } $scheme = ($parts['scheme'] ?? 'https') === 'http' ? 'http' : 'https'; $port = isset($parts['port']) ? ':' . (int) $parts['port'] : ''; return $scheme . '://' . $parts['host'] . $port . '/app/leads?focus=' . $leadId; } /** * @param array $payload * @param array{ok: bool, code: int, error: string|null, duplicate: bool, lead_id?: int|null} $result * @return array */ function adv_click_ingest_sent_log_entry(array $payload, array $result, string $ingestUrl, int $attempt, ?int $now = null): array { $leadId = isset($result['lead_id']) ? (int) $result['lead_id'] : 0; $phone = (string) ($payload['cellulare'] ?? $payload['telefono'] ?? $payload['phone'] ?? ''); if ($result['ok']) { $esito = !empty($result['duplicate']) ? 'duplicato' : 'creato'; } else { $esito = $result['code'] > 0 ? 'errore_http' : 'errore_rete'; } return [ 'at' => gmdate('c', $now ?? time()), 'esito' => $esito, 'http_code' => (int) $result['code'], 'duplicate' => !empty($result['duplicate']), 'lead_id' => $leadId > 0 ? $leadId : null, 'lead_url' => adv_click_ingest_crm_lead_url($ingestUrl, $leadId), 'email' => adv_click_ingest_mask_email((string) ($payload['email'] ?? '')), 'telefono' => adv_click_ingest_mask_phone($phone), 'form' => substr((string) ($payload['form_plugin'] ?? ''), 0, 40), 'idempotency_key' => substr((string) ($payload['idempotency_key'] ?? ''), 0, 64), 'attempt' => max(1, $attempt), 'error' => $result['ok'] ? '' : substr((string) ($result['error'] ?? ''), 0, 160), ]; } /** * @param list> $log * @param array $entry * @return list> */ function adv_click_ingest_sent_log_push(array $log, array $entry, int $max = ADV_CLICK_SENT_LOG_MAX): array { $log[] = $entry; if (count($log) > $max) { $log = array_slice($log, -$max); } return array_values($log); } /** * @return list> */ function adv_click_ingest_sent_log_get(): array { $log = get_option(ADV_CLICK_SENT_LOG_OPTION, []); return is_array($log) ? array_values(array_filter($log, 'is_array')) : []; } /** * @param array $payload * @param array $result */ function adv_click_ingest_sent_log_record(array $payload, array $result, string $ingestUrl, int $attempt = 1): void { try { $log = adv_click_ingest_sent_log_push( adv_click_ingest_sent_log_get(), adv_click_ingest_sent_log_entry($payload, $result, $ingestUrl, $attempt), ); update_option(ADV_CLICK_SENT_LOG_OPTION, $log, false); } catch (Throwable) { // Il registro non deve mai bloccare l'invio del lead. } } function adv_click_ingest_render_sent_log_page(): void { if (!current_user_can('manage_options')) { return; } if (isset($_POST['adv_click_sent_log_clear']) && check_admin_referer('adv_click_sent_log_clear')) { update_option(ADV_CLICK_SENT_LOG_OPTION, [], false); } $log = array_reverse(adv_click_ingest_sent_log_get()); $labels = [ 'creato' => __('Creato', 'adv-click-lead-ingest'), 'duplicato' => __('Duplicato (lead esistente)', 'adv-click-lead-ingest'), 'errore_http' => __('Errore HTTP (in coda)', 'adv-click-lead-ingest'), 'errore_rete' => __('Errore rete/config (in coda)', 'adv-click-lead-ingest'), ]; echo '

' . esc_html__('ADV.click — Lead inviati al CRM', 'adv-click-lead-ingest') . '

'; echo '

' . esc_html(sprintf( /* translators: %d: max entries */ __('Ultimi %d tentativi di invio (più recenti in alto). Privacy: email e telefono sono mascherati; messaggio, nome e IP non vengono salvati. Il registro sta nell\'option adv_click_ingest_sent_log (non autoload) e viene cancellato alla disinstallazione del plugin.', 'adv-click-lead-ingest'), ADV_CLICK_SENT_LOG_MAX, )) . '

'; echo '

' . esc_html(sprintf( /* translators: %d: pending count */ __('Lead in coda di reinvio: %d', 'adv-click-lead-ingest'), count(adv_click_ingest_pending_get()), )) . '

'; echo ''; foreach ([__('Data (UTC)', 'adv-click-lead-ingest'), __('Esito', 'adv-click-lead-ingest'), 'HTTP', __('Lead CRM', 'adv-click-lead-ingest'), 'Email', __('Telefono', 'adv-click-lead-ingest'), 'Form', __('Tentativo', 'adv-click-lead-ingest'), __('Errore', 'adv-click-lead-ingest')] as $h) { echo ''; } echo ''; if ($log === []) { echo ''; } foreach ($log as $row) { $esito = (string) ($row['esito'] ?? ''); $lead = ''; if (!empty($row['lead_id'])) { $lead = !empty($row['lead_url']) ? '#' . (int) $row['lead_id'] . '' : '#' . (int) $row['lead_id']; } echo '' . '' . '' . '' . '' . '' . '' . '' . ''; } echo '
' . esc_html($h) . '
' . esc_html__('Nessun invio registrato.', 'adv-click-lead-ingest') . '
' . esc_html((string) ($row['at'] ?? '')) . '' . esc_html($labels[$esito] ?? $esito) . '' . esc_html((string) ($row['http_code'] ?? '')) . '' . $lead . '' . esc_html((string) ($row['email'] ?? '')) . '' . esc_html((string) ($row['telefono'] ?? '')) . '' . esc_html((string) ($row['form'] ?? '')) . '' . esc_html((string) ($row['attempt'] ?? '')) . '' . esc_html((string) ($row['error'] ?? '')) . '
'; echo '
'; wp_nonce_field('adv_click_sent_log_clear'); echo '
'; }