PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, AI Services / 7.6.6
WPBot – AI ChatBot for Live Support, Lead Generation, AI Services v7.6.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 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 / functions.php

functions.php in WPBot – AI ChatBot for Live Support, Lead Generation, AI Services 7.6.6, at functions.php

1,830 lines 97.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @param $type
4 * Display wpwBot Icon ball
5 */
6 if (!defined('ABSPATH')) exit; // Exit if accessed directly
7 add_action('wp_footer', 'wp_chatbot_load_footer_html');
8 add_action( 'admin_footer', 'qcld_style_for_hide_iframe');
9 function qcld_style_for_hide_iframe(){
10 ?>
11 <script>
12 jQuery( document ).ready(function() {
13 setInterval(function(){
14 if(document.querySelector('.wp-block-legacy-widget__edit-preview-iframe')){
15 document.querySelector('.wp-block-legacy-widget__edit-preview-iframe').addEventListener("load", ev => {
16 jQuery('.wp-block-legacy-widget__edit-preview-iframe').contents().find('#wp-chatbot-chat-container').hide();
17 });
18
19 }
20 }, 500);
21 });
22 </script>
23 <?php
24 }
25 function wp_chatbot_load_footer_html(){
26 if ( get_option('disable_wp_chatbot') != 1 && wp_chatbot_load_controlling() === true) {
27
28 ?>
29 <style>
30 <?php if(get_option('wp_chatbot_custom_css')!="") {
31
32 echo get_option('wp_chatbot_custom_css');
33 }
34 ?>
35 </style>
36
37 <?php if (get_option('qcld_wb_chatbot_change_bg') == 1) {
38 if (get_option('qcld_wb_chatbot_board_bg_path') != "") {
39 $qcld_wb_chatbot_board_bg_path = get_option('qcld_wb_chatbot_board_bg_path');
40 } else {
41 $qcld_wb_chatbot_board_bg_path = QCLD_wpCHATBOT_IMG_URL . 'background/background.png';
42 }
43 ?>
44 <style>
45 .wp-chatbot-container {
46 background-image: url(<?php echo esc_url($qcld_wb_chatbot_board_bg_path); ?>) !important;
47 }
48 </style>
49 <?php }
50 $wp_chatbot_enable_rtl = "";
51 if (get_option('enable_wp_chatbot_rtl') == '1') {
52 $wp_chatbot_enable_rtl .= "wp-chatbot-rtl";
53 }
54 $wp_chatbot_enable_mobile_screen = "";
55 // if (get_option('enable_wp_chatbot_mobile_full_screen')==1) {
56 $wp_chatbot_enable_mobile_screen .= "wp-chatbot-mobile-full-screen";
57 // }
58 ?>
59 <div id="wp-chatbot-chat-container" class="<?php echo esc_attr($wp_chatbot_enable_rtl .' '.$wp_chatbot_enable_mobile_screen); ?>">
60 <div id="wp-chatbot-integration-container">
61 <div class="wp-chatbot-integration-button-container">
62 <?php if (get_option('enable_wp_chatbot_skype_floating_icon') == 1) { ?>
63 <a href="skype:<?php echo esc_attr(get_option('enable_wp_chatbot_skype_id')); ?>?chat"><span
64 class="inetegration-skype-btn" title="<?php esc_attr_e('Skype', 'chatbot'); ?>"> </span></a>
65 <?php } ?>
66 <?php if (get_option('enable_wp_chatbot_floating_whats') == 1) { ?>
67 <a href="<?php echo esc_url('https://api.whatsapp.com/send?phone=' . get_option('qlcd_wp_chatbot_whats_num')); ?>"
68 target="_blank"><span class="intergration-whats"
69 title="<?php esc_html_e('WhatsApp', 'chatbot'); ?>"></span></a>
70 <?php } ?>
71 <?php if (get_option('enable_wp_chatbot_floating_viber') == 1) { ?>
72 <a href="<?php echo esc_url('https://live.viber.com/#/' . get_option('qlcd_wp_chatbot_viber_acc')); ?>"
73 target="_blank"><span class="intergration-viber"
74 title="<?php esc_html_e('Viber', 'chatbot'); ?>"></span></a>
75 <?php } ?>
76 <?php if (get_option('enable_wp_chatbot_floating_phone') == 1 && get_option('qlcd_wp_chatbot_phone') != "") { ?>
77 <a href="tel:<?php echo esc_attr(get_option('qlcd_wp_chatbot_phone')); ?>"><span
78 class="intergration-phone"
79 title="<?php esc_html_e('Phone', 'chatbot'); ?>"> </span></a>
80 <?php } ?>
81 <?php if (get_option('enable_wp_chatbot_floating_link') == 1 && get_option('qlcd_wp_chatbot_weblink') != "") { ?>
82 <a href="<?php echo esc_url(get_option('qlcd_wp_chatbot_weblink')); ?>" target="_blank"><span
83 class="intergration-weblink" title="<?php esc_html_e('Web Link', 'chatbot'); ?>"></span></a>
84 <?php } ?>
85 </div>
86 </div>
87 <?php
88 //Get wpcommerce cart
89
90 $qcld_wb_chatbot_theme = get_option('qcld_wb_chatbot_theme');
91 if (file_exists(QCLD_wpCHATBOT_PLUGIN_DIR_PATH . '/templates/' . $qcld_wb_chatbot_theme . '/style.css')) {
92 wp_register_style('qcld-wp-chatbot-style', plugins_url(basename(plugin_dir_path(__FILE__)) . '/templates/' . $qcld_wb_chatbot_theme . '/style.css', basename(__FILE__)), '', QCLD_wpCHATBOT_VERSION, 'screen');
93 wp_enqueue_style('qcld-wp-chatbot-style');
94 }
95 if (file_exists(QCLD_wpCHATBOT_PLUGIN_DIR_PATH . '/templates/' . $qcld_wb_chatbot_theme . '/template.php')) {
96 require_once(QCLD_wpCHATBOT_PLUGIN_DIR_PATH . '/templates/' . $qcld_wb_chatbot_theme . '/template.php');
97 } else {
98 echo "<h2>" . esc_html__('No wpWBot Theme Found!', 'chatbot') . "</h2>";
99 }
100 ?>
101 <?php
102 if (get_option('disable_wp_chatbot_notification') != 1) {
103 ?>
104 <div id="wp-chatbot-notification-container" class="wp-chatbot-notification-container">
105 <div class="wp-chatbot-notification-controller">
106 <span class="wp-chatbot-notification-close">
107 <?php esc_html_e('X', 'chatbot'); ?>
108 </span>
109 </div>
110 <?php
111 $testingTip="";
112 if (get_option('wp_chatbot_agent_image') == "custom-agent.png") {
113 $wp_chatbot_custom_agent_path = get_option('wp_chatbot_custom_agent_path');
114 } else if (get_option('wp_chatbot_agent_image') != "custom-agent.png") {
115 $wp_chatbot_custom_agent_path = QCLD_wpCHATBOT_IMG_URL . get_option('wp_chatbot_agent_image');
116 } else {
117 $wp_chatbot_custom_agent_path = QCLD_wpCHATBOT_IMG_URL . 'custom-agent.png';
118 }
119 ?>
120 <div class="wp-chatbot-notification-agent-profile">
121 <div class="wp-chatbot-notification-widget-avatar" ><img
122 src="<?php echo esc_attr($wp_chatbot_custom_agent_path); ?>" alt=""></div>
123 <div class="wp-chatbot-notification-welcome"><?php echo wp_kses_post(wpb_randmom_message_handle(maybe_unserialize(get_option('qlcd_wp_chatbot_welcome')))) . ' <strong>' . esc_html(get_option('qlcd_wp_chatbot_host')) . '</strong>'; ?></div>
124 </div>
125 <?php
126 //update_option('qlcd_wp_chatbot_notifications','Welcome to WpBot');
127 $notifications = qcld_wb_chatbot_func_str_replace(wp_kses_post(maybe_unserialize(get_option('qlcd_wp_chatbot_notifications'))));
128
129 ?>
130
131 <div class="wp-chatbot-notification-message"><?php echo esc_html($notifications[0]); ?></div>
132 </div>
133 <?php } ?>
134 <!--wp-chatbot-board-container-->
135 <div id="wp-chatbot-ball" class="">
136 <div class="wp-chatbot-ball">
137 <div class="wp-chatbot-ball-animator wp-chatbot-ball-animation-switch"></div>
138 <?php
139 if (get_option('wp_chatbot_icon') == "custom.png") {
140 $wp_chatbot_custom_icon_path = (!empty(get_option('wp_chatbot_custom_icon_path'))) ? get_option('wp_chatbot_custom_icon_path') : QCLD_wpCHATBOT_IMG_URL . 'icon-1.png';
141
142 } else if (get_option('wp_chatbot_icon') != "custom.png") {
143 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . get_option('wp_chatbot_icon');
144 } else {
145 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . 'custom.png';
146 }
147 ?>
148 <img src="<?php echo esc_url($wp_chatbot_custom_icon_path); ?>"
149 alt="wpChatIcon" qcld_agent="<?php echo esc_url($wp_chatbot_custom_icon_path); ?>" >
150
151 </div>
152 </div>
153 <?php
154 $fb_app_id = get_option('qlcd_wp_chatbot_fb_app_id');
155 $fb_page_id = get_option('qlcd_wp_chatbot_fb_page_id');
156 $fb_mgs_color = get_option('qlcd_wp_chatbot_fb_color') != '' ? get_option('qlcd_wp_chatbot_fb_color') : '#0084ff';
157 $fb_mgs_in = get_option('qlcd_wp_chatbot_fb_in_msg') != '' ? get_option('qlcd_wp_chatbot_fb_in_msg') : 'You are welcome';
158 $fb_mgs_out = get_option('qlcd_wp_chatbot_fb_out_msg') != '' ? get_option('qlcd_wp_chatbot_fb_out_msg') : 'You are not logged in';
159 if (get_option('enable_wp_chatbot_messenger') == 1 && get_option('enable_wp_chatbot_messenger_floating_icon') == 1) {
160 ?>
161 <!-- wp-chatbot-board-container-->
162 <script>
163
164 window.fbAsyncInit = function () {
165 FB.init({
166 appId: '<?php echo esc_js($fb_app_id); ?>',
167 autoLogAppEvents: true,
168 xfbml: true,
169 version: 'v2.12'
170 });
171 };
172
173 (function (d, s, id) {
174 var js, fjs = d.getElementsByTagName(s)[0];
175 if (d.getElementById(id)) return;
176 js = d.createElement(s);
177 js.id = id;
178 js.src = 'https://connect.facebook.net/en_US/sdk.js';
179 fjs.parentNode.insertBefore(js, fjs);
180 }(document, 'script', 'facebook-jssdk'));
181 </script>
182 <div class="fb-customerchat"
183 page_id="<?php echo esc_attr($fb_page_id); ?>"
184 greeting_dialog_display="hide"
185 theme_color="<?php echo esc_attr($fb_mgs_color); ?>"
186 logged_in_greeting="<?php echo esc_attr($fb_mgs_in); ?>"
187 logged_out_greeting="<?php echo esc_attr($fb_mgs_out); ?>"></div>
188 <?php
189 }
190 ?>
191 <!--container-->
192 <!--wp-chatbot-ball-wrapper-->
193 </div>
194
195 <?php
196
197 if ( get_transient( 'qcld_bot_clear_cache' ) ) {
198 echo '<script type="text/javascript">var wpbot_clear_cache = 1 </script>';
199 delete_transient( 'qcld_bot_clear_cache' );
200 }
201
202 }else{
203 ?>
204 <script>
205 var openingHourIsFn = 1;
206 </script>
207 <?php
208 }
209 }
210 //wp_chatbot load control handler.
211 function wp_chatbot_load_controlling(){
212 $wp_chatbot_load = true;
213
214
215 if (get_option('wp_chatbot_show_pages') == 'off') {
216 $wp_chatbot_select_pages = unserialize(get_option('wp_chatbot_show_pages_list'));
217 if (is_page() && !empty($wp_chatbot_select_pages)) {
218
219 if (in_array(get_the_ID(), $wp_chatbot_select_pages) == true) {
220
221 $wp_chatbot_load = true;
222 } else {
223 $wp_chatbot_load = false;
224 }
225
226 }
227
228 if(function_exists('is_shop')){
229 if (is_shop() || is_cart() || is_checkout() || 'product' == get_post_type()) {
230 $wp_chatbot_load = false;
231 }
232 }
233
234 }
235 if (get_option('wp_chatbot_show_wpcommerce') == 'off') {
236
237 }
238 //load wpwbot shortcode template and prevent default wpwbot from footer.
239 if (is_page()) {
240 $page_id = get_the_ID();
241 $page = get_post($page_id);
242 if (has_shortcode($page->post_content, 'wpwbot')) {
243 $wp_chatbot_load = false;
244 }
245 }
246
247 $post_list = maybe_unserialize(get_option('wp_chatbot_exclude_post_list'));
248
249 if( is_array( $post_list ) && in_array(get_post_type(), $post_list)){
250 $wp_chatbot_load = false;
251 }
252 if (wp_chatbot_is_mobile() && get_option('disable_wp_chatbot_on_mobile') == 1) {
253 $wp_chatbot_load = false;
254 }
255
256 if (get_option('wp_chatbot_show_home_page') == 'off' && is_home()) {
257 $wp_chatbot_load = false;
258 }
259
260 if (get_option('wp_chatbot_show_posts') == 'off' && 'post' == get_post_type()) {
261 $wp_chatbot_load = false;
262 }
263
264 if(is_admin()){
265 $wp_chatbot_load = false;
266 }
267 return $wp_chatbot_load;
268 }
269 //checking Devices
270 function wp_chatbot_is_mobile(){
271 $useragent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
272 if (preg_match('/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i', $useragent) || preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i', substr($useragent, 0, 4))) {
273 return true;
274 } else {
275 return false;
276 }
277 }
278 //Checking wpwbot opening hour
279 function wp_chatbot_check_opening_hours(){
280 $curent_day=strtolower(date('l',strtotime(current_time( 'mysql' ))));
281 $current_time=date('H:i',strtotime(current_time( 'mysql')));
282 $is_wpwbot_open =false;
283 if(get_option('wpwbot_hours')) {
284 $wpwbot_times = wp_kses_post(unserialize(get_option('wpwbot_hours')));
285 if (isset($wpwbot_times[$curent_day])) {
286 $day_times = $wpwbot_times[$curent_day];
287 if (!empty($day_times)) {
288 foreach ($day_times as $day_time) {
289 if(strtotime($current_time) > strtotime($day_time[0]) && strtotime($current_time) < strtotime($day_time[1]) ){
290 $is_wpwbot_open=true;
291 }
292 }
293 }
294 }
295 }
296 return $is_wpwbot_open;
297 }
298
299 /**
300 * wpwBot Search keyword product
301 */
302 add_action('wp_ajax_qcld_wb_chatbot_keyword', 'qcld_wb_chatbot_keyword');
303 add_action('wp_ajax_nopriv_qcld_wb_chatbot_keyword', 'qcld_wb_chatbot_keyword');
304 function qcld_wb_chatbot_keyword(){
305 $keyword = sanitize_text_field($_POST['keyword']);
306 $product_per_page = get_option('qlcd_wp_chatbot_ppp') != '' ? get_option('qlcd_wp_chatbot_ppp') : 10;
307 if (get_option('qlcd_wp_chatbot_search_option') == 'standard') {
308 $product_orderby = sanitize_text_field(get_option('qlcd_wp_chatbot_product_orderby') != '' ? get_option('qlcd_wp_chatbot_product_orderby') : 'title');
309 $product_order = sanitize_text_field(get_option('qlcd_wp_chatbot_product_order') != '' ? get_option('qlcd_wp_chatbot_product_order') : 'ASC');
310 //Merging all query together.
311 $argu_params = array(
312 'post_type' => 'product',
313 'post_status' => 'publish',
314 'posts_per_page' => $product_per_page,
315 'orderby' => $product_orderby,
316 'order' => $product_order,
317 's' => $keyword,
318 );
319 /******
320 *WP Query Operation to get products.*
321 *******/
322 $product_query = new WP_Query($argu_params);
323 $product_num = $product_query->post_count;
324 //Getting total product number by string.
325 $total_argu = array('post_type' => 'product', 's' => $keyword, 'posts_per_page' => 100);
326 $total_query = new WP_Query($total_argu);
327 $total_product_num = $total_query->post_count;
328 $html = '<div class="wp-chatbot-products-area">';
329 $_pf = new WC_Product_Factory();
330 //repeating the products
331 if ($product_num > 0) {
332 $html .= '<ul class="wp-chatbot-products">';
333 while ($product_query->have_posts()) : $product_query->the_post();
334 $product = $_pf->get_product(get_the_ID());
335 if (wp_chatbot_product_controlling(get_the_ID()) == true) {
336 $html .= '<li class="wp-chatbot-product">';
337 $html .= '<a target="_blank" href="' . get_permalink(get_the_ID()) . '" wp-chatbot-pid= "' . get_the_ID() . '" title="' . esc_attr($product->post->post_title ? $product->post->post_title : get_the_ID()) . '">';
338 $html .= get_the_post_thumbnail(get_the_ID(), 'shop_catalog') . '
339 <div class="wp-chatbot-product-summary">
340 <div class="wp-chatbot-product-table">
341 <div class="wp-chatbot-product-table-cell">
342 <h3 class="wp-chatbot-product-title">' . $product->post->post_title . '</h3>
343 <div class="price">' . $product->get_price_html() . '</div>';
344 $html .= ' </div>
345 </div>
346 </div></a>
347 </li>';
348 }
349 endwhile;
350 wp_reset_postdata();
351 $html .= '</ul>';
352 if ($total_product_num > $product_per_page && $product_per_page > 0 ) {
353 $html .= '<p style="text-align: center"><button type="button" id="wp-chatbot-loadmore" data-offset="' . $product_per_page . '" data-search-type="product" data-search-term="' . $keyword . '" >' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_load_more')))) . ' <span id="wp-chatbot-loadmore-loader"></span></button> </p>';
354 }
355 }
356 $html .= '</div>';
357 } else if (get_option('qlcd_wp_chatbot_search_option') == 'advanced') {
358 $result = wpwBot_Search::factory()->search($keyword);
359 $products = $result['products'];
360 $product_num = count($result['products']);
361 $total_product_num = $result['total_products'];
362 $more_product_ids = implode(",", $result['more_ids']);
363 $html = '<div class="wp-chatbot-products-area">';
364 $_pf = new WC_Product_Factory();
365 //repeating the products
366 if ($product_num > 0) {
367 $html .= '<ul class="wp-chatbot-products">';
368 foreach ($products as $product) {
369 if (wp_chatbot_product_controlling($product->get_id()) == true) {
370 $html .= '<li class="wp-chatbot-product">';
371 $html .= '<a target="_blank" href="' . get_permalink($product->get_id()) . '" wp-chatbot-pid= "' . $product->get_id() . '" title="' . esc_attr($product->get_title()) . '">';
372 $html .= get_the_post_thumbnail($product->get_id(), 'shop_catalog') . '
373 <div class="wp-chatbot-product-summary">
374 <div class="wp-chatbot-product-table">
375 <div class="wp-chatbot-product-table-cell">
376 <h3 class="wp-chatbot-product-title">' . $product->get_title() . '</h3>
377 <div class="price">' . $product->get_price_html() . '</div>';
378 $html .= ' </div></div></div></a></li>';
379 }
380 }
381 $html .= '</ul>';
382 if ($total_product_num > $product_per_page && $product_per_page > 0) {
383 $html .= '<p style="text-align: center"><button type="button" id="wp-chatbot-loadmore" data-offset="' . $product_per_page . '" data-search-type="product" data-search-term="' . $more_product_ids . '" >' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_load_more')))) . ' <span id="wp-chatbot-loadmore-loader"></span></button> </p>';
384 }
385 }
386 $html .= '</div>';
387 }
388 $response = array('html' => $html, 'product_num' => $total_product_num, 'per_page' => $product_per_page);
389 wp_send_json($response);
390
391 }
392 /**
393 * wpwBot Categories
394 */
395 add_action('wp_ajax_qcld_wb_chatbot_category', 'qcld_wb_chatbot_category');
396 add_action('wp_ajax_nopriv_qcld_wb_chatbot_category', 'qcld_wb_chatbot_category');
397 function qcld_wb_chatbot_category(){
398 $category_type="common";
399 if (get_option('wp_chatbot_show_parent_category') != "") {
400 $terms = get_terms('product_cat', array('parent' => 0, 'hide_empty' => true, 'fields' => 'all'));
401
402 } else {
403 $terms = get_terms('product_cat', array('hide_empty' => true, 'fields' => 'all'));
404 }
405 $html = "";
406 foreach ($terms as $term) {
407 $child_terms=get_terms('product_cat', array('parent' => $term->term_id, 'hide_empty' => true, 'fields' => 'all'));
408 if(get_option('wp_chatbot_show_sub_category')==1 && count($child_terms) >0){
409 $category_type="hasChilds";
410 }
411 $html .= '<span class="qcld-chatbot-product-category" data-category-type="' . $category_type . '" data-category-slug="' . $term->slug . '" data-category-id="' . $term->term_id . '">' . $term->name . '</span>';
412 }
413 wp_send_json($html);
414
415 }
416 /**
417 * wpwBot Sub categories
418 */
419 add_action('wp_ajax_qcld_wb_chatbot_sub_category', 'qcld_wb_chatbot_sub_category');
420 add_action('wp_ajax_nopriv_qcld_wb_chatbot_sub_category', 'qcld_wb_chatbot_sub_category');
421 function qcld_wb_chatbot_sub_category(){
422 $parent_id = stripslashes($_POST['parent_id']);
423 $terms = get_terms('product_cat', array('parent' => $parent_id, 'hide_empty' => true, 'fields' => 'all'));
424 $html = "";
425 foreach ($terms as $term) {
426 $html .= '<span class="qcld-chatbot-product-category" data-category-type="common" data-category-slug="' . $term->slug . '" data-category-id="' . $term->term_id . '">' . $term->name . '</span>';
427 }
428 wp_send_json($html);
429
430 }
431 /**
432 * wpwBot category product
433 */
434 add_action('wp_ajax_qcld_wb_chatbot_category_products', 'qcld_wb_chatbot_category_products');
435 add_action('wp_ajax_nopriv_qcld_wb_chatbot_category_products', 'qcld_wb_chatbot_category_products');
436 function qcld_wb_chatbot_category_products(){
437 $category_id = stripslashes($_POST['category']);
438 $product_per_page = sanitize_text_field(get_option('qlcd_wp_chatbot_ppp') != '' ? get_option('qlcd_wp_chatbot_ppp') : 10);
439 $product_orderby = sanitize_text_field(get_option('qlcd_wp_chatbot_product_orderby') != '' ? get_option('qlcd_wp_chatbot_product_orderby') : 'title');
440 $product_order = sanitize_text_field(get_option('qlcd_wp_chatbot_product_order') != '' ? get_option('qlcd_wp_chatbot_product_order') : 'ASC');
441 //Merging all query together.
442 $argu_params = array(
443 'post_type' => 'product',
444 'post_status' => 'publish',
445 'ignore_sticky_posts' => 1,
446 'orderby' => $product_orderby,
447 'order' => $product_order,
448 'posts_per_page' => $product_per_page,
449 'tax_query' => array(
450 array(
451 'taxonomy' => 'product_cat',
452 'field' => 'term_id',
453 'terms' => $category_id,
454 'operator' => 'IN'
455 )
456 )
457 );
458 /******
459 *WP Query Operation to get products.*
460 *******/
461 $product_query = new WP_Query($argu_params);
462 $product_num = $product_query->post_count;
463 //Getting total product number by string.
464 $total_argu = array(
465 'post_type' => 'product',
466 'post_status' => 'publish',
467 'posts_per_page' => 100,
468 'tax_query' => array(
469 array(
470 'taxonomy' => 'product_cat',
471 'field' => 'term_id',
472 'terms' => $category_id,
473 'operator' => 'IN'
474 )
475 )
476 );
477 $total_query = new WP_Query($total_argu);
478 $total_product_num = $total_query->post_count;
479 $_pf = new WC_Product_Factory();
480 //repeating the products
481 $html = '';
482 if ($product_num > 0) {
483 $html .= '<div class="wp-chatbot-products-area">';
484 $html .= '<ul class="wp-chatbot-products">';
485 while ($product_query->have_posts()) : $product_query->the_post();
486 $product = $_pf->get_product(get_the_ID());
487 //$qcld_thumb = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'shop_thumbnail' );
488 $html .= '<li class="wp-chatbot-product">';
489 $html .= '<a class="wp-chatbot-product-url" wp-chatbot-pid= "' . get_the_ID() . '" target="_blank" href="' . get_permalink(get_the_ID()) . '" title="' . esc_attr($product->get_title() ? $product->get_title() : get_the_ID()) . '">';
490 $html .= get_the_post_thumbnail(get_the_ID(), 'shop_catalog') . '
491 <div class="wp-chatbot-product-summary">
492 <div class="wp-chatbot-product-table">
493 <div class="wp-chatbot-product-table-cell">
494 <h3 class="wp-chatbot-product-title">' . $product->get_title() . '</h3>
495 <div class="price">' . $product->get_price_html() . '</div>';
496 $html .= ' </div>
497 </div>
498 </div></a>
499 </li>';
500 endwhile;
501 wp_reset_postdata();
502 $html .= '</ul>';
503 if ($total_product_num > $product_per_page && $product_per_page >0) {
504 $html .= '<p style="text-align: center"><button type="button" id="wp-chatbot-loadmore" data-offset="' . $product_per_page . '" data-search-type="category" data-search-term="' . $category_id . '" >' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_load_more')))) . ' <span id="wp-chatbot-loadmore-loader"></span></button> </p>';
505 }
506 $html .= '</div>';
507 } else {
508 $html = '';
509 }
510 $response = array('html' => $html, 'product_num' => $total_product_num, 'per_page' => $product_per_page);
511 wp_send_json($response);
512 }
513 /**
514 * wpwBot latest, featured, recent product
515 */
516 add_action('wp_ajax_qcld_wb_chatbot_featured_products', 'qcld_wb_chatbot_featured_products');
517 add_action('wp_ajax_nopriv_qcld_wb_chatbot_featured_products', 'qcld_wb_chatbot_featured_products');
518 function qcld_wb_chatbot_featured_products(){
519 $product_per_page = sanitize_text_field(get_option('qlcd_wp_chatbot_ppp') != '' ? get_option('qlcd_wp_chatbot_ppp') : 10);
520 $product_orderby = sanitize_text_field(get_option('qlcd_wp_chatbot_product_orderby') != '' ? get_option('qlcd_wp_chatbot_product_orderby') : 'title');
521 $product_order = sanitize_text_field(get_option('qlcd_wp_chatbot_product_order') != '' ? get_option('qlcd_wp_chatbot_product_order') : 'ASC');
522 //get featured products query.
523 $argu_params = array('post_status' => 'publish',
524 'posts_per_page' => $product_per_page,
525 'post_type' => 'product',
526 'post_status' => 'publish',
527 'tax_query' => array(array('taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'featured'))
528 );
529 /******
530 *WP Query Operation to get products.*
531 *******/
532 $product_query = new WP_Query($argu_params);
533 $product_num = $product_query->post_count;
534 //Getting total product number by string.
535 $total_argu = array('post_status' => 'publish',
536 'posts_per_page' => 100,
537 'post_type' => 'product',
538 'post_status' => 'publish',
539 'tax_query' => array(array('taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'featured',),)
540 );
541 $total_query = new WP_Query($total_argu);
542 $total_product_num = $total_query->post_count;
543 $html = '<div class="wp-chatbot-products-area">';
544 $_pf = new WC_Product_Factory();
545 //repeating the products
546 if ($product_num > 0) {
547 //$html .= '<p>sdf sdfdsf : '.$asdfdf.'</p>';
548 $html .= '<ul class="wp-chatbot-products">';
549 while ($product_query->have_posts()) : $product_query->the_post();
550 $product = $_pf->get_product(get_the_ID());
551 $html .= '<li class="wp-chatbot-product">';
552 $html .= '<a class="wp-chatbot-product-url" wp-chatbot-pid= "' . get_the_ID() . '" target="_blank" href="' . get_permalink(get_the_ID()) . '" title="' . esc_attr($product->get_title() ? $product->get_title() : get_the_ID()) . '">';
553 $html .= get_the_post_thumbnail(get_the_ID(), 'shop_catalog') . '
554 <div class="wp-chatbot-product-summary">
555 <div class="wp-chatbot-product-table">
556 <div class="wp-chatbot-product-table-cell">
557 <h3 class="wp-chatbot-product-title">' . $product->get_title() . '</h3>
558 <div class="price">' . $product->get_price_html() . '</div>';
559 $html .= ' </div>
560 </div>
561 </div></a>
562 </li>';
563 endwhile;
564 wp_reset_postdata();
565 $html .= '</ul>';
566 if ($total_product_num > $product_per_page) {
567 $html .= '<p style="text-align: center"><button type="button" id="wp-chatbot-loadmore" data-offset="' . $product_per_page . '" data-search-type="product" data-search-term="featured" >' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_load_more')))) . ' <span id="wp-chatbot-loadmore-loader"></span></button> </p>';
568 }
569 }
570 $html .= '</div>';
571 $response = array('html' => $html, 'product_num' => $total_product_num, 'per_page' => $product_per_page);
572 wp_send_json($response);
573 }
574 //Product display controll
575 function wp_chatbot_product_controlling($product_id){
576 $display_product = true;
577 //Controlling Out of Stock product display from back end.
578 $_pf = new WC_Product_Factory();
579 $product = $_pf->get_product($product_id);
580 if ($product->manage_stock == 'yes' && $product->stock_quantity <= 0 && get_option('wp_chatbot_exclude_stock_out_product') == 1) {
581 $display_product = false;
582 }
583 return $display_product;
584 }
585 //Get Sale products
586 add_action('wp_ajax_qcld_wb_chatbot_sale_products', 'qcld_wb_chatbot_sale_products');
587 add_action('wp_ajax_nopriv_qcld_wb_chatbot_sale_products', 'qcld_wb_chatbot_sale_products');
588 function qcld_wb_chatbot_sale_products(){
589 $product_per_page = sanitize_text_field(get_option('qlcd_wp_chatbot_ppp') != '' ? get_option('qlcd_wp_chatbot_ppp') : 10);
590 $product_orderby = sanitize_text_field(get_option('qlcd_wp_chatbot_product_orderby') != '' ? get_option('qlcd_wp_chatbot_product_orderby') : 'title');
591 $product_order = sanitize_text_field(get_option('qlcd_wp_chatbot_product_order') != '' ? get_option('qlcd_wp_chatbot_product_order') : 'ASC');
592 //get sale products query.
593 $argu_params = array(
594 'post_type' => 'product',
595 'post_status' => 'publish',
596 'posts_per_page' => $product_per_page,
597 'meta_query' => array(
598 'relation' => 'OR',
599 array( // Simple products type
600 'key' => '_sale_price',
601 'value' => 0,
602 'compare' => '>',
603 'type' => 'numeric'
604 ),
605 array( // Variable products type
606 'key' => '_min_variation_sale_price',
607 'value' => 0,
608 'compare' => '>',
609 'type' => 'numeric'
610 )
611 )
612 );
613 /******
614 *WP Query Operation to get products.*
615 *******/
616 $product_query = new WP_Query($argu_params);
617 $product_num = $product_query->post_count;
618 //Getting total product number by string.
619 $total_argu = array(
620 'post_type' => 'product',
621 'post_status' => 'publish',
622 'posts_per_page' => 100,
623 'meta_query' => array(
624 'relation' => 'OR',
625 array( // Simple products type
626 'key' => '_sale_price',
627 'value' => 0,
628 'compare' => '>',
629 'type' => 'numeric'
630 ),
631 array( // Variable products type
632 'key' => '_min_variation_sale_price',
633 'value' => 0,
634 'compare' => '>',
635 'type' => 'numeric'
636 )
637 )
638 );
639 $total_query = new WP_Query($total_argu);
640 $total_product_num = $total_query->post_count;
641 $html = '<div class="wp-chatbot-products-area">';
642 $_pf = new WC_Product_Factory();
643 //repeating the products
644 if ($product_num > 0) {
645 //$html .= '<p>sdf sdfdsf : '.$asdfdf.'</p>';
646 $html .= '<ul class="wp-chatbot-products">';
647 while ($product_query->have_posts()) : $product_query->the_post();
648 $product = $_pf->get_product(get_the_ID());
649 $html .= '<li class="wp-chatbot-product">';
650 $html .= '<a class="wp-chatbot-product-url" wp-chatbot-pid= "' . get_the_ID() . '" target="_blank" href="' . get_permalink(get_the_ID()) . '" title="' . esc_attr($product->get_title() ? $product->get_title() : get_the_ID()) . '">';
651 $html .= get_the_post_thumbnail(get_the_ID(), 'shop_catalog') . '
652 <div class="wp-chatbot-product-summary">
653 <div class="wp-chatbot-product-table">
654 <div class="wp-chatbot-product-table-cell">
655 <h3 class="wp-chatbot-product-title">' . $product->get_title() . '</h3>
656 <div class="price">' . $product->get_price_html() . '</div>';
657 $html .= ' </div>
658 </div>
659 </div></a>
660 </li>';
661 endwhile;
662 wp_reset_postdata();
663 $html .= '</ul>';
664 if ($total_product_num > $product_per_page) {
665 $html .= '<p style="text-align: center"><button type="button" id="wp-chatbot-loadmore" data-offset="' . $product_per_page . '" data-search-type="product" data-search-term="sale" >' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_load_more')))) . ' <span id="wp-chatbot-loadmore-loader"></span></button> </p>';
666 }
667 }
668 $html .= '</div>';
669 $response = array('html' => $html, 'product_num' => $total_product_num, 'per_page' => $product_per_page);
670 wp_send_json($response);
671 }
672 //load more
673 add_action('wp_ajax_qcld_wb_chatbot_load_more', 'qcld_wb_chatbot_load_more');
674 add_action('wp_ajax_nopriv_qcld_wb_chatbot_load_more', 'qcld_wb_chatbot_load_more');
675 function qcld_wb_chatbot_load_more(){
676 $offset = stripslashes($_POST['offset']);
677 $search_type = stripslashes($_POST['search_type']);
678 $search_term = stripslashes($_POST['search_term']);
679 $product_per_page = sanitize_text_field(get_option('qlcd_wp_chatbot_ppp') != '' ? get_option('qlcd_wp_chatbot_ppp') : 10);
680 $product_orderby = sanitize_text_field(get_option('qlcd_wp_chatbot_product_orderby') != '' ? get_option('qlcd_wp_chatbot_product_orderby') : 'title');
681 $product_order = sanitize_text_field(get_option('qlcd_wp_chatbot_product_order') != '' ? get_option('qlcd_wp_chatbot_product_order') : 'ASC');
682 $next_offset = intval($product_per_page + $offset);
683 if ($search_type == 'product' && $search_term != 'featured' && $search_term != 'sale' && get_option('qlcd_wp_chatbot_search_option') == 'advanced') {
684 //if have multiple ids then explode else have single need to array push
685 if (strpos($search_term, ',') !== false) {
686 $product_ids = explode(',', $search_term);
687 } else {
688 $product_ids = array($search_term);
689 }
690 $result = wpwBot_Search::factory()->get_load_more_products($product_ids);
691 $products = $result['products'];
692 $product_num = count($result['products']);
693 $total_product_num = $result['total_products'];
694 $more_product_ids = implode(",", $result['more_ids']);
695 $_pf = new WC_Product_Factory();
696 //repeating the products
697 $html = '';
698 if ($product_num > 0) {
699 foreach ($products as $product) {
700 $html .= '<li class="wp-chatbot-product">';
701 $html .= '<a target="_blank" href="' . get_permalink($product->get_id()) . '" wp-chatbot-pid= "' . $product->get_id() . '" title="' . esc_attr($product->get_title()) . '">';
702 $html .= get_the_post_thumbnail($product->get_id(), 'shop_catalog') . '
703 <div class="wp-chatbot-product-summary">
704 <div class="wp-chatbot-product-table">
705 <div class="wp-chatbot-product-table-cell">
706 <h3 class="wp-chatbot-product-title">' . $product->get_title() . '</h3>
707 <div class="price">' . $product->get_price_html() . '</div>';
708 $html .= ' </div></div></div></a></li>';
709 }
710 }
711 $response = array('html' => $html, 'product_num' => $total_product_num, 'search_term' => $more_product_ids, 'offset' => $next_offset, 'per_page' => $product_per_page);
712 } else {
713 if ($search_type == 'product' && $search_term != 'featured' && $search_term != 'sale') { //For Standard search
714 $argu_params = array(
715 'post_type' => 'product',
716 'post_status' => 'publish',
717 'posts_per_page' => $product_per_page,
718 'offset' => $offset,
719 'orderby' => $product_orderby,
720 'order' => $product_order,
721 's' => $search_term,
722 );
723 } else if ($search_type == 'category') {
724 $argu_params = array(
725 'post_type' => 'product',
726 'post_status' => 'publish',
727 'ignore_sticky_posts' => 1,
728 'posts_per_page' => $product_per_page,
729 'orderby' => $product_orderby,
730 'order' => $product_order,
731 'offset' => $offset,
732 'tax_query' => array(
733 array(
734 'taxonomy' => 'product_cat',
735 'field' => 'term_id',
736 'terms' => $search_term,
737 'operator' => 'IN'
738 )
739 )
740 );
741 } else if ($search_type == 'product' && $search_term == 'featured') {
742 $argu_params = array(
743 'post_type' => 'product',
744 'post_status' => 'publish',
745 'ignore_sticky_posts' => 1,
746 'posts_per_page' => $product_per_page,
747 'orderby' => $product_orderby,
748 'order' => $product_order,
749 'offset' => $offset,
750 'meta_query' => array('key' => '_featured', 'value' => 'yes')
751 );
752 } else if ($search_type == 'product' && $search_term == 'sale') {
753 $argu_params = array(
754 'post_type' => 'product',
755 'post_status' => 'publish',
756 'ignore_sticky_posts' => 1,
757 'posts_per_page' => $product_per_page,
758 'orderby' => $product_orderby,
759 'order' => $product_order,
760 'offset' => $offset,
761 'meta_query' => array(
762 'relation' => 'OR',
763 array( // Simple products type
764 'key' => '_sale_price',
765 'value' => 0,
766 'compare' => '>',
767 'type' => 'numeric'
768 ),
769 array( // Variable products type
770 'key' => '_min_variation_sale_price',
771 'value' => 0,
772 'compare' => '>',
773 'type' => 'numeric'
774 )
775 )
776 );
777 }
778 $product_query = new WP_Query($argu_params);
779 $product_num = $product_query->post_count;
780 $_pf = new WC_Product_Factory();
781 //repeating the products
782 $html = '';
783 if ($product_num > 0) {
784 while ($product_query->have_posts()) : $product_query->the_post();
785 $product = $_pf->get_product(get_the_ID());
786 $html .= '<li class="wp-chatbot-product">';
787 $html .= '<a target="_blank" href="' . get_permalink(get_the_ID()) . '" wp-chatbot-pid= "' . get_the_ID() . '" title="' . esc_attr($product->post->post_title ? $product->post->post_title : get_the_ID()) . '">';
788 $html .= get_the_post_thumbnail(get_the_ID(), 'shop_catalog') . '
789 <div class="wp-chatbot-product-summary">
790 <div class="wp-chatbot-product-table">
791 <div class="wp-chatbot-product-table-cell">
792 <h3 class="wp-chatbot-product-title">' . $product->post->post_title . '</h3>
793 <div class="price">' . $product->get_price_html() . '</div>';
794 $html .= ' </div>
795 </div>
796 </div></a>
797 </li>';
798 endwhile;
799 wp_reset_postdata();
800 } else {
801 $html .= '';
802 }
803 $response = array('html' => $html, 'product_num' => $product_num, 'search_term' => $search_term, 'offset' => $next_offset, 'per_page' => $product_per_page);
804 }
805 wp_send_json($response);
806 }
807 //product details
808 add_action('wp_ajax_qcld_wb_chatbot_product_details', 'qcld_wb_chatbot_product_details');
809 add_action('wp_ajax_nopriv_qcld_wb_chatbot_product_details', 'qcld_wb_chatbot_product_details');
810 function qcld_wb_chatbot_product_details(){
811 $product_id = stripslashes($_POST['wp_chatbot_pid']);
812 //Tracking product view from chat board
813 wp_chatbot_view_track_product_by_id($product_id);
814 //wpcommerce product factory
815 $wc_pf = new WC_Product_Factory();
816 $product = $wc_pf->get_product($product_id);
817 $product_type = $wc_pf->get_product_type($product_id);
818 $product_title = '<h2 id="wp-chatbot-product-title" ><a target="_blank" href="' . get_permalink($product->get_id()) . '">' . $product->get_title() . '</a></h2>';
819 $product_desc = apply_filters('the_excerpt', $product->post->post_excerpt);//$product->post->post_excerpt;
820 $gallery_ids = $product->get_gallery_image_ids();
821 //images processing..
822 $product_feature_image_id = get_post_thumbnail_id($product_id);
823 $product_feature_image = wp_get_attachment_image_src($product_feature_image_id, 'full');
824 $product_feature_thumb = wp_get_attachment_image_src($product_feature_image_id, 'shop_thumbnail');
825 //$full_img_src = $product_feature_image[0];
826 //$product_image = '<img style="width:500px;height:300px" src="' . $full_img_src . '" >';
827 $product_image = '<div class="wp-chatbot-product-image-large">
828 <a href="' . $product_feature_image[0] . '" id="wp-chatbot-product-image-large-path"><img id="wp-chatbot-product-image-large-src" src="' . $product_feature_image[0] . '" alt="Large Image" title="Zoom Image" /></a>
829 </div>';
830 $product_image .= '<div class="wp-chatbot-product-image-thumbs"><ul>
831 <li class="wp-chatbot-product-active-image-thumbs"><a href="' . $product_feature_image[0] . '" class="wp-chatbot-product-image-thumbs-path"><img class="wp-chatbot-product-image-thumbs-src" src="' . $product_feature_thumb[0] . '" alt="Thumb Image" /></a></li>';
832 if (!empty($gallery_ids)) {
833 foreach ($gallery_ids as $gallery_id) {
834 $gallery_image = wp_get_attachment_image_src($gallery_id, 'full');
835 $gallery_thumb = wp_get_attachment_image_src($gallery_id, 'shop_thumbnail');
836 $product_image .= '<li><a href="' . $gallery_image[0] . '" class="wp-chatbot-product-image-thumbs-path"><img class="wp-chatbot-product-image-thumbs-src" src="' . $gallery_thumb[0] . '" alt="Thumb Image" /></a></li>';
837 }
838 }
839 $product_image .= '</ul></div>';
840 $product_price = '<p class="wp-chatbot-product-price" id="wp-chatbot-product-price">' . $product->get_price_html() . '</p>';
841 $product_sku = '<p class="wp-chatbot-product-sku"> ' . __('SKU', 'chatbot') . ' : ' . $product->get_sku() . '</p>';
842 //if ( $product->is_in_stock() || $product->is_purchasable() )
843 //Handle variable product start
844 $variations = "";
845 $add_cart_button = "";
846 $product_quantity = "";
847 if ($product->is_in_stock()) {
848 if ($product_type == "variable") {
849 //Getting product variation number based details
850 $variations_data = array();
851 foreach ($product->get_children() as $child_id) {
852 $all_cfs = get_post_custom($child_id);
853 array_push($variations_data, array('variation_id' => $child_id, 'variation_data' => $all_cfs));
854 }
855 $variations_data = json_encode($variations_data);
856 $attributes = $product->get_attributes();
857 //Handling Variant & Non Variant products
858 $var_attrs = $product->get_variation_attributes();
859 $varation_names = array();
860 if (!empty($var_attrs)) {
861 foreach ($var_attrs as $key => $value) {
862 array_push($varation_names, $key);
863 }
864 }
865 $debug = $varation_names;
866 foreach ($attributes as $attribute) {
867 /*if (empty($attribute['is_visible']) || ($attribute['is_taxonomy'] && !taxonomy_exists($attribute['name']))) {
868 continue;
869 }*/
870 $title = wc_attribute_label($attribute['name']);
871 $name = $attribute['name'];
872 if ($attribute['is_taxonomy']) {
873 $values = wc_get_product_terms($product->get_id(), $attribute['name'], array('fields' => 'slugs'));
874 } else {
875 $values = array_map('trim', explode(WC_DELIMITER, $attribute['value']));
876 }
877 natsort($values);
878 if (!in_array($name, $varation_names)) {
879 $variations .= '<p><label for="' . sanitize_title($name) . '">' . $title . '</label> ' . ucfirst(implode(",", $values)) . '</p>';
880 } else {
881 $variations .= '<div class="wp-chatbot-variable-' . sanitize_title($name) . '">';
882 $variations .= '<label for="' . sanitize_title($name) . '">' . $title . '</label>';
883 $variations .= '<select id="' . esc_attr(sanitize_title($name)) . '" name="attribute_' . sanitize_title($name) . '" data-attribute_name="attribute_' . sanitize_title($name) . '" class="each_attribute">';
884 $variations .= '<option value="">' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_choose_option')))) . '</option>';
885 foreach ($values as $value) {
886 if (isset($_REQUEST['attribute_' . sanitize_title($name)])) {
887 $selected_value = $_REQUEST['attribute_' . sanitize_title($name)];
888 } else {
889 $selected_value = '';
890 }
891 $variations .= '<option value="' . esc_attr(strtolower($value)) . '"' . selected($selected_value, $value, false) . '>' . apply_filters('wpcommerce_variation_option_name', $value) . '</option>';
892 }
893 $variations .= '</select></div>';
894 }
895 }
896 $add_cart_button .= '<input type="button" id="wp-chatbot-variation-add-to-cart" wp-chatbot-product-id="' . $product_id . '" value="' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_add_to_cart')))) . '" variation_id="" />';
897 $add_cart_button .= "<input type='hidden' id='wp-chatbot-variation-data' data-product-variation='" . $variations_data . "' />";
898 } else {
899 $add_cart_button .= '<input type="button" id="wp-chatbot-add-cart-button" wp-chatbot-product-id="' . $product_id . '" value="' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_add_to_cart')))) . '" />';
900 }
901 //Handle variable product end.
902 $product_quantity .= '<label for="">' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_quantity')))) . '</label>
903 <input type="number" id="vPQuantity" value="1" />';
904 }
905 //$response=$full_size_image;
906 $response = array('title' => $product_title, 'description' => $product_desc, 'image' => $product_image, 'price' => $product_price, 'sku' => $product_sku, 'quantity' => $product_quantity, 'buttton' => $add_cart_button, 'variation' => $variations, 'type' => $product_type, 'debug' => $debug);
907 wp_send_json($response);
908 }
909 //Add to cart for variable product.
910 add_action('wp_ajax_qcld_variable_add_to_cart', 'qcld_variable_add_to_cart');
911 add_action('wp_ajax_nopriv_qcld_variable_add_to_cart', 'qcld_variable_add_to_cart');
912 function qcld_variable_add_to_cart(){
913 $product_id = stripslashes($_POST['p_id']);
914 $quantity = stripslashes($_POST['quantity']);
915 $variations_id = stripslashes($_POST['variations_id']);
916 $attrs = stripslashes($_POST['attributes']);
917 //echo wp_send_json(array('p_id'=>$product_id,'qnty'=>$quantity,'id'=>$variations_id,'att'=>$attrs));
918 $attributes = array();
919 foreach ($attrs as $attr) {
920 $single = explode("#", $attr);
921 if (isset($single[0])) {
922 $a_name = explode("_", $single[0]);
923 }
924 $attributes[$a_name[2]] = $single[1];
925 }
926 global $wpcommerce;
927 $result = $wpcommerce->cart->add_to_cart($product_id, $quantity, $variations_id, $attributes, null);
928 if ($result != false) {
929 wp_send_json('variable');
930 } else {
931 wp_send_json('error');
932 }
933 }
934 //Add to cart for simple product.
935 add_action('wp_ajax_qcld_wb_chatbot_add_to_cart', 'qcld_wb_chatbot_add_to_cart');
936 add_action('wp_ajax_nopriv_qcld_wb_chatbot_add_to_cart', 'qcld_wb_chatbot_add_to_cart');
937 function qcld_wb_chatbot_add_to_cart(){
938 $product_id = stripslashes($_POST['product_id']);
939 $product_quantity = stripslashes($_POST['quantity']);
940 global $wpcommerce;
941 $result = $wpcommerce->cart->add_to_cart($product_id, $product_quantity);
942 if ($result != false) {
943 wp_send_json('simple');
944 } else {
945 wp_send_json('error');
946 }
947 }
948 //Support part
949 add_action('wp_ajax_qcld_wb_chatbot_support_email', 'qcld_wb_chatbot_support_email');
950 add_action('wp_ajax_nopriv_qcld_wb_chatbot_support_email', 'qcld_wb_chatbot_support_email');
951 function qcld_wb_chatbot_support_email(){
952 $name = trim(sanitize_text_field($_POST['name']));
953 $email = sanitize_email($_POST['email']);
954 $message = sanitize_text_field($_POST['message']);
955 $subject = sanitize_text_field(get_option('qlcd_wp_chatbot_email_sub') != '' ? get_option('qlcd_wp_chatbot_email_sub') : 'Support Email from wpWBot by Client');
956 //Extract Domain
957 $url = get_site_url();
958 $url = parse_url($url);
959 $domain = $url['host'];
960 //$admin_email = "admin@" . $domain;
961 $admin_email = sanitize_email(get_option('admin_email'));
962 $toEmail = sanitize_email(get_option('qlcd_wp_chatbot_admin_email') != '' ? get_option('qlcd_wp_chatbot_admin_email') : $admin_email);
963
964
965 if(get_option('qlcd_wp_chatbot_from_email') && get_option('qlcd_wp_chatbot_from_email')!=''){
966 $fromEmail = get_option('qlcd_wp_chatbot_from_email');
967 }else{
968 $fromEmail = "wordpress@" . $domain;
969 }
970
971 //Starting messaging and status.
972 $response['status'] = 'fail';
973 $response['message'] = str_replace('\\', '',wp_kses_post(get_option('qlcd_wp_chatbot_email_fail')));
974 if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
975 $response['message'] = wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_invalid_email'))));
976 $response['status'] = 'fail';
977 } else {
978 //build email body
979 $bodyContent = "";
980 $bodyContent .= '<p><strong>' . __('Support Request Details', 'chatbot') . ':</strong></p><hr>';
981 $bodyContent .= '<p>' . __('Name', 'chatbot') . ' : ' . $name . '</p>';
982 $bodyContent .= '<p>' . __('Email', 'chatbot') . ' : ' . $email . '</p>';
983 $bodyContent .= '<p>' . __('Subject', 'chatbot') . ' : ' . $subject . '</p>';
984 $bodyContent .= '<p>' . __('Message', 'chatbot') . ' : ' . $message . '</p>';
985 $bodyContent .= '<p>' . __('Mail Generated on', 'chatbot') . ': ' . current_time('F j, Y, g:i a') . '</p>';
986 $to = $toEmail;
987 $body = $bodyContent;
988 $headers = array();
989 $headers[] = 'Content-Type: text/html; charset=UTF-8';
990 $headers[] = 'From: ' . $name . ' <' . $fromEmail . '>';
991 $headers[] = 'Reply-To: ' . $name . ' <' . $email . '>';
992 $result = wp_mail($to, $subject, $body, $headers);
993 if ($result) {
994 $response['status'] = 'success';
995 $response['message'] = str_replace('\\', '',wp_kses_post(get_option('qlcd_wp_chatbot_email_sent')));
996 }
997
998 }
999 echo wp_json_encode($response);
1000 die();
1001 }
1002 //Support Phone
1003 add_action('wp_ajax_qcld_wb_chatbot_support_phone', 'qcld_wb_chatbot_support_phone');
1004 add_action('wp_ajax_nopriv_qcld_wb_chatbot_support_phone', 'qcld_wb_chatbot_support_phone');
1005 function qcld_wb_chatbot_support_phone(){
1006 $name = trim(sanitize_text_field($_POST['name']));
1007 $phone =sanitize_text_field($_POST['phone']);
1008 $subject = 'WPBot Support Mail Request for Call Back';
1009 //Extract Domain
1010 $url = get_site_url();
1011 $url = parse_url($url);
1012 $domain = $url['host'];
1013 //$admin_email = "admin@" . $domain;
1014 $admin_email = get_option('admin_email');
1015 $toEmail = sanitize_email(get_option('qlcd_wp_chatbot_admin_email') != '' ? get_option('qlcd_wp_chatbot_admin_email') : $admin_email);
1016
1017
1018 if(get_option('qlcd_wp_chatbot_from_email') && get_option('qlcd_wp_chatbot_from_email')!=''){
1019 $fromEmail = sanitize_email(get_option('qlcd_wp_chatbot_from_email'));
1020 }else{
1021 $fromEmail = "wordpress@" . $domain;
1022 }
1023 //Starting messaging and status.
1024 $response['status'] = 'fail';
1025 $response['message'] = str_replace('\\', '',wp_kses_post(get_option('qlcd_wp_chatbot_phone_fail')));
1026 //build email body
1027 $bodyContent = "";
1028 $bodyContent .= '<p><strong>' . __('Support Request Details', 'chatbot') . ':</strong></p><hr>';
1029 $bodyContent .= '<p>' . __('Name', 'chatbot') . ' : ' . $name . '</p>';
1030 $bodyContent .= '<p>' . __('Phone', 'chatbot') . ' : ' . $phone . '</p>';
1031 $bodyContent .= '<p>' . __('Subject', 'chatbot') . ' : ' . $subject . '</p>';
1032 $bodyContent .= '<p>' . __('Message', 'chatbot') . ' : ' . __(' Call me at ', 'chatbot'). $phone . '</p>';
1033 $bodyContent .= '<p>' . __('Mail Generated on', 'chatbot') . ': ' . current_time('F j, Y, g:i a') . '</p>';
1034 $to = $toEmail;
1035 $body = $bodyContent;
1036 $headers = array();
1037 $headers[] = 'Content-Type: text/html; charset=UTF-8';
1038 $headers[] = 'From: ' . $name . ' <' . $fromEmail . '>';
1039 $headers[] = 'Reply-To: ' . $name . ' <' . $fromEmail . '>';
1040 $result = wp_mail($to, $subject, $body, $headers);
1041 if ($result) {
1042 $response['status'] = 'success';
1043 $response['message'] = str_replace('\\', '',wp_kses_post(get_option('qlcd_wp_chatbot_phone_sent')));
1044 }
1045 echo wp_json_encode($response);
1046 die();
1047 }
1048 // Order Status part. removed
1049
1050 function wpb_randmom_message_handle($items){
1051 return $items[rand(0, count($items) - 1)];
1052 }
1053 function qcld_wb_chatbot_func_str_replace($messages = array()){
1054 $refined_mesgses = array();
1055 foreach ($messages as $message) {
1056 $refined_msg = str_replace('\\', '', $message);
1057 array_push($refined_mesgses, $refined_msg);
1058 }
1059 return $refined_mesgses;
1060 }
1061 add_action('wp_ajax_qcld_wb_chatbot_login_user', 'qcld_wb_chatbot_login_user');
1062 add_action('wp_ajax_nopriv_qcld_wb_chatbot_login_user', 'qcld_wb_chatbot_login_user');
1063 function qcld_wb_chatbot_login_user(){
1064 // First check the nonce, if it fails the function will break
1065 check_ajax_referer('wpwbot-order-nonce', 'security');
1066 // Nonce is checked, get the POST data and sign user on
1067 $info = array();
1068 $info['user_login'] = trim(sanitize_text_field($_POST['user_name']));
1069 $info['user_password'] = trim(sanitize_text_field($_POST['user_pass']));
1070 $info['remember'] = true;
1071 $user_signon = wp_signon($info, false);
1072 $response = array();
1073 if (is_wp_error($user_signon)) {
1074 $response['status'] = "fail";
1075 $response['message'] = wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_order_password_incorrect'))));
1076 } else {
1077 $response = get_order_by_username($info['user_login']);
1078 $response['status'] = "success";
1079 }
1080 wp_send_json($response);
1081 }
1082 add_action('wp_ajax_qcld_wb_chatbot_loged_in_user_orders', 'qcld_wb_chatbot_loged_in_user_orders');
1083 add_action('wp_ajax_nopriv_qcld_wb_chatbot_loged_in_user_orders', 'qcld_wb_chatbot_loged_in_user_orders');
1084 function qcld_wb_chatbot_loged_in_user_orders(){
1085 $current_user = wp_get_current_user();
1086 $user_name = $current_user->user_login;
1087 $response = get_order_by_username($user_name);
1088 wp_send_json($response);
1089 }
1090 function get_order_by_username($user_name){
1091 global $post;
1092 $response = array();
1093 $response['status'] .= "success";
1094 $user = get_user_by('login', $user_name);
1095 // The query arguments
1096 $customer_orders = get_posts(array(
1097 'numberposts' => -1,
1098 'meta_key' => '_customer_user',
1099 'meta_value' => $user->ID,
1100 'post_type' => wc_get_order_types(),
1101 'post_status' => array_keys(wc_get_order_statuses()),
1102 'posts_per_page' => 10,
1103 'orderby' => 'date',
1104 ));
1105 $response['order_num'] = count($customer_orders);
1106 $order_html = '';
1107 if ($response['order_num'] > 0) {
1108 $response['message'] .= wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_order_found'))));
1109 $order_html .= '<div class="wp-chatbot-orders-container">
1110 <div class="wp-chatbot-orders-header">
1111 <div class="order-id">' . __('ID', 'chatbot') . '</div>
1112 <div class="order-date">' . __('Date', 'chatbot') . ' </div>
1113 <div class="order-items">' . __('Items', 'chatbot') . '</div>
1114 <div class="order-status">' . __('Status', 'chatbot') . '</div>
1115 </div>';
1116 foreach ($customer_orders as $order) {
1117 //Formatting order summery
1118 if (isset($_COOKIE['from_app']) && $_COOKIE['from_app'] == 'yes') {
1119 $thanks_page_id = sanitize_text_field(get_option('wp_chatbot_app_order_thankyou'));
1120 $thanks_parmanlink = esc_url(get_permalink($thanks_page_id));
1121 $order_url = '<a href="' . $thanks_parmanlink . '?order_id=' . $order->ID . '" >' . $order->ID . '</a>';
1122 } else {
1123 $order_url = '<a href="' . get_url(get_permalink(get_option('wpcommerce_myaccount_page_id')) . '/view-order/' . $order->ID) . '" target="_blank" >' . $order->ID . '</a>';
1124 }
1125 $order_html .= '<div class="wp-chatbot-orders-single">
1126 <div class="order-id"> ' . $order_url . '</div>
1127 <div class="order-date"> <p>' . date("m/d/Y", strtotime($order->post_date)) . '</p> </div>
1128 <div class="order-items">';
1129 $singleOrder = new WC_Order($order->ID);
1130 $items = $singleOrder->get_items();
1131 foreach ($items as $item) {
1132 $order_html .= '<p>' . $item["name"] . ' X ' . $item["qty"] . '</p>';
1133 }
1134 $order_html .= '</div>
1135 <div class="order-status">' . strtoupper(end(explode("-", $order->post_status))) . '</div>
1136
1137 </div>';
1138 }
1139 $order_html .= '</div>';
1140 } else {
1141 $response['message'] .= get_option('qlcd_wp_chatbot_sorry') . '! <strong>' . $user_name . '</strong>';
1142 $order_html .= '<p>' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_order_not_found')))) . '</p>';
1143 }
1144 $response['html'] = $order_html;
1145 return $response;
1146 }
1147 //Recently viewed products
1148 //keeping id in cookies as
1149 function wp_chatbot_track_product_view(){
1150 if (!is_singular('product')) {
1151 return;
1152 }
1153 global $post;
1154 wp_chatbot_view_track_product_by_id($post->ID);
1155 }
1156 function wp_chatbot_view_track_product_by_id($post_id){
1157 if (empty($_COOKIE['wp_chatbot_wpcommerce_recently_viewed']))
1158 $viewed_products = array();
1159 else
1160 $viewed_products = (array)explode('|', $_COOKIE['wp_chatbot_wpcommerce_recently_viewed']);
1161 if (!in_array($post_id, $viewed_products)) {
1162 $viewed_products[] = $post_id;
1163 }
1164 if (sizeof($viewed_products) > 15) {
1165 array_shift($viewed_products);
1166 }
1167 // Store for session only
1168 if( function_exists( 'wc_setcookie' ) ){
1169 wc_setcookie('wp_chatbot_wpcommerce_recently_viewed', implode('|', $viewed_products));
1170 }
1171 }
1172 add_action('template_redirect', 'wp_chatbot_track_product_view', 20);
1173 //recent view product ajax
1174 add_action('wp_ajax_qcld_wb_chatbot_recently_viewed_products', 'qcld_wb_chatbot_recently_viewed_products');
1175 add_action('wp_ajax_nopriv_qcld_wb_chatbot_recently_viewed_products', 'qcld_wb_chatbot_recently_viewed_products');
1176 function qcld_wb_chatbot_recently_viewed_products(){
1177 // Get wpCommerce Global
1178 $_pf = new WC_Product_Factory();
1179 //show post per page.
1180 $product_per_page = sanitize_text_field(get_option('qlcd_wp_chatbot_ppp') != '' ? get_option('qlcd_wp_chatbot_ppp') : 10);
1181 $wp_chatbot_product_title = wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_latest_product_welcome'))));
1182 // Get recently viewed product cookies data
1183 $viewed_products = !empty($_COOKIE['wp_chatbot_wpcommerce_recently_viewed']) ? (array)explode('|', $_COOKIE['wp_chatbot_wpcommerce_recently_viewed']) : array();
1184 $viewed_products = array_filter(array_map('absint', $viewed_products));
1185 //get featured products if has.
1186 $featured_products = new WP_Query(array('post_status' => 'publish', 'posts_per_page' => $product_per_page, 'post_type' => 'product', 'tax_query' => array(array('taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'featured'))));
1187 //Getting recently vieew products.
1188 if (!empty($viewed_products)) {
1189 $wp_chatbot_product_title = wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_viewed_product_welcome'))));
1190 $product_query = new WP_Query(array(
1191 'posts_per_page' => $product_per_page,
1192 'no_found_rows' => 1,
1193 'post_status' => 'publish',
1194 'post_type' => 'product',
1195 'post__in' => $viewed_products,
1196 ));
1197 //Getting featured products
1198 } else if ($featured_products->post_count > 0) {
1199 $wp_chatbot_product_title = wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_featured_product_welcome'))));
1200 $product_query = $featured_products;
1201 } else {
1202 $wp_chatbot_product_title = wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_latest_product_welcome'))));
1203 //Getting recent products
1204 $product_query = new WP_Query(array('post_status' => 'publish', 'posts_per_page' => $product_per_page, 'post_type' => 'product', 'orderby' => 'date', 'order' => 'DESC'));
1205 }
1206 if (get_option('wp_chatbot_custom_agent_path') != "" && get_option('wp_chatbot_agent_image') == "custom-agent.png") {
1207 $wp_chatbot_custom_icon_path = get_option('wp_chatbot_custom_agent_path');
1208 } else if (get_option('wp_chatbot_custom_agent_path') != "" && get_option('wp_chatbot_agent_image') != "custom-agent.png") {
1209 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . get_option('wp_chatbot_agent_image');
1210 } else {
1211 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . 'custom-agent.png';
1212 }
1213 $html = '<div class="wp-chatbot-agent-profile">
1214 <div class="wp-chatbot-widget-avatar"><img src="' . esc_url($wp_chatbot_custom_icon_path) . '" alt=""></div>
1215 <div class="wp-chatbot-widget-agent">' . esc_html(get_option('qlcd_wp_chatbot_agent')) . '</div>
1216 <div class="wp-chatbot-bubble">' . $wp_chatbot_product_title . '</div>
1217 </div>';
1218 if ($product_query->post_count > 0) {
1219 $html .= '<div class="wp-chatbot-products-area">';
1220 $html .= '<ul class="wp-chatbot-products">';
1221 while ($product_query->have_posts()) : $product_query->the_post();
1222 $product = $_pf->get_product(get_the_ID());
1223 $html .= '<li class="wp-chatbot-product">';
1224 $html .= '<a target="_blank" href="' . get_permalink(get_the_ID()) . '" wp-chatbot-pid= "' . get_the_ID() . '" title="' . esc_attr($product->post->post_title ? $product->post->post_title : get_the_ID()) . '">';
1225 $html .= get_the_post_thumbnail(get_the_ID(), 'shop_catalog') . '
1226 <div class="wp-chatbot-product-summary">
1227 <div class="wp-chatbot-product-table">
1228 <div class="wp-chatbot-product-table-cell">
1229 <h3 class="wp-chatbot-product-title">' . $product->post->post_title . '</h3>
1230 <div class="price">' . $product->get_price_html() . '</div>';
1231 $html .= ' </div>
1232 </div>
1233 </div></a>
1234 </li>';
1235 endwhile;
1236 wp_reset_query();
1237 wp_reset_postdata();
1238 $html .= '</ul></div>';
1239 } else {
1240 $html .= '<div class="wp-chatbot-products-area">';
1241 $html .= '<p style="text-align: center">You have no products !';
1242 $html .= '</div>';
1243 }
1244 wp_send_json($html);
1245 }
1246 //Recently viewed product shortcode
1247 add_shortcode('wpwbot_products', 'qcld_wb_chatbot_recently_viewed_shortcode');
1248 function qcld_wb_chatbot_recently_viewed_shortcode(){
1249 // Get wpCommerce Global
1250 $_pf = new WC_Product_Factory();
1251 $product_per_page = sanitize_text_field(get_option('qlcd_wp_chatbot_ppp') != '' ? get_option('qlcd_wp_chatbot_ppp') : 10);
1252 $wp_chatbot_product_title = wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_latest_product_welcome'))));
1253 // Get recently viewed product cookies data
1254 $viewed_products = !empty($_COOKIE['wp_chatbot_wpcommerce_recently_viewed']) ? (array)explode('|', $_COOKIE['wp_chatbot_wpcommerce_recently_viewed']) : array();
1255 $viewed_products = array_filter(array_map('absint', $viewed_products));
1256 //get featured products if has.
1257 $featured_products = new WP_Query(array('post_status' => 'publish', 'posts_per_page' => $product_per_page, 'post_type' => 'product', 'tax_query' => array(array('taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'featured'))));
1258 //Getting recently vieew products.
1259 if (!empty($viewed_products)) {
1260 $wp_chatbot_product_title = wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_viewed_product_welcome'))));
1261 $product_query = new WP_Query(array(
1262 'posts_per_page' => $product_per_page,
1263 'no_found_rows' => 1,
1264 'post_status' => 'publish',
1265 'post_type' => 'product',
1266 'post__in' => $viewed_products,
1267 ));
1268 //implementing featured products
1269 } else if ($featured_products->post_count > 0) {
1270 $wp_chatbot_product_title = wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_featured_product_welcome'))));
1271 $product_query = $featured_products;
1272 } else {
1273 $wp_chatbot_product_title = wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_latest_product_welcome'))));
1274 //Getting recent products
1275 $product_query = new WP_Query(array('post_status' => 'publish', 'posts_per_page' => $product_per_page, 'post_type' => 'product', 'orderby' => 'date', 'order' => 'DESC'));
1276 }
1277 if (get_option('wp_chatbot_custom_agent_path') != "" && get_option('wp_chatbot_agent_image') == "custom-agent.png") {
1278 $wp_chatbot_custom_icon_path = get_option('wp_chatbot_custom_agent_path');
1279 } else if (get_option('wp_chatbot_custom_agent_path') != "" && get_option('wp_chatbot_agent_image') != "custom-agent.png") {
1280 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . get_option('wp_chatbot_agent_image');
1281 } else {
1282 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . 'custom-agent.png';
1283 }
1284 $html = '<div class="wp-chatbot-agent-profile">
1285 <div class="wp-chatbot-widget-avatar"><img src="' . esc_url($wp_chatbot_custom_icon_path) . '" alt=""></div>
1286 <div class="wp-chatbot-widget-agent">' . esc_html(get_option('qlcd_wp_chatbot_agent')) . '</div>
1287 <div class="wp-chatbot-bubble">' . esc_html($wp_chatbot_product_title) . '</div>
1288 </div>';
1289 if ($product_query->post_count > 0) {
1290 $html .= '<div class="wp-chatbot-products-area">';
1291 $html .= '<ul class="wp-chatbot-products">';
1292 while ($product_query->have_posts()) : $product_query->the_post();
1293 $product = $_pf->get_product(get_the_ID());
1294 $html .= '<li class="wp-chatbot-product">';
1295 $html .= '<a target="_blank" href="' . get_permalink(get_the_ID()) . '" wp-chatbot-pid= "' . get_the_ID() . '" title="' . esc_attr($product->post->post_title ? $product->post->post_title : get_the_ID()) . '">';
1296 $html .= get_the_post_thumbnail(get_the_ID(), 'shop_catalog') . '
1297 <div class="wp-chatbot-product-summary">
1298 <div class="wp-chatbot-product-table">
1299 <div class="wp-chatbot-product-table-cell">
1300 <h3 class="wp-chatbot-product-title">' . $product->post->post_title . '</h3>
1301 <div class="price">' . $product->get_price_html() . '</div>';
1302 $html .= ' </div>
1303 </div>
1304 </div></a>
1305 </li>';
1306 endwhile;
1307 wp_reset_query();
1308 wp_reset_postdata();
1309 $html .= '</ul></div>';
1310 } else {
1311 $html .= '<div class="wp-chatbot-products-area">';
1312 $html .= '<p style="text-align: center">' . __('You have no products', 'chatbot') . ' !';
1313 $html .= '</div>';
1314 }
1315 return $html;
1316 }
1317 //Show cart for wp chatbot
1318 add_action('wp_ajax_qcld_wb_chatbot_show_cart', 'qcld_wb_chatbot_show_cart');
1319 add_action('wp_ajax_nopriv_qcld_wb_chatbot_show_cart', 'qcld_wb_chatbot_show_cart');
1320 function qcld_wb_chatbot_show_cart(){
1321 global $wpcommerce;
1322 // $cart_url = $wpcommerce->cart->get_cart_url();
1323 $cart_url = wc_get_cart_url();
1324 $checkout_url = wc_get_checkout_url();
1325 //$checkout_url = $wpcommerce->cart->get_checkout_url();
1326 $items = $wpcommerce->cart->get_cart();
1327 $itemCount = $wpcommerce->cart->cart_contents_count;
1328 $cart_title = wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_shopping_cart'))));
1329 $no_cart_item_msg = wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_no_cart_items'))));
1330 if (get_option('wp_chatbot_custom_agent_path') != "" && get_option('wp_chatbot_agent_image') == "custom-agent.png") {
1331 $wp_chatbot_custom_icon_path = get_option('wp_chatbot_custom_agent_path');
1332 } else if (get_option('wp_chatbot_custom_agent_path') != "" && get_option('wp_chatbot_agent_image') != "custom-agent.png") {
1333 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . get_option('wp_chatbot_agent_image');
1334 } else {
1335 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . 'custom-agent.png';
1336 }
1337 $html = '<div class="wp-chatbot-agent-profile">
1338 <div class="wp-chatbot-widget-avatar"><img src="' . esc_url($wp_chatbot_custom_icon_path) . '" alt=""></div>
1339 <div class="wp-chatbot-widget-agent">' . esc_html(get_option('qlcd_wp_chatbot_agent')) . '</div>
1340 <div class="wp-chatbot-bubble">' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_welcome')))) . '</div>
1341 </div>';
1342 if ($itemCount >= 1) {
1343 $html .= '<div class ="wp-chatbot-cart-container">';
1344 $html .= '<div class="wp-chatbot-cart-header"><div class="qcld-wp-chatbot-cell">' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_title')))) . '</div><div class="qcld-wp-chatbot-cell">' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_quantity')))) . '</div><div class="qcld-wp-chatbot-cell">' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_price')))) . '</div><div class="qcld-wp-chatbot-cell"></div></div>';
1345 $html .= '<div class ="wp-chatbot-cart-body">';
1346 foreach ($items as $item => $values) {
1347 $cart_item= apply_filters('wpcommerce_cart_item_product', $values['data'], $values, $item);
1348 //product image
1349 $getProductDetail = wc_get_product($values['product_id']);
1350 $price = get_post_meta($values['product_id'], '_price', true);
1351 $html .= '<div class="wp-chatbot-cart-single">
1352 <div class="qcld-wp-chatbot-cell"> <h3 class="wp-chatbot-title">' . $cart_item->get_title() . '</h3></div>';
1353 $html .= '<div class="qcld-wp-chatbot-cell">';
1354 $html .= '<input class="qcld-wp-chatbot-cart-item-qnty" data-cart-item="' . $item . '" type="number" min="1" value="' . $values['quantity'] . '"></div>';
1355 $html .= '<div class="qcld-wp-chatbot-cell"><span class="wp-chatbot-cart-price">' . apply_filters('wpcommerce_cart_item_price', WC()->cart->get_product_price($cart_item), $values, $item) . '</span> </div>';
1356 $html .= '<div class="qcld-wp-chatbot-cell"><span data-cart-item="' . $item . '" class="wp-chatbot-remove-cart-item">X</span></div> </div>';
1357 }
1358 $html .= ' </div>';//End of cart body
1359 $html .= '<div class="wp-chatbot-cart-single">
1360 <div class="qcld-wp-chatbot-cell"></div>
1361 <div class="qcld-wp-chatbot-cell"><strong>Total</strong></div>
1362 <div class="qcld-wp-chatbot-cell"><strong>' . $wpcommerce->cart->get_cart_total() . '</strong></div>
1363 </div>';
1364 $html .= '<div class="wp-chatbot-cart-footer"><div class="qcld-wp-chatbot-cart-page"><a class="wp-chatbot-cart-link" href="' . $cart_url . '" target="_blank" >' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_link')))) . '</a></div><div class="qcld-wp-chatbot-checkout"><a class="wp-chatbot-checkout-link" href="' . $checkout_url . '" target="_blank" >' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_checkout_link')))) . '</a></div></div>';
1365 $html .= ' </div>';
1366 } else {
1367 $html .= '<div class="wp-chatbot-cart-container">';
1368 $html .= '<div><p style="text-align:center">' . $no_cart_item_msg . '</p></div>';
1369 $html .= '</div>';
1370 }
1371 $response = array('html' => $html, 'items' => $itemCount);
1372 wp_send_json($response);
1373 }
1374 //cart onley for wp chatbot
1375 add_action('wp_ajax_qcld_wb_chatbot_only_cart', 'qcld_wb_chatbot_only_cart');
1376 add_action('wp_ajax_nopriv_qcld_wb_chatbot_only_cart', 'qcld_wb_chatbot_only_cart');
1377 function qcld_wb_chatbot_only_cart(){
1378 global $wpcommerce;
1379 // $cart_url = $wpcommerce->cart->get_cart_url();
1380 $cart_url = wc_get_cart_url();
1381 $checkout_url = wc_get_checkout_url();
1382 //$checkout_url = $wpcommerce->cart->get_checkout_url();
1383 $items = $wpcommerce->cart->get_cart();
1384 $itemCount = $wpcommerce->cart->cart_contents_count;
1385 $no_cart_item_msg = wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_no_cart_items'))));
1386
1387 $html = '';
1388 if ($itemCount >= 1) {
1389 $html .= '<div class ="wp-chatbot-cart-container">';
1390 $html .= '<div class="wp-chatbot-cart-header"><div class="qcld-wp-chatbot-cell">' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_title')))) . '</div><div class="qcld-wp-chatbot-cell">' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_quantity')))) . '</div><div class="qcld-wp-chatbot-cell">' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_price')))) . '</div><div class="qcld-wp-chatbot-cell"></div></div>';
1391 $html .= '<div class ="wp-chatbot-cart-body">';
1392 foreach ($items as $item => $values) {
1393 $cart_item= apply_filters('wpcommerce_cart_item_product', $values['data'], $values, $item);
1394 //product image
1395 $getProductDetail = wc_get_product($values['product_id']);
1396 $price = get_post_meta($values['product_id'], '_price', true);
1397 $html .= '<div class="wp-chatbot-cart-single">
1398 <div class="qcld-wp-chatbot-cell"> <h3 class="wp-chatbot-title">' . $cart_item->get_title() . '</h3></div>';
1399 $html .= '<div class="qcld-wp-chatbot-cell">';
1400 $html .= '<input class="qcld-wp-chatbot-cart-item-qnty" data-cart-item="' . $item . '" type="number" min="1" value="' . $values['quantity'] . '"></div>';
1401 $html .= '<div class="qcld-wp-chatbot-cell"><span class="wp-chatbot-cart-price">' . apply_filters('wpcommerce_cart_item_price', WC()->cart->get_product_price($cart_item), $values, $item) . '</span> </div>';
1402 $html .= '<div class="qcld-wp-chatbot-cell"><span data-cart-item="' . $item . '" class="wp-chatbot-remove-cart-item">X</span></div> </div>';
1403 }
1404 $html .= ' </div>';//End of cart body
1405 $html .= '<div class="wp-chatbot-cart-single">
1406 <div class="qcld-wp-chatbot-cell"></div>
1407 <div class="qcld-wp-chatbot-cell"><strong>Total</strong></div>
1408 <div class="qcld-wp-chatbot-cell"><strong>' . $wpcommerce->cart->get_cart_total() . '</strong></div>
1409 </div>';
1410 $html .= '<div class="wp-chatbot-cart-footer"><div class="qcld-wp-chatbot-cart-page"><a class="wp-chatbot-cart-link" href="' . $cart_url . '" target="_blank" >' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_link')))) . '</a></div><div class="qcld-wp-chatbot-checkout"><a class="wp-chatbot-checkout-link" href="' . $checkout_url . '" target="_blank" >' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_checkout_link')))) . '</a></div></div>';
1411 $html .= ' </div>';
1412 } else {
1413 $html .= '<div class="wp-chatbot-cart-container">';
1414 $html .= '<div><p style="text-align:center">' . esc_html($no_cart_item_msg) . '</p></div>';
1415 $html .= '</div>';
1416 }
1417 $response = array('html' => $html, 'items' => $itemCount);
1418 wp_send_json($response);
1419 }
1420 //Cart show Shortcode
1421 add_shortcode('wpwbot_cart', 'qcld_wb_chatbot_cart_shortcode');
1422 function qcld_wb_chatbot_cart_shortcode(){
1423 global $wpcommerce;
1424 $items = $wpcommerce->cart->get_cart();
1425 $itemCount = $wpcommerce->cart->cart_contents_count;
1426 $cart_title = wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_shopping_cart'))));
1427 $no_cart_item_msg = wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_no_cart_items'))));
1428 if (get_option('wp_chatbot_custom_agent_path') != "" && get_option('wp_chatbot_agent_image') == "custom-agent.png") {
1429 $wp_chatbot_custom_icon_path = get_option('wp_chatbot_custom_agent_path');
1430 } else if (get_option('wp_chatbot_custom_agent_path') != "" && get_option('wp_chatbot_agent_image') != "custom-agent.png") {
1431 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . get_option('wp_chatbot_agent_image');
1432 } else {
1433 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . 'custom-agent.png';
1434 }
1435 $html = '<div class="wp-chatbot-agent-profile">
1436 <div class="wp-chatbot-widget-avatar"><img src="' . esc_url($wp_chatbot_custom_icon_path) . '" alt=""></div>
1437 <div class="wp-chatbot-widget-agent">' . esc_html(get_option('qlcd_wp_chatbot_agent')) . '</div>
1438 <div class="wp-chatbot-bubble">' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_welcome')))) . '</div>
1439 </div>';
1440 if ($itemCount >= 1) {
1441 $html .= '<div class ="wp-chatbot-cart-container">';
1442 $html .= '<div class="wp-chatbot-cart-header"><div class="qcld-wp-chatbot-cell">' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_title')))) . '</div><div class="qcld-wp-chatbot-cell">' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_quantity')))) . '</div><div class="qcld-wp-chatbot-cell">' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_price')))) . '</div> <div class="qcld-wp-chatbot-cell"></div> </div>';
1443 $html .= '<div class ="wp-chatbot-cart-body">';
1444 foreach ($items as $item => $values) {
1445 $cart_item = apply_filters('wpcommerce_cart_item_product', $values['data'], $values, $item);
1446 //product image
1447 $getProductDetail = wc_get_product($values['product_id']);
1448 $price = get_post_meta($values['product_id'], '_price', true);
1449 $html .= '<div class="wp-chatbot-cart-single">
1450 <div class="qcld-wp-chatbot-cell"> <h3 class="wp-chatbot-title">' . $cart_item->get_title() . '</h3></div>';
1451 $html .= '<div class="qcld-wp-chatbot-cell">';
1452 $html .= '<input class="qcld-wp-chatbot-cart-item-qnty" data-cart-item="' . $item . '" type="number" min="1" value="' . $values['quantity'] . '"></div>';
1453 $html .= '<div class="qcld-wp-chatbot-cell"><span class="wp-chatbot-cart-price">' . apply_filters('wpcommerce_cart_item_price', WC()->cart->get_product_price($cart_item), $values, $item) . '</span> </div>';
1454 $html .= '<div class="qcld-wp-chatbot-cell"><span data-cart-item="' . $item . '" class="wp-chatbot-remove-cart-item">X</span></div> </div>';
1455 }
1456 $html .= ' </div>';//End of cart body
1457 $html .= '<div class="wp-chatbot-cart-single">
1458 <div class="qcld-wp-chatbot-cell"></div>
1459 <div class="qcld-wp-chatbot-cell"><strong>Total</strong></div>
1460 <div class="qcld-wp-chatbot-cell"><strong>' . $wpcommerce->cart->get_cart_total() . '</strong></div>
1461 </div>';
1462 $html .= '<div class="wp-chatbot-cart-footer"><div class="qcld-wp-chatbot-cart-page"><a href="' . site_url() . '/cart" target="_blank" >' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_link')))) . '</a></div><div class="qcld-wp-chatbot-checkout"><a href="' . site_url() . '/checkout" target="_blank">' . wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_checkout_link')))) . '</a></div></div>';
1463 $html .= ' </div>';
1464 } else {
1465 $html .= '<div class="wp-chatbot-cart-container">';
1466 $html .= '<div><p style="text-align:center">' . $no_cart_item_msg . '</p></div>';
1467 $html .= '</div>';
1468 }
1469 // $response=array('html'=>$html,'items'=>$itemCount);
1470 return $html;
1471 }
1472 //Updating the cart items.
1473 add_action('wp_ajax_qcld_wb_chatbot_update_cart_item_number', 'qcld_wb_chatbot_update_cart_item_number');
1474 add_action('wp_ajax_nopriv_qcld_wb_chatbot_update_cart_item_number', 'qcld_wb_chatbot_update_cart_item_number');
1475 function qcld_wb_chatbot_update_cart_item_number(){
1476 //getting cart items n
1477 $cart_item_key = sanitize_text_field($_POST['cart_item_key']);
1478 $qnty = sanitize_text_field($_POST['qnty']);
1479 global $wpcommerce;
1480 $result = $wpcommerce->cart->set_quantity($cart_item_key, $qnty);
1481 wp_send_json($result);
1482 }
1483 //Show item after removing from cart page.
1484 add_action('wp_ajax_qcld_wb_chatbot_cart_item_remove', 'qcld_wb_chatbot_cart_item_remove');
1485 add_action('wp_ajax_nopriv_qcld_wb_chatbot_cart_item_remove', 'qcld_wb_chatbot_cart_item_remove');
1486 function qcld_wb_chatbot_cart_item_remove(){
1487 //getting cart items n
1488 $cart_item_key = sanitize_text_field($_POST['cart_item']);
1489 global $wpcommerce;
1490 $result = $wpcommerce->cart->remove_cart_item($cart_item_key);
1491 wp_send_json($result);
1492 }
1493 //Show cart page by shortcode.
1494 add_action('wp_ajax_qcld_wb_chatbot_cart_page', 'qcld_wb_chatbot_cart_page');
1495 add_action('wp_ajax_nopriv_qcld_wb_chatbot_cart_page', 'qcld_wb_chatbot_cart_page');
1496 function qcld_wb_chatbot_cart_page(){
1497 global $wpcommerce;
1498 $itemCount = $wpcommerce->cart->cart_contents_count;
1499 $html = "";
1500 if ($itemCount < 0) {
1501 $html .= wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_no_cart_items'))));
1502 } else {
1503 $html .= do_shortcode("[wpcommerce_cart]");
1504 }
1505 wp_send_json($html);
1506 }
1507 //Show checkout page by shortcode.
1508 add_action('wp_ajax_qcld_wb_chatbot_checkout_page', 'qcld_wb_chatbot_checkout_page');
1509 add_action('wp_ajax_nopriv_qcld_wb_chatbot_checkout_page', 'qcld_wb_chatbot_checkout_page');
1510 function qcld_wb_chatbot_checkout_page(){
1511 global $wpcommerce;
1512 $itemCount = $wpcommerce->cart->cart_contents_count;
1513 $html = "";
1514 if ($itemCount < 0) {
1515 $status = 'no';
1516 $html .= wp_kses_post(wpb_randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_no_cart_items'))));
1517 } else {
1518 $status = 'yes';
1519 $checkout_page_id = get_option('wp_chatbot_app_checkout');
1520 $checkout_parmanlink = esc_url(get_permalink($checkout_page_id));
1521 $html .= $checkout_parmanlink;
1522 }
1523 $response = array('status' => $status, 'html' => $html);
1524 wp_send_json($response);
1525 }
1526 //_dynamic_intent
1527 function qcld_dynamic_intent(){
1528 global $wpdb;
1529 $intents = array();
1530
1531 $ai_df = get_option('enable_wp_chatbot_dailogflow');
1532 $custom_intent_labels = maybe_unserialize( get_option('qlcd_wp_custon_intent_label'));
1533 if($ai_df==1 && isset($custom_intent_labels[0]) && trim($custom_intent_labels[0])!=''):
1534
1535 foreach($custom_intent_labels as $custom_intent_label):
1536 $intents[] = $custom_intent_label;
1537 endforeach;
1538
1539 endif;
1540
1541
1542
1543 if(class_exists('Qcformbuilder_Forms_Admin')){
1544
1545
1546 $results = $wpdb->get_results($wpdb->prepare("SELECT * FROM ". $wpdb->prefix."wfb_forms WHERE type= %s",'primary')); //DB Call OK, No Caching OK
1547
1548 if(!empty($results)){
1549
1550 foreach($results as $result){
1551 $form = maybe_unserialize($result->config);
1552 $intents[] = $form['name'];
1553 }
1554
1555 }
1556 }
1557
1558 if(!empty($intents)){
1559 return implode(", ", $intents);
1560 }else{
1561 return '';
1562 }
1563
1564 }
1565
1566 //User login on Checkout page.
1567 // add_action('wp_ajax_qcld_wb_chatbot_checkout_user_login', 'qcld_wb_chatbot_checkout_user_login');
1568 // add_action('wp_ajax_nopriv_qcld_wb_chatbot_checkout_user_login', 'qcld_wb_chatbot_checkout_user_login');
1569 // function qcld_wb_chatbot_checkout_user_login(){
1570 // // Nonce is checked, get the POST data and sign user on
1571 // $info = array();
1572 // //$info['nonce'] = $_POST['nonce_val'];
1573 // $info['user_login'] = trim(sanitize_text_field($_POST['user_name']));
1574 // $info['user_password'] = trim(sanitize_text_field($_POST['user_pass']));
1575 // $info['remember'] = true;
1576 // $user_signon = wp_signon($info, false);
1577 // // $response=$info;
1578 // $response = array();
1579 // if (is_wp_error($user_signon)) {
1580 // // $response['status'] = "fail";
1581 // $response = "no";
1582 // } else {
1583 // $response = "yes";
1584 // }
1585 // wp_send_json($response);
1586 // }
1587 // Load template for App Order Thank You page url
1588 function wp_chatbot_load_app_template($template){
1589 if (is_page('wpwbot-mobile-app')) {
1590 return dirname(__FILE__) . '/templates/app-templates/app.php';
1591 }
1592 return $template;
1593 }
1594 add_filter('template_include', 'wp_chatbot_load_app_template', 99);
1595 // Load template for App Order Thank You page url
1596 function wp_chatbot_load_app_order_thankyou_template($template){
1597 if (is_page('wpwbot-app-order-thankyou')) {
1598 return dirname(__FILE__) . '/templates/app-templates/app-order-thankyou.php';
1599 }
1600 return $template;
1601 }
1602 add_filter('template_include', 'wp_chatbot_load_app_order_thankyou_template', 99);
1603 // Load template for App checkout
1604 function wp_chatbot_load_app_checkout_template($template){
1605 if (is_page('wpwbot-app-checkout')) {
1606 return dirname(__FILE__) . '/templates/app-templates/app-checkout.php';
1607 }
1608 return $template;
1609 }
1610 add_filter('template_include', 'wp_chatbot_load_app_checkout_template', 99);
1611 // Create embed page when plugin install or activate
1612 //register_activation_hook(__FILE__, 'wp_chatbot_create_app_order_thankyou_page');
1613 add_action('init', 'wp_chatbot_create_app_checkout_thankyou_page');
1614 function wp_chatbot_create_app_checkout_thankyou_page(){
1615 if (get_option('wp_chatbot_app_pages') == 1) {
1616 //Mobile App page create
1617 if (get_page_by_title('wpwBot Mobile App') == NULL) {
1618 //post status and options
1619 $app_page = array(
1620 'comment_status' => 'closed',
1621 'ping_status' => 'closed',
1622 'post_author' => get_current_user_id(),
1623 'post_date' => date('Y-m-d H:i:s'),
1624 'post_status' => 'publish',
1625 'post_title' => 'wpwBot Mobile App',
1626 'post_name' => 'wpwbot-mobile-app',
1627 'post_type' => 'page',
1628 );
1629 //insert page and save the id
1630 $wpwbot_app = wp_insert_post($app_page, false);
1631 //save the id in the database
1632 update_option('wp_chatbot_app_checkout', $wpwbot_app);
1633 }
1634 //App checkout page create
1635 if (get_page_by_title('wpwBot App Checkout') == NULL) {
1636 //post status and options
1637 $checkout_page = array(
1638 'comment_status' => 'closed',
1639 'ping_status' => 'closed',
1640 'post_author' => get_current_user_id(),
1641 'post_date' => date('Y-m-d H:i:s'),
1642 'post_status' => 'publish',
1643 'post_title' => 'wpwBot App Checkout',
1644 'post_name' => 'wpwbot-app-checkout',
1645 'post_type' => 'page',
1646 );
1647 //insert page and save the id
1648 $app_checkout = wp_insert_post($checkout_page, false);
1649 //save the id in the database
1650 update_option('wp_chatbot_app_checkout', $app_checkout);
1651 }
1652 //App Order thank you page create
1653 if (get_page_by_title('wpwBot App Order Thank You') == NULL) {
1654 //post status and options
1655 $thankyou_page = array(
1656 'comment_status' => 'closed',
1657 'ping_status' => 'closed',
1658 'post_author' => get_current_user_id(),
1659 'post_date' => date('Y-m-d H:i:s'),
1660 'post_status' => 'publish',
1661 'post_title' => 'wpwBot App Order Thank You',
1662 'post_name' => 'wpwbot-app-order-thankyou',
1663 'post_type' => 'page',
1664 );
1665 //insert page and save the id
1666 $app_order_thankyou = wp_insert_post($thankyou_page, false);
1667 //save the id in the database
1668 update_option('wp_chatbot_app_order_thankyou', $app_order_thankyou);
1669 }
1670 }
1671 //Keep tracking from App by cookies
1672 if (isset($_GET['from']) && $_GET['from'] == 'app') {
1673 if (!isset($_COOKIE['from_app'])) {
1674 setcookie('from_app', 'yes', (time() + 3600), '/');
1675 }
1676 }
1677 }
1678 /***
1679 * Override order Thank page for mobile app
1680 */
1681 add_action('wpcommerce_thankyou', 'qcld_wb_chatbot__redirect_after_purchase', 10, 1);
1682 function qcld_wb_chatbot__redirect_after_purchase($order_get_id){
1683 if (isset($_COOKIE['from_app']) && $_COOKIE['from_app'] == 'yes') {
1684 global $wp;
1685 if (is_checkout() && !empty($wp->query_vars['order-received'])) {
1686 $thanks_page_id = get_option('wp_chatbot_app_order_thankyou');
1687 $thanks_parmanlink = esc_url(get_permalink($thanks_page_id));
1688 wp_redirect($thanks_parmanlink . '?order_id=' . $order_get_id);
1689 exit;
1690 }
1691 } else {
1692 remove_action('wpcommerce_thankyou', 'qcld_wb_chatbot__redirect_after_purchase');
1693 do_action('wpcommerce_thankyou', $order_get_id);
1694 }
1695 }
1696
1697 function qcld_choose_random($array){
1698 return $array[array_rand($array)];
1699 }
1700
1701 //User session count
1702 add_action('wp_ajax_qcld_wb_chatbot_session_count', 'qcld_wb_chatbot_session_count');
1703 add_action('wp_ajax_nopriv_qcld_wb_chatbot_session_count', 'qcld_wb_chatbot_session_count');
1704 function qcld_wb_chatbot_session_count(){
1705 // Nonce is checked, get the POST data and sign user on
1706 global $wpdb;
1707 $wpdb->show_errors = true;
1708 $tableuser = $wpdb->prefix.'wpbot_sessions';
1709 $response = array();
1710
1711
1712 $session_exists = $wpdb->get_row($wpdb->prepare("select * from $tableuser where 1 and id = %d",1)); //DB Call OK, No Caching OK
1713
1714 if(empty($session_exists)){
1715 $wpdb->insert(
1716 $tableuser,
1717 array(
1718 'session' => 1,
1719 )
1720 ); //DB Call OK, No Caching OK
1721 }else{
1722
1723 $session_id = $session_exists->id;
1724
1725 $wpdb->update(
1726 $tableuser,
1727 array(
1728 'session'=>($session_exists->session+1),
1729 ),
1730 array('id'=>$session_id),
1731 array(
1732 '%d',
1733 ),
1734 array('%d')
1735 ); //DB Call OK, No Caching OK
1736
1737 }
1738
1739 wp_send_json($response);
1740 }
1741
1742 /* WPBot Chat History Addon check */
1743 function qcld_wpbot_is_active_chat_history(){
1744
1745 if(function_exists('qcwp_chat_session_menu_fnc') || function_exists( 'qcpdcs_chat_session_menu_fnc' ) ){
1746 return 1;
1747 }else{
1748 return 0;
1749 }
1750
1751 }
1752
1753 function qcld_wpbot_input_validation( $data ) {
1754 $data = html_entity_decode($data);
1755 $data = trim($data);
1756 $data = stripslashes($data);
1757 $data = htmlspecialchars($data);
1758 return $data;
1759 }
1760 add_action('wp_ajax_qcld_small_talk_import', 'qcld_small_talk_import');
1761 function qcld_small_talk_import(){
1762
1763 global $wpdb;
1764
1765 $table = $wpdb->prefix.'wpbot_response';
1766
1767 $csvFile = file(QCLD_wpCHATBOT_PLUGIN_DIR_PATH . 'small_talk.csv');
1768
1769 foreach ($csvFile as $line) {
1770 $line = str_getcsv($line, ',', '"');
1771 $wpdb->insert($table, array(
1772 'query' => $line[0],
1773 'keyword' => $line[1],
1774 'response' => $line[2],
1775 'category'=> $line[3],
1776 'intent'=> '',
1777 //'lang'=> 'en_US',
1778 )); //DB Call OK, No Caching OK
1779 }
1780
1781 $table2 = $wpdb->prefix.'wpbot_response_category';
1782
1783 $wpdb->insert($table2, array(
1784 'name' => 'smalltalk',
1785 )); //DB Call OK, No Caching OK
1786
1787 update_option( 'qcld_small_talk_imported', 'yes' );
1788
1789 }
1790 function sanitize_array( &$array ) {
1791 if (!empty($array)) {
1792 foreach ($array as &$value) {
1793 if( !is_array($value) )
1794 // sanitize if value is not an array
1795 $value = sanitize_text_field( esc_html($value) );
1796 else
1797 // go inside this function again
1798 sanitize_array(esc_html($value));
1799 }
1800 }
1801 return $array;
1802 }
1803 function qcld_wpbot_meta_tags() {
1804 echo '<!-- "This site uses ChatBot for WordPress - WPBot from https://www.wpbot.pro/" -->';
1805 }
1806 add_action('wp_footer', 'qcld_wpbot_meta_tags', 100);
1807
1808 if ( ! function_exists( 'qcld_change_language_from_center' ) ) {
1809 add_action( 'wp_ajax_qcld_change_language_from_center', 'qcld_change_language_from_center' );
1810 add_action( 'wp_ajax_nopriv_qcld_change_language_from_center', 'qcld_change_language_from_center' );
1811 function qcld_change_language_from_center() {
1812 if ( ! current_user_can( 'manage_options' ) ) {
1813 wp_send_json_error( array( 'message' => 'Unauthorized.' ) );
1814 }
1815 $plugin_path = plugin_dir_path( __FILE__ );
1816 include $plugin_path . 'includes/admin/settings-fields.php';
1817 $json_file_path = $plugin_path . 'includes/language center.json'; // Adjust path as needed
1818
1819 $json_string = file_get_contents( $json_file_path );
1820 if ( isset( $_POST['language'] ) ) {
1821 $language = sanitize_text_field( wp_unslash( $_POST['language'] ) );
1822 $language_array= json_decode($json_string)->$language;
1823 update_option( 'wp_chatbot_language_center_language', $language );
1824 wp_send_json_success( array( 'message' => true, 'data' => $language_array, 'language' => $language ) );
1825 } else {
1826 wp_send_json_error( array( 'message' => 'Language not specified.' ) );
1827
1828 }
1829 }
1830 }