| @@ -2,35 +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 | 43 | 'identify_action', |
| 15 | 44 | 'menu_action', |
| 16 | - ]; | |
| 45 | + 'woocommerce_setup_store', | |
| 46 | + 'hide_promotional_banner', | |
| 47 | + 'get_survey', | |
| 48 | + 'submit_survey', | |
| 49 | + 'hide_survey', | |
| 50 | + 'dismiss_omnisend_notice', | |
| 51 | + ); | |
| 17 | 52 | |
| 18 | 53 | foreach ( $events as $event ) { |
| 19 | - add_action( 'wp_ajax_hostinger_' . $event, [ __CLASS__, $event ] ); | |
| 54 | + add_action( 'wp_ajax_hostinger_' . $event, array( $this, $event ) ); | |
| 20 | 55 | } |
| 56 | + | |
| 57 | + if ( Hostinger_Helper::is_woocommerce_site() ) { | |
| 58 | + add_action( 'wp_ajax_hostinger_woo_onboarding_choice', array( $this, 'woo_onboarding_choice' ) ); | |
| 59 | + } | |
| 21 | 60 | } |
| 22 | 61 | |
| 23 | - public static function publish_website(): void { | |
| 24 | - $publish = (bool) $_POST['maintenance']; | |
| 25 | - $nonce = sanitize_text_field( $_POST['nonce'] ); | |
| 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 ); | |
| 26 | 66 | |
| 27 | - $security_check = self::request_security_check( $nonce ); | |
| 67 | + if ( ! empty( $security_check ) ) { | |
| 68 | + wp_send_json_error( $security_check ); | |
| 69 | + } | |
| 28 | 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 | + | |
| 29 | 91 | if ( ! empty( $security_check ) ) { |
| 30 | 92 | wp_send_json_error( $security_check ); |
| 31 | 93 | } |
| 32 | 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 | + | |
| 33 | 156 | Hostinger_Settings::update_setting( 'maintenance_mode', $publish ? 1 : 0 ); |
| 34 | 157 | |
| 35 | 158 | require_once HOSTINGER_ABSPATH . 'includes/admin/onboarding/class-hostinger-onboarding.php'; |
| 36 | 159 | $content = new Hostinger_Onboarding(); |
| @@ -38,44 +161,46 @@ | ||
| 38 | 161 | if ( has_action( 'litespeed_purge_all' ) ) { |
| 39 | 162 | do_action( 'litespeed_purge_all' ); |
| 40 | 163 | } |
| 41 | 164 | |
| 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 | - ] ); | |
| 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 | + ); | |
| 49 | 174 | } |
| 50 | 175 | |
| 51 | - public static function complete_onboarding_step(): void { | |
| 52 | - $step = $_POST['step']; | |
| 53 | - $nonce = sanitize_text_field( $_POST['nonce'] ); | |
| 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'] ) : ''; | |
| 54 | 179 | |
| 55 | - $security_check = self::request_security_check( $nonce ); | |
| 180 | + $security_check = $this->request_security_check( $nonce ); | |
| 56 | 181 | |
| 57 | 182 | if ( ! empty( $security_check ) ) { |
| 58 | 183 | wp_send_json_error( $security_check ); |
| 59 | 184 | } |
| 60 | 185 | |
| 61 | - $completed_steps = get_option( 'hostinger_onboarding_steps', [] ); | |
| 62 | - if ( ! in_array( $step, array_column($completed_steps, 'action'), true ) ) { | |
| 63 | - $completed_steps[] = [ | |
| 186 | + $completed_steps = get_option( 'hostinger_onboarding_steps', array() ); | |
| 187 | + if ( ! in_array( $step, array_column( $completed_steps, 'action' ), true ) ) { | |
| 188 | + $completed_steps[] = array( | |
| 64 | 189 | 'action' => $step, |
| 65 | - 'date' => date( 'Y-m-d H:i:s' ), | |
| 66 | - ]; | |
| 190 | + 'date' => gmdate( 'Y-m-d H:i:s' ), | |
| 191 | + ); | |
| 67 | 192 | } |
| 68 | 193 | Hostinger_Settings::update_setting( 'onboarding_steps', $completed_steps ); |
| 69 | 194 | |
| 70 | - wp_send_json_success( [] ); | |
| 195 | + wp_send_json_success( array() ); | |
| 71 | 196 | } |
| 72 | 197 | |
| 73 | - public static function identify_action(): void { | |
| 198 | + public function identify_action(): void { | |
| 74 | 199 | $action = sanitize_text_field( $_POST['action_name'] ) ?? ''; |
| 75 | 200 | |
| 76 | 201 | if ( in_array( $action, Hostinger_Admin_Actions::ACTIONS_LIST, true ) ) { |
| 77 | - setcookie($action, $action, time() + (86400), '/'); | |
| 202 | + setcookie( $action, $action, time() + ( 86400 ), '/' ); | |
| 78 | 203 | wp_send_json_success( $action ); |
| 79 | 204 | } else { |
| 80 | 205 | wp_send_json_error( 'Invalid action' ); |
| 81 | 206 | } |
| @@ -80,14 +205,14 @@ | ||
| 80 | 205 | wp_send_json_error( 'Invalid action' ); |
| 81 | 206 | } |
| 82 | 207 | } |
| 83 | 208 | |
| 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'] ) ?? ''; | |
| 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'] ) ?? ''; | |
| 88 | 213 | |
| 89 | - $security_check = self::request_security_check( $nonce ); | |
| 214 | + $security_check = $this->request_security_check( $nonce ); | |
| 90 | 215 | |
| 91 | 216 | if ( ! empty( $security_check ) ) { |
| 92 | 217 | wp_send_json_error( $security_check ); |
| 93 | 218 | } |
| @@ -94,12 +219,12 @@ | ||
| 94 | 219 | |
| 95 | 220 | $amplitude = new Hostinger_Amplitude(); |
| 96 | 221 | $amplitude->track_menu_action( $event_action, $location ); |
| 97 | 222 | |
| 98 | - wp_send_json_success( [] ); | |
| 223 | + wp_send_json_success( array() ); | |
| 99 | 224 | } |
| 100 | 225 | |
| 101 | - public static function request_security_check( $nonce ) { | |
| 226 | + public function request_security_check( $nonce ) { | |
| 102 | 227 | if ( ! wp_verify_nonce( $nonce, 'hts-ajax-nonce' ) ) { |
| 103 | 228 | return 'Invalid nonce'; |
| 104 | 229 | } |
| 105 | 230 | |
| @@ -107,8 +232,67 @@ | ||
| 107 | 232 | return 'Lack of permissions'; |
| 108 | 233 | } |
| 109 | 234 | |
| 110 | 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(); | |
| 111 | 295 | } |
| 112 | 296 | } |
| 113 | 297 | |
| 114 | 298 | new Hostinger_Admin_Ajax(); |