Files
jenhomeforms/crm-display.php
josh 552c60981c
All checks were successful
Test and publish WordPress plugin / release (push) Successful in 33s
Add Houzez form exports and verified Gitea release updates
2026-10-28 13:14:12 -04:00

143 lines
8.2 KiB
PHP

<?php
defined('ABSPATH') || exit;
/** Enrich only the named CRM view fragments, without copying vendor templates. */
final class Jenhome_CRM_Display {
private static $buffers = [];
private static $started = false;
private static function target($file) {
foreach (['enquires/main.php', 'enquires/enquires.php', 'enquires/enquiry-info.php', 'leads/lead-inquiries.php', 'leads/lead-info.php'] as $part) {
if (str_ends_with(wp_normalize_path($file), '/template-parts/dashboard/board/' . $part)) return $part;
}
return '';
}
public static function boot() {
if (self::$started) return;
self::$started = true;
add_action('wp_before_load_template', [__CLASS__, 'before'], 10, 1);
add_action('wp_after_load_template', [__CLASS__, 'after'], 10, 1);
}
public static function before($file) {
if (!self::target($file) || !is_user_logged_in() || !current_user_can('read')) return;
self::$buffers[] = [$file, ob_get_level()];
ob_start();
}
public static function after($file) {
$last = end(self::$buffers);
if (!$last || $last[0] !== $file || ob_get_level() !== $last[1] + 1) return;
$html = ob_get_clean();
array_pop(self::$buffers);
$detail_id = self::target($file) === 'enquires/enquiry-info.php' ? absint($_GET['enquiry'] ?? 0) : 0;
$lead_id = self::target($file) === 'leads/lead-info.php' ? absint($_GET['lead-id'] ?? 0) : 0;
echo self::decorate($html, $detail_id, $lead_id);
}
private static function text_node($doc, $tag, $value) {
$node = $doc->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('<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body><div id="jenhome-fragment">' . $html . '</div></body></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;
}
}