@@ -11,130 +11,130 @@ |
||
11 | 11 | */ |
12 | 12 | class EEM_Form_Submission extends EEM_Base |
13 | 13 | { |
14 | - /** |
|
15 | - * @var EEM_Form_Submission |
|
16 | - */ |
|
17 | - protected static $_instance; |
|
14 | + /** |
|
15 | + * @var EEM_Form_Submission |
|
16 | + */ |
|
17 | + protected static $_instance; |
|
18 | 18 | |
19 | - /** |
|
20 | - * @var RequestInterface |
|
21 | - */ |
|
22 | - private $request; |
|
19 | + /** |
|
20 | + * @var RequestInterface |
|
21 | + */ |
|
22 | + private $request; |
|
23 | 23 | |
24 | 24 | |
25 | - /** |
|
26 | - * EEM_Form_Submission constructor. |
|
27 | - * |
|
28 | - * @param string|null $timezone |
|
29 | - * @throws EE_Error |
|
30 | - */ |
|
31 | - protected function __construct(?string $timezone = '') |
|
32 | - { |
|
33 | - $this->singular_item = esc_html__('Form Submission', 'event_espresso'); |
|
34 | - $this->plural_item = esc_html__('Form Submissions', 'event_espresso'); |
|
25 | + /** |
|
26 | + * EEM_Form_Submission constructor. |
|
27 | + * |
|
28 | + * @param string|null $timezone |
|
29 | + * @throws EE_Error |
|
30 | + */ |
|
31 | + protected function __construct(?string $timezone = '') |
|
32 | + { |
|
33 | + $this->singular_item = esc_html__('Form Submission', 'event_espresso'); |
|
34 | + $this->plural_item = esc_html__('Form Submissions', 'event_espresso'); |
|
35 | 35 | |
36 | - $this->_tables = [ |
|
37 | - 'Form_Submission' => new EE_Primary_Table('esp_form_submission', 'FSB_UUID'), |
|
38 | - ]; |
|
39 | - $this->_fields = [ |
|
40 | - 'Form_Submission' => [ |
|
41 | - 'FSB_UUID' => new EE_Primary_Key_String_Field( |
|
42 | - 'FSB_UUID', |
|
43 | - esc_html__('Form Submission UUID (universally unique identifier)', 'event_espresso') |
|
44 | - ), |
|
45 | - 'FSC_UUID' => new EE_Foreign_Key_String_Field( |
|
46 | - 'FSC_UUID', |
|
47 | - esc_html__('Form Section UUID (universally unique identifier)', 'event_espresso'), |
|
48 | - false, |
|
49 | - '', |
|
50 | - 'Form_Section', |
|
51 | - false |
|
52 | - ), |
|
53 | - 'TXN_ID' => new EE_Foreign_Key_Int_Field( |
|
54 | - 'TXN_ID', |
|
55 | - esc_html__('Transaction ID', 'event_espresso'), |
|
56 | - false, |
|
57 | - 0, |
|
58 | - 'Transaction' |
|
59 | - ), |
|
60 | - 'FSB_data' => new EE_JSON_Field( |
|
61 | - 'FSB_data', |
|
62 | - esc_html__('Serialized form submission data', 'event_espresso'), |
|
63 | - true, |
|
64 | - null |
|
65 | - ), |
|
66 | - 'FSB_submitted' => new EE_Datetime_Field( |
|
67 | - 'FSB_submitted', |
|
68 | - esc_html__('Form submission timestamp', 'event_espresso'), |
|
69 | - false, |
|
70 | - EE_Datetime_Field::now, |
|
71 | - $timezone |
|
72 | - ), |
|
73 | - ], |
|
74 | - ]; |
|
75 | - $this->_model_relations = [ |
|
76 | - 'Form_Section' => new EE_Belongs_To_Relation(), |
|
77 | - 'Transaction' => new EE_Belongs_To_Relation(), |
|
78 | - ]; |
|
79 | - parent::__construct($timezone); |
|
80 | - $this->request = $this->getLoader()->getShared('EventEspresso\core\services\request\RequestInterface'); |
|
81 | - } |
|
36 | + $this->_tables = [ |
|
37 | + 'Form_Submission' => new EE_Primary_Table('esp_form_submission', 'FSB_UUID'), |
|
38 | + ]; |
|
39 | + $this->_fields = [ |
|
40 | + 'Form_Submission' => [ |
|
41 | + 'FSB_UUID' => new EE_Primary_Key_String_Field( |
|
42 | + 'FSB_UUID', |
|
43 | + esc_html__('Form Submission UUID (universally unique identifier)', 'event_espresso') |
|
44 | + ), |
|
45 | + 'FSC_UUID' => new EE_Foreign_Key_String_Field( |
|
46 | + 'FSC_UUID', |
|
47 | + esc_html__('Form Section UUID (universally unique identifier)', 'event_espresso'), |
|
48 | + false, |
|
49 | + '', |
|
50 | + 'Form_Section', |
|
51 | + false |
|
52 | + ), |
|
53 | + 'TXN_ID' => new EE_Foreign_Key_Int_Field( |
|
54 | + 'TXN_ID', |
|
55 | + esc_html__('Transaction ID', 'event_espresso'), |
|
56 | + false, |
|
57 | + 0, |
|
58 | + 'Transaction' |
|
59 | + ), |
|
60 | + 'FSB_data' => new EE_JSON_Field( |
|
61 | + 'FSB_data', |
|
62 | + esc_html__('Serialized form submission data', 'event_espresso'), |
|
63 | + true, |
|
64 | + null |
|
65 | + ), |
|
66 | + 'FSB_submitted' => new EE_Datetime_Field( |
|
67 | + 'FSB_submitted', |
|
68 | + esc_html__('Form submission timestamp', 'event_espresso'), |
|
69 | + false, |
|
70 | + EE_Datetime_Field::now, |
|
71 | + $timezone |
|
72 | + ), |
|
73 | + ], |
|
74 | + ]; |
|
75 | + $this->_model_relations = [ |
|
76 | + 'Form_Section' => new EE_Belongs_To_Relation(), |
|
77 | + 'Transaction' => new EE_Belongs_To_Relation(), |
|
78 | + ]; |
|
79 | + parent::__construct($timezone); |
|
80 | + $this->request = $this->getLoader()->getShared('EventEspresso\core\services\request\RequestInterface'); |
|
81 | + } |
|
82 | 82 | |
83 | 83 | |
84 | - /** |
|
85 | - * adds all default where conditions unless the current request originates from the admin |
|
86 | - * |
|
87 | - * @param array $query_params |
|
88 | - * @return array |
|
89 | - */ |
|
90 | - private function addDefaultWhereConditions(array $query_params): array |
|
91 | - { |
|
92 | - // might need to add a way to identify GQL requests for admin domains |
|
93 | - $query_params['default_where_conditions'] = $this->request->isAdmin() || $this->request->isAdminAjax() |
|
94 | - ? EEM_Base::default_where_conditions_none |
|
95 | - : EEM_Base::default_where_conditions_all; |
|
96 | - return $query_params; |
|
97 | - } |
|
84 | + /** |
|
85 | + * adds all default where conditions unless the current request originates from the admin |
|
86 | + * |
|
87 | + * @param array $query_params |
|
88 | + * @return array |
|
89 | + */ |
|
90 | + private function addDefaultWhereConditions(array $query_params): array |
|
91 | + { |
|
92 | + // might need to add a way to identify GQL requests for admin domains |
|
93 | + $query_params['default_where_conditions'] = $this->request->isAdmin() || $this->request->isAdminAjax() |
|
94 | + ? EEM_Base::default_where_conditions_none |
|
95 | + : EEM_Base::default_where_conditions_all; |
|
96 | + return $query_params; |
|
97 | + } |
|
98 | 98 | |
99 | 99 | |
100 | - /** |
|
101 | - * form sections should always be sorted in ascending order via the FSC_order field |
|
102 | - * |
|
103 | - * @param array $query_params |
|
104 | - * @return array |
|
105 | - */ |
|
106 | - private function addOrderByQueryParams(array $query_params): array |
|
107 | - { |
|
108 | - $query_params['order_by'] = ['FSB_submitted' => 'ASC']; |
|
109 | - return $query_params; |
|
110 | - } |
|
100 | + /** |
|
101 | + * form sections should always be sorted in ascending order via the FSC_order field |
|
102 | + * |
|
103 | + * @param array $query_params |
|
104 | + * @return array |
|
105 | + */ |
|
106 | + private function addOrderByQueryParams(array $query_params): array |
|
107 | + { |
|
108 | + $query_params['order_by'] = ['FSB_submitted' => 'ASC']; |
|
109 | + return $query_params; |
|
110 | + } |
|
111 | 111 | |
112 | 112 | |
113 | - /** |
|
114 | - * @param EE_Event $event |
|
115 | - * @return EE_Form_Submission[]|null |
|
116 | - * @throws EE_Error |
|
117 | - * @throws ReflectionException |
|
118 | - */ |
|
119 | - public function getAllFormSubmissionsForEvent(EE_Event $event): ?array |
|
120 | - { |
|
121 | - $query_params = [['FSC_UUID' => $event->registrationFormUuid()]]; |
|
122 | - $query_params = $this->addDefaultWhereConditions($query_params); |
|
123 | - $query_params = $this->addOrderByQueryParams($query_params); |
|
124 | - return $this->get_all($query_params); |
|
125 | - } |
|
113 | + /** |
|
114 | + * @param EE_Event $event |
|
115 | + * @return EE_Form_Submission[]|null |
|
116 | + * @throws EE_Error |
|
117 | + * @throws ReflectionException |
|
118 | + */ |
|
119 | + public function getAllFormSubmissionsForEvent(EE_Event $event): ?array |
|
120 | + { |
|
121 | + $query_params = [['FSC_UUID' => $event->registrationFormUuid()]]; |
|
122 | + $query_params = $this->addDefaultWhereConditions($query_params); |
|
123 | + $query_params = $this->addOrderByQueryParams($query_params); |
|
124 | + return $this->get_all($query_params); |
|
125 | + } |
|
126 | 126 | |
127 | 127 | |
128 | - /** |
|
129 | - * @param EE_Transaction $transaction |
|
130 | - * @return EE_Form_Submission|null |
|
131 | - * @throws EE_Error |
|
132 | - * @throws ReflectionException |
|
133 | - */ |
|
134 | - public function getFormSubmissionForTransaction(EE_Transaction $transaction): ?EE_Form_Submission |
|
135 | - { |
|
136 | - $query_params = [['TXN_ID' => $transaction->ID()]]; |
|
137 | - $query_params = $this->addDefaultWhereConditions($query_params); |
|
138 | - return $this->get_one($query_params); |
|
139 | - } |
|
128 | + /** |
|
129 | + * @param EE_Transaction $transaction |
|
130 | + * @return EE_Form_Submission|null |
|
131 | + * @throws EE_Error |
|
132 | + * @throws ReflectionException |
|
133 | + */ |
|
134 | + public function getFormSubmissionForTransaction(EE_Transaction $transaction): ?EE_Form_Submission |
|
135 | + { |
|
136 | + $query_params = [['TXN_ID' => $transaction->ID()]]; |
|
137 | + $query_params = $this->addDefaultWhereConditions($query_params); |
|
138 | + return $this->get_one($query_params); |
|
139 | + } |
|
140 | 140 | } |
@@ -10,50 +10,50 @@ |
||
10 | 10 | */ |
11 | 11 | class EEM_Ticket_Price extends EEM_Base |
12 | 12 | { |
13 | - // private instance of the EEM_Ticket_Price object |
|
14 | - protected static $_instance = null; |
|
13 | + // private instance of the EEM_Ticket_Price object |
|
14 | + protected static $_instance = null; |
|
15 | 15 | |
16 | 16 | |
17 | - /** |
|
18 | - * private constructor to prevent direct creation |
|
19 | - * |
|
20 | - * @param string|null $timezone string representing the timezone we want to set for returned Date Time Strings (and |
|
21 | - * any incoming timezone data that gets saved). Note this just sends the timezone |
|
22 | - * info to the date time model field objects. Default is NULL (and will be assumed |
|
23 | - * using the set timezone in the 'timezone_string' wp option) |
|
24 | - * @throws EE_Error |
|
25 | - */ |
|
26 | - protected function __construct(?string $timezone = '') |
|
27 | - { |
|
28 | - $this->singular_item = esc_html__('Ticket Price', 'event_espresso'); |
|
29 | - $this->plural_item = esc_html__('Ticket Prices', 'event_espresso'); |
|
17 | + /** |
|
18 | + * private constructor to prevent direct creation |
|
19 | + * |
|
20 | + * @param string|null $timezone string representing the timezone we want to set for returned Date Time Strings (and |
|
21 | + * any incoming timezone data that gets saved). Note this just sends the timezone |
|
22 | + * info to the date time model field objects. Default is NULL (and will be assumed |
|
23 | + * using the set timezone in the 'timezone_string' wp option) |
|
24 | + * @throws EE_Error |
|
25 | + */ |
|
26 | + protected function __construct(?string $timezone = '') |
|
27 | + { |
|
28 | + $this->singular_item = esc_html__('Ticket Price', 'event_espresso'); |
|
29 | + $this->plural_item = esc_html__('Ticket Prices', 'event_espresso'); |
|
30 | 30 | |
31 | - $this->_tables = [ |
|
32 | - 'Ticket_Price' => new EE_Primary_Table('esp_ticket_price', 'TKP_ID'), |
|
33 | - ]; |
|
34 | - $this->_fields = [ |
|
35 | - 'Ticket_Price' => [ |
|
36 | - 'TKP_ID' => new EE_Primary_Key_Int_Field('TKP_ID', 'Ticket Price ID'), |
|
37 | - 'TKT_ID' => new EE_Foreign_Key_Int_Field('TKT_ID', 'Ticket Id', false, 0, 'Ticket'), |
|
38 | - 'PRC_ID' => new EE_Foreign_Key_Int_Field('PRC_ID', 'Price ID', false, 0, 'Price'), |
|
39 | - ], |
|
40 | - ]; |
|
41 | - $this->_model_relations = [ |
|
42 | - 'Ticket' => new EE_Belongs_To_Relation(), |
|
43 | - 'Price' => new EE_Belongs_To_Relation(), |
|
44 | - ]; |
|
45 | - $this->_model_chain_to_wp_user = 'Ticket'; |
|
46 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = |
|
47 | - new EE_Restriction_Generator_Default_Public('Ticket.TKT_is_default', 'Ticket.Datetime.Event'); |
|
48 | - // account for default tickets in the caps |
|
49 | - $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = |
|
50 | - new EE_Restriction_Generator_Default_Protected('Ticket.TKT_is_default', 'Ticket.Datetime.Event'); |
|
51 | - $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = |
|
52 | - new EE_Restriction_Generator_Default_Protected('Ticket.TKT_is_default', 'Ticket.Datetime.Event'); |
|
53 | - $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = |
|
54 | - new EE_Restriction_Generator_Default_Protected('Ticket.TKT_is_default', 'Ticket.Datetime.Event'); |
|
55 | - // follow the caps of the ticket |
|
56 | - $this->_caps_slug = 'tickets'; |
|
57 | - parent::__construct($timezone); |
|
58 | - } |
|
31 | + $this->_tables = [ |
|
32 | + 'Ticket_Price' => new EE_Primary_Table('esp_ticket_price', 'TKP_ID'), |
|
33 | + ]; |
|
34 | + $this->_fields = [ |
|
35 | + 'Ticket_Price' => [ |
|
36 | + 'TKP_ID' => new EE_Primary_Key_Int_Field('TKP_ID', 'Ticket Price ID'), |
|
37 | + 'TKT_ID' => new EE_Foreign_Key_Int_Field('TKT_ID', 'Ticket Id', false, 0, 'Ticket'), |
|
38 | + 'PRC_ID' => new EE_Foreign_Key_Int_Field('PRC_ID', 'Price ID', false, 0, 'Price'), |
|
39 | + ], |
|
40 | + ]; |
|
41 | + $this->_model_relations = [ |
|
42 | + 'Ticket' => new EE_Belongs_To_Relation(), |
|
43 | + 'Price' => new EE_Belongs_To_Relation(), |
|
44 | + ]; |
|
45 | + $this->_model_chain_to_wp_user = 'Ticket'; |
|
46 | + $this->_cap_restriction_generators[ EEM_Base::caps_read ] = |
|
47 | + new EE_Restriction_Generator_Default_Public('Ticket.TKT_is_default', 'Ticket.Datetime.Event'); |
|
48 | + // account for default tickets in the caps |
|
49 | + $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = |
|
50 | + new EE_Restriction_Generator_Default_Protected('Ticket.TKT_is_default', 'Ticket.Datetime.Event'); |
|
51 | + $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = |
|
52 | + new EE_Restriction_Generator_Default_Protected('Ticket.TKT_is_default', 'Ticket.Datetime.Event'); |
|
53 | + $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = |
|
54 | + new EE_Restriction_Generator_Default_Protected('Ticket.TKT_is_default', 'Ticket.Datetime.Event'); |
|
55 | + // follow the caps of the ticket |
|
56 | + $this->_caps_slug = 'tickets'; |
|
57 | + parent::__construct($timezone); |
|
58 | + } |
|
59 | 59 | } |
@@ -38,19 +38,19 @@ |
||
38 | 38 | 'PRC_ID' => new EE_Foreign_Key_Int_Field('PRC_ID', 'Price ID', false, 0, 'Price'), |
39 | 39 | ], |
40 | 40 | ]; |
41 | - $this->_model_relations = [ |
|
41 | + $this->_model_relations = [ |
|
42 | 42 | 'Ticket' => new EE_Belongs_To_Relation(), |
43 | 43 | 'Price' => new EE_Belongs_To_Relation(), |
44 | 44 | ]; |
45 | 45 | $this->_model_chain_to_wp_user = 'Ticket'; |
46 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = |
|
46 | + $this->_cap_restriction_generators[EEM_Base::caps_read] = |
|
47 | 47 | new EE_Restriction_Generator_Default_Public('Ticket.TKT_is_default', 'Ticket.Datetime.Event'); |
48 | 48 | // account for default tickets in the caps |
49 | - $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = |
|
49 | + $this->_cap_restriction_generators[EEM_Base::caps_read_admin] = |
|
50 | 50 | new EE_Restriction_Generator_Default_Protected('Ticket.TKT_is_default', 'Ticket.Datetime.Event'); |
51 | - $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = |
|
51 | + $this->_cap_restriction_generators[EEM_Base::caps_edit] = |
|
52 | 52 | new EE_Restriction_Generator_Default_Protected('Ticket.TKT_is_default', 'Ticket.Datetime.Event'); |
53 | - $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = |
|
53 | + $this->_cap_restriction_generators[EEM_Base::caps_delete] = |
|
54 | 54 | new EE_Restriction_Generator_Default_Protected('Ticket.TKT_is_default', 'Ticket.Datetime.Event'); |
55 | 55 | // follow the caps of the ticket |
56 | 56 | $this->_caps_slug = 'tickets'; |
@@ -26,237 +26,237 @@ |
||
26 | 26 | */ |
27 | 27 | class EEM_Form_Element extends EEM_Base |
28 | 28 | { |
29 | - /** |
|
30 | - * @var EEM_Form_Element |
|
31 | - */ |
|
32 | - protected static $_instance; |
|
29 | + /** |
|
30 | + * @var EEM_Form_Element |
|
31 | + */ |
|
32 | + protected static $_instance; |
|
33 | 33 | |
34 | - /** |
|
35 | - * @var RequestInterface |
|
36 | - */ |
|
37 | - private $request; |
|
34 | + /** |
|
35 | + * @var RequestInterface |
|
36 | + */ |
|
37 | + private $request; |
|
38 | 38 | |
39 | - /** |
|
40 | - * @var InputTypes |
|
41 | - */ |
|
42 | - private $input_types; |
|
39 | + /** |
|
40 | + * @var InputTypes |
|
41 | + */ |
|
42 | + private $input_types; |
|
43 | 43 | |
44 | 44 | |
45 | - /** |
|
46 | - * EEM_Form_Element constructor. |
|
47 | - * |
|
48 | - * @param FormStatus $form_status |
|
49 | - * @param InputTypes $input_types |
|
50 | - * @param string|null $timezone |
|
51 | - * @throws EE_Error |
|
52 | - */ |
|
53 | - protected function __construct(FormStatus $form_status, InputTypes $input_types, ?string $timezone = '') |
|
54 | - { |
|
55 | - $this->input_types = $input_types; |
|
56 | - $this->singular_item = esc_html__('Form Element', 'event_espresso'); |
|
57 | - $this->plural_item = esc_html__('Form Elements', 'event_espresso'); |
|
45 | + /** |
|
46 | + * EEM_Form_Element constructor. |
|
47 | + * |
|
48 | + * @param FormStatus $form_status |
|
49 | + * @param InputTypes $input_types |
|
50 | + * @param string|null $timezone |
|
51 | + * @throws EE_Error |
|
52 | + */ |
|
53 | + protected function __construct(FormStatus $form_status, InputTypes $input_types, ?string $timezone = '') |
|
54 | + { |
|
55 | + $this->input_types = $input_types; |
|
56 | + $this->singular_item = esc_html__('Form Element', 'event_espresso'); |
|
57 | + $this->plural_item = esc_html__('Form Elements', 'event_espresso'); |
|
58 | 58 | |
59 | - $this->_tables = [ |
|
60 | - 'Form_Element' => new EE_Primary_Table('esp_form_element', 'FIN_UUID'), |
|
61 | - ]; |
|
62 | - $this->_fields = [ |
|
63 | - 'Form_Element' => [ |
|
64 | - 'FIN_UUID' => new EE_Primary_Key_String_Field( |
|
65 | - 'FIN_UUID', |
|
66 | - esc_html__('Form Element UUID (universally unique identifier)', 'event_espresso') |
|
67 | - ), |
|
68 | - 'FSC_UUID' => new EE_Foreign_Key_String_Field( |
|
69 | - 'FSC_UUID', |
|
70 | - esc_html__('UUID of parent form section this form input belongs to.', 'event_espresso'), |
|
71 | - false, |
|
72 | - null, |
|
73 | - ['Form_Section'], |
|
74 | - false |
|
75 | - ), |
|
76 | - 'FIN_adminOnly' => new EE_Boolean_Field( |
|
77 | - 'FIN_adminOnly', |
|
78 | - esc_html__( |
|
79 | - 'Whether or not input is only displayed in the admin. If false, input will appear in public forms', |
|
80 | - 'event_espresso' |
|
81 | - ), |
|
82 | - false, |
|
83 | - false |
|
84 | - ), |
|
85 | - 'FIN_attributes' => new EE_JSON_Field( |
|
86 | - 'FIN_attributes', |
|
87 | - esc_html__( |
|
88 | - 'JSON string of HTML attributes such as class, max, min, placeholder, type, etc.', |
|
89 | - 'event_espresso' |
|
90 | - ), |
|
91 | - true, |
|
92 | - '{}' |
|
93 | - ), |
|
94 | - 'FIN_helpText' => new EE_JSON_Field( |
|
95 | - 'FIN_helpText', |
|
96 | - esc_html__( |
|
97 | - 'JSON string of properties pertaining to any help text required for an input.', |
|
98 | - 'event_espresso' |
|
99 | - ), |
|
100 | - true, |
|
101 | - '{}' |
|
102 | - ), |
|
103 | - 'FIN_label' => new EE_JSON_Field( |
|
104 | - 'FIN_label', |
|
105 | - esc_html__( |
|
106 | - 'JSON string of properties pertaining to an element\'s label.', |
|
107 | - 'event_espresso' |
|
108 | - ), |
|
109 | - true, |
|
110 | - '{}' |
|
111 | - ), |
|
112 | - 'FIN_mapsTo' => new EE_Plain_Text_Field( |
|
113 | - 'FIN_mapsTo', |
|
114 | - esc_html__( |
|
115 | - 'Model and Fields name that this element maps to; ex: Attendee.email', |
|
116 | - 'event_espresso' |
|
117 | - ), |
|
118 | - true, |
|
119 | - null |
|
120 | - ), |
|
121 | - 'FIN_options' => new EE_JSON_Field( |
|
122 | - 'FIN_options', |
|
123 | - esc_html__( |
|
124 | - 'JSON string of options for ENUM type inputs like checkboxes, radio buttons, select inputs, etc.', |
|
125 | - 'event_espresso' |
|
126 | - ), |
|
127 | - true, |
|
128 | - '[]' |
|
129 | - ), |
|
130 | - 'FIN_order' => new EE_Integer_Field( |
|
131 | - 'FIN_order', |
|
132 | - esc_html__('Order in which form input appears in a form.', 'event_espresso'), |
|
133 | - false, |
|
134 | - 0 |
|
135 | - ), |
|
136 | - 'FIN_required' => new EE_JSON_Field( |
|
137 | - 'FIN_required', |
|
138 | - esc_html__( |
|
139 | - 'properties pertaining to an input\'s required status and the validation text to display.', |
|
140 | - 'event_espresso' |
|
141 | - ), |
|
142 | - false, |
|
143 | - false |
|
144 | - ), |
|
145 | - 'FIN_status' => new EE_Enum_Text_Field( |
|
146 | - 'FIN_status', |
|
147 | - esc_html( |
|
148 | - sprintf( |
|
149 | - /* translators: 1 class name */ |
|
150 | - __( |
|
151 | - 'Whether form element is active, archived, trashed, or used as a default on new forms. Values correspond to the %1$s class constants.', |
|
152 | - 'event_espresso' |
|
153 | - ), |
|
154 | - 'EventEspresso\core\services\form\meta\FormStatus' |
|
155 | - ) |
|
156 | - ), |
|
157 | - false, |
|
158 | - FormStatus::ACTIVE, |
|
159 | - $form_status->validStatusOptions() |
|
160 | - ), |
|
161 | - 'FIN_type' => new EE_Enum_Text_Field( |
|
162 | - 'FIN_type', |
|
163 | - esc_html__('Form element type.', 'event_espresso'), |
|
164 | - false, |
|
165 | - null, |
|
166 | - $input_types->validTypeOptions() |
|
167 | - ), |
|
168 | - 'FIN_wpUser' => new EE_WP_User_Field( |
|
169 | - 'FIN_wpUser', |
|
170 | - esc_html__('ID of the WP User that created this form input.', 'event_espresso'), |
|
171 | - false |
|
172 | - ), |
|
173 | - ], |
|
174 | - ]; |
|
59 | + $this->_tables = [ |
|
60 | + 'Form_Element' => new EE_Primary_Table('esp_form_element', 'FIN_UUID'), |
|
61 | + ]; |
|
62 | + $this->_fields = [ |
|
63 | + 'Form_Element' => [ |
|
64 | + 'FIN_UUID' => new EE_Primary_Key_String_Field( |
|
65 | + 'FIN_UUID', |
|
66 | + esc_html__('Form Element UUID (universally unique identifier)', 'event_espresso') |
|
67 | + ), |
|
68 | + 'FSC_UUID' => new EE_Foreign_Key_String_Field( |
|
69 | + 'FSC_UUID', |
|
70 | + esc_html__('UUID of parent form section this form input belongs to.', 'event_espresso'), |
|
71 | + false, |
|
72 | + null, |
|
73 | + ['Form_Section'], |
|
74 | + false |
|
75 | + ), |
|
76 | + 'FIN_adminOnly' => new EE_Boolean_Field( |
|
77 | + 'FIN_adminOnly', |
|
78 | + esc_html__( |
|
79 | + 'Whether or not input is only displayed in the admin. If false, input will appear in public forms', |
|
80 | + 'event_espresso' |
|
81 | + ), |
|
82 | + false, |
|
83 | + false |
|
84 | + ), |
|
85 | + 'FIN_attributes' => new EE_JSON_Field( |
|
86 | + 'FIN_attributes', |
|
87 | + esc_html__( |
|
88 | + 'JSON string of HTML attributes such as class, max, min, placeholder, type, etc.', |
|
89 | + 'event_espresso' |
|
90 | + ), |
|
91 | + true, |
|
92 | + '{}' |
|
93 | + ), |
|
94 | + 'FIN_helpText' => new EE_JSON_Field( |
|
95 | + 'FIN_helpText', |
|
96 | + esc_html__( |
|
97 | + 'JSON string of properties pertaining to any help text required for an input.', |
|
98 | + 'event_espresso' |
|
99 | + ), |
|
100 | + true, |
|
101 | + '{}' |
|
102 | + ), |
|
103 | + 'FIN_label' => new EE_JSON_Field( |
|
104 | + 'FIN_label', |
|
105 | + esc_html__( |
|
106 | + 'JSON string of properties pertaining to an element\'s label.', |
|
107 | + 'event_espresso' |
|
108 | + ), |
|
109 | + true, |
|
110 | + '{}' |
|
111 | + ), |
|
112 | + 'FIN_mapsTo' => new EE_Plain_Text_Field( |
|
113 | + 'FIN_mapsTo', |
|
114 | + esc_html__( |
|
115 | + 'Model and Fields name that this element maps to; ex: Attendee.email', |
|
116 | + 'event_espresso' |
|
117 | + ), |
|
118 | + true, |
|
119 | + null |
|
120 | + ), |
|
121 | + 'FIN_options' => new EE_JSON_Field( |
|
122 | + 'FIN_options', |
|
123 | + esc_html__( |
|
124 | + 'JSON string of options for ENUM type inputs like checkboxes, radio buttons, select inputs, etc.', |
|
125 | + 'event_espresso' |
|
126 | + ), |
|
127 | + true, |
|
128 | + '[]' |
|
129 | + ), |
|
130 | + 'FIN_order' => new EE_Integer_Field( |
|
131 | + 'FIN_order', |
|
132 | + esc_html__('Order in which form input appears in a form.', 'event_espresso'), |
|
133 | + false, |
|
134 | + 0 |
|
135 | + ), |
|
136 | + 'FIN_required' => new EE_JSON_Field( |
|
137 | + 'FIN_required', |
|
138 | + esc_html__( |
|
139 | + 'properties pertaining to an input\'s required status and the validation text to display.', |
|
140 | + 'event_espresso' |
|
141 | + ), |
|
142 | + false, |
|
143 | + false |
|
144 | + ), |
|
145 | + 'FIN_status' => new EE_Enum_Text_Field( |
|
146 | + 'FIN_status', |
|
147 | + esc_html( |
|
148 | + sprintf( |
|
149 | + /* translators: 1 class name */ |
|
150 | + __( |
|
151 | + 'Whether form element is active, archived, trashed, or used as a default on new forms. Values correspond to the %1$s class constants.', |
|
152 | + 'event_espresso' |
|
153 | + ), |
|
154 | + 'EventEspresso\core\services\form\meta\FormStatus' |
|
155 | + ) |
|
156 | + ), |
|
157 | + false, |
|
158 | + FormStatus::ACTIVE, |
|
159 | + $form_status->validStatusOptions() |
|
160 | + ), |
|
161 | + 'FIN_type' => new EE_Enum_Text_Field( |
|
162 | + 'FIN_type', |
|
163 | + esc_html__('Form element type.', 'event_espresso'), |
|
164 | + false, |
|
165 | + null, |
|
166 | + $input_types->validTypeOptions() |
|
167 | + ), |
|
168 | + 'FIN_wpUser' => new EE_WP_User_Field( |
|
169 | + 'FIN_wpUser', |
|
170 | + esc_html__('ID of the WP User that created this form input.', 'event_espresso'), |
|
171 | + false |
|
172 | + ), |
|
173 | + ], |
|
174 | + ]; |
|
175 | 175 | |
176 | - $this->_model_relations = [ |
|
177 | - 'Form_Section' => new EE_Belongs_To_Relation(), |
|
178 | - 'WP_User' => new EE_Belongs_To_Relation(), |
|
179 | - ]; |
|
180 | - // this model is generally available for reading |
|
181 | - $this->_cap_restriction_generators = [ |
|
182 | - EEM_Base::caps_read => new EE_Restriction_Generator_Public(), |
|
183 | - EEM_Base::caps_read_admin => new EE_Restriction_Generator_Reg_Form('FIN_applies_to'), |
|
184 | - EEM_Base::caps_edit => new EE_Restriction_Generator_Reg_Form('FIN_applies_to'), |
|
185 | - EEM_Base::caps_delete => new EE_Restriction_Generator_Reg_Form('FIN_applies_to'), |
|
186 | - ]; |
|
187 | - parent::__construct($timezone); |
|
188 | - $this->request = $this->getLoader()->getShared('EventEspresso\core\services\request\RequestInterface'); |
|
189 | - } |
|
176 | + $this->_model_relations = [ |
|
177 | + 'Form_Section' => new EE_Belongs_To_Relation(), |
|
178 | + 'WP_User' => new EE_Belongs_To_Relation(), |
|
179 | + ]; |
|
180 | + // this model is generally available for reading |
|
181 | + $this->_cap_restriction_generators = [ |
|
182 | + EEM_Base::caps_read => new EE_Restriction_Generator_Public(), |
|
183 | + EEM_Base::caps_read_admin => new EE_Restriction_Generator_Reg_Form('FIN_applies_to'), |
|
184 | + EEM_Base::caps_edit => new EE_Restriction_Generator_Reg_Form('FIN_applies_to'), |
|
185 | + EEM_Base::caps_delete => new EE_Restriction_Generator_Reg_Form('FIN_applies_to'), |
|
186 | + ]; |
|
187 | + parent::__construct($timezone); |
|
188 | + $this->request = $this->getLoader()->getShared('EventEspresso\core\services\request\RequestInterface'); |
|
189 | + } |
|
190 | 190 | |
191 | 191 | |
192 | - /** |
|
193 | - * @param array $query_params |
|
194 | - * @return array |
|
195 | - */ |
|
196 | - private function addDefaultWhereConditions(array $query_params): array |
|
197 | - { |
|
198 | - // might need to add a way to identify GQL requests for admin domains |
|
199 | - $admin_request = $this->request->isAdmin() || $this->request->isAdminAjax(); |
|
200 | - $query_params['default_where_conditions'] = $admin_request |
|
201 | - ? EEM_Base::default_where_conditions_none |
|
202 | - : EEM_Base::default_where_conditions_all; |
|
203 | - return $query_params; |
|
204 | - } |
|
192 | + /** |
|
193 | + * @param array $query_params |
|
194 | + * @return array |
|
195 | + */ |
|
196 | + private function addDefaultWhereConditions(array $query_params): array |
|
197 | + { |
|
198 | + // might need to add a way to identify GQL requests for admin domains |
|
199 | + $admin_request = $this->request->isAdmin() || $this->request->isAdminAjax(); |
|
200 | + $query_params['default_where_conditions'] = $admin_request |
|
201 | + ? EEM_Base::default_where_conditions_none |
|
202 | + : EEM_Base::default_where_conditions_all; |
|
203 | + return $query_params; |
|
204 | + } |
|
205 | 205 | |
206 | 206 | |
207 | - /** |
|
208 | - * form inputs should always be sorted in ascending order via the FIN_order field |
|
209 | - * |
|
210 | - * @param array $query_params |
|
211 | - * @return array |
|
212 | - */ |
|
213 | - private function addOrderByQueryParams(array $query_params): array |
|
214 | - { |
|
215 | - $query_params['order_by'] = ['FIN_order' => 'ASC']; |
|
216 | - return $query_params; |
|
217 | - } |
|
207 | + /** |
|
208 | + * form inputs should always be sorted in ascending order via the FIN_order field |
|
209 | + * |
|
210 | + * @param array $query_params |
|
211 | + * @return array |
|
212 | + */ |
|
213 | + private function addOrderByQueryParams(array $query_params): array |
|
214 | + { |
|
215 | + $query_params['order_by'] = ['FIN_order' => 'ASC']; |
|
216 | + return $query_params; |
|
217 | + } |
|
218 | 218 | |
219 | 219 | |
220 | - /** |
|
221 | - * @param EE_Form_Section $form_section |
|
222 | - * @param EE_Form_Element[] $all_form_elements |
|
223 | - * @return EE_Form_Element[] |
|
224 | - * @throws EE_Error |
|
225 | - * @throws ReflectionException |
|
226 | - */ |
|
227 | - public function filterFormElementsForFormSection(EE_Form_Section $form_section, array $all_form_elements): array |
|
228 | - { |
|
229 | - return array_filter($all_form_elements, $form_section->formElementFilter()); |
|
230 | - } |
|
220 | + /** |
|
221 | + * @param EE_Form_Section $form_section |
|
222 | + * @param EE_Form_Element[] $all_form_elements |
|
223 | + * @return EE_Form_Element[] |
|
224 | + * @throws EE_Error |
|
225 | + * @throws ReflectionException |
|
226 | + */ |
|
227 | + public function filterFormElementsForFormSection(EE_Form_Section $form_section, array $all_form_elements): array |
|
228 | + { |
|
229 | + return array_filter($all_form_elements, $form_section->formElementFilter()); |
|
230 | + } |
|
231 | 231 | |
232 | 232 | |
233 | - /** |
|
234 | - * @param EE_Form_Section[] $form_sections |
|
235 | - * @return EE_Form_Element[] |
|
236 | - * @throws EE_Error |
|
237 | - * @throws ReflectionException |
|
238 | - */ |
|
239 | - public function getAllFormElementsForFormSections(array $form_sections): array |
|
240 | - { |
|
241 | - $FSC_UUIDs = []; |
|
242 | - foreach ($form_sections as $form_section) { |
|
243 | - if ($form_section instanceof EE_Form_Section) { |
|
244 | - $FSC_UUIDs[] = $form_section->UUID(); |
|
245 | - } |
|
246 | - } |
|
247 | - $where_params = ['FSC_UUID' => ['IN', $FSC_UUIDs]]; |
|
248 | - $query_params = $this->addDefaultWhereConditions([$where_params]); |
|
249 | - $query_params = $this->addOrderByQueryParams($query_params); |
|
250 | - return $this->get_all($query_params); |
|
251 | - } |
|
233 | + /** |
|
234 | + * @param EE_Form_Section[] $form_sections |
|
235 | + * @return EE_Form_Element[] |
|
236 | + * @throws EE_Error |
|
237 | + * @throws ReflectionException |
|
238 | + */ |
|
239 | + public function getAllFormElementsForFormSections(array $form_sections): array |
|
240 | + { |
|
241 | + $FSC_UUIDs = []; |
|
242 | + foreach ($form_sections as $form_section) { |
|
243 | + if ($form_section instanceof EE_Form_Section) { |
|
244 | + $FSC_UUIDs[] = $form_section->UUID(); |
|
245 | + } |
|
246 | + } |
|
247 | + $where_params = ['FSC_UUID' => ['IN', $FSC_UUIDs]]; |
|
248 | + $query_params = $this->addDefaultWhereConditions([$where_params]); |
|
249 | + $query_params = $this->addOrderByQueryParams($query_params); |
|
250 | + return $this->get_all($query_params); |
|
251 | + } |
|
252 | 252 | |
253 | 253 | |
254 | - /** |
|
255 | - * @param bool $constants_only |
|
256 | - * @return array |
|
257 | - */ |
|
258 | - public function validTypeOptions(bool $constants_only = false): array |
|
259 | - { |
|
260 | - return $this->input_types->validTypeOptions($constants_only); |
|
261 | - } |
|
254 | + /** |
|
255 | + * @param bool $constants_only |
|
256 | + * @return array |
|
257 | + */ |
|
258 | + public function validTypeOptions(bool $constants_only = false): array |
|
259 | + { |
|
260 | + return $this->input_types->validTypeOptions($constants_only); |
|
261 | + } |
|
262 | 262 | } |
@@ -9,94 +9,94 @@ discard block |
||
9 | 9 | */ |
10 | 10 | class EEM_Change_Log extends EEM_Base |
11 | 11 | { |
12 | - /** |
|
13 | - * the related object was created log type |
|
14 | - */ |
|
15 | - const type_create = 'create'; |
|
12 | + /** |
|
13 | + * the related object was created log type |
|
14 | + */ |
|
15 | + const type_create = 'create'; |
|
16 | 16 | |
17 | - /** |
|
18 | - * the related object was updated (changed, or soft-deleted) |
|
19 | - */ |
|
20 | - const type_update = 'update'; |
|
17 | + /** |
|
18 | + * the related object was updated (changed, or soft-deleted) |
|
19 | + */ |
|
20 | + const type_update = 'update'; |
|
21 | 21 | |
22 | - /** |
|
23 | - * the related object was trashed/restored/deleted |
|
24 | - */ |
|
25 | - const type_delete = 'delete'; |
|
22 | + /** |
|
23 | + * the related object was trashed/restored/deleted |
|
24 | + */ |
|
25 | + const type_delete = 'delete'; |
|
26 | 26 | |
27 | - /** |
|
28 | - * the related item had something worth noting happen on it, but |
|
29 | - * only for the purposes of debugging problems |
|
30 | - */ |
|
31 | - const type_debug = 'debug'; |
|
27 | + /** |
|
28 | + * the related item had something worth noting happen on it, but |
|
29 | + * only for the purposes of debugging problems |
|
30 | + */ |
|
31 | + const type_debug = 'debug'; |
|
32 | 32 | |
33 | - /** |
|
34 | - * the related item had an error occur on it |
|
35 | - */ |
|
36 | - const type_error = 'error'; |
|
33 | + /** |
|
34 | + * the related item had an error occur on it |
|
35 | + */ |
|
36 | + const type_error = 'error'; |
|
37 | 37 | |
38 | - /** |
|
39 | - * the related item is regarding some gateway interaction, like an IPN |
|
40 | - * or request to process a payment |
|
41 | - */ |
|
42 | - const type_gateway = 'gateway'; |
|
38 | + /** |
|
39 | + * the related item is regarding some gateway interaction, like an IPN |
|
40 | + * or request to process a payment |
|
41 | + */ |
|
42 | + const type_gateway = 'gateway'; |
|
43 | 43 | |
44 | - /** |
|
45 | - * private instance of the EEM_Change_Log object |
|
46 | - * |
|
47 | - * @access private |
|
48 | - * @var EEM_Change_Log $_instance |
|
49 | - */ |
|
50 | - protected static $_instance = null; |
|
44 | + /** |
|
45 | + * private instance of the EEM_Change_Log object |
|
46 | + * |
|
47 | + * @access private |
|
48 | + * @var EEM_Change_Log $_instance |
|
49 | + */ |
|
50 | + protected static $_instance = null; |
|
51 | 51 | |
52 | 52 | |
53 | - /** |
|
54 | - * @param string|null $timezone |
|
55 | - * @throws EE_Error |
|
56 | - */ |
|
57 | - protected function __construct(?string $timezone = '') |
|
58 | - { |
|
59 | - $this->singular_item = esc_html__('Log', 'event_espresso'); |
|
60 | - $this->plural_item = esc_html__('Logs', 'event_espresso'); |
|
61 | - $this->_tables = [ |
|
62 | - 'Log' => new EE_Primary_Table('esp_log', 'LOG_ID'), |
|
63 | - ]; |
|
64 | - $models_this_can_attach_to = array_keys(EE_Registry::instance()->non_abstract_db_models); |
|
65 | - $this->_fields = [ |
|
66 | - 'Log' => [ |
|
67 | - 'LOG_ID' => new EE_Primary_Key_Int_Field('LOG_ID', esc_html__('Log ID', 'event_espresso')), |
|
68 | - 'LOG_time' => new EE_Datetime_Field( |
|
69 | - 'LOG_time', |
|
70 | - esc_html__("Log Time", 'event_espresso'), |
|
71 | - false, |
|
72 | - EE_Datetime_Field::now |
|
73 | - ), |
|
74 | - 'OBJ_ID' => new EE_Foreign_Key_String_Field( |
|
75 | - 'OBJ_ID', |
|
76 | - esc_html__("Object ID (int or string)", 'event_espresso'), |
|
77 | - true, |
|
78 | - null, |
|
79 | - $models_this_can_attach_to |
|
80 | - ), |
|
81 | - 'OBJ_type' => new EE_Any_Foreign_Model_Name_Field( |
|
82 | - 'OBJ_type', |
|
83 | - esc_html__("Object Type", 'event_espresso'), |
|
84 | - true, |
|
85 | - null, |
|
86 | - $models_this_can_attach_to |
|
87 | - ), |
|
88 | - 'LOG_type' => new EE_Plain_Text_Field( |
|
89 | - 'LOG_type', |
|
90 | - esc_html__("Type of log entry", "event_espresso"), |
|
91 | - false, |
|
92 | - self::type_debug |
|
93 | - ), |
|
94 | - 'LOG_message' => new EE_Maybe_Serialized_Text_Field( |
|
95 | - 'LOG_message', |
|
96 | - esc_html__("Log Message (body)", 'event_espresso'), |
|
97 | - true |
|
98 | - ), |
|
99 | - /* |
|
53 | + /** |
|
54 | + * @param string|null $timezone |
|
55 | + * @throws EE_Error |
|
56 | + */ |
|
57 | + protected function __construct(?string $timezone = '') |
|
58 | + { |
|
59 | + $this->singular_item = esc_html__('Log', 'event_espresso'); |
|
60 | + $this->plural_item = esc_html__('Logs', 'event_espresso'); |
|
61 | + $this->_tables = [ |
|
62 | + 'Log' => new EE_Primary_Table('esp_log', 'LOG_ID'), |
|
63 | + ]; |
|
64 | + $models_this_can_attach_to = array_keys(EE_Registry::instance()->non_abstract_db_models); |
|
65 | + $this->_fields = [ |
|
66 | + 'Log' => [ |
|
67 | + 'LOG_ID' => new EE_Primary_Key_Int_Field('LOG_ID', esc_html__('Log ID', 'event_espresso')), |
|
68 | + 'LOG_time' => new EE_Datetime_Field( |
|
69 | + 'LOG_time', |
|
70 | + esc_html__("Log Time", 'event_espresso'), |
|
71 | + false, |
|
72 | + EE_Datetime_Field::now |
|
73 | + ), |
|
74 | + 'OBJ_ID' => new EE_Foreign_Key_String_Field( |
|
75 | + 'OBJ_ID', |
|
76 | + esc_html__("Object ID (int or string)", 'event_espresso'), |
|
77 | + true, |
|
78 | + null, |
|
79 | + $models_this_can_attach_to |
|
80 | + ), |
|
81 | + 'OBJ_type' => new EE_Any_Foreign_Model_Name_Field( |
|
82 | + 'OBJ_type', |
|
83 | + esc_html__("Object Type", 'event_espresso'), |
|
84 | + true, |
|
85 | + null, |
|
86 | + $models_this_can_attach_to |
|
87 | + ), |
|
88 | + 'LOG_type' => new EE_Plain_Text_Field( |
|
89 | + 'LOG_type', |
|
90 | + esc_html__("Type of log entry", "event_espresso"), |
|
91 | + false, |
|
92 | + self::type_debug |
|
93 | + ), |
|
94 | + 'LOG_message' => new EE_Maybe_Serialized_Text_Field( |
|
95 | + 'LOG_message', |
|
96 | + esc_html__("Log Message (body)", 'event_espresso'), |
|
97 | + true |
|
98 | + ), |
|
99 | + /* |
|
100 | 100 | * Note: when querying for a change log's user, the OBJ_ID and OBJ_type fields are used, |
101 | 101 | * not the LOG_wp_user field. E.g., |
102 | 102 | * `EEM_Change_Log::instance()->get_all(array(array('WP_User.ID'=>1)))` will actually return |
@@ -105,162 +105,162 @@ discard block |
||
105 | 105 | * If you want the latter, you can't use the model's magic joining. E.g, you would need to do |
106 | 106 | * `EEM_Change_Log::instance()->get_all(array(array('LOG_wp_user' => 1)))`. |
107 | 107 | */ |
108 | - 'LOG_wp_user' => new EE_WP_User_Field( |
|
109 | - 'LOG_wp_user', |
|
110 | - esc_html__("User who was logged in while this occurred", 'event_espresso'), |
|
111 | - true |
|
112 | - ), |
|
113 | - ], |
|
114 | - ]; |
|
115 | - $this->_model_relations = []; |
|
116 | - foreach ($models_this_can_attach_to as $model) { |
|
117 | - if ($model != 'Change_Log') { |
|
118 | - $this->_model_relations[ $model ] = new EE_Belongs_To_Any_Relation(); |
|
119 | - } |
|
120 | - } |
|
121 | - // use completely custom caps for this |
|
122 | - unset($this->_cap_restriction_generators); |
|
123 | - // caps-wise this is all-or-nothing: if you have the default role you can access anything, otherwise nothing |
|
124 | - foreach ($this->_cap_contexts_to_cap_action_map as $cap_context => $action) { |
|
125 | - $this->_cap_restrictions[ $cap_context ][ EE_Restriction_Generator_Base::get_default_restrictions_cap() ] |
|
126 | - = new EE_Return_None_Where_Conditions(); |
|
127 | - } |
|
128 | - parent::__construct($timezone); |
|
129 | - } |
|
108 | + 'LOG_wp_user' => new EE_WP_User_Field( |
|
109 | + 'LOG_wp_user', |
|
110 | + esc_html__("User who was logged in while this occurred", 'event_espresso'), |
|
111 | + true |
|
112 | + ), |
|
113 | + ], |
|
114 | + ]; |
|
115 | + $this->_model_relations = []; |
|
116 | + foreach ($models_this_can_attach_to as $model) { |
|
117 | + if ($model != 'Change_Log') { |
|
118 | + $this->_model_relations[ $model ] = new EE_Belongs_To_Any_Relation(); |
|
119 | + } |
|
120 | + } |
|
121 | + // use completely custom caps for this |
|
122 | + unset($this->_cap_restriction_generators); |
|
123 | + // caps-wise this is all-or-nothing: if you have the default role you can access anything, otherwise nothing |
|
124 | + foreach ($this->_cap_contexts_to_cap_action_map as $cap_context => $action) { |
|
125 | + $this->_cap_restrictions[ $cap_context ][ EE_Restriction_Generator_Base::get_default_restrictions_cap() ] |
|
126 | + = new EE_Return_None_Where_Conditions(); |
|
127 | + } |
|
128 | + parent::__construct($timezone); |
|
129 | + } |
|
130 | 130 | |
131 | 131 | |
132 | - /** |
|
133 | - * @param string $log_type !see the acceptable values of LOG_type in EEM__Change_Log::__construct |
|
134 | - * @param array|string $message array|string of the message you want to record |
|
135 | - * @param EE_Base_Class|null $related_model_obj |
|
136 | - * @return EE_Change_Log |
|
137 | - * @throws EE_Error |
|
138 | - * @throws ReflectionException |
|
139 | - */ |
|
140 | - public function log(string $log_type, $message, ?EE_Base_Class $related_model_obj): EE_Change_Log |
|
141 | - { |
|
142 | - $obj_id = null; |
|
143 | - $obj_type = null; |
|
144 | - if ($related_model_obj instanceof EE_Base_Class) { |
|
145 | - $obj_id = $related_model_obj->ID(); |
|
146 | - $obj_type = $related_model_obj->get_model()->get_this_model_name(); |
|
147 | - } |
|
148 | - $log = EE_Change_Log::new_instance( |
|
149 | - [ |
|
150 | - 'LOG_type' => $log_type, |
|
151 | - 'LOG_message' => $message, |
|
152 | - 'OBJ_ID' => $obj_id, |
|
153 | - 'OBJ_type' => $obj_type, |
|
154 | - ] |
|
155 | - ); |
|
156 | - $log->save(); |
|
157 | - return $log; |
|
158 | - } |
|
132 | + /** |
|
133 | + * @param string $log_type !see the acceptable values of LOG_type in EEM__Change_Log::__construct |
|
134 | + * @param array|string $message array|string of the message you want to record |
|
135 | + * @param EE_Base_Class|null $related_model_obj |
|
136 | + * @return EE_Change_Log |
|
137 | + * @throws EE_Error |
|
138 | + * @throws ReflectionException |
|
139 | + */ |
|
140 | + public function log(string $log_type, $message, ?EE_Base_Class $related_model_obj): EE_Change_Log |
|
141 | + { |
|
142 | + $obj_id = null; |
|
143 | + $obj_type = null; |
|
144 | + if ($related_model_obj instanceof EE_Base_Class) { |
|
145 | + $obj_id = $related_model_obj->ID(); |
|
146 | + $obj_type = $related_model_obj->get_model()->get_this_model_name(); |
|
147 | + } |
|
148 | + $log = EE_Change_Log::new_instance( |
|
149 | + [ |
|
150 | + 'LOG_type' => $log_type, |
|
151 | + 'LOG_message' => $message, |
|
152 | + 'OBJ_ID' => $obj_id, |
|
153 | + 'OBJ_type' => $obj_type, |
|
154 | + ] |
|
155 | + ); |
|
156 | + $log->save(); |
|
157 | + return $log; |
|
158 | + } |
|
159 | 159 | |
160 | 160 | |
161 | - /** |
|
162 | - * Adds a gateway log for the specified object, given its ID and type |
|
163 | - * |
|
164 | - * @param array|string $message |
|
165 | - * @param int|string $related_obj_id |
|
166 | - * @param string $related_obj_type |
|
167 | - * @return EE_Change_Log |
|
168 | - * @throws ReflectionException |
|
169 | - * @throws EE_Error |
|
170 | - */ |
|
171 | - public function gateway_log($message, $related_obj_id, string $related_obj_type): EE_Change_Log |
|
172 | - { |
|
173 | - if (! EE_Registry::instance()->is_model_name($related_obj_type)) { |
|
174 | - throw new EE_Error( |
|
175 | - sprintf( |
|
176 | - esc_html__( |
|
177 | - "'%s' is not a model name. A model name must be provided when making a gateway log. Eg, 'Payment', 'Payment_Method', etc", |
|
178 | - "event_espresso" |
|
179 | - ), |
|
180 | - $related_obj_type |
|
181 | - ) |
|
182 | - ); |
|
183 | - } |
|
184 | - $log = EE_Change_Log::new_instance( |
|
185 | - [ |
|
186 | - 'LOG_type' => EEM_Change_Log::type_gateway, |
|
187 | - 'LOG_message' => $message, |
|
188 | - 'OBJ_ID' => $related_obj_id, |
|
189 | - 'OBJ_type' => $related_obj_type, |
|
190 | - ] |
|
191 | - ); |
|
192 | - $log->save(); |
|
193 | - return $log; |
|
194 | - } |
|
161 | + /** |
|
162 | + * Adds a gateway log for the specified object, given its ID and type |
|
163 | + * |
|
164 | + * @param array|string $message |
|
165 | + * @param int|string $related_obj_id |
|
166 | + * @param string $related_obj_type |
|
167 | + * @return EE_Change_Log |
|
168 | + * @throws ReflectionException |
|
169 | + * @throws EE_Error |
|
170 | + */ |
|
171 | + public function gateway_log($message, $related_obj_id, string $related_obj_type): EE_Change_Log |
|
172 | + { |
|
173 | + if (! EE_Registry::instance()->is_model_name($related_obj_type)) { |
|
174 | + throw new EE_Error( |
|
175 | + sprintf( |
|
176 | + esc_html__( |
|
177 | + "'%s' is not a model name. A model name must be provided when making a gateway log. Eg, 'Payment', 'Payment_Method', etc", |
|
178 | + "event_espresso" |
|
179 | + ), |
|
180 | + $related_obj_type |
|
181 | + ) |
|
182 | + ); |
|
183 | + } |
|
184 | + $log = EE_Change_Log::new_instance( |
|
185 | + [ |
|
186 | + 'LOG_type' => EEM_Change_Log::type_gateway, |
|
187 | + 'LOG_message' => $message, |
|
188 | + 'OBJ_ID' => $related_obj_id, |
|
189 | + 'OBJ_type' => $related_obj_type, |
|
190 | + ] |
|
191 | + ); |
|
192 | + $log->save(); |
|
193 | + return $log; |
|
194 | + } |
|
195 | 195 | |
196 | 196 | |
197 | - /** |
|
198 | - * Just gets the bare-bones wpdb results as an array in cases where efficiency is essential |
|
199 | - * |
|
200 | - * @param array $query_params |
|
201 | - * @return array of arrays |
|
202 | - * @throws EE_Error |
|
203 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
204 | - */ |
|
205 | - public function get_all_efficiently(array $query_params): array |
|
206 | - { |
|
207 | - return $this->_get_all_wpdb_results($query_params); |
|
208 | - } |
|
197 | + /** |
|
198 | + * Just gets the bare-bones wpdb results as an array in cases where efficiency is essential |
|
199 | + * |
|
200 | + * @param array $query_params |
|
201 | + * @return array of arrays |
|
202 | + * @throws EE_Error |
|
203 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
204 | + */ |
|
205 | + public function get_all_efficiently(array $query_params): array |
|
206 | + { |
|
207 | + return $this->_get_all_wpdb_results($query_params); |
|
208 | + } |
|
209 | 209 | |
210 | 210 | |
211 | - /** |
|
212 | - * Executes a database query to delete gateway logs. Does not affect model objects, so if you attempt to use |
|
213 | - * models after this, they may be out-of-sync with the database |
|
214 | - * |
|
215 | - * @param DateTime $datetime |
|
216 | - * @return false|int |
|
217 | - * @throws EE_Error |
|
218 | - */ |
|
219 | - public function delete_gateway_logs_older_than(DateTime $datetime) |
|
220 | - { |
|
221 | - global $wpdb; |
|
222 | - return $wpdb->query( |
|
223 | - $wpdb->prepare( |
|
224 | - 'DELETE FROM ' . $this->table() . ' WHERE LOG_type = %s AND LOG_time < %s', |
|
225 | - EEM_Change_Log::type_gateway, |
|
226 | - $datetime->format(EE_Datetime_Field::mysql_timestamp_format) |
|
227 | - ) |
|
228 | - ); |
|
229 | - } |
|
211 | + /** |
|
212 | + * Executes a database query to delete gateway logs. Does not affect model objects, so if you attempt to use |
|
213 | + * models after this, they may be out-of-sync with the database |
|
214 | + * |
|
215 | + * @param DateTime $datetime |
|
216 | + * @return false|int |
|
217 | + * @throws EE_Error |
|
218 | + */ |
|
219 | + public function delete_gateway_logs_older_than(DateTime $datetime) |
|
220 | + { |
|
221 | + global $wpdb; |
|
222 | + return $wpdb->query( |
|
223 | + $wpdb->prepare( |
|
224 | + 'DELETE FROM ' . $this->table() . ' WHERE LOG_type = %s AND LOG_time < %s', |
|
225 | + EEM_Change_Log::type_gateway, |
|
226 | + $datetime->format(EE_Datetime_Field::mysql_timestamp_format) |
|
227 | + ) |
|
228 | + ); |
|
229 | + } |
|
230 | 230 | |
231 | 231 | |
232 | - /** |
|
233 | - * Returns the map of type to pretty label for identifiers used for `LOG_type`. Client code can register their own |
|
234 | - * map vai the given filter. |
|
235 | - * |
|
236 | - * @return array |
|
237 | - */ |
|
238 | - public static function get_pretty_label_map_for_registered_types(): array |
|
239 | - { |
|
240 | - return apply_filters( |
|
241 | - 'FHEE__EEM_Change_Log__get_pretty_label_map_for_registered_types', |
|
242 | - [ |
|
243 | - self::type_create => esc_html__("Create", "event_espresso"), |
|
244 | - self::type_update => esc_html__("Update", "event_espresso"), |
|
245 | - self::type_delete => esc_html__("Delete", "event_espresso"), |
|
246 | - self::type_debug => esc_html__("Debug", "event_espresso"), |
|
247 | - self::type_error => esc_html__("Error", "event_espresso"), |
|
248 | - self::type_gateway => esc_html__("Gateway Interaction (IPN or Direct Payment)", 'event_espresso'), |
|
249 | - ] |
|
250 | - ); |
|
251 | - } |
|
232 | + /** |
|
233 | + * Returns the map of type to pretty label for identifiers used for `LOG_type`. Client code can register their own |
|
234 | + * map vai the given filter. |
|
235 | + * |
|
236 | + * @return array |
|
237 | + */ |
|
238 | + public static function get_pretty_label_map_for_registered_types(): array |
|
239 | + { |
|
240 | + return apply_filters( |
|
241 | + 'FHEE__EEM_Change_Log__get_pretty_label_map_for_registered_types', |
|
242 | + [ |
|
243 | + self::type_create => esc_html__("Create", "event_espresso"), |
|
244 | + self::type_update => esc_html__("Update", "event_espresso"), |
|
245 | + self::type_delete => esc_html__("Delete", "event_espresso"), |
|
246 | + self::type_debug => esc_html__("Debug", "event_espresso"), |
|
247 | + self::type_error => esc_html__("Error", "event_espresso"), |
|
248 | + self::type_gateway => esc_html__("Gateway Interaction (IPN or Direct Payment)", 'event_espresso'), |
|
249 | + ] |
|
250 | + ); |
|
251 | + } |
|
252 | 252 | |
253 | 253 | |
254 | - /** |
|
255 | - * Return the pretty (localized) label for the given log type identifier. |
|
256 | - * |
|
257 | - * @param string $type_identifier |
|
258 | - * @return string |
|
259 | - */ |
|
260 | - public static function get_pretty_label_for_type(string $type_identifier): string |
|
261 | - { |
|
262 | - $type_identifier_map = self::get_pretty_label_map_for_registered_types(); |
|
263 | - // we fall back to the incoming type identifier if there is no localized label for it. |
|
264 | - return $type_identifier_map[ $type_identifier ] ?? $type_identifier; |
|
265 | - } |
|
254 | + /** |
|
255 | + * Return the pretty (localized) label for the given log type identifier. |
|
256 | + * |
|
257 | + * @param string $type_identifier |
|
258 | + * @return string |
|
259 | + */ |
|
260 | + public static function get_pretty_label_for_type(string $type_identifier): string |
|
261 | + { |
|
262 | + $type_identifier_map = self::get_pretty_label_map_for_registered_types(); |
|
263 | + // we fall back to the incoming type identifier if there is no localized label for it. |
|
264 | + return $type_identifier_map[ $type_identifier ] ?? $type_identifier; |
|
265 | + } |
|
266 | 266 | } |
@@ -16,456 +16,456 @@ |
||
16 | 16 | */ |
17 | 17 | class EEM_Payment_Method extends EEM_Base |
18 | 18 | { |
19 | - const scope_cart = 'CART'; |
|
20 | - |
|
21 | - const scope_admin = 'ADMIN'; |
|
22 | - |
|
23 | - const scope_api = 'API'; |
|
24 | - |
|
25 | - /** |
|
26 | - * @type EEM_Payment_Method |
|
27 | - */ |
|
28 | - protected static $_instance = null; |
|
29 | - |
|
30 | - |
|
31 | - /** |
|
32 | - * private constructor to prevent direct creation |
|
33 | - * |
|
34 | - * @param string|null $timezone |
|
35 | - * @throws EE_Error |
|
36 | - */ |
|
37 | - protected function __construct(?string $timezone = '') |
|
38 | - { |
|
39 | - $this->singular_item = esc_html__('Payment Method', 'event_espresso'); |
|
40 | - $this->plural_item = esc_html__('Payment Methods', 'event_espresso'); |
|
41 | - $this->_tables = [ |
|
42 | - 'Payment_Method' => new EE_Primary_Table('esp_payment_method', 'PMD_ID'), |
|
43 | - ]; |
|
44 | - $this->_fields = [ |
|
45 | - 'Payment_Method' => [ |
|
46 | - 'PMD_ID' => new EE_Primary_Key_Int_Field( |
|
47 | - 'PMD_ID', |
|
48 | - esc_html__('ID', 'event_espresso') |
|
49 | - ), |
|
50 | - 'PMD_type' => new EE_Plain_Text_Field( |
|
51 | - 'PMD_type', |
|
52 | - esc_html__('Payment Method Type', 'event_espresso'), |
|
53 | - false, |
|
54 | - 'Admin_Only' |
|
55 | - ), |
|
56 | - 'PMD_name' => new EE_Plain_Text_Field( |
|
57 | - 'PMD_name', |
|
58 | - esc_html__('Name', 'event_espresso'), |
|
59 | - false |
|
60 | - ), |
|
61 | - 'PMD_desc' => new EE_Post_Content_Field( |
|
62 | - 'PMD_desc', |
|
63 | - esc_html__('Description', 'event_espresso'), |
|
64 | - false, |
|
65 | - '' |
|
66 | - ), |
|
67 | - 'PMD_admin_name' => new EE_Plain_Text_Field( |
|
68 | - 'PMD_admin_name', |
|
69 | - esc_html__('Admin-Only Name', 'event_espresso'), |
|
70 | - true |
|
71 | - ), |
|
72 | - 'PMD_admin_desc' => new EE_Post_Content_Field( |
|
73 | - 'PMD_admin_desc', |
|
74 | - esc_html__('Admin-Only Description', 'event_espresso'), |
|
75 | - true |
|
76 | - ), |
|
77 | - 'PMD_slug' => new EE_Slug_Field( |
|
78 | - 'PMD_slug', |
|
79 | - esc_html__('Slug', 'event_espresso'), |
|
80 | - false |
|
81 | - ), |
|
82 | - 'PMD_order' => new EE_Integer_Field( |
|
83 | - 'PMD_order', |
|
84 | - esc_html__('Order', 'event_espresso'), |
|
85 | - false, |
|
86 | - 0 |
|
87 | - ), |
|
88 | - 'PMD_debug_mode' => new EE_Boolean_Field( |
|
89 | - 'PMD_debug_mode', |
|
90 | - esc_html__('Sandbox Mode On? (AKA: debug mode)', 'event_espresso'), |
|
91 | - false, |
|
92 | - false |
|
93 | - ), |
|
94 | - 'PMD_wp_user' => new EE_WP_User_Field( |
|
95 | - 'PMD_wp_user', |
|
96 | - esc_html__('Payment Method Creator ID', 'event_espresso'), |
|
97 | - false |
|
98 | - ), |
|
99 | - 'PMD_open_by_default' => new EE_Boolean_Field( |
|
100 | - 'PMD_open_by_default', |
|
101 | - esc_html__('Open by Default?', 'event_espresso'), |
|
102 | - false, |
|
103 | - false |
|
104 | - ), |
|
105 | - 'PMD_button_url' => new EE_Plain_Text_Field( |
|
106 | - 'PMD_button_url', |
|
107 | - esc_html__('Button URL', 'event_espresso'), |
|
108 | - true, |
|
109 | - '' |
|
110 | - ), |
|
111 | - 'PMD_scope' => new EE_Serialized_Text_Field( |
|
112 | - 'PMD_scope', |
|
113 | - esc_html__('Usable From?', 'event_espresso'), |
|
114 | - false, |
|
115 | - []// possible values currently are 'CART','ADMIN','API' |
|
116 | - ), |
|
117 | - ], |
|
118 | - ]; |
|
119 | - $this->_model_relations = [ |
|
120 | - 'Currency' => new EE_HABTM_Relation('Currency_Payment_Method'), |
|
121 | - 'Payment' => new EE_Has_Many_Relation(), |
|
122 | - 'Transaction' => new EE_Has_Many_Relation(), |
|
123 | - 'WP_User' => new EE_Belongs_To_Relation(), |
|
124 | - ]; |
|
125 | - parent::__construct($timezone); |
|
126 | - } |
|
127 | - |
|
128 | - |
|
129 | - /** |
|
130 | - * Gets one by the slug provided |
|
131 | - * |
|
132 | - * @param string $slug |
|
133 | - * @return EE_Base_Class|EE_Payment_Method|EE_Soft_Delete_Base_Class|NULL |
|
134 | - * @throws EE_Error |
|
135 | - */ |
|
136 | - public function get_one_by_slug($slug) |
|
137 | - { |
|
138 | - return $this->get_one([['PMD_slug' => $slug]]); |
|
139 | - } |
|
140 | - |
|
141 | - |
|
142 | - /** |
|
143 | - * Gets all the acceptable scopes for payment methods. |
|
144 | - * Keys are their names as store din the DB, and values are nice names for displaying them |
|
145 | - * |
|
146 | - * @return array |
|
147 | - */ |
|
148 | - public function scopes() |
|
149 | - { |
|
150 | - return apply_filters( |
|
151 | - 'FHEE__EEM_Payment_Method__scopes', |
|
152 | - [ |
|
153 | - EEM_Payment_Method::scope_cart => esc_html__('Front-end Registration Page', 'event_espresso'), |
|
154 | - EEM_Payment_Method::scope_admin => esc_html__( |
|
155 | - 'Admin Registration Page (no online processing)', |
|
156 | - 'event_espresso' |
|
157 | - ), |
|
158 | - ] |
|
159 | - ); |
|
160 | - } |
|
161 | - |
|
162 | - |
|
163 | - /** |
|
164 | - * Determines if this is an valid scope |
|
165 | - * |
|
166 | - * @param string $scope like one of EEM_Payment_Method::instance()->scopes() |
|
167 | - * @return boolean |
|
168 | - */ |
|
169 | - public function is_valid_scope($scope) |
|
170 | - { |
|
171 | - $scopes = $this->scopes(); |
|
172 | - if (isset($scopes[ $scope ])) { |
|
173 | - return true; |
|
174 | - } |
|
175 | - return false; |
|
176 | - } |
|
177 | - |
|
178 | - |
|
179 | - /** |
|
180 | - * Gets all active payment methods |
|
181 | - * |
|
182 | - * @param string $scope one of |
|
183 | - * @param array $query_params |
|
184 | - * @return EE_Base_Class[]|EE_Payment_Method[] |
|
185 | - * @throws EE_Error |
|
186 | - */ |
|
187 | - public function get_all_active($scope = null, $query_params = []) |
|
188 | - { |
|
189 | - if (! isset($query_params['order_by']) && ! isset($query_params['order'])) { |
|
190 | - $query_params['order_by'] = ['PMD_order' => 'ASC', 'PMD_ID' => 'ASC']; |
|
191 | - } |
|
192 | - return $this->get_all($this->_get_query_params_for_all_active($scope, $query_params)); |
|
193 | - } |
|
194 | - |
|
195 | - |
|
196 | - /** |
|
197 | - * Counts all active gateways in the specified scope |
|
198 | - * |
|
199 | - * @param string $scope one of EEM_Payment_Method::scope_* |
|
200 | - * @param array $query_params |
|
201 | - * @return int |
|
202 | - * @throws EE_Error |
|
203 | - */ |
|
204 | - public function count_active($scope = null, $query_params = []) |
|
205 | - { |
|
206 | - return $this->count($this->_get_query_params_for_all_active($scope, $query_params)); |
|
207 | - } |
|
208 | - |
|
209 | - |
|
210 | - /** |
|
211 | - * Creates the $query_params that can be passed into any EEM_Payment_Method as their $query_params |
|
212 | - * argument to get all active for a given scope |
|
213 | - * |
|
214 | - * @param string $scope one of the constants EEM_Payment_Method::scope_* |
|
215 | - * @param array $query_params |
|
216 | - * @return array |
|
217 | - * @throws EE_Error |
|
218 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
219 | - */ |
|
220 | - protected function _get_query_params_for_all_active($scope = null, $query_params = []) |
|
221 | - { |
|
222 | - if ($scope) { |
|
223 | - if ($this->is_valid_scope($scope)) { |
|
224 | - return array_replace_recursive([['PMD_scope' => ['LIKE', "%$scope%"]]], $query_params); |
|
225 | - } |
|
226 | - throw new EE_Error( |
|
227 | - sprintf( |
|
228 | - esc_html__("'%s' is not a valid scope for a payment method", 'event_espresso'), |
|
229 | - $scope |
|
230 | - ) |
|
231 | - ); |
|
232 | - } |
|
233 | - $acceptable_scopes = []; |
|
234 | - $count = 0; |
|
235 | - foreach ($this->scopes() as $scope_name => $desc) { |
|
236 | - $count++; |
|
237 | - $acceptable_scopes[ 'PMD_scope*' . $count ] = ['LIKE', '%' . $scope_name . '%']; |
|
238 | - } |
|
239 | - return array_replace_recursive([['OR*active_scope' => $acceptable_scopes]], $query_params); |
|
240 | - } |
|
241 | - |
|
242 | - |
|
243 | - /** |
|
244 | - * Creates the $query_params that can be passed into any EEM_Payment_Method as their $query_params |
|
245 | - * argument to get all active for a given scope |
|
246 | - * |
|
247 | - * @param string $scope one of the constants EEM_Payment_Method::scope_* |
|
248 | - * @param array $query_params |
|
249 | - * @return array |
|
250 | - * @throws EE_Error |
|
251 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
252 | - */ |
|
253 | - public function get_query_params_for_all_active($scope = null, $query_params = []) |
|
254 | - { |
|
255 | - return $this->_get_query_params_for_all_active($scope, $query_params); |
|
256 | - } |
|
257 | - |
|
258 | - |
|
259 | - /** |
|
260 | - * Gets one active payment method. see @get_all_active for documentation |
|
261 | - * |
|
262 | - * @param string $scope |
|
263 | - * @param array $query_params |
|
264 | - * @return EE_Base_Class|EE_Payment_Method|EE_Soft_Delete_Base_Class|NULL |
|
265 | - * @throws EE_Error |
|
266 | - */ |
|
267 | - public function get_one_active($scope = null, $query_params = []) |
|
268 | - { |
|
269 | - return $this->get_one($this->_get_query_params_for_all_active($scope, $query_params)); |
|
270 | - } |
|
271 | - |
|
272 | - |
|
273 | - /** |
|
274 | - * Gets one payment method of that type, regardless of whether its active or not |
|
275 | - * |
|
276 | - * @param string $type |
|
277 | - * @return EE_Base_Class|EE_Payment_Method|EE_Soft_Delete_Base_Class|NULL |
|
278 | - * @throws EE_Error |
|
279 | - */ |
|
280 | - public function get_one_of_type($type) |
|
281 | - { |
|
282 | - return $this->get_one([['PMD_type' => $type]]); |
|
283 | - } |
|
284 | - |
|
285 | - |
|
286 | - /** |
|
287 | - * Overrides parent ot also check by the slug |
|
288 | - * |
|
289 | - * @param string|int|EE_Payment_Method $base_class_obj_or_id |
|
290 | - * @param boolean $ensure_is_in_db |
|
291 | - * @return EE_Base_Class|EE_Payment_Method|EE_Soft_Delete_Base_Class|int|string |
|
292 | - * @throws EE_Error |
|
293 | - * @see EEM_Base::ensure_is_obj() |
|
294 | - */ |
|
295 | - public function ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db = false) |
|
296 | - { |
|
297 | - // first: check if it's a slug |
|
298 | - if (is_string($base_class_obj_or_id)) { |
|
299 | - $obj = $this->get_one_by_slug($base_class_obj_or_id); |
|
300 | - if ($obj) { |
|
301 | - return $obj; |
|
302 | - } |
|
303 | - } |
|
304 | - // ok so it wasn't a slug we were passed. try the usual then (ie, it's an object or an ID) |
|
305 | - try { |
|
306 | - return parent::ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db); |
|
307 | - } catch (EE_Error $e) { |
|
308 | - // handle it outside the catch |
|
309 | - } |
|
310 | - throw new EE_Error( |
|
311 | - sprintf( |
|
312 | - esc_html__("'%s' is neither a Payment Method ID, slug, nor object.", 'event_espresso'), |
|
313 | - $base_class_obj_or_id |
|
314 | - ) |
|
315 | - ); |
|
316 | - } |
|
317 | - |
|
318 | - |
|
319 | - /** |
|
320 | - * Gets the ID of this object, or if its a string finds the object's id |
|
321 | - * associated with that slug |
|
322 | - * |
|
323 | - * @param mixed $base_obj_or_id_or_slug |
|
324 | - * @return int |
|
325 | - * @throws EE_Error |
|
326 | - */ |
|
327 | - public function ensure_is_ID($base_obj_or_id_or_slug) |
|
328 | - { |
|
329 | - if (is_string($base_obj_or_id_or_slug)) { |
|
330 | - // assume it's a slug |
|
331 | - $base_obj_or_id_or_slug = $this->get_one_by_slug($base_obj_or_id_or_slug); |
|
332 | - } |
|
333 | - return parent::ensure_is_ID($base_obj_or_id_or_slug); |
|
334 | - } |
|
335 | - |
|
336 | - |
|
337 | - /** |
|
338 | - * Verifies the button urls on all the passed payment methods have a valid button url. |
|
339 | - * If not, resets them to their default. |
|
340 | - * |
|
341 | - * @param EE_Payment_Method[] $payment_methods if NULL, defaults to all payment methods active in the cart |
|
342 | - * @throws EE_Error |
|
343 | - * @throws ReflectionException |
|
344 | - */ |
|
345 | - public function verify_button_urls($payment_methods = null) |
|
346 | - { |
|
347 | - $payment_methods = is_array($payment_methods) |
|
348 | - ? $payment_methods |
|
349 | - : $this->get_all_active(EEM_Payment_Method::scope_cart); |
|
350 | - foreach ($payment_methods as $payment_method) { |
|
351 | - try { |
|
352 | - // If there is really no button URL at all, or if the button URLs still point to decaf folder even |
|
353 | - // though this is a caffeinated install, reset it to the default. |
|
354 | - $current_button_url = $payment_method->button_url(); |
|
355 | - if ( |
|
356 | - empty($current_button_url) |
|
357 | - || ( |
|
358 | - strpos($current_button_url, 'decaf') !== false |
|
359 | - && strpos($payment_method->type_obj()->default_button_url(), 'decaf') === false |
|
360 | - ) |
|
361 | - ) { |
|
362 | - $payment_method->save( |
|
363 | - [ |
|
364 | - 'PMD_button_url' => $payment_method->type_obj()->default_button_url(), |
|
365 | - ] |
|
366 | - ); |
|
367 | - } |
|
368 | - } catch (EE_Error $e) { |
|
369 | - $payment_method->deactivate(); |
|
370 | - } |
|
371 | - } |
|
372 | - } |
|
373 | - |
|
374 | - |
|
375 | - /** |
|
376 | - * Overrides parent to not only turn wpdb results into EE_Payment_Method objects, |
|
377 | - * but also verifies the payment method type of each is a usable object. If not, |
|
378 | - * deactivate it, sets a notification, and deactivates it |
|
379 | - * |
|
380 | - * @param array $rows |
|
381 | - * @return EE_Payment_Method[] |
|
382 | - * @throws EE_Error |
|
383 | - * @throws InvalidDataTypeException |
|
384 | - * @throws ReflectionException |
|
385 | - */ |
|
386 | - protected function _create_objects($rows = []) |
|
387 | - { |
|
388 | - EE_Registry::instance()->load_lib('Payment_Method_Manager'); |
|
389 | - $payment_methods = parent::_create_objects($rows); |
|
390 | - /* @var $payment_methods EE_Payment_Method[] */ |
|
391 | - $usable_payment_methods = []; |
|
392 | - foreach ($payment_methods as $key => $payment_method) { |
|
393 | - if (EE_Payment_Method_Manager::instance()->payment_method_type_exists($payment_method->type())) { |
|
394 | - $usable_payment_methods[ $key ] = $payment_method; |
|
395 | - // some payment methods enqueue their scripts in EE_PMT_*::__construct |
|
396 | - // which is kinda a no-no (just because it's being constructed doesn't mean we need to enqueue |
|
397 | - // its scripts). but for backwards-compat we should continue to do that |
|
398 | - $payment_method->type_obj(); |
|
399 | - } elseif ($payment_method->active()) { |
|
400 | - // only deactivate and notify the admin if the payment is active somewhere |
|
401 | - $payment_method->deactivate(); |
|
402 | - $payment_method->save(); |
|
403 | - do_action( |
|
404 | - 'AHEE__EEM_Payment_Method___create_objects_auto_deactivated_payment_method', |
|
405 | - $payment_method |
|
406 | - ); |
|
407 | - new PersistentAdminNotice( |
|
408 | - 'auto-deactivated-' . $payment_method->type(), |
|
409 | - sprintf( |
|
410 | - esc_html__( |
|
411 | - 'The payment method %1$s was automatically deactivated because it appears its associated Event Espresso Addon was recently deactivated.%2$sIt can be reactivated on the %3$sPlugins admin page%4$s, then you can reactivate the payment method.', |
|
412 | - 'event_espresso' |
|
413 | - ), |
|
414 | - $payment_method->admin_name(), |
|
415 | - '<br />', |
|
416 | - '<a href="' . admin_url('plugins.php') . '">', |
|
417 | - '</a>' |
|
418 | - ), |
|
419 | - true |
|
420 | - ); |
|
421 | - } |
|
422 | - } |
|
423 | - return $usable_payment_methods; |
|
424 | - } |
|
425 | - |
|
426 | - |
|
427 | - /** |
|
428 | - * Gets all the payment methods which can be used for transaction |
|
429 | - * (according to the relations between payment methods and events, and |
|
430 | - * the currencies used for the transaction and their relation to payment methods) |
|
431 | - * |
|
432 | - * @param EE_Transaction $transaction |
|
433 | - * @param string $scope @see EEM_Payment_Method::get_all_for_events |
|
434 | - * @return EE_Payment_Method[] |
|
435 | - * @throws EE_Error |
|
436 | - */ |
|
437 | - public function get_all_for_transaction($transaction, $scope) |
|
438 | - { |
|
439 | - // give addons a chance to override what payment methods are chosen based on the transaction |
|
440 | - return apply_filters( |
|
441 | - 'FHEE__EEM_Payment_Method__get_all_for_transaction__payment_methods', |
|
442 | - $this->get_all_active($scope, ['group_by' => 'PMD_type']), |
|
443 | - $transaction, |
|
444 | - $scope |
|
445 | - ); |
|
446 | - } |
|
447 | - |
|
448 | - |
|
449 | - /** |
|
450 | - * Returns the payment method used for the last payment made for a registration. |
|
451 | - * Note: if an offline payment method was selected on the related transaction then this will have no payment |
|
452 | - * methods returned. It will ONLY return a payment method for a PAYMENT recorded against the registration. |
|
453 | - * |
|
454 | - * @param EE_Registration|int $registration_or_reg_id Either the EE_Registration object or the id for the |
|
455 | - * registration. |
|
456 | - * @return EE_Payment|null |
|
457 | - * @throws EE_Error |
|
458 | - */ |
|
459 | - public function get_last_used_for_registration($registration_or_reg_id) |
|
460 | - { |
|
461 | - $registration_id = EEM_Registration::instance()->ensure_is_ID($registration_or_reg_id); |
|
462 | - |
|
463 | - $query_params = [ |
|
464 | - 0 => [ |
|
465 | - 'Payment.Registration.REG_ID' => $registration_id, |
|
466 | - ], |
|
467 | - 'order_by' => ['Payment.PAY_ID' => 'DESC'], |
|
468 | - ]; |
|
469 | - return $this->get_one($query_params); |
|
470 | - } |
|
19 | + const scope_cart = 'CART'; |
|
20 | + |
|
21 | + const scope_admin = 'ADMIN'; |
|
22 | + |
|
23 | + const scope_api = 'API'; |
|
24 | + |
|
25 | + /** |
|
26 | + * @type EEM_Payment_Method |
|
27 | + */ |
|
28 | + protected static $_instance = null; |
|
29 | + |
|
30 | + |
|
31 | + /** |
|
32 | + * private constructor to prevent direct creation |
|
33 | + * |
|
34 | + * @param string|null $timezone |
|
35 | + * @throws EE_Error |
|
36 | + */ |
|
37 | + protected function __construct(?string $timezone = '') |
|
38 | + { |
|
39 | + $this->singular_item = esc_html__('Payment Method', 'event_espresso'); |
|
40 | + $this->plural_item = esc_html__('Payment Methods', 'event_espresso'); |
|
41 | + $this->_tables = [ |
|
42 | + 'Payment_Method' => new EE_Primary_Table('esp_payment_method', 'PMD_ID'), |
|
43 | + ]; |
|
44 | + $this->_fields = [ |
|
45 | + 'Payment_Method' => [ |
|
46 | + 'PMD_ID' => new EE_Primary_Key_Int_Field( |
|
47 | + 'PMD_ID', |
|
48 | + esc_html__('ID', 'event_espresso') |
|
49 | + ), |
|
50 | + 'PMD_type' => new EE_Plain_Text_Field( |
|
51 | + 'PMD_type', |
|
52 | + esc_html__('Payment Method Type', 'event_espresso'), |
|
53 | + false, |
|
54 | + 'Admin_Only' |
|
55 | + ), |
|
56 | + 'PMD_name' => new EE_Plain_Text_Field( |
|
57 | + 'PMD_name', |
|
58 | + esc_html__('Name', 'event_espresso'), |
|
59 | + false |
|
60 | + ), |
|
61 | + 'PMD_desc' => new EE_Post_Content_Field( |
|
62 | + 'PMD_desc', |
|
63 | + esc_html__('Description', 'event_espresso'), |
|
64 | + false, |
|
65 | + '' |
|
66 | + ), |
|
67 | + 'PMD_admin_name' => new EE_Plain_Text_Field( |
|
68 | + 'PMD_admin_name', |
|
69 | + esc_html__('Admin-Only Name', 'event_espresso'), |
|
70 | + true |
|
71 | + ), |
|
72 | + 'PMD_admin_desc' => new EE_Post_Content_Field( |
|
73 | + 'PMD_admin_desc', |
|
74 | + esc_html__('Admin-Only Description', 'event_espresso'), |
|
75 | + true |
|
76 | + ), |
|
77 | + 'PMD_slug' => new EE_Slug_Field( |
|
78 | + 'PMD_slug', |
|
79 | + esc_html__('Slug', 'event_espresso'), |
|
80 | + false |
|
81 | + ), |
|
82 | + 'PMD_order' => new EE_Integer_Field( |
|
83 | + 'PMD_order', |
|
84 | + esc_html__('Order', 'event_espresso'), |
|
85 | + false, |
|
86 | + 0 |
|
87 | + ), |
|
88 | + 'PMD_debug_mode' => new EE_Boolean_Field( |
|
89 | + 'PMD_debug_mode', |
|
90 | + esc_html__('Sandbox Mode On? (AKA: debug mode)', 'event_espresso'), |
|
91 | + false, |
|
92 | + false |
|
93 | + ), |
|
94 | + 'PMD_wp_user' => new EE_WP_User_Field( |
|
95 | + 'PMD_wp_user', |
|
96 | + esc_html__('Payment Method Creator ID', 'event_espresso'), |
|
97 | + false |
|
98 | + ), |
|
99 | + 'PMD_open_by_default' => new EE_Boolean_Field( |
|
100 | + 'PMD_open_by_default', |
|
101 | + esc_html__('Open by Default?', 'event_espresso'), |
|
102 | + false, |
|
103 | + false |
|
104 | + ), |
|
105 | + 'PMD_button_url' => new EE_Plain_Text_Field( |
|
106 | + 'PMD_button_url', |
|
107 | + esc_html__('Button URL', 'event_espresso'), |
|
108 | + true, |
|
109 | + '' |
|
110 | + ), |
|
111 | + 'PMD_scope' => new EE_Serialized_Text_Field( |
|
112 | + 'PMD_scope', |
|
113 | + esc_html__('Usable From?', 'event_espresso'), |
|
114 | + false, |
|
115 | + []// possible values currently are 'CART','ADMIN','API' |
|
116 | + ), |
|
117 | + ], |
|
118 | + ]; |
|
119 | + $this->_model_relations = [ |
|
120 | + 'Currency' => new EE_HABTM_Relation('Currency_Payment_Method'), |
|
121 | + 'Payment' => new EE_Has_Many_Relation(), |
|
122 | + 'Transaction' => new EE_Has_Many_Relation(), |
|
123 | + 'WP_User' => new EE_Belongs_To_Relation(), |
|
124 | + ]; |
|
125 | + parent::__construct($timezone); |
|
126 | + } |
|
127 | + |
|
128 | + |
|
129 | + /** |
|
130 | + * Gets one by the slug provided |
|
131 | + * |
|
132 | + * @param string $slug |
|
133 | + * @return EE_Base_Class|EE_Payment_Method|EE_Soft_Delete_Base_Class|NULL |
|
134 | + * @throws EE_Error |
|
135 | + */ |
|
136 | + public function get_one_by_slug($slug) |
|
137 | + { |
|
138 | + return $this->get_one([['PMD_slug' => $slug]]); |
|
139 | + } |
|
140 | + |
|
141 | + |
|
142 | + /** |
|
143 | + * Gets all the acceptable scopes for payment methods. |
|
144 | + * Keys are their names as store din the DB, and values are nice names for displaying them |
|
145 | + * |
|
146 | + * @return array |
|
147 | + */ |
|
148 | + public function scopes() |
|
149 | + { |
|
150 | + return apply_filters( |
|
151 | + 'FHEE__EEM_Payment_Method__scopes', |
|
152 | + [ |
|
153 | + EEM_Payment_Method::scope_cart => esc_html__('Front-end Registration Page', 'event_espresso'), |
|
154 | + EEM_Payment_Method::scope_admin => esc_html__( |
|
155 | + 'Admin Registration Page (no online processing)', |
|
156 | + 'event_espresso' |
|
157 | + ), |
|
158 | + ] |
|
159 | + ); |
|
160 | + } |
|
161 | + |
|
162 | + |
|
163 | + /** |
|
164 | + * Determines if this is an valid scope |
|
165 | + * |
|
166 | + * @param string $scope like one of EEM_Payment_Method::instance()->scopes() |
|
167 | + * @return boolean |
|
168 | + */ |
|
169 | + public function is_valid_scope($scope) |
|
170 | + { |
|
171 | + $scopes = $this->scopes(); |
|
172 | + if (isset($scopes[ $scope ])) { |
|
173 | + return true; |
|
174 | + } |
|
175 | + return false; |
|
176 | + } |
|
177 | + |
|
178 | + |
|
179 | + /** |
|
180 | + * Gets all active payment methods |
|
181 | + * |
|
182 | + * @param string $scope one of |
|
183 | + * @param array $query_params |
|
184 | + * @return EE_Base_Class[]|EE_Payment_Method[] |
|
185 | + * @throws EE_Error |
|
186 | + */ |
|
187 | + public function get_all_active($scope = null, $query_params = []) |
|
188 | + { |
|
189 | + if (! isset($query_params['order_by']) && ! isset($query_params['order'])) { |
|
190 | + $query_params['order_by'] = ['PMD_order' => 'ASC', 'PMD_ID' => 'ASC']; |
|
191 | + } |
|
192 | + return $this->get_all($this->_get_query_params_for_all_active($scope, $query_params)); |
|
193 | + } |
|
194 | + |
|
195 | + |
|
196 | + /** |
|
197 | + * Counts all active gateways in the specified scope |
|
198 | + * |
|
199 | + * @param string $scope one of EEM_Payment_Method::scope_* |
|
200 | + * @param array $query_params |
|
201 | + * @return int |
|
202 | + * @throws EE_Error |
|
203 | + */ |
|
204 | + public function count_active($scope = null, $query_params = []) |
|
205 | + { |
|
206 | + return $this->count($this->_get_query_params_for_all_active($scope, $query_params)); |
|
207 | + } |
|
208 | + |
|
209 | + |
|
210 | + /** |
|
211 | + * Creates the $query_params that can be passed into any EEM_Payment_Method as their $query_params |
|
212 | + * argument to get all active for a given scope |
|
213 | + * |
|
214 | + * @param string $scope one of the constants EEM_Payment_Method::scope_* |
|
215 | + * @param array $query_params |
|
216 | + * @return array |
|
217 | + * @throws EE_Error |
|
218 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
219 | + */ |
|
220 | + protected function _get_query_params_for_all_active($scope = null, $query_params = []) |
|
221 | + { |
|
222 | + if ($scope) { |
|
223 | + if ($this->is_valid_scope($scope)) { |
|
224 | + return array_replace_recursive([['PMD_scope' => ['LIKE', "%$scope%"]]], $query_params); |
|
225 | + } |
|
226 | + throw new EE_Error( |
|
227 | + sprintf( |
|
228 | + esc_html__("'%s' is not a valid scope for a payment method", 'event_espresso'), |
|
229 | + $scope |
|
230 | + ) |
|
231 | + ); |
|
232 | + } |
|
233 | + $acceptable_scopes = []; |
|
234 | + $count = 0; |
|
235 | + foreach ($this->scopes() as $scope_name => $desc) { |
|
236 | + $count++; |
|
237 | + $acceptable_scopes[ 'PMD_scope*' . $count ] = ['LIKE', '%' . $scope_name . '%']; |
|
238 | + } |
|
239 | + return array_replace_recursive([['OR*active_scope' => $acceptable_scopes]], $query_params); |
|
240 | + } |
|
241 | + |
|
242 | + |
|
243 | + /** |
|
244 | + * Creates the $query_params that can be passed into any EEM_Payment_Method as their $query_params |
|
245 | + * argument to get all active for a given scope |
|
246 | + * |
|
247 | + * @param string $scope one of the constants EEM_Payment_Method::scope_* |
|
248 | + * @param array $query_params |
|
249 | + * @return array |
|
250 | + * @throws EE_Error |
|
251 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
252 | + */ |
|
253 | + public function get_query_params_for_all_active($scope = null, $query_params = []) |
|
254 | + { |
|
255 | + return $this->_get_query_params_for_all_active($scope, $query_params); |
|
256 | + } |
|
257 | + |
|
258 | + |
|
259 | + /** |
|
260 | + * Gets one active payment method. see @get_all_active for documentation |
|
261 | + * |
|
262 | + * @param string $scope |
|
263 | + * @param array $query_params |
|
264 | + * @return EE_Base_Class|EE_Payment_Method|EE_Soft_Delete_Base_Class|NULL |
|
265 | + * @throws EE_Error |
|
266 | + */ |
|
267 | + public function get_one_active($scope = null, $query_params = []) |
|
268 | + { |
|
269 | + return $this->get_one($this->_get_query_params_for_all_active($scope, $query_params)); |
|
270 | + } |
|
271 | + |
|
272 | + |
|
273 | + /** |
|
274 | + * Gets one payment method of that type, regardless of whether its active or not |
|
275 | + * |
|
276 | + * @param string $type |
|
277 | + * @return EE_Base_Class|EE_Payment_Method|EE_Soft_Delete_Base_Class|NULL |
|
278 | + * @throws EE_Error |
|
279 | + */ |
|
280 | + public function get_one_of_type($type) |
|
281 | + { |
|
282 | + return $this->get_one([['PMD_type' => $type]]); |
|
283 | + } |
|
284 | + |
|
285 | + |
|
286 | + /** |
|
287 | + * Overrides parent ot also check by the slug |
|
288 | + * |
|
289 | + * @param string|int|EE_Payment_Method $base_class_obj_or_id |
|
290 | + * @param boolean $ensure_is_in_db |
|
291 | + * @return EE_Base_Class|EE_Payment_Method|EE_Soft_Delete_Base_Class|int|string |
|
292 | + * @throws EE_Error |
|
293 | + * @see EEM_Base::ensure_is_obj() |
|
294 | + */ |
|
295 | + public function ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db = false) |
|
296 | + { |
|
297 | + // first: check if it's a slug |
|
298 | + if (is_string($base_class_obj_or_id)) { |
|
299 | + $obj = $this->get_one_by_slug($base_class_obj_or_id); |
|
300 | + if ($obj) { |
|
301 | + return $obj; |
|
302 | + } |
|
303 | + } |
|
304 | + // ok so it wasn't a slug we were passed. try the usual then (ie, it's an object or an ID) |
|
305 | + try { |
|
306 | + return parent::ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db); |
|
307 | + } catch (EE_Error $e) { |
|
308 | + // handle it outside the catch |
|
309 | + } |
|
310 | + throw new EE_Error( |
|
311 | + sprintf( |
|
312 | + esc_html__("'%s' is neither a Payment Method ID, slug, nor object.", 'event_espresso'), |
|
313 | + $base_class_obj_or_id |
|
314 | + ) |
|
315 | + ); |
|
316 | + } |
|
317 | + |
|
318 | + |
|
319 | + /** |
|
320 | + * Gets the ID of this object, or if its a string finds the object's id |
|
321 | + * associated with that slug |
|
322 | + * |
|
323 | + * @param mixed $base_obj_or_id_or_slug |
|
324 | + * @return int |
|
325 | + * @throws EE_Error |
|
326 | + */ |
|
327 | + public function ensure_is_ID($base_obj_or_id_or_slug) |
|
328 | + { |
|
329 | + if (is_string($base_obj_or_id_or_slug)) { |
|
330 | + // assume it's a slug |
|
331 | + $base_obj_or_id_or_slug = $this->get_one_by_slug($base_obj_or_id_or_slug); |
|
332 | + } |
|
333 | + return parent::ensure_is_ID($base_obj_or_id_or_slug); |
|
334 | + } |
|
335 | + |
|
336 | + |
|
337 | + /** |
|
338 | + * Verifies the button urls on all the passed payment methods have a valid button url. |
|
339 | + * If not, resets them to their default. |
|
340 | + * |
|
341 | + * @param EE_Payment_Method[] $payment_methods if NULL, defaults to all payment methods active in the cart |
|
342 | + * @throws EE_Error |
|
343 | + * @throws ReflectionException |
|
344 | + */ |
|
345 | + public function verify_button_urls($payment_methods = null) |
|
346 | + { |
|
347 | + $payment_methods = is_array($payment_methods) |
|
348 | + ? $payment_methods |
|
349 | + : $this->get_all_active(EEM_Payment_Method::scope_cart); |
|
350 | + foreach ($payment_methods as $payment_method) { |
|
351 | + try { |
|
352 | + // If there is really no button URL at all, or if the button URLs still point to decaf folder even |
|
353 | + // though this is a caffeinated install, reset it to the default. |
|
354 | + $current_button_url = $payment_method->button_url(); |
|
355 | + if ( |
|
356 | + empty($current_button_url) |
|
357 | + || ( |
|
358 | + strpos($current_button_url, 'decaf') !== false |
|
359 | + && strpos($payment_method->type_obj()->default_button_url(), 'decaf') === false |
|
360 | + ) |
|
361 | + ) { |
|
362 | + $payment_method->save( |
|
363 | + [ |
|
364 | + 'PMD_button_url' => $payment_method->type_obj()->default_button_url(), |
|
365 | + ] |
|
366 | + ); |
|
367 | + } |
|
368 | + } catch (EE_Error $e) { |
|
369 | + $payment_method->deactivate(); |
|
370 | + } |
|
371 | + } |
|
372 | + } |
|
373 | + |
|
374 | + |
|
375 | + /** |
|
376 | + * Overrides parent to not only turn wpdb results into EE_Payment_Method objects, |
|
377 | + * but also verifies the payment method type of each is a usable object. If not, |
|
378 | + * deactivate it, sets a notification, and deactivates it |
|
379 | + * |
|
380 | + * @param array $rows |
|
381 | + * @return EE_Payment_Method[] |
|
382 | + * @throws EE_Error |
|
383 | + * @throws InvalidDataTypeException |
|
384 | + * @throws ReflectionException |
|
385 | + */ |
|
386 | + protected function _create_objects($rows = []) |
|
387 | + { |
|
388 | + EE_Registry::instance()->load_lib('Payment_Method_Manager'); |
|
389 | + $payment_methods = parent::_create_objects($rows); |
|
390 | + /* @var $payment_methods EE_Payment_Method[] */ |
|
391 | + $usable_payment_methods = []; |
|
392 | + foreach ($payment_methods as $key => $payment_method) { |
|
393 | + if (EE_Payment_Method_Manager::instance()->payment_method_type_exists($payment_method->type())) { |
|
394 | + $usable_payment_methods[ $key ] = $payment_method; |
|
395 | + // some payment methods enqueue their scripts in EE_PMT_*::__construct |
|
396 | + // which is kinda a no-no (just because it's being constructed doesn't mean we need to enqueue |
|
397 | + // its scripts). but for backwards-compat we should continue to do that |
|
398 | + $payment_method->type_obj(); |
|
399 | + } elseif ($payment_method->active()) { |
|
400 | + // only deactivate and notify the admin if the payment is active somewhere |
|
401 | + $payment_method->deactivate(); |
|
402 | + $payment_method->save(); |
|
403 | + do_action( |
|
404 | + 'AHEE__EEM_Payment_Method___create_objects_auto_deactivated_payment_method', |
|
405 | + $payment_method |
|
406 | + ); |
|
407 | + new PersistentAdminNotice( |
|
408 | + 'auto-deactivated-' . $payment_method->type(), |
|
409 | + sprintf( |
|
410 | + esc_html__( |
|
411 | + 'The payment method %1$s was automatically deactivated because it appears its associated Event Espresso Addon was recently deactivated.%2$sIt can be reactivated on the %3$sPlugins admin page%4$s, then you can reactivate the payment method.', |
|
412 | + 'event_espresso' |
|
413 | + ), |
|
414 | + $payment_method->admin_name(), |
|
415 | + '<br />', |
|
416 | + '<a href="' . admin_url('plugins.php') . '">', |
|
417 | + '</a>' |
|
418 | + ), |
|
419 | + true |
|
420 | + ); |
|
421 | + } |
|
422 | + } |
|
423 | + return $usable_payment_methods; |
|
424 | + } |
|
425 | + |
|
426 | + |
|
427 | + /** |
|
428 | + * Gets all the payment methods which can be used for transaction |
|
429 | + * (according to the relations between payment methods and events, and |
|
430 | + * the currencies used for the transaction and their relation to payment methods) |
|
431 | + * |
|
432 | + * @param EE_Transaction $transaction |
|
433 | + * @param string $scope @see EEM_Payment_Method::get_all_for_events |
|
434 | + * @return EE_Payment_Method[] |
|
435 | + * @throws EE_Error |
|
436 | + */ |
|
437 | + public function get_all_for_transaction($transaction, $scope) |
|
438 | + { |
|
439 | + // give addons a chance to override what payment methods are chosen based on the transaction |
|
440 | + return apply_filters( |
|
441 | + 'FHEE__EEM_Payment_Method__get_all_for_transaction__payment_methods', |
|
442 | + $this->get_all_active($scope, ['group_by' => 'PMD_type']), |
|
443 | + $transaction, |
|
444 | + $scope |
|
445 | + ); |
|
446 | + } |
|
447 | + |
|
448 | + |
|
449 | + /** |
|
450 | + * Returns the payment method used for the last payment made for a registration. |
|
451 | + * Note: if an offline payment method was selected on the related transaction then this will have no payment |
|
452 | + * methods returned. It will ONLY return a payment method for a PAYMENT recorded against the registration. |
|
453 | + * |
|
454 | + * @param EE_Registration|int $registration_or_reg_id Either the EE_Registration object or the id for the |
|
455 | + * registration. |
|
456 | + * @return EE_Payment|null |
|
457 | + * @throws EE_Error |
|
458 | + */ |
|
459 | + public function get_last_used_for_registration($registration_or_reg_id) |
|
460 | + { |
|
461 | + $registration_id = EEM_Registration::instance()->ensure_is_ID($registration_or_reg_id); |
|
462 | + |
|
463 | + $query_params = [ |
|
464 | + 0 => [ |
|
465 | + 'Payment.Registration.REG_ID' => $registration_id, |
|
466 | + ], |
|
467 | + 'order_by' => ['Payment.PAY_ID' => 'DESC'], |
|
468 | + ]; |
|
469 | + return $this->get_one($query_params); |
|
470 | + } |
|
471 | 471 | } |
@@ -169,7 +169,7 @@ discard block |
||
169 | 169 | public function is_valid_scope($scope) |
170 | 170 | { |
171 | 171 | $scopes = $this->scopes(); |
172 | - if (isset($scopes[ $scope ])) { |
|
172 | + if (isset($scopes[$scope])) { |
|
173 | 173 | return true; |
174 | 174 | } |
175 | 175 | return false; |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | */ |
187 | 187 | public function get_all_active($scope = null, $query_params = []) |
188 | 188 | { |
189 | - if (! isset($query_params['order_by']) && ! isset($query_params['order'])) { |
|
189 | + if ( ! isset($query_params['order_by']) && ! isset($query_params['order'])) { |
|
190 | 190 | $query_params['order_by'] = ['PMD_order' => 'ASC', 'PMD_ID' => 'ASC']; |
191 | 191 | } |
192 | 192 | return $this->get_all($this->_get_query_params_for_all_active($scope, $query_params)); |
@@ -234,7 +234,7 @@ discard block |
||
234 | 234 | $count = 0; |
235 | 235 | foreach ($this->scopes() as $scope_name => $desc) { |
236 | 236 | $count++; |
237 | - $acceptable_scopes[ 'PMD_scope*' . $count ] = ['LIKE', '%' . $scope_name . '%']; |
|
237 | + $acceptable_scopes['PMD_scope*'.$count] = ['LIKE', '%'.$scope_name.'%']; |
|
238 | 238 | } |
239 | 239 | return array_replace_recursive([['OR*active_scope' => $acceptable_scopes]], $query_params); |
240 | 240 | } |
@@ -391,7 +391,7 @@ discard block |
||
391 | 391 | $usable_payment_methods = []; |
392 | 392 | foreach ($payment_methods as $key => $payment_method) { |
393 | 393 | if (EE_Payment_Method_Manager::instance()->payment_method_type_exists($payment_method->type())) { |
394 | - $usable_payment_methods[ $key ] = $payment_method; |
|
394 | + $usable_payment_methods[$key] = $payment_method; |
|
395 | 395 | // some payment methods enqueue their scripts in EE_PMT_*::__construct |
396 | 396 | // which is kinda a no-no (just because it's being constructed doesn't mean we need to enqueue |
397 | 397 | // its scripts). but for backwards-compat we should continue to do that |
@@ -405,7 +405,7 @@ discard block |
||
405 | 405 | $payment_method |
406 | 406 | ); |
407 | 407 | new PersistentAdminNotice( |
408 | - 'auto-deactivated-' . $payment_method->type(), |
|
408 | + 'auto-deactivated-'.$payment_method->type(), |
|
409 | 409 | sprintf( |
410 | 410 | esc_html__( |
411 | 411 | 'The payment method %1$s was automatically deactivated because it appears its associated Event Espresso Addon was recently deactivated.%2$sIt can be reactivated on the %3$sPlugins admin page%4$s, then you can reactivate the payment method.', |
@@ -413,7 +413,7 @@ discard block |
||
413 | 413 | ), |
414 | 414 | $payment_method->admin_name(), |
415 | 415 | '<br />', |
416 | - '<a href="' . admin_url('plugins.php') . '">', |
|
416 | + '<a href="'.admin_url('plugins.php').'">', |
|
417 | 417 | '</a>' |
418 | 418 | ), |
419 | 419 | true |
@@ -10,229 +10,229 @@ |
||
10 | 10 | */ |
11 | 11 | abstract class EE_Display_Strategy_Base extends EE_Form_Input_Strategy_Base |
12 | 12 | { |
13 | - /** |
|
14 | - * @var string $_tag |
|
15 | - */ |
|
16 | - protected string $_tag = ''; |
|
17 | - |
|
18 | - |
|
19 | - /** |
|
20 | - * returns HTML and javascript related to the displaying of this input |
|
21 | - * |
|
22 | - * @return string |
|
23 | - */ |
|
24 | - abstract public function display(): string; |
|
25 | - |
|
26 | - |
|
27 | - /** |
|
28 | - * _remove_chars - takes an incoming string, and removes the string $chars from the end of it, but only if $chars |
|
29 | - * is already there |
|
30 | - * |
|
31 | - * @param string $string - the string being processed |
|
32 | - * @param string $chars - exact string of characters to remove |
|
33 | - * @return string |
|
34 | - */ |
|
35 | - protected function _remove_chars(string $string = '', string $chars = '-'): string |
|
36 | - { |
|
37 | - $char_length = strlen($chars) * -1; |
|
38 | - // if last three characters of string is " - ", then remove it |
|
39 | - return substr($string, $char_length) === $chars |
|
40 | - ? substr($string, 0, $char_length) |
|
41 | - : $string; |
|
42 | - } |
|
43 | - |
|
44 | - |
|
45 | - /** |
|
46 | - * _append_chars - takes an incoming string, and adds the string $chars to the end of it, but only if $chars is not |
|
47 | - * already there |
|
48 | - * |
|
49 | - * @param string $string - the string being processed |
|
50 | - * @param string $chars - exact string of characters to be added to end of string |
|
51 | - * @return string |
|
52 | - */ |
|
53 | - protected function _append_chars(string $string = '', string $chars = '-'): string |
|
54 | - { |
|
55 | - return $this->_remove_chars($string, $chars) . $chars; |
|
56 | - } |
|
57 | - |
|
58 | - |
|
59 | - /** |
|
60 | - * Gets the HTML IDs of all the inputs |
|
61 | - * |
|
62 | - * @param bool $add_pound_sign |
|
63 | - * @return array |
|
64 | - * @throws EE_Error |
|
65 | - */ |
|
66 | - public function get_html_input_ids(bool $add_pound_sign = false): array |
|
67 | - { |
|
68 | - return [$this->get_input()->html_id($add_pound_sign)]; |
|
69 | - } |
|
70 | - |
|
71 | - |
|
72 | - /** |
|
73 | - * Adds js variables for localization to the $other_js_data. These should be put |
|
74 | - * in each form's "other_data" javascript object. |
|
75 | - * |
|
76 | - * @param array $other_js_data |
|
77 | - * @return array |
|
78 | - */ |
|
79 | - public function get_other_js_data(array $other_js_data = []): array |
|
80 | - { |
|
81 | - return $other_js_data; |
|
82 | - } |
|
83 | - |
|
84 | - |
|
85 | - /** |
|
86 | - * Opportunity for this display strategy to call wp_enqueue_script and wp_enqueue_style. |
|
87 | - * This should be called during wp_enqueue_scripts |
|
88 | - */ |
|
89 | - public function enqueue_js() |
|
90 | - { |
|
91 | - } |
|
92 | - |
|
93 | - |
|
94 | - /** |
|
95 | - * returns string like: '<tag' |
|
96 | - * |
|
97 | - * @param string $tag |
|
98 | - * @return string |
|
99 | - */ |
|
100 | - protected function _opening_tag(string $tag): string |
|
101 | - { |
|
102 | - $this->_tag = $tag; |
|
103 | - return "<$this->_tag"; |
|
104 | - } |
|
105 | - |
|
106 | - |
|
107 | - /** |
|
108 | - * returns string like: '</tag> |
|
109 | - * |
|
110 | - * @return string |
|
111 | - */ |
|
112 | - protected function _closing_tag(): string |
|
113 | - { |
|
114 | - return "</$this->_tag>"; |
|
115 | - } |
|
116 | - |
|
117 | - |
|
118 | - /** |
|
119 | - * returns string like: '/>' |
|
120 | - * |
|
121 | - * @return string |
|
122 | - */ |
|
123 | - protected function _close_tag(): string |
|
124 | - { |
|
125 | - return '/>'; |
|
126 | - } |
|
127 | - |
|
128 | - |
|
129 | - /** |
|
130 | - * returns an array of standard HTML attributes that get added to nearly all inputs, |
|
131 | - * where string keys represent named attributes like id, class, etc |
|
132 | - * and numeric keys represent single attributes like 'required'. |
|
133 | - * Note: this does not include "value" because many inputs (like dropdowns, textareas, and checkboxes) don't use |
|
134 | - * it. |
|
135 | - * |
|
136 | - * @return array |
|
137 | - * @throws EE_Error |
|
138 | - */ |
|
139 | - protected function _standard_attributes_array(): array |
|
140 | - { |
|
141 | - return [ |
|
142 | - 'name' => $this->_input->html_name(), |
|
143 | - 'id' => $this->_input->html_id(), |
|
144 | - 'class' => $this->_input->html_class(true), |
|
145 | - 0 => ['required', $this->_input->required()], |
|
146 | - 1 => $this->_input->other_html_attributes(), |
|
147 | - 'style' => $this->_input->html_style(), |
|
148 | - ]; |
|
149 | - } |
|
150 | - |
|
151 | - |
|
152 | - /** |
|
153 | - * sets the attributes using the incoming array |
|
154 | - * and returns a string of all attributes rendered as valid HTML |
|
155 | - * |
|
156 | - * @param array $attributes |
|
157 | - * @return string |
|
158 | - */ |
|
159 | - protected function _attributes_string(array $attributes = []): string |
|
160 | - { |
|
161 | - $attributes = apply_filters( |
|
162 | - 'FHEE__EE_Display_Strategy_Base__attributes_string__attributes', |
|
163 | - $attributes, |
|
164 | - $this, |
|
165 | - $this->_input |
|
166 | - ); |
|
167 | - $attributes_string = ''; |
|
168 | - foreach ($attributes as $attribute => $value) { |
|
169 | - if ($attribute !== 'value' && empty($value)) { |
|
170 | - continue; |
|
171 | - } |
|
172 | - if (is_numeric($attribute)) { |
|
173 | - $add = true; |
|
174 | - if (is_array($value)) { |
|
175 | - $attribute = $value[0] ?? ''; |
|
176 | - $add = $value[1] ?? false; |
|
177 | - } else { |
|
178 | - $attribute = $value; |
|
179 | - } |
|
180 | - $attributes_string .= $this->_single_attribute($attribute, $add); |
|
181 | - } else { |
|
182 | - $attributes_string .= $this->_attribute($attribute, $value); |
|
183 | - } |
|
184 | - } |
|
185 | - return $attributes_string; |
|
186 | - } |
|
187 | - |
|
188 | - |
|
189 | - /** |
|
190 | - * returns string like: ' attribute="value"' |
|
191 | - * returns an empty string if $value is null |
|
192 | - * |
|
193 | - * @param string $attribute |
|
194 | - * @param string $value |
|
195 | - * @return string |
|
196 | - */ |
|
197 | - protected function _attribute(string $attribute, string $value = ''): string |
|
198 | - { |
|
199 | - if ($value === null) { |
|
200 | - return ''; |
|
201 | - } |
|
202 | - $value = esc_attr($value); |
|
203 | - return " $attribute=\"$value\""; |
|
204 | - } |
|
205 | - |
|
206 | - |
|
207 | - /** |
|
208 | - * returns string like: ' data-attribute="value"' |
|
209 | - * returns an empty string if $value is null |
|
210 | - * |
|
211 | - * @param string $attribute |
|
212 | - * @param string $value |
|
213 | - * @return string |
|
214 | - */ |
|
215 | - protected function _data_attribute(string $attribute, string $value = ''): string |
|
216 | - { |
|
217 | - if ($value === null) { |
|
218 | - return ''; |
|
219 | - } |
|
220 | - $value = esc_attr($value); |
|
221 | - return " data-$attribute=\"$value\""; |
|
222 | - } |
|
223 | - |
|
224 | - |
|
225 | - /** |
|
226 | - * returns string like: ' attribute' if $add is true |
|
227 | - * |
|
228 | - * @param string $attribute |
|
229 | - * @param boolean $add |
|
230 | - * @return string |
|
231 | - */ |
|
232 | - protected function _single_attribute(string $attribute, bool $add = true): string |
|
233 | - { |
|
234 | - return $add |
|
235 | - ? " $attribute" |
|
236 | - : ''; |
|
237 | - } |
|
13 | + /** |
|
14 | + * @var string $_tag |
|
15 | + */ |
|
16 | + protected string $_tag = ''; |
|
17 | + |
|
18 | + |
|
19 | + /** |
|
20 | + * returns HTML and javascript related to the displaying of this input |
|
21 | + * |
|
22 | + * @return string |
|
23 | + */ |
|
24 | + abstract public function display(): string; |
|
25 | + |
|
26 | + |
|
27 | + /** |
|
28 | + * _remove_chars - takes an incoming string, and removes the string $chars from the end of it, but only if $chars |
|
29 | + * is already there |
|
30 | + * |
|
31 | + * @param string $string - the string being processed |
|
32 | + * @param string $chars - exact string of characters to remove |
|
33 | + * @return string |
|
34 | + */ |
|
35 | + protected function _remove_chars(string $string = '', string $chars = '-'): string |
|
36 | + { |
|
37 | + $char_length = strlen($chars) * -1; |
|
38 | + // if last three characters of string is " - ", then remove it |
|
39 | + return substr($string, $char_length) === $chars |
|
40 | + ? substr($string, 0, $char_length) |
|
41 | + : $string; |
|
42 | + } |
|
43 | + |
|
44 | + |
|
45 | + /** |
|
46 | + * _append_chars - takes an incoming string, and adds the string $chars to the end of it, but only if $chars is not |
|
47 | + * already there |
|
48 | + * |
|
49 | + * @param string $string - the string being processed |
|
50 | + * @param string $chars - exact string of characters to be added to end of string |
|
51 | + * @return string |
|
52 | + */ |
|
53 | + protected function _append_chars(string $string = '', string $chars = '-'): string |
|
54 | + { |
|
55 | + return $this->_remove_chars($string, $chars) . $chars; |
|
56 | + } |
|
57 | + |
|
58 | + |
|
59 | + /** |
|
60 | + * Gets the HTML IDs of all the inputs |
|
61 | + * |
|
62 | + * @param bool $add_pound_sign |
|
63 | + * @return array |
|
64 | + * @throws EE_Error |
|
65 | + */ |
|
66 | + public function get_html_input_ids(bool $add_pound_sign = false): array |
|
67 | + { |
|
68 | + return [$this->get_input()->html_id($add_pound_sign)]; |
|
69 | + } |
|
70 | + |
|
71 | + |
|
72 | + /** |
|
73 | + * Adds js variables for localization to the $other_js_data. These should be put |
|
74 | + * in each form's "other_data" javascript object. |
|
75 | + * |
|
76 | + * @param array $other_js_data |
|
77 | + * @return array |
|
78 | + */ |
|
79 | + public function get_other_js_data(array $other_js_data = []): array |
|
80 | + { |
|
81 | + return $other_js_data; |
|
82 | + } |
|
83 | + |
|
84 | + |
|
85 | + /** |
|
86 | + * Opportunity for this display strategy to call wp_enqueue_script and wp_enqueue_style. |
|
87 | + * This should be called during wp_enqueue_scripts |
|
88 | + */ |
|
89 | + public function enqueue_js() |
|
90 | + { |
|
91 | + } |
|
92 | + |
|
93 | + |
|
94 | + /** |
|
95 | + * returns string like: '<tag' |
|
96 | + * |
|
97 | + * @param string $tag |
|
98 | + * @return string |
|
99 | + */ |
|
100 | + protected function _opening_tag(string $tag): string |
|
101 | + { |
|
102 | + $this->_tag = $tag; |
|
103 | + return "<$this->_tag"; |
|
104 | + } |
|
105 | + |
|
106 | + |
|
107 | + /** |
|
108 | + * returns string like: '</tag> |
|
109 | + * |
|
110 | + * @return string |
|
111 | + */ |
|
112 | + protected function _closing_tag(): string |
|
113 | + { |
|
114 | + return "</$this->_tag>"; |
|
115 | + } |
|
116 | + |
|
117 | + |
|
118 | + /** |
|
119 | + * returns string like: '/>' |
|
120 | + * |
|
121 | + * @return string |
|
122 | + */ |
|
123 | + protected function _close_tag(): string |
|
124 | + { |
|
125 | + return '/>'; |
|
126 | + } |
|
127 | + |
|
128 | + |
|
129 | + /** |
|
130 | + * returns an array of standard HTML attributes that get added to nearly all inputs, |
|
131 | + * where string keys represent named attributes like id, class, etc |
|
132 | + * and numeric keys represent single attributes like 'required'. |
|
133 | + * Note: this does not include "value" because many inputs (like dropdowns, textareas, and checkboxes) don't use |
|
134 | + * it. |
|
135 | + * |
|
136 | + * @return array |
|
137 | + * @throws EE_Error |
|
138 | + */ |
|
139 | + protected function _standard_attributes_array(): array |
|
140 | + { |
|
141 | + return [ |
|
142 | + 'name' => $this->_input->html_name(), |
|
143 | + 'id' => $this->_input->html_id(), |
|
144 | + 'class' => $this->_input->html_class(true), |
|
145 | + 0 => ['required', $this->_input->required()], |
|
146 | + 1 => $this->_input->other_html_attributes(), |
|
147 | + 'style' => $this->_input->html_style(), |
|
148 | + ]; |
|
149 | + } |
|
150 | + |
|
151 | + |
|
152 | + /** |
|
153 | + * sets the attributes using the incoming array |
|
154 | + * and returns a string of all attributes rendered as valid HTML |
|
155 | + * |
|
156 | + * @param array $attributes |
|
157 | + * @return string |
|
158 | + */ |
|
159 | + protected function _attributes_string(array $attributes = []): string |
|
160 | + { |
|
161 | + $attributes = apply_filters( |
|
162 | + 'FHEE__EE_Display_Strategy_Base__attributes_string__attributes', |
|
163 | + $attributes, |
|
164 | + $this, |
|
165 | + $this->_input |
|
166 | + ); |
|
167 | + $attributes_string = ''; |
|
168 | + foreach ($attributes as $attribute => $value) { |
|
169 | + if ($attribute !== 'value' && empty($value)) { |
|
170 | + continue; |
|
171 | + } |
|
172 | + if (is_numeric($attribute)) { |
|
173 | + $add = true; |
|
174 | + if (is_array($value)) { |
|
175 | + $attribute = $value[0] ?? ''; |
|
176 | + $add = $value[1] ?? false; |
|
177 | + } else { |
|
178 | + $attribute = $value; |
|
179 | + } |
|
180 | + $attributes_string .= $this->_single_attribute($attribute, $add); |
|
181 | + } else { |
|
182 | + $attributes_string .= $this->_attribute($attribute, $value); |
|
183 | + } |
|
184 | + } |
|
185 | + return $attributes_string; |
|
186 | + } |
|
187 | + |
|
188 | + |
|
189 | + /** |
|
190 | + * returns string like: ' attribute="value"' |
|
191 | + * returns an empty string if $value is null |
|
192 | + * |
|
193 | + * @param string $attribute |
|
194 | + * @param string $value |
|
195 | + * @return string |
|
196 | + */ |
|
197 | + protected function _attribute(string $attribute, string $value = ''): string |
|
198 | + { |
|
199 | + if ($value === null) { |
|
200 | + return ''; |
|
201 | + } |
|
202 | + $value = esc_attr($value); |
|
203 | + return " $attribute=\"$value\""; |
|
204 | + } |
|
205 | + |
|
206 | + |
|
207 | + /** |
|
208 | + * returns string like: ' data-attribute="value"' |
|
209 | + * returns an empty string if $value is null |
|
210 | + * |
|
211 | + * @param string $attribute |
|
212 | + * @param string $value |
|
213 | + * @return string |
|
214 | + */ |
|
215 | + protected function _data_attribute(string $attribute, string $value = ''): string |
|
216 | + { |
|
217 | + if ($value === null) { |
|
218 | + return ''; |
|
219 | + } |
|
220 | + $value = esc_attr($value); |
|
221 | + return " data-$attribute=\"$value\""; |
|
222 | + } |
|
223 | + |
|
224 | + |
|
225 | + /** |
|
226 | + * returns string like: ' attribute' if $add is true |
|
227 | + * |
|
228 | + * @param string $attribute |
|
229 | + * @param boolean $add |
|
230 | + * @return string |
|
231 | + */ |
|
232 | + protected function _single_attribute(string $attribute, bool $add = true): string |
|
233 | + { |
|
234 | + return $add |
|
235 | + ? " $attribute" |
|
236 | + : ''; |
|
237 | + } |
|
238 | 238 | } |
@@ -10,83 +10,83 @@ |
||
10 | 10 | */ |
11 | 11 | class EE_Select_Display_Strategy extends EE_Display_Strategy_Base |
12 | 12 | { |
13 | - /** |
|
14 | - * @return string of html to display the field |
|
15 | - * @throws EE_Error |
|
16 | - */ |
|
17 | - public function display(): string |
|
18 | - { |
|
19 | - if (! $this->_input instanceof EE_Form_Input_With_Options_Base) { |
|
20 | - throw new EE_Error( |
|
21 | - esc_html__( |
|
22 | - 'Cannot use Select Display Strategy with an input that doesn\'t have options', |
|
23 | - 'event_espresso' |
|
24 | - ) |
|
25 | - ); |
|
26 | - } |
|
13 | + /** |
|
14 | + * @return string of html to display the field |
|
15 | + * @throws EE_Error |
|
16 | + */ |
|
17 | + public function display(): string |
|
18 | + { |
|
19 | + if (! $this->_input instanceof EE_Form_Input_With_Options_Base) { |
|
20 | + throw new EE_Error( |
|
21 | + esc_html__( |
|
22 | + 'Cannot use Select Display Strategy with an input that doesn\'t have options', |
|
23 | + 'event_espresso' |
|
24 | + ) |
|
25 | + ); |
|
26 | + } |
|
27 | 27 | |
28 | - $html = EEH_HTML::nl(0, 'select'); |
|
29 | - $html .= '<select'; |
|
30 | - $html .= $this->_attributes_string( |
|
31 | - $this->_standard_attributes_array() |
|
32 | - ); |
|
33 | - $html .= '>'; |
|
28 | + $html = EEH_HTML::nl(0, 'select'); |
|
29 | + $html .= '<select'; |
|
30 | + $html .= $this->_attributes_string( |
|
31 | + $this->_standard_attributes_array() |
|
32 | + ); |
|
33 | + $html .= '>'; |
|
34 | 34 | |
35 | - if (EEH_Array::is_multi_dimensional_array($this->_input->options())) { |
|
36 | - EEH_HTML::indent(1, 'optgroup'); |
|
37 | - foreach ($this->_input->options() as $opt_group_label => $opt_group) { |
|
38 | - if (! empty($opt_group_label)) { |
|
39 | - $html .= EEH_HTML::nl(0, 'optgroup') . '<optgroup label="' . esc_attr($opt_group_label) . '">'; |
|
40 | - } |
|
41 | - EEH_HTML::indent(1, 'option'); |
|
42 | - $html .= $this->_display_options($opt_group); |
|
43 | - EEH_HTML::indent(-1, 'option'); |
|
44 | - if (! empty($opt_group_label)) { |
|
45 | - $html .= EEH_HTML::nl(0, 'optgroup') . '</optgroup>'; |
|
46 | - } |
|
47 | - } |
|
48 | - EEH_HTML::indent(-1, 'optgroup'); |
|
49 | - } else { |
|
50 | - $html .= $this->_display_options($this->_input->options()); |
|
51 | - } |
|
35 | + if (EEH_Array::is_multi_dimensional_array($this->_input->options())) { |
|
36 | + EEH_HTML::indent(1, 'optgroup'); |
|
37 | + foreach ($this->_input->options() as $opt_group_label => $opt_group) { |
|
38 | + if (! empty($opt_group_label)) { |
|
39 | + $html .= EEH_HTML::nl(0, 'optgroup') . '<optgroup label="' . esc_attr($opt_group_label) . '">'; |
|
40 | + } |
|
41 | + EEH_HTML::indent(1, 'option'); |
|
42 | + $html .= $this->_display_options($opt_group); |
|
43 | + EEH_HTML::indent(-1, 'option'); |
|
44 | + if (! empty($opt_group_label)) { |
|
45 | + $html .= EEH_HTML::nl(0, 'optgroup') . '</optgroup>'; |
|
46 | + } |
|
47 | + } |
|
48 | + EEH_HTML::indent(-1, 'optgroup'); |
|
49 | + } else { |
|
50 | + $html .= $this->_display_options($this->_input->options()); |
|
51 | + } |
|
52 | 52 | |
53 | - $html .= EEH_HTML::nl(0, 'select') . '</select>'; |
|
54 | - return $html; |
|
55 | - } |
|
53 | + $html .= EEH_HTML::nl(0, 'select') . '</select>'; |
|
54 | + return $html; |
|
55 | + } |
|
56 | 56 | |
57 | 57 | |
58 | - /** |
|
59 | - * Displays a flat list of options as option tags |
|
60 | - * |
|
61 | - * @param array $options |
|
62 | - * @return string |
|
63 | - */ |
|
64 | - protected function _display_options(array $options): string |
|
65 | - { |
|
66 | - $html = ''; |
|
67 | - EEH_HTML::indent(1, 'option'); |
|
68 | - foreach ($options as $value => $display_text) { |
|
69 | - // even if this input uses EE_Text_Normalization if one of the array keys is a numeric string, like "123", |
|
70 | - // PHP will have converted it to a PHP integer (eg 123). So we need to make sure it's a string |
|
71 | - $value = $this->_input->get_normalization_strategy()->unnormalize_one($value); |
|
72 | - $selected = $this->_check_if_option_selected($value) ? ' selected' : ''; |
|
73 | - $value = esc_attr($value); |
|
74 | - $html .= EEH_HTML::nl(0, 'option'); |
|
75 | - $html .= "<option value=\"$value\"$selected>$display_text</option>"; |
|
76 | - } |
|
77 | - EEH_HTML::indent(-1, 'option'); |
|
78 | - return $html; |
|
79 | - } |
|
58 | + /** |
|
59 | + * Displays a flat list of options as option tags |
|
60 | + * |
|
61 | + * @param array $options |
|
62 | + * @return string |
|
63 | + */ |
|
64 | + protected function _display_options(array $options): string |
|
65 | + { |
|
66 | + $html = ''; |
|
67 | + EEH_HTML::indent(1, 'option'); |
|
68 | + foreach ($options as $value => $display_text) { |
|
69 | + // even if this input uses EE_Text_Normalization if one of the array keys is a numeric string, like "123", |
|
70 | + // PHP will have converted it to a PHP integer (eg 123). So we need to make sure it's a string |
|
71 | + $value = $this->_input->get_normalization_strategy()->unnormalize_one($value); |
|
72 | + $selected = $this->_check_if_option_selected($value) ? ' selected' : ''; |
|
73 | + $value = esc_attr($value); |
|
74 | + $html .= EEH_HTML::nl(0, 'option'); |
|
75 | + $html .= "<option value=\"$value\"$selected>$display_text</option>"; |
|
76 | + } |
|
77 | + EEH_HTML::indent(-1, 'option'); |
|
78 | + return $html; |
|
79 | + } |
|
80 | 80 | |
81 | 81 | |
82 | - /** |
|
83 | - * Checks if that value is the one selected |
|
84 | - * |
|
85 | - * @param string|int $option_value unnormalized value option (string). How it will appear in the HTML. |
|
86 | - * @return bool |
|
87 | - */ |
|
88 | - protected function _check_if_option_selected($option_value): bool |
|
89 | - { |
|
90 | - return $option_value === $this->_input->raw_value(); |
|
91 | - } |
|
82 | + /** |
|
83 | + * Checks if that value is the one selected |
|
84 | + * |
|
85 | + * @param string|int $option_value unnormalized value option (string). How it will appear in the HTML. |
|
86 | + * @return bool |
|
87 | + */ |
|
88 | + protected function _check_if_option_selected($option_value): bool |
|
89 | + { |
|
90 | + return $option_value === $this->_input->raw_value(); |
|
91 | + } |
|
92 | 92 | } |
@@ -13,28 +13,28 @@ |
||
13 | 13 | */ |
14 | 14 | class EE_Plaintext_Validation_Strategy extends EE_Validation_Strategy_Base |
15 | 15 | { |
16 | - /** |
|
17 | - * @param null $validation_error_message |
|
18 | - */ |
|
19 | - public function __construct($validation_error_message = null) |
|
20 | - { |
|
21 | - if (! $validation_error_message) { |
|
22 | - $validation_error_message = esc_html__("HTML tags are not permitted in this field", "event_espresso"); |
|
23 | - } |
|
24 | - parent::__construct($validation_error_message); |
|
25 | - } |
|
16 | + /** |
|
17 | + * @param null $validation_error_message |
|
18 | + */ |
|
19 | + public function __construct($validation_error_message = null) |
|
20 | + { |
|
21 | + if (! $validation_error_message) { |
|
22 | + $validation_error_message = esc_html__("HTML tags are not permitted in this field", "event_espresso"); |
|
23 | + } |
|
24 | + parent::__construct($validation_error_message); |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * @param string|null $normalized_value |
|
29 | - * @throws EE_Validation_Error |
|
30 | - */ |
|
31 | - public function validate($normalized_value = '') |
|
32 | - { |
|
33 | - $normalized_value = (string) $normalized_value; |
|
34 | - $no_tags = wp_strip_all_tags($normalized_value); |
|
35 | - if (strlen($no_tags) < strlen(trim($normalized_value))) { |
|
36 | - throw new EE_Validation_Error($this->get_validation_error_message(), 'no_html_tags'); |
|
37 | - } |
|
38 | - parent::validate($normalized_value); |
|
39 | - } |
|
27 | + /** |
|
28 | + * @param string|null $normalized_value |
|
29 | + * @throws EE_Validation_Error |
|
30 | + */ |
|
31 | + public function validate($normalized_value = '') |
|
32 | + { |
|
33 | + $normalized_value = (string) $normalized_value; |
|
34 | + $no_tags = wp_strip_all_tags($normalized_value); |
|
35 | + if (strlen($no_tags) < strlen(trim($normalized_value))) { |
|
36 | + throw new EE_Validation_Error($this->get_validation_error_message(), 'no_html_tags'); |
|
37 | + } |
|
38 | + parent::validate($normalized_value); |
|
39 | + } |
|
40 | 40 | } |
@@ -14,1220 +14,1220 @@ |
||
14 | 14 | */ |
15 | 15 | abstract class EE_Form_Input_Base extends EE_Form_Section_Validatable |
16 | 16 | { |
17 | - /** |
|
18 | - * the input's name attribute |
|
19 | - * |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - protected $_html_name; |
|
23 | - |
|
24 | - /** |
|
25 | - * id for the html label tag |
|
26 | - * |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - protected $_html_label_id; |
|
30 | - |
|
31 | - /** |
|
32 | - * class for teh html label tag |
|
33 | - * |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - protected $_html_label_class; |
|
37 | - |
|
38 | - /** |
|
39 | - * style for teh html label tag |
|
40 | - * |
|
41 | - * @var string |
|
42 | - */ |
|
43 | - protected $_html_label_style; |
|
44 | - |
|
45 | - /** |
|
46 | - * text to be placed in the html label |
|
47 | - * |
|
48 | - * @var string |
|
49 | - */ |
|
50 | - protected $_html_label_text; |
|
51 | - |
|
52 | - /** |
|
53 | - * the full html label. If used, all other html_label_* properties are invalid |
|
54 | - * |
|
55 | - * @var string |
|
56 | - */ |
|
57 | - protected $_html_label; |
|
58 | - |
|
59 | - /** |
|
60 | - * HTML to use for help text (normally placed below form input), in a span which normally |
|
61 | - * has a class of 'description' |
|
62 | - * |
|
63 | - * @var string |
|
64 | - */ |
|
65 | - protected $_html_help_text; |
|
66 | - |
|
67 | - /** |
|
68 | - * CSS classes for displaying the help span |
|
69 | - * |
|
70 | - * @var string |
|
71 | - */ |
|
72 | - protected $_html_help_class = 'description'; |
|
73 | - |
|
74 | - /** |
|
75 | - * CSS to put in the style attribute on the help span |
|
76 | - * |
|
77 | - * @var string |
|
78 | - */ |
|
79 | - protected $_html_help_style; |
|
80 | - |
|
81 | - /** |
|
82 | - * Stores whether or not this input's response is required. |
|
83 | - * Because certain styling elements may also want to know that this |
|
84 | - * input is required etc. |
|
85 | - * |
|
86 | - * @var boolean |
|
87 | - */ |
|
88 | - protected $_required; |
|
89 | - |
|
90 | - /** |
|
91 | - * css class added to required inputs |
|
92 | - * |
|
93 | - * @var string |
|
94 | - */ |
|
95 | - protected $_required_css_class = 'ee-required'; |
|
96 | - |
|
97 | - /** |
|
98 | - * css styles applied to button type inputs |
|
99 | - * |
|
100 | - * @var string |
|
101 | - */ |
|
102 | - protected $_button_css_attributes; |
|
103 | - |
|
104 | - /** |
|
105 | - * The raw post data submitted for this |
|
106 | - * Generally unsafe for usage in client code |
|
107 | - * |
|
108 | - * @var mixed string or array |
|
109 | - */ |
|
110 | - protected $_raw_value; |
|
111 | - |
|
112 | - /** |
|
113 | - * Value normalized according to the input's normalization strategy. |
|
114 | - * The normalization strategy dictates whether this is a string, int, float, |
|
115 | - * boolean, or array of any of those. |
|
116 | - * |
|
117 | - * @var mixed |
|
118 | - */ |
|
119 | - protected $_normalized_value; |
|
120 | - |
|
121 | - |
|
122 | - /** |
|
123 | - * Normalized default value either initially set on the input, or provided by calling |
|
124 | - * set_default(). |
|
125 | - * |
|
126 | - * @var mixed |
|
127 | - */ |
|
128 | - protected $_default; |
|
129 | - |
|
130 | - /** |
|
131 | - * Strategy used for displaying this field. |
|
132 | - * Child classes must use _get_display_strategy to access it. |
|
133 | - * |
|
134 | - * @var EE_Display_Strategy_Base |
|
135 | - */ |
|
136 | - private $_display_strategy; |
|
137 | - |
|
138 | - /** |
|
139 | - * Gets all the validation strategies used on this field |
|
140 | - * |
|
141 | - * @var EE_Validation_Strategy_Base[] |
|
142 | - */ |
|
143 | - private $_validation_strategies = []; |
|
144 | - |
|
145 | - /** |
|
146 | - * The normalization strategy for this field |
|
147 | - * |
|
148 | - * @var EE_Normalization_Strategy_Base |
|
149 | - */ |
|
150 | - private $_normalization_strategy; |
|
151 | - |
|
152 | - /** |
|
153 | - * Strategy for removing sensitive data after we're done with the form input |
|
154 | - * |
|
155 | - * @var EE_Sensitive_Data_Removal_Base |
|
156 | - */ |
|
157 | - protected $_sensitive_data_removal_strategy; |
|
158 | - |
|
159 | - /** |
|
160 | - * Whether this input has been disabled or not. |
|
161 | - * If it's disabled while rendering, an extra hidden input is added that indicates it has been knowingly disabled. |
|
162 | - * (Client-side code that wants to dynamically disable it must also add this hidden input). |
|
163 | - * When the form is submitted, if the input is disabled in the PHP form section, then input is ignored. |
|
164 | - * If the input is missing from the request data but the hidden input indicating the input is disabled, then the |
|
165 | - * input is again ignored. |
|
166 | - * |
|
167 | - * @var boolean |
|
168 | - */ |
|
169 | - protected $disabled = false; |
|
170 | - |
|
171 | - |
|
172 | - /** |
|
173 | - * @param array $input_args { |
|
174 | - * @type string $html_name the html name for the input |
|
175 | - * @type string $html_label_id the id attribute to give to the html label tag |
|
176 | - * @type string $html_label_class the class attribute to give to the html label tag |
|
177 | - * @type string $html_label_style the style attribute to give ot teh label tag |
|
178 | - * @type string $html_label_text the text to put in the label tag |
|
179 | - * @type string $html_label the full html label. If used, |
|
180 | - * all other html_label_* args are invalid |
|
181 | - * @type string $html_help_text text to put in help element |
|
182 | - * @type string $html_help_style style attribute to give to teh help element |
|
183 | - * @type string $html_help_class class attribute to give to the help element |
|
184 | - * @type string $default default value NORMALIZED (eg, if providing the default |
|
185 | - * for a Yes_No_Input, you should provide TRUE or FALSE, not |
|
186 | - * '1' or '0') |
|
187 | - * @type EE_Display_Strategy_Base $display strategy |
|
188 | - * @type EE_Normalization_Strategy_Base $normalization_strategy |
|
189 | - * @type EE_Validation_Strategy_Base[] $validation_strategies |
|
190 | - * @type boolean $ignore_input special argument which can be used to avoid adding any |
|
191 | - * validation strategies, and sets the normalization |
|
192 | - * strategy to the Null normalization. This is good when you |
|
193 | - * want the input to be totally ignored server-side (like |
|
194 | - * when using React.js form inputs) |
|
195 | - * } |
|
196 | - */ |
|
197 | - public function __construct($input_args = []) |
|
198 | - { |
|
199 | - $input_args = (array) apply_filters('FHEE__EE_Form_Input_Base___construct__input_args', $input_args, $this); |
|
200 | - // the following properties must be cast as arrays |
|
201 | - if (isset($input_args['validation_strategies'])) { |
|
202 | - foreach ((array) $input_args['validation_strategies'] as $validation_strategy) { |
|
203 | - if ($validation_strategy instanceof EE_Validation_Strategy_Base && empty($input_args['ignore_input'])) { |
|
204 | - $this->_validation_strategies[ get_class($validation_strategy) ] = $validation_strategy; |
|
205 | - } |
|
206 | - } |
|
207 | - unset($input_args['validation_strategies']); |
|
208 | - } |
|
209 | - if (isset($input_args['ignore_input'])) { |
|
210 | - $this->_validation_strategies = []; |
|
211 | - } |
|
212 | - // loop thru incoming options |
|
213 | - foreach ($input_args as $key => $value) { |
|
214 | - // add underscore to $key to match property names |
|
215 | - $_key = '_' . $key; |
|
216 | - if (property_exists($this, $_key)) { |
|
217 | - $this->{$_key} = $value; |
|
218 | - } |
|
219 | - } |
|
220 | - // ensure that "required" is set correctly |
|
221 | - $this->set_required( |
|
222 | - $this->_required, |
|
223 | - $input_args['required_validation_error_message'] ?? null |
|
224 | - ); |
|
225 | - // $this->_html_name_specified = isset( $input_args['html_name'] ) ? TRUE : FALSE; |
|
226 | - $this->_display_strategy->_construct_finalize($this); |
|
227 | - foreach ($this->_validation_strategies as $validation_strategy) { |
|
228 | - $validation_strategy->_construct_finalize($this); |
|
229 | - } |
|
230 | - if (isset($input_args['ignore_input'])) { |
|
231 | - $this->_normalization_strategy = new EE_Null_Normalization(); |
|
232 | - } |
|
233 | - if (! $this->_normalization_strategy) { |
|
234 | - $this->_normalization_strategy = new EE_Text_Normalization(); |
|
235 | - } |
|
236 | - $this->_normalization_strategy->_construct_finalize($this); |
|
237 | - // at least we can use the normalization strategy to populate the default |
|
238 | - if (isset($input_args['default'])) { |
|
239 | - $this->set_default($input_args['default']); |
|
240 | - unset($input_args['default']); |
|
241 | - } |
|
242 | - if (! $this->_sensitive_data_removal_strategy) { |
|
243 | - $this->_sensitive_data_removal_strategy = new EE_No_Sensitive_Data_Removal(); |
|
244 | - } |
|
245 | - $this->_sensitive_data_removal_strategy->_construct_finalize($this); |
|
246 | - parent::__construct($input_args); |
|
247 | - } |
|
248 | - |
|
249 | - |
|
250 | - /** |
|
251 | - * Sets the html_name to its default value, if none was specified in teh constructor. |
|
252 | - * Calculation involves using the name and the parent's html_name |
|
253 | - * |
|
254 | - * @throws EE_Error |
|
255 | - */ |
|
256 | - protected function _set_default_html_name_if_empty() |
|
257 | - { |
|
258 | - if (! $this->_html_name) { |
|
259 | - $this->_html_name = $this->name(); |
|
260 | - if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
|
261 | - $this->_html_name = $this->_parent_section->html_name_prefix() . "[{$this->name()}]"; |
|
262 | - } |
|
263 | - } |
|
264 | - } |
|
265 | - |
|
266 | - |
|
267 | - /** |
|
268 | - * @param $parent_form_section |
|
269 | - * @param $name |
|
270 | - * @throws EE_Error |
|
271 | - */ |
|
272 | - public function _construct_finalize($parent_form_section, $name) |
|
273 | - { |
|
274 | - parent::_construct_finalize($parent_form_section, $name); |
|
275 | - if ($this->_html_label === null && $this->_html_label_text === null) { |
|
276 | - $this->_html_label_text = ucwords(str_replace("_", " ", $name)); |
|
277 | - } |
|
278 | - do_action('AHEE__EE_Form_Input_Base___construct_finalize__end', $this, $parent_form_section, $name); |
|
279 | - } |
|
280 | - |
|
281 | - |
|
282 | - /** |
|
283 | - * Returns the strategy for displaying this form input. If none is set, throws an exception. |
|
284 | - * |
|
285 | - * @return EE_Display_Strategy_Base |
|
286 | - * @throws EE_Error |
|
287 | - */ |
|
288 | - protected function _get_display_strategy() |
|
289 | - { |
|
290 | - $this->ensure_construct_finalized_called(); |
|
291 | - if (! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) { |
|
292 | - throw new EE_Error( |
|
293 | - sprintf( |
|
294 | - esc_html__( |
|
295 | - "Cannot get display strategy for form input with name %s and id %s, because it has not been set in the constructor", |
|
296 | - "event_espresso" |
|
297 | - ), |
|
298 | - $this->html_name(), |
|
299 | - $this->html_id() |
|
300 | - ) |
|
301 | - ); |
|
302 | - } else { |
|
303 | - return $this->_display_strategy; |
|
304 | - } |
|
305 | - } |
|
306 | - |
|
307 | - |
|
308 | - /** |
|
309 | - * Sets the display strategy. |
|
310 | - * |
|
311 | - * @param EE_Display_Strategy_Base $strategy |
|
312 | - */ |
|
313 | - protected function _set_display_strategy(EE_Display_Strategy_Base $strategy) |
|
314 | - { |
|
315 | - $this->_display_strategy = $strategy; |
|
316 | - } |
|
317 | - |
|
318 | - |
|
319 | - /** |
|
320 | - * Sets the sanitization strategy |
|
321 | - * |
|
322 | - * @param EE_Normalization_Strategy_Base $strategy |
|
323 | - */ |
|
324 | - protected function _set_normalization_strategy(EE_Normalization_Strategy_Base $strategy) |
|
325 | - { |
|
326 | - $this->_normalization_strategy = $strategy; |
|
327 | - } |
|
328 | - |
|
329 | - |
|
330 | - /** |
|
331 | - * Gets sensitive_data_removal_strategy |
|
332 | - * |
|
333 | - * @return EE_Sensitive_Data_Removal_Base |
|
334 | - */ |
|
335 | - public function get_sensitive_data_removal_strategy() |
|
336 | - { |
|
337 | - return $this->_sensitive_data_removal_strategy; |
|
338 | - } |
|
339 | - |
|
340 | - |
|
341 | - /** |
|
342 | - * Sets sensitive_data_removal_strategy |
|
343 | - * |
|
344 | - * @param EE_Sensitive_Data_Removal_Base $sensitive_data_removal_strategy |
|
345 | - * @return void |
|
346 | - */ |
|
347 | - public function set_sensitive_data_removal_strategy($sensitive_data_removal_strategy) |
|
348 | - { |
|
349 | - $this->_sensitive_data_removal_strategy = $sensitive_data_removal_strategy; |
|
350 | - } |
|
351 | - |
|
352 | - |
|
353 | - /** |
|
354 | - * Gets the display strategy for this input |
|
355 | - * |
|
356 | - * @return EE_Display_Strategy_Base |
|
357 | - */ |
|
358 | - public function get_display_strategy() |
|
359 | - { |
|
360 | - return $this->_display_strategy; |
|
361 | - } |
|
362 | - |
|
363 | - |
|
364 | - /** |
|
365 | - * Overwrites the display strategy |
|
366 | - * |
|
367 | - * @param EE_Display_Strategy_Base $display_strategy |
|
368 | - */ |
|
369 | - public function set_display_strategy($display_strategy) |
|
370 | - { |
|
371 | - $this->_display_strategy = $display_strategy; |
|
372 | - $this->_display_strategy->_construct_finalize($this); |
|
373 | - } |
|
374 | - |
|
375 | - |
|
376 | - /** |
|
377 | - * Gets the normalization strategy set on this input |
|
378 | - * |
|
379 | - * @return EE_Normalization_Strategy_Base |
|
380 | - */ |
|
381 | - public function get_normalization_strategy() |
|
382 | - { |
|
383 | - return $this->_normalization_strategy; |
|
384 | - } |
|
385 | - |
|
386 | - |
|
387 | - /** |
|
388 | - * Overwrites the normalization strategy |
|
389 | - * |
|
390 | - * @param EE_Normalization_Strategy_Base $normalization_strategy |
|
391 | - */ |
|
392 | - public function set_normalization_strategy($normalization_strategy) |
|
393 | - { |
|
394 | - $this->_normalization_strategy = $normalization_strategy; |
|
395 | - $this->_normalization_strategy->_construct_finalize($this); |
|
396 | - } |
|
397 | - |
|
398 | - |
|
399 | - /** |
|
400 | - * Returns all teh validation strategies which apply to this field, numerically indexed |
|
401 | - * |
|
402 | - * @return EE_Validation_Strategy_Base[] |
|
403 | - */ |
|
404 | - public function get_validation_strategies() |
|
405 | - { |
|
406 | - return $this->_validation_strategies; |
|
407 | - } |
|
408 | - |
|
409 | - |
|
410 | - /** |
|
411 | - * Adds this strategy to the field so it will be used in both JS validation and server-side validation |
|
412 | - * |
|
413 | - * @param EE_Validation_Strategy_Base $validation_strategy |
|
414 | - * @return void |
|
415 | - */ |
|
416 | - protected function _add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
417 | - { |
|
418 | - $validation_strategy->_construct_finalize($this); |
|
419 | - $this->_validation_strategies[] = $validation_strategy; |
|
420 | - } |
|
421 | - |
|
422 | - |
|
423 | - /** |
|
424 | - * Adds a new validation strategy onto the form input |
|
425 | - * |
|
426 | - * @param EE_Validation_Strategy_Base $validation_strategy |
|
427 | - * @return void |
|
428 | - */ |
|
429 | - public function add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
430 | - { |
|
431 | - $this->_add_validation_strategy($validation_strategy); |
|
432 | - } |
|
433 | - |
|
434 | - |
|
435 | - /** |
|
436 | - * The classname of the validation strategy to remove |
|
437 | - * |
|
438 | - * @param string $validation_strategy_classname |
|
439 | - */ |
|
440 | - public function remove_validation_strategy($validation_strategy_classname) |
|
441 | - { |
|
442 | - foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
443 | - if ( |
|
444 | - $validation_strategy instanceof $validation_strategy_classname |
|
445 | - || is_subclass_of($validation_strategy, $validation_strategy_classname) |
|
446 | - ) { |
|
447 | - unset($this->_validation_strategies[ $key ]); |
|
448 | - } |
|
449 | - } |
|
450 | - } |
|
451 | - |
|
452 | - |
|
453 | - /** |
|
454 | - * returns true if input employs any of the validation strategy defined by the supplied array of classnames |
|
455 | - * |
|
456 | - * @param array $validation_strategy_classnames |
|
457 | - * @return bool |
|
458 | - */ |
|
459 | - public function has_validation_strategy($validation_strategy_classnames) |
|
460 | - { |
|
461 | - $validation_strategy_classnames = is_array($validation_strategy_classnames) |
|
462 | - ? $validation_strategy_classnames |
|
463 | - : [$validation_strategy_classnames]; |
|
464 | - foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
465 | - if (in_array($key, $validation_strategy_classnames)) { |
|
466 | - return true; |
|
467 | - } |
|
468 | - } |
|
469 | - return false; |
|
470 | - } |
|
471 | - |
|
472 | - |
|
473 | - /** |
|
474 | - * Gets the HTML |
|
475 | - * |
|
476 | - * @return string |
|
477 | - */ |
|
478 | - public function get_html() |
|
479 | - { |
|
480 | - return $this->_parent_section->get_html_for_input($this); |
|
481 | - } |
|
482 | - |
|
483 | - |
|
484 | - /** |
|
485 | - * Gets the HTML for the input itself (no label or errors) according to the |
|
486 | - * input's display strategy |
|
487 | - * Makes sure the JS and CSS are enqueued for it |
|
488 | - * |
|
489 | - * @return string |
|
490 | - * @throws EE_Error |
|
491 | - */ |
|
492 | - public function get_html_for_input() |
|
493 | - { |
|
494 | - return $this->_form_html_filter |
|
495 | - ? $this->_form_html_filter->filterHtml( |
|
496 | - $this->_get_display_strategy()->display(), |
|
497 | - $this |
|
498 | - ) |
|
499 | - : $this->_get_display_strategy()->display(); |
|
500 | - } |
|
501 | - |
|
502 | - |
|
503 | - /** |
|
504 | - * @return string |
|
505 | - */ |
|
506 | - public function html_other_attributes() |
|
507 | - { |
|
508 | - EE_Error::doing_it_wrong( |
|
509 | - __METHOD__, |
|
510 | - sprintf( |
|
511 | - esc_html__( |
|
512 | - 'This method is no longer in use. You should replace it by %s', |
|
513 | - 'event_espresso' |
|
514 | - ), |
|
515 | - 'EE_Form_Section_Base::other_html_attributes()' |
|
516 | - ), |
|
517 | - '4.10.2.p' |
|
518 | - ); |
|
519 | - |
|
520 | - return $this->other_html_attributes(); |
|
521 | - } |
|
522 | - |
|
523 | - |
|
524 | - /** |
|
525 | - * @param string $html_other_attributes |
|
526 | - */ |
|
527 | - public function set_html_other_attributes($html_other_attributes) |
|
528 | - { |
|
529 | - EE_Error::doing_it_wrong( |
|
530 | - __METHOD__, |
|
531 | - sprintf( |
|
532 | - esc_html__( |
|
533 | - 'This method is no longer in use. You should replace it by %s', |
|
534 | - 'event_espresso' |
|
535 | - ), |
|
536 | - 'EE_Form_Section_Base::set_other_html_attributes()' |
|
537 | - ), |
|
538 | - '4.10.2.p' |
|
539 | - ); |
|
540 | - |
|
541 | - $this->set_other_html_attributes($html_other_attributes); |
|
542 | - } |
|
543 | - |
|
544 | - |
|
545 | - /** |
|
546 | - * Gets the HTML for displaying the label for this form input |
|
547 | - * according to the form section's layout strategy |
|
548 | - * |
|
549 | - * @return string |
|
550 | - */ |
|
551 | - public function get_html_for_label() |
|
552 | - { |
|
553 | - return $this->_parent_section->get_layout_strategy()->display_label($this); |
|
554 | - } |
|
555 | - |
|
556 | - |
|
557 | - /** |
|
558 | - * Gets the HTML for displaying the errors section for this form input |
|
559 | - * according to the form section's layout strategy |
|
560 | - * |
|
561 | - * @return string |
|
562 | - */ |
|
563 | - public function get_html_for_errors() |
|
564 | - { |
|
565 | - return $this->_parent_section->get_layout_strategy()->display_errors($this); |
|
566 | - } |
|
567 | - |
|
568 | - |
|
569 | - /** |
|
570 | - * Gets the HTML for displaying the help text for this form input |
|
571 | - * according to the form section's layout strategy |
|
572 | - * |
|
573 | - * @return string |
|
574 | - */ |
|
575 | - public function get_html_for_help() |
|
576 | - { |
|
577 | - return $this->_parent_section->get_layout_strategy()->display_help_text($this); |
|
578 | - } |
|
579 | - |
|
580 | - |
|
581 | - /** |
|
582 | - * Validates the input's sanitized value (assumes _sanitize() has already been called) |
|
583 | - * and returns whether or not the form input's submitted value is value |
|
584 | - * |
|
585 | - * @return boolean |
|
586 | - */ |
|
587 | - protected function _validate() |
|
588 | - { |
|
589 | - if ($this->isDisabled()) { |
|
590 | - return true; |
|
591 | - } |
|
592 | - foreach ($this->_validation_strategies as $validation_strategy) { |
|
593 | - if ($validation_strategy instanceof EE_Validation_Strategy_Base) { |
|
594 | - try { |
|
595 | - $validation_strategy->validate($this->normalized_value()); |
|
596 | - } catch (EE_Validation_Error $e) { |
|
597 | - $this->add_validation_error($e); |
|
598 | - } |
|
599 | - } |
|
600 | - } |
|
601 | - if ($this->get_validation_errors()) { |
|
602 | - return false; |
|
603 | - } else { |
|
604 | - return true; |
|
605 | - } |
|
606 | - } |
|
607 | - |
|
608 | - |
|
609 | - /** |
|
610 | - * Performs basic sanitization on this value. But what sanitization can be performed anyways? |
|
611 | - * This value MIGHT be allowed to have tags, so we can't really remove them. |
|
612 | - * |
|
613 | - * @param string $value |
|
614 | - * @return null|string |
|
615 | - */ |
|
616 | - protected function _sanitize($value) |
|
617 | - { |
|
618 | - return $value !== null |
|
619 | - ? stripslashes(html_entity_decode(trim((string) $value))) |
|
620 | - : null; |
|
621 | - } |
|
622 | - |
|
623 | - |
|
624 | - /** |
|
625 | - * Picks out the form value that relates to this form input, |
|
626 | - * and stores it as the sanitized value on the form input, and sets the normalized value. |
|
627 | - * Returns whether or not any validation errors occurred |
|
628 | - * |
|
629 | - * @param array $req_data |
|
630 | - * @return boolean whether or not there was an error |
|
631 | - * @throws EE_Error |
|
632 | - */ |
|
633 | - protected function _normalize($req_data) |
|
634 | - { |
|
635 | - // any existing validation errors don't apply so clear them |
|
636 | - $this->_validation_errors = []; |
|
637 | - // if the input is disabled, ignore whatever input was sent in |
|
638 | - if ($this->isDisabled()) { |
|
639 | - $this->_set_raw_value(null); |
|
640 | - $this->_set_normalized_value($this->get_default()); |
|
641 | - return false; |
|
642 | - } |
|
643 | - try { |
|
644 | - $raw_input = $this->find_form_data_for_this_section($req_data); |
|
645 | - // super simple sanitization for now |
|
646 | - if (is_array($raw_input)) { |
|
647 | - $raw_value = []; |
|
648 | - foreach ($raw_input as $key => $value) { |
|
649 | - $raw_value[ $key ] = $this->_sanitize($value); |
|
650 | - } |
|
651 | - $this->_set_raw_value($raw_value); |
|
652 | - } else { |
|
653 | - $this->_set_raw_value($this->_sanitize($raw_input)); |
|
654 | - } |
|
655 | - // we want to mostly leave the input alone in case we need to re-display it to the user |
|
656 | - $this->_set_normalized_value($this->_normalization_strategy->normalize($this->raw_value())); |
|
657 | - return false; |
|
658 | - } catch (EE_Validation_Error $e) { |
|
659 | - $this->add_validation_error($e); |
|
660 | - return true; |
|
661 | - } |
|
662 | - } |
|
663 | - |
|
664 | - |
|
665 | - /** |
|
666 | - * @return string |
|
667 | - * @throws EE_Error |
|
668 | - */ |
|
669 | - public function html_name() |
|
670 | - { |
|
671 | - $this->_set_default_html_name_if_empty(); |
|
672 | - return $this->_html_name ?? ''; |
|
673 | - } |
|
674 | - |
|
675 | - |
|
676 | - /** |
|
677 | - * @return string |
|
678 | - * @throws EE_Error |
|
679 | - */ |
|
680 | - public function html_label_id() |
|
681 | - { |
|
682 | - return ! empty($this->_html_label_id) |
|
683 | - ? $this->_html_label_id |
|
684 | - : $this->html_id() . '-lbl'; |
|
685 | - } |
|
686 | - |
|
687 | - |
|
688 | - /** |
|
689 | - * @return string |
|
690 | - */ |
|
691 | - public function html_label_class() |
|
692 | - { |
|
693 | - return $this->_html_label_class ?? ''; |
|
694 | - } |
|
695 | - |
|
696 | - |
|
697 | - /** |
|
698 | - * @param string $html_class |
|
699 | - */ |
|
700 | - public function add_html_label_class(string $html_class) |
|
701 | - { |
|
702 | - $this->_html_label_class .= ' ' . trim($html_class); |
|
703 | - } |
|
704 | - |
|
705 | - |
|
706 | - /** |
|
707 | - * @return string |
|
708 | - */ |
|
709 | - public function html_label_style() |
|
710 | - { |
|
711 | - return $this->_html_label_style ?? ''; |
|
712 | - } |
|
713 | - |
|
714 | - |
|
715 | - /** |
|
716 | - * @return string |
|
717 | - */ |
|
718 | - public function html_label_text() |
|
719 | - { |
|
720 | - return $this->_html_label_text ?? ''; |
|
721 | - } |
|
722 | - |
|
723 | - |
|
724 | - /** |
|
725 | - * @return string |
|
726 | - */ |
|
727 | - public function html_help_text() |
|
728 | - { |
|
729 | - return $this->_html_help_text ?? ''; |
|
730 | - } |
|
731 | - |
|
732 | - |
|
733 | - /** |
|
734 | - * @return string |
|
735 | - */ |
|
736 | - public function html_help_class() |
|
737 | - { |
|
738 | - return $this->_html_help_class ?? ''; |
|
739 | - } |
|
740 | - |
|
741 | - |
|
742 | - /** |
|
743 | - * @return string |
|
744 | - */ |
|
745 | - public function html_help_style() |
|
746 | - { |
|
747 | - return $this->_html_style ?? ''; |
|
748 | - } |
|
749 | - |
|
750 | - |
|
751 | - /** |
|
752 | - * returns the raw, UNSAFE, input, almost exactly as the user submitted it. |
|
753 | - * Please note that almost all client code should instead use the normalized_value; |
|
754 | - * or possibly raw_value_in_form (which prepares the string for displaying in an HTML attribute on a tag, |
|
755 | - * mostly by escaping quotes) |
|
756 | - * Note, we do not store the exact original value sent in the user's request because |
|
757 | - * it may have malicious content, and we MIGHT want to store the form input in a transient or something... |
|
758 | - * in which case, we would have stored the malicious content to our database. |
|
759 | - * |
|
760 | - * @return mixed |
|
761 | - */ |
|
762 | - public function raw_value() |
|
763 | - { |
|
764 | - return $this->_raw_value; |
|
765 | - } |
|
766 | - |
|
767 | - |
|
768 | - /** |
|
769 | - * Returns a string safe to usage in form inputs when displaying, because |
|
770 | - * it escapes all html entities |
|
771 | - * |
|
772 | - * @return string |
|
773 | - */ |
|
774 | - public function raw_value_in_form(): string |
|
775 | - { |
|
776 | - return htmlentities((string) $this->raw_value(), ENT_QUOTES, 'UTF-8'); |
|
777 | - } |
|
778 | - |
|
779 | - |
|
780 | - /** |
|
781 | - * returns the value after it's been sanitized, and then converted into it's proper type |
|
782 | - * in PHP. Eg, a string, an int, an array, |
|
783 | - * |
|
784 | - * @return mixed |
|
785 | - */ |
|
786 | - public function normalized_value() |
|
787 | - { |
|
788 | - return $this->_normalized_value; |
|
789 | - } |
|
790 | - |
|
791 | - |
|
792 | - /** |
|
793 | - * Returns the normalized value is a presentable way. By default this is just |
|
794 | - * the normalized value by itself, but it can be overridden for when that's not |
|
795 | - * the best thing to display |
|
796 | - * |
|
797 | - * @return mixed |
|
798 | - */ |
|
799 | - public function pretty_value() |
|
800 | - { |
|
801 | - return $this->_normalized_value; |
|
802 | - } |
|
803 | - |
|
804 | - |
|
805 | - /** |
|
806 | - * When generating the JS for the jquery validation rules like<br> |
|
807 | - * <code>$( "#myform" ).validate({ |
|
808 | - * rules: { |
|
809 | - * password: "required", |
|
810 | - * password_again: { |
|
811 | - * equalTo: "#password" |
|
812 | - * } |
|
813 | - * } |
|
814 | - * });</code> |
|
815 | - * if this field had the name 'password_again', it should return |
|
816 | - * <br><code>password_again: { |
|
817 | - * equalTo: "#password" |
|
818 | - * }</code> |
|
819 | - * |
|
820 | - * @return array |
|
821 | - */ |
|
822 | - public function get_jquery_validation_rules(): array |
|
823 | - { |
|
824 | - $jquery_validation_js = []; |
|
825 | - $jquery_validation_rules = []; |
|
826 | - foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
827 | - $jquery_validation_rules = array_replace_recursive( |
|
828 | - $jquery_validation_rules, |
|
829 | - $validation_strategy->get_jquery_validation_rule_array() |
|
830 | - ); |
|
831 | - } |
|
832 | - if (! empty($jquery_validation_rules)) { |
|
833 | - foreach ($this->get_display_strategy()->get_html_input_ids(true) as $html_id_with_pound_sign) { |
|
834 | - $jquery_validation_js[ $html_id_with_pound_sign ] = $jquery_validation_rules; |
|
835 | - } |
|
836 | - } |
|
837 | - return $jquery_validation_js; |
|
838 | - } |
|
839 | - |
|
840 | - |
|
841 | - /** |
|
842 | - * Sets the input's default value for use in displaying in the form. Note: value should be |
|
843 | - * normalized (Eg, if providing a default of ra Yes_NO_Input you would provide TRUE or FALSE, not '1' or '0') |
|
844 | - * |
|
845 | - * @param mixed $value |
|
846 | - * @return void |
|
847 | - */ |
|
848 | - public function set_default($value) |
|
849 | - { |
|
850 | - $this->_default = $value; |
|
851 | - $this->_set_normalized_value($value); |
|
852 | - $this->_set_raw_value($value); |
|
853 | - } |
|
854 | - |
|
855 | - |
|
856 | - /** |
|
857 | - * Sets the normalized value on this input |
|
858 | - * |
|
859 | - * @param mixed $value |
|
860 | - */ |
|
861 | - protected function _set_normalized_value($value) |
|
862 | - { |
|
863 | - $this->_normalized_value = $value; |
|
864 | - } |
|
865 | - |
|
866 | - |
|
867 | - /** |
|
868 | - * Sets the raw value on this input (ie, exactly as the user submitted it) |
|
869 | - * |
|
870 | - * @param mixed $value |
|
871 | - */ |
|
872 | - protected function _set_raw_value($value) |
|
873 | - { |
|
874 | - $this->_raw_value = $this->_normalization_strategy->unnormalize($value); |
|
875 | - } |
|
876 | - |
|
877 | - |
|
878 | - /** |
|
879 | - * Sets the HTML label text after it has already been defined |
|
880 | - * |
|
881 | - * @param string $label |
|
882 | - * @return void |
|
883 | - */ |
|
884 | - public function set_html_label_text($label) |
|
885 | - { |
|
886 | - $this->_html_label_text = $label; |
|
887 | - } |
|
888 | - |
|
889 | - |
|
890 | - /** |
|
891 | - * Sets whether or not this field is required, and adjusts the validation strategy. |
|
892 | - * If you want to use the EE_Conditionally_Required_Validation_Strategy, |
|
893 | - * please add it as a validation strategy using add_validation_strategy as normal |
|
894 | - * |
|
895 | - * @param boolean $required boolean |
|
896 | - * @param null $required_text |
|
897 | - */ |
|
898 | - public function set_required($required = true, $required_text = null) |
|
899 | - { |
|
900 | - $required = filter_var($required, FILTER_VALIDATE_BOOLEAN); |
|
901 | - // whether $required is a string or a boolean, we want to add a required validation strategy |
|
902 | - if ($required) { |
|
903 | - $this->_add_validation_strategy(new EE_Required_Validation_Strategy($required_text)); |
|
904 | - } else { |
|
905 | - $this->remove_validation_strategy('EE_Required_Validation_Strategy'); |
|
906 | - } |
|
907 | - $this->_required = $required; |
|
908 | - } |
|
909 | - |
|
910 | - |
|
911 | - /** |
|
912 | - * Returns whether or not this field is required |
|
913 | - * |
|
914 | - * @return boolean |
|
915 | - */ |
|
916 | - public function required() |
|
917 | - { |
|
918 | - return $this->_required; |
|
919 | - } |
|
920 | - |
|
921 | - |
|
922 | - /** |
|
923 | - * @param string $required_css_class |
|
924 | - */ |
|
925 | - public function set_required_css_class($required_css_class) |
|
926 | - { |
|
927 | - $this->_required_css_class = $required_css_class; |
|
928 | - } |
|
929 | - |
|
930 | - |
|
931 | - /** |
|
932 | - * @return string |
|
933 | - */ |
|
934 | - public function required_css_class() |
|
935 | - { |
|
936 | - return $this->_required_css_class; |
|
937 | - } |
|
938 | - |
|
939 | - |
|
940 | - /** |
|
941 | - * @param bool $add_required |
|
942 | - * @return string |
|
943 | - */ |
|
944 | - public function html_class($add_required = false) |
|
945 | - { |
|
946 | - return $add_required && $this->required() |
|
947 | - ? $this->required_css_class() . ' ' . $this->_html_class |
|
948 | - : $this->_html_class; |
|
949 | - } |
|
950 | - |
|
951 | - |
|
952 | - /** |
|
953 | - * Sets the help text, in case |
|
954 | - * |
|
955 | - * @param string $text |
|
956 | - */ |
|
957 | - public function set_html_help_text($text) |
|
958 | - { |
|
959 | - $this->_html_help_text = $text; |
|
960 | - } |
|
961 | - |
|
962 | - |
|
963 | - /** |
|
964 | - * Uses the sensitive data removal strategy to remove the sensitive data from this |
|
965 | - * input. If there is any kind of sensitive data removal on this input, we clear |
|
966 | - * out the raw value completely |
|
967 | - * |
|
968 | - * @return void |
|
969 | - */ |
|
970 | - public function clean_sensitive_data() |
|
971 | - { |
|
972 | - // if we do ANY kind of sensitive data removal on this, then just clear out the raw value |
|
973 | - // if we need more logic than this we'll make a strategy for it |
|
974 | - if ( |
|
975 | - $this->_sensitive_data_removal_strategy |
|
976 | - && ! $this->_sensitive_data_removal_strategy instanceof EE_No_Sensitive_Data_Removal |
|
977 | - ) { |
|
978 | - $this->_set_raw_value(null); |
|
979 | - } |
|
980 | - // and clean the normalized value according to the appropriate strategy |
|
981 | - $this->_set_normalized_value( |
|
982 | - $this->get_sensitive_data_removal_strategy()->remove_sensitive_data( |
|
983 | - $this->_normalized_value |
|
984 | - ) |
|
985 | - ); |
|
986 | - } |
|
987 | - |
|
988 | - |
|
989 | - /** |
|
990 | - * @param bool $primary |
|
991 | - * @param string $button_size |
|
992 | - * @param string $other_attributes |
|
993 | - */ |
|
994 | - public function set_button_css_attributes($primary = true, $button_size = '', $other_attributes = '') |
|
995 | - { |
|
996 | - $button_css_attributes = 'button'; |
|
997 | - $button_css_attributes .= $primary === true |
|
998 | - ? ' button--primary' |
|
999 | - : ' button--secondary'; |
|
1000 | - switch ($button_size) { |
|
1001 | - case 'xs': |
|
1002 | - case 'extra-small': |
|
1003 | - $button_css_attributes .= ' button-xs'; |
|
1004 | - break; |
|
1005 | - case 'sm': |
|
1006 | - case 'small': |
|
1007 | - $button_css_attributes .= ' button-sm'; |
|
1008 | - break; |
|
1009 | - case 'lg': |
|
1010 | - case 'large': |
|
1011 | - $button_css_attributes .= ' button-lg'; |
|
1012 | - break; |
|
1013 | - case 'block': |
|
1014 | - $button_css_attributes .= ' button-block'; |
|
1015 | - break; |
|
1016 | - case 'md': |
|
1017 | - case 'medium': |
|
1018 | - default: |
|
1019 | - $button_css_attributes .= ''; |
|
1020 | - } |
|
1021 | - $this->_button_css_attributes .= ! empty($other_attributes) |
|
1022 | - ? $button_css_attributes . ' ' . $other_attributes |
|
1023 | - : $button_css_attributes; |
|
1024 | - } |
|
1025 | - |
|
1026 | - |
|
1027 | - /** |
|
1028 | - * @return string |
|
1029 | - */ |
|
1030 | - public function button_css_attributes() |
|
1031 | - { |
|
1032 | - if (empty($this->_button_css_attributes)) { |
|
1033 | - $this->set_button_css_attributes(); |
|
1034 | - } |
|
1035 | - return $this->_button_css_attributes; |
|
1036 | - } |
|
1037 | - |
|
1038 | - |
|
1039 | - /** |
|
1040 | - * find_form_data_for_this_section |
|
1041 | - * using this section's name and its parents, finds the value of the form data that corresponds to it. |
|
1042 | - * For example, if this form section's HTML name is my_form[subform][form_input_1], |
|
1043 | - * then it's value should be in request at request['my_form']['subform']['form_input_1']. |
|
1044 | - * (If that doesn't exist, we also check for this subsection's name |
|
1045 | - * at the TOP LEVEL of the request data. Eg request['form_input_1'].) |
|
1046 | - * This function finds its value in the form. |
|
1047 | - * |
|
1048 | - * @param array $req_data |
|
1049 | - * @return mixed whatever the raw value of this form section is in the request data |
|
1050 | - * @throws EE_Error |
|
1051 | - */ |
|
1052 | - public function find_form_data_for_this_section($req_data) |
|
1053 | - { |
|
1054 | - $name_parts = $this->getInputNameParts(); |
|
1055 | - // now get the value for the input |
|
1056 | - $value = $this->findRequestForSectionUsingNameParts($name_parts, $req_data); |
|
1057 | - // check if this thing's name is at the TOP level of the request data |
|
1058 | - if ($value === null && isset($req_data[ $this->name() ])) { |
|
1059 | - $value = $req_data[ $this->name() ]; |
|
1060 | - } |
|
1061 | - return $value; |
|
1062 | - } |
|
1063 | - |
|
1064 | - |
|
1065 | - /** |
|
1066 | - * If this input's name is something like "foo[bar][baz]" |
|
1067 | - * returns an array like `array('foo','bar',baz')` |
|
1068 | - * |
|
1069 | - * @return array |
|
1070 | - * @throws EE_Error |
|
1071 | - */ |
|
1072 | - protected function getInputNameParts() |
|
1073 | - { |
|
1074 | - // break up the html name by "[]" |
|
1075 | - if (strpos($this->html_name(), '[') !== false) { |
|
1076 | - $before_any_brackets = substr($this->html_name(), 0, strpos($this->html_name(), '[')); |
|
1077 | - } else { |
|
1078 | - $before_any_brackets = $this->html_name(); |
|
1079 | - } |
|
1080 | - // grab all of the segments |
|
1081 | - preg_match_all('~\[([^]]*)\]~', $this->html_name(), $matches); |
|
1082 | - if (isset($matches[1]) && is_array($matches[1])) { |
|
1083 | - $name_parts = $matches[1]; |
|
1084 | - array_unshift($name_parts, $before_any_brackets); |
|
1085 | - } else { |
|
1086 | - $name_parts = [$before_any_brackets]; |
|
1087 | - } |
|
1088 | - return $name_parts; |
|
1089 | - } |
|
1090 | - |
|
1091 | - |
|
1092 | - /** |
|
1093 | - * @param array $html_name_parts |
|
1094 | - * @param array $req_data |
|
1095 | - * @return array | NULL |
|
1096 | - */ |
|
1097 | - public function findRequestForSectionUsingNameParts($html_name_parts, $req_data) |
|
1098 | - { |
|
1099 | - $first_part_to_consider = array_shift($html_name_parts); |
|
1100 | - if (isset($req_data[ $first_part_to_consider ])) { |
|
1101 | - if (empty($html_name_parts)) { |
|
1102 | - return $req_data[ $first_part_to_consider ]; |
|
1103 | - } else { |
|
1104 | - return $this->findRequestForSectionUsingNameParts( |
|
1105 | - $html_name_parts, |
|
1106 | - $req_data[ $first_part_to_consider ] |
|
1107 | - ); |
|
1108 | - } |
|
1109 | - } else { |
|
1110 | - return null; |
|
1111 | - } |
|
1112 | - } |
|
1113 | - |
|
1114 | - |
|
1115 | - /** |
|
1116 | - * Checks if this form input's data is in the request data |
|
1117 | - * |
|
1118 | - * @param array $req_data |
|
1119 | - * @return boolean |
|
1120 | - * @throws EE_Error |
|
1121 | - */ |
|
1122 | - public function form_data_present_in($req_data = null) |
|
1123 | - { |
|
1124 | - if ($req_data === null) { |
|
1125 | - /** @var RequestInterface $request */ |
|
1126 | - $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
1127 | - $req_data = $request->postParams(); |
|
1128 | - } |
|
1129 | - $checked_value = $this->find_form_data_for_this_section($req_data); |
|
1130 | - if ($checked_value !== null) { |
|
1131 | - return true; |
|
1132 | - } else { |
|
1133 | - return false; |
|
1134 | - } |
|
1135 | - } |
|
1136 | - |
|
1137 | - |
|
1138 | - /** |
|
1139 | - * Overrides parent to add js data from validation and display strategies |
|
1140 | - * |
|
1141 | - * @param array $form_other_js_data |
|
1142 | - * @return array |
|
1143 | - */ |
|
1144 | - public function get_other_js_data($form_other_js_data = []) |
|
1145 | - { |
|
1146 | - return $this->get_other_js_data_from_strategies($form_other_js_data); |
|
1147 | - } |
|
1148 | - |
|
1149 | - |
|
1150 | - /** |
|
1151 | - * Gets other JS data for localization from this input's strategies, like |
|
1152 | - * the validation strategies and the display strategy |
|
1153 | - * |
|
1154 | - * @param array $form_other_js_data |
|
1155 | - * @return array |
|
1156 | - */ |
|
1157 | - public function get_other_js_data_from_strategies($form_other_js_data = []) |
|
1158 | - { |
|
1159 | - $form_other_js_data = $this->get_display_strategy()->get_other_js_data($form_other_js_data); |
|
1160 | - foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
1161 | - $form_other_js_data = $validation_strategy->get_other_js_data($form_other_js_data); |
|
1162 | - } |
|
1163 | - return $form_other_js_data; |
|
1164 | - } |
|
1165 | - |
|
1166 | - |
|
1167 | - /** |
|
1168 | - * Override parent because we want to give our strategies an opportunity to enqueue some js and css |
|
1169 | - * |
|
1170 | - * @return void |
|
1171 | - */ |
|
1172 | - public function enqueue_js() |
|
1173 | - { |
|
1174 | - // ask our display strategy and validation strategies if they have js to enqueue |
|
1175 | - $this->enqueue_js_from_strategies(); |
|
1176 | - } |
|
1177 | - |
|
1178 | - |
|
1179 | - /** |
|
1180 | - * Tells strategies when its ok to enqueue their js and css |
|
1181 | - * |
|
1182 | - * @return void |
|
1183 | - */ |
|
1184 | - public function enqueue_js_from_strategies() |
|
1185 | - { |
|
1186 | - $this->get_display_strategy()->enqueue_js(); |
|
1187 | - foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
1188 | - $validation_strategy->enqueue_js(); |
|
1189 | - } |
|
1190 | - } |
|
1191 | - |
|
1192 | - |
|
1193 | - /** |
|
1194 | - * Gets the default value set on the input (not the current value, which may have been |
|
1195 | - * changed because of a form submission). If no default was set, this us null. |
|
1196 | - * |
|
1197 | - * @return mixed |
|
1198 | - */ |
|
1199 | - public function get_default() |
|
1200 | - { |
|
1201 | - return $this->_default; |
|
1202 | - } |
|
1203 | - |
|
1204 | - |
|
1205 | - /** |
|
1206 | - * Makes this input disabled. That means it will have the HTML attribute 'disabled="disabled"', |
|
1207 | - * and server-side if any input was received it will be ignored |
|
1208 | - */ |
|
1209 | - public function disable($disable = true) |
|
1210 | - { |
|
1211 | - $disabled_attribute = ' disabled="disabled"'; |
|
1212 | - $this->disabled = filter_var($disable, FILTER_VALIDATE_BOOLEAN); |
|
1213 | - if ($this->disabled) { |
|
1214 | - if (strpos($this->_other_html_attributes, $disabled_attribute) === false) { |
|
1215 | - $this->_other_html_attributes .= $disabled_attribute; |
|
1216 | - } |
|
1217 | - $this->_set_normalized_value($this->get_default()); |
|
1218 | - } else { |
|
1219 | - $this->_other_html_attributes = str_replace($disabled_attribute, '', $this->_other_html_attributes); |
|
1220 | - } |
|
1221 | - } |
|
1222 | - |
|
1223 | - |
|
1224 | - /** |
|
1225 | - * Returns whether or not this input is currently disabled. |
|
1226 | - * |
|
1227 | - * @return bool |
|
1228 | - */ |
|
1229 | - public function isDisabled() |
|
1230 | - { |
|
1231 | - return $this->disabled; |
|
1232 | - } |
|
17 | + /** |
|
18 | + * the input's name attribute |
|
19 | + * |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + protected $_html_name; |
|
23 | + |
|
24 | + /** |
|
25 | + * id for the html label tag |
|
26 | + * |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + protected $_html_label_id; |
|
30 | + |
|
31 | + /** |
|
32 | + * class for teh html label tag |
|
33 | + * |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + protected $_html_label_class; |
|
37 | + |
|
38 | + /** |
|
39 | + * style for teh html label tag |
|
40 | + * |
|
41 | + * @var string |
|
42 | + */ |
|
43 | + protected $_html_label_style; |
|
44 | + |
|
45 | + /** |
|
46 | + * text to be placed in the html label |
|
47 | + * |
|
48 | + * @var string |
|
49 | + */ |
|
50 | + protected $_html_label_text; |
|
51 | + |
|
52 | + /** |
|
53 | + * the full html label. If used, all other html_label_* properties are invalid |
|
54 | + * |
|
55 | + * @var string |
|
56 | + */ |
|
57 | + protected $_html_label; |
|
58 | + |
|
59 | + /** |
|
60 | + * HTML to use for help text (normally placed below form input), in a span which normally |
|
61 | + * has a class of 'description' |
|
62 | + * |
|
63 | + * @var string |
|
64 | + */ |
|
65 | + protected $_html_help_text; |
|
66 | + |
|
67 | + /** |
|
68 | + * CSS classes for displaying the help span |
|
69 | + * |
|
70 | + * @var string |
|
71 | + */ |
|
72 | + protected $_html_help_class = 'description'; |
|
73 | + |
|
74 | + /** |
|
75 | + * CSS to put in the style attribute on the help span |
|
76 | + * |
|
77 | + * @var string |
|
78 | + */ |
|
79 | + protected $_html_help_style; |
|
80 | + |
|
81 | + /** |
|
82 | + * Stores whether or not this input's response is required. |
|
83 | + * Because certain styling elements may also want to know that this |
|
84 | + * input is required etc. |
|
85 | + * |
|
86 | + * @var boolean |
|
87 | + */ |
|
88 | + protected $_required; |
|
89 | + |
|
90 | + /** |
|
91 | + * css class added to required inputs |
|
92 | + * |
|
93 | + * @var string |
|
94 | + */ |
|
95 | + protected $_required_css_class = 'ee-required'; |
|
96 | + |
|
97 | + /** |
|
98 | + * css styles applied to button type inputs |
|
99 | + * |
|
100 | + * @var string |
|
101 | + */ |
|
102 | + protected $_button_css_attributes; |
|
103 | + |
|
104 | + /** |
|
105 | + * The raw post data submitted for this |
|
106 | + * Generally unsafe for usage in client code |
|
107 | + * |
|
108 | + * @var mixed string or array |
|
109 | + */ |
|
110 | + protected $_raw_value; |
|
111 | + |
|
112 | + /** |
|
113 | + * Value normalized according to the input's normalization strategy. |
|
114 | + * The normalization strategy dictates whether this is a string, int, float, |
|
115 | + * boolean, or array of any of those. |
|
116 | + * |
|
117 | + * @var mixed |
|
118 | + */ |
|
119 | + protected $_normalized_value; |
|
120 | + |
|
121 | + |
|
122 | + /** |
|
123 | + * Normalized default value either initially set on the input, or provided by calling |
|
124 | + * set_default(). |
|
125 | + * |
|
126 | + * @var mixed |
|
127 | + */ |
|
128 | + protected $_default; |
|
129 | + |
|
130 | + /** |
|
131 | + * Strategy used for displaying this field. |
|
132 | + * Child classes must use _get_display_strategy to access it. |
|
133 | + * |
|
134 | + * @var EE_Display_Strategy_Base |
|
135 | + */ |
|
136 | + private $_display_strategy; |
|
137 | + |
|
138 | + /** |
|
139 | + * Gets all the validation strategies used on this field |
|
140 | + * |
|
141 | + * @var EE_Validation_Strategy_Base[] |
|
142 | + */ |
|
143 | + private $_validation_strategies = []; |
|
144 | + |
|
145 | + /** |
|
146 | + * The normalization strategy for this field |
|
147 | + * |
|
148 | + * @var EE_Normalization_Strategy_Base |
|
149 | + */ |
|
150 | + private $_normalization_strategy; |
|
151 | + |
|
152 | + /** |
|
153 | + * Strategy for removing sensitive data after we're done with the form input |
|
154 | + * |
|
155 | + * @var EE_Sensitive_Data_Removal_Base |
|
156 | + */ |
|
157 | + protected $_sensitive_data_removal_strategy; |
|
158 | + |
|
159 | + /** |
|
160 | + * Whether this input has been disabled or not. |
|
161 | + * If it's disabled while rendering, an extra hidden input is added that indicates it has been knowingly disabled. |
|
162 | + * (Client-side code that wants to dynamically disable it must also add this hidden input). |
|
163 | + * When the form is submitted, if the input is disabled in the PHP form section, then input is ignored. |
|
164 | + * If the input is missing from the request data but the hidden input indicating the input is disabled, then the |
|
165 | + * input is again ignored. |
|
166 | + * |
|
167 | + * @var boolean |
|
168 | + */ |
|
169 | + protected $disabled = false; |
|
170 | + |
|
171 | + |
|
172 | + /** |
|
173 | + * @param array $input_args { |
|
174 | + * @type string $html_name the html name for the input |
|
175 | + * @type string $html_label_id the id attribute to give to the html label tag |
|
176 | + * @type string $html_label_class the class attribute to give to the html label tag |
|
177 | + * @type string $html_label_style the style attribute to give ot teh label tag |
|
178 | + * @type string $html_label_text the text to put in the label tag |
|
179 | + * @type string $html_label the full html label. If used, |
|
180 | + * all other html_label_* args are invalid |
|
181 | + * @type string $html_help_text text to put in help element |
|
182 | + * @type string $html_help_style style attribute to give to teh help element |
|
183 | + * @type string $html_help_class class attribute to give to the help element |
|
184 | + * @type string $default default value NORMALIZED (eg, if providing the default |
|
185 | + * for a Yes_No_Input, you should provide TRUE or FALSE, not |
|
186 | + * '1' or '0') |
|
187 | + * @type EE_Display_Strategy_Base $display strategy |
|
188 | + * @type EE_Normalization_Strategy_Base $normalization_strategy |
|
189 | + * @type EE_Validation_Strategy_Base[] $validation_strategies |
|
190 | + * @type boolean $ignore_input special argument which can be used to avoid adding any |
|
191 | + * validation strategies, and sets the normalization |
|
192 | + * strategy to the Null normalization. This is good when you |
|
193 | + * want the input to be totally ignored server-side (like |
|
194 | + * when using React.js form inputs) |
|
195 | + * } |
|
196 | + */ |
|
197 | + public function __construct($input_args = []) |
|
198 | + { |
|
199 | + $input_args = (array) apply_filters('FHEE__EE_Form_Input_Base___construct__input_args', $input_args, $this); |
|
200 | + // the following properties must be cast as arrays |
|
201 | + if (isset($input_args['validation_strategies'])) { |
|
202 | + foreach ((array) $input_args['validation_strategies'] as $validation_strategy) { |
|
203 | + if ($validation_strategy instanceof EE_Validation_Strategy_Base && empty($input_args['ignore_input'])) { |
|
204 | + $this->_validation_strategies[ get_class($validation_strategy) ] = $validation_strategy; |
|
205 | + } |
|
206 | + } |
|
207 | + unset($input_args['validation_strategies']); |
|
208 | + } |
|
209 | + if (isset($input_args['ignore_input'])) { |
|
210 | + $this->_validation_strategies = []; |
|
211 | + } |
|
212 | + // loop thru incoming options |
|
213 | + foreach ($input_args as $key => $value) { |
|
214 | + // add underscore to $key to match property names |
|
215 | + $_key = '_' . $key; |
|
216 | + if (property_exists($this, $_key)) { |
|
217 | + $this->{$_key} = $value; |
|
218 | + } |
|
219 | + } |
|
220 | + // ensure that "required" is set correctly |
|
221 | + $this->set_required( |
|
222 | + $this->_required, |
|
223 | + $input_args['required_validation_error_message'] ?? null |
|
224 | + ); |
|
225 | + // $this->_html_name_specified = isset( $input_args['html_name'] ) ? TRUE : FALSE; |
|
226 | + $this->_display_strategy->_construct_finalize($this); |
|
227 | + foreach ($this->_validation_strategies as $validation_strategy) { |
|
228 | + $validation_strategy->_construct_finalize($this); |
|
229 | + } |
|
230 | + if (isset($input_args['ignore_input'])) { |
|
231 | + $this->_normalization_strategy = new EE_Null_Normalization(); |
|
232 | + } |
|
233 | + if (! $this->_normalization_strategy) { |
|
234 | + $this->_normalization_strategy = new EE_Text_Normalization(); |
|
235 | + } |
|
236 | + $this->_normalization_strategy->_construct_finalize($this); |
|
237 | + // at least we can use the normalization strategy to populate the default |
|
238 | + if (isset($input_args['default'])) { |
|
239 | + $this->set_default($input_args['default']); |
|
240 | + unset($input_args['default']); |
|
241 | + } |
|
242 | + if (! $this->_sensitive_data_removal_strategy) { |
|
243 | + $this->_sensitive_data_removal_strategy = new EE_No_Sensitive_Data_Removal(); |
|
244 | + } |
|
245 | + $this->_sensitive_data_removal_strategy->_construct_finalize($this); |
|
246 | + parent::__construct($input_args); |
|
247 | + } |
|
248 | + |
|
249 | + |
|
250 | + /** |
|
251 | + * Sets the html_name to its default value, if none was specified in teh constructor. |
|
252 | + * Calculation involves using the name and the parent's html_name |
|
253 | + * |
|
254 | + * @throws EE_Error |
|
255 | + */ |
|
256 | + protected function _set_default_html_name_if_empty() |
|
257 | + { |
|
258 | + if (! $this->_html_name) { |
|
259 | + $this->_html_name = $this->name(); |
|
260 | + if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
|
261 | + $this->_html_name = $this->_parent_section->html_name_prefix() . "[{$this->name()}]"; |
|
262 | + } |
|
263 | + } |
|
264 | + } |
|
265 | + |
|
266 | + |
|
267 | + /** |
|
268 | + * @param $parent_form_section |
|
269 | + * @param $name |
|
270 | + * @throws EE_Error |
|
271 | + */ |
|
272 | + public function _construct_finalize($parent_form_section, $name) |
|
273 | + { |
|
274 | + parent::_construct_finalize($parent_form_section, $name); |
|
275 | + if ($this->_html_label === null && $this->_html_label_text === null) { |
|
276 | + $this->_html_label_text = ucwords(str_replace("_", " ", $name)); |
|
277 | + } |
|
278 | + do_action('AHEE__EE_Form_Input_Base___construct_finalize__end', $this, $parent_form_section, $name); |
|
279 | + } |
|
280 | + |
|
281 | + |
|
282 | + /** |
|
283 | + * Returns the strategy for displaying this form input. If none is set, throws an exception. |
|
284 | + * |
|
285 | + * @return EE_Display_Strategy_Base |
|
286 | + * @throws EE_Error |
|
287 | + */ |
|
288 | + protected function _get_display_strategy() |
|
289 | + { |
|
290 | + $this->ensure_construct_finalized_called(); |
|
291 | + if (! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) { |
|
292 | + throw new EE_Error( |
|
293 | + sprintf( |
|
294 | + esc_html__( |
|
295 | + "Cannot get display strategy for form input with name %s and id %s, because it has not been set in the constructor", |
|
296 | + "event_espresso" |
|
297 | + ), |
|
298 | + $this->html_name(), |
|
299 | + $this->html_id() |
|
300 | + ) |
|
301 | + ); |
|
302 | + } else { |
|
303 | + return $this->_display_strategy; |
|
304 | + } |
|
305 | + } |
|
306 | + |
|
307 | + |
|
308 | + /** |
|
309 | + * Sets the display strategy. |
|
310 | + * |
|
311 | + * @param EE_Display_Strategy_Base $strategy |
|
312 | + */ |
|
313 | + protected function _set_display_strategy(EE_Display_Strategy_Base $strategy) |
|
314 | + { |
|
315 | + $this->_display_strategy = $strategy; |
|
316 | + } |
|
317 | + |
|
318 | + |
|
319 | + /** |
|
320 | + * Sets the sanitization strategy |
|
321 | + * |
|
322 | + * @param EE_Normalization_Strategy_Base $strategy |
|
323 | + */ |
|
324 | + protected function _set_normalization_strategy(EE_Normalization_Strategy_Base $strategy) |
|
325 | + { |
|
326 | + $this->_normalization_strategy = $strategy; |
|
327 | + } |
|
328 | + |
|
329 | + |
|
330 | + /** |
|
331 | + * Gets sensitive_data_removal_strategy |
|
332 | + * |
|
333 | + * @return EE_Sensitive_Data_Removal_Base |
|
334 | + */ |
|
335 | + public function get_sensitive_data_removal_strategy() |
|
336 | + { |
|
337 | + return $this->_sensitive_data_removal_strategy; |
|
338 | + } |
|
339 | + |
|
340 | + |
|
341 | + /** |
|
342 | + * Sets sensitive_data_removal_strategy |
|
343 | + * |
|
344 | + * @param EE_Sensitive_Data_Removal_Base $sensitive_data_removal_strategy |
|
345 | + * @return void |
|
346 | + */ |
|
347 | + public function set_sensitive_data_removal_strategy($sensitive_data_removal_strategy) |
|
348 | + { |
|
349 | + $this->_sensitive_data_removal_strategy = $sensitive_data_removal_strategy; |
|
350 | + } |
|
351 | + |
|
352 | + |
|
353 | + /** |
|
354 | + * Gets the display strategy for this input |
|
355 | + * |
|
356 | + * @return EE_Display_Strategy_Base |
|
357 | + */ |
|
358 | + public function get_display_strategy() |
|
359 | + { |
|
360 | + return $this->_display_strategy; |
|
361 | + } |
|
362 | + |
|
363 | + |
|
364 | + /** |
|
365 | + * Overwrites the display strategy |
|
366 | + * |
|
367 | + * @param EE_Display_Strategy_Base $display_strategy |
|
368 | + */ |
|
369 | + public function set_display_strategy($display_strategy) |
|
370 | + { |
|
371 | + $this->_display_strategy = $display_strategy; |
|
372 | + $this->_display_strategy->_construct_finalize($this); |
|
373 | + } |
|
374 | + |
|
375 | + |
|
376 | + /** |
|
377 | + * Gets the normalization strategy set on this input |
|
378 | + * |
|
379 | + * @return EE_Normalization_Strategy_Base |
|
380 | + */ |
|
381 | + public function get_normalization_strategy() |
|
382 | + { |
|
383 | + return $this->_normalization_strategy; |
|
384 | + } |
|
385 | + |
|
386 | + |
|
387 | + /** |
|
388 | + * Overwrites the normalization strategy |
|
389 | + * |
|
390 | + * @param EE_Normalization_Strategy_Base $normalization_strategy |
|
391 | + */ |
|
392 | + public function set_normalization_strategy($normalization_strategy) |
|
393 | + { |
|
394 | + $this->_normalization_strategy = $normalization_strategy; |
|
395 | + $this->_normalization_strategy->_construct_finalize($this); |
|
396 | + } |
|
397 | + |
|
398 | + |
|
399 | + /** |
|
400 | + * Returns all teh validation strategies which apply to this field, numerically indexed |
|
401 | + * |
|
402 | + * @return EE_Validation_Strategy_Base[] |
|
403 | + */ |
|
404 | + public function get_validation_strategies() |
|
405 | + { |
|
406 | + return $this->_validation_strategies; |
|
407 | + } |
|
408 | + |
|
409 | + |
|
410 | + /** |
|
411 | + * Adds this strategy to the field so it will be used in both JS validation and server-side validation |
|
412 | + * |
|
413 | + * @param EE_Validation_Strategy_Base $validation_strategy |
|
414 | + * @return void |
|
415 | + */ |
|
416 | + protected function _add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
417 | + { |
|
418 | + $validation_strategy->_construct_finalize($this); |
|
419 | + $this->_validation_strategies[] = $validation_strategy; |
|
420 | + } |
|
421 | + |
|
422 | + |
|
423 | + /** |
|
424 | + * Adds a new validation strategy onto the form input |
|
425 | + * |
|
426 | + * @param EE_Validation_Strategy_Base $validation_strategy |
|
427 | + * @return void |
|
428 | + */ |
|
429 | + public function add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
430 | + { |
|
431 | + $this->_add_validation_strategy($validation_strategy); |
|
432 | + } |
|
433 | + |
|
434 | + |
|
435 | + /** |
|
436 | + * The classname of the validation strategy to remove |
|
437 | + * |
|
438 | + * @param string $validation_strategy_classname |
|
439 | + */ |
|
440 | + public function remove_validation_strategy($validation_strategy_classname) |
|
441 | + { |
|
442 | + foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
443 | + if ( |
|
444 | + $validation_strategy instanceof $validation_strategy_classname |
|
445 | + || is_subclass_of($validation_strategy, $validation_strategy_classname) |
|
446 | + ) { |
|
447 | + unset($this->_validation_strategies[ $key ]); |
|
448 | + } |
|
449 | + } |
|
450 | + } |
|
451 | + |
|
452 | + |
|
453 | + /** |
|
454 | + * returns true if input employs any of the validation strategy defined by the supplied array of classnames |
|
455 | + * |
|
456 | + * @param array $validation_strategy_classnames |
|
457 | + * @return bool |
|
458 | + */ |
|
459 | + public function has_validation_strategy($validation_strategy_classnames) |
|
460 | + { |
|
461 | + $validation_strategy_classnames = is_array($validation_strategy_classnames) |
|
462 | + ? $validation_strategy_classnames |
|
463 | + : [$validation_strategy_classnames]; |
|
464 | + foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
465 | + if (in_array($key, $validation_strategy_classnames)) { |
|
466 | + return true; |
|
467 | + } |
|
468 | + } |
|
469 | + return false; |
|
470 | + } |
|
471 | + |
|
472 | + |
|
473 | + /** |
|
474 | + * Gets the HTML |
|
475 | + * |
|
476 | + * @return string |
|
477 | + */ |
|
478 | + public function get_html() |
|
479 | + { |
|
480 | + return $this->_parent_section->get_html_for_input($this); |
|
481 | + } |
|
482 | + |
|
483 | + |
|
484 | + /** |
|
485 | + * Gets the HTML for the input itself (no label or errors) according to the |
|
486 | + * input's display strategy |
|
487 | + * Makes sure the JS and CSS are enqueued for it |
|
488 | + * |
|
489 | + * @return string |
|
490 | + * @throws EE_Error |
|
491 | + */ |
|
492 | + public function get_html_for_input() |
|
493 | + { |
|
494 | + return $this->_form_html_filter |
|
495 | + ? $this->_form_html_filter->filterHtml( |
|
496 | + $this->_get_display_strategy()->display(), |
|
497 | + $this |
|
498 | + ) |
|
499 | + : $this->_get_display_strategy()->display(); |
|
500 | + } |
|
501 | + |
|
502 | + |
|
503 | + /** |
|
504 | + * @return string |
|
505 | + */ |
|
506 | + public function html_other_attributes() |
|
507 | + { |
|
508 | + EE_Error::doing_it_wrong( |
|
509 | + __METHOD__, |
|
510 | + sprintf( |
|
511 | + esc_html__( |
|
512 | + 'This method is no longer in use. You should replace it by %s', |
|
513 | + 'event_espresso' |
|
514 | + ), |
|
515 | + 'EE_Form_Section_Base::other_html_attributes()' |
|
516 | + ), |
|
517 | + '4.10.2.p' |
|
518 | + ); |
|
519 | + |
|
520 | + return $this->other_html_attributes(); |
|
521 | + } |
|
522 | + |
|
523 | + |
|
524 | + /** |
|
525 | + * @param string $html_other_attributes |
|
526 | + */ |
|
527 | + public function set_html_other_attributes($html_other_attributes) |
|
528 | + { |
|
529 | + EE_Error::doing_it_wrong( |
|
530 | + __METHOD__, |
|
531 | + sprintf( |
|
532 | + esc_html__( |
|
533 | + 'This method is no longer in use. You should replace it by %s', |
|
534 | + 'event_espresso' |
|
535 | + ), |
|
536 | + 'EE_Form_Section_Base::set_other_html_attributes()' |
|
537 | + ), |
|
538 | + '4.10.2.p' |
|
539 | + ); |
|
540 | + |
|
541 | + $this->set_other_html_attributes($html_other_attributes); |
|
542 | + } |
|
543 | + |
|
544 | + |
|
545 | + /** |
|
546 | + * Gets the HTML for displaying the label for this form input |
|
547 | + * according to the form section's layout strategy |
|
548 | + * |
|
549 | + * @return string |
|
550 | + */ |
|
551 | + public function get_html_for_label() |
|
552 | + { |
|
553 | + return $this->_parent_section->get_layout_strategy()->display_label($this); |
|
554 | + } |
|
555 | + |
|
556 | + |
|
557 | + /** |
|
558 | + * Gets the HTML for displaying the errors section for this form input |
|
559 | + * according to the form section's layout strategy |
|
560 | + * |
|
561 | + * @return string |
|
562 | + */ |
|
563 | + public function get_html_for_errors() |
|
564 | + { |
|
565 | + return $this->_parent_section->get_layout_strategy()->display_errors($this); |
|
566 | + } |
|
567 | + |
|
568 | + |
|
569 | + /** |
|
570 | + * Gets the HTML for displaying the help text for this form input |
|
571 | + * according to the form section's layout strategy |
|
572 | + * |
|
573 | + * @return string |
|
574 | + */ |
|
575 | + public function get_html_for_help() |
|
576 | + { |
|
577 | + return $this->_parent_section->get_layout_strategy()->display_help_text($this); |
|
578 | + } |
|
579 | + |
|
580 | + |
|
581 | + /** |
|
582 | + * Validates the input's sanitized value (assumes _sanitize() has already been called) |
|
583 | + * and returns whether or not the form input's submitted value is value |
|
584 | + * |
|
585 | + * @return boolean |
|
586 | + */ |
|
587 | + protected function _validate() |
|
588 | + { |
|
589 | + if ($this->isDisabled()) { |
|
590 | + return true; |
|
591 | + } |
|
592 | + foreach ($this->_validation_strategies as $validation_strategy) { |
|
593 | + if ($validation_strategy instanceof EE_Validation_Strategy_Base) { |
|
594 | + try { |
|
595 | + $validation_strategy->validate($this->normalized_value()); |
|
596 | + } catch (EE_Validation_Error $e) { |
|
597 | + $this->add_validation_error($e); |
|
598 | + } |
|
599 | + } |
|
600 | + } |
|
601 | + if ($this->get_validation_errors()) { |
|
602 | + return false; |
|
603 | + } else { |
|
604 | + return true; |
|
605 | + } |
|
606 | + } |
|
607 | + |
|
608 | + |
|
609 | + /** |
|
610 | + * Performs basic sanitization on this value. But what sanitization can be performed anyways? |
|
611 | + * This value MIGHT be allowed to have tags, so we can't really remove them. |
|
612 | + * |
|
613 | + * @param string $value |
|
614 | + * @return null|string |
|
615 | + */ |
|
616 | + protected function _sanitize($value) |
|
617 | + { |
|
618 | + return $value !== null |
|
619 | + ? stripslashes(html_entity_decode(trim((string) $value))) |
|
620 | + : null; |
|
621 | + } |
|
622 | + |
|
623 | + |
|
624 | + /** |
|
625 | + * Picks out the form value that relates to this form input, |
|
626 | + * and stores it as the sanitized value on the form input, and sets the normalized value. |
|
627 | + * Returns whether or not any validation errors occurred |
|
628 | + * |
|
629 | + * @param array $req_data |
|
630 | + * @return boolean whether or not there was an error |
|
631 | + * @throws EE_Error |
|
632 | + */ |
|
633 | + protected function _normalize($req_data) |
|
634 | + { |
|
635 | + // any existing validation errors don't apply so clear them |
|
636 | + $this->_validation_errors = []; |
|
637 | + // if the input is disabled, ignore whatever input was sent in |
|
638 | + if ($this->isDisabled()) { |
|
639 | + $this->_set_raw_value(null); |
|
640 | + $this->_set_normalized_value($this->get_default()); |
|
641 | + return false; |
|
642 | + } |
|
643 | + try { |
|
644 | + $raw_input = $this->find_form_data_for_this_section($req_data); |
|
645 | + // super simple sanitization for now |
|
646 | + if (is_array($raw_input)) { |
|
647 | + $raw_value = []; |
|
648 | + foreach ($raw_input as $key => $value) { |
|
649 | + $raw_value[ $key ] = $this->_sanitize($value); |
|
650 | + } |
|
651 | + $this->_set_raw_value($raw_value); |
|
652 | + } else { |
|
653 | + $this->_set_raw_value($this->_sanitize($raw_input)); |
|
654 | + } |
|
655 | + // we want to mostly leave the input alone in case we need to re-display it to the user |
|
656 | + $this->_set_normalized_value($this->_normalization_strategy->normalize($this->raw_value())); |
|
657 | + return false; |
|
658 | + } catch (EE_Validation_Error $e) { |
|
659 | + $this->add_validation_error($e); |
|
660 | + return true; |
|
661 | + } |
|
662 | + } |
|
663 | + |
|
664 | + |
|
665 | + /** |
|
666 | + * @return string |
|
667 | + * @throws EE_Error |
|
668 | + */ |
|
669 | + public function html_name() |
|
670 | + { |
|
671 | + $this->_set_default_html_name_if_empty(); |
|
672 | + return $this->_html_name ?? ''; |
|
673 | + } |
|
674 | + |
|
675 | + |
|
676 | + /** |
|
677 | + * @return string |
|
678 | + * @throws EE_Error |
|
679 | + */ |
|
680 | + public function html_label_id() |
|
681 | + { |
|
682 | + return ! empty($this->_html_label_id) |
|
683 | + ? $this->_html_label_id |
|
684 | + : $this->html_id() . '-lbl'; |
|
685 | + } |
|
686 | + |
|
687 | + |
|
688 | + /** |
|
689 | + * @return string |
|
690 | + */ |
|
691 | + public function html_label_class() |
|
692 | + { |
|
693 | + return $this->_html_label_class ?? ''; |
|
694 | + } |
|
695 | + |
|
696 | + |
|
697 | + /** |
|
698 | + * @param string $html_class |
|
699 | + */ |
|
700 | + public function add_html_label_class(string $html_class) |
|
701 | + { |
|
702 | + $this->_html_label_class .= ' ' . trim($html_class); |
|
703 | + } |
|
704 | + |
|
705 | + |
|
706 | + /** |
|
707 | + * @return string |
|
708 | + */ |
|
709 | + public function html_label_style() |
|
710 | + { |
|
711 | + return $this->_html_label_style ?? ''; |
|
712 | + } |
|
713 | + |
|
714 | + |
|
715 | + /** |
|
716 | + * @return string |
|
717 | + */ |
|
718 | + public function html_label_text() |
|
719 | + { |
|
720 | + return $this->_html_label_text ?? ''; |
|
721 | + } |
|
722 | + |
|
723 | + |
|
724 | + /** |
|
725 | + * @return string |
|
726 | + */ |
|
727 | + public function html_help_text() |
|
728 | + { |
|
729 | + return $this->_html_help_text ?? ''; |
|
730 | + } |
|
731 | + |
|
732 | + |
|
733 | + /** |
|
734 | + * @return string |
|
735 | + */ |
|
736 | + public function html_help_class() |
|
737 | + { |
|
738 | + return $this->_html_help_class ?? ''; |
|
739 | + } |
|
740 | + |
|
741 | + |
|
742 | + /** |
|
743 | + * @return string |
|
744 | + */ |
|
745 | + public function html_help_style() |
|
746 | + { |
|
747 | + return $this->_html_style ?? ''; |
|
748 | + } |
|
749 | + |
|
750 | + |
|
751 | + /** |
|
752 | + * returns the raw, UNSAFE, input, almost exactly as the user submitted it. |
|
753 | + * Please note that almost all client code should instead use the normalized_value; |
|
754 | + * or possibly raw_value_in_form (which prepares the string for displaying in an HTML attribute on a tag, |
|
755 | + * mostly by escaping quotes) |
|
756 | + * Note, we do not store the exact original value sent in the user's request because |
|
757 | + * it may have malicious content, and we MIGHT want to store the form input in a transient or something... |
|
758 | + * in which case, we would have stored the malicious content to our database. |
|
759 | + * |
|
760 | + * @return mixed |
|
761 | + */ |
|
762 | + public function raw_value() |
|
763 | + { |
|
764 | + return $this->_raw_value; |
|
765 | + } |
|
766 | + |
|
767 | + |
|
768 | + /** |
|
769 | + * Returns a string safe to usage in form inputs when displaying, because |
|
770 | + * it escapes all html entities |
|
771 | + * |
|
772 | + * @return string |
|
773 | + */ |
|
774 | + public function raw_value_in_form(): string |
|
775 | + { |
|
776 | + return htmlentities((string) $this->raw_value(), ENT_QUOTES, 'UTF-8'); |
|
777 | + } |
|
778 | + |
|
779 | + |
|
780 | + /** |
|
781 | + * returns the value after it's been sanitized, and then converted into it's proper type |
|
782 | + * in PHP. Eg, a string, an int, an array, |
|
783 | + * |
|
784 | + * @return mixed |
|
785 | + */ |
|
786 | + public function normalized_value() |
|
787 | + { |
|
788 | + return $this->_normalized_value; |
|
789 | + } |
|
790 | + |
|
791 | + |
|
792 | + /** |
|
793 | + * Returns the normalized value is a presentable way. By default this is just |
|
794 | + * the normalized value by itself, but it can be overridden for when that's not |
|
795 | + * the best thing to display |
|
796 | + * |
|
797 | + * @return mixed |
|
798 | + */ |
|
799 | + public function pretty_value() |
|
800 | + { |
|
801 | + return $this->_normalized_value; |
|
802 | + } |
|
803 | + |
|
804 | + |
|
805 | + /** |
|
806 | + * When generating the JS for the jquery validation rules like<br> |
|
807 | + * <code>$( "#myform" ).validate({ |
|
808 | + * rules: { |
|
809 | + * password: "required", |
|
810 | + * password_again: { |
|
811 | + * equalTo: "#password" |
|
812 | + * } |
|
813 | + * } |
|
814 | + * });</code> |
|
815 | + * if this field had the name 'password_again', it should return |
|
816 | + * <br><code>password_again: { |
|
817 | + * equalTo: "#password" |
|
818 | + * }</code> |
|
819 | + * |
|
820 | + * @return array |
|
821 | + */ |
|
822 | + public function get_jquery_validation_rules(): array |
|
823 | + { |
|
824 | + $jquery_validation_js = []; |
|
825 | + $jquery_validation_rules = []; |
|
826 | + foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
827 | + $jquery_validation_rules = array_replace_recursive( |
|
828 | + $jquery_validation_rules, |
|
829 | + $validation_strategy->get_jquery_validation_rule_array() |
|
830 | + ); |
|
831 | + } |
|
832 | + if (! empty($jquery_validation_rules)) { |
|
833 | + foreach ($this->get_display_strategy()->get_html_input_ids(true) as $html_id_with_pound_sign) { |
|
834 | + $jquery_validation_js[ $html_id_with_pound_sign ] = $jquery_validation_rules; |
|
835 | + } |
|
836 | + } |
|
837 | + return $jquery_validation_js; |
|
838 | + } |
|
839 | + |
|
840 | + |
|
841 | + /** |
|
842 | + * Sets the input's default value for use in displaying in the form. Note: value should be |
|
843 | + * normalized (Eg, if providing a default of ra Yes_NO_Input you would provide TRUE or FALSE, not '1' or '0') |
|
844 | + * |
|
845 | + * @param mixed $value |
|
846 | + * @return void |
|
847 | + */ |
|
848 | + public function set_default($value) |
|
849 | + { |
|
850 | + $this->_default = $value; |
|
851 | + $this->_set_normalized_value($value); |
|
852 | + $this->_set_raw_value($value); |
|
853 | + } |
|
854 | + |
|
855 | + |
|
856 | + /** |
|
857 | + * Sets the normalized value on this input |
|
858 | + * |
|
859 | + * @param mixed $value |
|
860 | + */ |
|
861 | + protected function _set_normalized_value($value) |
|
862 | + { |
|
863 | + $this->_normalized_value = $value; |
|
864 | + } |
|
865 | + |
|
866 | + |
|
867 | + /** |
|
868 | + * Sets the raw value on this input (ie, exactly as the user submitted it) |
|
869 | + * |
|
870 | + * @param mixed $value |
|
871 | + */ |
|
872 | + protected function _set_raw_value($value) |
|
873 | + { |
|
874 | + $this->_raw_value = $this->_normalization_strategy->unnormalize($value); |
|
875 | + } |
|
876 | + |
|
877 | + |
|
878 | + /** |
|
879 | + * Sets the HTML label text after it has already been defined |
|
880 | + * |
|
881 | + * @param string $label |
|
882 | + * @return void |
|
883 | + */ |
|
884 | + public function set_html_label_text($label) |
|
885 | + { |
|
886 | + $this->_html_label_text = $label; |
|
887 | + } |
|
888 | + |
|
889 | + |
|
890 | + /** |
|
891 | + * Sets whether or not this field is required, and adjusts the validation strategy. |
|
892 | + * If you want to use the EE_Conditionally_Required_Validation_Strategy, |
|
893 | + * please add it as a validation strategy using add_validation_strategy as normal |
|
894 | + * |
|
895 | + * @param boolean $required boolean |
|
896 | + * @param null $required_text |
|
897 | + */ |
|
898 | + public function set_required($required = true, $required_text = null) |
|
899 | + { |
|
900 | + $required = filter_var($required, FILTER_VALIDATE_BOOLEAN); |
|
901 | + // whether $required is a string or a boolean, we want to add a required validation strategy |
|
902 | + if ($required) { |
|
903 | + $this->_add_validation_strategy(new EE_Required_Validation_Strategy($required_text)); |
|
904 | + } else { |
|
905 | + $this->remove_validation_strategy('EE_Required_Validation_Strategy'); |
|
906 | + } |
|
907 | + $this->_required = $required; |
|
908 | + } |
|
909 | + |
|
910 | + |
|
911 | + /** |
|
912 | + * Returns whether or not this field is required |
|
913 | + * |
|
914 | + * @return boolean |
|
915 | + */ |
|
916 | + public function required() |
|
917 | + { |
|
918 | + return $this->_required; |
|
919 | + } |
|
920 | + |
|
921 | + |
|
922 | + /** |
|
923 | + * @param string $required_css_class |
|
924 | + */ |
|
925 | + public function set_required_css_class($required_css_class) |
|
926 | + { |
|
927 | + $this->_required_css_class = $required_css_class; |
|
928 | + } |
|
929 | + |
|
930 | + |
|
931 | + /** |
|
932 | + * @return string |
|
933 | + */ |
|
934 | + public function required_css_class() |
|
935 | + { |
|
936 | + return $this->_required_css_class; |
|
937 | + } |
|
938 | + |
|
939 | + |
|
940 | + /** |
|
941 | + * @param bool $add_required |
|
942 | + * @return string |
|
943 | + */ |
|
944 | + public function html_class($add_required = false) |
|
945 | + { |
|
946 | + return $add_required && $this->required() |
|
947 | + ? $this->required_css_class() . ' ' . $this->_html_class |
|
948 | + : $this->_html_class; |
|
949 | + } |
|
950 | + |
|
951 | + |
|
952 | + /** |
|
953 | + * Sets the help text, in case |
|
954 | + * |
|
955 | + * @param string $text |
|
956 | + */ |
|
957 | + public function set_html_help_text($text) |
|
958 | + { |
|
959 | + $this->_html_help_text = $text; |
|
960 | + } |
|
961 | + |
|
962 | + |
|
963 | + /** |
|
964 | + * Uses the sensitive data removal strategy to remove the sensitive data from this |
|
965 | + * input. If there is any kind of sensitive data removal on this input, we clear |
|
966 | + * out the raw value completely |
|
967 | + * |
|
968 | + * @return void |
|
969 | + */ |
|
970 | + public function clean_sensitive_data() |
|
971 | + { |
|
972 | + // if we do ANY kind of sensitive data removal on this, then just clear out the raw value |
|
973 | + // if we need more logic than this we'll make a strategy for it |
|
974 | + if ( |
|
975 | + $this->_sensitive_data_removal_strategy |
|
976 | + && ! $this->_sensitive_data_removal_strategy instanceof EE_No_Sensitive_Data_Removal |
|
977 | + ) { |
|
978 | + $this->_set_raw_value(null); |
|
979 | + } |
|
980 | + // and clean the normalized value according to the appropriate strategy |
|
981 | + $this->_set_normalized_value( |
|
982 | + $this->get_sensitive_data_removal_strategy()->remove_sensitive_data( |
|
983 | + $this->_normalized_value |
|
984 | + ) |
|
985 | + ); |
|
986 | + } |
|
987 | + |
|
988 | + |
|
989 | + /** |
|
990 | + * @param bool $primary |
|
991 | + * @param string $button_size |
|
992 | + * @param string $other_attributes |
|
993 | + */ |
|
994 | + public function set_button_css_attributes($primary = true, $button_size = '', $other_attributes = '') |
|
995 | + { |
|
996 | + $button_css_attributes = 'button'; |
|
997 | + $button_css_attributes .= $primary === true |
|
998 | + ? ' button--primary' |
|
999 | + : ' button--secondary'; |
|
1000 | + switch ($button_size) { |
|
1001 | + case 'xs': |
|
1002 | + case 'extra-small': |
|
1003 | + $button_css_attributes .= ' button-xs'; |
|
1004 | + break; |
|
1005 | + case 'sm': |
|
1006 | + case 'small': |
|
1007 | + $button_css_attributes .= ' button-sm'; |
|
1008 | + break; |
|
1009 | + case 'lg': |
|
1010 | + case 'large': |
|
1011 | + $button_css_attributes .= ' button-lg'; |
|
1012 | + break; |
|
1013 | + case 'block': |
|
1014 | + $button_css_attributes .= ' button-block'; |
|
1015 | + break; |
|
1016 | + case 'md': |
|
1017 | + case 'medium': |
|
1018 | + default: |
|
1019 | + $button_css_attributes .= ''; |
|
1020 | + } |
|
1021 | + $this->_button_css_attributes .= ! empty($other_attributes) |
|
1022 | + ? $button_css_attributes . ' ' . $other_attributes |
|
1023 | + : $button_css_attributes; |
|
1024 | + } |
|
1025 | + |
|
1026 | + |
|
1027 | + /** |
|
1028 | + * @return string |
|
1029 | + */ |
|
1030 | + public function button_css_attributes() |
|
1031 | + { |
|
1032 | + if (empty($this->_button_css_attributes)) { |
|
1033 | + $this->set_button_css_attributes(); |
|
1034 | + } |
|
1035 | + return $this->_button_css_attributes; |
|
1036 | + } |
|
1037 | + |
|
1038 | + |
|
1039 | + /** |
|
1040 | + * find_form_data_for_this_section |
|
1041 | + * using this section's name and its parents, finds the value of the form data that corresponds to it. |
|
1042 | + * For example, if this form section's HTML name is my_form[subform][form_input_1], |
|
1043 | + * then it's value should be in request at request['my_form']['subform']['form_input_1']. |
|
1044 | + * (If that doesn't exist, we also check for this subsection's name |
|
1045 | + * at the TOP LEVEL of the request data. Eg request['form_input_1'].) |
|
1046 | + * This function finds its value in the form. |
|
1047 | + * |
|
1048 | + * @param array $req_data |
|
1049 | + * @return mixed whatever the raw value of this form section is in the request data |
|
1050 | + * @throws EE_Error |
|
1051 | + */ |
|
1052 | + public function find_form_data_for_this_section($req_data) |
|
1053 | + { |
|
1054 | + $name_parts = $this->getInputNameParts(); |
|
1055 | + // now get the value for the input |
|
1056 | + $value = $this->findRequestForSectionUsingNameParts($name_parts, $req_data); |
|
1057 | + // check if this thing's name is at the TOP level of the request data |
|
1058 | + if ($value === null && isset($req_data[ $this->name() ])) { |
|
1059 | + $value = $req_data[ $this->name() ]; |
|
1060 | + } |
|
1061 | + return $value; |
|
1062 | + } |
|
1063 | + |
|
1064 | + |
|
1065 | + /** |
|
1066 | + * If this input's name is something like "foo[bar][baz]" |
|
1067 | + * returns an array like `array('foo','bar',baz')` |
|
1068 | + * |
|
1069 | + * @return array |
|
1070 | + * @throws EE_Error |
|
1071 | + */ |
|
1072 | + protected function getInputNameParts() |
|
1073 | + { |
|
1074 | + // break up the html name by "[]" |
|
1075 | + if (strpos($this->html_name(), '[') !== false) { |
|
1076 | + $before_any_brackets = substr($this->html_name(), 0, strpos($this->html_name(), '[')); |
|
1077 | + } else { |
|
1078 | + $before_any_brackets = $this->html_name(); |
|
1079 | + } |
|
1080 | + // grab all of the segments |
|
1081 | + preg_match_all('~\[([^]]*)\]~', $this->html_name(), $matches); |
|
1082 | + if (isset($matches[1]) && is_array($matches[1])) { |
|
1083 | + $name_parts = $matches[1]; |
|
1084 | + array_unshift($name_parts, $before_any_brackets); |
|
1085 | + } else { |
|
1086 | + $name_parts = [$before_any_brackets]; |
|
1087 | + } |
|
1088 | + return $name_parts; |
|
1089 | + } |
|
1090 | + |
|
1091 | + |
|
1092 | + /** |
|
1093 | + * @param array $html_name_parts |
|
1094 | + * @param array $req_data |
|
1095 | + * @return array | NULL |
|
1096 | + */ |
|
1097 | + public function findRequestForSectionUsingNameParts($html_name_parts, $req_data) |
|
1098 | + { |
|
1099 | + $first_part_to_consider = array_shift($html_name_parts); |
|
1100 | + if (isset($req_data[ $first_part_to_consider ])) { |
|
1101 | + if (empty($html_name_parts)) { |
|
1102 | + return $req_data[ $first_part_to_consider ]; |
|
1103 | + } else { |
|
1104 | + return $this->findRequestForSectionUsingNameParts( |
|
1105 | + $html_name_parts, |
|
1106 | + $req_data[ $first_part_to_consider ] |
|
1107 | + ); |
|
1108 | + } |
|
1109 | + } else { |
|
1110 | + return null; |
|
1111 | + } |
|
1112 | + } |
|
1113 | + |
|
1114 | + |
|
1115 | + /** |
|
1116 | + * Checks if this form input's data is in the request data |
|
1117 | + * |
|
1118 | + * @param array $req_data |
|
1119 | + * @return boolean |
|
1120 | + * @throws EE_Error |
|
1121 | + */ |
|
1122 | + public function form_data_present_in($req_data = null) |
|
1123 | + { |
|
1124 | + if ($req_data === null) { |
|
1125 | + /** @var RequestInterface $request */ |
|
1126 | + $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
1127 | + $req_data = $request->postParams(); |
|
1128 | + } |
|
1129 | + $checked_value = $this->find_form_data_for_this_section($req_data); |
|
1130 | + if ($checked_value !== null) { |
|
1131 | + return true; |
|
1132 | + } else { |
|
1133 | + return false; |
|
1134 | + } |
|
1135 | + } |
|
1136 | + |
|
1137 | + |
|
1138 | + /** |
|
1139 | + * Overrides parent to add js data from validation and display strategies |
|
1140 | + * |
|
1141 | + * @param array $form_other_js_data |
|
1142 | + * @return array |
|
1143 | + */ |
|
1144 | + public function get_other_js_data($form_other_js_data = []) |
|
1145 | + { |
|
1146 | + return $this->get_other_js_data_from_strategies($form_other_js_data); |
|
1147 | + } |
|
1148 | + |
|
1149 | + |
|
1150 | + /** |
|
1151 | + * Gets other JS data for localization from this input's strategies, like |
|
1152 | + * the validation strategies and the display strategy |
|
1153 | + * |
|
1154 | + * @param array $form_other_js_data |
|
1155 | + * @return array |
|
1156 | + */ |
|
1157 | + public function get_other_js_data_from_strategies($form_other_js_data = []) |
|
1158 | + { |
|
1159 | + $form_other_js_data = $this->get_display_strategy()->get_other_js_data($form_other_js_data); |
|
1160 | + foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
1161 | + $form_other_js_data = $validation_strategy->get_other_js_data($form_other_js_data); |
|
1162 | + } |
|
1163 | + return $form_other_js_data; |
|
1164 | + } |
|
1165 | + |
|
1166 | + |
|
1167 | + /** |
|
1168 | + * Override parent because we want to give our strategies an opportunity to enqueue some js and css |
|
1169 | + * |
|
1170 | + * @return void |
|
1171 | + */ |
|
1172 | + public function enqueue_js() |
|
1173 | + { |
|
1174 | + // ask our display strategy and validation strategies if they have js to enqueue |
|
1175 | + $this->enqueue_js_from_strategies(); |
|
1176 | + } |
|
1177 | + |
|
1178 | + |
|
1179 | + /** |
|
1180 | + * Tells strategies when its ok to enqueue their js and css |
|
1181 | + * |
|
1182 | + * @return void |
|
1183 | + */ |
|
1184 | + public function enqueue_js_from_strategies() |
|
1185 | + { |
|
1186 | + $this->get_display_strategy()->enqueue_js(); |
|
1187 | + foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
1188 | + $validation_strategy->enqueue_js(); |
|
1189 | + } |
|
1190 | + } |
|
1191 | + |
|
1192 | + |
|
1193 | + /** |
|
1194 | + * Gets the default value set on the input (not the current value, which may have been |
|
1195 | + * changed because of a form submission). If no default was set, this us null. |
|
1196 | + * |
|
1197 | + * @return mixed |
|
1198 | + */ |
|
1199 | + public function get_default() |
|
1200 | + { |
|
1201 | + return $this->_default; |
|
1202 | + } |
|
1203 | + |
|
1204 | + |
|
1205 | + /** |
|
1206 | + * Makes this input disabled. That means it will have the HTML attribute 'disabled="disabled"', |
|
1207 | + * and server-side if any input was received it will be ignored |
|
1208 | + */ |
|
1209 | + public function disable($disable = true) |
|
1210 | + { |
|
1211 | + $disabled_attribute = ' disabled="disabled"'; |
|
1212 | + $this->disabled = filter_var($disable, FILTER_VALIDATE_BOOLEAN); |
|
1213 | + if ($this->disabled) { |
|
1214 | + if (strpos($this->_other_html_attributes, $disabled_attribute) === false) { |
|
1215 | + $this->_other_html_attributes .= $disabled_attribute; |
|
1216 | + } |
|
1217 | + $this->_set_normalized_value($this->get_default()); |
|
1218 | + } else { |
|
1219 | + $this->_other_html_attributes = str_replace($disabled_attribute, '', $this->_other_html_attributes); |
|
1220 | + } |
|
1221 | + } |
|
1222 | + |
|
1223 | + |
|
1224 | + /** |
|
1225 | + * Returns whether or not this input is currently disabled. |
|
1226 | + * |
|
1227 | + * @return bool |
|
1228 | + */ |
|
1229 | + public function isDisabled() |
|
1230 | + { |
|
1231 | + return $this->disabled; |
|
1232 | + } |
|
1233 | 1233 | } |
@@ -201,7 +201,7 @@ discard block |
||
201 | 201 | if (isset($input_args['validation_strategies'])) { |
202 | 202 | foreach ((array) $input_args['validation_strategies'] as $validation_strategy) { |
203 | 203 | if ($validation_strategy instanceof EE_Validation_Strategy_Base && empty($input_args['ignore_input'])) { |
204 | - $this->_validation_strategies[ get_class($validation_strategy) ] = $validation_strategy; |
|
204 | + $this->_validation_strategies[get_class($validation_strategy)] = $validation_strategy; |
|
205 | 205 | } |
206 | 206 | } |
207 | 207 | unset($input_args['validation_strategies']); |
@@ -212,7 +212,7 @@ discard block |
||
212 | 212 | // loop thru incoming options |
213 | 213 | foreach ($input_args as $key => $value) { |
214 | 214 | // add underscore to $key to match property names |
215 | - $_key = '_' . $key; |
|
215 | + $_key = '_'.$key; |
|
216 | 216 | if (property_exists($this, $_key)) { |
217 | 217 | $this->{$_key} = $value; |
218 | 218 | } |
@@ -230,7 +230,7 @@ discard block |
||
230 | 230 | if (isset($input_args['ignore_input'])) { |
231 | 231 | $this->_normalization_strategy = new EE_Null_Normalization(); |
232 | 232 | } |
233 | - if (! $this->_normalization_strategy) { |
|
233 | + if ( ! $this->_normalization_strategy) { |
|
234 | 234 | $this->_normalization_strategy = new EE_Text_Normalization(); |
235 | 235 | } |
236 | 236 | $this->_normalization_strategy->_construct_finalize($this); |
@@ -239,7 +239,7 @@ discard block |
||
239 | 239 | $this->set_default($input_args['default']); |
240 | 240 | unset($input_args['default']); |
241 | 241 | } |
242 | - if (! $this->_sensitive_data_removal_strategy) { |
|
242 | + if ( ! $this->_sensitive_data_removal_strategy) { |
|
243 | 243 | $this->_sensitive_data_removal_strategy = new EE_No_Sensitive_Data_Removal(); |
244 | 244 | } |
245 | 245 | $this->_sensitive_data_removal_strategy->_construct_finalize($this); |
@@ -255,10 +255,10 @@ discard block |
||
255 | 255 | */ |
256 | 256 | protected function _set_default_html_name_if_empty() |
257 | 257 | { |
258 | - if (! $this->_html_name) { |
|
258 | + if ( ! $this->_html_name) { |
|
259 | 259 | $this->_html_name = $this->name(); |
260 | 260 | if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
261 | - $this->_html_name = $this->_parent_section->html_name_prefix() . "[{$this->name()}]"; |
|
261 | + $this->_html_name = $this->_parent_section->html_name_prefix()."[{$this->name()}]"; |
|
262 | 262 | } |
263 | 263 | } |
264 | 264 | } |
@@ -288,7 +288,7 @@ discard block |
||
288 | 288 | protected function _get_display_strategy() |
289 | 289 | { |
290 | 290 | $this->ensure_construct_finalized_called(); |
291 | - if (! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) { |
|
291 | + if ( ! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) { |
|
292 | 292 | throw new EE_Error( |
293 | 293 | sprintf( |
294 | 294 | esc_html__( |
@@ -444,7 +444,7 @@ discard block |
||
444 | 444 | $validation_strategy instanceof $validation_strategy_classname |
445 | 445 | || is_subclass_of($validation_strategy, $validation_strategy_classname) |
446 | 446 | ) { |
447 | - unset($this->_validation_strategies[ $key ]); |
|
447 | + unset($this->_validation_strategies[$key]); |
|
448 | 448 | } |
449 | 449 | } |
450 | 450 | } |
@@ -646,7 +646,7 @@ discard block |
||
646 | 646 | if (is_array($raw_input)) { |
647 | 647 | $raw_value = []; |
648 | 648 | foreach ($raw_input as $key => $value) { |
649 | - $raw_value[ $key ] = $this->_sanitize($value); |
|
649 | + $raw_value[$key] = $this->_sanitize($value); |
|
650 | 650 | } |
651 | 651 | $this->_set_raw_value($raw_value); |
652 | 652 | } else { |
@@ -681,7 +681,7 @@ discard block |
||
681 | 681 | { |
682 | 682 | return ! empty($this->_html_label_id) |
683 | 683 | ? $this->_html_label_id |
684 | - : $this->html_id() . '-lbl'; |
|
684 | + : $this->html_id().'-lbl'; |
|
685 | 685 | } |
686 | 686 | |
687 | 687 | |
@@ -699,7 +699,7 @@ discard block |
||
699 | 699 | */ |
700 | 700 | public function add_html_label_class(string $html_class) |
701 | 701 | { |
702 | - $this->_html_label_class .= ' ' . trim($html_class); |
|
702 | + $this->_html_label_class .= ' '.trim($html_class); |
|
703 | 703 | } |
704 | 704 | |
705 | 705 | |
@@ -829,9 +829,9 @@ discard block |
||
829 | 829 | $validation_strategy->get_jquery_validation_rule_array() |
830 | 830 | ); |
831 | 831 | } |
832 | - if (! empty($jquery_validation_rules)) { |
|
832 | + if ( ! empty($jquery_validation_rules)) { |
|
833 | 833 | foreach ($this->get_display_strategy()->get_html_input_ids(true) as $html_id_with_pound_sign) { |
834 | - $jquery_validation_js[ $html_id_with_pound_sign ] = $jquery_validation_rules; |
|
834 | + $jquery_validation_js[$html_id_with_pound_sign] = $jquery_validation_rules; |
|
835 | 835 | } |
836 | 836 | } |
837 | 837 | return $jquery_validation_js; |
@@ -944,7 +944,7 @@ discard block |
||
944 | 944 | public function html_class($add_required = false) |
945 | 945 | { |
946 | 946 | return $add_required && $this->required() |
947 | - ? $this->required_css_class() . ' ' . $this->_html_class |
|
947 | + ? $this->required_css_class().' '.$this->_html_class |
|
948 | 948 | : $this->_html_class; |
949 | 949 | } |
950 | 950 | |
@@ -1019,7 +1019,7 @@ discard block |
||
1019 | 1019 | $button_css_attributes .= ''; |
1020 | 1020 | } |
1021 | 1021 | $this->_button_css_attributes .= ! empty($other_attributes) |
1022 | - ? $button_css_attributes . ' ' . $other_attributes |
|
1022 | + ? $button_css_attributes.' '.$other_attributes |
|
1023 | 1023 | : $button_css_attributes; |
1024 | 1024 | } |
1025 | 1025 | |
@@ -1055,8 +1055,8 @@ discard block |
||
1055 | 1055 | // now get the value for the input |
1056 | 1056 | $value = $this->findRequestForSectionUsingNameParts($name_parts, $req_data); |
1057 | 1057 | // check if this thing's name is at the TOP level of the request data |
1058 | - if ($value === null && isset($req_data[ $this->name() ])) { |
|
1059 | - $value = $req_data[ $this->name() ]; |
|
1058 | + if ($value === null && isset($req_data[$this->name()])) { |
|
1059 | + $value = $req_data[$this->name()]; |
|
1060 | 1060 | } |
1061 | 1061 | return $value; |
1062 | 1062 | } |
@@ -1097,13 +1097,13 @@ discard block |
||
1097 | 1097 | public function findRequestForSectionUsingNameParts($html_name_parts, $req_data) |
1098 | 1098 | { |
1099 | 1099 | $first_part_to_consider = array_shift($html_name_parts); |
1100 | - if (isset($req_data[ $first_part_to_consider ])) { |
|
1100 | + if (isset($req_data[$first_part_to_consider])) { |
|
1101 | 1101 | if (empty($html_name_parts)) { |
1102 | - return $req_data[ $first_part_to_consider ]; |
|
1102 | + return $req_data[$first_part_to_consider]; |
|
1103 | 1103 | } else { |
1104 | 1104 | return $this->findRequestForSectionUsingNameParts( |
1105 | 1105 | $html_name_parts, |
1106 | - $req_data[ $first_part_to_consider ] |
|
1106 | + $req_data[$first_part_to_consider] |
|
1107 | 1107 | ); |
1108 | 1108 | } |
1109 | 1109 | } else { |