3 Commits

Author SHA1 Message Date
josh
c690c0f8cf Put email last in contact exports
All checks were successful
Test and publish WordPress plugin / release (push) Successful in 18s
2026-11-02 12:00:46 -05:00
josh
0fe8914981 Make CRM exports download reliably
All checks were successful
Test and publish WordPress plugin / release (push) Successful in 19s
2026-11-02 11:56:28 -05:00
josh
f63e052ccd Assign Jen Home inquiries to designated admin
All checks were successful
Test and publish WordPress plugin / release (push) Successful in 23s
2026-11-02 11:09:32 -05:00
6 changed files with 146 additions and 27 deletions

View File

@@ -1,3 +1,19 @@
## 1.5.0
- Move phone columns before Email and make Email the final column in both CSV exports.
- Build and validate the plugin package on every push; published stable Gitea releases remain the WordPress update source.
## 1.4.0
- Make Leads and Inquiries export buttons submit a normal authenticated CSV download instead of relying on asynchronous AJAX/Blob downloads.
- Keep the established 24-column Leads and eight-column Inquiries layouts.
## 1.3.0
- Enable the plugin on the approved Jen Home production site as well as staging.
- Assign each new inquiry and its unique linked lead to WordPress user ID 7.
- Preserve the existing Houzez owner filter and avoid changing historical records.
## 1.2.2
- Remove the Source Link column and its values from both Leads and Inquiries CSVs.

View File

