$val) { $fields[] = [ 'label' => $key, 'id' => sanitize_title((string)$key), 'value' => $val, ]; } } // 1. Try to find an email field foreach ($fields as $field) { $label = (string)($field['label'] ?? ''); $id = (string)($field['id'] ?? ''); if (stripos($label, 'email') !== false || stripos($id, 'email') !== false) { $val = is_array($field['value'] ?? '') ? implode(', ', $field['value']) : (string)($field['value'] ?? ''); if ($val !== '') { return esc_html($val); } } } // 2. Try to find a name field foreach ($fields as $field) { $label = (string)($field['label'] ?? ''); $id = (string)($field['id'] ?? ''); if (stripos($label, 'name') !== false || stripos($id, 'name') !== false) { $val = is_array($field['value'] ?? '') ? implode(', ', $field['value']) : (string)($field['value'] ?? ''); if ($val !== '') { return esc_html($val); } } } // 3. Fallback to the first non-empty field foreach ($fields as $field) { $val = is_array($field['value'] ?? '') ? implode(', ', $field['value']) : (string)($field['value'] ?? ''); if ($val !== '') { return esc_html($val); } } return '—'; } /** * Get the page title from its URL. * * @param string $url * @return string */ public static function getPageTitleByUrl(string $url): string { if (empty($url)) { return '—'; } $postId = url_to_postid($url); if ($postId) { return esc_html(get_the_title($postId)); } // Fallback to path $path = wp_parse_url($url, PHP_URL_PATH); if ($path) { return esc_html(trim((string)$path, '/')); } return esc_html($url); } }