| 1 |
<?php |
| 2 |
namespace UninstallerForm\Api; |
| 3 |
|
| 4 |
use WP_REST_Request; |
| 5 |
|
| 6 |
/** |
| 7 |
* Feedback controller for the uninstaller form. |
| 8 |
* |
| 9 |
* @since 1.0.0 |
| 10 |
* |
| 11 |
* @package UNINSTALLER_FORM |
| 12 |
*/ |
| 13 |
class FeedbackController { |
| 14 |
protected $plugin_file; |
| 15 |
protected $plugin_text_domain; |
| 16 |
protected $plugin_name; |
| 17 |
protected $plugin_slug; |
| 18 |
|
| 19 |
/** |
| 20 |
* Store namespace |
| 21 |
* |
| 22 |
* @since 1.0.0 |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
protected $namespace; |
| 27 |
|
| 28 |
/** |
| 29 |
* Store rest base |
| 30 |
* |
| 31 |
* @since 1.0.0 |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
protected $rest_base = 'feedback'; |
| 36 |
|
| 37 |
/** |
| 38 |
* Store webhook |
| 39 |
* |
| 40 |
* @since 1.0.0 |
| 41 |
* |
| 42 |
* @var string |
| 43 |
*/ |
| 44 |
protected $webhook; |
| 45 |
|
| 46 |
/** |
| 47 |
* FeedbackController Constructor. |
| 48 |
* |
| 49 |
* @param string $plugin_file The path to the plugin file. |
| 50 |
* @param string $plugin_text_domain The text domain of the plugin. |
| 51 |
* @param string $plugin_name The name of the plugin. |
| 52 |
* @param string $plugin_slug The slug of the plugin. |
| 53 |
* |
| 54 |
* @since 1.0.0 |
| 55 |
*/ |
| 56 |
public function __construct($plugin_file, $plugin_text_domain, $plugin_name, $plugin_slug,$webhook='') { |
| 57 |
$this->plugin_file = $plugin_file; |
| 58 |
$this->plugin_text_domain = $plugin_text_domain; |
| 59 |
$this->plugin_name = $plugin_name; |
| 60 |
$this->plugin_slug = $plugin_slug; |
| 61 |
$this->namespace = $plugin_slug . '/v1'; |
| 62 |
$this->webhook = $webhook; |
| 63 |
$this->register_routes(); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Register REST routes for the feedback controller. |
| 68 |
* |
| 69 |
* @since 1.0.0 |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
protected function register_routes() { |
| 74 |
register_rest_route($this->namespace, $this->rest_base, [ |
| 75 |
'methods' => 'POST', |
| 76 |
'callback' => [$this, 'handle_feedback'], |
| 77 |
'permission_callback' => function () { |
| 78 |
return current_user_can('manage_options'); |
| 79 |
}, |
| 80 |
]); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Handle feedback submission. |
| 85 |
* |
| 86 |
* @since 1.0.0 |
| 87 |
* |
| 88 |
* @param WP_REST_Request $request The request object. |
| 89 |
* @return WP_REST_Response The response object. |
| 90 |
*/ |
| 91 |
public function handle_feedback(WP_REST_Request $request) { |
| 92 |
$nonce = $request->get_header('X-WP-Nonce'); |
| 93 |
if (! wp_verify_nonce($nonce, 'wp_rest')) { |
| 94 |
return rest_ensure_response([ |
| 95 |
'status_code' => 403, |
| 96 |
'success' => 0, |
| 97 |
'message' => 'Invalid nonce. Unauthorized request.', |
| 98 |
]); |
| 99 |
} |
| 100 |
|
| 101 |
$data = $request->get_json_params(); |
| 102 |
$feedback = ! empty($data['feedback']) ? sanitize_text_field($data['feedback']) : 'No feedback'; |
| 103 |
$reasons = ! empty($data['reasons']) ? sanitize_text_field($data['reasons']) : 'No reasons'; |
| 104 |
$theme_name = ! empty($data['theme_name']) ? sanitize_text_field($data['theme_name']) : ''; |
| 105 |
$is_agree = isset( $data['is_agree'] ) && filter_var( $data['is_agree'], FILTER_VALIDATE_BOOLEAN ); |
| 106 |
|
| 107 |
$current_user = wp_get_current_user(); |
| 108 |
$user_name = $current_user->exists() ? $current_user->display_name : 'Guest'; |
| 109 |
$user_email = $current_user->exists() ? $current_user->user_email : ''; |
| 110 |
|
| 111 |
$customer_name = ! empty($data['customer_name']) ? sanitize_text_field($data['customer_name']) : $user_name; |
| 112 |
$customer_email = ! empty($data['customer_email']) ? sanitize_text_field($data['customer_email']) : 'Not Given'; |
| 113 |
|
| 114 |
// if (! $this->verify_email_status($customer_email)) { |
| 115 |
// $customer_email = ''; |
| 116 |
// } |
| 117 |
|
| 118 |
// Get current user info |
| 119 |
|
| 120 |
|
| 121 |
try { |
| 122 |
if (! empty($customer_email) && ! empty($reasons)) { |
| 123 |
|
| 124 |
$feedback_response = wp_remote_post('https://products.arraytics.com/feedback/wp-json/afp/v1/feedback', [ |
| 125 |
'method' => 'POST', |
| 126 |
'headers' => [ |
| 127 |
'Content-Type' => 'application/json', |
| 128 |
], |
| 129 |
'body' => json_encode([ |
| 130 |
'customer_name' => $customer_name, |
| 131 |
'feedback' => $feedback, |
| 132 |
'customer_email' => $customer_email, |
| 133 |
'plugin_name' => $this->plugin_name, |
| 134 |
'theme' => $theme_name, |
| 135 |
'reason' => explode(',',$reasons), |
| 136 |
'is_agree' => $is_agree |
| 137 |
]), |
| 138 |
]); |
| 139 |
|
| 140 |
//Send data through webhook |
| 141 |
|
| 142 |
if(empty($this->webhook)){ |
| 143 |
$this->webhook = "https://themewinter.com/?fluentcrm=1&route=contact&hash=50d358fa-e039-4459-a3d0-ef73b3c7d451"; |
| 144 |
} |
| 145 |
$body = [ |
| 146 |
'customer_name' => $customer_name, |
| 147 |
'email' => $customer_email, |
| 148 |
'plugin_name' => $this->plugin_name, |
| 149 |
'reason' => $reasons, |
| 150 |
'feedback' => $feedback, |
| 151 |
'theme_name' => $theme_name, |
| 152 |
]; |
| 153 |
$webhook_response = wp_remote_post($this->webhook, ['body' => $body]); |
| 154 |
} |
| 155 |
} catch (\Exception $e) { |
| 156 |
return rest_ensure_response([ |
| 157 |
'status_code' => 500, |
| 158 |
'success' => 0, |
| 159 |
'message' => 'Unable to store feedback.', |
| 160 |
]); |
| 161 |
} |
| 162 |
|
| 163 |
return rest_ensure_response([ |
| 164 |
'status_code' => 200, |
| 165 |
'success' => 1, |
| 166 |
'message' => 'Feedback saved successfully.', |
| 167 |
]); |
| 168 |
} |
| 169 |
|
| 170 |
public function verify_email_status(string $email = "") { |
| 171 |
$api_key = '700tpaQtc06FcqN93Ljkoibz6oo76KWk'; // Replace with your actual API key |
| 172 |
$url = 'https://emailverifier.reoon.com/api/v1/verify'; |
| 173 |
|
| 174 |
$response = wp_remote_get(add_query_arg([ |
| 175 |
'email' => $email, |
| 176 |
'key' => $api_key, |
| 177 |
'mode' => 'quick', |
| 178 |
], $url)); |
| 179 |
|
| 180 |
if (is_wp_error($response)) { |
| 181 |
return 'error'; |
| 182 |
} |
| 183 |
|
| 184 |
$body = wp_remote_retrieve_body($response); |
| 185 |
$data = json_decode($body, true); |
| 186 |
|
| 187 |
return isset($data['status']) && $data['status'] === "valid"; |
| 188 |
} |
| 189 |
} |