| 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 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; |
| 13 |
public function __construct() { |
| 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 ); |
| 31 |
} |
| 32 |
|
| 33 |
public function define_ajax_events(): void { |
| 34 |
$events = array( |
| 35 |
'complete_onboarding_step', |
| 36 |
'publish_website', |
| 37 |
'identify_action', |
| 38 |
'menu_action', |
| 39 |
'woocommerce_setup_store', |
| 40 |
'hide_promotional_banner', |
| 41 |
'get_survey', |
| 42 |
'submit_survey', |
| 43 |
); |
| 44 |
|
| 45 |
foreach ( $events as $event ) { |
| 46 |
add_action( 'wp_ajax_hostinger_' . $event, array( $this, $event ) ); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 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 ); |
| 58 |
} |
| 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 |
|
| 65 |
wp_send_json( $questions_json ); |
| 66 |
} |
| 67 |
|
| 68 |
public function submit_survey(): void { |
| 69 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : ''; |
| 70 |
$survey_type = sanitize_text_field( $_POST['type'] ); |
| 71 |
$survey_results = sanitize_text_field( $_POST['survey_results'] ); |
| 72 |
$surveys = new Hostinger_Surveys( $this->settings, $this->helper, $this->config_handler, $this->survey_questions, $this->surveys_rest ); |
| 73 |
|
| 74 |
$security_check = self::request_security_check( $nonce ); |
| 75 |
|
| 76 |
if ( ! empty( $security_check ) ) { |
| 77 |
wp_send_json_error( $security_check ); |
| 78 |
} |
| 79 |
|
| 80 |
$decoded_json = json_decode( stripslashes( $survey_results ), true ); |
| 81 |
$surveys->submit_survey_answers( $decoded_json, $survey_type ); |
| 82 |
} |
| 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( [] ); |
| 98 |
} |
| 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 |
|
| 116 |
public static function publish_website(): void { |
| 117 |
$publish = (bool) $_POST['maintenance']; |
| 118 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : ''; |
| 119 |
$security_check = self::request_security_check( $nonce ); |
| 120 |
|
| 121 |
if ( ! empty( $security_check ) ) { |
| 122 |
wp_send_json_error( $security_check ); |
| 123 |
} |
| 124 |
|
| 125 |
Hostinger_Settings::update_setting( 'maintenance_mode', $publish ? 1 : 0 ); |
| 126 |
|
| 127 |
require_once HOSTINGER_ABSPATH . 'includes/admin/onboarding/class-hostinger-onboarding.php'; |
| 128 |
$content = new Hostinger_Onboarding(); |
| 129 |
|
| 130 |
if ( has_action( 'litespeed_purge_all' ) ) { |
| 131 |
do_action( 'litespeed_purge_all' ); |
| 132 |
} |
| 133 |
|
| 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 |
); |
| 143 |
} |
| 144 |
|
| 145 |
public static function complete_onboarding_step(): void { |
| 146 |
$step = $_POST['step']; |
| 147 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : ''; |
| 148 |
|
| 149 |
$security_check = self::request_security_check( $nonce ); |
| 150 |
|
| 151 |
if ( ! empty( $security_check ) ) { |
| 152 |
wp_send_json_error( $security_check ); |
| 153 |
} |
| 154 |
|
| 155 |
$completed_steps = get_option( 'hostinger_onboarding_steps', array() ); |
| 156 |
if ( ! in_array( $step, array_column( $completed_steps, 'action' ), true ) ) { |
| 157 |
$completed_steps[] = array( |
| 158 |
'action' => $step, |
| 159 |
'date' => date( 'Y-m-d H:i:s' ), |
| 160 |
); |
| 161 |
} |
| 162 |
Hostinger_Settings::update_setting( 'onboarding_steps', $completed_steps ); |
| 163 |
|
| 164 |
wp_send_json_success( array() ); |
| 165 |
} |
| 166 |
|
| 167 |
public static function identify_action(): void { |
| 168 |
$action = sanitize_text_field( $_POST['action_name'] ) ?? ''; |
| 169 |
|
| 170 |
if ( in_array( $action, Hostinger_Admin_Actions::ACTIONS_LIST, true ) ) { |
| 171 |
setcookie( $action, $action, time() + ( 86400 ), '/' ); |
| 172 |
wp_send_json_success( $action ); |
| 173 |
} else { |
| 174 |
wp_send_json_error( 'Invalid action' ); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 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'] ) ?? ''; |
| 182 |
|
| 183 |
$security_check = self::request_security_check( $nonce ); |
| 184 |
|
| 185 |
if ( ! empty( $security_check ) ) { |
| 186 |
wp_send_json_error( $security_check ); |
| 187 |
} |
| 188 |
|
| 189 |
$amplitude = new Hostinger_Amplitude(); |
| 190 |
$amplitude->track_menu_action( $event_action, $location ); |
| 191 |
|
| 192 |
wp_send_json_success( array() ); |
| 193 |
} |
| 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 |
} |
| 206 |
} |
| 207 |
|
| 208 |
new Hostinger_Admin_Ajax(); |
| 209 |
|