PluginProbe ʕ •ᴥ•ʔ
Hostinger Reach – AI-Powered Email Marketing for WordPress / 1.7.0
Hostinger Reach – AI-Powered Email Marketing for WordPress v1.7.0
1.7.2 1.7.1 1.7.0 1.6.0 1.5.9 1.5.8 1.5.7 1.5.6 1.5.5 1.5.4 1.5.3 1.5.2 1.5.1 1.5.0 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 trunk 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6
hostinger-reach / vendor / hostinger / hostinger-wp-surveys / src / Ajax.php
hostinger-reach / vendor / hostinger / hostinger-wp-surveys / src Last commit date
Ajax.php 3 months ago Assets.php 1 month ago Loader.php 1 year ago Rest.php 1 year ago SurveyLoader.php 1 year ago SurveyManager.php 1 month ago
Ajax.php
96 lines
1 <?php
2
3 namespace Hostinger\Surveys;
4
5 use Hostinger\WpHelper\Config;
6 use Hostinger\WpHelper\Utils as Helper;
7 use Hostinger\WpHelper\Requests\Client;
8 use Hostinger\WpHelper\Constants;
9 use Hostinger\Surveys\Rest as SurveysRest;
10 use Hostinger\Surveys\SurveyManager as HostingerSurveys;
11
12 defined('ABSPATH') || exit;
13
14 class Ajax
15 {
16 private const HIDE_SURVEY_TRANSIENT = 'hts_hide_survey';
17 private const THIRTY_DAYS = 86400 * 30;
18
19 private Config $configHandler;
20 private Helper $helper;
21 private SurveysRest $surveysRest;
22
23 public function __construct(
24 Helper $helper,
25 Config $configHandler,
26 Rest $surveysRest
27 ) {
28 $this->helper = $helper;
29 $this->configHandler = $configHandler;
30 $this->surveysRest = $surveysRest;
31
32 add_action('init', [ $this, 'defineAjaxEvents' ], 0);
33 }
34
35 public function defineAjaxEvents(): void
36 {
37 $events = [
38 'submitSurvey',
39 'hideSurvey',
40 ];
41
42 foreach ($events as $event) {
43 add_action('wp_ajax_hostinger_' . $event, [ $this, $event ]);
44 }
45 }
46
47 public function submitSurvey(): void
48 {
49 $nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';
50 $survey_results = isset($_POST['survey_results']) ? sanitize_text_field($_POST['survey_results']) : '';
51 $survey_location = isset($_POST['survey_location']) ? sanitize_text_field($_POST['survey_location']) : '';
52 $survey_id = isset($_POST['survey_id']) ? sanitize_text_field($_POST['survey_id']) : '';
53 $is_mobile = isset($_POST['is_mobile']) ? (int) $_POST['is_mobile'] : 0;
54 $surveys = new HostingerSurveys($this->helper, $this->configHandler, $this->surveysRest);
55
56 $security_check = $this->requestSecurityCheck($nonce);
57
58 if (! empty($security_check)) {
59 wp_send_json_error($security_check);
60 }
61
62 $decoded_json = json_decode(stripslashes($survey_results), true);
63 $surveys->submitSurveyAnswers($decoded_json, $survey_id, $survey_location, $is_mobile);
64 }
65
66 public function hideSurvey(): void
67 {
68 $nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';
69 $transient_key = self::HIDE_SURVEY_TRANSIENT;
70 $security_check = $this->requestSecurityCheck($nonce);
71
72 if (! empty($security_check)) {
73 wp_send_json_error($security_check);
74 }
75
76 if (false === get_transient($transient_key)) {
77 set_transient($transient_key, time(), self::THIRTY_DAYS);
78 }
79
80 wp_send_json_success([]);
81 }
82
83 public function requestSecurityCheck($nonce)
84 {
85 if (! wp_verify_nonce($nonce, 'hts-ajax-nonce')) {
86 return 'Invalid nonce';
87 }
88
89 if (! current_user_can('manage_options')) {
90 return 'Lack of permissions';
91 }
92
93 return false;
94 }
95 }
96