PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 3.2.2
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v3.2.2
8.7.8 8.7.7 8.7.6 8.7.5 8.7.4 8.7.3 8.7.2 8.7.1 8.7.0 8.6.9 8.6.8 8.6.7 8.6.6 8.6.5 8.6.4 8.6.2 8.6.1 8.6.0 8.5.9 8.5.8 8.5.7 8.5.6 8.5.5 8.5.4 8.5.3 All 534 releases
chatbot / class-plugin-deactivate-feedback.php

class-plugin-deactivate-feedback.php in WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services 3.2.2, at class-plugin-deactivate-feedback.php

398 lines 13.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Exit if accessed directly
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 if( ! class_exists( 'Wp_Usage_Feedback') ) {
9
10 class Wp_Usage_Feedback {
11
12 private $wpbot_version = '1.0.0';
13 private $home_url = '';
14 private $plugin_file = '';
15 private $plugin_name = '';
16 private $options = array();
17 private $require_optin = true;
18 private $include_goodbye_form = true;
19
20
21 /**
22 * Class constructor
23 *
24 * @param $_home_url The URL to the site we're sending data to
25 * @param $_plugin_file The file path for this plugin
26 * @param $_options Plugin options to track
27 * @param $_require_optin Whether user opt-in is required (always required on WordPress.org)
28 * @param $_include_goodbye_form Whether to include a form when the user deactivates
29 * @param $_marketing Marketing method:
30 * 0: Don't collect email addresses
31 * 1: Request permission same time as tracking opt-in
32 * 2: Request permission after opt-in
33 */
34 public function __construct(
35 $_plugin_file,
36 $_home_url,
37
38 $_require_optin=true,
39 $_include_goodbye_form=true) {
40
41 $this->plugin_file = $_plugin_file;
42 $this->home_url = 'plugins@quantumcloud.com';
43 $this->plugin_name = basename( $this->plugin_file, '.php' );
44
45 $this->require_optin = $_require_optin;
46 $this->include_goodbye_form = $_include_goodbye_form;
47
48
49 // Deactivation hook
50 register_deactivation_hook( $this->plugin_file, array( $this, 'deactivate_this_plugin' ) );
51
52 // Get it going
53 $this->init();
54
55 }
56
57 public function init() {
58
59 // Deactivation
60 add_filter( 'plugin_action_links_' . plugin_basename( $this->plugin_file ), array( $this, 'filter_action_links' ) );
61 add_action( 'admin_footer-plugins.php', array( $this, 'goodbye_ajax' ) );
62 add_action( 'wp_ajax_goodbye_form', array( $this, 'goodbye_form_callback' ) );
63
64
65 }
66
67 // In theme's functions.php or plug-in code:
68
69 function set_content_type(){
70 return "text/html";
71 }
72
73
74 /**
75 * Send the data to the home site
76 *
77 * @since 1.0.0
78 */
79 public function send_data( $body ) {
80 $message = '';
81 foreach($body as $key=>$value){
82
83 if($key=='active_plugins'){
84 $message .='<p> <b>'.$key.'</b>: '.(implode(', ',$value)).' </p>';
85 }
86 elseif($key=='inactive_plugins'){
87 $message .='<p> <b>'.$key.'</b>: '.(implode(', ',$value)).' </p>';
88 }else{
89 $message .='<p> <b>'.$key.'</b>: '.$value.' </p>';
90 }
91
92 }
93
94 $title = 'Plugin Deactivation Notice';
95 $headers = array('From: Anonymous <mailer@just-a-fake-from-address.com>');
96
97 add_filter( 'wp_mail_content_type', array($this, 'set_content_type') );
98 $email = wp_mail($this->home_url, $title, $message, $headers);
99 remove_filter('wp_mail_content_type', array($this, 'set_content_type'));
100
101 return $email;
102
103 }
104
105 /**
106 * Here we collect most of the data
107 *
108 * @since 1.0.0
109 */
110 public function get_data() {
111
112 // Use this to pass error messages back if necessary
113 $body['message'] = '';
114
115 // Use this array to send data back
116 $body = array();
117
118
119
120 /**
121 * Get our plugin data
122 * Currently we grab plugin name and version
123 * Or, return a message if the plugin data is not available
124 * @since 1.0.0
125 */
126 $plugin = $this->plugin_data();
127 if( empty( $plugin ) ) {
128 // We can't find the plugin data
129 // Send a message back to our home site
130 $body['message'] .= __( 'We can\'t detect any plugin information. This is most probably because you have not included the code in the plugin main file.', 'wpchatbot' );
131 $body['status'] = 'Data not found'; // Never translated
132 } else {
133 if( isset( $plugin['Name'] ) ) {
134 $body['plugin'] = sanitize_text_field( $plugin['Name'] );
135 }
136 if( isset( $plugin['Version'] ) ) {
137 $body['version'] = sanitize_text_field( $plugin['Version'] );
138 }
139
140 }
141
142 // Return the data
143 return $body;
144
145 }
146
147 /**
148 * Return plugin data
149 * @since 1.0.0
150 */
151 public function plugin_data() {
152 // Being cautious here
153 if( ! function_exists( 'get_plugin_data' ) ) {
154 include ABSPATH . '/wp-admin/includes/plugin.php';
155 }
156 // Retrieve current plugin information
157 $plugin = get_plugin_data( $this->plugin_file );
158 return $plugin;
159 }
160
161 /**
162 * Deactivating plugin
163 * @since 1.0.0
164 */
165 public function deactivate_this_plugin() {
166
167 $body = $this->get_data();
168 $body['status'] = 'Deactivated'; // Never translated
169 $body['deactivated_date'] = date('Y-m-d');
170
171 // Add deactivation form data
172 if( false !== get_option( 'wpbot_deactivation_reason_' . $this->plugin_name ) ) {
173 $body['deactivation_reason'] = get_option( 'wpbot_deactivation_reason_' . $this->plugin_name );
174 delete_option('wpbot_deactivation_reason_' . $this->plugin_name);
175 }
176 if( false !== get_option( 'wpbot_deactivation_details_' . $this->plugin_name ) ) {
177 $body['deactivation_details'] = get_option( 'wpbot_deactivation_details_' . $this->plugin_name );
178 delete_option('wpbot_deactivation_details_' . $this->plugin_name);
179 }
180
181 if(isset($body['deactivation_reason']) or isset($body['deactivation_details']))
182 $this->send_data( $body );
183
184
185 }
186
187 /**
188 * Filter the deactivation link to allow us to present a form when the user deactivates the plugin
189 * @since 1.0.0
190 */
191 public function filter_action_links( $links ) {
192
193 if( isset( $links['deactivate'] ) && $this->include_goodbye_form ) {
194 $deactivation_link = $links['deactivate'];
195 // Insert an onClick action to allow form before deactivating
196 $deactivation_link = str_replace( '<a ', '<div class="wpb-goodbye-form-wrapper"><span class="wpb-goodbye-form" id="wpb-goodbye-form-' . esc_attr( $this->plugin_name ) . '"></span></div><a onclick="javascript:event.preventDefault();" id="wpb-goodbye-link-' . esc_attr( $this->plugin_name ) . '" ', $deactivation_link );
197 $links['deactivate'] = $deactivation_link;
198 }
199 return $links;
200 }
201
202 /*
203 * Form text strings
204 * These are non-filterable and used as fallback in case filtered strings aren't set correctly
205 * @since 1.0.0
206 */
207 public function form_default_text() {
208 $form = array();
209 $form['heading'] = __( 'Sorry to see you go', 'wpchatbot' );
210 $form['body'] = __( 'Before you deactivate the plugin, would you quickly give us your reason for doing so?', 'wpchatbot' );
211 $form['options'] = array(
212 __( 'Found a Bug', 'wpchatbot' ),
213 __( 'Need More Features', 'wpchatbot' ),
214 __( 'Deactivating Temporarily', 'wpchatbot' ),
215 __( 'Upgrading to Pro', 'wpchatbot' ),
216
217 );
218 $form['email'] = __( 'Please provide your email so we can contact you if needed.', 'wpchatbot' );
219 $form['details'] = __( 'Please provide some details so we can improve the plugin', 'wpchatbot' );
220 return $form;
221 }
222
223 /**
224 * Form text strings
225 * These can be filtered
226 * The filter hook must be unique to the plugin
227 * @since 1.0.0
228 */
229 public function form_filterable_text() {
230 $form = $this->form_default_text();
231 return apply_filters( 'wpbot_form_text_' . esc_attr( $this->plugin_name ), $form );
232 }
233
234 /**
235 * Form text strings
236 * These can be filtered
237 * @since 1.0.0
238 */
239 public function goodbye_ajax() {
240 // Get our strings for the form
241 $form = $this->form_filterable_text();
242 if( ! isset( $form['heading'] ) || ! isset( $form['body'] ) || ! isset( $form['options'] ) || ! is_array( $form['options'] ) || ! isset( $form['details'] ) ) {
243 // If the form hasn't been filtered correctly, we revert to the default form
244 $form = $this->form_default_text();
245 }
246 // Build the HTML to go in the form
247 $html = '<div class="wpb-goodbye-form-head"><strong>' . esc_html( $form['heading'] ) . '</strong></div>';
248 $html .= '<div class="wpb-goodbye-form-body"><p>' . esc_html( $form['body'] ) . '</p>';
249 if( is_array( $form['options'] ) ) {
250 $html .= '<div class="wpb-goodbye-options"><p>';
251 foreach( $form['options'] as $option ) {
252 $html .= '<input type="radio" name="wpb-goodbye-options" id="' . str_replace( " ", "", esc_attr( $option ) ) . '" value="' . esc_attr( $option ) . '"> <label for="' . str_replace( " ", "", esc_attr( $option ) ) . '">' . esc_attr( $option ) . '</label><br>';
253 }
254 $html .= '</p><div id="wpb_additional_content" style="display:none;"><label for="wpb-goodbye-reasons">' . esc_html( $form['email'] ) .'</label><br><input type="email" name="wpb-goodbye-email" id="wpb-goodbye-email" value="'.get_option('admin_email').'" /> (Optional)';
255
256 $html .= '<br><label for="wpb-goodbye-reasons">' . esc_html( $form['details'] ) .'</label><textarea name="wpb-goodbye-reasons" id="wpb-goodbye-reasons" rows="2" style="width:100%"></textarea></div>';
257 $html .= '</div><!-- .wpb-goodbye-options -->';
258 }
259 $html .= '</div><!-- .wpb-goodbye-form-body -->';
260 $html .= '<p class="deactivating-spinner"><span class="spinner"></span> ' . __( 'Submitting form', 'wpbot-plugin' ) . '</p>';
261 ?>
262 <div class="wpb-goodbye-form-bg"></div>
263 <style type="text/css">
264 .wpb-form-active .wpb-goodbye-form-bg {
265 background: rgba( 0, 0, 0, .5 );
266 position: fixed;
267 top: 0;
268 left: 0;
269 width: 100%;
270 height: 100%;
271 }
272 .wpb-goodbye-form-wrapper {
273 position: relative;
274 z-index: 999;
275 display: none;
276 }
277 .wpb-form-active .wpb-goodbye-form-wrapper {
278 display: block;
279 }
280 .wpb-goodbye-form {
281 display: none;
282 }
283 .wpb-form-active .wpb-goodbye-form {
284 position: fixed;
285 max-width: 400px;
286 background: #fff;
287 white-space: normal;
288 z-index: 99;
289 top: 50%;
290 left: 50%;
291 transform: translate(-50%, -50%);
292 border-radius: 5px;
293 }
294 .wpb-goodbye-form-head {
295 background: #7a00aa;
296 color: #fff;
297 padding: 8px 18px;
298 text-align: center;
299 border-radius: 5px 5px 0px 0px;
300 }
301 .wpb-goodbye-form-body {
302 padding: 8px 18px;
303 color: #444;
304 }
305 .deactivating-spinner {
306 display: none;
307 }
308 .deactivating-spinner .spinner {
309 float: none;
310 margin: 4px 4px 0 18px;
311 vertical-align: bottom;
312 visibility: visible;
313 }
314 .wpb-goodbye-form-footer {
315 padding: 8px 18px;
316 }
317 </style>
318 <script>
319 jQuery(document).ready(function($){
320 $('input[type=radio]').live('change', function() {
321 if($(this).val()=='Deactivating Temporarily' || $(this).val()=='Upgrading to Pro'){
322 $('#wpb_additional_content').hide();
323 }else{
324 $('#wpb_additional_content').show();
325 }
326
327 });
328
329 $("#wpb-goodbye-link-<?php echo esc_attr( $this->plugin_name ); ?>").on("click",function(){
330 // We'll send the user to this deactivation link when they've completed or dismissed the form
331 var url = document.getElementById("wpb-goodbye-link-<?php echo esc_attr( $this->plugin_name ); ?>");
332 $('body').toggleClass('wpb-form-active');
333 $("#wpb-goodbye-form-<?php echo esc_attr( $this->plugin_name ); ?>").fadeIn();
334 $("#wpb-goodbye-form-<?php echo esc_attr( $this->plugin_name ); ?>").html( '<?php echo $html; ?>' + '<div class="wpb-goodbye-form-footer"><p><a id="wpb-submit-form" class="button primary" href="#">Submit and Deactivate</a>&nbsp;<a class="secondary button" href="'+url+'">Just Deactivate</a></p></div>');
335 $('#wpb-submit-form').on('click', function(e){
336 // As soon as we click, the body of the form should disappear
337 $("#wpb-goodbye-form-<?php echo esc_attr( $this->plugin_name ); ?> .wpb-goodbye-form-body").fadeOut();
338 $("#wpb-goodbye-form-<?php echo esc_attr( $this->plugin_name ); ?> .wpb-goodbye-form-footer").fadeOut();
339 // Fade in spinner
340 $("#wpb-goodbye-form-<?php echo esc_attr( $this->plugin_name ); ?> .deactivating-spinner").fadeIn();
341 e.preventDefault();
342 var values = new Array();
343 $.each($("input[name='wpb-goodbye-options[]']:checked"), function(){
344 values.push($(this).val());
345 });
346 var email = $('#wpb-goodbye-email').val();
347 var details = $('#wpb-goodbye-reasons').val();
348 var data = {
349 'action': 'goodbye_form',
350 'values': values,
351 'details': details,
352 'email': email,
353 'security': "<?php echo wp_create_nonce ( 'wpbot_goodbye_form' ); ?>",
354 'dataType': "json"
355 }
356
357 $.post(
358 ajaxurl,
359 data,
360 function(response){
361 // Redirect to original deactivation URL
362 window.location.href = url;
363 }
364 );
365 });
366 // If we click outside the form, the form will close
367 $('.wpb-goodbye-form-bg').on('click',function(){
368 $("#wpb-goodbye-form-<?php echo esc_attr( $this->plugin_name ); ?>").fadeOut();
369 $('body').removeClass('wpb-form-active');
370 });
371 });
372 });
373 </script>
374 <?php }
375
376 /**
377 * AJAX callback when the form is submitted
378 * @since 1.0.0
379 */
380 public function goodbye_form_callback() {
381 check_ajax_referer( 'wpbot_goodbye_form', 'security' );
382 if( isset( $_POST['values'] ) ) {
383 $values = json_encode( wp_unslash( $_POST['values'] ) );
384 update_option( 'wpbot_deactivation_reason_' . $this->plugin_name, $values );
385 }
386 if( isset( $_POST['details'] ) ) {
387 $details = sanitize_text_field( $_POST['details'] );
388 update_option( 'wpbot_deactivation_details_' . $this->plugin_name, $details );
389 }
390
391 echo 'success';
392 wp_die();
393 }
394
395 }
396
397 }
398