createElement($tag); $node->appendChild($doc->createTextNode(is_scalar($value) ? (string)$value : '')); return $node; } private static function row_id($xpath, $row) { $input = $xpath->query('.//input[@name="enquiry_multi_delete[]"]', $row)->item(0); if ($input) return absint($input->getAttribute('value')); foreach ($xpath->query('.//a[@href]', $row) as $link) { parse_str(wp_parse_url(html_entity_decode($link->getAttribute('href')), PHP_URL_QUERY) ?: '', $args); if (isset($args['enquiry'])) return absint($args['enquiry']); } return 0; } public static function decorate($html, $detail_id = 0, $lead_id = 0) { if (!is_user_logged_in() || !current_user_can('read') || !class_exists('DOMDocument')) return $html; $doc = new DOMDocument('1.0', 'UTF-8'); $old = libxml_use_internal_errors(true); $ok = $doc->loadHTML('
' . $html . '
', LIBXML_NONET); libxml_clear_errors(); libxml_use_internal_errors($old); if (!$ok) return $html; $xpath = new DOMXPath($doc); $root = $doc->getElementById('jenhome-fragment'); if (!$root) return $html; if ($lead_id) { $middle = Jenhome_Contact_Exports::lead_middle_name($lead_id, get_current_user_id()); $list = $xpath->query('.//div[contains(concat(" ", normalize-space(@class), " "), " inquiry-detail ")]/ul', $root)->item(0); if ($list && $middle !== '') { $li = $doc->createElement('li'); $li->setAttribute('class', 'border-bottom pb-2 jenhome-crm-field'); $li->appendChild(self::text_node($doc, 'strong', 'Middle Name')); $li->appendChild($doc->createElement('br')); $li->appendChild($doc->createTextNode($middle)); $anchor = $xpath->query('./li', $list)->item(0); if ($anchor) $list->insertBefore($li, $anchor->nextSibling); else $list->appendChild($li); } } $ids = $detail_id ? [$detail_id] : []; foreach ($xpath->query('.//tbody/tr', $root) as $tr) { $id = self::row_id($xpath, $tr); if ($id) $ids[] = $id; } $records = []; foreach (Jenhome_Contact_Exports::inquiry_records(get_current_user_id(), $ids) as $record) $records[$record['enquiry_id']] = $record; if ($detail_id && isset($records[$detail_id])) { $record = $records[$detail_id]; $meta = Jenhome_Contact_Exports::meta($record['enquiry_meta']); $list = $xpath->query('.//div[contains(concat(" ", normalize-space(@class), " "), " inquiry-detail ")]/ul', $root)->item(0); if ($list) { $anchor = $xpath->query('./li', $list)->item(0); $values = [ 'Submission Date' => Jenhome_Contact_Exports::submission_date($record['time'] ?? ''), 'First Name' => $record['first_name'] ?? '', 'Middle Name' => $record['middle_name'] ?? '', 'Last Name' => $record['last_name'] ?? '', 'Referral Source' => Jenhome_Contact_Exports::referral_source($record), 'Referrer Name' => Jenhome_Contact_Exports::referrer_name($record, $meta), 'Source Link' => html_entity_decode($record['source_link'] ?? '', ENT_QUOTES, 'UTF-8'), ]; foreach ($values as $label => $value) { $li = $doc->createElement('li'); $li->setAttribute('class', 'border-bottom pb-2 jenhome-crm-field'); $li->appendChild(self::text_node($doc, 'strong', $label)); $li->appendChild($doc->createElement('br')); if ($label === 'Source Link' && $value !== '') { $link = self::text_node($doc, 'a', $value); $link->setAttribute('href', esc_url($value)); $li->appendChild($link); } else $li->appendChild($doc->createTextNode($value !== '' ? $value : '—')); if ($anchor) $list->insertBefore($li, $anchor->nextSibling); else $list->appendChild($li); $anchor = $li; } if (Jenhome_Contact_Exports::has_referrer_mapping($record, $meta)) { // The same value is now shown once, under its correct label above. foreach ($xpath->query('./li/strong', $list) as $label) { if (trim($label->textContent) === __('Street Address', 'houzez')) $list->removeChild($label->parentNode); } } } } if (!$detail_id) { foreach ($xpath->query('.//table', $root) as $table) { $header = $xpath->query('./thead/tr', $table)->item(0); if (!$header) continue; $has_records = false; foreach ($xpath->query('./tbody/tr', $table) as $tr) if (self::row_id($xpath, $tr)) $has_records = true; if (!$has_records) continue; $anchor = null; foreach ($xpath->query('./th', $header) as $th) if (trim($th->textContent) === __('Inquiry Type', 'houzez')) $anchor = $th; foreach (['Referral Source', 'Referrer Name'] as $label) { $th = self::text_node($doc, 'th', $label); $th->setAttribute('data-label', $label); if ($anchor) $header->insertBefore($th, $anchor->nextSibling); else $header->appendChild($th); $anchor = $th; } foreach ($xpath->query('./tbody/tr', $table) as $tr) { $record = $records[self::row_id($xpath, $tr)] ?? null; $meta = $record ? Jenhome_Contact_Exports::meta($record['enquiry_meta']) : []; $values = ['Referral Source' => $record ? Jenhome_Contact_Exports::referral_source($record) : '', 'Referrer Name' => $record ? Jenhome_Contact_Exports::referrer_name($record, $meta) : '']; $anchor = null; foreach ($xpath->query('./td', $tr) as $td) if ($td->getAttribute('data-label') === __('Inquiry Type', 'houzez')) $anchor = $td; foreach ($values as $label => $value) { $td = self::text_node($doc, 'td', $value !== '' ? $value : '—'); $td->setAttribute('data-label', $label); if ($anchor) $tr->insertBefore($td, $anchor->nextSibling); else $tr->appendChild($td); $anchor = $td; } } } } $out = ''; foreach ($root->childNodes as $child) $out .= $doc->saveHTML($child); return $out; } }