| 1 |
<?php |
| 2 |
/** |
| 3 |
* Deactivation Survey Class for WPbot Automator |
| 4 |
* |
| 5 |
* @package WPbot_Automator |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPbot_Automator\Core; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Deactivation Survey Handler |
| 16 |
*/ |
| 17 |
class Deactivation_Survey { |
| 18 |
|
| 19 |
/** |
| 20 |
* Main plugin file path |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
private $plugin_file = ''; |
| 25 |
|
| 26 |
/** |
| 27 |
* Plugin name |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
private $plugin_name = 'wpbot-automator'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Destination email for feedback |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
private $home_url = '[email protected]'; |
| 39 |
|
| 40 |
/** |
| 41 |
* Class constructor |
| 42 |
* |
| 43 |
* @param string $plugin_file Main plugin file path. |
| 44 |
*/ |
| 45 |
public function __construct( $plugin_file ) { |
| 46 |
$this->plugin_file = $plugin_file; |
| 47 |
//error_log( 'WPbot Automator - Deactivation_Survey initialized with file: ' . $this->plugin_file ); |
| 48 |
|
| 49 |
// Deactivation hook. |
| 50 |
register_deactivation_hook( $this->plugin_file, array( $this, 'handle_deactivation' ) ); |
| 51 |
|
| 52 |
$this->init(); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Initialize hooks |
| 57 |
*/ |
| 58 |
public function init() { |
| 59 |
global $pagenow; |
| 60 |
$is_admin = is_admin() ? 'Yes' : 'No'; |
| 61 |
//error_log( 'WPbot Automator - Deactivation_Survey init. pagenow: ' . $pagenow . ', is_admin: ' . $is_admin ); |
| 62 |
|
| 63 |
add_filter( 'plugin_action_links', array( $this, 'filter_action_links' ), 20, 2 ); |
| 64 |
add_filter( 'network_admin_plugin_action_links', array( $this, 'filter_action_links' ), 20, 2 ); |
| 65 |
add_action( 'admin_footer', array( $this, 'render_form' ), 20 ); |
| 66 |
add_action( 'wp_ajax_wpbot_automator_goodbye_form', array( $this, 'ajax_callback' ) ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Set email content type to HTML |
| 71 |
* |
| 72 |
* @return string |
| 73 |
*/ |
| 74 |
public function set_content_type() { |
| 75 |
return 'text/html'; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Send deactivation feedback via email |
| 80 |
* |
| 81 |
* @param array $body Feedback data. |
| 82 |
* @return bool |
| 83 |
*/ |
| 84 |
private function send_feedback( $body ) { |
| 85 |
$message = ''; |
| 86 |
foreach ( $body as $key => $value ) { |
| 87 |
$message .= '<p><b>' . esc_html( ucfirst( str_replace( '_', ' ', $key ) ) ) . '</b>: ' . esc_html( $value ) . '</p>'; |
| 88 |
} |
| 89 |
|
| 90 |
$title = 'WPbot Automator - Plugin Deactivation Notice'; |
| 91 |
$headers = array( 'From: Anonymous <[email protected]>' ); |
| 92 |
|
| 93 |
add_filter( 'wp_mail_content_type', array( $this, 'set_content_type' ) ); |
| 94 |
$email = wp_mail( $this->home_url, $title, $message, $headers ); |
| 95 |
remove_filter( 'wp_mail_content_type', array( $this, 'set_content_type' ) ); |
| 96 |
|
| 97 |
return $email; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get plugin data |
| 102 |
* |
| 103 |
* @return array |
| 104 |
*/ |
| 105 |
private function get_plugin_info() { |
| 106 |
if ( ! function_exists( 'get_plugin_data' ) ) { |
| 107 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 108 |
} |
| 109 |
return get_plugin_data( $this->plugin_file ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Handle plugin deactivation |
| 114 |
*/ |
| 115 |
public function handle_deactivation() { |
| 116 |
$plugin_data = $this->get_plugin_info(); |
| 117 |
$body = array( |
| 118 |
'plugin_name' => $plugin_data['Name'], |
| 119 |
'version' => $plugin_data['Version'], |
| 120 |
'status' => 'Deactivated', |
| 121 |
'date' => date( 'Y-m-d H:i:s' ), |
| 122 |
'site_url' => get_site_url(), |
| 123 |
'reason' => get_option( 'wpbot_automator_deactivation_reason' ), |
| 124 |
'feedback_details' => get_option( 'wpbot_automator_deactivation_details' ), |
| 125 |
); |
| 126 |
|
| 127 |
if ( ! empty( $body['reason'] ) || ! empty( $body['feedback_details'] ) ) { |
| 128 |
$this->send_feedback( $body ); |
| 129 |
delete_option( 'wpbot_automator_deactivation_reason' ); |
| 130 |
delete_option( 'wpbot_automator_deactivation_details' ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Add deactivation prompt to plugin action links |
| 136 |
* |
| 137 |
* @param array $links Action links. |
| 138 |
* @param string $file Plugin file slug. |
| 139 |
* @return array |
| 140 |
*/ |
| 141 |
public function filter_action_links( $links, $file ) { |
| 142 |
$my_plugin = plugin_basename( $this->plugin_file ); |
| 143 |
|
| 144 |
// error_log( 'WPbot Automator - filter_action_links: Checking ' . $file ); |
| 145 |
|
| 146 |
if ( $file === $my_plugin && isset( $links['deactivate'] ) ) { |
| 147 |
//error_log( 'WPbot Automator - filter_action_links: MATCH FOUND for ' . $file ); |
| 148 |
$links['deactivate'] = str_replace( |
| 149 |
'<a ', |
| 150 |
'<a onclick="javascript:event.preventDefault();" id="wpbot-automator-deactivate-link" ', |
| 151 |
$links['deactivate'] |
| 152 |
); |
| 153 |
} |
| 154 |
return $links; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Render the deactivation survey markup and script |
| 159 |
*/ |
| 160 |
public function render_form() { |
| 161 |
global $pagenow; |
| 162 |
if ( 'plugins.php' !== $pagenow ) { |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
//error_log( 'WPbot Automator - render_form hitting plugins page footer' ); |
| 167 |
|
| 168 |
$heading = __( 'Sorry to see you go', 'wpbot-automator' ); |
| 169 |
$label = __( 'Please provide some details so we can improve the plugin', 'wpbot-automator' ); |
| 170 |
$submit = __( 'Submit & Deactivate', 'wpbot-automator' ); |
| 171 |
$skip = __( 'Just Deactivate', 'wpbot-automator' ); |
| 172 |
?> |
| 173 |
<div class="wpbot-goodbye-form-bg"></div> |
| 174 |
<div id="wpbot-automator-goodbye-form"></div> |
| 175 |
<style type="text/css"> |
| 176 |
.wpbot-goodbye-active .wpbot-goodbye-form-bg { |
| 177 |
background: rgba(0, 0, 0, .5); |
| 178 |
position: fixed; |
| 179 |
top: 0; |
| 180 |
left: 0; |
| 181 |
width: 100%; |
| 182 |
height: 100%; |
| 183 |
z-index: 9998; |
| 184 |
} |
| 185 |
#wpbot-automator-goodbye-form { display: none; } |
| 186 |
.wpbot-goodbye-active #wpbot-automator-goodbye-form { |
| 187 |
display: block; |
| 188 |
position: fixed; |
| 189 |
max-width: 450px; |
| 190 |
width: 90%; |
| 191 |
background: #fff; |
| 192 |
z-index: 9999; |
| 193 |
top: 50%; |
| 194 |
left: 50%; |
| 195 |
transform: translate(-50%, -50%); |
| 196 |
border-radius: 12px; |
| 197 |
box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04); |
| 198 |
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; |
| 199 |
overflow: hidden; |
| 200 |
} |
| 201 |
.wpbot-goodbye-head { |
| 202 |
background: #4f46e5; |
| 203 |
color: #fff; |
| 204 |
padding: 20px; |
| 205 |
text-align: center; |
| 206 |
font-size: 1.25rem; |
| 207 |
font-weight: 700; |
| 208 |
} |
| 209 |
.wpbot-goodbye-body { padding: 25px; } |
| 210 |
.wpbot-goodbye-body label { |
| 211 |
display: block; |
| 212 |
margin-bottom: 10px; |
| 213 |
font-weight: 500; |
| 214 |
color: #1e293b; |
| 215 |
} |
| 216 |
.wpbot-goodbye-body textarea { |
| 217 |
width: 100%; |
| 218 |
border: 1px solid #e2e8f0; |
| 219 |
border-radius: 8px; |
| 220 |
padding: 12px; |
| 221 |
font-size: 14px; |
| 222 |
margin-bottom: 5px; |
| 223 |
} |
| 224 |
.wpbot-goodbye-footer { |
| 225 |
padding: 20px; |
| 226 |
border-top: 1px solid #f1f5f9; |
| 227 |
display: flex; |
| 228 |
justify-content: space-between; |
| 229 |
align-items: center; |
| 230 |
} |
| 231 |
.wpbot-submit-btn { |
| 232 |
background: #4f46e5; |
| 233 |
color: #fff; |
| 234 |
border: none; |
| 235 |
padding: 10px 20px; |
| 236 |
border-radius: 8px; |
| 237 |
font-weight: 600; |
| 238 |
cursor: pointer; |
| 239 |
text-decoration: none; |
| 240 |
} |
| 241 |
.wpbot-submit-btn:hover { background: #4338ca; } |
| 242 |
.wpbot-skip-btn { |
| 243 |
color: #64748b; |
| 244 |
text-decoration: none; |
| 245 |
font-size: 14px; |
| 246 |
} |
| 247 |
.wpbot-skip-btn:hover { color: #4f46e5; } |
| 248 |
.wpbot-spinner { display: none; text-align: center; padding: 20px; } |
| 249 |
</style> |
| 250 |
<script> |
| 251 |
jQuery(document).ready(function($){ |
| 252 |
$("#wpbot-automator-deactivate-link").on("click", function(e){ |
| 253 |
e.preventDefault(); |
| 254 |
var url = $(this).attr('href'); |
| 255 |
$('body').addClass('wpbot-goodbye-active'); |
| 256 |
|
| 257 |
$("#wpbot-automator-goodbye-form").html(` |
| 258 |
<div class="wpbot-goodbye-head"><?php echo esc_html( $heading ); ?></div> |
| 259 |
<div class="wpbot-goodbye-body"> |
| 260 |
<label><?php echo esc_html( $label ); ?></label> |
| 261 |
<textarea id="wpbot-feedback-details" rows="4"></textarea> |
| 262 |
<div id="wpbot-survey-error" style="color:#ef4444; font-size: 13px; margin-top: 5px;"></div> |
| 263 |
</div> |
| 264 |
<div class="wpbot-spinner"><span class="spinner is-active"></span> Submitting...</div> |
| 265 |
<div class="wpbot-goodbye-footer"> |
| 266 |
<a class="wpbot-skip-btn" href="${url}"><?php echo esc_html( $skip ); ?></a> |
| 267 |
<button id="wpbot-submit-survey" class="wpbot-submit-btn"><?php echo esc_html( $submit ); ?></button> |
| 268 |
</div> |
| 269 |
`); |
| 270 |
|
| 271 |
$("#wpbot-submit-survey").on("click", function(){ |
| 272 |
var details = $("#wpbot-feedback-details").val(); |
| 273 |
if(!details) { |
| 274 |
$("#wpbot-survey-error").text("Please share a quick note to help us improve!"); |
| 275 |
return; |
| 276 |
} |
| 277 |
|
| 278 |
$(".wpbot-goodbye-body, .wpbot-goodbye-footer").hide(); |
| 279 |
$(".wpbot-spinner").show(); |
| 280 |
|
| 281 |
$.post(ajaxurl, { |
| 282 |
action: 'wpbot_automator_goodbye_form', |
| 283 |
details: details, |
| 284 |
nonce: '<?php echo wp_create_nonce( "wpbot_automator_survey" ); ?>' |
| 285 |
}, function() { |
| 286 |
window.location.href = url; |
| 287 |
}); |
| 288 |
}); |
| 289 |
|
| 290 |
$(".wpbot-goodbye-form-bg").on("click", function(){ |
| 291 |
$("body").removeClass("wpbot-goodbye-active"); |
| 292 |
}); |
| 293 |
}); |
| 294 |
}); |
| 295 |
</script> |
| 296 |
<?php |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* AJAX logic for saving survey feedback |
| 301 |
*/ |
| 302 |
public function ajax_callback() { |
| 303 |
check_ajax_referer( 'wpbot_automator_survey', 'nonce' ); |
| 304 |
if ( isset( $_POST['details'] ) ) { |
| 305 |
update_option( 'wpbot_automator_deactivation_reason', 'Feedback provided' ); |
| 306 |
update_option( 'wpbot_automator_deactivation_details', sanitize_text_field( $_POST['details'] ) ); |
| 307 |
} |
| 308 |
wp_send_json_success(); |
| 309 |
} |
| 310 |
} |
| 311 |
|