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