1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\services\registration\form\v1; |
4
|
|
|
|
5
|
|
|
use EE_Answer; |
6
|
|
|
use EE_Error; |
7
|
|
|
use EE_Question; |
8
|
|
|
use EE_Registration; |
9
|
|
|
use EE_State; |
10
|
|
|
use EEM_State; |
11
|
|
|
use ReflectionException; |
12
|
|
|
|
13
|
|
|
class StateOptions |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* the action being performed on the current step |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
public $action = ''; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var EEM_State |
24
|
|
|
*/ |
25
|
|
|
public $state_model; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var [][] |
29
|
|
|
*/ |
30
|
|
|
private $state_options = []; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* CountryOptions constructor. |
35
|
|
|
* |
36
|
|
|
* @param string $action |
37
|
|
|
* @param EEM_State $state_model |
38
|
|
|
*/ |
39
|
|
|
public function __construct(string $action, EEM_State $state_model) |
40
|
|
|
{ |
41
|
|
|
$this->action = $action; |
42
|
|
|
$this->state_model = $state_model; |
43
|
|
|
add_filter( |
44
|
|
|
'FHEE__EE_Question__generate_form_input__state_options', |
45
|
|
|
[$this, 'forLegacyFormInput'], |
46
|
|
|
10, |
47
|
|
|
4 |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Gets the list of states for the form input |
54
|
|
|
* |
55
|
|
|
* @param array|null $states_list deprecated prop from an old hook |
56
|
|
|
* @param EE_Question|null $question |
57
|
|
|
* @param EE_Registration|null $registration |
58
|
|
|
* @param EE_Answer|null $answer |
59
|
|
|
* @return array 2d keys are state IDs, values are their names |
60
|
|
|
* @throws EE_Error |
61
|
|
|
* @throws ReflectionException |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function forLegacyFormInput( |
64
|
|
|
array $states_list = null, |
65
|
|
|
EE_Question $question = null, |
66
|
|
|
EE_Registration $registration = null, |
67
|
|
|
EE_Answer $answer = null |
68
|
|
|
): array { |
69
|
|
|
if (! isset($this->state_options[ $this->action ])) { |
70
|
|
|
$this->generateLegacyStateOptions($question, $registration, $answer); |
71
|
|
|
} |
72
|
|
|
return $this->state_options[ $this->action ]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param EE_Question|null $question |
78
|
|
|
* @param EE_Registration|null $registration |
79
|
|
|
* @param EE_Answer|null $answer |
80
|
|
|
* @throws EE_Error |
81
|
|
|
* @throws ReflectionException |
82
|
|
|
*/ |
83
|
|
|
private function generateLegacyStateOptions( |
84
|
|
|
EE_Question $question = null, |
85
|
|
|
EE_Registration $registration = null, |
86
|
|
|
EE_Answer $answer = null |
87
|
|
|
) { |
88
|
|
|
$state_options = ['' => ['' => '']]; |
89
|
|
|
$states = $this->action === 'process_reg_step' |
90
|
|
|
? $this->state_model->get_all_states() |
91
|
|
|
: $this->state_model->get_all_active_states(); |
92
|
|
|
if (! empty($states)) { |
93
|
|
View Code Duplication |
foreach ($states as $state) { |
|
|
|
|
94
|
|
|
if ($state instanceof EE_State) { |
95
|
|
|
$state_options[ $state->country()->name() ][ $state->ID() ] = $state->name(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
$this->state_options[ $this->action ] = apply_filters( |
100
|
|
|
'FHEE__EE_SPCO_Reg_Step_Attendee_Information___generate_question_input__state_options', |
101
|
|
|
$state_options, |
102
|
|
|
$this, |
103
|
|
|
$registration, |
104
|
|
|
$question, |
105
|
|
|
$answer |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
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.