| @@ -2,60 +2,125 @@ | ||
| 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 TWO_DAYS = 172800; | |
| 8 | + private Hostinger_Config $config_handler; | |
| 9 | + private Hostinger_Settings $settings; | |
| 10 | + private Hostinger_Helper $helper; | |
| 11 | + private Hostinger_Surveys_Questions $survey_questions; | |
| 12 | + private Hostinger_Surveys_Rest $surveys_rest; | |
| 6 | 13 | public function __construct() { |
| 7 | - add_action( 'init', [ $this, 'define_ajax_events' ], 0 ); | |
| 14 | + $this->settings = new Hostinger_Settings(); | |
| 15 | + $this->helper = new Hostinger_Helper(); | |
| 16 | + $this->config_handler = new Hostinger_Config(); | |
| 17 | + if ( ! empty( Hostinger_Helper::get_api_token() ) ) { | |
| 18 | + $this->survey_questions = new Hostinger_Surveys_Questions(); | |
| 19 | + $client = new Hostinger_Requests_Client( | |
| 20 | + $this->config_handler->get_config_value( 'base_rest_uri', HOSTINGER_REST_URI ), | |
| 21 | + array( | |
| 22 | + Hostinger_Config::TOKEN_HEADER => $this->helper::get_api_token(), | |
| 23 | + Hostinger_Config::DOMAIN_HEADER => $this->helper->get_host_info(), | |
| 24 | + ) | |
| 25 | + ); | |
| 26 | + | |
| 27 | + $this->surveys_rest = new Hostinger_Surveys_Rest( $client ); | |
| 28 | + } | |
| 29 | + | |
| 30 | + add_action( 'init', array( $this, 'define_ajax_events' ), 0 ); | |
| 8 | 31 | } |
| 9 | 32 | |
| 10 | 33 | public function define_ajax_events(): void { |
| 11 | - $events = [ | |
| 34 | + $events = array( | |
| 12 | 35 | 'complete_onboarding_step', |
| 13 | 36 | 'publish_website', |
| 14 | 37 | 'identify_action', |
| 38 | + 'menu_action', | |
| 39 | + 'woocommerce_setup_store', | |
| 40 | + 'hide_promotional_banner', | |
| 15 | 41 | 'get_survey', |
| 16 | 42 | 'submit_survey', |
| 17 | - 'menu_action', | |
| 18 | - ]; | |
| 43 | + ); | |
| 19 | 44 | |
| 20 | 45 | foreach ( $events as $event ) { |
| 21 | - add_action( 'wp_ajax_hostinger_' . $event, [ __CLASS__, $event ] ); | |
| 22 | - add_action( 'wp_ajax_nopriv_hostinger_' . $event, [ __CLASS__, $event ] ); | |
| 46 | + add_action( 'wp_ajax_hostinger_' . $event, array( $this, $event ) ); | |
| 23 | 47 | } |
| 24 | 48 | } |
| 25 | 49 | |
| 26 | - public static function get_survey(): void { | |
| 27 | - $nonce = sanitize_text_field( $_POST['nonce'] ); | |
| 28 | - if ( ! wp_verify_nonce( $nonce, 'get_questions' ) ) { | |
| 29 | - wp_send_json_error( 'Invalid nonce.' ); | |
| 50 | + public function get_survey(): void { | |
| 51 | + $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : ''; | |
| 52 | + $type = sanitize_text_field( $_POST['type'] ) ?? 'ai_survey'; | |
| 53 | + | |
| 54 | + $security_check = self::request_security_check( $nonce ); | |
| 55 | + | |
| 56 | + if ( ! empty( $security_check ) ) { | |
| 57 | + wp_send_json_error( $security_check ); | |
| 30 | 58 | } |
| 31 | - $surveys = new Hostinger_Surveys(); | |
| 32 | - $required_questions = $surveys->get_required_survey_questions(); | |
| 33 | - $questions_json = $surveys->generate_json( $required_questions ); | |
| 34 | 59 | |
| 60 | + $surveys = new Hostinger_Surveys( $this->settings, $this->helper, $this->config_handler, $this->survey_questions, $this->surveys_rest ); | |
| 61 | + | |
| 62 | + $survey_questions = $surveys->get_wp_survey_questions(); | |
| 63 | + $questions_json = $surveys->get_specified_survey_questions( $survey_questions, $type ); | |
| 64 | + | |
| 35 | 65 | wp_send_json( $questions_json ); |
| 36 | 66 | } |
| 37 | 67 | |
| 38 | - public static function submit_survey(): void { | |
| 39 | - $nonce = sanitize_text_field( $_POST['nonce'] ); | |
| 68 | + public function submit_survey(): void { | |
| 69 | + $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : ''; | |
| 70 | + $survey_type = sanitize_text_field( $_POST['type'] ); | |
| 40 | 71 | $survey_results = sanitize_text_field( $_POST['survey_results'] ); |
| 41 | - $surveys = new Hostinger_Surveys(); | |
| 72 | + $surveys = new Hostinger_Surveys( $this->settings, $this->helper, $this->config_handler, $this->survey_questions, $this->surveys_rest ); | |
| 42 | 73 | |
| 43 | - if ( ! wp_verify_nonce( $nonce, 'submit_questions' ) ) { | |
| 44 | - wp_send_json_error( 'Invalid nonce.' ); | |
| 74 | + $security_check = self::request_security_check( $nonce ); | |
| 75 | + | |
| 76 | + if ( ! empty( $security_check ) ) { | |
| 77 | + wp_send_json_error( $security_check ); | |
| 45 | 78 | } |
| 46 | 79 | |
| 47 | - $decoded_json = json_decode(stripslashes($survey_results), true); | |
| 48 | - $surveys->submit_survey_answers( $decoded_json ); | |
| 80 | + $decoded_json = json_decode( stripslashes( $survey_results ), true ); | |
| 81 | + $surveys->submit_survey_answers( $decoded_json, $survey_type ); | |
| 82 | + } | |
| 49 | 83 | |
| 84 | + public static function hide_promotional_banner(): void { | |
| 85 | + $nonce = sanitize_text_field( $_POST['nonce'] ); | |
| 86 | + $transient_key = self::PROMOTIONAL_BANNER_TRANSIENT; | |
| 87 | + $security_check = self::request_security_check( $nonce ); | |
| 88 | + | |
| 89 | + if ( ! empty( $security_check ) ) { | |
| 90 | + wp_send_json_error( $security_check ); | |
| 91 | + } | |
| 92 | + | |
| 93 | + if ( false === get_transient( $transient_key ) ) { | |
| 94 | + set_transient( $transient_key, time(), self::TWO_DAYS ); | |
| 95 | + } | |
| 96 | + | |
| 97 | + wp_send_json_success( [] ); | |
| 50 | 98 | } |
| 51 | 99 | |
| 100 | + public static function woocommerce_setup_store(): void { | |
| 101 | + $nonce = sanitize_text_field( $_POST['nonce'] ); | |
| 102 | + $event_action = sanitize_text_field( $_POST['event_action'] ); | |
| 103 | + | |
| 104 | + $security_check = self::request_security_check( $nonce ); | |
| 105 | + | |
| 106 | + if ( ! empty( $security_check ) ) { | |
| 107 | + wp_send_json_error( $security_check ); | |
| 108 | + } | |
| 109 | + | |
| 110 | + $amplitude = new Hostinger_Amplitude(); | |
| 111 | + $amplitude->setup_store( $event_action ); | |
| 112 | + | |
| 113 | + wp_send_json_success( [] ); | |
| 114 | + } | |
| 115 | + | |
| 52 | 116 | public static function publish_website(): void { |
| 53 | - $nonce = sanitize_text_field( $_POST['nonce'] ); | |
| 54 | - $publish = filter_var( $_POST['maintenance'], FILTER_VALIDATE_BOOLEAN ); | |
| 117 | + $publish = (bool) $_POST['maintenance']; | |
| 118 | + $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : ''; | |
| 119 | + $security_check = self::request_security_check( $nonce ); | |
| 55 | 120 | |
| 56 | - if ( ! wp_verify_nonce( $nonce, 'hts-ajax-nonce' ) ) { | |
| 57 | - wp_send_json_error( 'Invalid nonce.' ); | |
| 121 | + if ( ! empty( $security_check ) ) { | |
| 122 | + wp_send_json_error( $security_check ); | |
| 58 | 123 | } |
| 59 | 124 | |
| 60 | 125 | Hostinger_Settings::update_setting( 'maintenance_mode', $publish ? 1 : 0 ); |
| 61 | 126 | |
| @@ -65,47 +130,46 @@ | ||
| 65 | 130 | if ( has_action( 'litespeed_purge_all' ) ) { |
| 66 | 131 | do_action( 'litespeed_purge_all' ); |
| 67 | 132 | } |
| 68 | 133 | |
| 69 | - wp_send_json_success( [ | |
| 70 | - 'published' => $publish, | |
| 71 | - 'title' => esc_html__( 'Website is published', 'hostinger' ), | |
| 72 | - 'description' => esc_html__( 'Congratulations! Your website is online.', 'hostinger' ), | |
| 73 | - 'content' => $content->get_content(), | |
| 74 | - 'preview_url' => home_url(), | |
| 75 | - ] ); | |
| 134 | + wp_send_json_success( | |
| 135 | + array( | |
| 136 | + 'published' => $publish, | |
| 137 | + 'title' => __( 'Website is published', 'hostinger' ), | |
| 138 | + 'description' => __( 'Congratulations! Your website is online.', 'hostinger' ), | |
| 139 | + 'content' => $content->get_content(), | |
| 140 | + 'preview_url' => home_url(), | |
| 141 | + ) | |
| 142 | + ); | |
| 76 | 143 | } |
| 77 | 144 | |
| 78 | 145 | public static function complete_onboarding_step(): void { |
| 79 | - $step = sanitize_text_field( $_POST['step'] ); | |
| 80 | - $completed_steps = get_option( 'hostinger_onboarding_steps', [] ); | |
| 81 | - $nonce = sanitize_text_field( $_POST['nonce'] ); | |
| 146 | + $step = $_POST['step']; | |
| 147 | + $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : ''; | |
| 82 | 148 | |
| 83 | - if ( ! wp_verify_nonce( $nonce, 'hts-ajax-nonce' ) ) { | |
| 84 | - wp_send_json_error( 'Invalid nonce.' ); | |
| 149 | + $security_check = self::request_security_check( $nonce ); | |
| 150 | + | |
| 151 | + if ( ! empty( $security_check ) ) { | |
| 152 | + wp_send_json_error( $security_check ); | |
| 85 | 153 | } |
| 86 | 154 | |
| 87 | - if ( ! in_array( $step, array_column($completed_steps, 'action'), true ) ) { | |
| 88 | - $completed_steps[] = [ | |
| 155 | + $completed_steps = get_option( 'hostinger_onboarding_steps', array() ); | |
| 156 | + if ( ! in_array( $step, array_column( $completed_steps, 'action' ), true ) ) { | |
| 157 | + $completed_steps[] = array( | |
| 89 | 158 | 'action' => $step, |
| 90 | - 'date' => gmdate( 'Y-m-d H:i:s' ), | |
| 91 | - ]; | |
| 159 | + 'date' => date( 'Y-m-d H:i:s' ), | |
| 160 | + ); | |
| 92 | 161 | } |
| 93 | 162 | Hostinger_Settings::update_setting( 'onboarding_steps', $completed_steps ); |
| 94 | 163 | |
| 95 | - wp_send_json_success( [] ); | |
| 164 | + wp_send_json_success( array() ); | |
| 96 | 165 | } |
| 97 | 166 | |
| 98 | 167 | public static function identify_action(): void { |
| 99 | - $action = sanitize_text_field( $_POST['action_name'] ?? '' ); | |
| 100 | - $nonce = sanitize_text_field( $_POST['nonce'] ); | |
| 168 | + $action = sanitize_text_field( $_POST['action_name'] ) ?? ''; | |
| 101 | 169 | |
| 102 | - if ( ! wp_verify_nonce( $nonce, 'hts-ajax-nonce' ) ) { | |
| 103 | - wp_send_json_error( 'Invalid nonce.' ); | |
| 104 | - } | |
| 105 | - | |
| 106 | 170 | if ( in_array( $action, Hostinger_Admin_Actions::ACTIONS_LIST, true ) ) { |
| 107 | - setcookie($action, $action, time() + (86400), '/'); | |
| 171 | + setcookie( $action, $action, time() + ( 86400 ), '/' ); | |
| 108 | 172 | wp_send_json_success( $action ); |
| 109 | 173 | } else { |
| 110 | 174 | wp_send_json_error( 'Invalid action' ); |
| 111 | 175 | } |
| @@ -111,20 +175,34 @@ | ||
| 111 | 175 | } |
| 112 | 176 | } |
| 113 | 177 | |
| 114 | 178 | public static function menu_action(): void { |
| 179 | + $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : ''; | |
| 180 | + $location = sanitize_text_field( $_POST['location'] ) ?? ''; | |
| 181 | + $event_action = sanitize_text_field( $_POST['event_action'] ) ?? ''; | |
| 115 | 182 | |
| 116 | - $nonce = sanitize_text_field( $_POST['nonce'] ); | |
| 117 | - $location = sanitize_text_field( $_POST['location'] ?? '' ); | |
| 118 | - $event_action = sanitize_text_field( $_POST['event_action'] ?? '' ); | |
| 183 | + $security_check = self::request_security_check( $nonce ); | |
| 119 | 184 | |
| 120 | - if ( ! wp_verify_nonce( $nonce, 'menu_actions' ) ) { | |
| 121 | - wp_send_json_error( 'Invalid nonce.' ); | |
| 185 | + if ( ! empty( $security_check ) ) { | |
| 186 | + wp_send_json_error( $security_check ); | |
| 122 | 187 | } |
| 123 | 188 | |
| 124 | 189 | $amplitude = new Hostinger_Amplitude(); |
| 125 | 190 | $amplitude->track_menu_action( $event_action, $location ); |
| 191 | + | |
| 192 | + wp_send_json_success( array() ); | |
| 126 | 193 | } |
| 127 | 194 | |
| 195 | + public static function request_security_check( $nonce ) { | |
| 196 | + if ( ! wp_verify_nonce( $nonce, 'hts-ajax-nonce' ) ) { | |
| 197 | + return 'Invalid nonce'; | |
| 198 | + } | |
| 199 | + | |
| 200 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 201 | + return 'Lack of permissions'; | |
| 202 | + } | |
| 203 | + | |
| 204 | + return false; | |
| 205 | + } | |
| 128 | 206 | } |
| 129 | 207 | |
| 130 | 208 | new Hostinger_Admin_Ajax(); |