PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, AI Services / 7.4.5
WPBot – AI ChatBot for Live Support, Lead Generation, AI Services v7.4.5
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 8.5.8 8.5.7 8.5.6 8.5.5 8.5.4 8.5.3 8.5.2 8.5.0 8.4.9 All 531 releases
chatbot / includes / class-common-function.php

class-common-function.php in WPBot – AI ChatBot for Live Support, Lead Generation, AI Services 7.4.5, at includes/class-common-function.php

131 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Common functions class
4 */
5 class Qcld_WPBot_Common_Functions {
6
7
8 private function __construct() {
9 add_action('wp_ajax_wpbot_save_feedback', array( $this, 'wpbot_save_feedback') );
10 add_action('wp_ajax_nopriv_wpbot_save_feedback', array( $this,'wpbot_save_feedback') );
11 add_action( 'wp_ajax_wpbot_save_report', array( $this, 'wpbot_save_report') );
12 add_action( 'wp_ajax_nopriv_wpbot_save_report', array( $this, 'wpbot_save_report') );
13 }
14 /**
15 * Remove stopwords from search query
16 *
17 * @param string $query The search query
18 * @param array $stopwords Array of stopwords to remove
19 * @return string Query with stopwords removed
20 */
21 public static function qcpd_remove_wa_stopwords($query, $stopwords) {
22 return preg_replace('/\b('.implode('|',$stopwords).')\b/','',$query);
23 }
24
25 /**
26 * Get relevant page links based on search query
27 *
28 * @param string $search_query The search query
29 * @return array Array of relevant page links
30 */
31 public static function qcpd_relevant_pagelink($search_query) {
32 $stopwords = explode(',', get_option('qlcd_wp_chatbot_stop_words'));
33
34 $finalQueryWordsWithoutStopWords = self::qcpd_remove_wa_stopwords(strtolower($search_query), $stopwords);
35
36 $cleanWordsWithoutPunctuationMarks = preg_replace('/[\p{P}]/u', '', $finalQueryWordsWithoutStopWords);
37
38 $q = trim($cleanWordsWithoutPunctuationMarks);
39
40 $links = [];
41
42 $post_type_array = get_option('qcld_openai_relevant_post');
43
44 $the_query = new WP_Query(array(
45 'post_status' => 'publish',
46 'posts_per_page' => 5,
47 's' => esc_attr($q),
48 'post_type' => $post_type_array
49 ));
50
51 if($the_query->have_posts()) {
52 while($the_query->have_posts()) {
53 $the_query->the_post();
54
55 $url = esc_url(get_permalink());
56 $link = '<a href=' . $url . ' target="_blank">' . get_the_title() . '</a>';
57 array_push($links, $link);
58 }
59 wp_reset_postdata();
60 }
61
62 $links = array_unique($links);
63 return $links;
64 }
65
66
67 public function wpbot_save_feedback() {
68 global $wpdb;
69
70 $table = $wpdb->prefix . 'wpbot_chat_report';
71
72 $user_id = intval($_POST['user_id']);
73 $conversation_id= intval($_POST['conversation_id']);
74 $message = sanitize_text_field($_POST['message']);
75 $feedback = sanitize_text_field($_POST['feedback']); // "like" or "dislike"
76 $meta_info = sanitize_textarea_field($_POST['meta_info']);
77 $date = current_time('mysql');
78
79 $inserted = $wpdb->insert($table, array(
80 'user_id' => $user_id,
81 'conversation_id'=> $conversation_id,
82 'message' => $message,
83 'feedback' => $feedback,
84 'meta_info' => $meta_info,
85 'created_at' => $date
86 ));
87
88 if ($inserted !== false) {
89 wp_send_json_success(array('message' => 'Feedback saved.'));
90 } else {
91 wp_send_json_error(array('message' => 'Failed to save feedback.'));
92 }
93 }
94
95 /**
96 * Save report func
97 *
98 */
99
100
101 public function wpbot_save_report() {
102 global $wpdb;
103 $table_report = $wpdb->prefix . 'wpbot_chat_report';
104
105 $email = sanitize_email( $_POST['email'] );
106 $message = sanitize_textarea_field( $_POST['message'] );
107 $report_text = sanitize_textarea_field( $_POST['report_text'] );
108
109 $wpdb->insert(
110 $table_report,
111 [
112 'user_id' => get_current_user_id(), // or match from wpbot_user.
113 'message' => $message,
114 'meta_info' => maybe_serialize( [ 'email' => $email, 'report_text' => $report_text ] ),
115 ],
116 [ '%d', '%s', '%s' ]
117 );
118
119 // Send report email to admin
120 $admin_email = get_option( 'admin_email' );
121 $subject = "New Chat Report";
122 $body = "Reported Message:\n{$message}\n\nReport Text:\n{$report_text}\n\nUser Email: {$email}";
123 wp_mail( $admin_email, $subject, $body );
124
125 wp_send_json_success();
126 }
127
128
129
130 }
131