PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 7.8.4
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v7.8.4
8.7.8 8.7.7 8.7.6 8.7.5 8.7.4 8.7.3 8.7.2 8.7.1 8.7.0 8.6.9 8.6.8 8.6.7 8.6.6 8.6.5 8.6.4 8.6.2 8.6.1 8.6.0 8.5.9 8.5.8 8.5.7 8.5.6 8.5.5 8.5.4 8.5.3 All 534 releases
chatbot / functions.php

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

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