| 1 |
<?php |
| 2 |
|
| 3 |
namespace QuadLayers\PluginFeedback; |
| 4 |
|
| 5 |
class AjaxHandler |
| 6 |
{ |
| 7 |
|
| 8 |
public static $instance; |
| 9 |
|
| 10 |
private function __construct() |
| 11 |
{ |
| 12 |
add_action('wp_ajax_quadlayers_send_feedback', [self::class, 'send_feedback']); |
| 13 |
} |
| 14 |
|
| 15 |
public static function instance() |
| 16 |
{ |
| 17 |
if (is_null(self::$instance)) { |
| 18 |
self::$instance = new self(); |
| 19 |
} |
| 20 |
return self::$instance; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Sends feedback from the deactivation survey via AJAX. |
| 25 |
*/ |
| 26 |
public static function send_feedback() |
| 27 |
{ |
| 28 |
// Check nonce for security |
| 29 |
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'quadlayers_send_feedback_nonce')) { |
| 30 |
wp_send_json_error(['message' => __('Invalid nonce.', 'wp-plugin-feedback')], 400); |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
// Validate required fields |
| 35 |
$pluginBasename = sanitize_text_field($_POST['plugin_basename'] ?? ''); |
| 36 |
$feedbackReason = sanitize_text_field($_POST['feedback_reason'] ?? ''); |
| 37 |
$feedbackDetails = sanitize_textarea_field($_POST['feedback_details'] ?? ''); |
| 38 |
$isAnonymous = filter_var($_POST['is_anonymous'] ?? false, FILTER_VALIDATE_BOOLEAN); |
| 39 |
$hasFeedback = filter_var($_POST['has_feedback'] ?? false, FILTER_VALIDATE_BOOLEAN); |
| 40 |
|
| 41 |
if (empty($pluginBasename)) { |
| 42 |
wp_send_json_error(['message' => __('Missing required fields.', 'wp-plugin-feedback')], 400); |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
// Create a transient with the hash as the key and set the expiration time to 7 days |
| 47 |
set_transient('ql_plugin_feedback_' . $pluginBasename, true, 7 * DAY_IN_SECONDS); |
| 48 |
|
| 49 |
// Send feedback (handling anonymous option) |
| 50 |
$result = (new Client($pluginBasename))->sendFeedback($feedbackReason, $feedbackDetails, $isAnonymous, $hasFeedback); |
| 51 |
|
| 52 |
if ($result) { |
| 53 |
wp_send_json_success(['message' => __('Feedback submitted successfully.', 'wp-plugin-feedback')]); |
| 54 |
} else { |
| 55 |
wp_send_json_error(['message' => __('Error sending feedback.', 'wp-plugin-feedback')], 500); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|