PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 8.8.4
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v8.8.4
8.8.4 8.8.3 8.8.2 8.8.1 8.8.0 8.7.9 8.7.8 8.7.7 8.7.6 8.7.5 8.7.4 8.7.3 8.7.2 8.7.1 8.7.0 8.6.9 8.6.8 8.6.7 8.6.6 8.6.5 8.6.4 8.6.2 8.6.1 8.6.0 8.5.9 All 540 releases
← All changes | addons/automator/includes/triggers/wpbot-triggers.php +207 -207 8.7.9 → 8.8.4 View file →
@@ -1,207 +1,207 @@
1 -<?php
2 -/**
3 - * WPBot Triggers
4 - *
5 - * @package WPbot_Automator
6 - */
7 -
8 -namespace WPbot_Automator\Triggers;
9 -
10 -if ( ! defined( 'ABSPATH' ) ) {
11 - exit;
12 -}
13 -
14 -/**
15 - * Class WPBot_Triggers
16 - */
17 -class WPBot_Triggers extends Trigger {
18 -
19 - /**
20 - * Constructor
21 - */
22 - public function __construct() {
23 - $this->id = 'wpbot';
24 - $this->group = __( 'WPBot', 'wpbot-automator' );
25 - }
26 -
27 - /**
28 - * Get available sub-triggers
29 - */
30 - public static function get_sub_triggers() {
31 - return array(
32 - 'wpbot_chat_session_saved' => array(
33 - 'title' => __( 'WPBot Chat Session Saved', 'wpbot-automator' ),
34 - 'hook' => 'wpbot_chat_session_saved_automator',
35 - ),
36 - 'conversationalforms_submit' => array(
37 - 'title' => __( 'Conversational Forms Submitted', 'wpbot-automator' ),
38 - 'hook' => 'qccf_value_saved',
39 - )
40 - );
41 - }
42 -
43 - /**
44 - * Register the triggers
45 - */
46 - public function register() {
47 - // Register the delayed cron trigger
48 - add_action( 'wpbot_automator_delayed_trigger', array( $this, 'fire_delayed_trigger' ), 10, 1 );
49 -
50 - $triggers = self::get_sub_triggers();
51 -
52 - foreach ( $triggers as $id => $config ) {
53 - add_action( $config['hook'], function( ...$args ) use ( $id ) {
54 - $this->handle_trigger( $id, $args );
55 - }, 10, 10 );
56 - }
57 - }
58 -
59 - /**
60 - * Handle the trigger execution
61 - *
62 - * @param string $sub_id Sub-trigger ID.
63 - * @param array $args Arguments from the hook.
64 - */
65 - public function handle_trigger( $sub_id, $args ) {
66 - $data = array();
67 -
68 - switch ( $sub_id ) {
69 - case 'wpbot_chat_session_saved':
70 - if ( isset( $args[0] ) && is_array( $args[0] ) ) {
71 - $data = $args[0];
72 - }
73 - break;
74 -
75 - case 'conversationalforms_submit':
76 - if ( isset( $args[0], $args[1] ) ) {
77 - $all_answers = $args[0];
78 - $submittedFormId = $args[1];
79 -
80 - $data = array(
81 - 'form_id' => $submittedFormId,
82 - 'form_name' => 'Conversational Form ' . $submittedFormId,
83 - 'fields' => $all_answers,
84 - );
85 -
86 - $form = array();
87 - if ( class_exists( 'Qcformbuilder_Forms_Forms' ) ) {
88 - $form = \Qcformbuilder_Forms_Forms::get_form( $submittedFormId );
89 - if ( ! empty( $form['name'] ) ) {
90 - $data['form_name'] = $form['name'];
91 - }
92 - }
93 -
94 - if ( is_array( $all_answers ) ) {
95 - foreach ( $all_answers as $answer ) {
96 - if ( is_object( $answer ) && isset( $answer->slug ) && isset( $answer->value ) ) {
97 - $slug = $answer->slug;
98 - $val = stripslashes( $answer->value );
99 -
100 - // Map by slug
101 - $safe_slug = preg_replace( '/[^a-z0-9]+/', '_', strtolower( $slug ) );
102 - $safe_slug = trim( $safe_slug, '_' );
103 - if ( $safe_slug && ! isset( $data[ $safe_slug ] ) ) {
104 - $data[ $safe_slug ] = $val;
105 - }
106 -
107 - // Aggressive mapping for email
108 - if ( ! isset( $data['email'] ) ) {
109 - if ( is_email( trim( $val ) ) ) {
110 - $data['email'] = trim( $val );
111 - } elseif ( strpos( $safe_slug, 'email' ) !== false ) {
112 - $data['email'] = $val;
113 - }
114 - }
115 -
116 - // Aggressive mapping for name
117 - if ( ! isset( $data['name'] ) ) {
118 - if ( strpos( $safe_slug, 'name' ) !== false || strpos( $safe_slug, 'first' ) !== false ) {
119 - $data['name'] = $val;
120 - }
121 - }
122 -
123 - // Map by label & heuristics
124 - if ( ! empty( $form['fields'] ) ) {
125 - foreach ( $form['fields'] as $field ) {
126 - if ( isset( $field['slug'] ) && $field['slug'] === $slug ) {
127 - if ( ! empty( $field['label'] ) ) {
128 - $safe_label = preg_replace( '/[^a-z0-9]+/', '_', strtolower( $field['label'] ) );
129 - $safe_label = trim( $safe_label, '_' );
130 - if ( $safe_label && ! isset( $data[ $safe_label ] ) ) {
131 - $data[ $safe_label ] = $val;
132 - }
133 -
134 - $label_lower = strtolower( $field['label'] );
135 - if ( strpos( $label_lower, 'email' ) !== false && ! isset( $data['email'] ) ) {
136 - $data['email'] = $val;
137 - } elseif ( ( strpos( $label_lower, 'name' ) !== false || strpos( $label_lower, 'first' ) !== false ) && ! isset( $data['name'] ) ) {
138 - $data['name'] = $val;
139 - }
140 - }
141 -
142 - if ( isset( $field['type'] ) ) {
143 - if ( $field['type'] === 'email' ) {
144 - $data['email'] = $val;
145 - } elseif ( in_array( $field['type'], array( 'name', 'first_name', 'text' ), true ) ) {
146 - // Only set if we haven't already deduced a name
147 - if ( $field['type'] === 'name' && ! isset( $data['name'] ) ) {
148 - $data['name'] = $val;
149 - }
150 - }
151 - }
152 - break;
153 - }
154 - }
155 - }
156 - }
157 - }
158 - }
159 - }
160 - break;
161 - }
162 -
163 - $this->run( array(
164 - 'sub_id' => $sub_id,
165 - 'data' => $data,
166 - ) );
167 - }
168 -
169 - /**
170 - * Fire the delayed trigger by fetching the latest data from the database
171 - *
172 - * @param string $session_id The chat session ID.
173 - */
174 - public function fire_delayed_trigger( $session_id ) {
175 - if ( empty( $session_id ) ) {
176 - return;
177 - }
178 -
179 - global $wpdb;
180 - $tableuser = $wpdb->prefix . 'wpbot_user';
181 - $tableconversation = $wpdb->prefix . 'wpbot_conversation';
182 -
183 - $user = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$tableuser} WHERE session_id = %s", $session_id ) );
184 -
185 - if ( $user ) {
186 - $conv = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$tableconversation} WHERE user_id = %d", $user->id ) );
187 -
188 - $clean_conversation = '';
189 - if ( $conv && ! empty( $conv->conversation ) ) {
190 - $decoded = html_entity_decode( wp_unslash( $conv->conversation ), ENT_QUOTES | ENT_HTML5, 'UTF-8' );
191 - // Add spaces/newlines around elements to prevent text merging
192 - $decoded = str_replace( array( '</li>', '</div>', '<br>', '<br/>', '</p>' ), "\n", $decoded );
193 - $clean_conversation = trim( wp_strip_all_tags( $decoded ) );
194 - }
195 -
196 - do_action( 'wpbot_chat_session_saved_automator', array(
197 - 'session_id' => $session_id,
198 - 'name' => $user->name,
199 - 'email' => $user->email,
200 - 'phone' => $user->phone,
201 - 'conversation' => $clean_conversation,
202 - 'source_url' => '', // Retained empty or could be parsed from session
203 - 'user_agent' => $conv ? $conv->environment_info : '',
204 - ) );
205 - }
206 - }
207 -}
1 +<?php
2 +/**
3 + * WPBot Triggers
4 + *
5 + * @package WPbot_Automator
6 + */
7 +
8 +namespace WPbot_Automator\Triggers;
9 +
10 +if ( ! defined( 'ABSPATH' ) ) {
11 + exit;
12 +}
13 +
14 +/**
15 + * Class WPBot_Triggers
16 + */
17 +class WPBot_Triggers extends Trigger {
18 +
19 + /**
20 + * Constructor
21 + */
22 + public function __construct() {
23 + $this->id = 'wpbot';
24 + $this->group = __( 'WPBot', 'wpbot-automator' );
25 + }
26 +
27 + /**
28 + * Get available sub-triggers
29 + */
30 + public static function get_sub_triggers() {
31 + return array(
32 + 'wpbot_chat_session_saved' => array(
33 + 'title' => __( 'WPBot Chat Session Saved', 'wpbot-automator' ),
34 + 'hook' => 'wpbot_chat_session_saved_automator',
35 + ),
36 + 'conversationalforms_submit' => array(
37 + 'title' => __( 'Conversational Forms Submitted', 'wpbot-automator' ),
38 + 'hook' => 'qccf_value_saved',
39 + )
40 + );
41 + }
42 +
43 + /**
44 + * Register the triggers
45 + */
46 + public function register() {
47 + // Register the delayed cron trigger
48 + add_action( 'wpbot_automator_delayed_trigger', array( $this, 'fire_delayed_trigger' ), 10, 1 );
49 +
50 + $triggers = self::get_sub_triggers();
51 +
52 + foreach ( $triggers as $id => $config ) {
53 + add_action( $config['hook'], function( ...$args ) use ( $id ) {
54 + $this->handle_trigger( $id, $args );
55 + }, 10, 10 );
56 + }
57 + }
58 +
59 + /**
60 + * Handle the trigger execution
61 + *
62 + * @param string $sub_id Sub-trigger ID.
63 + * @param array $args Arguments from the hook.
64 + */
65 + public function handle_trigger( $sub_id, $args ) {
66 + $data = array();
67 +
68 + switch ( $sub_id ) {
69 + case 'wpbot_chat_session_saved':
70 + if ( isset( $args[0] ) && is_array( $args[0] ) ) {
71 + $data = $args[0];
72 + }
73 + break;
74 +
75 + case 'conversationalforms_submit':
76 + if ( isset( $args[0], $args[1] ) ) {
77 + $all_answers = $args[0];
78 + $submittedFormId = $args[1];
79 +
80 + $data = array(
81 + 'form_id' => $submittedFormId,
82 + 'form_name' => 'Conversational Form ' . $submittedFormId,
83 + 'fields' => $all_answers,
84 + );
85 +
86 + $form = array();
87 + if ( class_exists( 'Qcformbuilder_Forms_Forms' ) ) {
88 + $form = \Qcformbuilder_Forms_Forms::get_form( $submittedFormId );
89 + if ( ! empty( $form['name'] ) ) {
90 + $data['form_name'] = $form['name'];
91 + }
92 + }
93 +
94 + if ( is_array( $all_answers ) ) {
95 + foreach ( $all_answers as $answer ) {
96 + if ( is_object( $answer ) && isset( $answer->slug ) && isset( $answer->value ) ) {
97 + $slug = $answer->slug;
98 + $val = stripslashes( $answer->value );
99 +
100 + // Map by slug
101 + $safe_slug = preg_replace( '/[^a-z0-9]+/', '_', strtolower( $slug ) );
102 + $safe_slug = trim( $safe_slug, '_' );
103 + if ( $safe_slug && ! isset( $data[ $safe_slug ] ) ) {
104 + $data[ $safe_slug ] = $val;
105 + }
106 +
107 + // Aggressive mapping for email
108 + if ( ! isset( $data['email'] ) ) {
109 + if ( is_email( trim( $val ) ) ) {
110 + $data['email'] = trim( $val );
111 + } elseif ( strpos( $safe_slug, 'email' ) !== false ) {
112 + $data['email'] = $val;
113 + }
114 + }
115 +
116 + // Aggressive mapping for name
117 + if ( ! isset( $data['name'] ) ) {
118 + if ( strpos( $safe_slug, 'name' ) !== false || strpos( $safe_slug, 'first' ) !== false ) {
119 + $data['name'] = $val;
120 + }
121 + }
122 +
123 + // Map by label & heuristics
124 + if ( ! empty( $form['fields'] ) ) {
125 + foreach ( $form['fields'] as $field ) {
126 + if ( isset( $field['slug'] ) && $field['slug'] === $slug ) {
127 + if ( ! empty( $field['label'] ) ) {
128 + $safe_label = preg_replace( '/[^a-z0-9]+/', '_', strtolower( $field['label'] ) );
129 + $safe_label = trim( $safe_label, '_' );
130 + if ( $safe_label && ! isset( $data[ $safe_label ] ) ) {
131 + $data[ $safe_label ] = $val;
132 + }
133 +
134 + $label_lower = strtolower( $field['label'] );
135 + if ( strpos( $label_lower, 'email' ) !== false && ! isset( $data['email'] ) ) {
136 + $data['email'] = $val;
137 + } elseif ( ( strpos( $label_lower, 'name' ) !== false || strpos( $label_lower, 'first' ) !== false ) && ! isset( $data['name'] ) ) {
138 + $data['name'] = $val;
139 + }
140 + }
141 +
142 + if ( isset( $field['type'] ) ) {
143 + if ( $field['type'] === 'email' ) {
144 + $data['email'] = $val;
145 + } elseif ( in_array( $field['type'], array( 'name', 'first_name', 'text' ), true ) ) {
146 + // Only set if we haven't already deduced a name
147 + if ( $field['type'] === 'name' && ! isset( $data['name'] ) ) {
148 + $data['name'] = $val;
149 + }
150 + }
151 + }
152 + break;
153 + }
154 + }
155 + }
156 + }
157 + }
158 + }
159 + }
160 + break;
161 + }
162 +
163 + $this->run( array(
164 + 'sub_id' => $sub_id,
165 + 'data' => $data,
166 + ) );
167 + }
168 +
169 + /**
170 + * Fire the delayed trigger by fetching the latest data from the database
171 + *
172 + * @param string $session_id The chat session ID.
173 + */
174 + public function fire_delayed_trigger( $session_id ) {
175 + if ( empty( $session_id ) ) {
176 + return;
177 + }
178 +
179 + global $wpdb;
180 + $tableuser = $wpdb->prefix . 'wpbot_user';
181 + $tableconversation = $wpdb->prefix . 'wpbot_conversation';
182 +
183 + $user = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$tableuser} WHERE session_id = %s", $session_id ) );
184 +
185 + if ( $user ) {
186 + $conv = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$tableconversation} WHERE user_id = %d", $user->id ) );
187 +
188 + $clean_conversation = '';
189 + if ( $conv && ! empty( $conv->conversation ) ) {
190 + $decoded = html_entity_decode( wp_unslash( $conv->conversation ), ENT_QUOTES | ENT_HTML5, 'UTF-8' );
191 + // Add spaces/newlines around elements to prevent text merging
192 + $decoded = str_replace( array( '</li>', '</div>', '<br>', '<br/>', '</p>' ), "\n", $decoded );
193 + $clean_conversation = trim( wp_strip_all_tags( $decoded ) );
194 + }
195 +
196 + do_action( 'wpbot_chat_session_saved_automator', array(
197 + 'session_id' => $session_id,
198 + 'name' => $user->name,
199 + 'email' => $user->email,
200 + 'phone' => $user->phone,
201 + 'conversation' => $clean_conversation,
202 + 'source_url' => '', // Retained empty or could be parsed from session
203 + 'user_agent' => $conv ? $conv->environment_info : '',
204 + ) );
205 + }
206 + }
207 +}