plugin_key = $plugin_key; $this->form_name = $form_name; $this->form_id = $form_id; } /** * Resolve the effective CRM label for this submission: * per-form custom label first, otherwise the detected form title. * @return string */ public function get_effective_label() { return Leadcrm_Options::get_form_label($this->plugin_key, $this->form_id, $this->form_name); } /** * Create an api endpoint url where we can deliver the submission * That's including the integration token & form name * Sample result, will be something like this: * http://localhost/integrations/api/v1/wpforms-webhook?leadcrm_token=1JASH23&cf_reference=website * @since 0.2 */ public function build_api_endpoint() { $token = Leadcrm_Options::get_single_value('leadcrm_token'); if (empty($token)) return null; $endpoint = Leadcrm_Constants::get_webhook_url($this->plugin_key); $cf_reference = Leadcrm_Options::get_single_value('cf_reference'); // Make the required information into a query string $queries = array( 'leadcrm_token' => $token, 'cf_reference' => $cf_reference ); $querystring = http_build_query($queries); return "{$endpoint}?{$querystring}"; } /** * Simply format payload in associative array into each respective content type * @param array $payload data to be formatted * @param ContentType $type type output for the payload */ private function format_payload($payload, $type) { if ($type == Leadcrm_Content_Type::JSON) return wp_json_encode($payload); if ($type == Leadcrm_Content_Type::FORM_URLENCODED) return http_build_query($payload); return $payload; } /** * To check if the integration is enabled or not * by retrieve the integration state from wp_options */ private function is_integration_enabled() { $is_enabled = Leadcrm_Options::get_single_value($this->plugin_key); return 'true' == $is_enabled; } /** * Make an API submission to the 365 CRM for the lead * @since 0.2 */ public function submit_lead_to_leadcrm($endpoint, $payload, $type) { $is_enabled = $this->is_integration_enabled(); if (!$endpoint || !$payload || !$is_enabled) return; $payload["form_name"] = $this->form_name; // Per-form CRM label: custom label if the admin set one, // otherwise the detected form title. Global cf_reference // (Website Name) is still sent separately by each caller. if (!isset($payload["label"]) || $payload["label"] === '') { $payload["label"] = $this->get_effective_label(); } $options = [ 'body' => self::format_payload($payload, $type), 'headers' => ['Content-Type' => $type], 'blocking' => false, 'data_format' => 'body', ]; wp_remote_post($endpoint, $options); } }