admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('adaire_deactivation_nonce'), ]); } // Step 1: render the feedback modal markup on the plugins page. public function render_modal() { global $pagenow; if ($pagenow !== 'plugins.php') { return; } ?>
'Unauthorized'], 403); } $feedback = $this->sanitize_feedback_payload($_POST); $default_recipient = defined('ADAIRE_FEEDBACK_EMAIL') ? ADAIRE_FEEDBACK_EMAIL : (function_exists('adaire_blocks_get_feedback_email') ? adaire_blocks_get_feedback_email() : self::DEFAULT_FEEDBACK_EMAIL); if (empty($default_recipient)) { $send_result = [ 'sent' => false, 'provider' => 'sendgrid', 'status' => null, 'error' => 'Feedback email not configured', 'from_email' => null, ]; $this->log_feedback_attempt($feedback, $send_result, ''); wp_send_json_success([ 'sent' => false, 'message' => 'Feedback email not configured', 'log_written' => true, ]); return; } $feedback_recipient = apply_filters('adaire_blocks_deactivation_feedback_to', $default_recipient, $feedback); $subject = 'Adaire Blocks Deactivation Feedback (Free Version)'; $message = $this->build_feedback_message($feedback); $send_result = $this->send_feedback_email($feedback_recipient, $subject, $message, $feedback['email']); $this->log_feedback_attempt($feedback, $send_result, $feedback_recipient); if (!$send_result['sent']) { $payload = ['message' => 'Failed to send feedback email']; if (!empty($send_result['error'])) { $payload['error'] = $send_result['error']; } wp_send_json_error($payload, 500); } wp_send_json_success(['sent' => true]); } // Step 2a: sanitize incoming feedback fields. private function sanitize_feedback_payload(array $raw_feedback_input) { return [ 'reason' => sanitize_text_field($raw_feedback_input['reason'] ?? ''), 'email' => sanitize_email($raw_feedback_input['email'] ?? ''), 'details' => sanitize_textarea_field($raw_feedback_input['details'] ?? ''), 'site' => $this->get_public_plugin_url(), ]; } // Step 2b: build the plain-text email body. private function build_feedback_message(array $feedback) { $reason_text = $feedback['reason'] ?: 'Not provided'; $details_text = $feedback['details'] ?: 'Not provided'; $email_text = $feedback['email'] ?: 'Not provided'; return implode("\n", [ 'A user has deactivated the Adaire Blocks Free plugin.', '', 'Site: ' . $feedback['site'], 'Reason: ' . $reason_text, 'Details: ' . $details_text, 'User Email: ' . $email_text, ]); } // Step 2c: send feedback via SendGrid. private function send_feedback_email($recipient, $subject, $message, $reply_to) { $reply_to_email = $reply_to && is_email($reply_to) ? $reply_to : null; if (!function_exists('adaire_blocks_get_sendgrid_api_key') || !adaire_blocks_get_sendgrid_api_key()) { return [ 'sent' => false, 'provider' => 'sendgrid', 'error' => 'SendGrid is not configured', ]; } $sendgrid_result = adaire_blocks_send_via_sendgrid($recipient, $subject, $message, $reply_to_email); return [ 'sent' => (bool) ($sendgrid_result['sent'] ?? false), 'provider' => $sendgrid_result['provider'] ?? 'sendgrid', 'status' => $sendgrid_result['status'] ?? null, 'error' => $sendgrid_result['error'] ?? null, 'from_email' => $sendgrid_result['from_email'] ?? null, ]; } // Step 2d: store a log entry for troubleshooting. private function log_feedback_attempt(array $feedback, array $send_result, $feedback_recipient) { $existing_logs = get_option('adaire_deact_log', []); if (!is_array($existing_logs)) { $existing_logs = []; } $from_email = $send_result['from_email'] ?? get_option('admin_email'); if (!$from_email || !is_email($from_email)) { $from_email = 'wordpress@example.com'; } $existing_logs[] = [ 'date' => current_time('mysql'), 'reason' => $feedback['reason'], 'email' => $feedback['email'], 'details' => $feedback['details'], 'site' => $feedback['site'], 'mail_sent' => (bool) ($send_result['sent'] ?? false), 'mail_provider' => $send_result['provider'] ?? 'sendgrid', 'mail_status' => $send_result['status'] ?? null, 'mail_error' => $send_result['error'] ?? null, 'to_email' => is_array($feedback_recipient) ? implode(', ', $feedback_recipient) : (string) $feedback_recipient, 'from_email' => $from_email, ]; update_option('adaire_deact_log', $existing_logs); } // Step 2e: get the public plugin URL (WordPress.org listing). private function get_public_plugin_url() { if (!function_exists('get_plugin_data')) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $plugin_data = get_plugin_data(ADAIRE_BLOCKS_PLUGIN_FILE, false, false); return $plugin_data['PluginURI'] ?? get_bloginfo('url'); } }