@@ -37,6 +37,8 @@ discard block |
||
37 | 37 | * @param \EE_State | string $state |
38 | 38 | * @param string $zip |
39 | 39 | * @param \EE_Country | string $country |
40 | + * @param integer $state |
|
41 | + * @param string $country |
|
40 | 42 | * @return GenericAddress |
41 | 43 | */ |
42 | 44 | public function __construct($address, $address2, $city, $state, $zip, $country) |
@@ -130,7 +132,7 @@ discard block |
||
130 | 132 | |
131 | 133 | |
132 | 134 | /** |
133 | - * @return \EE_State |
|
135 | + * @return string |
|
134 | 136 | */ |
135 | 137 | public function state_obj() |
136 | 138 | { |
@@ -183,7 +185,7 @@ discard block |
||
183 | 185 | |
184 | 186 | |
185 | 187 | /** |
186 | - * @return \EE_Country |
|
188 | + * @return string |
|
187 | 189 | */ |
188 | 190 | public function country_obj() |
189 | 191 | { |
@@ -12,205 +12,205 @@ |
||
12 | 12 | */ |
13 | 13 | class GenericAddress implements \EEI_Address |
14 | 14 | { |
15 | - // phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore |
|
16 | - private $_address = ''; |
|
17 | - |
|
18 | - private $_address2 = ''; |
|
19 | - |
|
20 | - private $_city = ''; |
|
21 | - |
|
22 | - private $_state_ID = ''; |
|
23 | - |
|
24 | - private $_state_obj = ''; |
|
25 | - |
|
26 | - private $_zip = ''; |
|
27 | - |
|
28 | - private $_country_ID = ''; |
|
29 | - |
|
30 | - private $_country_obj = ''; |
|
31 | - // phpcs:enable |
|
32 | - |
|
33 | - // phpcs:disable PSR2.Methods.MethodDeclaration.Underscore |
|
34 | - /** |
|
35 | - * @param string $address |
|
36 | - * @param string $address2 |
|
37 | - * @param string $city |
|
38 | - * @param \EE_State | string $state |
|
39 | - * @param string $zip |
|
40 | - * @param \EE_Country | string $country |
|
41 | - * @return GenericAddress |
|
42 | - */ |
|
43 | - public function __construct($address, $address2, $city, $state, $zip, $country) |
|
44 | - { |
|
45 | - $this->_address = $address; |
|
46 | - $this->_address2 = $address2; |
|
47 | - $this->_city = $city; |
|
48 | - if ($state instanceof \EE_State) { |
|
49 | - $this->_state_obj = $state; |
|
50 | - } else { |
|
51 | - $this->_state_ID = $state; |
|
52 | - $this->_state_obj = $this->_get_state_obj(); |
|
53 | - } |
|
54 | - $this->_zip = $zip; |
|
55 | - if ($country instanceof \EE_Country) { |
|
56 | - $this->_country_obj = $country; |
|
57 | - } else { |
|
58 | - $this->_country_ID = $country; |
|
59 | - $this->_country_obj = $this->_get_country_obj(); |
|
60 | - } |
|
61 | - } |
|
62 | - |
|
63 | - |
|
64 | - /** |
|
65 | - * @return string |
|
66 | - */ |
|
67 | - public function address() |
|
68 | - { |
|
69 | - return $this->_address; |
|
70 | - } |
|
71 | - |
|
72 | - |
|
73 | - /** |
|
74 | - * @return string |
|
75 | - */ |
|
76 | - public function address2() |
|
77 | - { |
|
78 | - return $this->_address2; |
|
79 | - } |
|
80 | - |
|
81 | - |
|
82 | - /** |
|
83 | - * @return string |
|
84 | - */ |
|
85 | - public function city() |
|
86 | - { |
|
87 | - return $this->_city; |
|
88 | - } |
|
89 | - |
|
90 | - // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
91 | - |
|
92 | - /** |
|
93 | - * @return \EE_State |
|
94 | - */ |
|
95 | - private function _get_state_obj() |
|
96 | - { |
|
97 | - return $this->_state_obj instanceof \EE_State |
|
98 | - ? $this->_state_obj |
|
99 | - : \EE_Registry::instance()->load_model('State')->get_one_by_ID($this->_state_ID); |
|
100 | - } |
|
101 | - |
|
102 | - |
|
103 | - /** |
|
104 | - * @return string |
|
105 | - */ |
|
106 | - public function state_ID() |
|
107 | - { |
|
108 | - return $this->_state_ID; |
|
109 | - } |
|
110 | - |
|
111 | - |
|
112 | - /** |
|
113 | - * @return string |
|
114 | - */ |
|
115 | - public function state_abbrev() |
|
116 | - { |
|
117 | - return $this->state_obj() instanceof \EE_State |
|
118 | - ? $this->state_obj()->abbrev() |
|
119 | - : ''; |
|
120 | - } |
|
121 | - |
|
122 | - |
|
123 | - /** |
|
124 | - * @return string |
|
125 | - */ |
|
126 | - public function state_name() |
|
127 | - { |
|
128 | - return $this->state_obj() instanceof \EE_State |
|
129 | - ? $this->state_obj()->name() |
|
130 | - : ''; |
|
131 | - } |
|
132 | - |
|
133 | - |
|
134 | - /** |
|
135 | - * @return \EE_State |
|
136 | - */ |
|
137 | - public function state_obj() |
|
138 | - { |
|
139 | - return $this->_state_obj; |
|
140 | - } |
|
141 | - |
|
142 | - |
|
143 | - /** |
|
144 | - * @return string |
|
145 | - */ |
|
146 | - public function state() |
|
147 | - { |
|
148 | - if (apply_filters('FHEE__EEI_Address__state__use_abbreviation', true, $this->state_obj())) { |
|
149 | - return $this->state_obj()->abbrev(); |
|
150 | - } else { |
|
151 | - return $this->state_name(); |
|
152 | - } |
|
153 | - } |
|
154 | - |
|
155 | - |
|
156 | - /** |
|
157 | - * @return \EE_Country |
|
158 | - */ |
|
159 | - private function _get_country_obj() |
|
160 | - { |
|
161 | - return $this->_country_obj instanceof \EE_Country |
|
162 | - ? $this->_country_obj |
|
163 | - : \EE_Registry::instance()->load_model('Country')->get_one_by_ID($this->_country_ID); |
|
164 | - } |
|
165 | - |
|
166 | - |
|
167 | - /** |
|
168 | - * @return string |
|
169 | - */ |
|
170 | - public function country_ID() |
|
171 | - { |
|
172 | - return $this->_country_ID; |
|
173 | - } |
|
174 | - |
|
175 | - |
|
176 | - /** |
|
177 | - * @return string |
|
178 | - */ |
|
179 | - public function country_name() |
|
180 | - { |
|
181 | - return $this->country_obj() instanceof \EE_Country |
|
182 | - ? $this->country_obj()->name() |
|
183 | - : ''; |
|
184 | - } |
|
185 | - |
|
186 | - |
|
187 | - /** |
|
188 | - * @return \EE_Country |
|
189 | - */ |
|
190 | - public function country_obj() |
|
191 | - { |
|
192 | - return $this->_country_obj; |
|
193 | - } |
|
194 | - |
|
195 | - |
|
196 | - /** |
|
197 | - * @return string |
|
198 | - */ |
|
199 | - public function country() |
|
200 | - { |
|
201 | - if (apply_filters('FHEE__EEI_Address__country__use_abbreviation', true, $this->country_obj())) { |
|
202 | - return $this->country_ID(); |
|
203 | - } else { |
|
204 | - return $this->country_name(); |
|
205 | - } |
|
206 | - } |
|
207 | - |
|
208 | - |
|
209 | - /** |
|
210 | - * @return string |
|
211 | - */ |
|
212 | - public function zip() |
|
213 | - { |
|
214 | - return $this->_zip; |
|
215 | - } |
|
15 | + // phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore |
|
16 | + private $_address = ''; |
|
17 | + |
|
18 | + private $_address2 = ''; |
|
19 | + |
|
20 | + private $_city = ''; |
|
21 | + |
|
22 | + private $_state_ID = ''; |
|
23 | + |
|
24 | + private $_state_obj = ''; |
|
25 | + |
|
26 | + private $_zip = ''; |
|
27 | + |
|
28 | + private $_country_ID = ''; |
|
29 | + |
|
30 | + private $_country_obj = ''; |
|
31 | + // phpcs:enable |
|
32 | + |
|
33 | + // phpcs:disable PSR2.Methods.MethodDeclaration.Underscore |
|
34 | + /** |
|
35 | + * @param string $address |
|
36 | + * @param string $address2 |
|
37 | + * @param string $city |
|
38 | + * @param \EE_State | string $state |
|
39 | + * @param string $zip |
|
40 | + * @param \EE_Country | string $country |
|
41 | + * @return GenericAddress |
|
42 | + */ |
|
43 | + public function __construct($address, $address2, $city, $state, $zip, $country) |
|
44 | + { |
|
45 | + $this->_address = $address; |
|
46 | + $this->_address2 = $address2; |
|
47 | + $this->_city = $city; |
|
48 | + if ($state instanceof \EE_State) { |
|
49 | + $this->_state_obj = $state; |
|
50 | + } else { |
|
51 | + $this->_state_ID = $state; |
|
52 | + $this->_state_obj = $this->_get_state_obj(); |
|
53 | + } |
|
54 | + $this->_zip = $zip; |
|
55 | + if ($country instanceof \EE_Country) { |
|
56 | + $this->_country_obj = $country; |
|
57 | + } else { |
|
58 | + $this->_country_ID = $country; |
|
59 | + $this->_country_obj = $this->_get_country_obj(); |
|
60 | + } |
|
61 | + } |
|
62 | + |
|
63 | + |
|
64 | + /** |
|
65 | + * @return string |
|
66 | + */ |
|
67 | + public function address() |
|
68 | + { |
|
69 | + return $this->_address; |
|
70 | + } |
|
71 | + |
|
72 | + |
|
73 | + /** |
|
74 | + * @return string |
|
75 | + */ |
|
76 | + public function address2() |
|
77 | + { |
|
78 | + return $this->_address2; |
|
79 | + } |
|
80 | + |
|
81 | + |
|
82 | + /** |
|
83 | + * @return string |
|
84 | + */ |
|
85 | + public function city() |
|
86 | + { |
|
87 | + return $this->_city; |
|
88 | + } |
|
89 | + |
|
90 | + // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
91 | + |
|
92 | + /** |
|
93 | + * @return \EE_State |
|
94 | + */ |
|
95 | + private function _get_state_obj() |
|
96 | + { |
|
97 | + return $this->_state_obj instanceof \EE_State |
|
98 | + ? $this->_state_obj |
|
99 | + : \EE_Registry::instance()->load_model('State')->get_one_by_ID($this->_state_ID); |
|
100 | + } |
|
101 | + |
|
102 | + |
|
103 | + /** |
|
104 | + * @return string |
|
105 | + */ |
|
106 | + public function state_ID() |
|
107 | + { |
|
108 | + return $this->_state_ID; |
|
109 | + } |
|
110 | + |
|
111 | + |
|
112 | + /** |
|
113 | + * @return string |
|
114 | + */ |
|
115 | + public function state_abbrev() |
|
116 | + { |
|
117 | + return $this->state_obj() instanceof \EE_State |
|
118 | + ? $this->state_obj()->abbrev() |
|
119 | + : ''; |
|
120 | + } |
|
121 | + |
|
122 | + |
|
123 | + /** |
|
124 | + * @return string |
|
125 | + */ |
|
126 | + public function state_name() |
|
127 | + { |
|
128 | + return $this->state_obj() instanceof \EE_State |
|
129 | + ? $this->state_obj()->name() |
|
130 | + : ''; |
|
131 | + } |
|
132 | + |
|
133 | + |
|
134 | + /** |
|
135 | + * @return \EE_State |
|
136 | + */ |
|
137 | + public function state_obj() |
|
138 | + { |
|
139 | + return $this->_state_obj; |
|
140 | + } |
|
141 | + |
|
142 | + |
|
143 | + /** |
|
144 | + * @return string |
|
145 | + */ |
|
146 | + public function state() |
|
147 | + { |
|
148 | + if (apply_filters('FHEE__EEI_Address__state__use_abbreviation', true, $this->state_obj())) { |
|
149 | + return $this->state_obj()->abbrev(); |
|
150 | + } else { |
|
151 | + return $this->state_name(); |
|
152 | + } |
|
153 | + } |
|
154 | + |
|
155 | + |
|
156 | + /** |
|
157 | + * @return \EE_Country |
|
158 | + */ |
|
159 | + private function _get_country_obj() |
|
160 | + { |
|
161 | + return $this->_country_obj instanceof \EE_Country |
|
162 | + ? $this->_country_obj |
|
163 | + : \EE_Registry::instance()->load_model('Country')->get_one_by_ID($this->_country_ID); |
|
164 | + } |
|
165 | + |
|
166 | + |
|
167 | + /** |
|
168 | + * @return string |
|
169 | + */ |
|
170 | + public function country_ID() |
|
171 | + { |
|
172 | + return $this->_country_ID; |
|
173 | + } |
|
174 | + |
|
175 | + |
|
176 | + /** |
|
177 | + * @return string |
|
178 | + */ |
|
179 | + public function country_name() |
|
180 | + { |
|
181 | + return $this->country_obj() instanceof \EE_Country |
|
182 | + ? $this->country_obj()->name() |
|
183 | + : ''; |
|
184 | + } |
|
185 | + |
|
186 | + |
|
187 | + /** |
|
188 | + * @return \EE_Country |
|
189 | + */ |
|
190 | + public function country_obj() |
|
191 | + { |
|
192 | + return $this->_country_obj; |
|
193 | + } |
|
194 | + |
|
195 | + |
|
196 | + /** |
|
197 | + * @return string |
|
198 | + */ |
|
199 | + public function country() |
|
200 | + { |
|
201 | + if (apply_filters('FHEE__EEI_Address__country__use_abbreviation', true, $this->country_obj())) { |
|
202 | + return $this->country_ID(); |
|
203 | + } else { |
|
204 | + return $this->country_name(); |
|
205 | + } |
|
206 | + } |
|
207 | + |
|
208 | + |
|
209 | + /** |
|
210 | + * @return string |
|
211 | + */ |
|
212 | + public function zip() |
|
213 | + { |
|
214 | + return $this->_zip; |
|
215 | + } |
|
216 | 216 | } |
@@ -34,8 +34,8 @@ |
||
34 | 34 | * @param EE_Transaction $transaction |
35 | 35 | * @param EE_Ticket $ticket |
36 | 36 | * @param EE_Line_Item $ticket_line_item |
37 | - * @param $reg_count |
|
38 | - * @param $reg_group_size |
|
37 | + * @param integer $reg_count |
|
38 | + * @param integer $reg_group_size |
|
39 | 39 | * @param string $reg_status |
40 | 40 | * @return EE_Registration |
41 | 41 | * @throws OutOfRangeException |
@@ -29,102 +29,102 @@ |
||
29 | 29 | { |
30 | 30 | |
31 | 31 | |
32 | - /** |
|
33 | - * @param EE_Event $event |
|
34 | - * @param EE_Transaction $transaction |
|
35 | - * @param EE_Ticket $ticket |
|
36 | - * @param EE_Line_Item $ticket_line_item |
|
37 | - * @param $reg_count |
|
38 | - * @param $reg_group_size |
|
39 | - * @param string $reg_status |
|
40 | - * @return EE_Registration |
|
41 | - * @throws OutOfRangeException |
|
42 | - * @throws EE_Error |
|
43 | - * @throws UnexpectedEntityException |
|
44 | - */ |
|
45 | - public function create( |
|
46 | - EE_Event $event, |
|
47 | - EE_Transaction $transaction, |
|
48 | - EE_Ticket $ticket, |
|
49 | - EE_Line_Item $ticket_line_item, |
|
50 | - $reg_count, |
|
51 | - $reg_group_size, |
|
52 | - $reg_status = EEM_Registration::status_id_incomplete |
|
53 | - ) { |
|
54 | - $registrations = $transaction->registrations(); |
|
55 | - $reg_count = $reg_count ? $reg_count : count($registrations) + 1; |
|
56 | - $reg_url_link = new RegUrlLink($reg_count, $ticket_line_item); |
|
57 | - $reg_code = new RegCode($reg_url_link, $transaction, $ticket); |
|
58 | - // generate new EE_Registration |
|
59 | - $registration = EE_Registration::new_instance( |
|
60 | - array( |
|
61 | - 'EVT_ID' => $event->ID(), |
|
62 | - 'TXN_ID' => $transaction->ID(), |
|
63 | - 'TKT_ID' => $ticket->ID(), |
|
64 | - 'STS_ID' => $reg_status, |
|
65 | - 'REG_final_price' => $this->resolveFinalPrice($transaction, $ticket, $ticket_line_item), |
|
66 | - 'REG_session' => EE_Registry::instance()->SSN->id(), |
|
67 | - 'REG_count' => $reg_count, |
|
68 | - 'REG_group_size' => $reg_group_size ? $reg_group_size : $this->incrementRegCount($registrations), |
|
69 | - 'REG_url_link' => $reg_url_link, |
|
70 | - 'REG_code' => $reg_code, |
|
71 | - ) |
|
72 | - ); |
|
73 | - if (! $registration instanceof EE_Registration) { |
|
74 | - throw new UnexpectedEntityException($registration, 'EE_Registration'); |
|
75 | - } |
|
76 | - // save registration so that we have an ID |
|
77 | - $registration->save(); |
|
78 | - // track reservation on reg but don't adjust ticket and datetime reserved counts |
|
79 | - // because that is done as soon as the tickets are added/removed from the cart |
|
80 | - $registration->reserve_ticket(false, 'CreateRegistrationService:' . __LINE__); |
|
81 | - $registration->_add_relation_to($event, 'Event', array(), $event->ID()); |
|
82 | - $registration->_add_relation_to($ticket, 'Ticket', array(), $ticket->ID()); |
|
83 | - $transaction->_add_relation_to($registration, 'Registration', array(), $registration->ID()); |
|
84 | - $registration->save(); |
|
85 | - return $registration; |
|
86 | - } |
|
32 | + /** |
|
33 | + * @param EE_Event $event |
|
34 | + * @param EE_Transaction $transaction |
|
35 | + * @param EE_Ticket $ticket |
|
36 | + * @param EE_Line_Item $ticket_line_item |
|
37 | + * @param $reg_count |
|
38 | + * @param $reg_group_size |
|
39 | + * @param string $reg_status |
|
40 | + * @return EE_Registration |
|
41 | + * @throws OutOfRangeException |
|
42 | + * @throws EE_Error |
|
43 | + * @throws UnexpectedEntityException |
|
44 | + */ |
|
45 | + public function create( |
|
46 | + EE_Event $event, |
|
47 | + EE_Transaction $transaction, |
|
48 | + EE_Ticket $ticket, |
|
49 | + EE_Line_Item $ticket_line_item, |
|
50 | + $reg_count, |
|
51 | + $reg_group_size, |
|
52 | + $reg_status = EEM_Registration::status_id_incomplete |
|
53 | + ) { |
|
54 | + $registrations = $transaction->registrations(); |
|
55 | + $reg_count = $reg_count ? $reg_count : count($registrations) + 1; |
|
56 | + $reg_url_link = new RegUrlLink($reg_count, $ticket_line_item); |
|
57 | + $reg_code = new RegCode($reg_url_link, $transaction, $ticket); |
|
58 | + // generate new EE_Registration |
|
59 | + $registration = EE_Registration::new_instance( |
|
60 | + array( |
|
61 | + 'EVT_ID' => $event->ID(), |
|
62 | + 'TXN_ID' => $transaction->ID(), |
|
63 | + 'TKT_ID' => $ticket->ID(), |
|
64 | + 'STS_ID' => $reg_status, |
|
65 | + 'REG_final_price' => $this->resolveFinalPrice($transaction, $ticket, $ticket_line_item), |
|
66 | + 'REG_session' => EE_Registry::instance()->SSN->id(), |
|
67 | + 'REG_count' => $reg_count, |
|
68 | + 'REG_group_size' => $reg_group_size ? $reg_group_size : $this->incrementRegCount($registrations), |
|
69 | + 'REG_url_link' => $reg_url_link, |
|
70 | + 'REG_code' => $reg_code, |
|
71 | + ) |
|
72 | + ); |
|
73 | + if (! $registration instanceof EE_Registration) { |
|
74 | + throw new UnexpectedEntityException($registration, 'EE_Registration'); |
|
75 | + } |
|
76 | + // save registration so that we have an ID |
|
77 | + $registration->save(); |
|
78 | + // track reservation on reg but don't adjust ticket and datetime reserved counts |
|
79 | + // because that is done as soon as the tickets are added/removed from the cart |
|
80 | + $registration->reserve_ticket(false, 'CreateRegistrationService:' . __LINE__); |
|
81 | + $registration->_add_relation_to($event, 'Event', array(), $event->ID()); |
|
82 | + $registration->_add_relation_to($ticket, 'Ticket', array(), $ticket->ID()); |
|
83 | + $transaction->_add_relation_to($registration, 'Registration', array(), $registration->ID()); |
|
84 | + $registration->save(); |
|
85 | + return $registration; |
|
86 | + } |
|
87 | 87 | |
88 | 88 | |
89 | - /** |
|
90 | - * @param EE_Transaction $transaction |
|
91 | - * @param EE_Ticket $ticket |
|
92 | - * @param EE_Line_Item $ticket_line_item |
|
93 | - * @return float |
|
94 | - * @throws EE_Error |
|
95 | - * @throws OutOfRangeException |
|
96 | - */ |
|
97 | - protected function resolveFinalPrice( |
|
98 | - EE_Transaction $transaction, |
|
99 | - EE_Ticket $ticket, |
|
100 | - EE_Line_Item $ticket_line_item |
|
101 | - ) { |
|
102 | - $final_price = EEH_Line_Item::calculate_final_price_for_ticket_line_item( |
|
103 | - $transaction->total_line_item(), |
|
104 | - $ticket_line_item |
|
105 | - ); |
|
106 | - $final_price = $final_price !== null ? $final_price : $ticket->get_ticket_total_with_taxes(); |
|
107 | - return (float) $final_price; |
|
108 | - } |
|
89 | + /** |
|
90 | + * @param EE_Transaction $transaction |
|
91 | + * @param EE_Ticket $ticket |
|
92 | + * @param EE_Line_Item $ticket_line_item |
|
93 | + * @return float |
|
94 | + * @throws EE_Error |
|
95 | + * @throws OutOfRangeException |
|
96 | + */ |
|
97 | + protected function resolveFinalPrice( |
|
98 | + EE_Transaction $transaction, |
|
99 | + EE_Ticket $ticket, |
|
100 | + EE_Line_Item $ticket_line_item |
|
101 | + ) { |
|
102 | + $final_price = EEH_Line_Item::calculate_final_price_for_ticket_line_item( |
|
103 | + $transaction->total_line_item(), |
|
104 | + $ticket_line_item |
|
105 | + ); |
|
106 | + $final_price = $final_price !== null ? $final_price : $ticket->get_ticket_total_with_taxes(); |
|
107 | + return (float) $final_price; |
|
108 | + } |
|
109 | 109 | |
110 | 110 | |
111 | - /** |
|
112 | - * @param EE_Registration[] $registrations |
|
113 | - * @param boolean $update_existing_registrations |
|
114 | - * @return int |
|
115 | - * @throws EE_Error |
|
116 | - */ |
|
117 | - protected function incrementRegCount(array $registrations, $update_existing_registrations = true) |
|
118 | - { |
|
119 | - $new_reg_count = count($registrations) + 1; |
|
120 | - if ($update_existing_registrations) { |
|
121 | - foreach ($registrations as $registration) { |
|
122 | - if ($registration instanceof EE_Registration) { |
|
123 | - $registration->set_count($new_reg_count); |
|
124 | - $registration->save(); |
|
125 | - } |
|
126 | - } |
|
127 | - } |
|
128 | - return $new_reg_count; |
|
129 | - } |
|
111 | + /** |
|
112 | + * @param EE_Registration[] $registrations |
|
113 | + * @param boolean $update_existing_registrations |
|
114 | + * @return int |
|
115 | + * @throws EE_Error |
|
116 | + */ |
|
117 | + protected function incrementRegCount(array $registrations, $update_existing_registrations = true) |
|
118 | + { |
|
119 | + $new_reg_count = count($registrations) + 1; |
|
120 | + if ($update_existing_registrations) { |
|
121 | + foreach ($registrations as $registration) { |
|
122 | + if ($registration instanceof EE_Registration) { |
|
123 | + $registration->set_count($new_reg_count); |
|
124 | + $registration->save(); |
|
125 | + } |
|
126 | + } |
|
127 | + } |
|
128 | + return $new_reg_count; |
|
129 | + } |
|
130 | 130 | } |
@@ -70,14 +70,14 @@ |
||
70 | 70 | 'REG_code' => $reg_code, |
71 | 71 | ) |
72 | 72 | ); |
73 | - if (! $registration instanceof EE_Registration) { |
|
73 | + if ( ! $registration instanceof EE_Registration) { |
|
74 | 74 | throw new UnexpectedEntityException($registration, 'EE_Registration'); |
75 | 75 | } |
76 | 76 | // save registration so that we have an ID |
77 | 77 | $registration->save(); |
78 | 78 | // track reservation on reg but don't adjust ticket and datetime reserved counts |
79 | 79 | // because that is done as soon as the tickets are added/removed from the cart |
80 | - $registration->reserve_ticket(false, 'CreateRegistrationService:' . __LINE__); |
|
80 | + $registration->reserve_ticket(false, 'CreateRegistrationService:'.__LINE__); |
|
81 | 81 | $registration->_add_relation_to($event, 'Event', array(), $event->ID()); |
82 | 82 | $registration->_add_relation_to($ticket, 'Ticket', array(), $ticket->ID()); |
83 | 83 | $transaction->_add_relation_to($registration, 'Registration', array(), $registration->ID()); |
@@ -42,7 +42,6 @@ discard block |
||
42 | 42 | /** |
43 | 43 | * Return the schema for a given model from a given model. |
44 | 44 | * |
45 | - * @param \EEM_Base $model |
|
46 | 45 | * @return array |
47 | 46 | */ |
48 | 47 | public function getModelSchema() |
@@ -120,7 +119,6 @@ discard block |
||
120 | 119 | /** |
121 | 120 | * Outputs the schema header for a model. |
122 | 121 | * |
123 | - * @param \EEM_Base $model |
|
124 | 122 | * @return array |
125 | 123 | */ |
126 | 124 | public function getInitialSchemaStructure() |
@@ -24,126 +24,126 @@ |
||
24 | 24 | class JsonModelSchema |
25 | 25 | { |
26 | 26 | |
27 | - /** |
|
28 | - * @var \EEM_Base |
|
29 | - */ |
|
30 | - protected $model; |
|
31 | - |
|
32 | - /** |
|
33 | - * JsonModelSchema constructor. |
|
34 | - * |
|
35 | - * @param \EEM_Base $model |
|
36 | - */ |
|
37 | - public function __construct(EEM_Base $model) |
|
38 | - { |
|
39 | - $this->model = $model; |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * Return the schema for a given model from a given model. |
|
44 | - * |
|
45 | - * @param \EEM_Base $model |
|
46 | - * @return array |
|
47 | - */ |
|
48 | - public function getModelSchema() |
|
49 | - { |
|
50 | - return $this->getModelSchemaForRelations( |
|
51 | - $this->model->relation_settings(), |
|
52 | - $this->getModelSchemaForFields( |
|
53 | - $this->model->field_settings(), |
|
54 | - $this->getInitialSchemaStructure() |
|
55 | - ) |
|
56 | - ); |
|
57 | - } |
|
58 | - |
|
59 | - |
|
60 | - /** |
|
61 | - * Get the schema for a given set of model fields. |
|
62 | - * |
|
63 | - * @param \EE_Model_Field_Base[] $model_fields |
|
64 | - * @return array |
|
65 | - */ |
|
66 | - public function getModelSchemaForFields(array $model_fields, array $schema) |
|
67 | - { |
|
68 | - foreach ($model_fields as $field => $model_field) { |
|
69 | - if (! $model_field instanceof EE_Model_Field_Base) { |
|
70 | - continue; |
|
71 | - } |
|
72 | - $schema['properties'][ $field ] = $model_field->getSchema(); |
|
73 | - |
|
74 | - // if this is a primary key field add the primary key item |
|
75 | - if ($model_field instanceof EE_Primary_Key_Field_Base) { |
|
76 | - $schema['properties'][ $field ]['primary_key'] = true; |
|
77 | - if ($model_field instanceof EE_Primary_Key_Int_Field) { |
|
78 | - $schema['properties'][ $field ]['readonly'] = true; |
|
79 | - } |
|
80 | - } |
|
81 | - |
|
82 | - // if this is a foreign key field add the foreign key item |
|
83 | - if ($model_field instanceof EE_Foreign_Key_Field_Base) { |
|
84 | - $schema['properties'][ $field ]['foreign_key'] = array( |
|
85 | - 'description' => esc_html__( |
|
86 | - 'This is a foreign key the points to the given models.', |
|
87 | - 'event_espresso' |
|
88 | - ), |
|
89 | - 'type' => 'array', |
|
90 | - 'enum' => $model_field->get_model_class_names_pointed_to(), |
|
91 | - ); |
|
92 | - } |
|
93 | - } |
|
94 | - return $schema; |
|
95 | - } |
|
96 | - |
|
97 | - |
|
98 | - /** |
|
99 | - * Get the schema for a given set of model relations |
|
100 | - * |
|
101 | - * @param EE_Model_Relation_Base[] $relations_on_model |
|
102 | - * @return array |
|
103 | - */ |
|
104 | - public function getModelSchemaForRelations(array $relations_on_model, array $schema) |
|
105 | - { |
|
106 | - foreach ($relations_on_model as $model_name => $relation) { |
|
107 | - if (! $relation instanceof EE_Model_Relation_Base) { |
|
108 | - continue; |
|
109 | - } |
|
110 | - $model_name_for_schema = $relation instanceof EE_Belongs_To_Relation |
|
111 | - ? strtolower($model_name) |
|
112 | - : EEH_Inflector::pluralize_and_lower($model_name); |
|
113 | - $schema['properties'][ $model_name_for_schema ] = $relation->getSchema(); |
|
114 | - $schema['properties'][ $model_name_for_schema ]['relation_model'] = $model_name; |
|
115 | - } |
|
116 | - return $schema; |
|
117 | - } |
|
118 | - |
|
119 | - |
|
120 | - /** |
|
121 | - * Outputs the schema header for a model. |
|
122 | - * |
|
123 | - * @param \EEM_Base $model |
|
124 | - * @return array |
|
125 | - */ |
|
126 | - public function getInitialSchemaStructure() |
|
127 | - { |
|
128 | - return array( |
|
129 | - '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
130 | - 'title' => $this->model->get_this_model_name(), |
|
131 | - 'type' => 'object', |
|
132 | - 'properties' => array(), |
|
133 | - ); |
|
134 | - } |
|
135 | - |
|
136 | - |
|
137 | - /** |
|
138 | - * Allows one to just use the object as a string to get the json. |
|
139 | - * eg. |
|
140 | - * $json_schema = new JsonModelSchema(EEM_Event::instance()); |
|
141 | - * echo $json_schema; //outputs the schema as a json formatted string. |
|
142 | - * |
|
143 | - * @return bool|false|mixed|string |
|
144 | - */ |
|
145 | - public function __toString() |
|
146 | - { |
|
147 | - return wp_json_encode($this->getModelSchema()); |
|
148 | - } |
|
27 | + /** |
|
28 | + * @var \EEM_Base |
|
29 | + */ |
|
30 | + protected $model; |
|
31 | + |
|
32 | + /** |
|
33 | + * JsonModelSchema constructor. |
|
34 | + * |
|
35 | + * @param \EEM_Base $model |
|
36 | + */ |
|
37 | + public function __construct(EEM_Base $model) |
|
38 | + { |
|
39 | + $this->model = $model; |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * Return the schema for a given model from a given model. |
|
44 | + * |
|
45 | + * @param \EEM_Base $model |
|
46 | + * @return array |
|
47 | + */ |
|
48 | + public function getModelSchema() |
|
49 | + { |
|
50 | + return $this->getModelSchemaForRelations( |
|
51 | + $this->model->relation_settings(), |
|
52 | + $this->getModelSchemaForFields( |
|
53 | + $this->model->field_settings(), |
|
54 | + $this->getInitialSchemaStructure() |
|
55 | + ) |
|
56 | + ); |
|
57 | + } |
|
58 | + |
|
59 | + |
|
60 | + /** |
|
61 | + * Get the schema for a given set of model fields. |
|
62 | + * |
|
63 | + * @param \EE_Model_Field_Base[] $model_fields |
|
64 | + * @return array |
|
65 | + */ |
|
66 | + public function getModelSchemaForFields(array $model_fields, array $schema) |
|
67 | + { |
|
68 | + foreach ($model_fields as $field => $model_field) { |
|
69 | + if (! $model_field instanceof EE_Model_Field_Base) { |
|
70 | + continue; |
|
71 | + } |
|
72 | + $schema['properties'][ $field ] = $model_field->getSchema(); |
|
73 | + |
|
74 | + // if this is a primary key field add the primary key item |
|
75 | + if ($model_field instanceof EE_Primary_Key_Field_Base) { |
|
76 | + $schema['properties'][ $field ]['primary_key'] = true; |
|
77 | + if ($model_field instanceof EE_Primary_Key_Int_Field) { |
|
78 | + $schema['properties'][ $field ]['readonly'] = true; |
|
79 | + } |
|
80 | + } |
|
81 | + |
|
82 | + // if this is a foreign key field add the foreign key item |
|
83 | + if ($model_field instanceof EE_Foreign_Key_Field_Base) { |
|
84 | + $schema['properties'][ $field ]['foreign_key'] = array( |
|
85 | + 'description' => esc_html__( |
|
86 | + 'This is a foreign key the points to the given models.', |
|
87 | + 'event_espresso' |
|
88 | + ), |
|
89 | + 'type' => 'array', |
|
90 | + 'enum' => $model_field->get_model_class_names_pointed_to(), |
|
91 | + ); |
|
92 | + } |
|
93 | + } |
|
94 | + return $schema; |
|
95 | + } |
|
96 | + |
|
97 | + |
|
98 | + /** |
|
99 | + * Get the schema for a given set of model relations |
|
100 | + * |
|
101 | + * @param EE_Model_Relation_Base[] $relations_on_model |
|
102 | + * @return array |
|
103 | + */ |
|
104 | + public function getModelSchemaForRelations(array $relations_on_model, array $schema) |
|
105 | + { |
|
106 | + foreach ($relations_on_model as $model_name => $relation) { |
|
107 | + if (! $relation instanceof EE_Model_Relation_Base) { |
|
108 | + continue; |
|
109 | + } |
|
110 | + $model_name_for_schema = $relation instanceof EE_Belongs_To_Relation |
|
111 | + ? strtolower($model_name) |
|
112 | + : EEH_Inflector::pluralize_and_lower($model_name); |
|
113 | + $schema['properties'][ $model_name_for_schema ] = $relation->getSchema(); |
|
114 | + $schema['properties'][ $model_name_for_schema ]['relation_model'] = $model_name; |
|
115 | + } |
|
116 | + return $schema; |
|
117 | + } |
|
118 | + |
|
119 | + |
|
120 | + /** |
|
121 | + * Outputs the schema header for a model. |
|
122 | + * |
|
123 | + * @param \EEM_Base $model |
|
124 | + * @return array |
|
125 | + */ |
|
126 | + public function getInitialSchemaStructure() |
|
127 | + { |
|
128 | + return array( |
|
129 | + '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
130 | + 'title' => $this->model->get_this_model_name(), |
|
131 | + 'type' => 'object', |
|
132 | + 'properties' => array(), |
|
133 | + ); |
|
134 | + } |
|
135 | + |
|
136 | + |
|
137 | + /** |
|
138 | + * Allows one to just use the object as a string to get the json. |
|
139 | + * eg. |
|
140 | + * $json_schema = new JsonModelSchema(EEM_Event::instance()); |
|
141 | + * echo $json_schema; //outputs the schema as a json formatted string. |
|
142 | + * |
|
143 | + * @return bool|false|mixed|string |
|
144 | + */ |
|
145 | + public function __toString() |
|
146 | + { |
|
147 | + return wp_json_encode($this->getModelSchema()); |
|
148 | + } |
|
149 | 149 | } |
@@ -66,22 +66,22 @@ discard block |
||
66 | 66 | public function getModelSchemaForFields(array $model_fields, array $schema) |
67 | 67 | { |
68 | 68 | foreach ($model_fields as $field => $model_field) { |
69 | - if (! $model_field instanceof EE_Model_Field_Base) { |
|
69 | + if ( ! $model_field instanceof EE_Model_Field_Base) { |
|
70 | 70 | continue; |
71 | 71 | } |
72 | - $schema['properties'][ $field ] = $model_field->getSchema(); |
|
72 | + $schema['properties'][$field] = $model_field->getSchema(); |
|
73 | 73 | |
74 | 74 | // if this is a primary key field add the primary key item |
75 | 75 | if ($model_field instanceof EE_Primary_Key_Field_Base) { |
76 | - $schema['properties'][ $field ]['primary_key'] = true; |
|
76 | + $schema['properties'][$field]['primary_key'] = true; |
|
77 | 77 | if ($model_field instanceof EE_Primary_Key_Int_Field) { |
78 | - $schema['properties'][ $field ]['readonly'] = true; |
|
78 | + $schema['properties'][$field]['readonly'] = true; |
|
79 | 79 | } |
80 | 80 | } |
81 | 81 | |
82 | 82 | // if this is a foreign key field add the foreign key item |
83 | 83 | if ($model_field instanceof EE_Foreign_Key_Field_Base) { |
84 | - $schema['properties'][ $field ]['foreign_key'] = array( |
|
84 | + $schema['properties'][$field]['foreign_key'] = array( |
|
85 | 85 | 'description' => esc_html__( |
86 | 86 | 'This is a foreign key the points to the given models.', |
87 | 87 | 'event_espresso' |
@@ -104,14 +104,14 @@ discard block |
||
104 | 104 | public function getModelSchemaForRelations(array $relations_on_model, array $schema) |
105 | 105 | { |
106 | 106 | foreach ($relations_on_model as $model_name => $relation) { |
107 | - if (! $relation instanceof EE_Model_Relation_Base) { |
|
107 | + if ( ! $relation instanceof EE_Model_Relation_Base) { |
|
108 | 108 | continue; |
109 | 109 | } |
110 | 110 | $model_name_for_schema = $relation instanceof EE_Belongs_To_Relation |
111 | 111 | ? strtolower($model_name) |
112 | 112 | : EEH_Inflector::pluralize_and_lower($model_name); |
113 | - $schema['properties'][ $model_name_for_schema ] = $relation->getSchema(); |
|
114 | - $schema['properties'][ $model_name_for_schema ]['relation_model'] = $model_name; |
|
113 | + $schema['properties'][$model_name_for_schema] = $relation->getSchema(); |
|
114 | + $schema['properties'][$model_name_for_schema]['relation_model'] = $model_name; |
|
115 | 115 | } |
116 | 116 | return $schema; |
117 | 117 | } |
@@ -90,6 +90,10 @@ |
||
90 | 90 | return \EEM_Attendee::instance()->count(array('caps' => \EEM_Base::caps_read_admin)); |
91 | 91 | } |
92 | 92 | |
93 | + /** |
|
94 | + * @param integer $offset |
|
95 | + * @param integer $limit |
|
96 | + */ |
|
93 | 97 | public function get_csv_data($offset, $limit) |
94 | 98 | { |
95 | 99 | $attendee_rows = \EEM_Attendee::instance()->get_all_wpdb_results( |
@@ -19,106 +19,106 @@ |
||
19 | 19 | class AttendeesReport extends JobHandlerFile |
20 | 20 | { |
21 | 21 | |
22 | - // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
23 | - public function create_job(JobParameters $job_parameters) |
|
24 | - { |
|
25 | - if (! \EE_Capabilities::instance()->current_user_can('ee_read_contacts', 'generating_report')) { |
|
26 | - throw new BatchRequestException( |
|
27 | - __('You do not have permission to view contacts', 'event_espresso') |
|
28 | - ); |
|
29 | - } |
|
30 | - $filepath = $this->create_file_from_job_with_name( |
|
31 | - $job_parameters->job_id(), |
|
32 | - __('contact-list-report.csv', 'event_espresso') |
|
33 | - ); |
|
34 | - $job_parameters->add_extra_data('filepath', $filepath); |
|
35 | - $job_parameters->set_job_size($this->count_units_to_process()); |
|
36 | - // we should also set the header columns |
|
37 | - $csv_data_for_row = $this->get_csv_data(0, 1); |
|
38 | - \EEH_Export::write_data_array_to_csv($filepath, $csv_data_for_row, true); |
|
39 | - // if we actually processed a row there, record it |
|
40 | - if ($job_parameters->job_size()) { |
|
41 | - $job_parameters->mark_processed(1); |
|
42 | - } |
|
43 | - return new JobStepResponse( |
|
44 | - $job_parameters, |
|
45 | - __('Contacts report started successfully...', 'event_espresso') |
|
46 | - ); |
|
47 | - } |
|
22 | + // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
23 | + public function create_job(JobParameters $job_parameters) |
|
24 | + { |
|
25 | + if (! \EE_Capabilities::instance()->current_user_can('ee_read_contacts', 'generating_report')) { |
|
26 | + throw new BatchRequestException( |
|
27 | + __('You do not have permission to view contacts', 'event_espresso') |
|
28 | + ); |
|
29 | + } |
|
30 | + $filepath = $this->create_file_from_job_with_name( |
|
31 | + $job_parameters->job_id(), |
|
32 | + __('contact-list-report.csv', 'event_espresso') |
|
33 | + ); |
|
34 | + $job_parameters->add_extra_data('filepath', $filepath); |
|
35 | + $job_parameters->set_job_size($this->count_units_to_process()); |
|
36 | + // we should also set the header columns |
|
37 | + $csv_data_for_row = $this->get_csv_data(0, 1); |
|
38 | + \EEH_Export::write_data_array_to_csv($filepath, $csv_data_for_row, true); |
|
39 | + // if we actually processed a row there, record it |
|
40 | + if ($job_parameters->job_size()) { |
|
41 | + $job_parameters->mark_processed(1); |
|
42 | + } |
|
43 | + return new JobStepResponse( |
|
44 | + $job_parameters, |
|
45 | + __('Contacts report started successfully...', 'event_espresso') |
|
46 | + ); |
|
47 | + } |
|
48 | 48 | |
49 | 49 | |
50 | - public function continue_job(JobParameters $job_parameters, $batch_size = 50) |
|
51 | - { |
|
52 | - $csv_data = $this->get_csv_data($job_parameters->units_processed(), $batch_size); |
|
53 | - \EEH_Export::write_data_array_to_csv( |
|
54 | - $job_parameters->extra_datum('filepath'), |
|
55 | - $csv_data, |
|
56 | - false |
|
57 | - ); |
|
58 | - $units_processed = count($csv_data); |
|
59 | - $job_parameters->mark_processed($units_processed); |
|
60 | - $extra_response_data = array( |
|
61 | - 'file_url' => '', |
|
62 | - ); |
|
63 | - if ($units_processed < $batch_size) { |
|
64 | - $job_parameters->set_status(JobParameters::status_complete); |
|
65 | - $extra_response_data['file_url'] = $this->get_url_to_file($job_parameters->extra_datum('filepath')); |
|
66 | - } |
|
67 | - return new JobStepResponse( |
|
68 | - $job_parameters, |
|
69 | - sprintf( |
|
70 | - __('Wrote %1$s rows to report CSV file...', 'event_espresso'), |
|
71 | - count($csv_data) |
|
72 | - ), |
|
73 | - $extra_response_data |
|
74 | - ); |
|
75 | - } |
|
50 | + public function continue_job(JobParameters $job_parameters, $batch_size = 50) |
|
51 | + { |
|
52 | + $csv_data = $this->get_csv_data($job_parameters->units_processed(), $batch_size); |
|
53 | + \EEH_Export::write_data_array_to_csv( |
|
54 | + $job_parameters->extra_datum('filepath'), |
|
55 | + $csv_data, |
|
56 | + false |
|
57 | + ); |
|
58 | + $units_processed = count($csv_data); |
|
59 | + $job_parameters->mark_processed($units_processed); |
|
60 | + $extra_response_data = array( |
|
61 | + 'file_url' => '', |
|
62 | + ); |
|
63 | + if ($units_processed < $batch_size) { |
|
64 | + $job_parameters->set_status(JobParameters::status_complete); |
|
65 | + $extra_response_data['file_url'] = $this->get_url_to_file($job_parameters->extra_datum('filepath')); |
|
66 | + } |
|
67 | + return new JobStepResponse( |
|
68 | + $job_parameters, |
|
69 | + sprintf( |
|
70 | + __('Wrote %1$s rows to report CSV file...', 'event_espresso'), |
|
71 | + count($csv_data) |
|
72 | + ), |
|
73 | + $extra_response_data |
|
74 | + ); |
|
75 | + } |
|
76 | 76 | |
77 | 77 | |
78 | - public function cleanup_job(JobParameters $job_parameters) |
|
79 | - { |
|
80 | - $this->_file_helper->delete( |
|
81 | - \EEH_File::remove_filename_from_filepath($job_parameters->extra_datum('filepath')), |
|
82 | - true, |
|
83 | - 'd' |
|
84 | - ); |
|
85 | - return new JobStepResponse($job_parameters, __('Cleaned up temporary file', 'event_espresso')); |
|
86 | - } |
|
78 | + public function cleanup_job(JobParameters $job_parameters) |
|
79 | + { |
|
80 | + $this->_file_helper->delete( |
|
81 | + \EEH_File::remove_filename_from_filepath($job_parameters->extra_datum('filepath')), |
|
82 | + true, |
|
83 | + 'd' |
|
84 | + ); |
|
85 | + return new JobStepResponse($job_parameters, __('Cleaned up temporary file', 'event_espresso')); |
|
86 | + } |
|
87 | 87 | |
88 | - public function count_units_to_process() |
|
89 | - { |
|
90 | - return \EEM_Attendee::instance()->count(array('caps' => \EEM_Base::caps_read_admin)); |
|
91 | - } |
|
88 | + public function count_units_to_process() |
|
89 | + { |
|
90 | + return \EEM_Attendee::instance()->count(array('caps' => \EEM_Base::caps_read_admin)); |
|
91 | + } |
|
92 | 92 | |
93 | - public function get_csv_data($offset, $limit) |
|
94 | - { |
|
95 | - $attendee_rows = \EEM_Attendee::instance()->get_all_wpdb_results( |
|
96 | - array( |
|
97 | - 'limit' => array($offset, $limit), |
|
98 | - 'force_join' => array('State', 'Country'), |
|
99 | - 'caps' => \EEM_Base::caps_read_admin, |
|
100 | - ) |
|
101 | - ); |
|
102 | - $csv_data = array(); |
|
103 | - foreach ($attendee_rows as $attendee_row) { |
|
104 | - $csv_row = array(); |
|
105 | - foreach (\EEM_Attendee::instance()->field_settings() as $field_name => $field_obj) { |
|
106 | - if ($field_name == 'STA_ID') { |
|
107 | - $state_name_field = \EEM_State::instance()->field_settings_for('STA_name'); |
|
108 | - $csv_row[ __('State', 'event_espresso') ] = $attendee_row[ $state_name_field->get_qualified_column() ]; |
|
109 | - } elseif ($field_name == 'CNT_ISO') { |
|
110 | - $country_name_field = \EEM_Country::instance()->field_settings_for('CNT_name'); |
|
111 | - $csv_row[ __('Country', 'event_espresso') ] = $attendee_row[ $country_name_field->get_qualified_column() ]; |
|
112 | - } else { |
|
113 | - $csv_row[ wp_specialchars_decode($field_obj->get_nicename(), ENT_QUOTES) ] = $attendee_row[ $field_obj->get_qualified_column() ]; |
|
114 | - } |
|
115 | - } |
|
116 | - $csv_data[] = apply_filters( |
|
117 | - 'FHEE___EventEspresso_core_libraries_batch_JobHandlers_AttendeesReport__get_csv_data__row', |
|
118 | - $csv_row, |
|
119 | - $attendee_row |
|
120 | - ); |
|
121 | - } |
|
122 | - return $csv_data; |
|
123 | - } |
|
93 | + public function get_csv_data($offset, $limit) |
|
94 | + { |
|
95 | + $attendee_rows = \EEM_Attendee::instance()->get_all_wpdb_results( |
|
96 | + array( |
|
97 | + 'limit' => array($offset, $limit), |
|
98 | + 'force_join' => array('State', 'Country'), |
|
99 | + 'caps' => \EEM_Base::caps_read_admin, |
|
100 | + ) |
|
101 | + ); |
|
102 | + $csv_data = array(); |
|
103 | + foreach ($attendee_rows as $attendee_row) { |
|
104 | + $csv_row = array(); |
|
105 | + foreach (\EEM_Attendee::instance()->field_settings() as $field_name => $field_obj) { |
|
106 | + if ($field_name == 'STA_ID') { |
|
107 | + $state_name_field = \EEM_State::instance()->field_settings_for('STA_name'); |
|
108 | + $csv_row[ __('State', 'event_espresso') ] = $attendee_row[ $state_name_field->get_qualified_column() ]; |
|
109 | + } elseif ($field_name == 'CNT_ISO') { |
|
110 | + $country_name_field = \EEM_Country::instance()->field_settings_for('CNT_name'); |
|
111 | + $csv_row[ __('Country', 'event_espresso') ] = $attendee_row[ $country_name_field->get_qualified_column() ]; |
|
112 | + } else { |
|
113 | + $csv_row[ wp_specialchars_decode($field_obj->get_nicename(), ENT_QUOTES) ] = $attendee_row[ $field_obj->get_qualified_column() ]; |
|
114 | + } |
|
115 | + } |
|
116 | + $csv_data[] = apply_filters( |
|
117 | + 'FHEE___EventEspresso_core_libraries_batch_JobHandlers_AttendeesReport__get_csv_data__row', |
|
118 | + $csv_row, |
|
119 | + $attendee_row |
|
120 | + ); |
|
121 | + } |
|
122 | + return $csv_data; |
|
123 | + } |
|
124 | 124 | } |
@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
23 | 23 | public function create_job(JobParameters $job_parameters) |
24 | 24 | { |
25 | - if (! \EE_Capabilities::instance()->current_user_can('ee_read_contacts', 'generating_report')) { |
|
25 | + if ( ! \EE_Capabilities::instance()->current_user_can('ee_read_contacts', 'generating_report')) { |
|
26 | 26 | throw new BatchRequestException( |
27 | 27 | __('You do not have permission to view contacts', 'event_espresso') |
28 | 28 | ); |
@@ -105,12 +105,12 @@ discard block |
||
105 | 105 | foreach (\EEM_Attendee::instance()->field_settings() as $field_name => $field_obj) { |
106 | 106 | if ($field_name == 'STA_ID') { |
107 | 107 | $state_name_field = \EEM_State::instance()->field_settings_for('STA_name'); |
108 | - $csv_row[ __('State', 'event_espresso') ] = $attendee_row[ $state_name_field->get_qualified_column() ]; |
|
108 | + $csv_row[__('State', 'event_espresso')] = $attendee_row[$state_name_field->get_qualified_column()]; |
|
109 | 109 | } elseif ($field_name == 'CNT_ISO') { |
110 | 110 | $country_name_field = \EEM_Country::instance()->field_settings_for('CNT_name'); |
111 | - $csv_row[ __('Country', 'event_espresso') ] = $attendee_row[ $country_name_field->get_qualified_column() ]; |
|
111 | + $csv_row[__('Country', 'event_espresso')] = $attendee_row[$country_name_field->get_qualified_column()]; |
|
112 | 112 | } else { |
113 | - $csv_row[ wp_specialchars_decode($field_obj->get_nicename(), ENT_QUOTES) ] = $attendee_row[ $field_obj->get_qualified_column() ]; |
|
113 | + $csv_row[wp_specialchars_decode($field_obj->get_nicename(), ENT_QUOTES)] = $attendee_row[$field_obj->get_qualified_column()]; |
|
114 | 114 | } |
115 | 115 | } |
116 | 116 | $csv_data[] = apply_filters( |
@@ -356,7 +356,7 @@ discard block |
||
356 | 356 | * Sets sensitive_data_removal_strategy |
357 | 357 | * |
358 | 358 | * @param EE_Sensitive_Data_Removal_Base $sensitive_data_removal_strategy |
359 | - * @return boolean |
|
359 | + * @return boolean|null |
|
360 | 360 | */ |
361 | 361 | public function set_sensitive_data_removal_strategy($sensitive_data_removal_strategy) |
362 | 362 | { |
@@ -476,7 +476,7 @@ discard block |
||
476 | 476 | /** |
477 | 477 | * returns true if input employs any of the validation strategy defined by the supplied array of classnames |
478 | 478 | * |
479 | - * @param array $validation_strategy_classnames |
|
479 | + * @param string[] $validation_strategy_classnames |
|
480 | 480 | * @return bool |
481 | 481 | */ |
482 | 482 | public function has_validation_strategy($validation_strategy_classnames) |
@@ -11,1240 +11,1240 @@ |
||
11 | 11 | abstract class EE_Form_Input_Base extends EE_Form_Section_Validatable |
12 | 12 | { |
13 | 13 | |
14 | - /** |
|
15 | - * the input's name attribute |
|
16 | - * |
|
17 | - * @var string |
|
18 | - */ |
|
19 | - protected $_html_name; |
|
20 | - |
|
21 | - /** |
|
22 | - * id for the html label tag |
|
23 | - * |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - protected $_html_label_id; |
|
27 | - |
|
28 | - /** |
|
29 | - * class for teh html label tag |
|
30 | - * |
|
31 | - * @var string |
|
32 | - */ |
|
33 | - protected $_html_label_class; |
|
34 | - |
|
35 | - /** |
|
36 | - * any additional html attributes that you may want to add |
|
37 | - * |
|
38 | - * @var string |
|
39 | - */ |
|
40 | - protected $_html_other_attributes; |
|
41 | - |
|
42 | - /** |
|
43 | - * style for teh html label tag |
|
44 | - * |
|
45 | - * @var string |
|
46 | - */ |
|
47 | - protected $_html_label_style; |
|
48 | - |
|
49 | - /** |
|
50 | - * text to be placed in the html label |
|
51 | - * |
|
52 | - * @var string |
|
53 | - */ |
|
54 | - protected $_html_label_text; |
|
55 | - |
|
56 | - /** |
|
57 | - * the full html label. If used, all other html_label_* properties are invalid |
|
58 | - * |
|
59 | - * @var string |
|
60 | - */ |
|
61 | - protected $_html_label; |
|
62 | - |
|
63 | - /** |
|
64 | - * HTML to use for help text (normally placed below form input), in a span which normally |
|
65 | - * has a class of 'description' |
|
66 | - * |
|
67 | - * @var string |
|
68 | - */ |
|
69 | - protected $_html_help_text; |
|
70 | - |
|
71 | - /** |
|
72 | - * CSS classes for displaying the help span |
|
73 | - * |
|
74 | - * @var string |
|
75 | - */ |
|
76 | - protected $_html_help_class = 'description'; |
|
77 | - |
|
78 | - /** |
|
79 | - * CSS to put in the style attribute on the help span |
|
80 | - * |
|
81 | - * @var string |
|
82 | - */ |
|
83 | - protected $_html_help_style; |
|
84 | - |
|
85 | - /** |
|
86 | - * Stores whether or not this input's response is required. |
|
87 | - * Because certain styling elements may also want to know that this |
|
88 | - * input is required etc. |
|
89 | - * |
|
90 | - * @var boolean |
|
91 | - */ |
|
92 | - protected $_required; |
|
93 | - |
|
94 | - /** |
|
95 | - * css class added to required inputs |
|
96 | - * |
|
97 | - * @var string |
|
98 | - */ |
|
99 | - protected $_required_css_class = 'ee-required'; |
|
100 | - |
|
101 | - /** |
|
102 | - * css styles applied to button type inputs |
|
103 | - * |
|
104 | - * @var string |
|
105 | - */ |
|
106 | - protected $_button_css_attributes; |
|
107 | - |
|
108 | - /** |
|
109 | - * The raw data submitted for this, like in the $_POST super global. |
|
110 | - * Generally unsafe for usage in client code |
|
111 | - * |
|
112 | - * @var mixed string or array |
|
113 | - */ |
|
114 | - protected $_raw_value; |
|
115 | - |
|
116 | - /** |
|
117 | - * Value normalized according to the input's normalization strategy. |
|
118 | - * The normalization strategy dictates whether this is a string, int, float, |
|
119 | - * boolean, or array of any of those. |
|
120 | - * |
|
121 | - * @var mixed |
|
122 | - */ |
|
123 | - protected $_normalized_value; |
|
124 | - |
|
125 | - |
|
126 | - /** |
|
127 | - * Normalized default value either initially set on the input, or provided by calling |
|
128 | - * set_default(). |
|
129 | - * @var mixed |
|
130 | - */ |
|
131 | - protected $_default; |
|
132 | - |
|
133 | - /** |
|
134 | - * Strategy used for displaying this field. |
|
135 | - * Child classes must use _get_display_strategy to access it. |
|
136 | - * |
|
137 | - * @var EE_Display_Strategy_Base |
|
138 | - */ |
|
139 | - private $_display_strategy; |
|
140 | - |
|
141 | - /** |
|
142 | - * Gets all the validation strategies used on this field |
|
143 | - * |
|
144 | - * @var EE_Validation_Strategy_Base[] |
|
145 | - */ |
|
146 | - private $_validation_strategies = array(); |
|
147 | - |
|
148 | - /** |
|
149 | - * The normalization strategy for this field |
|
150 | - * |
|
151 | - * @var EE_Normalization_Strategy_Base |
|
152 | - */ |
|
153 | - private $_normalization_strategy; |
|
154 | - |
|
155 | - /** |
|
156 | - * Strategy for removing sensitive data after we're done with the form input |
|
157 | - * |
|
158 | - * @var EE_Sensitive_Data_Removal_Base |
|
159 | - */ |
|
160 | - protected $_sensitive_data_removal_strategy; |
|
161 | - |
|
162 | - /** |
|
163 | - * Whether this input has been disabled or not. |
|
164 | - * If it's disabled while rendering, an extra hidden input is added that indicates it has been knowingly disabled. |
|
165 | - * (Client-side code that wants to dynamically disable it must also add this hidden input). |
|
166 | - * When the form is submitted, if the input is disabled in the PHP formsection, then input is ignored. |
|
167 | - * If the input is missing from the $_REQUEST data but the hidden input indicating the input is disabled, then the input is again ignored. |
|
168 | - * |
|
169 | - * @var boolean |
|
170 | - */ |
|
171 | - protected $disabled = false; |
|
172 | - |
|
173 | - |
|
174 | - |
|
175 | - /** |
|
176 | - * @param array $input_args { |
|
177 | - * @type string $html_name the html name for the input |
|
178 | - * @type string $html_label_id the id attribute to give to the html label tag |
|
179 | - * @type string $html_label_class the class attribute to give to the html label tag |
|
180 | - * @type string $html_label_style the style attribute to give ot teh label tag |
|
181 | - * @type string $html_label_text the text to put in the label tag |
|
182 | - * @type string $html_label the full html label. If used, |
|
183 | - * all other html_label_* args are invalid |
|
184 | - * @type string $html_help_text text to put in help element |
|
185 | - * @type string $html_help_style style attribute to give to teh help element |
|
186 | - * @type string $html_help_class class attribute to give to the help element |
|
187 | - * @type string $default default value NORMALIZED (eg, if providing the default |
|
188 | - * for a Yes_No_Input, you should provide TRUE or FALSE, not '1' or '0') |
|
189 | - * @type EE_Display_Strategy_Base $display strategy |
|
190 | - * @type EE_Normalization_Strategy_Base $normalization_strategy |
|
191 | - * @type EE_Validation_Strategy_Base[] $validation_strategies |
|
192 | - * @type boolean $ignore_input special argument which can be used to avoid adding any validation strategies, |
|
193 | - * and sets the normalization strategy to the Null normalization. This is good |
|
194 | - * when you want the input to be totally ignored server-side (like when using |
|
195 | - * React.js form inputs) |
|
196 | - * } |
|
197 | - */ |
|
198 | - public function __construct($input_args = array()) |
|
199 | - { |
|
200 | - $input_args = (array) apply_filters('FHEE__EE_Form_Input_Base___construct__input_args', $input_args, $this); |
|
201 | - // the following properties must be cast as arrays |
|
202 | - if (isset($input_args['validation_strategies'])) { |
|
203 | - foreach ((array) $input_args['validation_strategies'] as $validation_strategy) { |
|
204 | - if ($validation_strategy instanceof EE_Validation_Strategy_Base && empty($input_args['ignore_input'])) { |
|
205 | - $this->_validation_strategies[ get_class($validation_strategy) ] = $validation_strategy; |
|
206 | - } |
|
207 | - } |
|
208 | - unset($input_args['validation_strategies']); |
|
209 | - } |
|
210 | - if (isset($input_args['ignore_input'])) { |
|
211 | - $this->_validation_strategies = array(); |
|
212 | - } |
|
213 | - // loop thru incoming options |
|
214 | - foreach ($input_args as $key => $value) { |
|
215 | - // add underscore to $key to match property names |
|
216 | - $_key = '_' . $key; |
|
217 | - if (property_exists($this, $_key)) { |
|
218 | - $this->{$_key} = $value; |
|
219 | - } |
|
220 | - } |
|
221 | - // ensure that "required" is set correctly |
|
222 | - $this->set_required( |
|
223 | - $this->_required, |
|
224 | - isset($input_args['required_validation_error_message']) |
|
225 | - ? $input_args['required_validation_error_message'] |
|
226 | - : null |
|
227 | - ); |
|
228 | - // $this->_html_name_specified = isset( $input_args['html_name'] ) ? TRUE : FALSE; |
|
229 | - $this->_display_strategy->_construct_finalize($this); |
|
230 | - foreach ($this->_validation_strategies as $validation_strategy) { |
|
231 | - $validation_strategy->_construct_finalize($this); |
|
232 | - } |
|
233 | - if (isset($input_args['ignore_input'])) { |
|
234 | - $this->_normalization_strategy = new EE_Null_Normalization(); |
|
235 | - } |
|
236 | - if (! $this->_normalization_strategy) { |
|
237 | - $this->_normalization_strategy = new EE_Text_Normalization(); |
|
238 | - } |
|
239 | - $this->_normalization_strategy->_construct_finalize($this); |
|
240 | - // at least we can use the normalization strategy to populate the default |
|
241 | - if (isset($input_args['default'])) { |
|
242 | - $this->set_default($input_args['default']); |
|
243 | - unset($input_args['default']); |
|
244 | - } |
|
245 | - if (! $this->_sensitive_data_removal_strategy) { |
|
246 | - $this->_sensitive_data_removal_strategy = new EE_No_Sensitive_Data_Removal(); |
|
247 | - } |
|
248 | - $this->_sensitive_data_removal_strategy->_construct_finalize($this); |
|
249 | - parent::__construct($input_args); |
|
250 | - } |
|
251 | - |
|
252 | - |
|
253 | - |
|
254 | - /** |
|
255 | - * Sets the html_name to its default value, if none was specified in teh constructor. |
|
256 | - * Calculation involves using the name and the parent's html_name |
|
257 | - * |
|
258 | - * @throws \EE_Error |
|
259 | - */ |
|
260 | - protected function _set_default_html_name_if_empty() |
|
261 | - { |
|
262 | - if (! $this->_html_name) { |
|
263 | - $this->_html_name = $this->name(); |
|
264 | - if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
|
265 | - $this->_html_name = $this->_parent_section->html_name_prefix() . "[{$this->name()}]"; |
|
266 | - } |
|
267 | - } |
|
268 | - } |
|
269 | - |
|
270 | - |
|
271 | - |
|
272 | - /** |
|
273 | - * @param $parent_form_section |
|
274 | - * @param $name |
|
275 | - * @throws \EE_Error |
|
276 | - */ |
|
277 | - public function _construct_finalize($parent_form_section, $name) |
|
278 | - { |
|
279 | - parent::_construct_finalize($parent_form_section, $name); |
|
280 | - if ($this->_html_label === null && $this->_html_label_text === null) { |
|
281 | - $this->_html_label_text = ucwords(str_replace("_", " ", $name)); |
|
282 | - } |
|
283 | - do_action('AHEE__EE_Form_Input_Base___construct_finalize__end', $this, $parent_form_section, $name); |
|
284 | - } |
|
285 | - |
|
286 | - |
|
287 | - |
|
288 | - /** |
|
289 | - * Returns the strategy for displaying this form input. If none is set, throws an exception. |
|
290 | - * |
|
291 | - * @return EE_Display_Strategy_Base |
|
292 | - * @throws EE_Error |
|
293 | - */ |
|
294 | - protected function _get_display_strategy() |
|
295 | - { |
|
296 | - $this->ensure_construct_finalized_called(); |
|
297 | - if (! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) { |
|
298 | - throw new EE_Error( |
|
299 | - sprintf( |
|
300 | - __( |
|
301 | - "Cannot get display strategy for form input with name %s and id %s, because it has not been set in the constructor", |
|
302 | - "event_espresso" |
|
303 | - ), |
|
304 | - $this->html_name(), |
|
305 | - $this->html_id() |
|
306 | - ) |
|
307 | - ); |
|
308 | - } else { |
|
309 | - return $this->_display_strategy; |
|
310 | - } |
|
311 | - } |
|
312 | - |
|
313 | - |
|
314 | - |
|
315 | - /** |
|
316 | - * Sets the display strategy. |
|
317 | - * |
|
318 | - * @param EE_Display_Strategy_Base $strategy |
|
319 | - */ |
|
320 | - protected function _set_display_strategy(EE_Display_Strategy_Base $strategy) |
|
321 | - { |
|
322 | - $this->_display_strategy = $strategy; |
|
323 | - } |
|
324 | - |
|
325 | - |
|
326 | - |
|
327 | - /** |
|
328 | - * Sets the sanitization strategy |
|
329 | - * |
|
330 | - * @param EE_Normalization_Strategy_Base $strategy |
|
331 | - */ |
|
332 | - protected function _set_normalization_strategy(EE_Normalization_Strategy_Base $strategy) |
|
333 | - { |
|
334 | - $this->_normalization_strategy = $strategy; |
|
335 | - } |
|
336 | - |
|
337 | - |
|
338 | - |
|
339 | - /** |
|
340 | - * Gets sensitive_data_removal_strategy |
|
341 | - * |
|
342 | - * @return EE_Sensitive_Data_Removal_Base |
|
343 | - */ |
|
344 | - public function get_sensitive_data_removal_strategy() |
|
345 | - { |
|
346 | - return $this->_sensitive_data_removal_strategy; |
|
347 | - } |
|
348 | - |
|
349 | - |
|
350 | - |
|
351 | - /** |
|
352 | - * Sets sensitive_data_removal_strategy |
|
353 | - * |
|
354 | - * @param EE_Sensitive_Data_Removal_Base $sensitive_data_removal_strategy |
|
355 | - * @return boolean |
|
356 | - */ |
|
357 | - public function set_sensitive_data_removal_strategy($sensitive_data_removal_strategy) |
|
358 | - { |
|
359 | - $this->_sensitive_data_removal_strategy = $sensitive_data_removal_strategy; |
|
360 | - } |
|
361 | - |
|
362 | - |
|
363 | - |
|
364 | - /** |
|
365 | - * Gets the display strategy for this input |
|
366 | - * |
|
367 | - * @return EE_Display_Strategy_Base |
|
368 | - */ |
|
369 | - public function get_display_strategy() |
|
370 | - { |
|
371 | - return $this->_display_strategy; |
|
372 | - } |
|
373 | - |
|
374 | - |
|
375 | - |
|
376 | - /** |
|
377 | - * Overwrites the display strategy |
|
378 | - * |
|
379 | - * @param EE_Display_Strategy_Base $display_strategy |
|
380 | - */ |
|
381 | - public function set_display_strategy($display_strategy) |
|
382 | - { |
|
383 | - $this->_display_strategy = $display_strategy; |
|
384 | - $this->_display_strategy->_construct_finalize($this); |
|
385 | - } |
|
386 | - |
|
387 | - |
|
388 | - |
|
389 | - /** |
|
390 | - * Gets the normalization strategy set on this input |
|
391 | - * |
|
392 | - * @return EE_Normalization_Strategy_Base |
|
393 | - */ |
|
394 | - public function get_normalization_strategy() |
|
395 | - { |
|
396 | - return $this->_normalization_strategy; |
|
397 | - } |
|
398 | - |
|
399 | - |
|
400 | - |
|
401 | - /** |
|
402 | - * Overwrites the normalization strategy |
|
403 | - * |
|
404 | - * @param EE_Normalization_Strategy_Base $normalization_strategy |
|
405 | - */ |
|
406 | - public function set_normalization_strategy($normalization_strategy) |
|
407 | - { |
|
408 | - $this->_normalization_strategy = $normalization_strategy; |
|
409 | - $this->_normalization_strategy->_construct_finalize($this); |
|
410 | - } |
|
411 | - |
|
412 | - |
|
413 | - |
|
414 | - /** |
|
415 | - * Returns all teh validation strategies which apply to this field, numerically indexed |
|
416 | - * |
|
417 | - * @return EE_Validation_Strategy_Base[] |
|
418 | - */ |
|
419 | - public function get_validation_strategies() |
|
420 | - { |
|
421 | - return $this->_validation_strategies; |
|
422 | - } |
|
423 | - |
|
424 | - |
|
425 | - |
|
426 | - /** |
|
427 | - * Adds this strategy to the field so it will be used in both JS validation and server-side validation |
|
428 | - * |
|
429 | - * @param EE_Validation_Strategy_Base $validation_strategy |
|
430 | - * @return void |
|
431 | - */ |
|
432 | - protected function _add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
433 | - { |
|
434 | - $validation_strategy->_construct_finalize($this); |
|
435 | - $this->_validation_strategies[] = $validation_strategy; |
|
436 | - } |
|
437 | - |
|
438 | - |
|
439 | - |
|
440 | - /** |
|
441 | - * Adds a new validation strategy onto the form input |
|
442 | - * |
|
443 | - * @param EE_Validation_Strategy_Base $validation_strategy |
|
444 | - * @return void |
|
445 | - */ |
|
446 | - public function add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
447 | - { |
|
448 | - $this->_add_validation_strategy($validation_strategy); |
|
449 | - } |
|
450 | - |
|
451 | - |
|
452 | - |
|
453 | - /** |
|
454 | - * The classname of the validation strategy to remove |
|
455 | - * |
|
456 | - * @param string $validation_strategy_classname |
|
457 | - */ |
|
458 | - public function remove_validation_strategy($validation_strategy_classname) |
|
459 | - { |
|
460 | - foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
461 | - if ($validation_strategy instanceof $validation_strategy_classname |
|
462 | - || is_subclass_of($validation_strategy, $validation_strategy_classname) |
|
463 | - ) { |
|
464 | - unset($this->_validation_strategies[ $key ]); |
|
465 | - } |
|
466 | - } |
|
467 | - } |
|
468 | - |
|
469 | - |
|
470 | - |
|
471 | - /** |
|
472 | - * returns true if input employs any of the validation strategy defined by the supplied array of classnames |
|
473 | - * |
|
474 | - * @param array $validation_strategy_classnames |
|
475 | - * @return bool |
|
476 | - */ |
|
477 | - public function has_validation_strategy($validation_strategy_classnames) |
|
478 | - { |
|
479 | - $validation_strategy_classnames = is_array($validation_strategy_classnames) |
|
480 | - ? $validation_strategy_classnames |
|
481 | - : array($validation_strategy_classnames); |
|
482 | - foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
483 | - if (in_array($key, $validation_strategy_classnames)) { |
|
484 | - return true; |
|
485 | - } |
|
486 | - } |
|
487 | - return false; |
|
488 | - } |
|
489 | - |
|
490 | - |
|
491 | - |
|
492 | - /** |
|
493 | - * Gets the HTML |
|
494 | - * |
|
495 | - * @return string |
|
496 | - */ |
|
497 | - public function get_html() |
|
498 | - { |
|
499 | - return $this->_parent_section->get_html_for_input($this); |
|
500 | - } |
|
501 | - |
|
502 | - |
|
503 | - |
|
504 | - /** |
|
505 | - * Gets the HTML for the input itself (no label or errors) according to the |
|
506 | - * input's display strategy |
|
507 | - * Makes sure the JS and CSS are enqueued for it |
|
508 | - * |
|
509 | - * @return string |
|
510 | - * @throws \EE_Error |
|
511 | - */ |
|
512 | - public function get_html_for_input() |
|
513 | - { |
|
514 | - return $this->_form_html_filter |
|
515 | - ? $this->_form_html_filter->filterHtml( |
|
516 | - $this->_get_display_strategy()->display(), |
|
517 | - $this |
|
518 | - ) |
|
519 | - : $this->_get_display_strategy()->display(); |
|
520 | - } |
|
521 | - |
|
522 | - |
|
523 | - |
|
524 | - /** |
|
525 | - * @return string |
|
526 | - */ |
|
527 | - public function html_other_attributes() |
|
528 | - { |
|
529 | - return ! empty($this->_html_other_attributes) ? ' ' . $this->_html_other_attributes : ''; |
|
530 | - } |
|
531 | - |
|
532 | - |
|
533 | - |
|
534 | - /** |
|
535 | - * @param string $html_other_attributes |
|
536 | - */ |
|
537 | - public function set_html_other_attributes($html_other_attributes) |
|
538 | - { |
|
539 | - $this->_html_other_attributes = $html_other_attributes; |
|
540 | - } |
|
541 | - |
|
542 | - |
|
543 | - |
|
544 | - /** |
|
545 | - * Gets the HTML for displaying the label for this form input |
|
546 | - * according to the form section's layout strategy |
|
547 | - * |
|
548 | - * @return string |
|
549 | - */ |
|
550 | - public function get_html_for_label() |
|
551 | - { |
|
552 | - return $this->_parent_section->get_layout_strategy()->display_label($this); |
|
553 | - } |
|
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 | - /** |
|
571 | - * Gets the HTML for displaying the help text for this form input |
|
572 | - * according to the form section's layout strategy |
|
573 | - * |
|
574 | - * @return string |
|
575 | - */ |
|
576 | - public function get_html_for_help() |
|
577 | - { |
|
578 | - return $this->_parent_section->get_layout_strategy()->display_help_text($this); |
|
579 | - } |
|
580 | - |
|
581 | - |
|
582 | - |
|
583 | - /** |
|
584 | - * Validates the input's sanitized value (assumes _sanitize() has already been called) |
|
585 | - * and returns whether or not the form input's submitted value is value |
|
586 | - * |
|
587 | - * @return boolean |
|
588 | - */ |
|
589 | - protected function _validate() |
|
590 | - { |
|
591 | - if ($this->isDisabled()) { |
|
592 | - return true; |
|
593 | - } |
|
594 | - foreach ($this->_validation_strategies as $validation_strategy) { |
|
595 | - if ($validation_strategy instanceof EE_Validation_Strategy_Base) { |
|
596 | - try { |
|
597 | - $validation_strategy->validate($this->normalized_value()); |
|
598 | - } catch (EE_Validation_Error $e) { |
|
599 | - $this->add_validation_error($e); |
|
600 | - } |
|
601 | - } |
|
602 | - } |
|
603 | - if ($this->get_validation_errors()) { |
|
604 | - return false; |
|
605 | - } else { |
|
606 | - return true; |
|
607 | - } |
|
608 | - } |
|
609 | - |
|
610 | - |
|
611 | - |
|
612 | - /** |
|
613 | - * Performs basic sanitization on this value. But what sanitization can be performed anyways? |
|
614 | - * This value MIGHT be allowed to have tags, so we can't really remove them. |
|
615 | - * |
|
616 | - * @param string $value |
|
617 | - * @return null|string |
|
618 | - */ |
|
619 | - private function _sanitize($value) |
|
620 | - { |
|
621 | - return $value !== null ? stripslashes(html_entity_decode(trim($value))) : null; |
|
622 | - } |
|
623 | - |
|
624 | - |
|
625 | - |
|
626 | - /** |
|
627 | - * Picks out the form value that relates to this form input, |
|
628 | - * and stores it as the sanitized value on the form input, and sets the normalized value. |
|
629 | - * Returns whether or not any validation errors occurred |
|
630 | - * |
|
631 | - * @param array $req_data like $_POST |
|
632 | - * @return boolean whether or not there was an error |
|
633 | - * @throws \EE_Error |
|
634 | - */ |
|
635 | - protected function _normalize($req_data) |
|
636 | - { |
|
637 | - // any existing validation errors don't apply so clear them |
|
638 | - $this->_validation_errors = array(); |
|
639 | - // if the input is disabled, ignore whatever input was sent in |
|
640 | - if ($this->isDisabled()) { |
|
641 | - $this->_set_raw_value(null); |
|
642 | - $this->_set_normalized_value($this->get_default()); |
|
643 | - return false; |
|
644 | - } |
|
645 | - try { |
|
646 | - $raw_input = $this->find_form_data_for_this_section($req_data); |
|
647 | - // super simple sanitization for now |
|
648 | - if (is_array($raw_input)) { |
|
649 | - $raw_value = array(); |
|
650 | - foreach ($raw_input as $key => $value) { |
|
651 | - $raw_value[ $key ] = $this->_sanitize($value); |
|
652 | - } |
|
653 | - $this->_set_raw_value($raw_value); |
|
654 | - } else { |
|
655 | - $this->_set_raw_value($this->_sanitize($raw_input)); |
|
656 | - } |
|
657 | - // we want to mostly leave the input alone in case we need to re-display it to the user |
|
658 | - $this->_set_normalized_value($this->_normalization_strategy->normalize($this->raw_value())); |
|
659 | - return false; |
|
660 | - } catch (EE_Validation_Error $e) { |
|
661 | - $this->add_validation_error($e); |
|
662 | - return true; |
|
663 | - } |
|
664 | - } |
|
665 | - |
|
666 | - |
|
667 | - |
|
668 | - /** |
|
669 | - * @return string |
|
670 | - */ |
|
671 | - public function html_name() |
|
672 | - { |
|
673 | - $this->_set_default_html_name_if_empty(); |
|
674 | - return $this->_html_name; |
|
675 | - } |
|
676 | - |
|
677 | - |
|
678 | - |
|
679 | - /** |
|
680 | - * @return string |
|
681 | - */ |
|
682 | - public function html_label_id() |
|
683 | - { |
|
684 | - return ! empty($this->_html_label_id) ? $this->_html_label_id : $this->html_id() . '-lbl'; |
|
685 | - } |
|
686 | - |
|
687 | - |
|
688 | - |
|
689 | - /** |
|
690 | - * @return string |
|
691 | - */ |
|
692 | - public function html_label_class() |
|
693 | - { |
|
694 | - return $this->_html_label_class; |
|
695 | - } |
|
696 | - |
|
697 | - |
|
698 | - |
|
699 | - /** |
|
700 | - * @return string |
|
701 | - */ |
|
702 | - public function html_label_style() |
|
703 | - { |
|
704 | - return $this->_html_label_style; |
|
705 | - } |
|
706 | - |
|
707 | - |
|
708 | - |
|
709 | - /** |
|
710 | - * @return string |
|
711 | - */ |
|
712 | - public function html_label_text() |
|
713 | - { |
|
714 | - return $this->_html_label_text; |
|
715 | - } |
|
716 | - |
|
717 | - |
|
718 | - |
|
719 | - /** |
|
720 | - * @return string |
|
721 | - */ |
|
722 | - public function html_help_text() |
|
723 | - { |
|
724 | - return $this->_html_help_text; |
|
725 | - } |
|
726 | - |
|
727 | - |
|
728 | - |
|
729 | - /** |
|
730 | - * @return string |
|
731 | - */ |
|
732 | - public function html_help_class() |
|
733 | - { |
|
734 | - return $this->_html_help_class; |
|
735 | - } |
|
736 | - |
|
737 | - |
|
738 | - |
|
739 | - /** |
|
740 | - * @return string |
|
741 | - */ |
|
742 | - public function html_help_style() |
|
743 | - { |
|
744 | - return $this->_html_style; |
|
745 | - } |
|
746 | - |
|
747 | - |
|
748 | - |
|
749 | - /** |
|
750 | - * returns the raw, UNSAFE, input, almost exactly as the user submitted it. |
|
751 | - * Please note that almost all client code should instead use the normalized_value; |
|
752 | - * or possibly raw_value_in_form (which prepares the string for displaying in an HTML attribute on a tag, |
|
753 | - * mostly by escaping quotes) |
|
754 | - * Note, we do not store the exact original value sent in the user's request because |
|
755 | - * it may have malicious content, and we MIGHT want to store the form input in a transient or something... |
|
756 | - * in which case, we would have stored the malicious content to our database. |
|
757 | - * |
|
758 | - * @return string |
|
759 | - */ |
|
760 | - public function raw_value() |
|
761 | - { |
|
762 | - return $this->_raw_value; |
|
763 | - } |
|
764 | - |
|
765 | - |
|
766 | - |
|
767 | - /** |
|
768 | - * Returns a string safe to usage in form inputs when displaying, because |
|
769 | - * it escapes all html entities |
|
770 | - * |
|
771 | - * @return string |
|
772 | - */ |
|
773 | - public function raw_value_in_form() |
|
774 | - { |
|
775 | - return htmlentities($this->raw_value(), ENT_QUOTES, 'UTF-8'); |
|
776 | - } |
|
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 | - /** |
|
794 | - * Returns the normalized value is a presentable way. By default this is just |
|
795 | - * the normalized value by itself, but it can be overridden for when that's not |
|
796 | - * the best thing to display |
|
797 | - * |
|
798 | - * @return string |
|
799 | - */ |
|
800 | - public function pretty_value() |
|
801 | - { |
|
802 | - return $this->_normalized_value; |
|
803 | - } |
|
804 | - |
|
805 | - |
|
806 | - |
|
807 | - /** |
|
808 | - * When generating the JS for the jquery validation rules like<br> |
|
809 | - * <code>$( "#myform" ).validate({ |
|
810 | - * rules: { |
|
811 | - * password: "required", |
|
812 | - * password_again: { |
|
813 | - * equalTo: "#password" |
|
814 | - * } |
|
815 | - * } |
|
816 | - * });</code> |
|
817 | - * if this field had the name 'password_again', it should return |
|
818 | - * <br><code>password_again: { |
|
819 | - * equalTo: "#password" |
|
820 | - * }</code> |
|
821 | - * |
|
822 | - * @return array |
|
823 | - */ |
|
824 | - public function get_jquery_validation_rules() |
|
825 | - { |
|
826 | - $jquery_validation_js = array(); |
|
827 | - $jquery_validation_rules = array(); |
|
828 | - foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
829 | - $jquery_validation_rules = array_replace_recursive( |
|
830 | - $jquery_validation_rules, |
|
831 | - $validation_strategy->get_jquery_validation_rule_array() |
|
832 | - ); |
|
833 | - } |
|
834 | - if (! empty($jquery_validation_rules)) { |
|
835 | - foreach ($this->get_display_strategy()->get_html_input_ids(true) as $html_id_with_pound_sign) { |
|
836 | - $jquery_validation_js[ $html_id_with_pound_sign ] = $jquery_validation_rules; |
|
837 | - } |
|
838 | - } |
|
839 | - return $jquery_validation_js; |
|
840 | - } |
|
841 | - |
|
842 | - |
|
843 | - |
|
844 | - /** |
|
845 | - * Sets the input's default value for use in displaying in the form. Note: value should be |
|
846 | - * normalized (Eg, if providing a default of ra Yes_NO_Input you would provide TRUE or FALSE, not '1' or '0') |
|
847 | - * |
|
848 | - * @param mixed $value |
|
849 | - * @return void |
|
850 | - */ |
|
851 | - public function set_default($value) |
|
852 | - { |
|
853 | - $this->_default = $value; |
|
854 | - $this->_set_normalized_value($value); |
|
855 | - $this->_set_raw_value($value); |
|
856 | - } |
|
857 | - |
|
858 | - |
|
859 | - |
|
860 | - /** |
|
861 | - * Sets the normalized value on this input |
|
862 | - * |
|
863 | - * @param mixed $value |
|
864 | - */ |
|
865 | - protected function _set_normalized_value($value) |
|
866 | - { |
|
867 | - $this->_normalized_value = $value; |
|
868 | - } |
|
869 | - |
|
870 | - |
|
871 | - |
|
872 | - /** |
|
873 | - * Sets the raw value on this input (ie, exactly as the user submitted it) |
|
874 | - * |
|
875 | - * @param mixed $value |
|
876 | - */ |
|
877 | - protected function _set_raw_value($value) |
|
878 | - { |
|
879 | - $this->_raw_value = $this->_normalization_strategy->unnormalize($value); |
|
880 | - } |
|
881 | - |
|
882 | - |
|
883 | - |
|
884 | - /** |
|
885 | - * Sets the HTML label text after it has already been defined |
|
886 | - * |
|
887 | - * @param string $label |
|
888 | - * @return void |
|
889 | - */ |
|
890 | - public function set_html_label_text($label) |
|
891 | - { |
|
892 | - $this->_html_label_text = $label; |
|
893 | - } |
|
894 | - |
|
895 | - |
|
896 | - |
|
897 | - /** |
|
898 | - * Sets whether or not this field is required, and adjusts the validation strategy. |
|
899 | - * If you want to use the EE_Conditionally_Required_Validation_Strategy, |
|
900 | - * please add it as a validation strategy using add_validation_strategy as normal |
|
901 | - * |
|
902 | - * @param boolean $required boolean |
|
903 | - * @param null $required_text |
|
904 | - */ |
|
905 | - public function set_required($required = true, $required_text = null) |
|
906 | - { |
|
907 | - $required = filter_var($required, FILTER_VALIDATE_BOOLEAN); |
|
908 | - // whether $required is a string or a boolean, we want to add a required validation strategy |
|
909 | - if ($required) { |
|
910 | - $this->_add_validation_strategy(new EE_Required_Validation_Strategy($required_text)); |
|
911 | - } else { |
|
912 | - $this->remove_validation_strategy('EE_Required_Validation_Strategy'); |
|
913 | - } |
|
914 | - $this->_required = $required; |
|
915 | - } |
|
916 | - |
|
917 | - |
|
918 | - |
|
919 | - /** |
|
920 | - * Returns whether or not this field is required |
|
921 | - * |
|
922 | - * @return boolean |
|
923 | - */ |
|
924 | - public function required() |
|
925 | - { |
|
926 | - return $this->_required; |
|
927 | - } |
|
928 | - |
|
929 | - |
|
930 | - |
|
931 | - /** |
|
932 | - * @param string $required_css_class |
|
933 | - */ |
|
934 | - public function set_required_css_class($required_css_class) |
|
935 | - { |
|
936 | - $this->_required_css_class = $required_css_class; |
|
937 | - } |
|
938 | - |
|
939 | - |
|
940 | - |
|
941 | - /** |
|
942 | - * @return string |
|
943 | - */ |
|
944 | - public function required_css_class() |
|
945 | - { |
|
946 | - return $this->_required_css_class; |
|
947 | - } |
|
948 | - |
|
949 | - |
|
950 | - |
|
951 | - /** |
|
952 | - * @param bool $add_required |
|
953 | - * @return string |
|
954 | - */ |
|
955 | - public function html_class($add_required = false) |
|
956 | - { |
|
957 | - return $add_required && $this->required() |
|
958 | - ? $this->required_css_class() . ' ' . $this->_html_class |
|
959 | - : $this->_html_class; |
|
960 | - } |
|
961 | - |
|
962 | - |
|
963 | - /** |
|
964 | - * Sets the help text, in case |
|
965 | - * |
|
966 | - * @param string $text |
|
967 | - */ |
|
968 | - public function set_html_help_text($text) |
|
969 | - { |
|
970 | - $this->_html_help_text = $text; |
|
971 | - } |
|
972 | - |
|
973 | - |
|
974 | - |
|
975 | - /** |
|
976 | - * Uses the sensitive data removal strategy to remove the sensitive data from this |
|
977 | - * input. If there is any kind of sensitive data removal on this input, we clear |
|
978 | - * out the raw value completely |
|
979 | - * |
|
980 | - * @return void |
|
981 | - */ |
|
982 | - public function clean_sensitive_data() |
|
983 | - { |
|
984 | - // if we do ANY kind of sensitive data removal on this, then just clear out the raw value |
|
985 | - // if we need more logic than this we'll make a strategy for it |
|
986 | - if ($this->_sensitive_data_removal_strategy |
|
987 | - && ! $this->_sensitive_data_removal_strategy instanceof EE_No_Sensitive_Data_Removal |
|
988 | - ) { |
|
989 | - $this->_set_raw_value(null); |
|
990 | - } |
|
991 | - // and clean the normalized value according to the appropriate strategy |
|
992 | - $this->_set_normalized_value( |
|
993 | - $this->get_sensitive_data_removal_strategy()->remove_sensitive_data( |
|
994 | - $this->_normalized_value |
|
995 | - ) |
|
996 | - ); |
|
997 | - } |
|
998 | - |
|
999 | - |
|
1000 | - |
|
1001 | - /** |
|
1002 | - * @param bool $primary |
|
1003 | - * @param string $button_size |
|
1004 | - * @param string $other_attributes |
|
1005 | - */ |
|
1006 | - public function set_button_css_attributes($primary = true, $button_size = '', $other_attributes = '') |
|
1007 | - { |
|
1008 | - $button_css_attributes = 'button'; |
|
1009 | - $button_css_attributes .= $primary === true ? ' button-primary' : ' button-secondary'; |
|
1010 | - switch ($button_size) { |
|
1011 | - case 'xs': |
|
1012 | - case 'extra-small': |
|
1013 | - $button_css_attributes .= ' button-xs'; |
|
1014 | - break; |
|
1015 | - case 'sm': |
|
1016 | - case 'small': |
|
1017 | - $button_css_attributes .= ' button-sm'; |
|
1018 | - break; |
|
1019 | - case 'lg': |
|
1020 | - case 'large': |
|
1021 | - $button_css_attributes .= ' button-lg'; |
|
1022 | - break; |
|
1023 | - case 'block': |
|
1024 | - $button_css_attributes .= ' button-block'; |
|
1025 | - break; |
|
1026 | - case 'md': |
|
1027 | - case 'medium': |
|
1028 | - default: |
|
1029 | - $button_css_attributes .= ''; |
|
1030 | - } |
|
1031 | - $this->_button_css_attributes .= ! empty($other_attributes) |
|
1032 | - ? $button_css_attributes . ' ' . $other_attributes |
|
1033 | - : $button_css_attributes; |
|
1034 | - } |
|
1035 | - |
|
1036 | - |
|
1037 | - |
|
1038 | - /** |
|
1039 | - * @return string |
|
1040 | - */ |
|
1041 | - public function button_css_attributes() |
|
1042 | - { |
|
1043 | - if (empty($this->_button_css_attributes)) { |
|
1044 | - $this->set_button_css_attributes(); |
|
1045 | - } |
|
1046 | - return $this->_button_css_attributes; |
|
1047 | - } |
|
1048 | - |
|
1049 | - |
|
1050 | - |
|
1051 | - /** |
|
1052 | - * find_form_data_for_this_section |
|
1053 | - * using this section's name and its parents, finds the value of the form data that corresponds to it. |
|
1054 | - * For example, if this form section's HTML name is my_form[subform][form_input_1], |
|
1055 | - * then it's value should be in $_REQUEST at $_REQUEST['my_form']['subform']['form_input_1']. |
|
1056 | - * (If that doesn't exist, we also check for this subsection's name |
|
1057 | - * at the TOP LEVEL of the request data. Eg $_REQUEST['form_input_1'].) |
|
1058 | - * This function finds its value in the form. |
|
1059 | - * |
|
1060 | - * @param array $req_data |
|
1061 | - * @return mixed whatever the raw value of this form section is in the request data |
|
1062 | - * @throws \EE_Error |
|
1063 | - */ |
|
1064 | - public function find_form_data_for_this_section($req_data) |
|
1065 | - { |
|
1066 | - $name_parts = $this->getInputNameParts(); |
|
1067 | - // now get the value for the input |
|
1068 | - $value = $this->findRequestForSectionUsingNameParts($name_parts, $req_data); |
|
1069 | - // check if this thing's name is at the TOP level of the request data |
|
1070 | - if ($value === null && isset($req_data[ $this->name() ])) { |
|
1071 | - $value = $req_data[ $this->name() ]; |
|
1072 | - } |
|
1073 | - return $value; |
|
1074 | - } |
|
1075 | - |
|
1076 | - |
|
1077 | - |
|
1078 | - /** |
|
1079 | - * If this input's name is something like "foo[bar][baz]" |
|
1080 | - * returns an array like `array('foo','bar',baz')` |
|
1081 | - * @return array |
|
1082 | - */ |
|
1083 | - protected function getInputNameParts() |
|
1084 | - { |
|
1085 | - // break up the html name by "[]" |
|
1086 | - if (strpos($this->html_name(), '[') !== false) { |
|
1087 | - $before_any_brackets = substr($this->html_name(), 0, strpos($this->html_name(), '[')); |
|
1088 | - } else { |
|
1089 | - $before_any_brackets = $this->html_name(); |
|
1090 | - } |
|
1091 | - // grab all of the segments |
|
1092 | - preg_match_all('~\[([^]]*)\]~', $this->html_name(), $matches); |
|
1093 | - if (isset($matches[1]) && is_array($matches[1])) { |
|
1094 | - $name_parts = $matches[1]; |
|
1095 | - array_unshift($name_parts, $before_any_brackets); |
|
1096 | - } else { |
|
1097 | - $name_parts = array($before_any_brackets); |
|
1098 | - } |
|
1099 | - return $name_parts; |
|
1100 | - } |
|
1101 | - |
|
1102 | - |
|
1103 | - |
|
1104 | - /** |
|
1105 | - * @param array $html_name_parts |
|
1106 | - * @param array $req_data |
|
1107 | - * @return array | NULL |
|
1108 | - */ |
|
1109 | - public function findRequestForSectionUsingNameParts($html_name_parts, $req_data) |
|
1110 | - { |
|
1111 | - $first_part_to_consider = array_shift($html_name_parts); |
|
1112 | - if (isset($req_data[ $first_part_to_consider ])) { |
|
1113 | - if (empty($html_name_parts)) { |
|
1114 | - return $req_data[ $first_part_to_consider ]; |
|
1115 | - } else { |
|
1116 | - return $this->findRequestForSectionUsingNameParts( |
|
1117 | - $html_name_parts, |
|
1118 | - $req_data[ $first_part_to_consider ] |
|
1119 | - ); |
|
1120 | - } |
|
1121 | - } else { |
|
1122 | - return null; |
|
1123 | - } |
|
1124 | - } |
|
1125 | - |
|
1126 | - |
|
1127 | - |
|
1128 | - /** |
|
1129 | - * Checks if this form input's data is in the request data |
|
1130 | - * |
|
1131 | - * @param array $req_data like $_POST |
|
1132 | - * @return boolean |
|
1133 | - * @throws \EE_Error |
|
1134 | - */ |
|
1135 | - public function form_data_present_in($req_data = null) |
|
1136 | - { |
|
1137 | - if ($req_data === null) { |
|
1138 | - $req_data = $_POST; |
|
1139 | - } |
|
1140 | - $checked_value = $this->find_form_data_for_this_section($req_data); |
|
1141 | - if ($checked_value !== null) { |
|
1142 | - return true; |
|
1143 | - } else { |
|
1144 | - return false; |
|
1145 | - } |
|
1146 | - } |
|
1147 | - |
|
1148 | - |
|
1149 | - |
|
1150 | - /** |
|
1151 | - * Overrides parent to add js data from validation and display strategies |
|
1152 | - * |
|
1153 | - * @param array $form_other_js_data |
|
1154 | - * @return array |
|
1155 | - */ |
|
1156 | - public function get_other_js_data($form_other_js_data = array()) |
|
1157 | - { |
|
1158 | - $form_other_js_data = $this->get_other_js_data_from_strategies($form_other_js_data); |
|
1159 | - return $form_other_js_data; |
|
1160 | - } |
|
1161 | - |
|
1162 | - |
|
1163 | - |
|
1164 | - /** |
|
1165 | - * Gets other JS data for localization from this input's strategies, like |
|
1166 | - * the validation strategies and the display strategy |
|
1167 | - * |
|
1168 | - * @param array $form_other_js_data |
|
1169 | - * @return array |
|
1170 | - */ |
|
1171 | - public function get_other_js_data_from_strategies($form_other_js_data = array()) |
|
1172 | - { |
|
1173 | - $form_other_js_data = $this->get_display_strategy()->get_other_js_data($form_other_js_data); |
|
1174 | - foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
1175 | - $form_other_js_data = $validation_strategy->get_other_js_data($form_other_js_data); |
|
1176 | - } |
|
1177 | - return $form_other_js_data; |
|
1178 | - } |
|
1179 | - |
|
1180 | - |
|
1181 | - |
|
1182 | - /** |
|
1183 | - * Override parent because we want to give our strategies an opportunity to enqueue some js and css |
|
1184 | - * |
|
1185 | - * @return void |
|
1186 | - */ |
|
1187 | - public function enqueue_js() |
|
1188 | - { |
|
1189 | - // ask our display strategy and validation strategies if they have js to enqueue |
|
1190 | - $this->enqueue_js_from_strategies(); |
|
1191 | - } |
|
1192 | - |
|
1193 | - |
|
1194 | - |
|
1195 | - /** |
|
1196 | - * Tells strategies when its ok to enqueue their js and css |
|
1197 | - * |
|
1198 | - * @return void |
|
1199 | - */ |
|
1200 | - public function enqueue_js_from_strategies() |
|
1201 | - { |
|
1202 | - $this->get_display_strategy()->enqueue_js(); |
|
1203 | - foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
1204 | - $validation_strategy->enqueue_js(); |
|
1205 | - } |
|
1206 | - } |
|
1207 | - |
|
1208 | - |
|
1209 | - |
|
1210 | - /** |
|
1211 | - * Gets the default value set on the input (not the current value, which may have been |
|
1212 | - * changed because of a form submission). If no default was set, this us null. |
|
1213 | - * @return mixed |
|
1214 | - */ |
|
1215 | - public function get_default() |
|
1216 | - { |
|
1217 | - return $this->_default; |
|
1218 | - } |
|
1219 | - |
|
1220 | - |
|
1221 | - |
|
1222 | - /** |
|
1223 | - * Makes this input disabled. That means it will have the HTML attribute 'disabled="disabled"', |
|
1224 | - * and server-side if any input was received it will be ignored |
|
1225 | - */ |
|
1226 | - public function disable($disable = true) |
|
1227 | - { |
|
1228 | - $disabled_attribute = ' disabled="disabled"'; |
|
1229 | - $this->disabled = filter_var($disable, FILTER_VALIDATE_BOOLEAN); |
|
1230 | - if ($this->disabled) { |
|
1231 | - if (strpos($this->_other_html_attributes, $disabled_attribute) === false) { |
|
1232 | - $this->_other_html_attributes .= $disabled_attribute; |
|
1233 | - } |
|
1234 | - $this->_set_normalized_value($this->get_default()); |
|
1235 | - } else { |
|
1236 | - $this->_other_html_attributes = str_replace($disabled_attribute, '', $this->_other_html_attributes); |
|
1237 | - } |
|
1238 | - } |
|
1239 | - |
|
1240 | - |
|
1241 | - |
|
1242 | - /** |
|
1243 | - * Returns whether or not this input is currently disabled. |
|
1244 | - * @return bool |
|
1245 | - */ |
|
1246 | - public function isDisabled() |
|
1247 | - { |
|
1248 | - return $this->disabled; |
|
1249 | - } |
|
14 | + /** |
|
15 | + * the input's name attribute |
|
16 | + * |
|
17 | + * @var string |
|
18 | + */ |
|
19 | + protected $_html_name; |
|
20 | + |
|
21 | + /** |
|
22 | + * id for the html label tag |
|
23 | + * |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + protected $_html_label_id; |
|
27 | + |
|
28 | + /** |
|
29 | + * class for teh html label tag |
|
30 | + * |
|
31 | + * @var string |
|
32 | + */ |
|
33 | + protected $_html_label_class; |
|
34 | + |
|
35 | + /** |
|
36 | + * any additional html attributes that you may want to add |
|
37 | + * |
|
38 | + * @var string |
|
39 | + */ |
|
40 | + protected $_html_other_attributes; |
|
41 | + |
|
42 | + /** |
|
43 | + * style for teh html label tag |
|
44 | + * |
|
45 | + * @var string |
|
46 | + */ |
|
47 | + protected $_html_label_style; |
|
48 | + |
|
49 | + /** |
|
50 | + * text to be placed in the html label |
|
51 | + * |
|
52 | + * @var string |
|
53 | + */ |
|
54 | + protected $_html_label_text; |
|
55 | + |
|
56 | + /** |
|
57 | + * the full html label. If used, all other html_label_* properties are invalid |
|
58 | + * |
|
59 | + * @var string |
|
60 | + */ |
|
61 | + protected $_html_label; |
|
62 | + |
|
63 | + /** |
|
64 | + * HTML to use for help text (normally placed below form input), in a span which normally |
|
65 | + * has a class of 'description' |
|
66 | + * |
|
67 | + * @var string |
|
68 | + */ |
|
69 | + protected $_html_help_text; |
|
70 | + |
|
71 | + /** |
|
72 | + * CSS classes for displaying the help span |
|
73 | + * |
|
74 | + * @var string |
|
75 | + */ |
|
76 | + protected $_html_help_class = 'description'; |
|
77 | + |
|
78 | + /** |
|
79 | + * CSS to put in the style attribute on the help span |
|
80 | + * |
|
81 | + * @var string |
|
82 | + */ |
|
83 | + protected $_html_help_style; |
|
84 | + |
|
85 | + /** |
|
86 | + * Stores whether or not this input's response is required. |
|
87 | + * Because certain styling elements may also want to know that this |
|
88 | + * input is required etc. |
|
89 | + * |
|
90 | + * @var boolean |
|
91 | + */ |
|
92 | + protected $_required; |
|
93 | + |
|
94 | + /** |
|
95 | + * css class added to required inputs |
|
96 | + * |
|
97 | + * @var string |
|
98 | + */ |
|
99 | + protected $_required_css_class = 'ee-required'; |
|
100 | + |
|
101 | + /** |
|
102 | + * css styles applied to button type inputs |
|
103 | + * |
|
104 | + * @var string |
|
105 | + */ |
|
106 | + protected $_button_css_attributes; |
|
107 | + |
|
108 | + /** |
|
109 | + * The raw data submitted for this, like in the $_POST super global. |
|
110 | + * Generally unsafe for usage in client code |
|
111 | + * |
|
112 | + * @var mixed string or array |
|
113 | + */ |
|
114 | + protected $_raw_value; |
|
115 | + |
|
116 | + /** |
|
117 | + * Value normalized according to the input's normalization strategy. |
|
118 | + * The normalization strategy dictates whether this is a string, int, float, |
|
119 | + * boolean, or array of any of those. |
|
120 | + * |
|
121 | + * @var mixed |
|
122 | + */ |
|
123 | + protected $_normalized_value; |
|
124 | + |
|
125 | + |
|
126 | + /** |
|
127 | + * Normalized default value either initially set on the input, or provided by calling |
|
128 | + * set_default(). |
|
129 | + * @var mixed |
|
130 | + */ |
|
131 | + protected $_default; |
|
132 | + |
|
133 | + /** |
|
134 | + * Strategy used for displaying this field. |
|
135 | + * Child classes must use _get_display_strategy to access it. |
|
136 | + * |
|
137 | + * @var EE_Display_Strategy_Base |
|
138 | + */ |
|
139 | + private $_display_strategy; |
|
140 | + |
|
141 | + /** |
|
142 | + * Gets all the validation strategies used on this field |
|
143 | + * |
|
144 | + * @var EE_Validation_Strategy_Base[] |
|
145 | + */ |
|
146 | + private $_validation_strategies = array(); |
|
147 | + |
|
148 | + /** |
|
149 | + * The normalization strategy for this field |
|
150 | + * |
|
151 | + * @var EE_Normalization_Strategy_Base |
|
152 | + */ |
|
153 | + private $_normalization_strategy; |
|
154 | + |
|
155 | + /** |
|
156 | + * Strategy for removing sensitive data after we're done with the form input |
|
157 | + * |
|
158 | + * @var EE_Sensitive_Data_Removal_Base |
|
159 | + */ |
|
160 | + protected $_sensitive_data_removal_strategy; |
|
161 | + |
|
162 | + /** |
|
163 | + * Whether this input has been disabled or not. |
|
164 | + * If it's disabled while rendering, an extra hidden input is added that indicates it has been knowingly disabled. |
|
165 | + * (Client-side code that wants to dynamically disable it must also add this hidden input). |
|
166 | + * When the form is submitted, if the input is disabled in the PHP formsection, then input is ignored. |
|
167 | + * If the input is missing from the $_REQUEST data but the hidden input indicating the input is disabled, then the input is again ignored. |
|
168 | + * |
|
169 | + * @var boolean |
|
170 | + */ |
|
171 | + protected $disabled = false; |
|
172 | + |
|
173 | + |
|
174 | + |
|
175 | + /** |
|
176 | + * @param array $input_args { |
|
177 | + * @type string $html_name the html name for the input |
|
178 | + * @type string $html_label_id the id attribute to give to the html label tag |
|
179 | + * @type string $html_label_class the class attribute to give to the html label tag |
|
180 | + * @type string $html_label_style the style attribute to give ot teh label tag |
|
181 | + * @type string $html_label_text the text to put in the label tag |
|
182 | + * @type string $html_label the full html label. If used, |
|
183 | + * all other html_label_* args are invalid |
|
184 | + * @type string $html_help_text text to put in help element |
|
185 | + * @type string $html_help_style style attribute to give to teh help element |
|
186 | + * @type string $html_help_class class attribute to give to the help element |
|
187 | + * @type string $default default value NORMALIZED (eg, if providing the default |
|
188 | + * for a Yes_No_Input, you should provide TRUE or FALSE, not '1' or '0') |
|
189 | + * @type EE_Display_Strategy_Base $display strategy |
|
190 | + * @type EE_Normalization_Strategy_Base $normalization_strategy |
|
191 | + * @type EE_Validation_Strategy_Base[] $validation_strategies |
|
192 | + * @type boolean $ignore_input special argument which can be used to avoid adding any validation strategies, |
|
193 | + * and sets the normalization strategy to the Null normalization. This is good |
|
194 | + * when you want the input to be totally ignored server-side (like when using |
|
195 | + * React.js form inputs) |
|
196 | + * } |
|
197 | + */ |
|
198 | + public function __construct($input_args = array()) |
|
199 | + { |
|
200 | + $input_args = (array) apply_filters('FHEE__EE_Form_Input_Base___construct__input_args', $input_args, $this); |
|
201 | + // the following properties must be cast as arrays |
|
202 | + if (isset($input_args['validation_strategies'])) { |
|
203 | + foreach ((array) $input_args['validation_strategies'] as $validation_strategy) { |
|
204 | + if ($validation_strategy instanceof EE_Validation_Strategy_Base && empty($input_args['ignore_input'])) { |
|
205 | + $this->_validation_strategies[ get_class($validation_strategy) ] = $validation_strategy; |
|
206 | + } |
|
207 | + } |
|
208 | + unset($input_args['validation_strategies']); |
|
209 | + } |
|
210 | + if (isset($input_args['ignore_input'])) { |
|
211 | + $this->_validation_strategies = array(); |
|
212 | + } |
|
213 | + // loop thru incoming options |
|
214 | + foreach ($input_args as $key => $value) { |
|
215 | + // add underscore to $key to match property names |
|
216 | + $_key = '_' . $key; |
|
217 | + if (property_exists($this, $_key)) { |
|
218 | + $this->{$_key} = $value; |
|
219 | + } |
|
220 | + } |
|
221 | + // ensure that "required" is set correctly |
|
222 | + $this->set_required( |
|
223 | + $this->_required, |
|
224 | + isset($input_args['required_validation_error_message']) |
|
225 | + ? $input_args['required_validation_error_message'] |
|
226 | + : null |
|
227 | + ); |
|
228 | + // $this->_html_name_specified = isset( $input_args['html_name'] ) ? TRUE : FALSE; |
|
229 | + $this->_display_strategy->_construct_finalize($this); |
|
230 | + foreach ($this->_validation_strategies as $validation_strategy) { |
|
231 | + $validation_strategy->_construct_finalize($this); |
|
232 | + } |
|
233 | + if (isset($input_args['ignore_input'])) { |
|
234 | + $this->_normalization_strategy = new EE_Null_Normalization(); |
|
235 | + } |
|
236 | + if (! $this->_normalization_strategy) { |
|
237 | + $this->_normalization_strategy = new EE_Text_Normalization(); |
|
238 | + } |
|
239 | + $this->_normalization_strategy->_construct_finalize($this); |
|
240 | + // at least we can use the normalization strategy to populate the default |
|
241 | + if (isset($input_args['default'])) { |
|
242 | + $this->set_default($input_args['default']); |
|
243 | + unset($input_args['default']); |
|
244 | + } |
|
245 | + if (! $this->_sensitive_data_removal_strategy) { |
|
246 | + $this->_sensitive_data_removal_strategy = new EE_No_Sensitive_Data_Removal(); |
|
247 | + } |
|
248 | + $this->_sensitive_data_removal_strategy->_construct_finalize($this); |
|
249 | + parent::__construct($input_args); |
|
250 | + } |
|
251 | + |
|
252 | + |
|
253 | + |
|
254 | + /** |
|
255 | + * Sets the html_name to its default value, if none was specified in teh constructor. |
|
256 | + * Calculation involves using the name and the parent's html_name |
|
257 | + * |
|
258 | + * @throws \EE_Error |
|
259 | + */ |
|
260 | + protected function _set_default_html_name_if_empty() |
|
261 | + { |
|
262 | + if (! $this->_html_name) { |
|
263 | + $this->_html_name = $this->name(); |
|
264 | + if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
|
265 | + $this->_html_name = $this->_parent_section->html_name_prefix() . "[{$this->name()}]"; |
|
266 | + } |
|
267 | + } |
|
268 | + } |
|
269 | + |
|
270 | + |
|
271 | + |
|
272 | + /** |
|
273 | + * @param $parent_form_section |
|
274 | + * @param $name |
|
275 | + * @throws \EE_Error |
|
276 | + */ |
|
277 | + public function _construct_finalize($parent_form_section, $name) |
|
278 | + { |
|
279 | + parent::_construct_finalize($parent_form_section, $name); |
|
280 | + if ($this->_html_label === null && $this->_html_label_text === null) { |
|
281 | + $this->_html_label_text = ucwords(str_replace("_", " ", $name)); |
|
282 | + } |
|
283 | + do_action('AHEE__EE_Form_Input_Base___construct_finalize__end', $this, $parent_form_section, $name); |
|
284 | + } |
|
285 | + |
|
286 | + |
|
287 | + |
|
288 | + /** |
|
289 | + * Returns the strategy for displaying this form input. If none is set, throws an exception. |
|
290 | + * |
|
291 | + * @return EE_Display_Strategy_Base |
|
292 | + * @throws EE_Error |
|
293 | + */ |
|
294 | + protected function _get_display_strategy() |
|
295 | + { |
|
296 | + $this->ensure_construct_finalized_called(); |
|
297 | + if (! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) { |
|
298 | + throw new EE_Error( |
|
299 | + sprintf( |
|
300 | + __( |
|
301 | + "Cannot get display strategy for form input with name %s and id %s, because it has not been set in the constructor", |
|
302 | + "event_espresso" |
|
303 | + ), |
|
304 | + $this->html_name(), |
|
305 | + $this->html_id() |
|
306 | + ) |
|
307 | + ); |
|
308 | + } else { |
|
309 | + return $this->_display_strategy; |
|
310 | + } |
|
311 | + } |
|
312 | + |
|
313 | + |
|
314 | + |
|
315 | + /** |
|
316 | + * Sets the display strategy. |
|
317 | + * |
|
318 | + * @param EE_Display_Strategy_Base $strategy |
|
319 | + */ |
|
320 | + protected function _set_display_strategy(EE_Display_Strategy_Base $strategy) |
|
321 | + { |
|
322 | + $this->_display_strategy = $strategy; |
|
323 | + } |
|
324 | + |
|
325 | + |
|
326 | + |
|
327 | + /** |
|
328 | + * Sets the sanitization strategy |
|
329 | + * |
|
330 | + * @param EE_Normalization_Strategy_Base $strategy |
|
331 | + */ |
|
332 | + protected function _set_normalization_strategy(EE_Normalization_Strategy_Base $strategy) |
|
333 | + { |
|
334 | + $this->_normalization_strategy = $strategy; |
|
335 | + } |
|
336 | + |
|
337 | + |
|
338 | + |
|
339 | + /** |
|
340 | + * Gets sensitive_data_removal_strategy |
|
341 | + * |
|
342 | + * @return EE_Sensitive_Data_Removal_Base |
|
343 | + */ |
|
344 | + public function get_sensitive_data_removal_strategy() |
|
345 | + { |
|
346 | + return $this->_sensitive_data_removal_strategy; |
|
347 | + } |
|
348 | + |
|
349 | + |
|
350 | + |
|
351 | + /** |
|
352 | + * Sets sensitive_data_removal_strategy |
|
353 | + * |
|
354 | + * @param EE_Sensitive_Data_Removal_Base $sensitive_data_removal_strategy |
|
355 | + * @return boolean |
|
356 | + */ |
|
357 | + public function set_sensitive_data_removal_strategy($sensitive_data_removal_strategy) |
|
358 | + { |
|
359 | + $this->_sensitive_data_removal_strategy = $sensitive_data_removal_strategy; |
|
360 | + } |
|
361 | + |
|
362 | + |
|
363 | + |
|
364 | + /** |
|
365 | + * Gets the display strategy for this input |
|
366 | + * |
|
367 | + * @return EE_Display_Strategy_Base |
|
368 | + */ |
|
369 | + public function get_display_strategy() |
|
370 | + { |
|
371 | + return $this->_display_strategy; |
|
372 | + } |
|
373 | + |
|
374 | + |
|
375 | + |
|
376 | + /** |
|
377 | + * Overwrites the display strategy |
|
378 | + * |
|
379 | + * @param EE_Display_Strategy_Base $display_strategy |
|
380 | + */ |
|
381 | + public function set_display_strategy($display_strategy) |
|
382 | + { |
|
383 | + $this->_display_strategy = $display_strategy; |
|
384 | + $this->_display_strategy->_construct_finalize($this); |
|
385 | + } |
|
386 | + |
|
387 | + |
|
388 | + |
|
389 | + /** |
|
390 | + * Gets the normalization strategy set on this input |
|
391 | + * |
|
392 | + * @return EE_Normalization_Strategy_Base |
|
393 | + */ |
|
394 | + public function get_normalization_strategy() |
|
395 | + { |
|
396 | + return $this->_normalization_strategy; |
|
397 | + } |
|
398 | + |
|
399 | + |
|
400 | + |
|
401 | + /** |
|
402 | + * Overwrites the normalization strategy |
|
403 | + * |
|
404 | + * @param EE_Normalization_Strategy_Base $normalization_strategy |
|
405 | + */ |
|
406 | + public function set_normalization_strategy($normalization_strategy) |
|
407 | + { |
|
408 | + $this->_normalization_strategy = $normalization_strategy; |
|
409 | + $this->_normalization_strategy->_construct_finalize($this); |
|
410 | + } |
|
411 | + |
|
412 | + |
|
413 | + |
|
414 | + /** |
|
415 | + * Returns all teh validation strategies which apply to this field, numerically indexed |
|
416 | + * |
|
417 | + * @return EE_Validation_Strategy_Base[] |
|
418 | + */ |
|
419 | + public function get_validation_strategies() |
|
420 | + { |
|
421 | + return $this->_validation_strategies; |
|
422 | + } |
|
423 | + |
|
424 | + |
|
425 | + |
|
426 | + /** |
|
427 | + * Adds this strategy to the field so it will be used in both JS validation and server-side validation |
|
428 | + * |
|
429 | + * @param EE_Validation_Strategy_Base $validation_strategy |
|
430 | + * @return void |
|
431 | + */ |
|
432 | + protected function _add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
433 | + { |
|
434 | + $validation_strategy->_construct_finalize($this); |
|
435 | + $this->_validation_strategies[] = $validation_strategy; |
|
436 | + } |
|
437 | + |
|
438 | + |
|
439 | + |
|
440 | + /** |
|
441 | + * Adds a new validation strategy onto the form input |
|
442 | + * |
|
443 | + * @param EE_Validation_Strategy_Base $validation_strategy |
|
444 | + * @return void |
|
445 | + */ |
|
446 | + public function add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
447 | + { |
|
448 | + $this->_add_validation_strategy($validation_strategy); |
|
449 | + } |
|
450 | + |
|
451 | + |
|
452 | + |
|
453 | + /** |
|
454 | + * The classname of the validation strategy to remove |
|
455 | + * |
|
456 | + * @param string $validation_strategy_classname |
|
457 | + */ |
|
458 | + public function remove_validation_strategy($validation_strategy_classname) |
|
459 | + { |
|
460 | + foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
461 | + if ($validation_strategy instanceof $validation_strategy_classname |
|
462 | + || is_subclass_of($validation_strategy, $validation_strategy_classname) |
|
463 | + ) { |
|
464 | + unset($this->_validation_strategies[ $key ]); |
|
465 | + } |
|
466 | + } |
|
467 | + } |
|
468 | + |
|
469 | + |
|
470 | + |
|
471 | + /** |
|
472 | + * returns true if input employs any of the validation strategy defined by the supplied array of classnames |
|
473 | + * |
|
474 | + * @param array $validation_strategy_classnames |
|
475 | + * @return bool |
|
476 | + */ |
|
477 | + public function has_validation_strategy($validation_strategy_classnames) |
|
478 | + { |
|
479 | + $validation_strategy_classnames = is_array($validation_strategy_classnames) |
|
480 | + ? $validation_strategy_classnames |
|
481 | + : array($validation_strategy_classnames); |
|
482 | + foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
483 | + if (in_array($key, $validation_strategy_classnames)) { |
|
484 | + return true; |
|
485 | + } |
|
486 | + } |
|
487 | + return false; |
|
488 | + } |
|
489 | + |
|
490 | + |
|
491 | + |
|
492 | + /** |
|
493 | + * Gets the HTML |
|
494 | + * |
|
495 | + * @return string |
|
496 | + */ |
|
497 | + public function get_html() |
|
498 | + { |
|
499 | + return $this->_parent_section->get_html_for_input($this); |
|
500 | + } |
|
501 | + |
|
502 | + |
|
503 | + |
|
504 | + /** |
|
505 | + * Gets the HTML for the input itself (no label or errors) according to the |
|
506 | + * input's display strategy |
|
507 | + * Makes sure the JS and CSS are enqueued for it |
|
508 | + * |
|
509 | + * @return string |
|
510 | + * @throws \EE_Error |
|
511 | + */ |
|
512 | + public function get_html_for_input() |
|
513 | + { |
|
514 | + return $this->_form_html_filter |
|
515 | + ? $this->_form_html_filter->filterHtml( |
|
516 | + $this->_get_display_strategy()->display(), |
|
517 | + $this |
|
518 | + ) |
|
519 | + : $this->_get_display_strategy()->display(); |
|
520 | + } |
|
521 | + |
|
522 | + |
|
523 | + |
|
524 | + /** |
|
525 | + * @return string |
|
526 | + */ |
|
527 | + public function html_other_attributes() |
|
528 | + { |
|
529 | + return ! empty($this->_html_other_attributes) ? ' ' . $this->_html_other_attributes : ''; |
|
530 | + } |
|
531 | + |
|
532 | + |
|
533 | + |
|
534 | + /** |
|
535 | + * @param string $html_other_attributes |
|
536 | + */ |
|
537 | + public function set_html_other_attributes($html_other_attributes) |
|
538 | + { |
|
539 | + $this->_html_other_attributes = $html_other_attributes; |
|
540 | + } |
|
541 | + |
|
542 | + |
|
543 | + |
|
544 | + /** |
|
545 | + * Gets the HTML for displaying the label for this form input |
|
546 | + * according to the form section's layout strategy |
|
547 | + * |
|
548 | + * @return string |
|
549 | + */ |
|
550 | + public function get_html_for_label() |
|
551 | + { |
|
552 | + return $this->_parent_section->get_layout_strategy()->display_label($this); |
|
553 | + } |
|
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 | + /** |
|
571 | + * Gets the HTML for displaying the help text for this form input |
|
572 | + * according to the form section's layout strategy |
|
573 | + * |
|
574 | + * @return string |
|
575 | + */ |
|
576 | + public function get_html_for_help() |
|
577 | + { |
|
578 | + return $this->_parent_section->get_layout_strategy()->display_help_text($this); |
|
579 | + } |
|
580 | + |
|
581 | + |
|
582 | + |
|
583 | + /** |
|
584 | + * Validates the input's sanitized value (assumes _sanitize() has already been called) |
|
585 | + * and returns whether or not the form input's submitted value is value |
|
586 | + * |
|
587 | + * @return boolean |
|
588 | + */ |
|
589 | + protected function _validate() |
|
590 | + { |
|
591 | + if ($this->isDisabled()) { |
|
592 | + return true; |
|
593 | + } |
|
594 | + foreach ($this->_validation_strategies as $validation_strategy) { |
|
595 | + if ($validation_strategy instanceof EE_Validation_Strategy_Base) { |
|
596 | + try { |
|
597 | + $validation_strategy->validate($this->normalized_value()); |
|
598 | + } catch (EE_Validation_Error $e) { |
|
599 | + $this->add_validation_error($e); |
|
600 | + } |
|
601 | + } |
|
602 | + } |
|
603 | + if ($this->get_validation_errors()) { |
|
604 | + return false; |
|
605 | + } else { |
|
606 | + return true; |
|
607 | + } |
|
608 | + } |
|
609 | + |
|
610 | + |
|
611 | + |
|
612 | + /** |
|
613 | + * Performs basic sanitization on this value. But what sanitization can be performed anyways? |
|
614 | + * This value MIGHT be allowed to have tags, so we can't really remove them. |
|
615 | + * |
|
616 | + * @param string $value |
|
617 | + * @return null|string |
|
618 | + */ |
|
619 | + private function _sanitize($value) |
|
620 | + { |
|
621 | + return $value !== null ? stripslashes(html_entity_decode(trim($value))) : null; |
|
622 | + } |
|
623 | + |
|
624 | + |
|
625 | + |
|
626 | + /** |
|
627 | + * Picks out the form value that relates to this form input, |
|
628 | + * and stores it as the sanitized value on the form input, and sets the normalized value. |
|
629 | + * Returns whether or not any validation errors occurred |
|
630 | + * |
|
631 | + * @param array $req_data like $_POST |
|
632 | + * @return boolean whether or not there was an error |
|
633 | + * @throws \EE_Error |
|
634 | + */ |
|
635 | + protected function _normalize($req_data) |
|
636 | + { |
|
637 | + // any existing validation errors don't apply so clear them |
|
638 | + $this->_validation_errors = array(); |
|
639 | + // if the input is disabled, ignore whatever input was sent in |
|
640 | + if ($this->isDisabled()) { |
|
641 | + $this->_set_raw_value(null); |
|
642 | + $this->_set_normalized_value($this->get_default()); |
|
643 | + return false; |
|
644 | + } |
|
645 | + try { |
|
646 | + $raw_input = $this->find_form_data_for_this_section($req_data); |
|
647 | + // super simple sanitization for now |
|
648 | + if (is_array($raw_input)) { |
|
649 | + $raw_value = array(); |
|
650 | + foreach ($raw_input as $key => $value) { |
|
651 | + $raw_value[ $key ] = $this->_sanitize($value); |
|
652 | + } |
|
653 | + $this->_set_raw_value($raw_value); |
|
654 | + } else { |
|
655 | + $this->_set_raw_value($this->_sanitize($raw_input)); |
|
656 | + } |
|
657 | + // we want to mostly leave the input alone in case we need to re-display it to the user |
|
658 | + $this->_set_normalized_value($this->_normalization_strategy->normalize($this->raw_value())); |
|
659 | + return false; |
|
660 | + } catch (EE_Validation_Error $e) { |
|
661 | + $this->add_validation_error($e); |
|
662 | + return true; |
|
663 | + } |
|
664 | + } |
|
665 | + |
|
666 | + |
|
667 | + |
|
668 | + /** |
|
669 | + * @return string |
|
670 | + */ |
|
671 | + public function html_name() |
|
672 | + { |
|
673 | + $this->_set_default_html_name_if_empty(); |
|
674 | + return $this->_html_name; |
|
675 | + } |
|
676 | + |
|
677 | + |
|
678 | + |
|
679 | + /** |
|
680 | + * @return string |
|
681 | + */ |
|
682 | + public function html_label_id() |
|
683 | + { |
|
684 | + return ! empty($this->_html_label_id) ? $this->_html_label_id : $this->html_id() . '-lbl'; |
|
685 | + } |
|
686 | + |
|
687 | + |
|
688 | + |
|
689 | + /** |
|
690 | + * @return string |
|
691 | + */ |
|
692 | + public function html_label_class() |
|
693 | + { |
|
694 | + return $this->_html_label_class; |
|
695 | + } |
|
696 | + |
|
697 | + |
|
698 | + |
|
699 | + /** |
|
700 | + * @return string |
|
701 | + */ |
|
702 | + public function html_label_style() |
|
703 | + { |
|
704 | + return $this->_html_label_style; |
|
705 | + } |
|
706 | + |
|
707 | + |
|
708 | + |
|
709 | + /** |
|
710 | + * @return string |
|
711 | + */ |
|
712 | + public function html_label_text() |
|
713 | + { |
|
714 | + return $this->_html_label_text; |
|
715 | + } |
|
716 | + |
|
717 | + |
|
718 | + |
|
719 | + /** |
|
720 | + * @return string |
|
721 | + */ |
|
722 | + public function html_help_text() |
|
723 | + { |
|
724 | + return $this->_html_help_text; |
|
725 | + } |
|
726 | + |
|
727 | + |
|
728 | + |
|
729 | + /** |
|
730 | + * @return string |
|
731 | + */ |
|
732 | + public function html_help_class() |
|
733 | + { |
|
734 | + return $this->_html_help_class; |
|
735 | + } |
|
736 | + |
|
737 | + |
|
738 | + |
|
739 | + /** |
|
740 | + * @return string |
|
741 | + */ |
|
742 | + public function html_help_style() |
|
743 | + { |
|
744 | + return $this->_html_style; |
|
745 | + } |
|
746 | + |
|
747 | + |
|
748 | + |
|
749 | + /** |
|
750 | + * returns the raw, UNSAFE, input, almost exactly as the user submitted it. |
|
751 | + * Please note that almost all client code should instead use the normalized_value; |
|
752 | + * or possibly raw_value_in_form (which prepares the string for displaying in an HTML attribute on a tag, |
|
753 | + * mostly by escaping quotes) |
|
754 | + * Note, we do not store the exact original value sent in the user's request because |
|
755 | + * it may have malicious content, and we MIGHT want to store the form input in a transient or something... |
|
756 | + * in which case, we would have stored the malicious content to our database. |
|
757 | + * |
|
758 | + * @return string |
|
759 | + */ |
|
760 | + public function raw_value() |
|
761 | + { |
|
762 | + return $this->_raw_value; |
|
763 | + } |
|
764 | + |
|
765 | + |
|
766 | + |
|
767 | + /** |
|
768 | + * Returns a string safe to usage in form inputs when displaying, because |
|
769 | + * it escapes all html entities |
|
770 | + * |
|
771 | + * @return string |
|
772 | + */ |
|
773 | + public function raw_value_in_form() |
|
774 | + { |
|
775 | + return htmlentities($this->raw_value(), ENT_QUOTES, 'UTF-8'); |
|
776 | + } |
|
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 | + /** |
|
794 | + * Returns the normalized value is a presentable way. By default this is just |
|
795 | + * the normalized value by itself, but it can be overridden for when that's not |
|
796 | + * the best thing to display |
|
797 | + * |
|
798 | + * @return string |
|
799 | + */ |
|
800 | + public function pretty_value() |
|
801 | + { |
|
802 | + return $this->_normalized_value; |
|
803 | + } |
|
804 | + |
|
805 | + |
|
806 | + |
|
807 | + /** |
|
808 | + * When generating the JS for the jquery validation rules like<br> |
|
809 | + * <code>$( "#myform" ).validate({ |
|
810 | + * rules: { |
|
811 | + * password: "required", |
|
812 | + * password_again: { |
|
813 | + * equalTo: "#password" |
|
814 | + * } |
|
815 | + * } |
|
816 | + * });</code> |
|
817 | + * if this field had the name 'password_again', it should return |
|
818 | + * <br><code>password_again: { |
|
819 | + * equalTo: "#password" |
|
820 | + * }</code> |
|
821 | + * |
|
822 | + * @return array |
|
823 | + */ |
|
824 | + public function get_jquery_validation_rules() |
|
825 | + { |
|
826 | + $jquery_validation_js = array(); |
|
827 | + $jquery_validation_rules = array(); |
|
828 | + foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
829 | + $jquery_validation_rules = array_replace_recursive( |
|
830 | + $jquery_validation_rules, |
|
831 | + $validation_strategy->get_jquery_validation_rule_array() |
|
832 | + ); |
|
833 | + } |
|
834 | + if (! empty($jquery_validation_rules)) { |
|
835 | + foreach ($this->get_display_strategy()->get_html_input_ids(true) as $html_id_with_pound_sign) { |
|
836 | + $jquery_validation_js[ $html_id_with_pound_sign ] = $jquery_validation_rules; |
|
837 | + } |
|
838 | + } |
|
839 | + return $jquery_validation_js; |
|
840 | + } |
|
841 | + |
|
842 | + |
|
843 | + |
|
844 | + /** |
|
845 | + * Sets the input's default value for use in displaying in the form. Note: value should be |
|
846 | + * normalized (Eg, if providing a default of ra Yes_NO_Input you would provide TRUE or FALSE, not '1' or '0') |
|
847 | + * |
|
848 | + * @param mixed $value |
|
849 | + * @return void |
|
850 | + */ |
|
851 | + public function set_default($value) |
|
852 | + { |
|
853 | + $this->_default = $value; |
|
854 | + $this->_set_normalized_value($value); |
|
855 | + $this->_set_raw_value($value); |
|
856 | + } |
|
857 | + |
|
858 | + |
|
859 | + |
|
860 | + /** |
|
861 | + * Sets the normalized value on this input |
|
862 | + * |
|
863 | + * @param mixed $value |
|
864 | + */ |
|
865 | + protected function _set_normalized_value($value) |
|
866 | + { |
|
867 | + $this->_normalized_value = $value; |
|
868 | + } |
|
869 | + |
|
870 | + |
|
871 | + |
|
872 | + /** |
|
873 | + * Sets the raw value on this input (ie, exactly as the user submitted it) |
|
874 | + * |
|
875 | + * @param mixed $value |
|
876 | + */ |
|
877 | + protected function _set_raw_value($value) |
|
878 | + { |
|
879 | + $this->_raw_value = $this->_normalization_strategy->unnormalize($value); |
|
880 | + } |
|
881 | + |
|
882 | + |
|
883 | + |
|
884 | + /** |
|
885 | + * Sets the HTML label text after it has already been defined |
|
886 | + * |
|
887 | + * @param string $label |
|
888 | + * @return void |
|
889 | + */ |
|
890 | + public function set_html_label_text($label) |
|
891 | + { |
|
892 | + $this->_html_label_text = $label; |
|
893 | + } |
|
894 | + |
|
895 | + |
|
896 | + |
|
897 | + /** |
|
898 | + * Sets whether or not this field is required, and adjusts the validation strategy. |
|
899 | + * If you want to use the EE_Conditionally_Required_Validation_Strategy, |
|
900 | + * please add it as a validation strategy using add_validation_strategy as normal |
|
901 | + * |
|
902 | + * @param boolean $required boolean |
|
903 | + * @param null $required_text |
|
904 | + */ |
|
905 | + public function set_required($required = true, $required_text = null) |
|
906 | + { |
|
907 | + $required = filter_var($required, FILTER_VALIDATE_BOOLEAN); |
|
908 | + // whether $required is a string or a boolean, we want to add a required validation strategy |
|
909 | + if ($required) { |
|
910 | + $this->_add_validation_strategy(new EE_Required_Validation_Strategy($required_text)); |
|
911 | + } else { |
|
912 | + $this->remove_validation_strategy('EE_Required_Validation_Strategy'); |
|
913 | + } |
|
914 | + $this->_required = $required; |
|
915 | + } |
|
916 | + |
|
917 | + |
|
918 | + |
|
919 | + /** |
|
920 | + * Returns whether or not this field is required |
|
921 | + * |
|
922 | + * @return boolean |
|
923 | + */ |
|
924 | + public function required() |
|
925 | + { |
|
926 | + return $this->_required; |
|
927 | + } |
|
928 | + |
|
929 | + |
|
930 | + |
|
931 | + /** |
|
932 | + * @param string $required_css_class |
|
933 | + */ |
|
934 | + public function set_required_css_class($required_css_class) |
|
935 | + { |
|
936 | + $this->_required_css_class = $required_css_class; |
|
937 | + } |
|
938 | + |
|
939 | + |
|
940 | + |
|
941 | + /** |
|
942 | + * @return string |
|
943 | + */ |
|
944 | + public function required_css_class() |
|
945 | + { |
|
946 | + return $this->_required_css_class; |
|
947 | + } |
|
948 | + |
|
949 | + |
|
950 | + |
|
951 | + /** |
|
952 | + * @param bool $add_required |
|
953 | + * @return string |
|
954 | + */ |
|
955 | + public function html_class($add_required = false) |
|
956 | + { |
|
957 | + return $add_required && $this->required() |
|
958 | + ? $this->required_css_class() . ' ' . $this->_html_class |
|
959 | + : $this->_html_class; |
|
960 | + } |
|
961 | + |
|
962 | + |
|
963 | + /** |
|
964 | + * Sets the help text, in case |
|
965 | + * |
|
966 | + * @param string $text |
|
967 | + */ |
|
968 | + public function set_html_help_text($text) |
|
969 | + { |
|
970 | + $this->_html_help_text = $text; |
|
971 | + } |
|
972 | + |
|
973 | + |
|
974 | + |
|
975 | + /** |
|
976 | + * Uses the sensitive data removal strategy to remove the sensitive data from this |
|
977 | + * input. If there is any kind of sensitive data removal on this input, we clear |
|
978 | + * out the raw value completely |
|
979 | + * |
|
980 | + * @return void |
|
981 | + */ |
|
982 | + public function clean_sensitive_data() |
|
983 | + { |
|
984 | + // if we do ANY kind of sensitive data removal on this, then just clear out the raw value |
|
985 | + // if we need more logic than this we'll make a strategy for it |
|
986 | + if ($this->_sensitive_data_removal_strategy |
|
987 | + && ! $this->_sensitive_data_removal_strategy instanceof EE_No_Sensitive_Data_Removal |
|
988 | + ) { |
|
989 | + $this->_set_raw_value(null); |
|
990 | + } |
|
991 | + // and clean the normalized value according to the appropriate strategy |
|
992 | + $this->_set_normalized_value( |
|
993 | + $this->get_sensitive_data_removal_strategy()->remove_sensitive_data( |
|
994 | + $this->_normalized_value |
|
995 | + ) |
|
996 | + ); |
|
997 | + } |
|
998 | + |
|
999 | + |
|
1000 | + |
|
1001 | + /** |
|
1002 | + * @param bool $primary |
|
1003 | + * @param string $button_size |
|
1004 | + * @param string $other_attributes |
|
1005 | + */ |
|
1006 | + public function set_button_css_attributes($primary = true, $button_size = '', $other_attributes = '') |
|
1007 | + { |
|
1008 | + $button_css_attributes = 'button'; |
|
1009 | + $button_css_attributes .= $primary === true ? ' button-primary' : ' button-secondary'; |
|
1010 | + switch ($button_size) { |
|
1011 | + case 'xs': |
|
1012 | + case 'extra-small': |
|
1013 | + $button_css_attributes .= ' button-xs'; |
|
1014 | + break; |
|
1015 | + case 'sm': |
|
1016 | + case 'small': |
|
1017 | + $button_css_attributes .= ' button-sm'; |
|
1018 | + break; |
|
1019 | + case 'lg': |
|
1020 | + case 'large': |
|
1021 | + $button_css_attributes .= ' button-lg'; |
|
1022 | + break; |
|
1023 | + case 'block': |
|
1024 | + $button_css_attributes .= ' button-block'; |
|
1025 | + break; |
|
1026 | + case 'md': |
|
1027 | + case 'medium': |
|
1028 | + default: |
|
1029 | + $button_css_attributes .= ''; |
|
1030 | + } |
|
1031 | + $this->_button_css_attributes .= ! empty($other_attributes) |
|
1032 | + ? $button_css_attributes . ' ' . $other_attributes |
|
1033 | + : $button_css_attributes; |
|
1034 | + } |
|
1035 | + |
|
1036 | + |
|
1037 | + |
|
1038 | + /** |
|
1039 | + * @return string |
|
1040 | + */ |
|
1041 | + public function button_css_attributes() |
|
1042 | + { |
|
1043 | + if (empty($this->_button_css_attributes)) { |
|
1044 | + $this->set_button_css_attributes(); |
|
1045 | + } |
|
1046 | + return $this->_button_css_attributes; |
|
1047 | + } |
|
1048 | + |
|
1049 | + |
|
1050 | + |
|
1051 | + /** |
|
1052 | + * find_form_data_for_this_section |
|
1053 | + * using this section's name and its parents, finds the value of the form data that corresponds to it. |
|
1054 | + * For example, if this form section's HTML name is my_form[subform][form_input_1], |
|
1055 | + * then it's value should be in $_REQUEST at $_REQUEST['my_form']['subform']['form_input_1']. |
|
1056 | + * (If that doesn't exist, we also check for this subsection's name |
|
1057 | + * at the TOP LEVEL of the request data. Eg $_REQUEST['form_input_1'].) |
|
1058 | + * This function finds its value in the form. |
|
1059 | + * |
|
1060 | + * @param array $req_data |
|
1061 | + * @return mixed whatever the raw value of this form section is in the request data |
|
1062 | + * @throws \EE_Error |
|
1063 | + */ |
|
1064 | + public function find_form_data_for_this_section($req_data) |
|
1065 | + { |
|
1066 | + $name_parts = $this->getInputNameParts(); |
|
1067 | + // now get the value for the input |
|
1068 | + $value = $this->findRequestForSectionUsingNameParts($name_parts, $req_data); |
|
1069 | + // check if this thing's name is at the TOP level of the request data |
|
1070 | + if ($value === null && isset($req_data[ $this->name() ])) { |
|
1071 | + $value = $req_data[ $this->name() ]; |
|
1072 | + } |
|
1073 | + return $value; |
|
1074 | + } |
|
1075 | + |
|
1076 | + |
|
1077 | + |
|
1078 | + /** |
|
1079 | + * If this input's name is something like "foo[bar][baz]" |
|
1080 | + * returns an array like `array('foo','bar',baz')` |
|
1081 | + * @return array |
|
1082 | + */ |
|
1083 | + protected function getInputNameParts() |
|
1084 | + { |
|
1085 | + // break up the html name by "[]" |
|
1086 | + if (strpos($this->html_name(), '[') !== false) { |
|
1087 | + $before_any_brackets = substr($this->html_name(), 0, strpos($this->html_name(), '[')); |
|
1088 | + } else { |
|
1089 | + $before_any_brackets = $this->html_name(); |
|
1090 | + } |
|
1091 | + // grab all of the segments |
|
1092 | + preg_match_all('~\[([^]]*)\]~', $this->html_name(), $matches); |
|
1093 | + if (isset($matches[1]) && is_array($matches[1])) { |
|
1094 | + $name_parts = $matches[1]; |
|
1095 | + array_unshift($name_parts, $before_any_brackets); |
|
1096 | + } else { |
|
1097 | + $name_parts = array($before_any_brackets); |
|
1098 | + } |
|
1099 | + return $name_parts; |
|
1100 | + } |
|
1101 | + |
|
1102 | + |
|
1103 | + |
|
1104 | + /** |
|
1105 | + * @param array $html_name_parts |
|
1106 | + * @param array $req_data |
|
1107 | + * @return array | NULL |
|
1108 | + */ |
|
1109 | + public function findRequestForSectionUsingNameParts($html_name_parts, $req_data) |
|
1110 | + { |
|
1111 | + $first_part_to_consider = array_shift($html_name_parts); |
|
1112 | + if (isset($req_data[ $first_part_to_consider ])) { |
|
1113 | + if (empty($html_name_parts)) { |
|
1114 | + return $req_data[ $first_part_to_consider ]; |
|
1115 | + } else { |
|
1116 | + return $this->findRequestForSectionUsingNameParts( |
|
1117 | + $html_name_parts, |
|
1118 | + $req_data[ $first_part_to_consider ] |
|
1119 | + ); |
|
1120 | + } |
|
1121 | + } else { |
|
1122 | + return null; |
|
1123 | + } |
|
1124 | + } |
|
1125 | + |
|
1126 | + |
|
1127 | + |
|
1128 | + /** |
|
1129 | + * Checks if this form input's data is in the request data |
|
1130 | + * |
|
1131 | + * @param array $req_data like $_POST |
|
1132 | + * @return boolean |
|
1133 | + * @throws \EE_Error |
|
1134 | + */ |
|
1135 | + public function form_data_present_in($req_data = null) |
|
1136 | + { |
|
1137 | + if ($req_data === null) { |
|
1138 | + $req_data = $_POST; |
|
1139 | + } |
|
1140 | + $checked_value = $this->find_form_data_for_this_section($req_data); |
|
1141 | + if ($checked_value !== null) { |
|
1142 | + return true; |
|
1143 | + } else { |
|
1144 | + return false; |
|
1145 | + } |
|
1146 | + } |
|
1147 | + |
|
1148 | + |
|
1149 | + |
|
1150 | + /** |
|
1151 | + * Overrides parent to add js data from validation and display strategies |
|
1152 | + * |
|
1153 | + * @param array $form_other_js_data |
|
1154 | + * @return array |
|
1155 | + */ |
|
1156 | + public function get_other_js_data($form_other_js_data = array()) |
|
1157 | + { |
|
1158 | + $form_other_js_data = $this->get_other_js_data_from_strategies($form_other_js_data); |
|
1159 | + return $form_other_js_data; |
|
1160 | + } |
|
1161 | + |
|
1162 | + |
|
1163 | + |
|
1164 | + /** |
|
1165 | + * Gets other JS data for localization from this input's strategies, like |
|
1166 | + * the validation strategies and the display strategy |
|
1167 | + * |
|
1168 | + * @param array $form_other_js_data |
|
1169 | + * @return array |
|
1170 | + */ |
|
1171 | + public function get_other_js_data_from_strategies($form_other_js_data = array()) |
|
1172 | + { |
|
1173 | + $form_other_js_data = $this->get_display_strategy()->get_other_js_data($form_other_js_data); |
|
1174 | + foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
1175 | + $form_other_js_data = $validation_strategy->get_other_js_data($form_other_js_data); |
|
1176 | + } |
|
1177 | + return $form_other_js_data; |
|
1178 | + } |
|
1179 | + |
|
1180 | + |
|
1181 | + |
|
1182 | + /** |
|
1183 | + * Override parent because we want to give our strategies an opportunity to enqueue some js and css |
|
1184 | + * |
|
1185 | + * @return void |
|
1186 | + */ |
|
1187 | + public function enqueue_js() |
|
1188 | + { |
|
1189 | + // ask our display strategy and validation strategies if they have js to enqueue |
|
1190 | + $this->enqueue_js_from_strategies(); |
|
1191 | + } |
|
1192 | + |
|
1193 | + |
|
1194 | + |
|
1195 | + /** |
|
1196 | + * Tells strategies when its ok to enqueue their js and css |
|
1197 | + * |
|
1198 | + * @return void |
|
1199 | + */ |
|
1200 | + public function enqueue_js_from_strategies() |
|
1201 | + { |
|
1202 | + $this->get_display_strategy()->enqueue_js(); |
|
1203 | + foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
1204 | + $validation_strategy->enqueue_js(); |
|
1205 | + } |
|
1206 | + } |
|
1207 | + |
|
1208 | + |
|
1209 | + |
|
1210 | + /** |
|
1211 | + * Gets the default value set on the input (not the current value, which may have been |
|
1212 | + * changed because of a form submission). If no default was set, this us null. |
|
1213 | + * @return mixed |
|
1214 | + */ |
|
1215 | + public function get_default() |
|
1216 | + { |
|
1217 | + return $this->_default; |
|
1218 | + } |
|
1219 | + |
|
1220 | + |
|
1221 | + |
|
1222 | + /** |
|
1223 | + * Makes this input disabled. That means it will have the HTML attribute 'disabled="disabled"', |
|
1224 | + * and server-side if any input was received it will be ignored |
|
1225 | + */ |
|
1226 | + public function disable($disable = true) |
|
1227 | + { |
|
1228 | + $disabled_attribute = ' disabled="disabled"'; |
|
1229 | + $this->disabled = filter_var($disable, FILTER_VALIDATE_BOOLEAN); |
|
1230 | + if ($this->disabled) { |
|
1231 | + if (strpos($this->_other_html_attributes, $disabled_attribute) === false) { |
|
1232 | + $this->_other_html_attributes .= $disabled_attribute; |
|
1233 | + } |
|
1234 | + $this->_set_normalized_value($this->get_default()); |
|
1235 | + } else { |
|
1236 | + $this->_other_html_attributes = str_replace($disabled_attribute, '', $this->_other_html_attributes); |
|
1237 | + } |
|
1238 | + } |
|
1239 | + |
|
1240 | + |
|
1241 | + |
|
1242 | + /** |
|
1243 | + * Returns whether or not this input is currently disabled. |
|
1244 | + * @return bool |
|
1245 | + */ |
|
1246 | + public function isDisabled() |
|
1247 | + { |
|
1248 | + return $this->disabled; |
|
1249 | + } |
|
1250 | 1250 | } |
@@ -202,7 +202,7 @@ discard block |
||
202 | 202 | if (isset($input_args['validation_strategies'])) { |
203 | 203 | foreach ((array) $input_args['validation_strategies'] as $validation_strategy) { |
204 | 204 | if ($validation_strategy instanceof EE_Validation_Strategy_Base && empty($input_args['ignore_input'])) { |
205 | - $this->_validation_strategies[ get_class($validation_strategy) ] = $validation_strategy; |
|
205 | + $this->_validation_strategies[get_class($validation_strategy)] = $validation_strategy; |
|
206 | 206 | } |
207 | 207 | } |
208 | 208 | unset($input_args['validation_strategies']); |
@@ -213,7 +213,7 @@ discard block |
||
213 | 213 | // loop thru incoming options |
214 | 214 | foreach ($input_args as $key => $value) { |
215 | 215 | // add underscore to $key to match property names |
216 | - $_key = '_' . $key; |
|
216 | + $_key = '_'.$key; |
|
217 | 217 | if (property_exists($this, $_key)) { |
218 | 218 | $this->{$_key} = $value; |
219 | 219 | } |
@@ -233,7 +233,7 @@ discard block |
||
233 | 233 | if (isset($input_args['ignore_input'])) { |
234 | 234 | $this->_normalization_strategy = new EE_Null_Normalization(); |
235 | 235 | } |
236 | - if (! $this->_normalization_strategy) { |
|
236 | + if ( ! $this->_normalization_strategy) { |
|
237 | 237 | $this->_normalization_strategy = new EE_Text_Normalization(); |
238 | 238 | } |
239 | 239 | $this->_normalization_strategy->_construct_finalize($this); |
@@ -242,7 +242,7 @@ discard block |
||
242 | 242 | $this->set_default($input_args['default']); |
243 | 243 | unset($input_args['default']); |
244 | 244 | } |
245 | - if (! $this->_sensitive_data_removal_strategy) { |
|
245 | + if ( ! $this->_sensitive_data_removal_strategy) { |
|
246 | 246 | $this->_sensitive_data_removal_strategy = new EE_No_Sensitive_Data_Removal(); |
247 | 247 | } |
248 | 248 | $this->_sensitive_data_removal_strategy->_construct_finalize($this); |
@@ -259,10 +259,10 @@ discard block |
||
259 | 259 | */ |
260 | 260 | protected function _set_default_html_name_if_empty() |
261 | 261 | { |
262 | - if (! $this->_html_name) { |
|
262 | + if ( ! $this->_html_name) { |
|
263 | 263 | $this->_html_name = $this->name(); |
264 | 264 | if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
265 | - $this->_html_name = $this->_parent_section->html_name_prefix() . "[{$this->name()}]"; |
|
265 | + $this->_html_name = $this->_parent_section->html_name_prefix()."[{$this->name()}]"; |
|
266 | 266 | } |
267 | 267 | } |
268 | 268 | } |
@@ -294,7 +294,7 @@ discard block |
||
294 | 294 | protected function _get_display_strategy() |
295 | 295 | { |
296 | 296 | $this->ensure_construct_finalized_called(); |
297 | - if (! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) { |
|
297 | + if ( ! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) { |
|
298 | 298 | throw new EE_Error( |
299 | 299 | sprintf( |
300 | 300 | __( |
@@ -461,7 +461,7 @@ discard block |
||
461 | 461 | if ($validation_strategy instanceof $validation_strategy_classname |
462 | 462 | || is_subclass_of($validation_strategy, $validation_strategy_classname) |
463 | 463 | ) { |
464 | - unset($this->_validation_strategies[ $key ]); |
|
464 | + unset($this->_validation_strategies[$key]); |
|
465 | 465 | } |
466 | 466 | } |
467 | 467 | } |
@@ -526,7 +526,7 @@ discard block |
||
526 | 526 | */ |
527 | 527 | public function html_other_attributes() |
528 | 528 | { |
529 | - return ! empty($this->_html_other_attributes) ? ' ' . $this->_html_other_attributes : ''; |
|
529 | + return ! empty($this->_html_other_attributes) ? ' '.$this->_html_other_attributes : ''; |
|
530 | 530 | } |
531 | 531 | |
532 | 532 | |
@@ -648,7 +648,7 @@ discard block |
||
648 | 648 | if (is_array($raw_input)) { |
649 | 649 | $raw_value = array(); |
650 | 650 | foreach ($raw_input as $key => $value) { |
651 | - $raw_value[ $key ] = $this->_sanitize($value); |
|
651 | + $raw_value[$key] = $this->_sanitize($value); |
|
652 | 652 | } |
653 | 653 | $this->_set_raw_value($raw_value); |
654 | 654 | } else { |
@@ -681,7 +681,7 @@ discard block |
||
681 | 681 | */ |
682 | 682 | public function html_label_id() |
683 | 683 | { |
684 | - return ! empty($this->_html_label_id) ? $this->_html_label_id : $this->html_id() . '-lbl'; |
|
684 | + return ! empty($this->_html_label_id) ? $this->_html_label_id : $this->html_id().'-lbl'; |
|
685 | 685 | } |
686 | 686 | |
687 | 687 | |
@@ -831,9 +831,9 @@ discard block |
||
831 | 831 | $validation_strategy->get_jquery_validation_rule_array() |
832 | 832 | ); |
833 | 833 | } |
834 | - if (! empty($jquery_validation_rules)) { |
|
834 | + if ( ! empty($jquery_validation_rules)) { |
|
835 | 835 | foreach ($this->get_display_strategy()->get_html_input_ids(true) as $html_id_with_pound_sign) { |
836 | - $jquery_validation_js[ $html_id_with_pound_sign ] = $jquery_validation_rules; |
|
836 | + $jquery_validation_js[$html_id_with_pound_sign] = $jquery_validation_rules; |
|
837 | 837 | } |
838 | 838 | } |
839 | 839 | return $jquery_validation_js; |
@@ -955,7 +955,7 @@ discard block |
||
955 | 955 | public function html_class($add_required = false) |
956 | 956 | { |
957 | 957 | return $add_required && $this->required() |
958 | - ? $this->required_css_class() . ' ' . $this->_html_class |
|
958 | + ? $this->required_css_class().' '.$this->_html_class |
|
959 | 959 | : $this->_html_class; |
960 | 960 | } |
961 | 961 | |
@@ -1029,7 +1029,7 @@ discard block |
||
1029 | 1029 | $button_css_attributes .= ''; |
1030 | 1030 | } |
1031 | 1031 | $this->_button_css_attributes .= ! empty($other_attributes) |
1032 | - ? $button_css_attributes . ' ' . $other_attributes |
|
1032 | + ? $button_css_attributes.' '.$other_attributes |
|
1033 | 1033 | : $button_css_attributes; |
1034 | 1034 | } |
1035 | 1035 | |
@@ -1067,8 +1067,8 @@ discard block |
||
1067 | 1067 | // now get the value for the input |
1068 | 1068 | $value = $this->findRequestForSectionUsingNameParts($name_parts, $req_data); |
1069 | 1069 | // check if this thing's name is at the TOP level of the request data |
1070 | - if ($value === null && isset($req_data[ $this->name() ])) { |
|
1071 | - $value = $req_data[ $this->name() ]; |
|
1070 | + if ($value === null && isset($req_data[$this->name()])) { |
|
1071 | + $value = $req_data[$this->name()]; |
|
1072 | 1072 | } |
1073 | 1073 | return $value; |
1074 | 1074 | } |
@@ -1109,13 +1109,13 @@ discard block |
||
1109 | 1109 | public function findRequestForSectionUsingNameParts($html_name_parts, $req_data) |
1110 | 1110 | { |
1111 | 1111 | $first_part_to_consider = array_shift($html_name_parts); |
1112 | - if (isset($req_data[ $first_part_to_consider ])) { |
|
1112 | + if (isset($req_data[$first_part_to_consider])) { |
|
1113 | 1113 | if (empty($html_name_parts)) { |
1114 | - return $req_data[ $first_part_to_consider ]; |
|
1114 | + return $req_data[$first_part_to_consider]; |
|
1115 | 1115 | } else { |
1116 | 1116 | return $this->findRequestForSectionUsingNameParts( |
1117 | 1117 | $html_name_parts, |
1118 | - $req_data[ $first_part_to_consider ] |
|
1118 | + $req_data[$first_part_to_consider] |
|
1119 | 1119 | ); |
1120 | 1120 | } |
1121 | 1121 | } else { |
@@ -1306,7 +1306,7 @@ |
||
1306 | 1306 | * |
1307 | 1307 | * @param EEM_Base $model |
1308 | 1308 | * @param WP_REST_Request $request |
1309 | - * @param null $context |
|
1309 | + * @param string $context |
|
1310 | 1310 | * @return array|WP_Error |
1311 | 1311 | */ |
1312 | 1312 | public function getOneOrReportPermissionError(EEM_Base $model, WP_REST_Request $request, $context = null) |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | $controller = new Read(); |
65 | 65 | try { |
66 | 66 | $controller->setRequestedVersion($version); |
67 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
67 | + if ( ! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
68 | 68 | return $controller->sendResponse( |
69 | 69 | new WP_Error( |
70 | 70 | 'endpoint_parsing_error', |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | $controller = new Read(); |
103 | 103 | try { |
104 | 104 | $controller->setRequestedVersion($version); |
105 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
105 | + if ( ! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
106 | 106 | return array(); |
107 | 107 | } |
108 | 108 | // get the model for this version |
@@ -159,11 +159,11 @@ discard block |
||
159 | 159 | */ |
160 | 160 | protected function translateDefaultsForRestResponse($field_name, EE_Model_Field_Base $field, array $schema) |
161 | 161 | { |
162 | - if (isset($schema['properties'][ $field_name ]['default'])) { |
|
163 | - if (is_array($schema['properties'][ $field_name ]['default'])) { |
|
164 | - foreach ($schema['properties'][ $field_name ]['default'] as $default_key => $default_value) { |
|
162 | + if (isset($schema['properties'][$field_name]['default'])) { |
|
163 | + if (is_array($schema['properties'][$field_name]['default'])) { |
|
164 | + foreach ($schema['properties'][$field_name]['default'] as $default_key => $default_value) { |
|
165 | 165 | if ($default_key === 'raw') { |
166 | - $schema['properties'][ $field_name ]['default'][ $default_key ] = |
|
166 | + $schema['properties'][$field_name]['default'][$default_key] = |
|
167 | 167 | ModelDataTranslator::prepareFieldValueForJson( |
168 | 168 | $field, |
169 | 169 | $default_value, |
@@ -172,9 +172,9 @@ discard block |
||
172 | 172 | } |
173 | 173 | } |
174 | 174 | } else { |
175 | - $schema['properties'][ $field_name ]['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
175 | + $schema['properties'][$field_name]['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
176 | 176 | $field, |
177 | - $schema['properties'][ $field_name ]['default'], |
|
177 | + $schema['properties'][$field_name]['default'], |
|
178 | 178 | $this->getModelVersionInfo()->requestedVersion() |
179 | 179 | ); |
180 | 180 | } |
@@ -196,9 +196,9 @@ discard block |
||
196 | 196 | protected function maybeAddExtraFieldsToSchema($field_name, EE_Model_Field_Base $field, array $schema) |
197 | 197 | { |
198 | 198 | if ($field instanceof EE_Datetime_Field) { |
199 | - $schema['properties'][ $field_name . '_gmt' ] = $field->getSchema(); |
|
199 | + $schema['properties'][$field_name.'_gmt'] = $field->getSchema(); |
|
200 | 200 | // modify the description |
201 | - $schema['properties'][ $field_name . '_gmt' ]['description'] = sprintf( |
|
201 | + $schema['properties'][$field_name.'_gmt']['description'] = sprintf( |
|
202 | 202 | esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
203 | 203 | wp_specialchars_decode($field->get_nicename(), ENT_QUOTES) |
204 | 204 | ); |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | $controller = new Read(); |
239 | 239 | try { |
240 | 240 | $controller->setRequestedVersion($version); |
241 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
241 | + if ( ! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
242 | 242 | return $controller->sendResponse( |
243 | 243 | new WP_Error( |
244 | 244 | 'endpoint_parsing_error', |
@@ -283,7 +283,7 @@ discard block |
||
283 | 283 | $controller = new Read(); |
284 | 284 | try { |
285 | 285 | $controller->setRequestedVersion($version); |
286 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
286 | + if ( ! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
287 | 287 | return $controller->sendResponse( |
288 | 288 | new WP_Error( |
289 | 289 | 'endpoint_parsing_error', |
@@ -298,7 +298,7 @@ discard block |
||
298 | 298 | ); |
299 | 299 | } |
300 | 300 | $main_model = $controller->getModelVersionInfo()->loadModel($model_name); |
301 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($related_model_name)) { |
|
301 | + if ( ! $controller->getModelVersionInfo()->isModelNameInThisVersion($related_model_name)) { |
|
302 | 302 | return $controller->sendResponse( |
303 | 303 | new WP_Error( |
304 | 304 | 'endpoint_parsing_error', |
@@ -335,7 +335,7 @@ discard block |
||
335 | 335 | public function getEntitiesFromModel($model, $request) |
336 | 336 | { |
337 | 337 | $query_params = $this->createModelQueryParams($model, $request->get_params()); |
338 | - if (! Capabilities::currentUserHasPartialAccessTo($model, $query_params['caps'])) { |
|
338 | + if ( ! Capabilities::currentUserHasPartialAccessTo($model, $query_params['caps'])) { |
|
339 | 339 | $model_name_plural = EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
340 | 340 | return new WP_Error( |
341 | 341 | sprintf('rest_%s_cannot_list', $model_name_plural), |
@@ -347,7 +347,7 @@ discard block |
||
347 | 347 | array('status' => 403) |
348 | 348 | ); |
349 | 349 | } |
350 | - if (! $request->get_header('no_rest_headers')) { |
|
350 | + if ( ! $request->get_header('no_rest_headers')) { |
|
351 | 351 | $this->setHeadersFromQueryParams($model, $query_params); |
352 | 352 | } |
353 | 353 | /** @type array $results */ |
@@ -382,7 +382,7 @@ discard block |
||
382 | 382 | $context = $this->validateContext($request->get_param('caps')); |
383 | 383 | $model = $relation->get_this_model(); |
384 | 384 | $related_model = $relation->get_other_model(); |
385 | - if (! isset($primary_model_query_params[0])) { |
|
385 | + if ( ! isset($primary_model_query_params[0])) { |
|
386 | 386 | $primary_model_query_params[0] = array(); |
387 | 387 | } |
388 | 388 | // check if they can access the 1st model object |
@@ -399,7 +399,7 @@ discard block |
||
399 | 399 | $restricted_query_params['caps'] = $context; |
400 | 400 | $this->setDebugInfo('main model query params', $restricted_query_params); |
401 | 401 | $this->setDebugInfo('missing caps', Capabilities::getMissingPermissionsString($related_model, $context)); |
402 | - if (! ( |
|
402 | + if ( ! ( |
|
403 | 403 | Capabilities::currentUserHasPartialAccessTo($related_model, $context) |
404 | 404 | && $model->exists($restricted_query_params) |
405 | 405 | ) |
@@ -432,13 +432,13 @@ discard block |
||
432 | 432 | } |
433 | 433 | $query_params = $this->createModelQueryParams($relation->get_other_model(), $request->get_params()); |
434 | 434 | foreach ($primary_model_query_params[0] as $where_condition_key => $where_condition_value) { |
435 | - $query_params[0][ $relation->get_this_model()->get_this_model_name() |
|
435 | + $query_params[0][$relation->get_this_model()->get_this_model_name() |
|
436 | 436 | . '.' |
437 | - . $where_condition_key ] = $where_condition_value; |
|
437 | + . $where_condition_key] = $where_condition_value; |
|
438 | 438 | } |
439 | 439 | $query_params['default_where_conditions'] = 'none'; |
440 | 440 | $query_params['caps'] = $context; |
441 | - if (! $request->get_header('no_rest_headers')) { |
|
441 | + if ( ! $request->get_header('no_rest_headers')) { |
|
442 | 442 | $this->setHeadersFromQueryParams($relation->get_other_model(), $query_params); |
443 | 443 | } |
444 | 444 | /** @type array $results */ |
@@ -489,7 +489,7 @@ discard block |
||
489 | 489 | */ |
490 | 490 | public function getEntitiesFromRelation($id, $relation, $request) |
491 | 491 | { |
492 | - if (! $relation->get_this_model()->has_primary_key_field()) { |
|
492 | + if ( ! $relation->get_this_model()->has_primary_key_field()) { |
|
493 | 493 | throw new EE_Error( |
494 | 494 | sprintf( |
495 | 495 | __( |
@@ -531,7 +531,7 @@ discard block |
||
531 | 531 | Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
532 | 532 | ); |
533 | 533 | // normally the limit to a 2-part array, where the 2nd item is the limit |
534 | - if (! isset($query_params['limit'])) { |
|
534 | + if ( ! isset($query_params['limit'])) { |
|
535 | 535 | $query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
536 | 536 | } |
537 | 537 | if (is_array($query_params['limit'])) { |
@@ -564,7 +564,7 @@ discard block |
||
564 | 564 | */ |
565 | 565 | public function createEntityFromWpdbResult($model, $db_row, $rest_request, $deprecated = null) |
566 | 566 | { |
567 | - if (! $rest_request instanceof WP_REST_Request) { |
|
567 | + if ( ! $rest_request instanceof WP_REST_Request) { |
|
568 | 568 | // ok so this was called in the old style, where the 3rd arg was |
569 | 569 | // $include, and the 4th arg was $context |
570 | 570 | // now setup the request just to avoid fatal errors, although we won't be able |
@@ -645,8 +645,8 @@ discard block |
||
645 | 645 | if ($do_chevy_shuffle) { |
646 | 646 | global $post; |
647 | 647 | $old_post = $post; |
648 | - $post = get_post($result[ $model->primary_key_name() ]); |
|
649 | - if (! $post instanceof \WP_Post) { |
|
648 | + $post = get_post($result[$model->primary_key_name()]); |
|
649 | + if ( ! $post instanceof \WP_Post) { |
|
650 | 650 | // well that's weird, because $result is what we JUST fetched from the database |
651 | 651 | throw new RestException( |
652 | 652 | 'error_fetching_post_from_database_results', |
@@ -656,7 +656,7 @@ discard block |
||
656 | 656 | ) |
657 | 657 | ); |
658 | 658 | } |
659 | - $model_object_classname = 'EE_' . $model->get_this_model_name(); |
|
659 | + $model_object_classname = 'EE_'.$model->get_this_model_name(); |
|
660 | 660 | $post->{$model_object_classname} = \EE_Registry::instance()->load_class( |
661 | 661 | $model_object_classname, |
662 | 662 | $result, |
@@ -667,13 +667,13 @@ discard block |
||
667 | 667 | foreach ($result as $field_name => $field_value) { |
668 | 668 | $field_obj = $model->field_settings_for($field_name); |
669 | 669 | if ($this->isSubclassOfOne($field_obj, $this->getModelVersionInfo()->fieldsIgnored())) { |
670 | - unset($result[ $field_name ]); |
|
670 | + unset($result[$field_name]); |
|
671 | 671 | } elseif ($this->isSubclassOfOne( |
672 | 672 | $field_obj, |
673 | 673 | $this->getModelVersionInfo()->fieldsThatHaveRenderedFormat() |
674 | 674 | ) |
675 | 675 | ) { |
676 | - $result[ $field_name ] = array( |
|
676 | + $result[$field_name] = array( |
|
677 | 677 | 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
678 | 678 | 'rendered' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
679 | 679 | ); |
@@ -682,7 +682,7 @@ discard block |
||
682 | 682 | $this->getModelVersionInfo()->fieldsThatHavePrettyFormat() |
683 | 683 | ) |
684 | 684 | ) { |
685 | - $result[ $field_name ] = array( |
|
685 | + $result[$field_name] = array( |
|
686 | 686 | 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
687 | 687 | 'pretty' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
688 | 688 | ); |
@@ -709,10 +709,10 @@ discard block |
||
709 | 709 | $this->getModelVersionInfo()->requestedVersion() |
710 | 710 | ); |
711 | 711 | } |
712 | - $result[ $field_name . '_gmt' ] = $gmt_date; |
|
713 | - $result[ $field_name ] = $local_date; |
|
712 | + $result[$field_name.'_gmt'] = $gmt_date; |
|
713 | + $result[$field_name] = $local_date; |
|
714 | 714 | } else { |
715 | - $result[ $field_name ] = $this->prepareFieldObjValueForJson($field_obj, $field_value); |
|
715 | + $result[$field_name] = $this->prepareFieldObjValueForJson($field_obj, $field_value); |
|
716 | 716 | } |
717 | 717 | } |
718 | 718 | if ($do_chevy_shuffle) { |
@@ -764,7 +764,7 @@ discard block |
||
764 | 764 | protected function addExtraFields(EEM_Base $model, $db_row, $entity_array) |
765 | 765 | { |
766 | 766 | if ($model instanceof EEM_CPT_Base) { |
767 | - $entity_array['link'] = get_permalink($db_row[ $model->get_primary_key_field()->get_qualified_column() ]); |
|
767 | + $entity_array['link'] = get_permalink($db_row[$model->get_primary_key_field()->get_qualified_column()]); |
|
768 | 768 | } |
769 | 769 | return $entity_array; |
770 | 770 | } |
@@ -789,7 +789,7 @@ discard block |
||
789 | 789 | 'href' => $this->getVersionedLinkTo( |
790 | 790 | EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
791 | 791 | . '/' |
792 | - . $entity_array[ $model->primary_key_name() ] |
|
792 | + . $entity_array[$model->primary_key_name()] |
|
793 | 793 | ), |
794 | 794 | ), |
795 | 795 | ); |
@@ -805,12 +805,12 @@ discard block |
||
805 | 805 | if ($model->has_primary_key_field()) { |
806 | 806 | foreach ($this->getModelVersionInfo()->relationSettings($model) as $relation_name => $relation_obj) { |
807 | 807 | $related_model_part = Read::getRelatedEntityName($relation_name, $relation_obj); |
808 | - $links[ EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part ] = array( |
|
808 | + $links[EED_Core_Rest_Api::ee_api_link_namespace.$related_model_part] = array( |
|
809 | 809 | array( |
810 | 810 | 'href' => $this->getVersionedLinkTo( |
811 | 811 | EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
812 | 812 | . '/' |
813 | - . $entity_array[ $model->primary_key_name() ] |
|
813 | + . $entity_array[$model->primary_key_name()] |
|
814 | 814 | . '/' |
815 | 815 | . $related_model_part |
816 | 816 | ), |
@@ -839,13 +839,13 @@ discard block |
||
839 | 839 | $db_row = array() |
840 | 840 | ) { |
841 | 841 | // if $db_row not included, hope the entity array has what we need |
842 | - if (! $db_row) { |
|
842 | + if ( ! $db_row) { |
|
843 | 843 | $db_row = $entity_array; |
844 | 844 | } |
845 | 845 | $includes_for_this_model = $this->explodeAndGetItemsPrefixedWith($rest_request->get_param('include'), ''); |
846 | 846 | $includes_for_this_model = $this->removeModelNamesFromArray($includes_for_this_model); |
847 | 847 | // if they passed in * or didn't specify any includes, return everything |
848 | - if (! in_array('*', $includes_for_this_model) |
|
848 | + if ( ! in_array('*', $includes_for_this_model) |
|
849 | 849 | && ! empty($includes_for_this_model) |
850 | 850 | ) { |
851 | 851 | if ($model->has_primary_key_field()) { |
@@ -891,7 +891,7 @@ discard block |
||
891 | 891 | $relation_obj, |
892 | 892 | $pretend_related_request |
893 | 893 | ); |
894 | - $entity_array[ Read::getRelatedEntityName($relation_name, $relation_obj) ] = $related_results |
|
894 | + $entity_array[Read::getRelatedEntityName($relation_name, $relation_obj)] = $related_results |
|
895 | 895 | instanceof |
896 | 896 | WP_Error |
897 | 897 | ? null |
@@ -1023,7 +1023,7 @@ discard block |
||
1023 | 1023 | */ |
1024 | 1024 | public function validateContext($context) |
1025 | 1025 | { |
1026 | - if (! $context) { |
|
1026 | + if ( ! $context) { |
|
1027 | 1027 | $context = EEM_Base::caps_read; |
1028 | 1028 | } |
1029 | 1029 | $valid_contexts = EEM_Base::valid_cap_contexts(); |
@@ -1048,7 +1048,7 @@ discard block |
||
1048 | 1048 | EEM_Base::default_where_conditions_minimum_all, |
1049 | 1049 | EEM_Base::default_where_conditions_minimum_others, |
1050 | 1050 | ); |
1051 | - if (! $default_query_params) { |
|
1051 | + if ( ! $default_query_params) { |
|
1052 | 1052 | $default_query_params = EEM_Base::default_where_conditions_all; |
1053 | 1053 | } |
1054 | 1054 | if (in_array( |
@@ -1131,14 +1131,14 @@ discard block |
||
1131 | 1131 | } |
1132 | 1132 | if (isset($query_parameters['limit'])) { |
1133 | 1133 | // limit should be either a string like '23' or '23,43', or an array with two items in it |
1134 | - if (! is_array($query_parameters['limit'])) { |
|
1134 | + if ( ! is_array($query_parameters['limit'])) { |
|
1135 | 1135 | $limit_array = explode(',', (string) $query_parameters['limit']); |
1136 | 1136 | } else { |
1137 | 1137 | $limit_array = $query_parameters['limit']; |
1138 | 1138 | } |
1139 | 1139 | $sanitized_limit = array(); |
1140 | 1140 | foreach ($limit_array as $key => $limit_part) { |
1141 | - if ($this->debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1141 | + if ($this->debug_mode && ( ! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1142 | 1142 | throw new EE_Error( |
1143 | 1143 | sprintf( |
1144 | 1144 | __( |
@@ -1184,9 +1184,9 @@ discard block |
||
1184 | 1184 | $model_ready_query_params = array(); |
1185 | 1185 | foreach ($query_params as $key => $value) { |
1186 | 1186 | if (is_array($value)) { |
1187 | - $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsKeyForModels($model, $value); |
|
1187 | + $model_ready_query_params[$key] = $this->prepareRestQueryParamsKeyForModels($model, $value); |
|
1188 | 1188 | } else { |
1189 | - $model_ready_query_params[ $key ] = $value; |
|
1189 | + $model_ready_query_params[$key] = $value; |
|
1190 | 1190 | } |
1191 | 1191 | } |
1192 | 1192 | return $model_ready_query_params; |
@@ -1204,9 +1204,9 @@ discard block |
||
1204 | 1204 | $model_ready_query_params = array(); |
1205 | 1205 | foreach ($query_params as $key => $value) { |
1206 | 1206 | if (is_array($value)) { |
1207 | - $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsValuesForModels($model, $value); |
|
1207 | + $model_ready_query_params[$key] = $this->prepareRestQueryParamsValuesForModels($model, $value); |
|
1208 | 1208 | } else { |
1209 | - $model_ready_query_params[ $key ] = $value; |
|
1209 | + $model_ready_query_params[$key] = $value; |
|
1210 | 1210 | } |
1211 | 1211 | } |
1212 | 1212 | return $model_ready_query_params; |
@@ -1238,17 +1238,17 @@ discard block |
||
1238 | 1238 | foreach ($exploded_contents as $item) { |
1239 | 1239 | $item = trim($item); |
1240 | 1240 | // if no prefix was provided, so we look for items with no "." in them |
1241 | - if (! $prefix) { |
|
1241 | + if ( ! $prefix) { |
|
1242 | 1242 | // does this item have a period? |
1243 | 1243 | if (strpos($item, '.') === false) { |
1244 | 1244 | // if not, then its what we're looking for |
1245 | 1245 | $contents_with_prefix[] = $item; |
1246 | 1246 | } |
1247 | - } elseif (strpos($item, $prefix . '.') === 0) { |
|
1247 | + } elseif (strpos($item, $prefix.'.') === 0) { |
|
1248 | 1248 | // this item has the prefix and a period, grab it |
1249 | 1249 | $contents_with_prefix[] = substr( |
1250 | 1250 | $item, |
1251 | - strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1251 | + strpos($item, $prefix.'.') + strlen($prefix.'.') |
|
1252 | 1252 | ); |
1253 | 1253 | } elseif ($item === $prefix) { |
1254 | 1254 | // this item is JUST the prefix |
@@ -1287,9 +1287,9 @@ discard block |
||
1287 | 1287 | if ($model_name) { |
1288 | 1288 | foreach ($includes as $field_to_include) { |
1289 | 1289 | $field_to_include = trim($field_to_include); |
1290 | - if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1290 | + if (strpos($field_to_include, $model_name.'.') === 0) { |
|
1291 | 1291 | // found the model name at the exact start |
1292 | - $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1292 | + $field_sans_model_name = str_replace($model_name.'.', '', $field_to_include); |
|
1293 | 1293 | $extracted_fields_to_include[] = $field_sans_model_name; |
1294 | 1294 | } elseif ($field_to_include == $model_name) { |
1295 | 1295 | $extracted_fields_to_include[] = '*'; |
@@ -1329,7 +1329,7 @@ discard block |
||
1329 | 1329 | $restricted_query_params['caps'] = $context; |
1330 | 1330 | $this->setDebugInfo('model query params', $restricted_query_params); |
1331 | 1331 | $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
1332 | - if (! empty($model_rows)) { |
|
1332 | + if ( ! empty($model_rows)) { |
|
1333 | 1333 | return $this->createEntityFromWpdbResult( |
1334 | 1334 | $model, |
1335 | 1335 | array_shift($model_rows), |
@@ -1339,10 +1339,10 @@ discard block |
||
1339 | 1339 | // ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
1340 | 1340 | $lowercase_model_name = strtolower($model->get_this_model_name()); |
1341 | 1341 | $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
1342 | - if (! empty($model_rows_found_sans_restrictions)) { |
|
1342 | + if ( ! empty($model_rows_found_sans_restrictions)) { |
|
1343 | 1343 | // you got shafted- it existed but we didn't want to tell you! |
1344 | 1344 | return new WP_Error( |
1345 | - 'rest_user_cannot_' . $context, |
|
1345 | + 'rest_user_cannot_'.$context, |
|
1346 | 1346 | sprintf( |
1347 | 1347 | __('Sorry, you cannot %1$s this %2$s. Missing permissions are: %3$s', 'event_espresso'), |
1348 | 1348 | $context, |
@@ -35,1337 +35,1337 @@ |
||
35 | 35 | { |
36 | 36 | |
37 | 37 | |
38 | - /** |
|
39 | - * @var CalculatedModelFields |
|
40 | - */ |
|
41 | - protected $fields_calculator; |
|
42 | - |
|
43 | - |
|
44 | - /** |
|
45 | - * Read constructor. |
|
46 | - */ |
|
47 | - public function __construct() |
|
48 | - { |
|
49 | - parent::__construct(); |
|
50 | - $this->fields_calculator = new CalculatedModelFields(); |
|
51 | - } |
|
52 | - |
|
53 | - |
|
54 | - /** |
|
55 | - * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
56 | - * |
|
57 | - * @param WP_REST_Request $request |
|
58 | - * @param string $version |
|
59 | - * @param string $model_name |
|
60 | - * @return \WP_REST_Response|WP_Error |
|
61 | - */ |
|
62 | - public static function handleRequestGetAll(WP_REST_Request $request, $version, $model_name) |
|
63 | - { |
|
64 | - $controller = new Read(); |
|
65 | - try { |
|
66 | - $controller->setRequestedVersion($version); |
|
67 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
68 | - return $controller->sendResponse( |
|
69 | - new WP_Error( |
|
70 | - 'endpoint_parsing_error', |
|
71 | - sprintf( |
|
72 | - __( |
|
73 | - 'There is no model for endpoint %s. Please contact event espresso support', |
|
74 | - 'event_espresso' |
|
75 | - ), |
|
76 | - $model_name |
|
77 | - ) |
|
78 | - ) |
|
79 | - ); |
|
80 | - } |
|
81 | - return $controller->sendResponse( |
|
82 | - $controller->getEntitiesFromModel( |
|
83 | - $controller->getModelVersionInfo()->loadModel($model_name), |
|
84 | - $request |
|
85 | - ) |
|
86 | - ); |
|
87 | - } catch (Exception $e) { |
|
88 | - return $controller->sendResponse($e); |
|
89 | - } |
|
90 | - } |
|
91 | - |
|
92 | - |
|
93 | - /** |
|
94 | - * Prepares and returns schema for any OPTIONS request. |
|
95 | - * |
|
96 | - * @param string $version The API endpoint version being used. |
|
97 | - * @param string $model_name Something like `Event` or `Registration` |
|
98 | - * @return array |
|
99 | - */ |
|
100 | - public static function handleSchemaRequest($version, $model_name) |
|
101 | - { |
|
102 | - $controller = new Read(); |
|
103 | - try { |
|
104 | - $controller->setRequestedVersion($version); |
|
105 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
106 | - return array(); |
|
107 | - } |
|
108 | - // get the model for this version |
|
109 | - $model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
110 | - $model_schema = new JsonModelSchema($model); |
|
111 | - return $model_schema->getModelSchemaForRelations( |
|
112 | - $controller->getModelVersionInfo()->relationSettings($model), |
|
113 | - $controller->customizeSchemaForRestResponse( |
|
114 | - $model, |
|
115 | - $model_schema->getModelSchemaForFields( |
|
116 | - $controller->getModelVersionInfo()->fieldsOnModelInThisVersion($model), |
|
117 | - $model_schema->getInitialSchemaStructure() |
|
118 | - ) |
|
119 | - ) |
|
120 | - ); |
|
121 | - } catch (Exception $e) { |
|
122 | - return array(); |
|
123 | - } |
|
124 | - } |
|
125 | - |
|
126 | - |
|
127 | - /** |
|
128 | - * This loops through each field in the given schema for the model and does the following: |
|
129 | - * - add any extra fields that are REST API specific and related to existing fields. |
|
130 | - * - transform default values into the correct format for a REST API response. |
|
131 | - * |
|
132 | - * @param EEM_Base $model |
|
133 | - * @param array $schema |
|
134 | - * @return array The final schema. |
|
135 | - */ |
|
136 | - protected function customizeSchemaForRestResponse(EEM_Base $model, array $schema) |
|
137 | - { |
|
138 | - foreach ($this->getModelVersionInfo()->fieldsOnModelInThisVersion($model) as $field_name => $field) { |
|
139 | - $schema = $this->translateDefaultsForRestResponse( |
|
140 | - $field_name, |
|
141 | - $field, |
|
142 | - $this->maybeAddExtraFieldsToSchema($field_name, $field, $schema) |
|
143 | - ); |
|
144 | - } |
|
145 | - return $schema; |
|
146 | - } |
|
147 | - |
|
148 | - |
|
149 | - /** |
|
150 | - * This is used to ensure that the 'default' value set in the schema response is formatted correctly for the REST |
|
151 | - * response. |
|
152 | - * |
|
153 | - * @param $field_name |
|
154 | - * @param EE_Model_Field_Base $field |
|
155 | - * @param array $schema |
|
156 | - * @return array |
|
157 | - * @throws ObjectDetectedException if a default value has a PHP object, which should never do (and if we |
|
158 | - * did, let's know about it ASAP, so let the exception bubble up) |
|
159 | - */ |
|
160 | - protected function translateDefaultsForRestResponse($field_name, EE_Model_Field_Base $field, array $schema) |
|
161 | - { |
|
162 | - if (isset($schema['properties'][ $field_name ]['default'])) { |
|
163 | - if (is_array($schema['properties'][ $field_name ]['default'])) { |
|
164 | - foreach ($schema['properties'][ $field_name ]['default'] as $default_key => $default_value) { |
|
165 | - if ($default_key === 'raw') { |
|
166 | - $schema['properties'][ $field_name ]['default'][ $default_key ] = |
|
167 | - ModelDataTranslator::prepareFieldValueForJson( |
|
168 | - $field, |
|
169 | - $default_value, |
|
170 | - $this->getModelVersionInfo()->requestedVersion() |
|
171 | - ); |
|
172 | - } |
|
173 | - } |
|
174 | - } else { |
|
175 | - $schema['properties'][ $field_name ]['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
176 | - $field, |
|
177 | - $schema['properties'][ $field_name ]['default'], |
|
178 | - $this->getModelVersionInfo()->requestedVersion() |
|
179 | - ); |
|
180 | - } |
|
181 | - } |
|
182 | - return $schema; |
|
183 | - } |
|
184 | - |
|
185 | - |
|
186 | - /** |
|
187 | - * Adds additional fields to the schema |
|
188 | - * The REST API returns a GMT value field for each datetime field in the resource. Thus the description about this |
|
189 | - * needs to be added to the schema. |
|
190 | - * |
|
191 | - * @param $field_name |
|
192 | - * @param EE_Model_Field_Base $field |
|
193 | - * @param array $schema |
|
194 | - * @return array |
|
195 | - */ |
|
196 | - protected function maybeAddExtraFieldsToSchema($field_name, EE_Model_Field_Base $field, array $schema) |
|
197 | - { |
|
198 | - if ($field instanceof EE_Datetime_Field) { |
|
199 | - $schema['properties'][ $field_name . '_gmt' ] = $field->getSchema(); |
|
200 | - // modify the description |
|
201 | - $schema['properties'][ $field_name . '_gmt' ]['description'] = sprintf( |
|
202 | - esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
|
203 | - wp_specialchars_decode($field->get_nicename(), ENT_QUOTES) |
|
204 | - ); |
|
205 | - } |
|
206 | - return $schema; |
|
207 | - } |
|
208 | - |
|
209 | - |
|
210 | - /** |
|
211 | - * Used to figure out the route from the request when a `WP_REST_Request` object is not available |
|
212 | - * |
|
213 | - * @return string |
|
214 | - */ |
|
215 | - protected function getRouteFromRequest() |
|
216 | - { |
|
217 | - if (isset($GLOBALS['wp']) |
|
218 | - && $GLOBALS['wp'] instanceof \WP |
|
219 | - && isset($GLOBALS['wp']->query_vars['rest_route']) |
|
220 | - ) { |
|
221 | - return $GLOBALS['wp']->query_vars['rest_route']; |
|
222 | - } else { |
|
223 | - return isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/'; |
|
224 | - } |
|
225 | - } |
|
226 | - |
|
227 | - |
|
228 | - /** |
|
229 | - * Gets a single entity related to the model indicated in the path and its id |
|
230 | - * |
|
231 | - * @param WP_REST_Request $request |
|
232 | - * @param string $version |
|
233 | - * @param string $model_name |
|
234 | - * @return \WP_REST_Response|WP_Error |
|
235 | - */ |
|
236 | - public static function handleRequestGetOne(WP_REST_Request $request, $version, $model_name) |
|
237 | - { |
|
238 | - $controller = new Read(); |
|
239 | - try { |
|
240 | - $controller->setRequestedVersion($version); |
|
241 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
242 | - return $controller->sendResponse( |
|
243 | - new WP_Error( |
|
244 | - 'endpoint_parsing_error', |
|
245 | - sprintf( |
|
246 | - __( |
|
247 | - 'There is no model for endpoint %s. Please contact event espresso support', |
|
248 | - 'event_espresso' |
|
249 | - ), |
|
250 | - $model_name |
|
251 | - ) |
|
252 | - ) |
|
253 | - ); |
|
254 | - } |
|
255 | - return $controller->sendResponse( |
|
256 | - $controller->getEntityFromModel( |
|
257 | - $controller->getModelVersionInfo()->loadModel($model_name), |
|
258 | - $request |
|
259 | - ) |
|
260 | - ); |
|
261 | - } catch (Exception $e) { |
|
262 | - return $controller->sendResponse($e); |
|
263 | - } |
|
264 | - } |
|
265 | - |
|
266 | - |
|
267 | - /** |
|
268 | - * Gets all the related entities (or if its a belongs-to relation just the one) |
|
269 | - * to the item with the given id |
|
270 | - * |
|
271 | - * @param WP_REST_Request $request |
|
272 | - * @param string $version |
|
273 | - * @param string $model_name |
|
274 | - * @param string $related_model_name |
|
275 | - * @return \WP_REST_Response|WP_Error |
|
276 | - */ |
|
277 | - public static function handleRequestGetRelated( |
|
278 | - WP_REST_Request $request, |
|
279 | - $version, |
|
280 | - $model_name, |
|
281 | - $related_model_name |
|
282 | - ) { |
|
283 | - $controller = new Read(); |
|
284 | - try { |
|
285 | - $controller->setRequestedVersion($version); |
|
286 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
287 | - return $controller->sendResponse( |
|
288 | - new WP_Error( |
|
289 | - 'endpoint_parsing_error', |
|
290 | - sprintf( |
|
291 | - __( |
|
292 | - 'There is no model for endpoint %s. Please contact event espresso support', |
|
293 | - 'event_espresso' |
|
294 | - ), |
|
295 | - $model_name |
|
296 | - ) |
|
297 | - ) |
|
298 | - ); |
|
299 | - } |
|
300 | - $main_model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
301 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($related_model_name)) { |
|
302 | - return $controller->sendResponse( |
|
303 | - new WP_Error( |
|
304 | - 'endpoint_parsing_error', |
|
305 | - sprintf( |
|
306 | - __( |
|
307 | - 'There is no model for endpoint %s. Please contact event espresso support', |
|
308 | - 'event_espresso' |
|
309 | - ), |
|
310 | - $related_model_name |
|
311 | - ) |
|
312 | - ) |
|
313 | - ); |
|
314 | - } |
|
315 | - return $controller->sendResponse( |
|
316 | - $controller->getEntitiesFromRelation( |
|
317 | - $request->get_param('id'), |
|
318 | - $main_model->related_settings_for($related_model_name), |
|
319 | - $request |
|
320 | - ) |
|
321 | - ); |
|
322 | - } catch (Exception $e) { |
|
323 | - return $controller->sendResponse($e); |
|
324 | - } |
|
325 | - } |
|
326 | - |
|
327 | - |
|
328 | - /** |
|
329 | - * Gets a collection for the given model and filters |
|
330 | - * |
|
331 | - * @param EEM_Base $model |
|
332 | - * @param WP_REST_Request $request |
|
333 | - * @return array|WP_Error |
|
334 | - */ |
|
335 | - public function getEntitiesFromModel($model, $request) |
|
336 | - { |
|
337 | - $query_params = $this->createModelQueryParams($model, $request->get_params()); |
|
338 | - if (! Capabilities::currentUserHasPartialAccessTo($model, $query_params['caps'])) { |
|
339 | - $model_name_plural = EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
340 | - return new WP_Error( |
|
341 | - sprintf('rest_%s_cannot_list', $model_name_plural), |
|
342 | - sprintf( |
|
343 | - __('Sorry, you are not allowed to list %1$s. Missing permissions: %2$s', 'event_espresso'), |
|
344 | - $model_name_plural, |
|
345 | - Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
|
346 | - ), |
|
347 | - array('status' => 403) |
|
348 | - ); |
|
349 | - } |
|
350 | - if (! $request->get_header('no_rest_headers')) { |
|
351 | - $this->setHeadersFromQueryParams($model, $query_params); |
|
352 | - } |
|
353 | - /** @type array $results */ |
|
354 | - $results = $model->get_all_wpdb_results($query_params); |
|
355 | - $nice_results = array(); |
|
356 | - foreach ($results as $result) { |
|
357 | - $nice_results[] = $this->createEntityFromWpdbResult( |
|
358 | - $model, |
|
359 | - $result, |
|
360 | - $request |
|
361 | - ); |
|
362 | - } |
|
363 | - return $nice_results; |
|
364 | - } |
|
365 | - |
|
366 | - |
|
367 | - /** |
|
368 | - * Gets the collection for given relation object |
|
369 | - * The same as Read::get_entities_from_model(), except if the relation |
|
370 | - * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
371 | - * the join-model-object into the results |
|
372 | - * |
|
373 | - * @param array $primary_model_query_params query params for finding the item from which |
|
374 | - * relations will be based |
|
375 | - * @param \EE_Model_Relation_Base $relation |
|
376 | - * @param WP_REST_Request $request |
|
377 | - * @return WP_Error|array |
|
378 | - * @throws RestException |
|
379 | - */ |
|
380 | - protected function getEntitiesFromRelationUsingModelQueryParams($primary_model_query_params, $relation, $request) |
|
381 | - { |
|
382 | - $context = $this->validateContext($request->get_param('caps')); |
|
383 | - $model = $relation->get_this_model(); |
|
384 | - $related_model = $relation->get_other_model(); |
|
385 | - if (! isset($primary_model_query_params[0])) { |
|
386 | - $primary_model_query_params[0] = array(); |
|
387 | - } |
|
388 | - // check if they can access the 1st model object |
|
389 | - $primary_model_query_params = array( |
|
390 | - 0 => $primary_model_query_params[0], |
|
391 | - 'limit' => 1, |
|
392 | - ); |
|
393 | - if ($model instanceof \EEM_Soft_Delete_Base) { |
|
394 | - $primary_model_query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included( |
|
395 | - $primary_model_query_params |
|
396 | - ); |
|
397 | - } |
|
398 | - $restricted_query_params = $primary_model_query_params; |
|
399 | - $restricted_query_params['caps'] = $context; |
|
400 | - $this->setDebugInfo('main model query params', $restricted_query_params); |
|
401 | - $this->setDebugInfo('missing caps', Capabilities::getMissingPermissionsString($related_model, $context)); |
|
402 | - if (! ( |
|
403 | - Capabilities::currentUserHasPartialAccessTo($related_model, $context) |
|
404 | - && $model->exists($restricted_query_params) |
|
405 | - ) |
|
406 | - ) { |
|
407 | - if ($relation instanceof EE_Belongs_To_Relation) { |
|
408 | - $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name()); |
|
409 | - } else { |
|
410 | - $related_model_name_maybe_plural = EEH_Inflector::pluralize_and_lower( |
|
411 | - $related_model->get_this_model_name() |
|
412 | - ); |
|
413 | - } |
|
414 | - return new WP_Error( |
|
415 | - sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), |
|
416 | - sprintf( |
|
417 | - __( |
|
418 | - 'Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', |
|
419 | - 'event_espresso' |
|
420 | - ), |
|
421 | - $related_model_name_maybe_plural, |
|
422 | - $relation->get_this_model()->get_this_model_name(), |
|
423 | - implode( |
|
424 | - ',', |
|
425 | - array_keys( |
|
426 | - Capabilities::getMissingPermissions($related_model, $context) |
|
427 | - ) |
|
428 | - ) |
|
429 | - ), |
|
430 | - array('status' => 403) |
|
431 | - ); |
|
432 | - } |
|
433 | - $query_params = $this->createModelQueryParams($relation->get_other_model(), $request->get_params()); |
|
434 | - foreach ($primary_model_query_params[0] as $where_condition_key => $where_condition_value) { |
|
435 | - $query_params[0][ $relation->get_this_model()->get_this_model_name() |
|
436 | - . '.' |
|
437 | - . $where_condition_key ] = $where_condition_value; |
|
438 | - } |
|
439 | - $query_params['default_where_conditions'] = 'none'; |
|
440 | - $query_params['caps'] = $context; |
|
441 | - if (! $request->get_header('no_rest_headers')) { |
|
442 | - $this->setHeadersFromQueryParams($relation->get_other_model(), $query_params); |
|
443 | - } |
|
444 | - /** @type array $results */ |
|
445 | - $results = $relation->get_other_model()->get_all_wpdb_results($query_params); |
|
446 | - $nice_results = array(); |
|
447 | - foreach ($results as $result) { |
|
448 | - $nice_result = $this->createEntityFromWpdbResult( |
|
449 | - $relation->get_other_model(), |
|
450 | - $result, |
|
451 | - $request |
|
452 | - ); |
|
453 | - if ($relation instanceof \EE_HABTM_Relation) { |
|
454 | - // put the unusual stuff (properties from the HABTM relation) first, and make sure |
|
455 | - // if there are conflicts we prefer the properties from the main model |
|
456 | - $join_model_result = $this->createEntityFromWpdbResult( |
|
457 | - $relation->get_join_model(), |
|
458 | - $result, |
|
459 | - $request |
|
460 | - ); |
|
461 | - $joined_result = array_merge($nice_result, $join_model_result); |
|
462 | - // but keep the meta stuff from the main model |
|
463 | - if (isset($nice_result['meta'])) { |
|
464 | - $joined_result['meta'] = $nice_result['meta']; |
|
465 | - } |
|
466 | - $nice_result = $joined_result; |
|
467 | - } |
|
468 | - $nice_results[] = $nice_result; |
|
469 | - } |
|
470 | - if ($relation instanceof EE_Belongs_To_Relation) { |
|
471 | - return array_shift($nice_results); |
|
472 | - } else { |
|
473 | - return $nice_results; |
|
474 | - } |
|
475 | - } |
|
476 | - |
|
477 | - |
|
478 | - /** |
|
479 | - * Gets the collection for given relation object |
|
480 | - * The same as Read::get_entities_from_model(), except if the relation |
|
481 | - * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
482 | - * the join-model-object into the results |
|
483 | - * |
|
484 | - * @param string $id the ID of the thing we are fetching related stuff from |
|
485 | - * @param \EE_Model_Relation_Base $relation |
|
486 | - * @param WP_REST_Request $request |
|
487 | - * @return array|WP_Error |
|
488 | - * @throws EE_Error |
|
489 | - */ |
|
490 | - public function getEntitiesFromRelation($id, $relation, $request) |
|
491 | - { |
|
492 | - if (! $relation->get_this_model()->has_primary_key_field()) { |
|
493 | - throw new EE_Error( |
|
494 | - sprintf( |
|
495 | - __( |
|
496 | - // @codingStandardsIgnoreStart |
|
497 | - 'Read::get_entities_from_relation should only be called from a model with a primary key, it was called from %1$s', |
|
498 | - // @codingStandardsIgnoreEnd |
|
499 | - 'event_espresso' |
|
500 | - ), |
|
501 | - $relation->get_this_model()->get_this_model_name() |
|
502 | - ) |
|
503 | - ); |
|
504 | - } |
|
505 | - return $this->getEntitiesFromRelationUsingModelQueryParams( |
|
506 | - array( |
|
507 | - array( |
|
508 | - $relation->get_this_model()->primary_key_name() => $id, |
|
509 | - ), |
|
510 | - ), |
|
511 | - $relation, |
|
512 | - $request |
|
513 | - ); |
|
514 | - } |
|
515 | - |
|
516 | - |
|
517 | - /** |
|
518 | - * Sets the headers that are based on the model and query params, |
|
519 | - * like the total records. This should only be called on the original request |
|
520 | - * from the client, not on subsequent internal |
|
521 | - * |
|
522 | - * @param EEM_Base $model |
|
523 | - * @param array $query_params |
|
524 | - * @return void |
|
525 | - */ |
|
526 | - protected function setHeadersFromQueryParams($model, $query_params) |
|
527 | - { |
|
528 | - $this->setDebugInfo('model query params', $query_params); |
|
529 | - $this->setDebugInfo( |
|
530 | - 'missing caps', |
|
531 | - Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
|
532 | - ); |
|
533 | - // normally the limit to a 2-part array, where the 2nd item is the limit |
|
534 | - if (! isset($query_params['limit'])) { |
|
535 | - $query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
|
536 | - } |
|
537 | - if (is_array($query_params['limit'])) { |
|
538 | - $limit_parts = $query_params['limit']; |
|
539 | - } else { |
|
540 | - $limit_parts = explode(',', $query_params['limit']); |
|
541 | - if (count($limit_parts) == 1) { |
|
542 | - $limit_parts = array(0, $limit_parts[0]); |
|
543 | - } |
|
544 | - } |
|
545 | - // remove the group by and having parts of the query, as those will |
|
546 | - // make the sql query return an array of values, instead of just a single value |
|
547 | - unset($query_params['group_by'], $query_params['having'], $query_params['limit']); |
|
548 | - $count = $model->count($query_params, null, true); |
|
549 | - $pages = $count / $limit_parts[1]; |
|
550 | - $this->setResponseHeader('Total', $count, false); |
|
551 | - $this->setResponseHeader('PageSize', $limit_parts[1], false); |
|
552 | - $this->setResponseHeader('TotalPages', ceil($pages), false); |
|
553 | - } |
|
554 | - |
|
555 | - |
|
556 | - /** |
|
557 | - * Changes database results into REST API entities |
|
558 | - * |
|
559 | - * @param EEM_Base $model |
|
560 | - * @param array $db_row like results from $wpdb->get_results() |
|
561 | - * @param WP_REST_Request $rest_request |
|
562 | - * @param string $deprecated no longer used |
|
563 | - * @return array ready for being converted into json for sending to client |
|
564 | - */ |
|
565 | - public function createEntityFromWpdbResult($model, $db_row, $rest_request, $deprecated = null) |
|
566 | - { |
|
567 | - if (! $rest_request instanceof WP_REST_Request) { |
|
568 | - // ok so this was called in the old style, where the 3rd arg was |
|
569 | - // $include, and the 4th arg was $context |
|
570 | - // now setup the request just to avoid fatal errors, although we won't be able |
|
571 | - // to truly make use of it because it's kinda devoid of info |
|
572 | - $rest_request = new WP_REST_Request(); |
|
573 | - $rest_request->set_param('include', $rest_request); |
|
574 | - $rest_request->set_param('caps', $deprecated); |
|
575 | - } |
|
576 | - if ($rest_request->get_param('caps') == null) { |
|
577 | - $rest_request->set_param('caps', EEM_Base::caps_read); |
|
578 | - } |
|
579 | - $entity_array = $this->createBareEntityFromWpdbResults($model, $db_row); |
|
580 | - $entity_array = $this->addExtraFields($model, $db_row, $entity_array); |
|
581 | - $entity_array['_links'] = $this->getEntityLinks($model, $db_row, $entity_array); |
|
582 | - $entity_array['_calculated_fields'] = $this->getEntityCalculations($model, $db_row, $rest_request); |
|
583 | - $entity_array = apply_filters( |
|
584 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_before_including_requested_models', |
|
585 | - $entity_array, |
|
586 | - $model, |
|
587 | - $rest_request->get_param('caps'), |
|
588 | - $rest_request, |
|
589 | - $this |
|
590 | - ); |
|
591 | - $entity_array = $this->includeRequestedModels($model, $rest_request, $entity_array, $db_row); |
|
592 | - $entity_array = apply_filters( |
|
593 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_before_inaccessible_field_removal', |
|
594 | - $entity_array, |
|
595 | - $model, |
|
596 | - $rest_request->get_param('caps'), |
|
597 | - $rest_request, |
|
598 | - $this |
|
599 | - ); |
|
600 | - $result_without_inaccessible_fields = Capabilities::filterOutInaccessibleEntityFields( |
|
601 | - $entity_array, |
|
602 | - $model, |
|
603 | - $rest_request->get_param('caps'), |
|
604 | - $this->getModelVersionInfo(), |
|
605 | - $model->get_index_primary_key_string( |
|
606 | - $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
607 | - ) |
|
608 | - ); |
|
609 | - $this->setDebugInfo( |
|
610 | - 'inaccessible fields', |
|
611 | - array_keys(array_diff_key($entity_array, $result_without_inaccessible_fields)) |
|
612 | - ); |
|
613 | - return apply_filters( |
|
614 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_return', |
|
615 | - $result_without_inaccessible_fields, |
|
616 | - $model, |
|
617 | - $rest_request->get_param('caps') |
|
618 | - ); |
|
619 | - } |
|
620 | - |
|
621 | - |
|
622 | - /** |
|
623 | - * Creates a REST entity array (JSON object we're going to return in the response, but |
|
624 | - * for now still a PHP array, but soon enough we'll call json_encode on it, don't worry), |
|
625 | - * from $wpdb->get_row( $sql, ARRAY_A) |
|
626 | - * |
|
627 | - * @param EEM_Base $model |
|
628 | - * @param array $db_row |
|
629 | - * @return array entity mostly ready for converting to JSON and sending in the response |
|
630 | - */ |
|
631 | - protected function createBareEntityFromWpdbResults(EEM_Base $model, $db_row) |
|
632 | - { |
|
633 | - $result = $model->deduce_fields_n_values_from_cols_n_values($db_row); |
|
634 | - $result = array_intersect_key( |
|
635 | - $result, |
|
636 | - $this->getModelVersionInfo()->fieldsOnModelInThisVersion($model) |
|
637 | - ); |
|
638 | - // if this is a CPT, we need to set the global $post to it, |
|
639 | - // otherwise shortcodes etc won't work properly while rendering it |
|
640 | - if ($model instanceof \EEM_CPT_Base) { |
|
641 | - $do_chevy_shuffle = true; |
|
642 | - } else { |
|
643 | - $do_chevy_shuffle = false; |
|
644 | - } |
|
645 | - if ($do_chevy_shuffle) { |
|
646 | - global $post; |
|
647 | - $old_post = $post; |
|
648 | - $post = get_post($result[ $model->primary_key_name() ]); |
|
649 | - if (! $post instanceof \WP_Post) { |
|
650 | - // well that's weird, because $result is what we JUST fetched from the database |
|
651 | - throw new RestException( |
|
652 | - 'error_fetching_post_from_database_results', |
|
653 | - esc_html__( |
|
654 | - 'An item was retrieved from the database but it\'s not a WP_Post like it should be.', |
|
655 | - 'event_espresso' |
|
656 | - ) |
|
657 | - ); |
|
658 | - } |
|
659 | - $model_object_classname = 'EE_' . $model->get_this_model_name(); |
|
660 | - $post->{$model_object_classname} = \EE_Registry::instance()->load_class( |
|
661 | - $model_object_classname, |
|
662 | - $result, |
|
663 | - false, |
|
664 | - false |
|
665 | - ); |
|
666 | - } |
|
667 | - foreach ($result as $field_name => $field_value) { |
|
668 | - $field_obj = $model->field_settings_for($field_name); |
|
669 | - if ($this->isSubclassOfOne($field_obj, $this->getModelVersionInfo()->fieldsIgnored())) { |
|
670 | - unset($result[ $field_name ]); |
|
671 | - } elseif ($this->isSubclassOfOne( |
|
672 | - $field_obj, |
|
673 | - $this->getModelVersionInfo()->fieldsThatHaveRenderedFormat() |
|
674 | - ) |
|
675 | - ) { |
|
676 | - $result[ $field_name ] = array( |
|
677 | - 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
|
678 | - 'rendered' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
|
679 | - ); |
|
680 | - } elseif ($this->isSubclassOfOne( |
|
681 | - $field_obj, |
|
682 | - $this->getModelVersionInfo()->fieldsThatHavePrettyFormat() |
|
683 | - ) |
|
684 | - ) { |
|
685 | - $result[ $field_name ] = array( |
|
686 | - 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
|
687 | - 'pretty' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
|
688 | - ); |
|
689 | - } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
690 | - $field_value = $field_obj->prepare_for_set_from_db($field_value); |
|
691 | - // if the value is null, but we're not supposed to permit null, then set to the field's default |
|
692 | - if (is_null($field_value)) { |
|
693 | - $field_value = $field_obj->getDefaultDateTimeObj(); |
|
694 | - } |
|
695 | - if (is_null($field_value)) { |
|
696 | - $gmt_date = $local_date = ModelDataTranslator::prepareFieldValuesForJson( |
|
697 | - $field_obj, |
|
698 | - $field_value, |
|
699 | - $this->getModelVersionInfo()->requestedVersion() |
|
700 | - ); |
|
701 | - } else { |
|
702 | - $timezone = $field_value->getTimezone(); |
|
703 | - EEH_DTT_Helper::setTimezone($field_value, new DateTimeZone('UTC')); |
|
704 | - $gmt_date = ModelDataTranslator::prepareFieldValuesForJson( |
|
705 | - $field_obj, |
|
706 | - $field_value, |
|
707 | - $this->getModelVersionInfo()->requestedVersion() |
|
708 | - ); |
|
709 | - EEH_DTT_Helper::setTimezone($field_value, $timezone); |
|
710 | - $local_date = ModelDataTranslator::prepareFieldValuesForJson( |
|
711 | - $field_obj, |
|
712 | - $field_value, |
|
713 | - $this->getModelVersionInfo()->requestedVersion() |
|
714 | - ); |
|
715 | - } |
|
716 | - $result[ $field_name . '_gmt' ] = $gmt_date; |
|
717 | - $result[ $field_name ] = $local_date; |
|
718 | - } else { |
|
719 | - $result[ $field_name ] = $this->prepareFieldObjValueForJson($field_obj, $field_value); |
|
720 | - } |
|
721 | - } |
|
722 | - if ($do_chevy_shuffle) { |
|
723 | - $post = $old_post; |
|
724 | - } |
|
725 | - return $result; |
|
726 | - } |
|
727 | - |
|
728 | - |
|
729 | - /** |
|
730 | - * Takes a value all the way from the DB representation, to the model object's representation, to the |
|
731 | - * user-facing PHP representation, to the REST API representation. (Assumes you've already taken from the DB |
|
732 | - * representation using $field_obj->prepare_for_set_from_db()) |
|
733 | - * |
|
734 | - * @param EE_Model_Field_Base $field_obj |
|
735 | - * @param mixed $value as it's stored on a model object |
|
736 | - * @param string $format valid values are 'normal' (default), 'pretty', 'datetime_obj' |
|
737 | - * @return mixed |
|
738 | - * @throws ObjectDetectedException if $value contains a PHP object |
|
739 | - */ |
|
740 | - protected function prepareFieldObjValueForJson(EE_Model_Field_Base $field_obj, $value, $format = 'normal') |
|
741 | - { |
|
742 | - $value = $field_obj->prepare_for_set_from_db($value); |
|
743 | - switch ($format) { |
|
744 | - case 'pretty': |
|
745 | - $value = $field_obj->prepare_for_pretty_echoing($value); |
|
746 | - break; |
|
747 | - case 'normal': |
|
748 | - default: |
|
749 | - $value = $field_obj->prepare_for_get($value); |
|
750 | - break; |
|
751 | - } |
|
752 | - return ModelDataTranslator::prepareFieldValuesForJson( |
|
753 | - $field_obj, |
|
754 | - $value, |
|
755 | - $this->getModelVersionInfo()->requestedVersion() |
|
756 | - ); |
|
757 | - } |
|
758 | - |
|
759 | - |
|
760 | - /** |
|
761 | - * Adds a few extra fields to the entity response |
|
762 | - * |
|
763 | - * @param EEM_Base $model |
|
764 | - * @param array $db_row |
|
765 | - * @param array $entity_array |
|
766 | - * @return array modified entity |
|
767 | - */ |
|
768 | - protected function addExtraFields(EEM_Base $model, $db_row, $entity_array) |
|
769 | - { |
|
770 | - if ($model instanceof EEM_CPT_Base) { |
|
771 | - $entity_array['link'] = get_permalink($db_row[ $model->get_primary_key_field()->get_qualified_column() ]); |
|
772 | - } |
|
773 | - return $entity_array; |
|
774 | - } |
|
775 | - |
|
776 | - |
|
777 | - /** |
|
778 | - * Gets links we want to add to the response |
|
779 | - * |
|
780 | - * @global \WP_REST_Server $wp_rest_server |
|
781 | - * @param EEM_Base $model |
|
782 | - * @param array $db_row |
|
783 | - * @param array $entity_array |
|
784 | - * @return array the _links item in the entity |
|
785 | - */ |
|
786 | - protected function getEntityLinks($model, $db_row, $entity_array) |
|
787 | - { |
|
788 | - // add basic links |
|
789 | - $links = array(); |
|
790 | - if ($model->has_primary_key_field()) { |
|
791 | - $links['self'] = array( |
|
792 | - array( |
|
793 | - 'href' => $this->getVersionedLinkTo( |
|
794 | - EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
795 | - . '/' |
|
796 | - . $entity_array[ $model->primary_key_name() ] |
|
797 | - ), |
|
798 | - ), |
|
799 | - ); |
|
800 | - } |
|
801 | - $links['collection'] = array( |
|
802 | - array( |
|
803 | - 'href' => $this->getVersionedLinkTo( |
|
804 | - EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
805 | - ), |
|
806 | - ), |
|
807 | - ); |
|
808 | - // add links to related models |
|
809 | - if ($model->has_primary_key_field()) { |
|
810 | - foreach ($this->getModelVersionInfo()->relationSettings($model) as $relation_name => $relation_obj) { |
|
811 | - $related_model_part = Read::getRelatedEntityName($relation_name, $relation_obj); |
|
812 | - $links[ EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part ] = array( |
|
813 | - array( |
|
814 | - 'href' => $this->getVersionedLinkTo( |
|
815 | - EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
816 | - . '/' |
|
817 | - . $entity_array[ $model->primary_key_name() ] |
|
818 | - . '/' |
|
819 | - . $related_model_part |
|
820 | - ), |
|
821 | - 'single' => $relation_obj instanceof EE_Belongs_To_Relation ? true : false, |
|
822 | - ), |
|
823 | - ); |
|
824 | - } |
|
825 | - } |
|
826 | - return $links; |
|
827 | - } |
|
828 | - |
|
829 | - |
|
830 | - /** |
|
831 | - * Adds the included models indicated in the request to the entity provided |
|
832 | - * |
|
833 | - * @param EEM_Base $model |
|
834 | - * @param WP_REST_Request $rest_request |
|
835 | - * @param array $entity_array |
|
836 | - * @param array $db_row |
|
837 | - * @return array the modified entity |
|
838 | - */ |
|
839 | - protected function includeRequestedModels( |
|
840 | - EEM_Base $model, |
|
841 | - WP_REST_Request $rest_request, |
|
842 | - $entity_array, |
|
843 | - $db_row = array() |
|
844 | - ) { |
|
845 | - // if $db_row not included, hope the entity array has what we need |
|
846 | - if (! $db_row) { |
|
847 | - $db_row = $entity_array; |
|
848 | - } |
|
849 | - $includes_for_this_model = $this->explodeAndGetItemsPrefixedWith($rest_request->get_param('include'), ''); |
|
850 | - $includes_for_this_model = $this->removeModelNamesFromArray($includes_for_this_model); |
|
851 | - // if they passed in * or didn't specify any includes, return everything |
|
852 | - if (! in_array('*', $includes_for_this_model) |
|
853 | - && ! empty($includes_for_this_model) |
|
854 | - ) { |
|
855 | - if ($model->has_primary_key_field()) { |
|
856 | - // always include the primary key. ya just gotta know that at least |
|
857 | - $includes_for_this_model[] = $model->primary_key_name(); |
|
858 | - } |
|
859 | - if ($this->explodeAndGetItemsPrefixedWith($rest_request->get_param('calculate'), '')) { |
|
860 | - $includes_for_this_model[] = '_calculated_fields'; |
|
861 | - } |
|
862 | - $entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model)); |
|
863 | - } |
|
864 | - $relation_settings = $this->getModelVersionInfo()->relationSettings($model); |
|
865 | - foreach ($relation_settings as $relation_name => $relation_obj) { |
|
866 | - $related_fields_to_include = $this->explodeAndGetItemsPrefixedWith( |
|
867 | - $rest_request->get_param('include'), |
|
868 | - $relation_name |
|
869 | - ); |
|
870 | - $related_fields_to_calculate = $this->explodeAndGetItemsPrefixedWith( |
|
871 | - $rest_request->get_param('calculate'), |
|
872 | - $relation_name |
|
873 | - ); |
|
874 | - // did they specify they wanted to include a related model, or |
|
875 | - // specific fields from a related model? |
|
876 | - // or did they specify to calculate a field from a related model? |
|
877 | - if ($related_fields_to_include || $related_fields_to_calculate) { |
|
878 | - // if so, we should include at least some part of the related model |
|
879 | - $pretend_related_request = new WP_REST_Request(); |
|
880 | - $pretend_related_request->set_query_params( |
|
881 | - array( |
|
882 | - 'caps' => $rest_request->get_param('caps'), |
|
883 | - 'include' => $related_fields_to_include, |
|
884 | - 'calculate' => $related_fields_to_calculate, |
|
885 | - ) |
|
886 | - ); |
|
887 | - $pretend_related_request->add_header('no_rest_headers', true); |
|
888 | - $primary_model_query_params = $model->alter_query_params_to_restrict_by_ID( |
|
889 | - $model->get_index_primary_key_string( |
|
890 | - $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
891 | - ) |
|
892 | - ); |
|
893 | - $related_results = $this->getEntitiesFromRelationUsingModelQueryParams( |
|
894 | - $primary_model_query_params, |
|
895 | - $relation_obj, |
|
896 | - $pretend_related_request |
|
897 | - ); |
|
898 | - $entity_array[ Read::getRelatedEntityName($relation_name, $relation_obj) ] = $related_results |
|
899 | - instanceof |
|
900 | - WP_Error |
|
901 | - ? null |
|
902 | - : $related_results; |
|
903 | - } |
|
904 | - } |
|
905 | - return $entity_array; |
|
906 | - } |
|
907 | - |
|
908 | - |
|
909 | - /** |
|
910 | - * Returns a new array with all the names of models removed. Eg |
|
911 | - * array( 'Event', 'Datetime.*', 'foobar' ) would become array( 'Datetime.*', 'foobar' ) |
|
912 | - * |
|
913 | - * @param array $arr |
|
914 | - * @return array |
|
915 | - */ |
|
916 | - private function removeModelNamesFromArray($arr) |
|
917 | - { |
|
918 | - return array_diff($arr, array_keys(EE_Registry::instance()->non_abstract_db_models)); |
|
919 | - } |
|
920 | - |
|
921 | - |
|
922 | - /** |
|
923 | - * Gets the calculated fields for the response |
|
924 | - * |
|
925 | - * @param EEM_Base $model |
|
926 | - * @param array $wpdb_row |
|
927 | - * @param WP_REST_Request $rest_request |
|
928 | - * @return \stdClass the _calculations item in the entity |
|
929 | - * @throws ObjectDetectedException if a default value has a PHP object, which should never do (and if we |
|
930 | - * did, let's know about it ASAP, so let the exception bubble up) |
|
931 | - */ |
|
932 | - protected function getEntityCalculations($model, $wpdb_row, $rest_request) |
|
933 | - { |
|
934 | - $calculated_fields = $this->explodeAndGetItemsPrefixedWith( |
|
935 | - $rest_request->get_param('calculate'), |
|
936 | - '' |
|
937 | - ); |
|
938 | - // note: setting calculate=* doesn't do anything |
|
939 | - $calculated_fields_to_return = new \stdClass(); |
|
940 | - foreach ($calculated_fields as $field_to_calculate) { |
|
941 | - try { |
|
942 | - $calculated_fields_to_return->$field_to_calculate = ModelDataTranslator::prepareFieldValueForJson( |
|
943 | - null, |
|
944 | - $this->fields_calculator->retrieveCalculatedFieldValue( |
|
945 | - $model, |
|
946 | - $field_to_calculate, |
|
947 | - $wpdb_row, |
|
948 | - $rest_request, |
|
949 | - $this |
|
950 | - ), |
|
951 | - $this->getModelVersionInfo()->requestedVersion() |
|
952 | - ); |
|
953 | - } catch (RestException $e) { |
|
954 | - // if we don't have permission to read it, just leave it out. but let devs know about the problem |
|
955 | - $this->setResponseHeader( |
|
956 | - 'Notices-Field-Calculation-Errors[' |
|
957 | - . $e->getStringCode() |
|
958 | - . '][' |
|
959 | - . $model->get_this_model_name() |
|
960 | - . '][' |
|
961 | - . $field_to_calculate |
|
962 | - . ']', |
|
963 | - $e->getMessage(), |
|
964 | - true |
|
965 | - ); |
|
966 | - } |
|
967 | - } |
|
968 | - return $calculated_fields_to_return; |
|
969 | - } |
|
970 | - |
|
971 | - |
|
972 | - /** |
|
973 | - * Gets the full URL to the resource, taking the requested version into account |
|
974 | - * |
|
975 | - * @param string $link_part_after_version_and_slash eg "events/10/datetimes" |
|
976 | - * @return string url eg "http://mysite.com/wp-json/ee/v4.6/events/10/datetimes" |
|
977 | - */ |
|
978 | - public function getVersionedLinkTo($link_part_after_version_and_slash) |
|
979 | - { |
|
980 | - return rest_url( |
|
981 | - EED_Core_Rest_Api::get_versioned_route_to( |
|
982 | - $link_part_after_version_and_slash, |
|
983 | - $this->getModelVersionInfo()->requestedVersion() |
|
984 | - ) |
|
985 | - ); |
|
986 | - } |
|
987 | - |
|
988 | - |
|
989 | - /** |
|
990 | - * Gets the correct lowercase name for the relation in the API according |
|
991 | - * to the relation's type |
|
992 | - * |
|
993 | - * @param string $relation_name |
|
994 | - * @param \EE_Model_Relation_Base $relation_obj |
|
995 | - * @return string |
|
996 | - */ |
|
997 | - public static function getRelatedEntityName($relation_name, $relation_obj) |
|
998 | - { |
|
999 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
1000 | - return strtolower($relation_name); |
|
1001 | - } else { |
|
1002 | - return EEH_Inflector::pluralize_and_lower($relation_name); |
|
1003 | - } |
|
1004 | - } |
|
1005 | - |
|
1006 | - |
|
1007 | - /** |
|
1008 | - * Gets the one model object with the specified id for the specified model |
|
1009 | - * |
|
1010 | - * @param EEM_Base $model |
|
1011 | - * @param WP_REST_Request $request |
|
1012 | - * @return array|WP_Error |
|
1013 | - */ |
|
1014 | - public function getEntityFromModel($model, $request) |
|
1015 | - { |
|
1016 | - $context = $this->validateContext($request->get_param('caps')); |
|
1017 | - return $this->getOneOrReportPermissionError($model, $request, $context); |
|
1018 | - } |
|
1019 | - |
|
1020 | - |
|
1021 | - /** |
|
1022 | - * If a context is provided which isn't valid, maybe it was added in a future |
|
1023 | - * version so just treat it as a default read |
|
1024 | - * |
|
1025 | - * @param string $context |
|
1026 | - * @return string array key of EEM_Base::cap_contexts_to_cap_action_map() |
|
1027 | - */ |
|
1028 | - public function validateContext($context) |
|
1029 | - { |
|
1030 | - if (! $context) { |
|
1031 | - $context = EEM_Base::caps_read; |
|
1032 | - } |
|
1033 | - $valid_contexts = EEM_Base::valid_cap_contexts(); |
|
1034 | - if (in_array($context, $valid_contexts)) { |
|
1035 | - return $context; |
|
1036 | - } else { |
|
1037 | - return EEM_Base::caps_read; |
|
1038 | - } |
|
1039 | - } |
|
1040 | - |
|
1041 | - |
|
1042 | - /** |
|
1043 | - * Verifies the passed in value is an allowable default where conditions value. |
|
1044 | - * |
|
1045 | - * @param $default_query_params |
|
1046 | - * @return string |
|
1047 | - */ |
|
1048 | - public function validateDefaultQueryParams($default_query_params) |
|
1049 | - { |
|
1050 | - $valid_default_where_conditions_for_api_calls = array( |
|
1051 | - EEM_Base::default_where_conditions_all, |
|
1052 | - EEM_Base::default_where_conditions_minimum_all, |
|
1053 | - EEM_Base::default_where_conditions_minimum_others, |
|
1054 | - ); |
|
1055 | - if (! $default_query_params) { |
|
1056 | - $default_query_params = EEM_Base::default_where_conditions_all; |
|
1057 | - } |
|
1058 | - if (in_array( |
|
1059 | - $default_query_params, |
|
1060 | - $valid_default_where_conditions_for_api_calls, |
|
1061 | - true |
|
1062 | - )) { |
|
1063 | - return $default_query_params; |
|
1064 | - } else { |
|
1065 | - return EEM_Base::default_where_conditions_all; |
|
1066 | - } |
|
1067 | - } |
|
1068 | - |
|
1069 | - |
|
1070 | - /** |
|
1071 | - * Translates API filter get parameter into $query_params array used by EEM_Base::get_all(). |
|
1072 | - * Note: right now the query parameter keys for fields (and related fields) |
|
1073 | - * can be left as-is, but it's quite possible this will change someday. |
|
1074 | - * Also, this method's contents might be candidate for moving to Model_Data_Translator |
|
1075 | - * |
|
1076 | - * @param EEM_Base $model |
|
1077 | - * @param array $query_parameters from $_GET parameter @see Read:handle_request_get_all |
|
1078 | - * @return array like what EEM_Base::get_all() expects or FALSE to indicate |
|
1079 | - * that absolutely no results should be returned |
|
1080 | - * @throws EE_Error |
|
1081 | - * @throws RestException |
|
1082 | - */ |
|
1083 | - public function createModelQueryParams($model, $query_parameters) |
|
1084 | - { |
|
1085 | - $model_query_params = array(); |
|
1086 | - if (isset($query_parameters['where'])) { |
|
1087 | - $model_query_params[0] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
1088 | - $query_parameters['where'], |
|
1089 | - $model, |
|
1090 | - $this->getModelVersionInfo()->requestedVersion() |
|
1091 | - ); |
|
1092 | - } |
|
1093 | - if (isset($query_parameters['order_by'])) { |
|
1094 | - $order_by = $query_parameters['order_by']; |
|
1095 | - } elseif (isset($query_parameters['orderby'])) { |
|
1096 | - $order_by = $query_parameters['orderby']; |
|
1097 | - } else { |
|
1098 | - $order_by = null; |
|
1099 | - } |
|
1100 | - if ($order_by !== null) { |
|
1101 | - if (is_array($order_by)) { |
|
1102 | - $order_by = ModelDataTranslator::prepareFieldNamesInArrayKeysFromJson($order_by); |
|
1103 | - } else { |
|
1104 | - // it's a single item |
|
1105 | - $order_by = ModelDataTranslator::prepareFieldNameFromJson($order_by); |
|
1106 | - } |
|
1107 | - $model_query_params['order_by'] = $order_by; |
|
1108 | - } |
|
1109 | - if (isset($query_parameters['group_by'])) { |
|
1110 | - $group_by = $query_parameters['group_by']; |
|
1111 | - } elseif (isset($query_parameters['groupby'])) { |
|
1112 | - $group_by = $query_parameters['groupby']; |
|
1113 | - } else { |
|
1114 | - $group_by = array_keys($model->get_combined_primary_key_fields()); |
|
1115 | - } |
|
1116 | - // make sure they're all real names |
|
1117 | - if (is_array($group_by)) { |
|
1118 | - $group_by = ModelDataTranslator::prepareFieldNamesFromJson($group_by); |
|
1119 | - } |
|
1120 | - if ($group_by !== null) { |
|
1121 | - $model_query_params['group_by'] = $group_by; |
|
1122 | - } |
|
1123 | - if (isset($query_parameters['having'])) { |
|
1124 | - $model_query_params['having'] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
1125 | - $query_parameters['having'], |
|
1126 | - $model, |
|
1127 | - $this->getModelVersionInfo()->requestedVersion() |
|
1128 | - ); |
|
1129 | - } |
|
1130 | - if (isset($query_parameters['order'])) { |
|
1131 | - $model_query_params['order'] = $query_parameters['order']; |
|
1132 | - } |
|
1133 | - if (isset($query_parameters['mine'])) { |
|
1134 | - $model_query_params = $model->alter_query_params_to_only_include_mine($model_query_params); |
|
1135 | - } |
|
1136 | - if (isset($query_parameters['limit'])) { |
|
1137 | - // limit should be either a string like '23' or '23,43', or an array with two items in it |
|
1138 | - if (! is_array($query_parameters['limit'])) { |
|
1139 | - $limit_array = explode(',', (string) $query_parameters['limit']); |
|
1140 | - } else { |
|
1141 | - $limit_array = $query_parameters['limit']; |
|
1142 | - } |
|
1143 | - $sanitized_limit = array(); |
|
1144 | - foreach ($limit_array as $key => $limit_part) { |
|
1145 | - if ($this->debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1146 | - throw new EE_Error( |
|
1147 | - sprintf( |
|
1148 | - __( |
|
1149 | - // @codingStandardsIgnoreStart |
|
1150 | - 'An invalid limit filter was provided. It was: %s. If the EE4 JSON REST API weren\'t in debug mode, this message would not appear.', |
|
1151 | - // @codingStandardsIgnoreEnd |
|
1152 | - 'event_espresso' |
|
1153 | - ), |
|
1154 | - wp_json_encode($query_parameters['limit']) |
|
1155 | - ) |
|
1156 | - ); |
|
1157 | - } |
|
1158 | - $sanitized_limit[] = (int) $limit_part; |
|
1159 | - } |
|
1160 | - $model_query_params['limit'] = implode(',', $sanitized_limit); |
|
1161 | - } else { |
|
1162 | - $model_query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
|
1163 | - } |
|
1164 | - if (isset($query_parameters['caps'])) { |
|
1165 | - $model_query_params['caps'] = $this->validateContext($query_parameters['caps']); |
|
1166 | - } else { |
|
1167 | - $model_query_params['caps'] = EEM_Base::caps_read; |
|
1168 | - } |
|
1169 | - if (isset($query_parameters['default_where_conditions'])) { |
|
1170 | - $model_query_params['default_where_conditions'] = $this->validateDefaultQueryParams( |
|
1171 | - $query_parameters['default_where_conditions'] |
|
1172 | - ); |
|
1173 | - } |
|
1174 | - return apply_filters('FHEE__Read__create_model_query_params', $model_query_params, $query_parameters, $model); |
|
1175 | - } |
|
1176 | - |
|
1177 | - |
|
1178 | - /** |
|
1179 | - * Changes the REST-style query params for use in the models |
|
1180 | - * |
|
1181 | - * @deprecated |
|
1182 | - * @param EEM_Base $model |
|
1183 | - * @param array $query_params sub-array from @see EEM_Base::get_all() |
|
1184 | - * @return array |
|
1185 | - */ |
|
1186 | - public function prepareRestQueryParamsKeyForModels($model, $query_params) |
|
1187 | - { |
|
1188 | - $model_ready_query_params = array(); |
|
1189 | - foreach ($query_params as $key => $value) { |
|
1190 | - if (is_array($value)) { |
|
1191 | - $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsKeyForModels($model, $value); |
|
1192 | - } else { |
|
1193 | - $model_ready_query_params[ $key ] = $value; |
|
1194 | - } |
|
1195 | - } |
|
1196 | - return $model_ready_query_params; |
|
1197 | - } |
|
1198 | - |
|
1199 | - |
|
1200 | - /** |
|
1201 | - * @deprecated instead use ModelDataTranslator::prepareFieldValuesFromJson() |
|
1202 | - * @param $model |
|
1203 | - * @param $query_params |
|
1204 | - * @return array |
|
1205 | - */ |
|
1206 | - public function prepareRestQueryParamsValuesForModels($model, $query_params) |
|
1207 | - { |
|
1208 | - $model_ready_query_params = array(); |
|
1209 | - foreach ($query_params as $key => $value) { |
|
1210 | - if (is_array($value)) { |
|
1211 | - $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsValuesForModels($model, $value); |
|
1212 | - } else { |
|
1213 | - $model_ready_query_params[ $key ] = $value; |
|
1214 | - } |
|
1215 | - } |
|
1216 | - return $model_ready_query_params; |
|
1217 | - } |
|
1218 | - |
|
1219 | - |
|
1220 | - /** |
|
1221 | - * Explodes the string on commas, and only returns items with $prefix followed by a period. |
|
1222 | - * If no prefix is specified, returns items with no period. |
|
1223 | - * |
|
1224 | - * @param string|array $string_to_explode eg "jibba,jabba, blah, blah, blah" or array('jibba', 'jabba' ) |
|
1225 | - * @param string $prefix "Event" or "foobar" |
|
1226 | - * @return array $string_to_exploded exploded on COMMAS, and if a prefix was specified |
|
1227 | - * we only return strings starting with that and a period; if no prefix was |
|
1228 | - * specified we return all items containing NO periods |
|
1229 | - */ |
|
1230 | - public function explodeAndGetItemsPrefixedWith($string_to_explode, $prefix) |
|
1231 | - { |
|
1232 | - if (is_string($string_to_explode)) { |
|
1233 | - $exploded_contents = explode(',', $string_to_explode); |
|
1234 | - } elseif (is_array($string_to_explode)) { |
|
1235 | - $exploded_contents = $string_to_explode; |
|
1236 | - } else { |
|
1237 | - $exploded_contents = array(); |
|
1238 | - } |
|
1239 | - // if the string was empty, we want an empty array |
|
1240 | - $exploded_contents = array_filter($exploded_contents); |
|
1241 | - $contents_with_prefix = array(); |
|
1242 | - foreach ($exploded_contents as $item) { |
|
1243 | - $item = trim($item); |
|
1244 | - // if no prefix was provided, so we look for items with no "." in them |
|
1245 | - if (! $prefix) { |
|
1246 | - // does this item have a period? |
|
1247 | - if (strpos($item, '.') === false) { |
|
1248 | - // if not, then its what we're looking for |
|
1249 | - $contents_with_prefix[] = $item; |
|
1250 | - } |
|
1251 | - } elseif (strpos($item, $prefix . '.') === 0) { |
|
1252 | - // this item has the prefix and a period, grab it |
|
1253 | - $contents_with_prefix[] = substr( |
|
1254 | - $item, |
|
1255 | - strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1256 | - ); |
|
1257 | - } elseif ($item === $prefix) { |
|
1258 | - // this item is JUST the prefix |
|
1259 | - // so let's grab everything after, which is a blank string |
|
1260 | - $contents_with_prefix[] = ''; |
|
1261 | - } |
|
1262 | - } |
|
1263 | - return $contents_with_prefix; |
|
1264 | - } |
|
1265 | - |
|
1266 | - |
|
1267 | - /** |
|
1268 | - * @deprecated since 4.8.36.rc.001 You should instead use Read::explode_and_get_items_prefixed_with. |
|
1269 | - * Deprecated because its return values were really quite confusing- sometimes it returned |
|
1270 | - * an empty array (when the include string was blank or '*') or sometimes it returned |
|
1271 | - * array('*') (when you provided a model and a model of that kind was found). |
|
1272 | - * Parses the $include_string so we fetch all the field names relating to THIS model |
|
1273 | - * (ie have NO period in them), or for the provided model (ie start with the model |
|
1274 | - * name and then a period). |
|
1275 | - * @param string $include_string @see Read:handle_request_get_all |
|
1276 | - * @param string $model_name |
|
1277 | - * @return array of fields for this model. If $model_name is provided, then |
|
1278 | - * the fields for that model, with the model's name removed from each. |
|
1279 | - * If $include_string was blank or '*' returns an empty array |
|
1280 | - */ |
|
1281 | - public function extractIncludesForThisModel($include_string, $model_name = null) |
|
1282 | - { |
|
1283 | - if (is_array($include_string)) { |
|
1284 | - $include_string = implode(',', $include_string); |
|
1285 | - } |
|
1286 | - if ($include_string === '*' || $include_string === '') { |
|
1287 | - return array(); |
|
1288 | - } |
|
1289 | - $includes = explode(',', $include_string); |
|
1290 | - $extracted_fields_to_include = array(); |
|
1291 | - if ($model_name) { |
|
1292 | - foreach ($includes as $field_to_include) { |
|
1293 | - $field_to_include = trim($field_to_include); |
|
1294 | - if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1295 | - // found the model name at the exact start |
|
1296 | - $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1297 | - $extracted_fields_to_include[] = $field_sans_model_name; |
|
1298 | - } elseif ($field_to_include == $model_name) { |
|
1299 | - $extracted_fields_to_include[] = '*'; |
|
1300 | - } |
|
1301 | - } |
|
1302 | - } else { |
|
1303 | - // look for ones with no period |
|
1304 | - foreach ($includes as $field_to_include) { |
|
1305 | - $field_to_include = trim($field_to_include); |
|
1306 | - if (strpos($field_to_include, '.') === false |
|
1307 | - && ! $this->getModelVersionInfo()->isModelNameInThisVersion($field_to_include) |
|
1308 | - ) { |
|
1309 | - $extracted_fields_to_include[] = $field_to_include; |
|
1310 | - } |
|
1311 | - } |
|
1312 | - } |
|
1313 | - return $extracted_fields_to_include; |
|
1314 | - } |
|
1315 | - |
|
1316 | - |
|
1317 | - /** |
|
1318 | - * Gets the single item using the model according to the request in the context given, otherwise |
|
1319 | - * returns that it's inaccessible to the current user |
|
1320 | - * |
|
1321 | - * @param EEM_Base $model |
|
1322 | - * @param WP_REST_Request $request |
|
1323 | - * @param null $context |
|
1324 | - * @return array|WP_Error |
|
1325 | - */ |
|
1326 | - public function getOneOrReportPermissionError(EEM_Base $model, WP_REST_Request $request, $context = null) |
|
1327 | - { |
|
1328 | - $query_params = array(array($model->primary_key_name() => $request->get_param('id')), 'limit' => 1); |
|
1329 | - if ($model instanceof \EEM_Soft_Delete_Base) { |
|
1330 | - $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
1331 | - } |
|
1332 | - $restricted_query_params = $query_params; |
|
1333 | - $restricted_query_params['caps'] = $context; |
|
1334 | - $this->setDebugInfo('model query params', $restricted_query_params); |
|
1335 | - $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
|
1336 | - if (! empty($model_rows)) { |
|
1337 | - return $this->createEntityFromWpdbResult( |
|
1338 | - $model, |
|
1339 | - array_shift($model_rows), |
|
1340 | - $request |
|
1341 | - ); |
|
1342 | - } else { |
|
1343 | - // ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
|
1344 | - $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
1345 | - $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
|
1346 | - if (! empty($model_rows_found_sans_restrictions)) { |
|
1347 | - // you got shafted- it existed but we didn't want to tell you! |
|
1348 | - return new WP_Error( |
|
1349 | - 'rest_user_cannot_' . $context, |
|
1350 | - sprintf( |
|
1351 | - __('Sorry, you cannot %1$s this %2$s. Missing permissions are: %3$s', 'event_espresso'), |
|
1352 | - $context, |
|
1353 | - strtolower($model->get_this_model_name()), |
|
1354 | - Capabilities::getMissingPermissionsString( |
|
1355 | - $model, |
|
1356 | - $context |
|
1357 | - ) |
|
1358 | - ), |
|
1359 | - array('status' => 403) |
|
1360 | - ); |
|
1361 | - } else { |
|
1362 | - // it's not you. It just doesn't exist |
|
1363 | - return new WP_Error( |
|
1364 | - sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
1365 | - sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
1366 | - array('status' => 404) |
|
1367 | - ); |
|
1368 | - } |
|
1369 | - } |
|
1370 | - } |
|
38 | + /** |
|
39 | + * @var CalculatedModelFields |
|
40 | + */ |
|
41 | + protected $fields_calculator; |
|
42 | + |
|
43 | + |
|
44 | + /** |
|
45 | + * Read constructor. |
|
46 | + */ |
|
47 | + public function __construct() |
|
48 | + { |
|
49 | + parent::__construct(); |
|
50 | + $this->fields_calculator = new CalculatedModelFields(); |
|
51 | + } |
|
52 | + |
|
53 | + |
|
54 | + /** |
|
55 | + * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
56 | + * |
|
57 | + * @param WP_REST_Request $request |
|
58 | + * @param string $version |
|
59 | + * @param string $model_name |
|
60 | + * @return \WP_REST_Response|WP_Error |
|
61 | + */ |
|
62 | + public static function handleRequestGetAll(WP_REST_Request $request, $version, $model_name) |
|
63 | + { |
|
64 | + $controller = new Read(); |
|
65 | + try { |
|
66 | + $controller->setRequestedVersion($version); |
|
67 | + if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
68 | + return $controller->sendResponse( |
|
69 | + new WP_Error( |
|
70 | + 'endpoint_parsing_error', |
|
71 | + sprintf( |
|
72 | + __( |
|
73 | + 'There is no model for endpoint %s. Please contact event espresso support', |
|
74 | + 'event_espresso' |
|
75 | + ), |
|
76 | + $model_name |
|
77 | + ) |
|
78 | + ) |
|
79 | + ); |
|
80 | + } |
|
81 | + return $controller->sendResponse( |
|
82 | + $controller->getEntitiesFromModel( |
|
83 | + $controller->getModelVersionInfo()->loadModel($model_name), |
|
84 | + $request |
|
85 | + ) |
|
86 | + ); |
|
87 | + } catch (Exception $e) { |
|
88 | + return $controller->sendResponse($e); |
|
89 | + } |
|
90 | + } |
|
91 | + |
|
92 | + |
|
93 | + /** |
|
94 | + * Prepares and returns schema for any OPTIONS request. |
|
95 | + * |
|
96 | + * @param string $version The API endpoint version being used. |
|
97 | + * @param string $model_name Something like `Event` or `Registration` |
|
98 | + * @return array |
|
99 | + */ |
|
100 | + public static function handleSchemaRequest($version, $model_name) |
|
101 | + { |
|
102 | + $controller = new Read(); |
|
103 | + try { |
|
104 | + $controller->setRequestedVersion($version); |
|
105 | + if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
106 | + return array(); |
|
107 | + } |
|
108 | + // get the model for this version |
|
109 | + $model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
110 | + $model_schema = new JsonModelSchema($model); |
|
111 | + return $model_schema->getModelSchemaForRelations( |
|
112 | + $controller->getModelVersionInfo()->relationSettings($model), |
|
113 | + $controller->customizeSchemaForRestResponse( |
|
114 | + $model, |
|
115 | + $model_schema->getModelSchemaForFields( |
|
116 | + $controller->getModelVersionInfo()->fieldsOnModelInThisVersion($model), |
|
117 | + $model_schema->getInitialSchemaStructure() |
|
118 | + ) |
|
119 | + ) |
|
120 | + ); |
|
121 | + } catch (Exception $e) { |
|
122 | + return array(); |
|
123 | + } |
|
124 | + } |
|
125 | + |
|
126 | + |
|
127 | + /** |
|
128 | + * This loops through each field in the given schema for the model and does the following: |
|
129 | + * - add any extra fields that are REST API specific and related to existing fields. |
|
130 | + * - transform default values into the correct format for a REST API response. |
|
131 | + * |
|
132 | + * @param EEM_Base $model |
|
133 | + * @param array $schema |
|
134 | + * @return array The final schema. |
|
135 | + */ |
|
136 | + protected function customizeSchemaForRestResponse(EEM_Base $model, array $schema) |
|
137 | + { |
|
138 | + foreach ($this->getModelVersionInfo()->fieldsOnModelInThisVersion($model) as $field_name => $field) { |
|
139 | + $schema = $this->translateDefaultsForRestResponse( |
|
140 | + $field_name, |
|
141 | + $field, |
|
142 | + $this->maybeAddExtraFieldsToSchema($field_name, $field, $schema) |
|
143 | + ); |
|
144 | + } |
|
145 | + return $schema; |
|
146 | + } |
|
147 | + |
|
148 | + |
|
149 | + /** |
|
150 | + * This is used to ensure that the 'default' value set in the schema response is formatted correctly for the REST |
|
151 | + * response. |
|
152 | + * |
|
153 | + * @param $field_name |
|
154 | + * @param EE_Model_Field_Base $field |
|
155 | + * @param array $schema |
|
156 | + * @return array |
|
157 | + * @throws ObjectDetectedException if a default value has a PHP object, which should never do (and if we |
|
158 | + * did, let's know about it ASAP, so let the exception bubble up) |
|
159 | + */ |
|
160 | + protected function translateDefaultsForRestResponse($field_name, EE_Model_Field_Base $field, array $schema) |
|
161 | + { |
|
162 | + if (isset($schema['properties'][ $field_name ]['default'])) { |
|
163 | + if (is_array($schema['properties'][ $field_name ]['default'])) { |
|
164 | + foreach ($schema['properties'][ $field_name ]['default'] as $default_key => $default_value) { |
|
165 | + if ($default_key === 'raw') { |
|
166 | + $schema['properties'][ $field_name ]['default'][ $default_key ] = |
|
167 | + ModelDataTranslator::prepareFieldValueForJson( |
|
168 | + $field, |
|
169 | + $default_value, |
|
170 | + $this->getModelVersionInfo()->requestedVersion() |
|
171 | + ); |
|
172 | + } |
|
173 | + } |
|
174 | + } else { |
|
175 | + $schema['properties'][ $field_name ]['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
176 | + $field, |
|
177 | + $schema['properties'][ $field_name ]['default'], |
|
178 | + $this->getModelVersionInfo()->requestedVersion() |
|
179 | + ); |
|
180 | + } |
|
181 | + } |
|
182 | + return $schema; |
|
183 | + } |
|
184 | + |
|
185 | + |
|
186 | + /** |
|
187 | + * Adds additional fields to the schema |
|
188 | + * The REST API returns a GMT value field for each datetime field in the resource. Thus the description about this |
|
189 | + * needs to be added to the schema. |
|
190 | + * |
|
191 | + * @param $field_name |
|
192 | + * @param EE_Model_Field_Base $field |
|
193 | + * @param array $schema |
|
194 | + * @return array |
|
195 | + */ |
|
196 | + protected function maybeAddExtraFieldsToSchema($field_name, EE_Model_Field_Base $field, array $schema) |
|
197 | + { |
|
198 | + if ($field instanceof EE_Datetime_Field) { |
|
199 | + $schema['properties'][ $field_name . '_gmt' ] = $field->getSchema(); |
|
200 | + // modify the description |
|
201 | + $schema['properties'][ $field_name . '_gmt' ]['description'] = sprintf( |
|
202 | + esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
|
203 | + wp_specialchars_decode($field->get_nicename(), ENT_QUOTES) |
|
204 | + ); |
|
205 | + } |
|
206 | + return $schema; |
|
207 | + } |
|
208 | + |
|
209 | + |
|
210 | + /** |
|
211 | + * Used to figure out the route from the request when a `WP_REST_Request` object is not available |
|
212 | + * |
|
213 | + * @return string |
|
214 | + */ |
|
215 | + protected function getRouteFromRequest() |
|
216 | + { |
|
217 | + if (isset($GLOBALS['wp']) |
|
218 | + && $GLOBALS['wp'] instanceof \WP |
|
219 | + && isset($GLOBALS['wp']->query_vars['rest_route']) |
|
220 | + ) { |
|
221 | + return $GLOBALS['wp']->query_vars['rest_route']; |
|
222 | + } else { |
|
223 | + return isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/'; |
|
224 | + } |
|
225 | + } |
|
226 | + |
|
227 | + |
|
228 | + /** |
|
229 | + * Gets a single entity related to the model indicated in the path and its id |
|
230 | + * |
|
231 | + * @param WP_REST_Request $request |
|
232 | + * @param string $version |
|
233 | + * @param string $model_name |
|
234 | + * @return \WP_REST_Response|WP_Error |
|
235 | + */ |
|
236 | + public static function handleRequestGetOne(WP_REST_Request $request, $version, $model_name) |
|
237 | + { |
|
238 | + $controller = new Read(); |
|
239 | + try { |
|
240 | + $controller->setRequestedVersion($version); |
|
241 | + if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
242 | + return $controller->sendResponse( |
|
243 | + new WP_Error( |
|
244 | + 'endpoint_parsing_error', |
|
245 | + sprintf( |
|
246 | + __( |
|
247 | + 'There is no model for endpoint %s. Please contact event espresso support', |
|
248 | + 'event_espresso' |
|
249 | + ), |
|
250 | + $model_name |
|
251 | + ) |
|
252 | + ) |
|
253 | + ); |
|
254 | + } |
|
255 | + return $controller->sendResponse( |
|
256 | + $controller->getEntityFromModel( |
|
257 | + $controller->getModelVersionInfo()->loadModel($model_name), |
|
258 | + $request |
|
259 | + ) |
|
260 | + ); |
|
261 | + } catch (Exception $e) { |
|
262 | + return $controller->sendResponse($e); |
|
263 | + } |
|
264 | + } |
|
265 | + |
|
266 | + |
|
267 | + /** |
|
268 | + * Gets all the related entities (or if its a belongs-to relation just the one) |
|
269 | + * to the item with the given id |
|
270 | + * |
|
271 | + * @param WP_REST_Request $request |
|
272 | + * @param string $version |
|
273 | + * @param string $model_name |
|
274 | + * @param string $related_model_name |
|
275 | + * @return \WP_REST_Response|WP_Error |
|
276 | + */ |
|
277 | + public static function handleRequestGetRelated( |
|
278 | + WP_REST_Request $request, |
|
279 | + $version, |
|
280 | + $model_name, |
|
281 | + $related_model_name |
|
282 | + ) { |
|
283 | + $controller = new Read(); |
|
284 | + try { |
|
285 | + $controller->setRequestedVersion($version); |
|
286 | + if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
287 | + return $controller->sendResponse( |
|
288 | + new WP_Error( |
|
289 | + 'endpoint_parsing_error', |
|
290 | + sprintf( |
|
291 | + __( |
|
292 | + 'There is no model for endpoint %s. Please contact event espresso support', |
|
293 | + 'event_espresso' |
|
294 | + ), |
|
295 | + $model_name |
|
296 | + ) |
|
297 | + ) |
|
298 | + ); |
|
299 | + } |
|
300 | + $main_model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
301 | + if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($related_model_name)) { |
|
302 | + return $controller->sendResponse( |
|
303 | + new WP_Error( |
|
304 | + 'endpoint_parsing_error', |
|
305 | + sprintf( |
|
306 | + __( |
|
307 | + 'There is no model for endpoint %s. Please contact event espresso support', |
|
308 | + 'event_espresso' |
|
309 | + ), |
|
310 | + $related_model_name |
|
311 | + ) |
|
312 | + ) |
|
313 | + ); |
|
314 | + } |
|
315 | + return $controller->sendResponse( |
|
316 | + $controller->getEntitiesFromRelation( |
|
317 | + $request->get_param('id'), |
|
318 | + $main_model->related_settings_for($related_model_name), |
|
319 | + $request |
|
320 | + ) |
|
321 | + ); |
|
322 | + } catch (Exception $e) { |
|
323 | + return $controller->sendResponse($e); |
|
324 | + } |
|
325 | + } |
|
326 | + |
|
327 | + |
|
328 | + /** |
|
329 | + * Gets a collection for the given model and filters |
|
330 | + * |
|
331 | + * @param EEM_Base $model |
|
332 | + * @param WP_REST_Request $request |
|
333 | + * @return array|WP_Error |
|
334 | + */ |
|
335 | + public function getEntitiesFromModel($model, $request) |
|
336 | + { |
|
337 | + $query_params = $this->createModelQueryParams($model, $request->get_params()); |
|
338 | + if (! Capabilities::currentUserHasPartialAccessTo($model, $query_params['caps'])) { |
|
339 | + $model_name_plural = EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
340 | + return new WP_Error( |
|
341 | + sprintf('rest_%s_cannot_list', $model_name_plural), |
|
342 | + sprintf( |
|
343 | + __('Sorry, you are not allowed to list %1$s. Missing permissions: %2$s', 'event_espresso'), |
|
344 | + $model_name_plural, |
|
345 | + Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
|
346 | + ), |
|
347 | + array('status' => 403) |
|
348 | + ); |
|
349 | + } |
|
350 | + if (! $request->get_header('no_rest_headers')) { |
|
351 | + $this->setHeadersFromQueryParams($model, $query_params); |
|
352 | + } |
|
353 | + /** @type array $results */ |
|
354 | + $results = $model->get_all_wpdb_results($query_params); |
|
355 | + $nice_results = array(); |
|
356 | + foreach ($results as $result) { |
|
357 | + $nice_results[] = $this->createEntityFromWpdbResult( |
|
358 | + $model, |
|
359 | + $result, |
|
360 | + $request |
|
361 | + ); |
|
362 | + } |
|
363 | + return $nice_results; |
|
364 | + } |
|
365 | + |
|
366 | + |
|
367 | + /** |
|
368 | + * Gets the collection for given relation object |
|
369 | + * The same as Read::get_entities_from_model(), except if the relation |
|
370 | + * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
371 | + * the join-model-object into the results |
|
372 | + * |
|
373 | + * @param array $primary_model_query_params query params for finding the item from which |
|
374 | + * relations will be based |
|
375 | + * @param \EE_Model_Relation_Base $relation |
|
376 | + * @param WP_REST_Request $request |
|
377 | + * @return WP_Error|array |
|
378 | + * @throws RestException |
|
379 | + */ |
|
380 | + protected function getEntitiesFromRelationUsingModelQueryParams($primary_model_query_params, $relation, $request) |
|
381 | + { |
|
382 | + $context = $this->validateContext($request->get_param('caps')); |
|
383 | + $model = $relation->get_this_model(); |
|
384 | + $related_model = $relation->get_other_model(); |
|
385 | + if (! isset($primary_model_query_params[0])) { |
|
386 | + $primary_model_query_params[0] = array(); |
|
387 | + } |
|
388 | + // check if they can access the 1st model object |
|
389 | + $primary_model_query_params = array( |
|
390 | + 0 => $primary_model_query_params[0], |
|
391 | + 'limit' => 1, |
|
392 | + ); |
|
393 | + if ($model instanceof \EEM_Soft_Delete_Base) { |
|
394 | + $primary_model_query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included( |
|
395 | + $primary_model_query_params |
|
396 | + ); |
|
397 | + } |
|
398 | + $restricted_query_params = $primary_model_query_params; |
|
399 | + $restricted_query_params['caps'] = $context; |
|
400 | + $this->setDebugInfo('main model query params', $restricted_query_params); |
|
401 | + $this->setDebugInfo('missing caps', Capabilities::getMissingPermissionsString($related_model, $context)); |
|
402 | + if (! ( |
|
403 | + Capabilities::currentUserHasPartialAccessTo($related_model, $context) |
|
404 | + && $model->exists($restricted_query_params) |
|
405 | + ) |
|
406 | + ) { |
|
407 | + if ($relation instanceof EE_Belongs_To_Relation) { |
|
408 | + $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name()); |
|
409 | + } else { |
|
410 | + $related_model_name_maybe_plural = EEH_Inflector::pluralize_and_lower( |
|
411 | + $related_model->get_this_model_name() |
|
412 | + ); |
|
413 | + } |
|
414 | + return new WP_Error( |
|
415 | + sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), |
|
416 | + sprintf( |
|
417 | + __( |
|
418 | + 'Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', |
|
419 | + 'event_espresso' |
|
420 | + ), |
|
421 | + $related_model_name_maybe_plural, |
|
422 | + $relation->get_this_model()->get_this_model_name(), |
|
423 | + implode( |
|
424 | + ',', |
|
425 | + array_keys( |
|
426 | + Capabilities::getMissingPermissions($related_model, $context) |
|
427 | + ) |
|
428 | + ) |
|
429 | + ), |
|
430 | + array('status' => 403) |
|
431 | + ); |
|
432 | + } |
|
433 | + $query_params = $this->createModelQueryParams($relation->get_other_model(), $request->get_params()); |
|
434 | + foreach ($primary_model_query_params[0] as $where_condition_key => $where_condition_value) { |
|
435 | + $query_params[0][ $relation->get_this_model()->get_this_model_name() |
|
436 | + . '.' |
|
437 | + . $where_condition_key ] = $where_condition_value; |
|
438 | + } |
|
439 | + $query_params['default_where_conditions'] = 'none'; |
|
440 | + $query_params['caps'] = $context; |
|
441 | + if (! $request->get_header('no_rest_headers')) { |
|
442 | + $this->setHeadersFromQueryParams($relation->get_other_model(), $query_params); |
|
443 | + } |
|
444 | + /** @type array $results */ |
|
445 | + $results = $relation->get_other_model()->get_all_wpdb_results($query_params); |
|
446 | + $nice_results = array(); |
|
447 | + foreach ($results as $result) { |
|
448 | + $nice_result = $this->createEntityFromWpdbResult( |
|
449 | + $relation->get_other_model(), |
|
450 | + $result, |
|
451 | + $request |
|
452 | + ); |
|
453 | + if ($relation instanceof \EE_HABTM_Relation) { |
|
454 | + // put the unusual stuff (properties from the HABTM relation) first, and make sure |
|
455 | + // if there are conflicts we prefer the properties from the main model |
|
456 | + $join_model_result = $this->createEntityFromWpdbResult( |
|
457 | + $relation->get_join_model(), |
|
458 | + $result, |
|
459 | + $request |
|
460 | + ); |
|
461 | + $joined_result = array_merge($nice_result, $join_model_result); |
|
462 | + // but keep the meta stuff from the main model |
|
463 | + if (isset($nice_result['meta'])) { |
|
464 | + $joined_result['meta'] = $nice_result['meta']; |
|
465 | + } |
|
466 | + $nice_result = $joined_result; |
|
467 | + } |
|
468 | + $nice_results[] = $nice_result; |
|
469 | + } |
|
470 | + if ($relation instanceof EE_Belongs_To_Relation) { |
|
471 | + return array_shift($nice_results); |
|
472 | + } else { |
|
473 | + return $nice_results; |
|
474 | + } |
|
475 | + } |
|
476 | + |
|
477 | + |
|
478 | + /** |
|
479 | + * Gets the collection for given relation object |
|
480 | + * The same as Read::get_entities_from_model(), except if the relation |
|
481 | + * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
482 | + * the join-model-object into the results |
|
483 | + * |
|
484 | + * @param string $id the ID of the thing we are fetching related stuff from |
|
485 | + * @param \EE_Model_Relation_Base $relation |
|
486 | + * @param WP_REST_Request $request |
|
487 | + * @return array|WP_Error |
|
488 | + * @throws EE_Error |
|
489 | + */ |
|
490 | + public function getEntitiesFromRelation($id, $relation, $request) |
|
491 | + { |
|
492 | + if (! $relation->get_this_model()->has_primary_key_field()) { |
|
493 | + throw new EE_Error( |
|
494 | + sprintf( |
|
495 | + __( |
|
496 | + // @codingStandardsIgnoreStart |
|
497 | + 'Read::get_entities_from_relation should only be called from a model with a primary key, it was called from %1$s', |
|
498 | + // @codingStandardsIgnoreEnd |
|
499 | + 'event_espresso' |
|
500 | + ), |
|
501 | + $relation->get_this_model()->get_this_model_name() |
|
502 | + ) |
|
503 | + ); |
|
504 | + } |
|
505 | + return $this->getEntitiesFromRelationUsingModelQueryParams( |
|
506 | + array( |
|
507 | + array( |
|
508 | + $relation->get_this_model()->primary_key_name() => $id, |
|
509 | + ), |
|
510 | + ), |
|
511 | + $relation, |
|
512 | + $request |
|
513 | + ); |
|
514 | + } |
|
515 | + |
|
516 | + |
|
517 | + /** |
|
518 | + * Sets the headers that are based on the model and query params, |
|
519 | + * like the total records. This should only be called on the original request |
|
520 | + * from the client, not on subsequent internal |
|
521 | + * |
|
522 | + * @param EEM_Base $model |
|
523 | + * @param array $query_params |
|
524 | + * @return void |
|
525 | + */ |
|
526 | + protected function setHeadersFromQueryParams($model, $query_params) |
|
527 | + { |
|
528 | + $this->setDebugInfo('model query params', $query_params); |
|
529 | + $this->setDebugInfo( |
|
530 | + 'missing caps', |
|
531 | + Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
|
532 | + ); |
|
533 | + // normally the limit to a 2-part array, where the 2nd item is the limit |
|
534 | + if (! isset($query_params['limit'])) { |
|
535 | + $query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
|
536 | + } |
|
537 | + if (is_array($query_params['limit'])) { |
|
538 | + $limit_parts = $query_params['limit']; |
|
539 | + } else { |
|
540 | + $limit_parts = explode(',', $query_params['limit']); |
|
541 | + if (count($limit_parts) == 1) { |
|
542 | + $limit_parts = array(0, $limit_parts[0]); |
|
543 | + } |
|
544 | + } |
|
545 | + // remove the group by and having parts of the query, as those will |
|
546 | + // make the sql query return an array of values, instead of just a single value |
|
547 | + unset($query_params['group_by'], $query_params['having'], $query_params['limit']); |
|
548 | + $count = $model->count($query_params, null, true); |
|
549 | + $pages = $count / $limit_parts[1]; |
|
550 | + $this->setResponseHeader('Total', $count, false); |
|
551 | + $this->setResponseHeader('PageSize', $limit_parts[1], false); |
|
552 | + $this->setResponseHeader('TotalPages', ceil($pages), false); |
|
553 | + } |
|
554 | + |
|
555 | + |
|
556 | + /** |
|
557 | + * Changes database results into REST API entities |
|
558 | + * |
|
559 | + * @param EEM_Base $model |
|
560 | + * @param array $db_row like results from $wpdb->get_results() |
|
561 | + * @param WP_REST_Request $rest_request |
|
562 | + * @param string $deprecated no longer used |
|
563 | + * @return array ready for being converted into json for sending to client |
|
564 | + */ |
|
565 | + public function createEntityFromWpdbResult($model, $db_row, $rest_request, $deprecated = null) |
|
566 | + { |
|
567 | + if (! $rest_request instanceof WP_REST_Request) { |
|
568 | + // ok so this was called in the old style, where the 3rd arg was |
|
569 | + // $include, and the 4th arg was $context |
|
570 | + // now setup the request just to avoid fatal errors, although we won't be able |
|
571 | + // to truly make use of it because it's kinda devoid of info |
|
572 | + $rest_request = new WP_REST_Request(); |
|
573 | + $rest_request->set_param('include', $rest_request); |
|
574 | + $rest_request->set_param('caps', $deprecated); |
|
575 | + } |
|
576 | + if ($rest_request->get_param('caps') == null) { |
|
577 | + $rest_request->set_param('caps', EEM_Base::caps_read); |
|
578 | + } |
|
579 | + $entity_array = $this->createBareEntityFromWpdbResults($model, $db_row); |
|
580 | + $entity_array = $this->addExtraFields($model, $db_row, $entity_array); |
|
581 | + $entity_array['_links'] = $this->getEntityLinks($model, $db_row, $entity_array); |
|
582 | + $entity_array['_calculated_fields'] = $this->getEntityCalculations($model, $db_row, $rest_request); |
|
583 | + $entity_array = apply_filters( |
|
584 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_before_including_requested_models', |
|
585 | + $entity_array, |
|
586 | + $model, |
|
587 | + $rest_request->get_param('caps'), |
|
588 | + $rest_request, |
|
589 | + $this |
|
590 | + ); |
|
591 | + $entity_array = $this->includeRequestedModels($model, $rest_request, $entity_array, $db_row); |
|
592 | + $entity_array = apply_filters( |
|
593 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_before_inaccessible_field_removal', |
|
594 | + $entity_array, |
|
595 | + $model, |
|
596 | + $rest_request->get_param('caps'), |
|
597 | + $rest_request, |
|
598 | + $this |
|
599 | + ); |
|
600 | + $result_without_inaccessible_fields = Capabilities::filterOutInaccessibleEntityFields( |
|
601 | + $entity_array, |
|
602 | + $model, |
|
603 | + $rest_request->get_param('caps'), |
|
604 | + $this->getModelVersionInfo(), |
|
605 | + $model->get_index_primary_key_string( |
|
606 | + $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
607 | + ) |
|
608 | + ); |
|
609 | + $this->setDebugInfo( |
|
610 | + 'inaccessible fields', |
|
611 | + array_keys(array_diff_key($entity_array, $result_without_inaccessible_fields)) |
|
612 | + ); |
|
613 | + return apply_filters( |
|
614 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_return', |
|
615 | + $result_without_inaccessible_fields, |
|
616 | + $model, |
|
617 | + $rest_request->get_param('caps') |
|
618 | + ); |
|
619 | + } |
|
620 | + |
|
621 | + |
|
622 | + /** |
|
623 | + * Creates a REST entity array (JSON object we're going to return in the response, but |
|
624 | + * for now still a PHP array, but soon enough we'll call json_encode on it, don't worry), |
|
625 | + * from $wpdb->get_row( $sql, ARRAY_A) |
|
626 | + * |
|
627 | + * @param EEM_Base $model |
|
628 | + * @param array $db_row |
|
629 | + * @return array entity mostly ready for converting to JSON and sending in the response |
|
630 | + */ |
|
631 | + protected function createBareEntityFromWpdbResults(EEM_Base $model, $db_row) |
|
632 | + { |
|
633 | + $result = $model->deduce_fields_n_values_from_cols_n_values($db_row); |
|
634 | + $result = array_intersect_key( |
|
635 | + $result, |
|
636 | + $this->getModelVersionInfo()->fieldsOnModelInThisVersion($model) |
|
637 | + ); |
|
638 | + // if this is a CPT, we need to set the global $post to it, |
|
639 | + // otherwise shortcodes etc won't work properly while rendering it |
|
640 | + if ($model instanceof \EEM_CPT_Base) { |
|
641 | + $do_chevy_shuffle = true; |
|
642 | + } else { |
|
643 | + $do_chevy_shuffle = false; |
|
644 | + } |
|
645 | + if ($do_chevy_shuffle) { |
|
646 | + global $post; |
|
647 | + $old_post = $post; |
|
648 | + $post = get_post($result[ $model->primary_key_name() ]); |
|
649 | + if (! $post instanceof \WP_Post) { |
|
650 | + // well that's weird, because $result is what we JUST fetched from the database |
|
651 | + throw new RestException( |
|
652 | + 'error_fetching_post_from_database_results', |
|
653 | + esc_html__( |
|
654 | + 'An item was retrieved from the database but it\'s not a WP_Post like it should be.', |
|
655 | + 'event_espresso' |
|
656 | + ) |
|
657 | + ); |
|
658 | + } |
|
659 | + $model_object_classname = 'EE_' . $model->get_this_model_name(); |
|
660 | + $post->{$model_object_classname} = \EE_Registry::instance()->load_class( |
|
661 | + $model_object_classname, |
|
662 | + $result, |
|
663 | + false, |
|
664 | + false |
|
665 | + ); |
|
666 | + } |
|
667 | + foreach ($result as $field_name => $field_value) { |
|
668 | + $field_obj = $model->field_settings_for($field_name); |
|
669 | + if ($this->isSubclassOfOne($field_obj, $this->getModelVersionInfo()->fieldsIgnored())) { |
|
670 | + unset($result[ $field_name ]); |
|
671 | + } elseif ($this->isSubclassOfOne( |
|
672 | + $field_obj, |
|
673 | + $this->getModelVersionInfo()->fieldsThatHaveRenderedFormat() |
|
674 | + ) |
|
675 | + ) { |
|
676 | + $result[ $field_name ] = array( |
|
677 | + 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
|
678 | + 'rendered' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
|
679 | + ); |
|
680 | + } elseif ($this->isSubclassOfOne( |
|
681 | + $field_obj, |
|
682 | + $this->getModelVersionInfo()->fieldsThatHavePrettyFormat() |
|
683 | + ) |
|
684 | + ) { |
|
685 | + $result[ $field_name ] = array( |
|
686 | + 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
|
687 | + 'pretty' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
|
688 | + ); |
|
689 | + } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
690 | + $field_value = $field_obj->prepare_for_set_from_db($field_value); |
|
691 | + // if the value is null, but we're not supposed to permit null, then set to the field's default |
|
692 | + if (is_null($field_value)) { |
|
693 | + $field_value = $field_obj->getDefaultDateTimeObj(); |
|
694 | + } |
|
695 | + if (is_null($field_value)) { |
|
696 | + $gmt_date = $local_date = ModelDataTranslator::prepareFieldValuesForJson( |
|
697 | + $field_obj, |
|
698 | + $field_value, |
|
699 | + $this->getModelVersionInfo()->requestedVersion() |
|
700 | + ); |
|
701 | + } else { |
|
702 | + $timezone = $field_value->getTimezone(); |
|
703 | + EEH_DTT_Helper::setTimezone($field_value, new DateTimeZone('UTC')); |
|
704 | + $gmt_date = ModelDataTranslator::prepareFieldValuesForJson( |
|
705 | + $field_obj, |
|
706 | + $field_value, |
|
707 | + $this->getModelVersionInfo()->requestedVersion() |
|
708 | + ); |
|
709 | + EEH_DTT_Helper::setTimezone($field_value, $timezone); |
|
710 | + $local_date = ModelDataTranslator::prepareFieldValuesForJson( |
|
711 | + $field_obj, |
|
712 | + $field_value, |
|
713 | + $this->getModelVersionInfo()->requestedVersion() |
|
714 | + ); |
|
715 | + } |
|
716 | + $result[ $field_name . '_gmt' ] = $gmt_date; |
|
717 | + $result[ $field_name ] = $local_date; |
|
718 | + } else { |
|
719 | + $result[ $field_name ] = $this->prepareFieldObjValueForJson($field_obj, $field_value); |
|
720 | + } |
|
721 | + } |
|
722 | + if ($do_chevy_shuffle) { |
|
723 | + $post = $old_post; |
|
724 | + } |
|
725 | + return $result; |
|
726 | + } |
|
727 | + |
|
728 | + |
|
729 | + /** |
|
730 | + * Takes a value all the way from the DB representation, to the model object's representation, to the |
|
731 | + * user-facing PHP representation, to the REST API representation. (Assumes you've already taken from the DB |
|
732 | + * representation using $field_obj->prepare_for_set_from_db()) |
|
733 | + * |
|
734 | + * @param EE_Model_Field_Base $field_obj |
|
735 | + * @param mixed $value as it's stored on a model object |
|
736 | + * @param string $format valid values are 'normal' (default), 'pretty', 'datetime_obj' |
|
737 | + * @return mixed |
|
738 | + * @throws ObjectDetectedException if $value contains a PHP object |
|
739 | + */ |
|
740 | + protected function prepareFieldObjValueForJson(EE_Model_Field_Base $field_obj, $value, $format = 'normal') |
|
741 | + { |
|
742 | + $value = $field_obj->prepare_for_set_from_db($value); |
|
743 | + switch ($format) { |
|
744 | + case 'pretty': |
|
745 | + $value = $field_obj->prepare_for_pretty_echoing($value); |
|
746 | + break; |
|
747 | + case 'normal': |
|
748 | + default: |
|
749 | + $value = $field_obj->prepare_for_get($value); |
|
750 | + break; |
|
751 | + } |
|
752 | + return ModelDataTranslator::prepareFieldValuesForJson( |
|
753 | + $field_obj, |
|
754 | + $value, |
|
755 | + $this->getModelVersionInfo()->requestedVersion() |
|
756 | + ); |
|
757 | + } |
|
758 | + |
|
759 | + |
|
760 | + /** |
|
761 | + * Adds a few extra fields to the entity response |
|
762 | + * |
|
763 | + * @param EEM_Base $model |
|
764 | + * @param array $db_row |
|
765 | + * @param array $entity_array |
|
766 | + * @return array modified entity |
|
767 | + */ |
|
768 | + protected function addExtraFields(EEM_Base $model, $db_row, $entity_array) |
|
769 | + { |
|
770 | + if ($model instanceof EEM_CPT_Base) { |
|
771 | + $entity_array['link'] = get_permalink($db_row[ $model->get_primary_key_field()->get_qualified_column() ]); |
|
772 | + } |
|
773 | + return $entity_array; |
|
774 | + } |
|
775 | + |
|
776 | + |
|
777 | + /** |
|
778 | + * Gets links we want to add to the response |
|
779 | + * |
|
780 | + * @global \WP_REST_Server $wp_rest_server |
|
781 | + * @param EEM_Base $model |
|
782 | + * @param array $db_row |
|
783 | + * @param array $entity_array |
|
784 | + * @return array the _links item in the entity |
|
785 | + */ |
|
786 | + protected function getEntityLinks($model, $db_row, $entity_array) |
|
787 | + { |
|
788 | + // add basic links |
|
789 | + $links = array(); |
|
790 | + if ($model->has_primary_key_field()) { |
|
791 | + $links['self'] = array( |
|
792 | + array( |
|
793 | + 'href' => $this->getVersionedLinkTo( |
|
794 | + EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
795 | + . '/' |
|
796 | + . $entity_array[ $model->primary_key_name() ] |
|
797 | + ), |
|
798 | + ), |
|
799 | + ); |
|
800 | + } |
|
801 | + $links['collection'] = array( |
|
802 | + array( |
|
803 | + 'href' => $this->getVersionedLinkTo( |
|
804 | + EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
805 | + ), |
|
806 | + ), |
|
807 | + ); |
|
808 | + // add links to related models |
|
809 | + if ($model->has_primary_key_field()) { |
|
810 | + foreach ($this->getModelVersionInfo()->relationSettings($model) as $relation_name => $relation_obj) { |
|
811 | + $related_model_part = Read::getRelatedEntityName($relation_name, $relation_obj); |
|
812 | + $links[ EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part ] = array( |
|
813 | + array( |
|
814 | + 'href' => $this->getVersionedLinkTo( |
|
815 | + EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
816 | + . '/' |
|
817 | + . $entity_array[ $model->primary_key_name() ] |
|
818 | + . '/' |
|
819 | + . $related_model_part |
|
820 | + ), |
|
821 | + 'single' => $relation_obj instanceof EE_Belongs_To_Relation ? true : false, |
|
822 | + ), |
|
823 | + ); |
|
824 | + } |
|
825 | + } |
|
826 | + return $links; |
|
827 | + } |
|
828 | + |
|
829 | + |
|
830 | + /** |
|
831 | + * Adds the included models indicated in the request to the entity provided |
|
832 | + * |
|
833 | + * @param EEM_Base $model |
|
834 | + * @param WP_REST_Request $rest_request |
|
835 | + * @param array $entity_array |
|
836 | + * @param array $db_row |
|
837 | + * @return array the modified entity |
|
838 | + */ |
|
839 | + protected function includeRequestedModels( |
|
840 | + EEM_Base $model, |
|
841 | + WP_REST_Request $rest_request, |
|
842 | + $entity_array, |
|
843 | + $db_row = array() |
|
844 | + ) { |
|
845 | + // if $db_row not included, hope the entity array has what we need |
|
846 | + if (! $db_row) { |
|
847 | + $db_row = $entity_array; |
|
848 | + } |
|
849 | + $includes_for_this_model = $this->explodeAndGetItemsPrefixedWith($rest_request->get_param('include'), ''); |
|
850 | + $includes_for_this_model = $this->removeModelNamesFromArray($includes_for_this_model); |
|
851 | + // if they passed in * or didn't specify any includes, return everything |
|
852 | + if (! in_array('*', $includes_for_this_model) |
|
853 | + && ! empty($includes_for_this_model) |
|
854 | + ) { |
|
855 | + if ($model->has_primary_key_field()) { |
|
856 | + // always include the primary key. ya just gotta know that at least |
|
857 | + $includes_for_this_model[] = $model->primary_key_name(); |
|
858 | + } |
|
859 | + if ($this->explodeAndGetItemsPrefixedWith($rest_request->get_param('calculate'), '')) { |
|
860 | + $includes_for_this_model[] = '_calculated_fields'; |
|
861 | + } |
|
862 | + $entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model)); |
|
863 | + } |
|
864 | + $relation_settings = $this->getModelVersionInfo()->relationSettings($model); |
|
865 | + foreach ($relation_settings as $relation_name => $relation_obj) { |
|
866 | + $related_fields_to_include = $this->explodeAndGetItemsPrefixedWith( |
|
867 | + $rest_request->get_param('include'), |
|
868 | + $relation_name |
|
869 | + ); |
|
870 | + $related_fields_to_calculate = $this->explodeAndGetItemsPrefixedWith( |
|
871 | + $rest_request->get_param('calculate'), |
|
872 | + $relation_name |
|
873 | + ); |
|
874 | + // did they specify they wanted to include a related model, or |
|
875 | + // specific fields from a related model? |
|
876 | + // or did they specify to calculate a field from a related model? |
|
877 | + if ($related_fields_to_include || $related_fields_to_calculate) { |
|
878 | + // if so, we should include at least some part of the related model |
|
879 | + $pretend_related_request = new WP_REST_Request(); |
|
880 | + $pretend_related_request->set_query_params( |
|
881 | + array( |
|
882 | + 'caps' => $rest_request->get_param('caps'), |
|
883 | + 'include' => $related_fields_to_include, |
|
884 | + 'calculate' => $related_fields_to_calculate, |
|
885 | + ) |
|
886 | + ); |
|
887 | + $pretend_related_request->add_header('no_rest_headers', true); |
|
888 | + $primary_model_query_params = $model->alter_query_params_to_restrict_by_ID( |
|
889 | + $model->get_index_primary_key_string( |
|
890 | + $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
891 | + ) |
|
892 | + ); |
|
893 | + $related_results = $this->getEntitiesFromRelationUsingModelQueryParams( |
|
894 | + $primary_model_query_params, |
|
895 | + $relation_obj, |
|
896 | + $pretend_related_request |
|
897 | + ); |
|
898 | + $entity_array[ Read::getRelatedEntityName($relation_name, $relation_obj) ] = $related_results |
|
899 | + instanceof |
|
900 | + WP_Error |
|
901 | + ? null |
|
902 | + : $related_results; |
|
903 | + } |
|
904 | + } |
|
905 | + return $entity_array; |
|
906 | + } |
|
907 | + |
|
908 | + |
|
909 | + /** |
|
910 | + * Returns a new array with all the names of models removed. Eg |
|
911 | + * array( 'Event', 'Datetime.*', 'foobar' ) would become array( 'Datetime.*', 'foobar' ) |
|
912 | + * |
|
913 | + * @param array $arr |
|
914 | + * @return array |
|
915 | + */ |
|
916 | + private function removeModelNamesFromArray($arr) |
|
917 | + { |
|
918 | + return array_diff($arr, array_keys(EE_Registry::instance()->non_abstract_db_models)); |
|
919 | + } |
|
920 | + |
|
921 | + |
|
922 | + /** |
|
923 | + * Gets the calculated fields for the response |
|
924 | + * |
|
925 | + * @param EEM_Base $model |
|
926 | + * @param array $wpdb_row |
|
927 | + * @param WP_REST_Request $rest_request |
|
928 | + * @return \stdClass the _calculations item in the entity |
|
929 | + * @throws ObjectDetectedException if a default value has a PHP object, which should never do (and if we |
|
930 | + * did, let's know about it ASAP, so let the exception bubble up) |
|
931 | + */ |
|
932 | + protected function getEntityCalculations($model, $wpdb_row, $rest_request) |
|
933 | + { |
|
934 | + $calculated_fields = $this->explodeAndGetItemsPrefixedWith( |
|
935 | + $rest_request->get_param('calculate'), |
|
936 | + '' |
|
937 | + ); |
|
938 | + // note: setting calculate=* doesn't do anything |
|
939 | + $calculated_fields_to_return = new \stdClass(); |
|
940 | + foreach ($calculated_fields as $field_to_calculate) { |
|
941 | + try { |
|
942 | + $calculated_fields_to_return->$field_to_calculate = ModelDataTranslator::prepareFieldValueForJson( |
|
943 | + null, |
|
944 | + $this->fields_calculator->retrieveCalculatedFieldValue( |
|
945 | + $model, |
|
946 | + $field_to_calculate, |
|
947 | + $wpdb_row, |
|
948 | + $rest_request, |
|
949 | + $this |
|
950 | + ), |
|
951 | + $this->getModelVersionInfo()->requestedVersion() |
|
952 | + ); |
|
953 | + } catch (RestException $e) { |
|
954 | + // if we don't have permission to read it, just leave it out. but let devs know about the problem |
|
955 | + $this->setResponseHeader( |
|
956 | + 'Notices-Field-Calculation-Errors[' |
|
957 | + . $e->getStringCode() |
|
958 | + . '][' |
|
959 | + . $model->get_this_model_name() |
|
960 | + . '][' |
|
961 | + . $field_to_calculate |
|
962 | + . ']', |
|
963 | + $e->getMessage(), |
|
964 | + true |
|
965 | + ); |
|
966 | + } |
|
967 | + } |
|
968 | + return $calculated_fields_to_return; |
|
969 | + } |
|
970 | + |
|
971 | + |
|
972 | + /** |
|
973 | + * Gets the full URL to the resource, taking the requested version into account |
|
974 | + * |
|
975 | + * @param string $link_part_after_version_and_slash eg "events/10/datetimes" |
|
976 | + * @return string url eg "http://mysite.com/wp-json/ee/v4.6/events/10/datetimes" |
|
977 | + */ |
|
978 | + public function getVersionedLinkTo($link_part_after_version_and_slash) |
|
979 | + { |
|
980 | + return rest_url( |
|
981 | + EED_Core_Rest_Api::get_versioned_route_to( |
|
982 | + $link_part_after_version_and_slash, |
|
983 | + $this->getModelVersionInfo()->requestedVersion() |
|
984 | + ) |
|
985 | + ); |
|
986 | + } |
|
987 | + |
|
988 | + |
|
989 | + /** |
|
990 | + * Gets the correct lowercase name for the relation in the API according |
|
991 | + * to the relation's type |
|
992 | + * |
|
993 | + * @param string $relation_name |
|
994 | + * @param \EE_Model_Relation_Base $relation_obj |
|
995 | + * @return string |
|
996 | + */ |
|
997 | + public static function getRelatedEntityName($relation_name, $relation_obj) |
|
998 | + { |
|
999 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
1000 | + return strtolower($relation_name); |
|
1001 | + } else { |
|
1002 | + return EEH_Inflector::pluralize_and_lower($relation_name); |
|
1003 | + } |
|
1004 | + } |
|
1005 | + |
|
1006 | + |
|
1007 | + /** |
|
1008 | + * Gets the one model object with the specified id for the specified model |
|
1009 | + * |
|
1010 | + * @param EEM_Base $model |
|
1011 | + * @param WP_REST_Request $request |
|
1012 | + * @return array|WP_Error |
|
1013 | + */ |
|
1014 | + public function getEntityFromModel($model, $request) |
|
1015 | + { |
|
1016 | + $context = $this->validateContext($request->get_param('caps')); |
|
1017 | + return $this->getOneOrReportPermissionError($model, $request, $context); |
|
1018 | + } |
|
1019 | + |
|
1020 | + |
|
1021 | + /** |
|
1022 | + * If a context is provided which isn't valid, maybe it was added in a future |
|
1023 | + * version so just treat it as a default read |
|
1024 | + * |
|
1025 | + * @param string $context |
|
1026 | + * @return string array key of EEM_Base::cap_contexts_to_cap_action_map() |
|
1027 | + */ |
|
1028 | + public function validateContext($context) |
|
1029 | + { |
|
1030 | + if (! $context) { |
|
1031 | + $context = EEM_Base::caps_read; |
|
1032 | + } |
|
1033 | + $valid_contexts = EEM_Base::valid_cap_contexts(); |
|
1034 | + if (in_array($context, $valid_contexts)) { |
|
1035 | + return $context; |
|
1036 | + } else { |
|
1037 | + return EEM_Base::caps_read; |
|
1038 | + } |
|
1039 | + } |
|
1040 | + |
|
1041 | + |
|
1042 | + /** |
|
1043 | + * Verifies the passed in value is an allowable default where conditions value. |
|
1044 | + * |
|
1045 | + * @param $default_query_params |
|
1046 | + * @return string |
|
1047 | + */ |
|
1048 | + public function validateDefaultQueryParams($default_query_params) |
|
1049 | + { |
|
1050 | + $valid_default_where_conditions_for_api_calls = array( |
|
1051 | + EEM_Base::default_where_conditions_all, |
|
1052 | + EEM_Base::default_where_conditions_minimum_all, |
|
1053 | + EEM_Base::default_where_conditions_minimum_others, |
|
1054 | + ); |
|
1055 | + if (! $default_query_params) { |
|
1056 | + $default_query_params = EEM_Base::default_where_conditions_all; |
|
1057 | + } |
|
1058 | + if (in_array( |
|
1059 | + $default_query_params, |
|
1060 | + $valid_default_where_conditions_for_api_calls, |
|
1061 | + true |
|
1062 | + )) { |
|
1063 | + return $default_query_params; |
|
1064 | + } else { |
|
1065 | + return EEM_Base::default_where_conditions_all; |
|
1066 | + } |
|
1067 | + } |
|
1068 | + |
|
1069 | + |
|
1070 | + /** |
|
1071 | + * Translates API filter get parameter into $query_params array used by EEM_Base::get_all(). |
|
1072 | + * Note: right now the query parameter keys for fields (and related fields) |
|
1073 | + * can be left as-is, but it's quite possible this will change someday. |
|
1074 | + * Also, this method's contents might be candidate for moving to Model_Data_Translator |
|
1075 | + * |
|
1076 | + * @param EEM_Base $model |
|
1077 | + * @param array $query_parameters from $_GET parameter @see Read:handle_request_get_all |
|
1078 | + * @return array like what EEM_Base::get_all() expects or FALSE to indicate |
|
1079 | + * that absolutely no results should be returned |
|
1080 | + * @throws EE_Error |
|
1081 | + * @throws RestException |
|
1082 | + */ |
|
1083 | + public function createModelQueryParams($model, $query_parameters) |
|
1084 | + { |
|
1085 | + $model_query_params = array(); |
|
1086 | + if (isset($query_parameters['where'])) { |
|
1087 | + $model_query_params[0] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
1088 | + $query_parameters['where'], |
|
1089 | + $model, |
|
1090 | + $this->getModelVersionInfo()->requestedVersion() |
|
1091 | + ); |
|
1092 | + } |
|
1093 | + if (isset($query_parameters['order_by'])) { |
|
1094 | + $order_by = $query_parameters['order_by']; |
|
1095 | + } elseif (isset($query_parameters['orderby'])) { |
|
1096 | + $order_by = $query_parameters['orderby']; |
|
1097 | + } else { |
|
1098 | + $order_by = null; |
|
1099 | + } |
|
1100 | + if ($order_by !== null) { |
|
1101 | + if (is_array($order_by)) { |
|
1102 | + $order_by = ModelDataTranslator::prepareFieldNamesInArrayKeysFromJson($order_by); |
|
1103 | + } else { |
|
1104 | + // it's a single item |
|
1105 | + $order_by = ModelDataTranslator::prepareFieldNameFromJson($order_by); |
|
1106 | + } |
|
1107 | + $model_query_params['order_by'] = $order_by; |
|
1108 | + } |
|
1109 | + if (isset($query_parameters['group_by'])) { |
|
1110 | + $group_by = $query_parameters['group_by']; |
|
1111 | + } elseif (isset($query_parameters['groupby'])) { |
|
1112 | + $group_by = $query_parameters['groupby']; |
|
1113 | + } else { |
|
1114 | + $group_by = array_keys($model->get_combined_primary_key_fields()); |
|
1115 | + } |
|
1116 | + // make sure they're all real names |
|
1117 | + if (is_array($group_by)) { |
|
1118 | + $group_by = ModelDataTranslator::prepareFieldNamesFromJson($group_by); |
|
1119 | + } |
|
1120 | + if ($group_by !== null) { |
|
1121 | + $model_query_params['group_by'] = $group_by; |
|
1122 | + } |
|
1123 | + if (isset($query_parameters['having'])) { |
|
1124 | + $model_query_params['having'] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
1125 | + $query_parameters['having'], |
|
1126 | + $model, |
|
1127 | + $this->getModelVersionInfo()->requestedVersion() |
|
1128 | + ); |
|
1129 | + } |
|
1130 | + if (isset($query_parameters['order'])) { |
|
1131 | + $model_query_params['order'] = $query_parameters['order']; |
|
1132 | + } |
|
1133 | + if (isset($query_parameters['mine'])) { |
|
1134 | + $model_query_params = $model->alter_query_params_to_only_include_mine($model_query_params); |
|
1135 | + } |
|
1136 | + if (isset($query_parameters['limit'])) { |
|
1137 | + // limit should be either a string like '23' or '23,43', or an array with two items in it |
|
1138 | + if (! is_array($query_parameters['limit'])) { |
|
1139 | + $limit_array = explode(',', (string) $query_parameters['limit']); |
|
1140 | + } else { |
|
1141 | + $limit_array = $query_parameters['limit']; |
|
1142 | + } |
|
1143 | + $sanitized_limit = array(); |
|
1144 | + foreach ($limit_array as $key => $limit_part) { |
|
1145 | + if ($this->debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1146 | + throw new EE_Error( |
|
1147 | + sprintf( |
|
1148 | + __( |
|
1149 | + // @codingStandardsIgnoreStart |
|
1150 | + 'An invalid limit filter was provided. It was: %s. If the EE4 JSON REST API weren\'t in debug mode, this message would not appear.', |
|
1151 | + // @codingStandardsIgnoreEnd |
|
1152 | + 'event_espresso' |
|
1153 | + ), |
|
1154 | + wp_json_encode($query_parameters['limit']) |
|
1155 | + ) |
|
1156 | + ); |
|
1157 | + } |
|
1158 | + $sanitized_limit[] = (int) $limit_part; |
|
1159 | + } |
|
1160 | + $model_query_params['limit'] = implode(',', $sanitized_limit); |
|
1161 | + } else { |
|
1162 | + $model_query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
|
1163 | + } |
|
1164 | + if (isset($query_parameters['caps'])) { |
|
1165 | + $model_query_params['caps'] = $this->validateContext($query_parameters['caps']); |
|
1166 | + } else { |
|
1167 | + $model_query_params['caps'] = EEM_Base::caps_read; |
|
1168 | + } |
|
1169 | + if (isset($query_parameters['default_where_conditions'])) { |
|
1170 | + $model_query_params['default_where_conditions'] = $this->validateDefaultQueryParams( |
|
1171 | + $query_parameters['default_where_conditions'] |
|
1172 | + ); |
|
1173 | + } |
|
1174 | + return apply_filters('FHEE__Read__create_model_query_params', $model_query_params, $query_parameters, $model); |
|
1175 | + } |
|
1176 | + |
|
1177 | + |
|
1178 | + /** |
|
1179 | + * Changes the REST-style query params for use in the models |
|
1180 | + * |
|
1181 | + * @deprecated |
|
1182 | + * @param EEM_Base $model |
|
1183 | + * @param array $query_params sub-array from @see EEM_Base::get_all() |
|
1184 | + * @return array |
|
1185 | + */ |
|
1186 | + public function prepareRestQueryParamsKeyForModels($model, $query_params) |
|
1187 | + { |
|
1188 | + $model_ready_query_params = array(); |
|
1189 | + foreach ($query_params as $key => $value) { |
|
1190 | + if (is_array($value)) { |
|
1191 | + $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsKeyForModels($model, $value); |
|
1192 | + } else { |
|
1193 | + $model_ready_query_params[ $key ] = $value; |
|
1194 | + } |
|
1195 | + } |
|
1196 | + return $model_ready_query_params; |
|
1197 | + } |
|
1198 | + |
|
1199 | + |
|
1200 | + /** |
|
1201 | + * @deprecated instead use ModelDataTranslator::prepareFieldValuesFromJson() |
|
1202 | + * @param $model |
|
1203 | + * @param $query_params |
|
1204 | + * @return array |
|
1205 | + */ |
|
1206 | + public function prepareRestQueryParamsValuesForModels($model, $query_params) |
|
1207 | + { |
|
1208 | + $model_ready_query_params = array(); |
|
1209 | + foreach ($query_params as $key => $value) { |
|
1210 | + if (is_array($value)) { |
|
1211 | + $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsValuesForModels($model, $value); |
|
1212 | + } else { |
|
1213 | + $model_ready_query_params[ $key ] = $value; |
|
1214 | + } |
|
1215 | + } |
|
1216 | + return $model_ready_query_params; |
|
1217 | + } |
|
1218 | + |
|
1219 | + |
|
1220 | + /** |
|
1221 | + * Explodes the string on commas, and only returns items with $prefix followed by a period. |
|
1222 | + * If no prefix is specified, returns items with no period. |
|
1223 | + * |
|
1224 | + * @param string|array $string_to_explode eg "jibba,jabba, blah, blah, blah" or array('jibba', 'jabba' ) |
|
1225 | + * @param string $prefix "Event" or "foobar" |
|
1226 | + * @return array $string_to_exploded exploded on COMMAS, and if a prefix was specified |
|
1227 | + * we only return strings starting with that and a period; if no prefix was |
|
1228 | + * specified we return all items containing NO periods |
|
1229 | + */ |
|
1230 | + public function explodeAndGetItemsPrefixedWith($string_to_explode, $prefix) |
|
1231 | + { |
|
1232 | + if (is_string($string_to_explode)) { |
|
1233 | + $exploded_contents = explode(',', $string_to_explode); |
|
1234 | + } elseif (is_array($string_to_explode)) { |
|
1235 | + $exploded_contents = $string_to_explode; |
|
1236 | + } else { |
|
1237 | + $exploded_contents = array(); |
|
1238 | + } |
|
1239 | + // if the string was empty, we want an empty array |
|
1240 | + $exploded_contents = array_filter($exploded_contents); |
|
1241 | + $contents_with_prefix = array(); |
|
1242 | + foreach ($exploded_contents as $item) { |
|
1243 | + $item = trim($item); |
|
1244 | + // if no prefix was provided, so we look for items with no "." in them |
|
1245 | + if (! $prefix) { |
|
1246 | + // does this item have a period? |
|
1247 | + if (strpos($item, '.') === false) { |
|
1248 | + // if not, then its what we're looking for |
|
1249 | + $contents_with_prefix[] = $item; |
|
1250 | + } |
|
1251 | + } elseif (strpos($item, $prefix . '.') === 0) { |
|
1252 | + // this item has the prefix and a period, grab it |
|
1253 | + $contents_with_prefix[] = substr( |
|
1254 | + $item, |
|
1255 | + strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1256 | + ); |
|
1257 | + } elseif ($item === $prefix) { |
|
1258 | + // this item is JUST the prefix |
|
1259 | + // so let's grab everything after, which is a blank string |
|
1260 | + $contents_with_prefix[] = ''; |
|
1261 | + } |
|
1262 | + } |
|
1263 | + return $contents_with_prefix; |
|
1264 | + } |
|
1265 | + |
|
1266 | + |
|
1267 | + /** |
|
1268 | + * @deprecated since 4.8.36.rc.001 You should instead use Read::explode_and_get_items_prefixed_with. |
|
1269 | + * Deprecated because its return values were really quite confusing- sometimes it returned |
|
1270 | + * an empty array (when the include string was blank or '*') or sometimes it returned |
|
1271 | + * array('*') (when you provided a model and a model of that kind was found). |
|
1272 | + * Parses the $include_string so we fetch all the field names relating to THIS model |
|
1273 | + * (ie have NO period in them), or for the provided model (ie start with the model |
|
1274 | + * name and then a period). |
|
1275 | + * @param string $include_string @see Read:handle_request_get_all |
|
1276 | + * @param string $model_name |
|
1277 | + * @return array of fields for this model. If $model_name is provided, then |
|
1278 | + * the fields for that model, with the model's name removed from each. |
|
1279 | + * If $include_string was blank or '*' returns an empty array |
|
1280 | + */ |
|
1281 | + public function extractIncludesForThisModel($include_string, $model_name = null) |
|
1282 | + { |
|
1283 | + if (is_array($include_string)) { |
|
1284 | + $include_string = implode(',', $include_string); |
|
1285 | + } |
|
1286 | + if ($include_string === '*' || $include_string === '') { |
|
1287 | + return array(); |
|
1288 | + } |
|
1289 | + $includes = explode(',', $include_string); |
|
1290 | + $extracted_fields_to_include = array(); |
|
1291 | + if ($model_name) { |
|
1292 | + foreach ($includes as $field_to_include) { |
|
1293 | + $field_to_include = trim($field_to_include); |
|
1294 | + if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1295 | + // found the model name at the exact start |
|
1296 | + $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1297 | + $extracted_fields_to_include[] = $field_sans_model_name; |
|
1298 | + } elseif ($field_to_include == $model_name) { |
|
1299 | + $extracted_fields_to_include[] = '*'; |
|
1300 | + } |
|
1301 | + } |
|
1302 | + } else { |
|
1303 | + // look for ones with no period |
|
1304 | + foreach ($includes as $field_to_include) { |
|
1305 | + $field_to_include = trim($field_to_include); |
|
1306 | + if (strpos($field_to_include, '.') === false |
|
1307 | + && ! $this->getModelVersionInfo()->isModelNameInThisVersion($field_to_include) |
|
1308 | + ) { |
|
1309 | + $extracted_fields_to_include[] = $field_to_include; |
|
1310 | + } |
|
1311 | + } |
|
1312 | + } |
|
1313 | + return $extracted_fields_to_include; |
|
1314 | + } |
|
1315 | + |
|
1316 | + |
|
1317 | + /** |
|
1318 | + * Gets the single item using the model according to the request in the context given, otherwise |
|
1319 | + * returns that it's inaccessible to the current user |
|
1320 | + * |
|
1321 | + * @param EEM_Base $model |
|
1322 | + * @param WP_REST_Request $request |
|
1323 | + * @param null $context |
|
1324 | + * @return array|WP_Error |
|
1325 | + */ |
|
1326 | + public function getOneOrReportPermissionError(EEM_Base $model, WP_REST_Request $request, $context = null) |
|
1327 | + { |
|
1328 | + $query_params = array(array($model->primary_key_name() => $request->get_param('id')), 'limit' => 1); |
|
1329 | + if ($model instanceof \EEM_Soft_Delete_Base) { |
|
1330 | + $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
1331 | + } |
|
1332 | + $restricted_query_params = $query_params; |
|
1333 | + $restricted_query_params['caps'] = $context; |
|
1334 | + $this->setDebugInfo('model query params', $restricted_query_params); |
|
1335 | + $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
|
1336 | + if (! empty($model_rows)) { |
|
1337 | + return $this->createEntityFromWpdbResult( |
|
1338 | + $model, |
|
1339 | + array_shift($model_rows), |
|
1340 | + $request |
|
1341 | + ); |
|
1342 | + } else { |
|
1343 | + // ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
|
1344 | + $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
1345 | + $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
|
1346 | + if (! empty($model_rows_found_sans_restrictions)) { |
|
1347 | + // you got shafted- it existed but we didn't want to tell you! |
|
1348 | + return new WP_Error( |
|
1349 | + 'rest_user_cannot_' . $context, |
|
1350 | + sprintf( |
|
1351 | + __('Sorry, you cannot %1$s this %2$s. Missing permissions are: %3$s', 'event_espresso'), |
|
1352 | + $context, |
|
1353 | + strtolower($model->get_this_model_name()), |
|
1354 | + Capabilities::getMissingPermissionsString( |
|
1355 | + $model, |
|
1356 | + $context |
|
1357 | + ) |
|
1358 | + ), |
|
1359 | + array('status' => 403) |
|
1360 | + ); |
|
1361 | + } else { |
|
1362 | + // it's not you. It just doesn't exist |
|
1363 | + return new WP_Error( |
|
1364 | + sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
1365 | + sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
1366 | + array('status' => 404) |
|
1367 | + ); |
|
1368 | + } |
|
1369 | + } |
|
1370 | + } |
|
1371 | 1371 | } |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | * Loads the class file for a given class name. |
121 | 121 | * |
122 | 122 | * @param string $class The fully-qualified class name. |
123 | - * @return mixed The mapped file name on success, or boolean false on |
|
123 | + * @return string|false The mapped file name on success, or boolean false on |
|
124 | 124 | * failure. |
125 | 125 | */ |
126 | 126 | public function loadClass($class) |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | * |
154 | 154 | * @param string $prefix The namespace prefix. |
155 | 155 | * @param string $relative_class The relative class name. |
156 | - * @return mixed Boolean false if no mapped file can be loaded, or the |
|
156 | + * @return string|false Boolean false if no mapped file can be loaded, or the |
|
157 | 157 | * name of the mapped file that was loaded. |
158 | 158 | */ |
159 | 159 | protected function loadMappedFile($prefix, $relative_class) |
@@ -45,150 +45,150 @@ |
||
45 | 45 | class Psr4Autoloader |
46 | 46 | { |
47 | 47 | |
48 | - /** |
|
49 | - * namespace separator |
|
50 | - */ |
|
51 | - const NS = '\\'; |
|
52 | - |
|
53 | - /** |
|
54 | - * An associative array where the key is a namespace prefix and the value |
|
55 | - * is an array of base directories for classes in that namespace. |
|
56 | - * |
|
57 | - * @var array |
|
58 | - */ |
|
59 | - protected $prefixes = array(); |
|
60 | - |
|
61 | - |
|
62 | - /** |
|
63 | - * returns an array of registered namespace prefixes |
|
64 | - * |
|
65 | - * @param string $prefix |
|
66 | - * @return array |
|
67 | - */ |
|
68 | - public function prefixes($prefix = '') |
|
69 | - { |
|
70 | - if (! empty($prefix)) { |
|
71 | - // are there any base directories for this namespace prefix? |
|
72 | - return isset($this->prefixes[ $prefix ]) ? $this->prefixes[ $prefix ] : array(); |
|
73 | - } |
|
74 | - return $this->prefixes; |
|
75 | - } |
|
76 | - |
|
77 | - |
|
78 | - /** |
|
79 | - * Register loader with SPL autoloader stack. |
|
80 | - * |
|
81 | - * @return void |
|
82 | - */ |
|
83 | - public function register() |
|
84 | - { |
|
85 | - spl_autoload_register(array($this, 'loadClass')); |
|
86 | - } |
|
87 | - |
|
88 | - |
|
89 | - /** |
|
90 | - * Adds a base directory for a namespace prefix. |
|
91 | - * |
|
92 | - * @param string $prefix The namespace prefix. |
|
93 | - * @param string $base_dir A base directory for class files in the |
|
94 | - * namespace. |
|
95 | - * @param bool $prepend If true, prepend the base directory to the stack |
|
96 | - * instead of appending it; this causes it to be searched first rather |
|
97 | - * than last. |
|
98 | - * @return void |
|
99 | - */ |
|
100 | - public function addNamespace($prefix, $base_dir, $prepend = false) |
|
101 | - { |
|
102 | - // normalize namespace prefix |
|
103 | - $prefix = trim($prefix, Psr4Autoloader::NS) . Psr4Autoloader::NS; |
|
104 | - // normalize the base directory with a trailing separator |
|
105 | - $base_dir = \EEH_File::standardise_and_end_with_directory_separator($base_dir); |
|
106 | - // initialize the namespace prefix array |
|
107 | - if (isset($this->prefixes[ $prefix ]) === false) { |
|
108 | - $this->prefixes[ $prefix ] = array(); |
|
109 | - } |
|
110 | - // retain the base directory for the namespace prefix |
|
111 | - if ($prepend) { |
|
112 | - array_unshift($this->prefixes[ $prefix ], $base_dir); |
|
113 | - } else { |
|
114 | - $this->prefixes[ $prefix ][] = $base_dir; |
|
115 | - } |
|
116 | - } |
|
117 | - |
|
118 | - |
|
119 | - /** |
|
120 | - * Loads the class file for a given class name. |
|
121 | - * |
|
122 | - * @param string $class The fully-qualified class name. |
|
123 | - * @return mixed The mapped file name on success, or boolean false on |
|
124 | - * failure. |
|
125 | - */ |
|
126 | - public function loadClass($class) |
|
127 | - { |
|
128 | - // the current namespace prefix |
|
129 | - $prefix = $class; |
|
130 | - // work backwards through the namespace names of the fully-qualified |
|
131 | - // class name to find a mapped file name |
|
132 | - while (false !== $pos = strrpos($prefix, Psr4Autoloader::NS)) { |
|
133 | - // retain the trailing namespace separator in the prefix |
|
134 | - $prefix = substr($class, 0, $pos + 1); |
|
135 | - // the rest is the relative class name |
|
136 | - $relative_class = substr($class, $pos + 1); |
|
137 | - // try to load a mapped file for the prefix and relative class |
|
138 | - $mapped_file = $this->loadMappedFile($prefix, $relative_class); |
|
139 | - if ($mapped_file) { |
|
140 | - return $mapped_file; |
|
141 | - } |
|
142 | - // remove the trailing namespace separator for the next iteration |
|
143 | - // of strrpos() |
|
144 | - $prefix = rtrim($prefix, Psr4Autoloader::NS); |
|
145 | - } |
|
146 | - // never found a mapped file |
|
147 | - return false; |
|
148 | - } |
|
149 | - |
|
150 | - |
|
151 | - /** |
|
152 | - * Load the mapped file for a namespace prefix and relative class. |
|
153 | - * |
|
154 | - * @param string $prefix The namespace prefix. |
|
155 | - * @param string $relative_class The relative class name. |
|
156 | - * @return mixed Boolean false if no mapped file can be loaded, or the |
|
157 | - * name of the mapped file that was loaded. |
|
158 | - */ |
|
159 | - protected function loadMappedFile($prefix, $relative_class) |
|
160 | - { |
|
161 | - // look through base directories for this namespace prefix |
|
162 | - foreach ($this->prefixes($prefix) as $base_dir) { |
|
163 | - // replace the namespace prefix with the base directory, |
|
164 | - // replace namespace separators with directory separators |
|
165 | - // in the relative class name, append with .php |
|
166 | - $file = $base_dir |
|
167 | - . str_replace(Psr4Autoloader::NS, DS, $relative_class) |
|
168 | - . '.php'; |
|
169 | - // if the mapped file exists, require it |
|
170 | - if ($this->requireFile($file)) { |
|
171 | - // yes, we're done |
|
172 | - return $file; |
|
173 | - } |
|
174 | - } |
|
175 | - // never found it |
|
176 | - return false; |
|
177 | - } |
|
178 | - |
|
179 | - |
|
180 | - /** |
|
181 | - * If a file exists, require it from the file system. |
|
182 | - * |
|
183 | - * @param string $file The file to require. |
|
184 | - * @return bool True if the file exists, false if not. |
|
185 | - */ |
|
186 | - protected function requireFile($file) |
|
187 | - { |
|
188 | - if (file_exists($file)) { |
|
189 | - require $file; |
|
190 | - return true; |
|
191 | - } |
|
192 | - return false; |
|
193 | - } |
|
48 | + /** |
|
49 | + * namespace separator |
|
50 | + */ |
|
51 | + const NS = '\\'; |
|
52 | + |
|
53 | + /** |
|
54 | + * An associative array where the key is a namespace prefix and the value |
|
55 | + * is an array of base directories for classes in that namespace. |
|
56 | + * |
|
57 | + * @var array |
|
58 | + */ |
|
59 | + protected $prefixes = array(); |
|
60 | + |
|
61 | + |
|
62 | + /** |
|
63 | + * returns an array of registered namespace prefixes |
|
64 | + * |
|
65 | + * @param string $prefix |
|
66 | + * @return array |
|
67 | + */ |
|
68 | + public function prefixes($prefix = '') |
|
69 | + { |
|
70 | + if (! empty($prefix)) { |
|
71 | + // are there any base directories for this namespace prefix? |
|
72 | + return isset($this->prefixes[ $prefix ]) ? $this->prefixes[ $prefix ] : array(); |
|
73 | + } |
|
74 | + return $this->prefixes; |
|
75 | + } |
|
76 | + |
|
77 | + |
|
78 | + /** |
|
79 | + * Register loader with SPL autoloader stack. |
|
80 | + * |
|
81 | + * @return void |
|
82 | + */ |
|
83 | + public function register() |
|
84 | + { |
|
85 | + spl_autoload_register(array($this, 'loadClass')); |
|
86 | + } |
|
87 | + |
|
88 | + |
|
89 | + /** |
|
90 | + * Adds a base directory for a namespace prefix. |
|
91 | + * |
|
92 | + * @param string $prefix The namespace prefix. |
|
93 | + * @param string $base_dir A base directory for class files in the |
|
94 | + * namespace. |
|
95 | + * @param bool $prepend If true, prepend the base directory to the stack |
|
96 | + * instead of appending it; this causes it to be searched first rather |
|
97 | + * than last. |
|
98 | + * @return void |
|
99 | + */ |
|
100 | + public function addNamespace($prefix, $base_dir, $prepend = false) |
|
101 | + { |
|
102 | + // normalize namespace prefix |
|
103 | + $prefix = trim($prefix, Psr4Autoloader::NS) . Psr4Autoloader::NS; |
|
104 | + // normalize the base directory with a trailing separator |
|
105 | + $base_dir = \EEH_File::standardise_and_end_with_directory_separator($base_dir); |
|
106 | + // initialize the namespace prefix array |
|
107 | + if (isset($this->prefixes[ $prefix ]) === false) { |
|
108 | + $this->prefixes[ $prefix ] = array(); |
|
109 | + } |
|
110 | + // retain the base directory for the namespace prefix |
|
111 | + if ($prepend) { |
|
112 | + array_unshift($this->prefixes[ $prefix ], $base_dir); |
|
113 | + } else { |
|
114 | + $this->prefixes[ $prefix ][] = $base_dir; |
|
115 | + } |
|
116 | + } |
|
117 | + |
|
118 | + |
|
119 | + /** |
|
120 | + * Loads the class file for a given class name. |
|
121 | + * |
|
122 | + * @param string $class The fully-qualified class name. |
|
123 | + * @return mixed The mapped file name on success, or boolean false on |
|
124 | + * failure. |
|
125 | + */ |
|
126 | + public function loadClass($class) |
|
127 | + { |
|
128 | + // the current namespace prefix |
|
129 | + $prefix = $class; |
|
130 | + // work backwards through the namespace names of the fully-qualified |
|
131 | + // class name to find a mapped file name |
|
132 | + while (false !== $pos = strrpos($prefix, Psr4Autoloader::NS)) { |
|
133 | + // retain the trailing namespace separator in the prefix |
|
134 | + $prefix = substr($class, 0, $pos + 1); |
|
135 | + // the rest is the relative class name |
|
136 | + $relative_class = substr($class, $pos + 1); |
|
137 | + // try to load a mapped file for the prefix and relative class |
|
138 | + $mapped_file = $this->loadMappedFile($prefix, $relative_class); |
|
139 | + if ($mapped_file) { |
|
140 | + return $mapped_file; |
|
141 | + } |
|
142 | + // remove the trailing namespace separator for the next iteration |
|
143 | + // of strrpos() |
|
144 | + $prefix = rtrim($prefix, Psr4Autoloader::NS); |
|
145 | + } |
|
146 | + // never found a mapped file |
|
147 | + return false; |
|
148 | + } |
|
149 | + |
|
150 | + |
|
151 | + /** |
|
152 | + * Load the mapped file for a namespace prefix and relative class. |
|
153 | + * |
|
154 | + * @param string $prefix The namespace prefix. |
|
155 | + * @param string $relative_class The relative class name. |
|
156 | + * @return mixed Boolean false if no mapped file can be loaded, or the |
|
157 | + * name of the mapped file that was loaded. |
|
158 | + */ |
|
159 | + protected function loadMappedFile($prefix, $relative_class) |
|
160 | + { |
|
161 | + // look through base directories for this namespace prefix |
|
162 | + foreach ($this->prefixes($prefix) as $base_dir) { |
|
163 | + // replace the namespace prefix with the base directory, |
|
164 | + // replace namespace separators with directory separators |
|
165 | + // in the relative class name, append with .php |
|
166 | + $file = $base_dir |
|
167 | + . str_replace(Psr4Autoloader::NS, DS, $relative_class) |
|
168 | + . '.php'; |
|
169 | + // if the mapped file exists, require it |
|
170 | + if ($this->requireFile($file)) { |
|
171 | + // yes, we're done |
|
172 | + return $file; |
|
173 | + } |
|
174 | + } |
|
175 | + // never found it |
|
176 | + return false; |
|
177 | + } |
|
178 | + |
|
179 | + |
|
180 | + /** |
|
181 | + * If a file exists, require it from the file system. |
|
182 | + * |
|
183 | + * @param string $file The file to require. |
|
184 | + * @return bool True if the file exists, false if not. |
|
185 | + */ |
|
186 | + protected function requireFile($file) |
|
187 | + { |
|
188 | + if (file_exists($file)) { |
|
189 | + require $file; |
|
190 | + return true; |
|
191 | + } |
|
192 | + return false; |
|
193 | + } |
|
194 | 194 | } |
@@ -67,9 +67,9 @@ discard block |
||
67 | 67 | */ |
68 | 68 | public function prefixes($prefix = '') |
69 | 69 | { |
70 | - if (! empty($prefix)) { |
|
70 | + if ( ! empty($prefix)) { |
|
71 | 71 | // are there any base directories for this namespace prefix? |
72 | - return isset($this->prefixes[ $prefix ]) ? $this->prefixes[ $prefix ] : array(); |
|
72 | + return isset($this->prefixes[$prefix]) ? $this->prefixes[$prefix] : array(); |
|
73 | 73 | } |
74 | 74 | return $this->prefixes; |
75 | 75 | } |
@@ -100,18 +100,18 @@ discard block |
||
100 | 100 | public function addNamespace($prefix, $base_dir, $prepend = false) |
101 | 101 | { |
102 | 102 | // normalize namespace prefix |
103 | - $prefix = trim($prefix, Psr4Autoloader::NS) . Psr4Autoloader::NS; |
|
103 | + $prefix = trim($prefix, Psr4Autoloader::NS).Psr4Autoloader::NS; |
|
104 | 104 | // normalize the base directory with a trailing separator |
105 | 105 | $base_dir = \EEH_File::standardise_and_end_with_directory_separator($base_dir); |
106 | 106 | // initialize the namespace prefix array |
107 | - if (isset($this->prefixes[ $prefix ]) === false) { |
|
108 | - $this->prefixes[ $prefix ] = array(); |
|
107 | + if (isset($this->prefixes[$prefix]) === false) { |
|
108 | + $this->prefixes[$prefix] = array(); |
|
109 | 109 | } |
110 | 110 | // retain the base directory for the namespace prefix |
111 | 111 | if ($prepend) { |
112 | - array_unshift($this->prefixes[ $prefix ], $base_dir); |
|
112 | + array_unshift($this->prefixes[$prefix], $base_dir); |
|
113 | 113 | } else { |
114 | - $this->prefixes[ $prefix ][] = $base_dir; |
|
114 | + $this->prefixes[$prefix][] = $base_dir; |
|
115 | 115 | } |
116 | 116 | } |
117 | 117 |
@@ -30,7 +30,7 @@ |
||
30 | 30 | /** |
31 | 31 | * @param RecipeInterface $recipe |
32 | 32 | * @param array $arguments |
33 | - * @return mixed |
|
33 | + * @return boolean |
|
34 | 34 | */ |
35 | 35 | public function brew(RecipeInterface $recipe, $arguments = array()) |
36 | 36 | { |
@@ -18,43 +18,43 @@ |
||
18 | 18 | { |
19 | 19 | |
20 | 20 | |
21 | - /** |
|
22 | - * @return string |
|
23 | - */ |
|
24 | - public function type() |
|
25 | - { |
|
26 | - return CoffeeMaker::BREW_SHARED; |
|
27 | - } |
|
21 | + /** |
|
22 | + * @return string |
|
23 | + */ |
|
24 | + public function type() |
|
25 | + { |
|
26 | + return CoffeeMaker::BREW_SHARED; |
|
27 | + } |
|
28 | 28 | |
29 | 29 | |
30 | - /** |
|
31 | - * @param RecipeInterface $recipe |
|
32 | - * @param array $arguments |
|
33 | - * @return mixed |
|
34 | - */ |
|
35 | - public function brew(RecipeInterface $recipe, $arguments = array()) |
|
36 | - { |
|
37 | - $this->resolveClassAndFilepath($recipe); |
|
38 | - $reflector = $this->injector()->getReflectionClass($recipe->fqcn()); |
|
39 | - $method = $this->resolveInstantiationMethod($reflector); |
|
40 | - switch ($method) { |
|
41 | - case 'instance': |
|
42 | - case 'new_instance': |
|
43 | - case 'new_instance_from_db': |
|
44 | - $service = call_user_func_array( |
|
45 | - array($reflector->getName(), $method), |
|
46 | - $this->injector()->resolveDependencies($recipe, $reflector, $arguments) |
|
47 | - ); |
|
48 | - break; |
|
49 | - case 'newInstance': |
|
50 | - $service = $reflector->newInstance(); |
|
51 | - break; |
|
52 | - case 'newInstanceArgs': |
|
53 | - default: |
|
54 | - $service = $reflector->newInstanceArgs( |
|
55 | - $this->injector()->resolveDependencies($recipe, $reflector, $arguments) |
|
56 | - ); |
|
57 | - } |
|
58 | - return $this->coffeePot()->addService($recipe->identifier(), $service); |
|
59 | - } |
|
30 | + /** |
|
31 | + * @param RecipeInterface $recipe |
|
32 | + * @param array $arguments |
|
33 | + * @return mixed |
|
34 | + */ |
|
35 | + public function brew(RecipeInterface $recipe, $arguments = array()) |
|
36 | + { |
|
37 | + $this->resolveClassAndFilepath($recipe); |
|
38 | + $reflector = $this->injector()->getReflectionClass($recipe->fqcn()); |
|
39 | + $method = $this->resolveInstantiationMethod($reflector); |
|
40 | + switch ($method) { |
|
41 | + case 'instance': |
|
42 | + case 'new_instance': |
|
43 | + case 'new_instance_from_db': |
|
44 | + $service = call_user_func_array( |
|
45 | + array($reflector->getName(), $method), |
|
46 | + $this->injector()->resolveDependencies($recipe, $reflector, $arguments) |
|
47 | + ); |
|
48 | + break; |
|
49 | + case 'newInstance': |
|
50 | + $service = $reflector->newInstance(); |
|
51 | + break; |
|
52 | + case 'newInstanceArgs': |
|
53 | + default: |
|
54 | + $service = $reflector->newInstanceArgs( |
|
55 | + $this->injector()->resolveDependencies($recipe, $reflector, $arguments) |
|
56 | + ); |
|
57 | + } |
|
58 | + return $this->coffeePot()->addService($recipe->identifier(), $service); |
|
59 | + } |
|
60 | 60 | } |
@@ -33,7 +33,7 @@ |
||
33 | 33 | /** |
34 | 34 | * Taken from https://gist.github.com/jaywilliams/119517 |
35 | 35 | * |
36 | - * @param $string |
|
36 | + * @param string $string |
|
37 | 37 | * @return string |
38 | 38 | */ |
39 | 39 | protected function convertAscii($string) |
@@ -2,8 +2,6 @@ |
||
2 | 2 | |
3 | 3 | namespace EventEspresso\core\services\formatters; |
4 | 4 | |
5 | -use EventEspresso\core\exceptions\InvalidDataTypeException; |
|
6 | - |
|
7 | 5 | /** |
8 | 6 | * Class AsciiOnly |
9 | 7 | * Removes all non-ascii characters from the string |
@@ -15,58 +15,58 @@ |
||
15 | 15 | class AsciiOnly extends FormatterBase |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * Removes all non Ascii characters from string |
|
20 | - * |
|
21 | - * @param string|int|float $input anything easily cast into a string |
|
22 | - * @return string |
|
23 | - */ |
|
24 | - public function format($input) |
|
25 | - { |
|
26 | - // in case an int or float etc was passed in |
|
27 | - $input = (string) $input; |
|
28 | - $input = $this->convertAscii($input); |
|
29 | - return $input; |
|
30 | - } |
|
18 | + /** |
|
19 | + * Removes all non Ascii characters from string |
|
20 | + * |
|
21 | + * @param string|int|float $input anything easily cast into a string |
|
22 | + * @return string |
|
23 | + */ |
|
24 | + public function format($input) |
|
25 | + { |
|
26 | + // in case an int or float etc was passed in |
|
27 | + $input = (string) $input; |
|
28 | + $input = $this->convertAscii($input); |
|
29 | + return $input; |
|
30 | + } |
|
31 | 31 | |
32 | 32 | |
33 | - /** |
|
34 | - * Taken from https://gist.github.com/jaywilliams/119517 |
|
35 | - * |
|
36 | - * @param $string |
|
37 | - * @return string |
|
38 | - */ |
|
39 | - protected function convertAscii($string) |
|
40 | - { |
|
41 | - // Replace Single Curly Quotes |
|
42 | - $search[] = chr(226) . chr(128) . chr(152); |
|
43 | - $replace[] = "'"; |
|
44 | - $search[] = chr(226) . chr(128) . chr(153); |
|
45 | - $replace[] = "'"; |
|
46 | - // Replace Smart Double Curly Quotes |
|
47 | - $search[] = chr(226) . chr(128) . chr(156); |
|
48 | - $replace[] = '"'; |
|
49 | - $search[] = chr(226) . chr(128) . chr(157); |
|
50 | - $replace[] = '"'; |
|
51 | - // Replace En Dash |
|
52 | - $search[] = chr(226) . chr(128) . chr(147); |
|
53 | - $replace[] = '--'; |
|
54 | - // Replace Em Dash |
|
55 | - $search[] = chr(226) . chr(128) . chr(148); |
|
56 | - $replace[] = '---'; |
|
57 | - // Replace Bullet |
|
58 | - $search[] = chr(226) . chr(128) . chr(162); |
|
59 | - $replace[] = '*'; |
|
60 | - // Replace Middle Dot |
|
61 | - $search[] = chr(194) . chr(183); |
|
62 | - $replace[] = '*'; |
|
63 | - // Replace Ellipsis with three consecutive dots |
|
64 | - $search[] = chr(226) . chr(128) . chr(166); |
|
65 | - $replace[] = '...'; |
|
66 | - // Apply Replacements |
|
67 | - $string = str_replace($search, $replace, $string); |
|
68 | - // Remove any non-ASCII Characters |
|
69 | - $string = preg_replace("/[^\x01-\x7F]/", "", $string); |
|
70 | - return $string; |
|
71 | - } |
|
33 | + /** |
|
34 | + * Taken from https://gist.github.com/jaywilliams/119517 |
|
35 | + * |
|
36 | + * @param $string |
|
37 | + * @return string |
|
38 | + */ |
|
39 | + protected function convertAscii($string) |
|
40 | + { |
|
41 | + // Replace Single Curly Quotes |
|
42 | + $search[] = chr(226) . chr(128) . chr(152); |
|
43 | + $replace[] = "'"; |
|
44 | + $search[] = chr(226) . chr(128) . chr(153); |
|
45 | + $replace[] = "'"; |
|
46 | + // Replace Smart Double Curly Quotes |
|
47 | + $search[] = chr(226) . chr(128) . chr(156); |
|
48 | + $replace[] = '"'; |
|
49 | + $search[] = chr(226) . chr(128) . chr(157); |
|
50 | + $replace[] = '"'; |
|
51 | + // Replace En Dash |
|
52 | + $search[] = chr(226) . chr(128) . chr(147); |
|
53 | + $replace[] = '--'; |
|
54 | + // Replace Em Dash |
|
55 | + $search[] = chr(226) . chr(128) . chr(148); |
|
56 | + $replace[] = '---'; |
|
57 | + // Replace Bullet |
|
58 | + $search[] = chr(226) . chr(128) . chr(162); |
|
59 | + $replace[] = '*'; |
|
60 | + // Replace Middle Dot |
|
61 | + $search[] = chr(194) . chr(183); |
|
62 | + $replace[] = '*'; |
|
63 | + // Replace Ellipsis with three consecutive dots |
|
64 | + $search[] = chr(226) . chr(128) . chr(166); |
|
65 | + $replace[] = '...'; |
|
66 | + // Apply Replacements |
|
67 | + $string = str_replace($search, $replace, $string); |
|
68 | + // Remove any non-ASCII Characters |
|
69 | + $string = preg_replace("/[^\x01-\x7F]/", "", $string); |
|
70 | + return $string; |
|
71 | + } |
|
72 | 72 | } |
@@ -39,29 +39,29 @@ |
||
39 | 39 | protected function convertAscii($string) |
40 | 40 | { |
41 | 41 | // Replace Single Curly Quotes |
42 | - $search[] = chr(226) . chr(128) . chr(152); |
|
42 | + $search[] = chr(226).chr(128).chr(152); |
|
43 | 43 | $replace[] = "'"; |
44 | - $search[] = chr(226) . chr(128) . chr(153); |
|
44 | + $search[] = chr(226).chr(128).chr(153); |
|
45 | 45 | $replace[] = "'"; |
46 | 46 | // Replace Smart Double Curly Quotes |
47 | - $search[] = chr(226) . chr(128) . chr(156); |
|
47 | + $search[] = chr(226).chr(128).chr(156); |
|
48 | 48 | $replace[] = '"'; |
49 | - $search[] = chr(226) . chr(128) . chr(157); |
|
49 | + $search[] = chr(226).chr(128).chr(157); |
|
50 | 50 | $replace[] = '"'; |
51 | 51 | // Replace En Dash |
52 | - $search[] = chr(226) . chr(128) . chr(147); |
|
52 | + $search[] = chr(226).chr(128).chr(147); |
|
53 | 53 | $replace[] = '--'; |
54 | 54 | // Replace Em Dash |
55 | - $search[] = chr(226) . chr(128) . chr(148); |
|
55 | + $search[] = chr(226).chr(128).chr(148); |
|
56 | 56 | $replace[] = '---'; |
57 | 57 | // Replace Bullet |
58 | - $search[] = chr(226) . chr(128) . chr(162); |
|
58 | + $search[] = chr(226).chr(128).chr(162); |
|
59 | 59 | $replace[] = '*'; |
60 | 60 | // Replace Middle Dot |
61 | - $search[] = chr(194) . chr(183); |
|
61 | + $search[] = chr(194).chr(183); |
|
62 | 62 | $replace[] = '*'; |
63 | 63 | // Replace Ellipsis with three consecutive dots |
64 | - $search[] = chr(226) . chr(128) . chr(166); |
|
64 | + $search[] = chr(226).chr(128).chr(166); |
|
65 | 65 | $replace[] = '...'; |
66 | 66 | // Apply Replacements |
67 | 67 | $string = str_replace($search, $replace, $string); |