-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathGeneric_Plugin_Survey.php
210 lines (182 loc) · 4.8 KB
/
Generic_Plugin_Survey.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* File: Generic_Plugin_Survey.php
*
* @since 2.8.3
*
* @package W3TC
*/
namespace W3TC;
/**
* Class Generic_Plugin_Survey
*
* @since 2.8.3
*
* phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore
*/
class Generic_Plugin_Survey {
/**
* Config
*
* @since 2.8.3
*
* @var Config
*/
private $_config;
/**
* W3TC Pro license key.
*
* @since 2.8.3
*
* @var string
*/
private $license_key = '';
/**
* W3TC Pro licensed home URL.
*
* @since 2.8.3
*
* @var string
*/
private $home_url;
/**
* W3TC Pro licensed product item name.
*
* @since 2.8.3
*
* @var string
*/
private $item_name = '';
/**
* Constructor
*
* @since 2.8.3
*
* @return void
*/
public function __construct() {
$this->_config = Dispatcher::config();
if ( Util_Environment::is_w3tc_pro( $this->_config ) ) {
$this->license_key = $this->_config->get_string( 'plugin.license_key' );
$this->item_name = W3TC_PURCHASE_PRODUCT_NAME;
}
$this->home_url = network_home_url();
}
/**
* Runs plugin
*
* @since 2.8.3
*
* @return void
*/
public function run() {
add_action( 'w3tc_ajax_exit_survey_render', array( $this, 'w3tc_ajax_exit_survey_render' ) );
add_action( 'w3tc_ajax_exit_survey_submit', array( $this, 'w3tc_ajax_exit_survey_submit' ) );
add_action( 'w3tc_ajax_exit_survey_skip', array( $this, 'w3tc_ajax_exit_survey_skip' ) );
}
/**
* Renders the exit survey lightbox content
*
* @since 2.8.3
*
* @return void
*/
public function w3tc_ajax_exit_survey_render() {
if ( ! \user_can( \get_current_user_id(), 'manage_options' ) ) {
return;
}
// Verify nonce.
if ( ! wp_verify_nonce( Util_Request::get_string( '_wpnonce' ), 'w3tc' ) ) {
wp_send_json_error( array( 'message' => 'Invalid nonce.' ) );
}
include W3TC_INC_DIR . '/lightbox/exit_survey.php';
}
/**
* Processes the exit survey submission and sends it to the API.
*
* @since 2.8.3
*
* @return void
*/
public function w3tc_ajax_exit_survey_submit() {
if ( ! \user_can( \get_current_user_id(), 'manage_options' ) ) {
return;
}
// Verify nonce.
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( Util_Request::get_string( '_wpnonce' ), 'w3tc' ) ) {
wp_send_json_error( array( 'message' => 'Invalid nonce.' ) );
}
// Collect survey data.
$uninstall_reason = sanitize_text_field( Util_Request::get_string( 'reason' ) );
$email = sanitize_email( Util_Request::get_string( 'email' ) );
$other = sanitize_text_field( Util_Request::get_string( 'other' ) );
$remove_data = sanitize_text_field( Util_Request::get_string( 'remove' ) );
// Prepare the data to send to the API.
$data = array(
'type' => 'exit',
'license_key' => $this->license_key,
'home_url' => $this->home_url,
'item_name' => $this->item_name,
'reason' => $uninstall_reason,
);
// Add 'email' to $data only if the $email is non-blank.
if ( ! empty( $email ) ) {
$data['email'] = $email;
}
// Add 'other' to $data only $other is non-blank.
if ( ! empty( $other ) ) {
$data['other'] = $other;
}
if ( Util_Environment::is_pro_constant( $this->_config ) ) {
$data['pro_c'] = 1;
}
// Send the data to your API server using wp_remote_post.
$response = wp_remote_post(
Util_Environment::get_api_base_url() . '/surveys',
array(
'method' => 'POST',
'headers' => array(
'Content-Type' => 'application/json',
),
'body' => wp_json_encode( $data ),
)
);
// Check the API response.
if ( is_wp_error( $response ) ) {
wp_send_json_error( array( 'message' => 'Failed to submit data to the API.' ) );
}
// Handle API response.
$response_body = wp_remote_retrieve_body( $response );
$api_response = json_decode( $response_body );
if ( $api_response && 201 === wp_remote_retrieve_response_code( $response ) ) {
if ( 'yes' === $remove_data ) {
update_option( 'w3tc_remove_data', true );
}
wp_send_json_success( array( 'message' => 'Thank you for your feedback!' ) );
} else {
wp_send_json_error( array( 'message' => 'API error: ' . $api_response->message ) );
}
}
/**
* Skips the exit survey and processes removing plugin data.
*
* @since 2.8.8
*
* @return void
*/
public function w3tc_ajax_exit_survey_skip() {
if ( ! \user_can( \get_current_user_id(), 'manage_options' ) ) {
return;
}
// Verify nonce.
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( Util_Request::get_string( '_wpnonce' ), 'w3tc' ) ) {
wp_send_json_error( array( 'message' => 'Invalid nonce.' ) );
}
// Collect remove data flag.
$remove_data = Util_Request::get_string( 'remove' );
if ( 'yes' === $remove_data ) {
update_option( 'w3tc_remove_data', true );
wp_send_json_success( array( 'message' => 'Plugin data will be removed!' ) );
}
}
}