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