PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 6.6.0
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v6.6.0
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 8.5.2 All 533 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 6.6.0, at class-plugin-deactivate-feedback.php

415 lines 13.8 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'] = __( '', '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 email so we can contact with bug fixes', '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 /*
252 foreach( $form['options'] as $option ) {
253 $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>';
254 }
255 */
256 $html .= '</p><div id="wpb_additional_content" style=""><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)';
257
258 $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 id="wpbot_deactivation_error"></div></div>';
259
260 $html .= '</div><!-- .wpb-goodbye-options -->';
261 }
262 $html .= '</div><!-- .wpb-goodbye-form-body -->';
263 $html .= '<p class="deactivating-spinner"><span class="spinner"></span> ' . __( 'Submitting form', 'wpbot-plugin' ) . '</p>';
264 ?>
265 <div class="wpb-goodbye-form-bg"></div>
266 <style type="text/css">
267 .wpb-form-active .wpb-goodbye-form-bg {
268 background: rgba( 0, 0, 0, .5 );
269 position: fixed;
270 top: 0;
271 left: 0;
272 width: 100%;
273 height: 100%;
274 }
275 .wpb-goodbye-form-wrapper {
276 position: relative;
277 z-index: 999;
278 display: none;
279 }
280 .wpb-form-active .wpb-goodbye-form-wrapper {
281 display: block;
282 }
283 .wpb-goodbye-form {
284 display: none;
285 }
286 .wpb-form-active .wpb-goodbye-form {
287 position: fixed;
288 max-width: 400px;
289 background: #fff;
290 white-space: normal;
291 z-index: 99;
292 top: 50%;
293 left: 50%;
294 transform: translate(-50%, -50%);
295 border-radius: 5px;
296 }
297 .wpb-goodbye-form-head {
298 background: #7a00aa;
299 color: #fff;
300 padding: 8px 18px;
301 text-align: center;
302 border-radius: 5px 5px 0px 0px;
303 }
304 .wpb-goodbye-form-body {
305 padding: 8px 18px;
306 color: #444;
307 }
308 .deactivating-spinner {
309 display: none;
310 }
311 .deactivating-spinner .spinner {
312 float: none;
313 margin: 4px 4px 0 18px;
314 vertical-align: bottom;
315 visibility: visible;
316 }
317 .wpb-goodbye-form-footer {
318 padding: 8px 18px;
319 min-height: 40px;
320 }
321 #wpbot_deactivation_error{color:red}
322 .wpbot_submit_deactivate{float:right}
323 .wpbot_just_deactivate{float: left;
324 font-size: 12px;
325 }
326 </style>
327 <script>
328 jQuery(document).ready(function($){
329 $('input[type=radio]').on('change', function() {
330 if($(this).val()=='Deactivating Temporarily' || $(this).val()=='Upgrading to Pro'){
331 $('#wpb_additional_content').hide();
332 }else{
333 $('#wpb_additional_content').show();
334 }
335
336 });
337
338 $("#wpb-goodbye-link-<?php echo esc_attr( $this->plugin_name ); ?>").on("click",function(){
339 // We'll send the user to this deactivation link when they've completed or dismissed the form
340 var url = document.getElementById("wpb-goodbye-link-<?php echo esc_attr( $this->plugin_name ); ?>");
341 $('body').toggleClass('wpb-form-active');
342 $("#wpb-goodbye-form-<?php echo esc_attr( $this->plugin_name ); ?>").fadeIn();
343 $("#wpb-goodbye-form-<?php echo esc_attr( $this->plugin_name ); ?>").html( '<?php echo wp_kses_post( $html ); ?>' + '<div class="wpb-goodbye-form-footer"><p><a class="wpbot_just_deactivate" href="'+url+'">Just Deactivate</a> <a id="wpb-submit-form" class="button primary wpbot_submit_deactivate" href="#">Submit and Deactivate</a></p></div>');
344 $('#wpb-goodbye-reasons').focus();
345 $('#wpb-submit-form').on('click', function(e){
346
347
348 e.preventDefault();
349
350 if($('#wpb-goodbye-reasons').val()==''){
351 jQuery('#wpbot_deactivation_error').html('Please provide some details to improve the plugin for you!');
352 $('#wpb-goodbye-reasons').focus();
353 return;
354 }
355
356 // As soon as we click, the body of the form should disappear
357 $("#wpb-goodbye-form-<?php echo esc_attr( $this->plugin_name ); ?> .wpb-goodbye-form-body").fadeOut();
358 $("#wpb-goodbye-form-<?php echo esc_attr( $this->plugin_name ); ?> .wpb-goodbye-form-footer").fadeOut();
359 // Fade in spinner
360 $("#wpb-goodbye-form-<?php echo esc_attr( $this->plugin_name ); ?> .deactivating-spinner").fadeIn();
361
362 var values = new Array();
363 $.each($("input[name='wpb-goodbye-options[]']:checked"), function(){
364 values.push($(this).val());
365 });
366 var email = $('#wpb-goodbye-email').val();
367 var details = $('#wpb-goodbye-reasons').val();
368 var data = {
369 'action': 'goodbye_form',
370 'values': values,
371 'details': details,
372 'email': email,
373 'security': "<?php echo sanitize_key( wp_create_nonce( 'wpbot_goodbye_form' ) ); ?>",
374 'dataType': "json"
375 }
376
377 $.post(
378 ajaxurl,
379 data,
380 function(response){
381 // Redirect to original deactivation URL
382 window.location.href = url;
383 }
384 );
385 });
386 // If we click outside the form, the form will close
387 $('.wpb-goodbye-form-bg').on('click',function(){
388 $("#wpb-goodbye-form-<?php echo esc_attr( $this->plugin_name ); ?>").fadeOut();
389 $('body').removeClass('wpb-form-active');
390 });
391 });
392 });
393 </script>
394 <?php }
395
396 /**
397 * AJAX callback when the form is submitted
398 * @since 1.0.0
399 */
400 public function goodbye_form_callback() {
401 check_ajax_referer( 'wpbot_goodbye_form', 'security' );
402
403 if( isset( $_POST['details'] ) ) {
404 $details = sanitize_text_field( $_POST['details'] );
405 update_option( 'wpbot_deactivation_details_' . $this->plugin_name, $details );
406 }
407
408 echo 'success';
409 wp_die();
410 }
411
412 }
413
414 }
415