PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, AI Services / 4.7.3
WPBot – AI ChatBot for Live Support, Lead Generation, AI Services v4.7.3
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 4.7.3, at functions.php

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