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 |