1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\services\registration\form\v1; |
4
|
|
|
|
5
|
|
|
use EE_Answer; |
6
|
|
|
use EE_Country; |
7
|
|
|
use EE_Error; |
8
|
|
|
use EE_Question; |
9
|
|
|
use EE_Registration; |
10
|
|
|
use EEM_Answer; |
11
|
|
|
use EEM_Country; |
12
|
|
|
use ReflectionException; |
13
|
|
|
|
14
|
|
|
class CountryOptions |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* the action being performed on the current step |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
public $action = ''; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var EEM_Answer |
25
|
|
|
*/ |
26
|
|
|
public $answer_model; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var EEM_Country |
30
|
|
|
*/ |
31
|
|
|
public $country_model; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var [][] |
35
|
|
|
*/ |
36
|
|
|
private $country_options = []; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* CountryOptions constructor. |
41
|
|
|
* |
42
|
|
|
* @param string $action |
43
|
|
|
* @param EEM_Answer $answer_model |
44
|
|
|
* @param EEM_Country $country_model |
45
|
|
|
*/ |
46
|
|
|
public function __construct(string $action, EEM_Answer $answer_model, EEM_Country $country_model) |
47
|
|
|
{ |
48
|
|
|
$this->action = $action; |
49
|
|
|
$this->answer_model = $answer_model; |
50
|
|
|
$this->country_model = $country_model; |
51
|
|
|
add_filter( |
52
|
|
|
'FHEE__EE_Question__generate_form_input__country_options', |
53
|
|
|
[$this, 'forLegacyFormInput'], |
54
|
|
|
10, |
55
|
|
|
4 |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Gets the list of countries for the form input |
62
|
|
|
* |
63
|
|
|
* @param array|null $countries_list deprecated prop from an old hook |
64
|
|
|
* @param EE_Question|null $question |
65
|
|
|
* @param EE_Registration|null $registration |
66
|
|
|
* @param EE_Answer|null $answer deprecated prop from an old hook |
67
|
|
|
* @return array 2d keys are country IDs, values are their names |
68
|
|
|
* @throws EE_Error |
69
|
|
|
* @throws ReflectionException |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function forLegacyFormInput( |
72
|
|
|
array $countries_list = null, |
73
|
|
|
EE_Question $question = null, |
74
|
|
|
EE_Registration $registration = null, |
75
|
|
|
EE_Answer $answer = null |
76
|
|
|
): array { |
77
|
|
|
if (! isset($this->country_options[ $this->action ])) { |
78
|
|
|
$this->generateLegacyCountryOptions($question, $registration); |
79
|
|
|
} |
80
|
|
|
return $this->country_options[ $this->action ]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param EE_Question|null $question |
86
|
|
|
* @param EE_Registration|null $registration |
87
|
|
|
* @throws EE_Error |
88
|
|
|
* @throws ReflectionException |
89
|
|
|
*/ |
90
|
|
|
private function generateLegacyCountryOptions(EE_Question $question = null, EE_Registration $registration = null) |
91
|
|
|
{ |
92
|
|
|
// get possibly cached list of countries |
93
|
|
|
$countries = $this->action === 'process_reg_step' |
94
|
|
|
? $this->country_model->get_all_countries() |
95
|
|
|
: $this->country_model->get_all_active_countries(); |
96
|
|
|
// start with an empty option |
97
|
|
|
$country_options = ['' => '']; |
98
|
|
|
if (! empty($countries)) { |
99
|
|
|
foreach ($countries as $country) { |
|
|
|
|
100
|
|
|
if ($country instanceof EE_Country) { |
101
|
|
|
$country_options[ $country->ID() ] = $country->name(); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
if ($question instanceof EE_Question && $registration instanceof EE_Registration) { |
106
|
|
|
$answer = $this->answer_model->get_one( |
107
|
|
|
[['QST_ID' => $question->ID(), 'REG_ID' => $registration->ID()]] |
108
|
|
|
); |
109
|
|
|
} else { |
110
|
|
|
$answer = EE_Answer::new_instance(); |
111
|
|
|
} |
112
|
|
|
$this->country_options[ $this->action ] = apply_filters( |
113
|
|
|
'FHEE__EE_SPCO_Reg_Step_Attendee_Information___generate_question_input__country_options', |
114
|
|
|
$country_options, |
115
|
|
|
$this, |
116
|
|
|
$registration, |
117
|
|
|
$question, |
118
|
|
|
$answer |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.