@@ -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( |
@@ -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) |
@@ -35,1323 +35,1323 @@ |
||
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 | - $timezone = $field_value->getTimezone(); |
|
692 | - EEH_DTT_Helper::setTimezone($field_value, new DateTimeZone('UTC')); |
|
693 | - $result[ $field_name . '_gmt' ] = ModelDataTranslator::prepareFieldValuesForJson( |
|
694 | - $field_obj, |
|
695 | - $field_value, |
|
696 | - $this->getModelVersionInfo()->requestedVersion() |
|
697 | - ); |
|
698 | - EEH_DTT_Helper::setTimezone($field_value, $timezone); |
|
699 | - $result[ $field_name ] = ModelDataTranslator::prepareFieldValuesForJson( |
|
700 | - $field_obj, |
|
701 | - $field_value, |
|
702 | - $this->getModelVersionInfo()->requestedVersion() |
|
703 | - ); |
|
704 | - } else { |
|
705 | - $result[ $field_name ] = $this->prepareFieldObjValueForJson($field_obj, $field_value); |
|
706 | - } |
|
707 | - } |
|
708 | - if ($do_chevy_shuffle) { |
|
709 | - $post = $old_post; |
|
710 | - } |
|
711 | - return $result; |
|
712 | - } |
|
713 | - |
|
714 | - |
|
715 | - /** |
|
716 | - * Takes a value all the way from the DB representation, to the model object's representation, to the |
|
717 | - * user-facing PHP representation, to the REST API representation. (Assumes you've already taken from the DB |
|
718 | - * representation using $field_obj->prepare_for_set_from_db()) |
|
719 | - * |
|
720 | - * @param EE_Model_Field_Base $field_obj |
|
721 | - * @param mixed $value as it's stored on a model object |
|
722 | - * @param string $format valid values are 'normal' (default), 'pretty', 'datetime_obj' |
|
723 | - * @return mixed |
|
724 | - * @throws ObjectDetectedException if $value contains a PHP object |
|
725 | - */ |
|
726 | - protected function prepareFieldObjValueForJson(EE_Model_Field_Base $field_obj, $value, $format = 'normal') |
|
727 | - { |
|
728 | - $value = $field_obj->prepare_for_set_from_db($value); |
|
729 | - switch ($format) { |
|
730 | - case 'pretty': |
|
731 | - $value = $field_obj->prepare_for_pretty_echoing($value); |
|
732 | - break; |
|
733 | - case 'normal': |
|
734 | - default: |
|
735 | - $value = $field_obj->prepare_for_get($value); |
|
736 | - break; |
|
737 | - } |
|
738 | - return ModelDataTranslator::prepareFieldValuesForJson( |
|
739 | - $field_obj, |
|
740 | - $value, |
|
741 | - $this->getModelVersionInfo()->requestedVersion() |
|
742 | - ); |
|
743 | - } |
|
744 | - |
|
745 | - |
|
746 | - /** |
|
747 | - * Adds a few extra fields to the entity response |
|
748 | - * |
|
749 | - * @param EEM_Base $model |
|
750 | - * @param array $db_row |
|
751 | - * @param array $entity_array |
|
752 | - * @return array modified entity |
|
753 | - */ |
|
754 | - protected function addExtraFields(EEM_Base $model, $db_row, $entity_array) |
|
755 | - { |
|
756 | - if ($model instanceof EEM_CPT_Base) { |
|
757 | - $entity_array['link'] = get_permalink($db_row[ $model->get_primary_key_field()->get_qualified_column() ]); |
|
758 | - } |
|
759 | - return $entity_array; |
|
760 | - } |
|
761 | - |
|
762 | - |
|
763 | - /** |
|
764 | - * Gets links we want to add to the response |
|
765 | - * |
|
766 | - * @global \WP_REST_Server $wp_rest_server |
|
767 | - * @param EEM_Base $model |
|
768 | - * @param array $db_row |
|
769 | - * @param array $entity_array |
|
770 | - * @return array the _links item in the entity |
|
771 | - */ |
|
772 | - protected function getEntityLinks($model, $db_row, $entity_array) |
|
773 | - { |
|
774 | - // add basic links |
|
775 | - $links = array(); |
|
776 | - if ($model->has_primary_key_field()) { |
|
777 | - $links['self'] = array( |
|
778 | - array( |
|
779 | - 'href' => $this->getVersionedLinkTo( |
|
780 | - EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
781 | - . '/' |
|
782 | - . $entity_array[ $model->primary_key_name() ] |
|
783 | - ), |
|
784 | - ), |
|
785 | - ); |
|
786 | - } |
|
787 | - $links['collection'] = array( |
|
788 | - array( |
|
789 | - 'href' => $this->getVersionedLinkTo( |
|
790 | - EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
791 | - ), |
|
792 | - ), |
|
793 | - ); |
|
794 | - // add links to related models |
|
795 | - if ($model->has_primary_key_field()) { |
|
796 | - foreach ($this->getModelVersionInfo()->relationSettings($model) as $relation_name => $relation_obj) { |
|
797 | - $related_model_part = Read::getRelatedEntityName($relation_name, $relation_obj); |
|
798 | - $links[ EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part ] = array( |
|
799 | - array( |
|
800 | - 'href' => $this->getVersionedLinkTo( |
|
801 | - EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
802 | - . '/' |
|
803 | - . $entity_array[ $model->primary_key_name() ] |
|
804 | - . '/' |
|
805 | - . $related_model_part |
|
806 | - ), |
|
807 | - 'single' => $relation_obj instanceof EE_Belongs_To_Relation ? true : false, |
|
808 | - ), |
|
809 | - ); |
|
810 | - } |
|
811 | - } |
|
812 | - return $links; |
|
813 | - } |
|
814 | - |
|
815 | - |
|
816 | - /** |
|
817 | - * Adds the included models indicated in the request to the entity provided |
|
818 | - * |
|
819 | - * @param EEM_Base $model |
|
820 | - * @param WP_REST_Request $rest_request |
|
821 | - * @param array $entity_array |
|
822 | - * @param array $db_row |
|
823 | - * @return array the modified entity |
|
824 | - */ |
|
825 | - protected function includeRequestedModels( |
|
826 | - EEM_Base $model, |
|
827 | - WP_REST_Request $rest_request, |
|
828 | - $entity_array, |
|
829 | - $db_row = array() |
|
830 | - ) { |
|
831 | - // if $db_row not included, hope the entity array has what we need |
|
832 | - if (! $db_row) { |
|
833 | - $db_row = $entity_array; |
|
834 | - } |
|
835 | - $includes_for_this_model = $this->explodeAndGetItemsPrefixedWith($rest_request->get_param('include'), ''); |
|
836 | - $includes_for_this_model = $this->removeModelNamesFromArray($includes_for_this_model); |
|
837 | - // if they passed in * or didn't specify any includes, return everything |
|
838 | - if (! in_array('*', $includes_for_this_model) |
|
839 | - && ! empty($includes_for_this_model) |
|
840 | - ) { |
|
841 | - if ($model->has_primary_key_field()) { |
|
842 | - // always include the primary key. ya just gotta know that at least |
|
843 | - $includes_for_this_model[] = $model->primary_key_name(); |
|
844 | - } |
|
845 | - if ($this->explodeAndGetItemsPrefixedWith($rest_request->get_param('calculate'), '')) { |
|
846 | - $includes_for_this_model[] = '_calculated_fields'; |
|
847 | - } |
|
848 | - $entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model)); |
|
849 | - } |
|
850 | - $relation_settings = $this->getModelVersionInfo()->relationSettings($model); |
|
851 | - foreach ($relation_settings as $relation_name => $relation_obj) { |
|
852 | - $related_fields_to_include = $this->explodeAndGetItemsPrefixedWith( |
|
853 | - $rest_request->get_param('include'), |
|
854 | - $relation_name |
|
855 | - ); |
|
856 | - $related_fields_to_calculate = $this->explodeAndGetItemsPrefixedWith( |
|
857 | - $rest_request->get_param('calculate'), |
|
858 | - $relation_name |
|
859 | - ); |
|
860 | - // did they specify they wanted to include a related model, or |
|
861 | - // specific fields from a related model? |
|
862 | - // or did they specify to calculate a field from a related model? |
|
863 | - if ($related_fields_to_include || $related_fields_to_calculate) { |
|
864 | - // if so, we should include at least some part of the related model |
|
865 | - $pretend_related_request = new WP_REST_Request(); |
|
866 | - $pretend_related_request->set_query_params( |
|
867 | - array( |
|
868 | - 'caps' => $rest_request->get_param('caps'), |
|
869 | - 'include' => $related_fields_to_include, |
|
870 | - 'calculate' => $related_fields_to_calculate, |
|
871 | - ) |
|
872 | - ); |
|
873 | - $pretend_related_request->add_header('no_rest_headers', true); |
|
874 | - $primary_model_query_params = $model->alter_query_params_to_restrict_by_ID( |
|
875 | - $model->get_index_primary_key_string( |
|
876 | - $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
877 | - ) |
|
878 | - ); |
|
879 | - $related_results = $this->getEntitiesFromRelationUsingModelQueryParams( |
|
880 | - $primary_model_query_params, |
|
881 | - $relation_obj, |
|
882 | - $pretend_related_request |
|
883 | - ); |
|
884 | - $entity_array[ Read::getRelatedEntityName($relation_name, $relation_obj) ] = $related_results |
|
885 | - instanceof |
|
886 | - WP_Error |
|
887 | - ? null |
|
888 | - : $related_results; |
|
889 | - } |
|
890 | - } |
|
891 | - return $entity_array; |
|
892 | - } |
|
893 | - |
|
894 | - |
|
895 | - /** |
|
896 | - * Returns a new array with all the names of models removed. Eg |
|
897 | - * array( 'Event', 'Datetime.*', 'foobar' ) would become array( 'Datetime.*', 'foobar' ) |
|
898 | - * |
|
899 | - * @param array $arr |
|
900 | - * @return array |
|
901 | - */ |
|
902 | - private function removeModelNamesFromArray($arr) |
|
903 | - { |
|
904 | - return array_diff($arr, array_keys(EE_Registry::instance()->non_abstract_db_models)); |
|
905 | - } |
|
906 | - |
|
907 | - |
|
908 | - /** |
|
909 | - * Gets the calculated fields for the response |
|
910 | - * |
|
911 | - * @param EEM_Base $model |
|
912 | - * @param array $wpdb_row |
|
913 | - * @param WP_REST_Request $rest_request |
|
914 | - * @return \stdClass the _calculations item in the entity |
|
915 | - * @throws ObjectDetectedException if a default value has a PHP object, which should never do (and if we |
|
916 | - * did, let's know about it ASAP, so let the exception bubble up) |
|
917 | - */ |
|
918 | - protected function getEntityCalculations($model, $wpdb_row, $rest_request) |
|
919 | - { |
|
920 | - $calculated_fields = $this->explodeAndGetItemsPrefixedWith( |
|
921 | - $rest_request->get_param('calculate'), |
|
922 | - '' |
|
923 | - ); |
|
924 | - // note: setting calculate=* doesn't do anything |
|
925 | - $calculated_fields_to_return = new \stdClass(); |
|
926 | - foreach ($calculated_fields as $field_to_calculate) { |
|
927 | - try { |
|
928 | - $calculated_fields_to_return->$field_to_calculate = ModelDataTranslator::prepareFieldValueForJson( |
|
929 | - null, |
|
930 | - $this->fields_calculator->retrieveCalculatedFieldValue( |
|
931 | - $model, |
|
932 | - $field_to_calculate, |
|
933 | - $wpdb_row, |
|
934 | - $rest_request, |
|
935 | - $this |
|
936 | - ), |
|
937 | - $this->getModelVersionInfo()->requestedVersion() |
|
938 | - ); |
|
939 | - } catch (RestException $e) { |
|
940 | - // if we don't have permission to read it, just leave it out. but let devs know about the problem |
|
941 | - $this->setResponseHeader( |
|
942 | - 'Notices-Field-Calculation-Errors[' |
|
943 | - . $e->getStringCode() |
|
944 | - . '][' |
|
945 | - . $model->get_this_model_name() |
|
946 | - . '][' |
|
947 | - . $field_to_calculate |
|
948 | - . ']', |
|
949 | - $e->getMessage(), |
|
950 | - true |
|
951 | - ); |
|
952 | - } |
|
953 | - } |
|
954 | - return $calculated_fields_to_return; |
|
955 | - } |
|
956 | - |
|
957 | - |
|
958 | - /** |
|
959 | - * Gets the full URL to the resource, taking the requested version into account |
|
960 | - * |
|
961 | - * @param string $link_part_after_version_and_slash eg "events/10/datetimes" |
|
962 | - * @return string url eg "http://mysite.com/wp-json/ee/v4.6/events/10/datetimes" |
|
963 | - */ |
|
964 | - public function getVersionedLinkTo($link_part_after_version_and_slash) |
|
965 | - { |
|
966 | - return rest_url( |
|
967 | - EED_Core_Rest_Api::get_versioned_route_to( |
|
968 | - $link_part_after_version_and_slash, |
|
969 | - $this->getModelVersionInfo()->requestedVersion() |
|
970 | - ) |
|
971 | - ); |
|
972 | - } |
|
973 | - |
|
974 | - |
|
975 | - /** |
|
976 | - * Gets the correct lowercase name for the relation in the API according |
|
977 | - * to the relation's type |
|
978 | - * |
|
979 | - * @param string $relation_name |
|
980 | - * @param \EE_Model_Relation_Base $relation_obj |
|
981 | - * @return string |
|
982 | - */ |
|
983 | - public static function getRelatedEntityName($relation_name, $relation_obj) |
|
984 | - { |
|
985 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
986 | - return strtolower($relation_name); |
|
987 | - } else { |
|
988 | - return EEH_Inflector::pluralize_and_lower($relation_name); |
|
989 | - } |
|
990 | - } |
|
991 | - |
|
992 | - |
|
993 | - /** |
|
994 | - * Gets the one model object with the specified id for the specified model |
|
995 | - * |
|
996 | - * @param EEM_Base $model |
|
997 | - * @param WP_REST_Request $request |
|
998 | - * @return array|WP_Error |
|
999 | - */ |
|
1000 | - public function getEntityFromModel($model, $request) |
|
1001 | - { |
|
1002 | - $context = $this->validateContext($request->get_param('caps')); |
|
1003 | - return $this->getOneOrReportPermissionError($model, $request, $context); |
|
1004 | - } |
|
1005 | - |
|
1006 | - |
|
1007 | - /** |
|
1008 | - * If a context is provided which isn't valid, maybe it was added in a future |
|
1009 | - * version so just treat it as a default read |
|
1010 | - * |
|
1011 | - * @param string $context |
|
1012 | - * @return string array key of EEM_Base::cap_contexts_to_cap_action_map() |
|
1013 | - */ |
|
1014 | - public function validateContext($context) |
|
1015 | - { |
|
1016 | - if (! $context) { |
|
1017 | - $context = EEM_Base::caps_read; |
|
1018 | - } |
|
1019 | - $valid_contexts = EEM_Base::valid_cap_contexts(); |
|
1020 | - if (in_array($context, $valid_contexts)) { |
|
1021 | - return $context; |
|
1022 | - } else { |
|
1023 | - return EEM_Base::caps_read; |
|
1024 | - } |
|
1025 | - } |
|
1026 | - |
|
1027 | - |
|
1028 | - /** |
|
1029 | - * Verifies the passed in value is an allowable default where conditions value. |
|
1030 | - * |
|
1031 | - * @param $default_query_params |
|
1032 | - * @return string |
|
1033 | - */ |
|
1034 | - public function validateDefaultQueryParams($default_query_params) |
|
1035 | - { |
|
1036 | - $valid_default_where_conditions_for_api_calls = array( |
|
1037 | - EEM_Base::default_where_conditions_all, |
|
1038 | - EEM_Base::default_where_conditions_minimum_all, |
|
1039 | - EEM_Base::default_where_conditions_minimum_others, |
|
1040 | - ); |
|
1041 | - if (! $default_query_params) { |
|
1042 | - $default_query_params = EEM_Base::default_where_conditions_all; |
|
1043 | - } |
|
1044 | - if (in_array( |
|
1045 | - $default_query_params, |
|
1046 | - $valid_default_where_conditions_for_api_calls, |
|
1047 | - true |
|
1048 | - )) { |
|
1049 | - return $default_query_params; |
|
1050 | - } else { |
|
1051 | - return EEM_Base::default_where_conditions_all; |
|
1052 | - } |
|
1053 | - } |
|
1054 | - |
|
1055 | - |
|
1056 | - /** |
|
1057 | - * Translates API filter get parameter into $query_params array used by EEM_Base::get_all(). |
|
1058 | - * Note: right now the query parameter keys for fields (and related fields) |
|
1059 | - * can be left as-is, but it's quite possible this will change someday. |
|
1060 | - * Also, this method's contents might be candidate for moving to Model_Data_Translator |
|
1061 | - * |
|
1062 | - * @param EEM_Base $model |
|
1063 | - * @param array $query_parameters from $_GET parameter @see Read:handle_request_get_all |
|
1064 | - * @return array like what EEM_Base::get_all() expects or FALSE to indicate |
|
1065 | - * that absolutely no results should be returned |
|
1066 | - * @throws EE_Error |
|
1067 | - * @throws RestException |
|
1068 | - */ |
|
1069 | - public function createModelQueryParams($model, $query_parameters) |
|
1070 | - { |
|
1071 | - $model_query_params = array(); |
|
1072 | - if (isset($query_parameters['where'])) { |
|
1073 | - $model_query_params[0] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
1074 | - $query_parameters['where'], |
|
1075 | - $model, |
|
1076 | - $this->getModelVersionInfo()->requestedVersion() |
|
1077 | - ); |
|
1078 | - } |
|
1079 | - if (isset($query_parameters['order_by'])) { |
|
1080 | - $order_by = $query_parameters['order_by']; |
|
1081 | - } elseif (isset($query_parameters['orderby'])) { |
|
1082 | - $order_by = $query_parameters['orderby']; |
|
1083 | - } else { |
|
1084 | - $order_by = null; |
|
1085 | - } |
|
1086 | - if ($order_by !== null) { |
|
1087 | - if (is_array($order_by)) { |
|
1088 | - $order_by = ModelDataTranslator::prepareFieldNamesInArrayKeysFromJson($order_by); |
|
1089 | - } else { |
|
1090 | - // it's a single item |
|
1091 | - $order_by = ModelDataTranslator::prepareFieldNameFromJson($order_by); |
|
1092 | - } |
|
1093 | - $model_query_params['order_by'] = $order_by; |
|
1094 | - } |
|
1095 | - if (isset($query_parameters['group_by'])) { |
|
1096 | - $group_by = $query_parameters['group_by']; |
|
1097 | - } elseif (isset($query_parameters['groupby'])) { |
|
1098 | - $group_by = $query_parameters['groupby']; |
|
1099 | - } else { |
|
1100 | - $group_by = array_keys($model->get_combined_primary_key_fields()); |
|
1101 | - } |
|
1102 | - // make sure they're all real names |
|
1103 | - if (is_array($group_by)) { |
|
1104 | - $group_by = ModelDataTranslator::prepareFieldNamesFromJson($group_by); |
|
1105 | - } |
|
1106 | - if ($group_by !== null) { |
|
1107 | - $model_query_params['group_by'] = $group_by; |
|
1108 | - } |
|
1109 | - if (isset($query_parameters['having'])) { |
|
1110 | - $model_query_params['having'] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
1111 | - $query_parameters['having'], |
|
1112 | - $model, |
|
1113 | - $this->getModelVersionInfo()->requestedVersion() |
|
1114 | - ); |
|
1115 | - } |
|
1116 | - if (isset($query_parameters['order'])) { |
|
1117 | - $model_query_params['order'] = $query_parameters['order']; |
|
1118 | - } |
|
1119 | - if (isset($query_parameters['mine'])) { |
|
1120 | - $model_query_params = $model->alter_query_params_to_only_include_mine($model_query_params); |
|
1121 | - } |
|
1122 | - if (isset($query_parameters['limit'])) { |
|
1123 | - // limit should be either a string like '23' or '23,43', or an array with two items in it |
|
1124 | - if (! is_array($query_parameters['limit'])) { |
|
1125 | - $limit_array = explode(',', (string) $query_parameters['limit']); |
|
1126 | - } else { |
|
1127 | - $limit_array = $query_parameters['limit']; |
|
1128 | - } |
|
1129 | - $sanitized_limit = array(); |
|
1130 | - foreach ($limit_array as $key => $limit_part) { |
|
1131 | - if ($this->debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1132 | - throw new EE_Error( |
|
1133 | - sprintf( |
|
1134 | - __( |
|
1135 | - // @codingStandardsIgnoreStart |
|
1136 | - '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.', |
|
1137 | - // @codingStandardsIgnoreEnd |
|
1138 | - 'event_espresso' |
|
1139 | - ), |
|
1140 | - wp_json_encode($query_parameters['limit']) |
|
1141 | - ) |
|
1142 | - ); |
|
1143 | - } |
|
1144 | - $sanitized_limit[] = (int) $limit_part; |
|
1145 | - } |
|
1146 | - $model_query_params['limit'] = implode(',', $sanitized_limit); |
|
1147 | - } else { |
|
1148 | - $model_query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
|
1149 | - } |
|
1150 | - if (isset($query_parameters['caps'])) { |
|
1151 | - $model_query_params['caps'] = $this->validateContext($query_parameters['caps']); |
|
1152 | - } else { |
|
1153 | - $model_query_params['caps'] = EEM_Base::caps_read; |
|
1154 | - } |
|
1155 | - if (isset($query_parameters['default_where_conditions'])) { |
|
1156 | - $model_query_params['default_where_conditions'] = $this->validateDefaultQueryParams( |
|
1157 | - $query_parameters['default_where_conditions'] |
|
1158 | - ); |
|
1159 | - } |
|
1160 | - return apply_filters('FHEE__Read__create_model_query_params', $model_query_params, $query_parameters, $model); |
|
1161 | - } |
|
1162 | - |
|
1163 | - |
|
1164 | - /** |
|
1165 | - * Changes the REST-style query params for use in the models |
|
1166 | - * |
|
1167 | - * @deprecated |
|
1168 | - * @param EEM_Base $model |
|
1169 | - * @param array $query_params sub-array from @see EEM_Base::get_all() |
|
1170 | - * @return array |
|
1171 | - */ |
|
1172 | - public function prepareRestQueryParamsKeyForModels($model, $query_params) |
|
1173 | - { |
|
1174 | - $model_ready_query_params = array(); |
|
1175 | - foreach ($query_params as $key => $value) { |
|
1176 | - if (is_array($value)) { |
|
1177 | - $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsKeyForModels($model, $value); |
|
1178 | - } else { |
|
1179 | - $model_ready_query_params[ $key ] = $value; |
|
1180 | - } |
|
1181 | - } |
|
1182 | - return $model_ready_query_params; |
|
1183 | - } |
|
1184 | - |
|
1185 | - |
|
1186 | - /** |
|
1187 | - * @deprecated instead use ModelDataTranslator::prepareFieldValuesFromJson() |
|
1188 | - * @param $model |
|
1189 | - * @param $query_params |
|
1190 | - * @return array |
|
1191 | - */ |
|
1192 | - public function prepareRestQueryParamsValuesForModels($model, $query_params) |
|
1193 | - { |
|
1194 | - $model_ready_query_params = array(); |
|
1195 | - foreach ($query_params as $key => $value) { |
|
1196 | - if (is_array($value)) { |
|
1197 | - $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsValuesForModels($model, $value); |
|
1198 | - } else { |
|
1199 | - $model_ready_query_params[ $key ] = $value; |
|
1200 | - } |
|
1201 | - } |
|
1202 | - return $model_ready_query_params; |
|
1203 | - } |
|
1204 | - |
|
1205 | - |
|
1206 | - /** |
|
1207 | - * Explodes the string on commas, and only returns items with $prefix followed by a period. |
|
1208 | - * If no prefix is specified, returns items with no period. |
|
1209 | - * |
|
1210 | - * @param string|array $string_to_explode eg "jibba,jabba, blah, blah, blah" or array('jibba', 'jabba' ) |
|
1211 | - * @param string $prefix "Event" or "foobar" |
|
1212 | - * @return array $string_to_exploded exploded on COMMAS, and if a prefix was specified |
|
1213 | - * we only return strings starting with that and a period; if no prefix was |
|
1214 | - * specified we return all items containing NO periods |
|
1215 | - */ |
|
1216 | - public function explodeAndGetItemsPrefixedWith($string_to_explode, $prefix) |
|
1217 | - { |
|
1218 | - if (is_string($string_to_explode)) { |
|
1219 | - $exploded_contents = explode(',', $string_to_explode); |
|
1220 | - } elseif (is_array($string_to_explode)) { |
|
1221 | - $exploded_contents = $string_to_explode; |
|
1222 | - } else { |
|
1223 | - $exploded_contents = array(); |
|
1224 | - } |
|
1225 | - // if the string was empty, we want an empty array |
|
1226 | - $exploded_contents = array_filter($exploded_contents); |
|
1227 | - $contents_with_prefix = array(); |
|
1228 | - foreach ($exploded_contents as $item) { |
|
1229 | - $item = trim($item); |
|
1230 | - // if no prefix was provided, so we look for items with no "." in them |
|
1231 | - if (! $prefix) { |
|
1232 | - // does this item have a period? |
|
1233 | - if (strpos($item, '.') === false) { |
|
1234 | - // if not, then its what we're looking for |
|
1235 | - $contents_with_prefix[] = $item; |
|
1236 | - } |
|
1237 | - } elseif (strpos($item, $prefix . '.') === 0) { |
|
1238 | - // this item has the prefix and a period, grab it |
|
1239 | - $contents_with_prefix[] = substr( |
|
1240 | - $item, |
|
1241 | - strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1242 | - ); |
|
1243 | - } elseif ($item === $prefix) { |
|
1244 | - // this item is JUST the prefix |
|
1245 | - // so let's grab everything after, which is a blank string |
|
1246 | - $contents_with_prefix[] = ''; |
|
1247 | - } |
|
1248 | - } |
|
1249 | - return $contents_with_prefix; |
|
1250 | - } |
|
1251 | - |
|
1252 | - |
|
1253 | - /** |
|
1254 | - * @deprecated since 4.8.36.rc.001 You should instead use Read::explode_and_get_items_prefixed_with. |
|
1255 | - * Deprecated because its return values were really quite confusing- sometimes it returned |
|
1256 | - * an empty array (when the include string was blank or '*') or sometimes it returned |
|
1257 | - * array('*') (when you provided a model and a model of that kind was found). |
|
1258 | - * Parses the $include_string so we fetch all the field names relating to THIS model |
|
1259 | - * (ie have NO period in them), or for the provided model (ie start with the model |
|
1260 | - * name and then a period). |
|
1261 | - * @param string $include_string @see Read:handle_request_get_all |
|
1262 | - * @param string $model_name |
|
1263 | - * @return array of fields for this model. If $model_name is provided, then |
|
1264 | - * the fields for that model, with the model's name removed from each. |
|
1265 | - * If $include_string was blank or '*' returns an empty array |
|
1266 | - */ |
|
1267 | - public function extractIncludesForThisModel($include_string, $model_name = null) |
|
1268 | - { |
|
1269 | - if (is_array($include_string)) { |
|
1270 | - $include_string = implode(',', $include_string); |
|
1271 | - } |
|
1272 | - if ($include_string === '*' || $include_string === '') { |
|
1273 | - return array(); |
|
1274 | - } |
|
1275 | - $includes = explode(',', $include_string); |
|
1276 | - $extracted_fields_to_include = array(); |
|
1277 | - if ($model_name) { |
|
1278 | - foreach ($includes as $field_to_include) { |
|
1279 | - $field_to_include = trim($field_to_include); |
|
1280 | - if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1281 | - // found the model name at the exact start |
|
1282 | - $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1283 | - $extracted_fields_to_include[] = $field_sans_model_name; |
|
1284 | - } elseif ($field_to_include == $model_name) { |
|
1285 | - $extracted_fields_to_include[] = '*'; |
|
1286 | - } |
|
1287 | - } |
|
1288 | - } else { |
|
1289 | - // look for ones with no period |
|
1290 | - foreach ($includes as $field_to_include) { |
|
1291 | - $field_to_include = trim($field_to_include); |
|
1292 | - if (strpos($field_to_include, '.') === false |
|
1293 | - && ! $this->getModelVersionInfo()->isModelNameInThisVersion($field_to_include) |
|
1294 | - ) { |
|
1295 | - $extracted_fields_to_include[] = $field_to_include; |
|
1296 | - } |
|
1297 | - } |
|
1298 | - } |
|
1299 | - return $extracted_fields_to_include; |
|
1300 | - } |
|
1301 | - |
|
1302 | - |
|
1303 | - /** |
|
1304 | - * Gets the single item using the model according to the request in the context given, otherwise |
|
1305 | - * returns that it's inaccessible to the current user |
|
1306 | - * |
|
1307 | - * @param EEM_Base $model |
|
1308 | - * @param WP_REST_Request $request |
|
1309 | - * @param null $context |
|
1310 | - * @return array|WP_Error |
|
1311 | - */ |
|
1312 | - public function getOneOrReportPermissionError(EEM_Base $model, WP_REST_Request $request, $context = null) |
|
1313 | - { |
|
1314 | - $query_params = array(array($model->primary_key_name() => $request->get_param('id')), 'limit' => 1); |
|
1315 | - if ($model instanceof \EEM_Soft_Delete_Base) { |
|
1316 | - $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
1317 | - } |
|
1318 | - $restricted_query_params = $query_params; |
|
1319 | - $restricted_query_params['caps'] = $context; |
|
1320 | - $this->setDebugInfo('model query params', $restricted_query_params); |
|
1321 | - $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
|
1322 | - if (! empty($model_rows)) { |
|
1323 | - return $this->createEntityFromWpdbResult( |
|
1324 | - $model, |
|
1325 | - array_shift($model_rows), |
|
1326 | - $request |
|
1327 | - ); |
|
1328 | - } else { |
|
1329 | - // ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
|
1330 | - $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
1331 | - $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
|
1332 | - if (! empty($model_rows_found_sans_restrictions)) { |
|
1333 | - // you got shafted- it existed but we didn't want to tell you! |
|
1334 | - return new WP_Error( |
|
1335 | - 'rest_user_cannot_' . $context, |
|
1336 | - sprintf( |
|
1337 | - __('Sorry, you cannot %1$s this %2$s. Missing permissions are: %3$s', 'event_espresso'), |
|
1338 | - $context, |
|
1339 | - strtolower($model->get_this_model_name()), |
|
1340 | - Capabilities::getMissingPermissionsString( |
|
1341 | - $model, |
|
1342 | - $context |
|
1343 | - ) |
|
1344 | - ), |
|
1345 | - array('status' => 403) |
|
1346 | - ); |
|
1347 | - } else { |
|
1348 | - // it's not you. It just doesn't exist |
|
1349 | - return new WP_Error( |
|
1350 | - sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
1351 | - sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
1352 | - array('status' => 404) |
|
1353 | - ); |
|
1354 | - } |
|
1355 | - } |
|
1356 | - } |
|
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 | + $timezone = $field_value->getTimezone(); |
|
692 | + EEH_DTT_Helper::setTimezone($field_value, new DateTimeZone('UTC')); |
|
693 | + $result[ $field_name . '_gmt' ] = ModelDataTranslator::prepareFieldValuesForJson( |
|
694 | + $field_obj, |
|
695 | + $field_value, |
|
696 | + $this->getModelVersionInfo()->requestedVersion() |
|
697 | + ); |
|
698 | + EEH_DTT_Helper::setTimezone($field_value, $timezone); |
|
699 | + $result[ $field_name ] = ModelDataTranslator::prepareFieldValuesForJson( |
|
700 | + $field_obj, |
|
701 | + $field_value, |
|
702 | + $this->getModelVersionInfo()->requestedVersion() |
|
703 | + ); |
|
704 | + } else { |
|
705 | + $result[ $field_name ] = $this->prepareFieldObjValueForJson($field_obj, $field_value); |
|
706 | + } |
|
707 | + } |
|
708 | + if ($do_chevy_shuffle) { |
|
709 | + $post = $old_post; |
|
710 | + } |
|
711 | + return $result; |
|
712 | + } |
|
713 | + |
|
714 | + |
|
715 | + /** |
|
716 | + * Takes a value all the way from the DB representation, to the model object's representation, to the |
|
717 | + * user-facing PHP representation, to the REST API representation. (Assumes you've already taken from the DB |
|
718 | + * representation using $field_obj->prepare_for_set_from_db()) |
|
719 | + * |
|
720 | + * @param EE_Model_Field_Base $field_obj |
|
721 | + * @param mixed $value as it's stored on a model object |
|
722 | + * @param string $format valid values are 'normal' (default), 'pretty', 'datetime_obj' |
|
723 | + * @return mixed |
|
724 | + * @throws ObjectDetectedException if $value contains a PHP object |
|
725 | + */ |
|
726 | + protected function prepareFieldObjValueForJson(EE_Model_Field_Base $field_obj, $value, $format = 'normal') |
|
727 | + { |
|
728 | + $value = $field_obj->prepare_for_set_from_db($value); |
|
729 | + switch ($format) { |
|
730 | + case 'pretty': |
|
731 | + $value = $field_obj->prepare_for_pretty_echoing($value); |
|
732 | + break; |
|
733 | + case 'normal': |
|
734 | + default: |
|
735 | + $value = $field_obj->prepare_for_get($value); |
|
736 | + break; |
|
737 | + } |
|
738 | + return ModelDataTranslator::prepareFieldValuesForJson( |
|
739 | + $field_obj, |
|
740 | + $value, |
|
741 | + $this->getModelVersionInfo()->requestedVersion() |
|
742 | + ); |
|
743 | + } |
|
744 | + |
|
745 | + |
|
746 | + /** |
|
747 | + * Adds a few extra fields to the entity response |
|
748 | + * |
|
749 | + * @param EEM_Base $model |
|
750 | + * @param array $db_row |
|
751 | + * @param array $entity_array |
|
752 | + * @return array modified entity |
|
753 | + */ |
|
754 | + protected function addExtraFields(EEM_Base $model, $db_row, $entity_array) |
|
755 | + { |
|
756 | + if ($model instanceof EEM_CPT_Base) { |
|
757 | + $entity_array['link'] = get_permalink($db_row[ $model->get_primary_key_field()->get_qualified_column() ]); |
|
758 | + } |
|
759 | + return $entity_array; |
|
760 | + } |
|
761 | + |
|
762 | + |
|
763 | + /** |
|
764 | + * Gets links we want to add to the response |
|
765 | + * |
|
766 | + * @global \WP_REST_Server $wp_rest_server |
|
767 | + * @param EEM_Base $model |
|
768 | + * @param array $db_row |
|
769 | + * @param array $entity_array |
|
770 | + * @return array the _links item in the entity |
|
771 | + */ |
|
772 | + protected function getEntityLinks($model, $db_row, $entity_array) |
|
773 | + { |
|
774 | + // add basic links |
|
775 | + $links = array(); |
|
776 | + if ($model->has_primary_key_field()) { |
|
777 | + $links['self'] = array( |
|
778 | + array( |
|
779 | + 'href' => $this->getVersionedLinkTo( |
|
780 | + EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
781 | + . '/' |
|
782 | + . $entity_array[ $model->primary_key_name() ] |
|
783 | + ), |
|
784 | + ), |
|
785 | + ); |
|
786 | + } |
|
787 | + $links['collection'] = array( |
|
788 | + array( |
|
789 | + 'href' => $this->getVersionedLinkTo( |
|
790 | + EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
791 | + ), |
|
792 | + ), |
|
793 | + ); |
|
794 | + // add links to related models |
|
795 | + if ($model->has_primary_key_field()) { |
|
796 | + foreach ($this->getModelVersionInfo()->relationSettings($model) as $relation_name => $relation_obj) { |
|
797 | + $related_model_part = Read::getRelatedEntityName($relation_name, $relation_obj); |
|
798 | + $links[ EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part ] = array( |
|
799 | + array( |
|
800 | + 'href' => $this->getVersionedLinkTo( |
|
801 | + EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
802 | + . '/' |
|
803 | + . $entity_array[ $model->primary_key_name() ] |
|
804 | + . '/' |
|
805 | + . $related_model_part |
|
806 | + ), |
|
807 | + 'single' => $relation_obj instanceof EE_Belongs_To_Relation ? true : false, |
|
808 | + ), |
|
809 | + ); |
|
810 | + } |
|
811 | + } |
|
812 | + return $links; |
|
813 | + } |
|
814 | + |
|
815 | + |
|
816 | + /** |
|
817 | + * Adds the included models indicated in the request to the entity provided |
|
818 | + * |
|
819 | + * @param EEM_Base $model |
|
820 | + * @param WP_REST_Request $rest_request |
|
821 | + * @param array $entity_array |
|
822 | + * @param array $db_row |
|
823 | + * @return array the modified entity |
|
824 | + */ |
|
825 | + protected function includeRequestedModels( |
|
826 | + EEM_Base $model, |
|
827 | + WP_REST_Request $rest_request, |
|
828 | + $entity_array, |
|
829 | + $db_row = array() |
|
830 | + ) { |
|
831 | + // if $db_row not included, hope the entity array has what we need |
|
832 | + if (! $db_row) { |
|
833 | + $db_row = $entity_array; |
|
834 | + } |
|
835 | + $includes_for_this_model = $this->explodeAndGetItemsPrefixedWith($rest_request->get_param('include'), ''); |
|
836 | + $includes_for_this_model = $this->removeModelNamesFromArray($includes_for_this_model); |
|
837 | + // if they passed in * or didn't specify any includes, return everything |
|
838 | + if (! in_array('*', $includes_for_this_model) |
|
839 | + && ! empty($includes_for_this_model) |
|
840 | + ) { |
|
841 | + if ($model->has_primary_key_field()) { |
|
842 | + // always include the primary key. ya just gotta know that at least |
|
843 | + $includes_for_this_model[] = $model->primary_key_name(); |
|
844 | + } |
|
845 | + if ($this->explodeAndGetItemsPrefixedWith($rest_request->get_param('calculate'), '')) { |
|
846 | + $includes_for_this_model[] = '_calculated_fields'; |
|
847 | + } |
|
848 | + $entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model)); |
|
849 | + } |
|
850 | + $relation_settings = $this->getModelVersionInfo()->relationSettings($model); |
|
851 | + foreach ($relation_settings as $relation_name => $relation_obj) { |
|
852 | + $related_fields_to_include = $this->explodeAndGetItemsPrefixedWith( |
|
853 | + $rest_request->get_param('include'), |
|
854 | + $relation_name |
|
855 | + ); |
|
856 | + $related_fields_to_calculate = $this->explodeAndGetItemsPrefixedWith( |
|
857 | + $rest_request->get_param('calculate'), |
|
858 | + $relation_name |
|
859 | + ); |
|
860 | + // did they specify they wanted to include a related model, or |
|
861 | + // specific fields from a related model? |
|
862 | + // or did they specify to calculate a field from a related model? |
|
863 | + if ($related_fields_to_include || $related_fields_to_calculate) { |
|
864 | + // if so, we should include at least some part of the related model |
|
865 | + $pretend_related_request = new WP_REST_Request(); |
|
866 | + $pretend_related_request->set_query_params( |
|
867 | + array( |
|
868 | + 'caps' => $rest_request->get_param('caps'), |
|
869 | + 'include' => $related_fields_to_include, |
|
870 | + 'calculate' => $related_fields_to_calculate, |
|
871 | + ) |
|
872 | + ); |
|
873 | + $pretend_related_request->add_header('no_rest_headers', true); |
|
874 | + $primary_model_query_params = $model->alter_query_params_to_restrict_by_ID( |
|
875 | + $model->get_index_primary_key_string( |
|
876 | + $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
877 | + ) |
|
878 | + ); |
|
879 | + $related_results = $this->getEntitiesFromRelationUsingModelQueryParams( |
|
880 | + $primary_model_query_params, |
|
881 | + $relation_obj, |
|
882 | + $pretend_related_request |
|
883 | + ); |
|
884 | + $entity_array[ Read::getRelatedEntityName($relation_name, $relation_obj) ] = $related_results |
|
885 | + instanceof |
|
886 | + WP_Error |
|
887 | + ? null |
|
888 | + : $related_results; |
|
889 | + } |
|
890 | + } |
|
891 | + return $entity_array; |
|
892 | + } |
|
893 | + |
|
894 | + |
|
895 | + /** |
|
896 | + * Returns a new array with all the names of models removed. Eg |
|
897 | + * array( 'Event', 'Datetime.*', 'foobar' ) would become array( 'Datetime.*', 'foobar' ) |
|
898 | + * |
|
899 | + * @param array $arr |
|
900 | + * @return array |
|
901 | + */ |
|
902 | + private function removeModelNamesFromArray($arr) |
|
903 | + { |
|
904 | + return array_diff($arr, array_keys(EE_Registry::instance()->non_abstract_db_models)); |
|
905 | + } |
|
906 | + |
|
907 | + |
|
908 | + /** |
|
909 | + * Gets the calculated fields for the response |
|
910 | + * |
|
911 | + * @param EEM_Base $model |
|
912 | + * @param array $wpdb_row |
|
913 | + * @param WP_REST_Request $rest_request |
|
914 | + * @return \stdClass the _calculations item in the entity |
|
915 | + * @throws ObjectDetectedException if a default value has a PHP object, which should never do (and if we |
|
916 | + * did, let's know about it ASAP, so let the exception bubble up) |
|
917 | + */ |
|
918 | + protected function getEntityCalculations($model, $wpdb_row, $rest_request) |
|
919 | + { |
|
920 | + $calculated_fields = $this->explodeAndGetItemsPrefixedWith( |
|
921 | + $rest_request->get_param('calculate'), |
|
922 | + '' |
|
923 | + ); |
|
924 | + // note: setting calculate=* doesn't do anything |
|
925 | + $calculated_fields_to_return = new \stdClass(); |
|
926 | + foreach ($calculated_fields as $field_to_calculate) { |
|
927 | + try { |
|
928 | + $calculated_fields_to_return->$field_to_calculate = ModelDataTranslator::prepareFieldValueForJson( |
|
929 | + null, |
|
930 | + $this->fields_calculator->retrieveCalculatedFieldValue( |
|
931 | + $model, |
|
932 | + $field_to_calculate, |
|
933 | + $wpdb_row, |
|
934 | + $rest_request, |
|
935 | + $this |
|
936 | + ), |
|
937 | + $this->getModelVersionInfo()->requestedVersion() |
|
938 | + ); |
|
939 | + } catch (RestException $e) { |
|
940 | + // if we don't have permission to read it, just leave it out. but let devs know about the problem |
|
941 | + $this->setResponseHeader( |
|
942 | + 'Notices-Field-Calculation-Errors[' |
|
943 | + . $e->getStringCode() |
|
944 | + . '][' |
|
945 | + . $model->get_this_model_name() |
|
946 | + . '][' |
|
947 | + . $field_to_calculate |
|
948 | + . ']', |
|
949 | + $e->getMessage(), |
|
950 | + true |
|
951 | + ); |
|
952 | + } |
|
953 | + } |
|
954 | + return $calculated_fields_to_return; |
|
955 | + } |
|
956 | + |
|
957 | + |
|
958 | + /** |
|
959 | + * Gets the full URL to the resource, taking the requested version into account |
|
960 | + * |
|
961 | + * @param string $link_part_after_version_and_slash eg "events/10/datetimes" |
|
962 | + * @return string url eg "http://mysite.com/wp-json/ee/v4.6/events/10/datetimes" |
|
963 | + */ |
|
964 | + public function getVersionedLinkTo($link_part_after_version_and_slash) |
|
965 | + { |
|
966 | + return rest_url( |
|
967 | + EED_Core_Rest_Api::get_versioned_route_to( |
|
968 | + $link_part_after_version_and_slash, |
|
969 | + $this->getModelVersionInfo()->requestedVersion() |
|
970 | + ) |
|
971 | + ); |
|
972 | + } |
|
973 | + |
|
974 | + |
|
975 | + /** |
|
976 | + * Gets the correct lowercase name for the relation in the API according |
|
977 | + * to the relation's type |
|
978 | + * |
|
979 | + * @param string $relation_name |
|
980 | + * @param \EE_Model_Relation_Base $relation_obj |
|
981 | + * @return string |
|
982 | + */ |
|
983 | + public static function getRelatedEntityName($relation_name, $relation_obj) |
|
984 | + { |
|
985 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
986 | + return strtolower($relation_name); |
|
987 | + } else { |
|
988 | + return EEH_Inflector::pluralize_and_lower($relation_name); |
|
989 | + } |
|
990 | + } |
|
991 | + |
|
992 | + |
|
993 | + /** |
|
994 | + * Gets the one model object with the specified id for the specified model |
|
995 | + * |
|
996 | + * @param EEM_Base $model |
|
997 | + * @param WP_REST_Request $request |
|
998 | + * @return array|WP_Error |
|
999 | + */ |
|
1000 | + public function getEntityFromModel($model, $request) |
|
1001 | + { |
|
1002 | + $context = $this->validateContext($request->get_param('caps')); |
|
1003 | + return $this->getOneOrReportPermissionError($model, $request, $context); |
|
1004 | + } |
|
1005 | + |
|
1006 | + |
|
1007 | + /** |
|
1008 | + * If a context is provided which isn't valid, maybe it was added in a future |
|
1009 | + * version so just treat it as a default read |
|
1010 | + * |
|
1011 | + * @param string $context |
|
1012 | + * @return string array key of EEM_Base::cap_contexts_to_cap_action_map() |
|
1013 | + */ |
|
1014 | + public function validateContext($context) |
|
1015 | + { |
|
1016 | + if (! $context) { |
|
1017 | + $context = EEM_Base::caps_read; |
|
1018 | + } |
|
1019 | + $valid_contexts = EEM_Base::valid_cap_contexts(); |
|
1020 | + if (in_array($context, $valid_contexts)) { |
|
1021 | + return $context; |
|
1022 | + } else { |
|
1023 | + return EEM_Base::caps_read; |
|
1024 | + } |
|
1025 | + } |
|
1026 | + |
|
1027 | + |
|
1028 | + /** |
|
1029 | + * Verifies the passed in value is an allowable default where conditions value. |
|
1030 | + * |
|
1031 | + * @param $default_query_params |
|
1032 | + * @return string |
|
1033 | + */ |
|
1034 | + public function validateDefaultQueryParams($default_query_params) |
|
1035 | + { |
|
1036 | + $valid_default_where_conditions_for_api_calls = array( |
|
1037 | + EEM_Base::default_where_conditions_all, |
|
1038 | + EEM_Base::default_where_conditions_minimum_all, |
|
1039 | + EEM_Base::default_where_conditions_minimum_others, |
|
1040 | + ); |
|
1041 | + if (! $default_query_params) { |
|
1042 | + $default_query_params = EEM_Base::default_where_conditions_all; |
|
1043 | + } |
|
1044 | + if (in_array( |
|
1045 | + $default_query_params, |
|
1046 | + $valid_default_where_conditions_for_api_calls, |
|
1047 | + true |
|
1048 | + )) { |
|
1049 | + return $default_query_params; |
|
1050 | + } else { |
|
1051 | + return EEM_Base::default_where_conditions_all; |
|
1052 | + } |
|
1053 | + } |
|
1054 | + |
|
1055 | + |
|
1056 | + /** |
|
1057 | + * Translates API filter get parameter into $query_params array used by EEM_Base::get_all(). |
|
1058 | + * Note: right now the query parameter keys for fields (and related fields) |
|
1059 | + * can be left as-is, but it's quite possible this will change someday. |
|
1060 | + * Also, this method's contents might be candidate for moving to Model_Data_Translator |
|
1061 | + * |
|
1062 | + * @param EEM_Base $model |
|
1063 | + * @param array $query_parameters from $_GET parameter @see Read:handle_request_get_all |
|
1064 | + * @return array like what EEM_Base::get_all() expects or FALSE to indicate |
|
1065 | + * that absolutely no results should be returned |
|
1066 | + * @throws EE_Error |
|
1067 | + * @throws RestException |
|
1068 | + */ |
|
1069 | + public function createModelQueryParams($model, $query_parameters) |
|
1070 | + { |
|
1071 | + $model_query_params = array(); |
|
1072 | + if (isset($query_parameters['where'])) { |
|
1073 | + $model_query_params[0] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
1074 | + $query_parameters['where'], |
|
1075 | + $model, |
|
1076 | + $this->getModelVersionInfo()->requestedVersion() |
|
1077 | + ); |
|
1078 | + } |
|
1079 | + if (isset($query_parameters['order_by'])) { |
|
1080 | + $order_by = $query_parameters['order_by']; |
|
1081 | + } elseif (isset($query_parameters['orderby'])) { |
|
1082 | + $order_by = $query_parameters['orderby']; |
|
1083 | + } else { |
|
1084 | + $order_by = null; |
|
1085 | + } |
|
1086 | + if ($order_by !== null) { |
|
1087 | + if (is_array($order_by)) { |
|
1088 | + $order_by = ModelDataTranslator::prepareFieldNamesInArrayKeysFromJson($order_by); |
|
1089 | + } else { |
|
1090 | + // it's a single item |
|
1091 | + $order_by = ModelDataTranslator::prepareFieldNameFromJson($order_by); |
|
1092 | + } |
|
1093 | + $model_query_params['order_by'] = $order_by; |
|
1094 | + } |
|
1095 | + if (isset($query_parameters['group_by'])) { |
|
1096 | + $group_by = $query_parameters['group_by']; |
|
1097 | + } elseif (isset($query_parameters['groupby'])) { |
|
1098 | + $group_by = $query_parameters['groupby']; |
|
1099 | + } else { |
|
1100 | + $group_by = array_keys($model->get_combined_primary_key_fields()); |
|
1101 | + } |
|
1102 | + // make sure they're all real names |
|
1103 | + if (is_array($group_by)) { |
|
1104 | + $group_by = ModelDataTranslator::prepareFieldNamesFromJson($group_by); |
|
1105 | + } |
|
1106 | + if ($group_by !== null) { |
|
1107 | + $model_query_params['group_by'] = $group_by; |
|
1108 | + } |
|
1109 | + if (isset($query_parameters['having'])) { |
|
1110 | + $model_query_params['having'] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
1111 | + $query_parameters['having'], |
|
1112 | + $model, |
|
1113 | + $this->getModelVersionInfo()->requestedVersion() |
|
1114 | + ); |
|
1115 | + } |
|
1116 | + if (isset($query_parameters['order'])) { |
|
1117 | + $model_query_params['order'] = $query_parameters['order']; |
|
1118 | + } |
|
1119 | + if (isset($query_parameters['mine'])) { |
|
1120 | + $model_query_params = $model->alter_query_params_to_only_include_mine($model_query_params); |
|
1121 | + } |
|
1122 | + if (isset($query_parameters['limit'])) { |
|
1123 | + // limit should be either a string like '23' or '23,43', or an array with two items in it |
|
1124 | + if (! is_array($query_parameters['limit'])) { |
|
1125 | + $limit_array = explode(',', (string) $query_parameters['limit']); |
|
1126 | + } else { |
|
1127 | + $limit_array = $query_parameters['limit']; |
|
1128 | + } |
|
1129 | + $sanitized_limit = array(); |
|
1130 | + foreach ($limit_array as $key => $limit_part) { |
|
1131 | + if ($this->debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1132 | + throw new EE_Error( |
|
1133 | + sprintf( |
|
1134 | + __( |
|
1135 | + // @codingStandardsIgnoreStart |
|
1136 | + '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.', |
|
1137 | + // @codingStandardsIgnoreEnd |
|
1138 | + 'event_espresso' |
|
1139 | + ), |
|
1140 | + wp_json_encode($query_parameters['limit']) |
|
1141 | + ) |
|
1142 | + ); |
|
1143 | + } |
|
1144 | + $sanitized_limit[] = (int) $limit_part; |
|
1145 | + } |
|
1146 | + $model_query_params['limit'] = implode(',', $sanitized_limit); |
|
1147 | + } else { |
|
1148 | + $model_query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
|
1149 | + } |
|
1150 | + if (isset($query_parameters['caps'])) { |
|
1151 | + $model_query_params['caps'] = $this->validateContext($query_parameters['caps']); |
|
1152 | + } else { |
|
1153 | + $model_query_params['caps'] = EEM_Base::caps_read; |
|
1154 | + } |
|
1155 | + if (isset($query_parameters['default_where_conditions'])) { |
|
1156 | + $model_query_params['default_where_conditions'] = $this->validateDefaultQueryParams( |
|
1157 | + $query_parameters['default_where_conditions'] |
|
1158 | + ); |
|
1159 | + } |
|
1160 | + return apply_filters('FHEE__Read__create_model_query_params', $model_query_params, $query_parameters, $model); |
|
1161 | + } |
|
1162 | + |
|
1163 | + |
|
1164 | + /** |
|
1165 | + * Changes the REST-style query params for use in the models |
|
1166 | + * |
|
1167 | + * @deprecated |
|
1168 | + * @param EEM_Base $model |
|
1169 | + * @param array $query_params sub-array from @see EEM_Base::get_all() |
|
1170 | + * @return array |
|
1171 | + */ |
|
1172 | + public function prepareRestQueryParamsKeyForModels($model, $query_params) |
|
1173 | + { |
|
1174 | + $model_ready_query_params = array(); |
|
1175 | + foreach ($query_params as $key => $value) { |
|
1176 | + if (is_array($value)) { |
|
1177 | + $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsKeyForModels($model, $value); |
|
1178 | + } else { |
|
1179 | + $model_ready_query_params[ $key ] = $value; |
|
1180 | + } |
|
1181 | + } |
|
1182 | + return $model_ready_query_params; |
|
1183 | + } |
|
1184 | + |
|
1185 | + |
|
1186 | + /** |
|
1187 | + * @deprecated instead use ModelDataTranslator::prepareFieldValuesFromJson() |
|
1188 | + * @param $model |
|
1189 | + * @param $query_params |
|
1190 | + * @return array |
|
1191 | + */ |
|
1192 | + public function prepareRestQueryParamsValuesForModels($model, $query_params) |
|
1193 | + { |
|
1194 | + $model_ready_query_params = array(); |
|
1195 | + foreach ($query_params as $key => $value) { |
|
1196 | + if (is_array($value)) { |
|
1197 | + $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsValuesForModels($model, $value); |
|
1198 | + } else { |
|
1199 | + $model_ready_query_params[ $key ] = $value; |
|
1200 | + } |
|
1201 | + } |
|
1202 | + return $model_ready_query_params; |
|
1203 | + } |
|
1204 | + |
|
1205 | + |
|
1206 | + /** |
|
1207 | + * Explodes the string on commas, and only returns items with $prefix followed by a period. |
|
1208 | + * If no prefix is specified, returns items with no period. |
|
1209 | + * |
|
1210 | + * @param string|array $string_to_explode eg "jibba,jabba, blah, blah, blah" or array('jibba', 'jabba' ) |
|
1211 | + * @param string $prefix "Event" or "foobar" |
|
1212 | + * @return array $string_to_exploded exploded on COMMAS, and if a prefix was specified |
|
1213 | + * we only return strings starting with that and a period; if no prefix was |
|
1214 | + * specified we return all items containing NO periods |
|
1215 | + */ |
|
1216 | + public function explodeAndGetItemsPrefixedWith($string_to_explode, $prefix) |
|
1217 | + { |
|
1218 | + if (is_string($string_to_explode)) { |
|
1219 | + $exploded_contents = explode(',', $string_to_explode); |
|
1220 | + } elseif (is_array($string_to_explode)) { |
|
1221 | + $exploded_contents = $string_to_explode; |
|
1222 | + } else { |
|
1223 | + $exploded_contents = array(); |
|
1224 | + } |
|
1225 | + // if the string was empty, we want an empty array |
|
1226 | + $exploded_contents = array_filter($exploded_contents); |
|
1227 | + $contents_with_prefix = array(); |
|
1228 | + foreach ($exploded_contents as $item) { |
|
1229 | + $item = trim($item); |
|
1230 | + // if no prefix was provided, so we look for items with no "." in them |
|
1231 | + if (! $prefix) { |
|
1232 | + // does this item have a period? |
|
1233 | + if (strpos($item, '.') === false) { |
|
1234 | + // if not, then its what we're looking for |
|
1235 | + $contents_with_prefix[] = $item; |
|
1236 | + } |
|
1237 | + } elseif (strpos($item, $prefix . '.') === 0) { |
|
1238 | + // this item has the prefix and a period, grab it |
|
1239 | + $contents_with_prefix[] = substr( |
|
1240 | + $item, |
|
1241 | + strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1242 | + ); |
|
1243 | + } elseif ($item === $prefix) { |
|
1244 | + // this item is JUST the prefix |
|
1245 | + // so let's grab everything after, which is a blank string |
|
1246 | + $contents_with_prefix[] = ''; |
|
1247 | + } |
|
1248 | + } |
|
1249 | + return $contents_with_prefix; |
|
1250 | + } |
|
1251 | + |
|
1252 | + |
|
1253 | + /** |
|
1254 | + * @deprecated since 4.8.36.rc.001 You should instead use Read::explode_and_get_items_prefixed_with. |
|
1255 | + * Deprecated because its return values were really quite confusing- sometimes it returned |
|
1256 | + * an empty array (when the include string was blank or '*') or sometimes it returned |
|
1257 | + * array('*') (when you provided a model and a model of that kind was found). |
|
1258 | + * Parses the $include_string so we fetch all the field names relating to THIS model |
|
1259 | + * (ie have NO period in them), or for the provided model (ie start with the model |
|
1260 | + * name and then a period). |
|
1261 | + * @param string $include_string @see Read:handle_request_get_all |
|
1262 | + * @param string $model_name |
|
1263 | + * @return array of fields for this model. If $model_name is provided, then |
|
1264 | + * the fields for that model, with the model's name removed from each. |
|
1265 | + * If $include_string was blank or '*' returns an empty array |
|
1266 | + */ |
|
1267 | + public function extractIncludesForThisModel($include_string, $model_name = null) |
|
1268 | + { |
|
1269 | + if (is_array($include_string)) { |
|
1270 | + $include_string = implode(',', $include_string); |
|
1271 | + } |
|
1272 | + if ($include_string === '*' || $include_string === '') { |
|
1273 | + return array(); |
|
1274 | + } |
|
1275 | + $includes = explode(',', $include_string); |
|
1276 | + $extracted_fields_to_include = array(); |
|
1277 | + if ($model_name) { |
|
1278 | + foreach ($includes as $field_to_include) { |
|
1279 | + $field_to_include = trim($field_to_include); |
|
1280 | + if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1281 | + // found the model name at the exact start |
|
1282 | + $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1283 | + $extracted_fields_to_include[] = $field_sans_model_name; |
|
1284 | + } elseif ($field_to_include == $model_name) { |
|
1285 | + $extracted_fields_to_include[] = '*'; |
|
1286 | + } |
|
1287 | + } |
|
1288 | + } else { |
|
1289 | + // look for ones with no period |
|
1290 | + foreach ($includes as $field_to_include) { |
|
1291 | + $field_to_include = trim($field_to_include); |
|
1292 | + if (strpos($field_to_include, '.') === false |
|
1293 | + && ! $this->getModelVersionInfo()->isModelNameInThisVersion($field_to_include) |
|
1294 | + ) { |
|
1295 | + $extracted_fields_to_include[] = $field_to_include; |
|
1296 | + } |
|
1297 | + } |
|
1298 | + } |
|
1299 | + return $extracted_fields_to_include; |
|
1300 | + } |
|
1301 | + |
|
1302 | + |
|
1303 | + /** |
|
1304 | + * Gets the single item using the model according to the request in the context given, otherwise |
|
1305 | + * returns that it's inaccessible to the current user |
|
1306 | + * |
|
1307 | + * @param EEM_Base $model |
|
1308 | + * @param WP_REST_Request $request |
|
1309 | + * @param null $context |
|
1310 | + * @return array|WP_Error |
|
1311 | + */ |
|
1312 | + public function getOneOrReportPermissionError(EEM_Base $model, WP_REST_Request $request, $context = null) |
|
1313 | + { |
|
1314 | + $query_params = array(array($model->primary_key_name() => $request->get_param('id')), 'limit' => 1); |
|
1315 | + if ($model instanceof \EEM_Soft_Delete_Base) { |
|
1316 | + $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
1317 | + } |
|
1318 | + $restricted_query_params = $query_params; |
|
1319 | + $restricted_query_params['caps'] = $context; |
|
1320 | + $this->setDebugInfo('model query params', $restricted_query_params); |
|
1321 | + $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
|
1322 | + if (! empty($model_rows)) { |
|
1323 | + return $this->createEntityFromWpdbResult( |
|
1324 | + $model, |
|
1325 | + array_shift($model_rows), |
|
1326 | + $request |
|
1327 | + ); |
|
1328 | + } else { |
|
1329 | + // ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
|
1330 | + $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
1331 | + $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
|
1332 | + if (! empty($model_rows_found_sans_restrictions)) { |
|
1333 | + // you got shafted- it existed but we didn't want to tell you! |
|
1334 | + return new WP_Error( |
|
1335 | + 'rest_user_cannot_' . $context, |
|
1336 | + sprintf( |
|
1337 | + __('Sorry, you cannot %1$s this %2$s. Missing permissions are: %3$s', 'event_espresso'), |
|
1338 | + $context, |
|
1339 | + strtolower($model->get_this_model_name()), |
|
1340 | + Capabilities::getMissingPermissionsString( |
|
1341 | + $model, |
|
1342 | + $context |
|
1343 | + ) |
|
1344 | + ), |
|
1345 | + array('status' => 403) |
|
1346 | + ); |
|
1347 | + } else { |
|
1348 | + // it's not you. It just doesn't exist |
|
1349 | + return new WP_Error( |
|
1350 | + sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
1351 | + sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
1352 | + array('status' => 404) |
|
1353 | + ); |
|
1354 | + } |
|
1355 | + } |
|
1356 | + } |
|
1357 | 1357 | } |
@@ -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 | ); |
@@ -690,19 +690,19 @@ discard block |
||
690 | 690 | $field_value = $field_obj->prepare_for_set_from_db($field_value); |
691 | 691 | $timezone = $field_value->getTimezone(); |
692 | 692 | EEH_DTT_Helper::setTimezone($field_value, new DateTimeZone('UTC')); |
693 | - $result[ $field_name . '_gmt' ] = ModelDataTranslator::prepareFieldValuesForJson( |
|
693 | + $result[$field_name.'_gmt'] = ModelDataTranslator::prepareFieldValuesForJson( |
|
694 | 694 | $field_obj, |
695 | 695 | $field_value, |
696 | 696 | $this->getModelVersionInfo()->requestedVersion() |
697 | 697 | ); |
698 | 698 | EEH_DTT_Helper::setTimezone($field_value, $timezone); |
699 | - $result[ $field_name ] = ModelDataTranslator::prepareFieldValuesForJson( |
|
699 | + $result[$field_name] = ModelDataTranslator::prepareFieldValuesForJson( |
|
700 | 700 | $field_obj, |
701 | 701 | $field_value, |
702 | 702 | $this->getModelVersionInfo()->requestedVersion() |
703 | 703 | ); |
704 | 704 | } else { |
705 | - $result[ $field_name ] = $this->prepareFieldObjValueForJson($field_obj, $field_value); |
|
705 | + $result[$field_name] = $this->prepareFieldObjValueForJson($field_obj, $field_value); |
|
706 | 706 | } |
707 | 707 | } |
708 | 708 | if ($do_chevy_shuffle) { |
@@ -754,7 +754,7 @@ discard block |
||
754 | 754 | protected function addExtraFields(EEM_Base $model, $db_row, $entity_array) |
755 | 755 | { |
756 | 756 | if ($model instanceof EEM_CPT_Base) { |
757 | - $entity_array['link'] = get_permalink($db_row[ $model->get_primary_key_field()->get_qualified_column() ]); |
|
757 | + $entity_array['link'] = get_permalink($db_row[$model->get_primary_key_field()->get_qualified_column()]); |
|
758 | 758 | } |
759 | 759 | return $entity_array; |
760 | 760 | } |
@@ -779,7 +779,7 @@ discard block |
||
779 | 779 | 'href' => $this->getVersionedLinkTo( |
780 | 780 | EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
781 | 781 | . '/' |
782 | - . $entity_array[ $model->primary_key_name() ] |
|
782 | + . $entity_array[$model->primary_key_name()] |
|
783 | 783 | ), |
784 | 784 | ), |
785 | 785 | ); |
@@ -795,12 +795,12 @@ discard block |
||
795 | 795 | if ($model->has_primary_key_field()) { |
796 | 796 | foreach ($this->getModelVersionInfo()->relationSettings($model) as $relation_name => $relation_obj) { |
797 | 797 | $related_model_part = Read::getRelatedEntityName($relation_name, $relation_obj); |
798 | - $links[ EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part ] = array( |
|
798 | + $links[EED_Core_Rest_Api::ee_api_link_namespace.$related_model_part] = array( |
|
799 | 799 | array( |
800 | 800 | 'href' => $this->getVersionedLinkTo( |
801 | 801 | EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
802 | 802 | . '/' |
803 | - . $entity_array[ $model->primary_key_name() ] |
|
803 | + . $entity_array[$model->primary_key_name()] |
|
804 | 804 | . '/' |
805 | 805 | . $related_model_part |
806 | 806 | ), |
@@ -829,13 +829,13 @@ discard block |
||
829 | 829 | $db_row = array() |
830 | 830 | ) { |
831 | 831 | // if $db_row not included, hope the entity array has what we need |
832 | - if (! $db_row) { |
|
832 | + if ( ! $db_row) { |
|
833 | 833 | $db_row = $entity_array; |
834 | 834 | } |
835 | 835 | $includes_for_this_model = $this->explodeAndGetItemsPrefixedWith($rest_request->get_param('include'), ''); |
836 | 836 | $includes_for_this_model = $this->removeModelNamesFromArray($includes_for_this_model); |
837 | 837 | // if they passed in * or didn't specify any includes, return everything |
838 | - if (! in_array('*', $includes_for_this_model) |
|
838 | + if ( ! in_array('*', $includes_for_this_model) |
|
839 | 839 | && ! empty($includes_for_this_model) |
840 | 840 | ) { |
841 | 841 | if ($model->has_primary_key_field()) { |
@@ -881,7 +881,7 @@ discard block |
||
881 | 881 | $relation_obj, |
882 | 882 | $pretend_related_request |
883 | 883 | ); |
884 | - $entity_array[ Read::getRelatedEntityName($relation_name, $relation_obj) ] = $related_results |
|
884 | + $entity_array[Read::getRelatedEntityName($relation_name, $relation_obj)] = $related_results |
|
885 | 885 | instanceof |
886 | 886 | WP_Error |
887 | 887 | ? null |
@@ -1013,7 +1013,7 @@ discard block |
||
1013 | 1013 | */ |
1014 | 1014 | public function validateContext($context) |
1015 | 1015 | { |
1016 | - if (! $context) { |
|
1016 | + if ( ! $context) { |
|
1017 | 1017 | $context = EEM_Base::caps_read; |
1018 | 1018 | } |
1019 | 1019 | $valid_contexts = EEM_Base::valid_cap_contexts(); |
@@ -1038,7 +1038,7 @@ discard block |
||
1038 | 1038 | EEM_Base::default_where_conditions_minimum_all, |
1039 | 1039 | EEM_Base::default_where_conditions_minimum_others, |
1040 | 1040 | ); |
1041 | - if (! $default_query_params) { |
|
1041 | + if ( ! $default_query_params) { |
|
1042 | 1042 | $default_query_params = EEM_Base::default_where_conditions_all; |
1043 | 1043 | } |
1044 | 1044 | if (in_array( |
@@ -1121,14 +1121,14 @@ discard block |
||
1121 | 1121 | } |
1122 | 1122 | if (isset($query_parameters['limit'])) { |
1123 | 1123 | // limit should be either a string like '23' or '23,43', or an array with two items in it |
1124 | - if (! is_array($query_parameters['limit'])) { |
|
1124 | + if ( ! is_array($query_parameters['limit'])) { |
|
1125 | 1125 | $limit_array = explode(',', (string) $query_parameters['limit']); |
1126 | 1126 | } else { |
1127 | 1127 | $limit_array = $query_parameters['limit']; |
1128 | 1128 | } |
1129 | 1129 | $sanitized_limit = array(); |
1130 | 1130 | foreach ($limit_array as $key => $limit_part) { |
1131 | - if ($this->debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1131 | + if ($this->debug_mode && ( ! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1132 | 1132 | throw new EE_Error( |
1133 | 1133 | sprintf( |
1134 | 1134 | __( |
@@ -1174,9 +1174,9 @@ discard block |
||
1174 | 1174 | $model_ready_query_params = array(); |
1175 | 1175 | foreach ($query_params as $key => $value) { |
1176 | 1176 | if (is_array($value)) { |
1177 | - $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsKeyForModels($model, $value); |
|
1177 | + $model_ready_query_params[$key] = $this->prepareRestQueryParamsKeyForModels($model, $value); |
|
1178 | 1178 | } else { |
1179 | - $model_ready_query_params[ $key ] = $value; |
|
1179 | + $model_ready_query_params[$key] = $value; |
|
1180 | 1180 | } |
1181 | 1181 | } |
1182 | 1182 | return $model_ready_query_params; |
@@ -1194,9 +1194,9 @@ discard block |
||
1194 | 1194 | $model_ready_query_params = array(); |
1195 | 1195 | foreach ($query_params as $key => $value) { |
1196 | 1196 | if (is_array($value)) { |
1197 | - $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsValuesForModels($model, $value); |
|
1197 | + $model_ready_query_params[$key] = $this->prepareRestQueryParamsValuesForModels($model, $value); |
|
1198 | 1198 | } else { |
1199 | - $model_ready_query_params[ $key ] = $value; |
|
1199 | + $model_ready_query_params[$key] = $value; |
|
1200 | 1200 | } |
1201 | 1201 | } |
1202 | 1202 | return $model_ready_query_params; |
@@ -1228,17 +1228,17 @@ discard block |
||
1228 | 1228 | foreach ($exploded_contents as $item) { |
1229 | 1229 | $item = trim($item); |
1230 | 1230 | // if no prefix was provided, so we look for items with no "." in them |
1231 | - if (! $prefix) { |
|
1231 | + if ( ! $prefix) { |
|
1232 | 1232 | // does this item have a period? |
1233 | 1233 | if (strpos($item, '.') === false) { |
1234 | 1234 | // if not, then its what we're looking for |
1235 | 1235 | $contents_with_prefix[] = $item; |
1236 | 1236 | } |
1237 | - } elseif (strpos($item, $prefix . '.') === 0) { |
|
1237 | + } elseif (strpos($item, $prefix.'.') === 0) { |
|
1238 | 1238 | // this item has the prefix and a period, grab it |
1239 | 1239 | $contents_with_prefix[] = substr( |
1240 | 1240 | $item, |
1241 | - strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1241 | + strpos($item, $prefix.'.') + strlen($prefix.'.') |
|
1242 | 1242 | ); |
1243 | 1243 | } elseif ($item === $prefix) { |
1244 | 1244 | // this item is JUST the prefix |
@@ -1277,9 +1277,9 @@ discard block |
||
1277 | 1277 | if ($model_name) { |
1278 | 1278 | foreach ($includes as $field_to_include) { |
1279 | 1279 | $field_to_include = trim($field_to_include); |
1280 | - if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1280 | + if (strpos($field_to_include, $model_name.'.') === 0) { |
|
1281 | 1281 | // found the model name at the exact start |
1282 | - $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1282 | + $field_sans_model_name = str_replace($model_name.'.', '', $field_to_include); |
|
1283 | 1283 | $extracted_fields_to_include[] = $field_sans_model_name; |
1284 | 1284 | } elseif ($field_to_include == $model_name) { |
1285 | 1285 | $extracted_fields_to_include[] = '*'; |
@@ -1319,7 +1319,7 @@ discard block |
||
1319 | 1319 | $restricted_query_params['caps'] = $context; |
1320 | 1320 | $this->setDebugInfo('model query params', $restricted_query_params); |
1321 | 1321 | $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
1322 | - if (! empty($model_rows)) { |
|
1322 | + if ( ! empty($model_rows)) { |
|
1323 | 1323 | return $this->createEntityFromWpdbResult( |
1324 | 1324 | $model, |
1325 | 1325 | array_shift($model_rows), |
@@ -1329,10 +1329,10 @@ discard block |
||
1329 | 1329 | // ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
1330 | 1330 | $lowercase_model_name = strtolower($model->get_this_model_name()); |
1331 | 1331 | $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
1332 | - if (! empty($model_rows_found_sans_restrictions)) { |
|
1332 | + if ( ! empty($model_rows_found_sans_restrictions)) { |
|
1333 | 1333 | // you got shafted- it existed but we didn't want to tell you! |
1334 | 1334 | return new WP_Error( |
1335 | - 'rest_user_cannot_' . $context, |
|
1335 | + 'rest_user_cannot_'.$context, |
|
1336 | 1336 | sprintf( |
1337 | 1337 | __('Sorry, you cannot %1$s this %2$s. Missing permissions are: %3$s', 'event_espresso'), |
1338 | 1338 | $context, |
@@ -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 |
@@ -162,7 +162,7 @@ |
||
162 | 162 | * |
163 | 163 | * @access protected |
164 | 164 | * @param $entity |
165 | - * @param mixed $identifier |
|
165 | + * @param string $identifier |
|
166 | 166 | * @return string |
167 | 167 | * @throws \EventEspresso\core\exceptions\InvalidEntityException |
168 | 168 | */ |
@@ -21,282 +21,282 @@ |
||
21 | 21 | class CollectionLoader |
22 | 22 | { |
23 | 23 | |
24 | - /** |
|
25 | - * possible return value when adding entities to a collection. |
|
26 | - * denotes that the entity was NOT ADDED to the collection |
|
27 | - */ |
|
28 | - const ENTITY_NOT_ADDED = 'entity-not-added-to-collection'; |
|
24 | + /** |
|
25 | + * possible return value when adding entities to a collection. |
|
26 | + * denotes that the entity was NOT ADDED to the collection |
|
27 | + */ |
|
28 | + const ENTITY_NOT_ADDED = 'entity-not-added-to-collection'; |
|
29 | 29 | |
30 | - /** |
|
31 | - * possible return value when adding entities to a collection. |
|
32 | - * denotes that the entity was SUCCESSFULLY ADDED to the collection |
|
33 | - */ |
|
34 | - const ENTITY_ADDED = 'entity-added-to-collection'; |
|
30 | + /** |
|
31 | + * possible return value when adding entities to a collection. |
|
32 | + * denotes that the entity was SUCCESSFULLY ADDED to the collection |
|
33 | + */ |
|
34 | + const ENTITY_ADDED = 'entity-added-to-collection'; |
|
35 | 35 | |
36 | - /** |
|
37 | - * possible return value when adding entities to a collection. |
|
38 | - * denotes that the entity was ALREADY ADDED to the collection, |
|
39 | - * and therefore could not be added again. |
|
40 | - */ |
|
41 | - const ENTITY_EXISTS = 'entity-already-in-collection'; |
|
36 | + /** |
|
37 | + * possible return value when adding entities to a collection. |
|
38 | + * denotes that the entity was ALREADY ADDED to the collection, |
|
39 | + * and therefore could not be added again. |
|
40 | + */ |
|
41 | + const ENTITY_EXISTS = 'entity-already-in-collection'; |
|
42 | 42 | |
43 | 43 | |
44 | - /** |
|
45 | - * @var CollectionDetailsInterface $collection_details |
|
46 | - */ |
|
47 | - protected $collection_details; |
|
44 | + /** |
|
45 | + * @var CollectionDetailsInterface $collection_details |
|
46 | + */ |
|
47 | + protected $collection_details; |
|
48 | 48 | |
49 | - /** |
|
50 | - * @var CollectionInterface $collection |
|
51 | - */ |
|
52 | - protected $collection; |
|
49 | + /** |
|
50 | + * @var CollectionInterface $collection |
|
51 | + */ |
|
52 | + protected $collection; |
|
53 | 53 | |
54 | - /** |
|
55 | - * @var FileLocator $file_locator |
|
56 | - */ |
|
57 | - protected $file_locator; |
|
54 | + /** |
|
55 | + * @var FileLocator $file_locator |
|
56 | + */ |
|
57 | + protected $file_locator; |
|
58 | 58 | |
59 | 59 | |
60 | - /** |
|
61 | - * CollectionLoader constructor. |
|
62 | - * |
|
63 | - * @param CollectionDetailsInterface $collection_details |
|
64 | - * @param CollectionInterface $collection |
|
65 | - * @param LocatorInterface $file_locator |
|
66 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
67 | - * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
68 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
69 | - * @throws \EventEspresso\core\exceptions\InvalidFilePathException |
|
70 | - * @throws \EventEspresso\core\exceptions\InvalidEntityException |
|
71 | - */ |
|
72 | - public function __construct( |
|
73 | - CollectionDetailsInterface $collection_details, |
|
74 | - CollectionInterface $collection = null, |
|
75 | - LocatorInterface $file_locator = null |
|
76 | - ) { |
|
77 | - $this->collection_details = $collection_details; |
|
78 | - if (! $collection instanceof CollectionInterface) { |
|
79 | - $collection = new Collection($this->collection_details->getCollectionInterface()); |
|
80 | - } |
|
81 | - $this->collection = $collection; |
|
82 | - $this->file_locator = $file_locator; |
|
83 | - $this->loadAllFromFilepaths(); |
|
84 | - $this->loadFromFQCNs(); |
|
85 | - } |
|
60 | + /** |
|
61 | + * CollectionLoader constructor. |
|
62 | + * |
|
63 | + * @param CollectionDetailsInterface $collection_details |
|
64 | + * @param CollectionInterface $collection |
|
65 | + * @param LocatorInterface $file_locator |
|
66 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
67 | + * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
68 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
69 | + * @throws \EventEspresso\core\exceptions\InvalidFilePathException |
|
70 | + * @throws \EventEspresso\core\exceptions\InvalidEntityException |
|
71 | + */ |
|
72 | + public function __construct( |
|
73 | + CollectionDetailsInterface $collection_details, |
|
74 | + CollectionInterface $collection = null, |
|
75 | + LocatorInterface $file_locator = null |
|
76 | + ) { |
|
77 | + $this->collection_details = $collection_details; |
|
78 | + if (! $collection instanceof CollectionInterface) { |
|
79 | + $collection = new Collection($this->collection_details->getCollectionInterface()); |
|
80 | + } |
|
81 | + $this->collection = $collection; |
|
82 | + $this->file_locator = $file_locator; |
|
83 | + $this->loadAllFromFilepaths(); |
|
84 | + $this->loadFromFQCNs(); |
|
85 | + } |
|
86 | 86 | |
87 | 87 | |
88 | - /** |
|
89 | - * @access public |
|
90 | - * @return \EventEspresso\core\services\collections\CollectionInterface |
|
91 | - */ |
|
92 | - public function getCollection() |
|
93 | - { |
|
94 | - return $this->collection; |
|
95 | - } |
|
88 | + /** |
|
89 | + * @access public |
|
90 | + * @return \EventEspresso\core\services\collections\CollectionInterface |
|
91 | + */ |
|
92 | + public function getCollection() |
|
93 | + { |
|
94 | + return $this->collection; |
|
95 | + } |
|
96 | 96 | |
97 | 97 | |
98 | - /** |
|
99 | - * @access protected |
|
100 | - * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
101 | - * @throws \EventEspresso\core\exceptions\InvalidFilePathException |
|
102 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
103 | - * @throws \EventEspresso\core\exceptions\InvalidEntityException |
|
104 | - */ |
|
105 | - protected function loadAllFromFilepaths() |
|
106 | - { |
|
107 | - if (! $this->file_locator instanceof FileLocator) { |
|
108 | - $this->file_locator = new FileLocator(); |
|
109 | - } |
|
110 | - $this->file_locator->setFileMask($this->collection_details->getFileMask()); |
|
111 | - // find all of the files that match the file mask in the specified folder |
|
112 | - $this->file_locator->locate($this->collection_details->getCollectionPaths()); |
|
113 | - // filter the results |
|
114 | - $filepaths = (array) apply_filters( |
|
115 | - 'FHEE__CollectionLoader__loadAllFromFilepath__filepaths', |
|
116 | - $this->file_locator->getFilePaths(), |
|
117 | - $this->collection_details->collectionName(), |
|
118 | - $this->collection_details |
|
119 | - ); |
|
120 | - if (empty($filepaths)) { |
|
121 | - return; |
|
122 | - } |
|
123 | - foreach ($filepaths as $filepath) { |
|
124 | - $this->loadClassFromFilepath($filepath); |
|
125 | - } |
|
126 | - } |
|
98 | + /** |
|
99 | + * @access protected |
|
100 | + * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
101 | + * @throws \EventEspresso\core\exceptions\InvalidFilePathException |
|
102 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
103 | + * @throws \EventEspresso\core\exceptions\InvalidEntityException |
|
104 | + */ |
|
105 | + protected function loadAllFromFilepaths() |
|
106 | + { |
|
107 | + if (! $this->file_locator instanceof FileLocator) { |
|
108 | + $this->file_locator = new FileLocator(); |
|
109 | + } |
|
110 | + $this->file_locator->setFileMask($this->collection_details->getFileMask()); |
|
111 | + // find all of the files that match the file mask in the specified folder |
|
112 | + $this->file_locator->locate($this->collection_details->getCollectionPaths()); |
|
113 | + // filter the results |
|
114 | + $filepaths = (array) apply_filters( |
|
115 | + 'FHEE__CollectionLoader__loadAllFromFilepath__filepaths', |
|
116 | + $this->file_locator->getFilePaths(), |
|
117 | + $this->collection_details->collectionName(), |
|
118 | + $this->collection_details |
|
119 | + ); |
|
120 | + if (empty($filepaths)) { |
|
121 | + return; |
|
122 | + } |
|
123 | + foreach ($filepaths as $filepath) { |
|
124 | + $this->loadClassFromFilepath($filepath); |
|
125 | + } |
|
126 | + } |
|
127 | 127 | |
128 | 128 | |
129 | - /** |
|
130 | - * loadClassFromFilepath |
|
131 | - * |
|
132 | - * @access protected |
|
133 | - * @param string $filepath |
|
134 | - * @return string |
|
135 | - * @throws \EventEspresso\core\exceptions\InvalidEntityException |
|
136 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
137 | - * @throws \EventEspresso\core\exceptions\InvalidFilePathException |
|
138 | - * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
139 | - */ |
|
140 | - protected function loadClassFromFilepath($filepath) |
|
141 | - { |
|
142 | - if (! is_string($filepath)) { |
|
143 | - throw new InvalidDataTypeException('$filepath', $filepath, 'string'); |
|
144 | - } |
|
145 | - if (! is_readable($filepath)) { |
|
146 | - throw new InvalidFilePathException($filepath); |
|
147 | - } |
|
148 | - require_once($filepath); |
|
149 | - // extract filename from path |
|
150 | - $file_name = basename($filepath); |
|
151 | - // now remove any file extensions |
|
152 | - $class_name = \EEH_File::get_classname_from_filepath_with_standard_filename($file_name); |
|
153 | - if (! class_exists($class_name)) { |
|
154 | - throw new InvalidClassException($class_name); |
|
155 | - } |
|
156 | - return $this->addEntityToCollection(new $class_name(), $file_name); |
|
157 | - } |
|
129 | + /** |
|
130 | + * loadClassFromFilepath |
|
131 | + * |
|
132 | + * @access protected |
|
133 | + * @param string $filepath |
|
134 | + * @return string |
|
135 | + * @throws \EventEspresso\core\exceptions\InvalidEntityException |
|
136 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
137 | + * @throws \EventEspresso\core\exceptions\InvalidFilePathException |
|
138 | + * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
139 | + */ |
|
140 | + protected function loadClassFromFilepath($filepath) |
|
141 | + { |
|
142 | + if (! is_string($filepath)) { |
|
143 | + throw new InvalidDataTypeException('$filepath', $filepath, 'string'); |
|
144 | + } |
|
145 | + if (! is_readable($filepath)) { |
|
146 | + throw new InvalidFilePathException($filepath); |
|
147 | + } |
|
148 | + require_once($filepath); |
|
149 | + // extract filename from path |
|
150 | + $file_name = basename($filepath); |
|
151 | + // now remove any file extensions |
|
152 | + $class_name = \EEH_File::get_classname_from_filepath_with_standard_filename($file_name); |
|
153 | + if (! class_exists($class_name)) { |
|
154 | + throw new InvalidClassException($class_name); |
|
155 | + } |
|
156 | + return $this->addEntityToCollection(new $class_name(), $file_name); |
|
157 | + } |
|
158 | 158 | |
159 | 159 | |
160 | - /** |
|
161 | - * addEntityToCollection |
|
162 | - * |
|
163 | - * @access protected |
|
164 | - * @param $entity |
|
165 | - * @param mixed $identifier |
|
166 | - * @return string |
|
167 | - * @throws \EventEspresso\core\exceptions\InvalidEntityException |
|
168 | - */ |
|
169 | - protected function addEntityToCollection($entity, $identifier) |
|
170 | - { |
|
171 | - do_action( |
|
172 | - 'FHEE__CollectionLoader__addEntityToCollection__entity', |
|
173 | - $entity, |
|
174 | - $this->collection_details->collectionName(), |
|
175 | - $this->collection_details |
|
176 | - ); |
|
177 | - $identifier = $this->setIdentifier($entity, $identifier); |
|
178 | - if ($this->collection->has($identifier)) { |
|
179 | - do_action( |
|
180 | - 'FHEE__CollectionLoader__addEntityToCollection__entity_already_added', |
|
181 | - $this, |
|
182 | - $this->collection_details->collectionName(), |
|
183 | - $this->collection_details |
|
184 | - ); |
|
185 | - return CollectionLoader::ENTITY_EXISTS; |
|
186 | - } |
|
187 | - if ($this->collection->add($entity, $identifier)) { |
|
188 | - do_action( |
|
189 | - 'FHEE__CollectionLoader__addEntityToCollection__entity_added', |
|
190 | - $this, |
|
191 | - $this->collection_details->collectionName(), |
|
192 | - $this->collection_details |
|
193 | - ); |
|
194 | - return CollectionLoader::ENTITY_ADDED; |
|
195 | - } |
|
196 | - do_action( |
|
197 | - 'FHEE__CollectionLoader__addEntityToCollection__entity_not_added', |
|
198 | - $this, |
|
199 | - $this->collection_details->collectionName(), |
|
200 | - $this->collection_details |
|
201 | - ); |
|
202 | - return CollectionLoader::ENTITY_NOT_ADDED; |
|
203 | - } |
|
160 | + /** |
|
161 | + * addEntityToCollection |
|
162 | + * |
|
163 | + * @access protected |
|
164 | + * @param $entity |
|
165 | + * @param mixed $identifier |
|
166 | + * @return string |
|
167 | + * @throws \EventEspresso\core\exceptions\InvalidEntityException |
|
168 | + */ |
|
169 | + protected function addEntityToCollection($entity, $identifier) |
|
170 | + { |
|
171 | + do_action( |
|
172 | + 'FHEE__CollectionLoader__addEntityToCollection__entity', |
|
173 | + $entity, |
|
174 | + $this->collection_details->collectionName(), |
|
175 | + $this->collection_details |
|
176 | + ); |
|
177 | + $identifier = $this->setIdentifier($entity, $identifier); |
|
178 | + if ($this->collection->has($identifier)) { |
|
179 | + do_action( |
|
180 | + 'FHEE__CollectionLoader__addEntityToCollection__entity_already_added', |
|
181 | + $this, |
|
182 | + $this->collection_details->collectionName(), |
|
183 | + $this->collection_details |
|
184 | + ); |
|
185 | + return CollectionLoader::ENTITY_EXISTS; |
|
186 | + } |
|
187 | + if ($this->collection->add($entity, $identifier)) { |
|
188 | + do_action( |
|
189 | + 'FHEE__CollectionLoader__addEntityToCollection__entity_added', |
|
190 | + $this, |
|
191 | + $this->collection_details->collectionName(), |
|
192 | + $this->collection_details |
|
193 | + ); |
|
194 | + return CollectionLoader::ENTITY_ADDED; |
|
195 | + } |
|
196 | + do_action( |
|
197 | + 'FHEE__CollectionLoader__addEntityToCollection__entity_not_added', |
|
198 | + $this, |
|
199 | + $this->collection_details->collectionName(), |
|
200 | + $this->collection_details |
|
201 | + ); |
|
202 | + return CollectionLoader::ENTITY_NOT_ADDED; |
|
203 | + } |
|
204 | 204 | |
205 | 205 | |
206 | - /** |
|
207 | - * setIdentifier |
|
208 | - * |
|
209 | - * @access protected |
|
210 | - * @param $entity |
|
211 | - * @param mixed $identifier |
|
212 | - * @return string |
|
213 | - * @throws \EventEspresso\core\exceptions\InvalidEntityException |
|
214 | - */ |
|
215 | - protected function setIdentifier($entity, $identifier) |
|
216 | - { |
|
217 | - switch ($this->collection_details->identifierType()) { |
|
218 | - // every unique object gets added to the collection, but not duplicates of the exact same object |
|
219 | - case CollectionDetails::ID_OBJECT_HASH: |
|
220 | - $identifier = spl_object_hash($entity); |
|
221 | - break; |
|
222 | - // only one entity per class can be added to collection, like a singleton |
|
223 | - case CollectionDetails::ID_CLASS_NAME: |
|
224 | - $identifier = get_class($entity); |
|
225 | - break; |
|
226 | - // objects added to the collection based on entity callback, so the entity itself decides |
|
227 | - case CollectionDetails::ID_CALLBACK_METHOD: |
|
228 | - $identifier_callback = $this->collection_details->identifierCallback(); |
|
229 | - if (! method_exists($entity, $identifier_callback)) { |
|
230 | - throw new InvalidEntityException( |
|
231 | - $entity, |
|
232 | - $this->collection_details->getCollectionInterface(), |
|
233 | - sprintf( |
|
234 | - __( |
|
235 | - 'The current collection is configured to use a method named "%1$s" when setting or retrieving objects. The supplied entity is an instance |
|
206 | + /** |
|
207 | + * setIdentifier |
|
208 | + * |
|
209 | + * @access protected |
|
210 | + * @param $entity |
|
211 | + * @param mixed $identifier |
|
212 | + * @return string |
|
213 | + * @throws \EventEspresso\core\exceptions\InvalidEntityException |
|
214 | + */ |
|
215 | + protected function setIdentifier($entity, $identifier) |
|
216 | + { |
|
217 | + switch ($this->collection_details->identifierType()) { |
|
218 | + // every unique object gets added to the collection, but not duplicates of the exact same object |
|
219 | + case CollectionDetails::ID_OBJECT_HASH: |
|
220 | + $identifier = spl_object_hash($entity); |
|
221 | + break; |
|
222 | + // only one entity per class can be added to collection, like a singleton |
|
223 | + case CollectionDetails::ID_CLASS_NAME: |
|
224 | + $identifier = get_class($entity); |
|
225 | + break; |
|
226 | + // objects added to the collection based on entity callback, so the entity itself decides |
|
227 | + case CollectionDetails::ID_CALLBACK_METHOD: |
|
228 | + $identifier_callback = $this->collection_details->identifierCallback(); |
|
229 | + if (! method_exists($entity, $identifier_callback)) { |
|
230 | + throw new InvalidEntityException( |
|
231 | + $entity, |
|
232 | + $this->collection_details->getCollectionInterface(), |
|
233 | + sprintf( |
|
234 | + __( |
|
235 | + 'The current collection is configured to use a method named "%1$s" when setting or retrieving objects. The supplied entity is an instance |
|
236 | 236 | of "%2$s", but does not contain this method.', |
237 | - 'event_espresso' |
|
238 | - ), |
|
239 | - $identifier_callback, |
|
240 | - get_class($entity) |
|
241 | - ) |
|
242 | - ); |
|
243 | - } |
|
244 | - $identifier = $entity->{$identifier_callback}(); |
|
245 | - break; |
|
246 | - } |
|
247 | - return apply_filters( |
|
248 | - 'FHEE__CollectionLoader__addEntityToCollection__identifier', |
|
249 | - $identifier, |
|
250 | - $this->collection_details->collectionName(), |
|
251 | - $this->collection_details |
|
252 | - ); |
|
253 | - } |
|
237 | + 'event_espresso' |
|
238 | + ), |
|
239 | + $identifier_callback, |
|
240 | + get_class($entity) |
|
241 | + ) |
|
242 | + ); |
|
243 | + } |
|
244 | + $identifier = $entity->{$identifier_callback}(); |
|
245 | + break; |
|
246 | + } |
|
247 | + return apply_filters( |
|
248 | + 'FHEE__CollectionLoader__addEntityToCollection__identifier', |
|
249 | + $identifier, |
|
250 | + $this->collection_details->collectionName(), |
|
251 | + $this->collection_details |
|
252 | + ); |
|
253 | + } |
|
254 | 254 | |
255 | 255 | |
256 | - /** |
|
257 | - * loadFromFQCNs |
|
258 | - * |
|
259 | - * @access protected |
|
260 | - * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
261 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
262 | - * @throws \EventEspresso\core\exceptions\InvalidEntityException |
|
263 | - */ |
|
264 | - protected function loadFromFQCNs() |
|
265 | - { |
|
266 | - $FQCNs = $this->collection_details->getCollectionFQCNs(); |
|
267 | - $FQCNs = (array) apply_filters( |
|
268 | - 'FHEE__CollectionLoader__loadAllFromFQCNs__FQCNs', |
|
269 | - $FQCNs, |
|
270 | - $this->collection_details->collectionName(), |
|
271 | - $this->collection_details |
|
272 | - ); |
|
273 | - foreach ($FQCNs as $FQCN) { |
|
274 | - $this->loadClassFromFQCN($FQCN); |
|
275 | - } |
|
276 | - } |
|
256 | + /** |
|
257 | + * loadFromFQCNs |
|
258 | + * |
|
259 | + * @access protected |
|
260 | + * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
261 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
262 | + * @throws \EventEspresso\core\exceptions\InvalidEntityException |
|
263 | + */ |
|
264 | + protected function loadFromFQCNs() |
|
265 | + { |
|
266 | + $FQCNs = $this->collection_details->getCollectionFQCNs(); |
|
267 | + $FQCNs = (array) apply_filters( |
|
268 | + 'FHEE__CollectionLoader__loadAllFromFQCNs__FQCNs', |
|
269 | + $FQCNs, |
|
270 | + $this->collection_details->collectionName(), |
|
271 | + $this->collection_details |
|
272 | + ); |
|
273 | + foreach ($FQCNs as $FQCN) { |
|
274 | + $this->loadClassFromFQCN($FQCN); |
|
275 | + } |
|
276 | + } |
|
277 | 277 | |
278 | 278 | |
279 | - /** |
|
280 | - * loadClassFromFQCN |
|
281 | - * |
|
282 | - * @access protected |
|
283 | - * @param string $FQCN Fully Qualified Class Name |
|
284 | - * @return string |
|
285 | - * @throws \EventEspresso\core\exceptions\InvalidEntityException |
|
286 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
287 | - * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
288 | - */ |
|
289 | - protected function loadClassFromFQCN($FQCN) |
|
290 | - { |
|
291 | - if (! is_string($FQCN)) { |
|
292 | - throw new InvalidDataTypeException('$FQCN', $FQCN, 'string'); |
|
293 | - } |
|
294 | - if (! class_exists($FQCN)) { |
|
295 | - throw new InvalidClassException($FQCN); |
|
296 | - } |
|
297 | - return $this->addEntityToCollection( |
|
298 | - \EE_Registry::instance()->create($FQCN), |
|
299 | - $FQCN |
|
300 | - ); |
|
301 | - } |
|
279 | + /** |
|
280 | + * loadClassFromFQCN |
|
281 | + * |
|
282 | + * @access protected |
|
283 | + * @param string $FQCN Fully Qualified Class Name |
|
284 | + * @return string |
|
285 | + * @throws \EventEspresso\core\exceptions\InvalidEntityException |
|
286 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
287 | + * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
288 | + */ |
|
289 | + protected function loadClassFromFQCN($FQCN) |
|
290 | + { |
|
291 | + if (! is_string($FQCN)) { |
|
292 | + throw new InvalidDataTypeException('$FQCN', $FQCN, 'string'); |
|
293 | + } |
|
294 | + if (! class_exists($FQCN)) { |
|
295 | + throw new InvalidClassException($FQCN); |
|
296 | + } |
|
297 | + return $this->addEntityToCollection( |
|
298 | + \EE_Registry::instance()->create($FQCN), |
|
299 | + $FQCN |
|
300 | + ); |
|
301 | + } |
|
302 | 302 | } |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | LocatorInterface $file_locator = null |
76 | 76 | ) { |
77 | 77 | $this->collection_details = $collection_details; |
78 | - if (! $collection instanceof CollectionInterface) { |
|
78 | + if ( ! $collection instanceof CollectionInterface) { |
|
79 | 79 | $collection = new Collection($this->collection_details->getCollectionInterface()); |
80 | 80 | } |
81 | 81 | $this->collection = $collection; |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | */ |
105 | 105 | protected function loadAllFromFilepaths() |
106 | 106 | { |
107 | - if (! $this->file_locator instanceof FileLocator) { |
|
107 | + if ( ! $this->file_locator instanceof FileLocator) { |
|
108 | 108 | $this->file_locator = new FileLocator(); |
109 | 109 | } |
110 | 110 | $this->file_locator->setFileMask($this->collection_details->getFileMask()); |
@@ -139,10 +139,10 @@ discard block |
||
139 | 139 | */ |
140 | 140 | protected function loadClassFromFilepath($filepath) |
141 | 141 | { |
142 | - if (! is_string($filepath)) { |
|
142 | + if ( ! is_string($filepath)) { |
|
143 | 143 | throw new InvalidDataTypeException('$filepath', $filepath, 'string'); |
144 | 144 | } |
145 | - if (! is_readable($filepath)) { |
|
145 | + if ( ! is_readable($filepath)) { |
|
146 | 146 | throw new InvalidFilePathException($filepath); |
147 | 147 | } |
148 | 148 | require_once($filepath); |
@@ -150,7 +150,7 @@ discard block |
||
150 | 150 | $file_name = basename($filepath); |
151 | 151 | // now remove any file extensions |
152 | 152 | $class_name = \EEH_File::get_classname_from_filepath_with_standard_filename($file_name); |
153 | - if (! class_exists($class_name)) { |
|
153 | + if ( ! class_exists($class_name)) { |
|
154 | 154 | throw new InvalidClassException($class_name); |
155 | 155 | } |
156 | 156 | return $this->addEntityToCollection(new $class_name(), $file_name); |
@@ -226,7 +226,7 @@ discard block |
||
226 | 226 | // objects added to the collection based on entity callback, so the entity itself decides |
227 | 227 | case CollectionDetails::ID_CALLBACK_METHOD: |
228 | 228 | $identifier_callback = $this->collection_details->identifierCallback(); |
229 | - if (! method_exists($entity, $identifier_callback)) { |
|
229 | + if ( ! method_exists($entity, $identifier_callback)) { |
|
230 | 230 | throw new InvalidEntityException( |
231 | 231 | $entity, |
232 | 232 | $this->collection_details->getCollectionInterface(), |
@@ -288,10 +288,10 @@ discard block |
||
288 | 288 | */ |
289 | 289 | protected function loadClassFromFQCN($FQCN) |
290 | 290 | { |
291 | - if (! is_string($FQCN)) { |
|
291 | + if ( ! is_string($FQCN)) { |
|
292 | 292 | throw new InvalidDataTypeException('$FQCN', $FQCN, 'string'); |
293 | 293 | } |
294 | - if (! class_exists($FQCN)) { |
|
294 | + if ( ! class_exists($FQCN)) { |
|
295 | 295 | throw new InvalidClassException($FQCN); |
296 | 296 | } |
297 | 297 | return $this->addEntityToCollection( |
@@ -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); |