Survey.php
71 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Admin\Surveys; |
| 4 | |
| 5 | use Hostinger\Surveys\SurveyManager; |
| 6 | |
| 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | die; |
| 9 | } |
| 10 | |
| 11 | abstract class Survey { |
| 12 | |
| 13 | protected const DAY_IN_SECONDS = 86400; |
| 14 | |
| 15 | private SurveyManager $survey_manager; |
| 16 | |
| 17 | public function __construct( SurveyManager $survey_manager ) { |
| 18 | $this->survey_manager = $survey_manager; |
| 19 | } |
| 20 | |
| 21 | public function init(): void { |
| 22 | add_filter( 'hostinger_add_surveys', array( $this, 'add_survey' ) ); |
| 23 | } |
| 24 | |
| 25 | public function add_survey( array $surveys ): array { |
| 26 | if ( $this->should_load_survey() ) { |
| 27 | $surveys[] = $this->get_survey(); |
| 28 | } |
| 29 | |
| 30 | return $surveys; |
| 31 | } |
| 32 | |
| 33 | protected function get_priority(): int { |
| 34 | return 100; |
| 35 | } |
| 36 | |
| 37 | protected function should_load_survey(): bool { |
| 38 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | $is_client_eligible = $this->survey_manager->isClientEligible(); |
| 43 | $is_not_completed = $this->survey_manager->isSurveyNotCompleted( $this->get_id() ); |
| 44 | $is_hidden = $this->survey_manager->isSurveyHidden(); |
| 45 | |
| 46 | return $is_client_eligible && $is_not_completed && ! $is_hidden; |
| 47 | } |
| 48 | |
| 49 | protected function get_survey(): array { |
| 50 | return $this->survey_manager->addSurvey( |
| 51 | $this->get_id(), |
| 52 | $this->get_score_question(), |
| 53 | $this->get_comment_question(), |
| 54 | $this->get_location(), |
| 55 | $this->get_priority(), |
| 56 | $this->get_review_url(), |
| 57 | $this->get_review_min_required_score() |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | abstract protected function get_score_question(): string; |
| 62 | |
| 63 | abstract protected function get_comment_question(): string; |
| 64 | |
| 65 | abstract protected function get_id(): string; |
| 66 | |
| 67 | abstract protected function get_location(): string; |
| 68 | abstract protected function get_review_url(): string; |
| 69 | abstract protected function get_review_min_required_score(): int; |
| 70 | } |
| 71 |