|
1 <?php |
|
2 /** |
|
3 * Displays the content of the dialog box when the user clicks on the "Deactivate" link on the plugin settings page |
|
4 * @package BestWebSoft |
|
5 * @since 2.1.3 |
|
6 */ |
|
7 if ( ! defined( 'ABSPATH' ) ) |
|
8 exit; |
|
9 |
|
10 /** |
|
11 * Displays a confirmation and feedback dialog box when the user clicks on the "Deactivate" link on the plugins |
|
12 * page. |
|
13 * |
|
14 * @since 2.1.3 |
|
15 */ |
|
16 if ( ! function_exists( 'bws_add_deactivation_feedback_dialog_box' ) ) { |
|
17 function bws_add_deactivation_feedback_dialog_box() { |
|
18 global $bstwbsftwppdtplgns_active_plugins; |
|
19 if ( empty( $bstwbsftwppdtplgns_active_plugins ) ) |
|
20 return; |
|
21 |
|
22 foreach ( $bstwbsftwppdtplgns_active_plugins as $basename => $plugin_data ) { |
|
23 |
|
24 $slug = dirname( $basename ); |
|
25 $plugin_id = sanitize_title( $plugin_data['Name'] ); |
|
26 |
|
27 $contact_support_template = __( 'Need help? We are ready to answer your questions.', 'bestwebsoft' ) . ' <a href="https://support.bestwebsoft.com/hc/en-us/requests/new" target="_blank">' . __( 'Contact Support', 'bestwebsoft' ) . '</a>'; |
|
28 |
|
29 $reasons = array( |
|
30 array( |
|
31 'id' => 'NOT_WORKING', |
|
32 'text' => __( 'The plugin is not working', 'bestwebsoft' ), |
|
33 'input_type' => 'textarea', |
|
34 'input_placeholder' => __( "Kindly share what didn't work so we can fix it in future updates...", 'bestwebsoft' ) |
|
35 ), |
|
36 array( |
|
37 'id' => 'DIDNT_WORK_AS_EXPECTED', |
|
38 'text' => __( "The plugin didn't work as expected", 'bestwebsoft' ), |
|
39 'input_type' => 'textarea', |
|
40 'input_placeholder' => __( 'What did you expect?', 'bestwebsoft' ) |
|
41 ), |
|
42 array( |
|
43 'id' => 'SUDDENLY_STOPPED_WORKING', |
|
44 'text' => __( 'The plugin suddenly stopped working', 'bestwebsoft' ), |
|
45 'input_type' => '', |
|
46 'input_placeholder' => '', |
|
47 'internal_message' => $contact_support_template |
|
48 ), |
|
49 array( |
|
50 'id' => 'BROKE_MY_SITE', |
|
51 'text' => __( 'The plugin broke my site', 'bestwebsoft' ), |
|
52 'input_type' => '', |
|
53 'input_placeholder' => '', |
|
54 'internal_message' => $contact_support_template |
|
55 ), |
|
56 array( |
|
57 'id' => 'COULDNT_MAKE_IT_WORK', |
|
58 'text' => __( "I couldn't understand how to get it work", 'bestwebsoft' ), |
|
59 'input_type' => '', |
|
60 'input_placeholder' => '', |
|
61 'internal_message' => $contact_support_template |
|
62 ), |
|
63 array( |
|
64 'id' => 'FOUND_A_BETTER_PLUGIN', |
|
65 'text' => __( 'I found a better plugin', 'bestwebsoft' ), |
|
66 'input_type' => 'textfield', |
|
67 'input_placeholder' => __( "What's the plugin name?", 'bestwebsoft' ) |
|
68 ), |
|
69 array( |
|
70 'id' => 'GREAT_BUT_NEED_SPECIFIC_FEATURE', |
|
71 'text' => __( "The plugin is great, but I need specific feature that you don't support", 'bestwebsoft' ), |
|
72 'input_type' => 'textarea', |
|
73 'input_placeholder' => __( 'What feature?', 'bestwebsoft' ) |
|
74 ), |
|
75 array( |
|
76 'id' => 'NO_LONGER_NEEDED', |
|
77 'text' => __( 'I no longer need the plugin', 'bestwebsoft' ), |
|
78 'input_type' => '', |
|
79 'input_placeholder' => '' |
|
80 ), |
|
81 array( |
|
82 'id' => 'TEMPORARY_DEACTIVATION', |
|
83 'text' => __( "It's a temporary deactivation, I'm just debugging an issue", 'bestwebsoft' ), |
|
84 'input_type' => '', |
|
85 'input_placeholder' => '' |
|
86 ), |
|
87 array( |
|
88 'id' => 'OTHER', |
|
89 'text' => __( 'Other', 'bestwebsoft' ), |
|
90 'input_type' => 'textfield', |
|
91 'input_placeholder' => '' |
|
92 ) |
|
93 ); |
|
94 |
|
95 $reasons_list_items_html = ''; |
|
96 |
|
97 foreach ( $reasons as $reason ) { |
|
98 $list_item_classes = 'bws-modal-reason' . ( ! empty( $reason['input_type'] ) ? ' has-input' : '' ); |
|
99 |
|
100 if ( ! empty( $reason['internal_message'] ) ) { |
|
101 $list_item_classes .= ' has-internal-message'; |
|
102 $reason_internal_message = $reason['internal_message']; |
|
103 } else { |
|
104 $reason_internal_message = ''; |
|
105 } |
|
106 |
|
107 $reasons_list_items_html .= '<li class="' . $list_item_classes . '" data-input-type="' . $reason['input_type'] . '" data-input-placeholder="' . $reason['input_placeholder'] . '"> |
|
108 <label> |
|
109 <span> |
|
110 <input type="radio" name="selected-reason" value="' . $reason['id'] . '"/> |
|
111 </span> |
|
112 <span>' . $reason['text'] . '</span> |
|
113 </label> |
|
114 <div class="bws-modal-internal-message">' . $reason_internal_message . '</div> |
|
115 </li>'; |
|
116 } ?> |
|
117 <script type="text/javascript"> |
|
118 (function($) { |
|
119 var modalHtml = |
|
120 '<div class="bws-modal bws-modal-deactivation-feedback">' |
|
121 + ' <div class="bws-modal-dialog">' |
|
122 + ' <div class="bws-modal-body">' |
|
123 + ' <h2><?php _e( 'Quick Feedback', 'bestwebsoft' ); ?></h2>' |
|
124 + ' <div class="bws-modal-panel active"><p><?php _e( 'If you have a moment, please let us know why you are deactivating', 'bestwebsoft' ); ?>:</p><ul>' + <?php echo json_encode( $reasons_list_items_html ); ?> + '</ul>' |
|
125 + ' <label class="bws-modal-anonymous-label">' |
|
126 + ' <input type="checkbox" checked="checked" />' |
|
127 + ' <?php _e( 'Send website data and allow to contact me back', 'bestwebsoft' ); ?>' |
|
128 + ' </label>' |
|
129 + ' </div>' |
|
130 + ' </div>' |
|
131 + ' <div class="bws-modal-footer">' |
|
132 + ' <a href="#" class="button button-secondary bws-modal-button-close"><?php _e( 'Cancel', 'bestwebsoft' ); ?></a>' |
|
133 + ' <a href="#" class="button button-primary bws-modal-button-deactivate"></a>' |
|
134 + ' <div class="clear"></div>' |
|
135 + ' </div>' |
|
136 + ' </div>' |
|
137 + '</div>', |
|
138 $modal = $( modalHtml ), |
|
139 $deactivateLink = $( '#the-list .active[data-plugin="<?php echo $basename; ?>"] .deactivate a' ), |
|
140 $anonymousFeedback = $modal.find( '.bws-modal-anonymous-label' ), |
|
141 selectedReasonID = false; |
|
142 |
|
143 /* WP added data-plugin attr after 4.5 version/ In prev version was id attr */ |
|
144 if ( 0 == $deactivateLink.length ) |
|
145 $deactivateLink = $( '#the-list .active#<?php echo $plugin_id; ?> .deactivate a' ); |
|
146 |
|
147 $modal.appendTo( $( 'body' ) ); |
|
148 |
|
149 BwsModalRegisterEventHandlers(); |
|
150 |
|
151 function BwsModalRegisterEventHandlers() { |
|
152 $deactivateLink.click( function( evt ) { |
|
153 evt.preventDefault(); |
|
154 |
|
155 /* Display the dialog box.*/ |
|
156 BwsModalReset(); |
|
157 $modal.addClass( 'active' ); |
|
158 $( 'body' ).addClass( 'has-bws-modal' ); |
|
159 }); |
|
160 |
|
161 $modal.on( 'input propertychange', '.bws-modal-reason-input input', function() { |
|
162 if ( ! BwsModalIsReasonSelected( 'OTHER' ) ) { |
|
163 return; |
|
164 } |
|
165 |
|
166 var reason = $( this ).val().trim(); |
|
167 |
|
168 /* If reason is not empty, remove the error-message class of the message container to change the message color back to default. */ |
|
169 if ( reason.length > 0 ) { |
|
170 $modal.find( '.message' ).removeClass( 'error-message' ); |
|
171 BwsModalEnableDeactivateButton(); |
|
172 } |
|
173 }); |
|
174 |
|
175 $modal.on( 'blur', '.bws-modal-reason-input input', function() { |
|
176 var $userReason = $( this ); |
|
177 |
|
178 setTimeout( function() { |
|
179 if ( ! BwsModalIsReasonSelected( 'OTHER' ) ) { |
|
180 return; |
|
181 } |
|
182 |
|
183 /* If reason is empty, add the error-message class to the message container to change the message color to red. */ |
|
184 if ( 0 === $userReason.val().trim().length ) { |
|
185 $modal.find( '.message' ).addClass( 'error-message' ); |
|
186 BwsModalDisableDeactivateButton(); |
|
187 } |
|
188 }, 150 ); |
|
189 }); |
|
190 |
|
191 $modal.on( 'click', '.bws-modal-footer .button', function( evt ) { |
|
192 evt.preventDefault(); |
|
193 |
|
194 if ( $( this ).hasClass( 'disabled' ) ) { |
|
195 return; |
|
196 } |
|
197 |
|
198 var _parent = $( this ).parents( '.bws-modal:first' ), |
|
199 _this = $( this ); |
|
200 |
|
201 if ( _this.hasClass( 'allow-deactivate' ) ) { |
|
202 var $radio = $modal.find( 'input[type="radio"]:checked' ); |
|
203 |
|
204 if ( 0 === $radio.length ) { |
|
205 /* If no selected reason, just deactivate the plugin. */ |
|
206 window.location.href = $deactivateLink.attr( 'href' ); |
|
207 return; |
|
208 } |
|
209 |
|
210 var $selected_reason = $radio.parents( 'li:first' ), |
|
211 $input = $selected_reason.find( 'textarea, input[type="text"]' ), |
|
212 userReason = ( 0 !== $input.length ) ? $input.val().trim() : ''; |
|
213 |
|
214 if ( BwsModalIsReasonSelected( 'OTHER' ) && '' === userReason ) { |
|
215 return; |
|
216 } |
|
217 |
|
218 var is_anonymous = ( $anonymousFeedback.find( 'input' ).is( ':checked' ) ) ? 0 : 1; |
|
219 |
|
220 $.ajax({ |
|
221 url : ajaxurl, |
|
222 method : 'POST', |
|
223 data : { |
|
224 'action' : 'bws_submit_uninstall_reason_action', |
|
225 'plugin' : '<?php echo $basename; ?>', |
|
226 'reason_id' : $radio.val(), |
|
227 'reason_info' : userReason, |
|
228 'is_anonymous' : is_anonymous, |
|
229 'bws_ajax_nonce' : '<?php echo wp_create_nonce( 'bws_ajax_nonce' ); ?>' |
|
230 }, |
|
231 beforeSend: function() { |
|
232 _parent.find( '.bws-modal-footer .button' ).addClass( 'disabled' ); |
|
233 _parent.find( '.bws-modal-footer .button-secondary' ).text( '<?php _e( 'Processing', 'bestwebsoft' ); ?>' + '...' ); |
|
234 }, |
|
235 complete : function( message ) { |
|
236 /* Do not show the dialog box, deactivate the plugin. */ |
|
237 window.location.href = $deactivateLink.attr( 'href' ); |
|
238 } |
|
239 }); |
|
240 } else if ( _this.hasClass( 'bws-modal-button-deactivate' ) ) { |
|
241 /* Change the Deactivate button's text and show the reasons panel. */ |
|
242 _parent.find( '.bws-modal-button-deactivate' ).addClass( 'allow-deactivate' ); |
|
243 BwsModalShowPanel(); |
|
244 } |
|
245 }); |
|
246 |
|
247 $modal.on( 'click', 'input[type="radio"]', function() { |
|
248 var $selectedReasonOption = $( this ); |
|
249 |
|
250 /* If the selection has not changed, do not proceed. */ |
|
251 if ( selectedReasonID === $selectedReasonOption.val() ) |
|
252 return; |
|
253 |
|
254 selectedReasonID = $selectedReasonOption.val(); |
|
255 |
|
256 $anonymousFeedback.show(); |
|
257 |
|
258 var _parent = $( this ).parents( 'li:first' ); |
|
259 |
|
260 $modal.find( '.bws-modal-reason-input' ).remove(); |
|
261 $modal.find( '.bws-modal-internal-message' ).hide(); |
|
262 $modal.find( '.bws-modal-button-deactivate' ).text( '<?php _e( 'Submit & Deactivate', 'bestwebsoft' ); ?>' ); |
|
263 |
|
264 BwsModalEnableDeactivateButton(); |
|
265 |
|
266 if ( _parent.hasClass( 'has-internal-message' ) ) { |
|
267 _parent.find( '.bws-modal-internal-message' ).show(); |
|
268 } |
|
269 |
|
270 if (_parent.hasClass('has-input')) { |
|
271 var reasonInputHtml = '<div class="bws-modal-reason-input"><span class="message"></span>' + ( ( 'textfield' === _parent.data( 'input-type' ) ) ? '<input type="text" />' : '<textarea rows="5" maxlength="200"></textarea>' ) + '</div>'; |
|
272 |
|
273 _parent.append( $( reasonInputHtml ) ); |
|
274 _parent.find( 'input, textarea' ).attr( 'placeholder', _parent.data( 'input-placeholder' ) ).focus(); |
|
275 |
|
276 if ( BwsModalIsReasonSelected( 'OTHER' ) ) { |
|
277 $modal.find( '.message' ).text( '<?php _e( 'Please tell us the reason so we can improve it.', 'bestwebsoft' ); ?>' ).show(); |
|
278 BwsModalDisableDeactivateButton(); |
|
279 } |
|
280 } |
|
281 }); |
|
282 |
|
283 /* If the user has clicked outside the window, cancel it. */ |
|
284 $modal.on( 'click', function( evt ) { |
|
285 var $target = $( evt.target ); |
|
286 |
|
287 /* If the user has clicked anywhere in the modal dialog, just return. */ |
|
288 if ( $target.hasClass( 'bws-modal-body' ) || $target.hasClass( 'bws-modal-footer' ) ) { |
|
289 return; |
|
290 } |
|
291 |
|
292 /* If the user has not clicked the close button and the clicked element is inside the modal dialog, just return. */ |
|
293 if ( ! $target.hasClass( 'bws-modal-button-close' ) && ( $target.parents( '.bws-modal-body' ).length > 0 || $target.parents( '.bws-modal-footer' ).length > 0 ) ) { |
|
294 return; |
|
295 } |
|
296 |
|
297 /* Close the modal dialog */ |
|
298 $modal.removeClass( 'active' ); |
|
299 $( 'body' ).removeClass( 'has-bws-modal' ); |
|
300 |
|
301 return false; |
|
302 }); |
|
303 } |
|
304 |
|
305 function BwsModalIsReasonSelected( reasonID ) { |
|
306 /* Get the selected radio input element.*/ |
|
307 return ( reasonID == $modal.find('input[type="radio"]:checked').val() ); |
|
308 } |
|
309 |
|
310 function BwsModalReset() { |
|
311 selectedReasonID = false; |
|
312 |
|
313 BwsModalEnableDeactivateButton(); |
|
314 |
|
315 /* Uncheck all radio buttons.*/ |
|
316 $modal.find( 'input[type="radio"]' ).prop( 'checked', false ); |
|
317 |
|
318 /* Remove all input fields ( textfield, textarea ).*/ |
|
319 $modal.find( '.bws-modal-reason-input' ).remove(); |
|
320 |
|
321 $modal.find( '.message' ).hide(); |
|
322 |
|
323 $anonymousFeedback.find( 'input' ).prop( 'checked', true ); |
|
324 |
|
325 /* Hide, since by default there is no selected reason.*/ |
|
326 $anonymousFeedback.hide(); |
|
327 |
|
328 var $deactivateButton = $modal.find( '.bws-modal-button-deactivate' ); |
|
329 |
|
330 $deactivateButton.addClass( 'allow-deactivate' ); |
|
331 BwsModalShowPanel(); |
|
332 } |
|
333 |
|
334 function BwsModalEnableDeactivateButton() { |
|
335 $modal.find( '.bws-modal-button-deactivate' ).removeClass( 'disabled' ); |
|
336 } |
|
337 |
|
338 function BwsModalDisableDeactivateButton() { |
|
339 $modal.find( '.bws-modal-button-deactivate' ).addClass( 'disabled' ); |
|
340 } |
|
341 |
|
342 function BwsModalShowPanel() { |
|
343 $modal.find( '.bws-modal-panel' ).addClass( 'active' ); |
|
344 /* Update the deactivate button's text */ |
|
345 $modal.find( '.bws-modal-button-deactivate' ).text( '<?php _e( 'Deactivate', 'bestwebsoft' ); ?>' ); |
|
346 } |
|
347 })(jQuery); |
|
348 </script> |
|
349 <?php } |
|
350 } |
|
351 } |
|
352 |
|
353 /** |
|
354 * Called after the user has submitted his reason for deactivating the plugin. |
|
355 * |
|
356 * @since 2.1.3 |
|
357 */ |
|
358 if ( ! function_exists( 'bws_submit_uninstall_reason_action' ) ) { |
|
359 function bws_submit_uninstall_reason_action() { |
|
360 global $bstwbsftwppdtplgns_options, $wp_version, $bstwbsftwppdtplgns_active_plugins, $current_user; |
|
361 |
|
362 wp_verify_nonce( $_REQUEST['bws_ajax_nonce'], 'bws_ajax_nonce' ); |
|
363 |
|
364 $reason_id = isset( $_REQUEST['reason_id'] ) ? stripcslashes( esc_html( $_REQUEST['reason_id'] ) ) : ''; |
|
365 $basename = isset( $_REQUEST['plugin'] ) ? stripcslashes( esc_html( $_REQUEST['plugin'] ) ) : ''; |
|
366 |
|
367 if ( empty( $reason_id ) || empty( $basename ) ) { |
|
368 exit; |
|
369 } |
|
370 |
|
371 $reason_info = isset( $_REQUEST['reason_info'] ) ? stripcslashes( esc_html( $_REQUEST['reason_info'] ) ) : ''; |
|
372 if ( ! empty( $reason_info ) ) { |
|
373 $reason_info = substr( $reason_info, 0, 255 ); |
|
374 } |
|
375 $is_anonymous = isset( $_REQUEST['is_anonymous'] ) && 1 == $_REQUEST['is_anonymous']; |
|
376 |
|
377 $options = array( |
|
378 'product' => $basename, |
|
379 'reason_id' => $reason_id, |
|
380 'reason_info' => $reason_info, |
|
381 ); |
|
382 |
|
383 if ( ! $is_anonymous ) { |
|
384 if ( ! isset( $bstwbsftwppdtplgns_options ) ) |
|
385 $bstwbsftwppdtplgns_options = ( is_multisite() ) ? get_site_option( 'bstwbsftwppdtplgns_options' ) : get_option( 'bstwbsftwppdtplgns_options' ); |
|
386 |
|
387 if ( ! empty( $bstwbsftwppdtplgns_options['track_usage']['usage_id'] ) ) { |
|
388 $options['usage_id'] = $bstwbsftwppdtplgns_options['track_usage']['usage_id']; |
|
389 } else { |
|
390 $options['usage_id'] = false; |
|
391 $options['url'] = get_bloginfo( 'url' ); |
|
392 $options['wp_version'] = $wp_version; |
|
393 $options['is_active'] = false; |
|
394 $options['version'] = $bstwbsftwppdtplgns_active_plugins[ $basename ]['Version']; |
|
395 } |
|
396 |
|
397 $options['email'] = $current_user->data->user_email; |
|
398 } |
|
399 |
|
400 /* send data */ |
|
401 $raw_response = wp_remote_post( 'http://bestwebsoft.com/wp-content/plugins/products-statistics/deactivation-feedback/', array( |
|
402 'method' => 'POST', |
|
403 'body' => $options, |
|
404 'timeout' => 15, |
|
405 ) ); |
|
406 |
|
407 if ( ! is_wp_error( $raw_response ) && 200 == wp_remote_retrieve_response_code( $raw_response ) ) { |
|
408 if ( ! $is_anonymous ) { |
|
409 $response = maybe_unserialize( wp_remote_retrieve_body( $raw_response ) ); |
|
410 |
|
411 if ( is_array( $response ) && ! empty( $response['usage_id'] ) && $response['usage_id'] != $options['usage_id'] ) { |
|
412 $bstwbsftwppdtplgns_options['track_usage']['usage_id'] = $response['usage_id']; |
|
413 |
|
414 if ( is_multisite() ) |
|
415 update_site_option( 'bstwbsftwppdtplgns_options', $bstwbsftwppdtplgns_options ); |
|
416 else |
|
417 update_option( 'bstwbsftwppdtplgns_options', $bstwbsftwppdtplgns_options ); |
|
418 } |
|
419 } |
|
420 |
|
421 echo 'done'; |
|
422 } else { |
|
423 echo $response->get_error_code() . ': ' . $response->get_error_message(); |
|
424 } |
|
425 exit; |
|
426 } |
|
427 } |
|
428 |
|
429 add_action( 'wp_ajax_bws_submit_uninstall_reason_action', 'bws_submit_uninstall_reason_action' ); |