PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, AI Services / 8.6.6
WPBot – AI ChatBot for Live Support, Lead Generation, AI Services v8.6.6
8.7.5 8.7.4 8.7.3 8.7.2 8.7.1 8.7.0 8.6.9 8.6.8 8.6.7 8.6.6 8.6.5 8.6.4 8.6.2 8.6.1 8.6.0 8.5.9 8.5.8 8.5.7 8.5.6 8.5.5 8.5.4 8.5.3 8.5.2 8.5.0 8.4.9 All 531 releases
chatbot / functions.php

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

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