@@ -1,6 +1,6 @@
# Jen Home Contact Fields and Exports
WordPress/Houzez customization for **staging.jenhome.net**.
WordPress/Houzez customization for **staging.jenhome.net** and **www.jenhome.net**.
Author: **Josh Stevens Sr** · Website: [hostLabs](https://hostlabs.pro/)
@@ -14,6 +14,11 @@ answer to “How did you hear about us?”. The existing friend-name question is
identified separately from a property address. Original forms, CRM persistence,
notifications and existing security checks remain in place.
New inquiries are assigned to WordPress user ID 7, with the linked Houzez lead
owner updated in the same transaction. The update applies only when the new
inquiry is the lead's sole inquiry; ambiguous relationships keep their native
ownership and are logged. Existing records are never reassigned automatically.
Inquiries CSV columns, in order:
1. Submission Date
@@ -22,14 +27,18 @@ Inquiries CSV columns, in order:
4. Last Name
5. Referral Source
6. Referrer Name
7. Email
8. Mobile
7. Mobile
8. Email
Submission dates use America/New_York with an explicit UTC offset. Leads exports
omit Source Link and retain their other existing fields. CRM views still show inquiry/property details even
Submission dates use America/New_York with an explicit UTC offset. Email is the
last column in both exports; phone fields come before it. Leads exports omit
Source Link and retain their other existing fields. CRM views still show inquiry/property details even
though the shorter Inquiries CSV omits them. Historical records are not backfilled
or split; relationships use IDs, never email matching.
The CRM export buttons submit the protected endpoints as normal browser downloads,
so the CSV is not dependent on an asynchronous JavaScript Blob download.
The plugin creates a prefixed `jenhome_contact_fields` extension table for middle
names and exact submission provenance. Native Houzez tables and vendor files are
not modified. Keep this table when rolling back or deactivating the plugin.
@@ -78,8 +87,10 @@ repository's Settings → Actions → Runners if retiring the computer.
1. Edit code and `CHANGELOG.md`; update both the plugin header Version and
`Jenhome_Contact_Exports::VERSION`.
2. Commit and push to `main`. The workflow lints PHP, runs the standalone updater
and CSV tests, and builds the ZIP.
2. Commit and push to `main`. Each push runs PHP lint, the standalone updater and
CSV tests, and builds the ZIP. WordPress checks the latest published stable
Gitea release, so a pushed commit is available to the updater after it is
validated on staging and published with its matching version tag.
3. After checks and staging validation pass, create and push a matching tag:
`git tag v1.2.3` then `git push origin v1.2.3`.
4. The tag workflow repeats checks, builds the package, uploads both assets to a
@@ -93,14 +104,15 @@ Local checks (PHP 8.1+ with ZIP support, Python 3):
```sh
php tests/unit.php
python3 scripts/build-release.py v1.2.2
python3 scripts/build-release.py v1.3.0
```
## Scope and rollback
The plugin runs only when both the home URL and WordPress directory identify
`staging.jenhome.net`. Installing it on production does not enable its behavior or
updater. A production rollout requires a separately reviewed scope change.
The plugin runs only when the home URL and WordPress directory match one of the
two approved installations: `staging.jenhome.net` in its staging directory or
`www.jenhome.net` in `httpdocs`. It also requires user ID 7 to exist as an
administrator. Other hosts and copied installations fail closed.
Back up the current plugin directory before the first updater installation.
Deactivate this plugin to return to native behavior, or reinstall the previous

View File

@@ -121,7 +121,35 @@
document.addEventListener('submit', event => {
if (relevant(event.target)) { enhance(event.target); sync(event.target); }
}, true);
// Preserve the vendor export buttons and download behavior while adding CSRF protection.
// Submit exports as a normal download. This avoids the vendor AJAX/Blob flow,
// which silently fails on browsers that block downloads from async callbacks.
document.addEventListener('click', event => {
const button = event.target.closest?.('#export-leads, #export-inquiries');
if (!button) return;
event.preventDefault();
event.stopImmediatePropagation();
if (!config.ajaxUrl || !config.exportNonce) {
window.alert('Please sign in again before exporting.');
return;
}
const form = document.createElement('form');
form.method = 'POST';
form.action = config.ajaxUrl;
form.style.display = 'none';
const action = document.createElement('input');
action.type = 'hidden';
action.name = 'action';
action.value = button.id === 'export-leads' ? 'houzez_crm_export_leads' : 'crm_export_inquiries';
const nonce = document.createElement('input');
nonce.type = 'hidden';
nonce.name = 'jenhome_export_nonce';
nonce.value = config.exportNonce;
form.append(action, nonce);
document.body.append(form);
form.submit();
window.setTimeout(() => form.remove(), 1000);
}, true);
// Keep the nonce attached if another Houzez view calls these endpoints by AJAX.
$.ajaxPrefilter(function (options) {
const url = new URL(options.url, location.href);
if (url.origin !== location.origin || !url.pathname.endsWith('/admin-ajax.php')) return;

View File

@@ -102,7 +102,7 @@ final class Jenhome_Gitea_Updater {
'version' => $release['version'], 'author' => 'Josh Stevens Sr', 'author_profile' => 'https://hostlabs.pro/',
'homepage' => 'https://hostlabs.pro/', 'requires' => '6.2', 'requires_php' => '8.1', 'tested' => '7.1.3',
'download_link' => $release['package'], 'last_updated' => $release['published_at'],
'sections' => ['description' => 'Site-specific Houzez contact fields and CSV exports for staging.jenhome.net.',
'sections' => ['description' => 'Site-specific Houzez contact fields, account 7 inquiry ownership and CSV exports for Jen Home staging and production.',
'changelog' => wpautop(esc_html($release['body']))],
];
}

View File

@@ -3,8 +3,8 @@
* Plugin Name: Jen Home Contact Fields and Exports
* Plugin URI: https://hostlabs.pro/
* Update URI: https://git.labs.casa/josh/jenhomeforms
* Description: Staging-only Houzez name/referral capture and protected CSV exports.
* Version: 1.2.2
* Description: Jen Home Houzez contact fields, inquiry ownership and protected CSV exports.
* Version: 1.5.0
* Requires at least: 6.2
* Requires PHP: 8.1
* Author: Josh Stevens Sr
@@ -13,7 +13,8 @@
defined('ABSPATH') || exit;
final class Jenhome_Contact_Exports {
const VERSION = '1.2.2';
const VERSION = '1.5.0';
const INQUIRY_OWNER_ID = 7;
const ACTIONS = ['houzez_ele_inquiry_form', 'houzez_ele_contact_form', 'houzez_property_agent_contact', 'houzez_schedule_send_message', 'houzez_contact_realtor'];
// Verified Elementor widgets: contact page and the two Get Started popups.
const REFERRAL_FORMS = ['6o0tsaob0', '1cbfa42a', '2a2beb9b'];
@@ -24,9 +25,11 @@ final class Jenhome_Contact_Exports {
private static $collector_wrapped = false;
public static function boot() {
// Fail closed on production, including if a staging clone is later copied there.
if (wp_parse_url(home_url(), PHP_URL_HOST) !== 'staging.jenhome.net'
|| basename(untrailingslashit(ABSPATH)) !== 'staging.jenhome.net') return;
// Fail closed outside the two explicitly approved Jen Home installations.
$host = strtolower((string)wp_parse_url(home_url(), PHP_URL_HOST));
$roots = ['staging.jenhome.net' => 'staging.jenhome.net', 'www.jenhome.net' => 'httpdocs'];
if (!isset($roots[$host]) || basename(untrailingslashit(ABSPATH)) !== $roots[$host]
|| !get_userdata(self::INQUIRY_OWNER_ID) || !user_can(self::INQUIRY_OWNER_ID, 'manage_options')) return;
require_once __DIR__ . '/gitea-updater.php';
Jenhome_Gitea_Updater::boot(__FILE__);
self::install_storage();
@@ -77,6 +80,7 @@ final class Jenhome_Contact_Exports {
wp_localize_script('jenhome-contact-exports', 'JenhomeContact', [
'actions' => self::ACTIONS, 'referralForms' => self::REFERRAL_FORMS,
'referrals' => self::referrals(), 'nonce' => wp_create_nonce('jenhome_contact'),
'ajaxUrl' => admin_url('admin-ajax.php'),
'exportNonce' => is_user_logged_in() ? wp_create_nonce('jenhome_export') : '',
]);
wp_register_style('jenhome-contact-exports', false, [], self::VERSION);
@@ -227,11 +231,52 @@ final class Jenhome_Contact_Exports {
$lead_id = $id;
} else return;
if (!$lead_id) return;
if ($enquiry_id) self::assign_inquiry_owner($lead_id, $enquiry_id);
$form = $enquiry_id && self::input('is_estimation') === 'yes' && empty($_POST['listing_id']) ? self::$referrer_form : '';
if (self::$middle_name === '' && $form === '') return;
$wpdb->insert($wpdb->prefix . 'jenhome_contact_fields', ['lead_id' => $lead_id, 'enquiry_id' => $enquiry_id, 'middle_name' => self::$middle_name, 'referrer_form' => $form], ['%d', '%d', '%s', '%s']);
}
/** Keep the native inquiry/lead relationship intact while assigning only this new inquiry. */
private static function assign_inquiry_owner($lead_id, $enquiry_id) {
global $wpdb;
$leads = $wpdb->prefix . 'houzez_crm_leads';
$inquiries = $wpdb->prefix . 'houzez_crm_enquiries';
$row = $wpdb->get_row($wpdb->prepare(
"SELECT e.user_id AS enquiry_user_id,l.user_id AS lead_user_id,(SELECT COUNT(*) FROM $inquiries x WHERE x.lead_id=e.lead_id) AS inquiry_count FROM $inquiries e JOIN $leads l ON l.lead_id=e.lead_id WHERE e.enquiry_id=%d AND e.lead_id=%d",
$enquiry_id, $lead_id
));
if (!$row || (int)$row->inquiry_count !== 1) {
error_log('Jen Home Contact Fields: skipped inquiry ownership update because its new inquiry/lead relationship was not unique.');
return false;
}
$old_enquiry_owner = (int)$row->enquiry_user_id;
$old_lead_owner = (int)$row->lead_user_id;
if ($old_enquiry_owner === self::INQUIRY_OWNER_ID && $old_lead_owner === self::INQUIRY_OWNER_ID) return true;
if ($wpdb->query('START TRANSACTION') === false) {
error_log('Jen Home Contact Fields: could not start inquiry ownership transaction.');
return false;
}
$lead_updated = $old_lead_owner === self::INQUIRY_OWNER_ID ? 0 : $wpdb->update(
$leads, ['user_id' => self::INQUIRY_OWNER_ID], ['lead_id' => $lead_id, 'user_id' => $old_lead_owner], ['%d'], ['%d', '%d']
);
$enquiry_updated = $old_enquiry_owner === self::INQUIRY_OWNER_ID ? 0 : $wpdb->update(
$inquiries, ['user_id' => self::INQUIRY_OWNER_ID], ['enquiry_id' => $enquiry_id, 'lead_id' => $lead_id, 'user_id' => $old_enquiry_owner], ['%d'], ['%d', '%d', '%d']
);
if (($old_lead_owner !== self::INQUIRY_OWNER_ID && $lead_updated !== 1)
|| ($old_enquiry_owner !== self::INQUIRY_OWNER_ID && $enquiry_updated !== 1)) {
$wpdb->query('ROLLBACK');
error_log('Jen Home Contact Fields: inquiry ownership update failed; changes were rolled back.');
return false;
}
if ($wpdb->query('COMMIT') === false) {
$wpdb->query('ROLLBACK');
error_log('Jen Home Contact Fields: inquiry ownership commit failed.');
return false;
}
return true;
}
public static function meta($value) {
$meta = is_serialized($value) ? @unserialize(trim($value), ['allowed_classes' => false]) : [];
return is_array($meta) ? $meta : [];
@@ -314,7 +359,7 @@ final class Jenhome_Contact_Exports {
public static function lead_rows($user_id) {
global $wpdb;
$fields = ['time', 'first_name', 'middle_name', 'last_name', 'source', 'referrer_name', 'prefix', 'display_name', 'email', 'mobile', 'home_phone', 'work_phone', 'address', 'city', 'state', 'country', 'zipcode', 'type', 'twitter_url', 'linkedin_url', 'facebook_url', 'private_note', 'message', 'inquiry_street_address'];
$fields = ['time', 'first_name', 'middle_name', 'last_name', 'source', 'referrer_name', 'prefix', 'display_name', 'mobile', 'home_phone', 'work_phone', 'address', 'city', 'state', 'country', 'zipcode', 'type', 'twitter_url', 'linkedin_url', 'facebook_url', 'private_note', 'message', 'inquiry_street_address', 'email'];
$rows = $wpdb->get_results($wpdb->prepare("SELECT l.*, f.middle_name FROM {$wpdb->prefix}houzez_crm_leads l LEFT JOIN {$wpdb->prefix}jenhome_contact_fields f ON f.lead_id=l.lead_id WHERE l.user_id = %d ORDER BY l.lead_id", $user_id), ARRAY_A);
$related = [];
foreach (self::inquiry_records($user_id) as $inquiry) {
@@ -333,7 +378,7 @@ final class Jenhome_Contact_Exports {
public static function export_leads() {
self::authorize_export();
self::write_csv('leads.csv', ['Submission Date', 'First Name', 'Middle Name', 'Last Name', 'Referral Source', 'Referrer Name', 'Prefix', 'Full Name', 'Email', 'Mobile', 'Home Phone', 'Work Phone', 'Address', 'City', 'County / State', 'Country', 'Postal Code / Zip', 'Type', 'Twitter', 'Linkedin', 'Facebook', 'Private Note', 'Message', 'Inquiry Street Address'], self::lead_rows(get_current_user_id()));
self::write_csv('leads.csv', ['Submission Date', 'First Name', 'Middle Name', 'Last Name', 'Referral Source', 'Referrer Name', 'Prefix', 'Full Name', 'Mobile', 'Home Phone', 'Work Phone', 'Address', 'City', 'County / State', 'Country', 'Postal Code / Zip', 'Type', 'Twitter', 'Linkedin', 'Facebook', 'Private Note', 'Message', 'Inquiry Street Address', 'Email'], self::lead_rows(get_current_user_id()));
}
public static function inquiry_records($user_id, $ids = null) {
@@ -361,14 +406,14 @@ final class Jenhome_Contact_Exports {
self::submission_date($row['time'] ?? ''),
$row['first_name'] ?? '', $row['middle_name'] ?? '', $row['last_name'] ?? '',
self::referral_source($row), self::referrer_name($row, $meta),
$row['email'] ?? '', $row['mobile'] ?? '',
$row['mobile'] ?? '', $row['email'] ?? '',
];
}, $rows);
}
public static function export_inquiries() {
self::authorize_export();
self::write_csv('inquiries.csv', ['Submission Date', 'First Name', 'Middle Name', 'Last Name', 'Referral Source', 'Referrer Name', 'Email', 'Mobile'], self::inquiry_rows(get_current_user_id()));
self::write_csv('inquiries.csv', ['Submission Date', 'First Name', 'Middle Name', 'Last Name', 'Referral Source', 'Referrer Name', 'Mobile', 'Email'], self::inquiry_rows(get_current_user_id()));
}
}
add_action('plugins_loaded', ['Jenhome_Contact_Exports', 'boot']);

View File

@@ -93,6 +93,24 @@ try {
$GLOBALS['responses'][$info['checksum_url']]=['status'=>200,'body'=>'not a checksum']; Jenhome_Gitea_Updater::clear();
check(Jenhome_Gitea_Updater::latest()===false,'invalid checksum metadata not advertised');
$GLOBALS['wpdb']=new class {
public $prefix='test_'; public $state=['enquiry_user_id'=>1,'lead_user_id'=>1,'inquiry_count'=>1]; public $updates=[]; public $transactions=[];
public function prepare($query,...$args) { return $query; }
public function get_row($query) { return (object)$this->state; }
public function query($sql) { $this->transactions[]=$sql; return 1; }
public function update($table,$data,$where,$formats,$where_formats) {
$this->updates[]=$table;
if (str_ends_with($table,'houzez_crm_leads')) $this->state['lead_user_id']=$data['user_id'];
if (str_ends_with($table,'houzez_crm_enquiries')) $this->state['enquiry_user_id']=$data['user_id'];
return 1;
}
};
$assign = new ReflectionMethod(Jenhome_Contact_Exports::class,'assign_inquiry_owner'); $assign->setAccessible(true);
check($assign->invoke(null,11,10)===true && $GLOBALS['wpdb']->state['lead_user_id']===7 && $GLOBALS['wpdb']->state['enquiry_user_id']===7,'new inquiry and its unique lead are assigned together');
check($GLOBALS['wpdb']->transactions===['START TRANSACTION','COMMIT'],'inquiry owner update commits as one transaction');
$GLOBALS['wpdb']->state=['enquiry_user_id'=>1,'lead_user_id'=>1,'inquiry_count'=>2]; $GLOBALS['wpdb']->transactions=[];
check($assign->invoke(null,11,10)===false && $GLOBALS['wpdb']->transactions===[],'ambiguous reused lead is not reassigned');
$GLOBALS['wpdb']=new class {
public $prefix='test_';
public function prepare($query,...$args) { return $query; }
@@ -107,16 +125,16 @@ try {
ob_start(); try { Jenhome_Contact_Exports::export_inquiries(); } catch(Test_Die $e) {} $csv=ob_get_clean();
$rows=explode("\r\n",substr($csv,3));
$header=str_getcsv($rows[0],',','"',''); $row=str_getcsv($rows[1],',','"','');
check($header===['Submission Date','First Name','Middle Name','Last Name','Referral Source','Referrer Name','Email','Mobile'],'requested eight-column inquiry export');
check(count($row)===8 && $row[2]==='Anaïs' && $row[5]==='María' && $row[6]==='synthetic@example.invalid' && $row[7]==='2025550100','export values align with headings');
check($header===['Submission Date','First Name','Middle Name','Last Name','Referral Source','Referrer Name','Mobile','Email'],'requested eight-column inquiry export with Email last');
check(count($row)===8 && $row[2]==='Anaïs' && $row[5]==='María' && $row[6]==='2025550100' && $row[7]==='synthetic@example.invalid','export values align with headings');
check(!str_contains($csv,'DO NOT EXPORT') && !str_contains($csv,'https://staging.jenhome.net/contact/'),'removed fields and source URL absent from inquiry CSV');
check($row[0]==='2026-10-28 11:30:00 -04:00','submission date remains first and localized');
ob_start(); try { Jenhome_Contact_Exports::export_leads(); } catch(Test_Die $e) {} $csv=ob_get_clean();
$rows=explode("\r\n",substr($csv,3));
$header=str_getcsv($rows[0],',','"',''); $row=str_getcsv($rows[1],',','"','');
check(count($header)===24 && count($row)===24 && !in_array('Source Link',$header,true),'lead export omits Source Link heading and value');
check($header===['Submission Date','First Name','Middle Name','Last Name','Referral Source','Referrer Name','Prefix','Full Name','Mobile','Home Phone','Work Phone','Address','City','County / State','Country','Postal Code / Zip','Type','Twitter','Linkedin','Facebook','Private Note','Message','Inquiry Street Address','Email'] && count($row)===24 && !in_array('Source Link',$header,true),'lead export has Email last and omits Source Link');
check(!str_contains($csv,'https://staging.jenhome.net/contact/'),'source URL absent from lead CSV');
$lead=array_combine($header,$row);
check($lead['Referral Source']==='Friend' && $lead['Referrer Name']==='María' && $lead['Email']==='synthetic@example.invalid' && $lead['Mobile']==='2025550100','lead referral and contact values stay aligned');
check($lead['Referral Source']==='Friend' && $lead['Referrer Name']==='María' && $lead['Email']==='synthetic@example.invalid' && $lead['Mobile']==='2025550100' && end($row)==='synthetic@example.invalid','lead referral and contact values stay aligned with Email last');
echo "PASS: $checks updater and CSV checks\n";
} finally { foreach($paths as $p) if(file_exists($p)) unlink($p); }