Assign Jen Home inquiries to designated admin
All checks were successful
Test and publish WordPress plugin / release (push) Successful in 23s
All checks were successful
Test and publish WordPress plugin / release (push) Successful in 23s
This commit is contained in:
@@ -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.3.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.3.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();
|
||||
@@ -227,11 +230,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 : [];
|
||||
|
||||
Reference in New Issue
Block a user