PluginProbe
Hostinger Tools / 2.1.1
Hostinger Tools v2.1.1
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 2.0.1 All 109 releases
← All changes | includes/admin/class-hostinger-admin-ajax.php +242 -46 1.9.7 → 2.1.1 View file →
@@ -2,29 +2,158 @@
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 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 +
6 18 public function __construct() {
7 - add_action( 'init', [ $this, 'define_ajax_events' ], 0 );
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 );
8 37 }
9 38
10 39 public function define_ajax_events(): void {
11 - $events = [
40 + $events = array(
12 41 'complete_onboarding_step',
13 42 'publish_website',
14 - 'track_click',
15 43 'identify_action',
16 44 'menu_action',
17 - ];
45 + 'woocommerce_setup_store',
46 + 'hide_promotional_banner',
47 + 'get_survey',
48 + 'submit_survey',
49 + 'hide_survey',
50 + 'dismiss_omnisend_notice',
51 + );
18 52
19 53 foreach ( $events as $event ) {
20 - add_action( 'wp_ajax_hostinger_' . $event, [ __CLASS__, $event ] );
21 - add_action( 'wp_ajax_nopriv_hostinger_' . $event, [ __CLASS__, $event ] );
54 + add_action( 'wp_ajax_hostinger_' . $event, array( $this, $event ) );
22 55 }
56 +
57 + if ( Hostinger_Helper::is_woocommerce_site() ) {
58 + add_action( 'wp_ajax_hostinger_woo_onboarding_choice', array( $this, 'woo_onboarding_choice' ) );
59 + }
23 60 }
24 61
25 - public static function publish_website(): void {
26 - $publish = (bool) $_POST['maintenance'];
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 +
27 156 Hostinger_Settings::update_setting( 'maintenance_mode', $publish ? 1 : 0 );
28 157
29 158 require_once HOSTINGER_ABSPATH . 'includes/admin/onboarding/class-hostinger-onboarding.php';
30 159 $content = new Hostinger_Onboarding();
@@ -32,52 +161,46 @@
32 161 if ( has_action( 'litespeed_purge_all' ) ) {
33 162 do_action( 'litespeed_purge_all' );
34 163 }
35 164
36 - wp_send_json_success( [
37 - 'published' => $publish,
38 - 'title' => __( 'Website is published', 'hostinger' ),
39 - 'description' => __( 'Congratulations! Your website is online.', 'hostinger' ),
40 - 'content' => $content->get_content(),
41 - 'preview_url' => home_url(),
42 - ] );
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 + );
43 174 }
44 175
45 - public static function complete_onboarding_step(): void {
46 - $step = $_POST['step'];
47 - $completed_steps = get_option( 'hostinger_onboarding_steps', [] );
48 - if ( ! in_array( $step, array_column($completed_steps, 'action'), true ) ) {
49 - $completed_steps[] = [
50 - 'action' => $step,
51 - 'date' => date( 'Y-m-d H:i:s' ),
52 - ];
53 - }
54 - Hostinger_Settings::update_setting( 'onboarding_steps', $completed_steps );
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'] ) : '';
55 179
56 - wp_send_json_success( [] );
57 - }
180 + $security_check = $this->request_security_check( $nonce );
58 181
59 - public static function track_click(): void {
60 - $valid_options = [
61 - 'hostinger_preview_button_click'
62 - ];
182 + if ( ! empty( $security_check ) ) {
183 + wp_send_json_error( $security_check );
184 + }
63 185
64 - $click_action = $_POST['click_action'];
65 - if ( ! in_array( $click_action, $valid_options, true ) ) {
66 - wp_send_json_error( __( 'invalid data', 'hostinger' ) );
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 + );
67 192 }
193 + Hostinger_Settings::update_setting( 'onboarding_steps', $completed_steps );
68 194
69 - $click_count = get_option( $click_action, 0 );
70 - Hostinger_Settings::update_setting( $click_action, ++ $click_count, 'no' );
71 -
72 - wp_send_json_success( [] );
195 + wp_send_json_success( array() );
73 196 }
74 197
75 - public static function identify_action(): void {
198 + public function identify_action(): void {
76 199 $action = sanitize_text_field( $_POST['action_name'] ) ?? '';
77 200
78 201 if ( in_array( $action, Hostinger_Admin_Actions::ACTIONS_LIST, true ) ) {
79 - setcookie($action, $action, time() + (86400), '/');
202 + setcookie( $action, $action, time() + ( 86400 ), '/' );
80 203 wp_send_json_success( $action );
81 204 } else {
82 205 wp_send_json_error( 'Invalid action' );
83 206 }
@@ -82,21 +205,94 @@
82 205 wp_send_json_error( 'Invalid action' );
83 206 }
84 207 }
85 208
86 - public static function menu_action(): void {
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'] ) ?? '';
87 213
88 - $nonce = sanitize_text_field( $_POST['nonce'] );
89 - $location = sanitize_text_field( $_POST['location'] ) ?? '';
90 - $event_action = sanitize_text_field( $_POST['event_action'] ) ?? '';
214 + $security_check = $this->request_security_check( $nonce );
91 215
92 - if ( ! wp_verify_nonce( $nonce, 'hts-ajax-nonce' ) ) {
93 - wp_send_json_error( 'Invalid nonce.' );
216 + if ( ! empty( $security_check ) ) {
217 + wp_send_json_error( $security_check );
94 218 }
95 219
96 220 $amplitude = new Hostinger_Amplitude();
97 221 $amplitude->track_menu_action( $event_action, $location );
222 +
223 + wp_send_json_success( array() );
98 224 }
99 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 + }
100 296 }
101 297
102 298 new Hostinger_Admin_Ajax();