PluginProbe
Chatbot for WordPress by Collect.chat ⚡️ / trunk
Chatbot for WordPress by Collect.chat ⚡️ vtrunk
trunk 1.0 1.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 2.0.0 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 All 59 releases
collectchat / collect.php

collect.php in Chatbot for WordPress by Collect.chat ⚡️ trunk, at collect.php

408 lines 16.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Collect.chat - Chatbot
4 * Version: 2.5.1
5 * Plugin URI: https://collect.chat
6 * Description: Chatbots are the simplest, easiest way to collect leads & data from visitors. Create free chatbot without coding using Collect.chat. Never miss an opportunity by engaging every site visitor.
7 * Author: Collect.chat Inc.
8 * Author URI: https://collect.chat
9 * License: GPLv3
10 */
11
12 // exit if accessed directly
13 if (!defined('ABSPATH'))
14 {
15 exit;
16 }
17
18 define('CC_PLUGIN_DIR', str_replace('\\', '/', dirname(__FILE__)));
19
20 if (!class_exists('ScriptLoader'))
21 {
22
23 class ScriptLoader
24 {
25
26
27 function __construct()
28 {
29 // Add text domain loading
30 add_action('plugins_loaded', array($this, 'load_plugin_textdomain'));
31
32 add_action('admin_init', array(&$this,
33 'admin_init'
34 ));
35
36 add_action('admin_menu', array(&$this,
37 'admin_menu'
38 ));
39
40 add_action('wp_head', array(&$this,
41 'wp_head'
42 ));
43
44 add_action('plugins_loaded', array(&$this,
45 'register_embed'
46 ));
47
48 $plugin = plugin_basename(__FILE__);
49 add_filter("plugin_action_links_$plugin", array(&$this,
50 'collectchat_settings_link'
51 ));
52
53 // Toaster if not configured
54 add_action('admin_notices', array($this, 'collectchat_settings_message'));
55
56
57 // Activation page auto redirect
58 add_action( 'activated_plugin', array(&$this,
59 'collectchat_activation_redirect'
60 ));
61
62
63 // Message to rate the plugin
64 add_action('admin_notices', array($this, 'collectchat_rating_request'));
65 add_action('admin_init', array($this, 'collectchat_ignore_notice'));
66
67 // Deactivation feedback
68 require_once dirname( __FILE__ ) . '/deactivation-feedback/register.php';
69 collectchat_feedback_include_init( plugin_basename( __FILE__ ) );
70
71 // Register oEmbed providers
72 wp_oembed_add_provider('https://links.collect.chat/*', 'https://dashboard.collect.chat/forms-embed');
73
74 }
75
76 // Add new method to load text domain
77 public function load_plugin_textdomain() {
78 load_plugin_textdomain(
79 'collectchat',
80 false,
81 dirname(plugin_basename(__FILE__)) . '/languages/'
82 );
83 }
84
85 function register_embed()
86 {
87 //Register shortcode
88 add_shortcode('collect-chat', array(&$this,
89 'embed_bot'
90 ));
91 }
92
93 //[collect-chat] oembed
94 function embed_bot($atts)
95 {
96 if (isset($atts['id']))
97 {
98 if (!isset($atts['height']))
99 {
100 $atts['height'] = "500";
101 }
102 $id = preg_replace("/[^a-zA-Z0-9]+/", "", $atts["id"]);
103 $height = preg_replace("/[^a-zA-Z0-9]+/", "", $atts["height"]);
104
105 return '<iframe src="https://links.collect.chat/' . $id . '" width="100%" height="' . $height . '" frameBorder="0" allowfullscreen></iframe>';
106 }
107 else
108 {
109 return 'Please enter a valid Collect.chat bot id';
110 }
111
112 }
113
114
115 function collectchat_activation_redirect($plugin)
116 {
117 if( $plugin == plugin_basename( __FILE__ ) ) {
118 exit(wp_redirect(admin_url('admin.php?page=collectchat')));
119 }
120 }
121
122 function collectchat_settings_link($links)
123 {
124 $settings_link = '<a href="options-general.php?page=collectchat">' . __('Settings', 'collectchat') . '</a>';
125 $support_link = '<a href="https://help.collect.chat" target="_blank">' . __('Support', 'collectchat') . '</a>';
126
127 array_push($links, $settings_link);
128 array_push($links, $support_link);
129
130 return $links;
131 }
132
133 function collectchat_settings_message()
134 {
135 $settings = get_option('collectchat-plugin-settings');
136 $postmeta_query_args = array(
137 'post_type' => 'post',
138 'meta_key' => '_inpost_head_script',
139 );
140 $postmeta_query = new WP_Query( $postmeta_query_args );
141 if (!(isset($settings) && !empty($settings['script'])) && !$postmeta_query->have_posts() && !(isset($_GET['page']) && $_GET['page'] == 'collectchat'))
142 {
143 ?>
144 <div class="notice notice-error" style="display: flex;">
145 <a href="https://collect.chat" class="logo" style="margin: auto;"><img src="https://collect.chat/assets/images/logo-black.png" width="60px" height="60px" alt="Collect.chat"/></a>
146 <div style="flex-grow: 1; margin: 15px 15px;">
147 <h4 style="margin: 0;"><?php _e('Add chatbot snippet to continue', 'collectchat'); ?></h4>
148 <p><?php _e('Oops!😓 It appears that your Collect.chat chatbot is not configured correctly.', 'collectchat'); ?></p>
149 </div>
150 <a href="https://dashboard.collect.chat/getstarted?user=<?php echo esc_attr(wp_get_current_user()->user_email); ?>&source=wordpress" target="_blank" class="button button-primary" style="margin: auto 15px; background-color: #208a46; border-color: #208a46; text-shadow: none; box-shadow: none;"><?php _e('Create a free account', 'collectchat'); ?></a>
151 <a href="admin.php?page=collectchat" class="button button-primary" style="margin: auto 15px; background-color: #f16334; border-color: #f16334; text-shadow: none; box-shadow: none;"><?php _e('Add the bot snippet', 'collectchat'); ?></a>
152 </div>
153 <?php
154 }
155 }
156
157 // Add admin notice to rate the plugin
158 function collectchat_rating_request(){
159 $settings = get_option('collectchat-plugin-settings');
160 if(!empty($settings['installedOn'])){
161 $ignore_rating = empty($settings['ignore_rating']) ? "" : $settings['ignore_rating'];
162 if($ignore_rating != "yes"){
163 $date1 = $settings['installedOn'];
164 $date2 = date("Y/m/d");
165 $diff = abs(strtotime($date2) - strtotime($date1));
166 $days = floor($diff / (60*60*24));
167 if($days >= 7){
168 $cc_new_URI = $_SERVER['REQUEST_URI'];
169 $cc_new_URI = add_query_arg('collectchat-ignore-notice', '0', $cc_new_URI);
170 $cc_new_URI = esc_url($cc_new_URI);
171 echo '<div class="notice notice-success">';
172 echo '<div style="display:flex;"><a href="https://collect.chat" class="logo" style="margin: auto;"><img src="https://collectcdn.com/assets/heart.gif" width="60px" height="60px" alt="Collect.chat"/></a>';
173 printf(__('<div style="flex-grow:1;margin: 15px;"><h4 style="margin: 0;">Awesome! You have been using <a href="admin.php?page=collectchat">Collect.chat</a> chatbot plugin for more than 1 week 😎</h4>
174 <p>Would you mind taking a few seconds to give it a 5-star rating on WordPress?<br/>Thank you in advance :)</p></div></div>'));
175 printf(__('<a href="%2$s" class="button button-primary" style="margin-bottom: 10px; background-color: #208a46; border-color: #208a46;" target="_blank">Ok, you deserved it</a>
176 <a class="button button-primary" style="margin-bottom: 10px;" href="%1$s">I already did</a>
177 <a class="button button-error" style="margin-bottom: 10px;" href="%1$s">No, not good enough</a>', 'advanced-database-cleaner'), $cc_new_URI,
178 'https://wordpress.org/support/plugin/collectchat/reviews/?filter=5');
179 echo "</div>";
180 }
181 }
182 }
183 }
184
185 function collectchat_ignore_notice(){
186 if(isset($_GET['collectchat-ignore-notice']) && $_GET['collectchat-ignore-notice'] == "0"){
187 $settings = get_option('collectchat-plugin-settings');
188 $settings['ignore_rating'] = "yes";
189 update_option('collectchat-plugin-settings', $settings, "no");
190 }
191 }
192
193 function collectchat_html_sanitize($input) {
194 $allowed_html = array(
195 'script' => array(),
196 );
197
198 if (current_user_can('unfiltered_html')) {
199 $input['script'] = wp_kses($input['script'], $allowed_html); // Script sanitization for users with the unfiltered_html capability
200 } else {
201 $input['script'] = wp_kses_post($input['script']); // Sanitize all content for other users
202 }
203 return $input;
204 }
205
206
207 function admin_init()
208 {
209
210 // register settings for sitewide script
211 register_setting('collectchat-settings-group', 'collectchat-plugin-settings', array( 'sanitize_callback' => array( $this, 'collectchat_html_sanitize') ));
212
213 add_settings_field('script', 'Script', 'trim', 'collectchat');
214 add_settings_field('showOn', 'Show On', 'trim', 'collectchat');
215 add_settings_field('installedOn', 'Show On', 'trim', 'collectchat');
216
217 // default value for settings
218 $initialSettings = get_option('collectchat-plugin-settings');
219 if ($initialSettings === false)
220 {
221 $initialSettings['showOn'] = 'all';
222 $initialSettings['installedOn'] = date("Y/m/d");
223 update_option('collectchat-plugin-settings', $initialSettings);
224 }
225 if($initialSettings === true && !$initialSettings['showOn']) {
226 $initialSettings['showOn'] = 'all';
227 update_option('collectchat-plugin-settings', $initialSettings);
228 }
229 if($initialSettings === true && !$initialSettings['installedOn']) {
230 $initialSettings['installedOn'] = date("Y/m/d");
231 update_option('collectchat-plugin-settings', $initialSettings);
232 }
233
234 // add meta box to all post types
235 add_meta_box('cc_all_post_meta', esc_html__('Collect.chat Snippet:', 'collectchat-settings') , 'collectchat_meta_setup', array(
236 'post',
237 'page'
238 ) , 'normal', 'default');
239
240 add_action('save_post', 'collectchat_post_meta_save');
241
242
243
244 }
245
246 // adds menu item to wordpress admin dashboard
247 function admin_menu()
248 {
249 add_menu_page(__('Collect.chat', 'collectchat-settings') , __('Collect.chat', 'collectchat-settings') , 'manage_options', 'collectchat', array(&$this,
250 'collectchat_options_panel'
251 ) , 'data:image/svg+xml;base64,PHN2ZyBpZD0ic3ZnIiB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHdpZHRoPSI0MDAiIGhlaWdodD0iNDAwIiB2aWV3Qm94PSIwLCAwLCA0MDAsNDAwIj48ZyBpZD0ic3ZnZyI+PHBhdGggaWQ9InBhdGgwIiBkPSJNMTkxLjM1OCA2NC45MjYgQyAxMTEuNjcyIDcwLjg0OSw1My41MTkgMTQzLjgwNCw2NS4zNTIgMjIzLjAwNiBDIDY3LjY4MiAyMzguNTk5LDc0LjI5MyAyNTcuMzc3LDgyLjA5OCAyNzAuNTcyIEwgODIuOTMzIDI3MS45ODMgNzkuMTQ5IDI4Ni4yMzkgQyA3Ny4wNjggMjk0LjA3OSw3NC41MjQgMzAzLjY3MCw3My40OTYgMzA3LjU1MSBDIDcyLjQ2NyAzMTEuNDMzLDcwLjE0MCAzMjAuMTk4LDY4LjMyMyAzMjcuMDI5IEwgNjUuMDIxIDMzOS40NTAgMTM0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ibm9uZSIgZmlsbD0iI2E0YTRiMCIgZmlsbC1ydWxlPSJldmVub2RkIj48L3BhdGg+PHBhdGggaWQ9InBhdGgzIiBkPSIiIHN0cm9rZT0ibm9uZSIgZmlsbD0iI2E0YTRiMCIgZmlsbC1ydWxlPSJldmVub2RkIj48L3BhdGg+PHBhdGggaWQ9InBhdGg0IiBkPSIiIHN0cm9rZT0ibm9uZSIgZmlsbD0iI2E0YTRiMCIgZmlsbC1ydWxlPSJldmVub2RkIj48L3BhdGg+PC9nPjwvc3ZnPg==');
252
253 }
254
255 function wp_head()
256 {
257
258 $settings = get_option('collectchat-plugin-settings');
259 $allowed_html = array(
260 'script' => array(),
261 );
262
263 if (is_array($settings) && array_key_exists('script', $settings))
264 {
265 $script = $settings['script'];
266 $showOn = $settings['showOn'];
267
268 // main bot
269 if ($script != '')
270 {
271 if (($showOn === 'all') || ($showOn === 'home' && (is_home() || is_front_page())) || ($showOn === 'nothome' && !is_home() && !is_front_page()) || !$showOn === 'none')
272 {
273 echo wp_kses($script, $allowed_html), wp_kses('<script type="text/javascript">var CollectChatWordpress = true;</script>', $allowed_html), "\n";
274 }
275 }
276 }
277
278 // post and page bots
279 $cc_post_meta = get_post_meta(get_the_ID() , '_inpost_head_script', true);
280 if ($cc_post_meta != '' && !is_home() && !is_front_page())
281 {
282
283 echo wp_kses($cc_post_meta['synth_header_script'], $allowed_html), wp_kses('<script type="text/javascript">var CollectChatWordpress = true;</script>', $allowed_html), "\n";
284
285 }
286
287 }
288
289 function collectchat_options_panel()
290 {
291 // Load options page
292 require_once (CC_PLUGIN_DIR . '/options.php');
293 }
294 }
295
296 function collectchat_meta_setup()
297 {
298 global $post;
299
300 // using an underscore, prevents the meta variable
301 // from showing up in the custom fields section
302 $meta = get_post_meta($post->ID, '_inpost_head_script', true);
303
304 // instead of writing HTML here, lets do an include
305 include_once (CC_PLUGIN_DIR . '/meta.php');
306
307 // create a custom nonce for submit verification later
308 echo '<input type="hidden" name="cc_post_meta_noncename" value="' . wp_create_nonce(__FILE__) . '" />';
309 }
310
311 function collectchat_post_meta_save($post_id)
312 {
313
314 // make sure data came from our meta box
315 if (!isset($_POST['cc_post_meta_noncename']) || !wp_verify_nonce($_POST['cc_post_meta_noncename'], __FILE__)) return $post_id;
316
317 // check user permissions
318 if ($_POST['post_type'] == 'page')
319 {
320 if (!current_user_can('edit_page', $post_id)) return $post_id;
321
322 }
323 else
324 {
325
326 if (!current_user_can('edit_post', $post_id)) return $post_id;
327
328 }
329
330 // only users with unfiltered_html capability can save scripts
331 if (!current_user_can('unfiltered_html')) return $post_id;
332
333 $current_data = get_post_meta($post_id, '_inpost_head_script', true);
334
335 $new_data = $_POST['_inpost_head_script'];
336
337 collectchat_post_meta_clean($new_data);
338
339 if ($current_data)
340 {
341
342 if (is_null($new_data)) delete_post_meta($post_id, '_inpost_head_script');
343
344 else update_post_meta($post_id, '_inpost_head_script', $new_data);
345
346 }
347 elseif (!is_null($new_data))
348 {
349
350 add_post_meta($post_id, '_inpost_head_script', $new_data, true);
351
352 }
353
354 return $post_id;
355 }
356
357 function collectchat_post_meta_clean(&$arr)
358 {
359
360 $allowed_html = array(
361 'script' => array(),
362 );
363
364 if (is_array($arr))
365 {
366
367 foreach ($arr as $i => $v)
368 {
369
370 if (is_array($arr[$i]))
371 {
372 collectchat_post_meta_clean($arr[$i]);
373
374 if (!count($arr[$i]))
375 {
376 unset($arr[$i]);
377 }
378
379 }
380 else
381 {
382
383
384
385 if (trim($arr[$i]) == '')
386 {
387 unset($arr[$i]);
388 } else {
389 if (current_user_can('unfiltered_html')) {
390 $arr[$i] = wp_kses($v, $allowed_html);
391 } else {
392 $arr[$i] = wp_kses_post($v);
393 }
394 }
395 }
396 }
397
398 if (!count($arr))
399 {
400 $arr = NULL;
401 }
402 }
403 }
404
405 $scripts = new ScriptLoader();
406
407 }
408 ?>