'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();
add_action('wp_enqueue_scripts', [__CLASS__, 'assets']);
add_filter('elementor/widget/render_content', [__CLASS__, 'widget'], 30, 2);
add_filter('wp_mail', [__CLASS__, 'mail_fields']);
add_action('wp_loaded', [__CLASS__, 'wrap_collector'], 20);
require_once __DIR__ . '/crm-display.php';
Jenhome_CRM_Display::boot();
foreach (['houzez_property_agent_contact_fields', 'houzez_schedule_tour_fields', 'houzez_realtor_contact_form_fields'] as $hook) {
add_action($hook, [__CLASS__, 'native_fields']);
}
foreach (self::ACTIONS as $action) {
add_action('wp_ajax_' . $action, [__CLASS__, 'prepare'], 0);
add_action('wp_ajax_nopriv_' . $action, [__CLASS__, 'prepare'], 0);
}
// Keep the existing buttons/endpoints. Run before (and terminate before) vendor exports.
add_action('wp_ajax_houzez_crm_export_leads', [__CLASS__, 'export_leads'], 0);
add_action('wp_ajax_crm_export_inquiries', [__CLASS__, 'export_inquiries'], 0);
}
/** Site-owned extension data: no changes to the vendor's tables or historical rows. */
public static function install_storage() {
if (get_option('jenhome_contact_fields_schema') === '1') return;
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$table = $wpdb->prefix . 'jenhome_contact_fields';
$collate = $wpdb->get_charset_collate();
dbDelta("CREATE TABLE $table (
lead_id bigint(20) unsigned NOT NULL,
enquiry_id bigint(20) unsigned NOT NULL DEFAULT 0,
middle_name text NOT NULL,
referrer_form varchar(32) NOT NULL DEFAULT '',
PRIMARY KEY (lead_id)
) ENGINE=InnoDB $collate;");
if ($wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $wpdb->esc_like($table))) === $table) {
update_option('jenhome_contact_fields_schema', '1', false);
}
}
public static function referrals() {
$settings = get_option('hcrm_lead_settings', []);
return array_values(array_filter(array_map('trim', explode(',', (string)($settings['source'] ?? '')))));
}
public static function assets() {
wp_enqueue_script('jenhome-contact-exports', plugins_url('forms.js', __FILE__), ['jquery'], self::VERSION, true);
wp_localize_script('jenhome-contact-exports', 'JenhomeContact', [
'actions' => self::ACTIONS, 'referralForms' => self::REFERRAL_FORMS,
'referrals' => self::referrals(), 'nonce' => wp_create_nonce('jenhome_contact'),
'exportNonce' => is_user_logged_in() ? wp_create_nonce('jenhome_export') : '',
]);
wp_register_style('jenhome-contact-exports', false, [], self::VERSION);
wp_enqueue_style('jenhome-contact-exports');
wp_add_inline_style('jenhome-contact-exports', '.jenhome-fields{width:100%;flex:0 0 100%;display:grid;grid-template-columns:1fr 1fr;gap:12px;margin-bottom:16px}.jenhome-field{min-width:0;display:flex;flex-direction:column;justify-content:flex-end}.jenhome-referral{grid-column:1/-1}.jenhome-field label{display:block;margin-bottom:5px}.jenhome-field .form-control{width:100%}@media(max-width:480px){.jenhome-fields{grid-template-columns:1fr}}');
}
public static function fields($first = true, $last = true, $referral = true, $middle = true) {
$html = ($first || $last || $referral || $middle) ? '
' : '';
foreach (['first_name' => ['First Name', 'given-name', $first], 'middle_name' => ['Middle Name (optional)', 'additional-name', $middle], 'last_name' => ['Last Name', 'family-name', $last]] as $key => $field) {
if (!$field[2]) continue;
$id = wp_unique_id('jenhome-' . $key . '-');
$html .= '
';
}
if ($referral) {
$id = wp_unique_id('jenhome-referral-');
$html .= '
';
}
return $html . (($first || $last || $referral || $middle) ? '
' : '') . '';
}
public static function native_fields() { echo self::fields(); }
/** Render-time adaptation leaves saved Elementor layouts and vendor templates intact. */
public static function widget($html, $widget) {
if (strpos($widget->get_name(), 'houzez') !== 0 || strpos($html, '~is', function($match) {
$form = $match[0];
$p = new WP_HTML_Tag_Processor($form);
$action = $form_id = '';
while ($p->next_tag()) {
if ($p->get_attribute('name') === 'action') $action = $p->get_attribute('value');
if ($p->get_attribute('name') === 'form_id') $form_id = $p->get_attribute('value');
}
if (!in_array($action, self::ACTIONS, true)) return $form;
$p = new WP_HTML_Tag_Processor($form);
$names = [];
while ($p->next_tag()) {
$name = $p->get_attribute('name');
if ($name === 'enquiry_type' && in_array($form_id, self::REFERRAL_FORMS, true)) {
$p->set_attribute('name', 'referral_source');
$name = 'referral_source';
}
if ($name === 'e_meta[streat_address]' && in_array($form_id, self::REFERRER_FORMS, true)) {
$p->set_attribute('name', 'referrer_name');
$name = 'referrer_name';
}
if (in_array($name, ['first_name', 'last_name'], true)) {
$p->set_attribute('required', true);
$p->set_attribute('autocomplete', $name === 'first_name' ? 'given-name' : 'family-name');
}
$names[] = $name;
}
$form = $p->get_updated_html();
if (!in_array('jenhome_contact_nonce', $names, true)) {
$fields = self::fields(!in_array('first_name', $names, true), !in_array('last_name', $names, true), !in_array('referral_source', $names, true), !in_array('middle_name', $names, true));
// Existing name fields stay in place; newly needed fields precede the submit button.
$pos = stripos($form, '