PluginProbe
Hostinger Tools / 2.1.1
Hostinger Tools v2.1.1
3.0.77 3.0.76 3.0.75 3.0.74 3.0.73 3.0.72 3.0.71 3.0.70 3.0.69 3.0.68 3.0.67 3.0.66 1.8.1 1.8.2 1.8.3 1.9.1 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.4 All 108 releases
hostinger / includes / admin / class-hostinger-admin-ajax.php

class-hostinger-admin-ajax.php in Hostinger Tools 2.1.1, at includes/admin/class-hostinger-admin-ajax.php

299 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 defined( 'ABSPATH' ) || exit;
4
5 class Hostinger_Admin_Ajax {
6 private const PROMOTIONAL_BANNER_TRANSIENT = 'hts_hide_promotional_banner_transient';
7 private const HIDE_SURVEY_TRANSIENT = 'hts_hide_survey';
8 private const HIDE_OMNISEND_NOTICE = 'hts_omnisend_notice_hidden';
9 private const TWO_DAYS = 86400 * 2;
10 private const THIRTY_DAYS = 86400 * 30;
11
12 private Hostinger_Config $config_handler;
13 private Hostinger_Settings $settings;
14 private Hostinger_Helper $helper;
15 private Hostinger_Surveys_Questions $survey_questions;
16 private Hostinger_Surveys_Rest $surveys_rest;
17
18 public function __construct() {
19 $this->settings = new Hostinger_Settings();
20 $this->helper = new Hostinger_Helper();
21 $this->config_handler = new Hostinger_Config();
22
23 if ( ! empty( Hostinger_Helper::get_api_token() ) ) {
24 $this->survey_questions = new Hostinger_Surveys_Questions();
25 $client = new Hostinger_Requests_Client(
26 $this->config_handler->get_config_value( 'base_rest_uri', HOSTINGER_REST_URI ),
27 array(
28 Hostinger_Config::TOKEN_HEADER => $this->helper::get_api_token(),
29 Hostinger_Config::DOMAIN_HEADER => $this->helper->get_host_info(),
30 )
31 );
32
33 $this->surveys_rest = new Hostinger_Surveys_Rest( $client );
34 }
35
36 add_action( 'init', array( $this, 'define_ajax_events' ), 0 );
37 }
38
39 public function define_ajax_events(): void {
40 $events = array(
41 'complete_onboarding_step',
42 'publish_website',
43 'identify_action',
44 'menu_action',
45 'woocommerce_setup_store',
46 'hide_promotional_banner',
47 'get_survey',
48 'submit_survey',
49 'hide_survey',
50 'dismiss_omnisend_notice',
51 );
52
53 foreach ( $events as $event ) {
54 add_action( 'wp_ajax_hostinger_' . $event, array( $this, $event ) );
55 }
56
57 if ( Hostinger_Helper::is_woocommerce_site() ) {
58 add_action( 'wp_ajax_hostinger_woo_onboarding_choice', array( $this, 'woo_onboarding_choice' ) );
59 }
60 }
61
62 public function get_survey(): void {
63 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
64 $type = isset( $_POST['type'] ) ? sanitize_text_field( $_POST['type'] ) : 'ai_survey';
65 $security_check = $this->request_security_check( $nonce );
66
67 if ( ! empty( $security_check ) ) {
68 wp_send_json_error( $security_check );
69 }
70
71 if ( get_transient( self::HIDE_SURVEY_TRANSIENT ) ) {
72 wp_send_json_error( 'Survey hidden' );
73 }
74
75 $surveys = new Hostinger_Surveys( $this->settings, $this->helper, $this->config_handler, $this->survey_questions, $this->surveys_rest );
76
77 $survey_questions = $surveys->get_wp_survey_questions();
78 $questions_json = $surveys->get_specified_survey_questions( $survey_questions, $type );
79
80 wp_send_json( $questions_json );
81 }
82
83 public function submit_survey(): void {
84 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
85 $survey_type = isset( $_POST['type'] ) ? sanitize_text_field( $_POST['type'] ) : '';
86 $survey_results = isset( $_POST['type'] ) ? sanitize_text_field( $_POST['survey_results'] ) : '';
87 $surveys = new Hostinger_Surveys( $this->settings, $this->helper, $this->config_handler, $this->survey_questions, $this->surveys_rest );
88
89 $security_check = $this->request_security_check( $nonce );
90
91 if ( ! empty( $security_check ) ) {
92 wp_send_json_error( $security_check );
93 }
94
95 $decoded_json = json_decode( stripslashes( $survey_results ), true );
96 $surveys->submit_survey_answers( $decoded_json, $survey_type );
97 }
98
99 public function hide_survey(): void {
100 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
101 $transient_key = self::HIDE_SURVEY_TRANSIENT;
102 $security_check = $this->request_security_check( $nonce );
103
104 if ( ! empty( $security_check ) ) {
105 wp_send_json_error( $security_check );
106 }
107
108 if ( false === get_transient( $transient_key ) ) {
109 set_transient( $transient_key, time(), self::THIRTY_DAYS );
110 }
111
112 wp_send_json_success( array() );
113 }
114
115 public function hide_promotional_banner(): void {
116 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
117 $transient_key = self::PROMOTIONAL_BANNER_TRANSIENT;
118 $security_check = $this->request_security_check( $nonce );
119
120 if ( ! empty( $security_check ) ) {
121 wp_send_json_error( $security_check );
122 }
123
124 if ( false === get_transient( $transient_key ) ) {
125 set_transient( $transient_key, time(), self::TWO_DAYS );
126 }
127
128 wp_send_json_success( array() );
129 }
130
131 public function woocommerce_setup_store(): void {
132 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
133 $event_action = isset( $_POST['event_action'] ) ? sanitize_text_field( $_POST['event_action'] ) : '';
134
135 $security_check = $this->request_security_check( $nonce );
136
137 if ( ! empty( $security_check ) ) {
138 wp_send_json_error( $security_check );
139 }
140
141 $amplitude = new Hostinger_Amplitude();
142 $amplitude->setup_store( $event_action );
143
144 wp_send_json_success( array() );
145 }
146
147 public function publish_website(): void {
148 $publish = (bool) $_POST['maintenance'];
149 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
150 $security_check = $this->request_security_check( $nonce );
151
152 if ( ! empty( $security_check ) ) {
153 wp_send_json_error( $security_check );
154 }
155
156 Hostinger_Settings::update_setting( 'maintenance_mode', $publish ? 1 : 0 );
157
158 require_once HOSTINGER_ABSPATH . 'includes/admin/onboarding/class-hostinger-onboarding.php';
159 $content = new Hostinger_Onboarding();
160
161 if ( has_action( 'litespeed_purge_all' ) ) {
162 do_action( 'litespeed_purge_all' );
163 }
164
165 wp_send_json_success(
166 array(
167 'published' => $publish,
168 'title' => __( 'Website is published', 'hostinger' ),
169 'description' => __( 'Congratulations! Your website is online.', 'hostinger' ),
170 'content' => $content->get_content(),
171 'preview_url' => home_url(),
172 )
173 );
174 }
175
176 public function complete_onboarding_step(): void {
177 $step = isset( $_POST['step'] ) ? sanitize_text_field( $_POST['step'] ) : '';
178 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
179
180 $security_check = $this->request_security_check( $nonce );
181
182 if ( ! empty( $security_check ) ) {
183 wp_send_json_error( $security_check );
184 }
185
186 $completed_steps = get_option( 'hostinger_onboarding_steps', array() );
187 if ( ! in_array( $step, array_column( $completed_steps, 'action' ), true ) ) {
188 $completed_steps[] = array(
189 'action' => $step,
190 'date' => gmdate( 'Y-m-d H:i:s' ),
191 );
192 }
193 Hostinger_Settings::update_setting( 'onboarding_steps', $completed_steps );
194
195 wp_send_json_success( array() );
196 }
197
198 public function identify_action(): void {
199 $action = sanitize_text_field( $_POST['action_name'] ) ?? '';
200
201 if ( in_array( $action, Hostinger_Admin_Actions::ACTIONS_LIST, true ) ) {
202 setcookie( $action, $action, time() + ( 86400 ), '/' );
203 wp_send_json_success( $action );
204 } else {
205 wp_send_json_error( 'Invalid action' );
206 }
207 }
208
209 public function menu_action(): void {
210 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
211 $location = sanitize_text_field( $_POST['location'] ) ?? '';
212 $event_action = sanitize_text_field( $_POST['event_action'] ) ?? '';
213
214 $security_check = $this->request_security_check( $nonce );
215
216 if ( ! empty( $security_check ) ) {
217 wp_send_json_error( $security_check );
218 }
219
220 $amplitude = new Hostinger_Amplitude();
221 $amplitude->track_menu_action( $event_action, $location );
222
223 wp_send_json_success( array() );
224 }
225
226 public function request_security_check( $nonce ) {
227 if ( ! wp_verify_nonce( $nonce, 'hts-ajax-nonce' ) ) {
228 return 'Invalid nonce';
229 }
230
231 if ( ! current_user_can( 'manage_options' ) ) {
232 return 'Lack of permissions';
233 }
234
235 return false;
236 }
237
238 public function woo_onboarding_choice(): void {
239 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
240 $choice = isset( $_POST['choice'] ) ? sanitize_text_field( $_POST['choice'] ) : '';
241 $security_check = $this->request_security_check( $nonce );
242
243 if ( ! empty( $security_check ) ) {
244 wp_send_json_error( $security_check );
245 }
246
247 $allowed_choices = array(
248 'woocommerce',
249 'hostinger',
250 );
251
252 if ( ! in_array( $choice, $allowed_choices, true ) || empty( $choice ) ) {
253 wp_send_json_error( __( 'Something went wrong. Try again.', 'hostinger' ) );
254 }
255
256 $base_url = 'https://' . $this->helper->get_host_info() . '/';
257
258 $redirect_url = '';
259
260 switch ( $choice ) {
261 case 'woocommerce':
262 $redirect_url = $base_url . 'wp-admin/admin.php?page=wc-admin&path=' . rawurlencode( '/setup-wizard' );
263
264 add_option( 'hostinger_woo_ready_message_shown', 0 );
265
266 break;
267 case 'hostinger':
268 $redirect_url = $base_url . 'wp-admin/admin.php?page=hostinger';
269 break;
270 }
271
272 update_option( 'hostinger_woo_onboarding_choice_value', $choice, false );
273 update_option( 'hostinger_woo_onboarding_choice', true, false );
274
275 if ( has_action( 'litespeed_purge_all' ) ) {
276 do_action( 'litespeed_purge_all' );
277 }
278
279 $response = array(
280 'redirect_url' => $redirect_url,
281 );
282
283 wp_send_json_success( $response );
284 }
285
286 public function dismiss_omnisend_notice(): void {
287 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
288
289 if ( ! wp_verify_nonce( $nonce, 'hts_close_omnisend' ) ) {
290 wp_send_json_error( 'Invalid nonce' );
291 }
292
293 $_SESSION[ self::HIDE_OMNISEND_NOTICE ] = true;
294 wp_die();
295 }
296 }
297
298 new Hostinger_Admin_Ajax();
299