*/ class Leadcrm_Options { /** * Save fields into on `wp_options`, * Be careful to call this, because it will override the existing 365 CRM options * If you only plan to save one field, try get_values() first, * and then add the one value you want to replace on top of existing option * @param array $fields payload to be saved into `wp_options` */ public static function set_values($fields) { $allowed_keys = array_column(Leadcrm_Constants::SUPPORTED_INTEGRATIONS, 'key'); array_push($allowed_keys, 'cf_reference', 'leadcrm_token', 'form_labels'); $filtered_values = array_intersect_key($fields, array_flip($allowed_keys)); // Sanitize the nested per-form labels map (integration => form_id => label). if (isset($filtered_values['form_labels'])) { $filtered_values['form_labels'] = self::sanitize_form_labels($filtered_values['form_labels']); } update_option(Leadcrm_Constants::WP_OPTION_NAME, $filtered_values); } /** * Get all value from `leadcrm_options` on `wp_options` * @return array */ public static function get_values() { $options = get_option(Leadcrm_Constants::WP_OPTION_NAME); return $options ? $options : array(); } /** * Get single value from `leadcrm_options` on `wp_options` * @param string $key option field identifier, e.g elementor_form */ public static function get_single_value($key) { $options = self::get_values(); return isset($options[$key]) ? $options[$key] : null; } /** * Get the stored per-form label map: * array(integration_key => array(form_id => label)) * @return array */ public static function get_form_labels() { $options = self::get_values(); if (isset($options['form_labels']) && is_array($options['form_labels'])) { return $options['form_labels']; } return array(); } /** * Resolve the effective CRM label for one submitted form. * Custom label wins; otherwise falls back to the form title. * * @param string $integration_key e.g. 'cf7' * @param string|int $form_id submitted form identifier (may be empty for page-embedded forms) * @param string $fallback_title form title detected at submission time * @return string */ public static function get_form_label($integration_key, $form_id, $fallback_title = '') { $form_id = (string) $form_id; if ($form_id !== '') { $all = self::get_form_labels(); if (isset($all[$integration_key]) && is_array($all[$integration_key])) { if (isset($all[$integration_key][$form_id])) { $custom = trim((string) $all[$integration_key][$form_id]); if ($custom !== '') return mb_substr($custom, 0, 120); } } } $fallback_title = trim((string) $fallback_title); if ($fallback_title !== '') return mb_substr($fallback_title, 0, 120); return ''; } /** * Clean a raw form_labels structure coming from $_POST (array or JSON string). * @param mixed $raw * @return array */ public static function sanitize_form_labels($raw) { if (is_string($raw)) { $decoded = json_decode(stripslashes($raw), true); if (is_array($decoded)) $raw = $decoded; else return array(); } if (!is_array($raw)) return array(); $allowed = array_column(Leadcrm_Constants::SUPPORTED_INTEGRATIONS, 'key'); $clean = array(); foreach ($raw as $integration_key => $forms) { if (!in_array($integration_key, $allowed, true)) continue; if (is_string($forms)) { $decoded = json_decode(stripslashes($forms), true); if (is_array($decoded)) $forms = $decoded; else continue; } if (!is_array($forms)) continue; foreach ($forms as $form_id => $label) { $form_id = (string) $form_id; if ($form_id === '') continue; if (function_exists('wp_unslash')) $label = wp_unslash($label); $label = trim((string) $label); if ($label === '') continue; // empty = use form title default if (function_exists('wp_strip_all_tags')) $label = wp_strip_all_tags($label); $label = mb_substr($label, 0, 120); if ($label === '') continue; if (!isset($clean[$integration_key])) $clean[$integration_key] = array(); $clean[$integration_key][$form_id] = $label; } } return $clean; } /** * Get all plugins that supported by our wordpress plugin * with the status of each plugin wether it is installed or not */ public static function get_available_integrations() { $add_detail = function ($integration) { $key = $integration['key']; $iconFile = $integration['iconFile']; $integration['enabled'] = Leadcrm_Options::get_single_value($key); $integration['status'] = Leadcrm_Integration_Status::get_status($integration); $integration['iconUrl'] = Leadcrm_Constants::get_plugin_icon_url($iconFile); $integration['listable'] = Leadcrm_Form_Providers::is_listable($key); // Recheck to make sure the integration is disabled when status is not activated $is_connected = $integration['status'] > Leadcrm_Integration_Status::ACTIVATED; $integration['enabled'] = $integration['enabled'] && $is_connected; return $integration; }; $result = array_map($add_detail, Leadcrm_Constants::SUPPORTED_INTEGRATIONS); usort($result, function ($left, $right) { return $right['status'] - $left['status']; }); return $result; } /** * Handler for new/updated options * coming from wp-admin dashboard (AJAX). */ public function save_options_handler() { if (function_exists('current_user_can') && !current_user_can('manage_options')) { wp_send_json_error(array('message' => 'Unauthorized'), 403); } if (function_exists('check_ajax_referer')) { if (!check_ajax_referer('leadcrm_save_options', 'nonce', false)) { wp_send_json_error(array('message' => 'Invalid security token. Please refresh and try again.'), 403); } } $submission = array(); $allowed_integrations = array_column(Leadcrm_Constants::SUPPORTED_INTEGRATIONS, 'key'); // Integration toggles: only enabled ones are posted by the UI. foreach ($allowed_integrations as $key) { if (isset($_POST[$key])) { $submission[$key] = 'true'; } } if (isset($_POST['leadcrm_token'])) { $submission['leadcrm_token'] = sanitize_text_field(wp_unslash($_POST['leadcrm_token'])); } if (isset($_POST['cf_reference'])) { $submission['cf_reference'] = sanitize_text_field(wp_unslash($_POST['cf_reference'])); } // form_labels arrives as a JSON string (URLSearchParams cannot nest objects). if (isset($_POST['form_labels'])) { $submission['form_labels'] = wp_unslash($_POST['form_labels']); } elseif (($existing = self::get_values()) && isset($existing['form_labels'])) { // Preserve existing labels when the UI posts without them. $submission['form_labels'] = $existing['form_labels']; } $existing_token = self::get_single_value('leadcrm_token'); if ($existing_token) { self::set_values($submission); wp_send_json_success(array('message' => 'Settings saved.')); } // If existing token does not exist // We will enable all of the installed integration by default $integrations = self::get_available_integrations(); $installed = array_filter($integrations, function ($integration) { return $integration['status'] >= Leadcrm_Integration_Status::INSTALLED; }); foreach($installed as $integration) { $key = $integration['key']; $submission[$key] = 'true'; } self::set_values($submission); wp_send_json_success(array('message' => 'Settings saved.')); } /** * AJAX handler: return the form list for one integration. * Action: wp_ajax_leadcrm_get_forms */ public function get_forms_handler() { if (function_exists('current_user_can') && !current_user_can('manage_options')) { wp_send_json_error(array('message' => 'Unauthorized'), 403); } if (function_exists('check_ajax_referer')) { if (!check_ajax_referer('leadcrm_save_options', 'nonce', false)) { wp_send_json_error(array('message' => 'Invalid security token. Please refresh and try again.'), 403); } } $key = isset($_REQUEST['integration']) ? sanitize_key($_REQUEST['integration']) : ''; if ($key === '') { wp_send_json_error(array('message' => 'Missing integration key'), 400); } if (!Leadcrm_Form_Providers::is_listable($key)) { wp_send_json_success(array('forms' => array(), 'listable' => false)); } $forms = Leadcrm_Form_Providers::get_forms($key); $labels = self::get_form_labels(); $saved = isset($labels[$key]) && is_array($labels[$key]) ? $labels[$key] : array(); foreach ($forms as &$form) { $fid = (string) $form['id']; $form['label'] = isset($saved[$fid]) ? $saved[$fid] : ''; } wp_send_json_success(array('forms' => array_values($forms), 'listable' => true)); } }