PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 1.8.0
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v1.8.0
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 1.8.0, at class-plugin-deactivate-feedback.php

382 lines 12.4 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? Only the option you selected in this form will be emailed to us.', 'wpchatbot' );
211 $form['options'] = array(
212 __( 'Found a Bug', 'wpchatbot' ),
213 __( 'Need More Features', 'wpchatbot' ),
214 __( 'Deactivating Temporarily', 'wpchatbot' ),
215
216 );
217 $form['details'] = __( 'Please provide some details so we can improve the plugin for you (optional)', 'wpchatbot' );
218 return $form;
219 }
220
221 /**
222 * Form text strings
223 * These can be filtered
224 * The filter hook must be unique to the plugin
225 * @since 1.0.0
226 */
227 public function form_filterable_text() {
228 $form = $this->form_default_text();
229 return apply_filters( 'wpbot_form_text_' . esc_attr( $this->plugin_name ), $form );
230 }
231
232 /**
233 * Form text strings
234 * These can be filtered
235 * @since 1.0.0
236 */
237 public function goodbye_ajax() {
238 // Get our strings for the form
239 $form = $this->form_filterable_text();
240 if( ! isset( $form['heading'] ) || ! isset( $form['body'] ) || ! isset( $form['options'] ) || ! is_array( $form['options'] ) || ! isset( $form['details'] ) ) {
241 // If the form hasn't been filtered correctly, we revert to the default form
242 $form = $this->form_default_text();
243 }
244 // Build the HTML to go in the form
245 $html = '<div class="wpb-goodbye-form-head"><strong>' . esc_html( $form['heading'] ) . '</strong></div>';
246 $html .= '<div class="wpb-goodbye-form-body"><p>' . esc_html( $form['body'] ) . '</p>';
247 if( is_array( $form['options'] ) ) {
248 $html .= '<div class="wpb-goodbye-options"><p>';
249 foreach( $form['options'] as $option ) {
250 $html .= '<input type="checkbox" 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>';
251 }
252 $html .= '</p><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>';
253 $html .= '</div><!-- .wpb-goodbye-options -->';
254 }
255 $html .= '</div><!-- .wpb-goodbye-form-body -->';
256 $html .= '<p class="deactivating-spinner"><span class="spinner"></span> ' . __( 'Submitting form', 'wpbot-plugin' ) . '</p>';
257 ?>
258 <div class="wpb-goodbye-form-bg"></div>
259 <style type="text/css">
260 .wpb-form-active .wpb-goodbye-form-bg {
261 background: rgba( 0, 0, 0, .5 );
262 position: fixed;
263 top: 0;
264 left: 0;
265 width: 100%;
266 height: 100%;
267 }
268 .wpb-goodbye-form-wrapper {
269 position: relative;
270 z-index: 999;
271 display: none;
272 }
273 .wpb-form-active .wpb-goodbye-form-wrapper {
274 display: block;
275 }
276 .wpb-goodbye-form {
277 display: none;
278 }
279 .wpb-form-active .wpb-goodbye-form {
280 position: fixed;
281 max-width: 400px;
282 background: #fff;
283 white-space: normal;
284 z-index: 99;
285 top: 50%;
286 left: 50%;
287 transform: translate(-50%, -50%);
288 border-radius: 5px;
289 }
290 .wpb-goodbye-form-head {
291 background: #7a00aa;
292 color: #fff;
293 padding: 8px 18px;
294 text-align: center;
295 border-radius: 5px 5px 0px 0px;
296 }
297 .wpb-goodbye-form-body {
298 padding: 8px 18px;
299 color: #444;
300 }
301 .deactivating-spinner {
302 display: none;
303 }
304 .deactivating-spinner .spinner {
305 float: none;
306 margin: 4px 4px 0 18px;
307 vertical-align: bottom;
308 visibility: visible;
309 }
310 .wpb-goodbye-form-footer {
311 padding: 8px 18px;
312 }
313 </style>
314 <script>
315 jQuery(document).ready(function($){
316 $("#wpb-goodbye-link-<?php echo esc_attr( $this->plugin_name ); ?>").on("click",function(){
317 // We'll send the user to this deactivation link when they've completed or dismissed the form
318 var url = document.getElementById("wpb-goodbye-link-<?php echo esc_attr( $this->plugin_name ); ?>");
319 $('body').toggleClass('wpb-form-active');
320 $("#wpb-goodbye-form-<?php echo esc_attr( $this->plugin_name ); ?>").fadeIn();
321 $("#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>');
322 $('#wpb-submit-form').on('click', function(e){
323 // As soon as we click, the body of the form should disappear
324 $("#wpb-goodbye-form-<?php echo esc_attr( $this->plugin_name ); ?> .wpb-goodbye-form-body").fadeOut();
325 $("#wpb-goodbye-form-<?php echo esc_attr( $this->plugin_name ); ?> .wpb-goodbye-form-footer").fadeOut();
326 // Fade in spinner
327 $("#wpb-goodbye-form-<?php echo esc_attr( $this->plugin_name ); ?> .deactivating-spinner").fadeIn();
328 e.preventDefault();
329 var values = new Array();
330 $.each($("input[name='wpb-goodbye-options[]']:checked"), function(){
331 values.push($(this).val());
332 });
333 var details = $('#wpb-goodbye-reasons').val();
334 var data = {
335 'action': 'goodbye_form',
336 'values': values,
337 'details': details,
338 'security': "<?php echo wp_create_nonce ( 'wpbot_goodbye_form' ); ?>",
339 'dataType': "json"
340 }
341 $.post(
342 ajaxurl,
343 data,
344 function(response){
345 // Redirect to original deactivation URL
346 window.location.href = url;
347 }
348 );
349 });
350 // If we click outside the form, the form will close
351 $('.wpb-goodbye-form-bg').on('click',function(){
352 $("#wpb-goodbye-form-<?php echo esc_attr( $this->plugin_name ); ?>").fadeOut();
353 $('body').removeClass('wpb-form-active');
354 });
355 });
356 });
357 </script>
358 <?php }
359
360 /**
361 * AJAX callback when the form is submitted
362 * @since 1.0.0
363 */
364 public function goodbye_form_callback() {
365 check_ajax_referer( 'wpbot_goodbye_form', 'security' );
366 if( isset( $_POST['values'] ) ) {
367 $values = json_encode( wp_unslash( $_POST['values'] ) );
368 update_option( 'wpbot_deactivation_reason_' . $this->plugin_name, $values );
369 }
370 if( isset( $_POST['details'] ) ) {
371 $details = sanitize_text_field( $_POST['details'] );
372 update_option( 'wpbot_deactivation_details_' . $this->plugin_name, $details );
373 }
374
375 echo 'success';
376 wp_die();
377 }
378
379 }
380
381 }
382