@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | $attributes['limit'] = (int) $attributes['limit']; |
117 | 117 | $display_on_archives = filter_var($attributes['display_on_archives'], FILTER_VALIDATE_BOOLEAN); |
118 | 118 | // don't display on archives unless 'display_on_archives' is true |
119 | - if ($attributes['limit'] === 0 || (! $display_on_archives && is_archive())) { |
|
119 | + if ($attributes['limit'] === 0 || ( ! $display_on_archives && is_archive())) { |
|
120 | 120 | return ''; |
121 | 121 | } |
122 | 122 | try { |
@@ -200,7 +200,7 @@ discard block |
||
200 | 200 | */ |
201 | 201 | private function validateEntities(array $attributes) |
202 | 202 | { |
203 | - if (! $this->template_args['event'] instanceof EE_Event |
|
203 | + if ( ! $this->template_args['event'] instanceof EE_Event |
|
204 | 204 | || ( |
205 | 205 | empty($attributes['event_id']) |
206 | 206 | && $attributes['datetime_id'] |
@@ -316,7 +316,7 @@ discard block |
||
316 | 316 | */ |
317 | 317 | private function getDatetime(array $attributes) |
318 | 318 | { |
319 | - if (! empty($attributes['datetime_id'])) { |
|
319 | + if ( ! empty($attributes['datetime_id'])) { |
|
320 | 320 | $datetime = EEM_Datetime::instance()->get_one_by_ID($attributes['datetime_id']); |
321 | 321 | if ($datetime instanceof EE_Datetime) { |
322 | 322 | return $datetime; |
@@ -336,7 +336,7 @@ discard block |
||
336 | 336 | */ |
337 | 337 | private function getTicket(array $attributes) |
338 | 338 | { |
339 | - if (! empty($attributes['ticket_id'])) { |
|
339 | + if ( ! empty($attributes['ticket_id'])) { |
|
340 | 340 | $ticket = EEM_Ticket::instance()->get_one_by_ID($attributes['ticket_id']); |
341 | 341 | if ($ticket instanceof EE_Ticket) { |
342 | 342 | return $ticket; |
@@ -353,7 +353,7 @@ discard block |
||
353 | 353 | private function setAdditionalQueryParams(array $attributes) |
354 | 354 | { |
355 | 355 | $reg_status_array = EEM_Registration::reg_status_array(); |
356 | - if (isset($reg_status_array[ $attributes['status'] ])) { |
|
356 | + if (isset($reg_status_array[$attributes['status']])) { |
|
357 | 357 | $this->query_params[0]['Registration.STS_ID'] = $attributes['status']; |
358 | 358 | } |
359 | 359 | if (absint($attributes['limit'])) { |
@@ -31,338 +31,338 @@ |
||
31 | 31 | class EspressoEventAttendees extends EspressoShortcode |
32 | 32 | { |
33 | 33 | |
34 | - private $query_params = array( |
|
35 | - 0 => array(), |
|
36 | - ); |
|
34 | + private $query_params = array( |
|
35 | + 0 => array(), |
|
36 | + ); |
|
37 | 37 | |
38 | - private $template_args = array( |
|
39 | - 'contacts' => array(), |
|
40 | - 'event' => null, |
|
41 | - 'datetime' => null, |
|
42 | - 'ticket' => null, |
|
43 | - ); |
|
38 | + private $template_args = array( |
|
39 | + 'contacts' => array(), |
|
40 | + 'event' => null, |
|
41 | + 'datetime' => null, |
|
42 | + 'ticket' => null, |
|
43 | + ); |
|
44 | 44 | |
45 | - /** |
|
46 | - * the actual shortcode tag that gets registered with WordPress |
|
47 | - * |
|
48 | - * @return string |
|
49 | - */ |
|
50 | - public function getTag() |
|
51 | - { |
|
52 | - return 'ESPRESSO_EVENT_ATTENDEES'; |
|
53 | - } |
|
45 | + /** |
|
46 | + * the actual shortcode tag that gets registered with WordPress |
|
47 | + * |
|
48 | + * @return string |
|
49 | + */ |
|
50 | + public function getTag() |
|
51 | + { |
|
52 | + return 'ESPRESSO_EVENT_ATTENDEES'; |
|
53 | + } |
|
54 | 54 | |
55 | 55 | |
56 | - /** |
|
57 | - * the time in seconds to cache the results of the processShortcode() method |
|
58 | - * 0 means the processShortcode() results will NOT be cached at all |
|
59 | - * |
|
60 | - * @return int |
|
61 | - */ |
|
62 | - public function cacheExpiration() |
|
63 | - { |
|
64 | - return 0; |
|
65 | - } |
|
56 | + /** |
|
57 | + * the time in seconds to cache the results of the processShortcode() method |
|
58 | + * 0 means the processShortcode() results will NOT be cached at all |
|
59 | + * |
|
60 | + * @return int |
|
61 | + */ |
|
62 | + public function cacheExpiration() |
|
63 | + { |
|
64 | + return 0; |
|
65 | + } |
|
66 | 66 | |
67 | 67 | |
68 | - /** |
|
69 | - * a place for adding any initialization code that needs to run prior to wp_header(). |
|
70 | - * this may be required for shortcodes that utilize a corresponding module, |
|
71 | - * and need to enqueue assets for that module |
|
72 | - * |
|
73 | - * @return void |
|
74 | - */ |
|
75 | - public function initializeShortcode() |
|
76 | - { |
|
77 | - $this->shortcodeHasBeenInitialized(); |
|
78 | - } |
|
68 | + /** |
|
69 | + * a place for adding any initialization code that needs to run prior to wp_header(). |
|
70 | + * this may be required for shortcodes that utilize a corresponding module, |
|
71 | + * and need to enqueue assets for that module |
|
72 | + * |
|
73 | + * @return void |
|
74 | + */ |
|
75 | + public function initializeShortcode() |
|
76 | + { |
|
77 | + $this->shortcodeHasBeenInitialized(); |
|
78 | + } |
|
79 | 79 | |
80 | 80 | |
81 | - /** |
|
82 | - * process_shortcode - ESPRESSO_EVENT_ATTENDEES - Returns a list of attendees to an event. |
|
83 | - * [ESPRESSO_EVENT_ATTENDEES] |
|
84 | - * - defaults to attendees for earliest active event, or earliest upcoming event. |
|
85 | - * [ESPRESSO_EVENT_ATTENDEES event_id=123] |
|
86 | - * - attendees for specific event. |
|
87 | - * [ESPRESSO_EVENT_ATTENDEES datetime_id=245] |
|
88 | - * - attendees for a specific datetime. |
|
89 | - * [ESPRESSO_EVENT_ATTENDEES ticket_id=123] |
|
90 | - * - attendees for a specific ticket. |
|
91 | - * [ESPRESSO_EVENT_ATTENDEES status=all] |
|
92 | - * - specific registration status (use status id) or all for all attendees regardless of status. |
|
93 | - * Note default is to only return approved attendees |
|
94 | - * [ESPRESSO_EVENT_ATTENDEES show_gravatar=true] |
|
95 | - * - default is to not return gravatar. Otherwise if this is set then return gravatar for email address given. |
|
96 | - * [ESPRESSO_EVENT_ATTENDEES display_on_archives=true] |
|
97 | - * - default is to not display attendees list on archive pages. |
|
98 | - * Note: because of the relationship between event_id, ticket_id, and datetime_id: |
|
99 | - * If more than one of those params is included, then preference is given to the following: |
|
100 | - * - event_id is used whenever its present and any others are ignored. |
|
101 | - * - if no event_id then datetime is used whenever its present and any others are ignored. |
|
102 | - * - otherwise ticket_id is used if present. |
|
103 | - * |
|
104 | - * @param array $attributes |
|
105 | - * @return string |
|
106 | - * @throws EE_Error |
|
107 | - * @throws InvalidDataTypeException |
|
108 | - * @throws InvalidInterfaceException |
|
109 | - * @throws InvalidArgumentException |
|
110 | - * @throws DomainException |
|
111 | - */ |
|
112 | - public function processShortcode($attributes = array()) |
|
113 | - { |
|
114 | - // grab attributes and merge with defaults |
|
115 | - $attributes = $this->getAttributes((array) $attributes); |
|
116 | - $attributes['limit'] = (int) $attributes['limit']; |
|
117 | - $display_on_archives = filter_var($attributes['display_on_archives'], FILTER_VALIDATE_BOOLEAN); |
|
118 | - // don't display on archives unless 'display_on_archives' is true |
|
119 | - if ($attributes['limit'] === 0 || (! $display_on_archives && is_archive())) { |
|
120 | - return ''; |
|
121 | - } |
|
122 | - try { |
|
123 | - $this->setBaseTemplateArguments($attributes); |
|
124 | - $this->validateEntities($attributes); |
|
125 | - $this->setBaseQueryParams(); |
|
126 | - } catch (EntityNotFoundException $e) { |
|
127 | - if (WP_DEBUG) { |
|
128 | - return '<div class="important-notice ee-error">' |
|
129 | - . $e->getMessage() |
|
130 | - . '</div>'; |
|
131 | - } |
|
132 | - return ''; |
|
133 | - } |
|
134 | - $this->setAdditionalQueryParams($attributes); |
|
135 | - // get contacts! |
|
136 | - $this->template_args['contacts'] = EEM_Attendee::instance()->get_all($this->query_params); |
|
137 | - // all set let's load up the template and return. |
|
138 | - return EEH_Template::locate_template( |
|
139 | - 'loop-espresso_event_attendees.php', |
|
140 | - $this->template_args |
|
141 | - ); |
|
142 | - } |
|
81 | + /** |
|
82 | + * process_shortcode - ESPRESSO_EVENT_ATTENDEES - Returns a list of attendees to an event. |
|
83 | + * [ESPRESSO_EVENT_ATTENDEES] |
|
84 | + * - defaults to attendees for earliest active event, or earliest upcoming event. |
|
85 | + * [ESPRESSO_EVENT_ATTENDEES event_id=123] |
|
86 | + * - attendees for specific event. |
|
87 | + * [ESPRESSO_EVENT_ATTENDEES datetime_id=245] |
|
88 | + * - attendees for a specific datetime. |
|
89 | + * [ESPRESSO_EVENT_ATTENDEES ticket_id=123] |
|
90 | + * - attendees for a specific ticket. |
|
91 | + * [ESPRESSO_EVENT_ATTENDEES status=all] |
|
92 | + * - specific registration status (use status id) or all for all attendees regardless of status. |
|
93 | + * Note default is to only return approved attendees |
|
94 | + * [ESPRESSO_EVENT_ATTENDEES show_gravatar=true] |
|
95 | + * - default is to not return gravatar. Otherwise if this is set then return gravatar for email address given. |
|
96 | + * [ESPRESSO_EVENT_ATTENDEES display_on_archives=true] |
|
97 | + * - default is to not display attendees list on archive pages. |
|
98 | + * Note: because of the relationship between event_id, ticket_id, and datetime_id: |
|
99 | + * If more than one of those params is included, then preference is given to the following: |
|
100 | + * - event_id is used whenever its present and any others are ignored. |
|
101 | + * - if no event_id then datetime is used whenever its present and any others are ignored. |
|
102 | + * - otherwise ticket_id is used if present. |
|
103 | + * |
|
104 | + * @param array $attributes |
|
105 | + * @return string |
|
106 | + * @throws EE_Error |
|
107 | + * @throws InvalidDataTypeException |
|
108 | + * @throws InvalidInterfaceException |
|
109 | + * @throws InvalidArgumentException |
|
110 | + * @throws DomainException |
|
111 | + */ |
|
112 | + public function processShortcode($attributes = array()) |
|
113 | + { |
|
114 | + // grab attributes and merge with defaults |
|
115 | + $attributes = $this->getAttributes((array) $attributes); |
|
116 | + $attributes['limit'] = (int) $attributes['limit']; |
|
117 | + $display_on_archives = filter_var($attributes['display_on_archives'], FILTER_VALIDATE_BOOLEAN); |
|
118 | + // don't display on archives unless 'display_on_archives' is true |
|
119 | + if ($attributes['limit'] === 0 || (! $display_on_archives && is_archive())) { |
|
120 | + return ''; |
|
121 | + } |
|
122 | + try { |
|
123 | + $this->setBaseTemplateArguments($attributes); |
|
124 | + $this->validateEntities($attributes); |
|
125 | + $this->setBaseQueryParams(); |
|
126 | + } catch (EntityNotFoundException $e) { |
|
127 | + if (WP_DEBUG) { |
|
128 | + return '<div class="important-notice ee-error">' |
|
129 | + . $e->getMessage() |
|
130 | + . '</div>'; |
|
131 | + } |
|
132 | + return ''; |
|
133 | + } |
|
134 | + $this->setAdditionalQueryParams($attributes); |
|
135 | + // get contacts! |
|
136 | + $this->template_args['contacts'] = EEM_Attendee::instance()->get_all($this->query_params); |
|
137 | + // all set let's load up the template and return. |
|
138 | + return EEH_Template::locate_template( |
|
139 | + 'loop-espresso_event_attendees.php', |
|
140 | + $this->template_args |
|
141 | + ); |
|
142 | + } |
|
143 | 143 | |
144 | 144 | |
145 | - /** |
|
146 | - * merge incoming attributes with filtered defaults |
|
147 | - * |
|
148 | - * @param array $attributes |
|
149 | - * @return array |
|
150 | - */ |
|
151 | - private function getAttributes(array $attributes) |
|
152 | - { |
|
153 | - return (array) apply_filters( |
|
154 | - 'EES_Espresso_Event_Attendees__process_shortcode__default_shortcode_atts', |
|
155 | - $attributes + array( |
|
156 | - 'event_id' => null, |
|
157 | - 'datetime_id' => null, |
|
158 | - 'ticket_id' => null, |
|
159 | - 'status' => EEM_Registration::status_id_approved, |
|
160 | - 'show_gravatar' => false, |
|
161 | - 'display_on_archives' => false, |
|
162 | - 'limit' => 999, |
|
163 | - ) |
|
164 | - ); |
|
165 | - } |
|
145 | + /** |
|
146 | + * merge incoming attributes with filtered defaults |
|
147 | + * |
|
148 | + * @param array $attributes |
|
149 | + * @return array |
|
150 | + */ |
|
151 | + private function getAttributes(array $attributes) |
|
152 | + { |
|
153 | + return (array) apply_filters( |
|
154 | + 'EES_Espresso_Event_Attendees__process_shortcode__default_shortcode_atts', |
|
155 | + $attributes + array( |
|
156 | + 'event_id' => null, |
|
157 | + 'datetime_id' => null, |
|
158 | + 'ticket_id' => null, |
|
159 | + 'status' => EEM_Registration::status_id_approved, |
|
160 | + 'show_gravatar' => false, |
|
161 | + 'display_on_archives' => false, |
|
162 | + 'limit' => 999, |
|
163 | + ) |
|
164 | + ); |
|
165 | + } |
|
166 | 166 | |
167 | 167 | |
168 | - /** |
|
169 | - * Set all the base template arguments from the incoming attributes. |
|
170 | - * * Note: because of the relationship between event_id, ticket_id, and datetime_id: |
|
171 | - * If more than one of those params is included, then preference is given to the following: |
|
172 | - * - event_id is used whenever its present and any others are ignored. |
|
173 | - * - if no event_id then datetime is used whenever its present and any others are ignored. |
|
174 | - * - otherwise ticket_id is used if present. |
|
175 | - * |
|
176 | - * @param array $attributes |
|
177 | - * @throws EE_Error |
|
178 | - * @throws InvalidDataTypeException |
|
179 | - * @throws InvalidInterfaceException |
|
180 | - * @throws InvalidArgumentException |
|
181 | - */ |
|
182 | - private function setBaseTemplateArguments(array $attributes) |
|
183 | - { |
|
184 | - $this->template_args['show_gravatar'] = $attributes['show_gravatar']; |
|
185 | - $this->template_args['event'] = $this->getEvent($attributes); |
|
186 | - $this->template_args['datetime'] = empty($attributes['event_id']) |
|
187 | - ? $this->getDatetime($attributes) |
|
188 | - : null; |
|
189 | - $this->template_args['ticket'] = empty($attributes['datetime_id']) && empty($attributes['event_id']) |
|
190 | - ? $this->getTicket($attributes) |
|
191 | - : null; |
|
192 | - } |
|
168 | + /** |
|
169 | + * Set all the base template arguments from the incoming attributes. |
|
170 | + * * Note: because of the relationship between event_id, ticket_id, and datetime_id: |
|
171 | + * If more than one of those params is included, then preference is given to the following: |
|
172 | + * - event_id is used whenever its present and any others are ignored. |
|
173 | + * - if no event_id then datetime is used whenever its present and any others are ignored. |
|
174 | + * - otherwise ticket_id is used if present. |
|
175 | + * |
|
176 | + * @param array $attributes |
|
177 | + * @throws EE_Error |
|
178 | + * @throws InvalidDataTypeException |
|
179 | + * @throws InvalidInterfaceException |
|
180 | + * @throws InvalidArgumentException |
|
181 | + */ |
|
182 | + private function setBaseTemplateArguments(array $attributes) |
|
183 | + { |
|
184 | + $this->template_args['show_gravatar'] = $attributes['show_gravatar']; |
|
185 | + $this->template_args['event'] = $this->getEvent($attributes); |
|
186 | + $this->template_args['datetime'] = empty($attributes['event_id']) |
|
187 | + ? $this->getDatetime($attributes) |
|
188 | + : null; |
|
189 | + $this->template_args['ticket'] = empty($attributes['datetime_id']) && empty($attributes['event_id']) |
|
190 | + ? $this->getTicket($attributes) |
|
191 | + : null; |
|
192 | + } |
|
193 | 193 | |
194 | 194 | |
195 | - /** |
|
196 | - * Validates the presence of entities for the given attribute values. |
|
197 | - * |
|
198 | - * @param array $attributes |
|
199 | - * @throws EntityNotFoundException |
|
200 | - */ |
|
201 | - private function validateEntities(array $attributes) |
|
202 | - { |
|
203 | - if (! $this->template_args['event'] instanceof EE_Event |
|
204 | - || ( |
|
205 | - empty($attributes['event_id']) |
|
206 | - && $attributes['datetime_id'] |
|
207 | - && ! $this->template_args['datetime'] instanceof EE_Datetime |
|
208 | - ) |
|
209 | - || ( |
|
210 | - empty($attributes['event_id']) |
|
211 | - && empty($attributes['datetime_id']) |
|
212 | - && $attributes['ticket_id'] |
|
213 | - && ! $this->template_args['ticket'] instanceof EE_Ticket |
|
214 | - ) |
|
215 | - ) { |
|
216 | - throw new EntityNotFoundException( |
|
217 | - '', |
|
218 | - '', |
|
219 | - esc_html__( |
|
220 | - 'The [ESPRESSO_EVENT_ATTENDEES] shortcode has been used incorrectly. Please double check the arguments you used for any typos. In the case of ID type arguments, its possible the given ID does not correspond to existing data in the database.', |
|
221 | - 'event_espresso' |
|
222 | - ) |
|
223 | - ); |
|
224 | - } |
|
225 | - } |
|
195 | + /** |
|
196 | + * Validates the presence of entities for the given attribute values. |
|
197 | + * |
|
198 | + * @param array $attributes |
|
199 | + * @throws EntityNotFoundException |
|
200 | + */ |
|
201 | + private function validateEntities(array $attributes) |
|
202 | + { |
|
203 | + if (! $this->template_args['event'] instanceof EE_Event |
|
204 | + || ( |
|
205 | + empty($attributes['event_id']) |
|
206 | + && $attributes['datetime_id'] |
|
207 | + && ! $this->template_args['datetime'] instanceof EE_Datetime |
|
208 | + ) |
|
209 | + || ( |
|
210 | + empty($attributes['event_id']) |
|
211 | + && empty($attributes['datetime_id']) |
|
212 | + && $attributes['ticket_id'] |
|
213 | + && ! $this->template_args['ticket'] instanceof EE_Ticket |
|
214 | + ) |
|
215 | + ) { |
|
216 | + throw new EntityNotFoundException( |
|
217 | + '', |
|
218 | + '', |
|
219 | + esc_html__( |
|
220 | + 'The [ESPRESSO_EVENT_ATTENDEES] shortcode has been used incorrectly. Please double check the arguments you used for any typos. In the case of ID type arguments, its possible the given ID does not correspond to existing data in the database.', |
|
221 | + 'event_espresso' |
|
222 | + ) |
|
223 | + ); |
|
224 | + } |
|
225 | + } |
|
226 | 226 | |
227 | 227 | |
228 | - /** |
|
229 | - * Sets the query params for the base query elements. |
|
230 | - */ |
|
231 | - private function setBaseQueryParams() |
|
232 | - { |
|
233 | - switch (true) { |
|
234 | - case $this->template_args['datetime'] instanceof EE_Datetime: |
|
235 | - $this->query_params = array( |
|
236 | - 0 => array( |
|
237 | - 'Registration.Ticket.Datetime.DTT_ID' => $this->template_args['datetime']->ID(), |
|
238 | - ), |
|
239 | - 'default_where_conditions' => 'this_model_only', |
|
240 | - ); |
|
241 | - break; |
|
242 | - case $this->template_args['ticket'] instanceof EE_Ticket: |
|
243 | - $this->query_params[0] = array( |
|
244 | - 'Registration.TKT_ID' => $this->template_args['ticket']->ID(), |
|
245 | - ); |
|
246 | - break; |
|
247 | - case $this->template_args['event'] instanceof EE_Event: |
|
248 | - $this->query_params[0] = array( |
|
249 | - 'Registration.EVT_ID' => $this->template_args['event']->ID(), |
|
250 | - ); |
|
251 | - break; |
|
252 | - } |
|
253 | - } |
|
228 | + /** |
|
229 | + * Sets the query params for the base query elements. |
|
230 | + */ |
|
231 | + private function setBaseQueryParams() |
|
232 | + { |
|
233 | + switch (true) { |
|
234 | + case $this->template_args['datetime'] instanceof EE_Datetime: |
|
235 | + $this->query_params = array( |
|
236 | + 0 => array( |
|
237 | + 'Registration.Ticket.Datetime.DTT_ID' => $this->template_args['datetime']->ID(), |
|
238 | + ), |
|
239 | + 'default_where_conditions' => 'this_model_only', |
|
240 | + ); |
|
241 | + break; |
|
242 | + case $this->template_args['ticket'] instanceof EE_Ticket: |
|
243 | + $this->query_params[0] = array( |
|
244 | + 'Registration.TKT_ID' => $this->template_args['ticket']->ID(), |
|
245 | + ); |
|
246 | + break; |
|
247 | + case $this->template_args['event'] instanceof EE_Event: |
|
248 | + $this->query_params[0] = array( |
|
249 | + 'Registration.EVT_ID' => $this->template_args['event']->ID(), |
|
250 | + ); |
|
251 | + break; |
|
252 | + } |
|
253 | + } |
|
254 | 254 | |
255 | 255 | |
256 | - /** |
|
257 | - * @param array $attributes |
|
258 | - * @return EE_Event|null |
|
259 | - * @throws EE_Error |
|
260 | - * @throws InvalidDataTypeException |
|
261 | - * @throws InvalidInterfaceException |
|
262 | - * @throws InvalidArgumentException |
|
263 | - */ |
|
264 | - private function getEvent(array $attributes) |
|
265 | - { |
|
266 | - switch (true) { |
|
267 | - case ! empty($attributes['event_id']): |
|
268 | - $event = EEM_Event::instance()->get_one_by_ID($attributes['event_id']); |
|
269 | - break; |
|
270 | - case ! empty($attributes['datetime_id']): |
|
271 | - $event = EEM_Event::instance()->get_one(array( |
|
272 | - array( |
|
273 | - 'Datetime.DTT_ID' => $attributes['datetime_id'], |
|
274 | - ), |
|
275 | - )); |
|
276 | - break; |
|
277 | - case ! empty($attributes['ticket_id']): |
|
278 | - $event = EEM_Event::instance()->get_one(array( |
|
279 | - array( |
|
280 | - 'Datetime.Ticket.TKT_ID' => $attributes['ticket_id'], |
|
281 | - ), |
|
282 | - 'default_where_conditions' => 'none' |
|
283 | - )); |
|
284 | - break; |
|
285 | - case is_espresso_event(): |
|
286 | - $event = EEH_Event_View::get_event(); |
|
287 | - break; |
|
288 | - default: |
|
289 | - // one last shot... |
|
290 | - // try getting the earliest active event |
|
291 | - $events = EEM_Event::instance()->get_active_events(array( |
|
292 | - 'limit' => 1, |
|
293 | - 'order_by' => array('Datetime.DTT_EVT_start' => 'ASC'), |
|
294 | - )); |
|
295 | - // if none then get the next upcoming |
|
296 | - $events = empty($events) |
|
297 | - ? EEM_Event::instance()->get_upcoming_events(array( |
|
298 | - 'limit' => 1, |
|
299 | - 'order_by' => array('Datetime.DTT_EVT_start' => 'ASC'), |
|
300 | - )) |
|
301 | - : $events; |
|
302 | - $event = reset($events); |
|
303 | - } |
|
256 | + /** |
|
257 | + * @param array $attributes |
|
258 | + * @return EE_Event|null |
|
259 | + * @throws EE_Error |
|
260 | + * @throws InvalidDataTypeException |
|
261 | + * @throws InvalidInterfaceException |
|
262 | + * @throws InvalidArgumentException |
|
263 | + */ |
|
264 | + private function getEvent(array $attributes) |
|
265 | + { |
|
266 | + switch (true) { |
|
267 | + case ! empty($attributes['event_id']): |
|
268 | + $event = EEM_Event::instance()->get_one_by_ID($attributes['event_id']); |
|
269 | + break; |
|
270 | + case ! empty($attributes['datetime_id']): |
|
271 | + $event = EEM_Event::instance()->get_one(array( |
|
272 | + array( |
|
273 | + 'Datetime.DTT_ID' => $attributes['datetime_id'], |
|
274 | + ), |
|
275 | + )); |
|
276 | + break; |
|
277 | + case ! empty($attributes['ticket_id']): |
|
278 | + $event = EEM_Event::instance()->get_one(array( |
|
279 | + array( |
|
280 | + 'Datetime.Ticket.TKT_ID' => $attributes['ticket_id'], |
|
281 | + ), |
|
282 | + 'default_where_conditions' => 'none' |
|
283 | + )); |
|
284 | + break; |
|
285 | + case is_espresso_event(): |
|
286 | + $event = EEH_Event_View::get_event(); |
|
287 | + break; |
|
288 | + default: |
|
289 | + // one last shot... |
|
290 | + // try getting the earliest active event |
|
291 | + $events = EEM_Event::instance()->get_active_events(array( |
|
292 | + 'limit' => 1, |
|
293 | + 'order_by' => array('Datetime.DTT_EVT_start' => 'ASC'), |
|
294 | + )); |
|
295 | + // if none then get the next upcoming |
|
296 | + $events = empty($events) |
|
297 | + ? EEM_Event::instance()->get_upcoming_events(array( |
|
298 | + 'limit' => 1, |
|
299 | + 'order_by' => array('Datetime.DTT_EVT_start' => 'ASC'), |
|
300 | + )) |
|
301 | + : $events; |
|
302 | + $event = reset($events); |
|
303 | + } |
|
304 | 304 | |
305 | - return $event instanceof EE_Event ? $event : null; |
|
306 | - } |
|
305 | + return $event instanceof EE_Event ? $event : null; |
|
306 | + } |
|
307 | 307 | |
308 | 308 | |
309 | - /** |
|
310 | - * @param array $attributes |
|
311 | - * @return EE_Datetime|null |
|
312 | - * @throws EE_Error |
|
313 | - * @throws InvalidDataTypeException |
|
314 | - * @throws InvalidInterfaceException |
|
315 | - * @throws InvalidArgumentException |
|
316 | - */ |
|
317 | - private function getDatetime(array $attributes) |
|
318 | - { |
|
319 | - if (! empty($attributes['datetime_id'])) { |
|
320 | - $datetime = EEM_Datetime::instance()->get_one_by_ID($attributes['datetime_id']); |
|
321 | - if ($datetime instanceof EE_Datetime) { |
|
322 | - return $datetime; |
|
323 | - } |
|
324 | - } |
|
325 | - return null; |
|
326 | - } |
|
309 | + /** |
|
310 | + * @param array $attributes |
|
311 | + * @return EE_Datetime|null |
|
312 | + * @throws EE_Error |
|
313 | + * @throws InvalidDataTypeException |
|
314 | + * @throws InvalidInterfaceException |
|
315 | + * @throws InvalidArgumentException |
|
316 | + */ |
|
317 | + private function getDatetime(array $attributes) |
|
318 | + { |
|
319 | + if (! empty($attributes['datetime_id'])) { |
|
320 | + $datetime = EEM_Datetime::instance()->get_one_by_ID($attributes['datetime_id']); |
|
321 | + if ($datetime instanceof EE_Datetime) { |
|
322 | + return $datetime; |
|
323 | + } |
|
324 | + } |
|
325 | + return null; |
|
326 | + } |
|
327 | 327 | |
328 | 328 | |
329 | - /** |
|
330 | - * @param array $attributes |
|
331 | - * @return \EE_Base_Class|EE_Ticket|null |
|
332 | - * @throws EE_Error |
|
333 | - * @throws InvalidDataTypeException |
|
334 | - * @throws InvalidInterfaceException |
|
335 | - * @throws InvalidArgumentException |
|
336 | - */ |
|
337 | - private function getTicket(array $attributes) |
|
338 | - { |
|
339 | - if (! empty($attributes['ticket_id'])) { |
|
340 | - $ticket = EEM_Ticket::instance()->get_one_by_ID($attributes['ticket_id']); |
|
341 | - if ($ticket instanceof EE_Ticket) { |
|
342 | - return $ticket; |
|
343 | - } |
|
344 | - } |
|
345 | - return null; |
|
346 | - } |
|
329 | + /** |
|
330 | + * @param array $attributes |
|
331 | + * @return \EE_Base_Class|EE_Ticket|null |
|
332 | + * @throws EE_Error |
|
333 | + * @throws InvalidDataTypeException |
|
334 | + * @throws InvalidInterfaceException |
|
335 | + * @throws InvalidArgumentException |
|
336 | + */ |
|
337 | + private function getTicket(array $attributes) |
|
338 | + { |
|
339 | + if (! empty($attributes['ticket_id'])) { |
|
340 | + $ticket = EEM_Ticket::instance()->get_one_by_ID($attributes['ticket_id']); |
|
341 | + if ($ticket instanceof EE_Ticket) { |
|
342 | + return $ticket; |
|
343 | + } |
|
344 | + } |
|
345 | + return null; |
|
346 | + } |
|
347 | 347 | |
348 | 348 | |
349 | - /** |
|
350 | - * @param array $attributes |
|
351 | - * @throws EE_Error |
|
352 | - */ |
|
353 | - private function setAdditionalQueryParams(array $attributes) |
|
354 | - { |
|
355 | - $reg_status_array = EEM_Registration::reg_status_array(); |
|
356 | - if (isset($reg_status_array[ $attributes['status'] ])) { |
|
357 | - $this->query_params[0]['Registration.STS_ID'] = $attributes['status']; |
|
358 | - } |
|
359 | - if (absint($attributes['limit'])) { |
|
360 | - $this->query_params['limit'] = $attributes['limit']; |
|
361 | - } |
|
362 | - $this->query_params['group_by'] = array('ATT_ID'); |
|
363 | - $this->query_params['order_by'] = (array) apply_filters( |
|
364 | - 'FHEE__EES_Espresso_Event_Attendees__process_shortcode__order_by', |
|
365 | - array('ATT_lname' => 'ASC', 'ATT_fname' => 'ASC') |
|
366 | - ); |
|
367 | - } |
|
349 | + /** |
|
350 | + * @param array $attributes |
|
351 | + * @throws EE_Error |
|
352 | + */ |
|
353 | + private function setAdditionalQueryParams(array $attributes) |
|
354 | + { |
|
355 | + $reg_status_array = EEM_Registration::reg_status_array(); |
|
356 | + if (isset($reg_status_array[ $attributes['status'] ])) { |
|
357 | + $this->query_params[0]['Registration.STS_ID'] = $attributes['status']; |
|
358 | + } |
|
359 | + if (absint($attributes['limit'])) { |
|
360 | + $this->query_params['limit'] = $attributes['limit']; |
|
361 | + } |
|
362 | + $this->query_params['group_by'] = array('ATT_ID'); |
|
363 | + $this->query_params['order_by'] = (array) apply_filters( |
|
364 | + 'FHEE__EES_Espresso_Event_Attendees__process_shortcode__order_by', |
|
365 | + array('ATT_lname' => 'ASC', 'ATT_fname' => 'ASC') |
|
366 | + ); |
|
367 | + } |
|
368 | 368 | } |
@@ -317,6 +317,7 @@ discard block |
||
317 | 317 | |
318 | 318 | /** |
319 | 319 | * @param mixed string | EED_Module $module |
320 | + * @param string $module |
|
320 | 321 | * @throws OutOfBoundsException |
321 | 322 | * @throws InvalidArgumentException |
322 | 323 | * @throws InvalidInterfaceException |
@@ -720,7 +721,7 @@ discard block |
||
720 | 721 | * and thus call a different method to instantiate |
721 | 722 | * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
722 | 723 | * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
723 | - * @return bool|null|mixed null = failure to load or instantiate class object. |
|
724 | + * @return EventEspresso\core\services\commands\CommandInterface null = failure to load or instantiate class object. |
|
724 | 725 | * object = class loaded and instantiated successfully. |
725 | 726 | * bool = fail or success when $load_only is true |
726 | 727 | * @throws InvalidInterfaceException |
@@ -791,7 +792,7 @@ discard block |
||
791 | 792 | /** |
792 | 793 | * Recursively checks that a class exists and potentially attempts to load classes with non-FQCNs |
793 | 794 | * |
794 | - * @param string|object $class_name |
|
795 | + * @param string $class_name |
|
795 | 796 | * @param array $arguments |
796 | 797 | * @param int $attempt |
797 | 798 | * @return mixed |
@@ -1370,7 +1371,7 @@ discard block |
||
1370 | 1371 | * @param string $class_name |
1371 | 1372 | * @param string $param_class |
1372 | 1373 | * @param array $arguments |
1373 | - * @param mixed $index |
|
1374 | + * @param integer $index |
|
1374 | 1375 | * @return array |
1375 | 1376 | * @throws InvalidArgumentException |
1376 | 1377 | * @throws InvalidInterfaceException |
@@ -23,1679 +23,1679 @@ |
||
23 | 23 | class EE_Registry implements ResettableInterface |
24 | 24 | { |
25 | 25 | |
26 | - /** |
|
27 | - * @var EE_Registry $_instance |
|
28 | - */ |
|
29 | - private static $_instance; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var EE_Dependency_Map $_dependency_map |
|
33 | - */ |
|
34 | - protected $_dependency_map; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var Mirror |
|
38 | - */ |
|
39 | - private $mirror; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var ClassInterfaceCache $class_cache |
|
43 | - */ |
|
44 | - private $class_cache; |
|
45 | - |
|
46 | - /** |
|
47 | - * @var array $_class_abbreviations |
|
48 | - */ |
|
49 | - protected $_class_abbreviations = array(); |
|
50 | - |
|
51 | - /** |
|
52 | - * @var CommandBusInterface $BUS |
|
53 | - */ |
|
54 | - public $BUS; |
|
55 | - |
|
56 | - /** |
|
57 | - * @var EE_Cart $CART |
|
58 | - */ |
|
59 | - public $CART; |
|
60 | - |
|
61 | - /** |
|
62 | - * @var EE_Config $CFG |
|
63 | - */ |
|
64 | - public $CFG; |
|
65 | - |
|
66 | - /** |
|
67 | - * @var EE_Network_Config $NET_CFG |
|
68 | - */ |
|
69 | - public $NET_CFG; |
|
70 | - |
|
71 | - /** |
|
72 | - * StdClass object for storing library classes in |
|
73 | - * |
|
74 | - * @var RegistryContainer $LIB |
|
75 | - */ |
|
76 | - public $LIB; |
|
77 | - |
|
78 | - /** |
|
79 | - * @var EE_Request_Handler $REQ |
|
80 | - */ |
|
81 | - public $REQ; |
|
82 | - |
|
83 | - /** |
|
84 | - * @var EE_Session $SSN |
|
85 | - */ |
|
86 | - public $SSN; |
|
87 | - |
|
88 | - /** |
|
89 | - * @since 4.5.0 |
|
90 | - * @var EE_Capabilities $CAP |
|
91 | - */ |
|
92 | - public $CAP; |
|
93 | - |
|
94 | - /** |
|
95 | - * @since 4.9.0 |
|
96 | - * @var EE_Message_Resource_Manager $MRM |
|
97 | - */ |
|
98 | - public $MRM; |
|
99 | - |
|
100 | - /** |
|
101 | - * @var Registry $AssetsRegistry |
|
102 | - */ |
|
103 | - public $AssetsRegistry; |
|
104 | - |
|
105 | - /** |
|
106 | - * StdClass object for holding addons which have registered themselves to work with EE core |
|
107 | - * |
|
108 | - * @var EE_Addon[] $addons |
|
109 | - */ |
|
110 | - public $addons; |
|
111 | - |
|
112 | - /** |
|
113 | - * keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
|
114 | - * |
|
115 | - * @var EEM_Base[] $models |
|
116 | - */ |
|
117 | - public $models = array(); |
|
118 | - |
|
119 | - /** |
|
120 | - * @var EED_Module[] $modules |
|
121 | - */ |
|
122 | - public $modules; |
|
123 | - |
|
124 | - /** |
|
125 | - * @var EES_Shortcode[] $shortcodes |
|
126 | - */ |
|
127 | - public $shortcodes; |
|
128 | - |
|
129 | - /** |
|
130 | - * @var WP_Widget[] $widgets |
|
131 | - */ |
|
132 | - public $widgets; |
|
133 | - |
|
134 | - /** |
|
135 | - * this is an array of all implemented model names (i.e. not the parent abstract models, or models |
|
136 | - * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
|
137 | - * Keys are model "short names" (eg "Event") as used in model relations, and values are |
|
138 | - * classnames (eg "EEM_Event") |
|
139 | - * |
|
140 | - * @var array $non_abstract_db_models |
|
141 | - */ |
|
142 | - public $non_abstract_db_models = array(); |
|
143 | - |
|
144 | - /** |
|
145 | - * internationalization for JS strings |
|
146 | - * usage: EE_Registry::i18n_js_strings['string_key'] = esc_html__( 'string to translate.', 'event_espresso' ); |
|
147 | - * in js file: var translatedString = eei18n.string_key; |
|
148 | - * |
|
149 | - * @var array $i18n_js_strings |
|
150 | - */ |
|
151 | - public static $i18n_js_strings = array(); |
|
152 | - |
|
153 | - /** |
|
154 | - * $main_file - path to espresso.php |
|
155 | - * |
|
156 | - * @var array $main_file |
|
157 | - */ |
|
158 | - public $main_file; |
|
159 | - |
|
160 | - /** |
|
161 | - * array of ReflectionClass objects where the key is the class name |
|
162 | - * |
|
163 | - * @deprecated 4.9.62.p |
|
164 | - * @var ReflectionClass[] $_reflectors |
|
165 | - */ |
|
166 | - public $_reflectors; |
|
167 | - |
|
168 | - /** |
|
169 | - * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
|
170 | - * |
|
171 | - * @var boolean $_cache_on |
|
172 | - */ |
|
173 | - protected $_cache_on = true; |
|
174 | - |
|
175 | - /** |
|
176 | - * @var ObjectIdentifier |
|
177 | - */ |
|
178 | - private $object_identifier; |
|
179 | - |
|
180 | - |
|
181 | - /** |
|
182 | - * @singleton method used to instantiate class object |
|
183 | - * @param EE_Dependency_Map|null $dependency_map |
|
184 | - * @param Mirror|null $mirror |
|
185 | - * @param ClassInterfaceCache|null $class_cache |
|
186 | - * @param ObjectIdentifier|null $object_identifier |
|
187 | - * @return EE_Registry instance |
|
188 | - */ |
|
189 | - public static function instance( |
|
190 | - EE_Dependency_Map $dependency_map = null, |
|
191 | - Mirror $mirror = null, |
|
192 | - ClassInterfaceCache $class_cache = null, |
|
193 | - ObjectIdentifier $object_identifier = null |
|
194 | - ) { |
|
195 | - // check if class object is instantiated |
|
196 | - if (! self::$_instance instanceof EE_Registry |
|
197 | - && $dependency_map instanceof EE_Dependency_Map |
|
198 | - && $mirror instanceof Mirror |
|
199 | - && $class_cache instanceof ClassInterfaceCache |
|
200 | - && $object_identifier instanceof ObjectIdentifier |
|
201 | - ) { |
|
202 | - self::$_instance = new self( |
|
203 | - $dependency_map, |
|
204 | - $mirror, |
|
205 | - $class_cache, |
|
206 | - $object_identifier |
|
207 | - ); |
|
208 | - } |
|
209 | - return self::$_instance; |
|
210 | - } |
|
211 | - |
|
212 | - |
|
213 | - /** |
|
214 | - * protected constructor to prevent direct creation |
|
215 | - * |
|
216 | - * @Constructor |
|
217 | - * @param EE_Dependency_Map $dependency_map |
|
218 | - * @param Mirror $mirror |
|
219 | - * @param ClassInterfaceCache $class_cache |
|
220 | - * @param ObjectIdentifier $object_identifier |
|
221 | - */ |
|
222 | - protected function __construct( |
|
223 | - EE_Dependency_Map $dependency_map, |
|
224 | - Mirror $mirror, |
|
225 | - ClassInterfaceCache $class_cache, |
|
226 | - ObjectIdentifier $object_identifier |
|
227 | - ) { |
|
228 | - $this->_dependency_map = $dependency_map; |
|
229 | - $this->mirror = $mirror; |
|
230 | - $this->class_cache = $class_cache; |
|
231 | - $this->object_identifier = $object_identifier; |
|
232 | - // $registry_container = new RegistryContainer(); |
|
233 | - $this->LIB = new RegistryContainer(); |
|
234 | - $this->addons = new RegistryContainer(); |
|
235 | - $this->modules = new RegistryContainer(); |
|
236 | - $this->shortcodes = new RegistryContainer(); |
|
237 | - $this->widgets = new RegistryContainer(); |
|
238 | - add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
239 | - } |
|
240 | - |
|
241 | - |
|
242 | - /** |
|
243 | - * initialize |
|
244 | - * |
|
245 | - * @throws OutOfBoundsException |
|
246 | - * @throws InvalidArgumentException |
|
247 | - * @throws InvalidInterfaceException |
|
248 | - * @throws InvalidDataTypeException |
|
249 | - * @throws EE_Error |
|
250 | - * @throws ReflectionException |
|
251 | - */ |
|
252 | - public function initialize() |
|
253 | - { |
|
254 | - $this->_class_abbreviations = apply_filters( |
|
255 | - 'FHEE__EE_Registry____construct___class_abbreviations', |
|
256 | - array( |
|
257 | - 'EE_Config' => 'CFG', |
|
258 | - 'EE_Session' => 'SSN', |
|
259 | - 'EE_Capabilities' => 'CAP', |
|
260 | - 'EE_Cart' => 'CART', |
|
261 | - 'EE_Network_Config' => 'NET_CFG', |
|
262 | - 'EE_Request_Handler' => 'REQ', |
|
263 | - 'EE_Message_Resource_Manager' => 'MRM', |
|
264 | - 'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
|
265 | - 'EventEspresso\core\services\assets\Registry' => 'AssetsRegistry', |
|
266 | - ) |
|
267 | - ); |
|
268 | - $this->load_core('Base', array(), true); |
|
269 | - // add our request and response objects to the cache |
|
270 | - $request_loader = $this->_dependency_map->class_loader( |
|
271 | - 'EventEspresso\core\services\request\Request' |
|
272 | - ); |
|
273 | - $this->_set_cached_class( |
|
274 | - $request_loader(), |
|
275 | - 'EventEspresso\core\services\request\Request' |
|
276 | - ); |
|
277 | - $response_loader = $this->_dependency_map->class_loader( |
|
278 | - 'EventEspresso\core\services\request\Response' |
|
279 | - ); |
|
280 | - $this->_set_cached_class( |
|
281 | - $response_loader(), |
|
282 | - 'EventEspresso\core\services\request\Response' |
|
283 | - ); |
|
284 | - add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'init')); |
|
285 | - } |
|
286 | - |
|
287 | - |
|
288 | - /** |
|
289 | - * @return void |
|
290 | - */ |
|
291 | - public function init() |
|
292 | - { |
|
293 | - // Get current page protocol |
|
294 | - $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
|
295 | - // Output admin-ajax.php URL with same protocol as current page |
|
296 | - self::$i18n_js_strings['ajax_url'] = admin_url('admin-ajax.php', $protocol); |
|
297 | - self::$i18n_js_strings['wp_debug'] = defined('WP_DEBUG') ? WP_DEBUG : false; |
|
298 | - } |
|
299 | - |
|
300 | - |
|
301 | - /** |
|
302 | - * localize_i18n_js_strings |
|
303 | - * |
|
304 | - * @return string |
|
305 | - */ |
|
306 | - public static function localize_i18n_js_strings() |
|
307 | - { |
|
308 | - $i18n_js_strings = (array) self::$i18n_js_strings; |
|
309 | - foreach ($i18n_js_strings as $key => $value) { |
|
310 | - if (is_scalar($value)) { |
|
311 | - $i18n_js_strings[ $key ] = html_entity_decode((string) $value, ENT_QUOTES, 'UTF-8'); |
|
312 | - } |
|
313 | - } |
|
314 | - return '/* <![CDATA[ */ var eei18n = ' . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
|
315 | - } |
|
316 | - |
|
317 | - |
|
318 | - /** |
|
319 | - * @param mixed string | EED_Module $module |
|
320 | - * @throws OutOfBoundsException |
|
321 | - * @throws InvalidArgumentException |
|
322 | - * @throws InvalidInterfaceException |
|
323 | - * @throws InvalidDataTypeException |
|
324 | - * @throws EE_Error |
|
325 | - * @throws ReflectionException |
|
326 | - */ |
|
327 | - public function add_module($module) |
|
328 | - { |
|
329 | - if ($module instanceof EED_Module) { |
|
330 | - $module_class = get_class($module); |
|
331 | - $this->modules->{$module_class} = $module; |
|
332 | - } else { |
|
333 | - if (! class_exists('EE_Module_Request_Router', false)) { |
|
334 | - $this->load_core('Module_Request_Router'); |
|
335 | - } |
|
336 | - EE_Module_Request_Router::module_factory($module); |
|
337 | - } |
|
338 | - } |
|
339 | - |
|
340 | - |
|
341 | - /** |
|
342 | - * @param string $module_name |
|
343 | - * @return mixed EED_Module | NULL |
|
344 | - */ |
|
345 | - public function get_module($module_name = '') |
|
346 | - { |
|
347 | - return isset($this->modules->{$module_name}) |
|
348 | - ? $this->modules->{$module_name} |
|
349 | - : null; |
|
350 | - } |
|
351 | - |
|
352 | - |
|
353 | - /** |
|
354 | - * loads core classes - must be singletons |
|
355 | - * |
|
356 | - * @param string $class_name - simple class name ie: session |
|
357 | - * @param mixed $arguments |
|
358 | - * @param bool $load_only |
|
359 | - * @return mixed |
|
360 | - * @throws InvalidInterfaceException |
|
361 | - * @throws InvalidDataTypeException |
|
362 | - * @throws EE_Error |
|
363 | - * @throws ReflectionException |
|
364 | - * @throws InvalidArgumentException |
|
365 | - */ |
|
366 | - public function load_core($class_name, $arguments = array(), $load_only = false) |
|
367 | - { |
|
368 | - $core_paths = apply_filters( |
|
369 | - 'FHEE__EE_Registry__load_core__core_paths', |
|
370 | - array( |
|
371 | - EE_CORE, |
|
372 | - EE_ADMIN, |
|
373 | - EE_CPTS, |
|
374 | - EE_CORE . 'data_migration_scripts/', |
|
375 | - EE_CORE . 'capabilities/', |
|
376 | - EE_CORE . 'request_stack/', |
|
377 | - EE_CORE . 'middleware/', |
|
378 | - ) |
|
379 | - ); |
|
380 | - // retrieve instantiated class |
|
381 | - return $this->_load( |
|
382 | - $core_paths, |
|
383 | - 'EE_', |
|
384 | - $class_name, |
|
385 | - 'core', |
|
386 | - $arguments, |
|
387 | - false, |
|
388 | - true, |
|
389 | - $load_only |
|
390 | - ); |
|
391 | - } |
|
392 | - |
|
393 | - |
|
394 | - /** |
|
395 | - * loads service classes |
|
396 | - * |
|
397 | - * @param string $class_name - simple class name ie: session |
|
398 | - * @param mixed $arguments |
|
399 | - * @param bool $load_only |
|
400 | - * @return mixed |
|
401 | - * @throws InvalidInterfaceException |
|
402 | - * @throws InvalidDataTypeException |
|
403 | - * @throws EE_Error |
|
404 | - * @throws ReflectionException |
|
405 | - * @throws InvalidArgumentException |
|
406 | - */ |
|
407 | - public function load_service($class_name, $arguments = array(), $load_only = false) |
|
408 | - { |
|
409 | - $service_paths = apply_filters( |
|
410 | - 'FHEE__EE_Registry__load_service__service_paths', |
|
411 | - array( |
|
412 | - EE_CORE . 'services/', |
|
413 | - ) |
|
414 | - ); |
|
415 | - // retrieve instantiated class |
|
416 | - return $this->_load( |
|
417 | - $service_paths, |
|
418 | - 'EE_', |
|
419 | - $class_name, |
|
420 | - 'class', |
|
421 | - $arguments, |
|
422 | - false, |
|
423 | - true, |
|
424 | - $load_only |
|
425 | - ); |
|
426 | - } |
|
427 | - |
|
428 | - |
|
429 | - /** |
|
430 | - * loads data_migration_scripts |
|
431 | - * |
|
432 | - * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
|
433 | - * @param mixed $arguments |
|
434 | - * @return EE_Data_Migration_Script_Base|mixed |
|
435 | - * @throws InvalidInterfaceException |
|
436 | - * @throws InvalidDataTypeException |
|
437 | - * @throws EE_Error |
|
438 | - * @throws ReflectionException |
|
439 | - * @throws InvalidArgumentException |
|
440 | - */ |
|
441 | - public function load_dms($class_name, $arguments = array()) |
|
442 | - { |
|
443 | - // retrieve instantiated class |
|
444 | - return $this->_load( |
|
445 | - EE_Data_Migration_Manager::instance()->get_data_migration_script_folders(), |
|
446 | - 'EE_DMS_', |
|
447 | - $class_name, |
|
448 | - 'dms', |
|
449 | - $arguments, |
|
450 | - false, |
|
451 | - false |
|
452 | - ); |
|
453 | - } |
|
454 | - |
|
455 | - |
|
456 | - /** |
|
457 | - * loads object creating classes - must be singletons |
|
458 | - * |
|
459 | - * @param string $class_name - simple class name ie: attendee |
|
460 | - * @param mixed $arguments - an array of arguments to pass to the class |
|
461 | - * @param bool $from_db - some classes are instantiated from the db and thus call a different method to |
|
462 | - * instantiate |
|
463 | - * @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then |
|
464 | - * set this to FALSE (ie. when instantiating model objects from client in a loop) |
|
465 | - * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate |
|
466 | - * (default) |
|
467 | - * @return EE_Base_Class | bool |
|
468 | - * @throws InvalidInterfaceException |
|
469 | - * @throws InvalidDataTypeException |
|
470 | - * @throws EE_Error |
|
471 | - * @throws ReflectionException |
|
472 | - * @throws InvalidArgumentException |
|
473 | - */ |
|
474 | - public function load_class($class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false) |
|
475 | - { |
|
476 | - $paths = apply_filters( |
|
477 | - 'FHEE__EE_Registry__load_class__paths', |
|
478 | - array( |
|
479 | - EE_CORE, |
|
480 | - EE_CLASSES, |
|
481 | - EE_BUSINESS, |
|
482 | - ) |
|
483 | - ); |
|
484 | - // retrieve instantiated class |
|
485 | - return $this->_load( |
|
486 | - $paths, |
|
487 | - 'EE_', |
|
488 | - $class_name, |
|
489 | - 'class', |
|
490 | - $arguments, |
|
491 | - $from_db, |
|
492 | - $cache, |
|
493 | - $load_only |
|
494 | - ); |
|
495 | - } |
|
496 | - |
|
497 | - |
|
498 | - /** |
|
499 | - * loads helper classes - must be singletons |
|
500 | - * |
|
501 | - * @param string $class_name - simple class name ie: price |
|
502 | - * @param mixed $arguments |
|
503 | - * @param bool $load_only |
|
504 | - * @return EEH_Base | bool |
|
505 | - * @throws InvalidInterfaceException |
|
506 | - * @throws InvalidDataTypeException |
|
507 | - * @throws EE_Error |
|
508 | - * @throws ReflectionException |
|
509 | - * @throws InvalidArgumentException |
|
510 | - */ |
|
511 | - public function load_helper($class_name, $arguments = array(), $load_only = true) |
|
512 | - { |
|
513 | - // todo: add doing_it_wrong() in a few versions after all addons have had calls to this method removed |
|
514 | - $helper_paths = apply_filters('FHEE__EE_Registry__load_helper__helper_paths', array(EE_HELPERS)); |
|
515 | - // retrieve instantiated class |
|
516 | - return $this->_load( |
|
517 | - $helper_paths, |
|
518 | - 'EEH_', |
|
519 | - $class_name, |
|
520 | - 'helper', |
|
521 | - $arguments, |
|
522 | - false, |
|
523 | - true, |
|
524 | - $load_only |
|
525 | - ); |
|
526 | - } |
|
527 | - |
|
528 | - |
|
529 | - /** |
|
530 | - * loads core classes - must be singletons |
|
531 | - * |
|
532 | - * @param string $class_name - simple class name ie: session |
|
533 | - * @param mixed $arguments |
|
534 | - * @param bool $load_only |
|
535 | - * @param bool $cache whether to cache the object or not. |
|
536 | - * @return mixed |
|
537 | - * @throws InvalidInterfaceException |
|
538 | - * @throws InvalidDataTypeException |
|
539 | - * @throws EE_Error |
|
540 | - * @throws ReflectionException |
|
541 | - * @throws InvalidArgumentException |
|
542 | - */ |
|
543 | - public function load_lib($class_name, $arguments = array(), $load_only = false, $cache = true) |
|
544 | - { |
|
545 | - $paths = array( |
|
546 | - EE_LIBRARIES, |
|
547 | - EE_LIBRARIES . 'messages/', |
|
548 | - EE_LIBRARIES . 'shortcodes/', |
|
549 | - EE_LIBRARIES . 'qtips/', |
|
550 | - EE_LIBRARIES . 'payment_methods/', |
|
551 | - ); |
|
552 | - // retrieve instantiated class |
|
553 | - return $this->_load( |
|
554 | - $paths, |
|
555 | - 'EE_', |
|
556 | - $class_name, |
|
557 | - 'lib', |
|
558 | - $arguments, |
|
559 | - false, |
|
560 | - $cache, |
|
561 | - $load_only |
|
562 | - ); |
|
563 | - } |
|
564 | - |
|
565 | - |
|
566 | - /** |
|
567 | - * loads model classes - must be singletons |
|
568 | - * |
|
569 | - * @param string $class_name - simple class name ie: price |
|
570 | - * @param mixed $arguments |
|
571 | - * @param bool $load_only |
|
572 | - * @return EEM_Base | bool |
|
573 | - * @throws InvalidInterfaceException |
|
574 | - * @throws InvalidDataTypeException |
|
575 | - * @throws EE_Error |
|
576 | - * @throws ReflectionException |
|
577 | - * @throws InvalidArgumentException |
|
578 | - */ |
|
579 | - public function load_model($class_name, $arguments = array(), $load_only = false) |
|
580 | - { |
|
581 | - $paths = apply_filters( |
|
582 | - 'FHEE__EE_Registry__load_model__paths', |
|
583 | - array( |
|
584 | - EE_MODELS, |
|
585 | - EE_CORE, |
|
586 | - ) |
|
587 | - ); |
|
588 | - // retrieve instantiated class |
|
589 | - return $this->_load( |
|
590 | - $paths, |
|
591 | - 'EEM_', |
|
592 | - $class_name, |
|
593 | - 'model', |
|
594 | - $arguments, |
|
595 | - false, |
|
596 | - true, |
|
597 | - $load_only |
|
598 | - ); |
|
599 | - } |
|
600 | - |
|
601 | - |
|
602 | - /** |
|
603 | - * loads model classes - must be singletons |
|
604 | - * |
|
605 | - * @param string $class_name - simple class name ie: price |
|
606 | - * @param mixed $arguments |
|
607 | - * @param bool $load_only |
|
608 | - * @return mixed | bool |
|
609 | - * @throws InvalidInterfaceException |
|
610 | - * @throws InvalidDataTypeException |
|
611 | - * @throws EE_Error |
|
612 | - * @throws ReflectionException |
|
613 | - * @throws InvalidArgumentException |
|
614 | - */ |
|
615 | - public function load_model_class($class_name, $arguments = array(), $load_only = true) |
|
616 | - { |
|
617 | - $paths = array( |
|
618 | - EE_MODELS . 'fields/', |
|
619 | - EE_MODELS . 'helpers/', |
|
620 | - EE_MODELS . 'relations/', |
|
621 | - EE_MODELS . 'strategies/', |
|
622 | - ); |
|
623 | - // retrieve instantiated class |
|
624 | - return $this->_load( |
|
625 | - $paths, |
|
626 | - 'EE_', |
|
627 | - $class_name, |
|
628 | - '', |
|
629 | - $arguments, |
|
630 | - false, |
|
631 | - true, |
|
632 | - $load_only |
|
633 | - ); |
|
634 | - } |
|
635 | - |
|
636 | - |
|
637 | - /** |
|
638 | - * Determines if $model_name is the name of an actual EE model. |
|
639 | - * |
|
640 | - * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
|
641 | - * @return boolean |
|
642 | - */ |
|
643 | - public function is_model_name($model_name) |
|
644 | - { |
|
645 | - return isset($this->models[ $model_name ]); |
|
646 | - } |
|
647 | - |
|
648 | - |
|
649 | - /** |
|
650 | - * generic class loader |
|
651 | - * |
|
652 | - * @param string $path_to_file - directory path to file location, not including filename |
|
653 | - * @param string $file_name - file name ie: my_file.php, including extension |
|
654 | - * @param string $type - file type - core? class? helper? model? |
|
655 | - * @param mixed $arguments |
|
656 | - * @param bool $load_only |
|
657 | - * @return mixed |
|
658 | - * @throws InvalidInterfaceException |
|
659 | - * @throws InvalidDataTypeException |
|
660 | - * @throws EE_Error |
|
661 | - * @throws ReflectionException |
|
662 | - * @throws InvalidArgumentException |
|
663 | - */ |
|
664 | - public function load_file($path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true) |
|
665 | - { |
|
666 | - // retrieve instantiated class |
|
667 | - return $this->_load( |
|
668 | - $path_to_file, |
|
669 | - '', |
|
670 | - $file_name, |
|
671 | - $type, |
|
672 | - $arguments, |
|
673 | - false, |
|
674 | - true, |
|
675 | - $load_only |
|
676 | - ); |
|
677 | - } |
|
678 | - |
|
679 | - |
|
680 | - /** |
|
681 | - * @param string $path_to_file - directory path to file location, not including filename |
|
682 | - * @param string $class_name - full class name ie: My_Class |
|
683 | - * @param string $type - file type - core? class? helper? model? |
|
684 | - * @param mixed $arguments |
|
685 | - * @param bool $load_only |
|
686 | - * @return bool|EE_Addon|object |
|
687 | - * @throws InvalidInterfaceException |
|
688 | - * @throws InvalidDataTypeException |
|
689 | - * @throws EE_Error |
|
690 | - * @throws ReflectionException |
|
691 | - * @throws InvalidArgumentException |
|
692 | - */ |
|
693 | - public function load_addon($path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false) |
|
694 | - { |
|
695 | - // retrieve instantiated class |
|
696 | - return $this->_load( |
|
697 | - $path_to_file, |
|
698 | - 'addon', |
|
699 | - $class_name, |
|
700 | - $type, |
|
701 | - $arguments, |
|
702 | - false, |
|
703 | - true, |
|
704 | - $load_only |
|
705 | - ); |
|
706 | - } |
|
707 | - |
|
708 | - |
|
709 | - /** |
|
710 | - * instantiates, caches, and automatically resolves dependencies |
|
711 | - * for classes that use a Fully Qualified Class Name. |
|
712 | - * if the class is not capable of being loaded using PSR-4 autoloading, |
|
713 | - * then you need to use one of the existing load_*() methods |
|
714 | - * which can resolve the classname and filepath from the passed arguments |
|
715 | - * |
|
716 | - * @param bool|string $class_name Fully Qualified Class Name |
|
717 | - * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
|
718 | - * @param bool $cache whether to cache the instantiated object for reuse |
|
719 | - * @param bool $from_db some classes are instantiated from the db |
|
720 | - * and thus call a different method to instantiate |
|
721 | - * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
722 | - * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
|
723 | - * @return bool|null|mixed null = failure to load or instantiate class object. |
|
724 | - * object = class loaded and instantiated successfully. |
|
725 | - * bool = fail or success when $load_only is true |
|
726 | - * @throws InvalidInterfaceException |
|
727 | - * @throws InvalidDataTypeException |
|
728 | - * @throws EE_Error |
|
729 | - * @throws ReflectionException |
|
730 | - * @throws InvalidArgumentException |
|
731 | - */ |
|
732 | - public function create( |
|
733 | - $class_name = false, |
|
734 | - $arguments = array(), |
|
735 | - $cache = false, |
|
736 | - $from_db = false, |
|
737 | - $load_only = false, |
|
738 | - $addon = false |
|
739 | - ) { |
|
740 | - $class_name = ltrim($class_name, '\\'); |
|
741 | - $class_name = $this->class_cache->getFqnForAlias($class_name); |
|
742 | - $class_exists = $this->loadOrVerifyClassExists($class_name, $arguments); |
|
743 | - // if a non-FQCN was passed, then |
|
744 | - // verifyClassExists() might return an object |
|
745 | - // or it could return null if the class just could not be found anywhere |
|
746 | - if ($class_exists instanceof $class_name || $class_exists === null) { |
|
747 | - // either way, return the results |
|
748 | - return $class_exists; |
|
749 | - } |
|
750 | - $class_name = $class_exists; |
|
751 | - // if we're only loading the class and it already exists, then let's just return true immediately |
|
752 | - if ($load_only) { |
|
753 | - return true; |
|
754 | - } |
|
755 | - $addon = $addon ? 'addon' : ''; |
|
756 | - // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
757 | - // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
758 | - // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
759 | - if ($this->_cache_on && $cache && ! $load_only) { |
|
760 | - // return object if it's already cached |
|
761 | - $cached_class = $this->_get_cached_class($class_name, $addon, $arguments); |
|
762 | - if ($cached_class !== null) { |
|
763 | - return $cached_class; |
|
764 | - } |
|
765 | - }// obtain the loader method from the dependency map |
|
766 | - $loader = $this->_dependency_map->class_loader($class_name);// instantiate the requested object |
|
767 | - if ($loader instanceof Closure) { |
|
768 | - $class_obj = $loader($arguments); |
|
769 | - } else { |
|
770 | - if ($loader && method_exists($this, $loader)) { |
|
771 | - $class_obj = $this->{$loader}($class_name, $arguments); |
|
772 | - } else { |
|
773 | - $class_obj = $this->_create_object($class_name, $arguments, $addon, $from_db); |
|
774 | - } |
|
775 | - } |
|
776 | - if (($this->_cache_on && $cache) || $this->get_class_abbreviation($class_name, '')) { |
|
777 | - // save it for later... kinda like gum { : $ |
|
778 | - $this->_set_cached_class( |
|
779 | - $class_obj, |
|
780 | - $class_name, |
|
781 | - $addon, |
|
782 | - $from_db, |
|
783 | - $arguments |
|
784 | - ); |
|
785 | - } |
|
786 | - $this->_cache_on = true; |
|
787 | - return $class_obj; |
|
788 | - } |
|
789 | - |
|
790 | - |
|
791 | - /** |
|
792 | - * Recursively checks that a class exists and potentially attempts to load classes with non-FQCNs |
|
793 | - * |
|
794 | - * @param string|object $class_name |
|
795 | - * @param array $arguments |
|
796 | - * @param int $attempt |
|
797 | - * @return mixed |
|
798 | - */ |
|
799 | - private function loadOrVerifyClassExists($class_name, array $arguments, $attempt = 1) |
|
800 | - { |
|
801 | - if (is_object($class_name) || class_exists($class_name)) { |
|
802 | - return $class_name; |
|
803 | - } |
|
804 | - switch ($attempt) { |
|
805 | - case 1: |
|
806 | - // if it's a FQCN then maybe the class is registered with a preceding \ |
|
807 | - $class_name = strpos($class_name, '\\') !== false |
|
808 | - ? '\\' . ltrim($class_name, '\\') |
|
809 | - : $class_name; |
|
810 | - break; |
|
811 | - case 2: |
|
812 | - // |
|
813 | - $loader = $this->_dependency_map->class_loader($class_name); |
|
814 | - if ($loader && method_exists($this, $loader)) { |
|
815 | - return $this->{$loader}($class_name, $arguments); |
|
816 | - } |
|
817 | - break; |
|
818 | - case 3: |
|
819 | - default: |
|
820 | - return null; |
|
821 | - } |
|
822 | - $attempt++; |
|
823 | - return $this->loadOrVerifyClassExists($class_name, $arguments, $attempt); |
|
824 | - } |
|
825 | - |
|
826 | - |
|
827 | - /** |
|
828 | - * instantiates, caches, and injects dependencies for classes |
|
829 | - * |
|
830 | - * @param array $file_paths an array of paths to folders to look in |
|
831 | - * @param string $class_prefix EE or EEM or... ??? |
|
832 | - * @param bool|string $class_name $class name |
|
833 | - * @param string $type file type - core? class? helper? model? |
|
834 | - * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
|
835 | - * @param bool $from_db some classes are instantiated from the db |
|
836 | - * and thus call a different method to instantiate |
|
837 | - * @param bool $cache whether to cache the instantiated object for reuse |
|
838 | - * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
839 | - * @return bool|null|object null = failure to load or instantiate class object. |
|
840 | - * object = class loaded and instantiated successfully. |
|
841 | - * bool = fail or success when $load_only is true |
|
842 | - * @throws EE_Error |
|
843 | - * @throws ReflectionException |
|
844 | - * @throws InvalidInterfaceException |
|
845 | - * @throws InvalidDataTypeException |
|
846 | - * @throws InvalidArgumentException |
|
847 | - */ |
|
848 | - protected function _load( |
|
849 | - $file_paths = array(), |
|
850 | - $class_prefix = 'EE_', |
|
851 | - $class_name = false, |
|
852 | - $type = 'class', |
|
853 | - $arguments = array(), |
|
854 | - $from_db = false, |
|
855 | - $cache = true, |
|
856 | - $load_only = false |
|
857 | - ) { |
|
858 | - $class_name = ltrim($class_name, '\\'); |
|
859 | - // strip php file extension |
|
860 | - $class_name = str_replace('.php', '', trim($class_name)); |
|
861 | - // does the class have a prefix ? |
|
862 | - if (! empty($class_prefix) && $class_prefix !== 'addon') { |
|
863 | - // make sure $class_prefix is uppercase |
|
864 | - $class_prefix = strtoupper(trim($class_prefix)); |
|
865 | - // add class prefix ONCE!!! |
|
866 | - $class_name = $class_prefix . str_replace($class_prefix, '', $class_name); |
|
867 | - } |
|
868 | - $class_name = $this->class_cache->getFqnForAlias($class_name); |
|
869 | - $class_exists = class_exists($class_name, false); |
|
870 | - // if we're only loading the class and it already exists, then let's just return true immediately |
|
871 | - if ($load_only && $class_exists) { |
|
872 | - return true; |
|
873 | - } |
|
874 | - $arguments = is_array($arguments) ? $arguments : array($arguments); |
|
875 | - // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
876 | - // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
877 | - // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
878 | - if ($this->_cache_on && $cache && ! $load_only) { |
|
879 | - // return object if it's already cached |
|
880 | - $cached_class = $this->_get_cached_class($class_name, $class_prefix, $arguments); |
|
881 | - if ($cached_class !== null) { |
|
882 | - return $cached_class; |
|
883 | - } |
|
884 | - } |
|
885 | - // if the class doesn't already exist.. then we need to try and find the file and load it |
|
886 | - if (! $class_exists) { |
|
887 | - // get full path to file |
|
888 | - $path = $this->_resolve_path($class_name, $type, $file_paths); |
|
889 | - // load the file |
|
890 | - $loaded = $this->_require_file($path, $class_name, $type, $file_paths); |
|
891 | - // if we are only loading a file but NOT instantiating an object |
|
892 | - // then return boolean for whether class was loaded or not |
|
893 | - if ($load_only) { |
|
894 | - return $loaded; |
|
895 | - } |
|
896 | - // if an object was expected but loading failed, then return nothing |
|
897 | - if (! $loaded) { |
|
898 | - return null; |
|
899 | - } |
|
900 | - } |
|
901 | - // instantiate the requested object |
|
902 | - $class_obj = $this->_create_object($class_name, $arguments, $type, $from_db); |
|
903 | - if ($this->_cache_on && $cache) { |
|
904 | - // save it for later... kinda like gum { : $ |
|
905 | - $this->_set_cached_class( |
|
906 | - $class_obj, |
|
907 | - $class_name, |
|
908 | - $class_prefix, |
|
909 | - $from_db, |
|
910 | - $arguments |
|
911 | - ); |
|
912 | - } |
|
913 | - $this->_cache_on = true; |
|
914 | - return $class_obj; |
|
915 | - } |
|
916 | - |
|
917 | - |
|
918 | - /** |
|
919 | - * @param string $class_name |
|
920 | - * @param string $default have to specify something, but not anything that will conflict |
|
921 | - * @return mixed|string |
|
922 | - */ |
|
923 | - protected function get_class_abbreviation($class_name, $default = 'FANCY_BATMAN_PANTS') |
|
924 | - { |
|
925 | - return isset($this->_class_abbreviations[ $class_name ]) |
|
926 | - ? $this->_class_abbreviations[ $class_name ] |
|
927 | - : $default; |
|
928 | - } |
|
929 | - |
|
930 | - |
|
931 | - /** |
|
932 | - * attempts to find a cached version of the requested class |
|
933 | - * by looking in the following places: |
|
934 | - * $this->{$class_abbreviation} ie: $this->CART |
|
935 | - * $this->{$class_name} ie: $this->Some_Class |
|
936 | - * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
937 | - * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
|
938 | - * |
|
939 | - * @param string $class_name |
|
940 | - * @param string $class_prefix |
|
941 | - * @param array $arguments |
|
942 | - * @return mixed |
|
943 | - */ |
|
944 | - protected function _get_cached_class( |
|
945 | - $class_name, |
|
946 | - $class_prefix = '', |
|
947 | - $arguments = array() |
|
948 | - ) { |
|
949 | - if ($class_name === 'EE_Registry') { |
|
950 | - return $this; |
|
951 | - } |
|
952 | - $class_abbreviation = $this->get_class_abbreviation($class_name); |
|
953 | - // check if class has already been loaded, and return it if it has been |
|
954 | - if (isset($this->{$class_abbreviation})) { |
|
955 | - return $this->{$class_abbreviation}; |
|
956 | - } |
|
957 | - $class_name = str_replace('\\', '_', $class_name); |
|
958 | - if (isset($this->{$class_name})) { |
|
959 | - return $this->{$class_name}; |
|
960 | - } |
|
961 | - if ($class_prefix === 'addon' && isset($this->addons->{$class_name})) { |
|
962 | - return $this->addons->{$class_name}; |
|
963 | - } |
|
964 | - $object_identifier = $this->object_identifier->getIdentifier($class_name, $arguments); |
|
965 | - if (isset($this->LIB->{$object_identifier})) { |
|
966 | - return $this->LIB->{$object_identifier}; |
|
967 | - } |
|
968 | - foreach ($this->LIB as $key => $object) { |
|
969 | - if (// request does not contain new arguments and therefore no args identifier |
|
970 | - ! $this->object_identifier->hasArguments($object_identifier) |
|
971 | - // but previously cached class with args was found |
|
972 | - && $this->object_identifier->fqcnMatchesObjectIdentifier($class_name, $key) |
|
973 | - ) { |
|
974 | - return $object; |
|
975 | - } |
|
976 | - } |
|
977 | - return null; |
|
978 | - } |
|
979 | - |
|
980 | - |
|
981 | - /** |
|
982 | - * removes a cached version of the requested class |
|
983 | - * |
|
984 | - * @param string $class_name |
|
985 | - * @param boolean $addon |
|
986 | - * @param array $arguments |
|
987 | - * @return boolean |
|
988 | - */ |
|
989 | - public function clear_cached_class( |
|
990 | - $class_name, |
|
991 | - $addon = false, |
|
992 | - $arguments = array() |
|
993 | - ) { |
|
994 | - $class_abbreviation = $this->get_class_abbreviation($class_name); |
|
995 | - // check if class has already been loaded, and return it if it has been |
|
996 | - if (isset($this->{$class_abbreviation})) { |
|
997 | - $this->{$class_abbreviation} = null; |
|
998 | - return true; |
|
999 | - } |
|
1000 | - $class_name = str_replace('\\', '_', $class_name); |
|
1001 | - if (isset($this->{$class_name})) { |
|
1002 | - $this->{$class_name} = null; |
|
1003 | - return true; |
|
1004 | - } |
|
1005 | - if ($addon && isset($this->addons->{$class_name})) { |
|
1006 | - unset($this->addons->{$class_name}); |
|
1007 | - return true; |
|
1008 | - } |
|
1009 | - $class_name = $this->object_identifier->getIdentifier($class_name, $arguments); |
|
1010 | - if (isset($this->LIB->{$class_name})) { |
|
1011 | - unset($this->LIB->{$class_name}); |
|
1012 | - return true; |
|
1013 | - } |
|
1014 | - return false; |
|
1015 | - } |
|
1016 | - |
|
1017 | - |
|
1018 | - /** |
|
1019 | - * _set_cached_class |
|
1020 | - * attempts to cache the instantiated class locally |
|
1021 | - * in one of the following places, in the following order: |
|
1022 | - * $this->{class_abbreviation} ie: $this->CART |
|
1023 | - * $this->{$class_name} ie: $this->Some_Class |
|
1024 | - * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
|
1025 | - * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
1026 | - * |
|
1027 | - * @param object $class_obj |
|
1028 | - * @param string $class_name |
|
1029 | - * @param string $class_prefix |
|
1030 | - * @param bool $from_db |
|
1031 | - * @param array $arguments |
|
1032 | - * @return void |
|
1033 | - */ |
|
1034 | - protected function _set_cached_class( |
|
1035 | - $class_obj, |
|
1036 | - $class_name, |
|
1037 | - $class_prefix = '', |
|
1038 | - $from_db = false, |
|
1039 | - $arguments = array() |
|
1040 | - ) { |
|
1041 | - if ($class_name === 'EE_Registry' || empty($class_obj)) { |
|
1042 | - return; |
|
1043 | - } |
|
1044 | - // return newly instantiated class |
|
1045 | - $class_abbreviation = $this->get_class_abbreviation($class_name, ''); |
|
1046 | - if ($class_abbreviation) { |
|
1047 | - $this->{$class_abbreviation} = $class_obj; |
|
1048 | - return; |
|
1049 | - } |
|
1050 | - $class_name = str_replace('\\', '_', $class_name); |
|
1051 | - if (property_exists($this, $class_name)) { |
|
1052 | - $this->{$class_name} = $class_obj; |
|
1053 | - return; |
|
1054 | - } |
|
1055 | - if ($class_prefix === 'addon') { |
|
1056 | - $this->addons->{$class_name} = $class_obj; |
|
1057 | - return; |
|
1058 | - } |
|
1059 | - if (! $from_db) { |
|
1060 | - $class_name = $this->object_identifier->getIdentifier($class_name, $arguments); |
|
1061 | - $this->LIB->{$class_name} = $class_obj; |
|
1062 | - } |
|
1063 | - } |
|
1064 | - |
|
1065 | - |
|
1066 | - /** |
|
1067 | - * attempts to find a full valid filepath for the requested class. |
|
1068 | - * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
|
1069 | - * then returns that path if the target file has been found and is readable |
|
1070 | - * |
|
1071 | - * @param string $class_name |
|
1072 | - * @param string $type |
|
1073 | - * @param array $file_paths |
|
1074 | - * @return string | bool |
|
1075 | - */ |
|
1076 | - protected function _resolve_path($class_name, $type = '', $file_paths = array()) |
|
1077 | - { |
|
1078 | - // make sure $file_paths is an array |
|
1079 | - $file_paths = is_array($file_paths) |
|
1080 | - ? $file_paths |
|
1081 | - : array($file_paths); |
|
1082 | - // cycle thru paths |
|
1083 | - foreach ($file_paths as $key => $file_path) { |
|
1084 | - // convert all separators to proper /, if no filepath, then use EE_CLASSES |
|
1085 | - $file_path = $file_path |
|
1086 | - ? str_replace(array('/', '\\'), '/', $file_path) |
|
1087 | - : EE_CLASSES; |
|
1088 | - // prep file type |
|
1089 | - $type = ! empty($type) |
|
1090 | - ? trim($type, '.') . '.' |
|
1091 | - : ''; |
|
1092 | - // build full file path |
|
1093 | - $file_paths[ $key ] = rtrim($file_path, '/') . '/' . $class_name . '.' . $type . 'php'; |
|
1094 | - // does the file exist and can be read ? |
|
1095 | - if (is_readable($file_paths[ $key ])) { |
|
1096 | - return $file_paths[ $key ]; |
|
1097 | - } |
|
1098 | - } |
|
1099 | - return false; |
|
1100 | - } |
|
1101 | - |
|
1102 | - |
|
1103 | - /** |
|
1104 | - * basically just performs a require_once() |
|
1105 | - * but with some error handling |
|
1106 | - * |
|
1107 | - * @param string $path |
|
1108 | - * @param string $class_name |
|
1109 | - * @param string $type |
|
1110 | - * @param array $file_paths |
|
1111 | - * @return bool |
|
1112 | - * @throws EE_Error |
|
1113 | - * @throws ReflectionException |
|
1114 | - */ |
|
1115 | - protected function _require_file($path, $class_name, $type = '', $file_paths = array()) |
|
1116 | - { |
|
1117 | - $this->resolve_legacy_class_parent($class_name); |
|
1118 | - // don't give up! you gotta... |
|
1119 | - try { |
|
1120 | - // does the file exist and can it be read ? |
|
1121 | - if (! $path) { |
|
1122 | - // just in case the file has already been autoloaded, |
|
1123 | - // but discrepancies in the naming schema are preventing it from |
|
1124 | - // being loaded via one of the EE_Registry::load_*() methods, |
|
1125 | - // then let's try one last hail mary before throwing an exception |
|
1126 | - // and call class_exists() again, but with autoloading turned ON |
|
1127 | - if (class_exists($class_name)) { |
|
1128 | - return true; |
|
1129 | - } |
|
1130 | - // so sorry, can't find the file |
|
1131 | - throw new EE_Error( |
|
1132 | - sprintf( |
|
1133 | - esc_html__( |
|
1134 | - 'The %1$s file %2$s could not be located or is not readable due to file permissions. Please ensure that the following filepath(s) are correct: %3$s', |
|
1135 | - 'event_espresso' |
|
1136 | - ), |
|
1137 | - trim($type, '.'), |
|
1138 | - $class_name, |
|
1139 | - '<br />' . implode(',<br />', $file_paths) |
|
1140 | - ) |
|
1141 | - ); |
|
1142 | - } |
|
1143 | - // get the file |
|
1144 | - require_once($path); |
|
1145 | - // if the class isn't already declared somewhere |
|
1146 | - if (class_exists($class_name, false) === false) { |
|
1147 | - // so sorry, not a class |
|
1148 | - throw new EE_Error( |
|
1149 | - sprintf( |
|
1150 | - esc_html__( |
|
1151 | - 'The %s file %s does not appear to contain the %s Class.', |
|
1152 | - 'event_espresso' |
|
1153 | - ), |
|
1154 | - $type, |
|
1155 | - $path, |
|
1156 | - $class_name |
|
1157 | - ) |
|
1158 | - ); |
|
1159 | - } |
|
1160 | - } catch (EE_Error $e) { |
|
1161 | - $e->get_error(); |
|
1162 | - return false; |
|
1163 | - } |
|
1164 | - return true; |
|
1165 | - } |
|
1166 | - |
|
1167 | - |
|
1168 | - /** |
|
1169 | - * Some of our legacy classes that extended a parent class would simply use a require() statement |
|
1170 | - * before their class declaration in order to ensure that the parent class was loaded. |
|
1171 | - * This is not ideal, but it's nearly impossible to determine the parent class of a non-namespaced class, |
|
1172 | - * without triggering a fatal error because the parent class has yet to be loaded and therefore doesn't exist. |
|
1173 | - * |
|
1174 | - * @param string $class_name |
|
1175 | - */ |
|
1176 | - protected function resolve_legacy_class_parent($class_name = '') |
|
1177 | - { |
|
1178 | - try { |
|
1179 | - $legacy_parent_class_map = array( |
|
1180 | - 'EE_Payment_Processor' => 'core/business/EE_Processor_Base.class.php', |
|
1181 | - ); |
|
1182 | - if (isset($legacy_parent_class_map[ $class_name ])) { |
|
1183 | - require_once EE_PLUGIN_DIR_PATH . $legacy_parent_class_map[ $class_name ]; |
|
1184 | - } |
|
1185 | - } catch (Exception $exception) { |
|
1186 | - } |
|
1187 | - } |
|
1188 | - |
|
1189 | - |
|
1190 | - /** |
|
1191 | - * _create_object |
|
1192 | - * Attempts to instantiate the requested class via any of the |
|
1193 | - * commonly used instantiation methods employed throughout EE. |
|
1194 | - * The priority for instantiation is as follows: |
|
1195 | - * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
|
1196 | - * - model objects via their 'new_instance_from_db' method |
|
1197 | - * - model objects via their 'new_instance' method |
|
1198 | - * - "singleton" classes" via their 'instance' method |
|
1199 | - * - standard instantiable classes via their __constructor |
|
1200 | - * Prior to instantiation, if the classname exists in the dependency_map, |
|
1201 | - * then the constructor for the requested class will be examined to determine |
|
1202 | - * if any dependencies exist, and if they can be injected. |
|
1203 | - * If so, then those classes will be added to the array of arguments passed to the constructor |
|
1204 | - * |
|
1205 | - * @param string $class_name |
|
1206 | - * @param array $arguments |
|
1207 | - * @param string $type |
|
1208 | - * @param bool $from_db |
|
1209 | - * @return null|object|bool |
|
1210 | - * @throws InvalidArgumentException |
|
1211 | - * @throws InvalidInterfaceException |
|
1212 | - * @throws EE_Error |
|
1213 | - * @throws ReflectionException |
|
1214 | - * @throws InvalidDataTypeException |
|
1215 | - */ |
|
1216 | - protected function _create_object($class_name, $arguments = array(), $type = '', $from_db = false) |
|
1217 | - { |
|
1218 | - // create reflection |
|
1219 | - $reflector = $this->mirror->getReflectionClass($class_name); |
|
1220 | - // make sure arguments are an array |
|
1221 | - $arguments = is_array($arguments) |
|
1222 | - ? $arguments |
|
1223 | - : array($arguments); |
|
1224 | - // and if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
|
1225 | - // else wrap it in an additional array so that it doesn't get split into multiple parameters |
|
1226 | - $arguments = $this->_array_is_numerically_and_sequentially_indexed($arguments) |
|
1227 | - ? $arguments |
|
1228 | - : array($arguments); |
|
1229 | - // attempt to inject dependencies ? |
|
1230 | - if ($this->_dependency_map->has($class_name)) { |
|
1231 | - $arguments = $this->_resolve_dependencies($reflector, $class_name, $arguments); |
|
1232 | - } |
|
1233 | - // instantiate the class if possible |
|
1234 | - if ($reflector->isAbstract()) { |
|
1235 | - // nothing to instantiate, loading file was enough |
|
1236 | - // does not throw an exception so $instantiation_mode is unused |
|
1237 | - // $instantiation_mode = "1) no constructor abstract class"; |
|
1238 | - return true; |
|
1239 | - } |
|
1240 | - if (empty($arguments) |
|
1241 | - && $this->mirror->getConstructorFromReflection($reflector) === null |
|
1242 | - && $reflector->isInstantiable() |
|
1243 | - ) { |
|
1244 | - // no constructor = static methods only... nothing to instantiate, loading file was enough |
|
1245 | - // $instantiation_mode = "2) no constructor but instantiable"; |
|
1246 | - return $reflector->newInstance(); |
|
1247 | - } |
|
1248 | - if ($from_db && method_exists($class_name, 'new_instance_from_db')) { |
|
1249 | - // $instantiation_mode = "3) new_instance_from_db()"; |
|
1250 | - return call_user_func_array(array($class_name, 'new_instance_from_db'), $arguments); |
|
1251 | - } |
|
1252 | - if (method_exists($class_name, 'new_instance')) { |
|
1253 | - // $instantiation_mode = "4) new_instance()"; |
|
1254 | - return call_user_func_array(array($class_name, 'new_instance'), $arguments); |
|
1255 | - } |
|
1256 | - if (method_exists($class_name, 'instance')) { |
|
1257 | - // $instantiation_mode = "5) instance()"; |
|
1258 | - return call_user_func_array(array($class_name, 'instance'), $arguments); |
|
1259 | - } |
|
1260 | - if ($reflector->isInstantiable()) { |
|
1261 | - // $instantiation_mode = "6) constructor"; |
|
1262 | - return $reflector->newInstanceArgs($arguments); |
|
1263 | - } |
|
1264 | - // heh ? something's not right ! |
|
1265 | - throw new EE_Error( |
|
1266 | - sprintf( |
|
1267 | - __('The %s file %s could not be instantiated.', 'event_espresso'), |
|
1268 | - $type, |
|
1269 | - $class_name |
|
1270 | - ) |
|
1271 | - ); |
|
1272 | - } |
|
1273 | - |
|
1274 | - |
|
1275 | - /** |
|
1276 | - * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
|
1277 | - * @param array $array |
|
1278 | - * @return bool |
|
1279 | - */ |
|
1280 | - protected function _array_is_numerically_and_sequentially_indexed(array $array) |
|
1281 | - { |
|
1282 | - return ! empty($array) |
|
1283 | - ? array_keys($array) === range(0, count($array) - 1) |
|
1284 | - : true; |
|
1285 | - } |
|
1286 | - |
|
1287 | - |
|
1288 | - /** |
|
1289 | - * _resolve_dependencies |
|
1290 | - * examines the constructor for the requested class to determine |
|
1291 | - * if any dependencies exist, and if they can be injected. |
|
1292 | - * If so, then those classes will be added to the array of arguments passed to the constructor |
|
1293 | - * PLZ NOTE: this is achieved by type hinting the constructor params |
|
1294 | - * For example: |
|
1295 | - * if attempting to load a class "Foo" with the following constructor: |
|
1296 | - * __construct( Bar $bar_class, Fighter $grohl_class ) |
|
1297 | - * then $bar_class and $grohl_class will be added to the $arguments array, |
|
1298 | - * but only IF they are NOT already present in the incoming arguments array, |
|
1299 | - * and the correct classes can be loaded |
|
1300 | - * |
|
1301 | - * @param ReflectionClass $reflector |
|
1302 | - * @param string $class_name |
|
1303 | - * @param array $arguments |
|
1304 | - * @return array |
|
1305 | - * @throws InvalidArgumentException |
|
1306 | - * @throws InvalidDataTypeException |
|
1307 | - * @throws InvalidInterfaceException |
|
1308 | - * @throws ReflectionException |
|
1309 | - */ |
|
1310 | - protected function _resolve_dependencies(ReflectionClass $reflector, $class_name, array $arguments = array()) |
|
1311 | - { |
|
1312 | - // let's examine the constructor |
|
1313 | - $constructor = $this->mirror->getConstructorFromReflection($reflector); |
|
1314 | - // whu? huh? nothing? |
|
1315 | - if (! $constructor) { |
|
1316 | - return $arguments; |
|
1317 | - } |
|
1318 | - // get constructor parameters |
|
1319 | - $params = $this->mirror->getParametersFromReflection($reflector); |
|
1320 | - // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
|
1321 | - $argument_keys = array_keys($arguments); |
|
1322 | - // now loop thru all of the constructors expected parameters |
|
1323 | - foreach ($params as $index => $param) { |
|
1324 | - try { |
|
1325 | - // is this a dependency for a specific class ? |
|
1326 | - $param_class = $this->mirror->getParameterClassName($param, $class_name, $index); |
|
1327 | - } catch (ReflectionException $exception) { |
|
1328 | - // uh-oh... most likely a legacy class that has not been autoloaded |
|
1329 | - // let's try to derive the classname from what we have now |
|
1330 | - // and hope that the property var name is close to the class name |
|
1331 | - $param_class = $param->getName(); |
|
1332 | - $param_class = str_replace('_', ' ', $param_class); |
|
1333 | - $param_class = ucwords($param_class); |
|
1334 | - $param_class = str_replace(' ', '_', $param_class); |
|
1335 | - } |
|
1336 | - // BUT WAIT !!! This class may be an alias for something else (or getting replaced at runtime) |
|
1337 | - $param_class = $this->class_cache->isAlias($param_class, $class_name) |
|
1338 | - ? $this->class_cache->getFqnForAlias($param_class, $class_name) |
|
1339 | - : $param_class; |
|
1340 | - if (// param is not even a class |
|
1341 | - $param_class === null |
|
1342 | - // and something already exists in the incoming arguments for this param |
|
1343 | - && array_key_exists($index, $argument_keys) |
|
1344 | - && array_key_exists($argument_keys[ $index ], $arguments) |
|
1345 | - ) { |
|
1346 | - // so let's skip this argument and move on to the next |
|
1347 | - continue; |
|
1348 | - } |
|
1349 | - if (// parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
|
1350 | - $param_class !== null |
|
1351 | - && isset($argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ]) |
|
1352 | - && $arguments[ $argument_keys[ $index ] ] instanceof $param_class |
|
1353 | - ) { |
|
1354 | - // skip this argument and move on to the next |
|
1355 | - continue; |
|
1356 | - } |
|
1357 | - if (// parameter is type hinted as a class, and should be injected |
|
1358 | - $param_class !== null |
|
1359 | - && $this->_dependency_map->has_dependency_for_class($class_name, $param_class) |
|
1360 | - ) { |
|
1361 | - $arguments = $this->_resolve_dependency( |
|
1362 | - $class_name, |
|
1363 | - $param_class, |
|
1364 | - $arguments, |
|
1365 | - $index |
|
1366 | - ); |
|
1367 | - } |
|
1368 | - if (empty($arguments[ $index ])) { |
|
1369 | - $arguments[ $index ] = $this->mirror->getParameterDefaultValue( |
|
1370 | - $param, |
|
1371 | - $class_name, |
|
1372 | - $index |
|
1373 | - ); |
|
1374 | - } |
|
1375 | - } |
|
1376 | - return $arguments; |
|
1377 | - } |
|
1378 | - |
|
1379 | - |
|
1380 | - /** |
|
1381 | - * @param string $class_name |
|
1382 | - * @param string $param_class |
|
1383 | - * @param array $arguments |
|
1384 | - * @param mixed $index |
|
1385 | - * @return array |
|
1386 | - * @throws InvalidArgumentException |
|
1387 | - * @throws InvalidInterfaceException |
|
1388 | - * @throws InvalidDataTypeException |
|
1389 | - */ |
|
1390 | - protected function _resolve_dependency($class_name, $param_class, $arguments, $index) |
|
1391 | - { |
|
1392 | - $dependency = null; |
|
1393 | - // should dependency be loaded from cache ? |
|
1394 | - $cache_on = $this->_dependency_map->loading_strategy_for_class_dependency( |
|
1395 | - $class_name, |
|
1396 | - $param_class |
|
1397 | - ); |
|
1398 | - $cache_on = $cache_on !== EE_Dependency_Map::load_new_object; |
|
1399 | - // we might have a dependency... |
|
1400 | - // let's MAYBE try and find it in our cache if that's what's been requested |
|
1401 | - $cached_class = $cache_on |
|
1402 | - ? $this->_get_cached_class($param_class) |
|
1403 | - : null; |
|
1404 | - // and grab it if it exists |
|
1405 | - if ($cached_class instanceof $param_class) { |
|
1406 | - $dependency = $cached_class; |
|
1407 | - } elseif ($param_class !== $class_name) { |
|
1408 | - // obtain the loader method from the dependency map |
|
1409 | - $loader = $this->_dependency_map->class_loader($param_class); |
|
1410 | - // is loader a custom closure ? |
|
1411 | - if ($loader instanceof Closure) { |
|
1412 | - $dependency = $loader($arguments); |
|
1413 | - } else { |
|
1414 | - // set the cache on property for the recursive loading call |
|
1415 | - $this->_cache_on = $cache_on; |
|
1416 | - // if not, then let's try and load it via the registry |
|
1417 | - if ($loader && method_exists($this, $loader)) { |
|
1418 | - $dependency = $this->{$loader}($param_class); |
|
1419 | - } else { |
|
1420 | - $dependency = LoaderFactory::getLoader()->load( |
|
1421 | - $param_class, |
|
1422 | - array(), |
|
1423 | - $cache_on |
|
1424 | - ); |
|
1425 | - } |
|
1426 | - } |
|
1427 | - } |
|
1428 | - // did we successfully find the correct dependency ? |
|
1429 | - if ($dependency instanceof $param_class) { |
|
1430 | - // then let's inject it into the incoming array of arguments at the correct location |
|
1431 | - $arguments[ $index ] = $dependency; |
|
1432 | - } |
|
1433 | - return $arguments; |
|
1434 | - } |
|
1435 | - |
|
1436 | - |
|
1437 | - /** |
|
1438 | - * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
|
1439 | - * |
|
1440 | - * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
|
1441 | - * in the EE_Dependency_Map::$_class_loaders array, |
|
1442 | - * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
|
1443 | - * @param array $arguments |
|
1444 | - * @return object |
|
1445 | - */ |
|
1446 | - public static function factory($classname, $arguments = array()) |
|
1447 | - { |
|
1448 | - $loader = self::instance()->_dependency_map->class_loader($classname); |
|
1449 | - if ($loader instanceof Closure) { |
|
1450 | - return $loader($arguments); |
|
1451 | - } |
|
1452 | - if (method_exists(self::instance(), $loader)) { |
|
1453 | - return self::instance()->{$loader}($classname, $arguments); |
|
1454 | - } |
|
1455 | - return null; |
|
1456 | - } |
|
1457 | - |
|
1458 | - |
|
1459 | - /** |
|
1460 | - * Gets the addon by its class name |
|
1461 | - * |
|
1462 | - * @param string $class_name |
|
1463 | - * @return EE_Addon |
|
1464 | - */ |
|
1465 | - public function getAddon($class_name) |
|
1466 | - { |
|
1467 | - $class_name = str_replace('\\', '_', $class_name); |
|
1468 | - if (isset($this->addons->{$class_name})) { |
|
1469 | - return $this->addons->{$class_name}; |
|
1470 | - } else { |
|
1471 | - return null; |
|
1472 | - } |
|
1473 | - } |
|
1474 | - |
|
1475 | - |
|
1476 | - /** |
|
1477 | - * removes the addon from the internal cache |
|
1478 | - * |
|
1479 | - * @param string $class_name |
|
1480 | - * @return void |
|
1481 | - */ |
|
1482 | - public function removeAddon($class_name) |
|
1483 | - { |
|
1484 | - $class_name = str_replace('\\', '_', $class_name); |
|
1485 | - unset($this->addons->{$class_name}); |
|
1486 | - } |
|
1487 | - |
|
1488 | - |
|
1489 | - /** |
|
1490 | - * Gets the addon by its name/slug (not classname. For that, just |
|
1491 | - * use the get_addon() method above |
|
1492 | - * |
|
1493 | - * @param string $name |
|
1494 | - * @return EE_Addon |
|
1495 | - */ |
|
1496 | - public function get_addon_by_name($name) |
|
1497 | - { |
|
1498 | - foreach ($this->addons as $addon) { |
|
1499 | - if ($addon->name() === $name) { |
|
1500 | - return $addon; |
|
1501 | - } |
|
1502 | - } |
|
1503 | - return null; |
|
1504 | - } |
|
1505 | - |
|
1506 | - |
|
1507 | - /** |
|
1508 | - * Gets an array of all the registered addons, where the keys are their names. |
|
1509 | - * (ie, what each returns for their name() function) |
|
1510 | - * They're already available on EE_Registry::instance()->addons as properties, |
|
1511 | - * where each property's name is the addon's classname, |
|
1512 | - * So if you just want to get the addon by classname, |
|
1513 | - * OR use the get_addon() method above. |
|
1514 | - * PLEASE NOTE: |
|
1515 | - * addons with Fully Qualified Class Names |
|
1516 | - * have had the namespace separators converted to underscores, |
|
1517 | - * so a classname like Fully\Qualified\ClassName |
|
1518 | - * would have been converted to Fully_Qualified_ClassName |
|
1519 | - * |
|
1520 | - * @return EE_Addon[] where the KEYS are the addon's name() |
|
1521 | - */ |
|
1522 | - public function get_addons_by_name() |
|
1523 | - { |
|
1524 | - $addons = array(); |
|
1525 | - foreach ($this->addons as $addon) { |
|
1526 | - $addons[ $addon->name() ] = $addon; |
|
1527 | - } |
|
1528 | - return $addons; |
|
1529 | - } |
|
1530 | - |
|
1531 | - |
|
1532 | - /** |
|
1533 | - * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
|
1534 | - * a stale copy of it around |
|
1535 | - * |
|
1536 | - * @param string $model_name |
|
1537 | - * @return \EEM_Base |
|
1538 | - * @throws \EE_Error |
|
1539 | - */ |
|
1540 | - public function reset_model($model_name) |
|
1541 | - { |
|
1542 | - $model_class_name = strpos($model_name, 'EEM_') !== 0 |
|
1543 | - ? "EEM_{$model_name}" |
|
1544 | - : $model_name; |
|
1545 | - if (! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
1546 | - return null; |
|
1547 | - } |
|
1548 | - // get that model reset it and make sure we nuke the old reference to it |
|
1549 | - if ($this->LIB->{$model_class_name} instanceof $model_class_name |
|
1550 | - && is_callable( |
|
1551 | - array($model_class_name, 'reset') |
|
1552 | - )) { |
|
1553 | - $this->LIB->{$model_class_name} = $this->LIB->{$model_class_name}->reset(); |
|
1554 | - } else { |
|
1555 | - throw new EE_Error( |
|
1556 | - sprintf( |
|
1557 | - esc_html__('Model %s does not have a method "reset"', 'event_espresso'), |
|
1558 | - $model_name |
|
1559 | - ) |
|
1560 | - ); |
|
1561 | - } |
|
1562 | - return $this->LIB->{$model_class_name}; |
|
1563 | - } |
|
1564 | - |
|
1565 | - |
|
1566 | - /** |
|
1567 | - * Resets the registry. |
|
1568 | - * The criteria for what gets reset is based on what can be shared between sites on the same request when |
|
1569 | - * switch_to_blog is used in a multisite install. Here is a list of things that are NOT reset. |
|
1570 | - * - $_dependency_map |
|
1571 | - * - $_class_abbreviations |
|
1572 | - * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
|
1573 | - * - $REQ: Still on the same request so no need to change. |
|
1574 | - * - $CAP: There is no site specific state in the EE_Capability class. |
|
1575 | - * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only |
|
1576 | - * one Session can be active in a single request. Resetting could resolve in "headers already sent" errors. |
|
1577 | - * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
|
1578 | - * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
|
1579 | - * switch or on the restore. |
|
1580 | - * - $modules |
|
1581 | - * - $shortcodes |
|
1582 | - * - $widgets |
|
1583 | - * |
|
1584 | - * @param boolean $hard [deprecated] |
|
1585 | - * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
|
1586 | - * or just reset without re-instantiating (handy to set to FALSE if you're not |
|
1587 | - * sure if you CAN currently reinstantiate the singletons at the moment) |
|
1588 | - * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so |
|
1589 | - * client |
|
1590 | - * code instead can just change the model context to a different blog id if |
|
1591 | - * necessary |
|
1592 | - * @return EE_Registry |
|
1593 | - * @throws InvalidInterfaceException |
|
1594 | - * @throws InvalidDataTypeException |
|
1595 | - * @throws EE_Error |
|
1596 | - * @throws ReflectionException |
|
1597 | - * @throws InvalidArgumentException |
|
1598 | - */ |
|
1599 | - public static function reset($hard = false, $reinstantiate = true, $reset_models = true) |
|
1600 | - { |
|
1601 | - $instance = self::instance(); |
|
1602 | - $instance->_cache_on = true; |
|
1603 | - // reset some "special" classes |
|
1604 | - EEH_Activation::reset(); |
|
1605 | - $hard = apply_filters('FHEE__EE_Registry__reset__hard', $hard); |
|
1606 | - $instance->CFG = EE_Config::reset($hard, $reinstantiate); |
|
1607 | - $instance->CART = null; |
|
1608 | - $instance->MRM = null; |
|
1609 | - $instance->AssetsRegistry = LoaderFactory::getLoader()->getShared( |
|
1610 | - 'EventEspresso\core\services\assets\Registry' |
|
1611 | - ); |
|
1612 | - // messages reset |
|
1613 | - EED_Messages::reset(); |
|
1614 | - // handle of objects cached on LIB |
|
1615 | - foreach (array('LIB', 'modules') as $cache) { |
|
1616 | - foreach ($instance->{$cache} as $class_name => $class) { |
|
1617 | - if (self::_reset_and_unset_object($class, $reset_models)) { |
|
1618 | - unset($instance->{$cache}->{$class_name}); |
|
1619 | - } |
|
1620 | - } |
|
1621 | - } |
|
1622 | - return $instance; |
|
1623 | - } |
|
1624 | - |
|
1625 | - |
|
1626 | - /** |
|
1627 | - * if passed object implements ResettableInterface, then call it's reset() method |
|
1628 | - * if passed object implements InterminableInterface, then return false, |
|
1629 | - * to indicate that it should NOT be cleared from the Registry cache |
|
1630 | - * |
|
1631 | - * @param $object |
|
1632 | - * @param bool $reset_models |
|
1633 | - * @return bool returns true if cached object should be unset |
|
1634 | - */ |
|
1635 | - private static function _reset_and_unset_object($object, $reset_models) |
|
1636 | - { |
|
1637 | - if (! is_object($object)) { |
|
1638 | - // don't unset anything that's not an object |
|
1639 | - return false; |
|
1640 | - } |
|
1641 | - if ($object instanceof EED_Module) { |
|
1642 | - $object::reset(); |
|
1643 | - // don't unset modules |
|
1644 | - return false; |
|
1645 | - } |
|
1646 | - if ($object instanceof ResettableInterface) { |
|
1647 | - if ($object instanceof EEM_Base) { |
|
1648 | - if ($reset_models) { |
|
1649 | - $object->reset(); |
|
1650 | - return true; |
|
1651 | - } |
|
1652 | - return false; |
|
1653 | - } |
|
1654 | - $object->reset(); |
|
1655 | - return true; |
|
1656 | - } |
|
1657 | - if (! $object instanceof InterminableInterface) { |
|
1658 | - return true; |
|
1659 | - } |
|
1660 | - return false; |
|
1661 | - } |
|
1662 | - |
|
1663 | - |
|
1664 | - /** |
|
1665 | - * Gets all the custom post type models defined |
|
1666 | - * |
|
1667 | - * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
|
1668 | - */ |
|
1669 | - public function cpt_models() |
|
1670 | - { |
|
1671 | - $cpt_models = array(); |
|
1672 | - foreach ($this->non_abstract_db_models as $short_name => $classname) { |
|
1673 | - if (is_subclass_of($classname, 'EEM_CPT_Base')) { |
|
1674 | - $cpt_models[ $short_name ] = $classname; |
|
1675 | - } |
|
1676 | - } |
|
1677 | - return $cpt_models; |
|
1678 | - } |
|
1679 | - |
|
1680 | - |
|
1681 | - /** |
|
1682 | - * @return \EE_Config |
|
1683 | - */ |
|
1684 | - public static function CFG() |
|
1685 | - { |
|
1686 | - return self::instance()->CFG; |
|
1687 | - } |
|
1688 | - |
|
1689 | - |
|
1690 | - /** |
|
1691 | - * @deprecated 4.9.62.p |
|
1692 | - * @param string $class_name |
|
1693 | - * @return ReflectionClass |
|
1694 | - * @throws ReflectionException |
|
1695 | - * @throws InvalidDataTypeException |
|
1696 | - */ |
|
1697 | - public function get_ReflectionClass($class_name) |
|
1698 | - { |
|
1699 | - return $this->mirror->getReflectionClass($class_name); |
|
1700 | - } |
|
26 | + /** |
|
27 | + * @var EE_Registry $_instance |
|
28 | + */ |
|
29 | + private static $_instance; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var EE_Dependency_Map $_dependency_map |
|
33 | + */ |
|
34 | + protected $_dependency_map; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var Mirror |
|
38 | + */ |
|
39 | + private $mirror; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var ClassInterfaceCache $class_cache |
|
43 | + */ |
|
44 | + private $class_cache; |
|
45 | + |
|
46 | + /** |
|
47 | + * @var array $_class_abbreviations |
|
48 | + */ |
|
49 | + protected $_class_abbreviations = array(); |
|
50 | + |
|
51 | + /** |
|
52 | + * @var CommandBusInterface $BUS |
|
53 | + */ |
|
54 | + public $BUS; |
|
55 | + |
|
56 | + /** |
|
57 | + * @var EE_Cart $CART |
|
58 | + */ |
|
59 | + public $CART; |
|
60 | + |
|
61 | + /** |
|
62 | + * @var EE_Config $CFG |
|
63 | + */ |
|
64 | + public $CFG; |
|
65 | + |
|
66 | + /** |
|
67 | + * @var EE_Network_Config $NET_CFG |
|
68 | + */ |
|
69 | + public $NET_CFG; |
|
70 | + |
|
71 | + /** |
|
72 | + * StdClass object for storing library classes in |
|
73 | + * |
|
74 | + * @var RegistryContainer $LIB |
|
75 | + */ |
|
76 | + public $LIB; |
|
77 | + |
|
78 | + /** |
|
79 | + * @var EE_Request_Handler $REQ |
|
80 | + */ |
|
81 | + public $REQ; |
|
82 | + |
|
83 | + /** |
|
84 | + * @var EE_Session $SSN |
|
85 | + */ |
|
86 | + public $SSN; |
|
87 | + |
|
88 | + /** |
|
89 | + * @since 4.5.0 |
|
90 | + * @var EE_Capabilities $CAP |
|
91 | + */ |
|
92 | + public $CAP; |
|
93 | + |
|
94 | + /** |
|
95 | + * @since 4.9.0 |
|
96 | + * @var EE_Message_Resource_Manager $MRM |
|
97 | + */ |
|
98 | + public $MRM; |
|
99 | + |
|
100 | + /** |
|
101 | + * @var Registry $AssetsRegistry |
|
102 | + */ |
|
103 | + public $AssetsRegistry; |
|
104 | + |
|
105 | + /** |
|
106 | + * StdClass object for holding addons which have registered themselves to work with EE core |
|
107 | + * |
|
108 | + * @var EE_Addon[] $addons |
|
109 | + */ |
|
110 | + public $addons; |
|
111 | + |
|
112 | + /** |
|
113 | + * keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
|
114 | + * |
|
115 | + * @var EEM_Base[] $models |
|
116 | + */ |
|
117 | + public $models = array(); |
|
118 | + |
|
119 | + /** |
|
120 | + * @var EED_Module[] $modules |
|
121 | + */ |
|
122 | + public $modules; |
|
123 | + |
|
124 | + /** |
|
125 | + * @var EES_Shortcode[] $shortcodes |
|
126 | + */ |
|
127 | + public $shortcodes; |
|
128 | + |
|
129 | + /** |
|
130 | + * @var WP_Widget[] $widgets |
|
131 | + */ |
|
132 | + public $widgets; |
|
133 | + |
|
134 | + /** |
|
135 | + * this is an array of all implemented model names (i.e. not the parent abstract models, or models |
|
136 | + * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
|
137 | + * Keys are model "short names" (eg "Event") as used in model relations, and values are |
|
138 | + * classnames (eg "EEM_Event") |
|
139 | + * |
|
140 | + * @var array $non_abstract_db_models |
|
141 | + */ |
|
142 | + public $non_abstract_db_models = array(); |
|
143 | + |
|
144 | + /** |
|
145 | + * internationalization for JS strings |
|
146 | + * usage: EE_Registry::i18n_js_strings['string_key'] = esc_html__( 'string to translate.', 'event_espresso' ); |
|
147 | + * in js file: var translatedString = eei18n.string_key; |
|
148 | + * |
|
149 | + * @var array $i18n_js_strings |
|
150 | + */ |
|
151 | + public static $i18n_js_strings = array(); |
|
152 | + |
|
153 | + /** |
|
154 | + * $main_file - path to espresso.php |
|
155 | + * |
|
156 | + * @var array $main_file |
|
157 | + */ |
|
158 | + public $main_file; |
|
159 | + |
|
160 | + /** |
|
161 | + * array of ReflectionClass objects where the key is the class name |
|
162 | + * |
|
163 | + * @deprecated 4.9.62.p |
|
164 | + * @var ReflectionClass[] $_reflectors |
|
165 | + */ |
|
166 | + public $_reflectors; |
|
167 | + |
|
168 | + /** |
|
169 | + * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
|
170 | + * |
|
171 | + * @var boolean $_cache_on |
|
172 | + */ |
|
173 | + protected $_cache_on = true; |
|
174 | + |
|
175 | + /** |
|
176 | + * @var ObjectIdentifier |
|
177 | + */ |
|
178 | + private $object_identifier; |
|
179 | + |
|
180 | + |
|
181 | + /** |
|
182 | + * @singleton method used to instantiate class object |
|
183 | + * @param EE_Dependency_Map|null $dependency_map |
|
184 | + * @param Mirror|null $mirror |
|
185 | + * @param ClassInterfaceCache|null $class_cache |
|
186 | + * @param ObjectIdentifier|null $object_identifier |
|
187 | + * @return EE_Registry instance |
|
188 | + */ |
|
189 | + public static function instance( |
|
190 | + EE_Dependency_Map $dependency_map = null, |
|
191 | + Mirror $mirror = null, |
|
192 | + ClassInterfaceCache $class_cache = null, |
|
193 | + ObjectIdentifier $object_identifier = null |
|
194 | + ) { |
|
195 | + // check if class object is instantiated |
|
196 | + if (! self::$_instance instanceof EE_Registry |
|
197 | + && $dependency_map instanceof EE_Dependency_Map |
|
198 | + && $mirror instanceof Mirror |
|
199 | + && $class_cache instanceof ClassInterfaceCache |
|
200 | + && $object_identifier instanceof ObjectIdentifier |
|
201 | + ) { |
|
202 | + self::$_instance = new self( |
|
203 | + $dependency_map, |
|
204 | + $mirror, |
|
205 | + $class_cache, |
|
206 | + $object_identifier |
|
207 | + ); |
|
208 | + } |
|
209 | + return self::$_instance; |
|
210 | + } |
|
211 | + |
|
212 | + |
|
213 | + /** |
|
214 | + * protected constructor to prevent direct creation |
|
215 | + * |
|
216 | + * @Constructor |
|
217 | + * @param EE_Dependency_Map $dependency_map |
|
218 | + * @param Mirror $mirror |
|
219 | + * @param ClassInterfaceCache $class_cache |
|
220 | + * @param ObjectIdentifier $object_identifier |
|
221 | + */ |
|
222 | + protected function __construct( |
|
223 | + EE_Dependency_Map $dependency_map, |
|
224 | + Mirror $mirror, |
|
225 | + ClassInterfaceCache $class_cache, |
|
226 | + ObjectIdentifier $object_identifier |
|
227 | + ) { |
|
228 | + $this->_dependency_map = $dependency_map; |
|
229 | + $this->mirror = $mirror; |
|
230 | + $this->class_cache = $class_cache; |
|
231 | + $this->object_identifier = $object_identifier; |
|
232 | + // $registry_container = new RegistryContainer(); |
|
233 | + $this->LIB = new RegistryContainer(); |
|
234 | + $this->addons = new RegistryContainer(); |
|
235 | + $this->modules = new RegistryContainer(); |
|
236 | + $this->shortcodes = new RegistryContainer(); |
|
237 | + $this->widgets = new RegistryContainer(); |
|
238 | + add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
239 | + } |
|
240 | + |
|
241 | + |
|
242 | + /** |
|
243 | + * initialize |
|
244 | + * |
|
245 | + * @throws OutOfBoundsException |
|
246 | + * @throws InvalidArgumentException |
|
247 | + * @throws InvalidInterfaceException |
|
248 | + * @throws InvalidDataTypeException |
|
249 | + * @throws EE_Error |
|
250 | + * @throws ReflectionException |
|
251 | + */ |
|
252 | + public function initialize() |
|
253 | + { |
|
254 | + $this->_class_abbreviations = apply_filters( |
|
255 | + 'FHEE__EE_Registry____construct___class_abbreviations', |
|
256 | + array( |
|
257 | + 'EE_Config' => 'CFG', |
|
258 | + 'EE_Session' => 'SSN', |
|
259 | + 'EE_Capabilities' => 'CAP', |
|
260 | + 'EE_Cart' => 'CART', |
|
261 | + 'EE_Network_Config' => 'NET_CFG', |
|
262 | + 'EE_Request_Handler' => 'REQ', |
|
263 | + 'EE_Message_Resource_Manager' => 'MRM', |
|
264 | + 'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
|
265 | + 'EventEspresso\core\services\assets\Registry' => 'AssetsRegistry', |
|
266 | + ) |
|
267 | + ); |
|
268 | + $this->load_core('Base', array(), true); |
|
269 | + // add our request and response objects to the cache |
|
270 | + $request_loader = $this->_dependency_map->class_loader( |
|
271 | + 'EventEspresso\core\services\request\Request' |
|
272 | + ); |
|
273 | + $this->_set_cached_class( |
|
274 | + $request_loader(), |
|
275 | + 'EventEspresso\core\services\request\Request' |
|
276 | + ); |
|
277 | + $response_loader = $this->_dependency_map->class_loader( |
|
278 | + 'EventEspresso\core\services\request\Response' |
|
279 | + ); |
|
280 | + $this->_set_cached_class( |
|
281 | + $response_loader(), |
|
282 | + 'EventEspresso\core\services\request\Response' |
|
283 | + ); |
|
284 | + add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'init')); |
|
285 | + } |
|
286 | + |
|
287 | + |
|
288 | + /** |
|
289 | + * @return void |
|
290 | + */ |
|
291 | + public function init() |
|
292 | + { |
|
293 | + // Get current page protocol |
|
294 | + $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
|
295 | + // Output admin-ajax.php URL with same protocol as current page |
|
296 | + self::$i18n_js_strings['ajax_url'] = admin_url('admin-ajax.php', $protocol); |
|
297 | + self::$i18n_js_strings['wp_debug'] = defined('WP_DEBUG') ? WP_DEBUG : false; |
|
298 | + } |
|
299 | + |
|
300 | + |
|
301 | + /** |
|
302 | + * localize_i18n_js_strings |
|
303 | + * |
|
304 | + * @return string |
|
305 | + */ |
|
306 | + public static function localize_i18n_js_strings() |
|
307 | + { |
|
308 | + $i18n_js_strings = (array) self::$i18n_js_strings; |
|
309 | + foreach ($i18n_js_strings as $key => $value) { |
|
310 | + if (is_scalar($value)) { |
|
311 | + $i18n_js_strings[ $key ] = html_entity_decode((string) $value, ENT_QUOTES, 'UTF-8'); |
|
312 | + } |
|
313 | + } |
|
314 | + return '/* <![CDATA[ */ var eei18n = ' . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
|
315 | + } |
|
316 | + |
|
317 | + |
|
318 | + /** |
|
319 | + * @param mixed string | EED_Module $module |
|
320 | + * @throws OutOfBoundsException |
|
321 | + * @throws InvalidArgumentException |
|
322 | + * @throws InvalidInterfaceException |
|
323 | + * @throws InvalidDataTypeException |
|
324 | + * @throws EE_Error |
|
325 | + * @throws ReflectionException |
|
326 | + */ |
|
327 | + public function add_module($module) |
|
328 | + { |
|
329 | + if ($module instanceof EED_Module) { |
|
330 | + $module_class = get_class($module); |
|
331 | + $this->modules->{$module_class} = $module; |
|
332 | + } else { |
|
333 | + if (! class_exists('EE_Module_Request_Router', false)) { |
|
334 | + $this->load_core('Module_Request_Router'); |
|
335 | + } |
|
336 | + EE_Module_Request_Router::module_factory($module); |
|
337 | + } |
|
338 | + } |
|
339 | + |
|
340 | + |
|
341 | + /** |
|
342 | + * @param string $module_name |
|
343 | + * @return mixed EED_Module | NULL |
|
344 | + */ |
|
345 | + public function get_module($module_name = '') |
|
346 | + { |
|
347 | + return isset($this->modules->{$module_name}) |
|
348 | + ? $this->modules->{$module_name} |
|
349 | + : null; |
|
350 | + } |
|
351 | + |
|
352 | + |
|
353 | + /** |
|
354 | + * loads core classes - must be singletons |
|
355 | + * |
|
356 | + * @param string $class_name - simple class name ie: session |
|
357 | + * @param mixed $arguments |
|
358 | + * @param bool $load_only |
|
359 | + * @return mixed |
|
360 | + * @throws InvalidInterfaceException |
|
361 | + * @throws InvalidDataTypeException |
|
362 | + * @throws EE_Error |
|
363 | + * @throws ReflectionException |
|
364 | + * @throws InvalidArgumentException |
|
365 | + */ |
|
366 | + public function load_core($class_name, $arguments = array(), $load_only = false) |
|
367 | + { |
|
368 | + $core_paths = apply_filters( |
|
369 | + 'FHEE__EE_Registry__load_core__core_paths', |
|
370 | + array( |
|
371 | + EE_CORE, |
|
372 | + EE_ADMIN, |
|
373 | + EE_CPTS, |
|
374 | + EE_CORE . 'data_migration_scripts/', |
|
375 | + EE_CORE . 'capabilities/', |
|
376 | + EE_CORE . 'request_stack/', |
|
377 | + EE_CORE . 'middleware/', |
|
378 | + ) |
|
379 | + ); |
|
380 | + // retrieve instantiated class |
|
381 | + return $this->_load( |
|
382 | + $core_paths, |
|
383 | + 'EE_', |
|
384 | + $class_name, |
|
385 | + 'core', |
|
386 | + $arguments, |
|
387 | + false, |
|
388 | + true, |
|
389 | + $load_only |
|
390 | + ); |
|
391 | + } |
|
392 | + |
|
393 | + |
|
394 | + /** |
|
395 | + * loads service classes |
|
396 | + * |
|
397 | + * @param string $class_name - simple class name ie: session |
|
398 | + * @param mixed $arguments |
|
399 | + * @param bool $load_only |
|
400 | + * @return mixed |
|
401 | + * @throws InvalidInterfaceException |
|
402 | + * @throws InvalidDataTypeException |
|
403 | + * @throws EE_Error |
|
404 | + * @throws ReflectionException |
|
405 | + * @throws InvalidArgumentException |
|
406 | + */ |
|
407 | + public function load_service($class_name, $arguments = array(), $load_only = false) |
|
408 | + { |
|
409 | + $service_paths = apply_filters( |
|
410 | + 'FHEE__EE_Registry__load_service__service_paths', |
|
411 | + array( |
|
412 | + EE_CORE . 'services/', |
|
413 | + ) |
|
414 | + ); |
|
415 | + // retrieve instantiated class |
|
416 | + return $this->_load( |
|
417 | + $service_paths, |
|
418 | + 'EE_', |
|
419 | + $class_name, |
|
420 | + 'class', |
|
421 | + $arguments, |
|
422 | + false, |
|
423 | + true, |
|
424 | + $load_only |
|
425 | + ); |
|
426 | + } |
|
427 | + |
|
428 | + |
|
429 | + /** |
|
430 | + * loads data_migration_scripts |
|
431 | + * |
|
432 | + * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
|
433 | + * @param mixed $arguments |
|
434 | + * @return EE_Data_Migration_Script_Base|mixed |
|
435 | + * @throws InvalidInterfaceException |
|
436 | + * @throws InvalidDataTypeException |
|
437 | + * @throws EE_Error |
|
438 | + * @throws ReflectionException |
|
439 | + * @throws InvalidArgumentException |
|
440 | + */ |
|
441 | + public function load_dms($class_name, $arguments = array()) |
|
442 | + { |
|
443 | + // retrieve instantiated class |
|
444 | + return $this->_load( |
|
445 | + EE_Data_Migration_Manager::instance()->get_data_migration_script_folders(), |
|
446 | + 'EE_DMS_', |
|
447 | + $class_name, |
|
448 | + 'dms', |
|
449 | + $arguments, |
|
450 | + false, |
|
451 | + false |
|
452 | + ); |
|
453 | + } |
|
454 | + |
|
455 | + |
|
456 | + /** |
|
457 | + * loads object creating classes - must be singletons |
|
458 | + * |
|
459 | + * @param string $class_name - simple class name ie: attendee |
|
460 | + * @param mixed $arguments - an array of arguments to pass to the class |
|
461 | + * @param bool $from_db - some classes are instantiated from the db and thus call a different method to |
|
462 | + * instantiate |
|
463 | + * @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then |
|
464 | + * set this to FALSE (ie. when instantiating model objects from client in a loop) |
|
465 | + * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate |
|
466 | + * (default) |
|
467 | + * @return EE_Base_Class | bool |
|
468 | + * @throws InvalidInterfaceException |
|
469 | + * @throws InvalidDataTypeException |
|
470 | + * @throws EE_Error |
|
471 | + * @throws ReflectionException |
|
472 | + * @throws InvalidArgumentException |
|
473 | + */ |
|
474 | + public function load_class($class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false) |
|
475 | + { |
|
476 | + $paths = apply_filters( |
|
477 | + 'FHEE__EE_Registry__load_class__paths', |
|
478 | + array( |
|
479 | + EE_CORE, |
|
480 | + EE_CLASSES, |
|
481 | + EE_BUSINESS, |
|
482 | + ) |
|
483 | + ); |
|
484 | + // retrieve instantiated class |
|
485 | + return $this->_load( |
|
486 | + $paths, |
|
487 | + 'EE_', |
|
488 | + $class_name, |
|
489 | + 'class', |
|
490 | + $arguments, |
|
491 | + $from_db, |
|
492 | + $cache, |
|
493 | + $load_only |
|
494 | + ); |
|
495 | + } |
|
496 | + |
|
497 | + |
|
498 | + /** |
|
499 | + * loads helper classes - must be singletons |
|
500 | + * |
|
501 | + * @param string $class_name - simple class name ie: price |
|
502 | + * @param mixed $arguments |
|
503 | + * @param bool $load_only |
|
504 | + * @return EEH_Base | bool |
|
505 | + * @throws InvalidInterfaceException |
|
506 | + * @throws InvalidDataTypeException |
|
507 | + * @throws EE_Error |
|
508 | + * @throws ReflectionException |
|
509 | + * @throws InvalidArgumentException |
|
510 | + */ |
|
511 | + public function load_helper($class_name, $arguments = array(), $load_only = true) |
|
512 | + { |
|
513 | + // todo: add doing_it_wrong() in a few versions after all addons have had calls to this method removed |
|
514 | + $helper_paths = apply_filters('FHEE__EE_Registry__load_helper__helper_paths', array(EE_HELPERS)); |
|
515 | + // retrieve instantiated class |
|
516 | + return $this->_load( |
|
517 | + $helper_paths, |
|
518 | + 'EEH_', |
|
519 | + $class_name, |
|
520 | + 'helper', |
|
521 | + $arguments, |
|
522 | + false, |
|
523 | + true, |
|
524 | + $load_only |
|
525 | + ); |
|
526 | + } |
|
527 | + |
|
528 | + |
|
529 | + /** |
|
530 | + * loads core classes - must be singletons |
|
531 | + * |
|
532 | + * @param string $class_name - simple class name ie: session |
|
533 | + * @param mixed $arguments |
|
534 | + * @param bool $load_only |
|
535 | + * @param bool $cache whether to cache the object or not. |
|
536 | + * @return mixed |
|
537 | + * @throws InvalidInterfaceException |
|
538 | + * @throws InvalidDataTypeException |
|
539 | + * @throws EE_Error |
|
540 | + * @throws ReflectionException |
|
541 | + * @throws InvalidArgumentException |
|
542 | + */ |
|
543 | + public function load_lib($class_name, $arguments = array(), $load_only = false, $cache = true) |
|
544 | + { |
|
545 | + $paths = array( |
|
546 | + EE_LIBRARIES, |
|
547 | + EE_LIBRARIES . 'messages/', |
|
548 | + EE_LIBRARIES . 'shortcodes/', |
|
549 | + EE_LIBRARIES . 'qtips/', |
|
550 | + EE_LIBRARIES . 'payment_methods/', |
|
551 | + ); |
|
552 | + // retrieve instantiated class |
|
553 | + return $this->_load( |
|
554 | + $paths, |
|
555 | + 'EE_', |
|
556 | + $class_name, |
|
557 | + 'lib', |
|
558 | + $arguments, |
|
559 | + false, |
|
560 | + $cache, |
|
561 | + $load_only |
|
562 | + ); |
|
563 | + } |
|
564 | + |
|
565 | + |
|
566 | + /** |
|
567 | + * loads model classes - must be singletons |
|
568 | + * |
|
569 | + * @param string $class_name - simple class name ie: price |
|
570 | + * @param mixed $arguments |
|
571 | + * @param bool $load_only |
|
572 | + * @return EEM_Base | bool |
|
573 | + * @throws InvalidInterfaceException |
|
574 | + * @throws InvalidDataTypeException |
|
575 | + * @throws EE_Error |
|
576 | + * @throws ReflectionException |
|
577 | + * @throws InvalidArgumentException |
|
578 | + */ |
|
579 | + public function load_model($class_name, $arguments = array(), $load_only = false) |
|
580 | + { |
|
581 | + $paths = apply_filters( |
|
582 | + 'FHEE__EE_Registry__load_model__paths', |
|
583 | + array( |
|
584 | + EE_MODELS, |
|
585 | + EE_CORE, |
|
586 | + ) |
|
587 | + ); |
|
588 | + // retrieve instantiated class |
|
589 | + return $this->_load( |
|
590 | + $paths, |
|
591 | + 'EEM_', |
|
592 | + $class_name, |
|
593 | + 'model', |
|
594 | + $arguments, |
|
595 | + false, |
|
596 | + true, |
|
597 | + $load_only |
|
598 | + ); |
|
599 | + } |
|
600 | + |
|
601 | + |
|
602 | + /** |
|
603 | + * loads model classes - must be singletons |
|
604 | + * |
|
605 | + * @param string $class_name - simple class name ie: price |
|
606 | + * @param mixed $arguments |
|
607 | + * @param bool $load_only |
|
608 | + * @return mixed | bool |
|
609 | + * @throws InvalidInterfaceException |
|
610 | + * @throws InvalidDataTypeException |
|
611 | + * @throws EE_Error |
|
612 | + * @throws ReflectionException |
|
613 | + * @throws InvalidArgumentException |
|
614 | + */ |
|
615 | + public function load_model_class($class_name, $arguments = array(), $load_only = true) |
|
616 | + { |
|
617 | + $paths = array( |
|
618 | + EE_MODELS . 'fields/', |
|
619 | + EE_MODELS . 'helpers/', |
|
620 | + EE_MODELS . 'relations/', |
|
621 | + EE_MODELS . 'strategies/', |
|
622 | + ); |
|
623 | + // retrieve instantiated class |
|
624 | + return $this->_load( |
|
625 | + $paths, |
|
626 | + 'EE_', |
|
627 | + $class_name, |
|
628 | + '', |
|
629 | + $arguments, |
|
630 | + false, |
|
631 | + true, |
|
632 | + $load_only |
|
633 | + ); |
|
634 | + } |
|
635 | + |
|
636 | + |
|
637 | + /** |
|
638 | + * Determines if $model_name is the name of an actual EE model. |
|
639 | + * |
|
640 | + * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
|
641 | + * @return boolean |
|
642 | + */ |
|
643 | + public function is_model_name($model_name) |
|
644 | + { |
|
645 | + return isset($this->models[ $model_name ]); |
|
646 | + } |
|
647 | + |
|
648 | + |
|
649 | + /** |
|
650 | + * generic class loader |
|
651 | + * |
|
652 | + * @param string $path_to_file - directory path to file location, not including filename |
|
653 | + * @param string $file_name - file name ie: my_file.php, including extension |
|
654 | + * @param string $type - file type - core? class? helper? model? |
|
655 | + * @param mixed $arguments |
|
656 | + * @param bool $load_only |
|
657 | + * @return mixed |
|
658 | + * @throws InvalidInterfaceException |
|
659 | + * @throws InvalidDataTypeException |
|
660 | + * @throws EE_Error |
|
661 | + * @throws ReflectionException |
|
662 | + * @throws InvalidArgumentException |
|
663 | + */ |
|
664 | + public function load_file($path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true) |
|
665 | + { |
|
666 | + // retrieve instantiated class |
|
667 | + return $this->_load( |
|
668 | + $path_to_file, |
|
669 | + '', |
|
670 | + $file_name, |
|
671 | + $type, |
|
672 | + $arguments, |
|
673 | + false, |
|
674 | + true, |
|
675 | + $load_only |
|
676 | + ); |
|
677 | + } |
|
678 | + |
|
679 | + |
|
680 | + /** |
|
681 | + * @param string $path_to_file - directory path to file location, not including filename |
|
682 | + * @param string $class_name - full class name ie: My_Class |
|
683 | + * @param string $type - file type - core? class? helper? model? |
|
684 | + * @param mixed $arguments |
|
685 | + * @param bool $load_only |
|
686 | + * @return bool|EE_Addon|object |
|
687 | + * @throws InvalidInterfaceException |
|
688 | + * @throws InvalidDataTypeException |
|
689 | + * @throws EE_Error |
|
690 | + * @throws ReflectionException |
|
691 | + * @throws InvalidArgumentException |
|
692 | + */ |
|
693 | + public function load_addon($path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false) |
|
694 | + { |
|
695 | + // retrieve instantiated class |
|
696 | + return $this->_load( |
|
697 | + $path_to_file, |
|
698 | + 'addon', |
|
699 | + $class_name, |
|
700 | + $type, |
|
701 | + $arguments, |
|
702 | + false, |
|
703 | + true, |
|
704 | + $load_only |
|
705 | + ); |
|
706 | + } |
|
707 | + |
|
708 | + |
|
709 | + /** |
|
710 | + * instantiates, caches, and automatically resolves dependencies |
|
711 | + * for classes that use a Fully Qualified Class Name. |
|
712 | + * if the class is not capable of being loaded using PSR-4 autoloading, |
|
713 | + * then you need to use one of the existing load_*() methods |
|
714 | + * which can resolve the classname and filepath from the passed arguments |
|
715 | + * |
|
716 | + * @param bool|string $class_name Fully Qualified Class Name |
|
717 | + * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
|
718 | + * @param bool $cache whether to cache the instantiated object for reuse |
|
719 | + * @param bool $from_db some classes are instantiated from the db |
|
720 | + * and thus call a different method to instantiate |
|
721 | + * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
722 | + * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
|
723 | + * @return bool|null|mixed null = failure to load or instantiate class object. |
|
724 | + * object = class loaded and instantiated successfully. |
|
725 | + * bool = fail or success when $load_only is true |
|
726 | + * @throws InvalidInterfaceException |
|
727 | + * @throws InvalidDataTypeException |
|
728 | + * @throws EE_Error |
|
729 | + * @throws ReflectionException |
|
730 | + * @throws InvalidArgumentException |
|
731 | + */ |
|
732 | + public function create( |
|
733 | + $class_name = false, |
|
734 | + $arguments = array(), |
|
735 | + $cache = false, |
|
736 | + $from_db = false, |
|
737 | + $load_only = false, |
|
738 | + $addon = false |
|
739 | + ) { |
|
740 | + $class_name = ltrim($class_name, '\\'); |
|
741 | + $class_name = $this->class_cache->getFqnForAlias($class_name); |
|
742 | + $class_exists = $this->loadOrVerifyClassExists($class_name, $arguments); |
|
743 | + // if a non-FQCN was passed, then |
|
744 | + // verifyClassExists() might return an object |
|
745 | + // or it could return null if the class just could not be found anywhere |
|
746 | + if ($class_exists instanceof $class_name || $class_exists === null) { |
|
747 | + // either way, return the results |
|
748 | + return $class_exists; |
|
749 | + } |
|
750 | + $class_name = $class_exists; |
|
751 | + // if we're only loading the class and it already exists, then let's just return true immediately |
|
752 | + if ($load_only) { |
|
753 | + return true; |
|
754 | + } |
|
755 | + $addon = $addon ? 'addon' : ''; |
|
756 | + // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
757 | + // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
758 | + // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
759 | + if ($this->_cache_on && $cache && ! $load_only) { |
|
760 | + // return object if it's already cached |
|
761 | + $cached_class = $this->_get_cached_class($class_name, $addon, $arguments); |
|
762 | + if ($cached_class !== null) { |
|
763 | + return $cached_class; |
|
764 | + } |
|
765 | + }// obtain the loader method from the dependency map |
|
766 | + $loader = $this->_dependency_map->class_loader($class_name);// instantiate the requested object |
|
767 | + if ($loader instanceof Closure) { |
|
768 | + $class_obj = $loader($arguments); |
|
769 | + } else { |
|
770 | + if ($loader && method_exists($this, $loader)) { |
|
771 | + $class_obj = $this->{$loader}($class_name, $arguments); |
|
772 | + } else { |
|
773 | + $class_obj = $this->_create_object($class_name, $arguments, $addon, $from_db); |
|
774 | + } |
|
775 | + } |
|
776 | + if (($this->_cache_on && $cache) || $this->get_class_abbreviation($class_name, '')) { |
|
777 | + // save it for later... kinda like gum { : $ |
|
778 | + $this->_set_cached_class( |
|
779 | + $class_obj, |
|
780 | + $class_name, |
|
781 | + $addon, |
|
782 | + $from_db, |
|
783 | + $arguments |
|
784 | + ); |
|
785 | + } |
|
786 | + $this->_cache_on = true; |
|
787 | + return $class_obj; |
|
788 | + } |
|
789 | + |
|
790 | + |
|
791 | + /** |
|
792 | + * Recursively checks that a class exists and potentially attempts to load classes with non-FQCNs |
|
793 | + * |
|
794 | + * @param string|object $class_name |
|
795 | + * @param array $arguments |
|
796 | + * @param int $attempt |
|
797 | + * @return mixed |
|
798 | + */ |
|
799 | + private function loadOrVerifyClassExists($class_name, array $arguments, $attempt = 1) |
|
800 | + { |
|
801 | + if (is_object($class_name) || class_exists($class_name)) { |
|
802 | + return $class_name; |
|
803 | + } |
|
804 | + switch ($attempt) { |
|
805 | + case 1: |
|
806 | + // if it's a FQCN then maybe the class is registered with a preceding \ |
|
807 | + $class_name = strpos($class_name, '\\') !== false |
|
808 | + ? '\\' . ltrim($class_name, '\\') |
|
809 | + : $class_name; |
|
810 | + break; |
|
811 | + case 2: |
|
812 | + // |
|
813 | + $loader = $this->_dependency_map->class_loader($class_name); |
|
814 | + if ($loader && method_exists($this, $loader)) { |
|
815 | + return $this->{$loader}($class_name, $arguments); |
|
816 | + } |
|
817 | + break; |
|
818 | + case 3: |
|
819 | + default: |
|
820 | + return null; |
|
821 | + } |
|
822 | + $attempt++; |
|
823 | + return $this->loadOrVerifyClassExists($class_name, $arguments, $attempt); |
|
824 | + } |
|
825 | + |
|
826 | + |
|
827 | + /** |
|
828 | + * instantiates, caches, and injects dependencies for classes |
|
829 | + * |
|
830 | + * @param array $file_paths an array of paths to folders to look in |
|
831 | + * @param string $class_prefix EE or EEM or... ??? |
|
832 | + * @param bool|string $class_name $class name |
|
833 | + * @param string $type file type - core? class? helper? model? |
|
834 | + * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
|
835 | + * @param bool $from_db some classes are instantiated from the db |
|
836 | + * and thus call a different method to instantiate |
|
837 | + * @param bool $cache whether to cache the instantiated object for reuse |
|
838 | + * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
839 | + * @return bool|null|object null = failure to load or instantiate class object. |
|
840 | + * object = class loaded and instantiated successfully. |
|
841 | + * bool = fail or success when $load_only is true |
|
842 | + * @throws EE_Error |
|
843 | + * @throws ReflectionException |
|
844 | + * @throws InvalidInterfaceException |
|
845 | + * @throws InvalidDataTypeException |
|
846 | + * @throws InvalidArgumentException |
|
847 | + */ |
|
848 | + protected function _load( |
|
849 | + $file_paths = array(), |
|
850 | + $class_prefix = 'EE_', |
|
851 | + $class_name = false, |
|
852 | + $type = 'class', |
|
853 | + $arguments = array(), |
|
854 | + $from_db = false, |
|
855 | + $cache = true, |
|
856 | + $load_only = false |
|
857 | + ) { |
|
858 | + $class_name = ltrim($class_name, '\\'); |
|
859 | + // strip php file extension |
|
860 | + $class_name = str_replace('.php', '', trim($class_name)); |
|
861 | + // does the class have a prefix ? |
|
862 | + if (! empty($class_prefix) && $class_prefix !== 'addon') { |
|
863 | + // make sure $class_prefix is uppercase |
|
864 | + $class_prefix = strtoupper(trim($class_prefix)); |
|
865 | + // add class prefix ONCE!!! |
|
866 | + $class_name = $class_prefix . str_replace($class_prefix, '', $class_name); |
|
867 | + } |
|
868 | + $class_name = $this->class_cache->getFqnForAlias($class_name); |
|
869 | + $class_exists = class_exists($class_name, false); |
|
870 | + // if we're only loading the class and it already exists, then let's just return true immediately |
|
871 | + if ($load_only && $class_exists) { |
|
872 | + return true; |
|
873 | + } |
|
874 | + $arguments = is_array($arguments) ? $arguments : array($arguments); |
|
875 | + // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
876 | + // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
877 | + // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
878 | + if ($this->_cache_on && $cache && ! $load_only) { |
|
879 | + // return object if it's already cached |
|
880 | + $cached_class = $this->_get_cached_class($class_name, $class_prefix, $arguments); |
|
881 | + if ($cached_class !== null) { |
|
882 | + return $cached_class; |
|
883 | + } |
|
884 | + } |
|
885 | + // if the class doesn't already exist.. then we need to try and find the file and load it |
|
886 | + if (! $class_exists) { |
|
887 | + // get full path to file |
|
888 | + $path = $this->_resolve_path($class_name, $type, $file_paths); |
|
889 | + // load the file |
|
890 | + $loaded = $this->_require_file($path, $class_name, $type, $file_paths); |
|
891 | + // if we are only loading a file but NOT instantiating an object |
|
892 | + // then return boolean for whether class was loaded or not |
|
893 | + if ($load_only) { |
|
894 | + return $loaded; |
|
895 | + } |
|
896 | + // if an object was expected but loading failed, then return nothing |
|
897 | + if (! $loaded) { |
|
898 | + return null; |
|
899 | + } |
|
900 | + } |
|
901 | + // instantiate the requested object |
|
902 | + $class_obj = $this->_create_object($class_name, $arguments, $type, $from_db); |
|
903 | + if ($this->_cache_on && $cache) { |
|
904 | + // save it for later... kinda like gum { : $ |
|
905 | + $this->_set_cached_class( |
|
906 | + $class_obj, |
|
907 | + $class_name, |
|
908 | + $class_prefix, |
|
909 | + $from_db, |
|
910 | + $arguments |
|
911 | + ); |
|
912 | + } |
|
913 | + $this->_cache_on = true; |
|
914 | + return $class_obj; |
|
915 | + } |
|
916 | + |
|
917 | + |
|
918 | + /** |
|
919 | + * @param string $class_name |
|
920 | + * @param string $default have to specify something, but not anything that will conflict |
|
921 | + * @return mixed|string |
|
922 | + */ |
|
923 | + protected function get_class_abbreviation($class_name, $default = 'FANCY_BATMAN_PANTS') |
|
924 | + { |
|
925 | + return isset($this->_class_abbreviations[ $class_name ]) |
|
926 | + ? $this->_class_abbreviations[ $class_name ] |
|
927 | + : $default; |
|
928 | + } |
|
929 | + |
|
930 | + |
|
931 | + /** |
|
932 | + * attempts to find a cached version of the requested class |
|
933 | + * by looking in the following places: |
|
934 | + * $this->{$class_abbreviation} ie: $this->CART |
|
935 | + * $this->{$class_name} ie: $this->Some_Class |
|
936 | + * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
937 | + * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
|
938 | + * |
|
939 | + * @param string $class_name |
|
940 | + * @param string $class_prefix |
|
941 | + * @param array $arguments |
|
942 | + * @return mixed |
|
943 | + */ |
|
944 | + protected function _get_cached_class( |
|
945 | + $class_name, |
|
946 | + $class_prefix = '', |
|
947 | + $arguments = array() |
|
948 | + ) { |
|
949 | + if ($class_name === 'EE_Registry') { |
|
950 | + return $this; |
|
951 | + } |
|
952 | + $class_abbreviation = $this->get_class_abbreviation($class_name); |
|
953 | + // check if class has already been loaded, and return it if it has been |
|
954 | + if (isset($this->{$class_abbreviation})) { |
|
955 | + return $this->{$class_abbreviation}; |
|
956 | + } |
|
957 | + $class_name = str_replace('\\', '_', $class_name); |
|
958 | + if (isset($this->{$class_name})) { |
|
959 | + return $this->{$class_name}; |
|
960 | + } |
|
961 | + if ($class_prefix === 'addon' && isset($this->addons->{$class_name})) { |
|
962 | + return $this->addons->{$class_name}; |
|
963 | + } |
|
964 | + $object_identifier = $this->object_identifier->getIdentifier($class_name, $arguments); |
|
965 | + if (isset($this->LIB->{$object_identifier})) { |
|
966 | + return $this->LIB->{$object_identifier}; |
|
967 | + } |
|
968 | + foreach ($this->LIB as $key => $object) { |
|
969 | + if (// request does not contain new arguments and therefore no args identifier |
|
970 | + ! $this->object_identifier->hasArguments($object_identifier) |
|
971 | + // but previously cached class with args was found |
|
972 | + && $this->object_identifier->fqcnMatchesObjectIdentifier($class_name, $key) |
|
973 | + ) { |
|
974 | + return $object; |
|
975 | + } |
|
976 | + } |
|
977 | + return null; |
|
978 | + } |
|
979 | + |
|
980 | + |
|
981 | + /** |
|
982 | + * removes a cached version of the requested class |
|
983 | + * |
|
984 | + * @param string $class_name |
|
985 | + * @param boolean $addon |
|
986 | + * @param array $arguments |
|
987 | + * @return boolean |
|
988 | + */ |
|
989 | + public function clear_cached_class( |
|
990 | + $class_name, |
|
991 | + $addon = false, |
|
992 | + $arguments = array() |
|
993 | + ) { |
|
994 | + $class_abbreviation = $this->get_class_abbreviation($class_name); |
|
995 | + // check if class has already been loaded, and return it if it has been |
|
996 | + if (isset($this->{$class_abbreviation})) { |
|
997 | + $this->{$class_abbreviation} = null; |
|
998 | + return true; |
|
999 | + } |
|
1000 | + $class_name = str_replace('\\', '_', $class_name); |
|
1001 | + if (isset($this->{$class_name})) { |
|
1002 | + $this->{$class_name} = null; |
|
1003 | + return true; |
|
1004 | + } |
|
1005 | + if ($addon && isset($this->addons->{$class_name})) { |
|
1006 | + unset($this->addons->{$class_name}); |
|
1007 | + return true; |
|
1008 | + } |
|
1009 | + $class_name = $this->object_identifier->getIdentifier($class_name, $arguments); |
|
1010 | + if (isset($this->LIB->{$class_name})) { |
|
1011 | + unset($this->LIB->{$class_name}); |
|
1012 | + return true; |
|
1013 | + } |
|
1014 | + return false; |
|
1015 | + } |
|
1016 | + |
|
1017 | + |
|
1018 | + /** |
|
1019 | + * _set_cached_class |
|
1020 | + * attempts to cache the instantiated class locally |
|
1021 | + * in one of the following places, in the following order: |
|
1022 | + * $this->{class_abbreviation} ie: $this->CART |
|
1023 | + * $this->{$class_name} ie: $this->Some_Class |
|
1024 | + * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
|
1025 | + * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
1026 | + * |
|
1027 | + * @param object $class_obj |
|
1028 | + * @param string $class_name |
|
1029 | + * @param string $class_prefix |
|
1030 | + * @param bool $from_db |
|
1031 | + * @param array $arguments |
|
1032 | + * @return void |
|
1033 | + */ |
|
1034 | + protected function _set_cached_class( |
|
1035 | + $class_obj, |
|
1036 | + $class_name, |
|
1037 | + $class_prefix = '', |
|
1038 | + $from_db = false, |
|
1039 | + $arguments = array() |
|
1040 | + ) { |
|
1041 | + if ($class_name === 'EE_Registry' || empty($class_obj)) { |
|
1042 | + return; |
|
1043 | + } |
|
1044 | + // return newly instantiated class |
|
1045 | + $class_abbreviation = $this->get_class_abbreviation($class_name, ''); |
|
1046 | + if ($class_abbreviation) { |
|
1047 | + $this->{$class_abbreviation} = $class_obj; |
|
1048 | + return; |
|
1049 | + } |
|
1050 | + $class_name = str_replace('\\', '_', $class_name); |
|
1051 | + if (property_exists($this, $class_name)) { |
|
1052 | + $this->{$class_name} = $class_obj; |
|
1053 | + return; |
|
1054 | + } |
|
1055 | + if ($class_prefix === 'addon') { |
|
1056 | + $this->addons->{$class_name} = $class_obj; |
|
1057 | + return; |
|
1058 | + } |
|
1059 | + if (! $from_db) { |
|
1060 | + $class_name = $this->object_identifier->getIdentifier($class_name, $arguments); |
|
1061 | + $this->LIB->{$class_name} = $class_obj; |
|
1062 | + } |
|
1063 | + } |
|
1064 | + |
|
1065 | + |
|
1066 | + /** |
|
1067 | + * attempts to find a full valid filepath for the requested class. |
|
1068 | + * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
|
1069 | + * then returns that path if the target file has been found and is readable |
|
1070 | + * |
|
1071 | + * @param string $class_name |
|
1072 | + * @param string $type |
|
1073 | + * @param array $file_paths |
|
1074 | + * @return string | bool |
|
1075 | + */ |
|
1076 | + protected function _resolve_path($class_name, $type = '', $file_paths = array()) |
|
1077 | + { |
|
1078 | + // make sure $file_paths is an array |
|
1079 | + $file_paths = is_array($file_paths) |
|
1080 | + ? $file_paths |
|
1081 | + : array($file_paths); |
|
1082 | + // cycle thru paths |
|
1083 | + foreach ($file_paths as $key => $file_path) { |
|
1084 | + // convert all separators to proper /, if no filepath, then use EE_CLASSES |
|
1085 | + $file_path = $file_path |
|
1086 | + ? str_replace(array('/', '\\'), '/', $file_path) |
|
1087 | + : EE_CLASSES; |
|
1088 | + // prep file type |
|
1089 | + $type = ! empty($type) |
|
1090 | + ? trim($type, '.') . '.' |
|
1091 | + : ''; |
|
1092 | + // build full file path |
|
1093 | + $file_paths[ $key ] = rtrim($file_path, '/') . '/' . $class_name . '.' . $type . 'php'; |
|
1094 | + // does the file exist and can be read ? |
|
1095 | + if (is_readable($file_paths[ $key ])) { |
|
1096 | + return $file_paths[ $key ]; |
|
1097 | + } |
|
1098 | + } |
|
1099 | + return false; |
|
1100 | + } |
|
1101 | + |
|
1102 | + |
|
1103 | + /** |
|
1104 | + * basically just performs a require_once() |
|
1105 | + * but with some error handling |
|
1106 | + * |
|
1107 | + * @param string $path |
|
1108 | + * @param string $class_name |
|
1109 | + * @param string $type |
|
1110 | + * @param array $file_paths |
|
1111 | + * @return bool |
|
1112 | + * @throws EE_Error |
|
1113 | + * @throws ReflectionException |
|
1114 | + */ |
|
1115 | + protected function _require_file($path, $class_name, $type = '', $file_paths = array()) |
|
1116 | + { |
|
1117 | + $this->resolve_legacy_class_parent($class_name); |
|
1118 | + // don't give up! you gotta... |
|
1119 | + try { |
|
1120 | + // does the file exist and can it be read ? |
|
1121 | + if (! $path) { |
|
1122 | + // just in case the file has already been autoloaded, |
|
1123 | + // but discrepancies in the naming schema are preventing it from |
|
1124 | + // being loaded via one of the EE_Registry::load_*() methods, |
|
1125 | + // then let's try one last hail mary before throwing an exception |
|
1126 | + // and call class_exists() again, but with autoloading turned ON |
|
1127 | + if (class_exists($class_name)) { |
|
1128 | + return true; |
|
1129 | + } |
|
1130 | + // so sorry, can't find the file |
|
1131 | + throw new EE_Error( |
|
1132 | + sprintf( |
|
1133 | + esc_html__( |
|
1134 | + 'The %1$s file %2$s could not be located or is not readable due to file permissions. Please ensure that the following filepath(s) are correct: %3$s', |
|
1135 | + 'event_espresso' |
|
1136 | + ), |
|
1137 | + trim($type, '.'), |
|
1138 | + $class_name, |
|
1139 | + '<br />' . implode(',<br />', $file_paths) |
|
1140 | + ) |
|
1141 | + ); |
|
1142 | + } |
|
1143 | + // get the file |
|
1144 | + require_once($path); |
|
1145 | + // if the class isn't already declared somewhere |
|
1146 | + if (class_exists($class_name, false) === false) { |
|
1147 | + // so sorry, not a class |
|
1148 | + throw new EE_Error( |
|
1149 | + sprintf( |
|
1150 | + esc_html__( |
|
1151 | + 'The %s file %s does not appear to contain the %s Class.', |
|
1152 | + 'event_espresso' |
|
1153 | + ), |
|
1154 | + $type, |
|
1155 | + $path, |
|
1156 | + $class_name |
|
1157 | + ) |
|
1158 | + ); |
|
1159 | + } |
|
1160 | + } catch (EE_Error $e) { |
|
1161 | + $e->get_error(); |
|
1162 | + return false; |
|
1163 | + } |
|
1164 | + return true; |
|
1165 | + } |
|
1166 | + |
|
1167 | + |
|
1168 | + /** |
|
1169 | + * Some of our legacy classes that extended a parent class would simply use a require() statement |
|
1170 | + * before their class declaration in order to ensure that the parent class was loaded. |
|
1171 | + * This is not ideal, but it's nearly impossible to determine the parent class of a non-namespaced class, |
|
1172 | + * without triggering a fatal error because the parent class has yet to be loaded and therefore doesn't exist. |
|
1173 | + * |
|
1174 | + * @param string $class_name |
|
1175 | + */ |
|
1176 | + protected function resolve_legacy_class_parent($class_name = '') |
|
1177 | + { |
|
1178 | + try { |
|
1179 | + $legacy_parent_class_map = array( |
|
1180 | + 'EE_Payment_Processor' => 'core/business/EE_Processor_Base.class.php', |
|
1181 | + ); |
|
1182 | + if (isset($legacy_parent_class_map[ $class_name ])) { |
|
1183 | + require_once EE_PLUGIN_DIR_PATH . $legacy_parent_class_map[ $class_name ]; |
|
1184 | + } |
|
1185 | + } catch (Exception $exception) { |
|
1186 | + } |
|
1187 | + } |
|
1188 | + |
|
1189 | + |
|
1190 | + /** |
|
1191 | + * _create_object |
|
1192 | + * Attempts to instantiate the requested class via any of the |
|
1193 | + * commonly used instantiation methods employed throughout EE. |
|
1194 | + * The priority for instantiation is as follows: |
|
1195 | + * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
|
1196 | + * - model objects via their 'new_instance_from_db' method |
|
1197 | + * - model objects via their 'new_instance' method |
|
1198 | + * - "singleton" classes" via their 'instance' method |
|
1199 | + * - standard instantiable classes via their __constructor |
|
1200 | + * Prior to instantiation, if the classname exists in the dependency_map, |
|
1201 | + * then the constructor for the requested class will be examined to determine |
|
1202 | + * if any dependencies exist, and if they can be injected. |
|
1203 | + * If so, then those classes will be added to the array of arguments passed to the constructor |
|
1204 | + * |
|
1205 | + * @param string $class_name |
|
1206 | + * @param array $arguments |
|
1207 | + * @param string $type |
|
1208 | + * @param bool $from_db |
|
1209 | + * @return null|object|bool |
|
1210 | + * @throws InvalidArgumentException |
|
1211 | + * @throws InvalidInterfaceException |
|
1212 | + * @throws EE_Error |
|
1213 | + * @throws ReflectionException |
|
1214 | + * @throws InvalidDataTypeException |
|
1215 | + */ |
|
1216 | + protected function _create_object($class_name, $arguments = array(), $type = '', $from_db = false) |
|
1217 | + { |
|
1218 | + // create reflection |
|
1219 | + $reflector = $this->mirror->getReflectionClass($class_name); |
|
1220 | + // make sure arguments are an array |
|
1221 | + $arguments = is_array($arguments) |
|
1222 | + ? $arguments |
|
1223 | + : array($arguments); |
|
1224 | + // and if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
|
1225 | + // else wrap it in an additional array so that it doesn't get split into multiple parameters |
|
1226 | + $arguments = $this->_array_is_numerically_and_sequentially_indexed($arguments) |
|
1227 | + ? $arguments |
|
1228 | + : array($arguments); |
|
1229 | + // attempt to inject dependencies ? |
|
1230 | + if ($this->_dependency_map->has($class_name)) { |
|
1231 | + $arguments = $this->_resolve_dependencies($reflector, $class_name, $arguments); |
|
1232 | + } |
|
1233 | + // instantiate the class if possible |
|
1234 | + if ($reflector->isAbstract()) { |
|
1235 | + // nothing to instantiate, loading file was enough |
|
1236 | + // does not throw an exception so $instantiation_mode is unused |
|
1237 | + // $instantiation_mode = "1) no constructor abstract class"; |
|
1238 | + return true; |
|
1239 | + } |
|
1240 | + if (empty($arguments) |
|
1241 | + && $this->mirror->getConstructorFromReflection($reflector) === null |
|
1242 | + && $reflector->isInstantiable() |
|
1243 | + ) { |
|
1244 | + // no constructor = static methods only... nothing to instantiate, loading file was enough |
|
1245 | + // $instantiation_mode = "2) no constructor but instantiable"; |
|
1246 | + return $reflector->newInstance(); |
|
1247 | + } |
|
1248 | + if ($from_db && method_exists($class_name, 'new_instance_from_db')) { |
|
1249 | + // $instantiation_mode = "3) new_instance_from_db()"; |
|
1250 | + return call_user_func_array(array($class_name, 'new_instance_from_db'), $arguments); |
|
1251 | + } |
|
1252 | + if (method_exists($class_name, 'new_instance')) { |
|
1253 | + // $instantiation_mode = "4) new_instance()"; |
|
1254 | + return call_user_func_array(array($class_name, 'new_instance'), $arguments); |
|
1255 | + } |
|
1256 | + if (method_exists($class_name, 'instance')) { |
|
1257 | + // $instantiation_mode = "5) instance()"; |
|
1258 | + return call_user_func_array(array($class_name, 'instance'), $arguments); |
|
1259 | + } |
|
1260 | + if ($reflector->isInstantiable()) { |
|
1261 | + // $instantiation_mode = "6) constructor"; |
|
1262 | + return $reflector->newInstanceArgs($arguments); |
|
1263 | + } |
|
1264 | + // heh ? something's not right ! |
|
1265 | + throw new EE_Error( |
|
1266 | + sprintf( |
|
1267 | + __('The %s file %s could not be instantiated.', 'event_espresso'), |
|
1268 | + $type, |
|
1269 | + $class_name |
|
1270 | + ) |
|
1271 | + ); |
|
1272 | + } |
|
1273 | + |
|
1274 | + |
|
1275 | + /** |
|
1276 | + * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
|
1277 | + * @param array $array |
|
1278 | + * @return bool |
|
1279 | + */ |
|
1280 | + protected function _array_is_numerically_and_sequentially_indexed(array $array) |
|
1281 | + { |
|
1282 | + return ! empty($array) |
|
1283 | + ? array_keys($array) === range(0, count($array) - 1) |
|
1284 | + : true; |
|
1285 | + } |
|
1286 | + |
|
1287 | + |
|
1288 | + /** |
|
1289 | + * _resolve_dependencies |
|
1290 | + * examines the constructor for the requested class to determine |
|
1291 | + * if any dependencies exist, and if they can be injected. |
|
1292 | + * If so, then those classes will be added to the array of arguments passed to the constructor |
|
1293 | + * PLZ NOTE: this is achieved by type hinting the constructor params |
|
1294 | + * For example: |
|
1295 | + * if attempting to load a class "Foo" with the following constructor: |
|
1296 | + * __construct( Bar $bar_class, Fighter $grohl_class ) |
|
1297 | + * then $bar_class and $grohl_class will be added to the $arguments array, |
|
1298 | + * but only IF they are NOT already present in the incoming arguments array, |
|
1299 | + * and the correct classes can be loaded |
|
1300 | + * |
|
1301 | + * @param ReflectionClass $reflector |
|
1302 | + * @param string $class_name |
|
1303 | + * @param array $arguments |
|
1304 | + * @return array |
|
1305 | + * @throws InvalidArgumentException |
|
1306 | + * @throws InvalidDataTypeException |
|
1307 | + * @throws InvalidInterfaceException |
|
1308 | + * @throws ReflectionException |
|
1309 | + */ |
|
1310 | + protected function _resolve_dependencies(ReflectionClass $reflector, $class_name, array $arguments = array()) |
|
1311 | + { |
|
1312 | + // let's examine the constructor |
|
1313 | + $constructor = $this->mirror->getConstructorFromReflection($reflector); |
|
1314 | + // whu? huh? nothing? |
|
1315 | + if (! $constructor) { |
|
1316 | + return $arguments; |
|
1317 | + } |
|
1318 | + // get constructor parameters |
|
1319 | + $params = $this->mirror->getParametersFromReflection($reflector); |
|
1320 | + // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
|
1321 | + $argument_keys = array_keys($arguments); |
|
1322 | + // now loop thru all of the constructors expected parameters |
|
1323 | + foreach ($params as $index => $param) { |
|
1324 | + try { |
|
1325 | + // is this a dependency for a specific class ? |
|
1326 | + $param_class = $this->mirror->getParameterClassName($param, $class_name, $index); |
|
1327 | + } catch (ReflectionException $exception) { |
|
1328 | + // uh-oh... most likely a legacy class that has not been autoloaded |
|
1329 | + // let's try to derive the classname from what we have now |
|
1330 | + // and hope that the property var name is close to the class name |
|
1331 | + $param_class = $param->getName(); |
|
1332 | + $param_class = str_replace('_', ' ', $param_class); |
|
1333 | + $param_class = ucwords($param_class); |
|
1334 | + $param_class = str_replace(' ', '_', $param_class); |
|
1335 | + } |
|
1336 | + // BUT WAIT !!! This class may be an alias for something else (or getting replaced at runtime) |
|
1337 | + $param_class = $this->class_cache->isAlias($param_class, $class_name) |
|
1338 | + ? $this->class_cache->getFqnForAlias($param_class, $class_name) |
|
1339 | + : $param_class; |
|
1340 | + if (// param is not even a class |
|
1341 | + $param_class === null |
|
1342 | + // and something already exists in the incoming arguments for this param |
|
1343 | + && array_key_exists($index, $argument_keys) |
|
1344 | + && array_key_exists($argument_keys[ $index ], $arguments) |
|
1345 | + ) { |
|
1346 | + // so let's skip this argument and move on to the next |
|
1347 | + continue; |
|
1348 | + } |
|
1349 | + if (// parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
|
1350 | + $param_class !== null |
|
1351 | + && isset($argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ]) |
|
1352 | + && $arguments[ $argument_keys[ $index ] ] instanceof $param_class |
|
1353 | + ) { |
|
1354 | + // skip this argument and move on to the next |
|
1355 | + continue; |
|
1356 | + } |
|
1357 | + if (// parameter is type hinted as a class, and should be injected |
|
1358 | + $param_class !== null |
|
1359 | + && $this->_dependency_map->has_dependency_for_class($class_name, $param_class) |
|
1360 | + ) { |
|
1361 | + $arguments = $this->_resolve_dependency( |
|
1362 | + $class_name, |
|
1363 | + $param_class, |
|
1364 | + $arguments, |
|
1365 | + $index |
|
1366 | + ); |
|
1367 | + } |
|
1368 | + if (empty($arguments[ $index ])) { |
|
1369 | + $arguments[ $index ] = $this->mirror->getParameterDefaultValue( |
|
1370 | + $param, |
|
1371 | + $class_name, |
|
1372 | + $index |
|
1373 | + ); |
|
1374 | + } |
|
1375 | + } |
|
1376 | + return $arguments; |
|
1377 | + } |
|
1378 | + |
|
1379 | + |
|
1380 | + /** |
|
1381 | + * @param string $class_name |
|
1382 | + * @param string $param_class |
|
1383 | + * @param array $arguments |
|
1384 | + * @param mixed $index |
|
1385 | + * @return array |
|
1386 | + * @throws InvalidArgumentException |
|
1387 | + * @throws InvalidInterfaceException |
|
1388 | + * @throws InvalidDataTypeException |
|
1389 | + */ |
|
1390 | + protected function _resolve_dependency($class_name, $param_class, $arguments, $index) |
|
1391 | + { |
|
1392 | + $dependency = null; |
|
1393 | + // should dependency be loaded from cache ? |
|
1394 | + $cache_on = $this->_dependency_map->loading_strategy_for_class_dependency( |
|
1395 | + $class_name, |
|
1396 | + $param_class |
|
1397 | + ); |
|
1398 | + $cache_on = $cache_on !== EE_Dependency_Map::load_new_object; |
|
1399 | + // we might have a dependency... |
|
1400 | + // let's MAYBE try and find it in our cache if that's what's been requested |
|
1401 | + $cached_class = $cache_on |
|
1402 | + ? $this->_get_cached_class($param_class) |
|
1403 | + : null; |
|
1404 | + // and grab it if it exists |
|
1405 | + if ($cached_class instanceof $param_class) { |
|
1406 | + $dependency = $cached_class; |
|
1407 | + } elseif ($param_class !== $class_name) { |
|
1408 | + // obtain the loader method from the dependency map |
|
1409 | + $loader = $this->_dependency_map->class_loader($param_class); |
|
1410 | + // is loader a custom closure ? |
|
1411 | + if ($loader instanceof Closure) { |
|
1412 | + $dependency = $loader($arguments); |
|
1413 | + } else { |
|
1414 | + // set the cache on property for the recursive loading call |
|
1415 | + $this->_cache_on = $cache_on; |
|
1416 | + // if not, then let's try and load it via the registry |
|
1417 | + if ($loader && method_exists($this, $loader)) { |
|
1418 | + $dependency = $this->{$loader}($param_class); |
|
1419 | + } else { |
|
1420 | + $dependency = LoaderFactory::getLoader()->load( |
|
1421 | + $param_class, |
|
1422 | + array(), |
|
1423 | + $cache_on |
|
1424 | + ); |
|
1425 | + } |
|
1426 | + } |
|
1427 | + } |
|
1428 | + // did we successfully find the correct dependency ? |
|
1429 | + if ($dependency instanceof $param_class) { |
|
1430 | + // then let's inject it into the incoming array of arguments at the correct location |
|
1431 | + $arguments[ $index ] = $dependency; |
|
1432 | + } |
|
1433 | + return $arguments; |
|
1434 | + } |
|
1435 | + |
|
1436 | + |
|
1437 | + /** |
|
1438 | + * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
|
1439 | + * |
|
1440 | + * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
|
1441 | + * in the EE_Dependency_Map::$_class_loaders array, |
|
1442 | + * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
|
1443 | + * @param array $arguments |
|
1444 | + * @return object |
|
1445 | + */ |
|
1446 | + public static function factory($classname, $arguments = array()) |
|
1447 | + { |
|
1448 | + $loader = self::instance()->_dependency_map->class_loader($classname); |
|
1449 | + if ($loader instanceof Closure) { |
|
1450 | + return $loader($arguments); |
|
1451 | + } |
|
1452 | + if (method_exists(self::instance(), $loader)) { |
|
1453 | + return self::instance()->{$loader}($classname, $arguments); |
|
1454 | + } |
|
1455 | + return null; |
|
1456 | + } |
|
1457 | + |
|
1458 | + |
|
1459 | + /** |
|
1460 | + * Gets the addon by its class name |
|
1461 | + * |
|
1462 | + * @param string $class_name |
|
1463 | + * @return EE_Addon |
|
1464 | + */ |
|
1465 | + public function getAddon($class_name) |
|
1466 | + { |
|
1467 | + $class_name = str_replace('\\', '_', $class_name); |
|
1468 | + if (isset($this->addons->{$class_name})) { |
|
1469 | + return $this->addons->{$class_name}; |
|
1470 | + } else { |
|
1471 | + return null; |
|
1472 | + } |
|
1473 | + } |
|
1474 | + |
|
1475 | + |
|
1476 | + /** |
|
1477 | + * removes the addon from the internal cache |
|
1478 | + * |
|
1479 | + * @param string $class_name |
|
1480 | + * @return void |
|
1481 | + */ |
|
1482 | + public function removeAddon($class_name) |
|
1483 | + { |
|
1484 | + $class_name = str_replace('\\', '_', $class_name); |
|
1485 | + unset($this->addons->{$class_name}); |
|
1486 | + } |
|
1487 | + |
|
1488 | + |
|
1489 | + /** |
|
1490 | + * Gets the addon by its name/slug (not classname. For that, just |
|
1491 | + * use the get_addon() method above |
|
1492 | + * |
|
1493 | + * @param string $name |
|
1494 | + * @return EE_Addon |
|
1495 | + */ |
|
1496 | + public function get_addon_by_name($name) |
|
1497 | + { |
|
1498 | + foreach ($this->addons as $addon) { |
|
1499 | + if ($addon->name() === $name) { |
|
1500 | + return $addon; |
|
1501 | + } |
|
1502 | + } |
|
1503 | + return null; |
|
1504 | + } |
|
1505 | + |
|
1506 | + |
|
1507 | + /** |
|
1508 | + * Gets an array of all the registered addons, where the keys are their names. |
|
1509 | + * (ie, what each returns for their name() function) |
|
1510 | + * They're already available on EE_Registry::instance()->addons as properties, |
|
1511 | + * where each property's name is the addon's classname, |
|
1512 | + * So if you just want to get the addon by classname, |
|
1513 | + * OR use the get_addon() method above. |
|
1514 | + * PLEASE NOTE: |
|
1515 | + * addons with Fully Qualified Class Names |
|
1516 | + * have had the namespace separators converted to underscores, |
|
1517 | + * so a classname like Fully\Qualified\ClassName |
|
1518 | + * would have been converted to Fully_Qualified_ClassName |
|
1519 | + * |
|
1520 | + * @return EE_Addon[] where the KEYS are the addon's name() |
|
1521 | + */ |
|
1522 | + public function get_addons_by_name() |
|
1523 | + { |
|
1524 | + $addons = array(); |
|
1525 | + foreach ($this->addons as $addon) { |
|
1526 | + $addons[ $addon->name() ] = $addon; |
|
1527 | + } |
|
1528 | + return $addons; |
|
1529 | + } |
|
1530 | + |
|
1531 | + |
|
1532 | + /** |
|
1533 | + * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
|
1534 | + * a stale copy of it around |
|
1535 | + * |
|
1536 | + * @param string $model_name |
|
1537 | + * @return \EEM_Base |
|
1538 | + * @throws \EE_Error |
|
1539 | + */ |
|
1540 | + public function reset_model($model_name) |
|
1541 | + { |
|
1542 | + $model_class_name = strpos($model_name, 'EEM_') !== 0 |
|
1543 | + ? "EEM_{$model_name}" |
|
1544 | + : $model_name; |
|
1545 | + if (! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
1546 | + return null; |
|
1547 | + } |
|
1548 | + // get that model reset it and make sure we nuke the old reference to it |
|
1549 | + if ($this->LIB->{$model_class_name} instanceof $model_class_name |
|
1550 | + && is_callable( |
|
1551 | + array($model_class_name, 'reset') |
|
1552 | + )) { |
|
1553 | + $this->LIB->{$model_class_name} = $this->LIB->{$model_class_name}->reset(); |
|
1554 | + } else { |
|
1555 | + throw new EE_Error( |
|
1556 | + sprintf( |
|
1557 | + esc_html__('Model %s does not have a method "reset"', 'event_espresso'), |
|
1558 | + $model_name |
|
1559 | + ) |
|
1560 | + ); |
|
1561 | + } |
|
1562 | + return $this->LIB->{$model_class_name}; |
|
1563 | + } |
|
1564 | + |
|
1565 | + |
|
1566 | + /** |
|
1567 | + * Resets the registry. |
|
1568 | + * The criteria for what gets reset is based on what can be shared between sites on the same request when |
|
1569 | + * switch_to_blog is used in a multisite install. Here is a list of things that are NOT reset. |
|
1570 | + * - $_dependency_map |
|
1571 | + * - $_class_abbreviations |
|
1572 | + * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
|
1573 | + * - $REQ: Still on the same request so no need to change. |
|
1574 | + * - $CAP: There is no site specific state in the EE_Capability class. |
|
1575 | + * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only |
|
1576 | + * one Session can be active in a single request. Resetting could resolve in "headers already sent" errors. |
|
1577 | + * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
|
1578 | + * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
|
1579 | + * switch or on the restore. |
|
1580 | + * - $modules |
|
1581 | + * - $shortcodes |
|
1582 | + * - $widgets |
|
1583 | + * |
|
1584 | + * @param boolean $hard [deprecated] |
|
1585 | + * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
|
1586 | + * or just reset without re-instantiating (handy to set to FALSE if you're not |
|
1587 | + * sure if you CAN currently reinstantiate the singletons at the moment) |
|
1588 | + * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so |
|
1589 | + * client |
|
1590 | + * code instead can just change the model context to a different blog id if |
|
1591 | + * necessary |
|
1592 | + * @return EE_Registry |
|
1593 | + * @throws InvalidInterfaceException |
|
1594 | + * @throws InvalidDataTypeException |
|
1595 | + * @throws EE_Error |
|
1596 | + * @throws ReflectionException |
|
1597 | + * @throws InvalidArgumentException |
|
1598 | + */ |
|
1599 | + public static function reset($hard = false, $reinstantiate = true, $reset_models = true) |
|
1600 | + { |
|
1601 | + $instance = self::instance(); |
|
1602 | + $instance->_cache_on = true; |
|
1603 | + // reset some "special" classes |
|
1604 | + EEH_Activation::reset(); |
|
1605 | + $hard = apply_filters('FHEE__EE_Registry__reset__hard', $hard); |
|
1606 | + $instance->CFG = EE_Config::reset($hard, $reinstantiate); |
|
1607 | + $instance->CART = null; |
|
1608 | + $instance->MRM = null; |
|
1609 | + $instance->AssetsRegistry = LoaderFactory::getLoader()->getShared( |
|
1610 | + 'EventEspresso\core\services\assets\Registry' |
|
1611 | + ); |
|
1612 | + // messages reset |
|
1613 | + EED_Messages::reset(); |
|
1614 | + // handle of objects cached on LIB |
|
1615 | + foreach (array('LIB', 'modules') as $cache) { |
|
1616 | + foreach ($instance->{$cache} as $class_name => $class) { |
|
1617 | + if (self::_reset_and_unset_object($class, $reset_models)) { |
|
1618 | + unset($instance->{$cache}->{$class_name}); |
|
1619 | + } |
|
1620 | + } |
|
1621 | + } |
|
1622 | + return $instance; |
|
1623 | + } |
|
1624 | + |
|
1625 | + |
|
1626 | + /** |
|
1627 | + * if passed object implements ResettableInterface, then call it's reset() method |
|
1628 | + * if passed object implements InterminableInterface, then return false, |
|
1629 | + * to indicate that it should NOT be cleared from the Registry cache |
|
1630 | + * |
|
1631 | + * @param $object |
|
1632 | + * @param bool $reset_models |
|
1633 | + * @return bool returns true if cached object should be unset |
|
1634 | + */ |
|
1635 | + private static function _reset_and_unset_object($object, $reset_models) |
|
1636 | + { |
|
1637 | + if (! is_object($object)) { |
|
1638 | + // don't unset anything that's not an object |
|
1639 | + return false; |
|
1640 | + } |
|
1641 | + if ($object instanceof EED_Module) { |
|
1642 | + $object::reset(); |
|
1643 | + // don't unset modules |
|
1644 | + return false; |
|
1645 | + } |
|
1646 | + if ($object instanceof ResettableInterface) { |
|
1647 | + if ($object instanceof EEM_Base) { |
|
1648 | + if ($reset_models) { |
|
1649 | + $object->reset(); |
|
1650 | + return true; |
|
1651 | + } |
|
1652 | + return false; |
|
1653 | + } |
|
1654 | + $object->reset(); |
|
1655 | + return true; |
|
1656 | + } |
|
1657 | + if (! $object instanceof InterminableInterface) { |
|
1658 | + return true; |
|
1659 | + } |
|
1660 | + return false; |
|
1661 | + } |
|
1662 | + |
|
1663 | + |
|
1664 | + /** |
|
1665 | + * Gets all the custom post type models defined |
|
1666 | + * |
|
1667 | + * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
|
1668 | + */ |
|
1669 | + public function cpt_models() |
|
1670 | + { |
|
1671 | + $cpt_models = array(); |
|
1672 | + foreach ($this->non_abstract_db_models as $short_name => $classname) { |
|
1673 | + if (is_subclass_of($classname, 'EEM_CPT_Base')) { |
|
1674 | + $cpt_models[ $short_name ] = $classname; |
|
1675 | + } |
|
1676 | + } |
|
1677 | + return $cpt_models; |
|
1678 | + } |
|
1679 | + |
|
1680 | + |
|
1681 | + /** |
|
1682 | + * @return \EE_Config |
|
1683 | + */ |
|
1684 | + public static function CFG() |
|
1685 | + { |
|
1686 | + return self::instance()->CFG; |
|
1687 | + } |
|
1688 | + |
|
1689 | + |
|
1690 | + /** |
|
1691 | + * @deprecated 4.9.62.p |
|
1692 | + * @param string $class_name |
|
1693 | + * @return ReflectionClass |
|
1694 | + * @throws ReflectionException |
|
1695 | + * @throws InvalidDataTypeException |
|
1696 | + */ |
|
1697 | + public function get_ReflectionClass($class_name) |
|
1698 | + { |
|
1699 | + return $this->mirror->getReflectionClass($class_name); |
|
1700 | + } |
|
1701 | 1701 | } |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | ObjectIdentifier $object_identifier = null |
194 | 194 | ) { |
195 | 195 | // check if class object is instantiated |
196 | - if (! self::$_instance instanceof EE_Registry |
|
196 | + if ( ! self::$_instance instanceof EE_Registry |
|
197 | 197 | && $dependency_map instanceof EE_Dependency_Map |
198 | 198 | && $mirror instanceof Mirror |
199 | 199 | && $class_cache instanceof ClassInterfaceCache |
@@ -308,10 +308,10 @@ discard block |
||
308 | 308 | $i18n_js_strings = (array) self::$i18n_js_strings; |
309 | 309 | foreach ($i18n_js_strings as $key => $value) { |
310 | 310 | if (is_scalar($value)) { |
311 | - $i18n_js_strings[ $key ] = html_entity_decode((string) $value, ENT_QUOTES, 'UTF-8'); |
|
311 | + $i18n_js_strings[$key] = html_entity_decode((string) $value, ENT_QUOTES, 'UTF-8'); |
|
312 | 312 | } |
313 | 313 | } |
314 | - return '/* <![CDATA[ */ var eei18n = ' . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
|
314 | + return '/* <![CDATA[ */ var eei18n = '.wp_json_encode($i18n_js_strings).'; /* ]]> */'; |
|
315 | 315 | } |
316 | 316 | |
317 | 317 | |
@@ -330,7 +330,7 @@ discard block |
||
330 | 330 | $module_class = get_class($module); |
331 | 331 | $this->modules->{$module_class} = $module; |
332 | 332 | } else { |
333 | - if (! class_exists('EE_Module_Request_Router', false)) { |
|
333 | + if ( ! class_exists('EE_Module_Request_Router', false)) { |
|
334 | 334 | $this->load_core('Module_Request_Router'); |
335 | 335 | } |
336 | 336 | EE_Module_Request_Router::module_factory($module); |
@@ -371,10 +371,10 @@ discard block |
||
371 | 371 | EE_CORE, |
372 | 372 | EE_ADMIN, |
373 | 373 | EE_CPTS, |
374 | - EE_CORE . 'data_migration_scripts/', |
|
375 | - EE_CORE . 'capabilities/', |
|
376 | - EE_CORE . 'request_stack/', |
|
377 | - EE_CORE . 'middleware/', |
|
374 | + EE_CORE.'data_migration_scripts/', |
|
375 | + EE_CORE.'capabilities/', |
|
376 | + EE_CORE.'request_stack/', |
|
377 | + EE_CORE.'middleware/', |
|
378 | 378 | ) |
379 | 379 | ); |
380 | 380 | // retrieve instantiated class |
@@ -409,7 +409,7 @@ discard block |
||
409 | 409 | $service_paths = apply_filters( |
410 | 410 | 'FHEE__EE_Registry__load_service__service_paths', |
411 | 411 | array( |
412 | - EE_CORE . 'services/', |
|
412 | + EE_CORE.'services/', |
|
413 | 413 | ) |
414 | 414 | ); |
415 | 415 | // retrieve instantiated class |
@@ -544,10 +544,10 @@ discard block |
||
544 | 544 | { |
545 | 545 | $paths = array( |
546 | 546 | EE_LIBRARIES, |
547 | - EE_LIBRARIES . 'messages/', |
|
548 | - EE_LIBRARIES . 'shortcodes/', |
|
549 | - EE_LIBRARIES . 'qtips/', |
|
550 | - EE_LIBRARIES . 'payment_methods/', |
|
547 | + EE_LIBRARIES.'messages/', |
|
548 | + EE_LIBRARIES.'shortcodes/', |
|
549 | + EE_LIBRARIES.'qtips/', |
|
550 | + EE_LIBRARIES.'payment_methods/', |
|
551 | 551 | ); |
552 | 552 | // retrieve instantiated class |
553 | 553 | return $this->_load( |
@@ -615,10 +615,10 @@ discard block |
||
615 | 615 | public function load_model_class($class_name, $arguments = array(), $load_only = true) |
616 | 616 | { |
617 | 617 | $paths = array( |
618 | - EE_MODELS . 'fields/', |
|
619 | - EE_MODELS . 'helpers/', |
|
620 | - EE_MODELS . 'relations/', |
|
621 | - EE_MODELS . 'strategies/', |
|
618 | + EE_MODELS.'fields/', |
|
619 | + EE_MODELS.'helpers/', |
|
620 | + EE_MODELS.'relations/', |
|
621 | + EE_MODELS.'strategies/', |
|
622 | 622 | ); |
623 | 623 | // retrieve instantiated class |
624 | 624 | return $this->_load( |
@@ -642,7 +642,7 @@ discard block |
||
642 | 642 | */ |
643 | 643 | public function is_model_name($model_name) |
644 | 644 | { |
645 | - return isset($this->models[ $model_name ]); |
|
645 | + return isset($this->models[$model_name]); |
|
646 | 646 | } |
647 | 647 | |
648 | 648 | |
@@ -763,7 +763,7 @@ discard block |
||
763 | 763 | return $cached_class; |
764 | 764 | } |
765 | 765 | }// obtain the loader method from the dependency map |
766 | - $loader = $this->_dependency_map->class_loader($class_name);// instantiate the requested object |
|
766 | + $loader = $this->_dependency_map->class_loader($class_name); // instantiate the requested object |
|
767 | 767 | if ($loader instanceof Closure) { |
768 | 768 | $class_obj = $loader($arguments); |
769 | 769 | } else { |
@@ -805,7 +805,7 @@ discard block |
||
805 | 805 | case 1: |
806 | 806 | // if it's a FQCN then maybe the class is registered with a preceding \ |
807 | 807 | $class_name = strpos($class_name, '\\') !== false |
808 | - ? '\\' . ltrim($class_name, '\\') |
|
808 | + ? '\\'.ltrim($class_name, '\\') |
|
809 | 809 | : $class_name; |
810 | 810 | break; |
811 | 811 | case 2: |
@@ -859,11 +859,11 @@ discard block |
||
859 | 859 | // strip php file extension |
860 | 860 | $class_name = str_replace('.php', '', trim($class_name)); |
861 | 861 | // does the class have a prefix ? |
862 | - if (! empty($class_prefix) && $class_prefix !== 'addon') { |
|
862 | + if ( ! empty($class_prefix) && $class_prefix !== 'addon') { |
|
863 | 863 | // make sure $class_prefix is uppercase |
864 | 864 | $class_prefix = strtoupper(trim($class_prefix)); |
865 | 865 | // add class prefix ONCE!!! |
866 | - $class_name = $class_prefix . str_replace($class_prefix, '', $class_name); |
|
866 | + $class_name = $class_prefix.str_replace($class_prefix, '', $class_name); |
|
867 | 867 | } |
868 | 868 | $class_name = $this->class_cache->getFqnForAlias($class_name); |
869 | 869 | $class_exists = class_exists($class_name, false); |
@@ -883,7 +883,7 @@ discard block |
||
883 | 883 | } |
884 | 884 | } |
885 | 885 | // if the class doesn't already exist.. then we need to try and find the file and load it |
886 | - if (! $class_exists) { |
|
886 | + if ( ! $class_exists) { |
|
887 | 887 | // get full path to file |
888 | 888 | $path = $this->_resolve_path($class_name, $type, $file_paths); |
889 | 889 | // load the file |
@@ -894,7 +894,7 @@ discard block |
||
894 | 894 | return $loaded; |
895 | 895 | } |
896 | 896 | // if an object was expected but loading failed, then return nothing |
897 | - if (! $loaded) { |
|
897 | + if ( ! $loaded) { |
|
898 | 898 | return null; |
899 | 899 | } |
900 | 900 | } |
@@ -922,8 +922,8 @@ discard block |
||
922 | 922 | */ |
923 | 923 | protected function get_class_abbreviation($class_name, $default = 'FANCY_BATMAN_PANTS') |
924 | 924 | { |
925 | - return isset($this->_class_abbreviations[ $class_name ]) |
|
926 | - ? $this->_class_abbreviations[ $class_name ] |
|
925 | + return isset($this->_class_abbreviations[$class_name]) |
|
926 | + ? $this->_class_abbreviations[$class_name] |
|
927 | 927 | : $default; |
928 | 928 | } |
929 | 929 | |
@@ -1056,7 +1056,7 @@ discard block |
||
1056 | 1056 | $this->addons->{$class_name} = $class_obj; |
1057 | 1057 | return; |
1058 | 1058 | } |
1059 | - if (! $from_db) { |
|
1059 | + if ( ! $from_db) { |
|
1060 | 1060 | $class_name = $this->object_identifier->getIdentifier($class_name, $arguments); |
1061 | 1061 | $this->LIB->{$class_name} = $class_obj; |
1062 | 1062 | } |
@@ -1087,13 +1087,13 @@ discard block |
||
1087 | 1087 | : EE_CLASSES; |
1088 | 1088 | // prep file type |
1089 | 1089 | $type = ! empty($type) |
1090 | - ? trim($type, '.') . '.' |
|
1090 | + ? trim($type, '.').'.' |
|
1091 | 1091 | : ''; |
1092 | 1092 | // build full file path |
1093 | - $file_paths[ $key ] = rtrim($file_path, '/') . '/' . $class_name . '.' . $type . 'php'; |
|
1093 | + $file_paths[$key] = rtrim($file_path, '/').'/'.$class_name.'.'.$type.'php'; |
|
1094 | 1094 | // does the file exist and can be read ? |
1095 | - if (is_readable($file_paths[ $key ])) { |
|
1096 | - return $file_paths[ $key ]; |
|
1095 | + if (is_readable($file_paths[$key])) { |
|
1096 | + return $file_paths[$key]; |
|
1097 | 1097 | } |
1098 | 1098 | } |
1099 | 1099 | return false; |
@@ -1118,7 +1118,7 @@ discard block |
||
1118 | 1118 | // don't give up! you gotta... |
1119 | 1119 | try { |
1120 | 1120 | // does the file exist and can it be read ? |
1121 | - if (! $path) { |
|
1121 | + if ( ! $path) { |
|
1122 | 1122 | // just in case the file has already been autoloaded, |
1123 | 1123 | // but discrepancies in the naming schema are preventing it from |
1124 | 1124 | // being loaded via one of the EE_Registry::load_*() methods, |
@@ -1136,7 +1136,7 @@ discard block |
||
1136 | 1136 | ), |
1137 | 1137 | trim($type, '.'), |
1138 | 1138 | $class_name, |
1139 | - '<br />' . implode(',<br />', $file_paths) |
|
1139 | + '<br />'.implode(',<br />', $file_paths) |
|
1140 | 1140 | ) |
1141 | 1141 | ); |
1142 | 1142 | } |
@@ -1179,8 +1179,8 @@ discard block |
||
1179 | 1179 | $legacy_parent_class_map = array( |
1180 | 1180 | 'EE_Payment_Processor' => 'core/business/EE_Processor_Base.class.php', |
1181 | 1181 | ); |
1182 | - if (isset($legacy_parent_class_map[ $class_name ])) { |
|
1183 | - require_once EE_PLUGIN_DIR_PATH . $legacy_parent_class_map[ $class_name ]; |
|
1182 | + if (isset($legacy_parent_class_map[$class_name])) { |
|
1183 | + require_once EE_PLUGIN_DIR_PATH.$legacy_parent_class_map[$class_name]; |
|
1184 | 1184 | } |
1185 | 1185 | } catch (Exception $exception) { |
1186 | 1186 | } |
@@ -1312,7 +1312,7 @@ discard block |
||
1312 | 1312 | // let's examine the constructor |
1313 | 1313 | $constructor = $this->mirror->getConstructorFromReflection($reflector); |
1314 | 1314 | // whu? huh? nothing? |
1315 | - if (! $constructor) { |
|
1315 | + if ( ! $constructor) { |
|
1316 | 1316 | return $arguments; |
1317 | 1317 | } |
1318 | 1318 | // get constructor parameters |
@@ -1341,15 +1341,15 @@ discard block |
||
1341 | 1341 | $param_class === null |
1342 | 1342 | // and something already exists in the incoming arguments for this param |
1343 | 1343 | && array_key_exists($index, $argument_keys) |
1344 | - && array_key_exists($argument_keys[ $index ], $arguments) |
|
1344 | + && array_key_exists($argument_keys[$index], $arguments) |
|
1345 | 1345 | ) { |
1346 | 1346 | // so let's skip this argument and move on to the next |
1347 | 1347 | continue; |
1348 | 1348 | } |
1349 | 1349 | if (// parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
1350 | 1350 | $param_class !== null |
1351 | - && isset($argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ]) |
|
1352 | - && $arguments[ $argument_keys[ $index ] ] instanceof $param_class |
|
1351 | + && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
1352 | + && $arguments[$argument_keys[$index]] instanceof $param_class |
|
1353 | 1353 | ) { |
1354 | 1354 | // skip this argument and move on to the next |
1355 | 1355 | continue; |
@@ -1365,8 +1365,8 @@ discard block |
||
1365 | 1365 | $index |
1366 | 1366 | ); |
1367 | 1367 | } |
1368 | - if (empty($arguments[ $index ])) { |
|
1369 | - $arguments[ $index ] = $this->mirror->getParameterDefaultValue( |
|
1368 | + if (empty($arguments[$index])) { |
|
1369 | + $arguments[$index] = $this->mirror->getParameterDefaultValue( |
|
1370 | 1370 | $param, |
1371 | 1371 | $class_name, |
1372 | 1372 | $index |
@@ -1428,7 +1428,7 @@ discard block |
||
1428 | 1428 | // did we successfully find the correct dependency ? |
1429 | 1429 | if ($dependency instanceof $param_class) { |
1430 | 1430 | // then let's inject it into the incoming array of arguments at the correct location |
1431 | - $arguments[ $index ] = $dependency; |
|
1431 | + $arguments[$index] = $dependency; |
|
1432 | 1432 | } |
1433 | 1433 | return $arguments; |
1434 | 1434 | } |
@@ -1523,7 +1523,7 @@ discard block |
||
1523 | 1523 | { |
1524 | 1524 | $addons = array(); |
1525 | 1525 | foreach ($this->addons as $addon) { |
1526 | - $addons[ $addon->name() ] = $addon; |
|
1526 | + $addons[$addon->name()] = $addon; |
|
1527 | 1527 | } |
1528 | 1528 | return $addons; |
1529 | 1529 | } |
@@ -1542,7 +1542,7 @@ discard block |
||
1542 | 1542 | $model_class_name = strpos($model_name, 'EEM_') !== 0 |
1543 | 1543 | ? "EEM_{$model_name}" |
1544 | 1544 | : $model_name; |
1545 | - if (! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
1545 | + if ( ! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
1546 | 1546 | return null; |
1547 | 1547 | } |
1548 | 1548 | // get that model reset it and make sure we nuke the old reference to it |
@@ -1634,7 +1634,7 @@ discard block |
||
1634 | 1634 | */ |
1635 | 1635 | private static function _reset_and_unset_object($object, $reset_models) |
1636 | 1636 | { |
1637 | - if (! is_object($object)) { |
|
1637 | + if ( ! is_object($object)) { |
|
1638 | 1638 | // don't unset anything that's not an object |
1639 | 1639 | return false; |
1640 | 1640 | } |
@@ -1654,7 +1654,7 @@ discard block |
||
1654 | 1654 | $object->reset(); |
1655 | 1655 | return true; |
1656 | 1656 | } |
1657 | - if (! $object instanceof InterminableInterface) { |
|
1657 | + if ( ! $object instanceof InterminableInterface) { |
|
1658 | 1658 | return true; |
1659 | 1659 | } |
1660 | 1660 | return false; |
@@ -1671,7 +1671,7 @@ discard block |
||
1671 | 1671 | $cpt_models = array(); |
1672 | 1672 | foreach ($this->non_abstract_db_models as $short_name => $classname) { |
1673 | 1673 | if (is_subclass_of($classname, 'EEM_CPT_Base')) { |
1674 | - $cpt_models[ $short_name ] = $classname; |
|
1674 | + $cpt_models[$short_name] = $classname; |
|
1675 | 1675 | } |
1676 | 1676 | } |
1677 | 1677 | return $cpt_models; |
@@ -194,7 +194,7 @@ discard block |
||
194 | 194 | * PLZ NOTE: the pointer is reset to the beginning of the collection before returning |
195 | 195 | * |
196 | 196 | * @param mixed $identifier |
197 | - * @return mixed |
|
197 | + * @return boolean |
|
198 | 198 | */ |
199 | 199 | public function get($identifier) |
200 | 200 | { |
@@ -309,7 +309,7 @@ discard block |
||
309 | 309 | * setCurrentUsingObject |
310 | 310 | * advances pointer to the provided object |
311 | 311 | * |
312 | - * @param $object |
|
312 | + * @param \EventEspresso\core\libraries\form_sections\form_handlers\SequentialStepForm $object |
|
313 | 313 | * @return boolean |
314 | 314 | */ |
315 | 315 | public function setCurrentUsingObject($object) |
@@ -347,7 +347,7 @@ discard block |
||
347 | 347 | * |
348 | 348 | * @see http://stackoverflow.com/a/8736013 |
349 | 349 | * @param $object |
350 | - * @return boolean|int|string |
|
350 | + * @return integer |
|
351 | 351 | */ |
352 | 352 | public function indexOf($object) |
353 | 353 | { |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | */ |
67 | 67 | protected function setCollectionInterface($collection_interface) |
68 | 68 | { |
69 | - if (! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
69 | + if ( ! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
70 | 70 | throw new InvalidInterfaceException($collection_interface); |
71 | 71 | } |
72 | 72 | $this->collection_interface = $collection_interface; |
@@ -111,13 +111,13 @@ discard block |
||
111 | 111 | protected function setCollectionIdentifier() |
112 | 112 | { |
113 | 113 | // hash a few collection details |
114 | - $identifier = md5(spl_object_hash($this) . $this->collection_interface . time()); |
|
114 | + $identifier = md5(spl_object_hash($this).$this->collection_interface.time()); |
|
115 | 115 | // grab a few characters from the start, middle, and end of the hash |
116 | 116 | $id = array(); |
117 | 117 | for ($x = 0; $x < 19; $x += 9) { |
118 | 118 | $id[] = substr($identifier, $x, 3); |
119 | 119 | } |
120 | - $this->collection_identifier = $this->collection_name . '-' . strtoupper(implode('-', $id)); |
|
120 | + $this->collection_identifier = $this->collection_name.'-'.strtoupper(implode('-', $id)); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | */ |
136 | 136 | public function add($object, $identifier = null) |
137 | 137 | { |
138 | - if (! $object instanceof $this->collection_interface) { |
|
138 | + if ( ! $object instanceof $this->collection_interface) { |
|
139 | 139 | throw new InvalidEntityException($object, $this->collection_interface); |
140 | 140 | } |
141 | 141 | if ($this->contains($object)) { |
@@ -351,7 +351,7 @@ discard block |
||
351 | 351 | */ |
352 | 352 | public function indexOf($object) |
353 | 353 | { |
354 | - if (! $this->contains($object)) { |
|
354 | + if ( ! $this->contains($object)) { |
|
355 | 355 | return false; |
356 | 356 | } |
357 | 357 | foreach ($this as $index => $obj) { |
@@ -420,9 +420,9 @@ discard block |
||
420 | 420 | $remaining_objects = $this->slice($index, $this->count() - $index); |
421 | 421 | foreach ($remaining_objects as $key => $remaining_object) { |
422 | 422 | // we need to grab the identifiers for each object and use them as keys |
423 | - $remaining_objects[ $remaining_object->getInfo() ] = $remaining_object; |
|
423 | + $remaining_objects[$remaining_object->getInfo()] = $remaining_object; |
|
424 | 424 | // and then remove the object from the current tracking array |
425 | - unset($remaining_objects[ $key ]); |
|
425 | + unset($remaining_objects[$key]); |
|
426 | 426 | // and then remove it from the Collection |
427 | 427 | $this->detach($remaining_object); |
428 | 428 | } |
@@ -446,17 +446,17 @@ discard block |
||
446 | 446 | */ |
447 | 447 | public function insertAt($objects, $index) |
448 | 448 | { |
449 | - if (! is_array($objects)) { |
|
449 | + if ( ! is_array($objects)) { |
|
450 | 450 | $objects = array($objects); |
451 | 451 | } |
452 | 452 | // check to ensure that objects don't already exist in the collection |
453 | 453 | foreach ($objects as $key => $object) { |
454 | 454 | if ($this->contains($object)) { |
455 | - unset($objects[ $key ]); |
|
455 | + unset($objects[$key]); |
|
456 | 456 | } |
457 | 457 | } |
458 | 458 | // do we have any objects left? |
459 | - if (! $objects) { |
|
459 | + if ( ! $objects) { |
|
460 | 460 | return; |
461 | 461 | } |
462 | 462 | // detach any objects at or past this index |
@@ -19,504 +19,504 @@ |
||
19 | 19 | class Collection extends SplObjectStorage implements CollectionInterface |
20 | 20 | { |
21 | 21 | |
22 | - /** |
|
23 | - * a unique string for identifying this collection |
|
24 | - * |
|
25 | - * @type string $collection_identifier |
|
26 | - */ |
|
27 | - protected $collection_identifier; |
|
28 | - |
|
29 | - |
|
30 | - /** |
|
31 | - * an interface (or class) name to be used for restricting the type of objects added to the storage |
|
32 | - * this should be set from within the child class constructor |
|
33 | - * |
|
34 | - * @type string $interface |
|
35 | - */ |
|
36 | - protected $collection_interface; |
|
37 | - |
|
38 | - /** |
|
39 | - * a short dash separated string describing the contents of this collection |
|
40 | - * used as the base for the $collection_identifier |
|
41 | - * defaults to the class short name if not set |
|
42 | - * |
|
43 | - * @type string $collection_identifier |
|
44 | - */ |
|
45 | - protected $collection_name; |
|
46 | - |
|
47 | - |
|
48 | - /** |
|
49 | - * Collection constructor |
|
50 | - * |
|
51 | - * @param string $collection_interface |
|
52 | - * @param string $collection_name |
|
53 | - * @throws InvalidInterfaceException |
|
54 | - */ |
|
55 | - public function __construct($collection_interface, $collection_name = '') |
|
56 | - { |
|
57 | - $this->setCollectionInterface($collection_interface); |
|
58 | - $this->setCollectionName($collection_name); |
|
59 | - $this->setCollectionIdentifier(); |
|
60 | - } |
|
61 | - |
|
62 | - |
|
63 | - /** |
|
64 | - * setCollectionInterface |
|
65 | - * |
|
66 | - * @param string $collection_interface |
|
67 | - * @throws InvalidInterfaceException |
|
68 | - */ |
|
69 | - protected function setCollectionInterface($collection_interface) |
|
70 | - { |
|
71 | - if (! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
72 | - throw new InvalidInterfaceException($collection_interface); |
|
73 | - } |
|
74 | - $this->collection_interface = $collection_interface; |
|
75 | - } |
|
76 | - |
|
77 | - |
|
78 | - /** |
|
79 | - * @return string |
|
80 | - */ |
|
81 | - public function collectionName() |
|
82 | - { |
|
83 | - return $this->collection_name; |
|
84 | - } |
|
85 | - |
|
86 | - |
|
87 | - /** |
|
88 | - * @param string $collection_name |
|
89 | - */ |
|
90 | - protected function setCollectionName($collection_name) |
|
91 | - { |
|
92 | - $this->collection_name = ! empty($collection_name) |
|
93 | - ? sanitize_key($collection_name) |
|
94 | - : basename(str_replace('\\', '/', get_class($this))); |
|
95 | - } |
|
96 | - |
|
97 | - |
|
98 | - /** |
|
99 | - * @return string |
|
100 | - */ |
|
101 | - public function collectionIdentifier() |
|
102 | - { |
|
103 | - return $this->collection_identifier; |
|
104 | - } |
|
105 | - |
|
106 | - |
|
107 | - /** |
|
108 | - * creates a very readable unique 9 character identifier like: CF2-532-DAC |
|
109 | - * and appends it to the non-qualified class name, ex: ThingCollection-CF2-532-DAC |
|
110 | - * |
|
111 | - * @return void |
|
112 | - */ |
|
113 | - protected function setCollectionIdentifier() |
|
114 | - { |
|
115 | - // hash a few collection details |
|
116 | - $identifier = md5(spl_object_hash($this) . $this->collection_interface . time()); |
|
117 | - // grab a few characters from the start, middle, and end of the hash |
|
118 | - $id = array(); |
|
119 | - for ($x = 0; $x < 19; $x += 9) { |
|
120 | - $id[] = substr($identifier, $x, 3); |
|
121 | - } |
|
122 | - $this->collection_identifier = $this->collection_name . '-' . strtoupper(implode('-', $id)); |
|
123 | - } |
|
124 | - |
|
125 | - |
|
126 | - /** |
|
127 | - * add |
|
128 | - * attaches an object to the Collection |
|
129 | - * and sets any supplied data associated with the current iterator entry |
|
130 | - * by calling EE_Object_Collection::set_identifier() |
|
131 | - * |
|
132 | - * @param $object |
|
133 | - * @param mixed $identifier |
|
134 | - * @return bool |
|
135 | - * @throws InvalidEntityException |
|
136 | - * @throws DuplicateCollectionIdentifierException |
|
137 | - */ |
|
138 | - public function add($object, $identifier = null) |
|
139 | - { |
|
140 | - if (! $object instanceof $this->collection_interface) { |
|
141 | - throw new InvalidEntityException($object, $this->collection_interface); |
|
142 | - } |
|
143 | - if ($this->contains($object)) { |
|
144 | - throw new DuplicateCollectionIdentifierException($identifier); |
|
145 | - } |
|
146 | - $this->attach($object); |
|
147 | - $this->setIdentifier($object, $identifier); |
|
148 | - return $this->contains($object); |
|
149 | - } |
|
150 | - |
|
151 | - |
|
152 | - /** |
|
153 | - * getIdentifier |
|
154 | - * if no $identifier is supplied, then the spl_object_hash() is used |
|
155 | - * |
|
156 | - * @param $object |
|
157 | - * @param mixed $identifier |
|
158 | - * @return bool |
|
159 | - */ |
|
160 | - public function getIdentifier($object, $identifier = null) |
|
161 | - { |
|
162 | - return ! empty($identifier) |
|
163 | - ? $identifier |
|
164 | - : spl_object_hash($object); |
|
165 | - } |
|
166 | - |
|
167 | - |
|
168 | - /** |
|
169 | - * setIdentifier |
|
170 | - * Sets the data associated with an object in the Collection |
|
171 | - * if no $identifier is supplied, then the spl_object_hash() is used |
|
172 | - * |
|
173 | - * @param $object |
|
174 | - * @param mixed $identifier |
|
175 | - * @return bool |
|
176 | - */ |
|
177 | - public function setIdentifier($object, $identifier = null) |
|
178 | - { |
|
179 | - $identifier = $this->getIdentifier($object, $identifier); |
|
180 | - $this->rewind(); |
|
181 | - while ($this->valid()) { |
|
182 | - if ($object === $this->current()) { |
|
183 | - $this->setInfo($identifier); |
|
184 | - $this->rewind(); |
|
185 | - return true; |
|
186 | - } |
|
187 | - $this->next(); |
|
188 | - } |
|
189 | - return false; |
|
190 | - } |
|
191 | - |
|
192 | - |
|
193 | - /** |
|
194 | - * get |
|
195 | - * finds and returns an object in the Collection based on the identifier that was set using addObject() |
|
196 | - * PLZ NOTE: the pointer is reset to the beginning of the collection before returning |
|
197 | - * |
|
198 | - * @param mixed $identifier |
|
199 | - * @return mixed |
|
200 | - */ |
|
201 | - public function get($identifier) |
|
202 | - { |
|
203 | - $this->rewind(); |
|
204 | - while ($this->valid()) { |
|
205 | - if ($identifier === $this->getInfo()) { |
|
206 | - $object = $this->current(); |
|
207 | - $this->rewind(); |
|
208 | - return $object; |
|
209 | - } |
|
210 | - $this->next(); |
|
211 | - } |
|
212 | - return null; |
|
213 | - } |
|
214 | - |
|
215 | - |
|
216 | - /** |
|
217 | - * has |
|
218 | - * returns TRUE or FALSE |
|
219 | - * depending on whether the object is within the Collection |
|
220 | - * based on the supplied $identifier |
|
221 | - * |
|
222 | - * @param mixed $identifier |
|
223 | - * @return bool |
|
224 | - */ |
|
225 | - public function has($identifier) |
|
226 | - { |
|
227 | - $this->rewind(); |
|
228 | - while ($this->valid()) { |
|
229 | - if ($identifier === $this->getInfo()) { |
|
230 | - $this->rewind(); |
|
231 | - return true; |
|
232 | - } |
|
233 | - $this->next(); |
|
234 | - } |
|
235 | - return false; |
|
236 | - } |
|
237 | - |
|
238 | - |
|
239 | - /** |
|
240 | - * hasObject |
|
241 | - * returns TRUE or FALSE depending on whether the supplied object is within the Collection |
|
242 | - * |
|
243 | - * @param $object |
|
244 | - * @return bool |
|
245 | - */ |
|
246 | - public function hasObject($object) |
|
247 | - { |
|
248 | - return $this->contains($object); |
|
249 | - } |
|
250 | - |
|
251 | - |
|
252 | - /** |
|
253 | - * hasObjects |
|
254 | - * returns true if there are objects within the Collection, and false if it is empty |
|
255 | - * |
|
256 | - * @return bool |
|
257 | - */ |
|
258 | - public function hasObjects() |
|
259 | - { |
|
260 | - return $this->count() !== 0; |
|
261 | - } |
|
262 | - |
|
263 | - |
|
264 | - /** |
|
265 | - * isEmpty |
|
266 | - * returns true if there are no objects within the Collection, and false if there are |
|
267 | - * |
|
268 | - * @return bool |
|
269 | - */ |
|
270 | - public function isEmpty() |
|
271 | - { |
|
272 | - return $this->count() === 0; |
|
273 | - } |
|
274 | - |
|
275 | - |
|
276 | - /** |
|
277 | - * remove |
|
278 | - * detaches an object from the Collection |
|
279 | - * |
|
280 | - * @param $object |
|
281 | - * @return bool |
|
282 | - */ |
|
283 | - public function remove($object) |
|
284 | - { |
|
285 | - $this->detach($object); |
|
286 | - return true; |
|
287 | - } |
|
288 | - |
|
289 | - |
|
290 | - /** |
|
291 | - * setCurrent |
|
292 | - * advances pointer to the object whose identifier matches that which was provided |
|
293 | - * |
|
294 | - * @param mixed $identifier |
|
295 | - * @return boolean |
|
296 | - */ |
|
297 | - public function setCurrent($identifier) |
|
298 | - { |
|
299 | - $this->rewind(); |
|
300 | - while ($this->valid()) { |
|
301 | - if ($identifier === $this->getInfo()) { |
|
302 | - return true; |
|
303 | - } |
|
304 | - $this->next(); |
|
305 | - } |
|
306 | - return false; |
|
307 | - } |
|
308 | - |
|
309 | - |
|
310 | - /** |
|
311 | - * setCurrentUsingObject |
|
312 | - * advances pointer to the provided object |
|
313 | - * |
|
314 | - * @param $object |
|
315 | - * @return boolean |
|
316 | - */ |
|
317 | - public function setCurrentUsingObject($object) |
|
318 | - { |
|
319 | - $this->rewind(); |
|
320 | - while ($this->valid()) { |
|
321 | - if ($this->current() === $object) { |
|
322 | - return true; |
|
323 | - } |
|
324 | - $this->next(); |
|
325 | - } |
|
326 | - return false; |
|
327 | - } |
|
328 | - |
|
329 | - |
|
330 | - /** |
|
331 | - * Returns the object occupying the index before the current object, |
|
332 | - * unless this is already the first object, in which case it just returns the first object |
|
333 | - * |
|
334 | - * @return mixed |
|
335 | - */ |
|
336 | - public function previous() |
|
337 | - { |
|
338 | - $index = $this->indexOf($this->current()); |
|
339 | - if ($index === 0) { |
|
340 | - return $this->current(); |
|
341 | - } |
|
342 | - $index--; |
|
343 | - return $this->objectAtIndex($index); |
|
344 | - } |
|
345 | - |
|
346 | - |
|
347 | - /** |
|
348 | - * Returns the index of a given object, or false if not found |
|
349 | - * |
|
350 | - * @see http://stackoverflow.com/a/8736013 |
|
351 | - * @param $object |
|
352 | - * @return boolean|int|string |
|
353 | - */ |
|
354 | - public function indexOf($object) |
|
355 | - { |
|
356 | - if (! $this->contains($object)) { |
|
357 | - return false; |
|
358 | - } |
|
359 | - foreach ($this as $index => $obj) { |
|
360 | - if ($obj === $object) { |
|
361 | - return $index; |
|
362 | - } |
|
363 | - } |
|
364 | - return false; |
|
365 | - } |
|
366 | - |
|
367 | - |
|
368 | - /** |
|
369 | - * Returns the object at the given index |
|
370 | - * |
|
371 | - * @see http://stackoverflow.com/a/8736013 |
|
372 | - * @param int $index |
|
373 | - * @return mixed |
|
374 | - */ |
|
375 | - public function objectAtIndex($index) |
|
376 | - { |
|
377 | - $iterator = new LimitIterator($this, $index, 1); |
|
378 | - $iterator->rewind(); |
|
379 | - return $iterator->current(); |
|
380 | - } |
|
381 | - |
|
382 | - |
|
383 | - /** |
|
384 | - * Returns the sequence of objects as specified by the offset and length |
|
385 | - * |
|
386 | - * @see http://stackoverflow.com/a/8736013 |
|
387 | - * @param int $offset |
|
388 | - * @param int $length |
|
389 | - * @return array |
|
390 | - */ |
|
391 | - public function slice($offset, $length) |
|
392 | - { |
|
393 | - $slice = array(); |
|
394 | - $iterator = new LimitIterator($this, $offset, $length); |
|
395 | - foreach ($iterator as $object) { |
|
396 | - $slice[] = $object; |
|
397 | - } |
|
398 | - return $slice; |
|
399 | - } |
|
400 | - |
|
401 | - |
|
402 | - /** |
|
403 | - * Inserts an object at a certain point |
|
404 | - * |
|
405 | - * @see http://stackoverflow.com/a/8736013 |
|
406 | - * @param mixed $object A single object |
|
407 | - * @param int $index |
|
408 | - * @param mixed $identifier |
|
409 | - * @return bool |
|
410 | - * @throws DuplicateCollectionIdentifierException |
|
411 | - * @throws InvalidEntityException |
|
412 | - */ |
|
413 | - public function insertObjectAt($object, $index, $identifier = null) |
|
414 | - { |
|
415 | - // check to ensure that objects don't already exist in the collection |
|
416 | - if ($this->has($identifier)) { |
|
417 | - throw new DuplicateCollectionIdentifierException($identifier); |
|
418 | - } |
|
419 | - // detach any objects at or past this index |
|
420 | - $remaining_objects = array(); |
|
421 | - if ($index < $this->count()) { |
|
422 | - $remaining_objects = $this->slice($index, $this->count() - $index); |
|
423 | - foreach ($remaining_objects as $key => $remaining_object) { |
|
424 | - // we need to grab the identifiers for each object and use them as keys |
|
425 | - $remaining_objects[ $remaining_object->getInfo() ] = $remaining_object; |
|
426 | - // and then remove the object from the current tracking array |
|
427 | - unset($remaining_objects[ $key ]); |
|
428 | - // and then remove it from the Collection |
|
429 | - $this->detach($remaining_object); |
|
430 | - } |
|
431 | - } |
|
432 | - // add the new object we're splicing in |
|
433 | - $this->add($object, $identifier); |
|
434 | - // attach the objects we previously detached |
|
435 | - foreach ($remaining_objects as $key => $remaining_object) { |
|
436 | - $this->add($remaining_object, $key); |
|
437 | - } |
|
438 | - return $this->contains($object); |
|
439 | - } |
|
440 | - |
|
441 | - |
|
442 | - /** |
|
443 | - * Inserts an object (or an array of objects) at a certain point |
|
444 | - * |
|
445 | - * @see http://stackoverflow.com/a/8736013 |
|
446 | - * @param mixed $objects A single object or an array of objects |
|
447 | - * @param int $index |
|
448 | - */ |
|
449 | - public function insertAt($objects, $index) |
|
450 | - { |
|
451 | - if (! is_array($objects)) { |
|
452 | - $objects = array($objects); |
|
453 | - } |
|
454 | - // check to ensure that objects don't already exist in the collection |
|
455 | - foreach ($objects as $key => $object) { |
|
456 | - if ($this->contains($object)) { |
|
457 | - unset($objects[ $key ]); |
|
458 | - } |
|
459 | - } |
|
460 | - // do we have any objects left? |
|
461 | - if (! $objects) { |
|
462 | - return; |
|
463 | - } |
|
464 | - // detach any objects at or past this index |
|
465 | - $remaining = array(); |
|
466 | - if ($index < $this->count()) { |
|
467 | - $remaining = $this->slice($index, $this->count() - $index); |
|
468 | - foreach ($remaining as $object) { |
|
469 | - $this->detach($object); |
|
470 | - } |
|
471 | - } |
|
472 | - // add the new objects we're splicing in |
|
473 | - foreach ($objects as $object) { |
|
474 | - $this->attach($object); |
|
475 | - } |
|
476 | - // attach the objects we previously detached |
|
477 | - foreach ($remaining as $object) { |
|
478 | - $this->attach($object); |
|
479 | - } |
|
480 | - } |
|
481 | - |
|
482 | - |
|
483 | - /** |
|
484 | - * Removes the object at the given index |
|
485 | - * |
|
486 | - * @see http://stackoverflow.com/a/8736013 |
|
487 | - * @param int $index |
|
488 | - */ |
|
489 | - public function removeAt($index) |
|
490 | - { |
|
491 | - $this->detach($this->objectAtIndex($index)); |
|
492 | - } |
|
493 | - |
|
494 | - |
|
495 | - /** |
|
496 | - * detaches ALL objects from the Collection |
|
497 | - */ |
|
498 | - public function detachAll() |
|
499 | - { |
|
500 | - $this->rewind(); |
|
501 | - while ($this->valid()) { |
|
502 | - $object = $this->current(); |
|
503 | - $this->next(); |
|
504 | - $this->detach($object); |
|
505 | - } |
|
506 | - } |
|
507 | - |
|
508 | - |
|
509 | - /** |
|
510 | - * unsets and detaches ALL objects from the Collection |
|
511 | - */ |
|
512 | - public function trashAndDetachAll() |
|
513 | - { |
|
514 | - $this->rewind(); |
|
515 | - while ($this->valid()) { |
|
516 | - $object = $this->current(); |
|
517 | - $this->next(); |
|
518 | - $this->detach($object); |
|
519 | - unset($object); |
|
520 | - } |
|
521 | - } |
|
22 | + /** |
|
23 | + * a unique string for identifying this collection |
|
24 | + * |
|
25 | + * @type string $collection_identifier |
|
26 | + */ |
|
27 | + protected $collection_identifier; |
|
28 | + |
|
29 | + |
|
30 | + /** |
|
31 | + * an interface (or class) name to be used for restricting the type of objects added to the storage |
|
32 | + * this should be set from within the child class constructor |
|
33 | + * |
|
34 | + * @type string $interface |
|
35 | + */ |
|
36 | + protected $collection_interface; |
|
37 | + |
|
38 | + /** |
|
39 | + * a short dash separated string describing the contents of this collection |
|
40 | + * used as the base for the $collection_identifier |
|
41 | + * defaults to the class short name if not set |
|
42 | + * |
|
43 | + * @type string $collection_identifier |
|
44 | + */ |
|
45 | + protected $collection_name; |
|
46 | + |
|
47 | + |
|
48 | + /** |
|
49 | + * Collection constructor |
|
50 | + * |
|
51 | + * @param string $collection_interface |
|
52 | + * @param string $collection_name |
|
53 | + * @throws InvalidInterfaceException |
|
54 | + */ |
|
55 | + public function __construct($collection_interface, $collection_name = '') |
|
56 | + { |
|
57 | + $this->setCollectionInterface($collection_interface); |
|
58 | + $this->setCollectionName($collection_name); |
|
59 | + $this->setCollectionIdentifier(); |
|
60 | + } |
|
61 | + |
|
62 | + |
|
63 | + /** |
|
64 | + * setCollectionInterface |
|
65 | + * |
|
66 | + * @param string $collection_interface |
|
67 | + * @throws InvalidInterfaceException |
|
68 | + */ |
|
69 | + protected function setCollectionInterface($collection_interface) |
|
70 | + { |
|
71 | + if (! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
72 | + throw new InvalidInterfaceException($collection_interface); |
|
73 | + } |
|
74 | + $this->collection_interface = $collection_interface; |
|
75 | + } |
|
76 | + |
|
77 | + |
|
78 | + /** |
|
79 | + * @return string |
|
80 | + */ |
|
81 | + public function collectionName() |
|
82 | + { |
|
83 | + return $this->collection_name; |
|
84 | + } |
|
85 | + |
|
86 | + |
|
87 | + /** |
|
88 | + * @param string $collection_name |
|
89 | + */ |
|
90 | + protected function setCollectionName($collection_name) |
|
91 | + { |
|
92 | + $this->collection_name = ! empty($collection_name) |
|
93 | + ? sanitize_key($collection_name) |
|
94 | + : basename(str_replace('\\', '/', get_class($this))); |
|
95 | + } |
|
96 | + |
|
97 | + |
|
98 | + /** |
|
99 | + * @return string |
|
100 | + */ |
|
101 | + public function collectionIdentifier() |
|
102 | + { |
|
103 | + return $this->collection_identifier; |
|
104 | + } |
|
105 | + |
|
106 | + |
|
107 | + /** |
|
108 | + * creates a very readable unique 9 character identifier like: CF2-532-DAC |
|
109 | + * and appends it to the non-qualified class name, ex: ThingCollection-CF2-532-DAC |
|
110 | + * |
|
111 | + * @return void |
|
112 | + */ |
|
113 | + protected function setCollectionIdentifier() |
|
114 | + { |
|
115 | + // hash a few collection details |
|
116 | + $identifier = md5(spl_object_hash($this) . $this->collection_interface . time()); |
|
117 | + // grab a few characters from the start, middle, and end of the hash |
|
118 | + $id = array(); |
|
119 | + for ($x = 0; $x < 19; $x += 9) { |
|
120 | + $id[] = substr($identifier, $x, 3); |
|
121 | + } |
|
122 | + $this->collection_identifier = $this->collection_name . '-' . strtoupper(implode('-', $id)); |
|
123 | + } |
|
124 | + |
|
125 | + |
|
126 | + /** |
|
127 | + * add |
|
128 | + * attaches an object to the Collection |
|
129 | + * and sets any supplied data associated with the current iterator entry |
|
130 | + * by calling EE_Object_Collection::set_identifier() |
|
131 | + * |
|
132 | + * @param $object |
|
133 | + * @param mixed $identifier |
|
134 | + * @return bool |
|
135 | + * @throws InvalidEntityException |
|
136 | + * @throws DuplicateCollectionIdentifierException |
|
137 | + */ |
|
138 | + public function add($object, $identifier = null) |
|
139 | + { |
|
140 | + if (! $object instanceof $this->collection_interface) { |
|
141 | + throw new InvalidEntityException($object, $this->collection_interface); |
|
142 | + } |
|
143 | + if ($this->contains($object)) { |
|
144 | + throw new DuplicateCollectionIdentifierException($identifier); |
|
145 | + } |
|
146 | + $this->attach($object); |
|
147 | + $this->setIdentifier($object, $identifier); |
|
148 | + return $this->contains($object); |
|
149 | + } |
|
150 | + |
|
151 | + |
|
152 | + /** |
|
153 | + * getIdentifier |
|
154 | + * if no $identifier is supplied, then the spl_object_hash() is used |
|
155 | + * |
|
156 | + * @param $object |
|
157 | + * @param mixed $identifier |
|
158 | + * @return bool |
|
159 | + */ |
|
160 | + public function getIdentifier($object, $identifier = null) |
|
161 | + { |
|
162 | + return ! empty($identifier) |
|
163 | + ? $identifier |
|
164 | + : spl_object_hash($object); |
|
165 | + } |
|
166 | + |
|
167 | + |
|
168 | + /** |
|
169 | + * setIdentifier |
|
170 | + * Sets the data associated with an object in the Collection |
|
171 | + * if no $identifier is supplied, then the spl_object_hash() is used |
|
172 | + * |
|
173 | + * @param $object |
|
174 | + * @param mixed $identifier |
|
175 | + * @return bool |
|
176 | + */ |
|
177 | + public function setIdentifier($object, $identifier = null) |
|
178 | + { |
|
179 | + $identifier = $this->getIdentifier($object, $identifier); |
|
180 | + $this->rewind(); |
|
181 | + while ($this->valid()) { |
|
182 | + if ($object === $this->current()) { |
|
183 | + $this->setInfo($identifier); |
|
184 | + $this->rewind(); |
|
185 | + return true; |
|
186 | + } |
|
187 | + $this->next(); |
|
188 | + } |
|
189 | + return false; |
|
190 | + } |
|
191 | + |
|
192 | + |
|
193 | + /** |
|
194 | + * get |
|
195 | + * finds and returns an object in the Collection based on the identifier that was set using addObject() |
|
196 | + * PLZ NOTE: the pointer is reset to the beginning of the collection before returning |
|
197 | + * |
|
198 | + * @param mixed $identifier |
|
199 | + * @return mixed |
|
200 | + */ |
|
201 | + public function get($identifier) |
|
202 | + { |
|
203 | + $this->rewind(); |
|
204 | + while ($this->valid()) { |
|
205 | + if ($identifier === $this->getInfo()) { |
|
206 | + $object = $this->current(); |
|
207 | + $this->rewind(); |
|
208 | + return $object; |
|
209 | + } |
|
210 | + $this->next(); |
|
211 | + } |
|
212 | + return null; |
|
213 | + } |
|
214 | + |
|
215 | + |
|
216 | + /** |
|
217 | + * has |
|
218 | + * returns TRUE or FALSE |
|
219 | + * depending on whether the object is within the Collection |
|
220 | + * based on the supplied $identifier |
|
221 | + * |
|
222 | + * @param mixed $identifier |
|
223 | + * @return bool |
|
224 | + */ |
|
225 | + public function has($identifier) |
|
226 | + { |
|
227 | + $this->rewind(); |
|
228 | + while ($this->valid()) { |
|
229 | + if ($identifier === $this->getInfo()) { |
|
230 | + $this->rewind(); |
|
231 | + return true; |
|
232 | + } |
|
233 | + $this->next(); |
|
234 | + } |
|
235 | + return false; |
|
236 | + } |
|
237 | + |
|
238 | + |
|
239 | + /** |
|
240 | + * hasObject |
|
241 | + * returns TRUE or FALSE depending on whether the supplied object is within the Collection |
|
242 | + * |
|
243 | + * @param $object |
|
244 | + * @return bool |
|
245 | + */ |
|
246 | + public function hasObject($object) |
|
247 | + { |
|
248 | + return $this->contains($object); |
|
249 | + } |
|
250 | + |
|
251 | + |
|
252 | + /** |
|
253 | + * hasObjects |
|
254 | + * returns true if there are objects within the Collection, and false if it is empty |
|
255 | + * |
|
256 | + * @return bool |
|
257 | + */ |
|
258 | + public function hasObjects() |
|
259 | + { |
|
260 | + return $this->count() !== 0; |
|
261 | + } |
|
262 | + |
|
263 | + |
|
264 | + /** |
|
265 | + * isEmpty |
|
266 | + * returns true if there are no objects within the Collection, and false if there are |
|
267 | + * |
|
268 | + * @return bool |
|
269 | + */ |
|
270 | + public function isEmpty() |
|
271 | + { |
|
272 | + return $this->count() === 0; |
|
273 | + } |
|
274 | + |
|
275 | + |
|
276 | + /** |
|
277 | + * remove |
|
278 | + * detaches an object from the Collection |
|
279 | + * |
|
280 | + * @param $object |
|
281 | + * @return bool |
|
282 | + */ |
|
283 | + public function remove($object) |
|
284 | + { |
|
285 | + $this->detach($object); |
|
286 | + return true; |
|
287 | + } |
|
288 | + |
|
289 | + |
|
290 | + /** |
|
291 | + * setCurrent |
|
292 | + * advances pointer to the object whose identifier matches that which was provided |
|
293 | + * |
|
294 | + * @param mixed $identifier |
|
295 | + * @return boolean |
|
296 | + */ |
|
297 | + public function setCurrent($identifier) |
|
298 | + { |
|
299 | + $this->rewind(); |
|
300 | + while ($this->valid()) { |
|
301 | + if ($identifier === $this->getInfo()) { |
|
302 | + return true; |
|
303 | + } |
|
304 | + $this->next(); |
|
305 | + } |
|
306 | + return false; |
|
307 | + } |
|
308 | + |
|
309 | + |
|
310 | + /** |
|
311 | + * setCurrentUsingObject |
|
312 | + * advances pointer to the provided object |
|
313 | + * |
|
314 | + * @param $object |
|
315 | + * @return boolean |
|
316 | + */ |
|
317 | + public function setCurrentUsingObject($object) |
|
318 | + { |
|
319 | + $this->rewind(); |
|
320 | + while ($this->valid()) { |
|
321 | + if ($this->current() === $object) { |
|
322 | + return true; |
|
323 | + } |
|
324 | + $this->next(); |
|
325 | + } |
|
326 | + return false; |
|
327 | + } |
|
328 | + |
|
329 | + |
|
330 | + /** |
|
331 | + * Returns the object occupying the index before the current object, |
|
332 | + * unless this is already the first object, in which case it just returns the first object |
|
333 | + * |
|
334 | + * @return mixed |
|
335 | + */ |
|
336 | + public function previous() |
|
337 | + { |
|
338 | + $index = $this->indexOf($this->current()); |
|
339 | + if ($index === 0) { |
|
340 | + return $this->current(); |
|
341 | + } |
|
342 | + $index--; |
|
343 | + return $this->objectAtIndex($index); |
|
344 | + } |
|
345 | + |
|
346 | + |
|
347 | + /** |
|
348 | + * Returns the index of a given object, or false if not found |
|
349 | + * |
|
350 | + * @see http://stackoverflow.com/a/8736013 |
|
351 | + * @param $object |
|
352 | + * @return boolean|int|string |
|
353 | + */ |
|
354 | + public function indexOf($object) |
|
355 | + { |
|
356 | + if (! $this->contains($object)) { |
|
357 | + return false; |
|
358 | + } |
|
359 | + foreach ($this as $index => $obj) { |
|
360 | + if ($obj === $object) { |
|
361 | + return $index; |
|
362 | + } |
|
363 | + } |
|
364 | + return false; |
|
365 | + } |
|
366 | + |
|
367 | + |
|
368 | + /** |
|
369 | + * Returns the object at the given index |
|
370 | + * |
|
371 | + * @see http://stackoverflow.com/a/8736013 |
|
372 | + * @param int $index |
|
373 | + * @return mixed |
|
374 | + */ |
|
375 | + public function objectAtIndex($index) |
|
376 | + { |
|
377 | + $iterator = new LimitIterator($this, $index, 1); |
|
378 | + $iterator->rewind(); |
|
379 | + return $iterator->current(); |
|
380 | + } |
|
381 | + |
|
382 | + |
|
383 | + /** |
|
384 | + * Returns the sequence of objects as specified by the offset and length |
|
385 | + * |
|
386 | + * @see http://stackoverflow.com/a/8736013 |
|
387 | + * @param int $offset |
|
388 | + * @param int $length |
|
389 | + * @return array |
|
390 | + */ |
|
391 | + public function slice($offset, $length) |
|
392 | + { |
|
393 | + $slice = array(); |
|
394 | + $iterator = new LimitIterator($this, $offset, $length); |
|
395 | + foreach ($iterator as $object) { |
|
396 | + $slice[] = $object; |
|
397 | + } |
|
398 | + return $slice; |
|
399 | + } |
|
400 | + |
|
401 | + |
|
402 | + /** |
|
403 | + * Inserts an object at a certain point |
|
404 | + * |
|
405 | + * @see http://stackoverflow.com/a/8736013 |
|
406 | + * @param mixed $object A single object |
|
407 | + * @param int $index |
|
408 | + * @param mixed $identifier |
|
409 | + * @return bool |
|
410 | + * @throws DuplicateCollectionIdentifierException |
|
411 | + * @throws InvalidEntityException |
|
412 | + */ |
|
413 | + public function insertObjectAt($object, $index, $identifier = null) |
|
414 | + { |
|
415 | + // check to ensure that objects don't already exist in the collection |
|
416 | + if ($this->has($identifier)) { |
|
417 | + throw new DuplicateCollectionIdentifierException($identifier); |
|
418 | + } |
|
419 | + // detach any objects at or past this index |
|
420 | + $remaining_objects = array(); |
|
421 | + if ($index < $this->count()) { |
|
422 | + $remaining_objects = $this->slice($index, $this->count() - $index); |
|
423 | + foreach ($remaining_objects as $key => $remaining_object) { |
|
424 | + // we need to grab the identifiers for each object and use them as keys |
|
425 | + $remaining_objects[ $remaining_object->getInfo() ] = $remaining_object; |
|
426 | + // and then remove the object from the current tracking array |
|
427 | + unset($remaining_objects[ $key ]); |
|
428 | + // and then remove it from the Collection |
|
429 | + $this->detach($remaining_object); |
|
430 | + } |
|
431 | + } |
|
432 | + // add the new object we're splicing in |
|
433 | + $this->add($object, $identifier); |
|
434 | + // attach the objects we previously detached |
|
435 | + foreach ($remaining_objects as $key => $remaining_object) { |
|
436 | + $this->add($remaining_object, $key); |
|
437 | + } |
|
438 | + return $this->contains($object); |
|
439 | + } |
|
440 | + |
|
441 | + |
|
442 | + /** |
|
443 | + * Inserts an object (or an array of objects) at a certain point |
|
444 | + * |
|
445 | + * @see http://stackoverflow.com/a/8736013 |
|
446 | + * @param mixed $objects A single object or an array of objects |
|
447 | + * @param int $index |
|
448 | + */ |
|
449 | + public function insertAt($objects, $index) |
|
450 | + { |
|
451 | + if (! is_array($objects)) { |
|
452 | + $objects = array($objects); |
|
453 | + } |
|
454 | + // check to ensure that objects don't already exist in the collection |
|
455 | + foreach ($objects as $key => $object) { |
|
456 | + if ($this->contains($object)) { |
|
457 | + unset($objects[ $key ]); |
|
458 | + } |
|
459 | + } |
|
460 | + // do we have any objects left? |
|
461 | + if (! $objects) { |
|
462 | + return; |
|
463 | + } |
|
464 | + // detach any objects at or past this index |
|
465 | + $remaining = array(); |
|
466 | + if ($index < $this->count()) { |
|
467 | + $remaining = $this->slice($index, $this->count() - $index); |
|
468 | + foreach ($remaining as $object) { |
|
469 | + $this->detach($object); |
|
470 | + } |
|
471 | + } |
|
472 | + // add the new objects we're splicing in |
|
473 | + foreach ($objects as $object) { |
|
474 | + $this->attach($object); |
|
475 | + } |
|
476 | + // attach the objects we previously detached |
|
477 | + foreach ($remaining as $object) { |
|
478 | + $this->attach($object); |
|
479 | + } |
|
480 | + } |
|
481 | + |
|
482 | + |
|
483 | + /** |
|
484 | + * Removes the object at the given index |
|
485 | + * |
|
486 | + * @see http://stackoverflow.com/a/8736013 |
|
487 | + * @param int $index |
|
488 | + */ |
|
489 | + public function removeAt($index) |
|
490 | + { |
|
491 | + $this->detach($this->objectAtIndex($index)); |
|
492 | + } |
|
493 | + |
|
494 | + |
|
495 | + /** |
|
496 | + * detaches ALL objects from the Collection |
|
497 | + */ |
|
498 | + public function detachAll() |
|
499 | + { |
|
500 | + $this->rewind(); |
|
501 | + while ($this->valid()) { |
|
502 | + $object = $this->current(); |
|
503 | + $this->next(); |
|
504 | + $this->detach($object); |
|
505 | + } |
|
506 | + } |
|
507 | + |
|
508 | + |
|
509 | + /** |
|
510 | + * unsets and detaches ALL objects from the Collection |
|
511 | + */ |
|
512 | + public function trashAndDetachAll() |
|
513 | + { |
|
514 | + $this->rewind(); |
|
515 | + while ($this->valid()) { |
|
516 | + $object = $this->current(); |
|
517 | + $this->next(); |
|
518 | + $this->detach($object); |
|
519 | + unset($object); |
|
520 | + } |
|
521 | + } |
|
522 | 522 | } |
@@ -175,7 +175,7 @@ |
||
175 | 175 | |
176 | 176 | /** |
177 | 177 | * @param $entity |
178 | - * @param mixed $identifier |
|
178 | + * @param string $identifier |
|
179 | 179 | * @return string |
180 | 180 | * @throws InvalidEntityException |
181 | 181 | * @throws DuplicateCollectionIdentifierException |
@@ -29,295 +29,295 @@ |
||
29 | 29 | class CollectionLoader |
30 | 30 | { |
31 | 31 | |
32 | - /** |
|
33 | - * possible return value when adding entities to a collection. |
|
34 | - * denotes that the entity was NOT ADDED to the collection |
|
35 | - */ |
|
36 | - const ENTITY_NOT_ADDED = 'entity-not-added-to-collection'; |
|
32 | + /** |
|
33 | + * possible return value when adding entities to a collection. |
|
34 | + * denotes that the entity was NOT ADDED to the collection |
|
35 | + */ |
|
36 | + const ENTITY_NOT_ADDED = 'entity-not-added-to-collection'; |
|
37 | 37 | |
38 | - /** |
|
39 | - * possible return value when adding entities to a collection. |
|
40 | - * denotes that the entity was SUCCESSFULLY ADDED to the collection |
|
41 | - */ |
|
42 | - const ENTITY_ADDED = 'entity-added-to-collection'; |
|
38 | + /** |
|
39 | + * possible return value when adding entities to a collection. |
|
40 | + * denotes that the entity was SUCCESSFULLY ADDED to the collection |
|
41 | + */ |
|
42 | + const ENTITY_ADDED = 'entity-added-to-collection'; |
|
43 | 43 | |
44 | - /** |
|
45 | - * possible return value when adding entities to a collection. |
|
46 | - * denotes that the entity was ALREADY ADDED to the collection, |
|
47 | - * and therefore could not be added again. |
|
48 | - */ |
|
49 | - const ENTITY_EXISTS = 'entity-already-in-collection'; |
|
44 | + /** |
|
45 | + * possible return value when adding entities to a collection. |
|
46 | + * denotes that the entity was ALREADY ADDED to the collection, |
|
47 | + * and therefore could not be added again. |
|
48 | + */ |
|
49 | + const ENTITY_EXISTS = 'entity-already-in-collection'; |
|
50 | 50 | |
51 | 51 | |
52 | - /** |
|
53 | - * @var CollectionDetailsInterface $collection_details |
|
54 | - */ |
|
55 | - protected $collection_details; |
|
52 | + /** |
|
53 | + * @var CollectionDetailsInterface $collection_details |
|
54 | + */ |
|
55 | + protected $collection_details; |
|
56 | 56 | |
57 | - /** |
|
58 | - * @var CollectionInterface $collection |
|
59 | - */ |
|
60 | - protected $collection; |
|
57 | + /** |
|
58 | + * @var CollectionInterface $collection |
|
59 | + */ |
|
60 | + protected $collection; |
|
61 | 61 | |
62 | - /** |
|
63 | - * @var FactoryInterface $entity_factory |
|
64 | - */ |
|
65 | - protected $entity_factory; |
|
62 | + /** |
|
63 | + * @var FactoryInterface $entity_factory |
|
64 | + */ |
|
65 | + protected $entity_factory; |
|
66 | 66 | |
67 | - /** |
|
68 | - * @var FileLocator $file_locator |
|
69 | - */ |
|
70 | - protected $file_locator; |
|
67 | + /** |
|
68 | + * @var FileLocator $file_locator |
|
69 | + */ |
|
70 | + protected $file_locator; |
|
71 | 71 | |
72 | 72 | |
73 | - /** |
|
74 | - * CollectionLoader constructor. |
|
75 | - * |
|
76 | - * @param CollectionDetailsInterface $collection_details |
|
77 | - * @param CollectionInterface $collection |
|
78 | - * @param LocatorInterface $file_locator |
|
79 | - * @param FactoryInterface|null $entity_factory |
|
80 | - * @throws CollectionLoaderException |
|
81 | - */ |
|
82 | - public function __construct( |
|
83 | - CollectionDetailsInterface $collection_details, |
|
84 | - CollectionInterface $collection = null, |
|
85 | - LocatorInterface $file_locator = null, |
|
86 | - FactoryInterface $entity_factory = null |
|
87 | - ) { |
|
88 | - try { |
|
89 | - $this->collection_details = $collection_details; |
|
90 | - if (! $collection instanceof CollectionInterface) { |
|
91 | - $collection = new Collection($this->collection_details->getCollectionInterface()); |
|
92 | - } |
|
93 | - $this->collection = $collection; |
|
94 | - $this->file_locator = $file_locator; |
|
95 | - $this->entity_factory = $entity_factory; |
|
96 | - $this->loadAllFromFilepaths(); |
|
97 | - $this->loadFromFQCNs(); |
|
98 | - } catch (Exception $exception) { |
|
99 | - throw new CollectionLoaderException($exception); |
|
100 | - } |
|
101 | - } |
|
73 | + /** |
|
74 | + * CollectionLoader constructor. |
|
75 | + * |
|
76 | + * @param CollectionDetailsInterface $collection_details |
|
77 | + * @param CollectionInterface $collection |
|
78 | + * @param LocatorInterface $file_locator |
|
79 | + * @param FactoryInterface|null $entity_factory |
|
80 | + * @throws CollectionLoaderException |
|
81 | + */ |
|
82 | + public function __construct( |
|
83 | + CollectionDetailsInterface $collection_details, |
|
84 | + CollectionInterface $collection = null, |
|
85 | + LocatorInterface $file_locator = null, |
|
86 | + FactoryInterface $entity_factory = null |
|
87 | + ) { |
|
88 | + try { |
|
89 | + $this->collection_details = $collection_details; |
|
90 | + if (! $collection instanceof CollectionInterface) { |
|
91 | + $collection = new Collection($this->collection_details->getCollectionInterface()); |
|
92 | + } |
|
93 | + $this->collection = $collection; |
|
94 | + $this->file_locator = $file_locator; |
|
95 | + $this->entity_factory = $entity_factory; |
|
96 | + $this->loadAllFromFilepaths(); |
|
97 | + $this->loadFromFQCNs(); |
|
98 | + } catch (Exception $exception) { |
|
99 | + throw new CollectionLoaderException($exception); |
|
100 | + } |
|
101 | + } |
|
102 | 102 | |
103 | 103 | |
104 | - /** |
|
105 | - * @return CollectionInterface |
|
106 | - */ |
|
107 | - public function getCollection() |
|
108 | - { |
|
109 | - return $this->collection; |
|
110 | - } |
|
104 | + /** |
|
105 | + * @return CollectionInterface |
|
106 | + */ |
|
107 | + public function getCollection() |
|
108 | + { |
|
109 | + return $this->collection; |
|
110 | + } |
|
111 | 111 | |
112 | 112 | |
113 | - /** |
|
114 | - * @throws InvalidClassException |
|
115 | - * @throws InvalidFilePathException |
|
116 | - * @throws InvalidDataTypeException |
|
117 | - * @throws InvalidEntityException |
|
118 | - * @throws DuplicateCollectionIdentifierException |
|
119 | - */ |
|
120 | - protected function loadAllFromFilepaths() |
|
121 | - { |
|
122 | - if (! $this->file_locator instanceof FileLocator) { |
|
123 | - $this->file_locator = new FileLocator(); |
|
124 | - } |
|
125 | - $this->file_locator->setFileMask($this->collection_details->getFileMask()); |
|
126 | - // find all of the files that match the file mask in the specified folder |
|
127 | - $this->file_locator->locate($this->collection_details->getCollectionPaths()); |
|
128 | - // filter the results |
|
129 | - $filepaths = (array) apply_filters( |
|
130 | - 'FHEE__CollectionLoader__loadAllFromFilepath__filepaths', |
|
131 | - $this->file_locator->getFilePaths(), |
|
132 | - $this->collection_details->collectionName(), |
|
133 | - $this->collection_details |
|
134 | - ); |
|
135 | - if (empty($filepaths)) { |
|
136 | - return; |
|
137 | - } |
|
138 | - foreach ($filepaths as $filepath) { |
|
139 | - $this->loadClassFromFilepath($filepath); |
|
140 | - } |
|
141 | - } |
|
113 | + /** |
|
114 | + * @throws InvalidClassException |
|
115 | + * @throws InvalidFilePathException |
|
116 | + * @throws InvalidDataTypeException |
|
117 | + * @throws InvalidEntityException |
|
118 | + * @throws DuplicateCollectionIdentifierException |
|
119 | + */ |
|
120 | + protected function loadAllFromFilepaths() |
|
121 | + { |
|
122 | + if (! $this->file_locator instanceof FileLocator) { |
|
123 | + $this->file_locator = new FileLocator(); |
|
124 | + } |
|
125 | + $this->file_locator->setFileMask($this->collection_details->getFileMask()); |
|
126 | + // find all of the files that match the file mask in the specified folder |
|
127 | + $this->file_locator->locate($this->collection_details->getCollectionPaths()); |
|
128 | + // filter the results |
|
129 | + $filepaths = (array) apply_filters( |
|
130 | + 'FHEE__CollectionLoader__loadAllFromFilepath__filepaths', |
|
131 | + $this->file_locator->getFilePaths(), |
|
132 | + $this->collection_details->collectionName(), |
|
133 | + $this->collection_details |
|
134 | + ); |
|
135 | + if (empty($filepaths)) { |
|
136 | + return; |
|
137 | + } |
|
138 | + foreach ($filepaths as $filepath) { |
|
139 | + $this->loadClassFromFilepath($filepath); |
|
140 | + } |
|
141 | + } |
|
142 | 142 | |
143 | 143 | |
144 | - /** |
|
145 | - * @param string $filepath |
|
146 | - * @return string |
|
147 | - * @throws InvalidEntityException |
|
148 | - * @throws InvalidDataTypeException |
|
149 | - * @throws InvalidFilePathException |
|
150 | - * @throws InvalidClassException |
|
151 | - * @throws DuplicateCollectionIdentifierException |
|
152 | - */ |
|
153 | - protected function loadClassFromFilepath($filepath) |
|
154 | - { |
|
155 | - if (! is_string($filepath)) { |
|
156 | - throw new InvalidDataTypeException('$filepath', $filepath, 'string'); |
|
157 | - } |
|
158 | - if (! is_readable($filepath)) { |
|
159 | - throw new InvalidFilePathException($filepath); |
|
160 | - } |
|
161 | - require_once $filepath; |
|
162 | - // extract filename from path |
|
163 | - $file_name = basename($filepath); |
|
164 | - // now remove any file extensions |
|
165 | - $class_name = EEH_File::get_classname_from_filepath_with_standard_filename($file_name); |
|
166 | - if (! class_exists($class_name)) { |
|
167 | - throw new InvalidClassException($class_name); |
|
168 | - } |
|
169 | - $entity = $this->entity_factory instanceof FactoryInterface |
|
170 | - ? call_user_func(array($this->entity_factory, 'create'), $class_name) |
|
171 | - : new $class_name(); |
|
172 | - return $this->addEntityToCollection($entity, $file_name); |
|
173 | - } |
|
144 | + /** |
|
145 | + * @param string $filepath |
|
146 | + * @return string |
|
147 | + * @throws InvalidEntityException |
|
148 | + * @throws InvalidDataTypeException |
|
149 | + * @throws InvalidFilePathException |
|
150 | + * @throws InvalidClassException |
|
151 | + * @throws DuplicateCollectionIdentifierException |
|
152 | + */ |
|
153 | + protected function loadClassFromFilepath($filepath) |
|
154 | + { |
|
155 | + if (! is_string($filepath)) { |
|
156 | + throw new InvalidDataTypeException('$filepath', $filepath, 'string'); |
|
157 | + } |
|
158 | + if (! is_readable($filepath)) { |
|
159 | + throw new InvalidFilePathException($filepath); |
|
160 | + } |
|
161 | + require_once $filepath; |
|
162 | + // extract filename from path |
|
163 | + $file_name = basename($filepath); |
|
164 | + // now remove any file extensions |
|
165 | + $class_name = EEH_File::get_classname_from_filepath_with_standard_filename($file_name); |
|
166 | + if (! class_exists($class_name)) { |
|
167 | + throw new InvalidClassException($class_name); |
|
168 | + } |
|
169 | + $entity = $this->entity_factory instanceof FactoryInterface |
|
170 | + ? call_user_func(array($this->entity_factory, 'create'), $class_name) |
|
171 | + : new $class_name(); |
|
172 | + return $this->addEntityToCollection($entity, $file_name); |
|
173 | + } |
|
174 | 174 | |
175 | 175 | |
176 | - /** |
|
177 | - * @param $entity |
|
178 | - * @param mixed $identifier |
|
179 | - * @return string |
|
180 | - * @throws InvalidEntityException |
|
181 | - * @throws DuplicateCollectionIdentifierException |
|
182 | - */ |
|
183 | - protected function addEntityToCollection($entity, $identifier) |
|
184 | - { |
|
185 | - do_action( |
|
186 | - 'FHEE__CollectionLoader__addEntityToCollection__entity', |
|
187 | - $entity, |
|
188 | - $this->collection_details->collectionName(), |
|
189 | - $this->collection_details |
|
190 | - ); |
|
191 | - $identifier = $this->setIdentifier($entity, $identifier); |
|
192 | - if ($this->collection->has($identifier)) { |
|
193 | - do_action( |
|
194 | - 'FHEE__CollectionLoader__addEntityToCollection__entity_already_added', |
|
195 | - $this, |
|
196 | - $this->collection_details->collectionName(), |
|
197 | - $this->collection_details |
|
198 | - ); |
|
199 | - return CollectionLoader::ENTITY_EXISTS; |
|
200 | - } |
|
201 | - if ($this->collection->add($entity, $identifier)) { |
|
202 | - do_action( |
|
203 | - 'FHEE__CollectionLoader__addEntityToCollection__entity_added', |
|
204 | - $this, |
|
205 | - $this->collection_details->collectionName(), |
|
206 | - $this->collection_details |
|
207 | - ); |
|
208 | - return CollectionLoader::ENTITY_ADDED; |
|
209 | - } |
|
210 | - do_action( |
|
211 | - 'FHEE__CollectionLoader__addEntityToCollection__entity_not_added', |
|
212 | - $this, |
|
213 | - $this->collection_details->collectionName(), |
|
214 | - $this->collection_details |
|
215 | - ); |
|
216 | - return CollectionLoader::ENTITY_NOT_ADDED; |
|
217 | - } |
|
176 | + /** |
|
177 | + * @param $entity |
|
178 | + * @param mixed $identifier |
|
179 | + * @return string |
|
180 | + * @throws InvalidEntityException |
|
181 | + * @throws DuplicateCollectionIdentifierException |
|
182 | + */ |
|
183 | + protected function addEntityToCollection($entity, $identifier) |
|
184 | + { |
|
185 | + do_action( |
|
186 | + 'FHEE__CollectionLoader__addEntityToCollection__entity', |
|
187 | + $entity, |
|
188 | + $this->collection_details->collectionName(), |
|
189 | + $this->collection_details |
|
190 | + ); |
|
191 | + $identifier = $this->setIdentifier($entity, $identifier); |
|
192 | + if ($this->collection->has($identifier)) { |
|
193 | + do_action( |
|
194 | + 'FHEE__CollectionLoader__addEntityToCollection__entity_already_added', |
|
195 | + $this, |
|
196 | + $this->collection_details->collectionName(), |
|
197 | + $this->collection_details |
|
198 | + ); |
|
199 | + return CollectionLoader::ENTITY_EXISTS; |
|
200 | + } |
|
201 | + if ($this->collection->add($entity, $identifier)) { |
|
202 | + do_action( |
|
203 | + 'FHEE__CollectionLoader__addEntityToCollection__entity_added', |
|
204 | + $this, |
|
205 | + $this->collection_details->collectionName(), |
|
206 | + $this->collection_details |
|
207 | + ); |
|
208 | + return CollectionLoader::ENTITY_ADDED; |
|
209 | + } |
|
210 | + do_action( |
|
211 | + 'FHEE__CollectionLoader__addEntityToCollection__entity_not_added', |
|
212 | + $this, |
|
213 | + $this->collection_details->collectionName(), |
|
214 | + $this->collection_details |
|
215 | + ); |
|
216 | + return CollectionLoader::ENTITY_NOT_ADDED; |
|
217 | + } |
|
218 | 218 | |
219 | 219 | |
220 | - /** |
|
221 | - * @param $entity |
|
222 | - * @param mixed $identifier |
|
223 | - * @return string |
|
224 | - * @throws InvalidEntityException |
|
225 | - */ |
|
226 | - protected function setIdentifier($entity, $identifier) |
|
227 | - { |
|
228 | - switch ($this->collection_details->identifierType()) { |
|
229 | - // every unique object gets added to the collection, but not duplicates of the exact same object |
|
230 | - case CollectionDetails::ID_OBJECT_HASH: |
|
231 | - $identifier = spl_object_hash($entity); |
|
232 | - break; |
|
233 | - // only one entity per class can be added to collection, like a singleton |
|
234 | - case CollectionDetails::ID_CLASS_NAME: |
|
235 | - $identifier = get_class($entity); |
|
236 | - break; |
|
237 | - // objects added to the collection based on entity callback, so the entity itself decides |
|
238 | - case CollectionDetails::ID_CALLBACK_METHOD: |
|
239 | - $identifier_callback = $this->collection_details->identifierCallback(); |
|
240 | - if (! method_exists($entity, $identifier_callback)) { |
|
241 | - throw new InvalidEntityException( |
|
242 | - $entity, |
|
243 | - $this->collection_details->getCollectionInterface(), |
|
244 | - sprintf( |
|
245 | - __( |
|
246 | - 'The current collection is configured to use a method named "%1$s" when setting or retrieving objects. The supplied entity is an instance |
|
220 | + /** |
|
221 | + * @param $entity |
|
222 | + * @param mixed $identifier |
|
223 | + * @return string |
|
224 | + * @throws InvalidEntityException |
|
225 | + */ |
|
226 | + protected function setIdentifier($entity, $identifier) |
|
227 | + { |
|
228 | + switch ($this->collection_details->identifierType()) { |
|
229 | + // every unique object gets added to the collection, but not duplicates of the exact same object |
|
230 | + case CollectionDetails::ID_OBJECT_HASH: |
|
231 | + $identifier = spl_object_hash($entity); |
|
232 | + break; |
|
233 | + // only one entity per class can be added to collection, like a singleton |
|
234 | + case CollectionDetails::ID_CLASS_NAME: |
|
235 | + $identifier = get_class($entity); |
|
236 | + break; |
|
237 | + // objects added to the collection based on entity callback, so the entity itself decides |
|
238 | + case CollectionDetails::ID_CALLBACK_METHOD: |
|
239 | + $identifier_callback = $this->collection_details->identifierCallback(); |
|
240 | + if (! method_exists($entity, $identifier_callback)) { |
|
241 | + throw new InvalidEntityException( |
|
242 | + $entity, |
|
243 | + $this->collection_details->getCollectionInterface(), |
|
244 | + sprintf( |
|
245 | + __( |
|
246 | + 'The current collection is configured to use a method named "%1$s" when setting or retrieving objects. The supplied entity is an instance |
|
247 | 247 | of "%2$s", but does not contain this method.', |
248 | - 'event_espresso' |
|
249 | - ), |
|
250 | - $identifier_callback, |
|
251 | - get_class($entity) |
|
252 | - ) |
|
253 | - ); |
|
254 | - } |
|
255 | - $identifier = $entity->{$identifier_callback}(); |
|
256 | - break; |
|
257 | - } |
|
258 | - return apply_filters( |
|
259 | - 'FHEE__CollectionLoader__addEntityToCollection__identifier', |
|
260 | - $identifier, |
|
261 | - $this->collection_details->collectionName(), |
|
262 | - $this->collection_details |
|
263 | - ); |
|
264 | - } |
|
248 | + 'event_espresso' |
|
249 | + ), |
|
250 | + $identifier_callback, |
|
251 | + get_class($entity) |
|
252 | + ) |
|
253 | + ); |
|
254 | + } |
|
255 | + $identifier = $entity->{$identifier_callback}(); |
|
256 | + break; |
|
257 | + } |
|
258 | + return apply_filters( |
|
259 | + 'FHEE__CollectionLoader__addEntityToCollection__identifier', |
|
260 | + $identifier, |
|
261 | + $this->collection_details->collectionName(), |
|
262 | + $this->collection_details |
|
263 | + ); |
|
264 | + } |
|
265 | 265 | |
266 | 266 | |
267 | - /** |
|
268 | - * @throws ReflectionException |
|
269 | - * @throws InvalidArgumentException |
|
270 | - * @throws InvalidInterfaceException |
|
271 | - * @throws EE_Error |
|
272 | - * @throws InvalidClassException |
|
273 | - * @throws InvalidDataTypeException |
|
274 | - * @throws InvalidEntityException |
|
275 | - * @throws DuplicateCollectionIdentifierException |
|
276 | - */ |
|
277 | - protected function loadFromFQCNs() |
|
278 | - { |
|
279 | - $FQCNs = $this->collection_details->getCollectionFQCNs(); |
|
280 | - $FQCNs = (array) apply_filters( |
|
281 | - 'FHEE__CollectionLoader__loadAllFromFQCNs__FQCNs', |
|
282 | - $FQCNs, |
|
283 | - $this->collection_details->collectionName(), |
|
284 | - $this->collection_details |
|
285 | - ); |
|
286 | - foreach ($FQCNs as $FQCN) { |
|
287 | - $this->loadClassFromFQCN($FQCN); |
|
288 | - } |
|
289 | - } |
|
267 | + /** |
|
268 | + * @throws ReflectionException |
|
269 | + * @throws InvalidArgumentException |
|
270 | + * @throws InvalidInterfaceException |
|
271 | + * @throws EE_Error |
|
272 | + * @throws InvalidClassException |
|
273 | + * @throws InvalidDataTypeException |
|
274 | + * @throws InvalidEntityException |
|
275 | + * @throws DuplicateCollectionIdentifierException |
|
276 | + */ |
|
277 | + protected function loadFromFQCNs() |
|
278 | + { |
|
279 | + $FQCNs = $this->collection_details->getCollectionFQCNs(); |
|
280 | + $FQCNs = (array) apply_filters( |
|
281 | + 'FHEE__CollectionLoader__loadAllFromFQCNs__FQCNs', |
|
282 | + $FQCNs, |
|
283 | + $this->collection_details->collectionName(), |
|
284 | + $this->collection_details |
|
285 | + ); |
|
286 | + foreach ($FQCNs as $FQCN) { |
|
287 | + $this->loadClassFromFQCN($FQCN); |
|
288 | + } |
|
289 | + } |
|
290 | 290 | |
291 | 291 | |
292 | - /** |
|
293 | - * @param string $FQCN Fully Qualified Class Name |
|
294 | - * @return string |
|
295 | - * @throws InvalidArgumentException |
|
296 | - * @throws InvalidInterfaceException |
|
297 | - * @throws ReflectionException |
|
298 | - * @throws EE_Error |
|
299 | - * @throws InvalidEntityException |
|
300 | - * @throws InvalidDataTypeException |
|
301 | - * @throws InvalidClassException |
|
302 | - * @throws DuplicateCollectionIdentifierException |
|
303 | - */ |
|
304 | - protected function loadClassFromFQCN($FQCN) |
|
305 | - { |
|
306 | - if (! is_string($FQCN)) { |
|
307 | - throw new InvalidDataTypeException('$FQCN', $FQCN, 'string'); |
|
308 | - } |
|
309 | - if (! class_exists($FQCN)) { |
|
310 | - throw new InvalidClassException($FQCN); |
|
311 | - } |
|
312 | - do_action( |
|
313 | - 'FHEE__CollectionLoader__loadClassFromFQCN__beforeLoading', |
|
314 | - $FQCN, |
|
315 | - $this->collection_details->collectionName(), |
|
316 | - $this->collection_details |
|
317 | - ); |
|
318 | - $entity = $this->entity_factory instanceof FactoryInterface |
|
319 | - ? call_user_func(array($this->entity_factory, 'create'), $FQCN) |
|
320 | - : EE_Registry::instance()->create($FQCN); |
|
321 | - return $this->addEntityToCollection($entity, $FQCN); |
|
322 | - } |
|
292 | + /** |
|
293 | + * @param string $FQCN Fully Qualified Class Name |
|
294 | + * @return string |
|
295 | + * @throws InvalidArgumentException |
|
296 | + * @throws InvalidInterfaceException |
|
297 | + * @throws ReflectionException |
|
298 | + * @throws EE_Error |
|
299 | + * @throws InvalidEntityException |
|
300 | + * @throws InvalidDataTypeException |
|
301 | + * @throws InvalidClassException |
|
302 | + * @throws DuplicateCollectionIdentifierException |
|
303 | + */ |
|
304 | + protected function loadClassFromFQCN($FQCN) |
|
305 | + { |
|
306 | + if (! is_string($FQCN)) { |
|
307 | + throw new InvalidDataTypeException('$FQCN', $FQCN, 'string'); |
|
308 | + } |
|
309 | + if (! class_exists($FQCN)) { |
|
310 | + throw new InvalidClassException($FQCN); |
|
311 | + } |
|
312 | + do_action( |
|
313 | + 'FHEE__CollectionLoader__loadClassFromFQCN__beforeLoading', |
|
314 | + $FQCN, |
|
315 | + $this->collection_details->collectionName(), |
|
316 | + $this->collection_details |
|
317 | + ); |
|
318 | + $entity = $this->entity_factory instanceof FactoryInterface |
|
319 | + ? call_user_func(array($this->entity_factory, 'create'), $FQCN) |
|
320 | + : EE_Registry::instance()->create($FQCN); |
|
321 | + return $this->addEntityToCollection($entity, $FQCN); |
|
322 | + } |
|
323 | 323 | } |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | ) { |
88 | 88 | try { |
89 | 89 | $this->collection_details = $collection_details; |
90 | - if (! $collection instanceof CollectionInterface) { |
|
90 | + if ( ! $collection instanceof CollectionInterface) { |
|
91 | 91 | $collection = new Collection($this->collection_details->getCollectionInterface()); |
92 | 92 | } |
93 | 93 | $this->collection = $collection; |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | */ |
120 | 120 | protected function loadAllFromFilepaths() |
121 | 121 | { |
122 | - if (! $this->file_locator instanceof FileLocator) { |
|
122 | + if ( ! $this->file_locator instanceof FileLocator) { |
|
123 | 123 | $this->file_locator = new FileLocator(); |
124 | 124 | } |
125 | 125 | $this->file_locator->setFileMask($this->collection_details->getFileMask()); |
@@ -152,10 +152,10 @@ discard block |
||
152 | 152 | */ |
153 | 153 | protected function loadClassFromFilepath($filepath) |
154 | 154 | { |
155 | - if (! is_string($filepath)) { |
|
155 | + if ( ! is_string($filepath)) { |
|
156 | 156 | throw new InvalidDataTypeException('$filepath', $filepath, 'string'); |
157 | 157 | } |
158 | - if (! is_readable($filepath)) { |
|
158 | + if ( ! is_readable($filepath)) { |
|
159 | 159 | throw new InvalidFilePathException($filepath); |
160 | 160 | } |
161 | 161 | require_once $filepath; |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | $file_name = basename($filepath); |
164 | 164 | // now remove any file extensions |
165 | 165 | $class_name = EEH_File::get_classname_from_filepath_with_standard_filename($file_name); |
166 | - if (! class_exists($class_name)) { |
|
166 | + if ( ! class_exists($class_name)) { |
|
167 | 167 | throw new InvalidClassException($class_name); |
168 | 168 | } |
169 | 169 | $entity = $this->entity_factory instanceof FactoryInterface |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | // objects added to the collection based on entity callback, so the entity itself decides |
238 | 238 | case CollectionDetails::ID_CALLBACK_METHOD: |
239 | 239 | $identifier_callback = $this->collection_details->identifierCallback(); |
240 | - if (! method_exists($entity, $identifier_callback)) { |
|
240 | + if ( ! method_exists($entity, $identifier_callback)) { |
|
241 | 241 | throw new InvalidEntityException( |
242 | 242 | $entity, |
243 | 243 | $this->collection_details->getCollectionInterface(), |
@@ -303,10 +303,10 @@ discard block |
||
303 | 303 | */ |
304 | 304 | protected function loadClassFromFQCN($FQCN) |
305 | 305 | { |
306 | - if (! is_string($FQCN)) { |
|
306 | + if ( ! is_string($FQCN)) { |
|
307 | 307 | throw new InvalidDataTypeException('$FQCN', $FQCN, 'string'); |
308 | 308 | } |
309 | - if (! class_exists($FQCN)) { |
|
309 | + if ( ! class_exists($FQCN)) { |
|
310 | 310 | throw new InvalidClassException($FQCN); |
311 | 311 | } |
312 | 312 | do_action( |
@@ -22,70 +22,70 @@ |
||
22 | 22 | interface BlockInterface |
23 | 23 | { |
24 | 24 | |
25 | - const NAME_SPACE = 'eventespresso'; |
|
26 | - |
|
27 | - /** |
|
28 | - * Perform any early setup required by the block |
|
29 | - * including setting the block type and supported post types |
|
30 | - * |
|
31 | - * @return void |
|
32 | - */ |
|
33 | - public function initialize(); |
|
34 | - |
|
35 | - |
|
36 | - /** |
|
37 | - * @return string |
|
38 | - */ |
|
39 | - public function blockType(); |
|
40 | - |
|
41 | - |
|
42 | - /** |
|
43 | - * AssetRegister that this editor block uses for asset registration |
|
44 | - * |
|
45 | - * @return BlockAssetManagerInterface |
|
46 | - */ |
|
47 | - public function assetManager(); |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * Registers the Editor Block with WP core; |
|
52 | - * Returns the registered block type on success, or false on failure. |
|
53 | - * |
|
54 | - * @return WP_Block_Type|false |
|
55 | - */ |
|
56 | - public function registerBlock(); |
|
57 | - |
|
58 | - |
|
59 | - /** |
|
60 | - * Un-registers the Editor Block with WP core; |
|
61 | - * Returns the registered block type on success, or false on failure. |
|
62 | - * |
|
63 | - * @return WP_Block_Type|false |
|
64 | - */ |
|
65 | - public function unRegisterBlock(); |
|
66 | - |
|
67 | - |
|
68 | - /** |
|
69 | - * returns an array of fully qualified class names |
|
70 | - * for RouteMatchSpecificationInterface objects |
|
71 | - * that specify routes that the block should be loaded for. |
|
72 | - * |
|
73 | - * @return array |
|
74 | - */ |
|
75 | - public function supportedRoutes(); |
|
76 | - |
|
77 | - |
|
78 | - /** |
|
79 | - * @return array |
|
80 | - */ |
|
81 | - public function getEditorContainer(); |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * returns the rendered HTML for the block |
|
86 | - * |
|
87 | - * @param array $attributes |
|
88 | - * @return string |
|
89 | - */ |
|
90 | - public function renderBlock(array $attributes = array()); |
|
25 | + const NAME_SPACE = 'eventespresso'; |
|
26 | + |
|
27 | + /** |
|
28 | + * Perform any early setup required by the block |
|
29 | + * including setting the block type and supported post types |
|
30 | + * |
|
31 | + * @return void |
|
32 | + */ |
|
33 | + public function initialize(); |
|
34 | + |
|
35 | + |
|
36 | + /** |
|
37 | + * @return string |
|
38 | + */ |
|
39 | + public function blockType(); |
|
40 | + |
|
41 | + |
|
42 | + /** |
|
43 | + * AssetRegister that this editor block uses for asset registration |
|
44 | + * |
|
45 | + * @return BlockAssetManagerInterface |
|
46 | + */ |
|
47 | + public function assetManager(); |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * Registers the Editor Block with WP core; |
|
52 | + * Returns the registered block type on success, or false on failure. |
|
53 | + * |
|
54 | + * @return WP_Block_Type|false |
|
55 | + */ |
|
56 | + public function registerBlock(); |
|
57 | + |
|
58 | + |
|
59 | + /** |
|
60 | + * Un-registers the Editor Block with WP core; |
|
61 | + * Returns the registered block type on success, or false on failure. |
|
62 | + * |
|
63 | + * @return WP_Block_Type|false |
|
64 | + */ |
|
65 | + public function unRegisterBlock(); |
|
66 | + |
|
67 | + |
|
68 | + /** |
|
69 | + * returns an array of fully qualified class names |
|
70 | + * for RouteMatchSpecificationInterface objects |
|
71 | + * that specify routes that the block should be loaded for. |
|
72 | + * |
|
73 | + * @return array |
|
74 | + */ |
|
75 | + public function supportedRoutes(); |
|
76 | + |
|
77 | + |
|
78 | + /** |
|
79 | + * @return array |
|
80 | + */ |
|
81 | + public function getEditorContainer(); |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * returns the rendered HTML for the block |
|
86 | + * |
|
87 | + * @param array $attributes |
|
88 | + * @return string |
|
89 | + */ |
|
90 | + public function renderBlock(array $attributes = array()); |
|
91 | 91 | } |
@@ -18,46 +18,46 @@ |
||
18 | 18 | abstract class BlockManager |
19 | 19 | { |
20 | 20 | |
21 | - /** |
|
22 | - * @var CollectionInterface|BlockInterface[] $blocks |
|
23 | - */ |
|
24 | - protected $blocks; |
|
25 | - |
|
26 | - /** |
|
27 | - * @var RequestInterface $request |
|
28 | - */ |
|
29 | - protected $request; |
|
30 | - |
|
31 | - |
|
32 | - |
|
33 | - /** |
|
34 | - * BlockManager constructor. |
|
35 | - * |
|
36 | - * @param BlockCollection $blocks |
|
37 | - * @param RequestInterface $request |
|
38 | - */ |
|
39 | - public function __construct( |
|
40 | - BlockCollection $blocks, |
|
41 | - RequestInterface $request |
|
42 | - ) { |
|
43 | - $this->blocks = $blocks; |
|
44 | - $this->request = $request; |
|
45 | - add_action($this->initHook(), array($this, 'initialize')); |
|
46 | - } |
|
47 | - |
|
48 | - |
|
49 | - /** |
|
50 | - * Returns the name of a hookpoint to be used to call initialize() |
|
51 | - * |
|
52 | - * @return string |
|
53 | - */ |
|
54 | - abstract public function initHook(); |
|
55 | - |
|
56 | - |
|
57 | - /** |
|
58 | - * Perform any early setup required for block editors to functions |
|
59 | - * |
|
60 | - * @return void |
|
61 | - */ |
|
62 | - abstract public function initialize(); |
|
21 | + /** |
|
22 | + * @var CollectionInterface|BlockInterface[] $blocks |
|
23 | + */ |
|
24 | + protected $blocks; |
|
25 | + |
|
26 | + /** |
|
27 | + * @var RequestInterface $request |
|
28 | + */ |
|
29 | + protected $request; |
|
30 | + |
|
31 | + |
|
32 | + |
|
33 | + /** |
|
34 | + * BlockManager constructor. |
|
35 | + * |
|
36 | + * @param BlockCollection $blocks |
|
37 | + * @param RequestInterface $request |
|
38 | + */ |
|
39 | + public function __construct( |
|
40 | + BlockCollection $blocks, |
|
41 | + RequestInterface $request |
|
42 | + ) { |
|
43 | + $this->blocks = $blocks; |
|
44 | + $this->request = $request; |
|
45 | + add_action($this->initHook(), array($this, 'initialize')); |
|
46 | + } |
|
47 | + |
|
48 | + |
|
49 | + /** |
|
50 | + * Returns the name of a hookpoint to be used to call initialize() |
|
51 | + * |
|
52 | + * @return string |
|
53 | + */ |
|
54 | + abstract public function initHook(); |
|
55 | + |
|
56 | + |
|
57 | + /** |
|
58 | + * Perform any early setup required for block editors to functions |
|
59 | + * |
|
60 | + * @return void |
|
61 | + */ |
|
62 | + abstract public function initialize(); |
|
63 | 63 | } |
@@ -16,25 +16,25 @@ |
||
16 | 16 | class CollectionLoaderException extends RuntimeException |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * DuplicateCollectionIdentifierException constructor. |
|
21 | - * |
|
22 | - * @param Exception $previous |
|
23 | - * @param string $message |
|
24 | - * @param int $code |
|
25 | - */ |
|
26 | - public function __construct(Exception $previous, $message = '', $code = 0) |
|
27 | - { |
|
28 | - if (empty($message)) { |
|
29 | - $message = sprintf( |
|
30 | - __( |
|
31 | - 'The following error occurred during the creation and/or loading of this collection: %1$s %2$s', |
|
32 | - 'event_espresso' |
|
33 | - ), |
|
34 | - '<br />', |
|
35 | - $previous->getMessage() |
|
36 | - ); |
|
37 | - } |
|
38 | - parent::__construct($message, $code, $previous); |
|
39 | - } |
|
19 | + /** |
|
20 | + * DuplicateCollectionIdentifierException constructor. |
|
21 | + * |
|
22 | + * @param Exception $previous |
|
23 | + * @param string $message |
|
24 | + * @param int $code |
|
25 | + */ |
|
26 | + public function __construct(Exception $previous, $message = '', $code = 0) |
|
27 | + { |
|
28 | + if (empty($message)) { |
|
29 | + $message = sprintf( |
|
30 | + __( |
|
31 | + 'The following error occurred during the creation and/or loading of this collection: %1$s %2$s', |
|
32 | + 'event_espresso' |
|
33 | + ), |
|
34 | + '<br />', |
|
35 | + $previous->getMessage() |
|
36 | + ); |
|
37 | + } |
|
38 | + parent::__construct($message, $code, $previous); |
|
39 | + } |
|
40 | 40 | } |
@@ -16,25 +16,25 @@ |
||
16 | 16 | class CollectionDetailsException extends RuntimeException |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * DuplicateCollectionIdentifierException constructor. |
|
21 | - * |
|
22 | - * @param Exception $previous |
|
23 | - * @param string $message |
|
24 | - * @param int $code |
|
25 | - */ |
|
26 | - public function __construct(Exception $previous, $message = '', $code = 0) |
|
27 | - { |
|
28 | - if (empty($message)) { |
|
29 | - $message = sprintf( |
|
30 | - __( |
|
31 | - 'The following error occurred during the collection details generation: %1$s %2$s', |
|
32 | - 'event_espresso' |
|
33 | - ), |
|
34 | - '<br />', |
|
35 | - $previous->getMessage() |
|
36 | - ); |
|
37 | - } |
|
38 | - parent::__construct($message, $code, $previous); |
|
39 | - } |
|
19 | + /** |
|
20 | + * DuplicateCollectionIdentifierException constructor. |
|
21 | + * |
|
22 | + * @param Exception $previous |
|
23 | + * @param string $message |
|
24 | + * @param int $code |
|
25 | + */ |
|
26 | + public function __construct(Exception $previous, $message = '', $code = 0) |
|
27 | + { |
|
28 | + if (empty($message)) { |
|
29 | + $message = sprintf( |
|
30 | + __( |
|
31 | + 'The following error occurred during the collection details generation: %1$s %2$s', |
|
32 | + 'event_espresso' |
|
33 | + ), |
|
34 | + '<br />', |
|
35 | + $previous->getMessage() |
|
36 | + ); |
|
37 | + } |
|
38 | + parent::__construct($message, $code, $previous); |
|
39 | + } |
|
40 | 40 | } |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | } |
100 | 100 | // If not, then attempt to deal with any errors, |
101 | 101 | // otherwise, try to hobble along without the session |
102 | - if (! $this->handleSessionSaveHandlerErrors()) { |
|
102 | + if ( ! $this->handleSessionSaveHandlerErrors()) { |
|
103 | 103 | return; |
104 | 104 | } |
105 | 105 | // there is no record of a fatal error while trying to start the session |
@@ -249,7 +249,7 @@ discard block |
||
249 | 249 | ), |
250 | 250 | '<a href="https://wordpress.org/plugins/wp-native-php-sessions/">', |
251 | 251 | '</a>', |
252 | - '<a href="' . $retry_session_url . '">' |
|
252 | + '<a href="'.$retry_session_url.'">' |
|
253 | 253 | ), |
254 | 254 | __FILE__, |
255 | 255 | __FUNCTION__, |
@@ -27,234 +27,234 @@ |
||
27 | 27 | */ |
28 | 28 | class SessionStartHandler |
29 | 29 | { |
30 | - const OPTION_NAME_SESSION_SAVE_HANDLER_STATUS = 'ee_session_save_handler_status'; |
|
31 | - const REQUEST_PARAM_RETRY_SESSION = 'ee_retry_session'; |
|
32 | - const SESSION_SAVE_HANDLER_STATUS_FAILED = 'session_save_handler_failed'; |
|
33 | - const SESSION_SAVE_HANDLER_STATUS_SUCCESS = 'session_save_handler_success'; |
|
34 | - const SESSION_SAVE_HANDLER_STATUS_UNKNOWN = 'session_save_handler_untested'; |
|
30 | + const OPTION_NAME_SESSION_SAVE_HANDLER_STATUS = 'ee_session_save_handler_status'; |
|
31 | + const REQUEST_PARAM_RETRY_SESSION = 'ee_retry_session'; |
|
32 | + const SESSION_SAVE_HANDLER_STATUS_FAILED = 'session_save_handler_failed'; |
|
33 | + const SESSION_SAVE_HANDLER_STATUS_SUCCESS = 'session_save_handler_success'; |
|
34 | + const SESSION_SAVE_HANDLER_STATUS_UNKNOWN = 'session_save_handler_untested'; |
|
35 | 35 | |
36 | - /** |
|
37 | - * @var RequestInterface $request |
|
38 | - */ |
|
39 | - protected $request; |
|
36 | + /** |
|
37 | + * @var RequestInterface $request |
|
38 | + */ |
|
39 | + protected $request; |
|
40 | 40 | |
41 | - /** |
|
42 | - * StartSession constructor. |
|
43 | - * |
|
44 | - * @param RequestInterface $request |
|
45 | - */ |
|
46 | - public function __construct(RequestInterface $request) |
|
47 | - { |
|
48 | - $this->request = $request; |
|
49 | - } |
|
41 | + /** |
|
42 | + * StartSession constructor. |
|
43 | + * |
|
44 | + * @param RequestInterface $request |
|
45 | + */ |
|
46 | + public function __construct(RequestInterface $request) |
|
47 | + { |
|
48 | + $this->request = $request; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Check if a custom session save handler is in play |
|
53 | - * and attempt to start the PHP session |
|
54 | - * |
|
55 | - * @since 4.9.68.p |
|
56 | - */ |
|
57 | - public function startSession() |
|
58 | - { |
|
59 | - // check that session has started |
|
60 | - if (session_id() === '') { |
|
61 | - // starts a new session if one doesn't already exist, or re-initiates an existing one |
|
62 | - if ($this->hasKnownCustomSessionSaveHandler()) { |
|
63 | - $this->checkCustomSessionSaveHandler(); |
|
64 | - } else { |
|
65 | - session_start(); |
|
66 | - } |
|
67 | - } |
|
68 | - } |
|
51 | + /** |
|
52 | + * Check if a custom session save handler is in play |
|
53 | + * and attempt to start the PHP session |
|
54 | + * |
|
55 | + * @since 4.9.68.p |
|
56 | + */ |
|
57 | + public function startSession() |
|
58 | + { |
|
59 | + // check that session has started |
|
60 | + if (session_id() === '') { |
|
61 | + // starts a new session if one doesn't already exist, or re-initiates an existing one |
|
62 | + if ($this->hasKnownCustomSessionSaveHandler()) { |
|
63 | + $this->checkCustomSessionSaveHandler(); |
|
64 | + } else { |
|
65 | + session_start(); |
|
66 | + } |
|
67 | + } |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * Returns `true` if the 'session.save_handler' ini setting matches a known custom handler |
|
72 | - * |
|
73 | - * @since 4.9.68.p |
|
74 | - * @return bool |
|
75 | - */ |
|
76 | - private function hasKnownCustomSessionSaveHandler() |
|
77 | - { |
|
78 | - return in_array( |
|
79 | - ini_get('session.save_handler'), |
|
80 | - array( |
|
81 | - 'user', |
|
82 | - ), |
|
83 | - true |
|
84 | - ); |
|
85 | - } |
|
70 | + /** |
|
71 | + * Returns `true` if the 'session.save_handler' ini setting matches a known custom handler |
|
72 | + * |
|
73 | + * @since 4.9.68.p |
|
74 | + * @return bool |
|
75 | + */ |
|
76 | + private function hasKnownCustomSessionSaveHandler() |
|
77 | + { |
|
78 | + return in_array( |
|
79 | + ini_get('session.save_handler'), |
|
80 | + array( |
|
81 | + 'user', |
|
82 | + ), |
|
83 | + true |
|
84 | + ); |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * Attempt to start the PHP session when a custom Session Save Handler is known to be set. |
|
89 | - * |
|
90 | - * @since 4.9.68.p |
|
91 | - */ |
|
92 | - private function checkCustomSessionSaveHandler() |
|
93 | - { |
|
94 | - // If we've already successfully tested the session save handler |
|
95 | - // on a previous request then just start the session |
|
96 | - if ($this->sessionSaveHandlerIsValid()) { |
|
97 | - session_start(); |
|
98 | - return; |
|
99 | - } |
|
100 | - // If not, then attempt to deal with any errors, |
|
101 | - // otherwise, try to hobble along without the session |
|
102 | - if (! $this->handleSessionSaveHandlerErrors()) { |
|
103 | - return; |
|
104 | - } |
|
105 | - // there is no record of a fatal error while trying to start the session |
|
106 | - // so let's see if there's a custom session save handler. Proceed with caution |
|
107 | - $this->initializeSessionSaveHandlerStatus(); |
|
108 | - // hold your breath, the custom session save handler might cause a fatal here... |
|
109 | - session_start(); |
|
110 | - // phew! we made it! the custom session handler is a-ok |
|
111 | - $this->setSessionSaveHandlerStatusToValid(); |
|
112 | - } |
|
87 | + /** |
|
88 | + * Attempt to start the PHP session when a custom Session Save Handler is known to be set. |
|
89 | + * |
|
90 | + * @since 4.9.68.p |
|
91 | + */ |
|
92 | + private function checkCustomSessionSaveHandler() |
|
93 | + { |
|
94 | + // If we've already successfully tested the session save handler |
|
95 | + // on a previous request then just start the session |
|
96 | + if ($this->sessionSaveHandlerIsValid()) { |
|
97 | + session_start(); |
|
98 | + return; |
|
99 | + } |
|
100 | + // If not, then attempt to deal with any errors, |
|
101 | + // otherwise, try to hobble along without the session |
|
102 | + if (! $this->handleSessionSaveHandlerErrors()) { |
|
103 | + return; |
|
104 | + } |
|
105 | + // there is no record of a fatal error while trying to start the session |
|
106 | + // so let's see if there's a custom session save handler. Proceed with caution |
|
107 | + $this->initializeSessionSaveHandlerStatus(); |
|
108 | + // hold your breath, the custom session save handler might cause a fatal here... |
|
109 | + session_start(); |
|
110 | + // phew! we made it! the custom session handler is a-ok |
|
111 | + $this->setSessionSaveHandlerStatusToValid(); |
|
112 | + } |
|
113 | 113 | |
114 | 114 | |
115 | - /** |
|
116 | - * retrieves the value for the 'ee_session_save_handler_status' WP option. |
|
117 | - * default value = 'session_save_handler_untested' |
|
118 | - * |
|
119 | - * @since 4.9.68.p |
|
120 | - * @return string |
|
121 | - */ |
|
122 | - private function getSessionSaveHandlerStatus() |
|
123 | - { |
|
124 | - return get_option( |
|
125 | - SessionStartHandler::OPTION_NAME_SESSION_SAVE_HANDLER_STATUS, |
|
126 | - SessionStartHandler::SESSION_SAVE_HANDLER_STATUS_UNKNOWN |
|
127 | - ); |
|
128 | - } |
|
115 | + /** |
|
116 | + * retrieves the value for the 'ee_session_save_handler_status' WP option. |
|
117 | + * default value = 'session_save_handler_untested' |
|
118 | + * |
|
119 | + * @since 4.9.68.p |
|
120 | + * @return string |
|
121 | + */ |
|
122 | + private function getSessionSaveHandlerStatus() |
|
123 | + { |
|
124 | + return get_option( |
|
125 | + SessionStartHandler::OPTION_NAME_SESSION_SAVE_HANDLER_STATUS, |
|
126 | + SessionStartHandler::SESSION_SAVE_HANDLER_STATUS_UNKNOWN |
|
127 | + ); |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * Sets the 'ee_session_save_handler_status' WP option value to 'session_save_handler_failed' |
|
132 | - * which can then be upgraded is everything works correctly |
|
133 | - * |
|
134 | - * @since 4.9.68.p |
|
135 | - * @return bool |
|
136 | - */ |
|
137 | - private function initializeSessionSaveHandlerStatus() |
|
138 | - { |
|
139 | - return update_option( |
|
140 | - SessionStartHandler::OPTION_NAME_SESSION_SAVE_HANDLER_STATUS, |
|
141 | - SessionStartHandler::SESSION_SAVE_HANDLER_STATUS_FAILED |
|
142 | - ); |
|
143 | - } |
|
130 | + /** |
|
131 | + * Sets the 'ee_session_save_handler_status' WP option value to 'session_save_handler_failed' |
|
132 | + * which can then be upgraded is everything works correctly |
|
133 | + * |
|
134 | + * @since 4.9.68.p |
|
135 | + * @return bool |
|
136 | + */ |
|
137 | + private function initializeSessionSaveHandlerStatus() |
|
138 | + { |
|
139 | + return update_option( |
|
140 | + SessionStartHandler::OPTION_NAME_SESSION_SAVE_HANDLER_STATUS, |
|
141 | + SessionStartHandler::SESSION_SAVE_HANDLER_STATUS_FAILED |
|
142 | + ); |
|
143 | + } |
|
144 | 144 | |
145 | - /** |
|
146 | - * Sets the 'ee_session_save_handler_status' WP option value to 'session_save_handler_success' |
|
147 | - * |
|
148 | - * @since 4.9.68.p |
|
149 | - * @return bool |
|
150 | - */ |
|
151 | - private function setSessionSaveHandlerStatusToValid() |
|
152 | - { |
|
153 | - return update_option( |
|
154 | - SessionStartHandler::OPTION_NAME_SESSION_SAVE_HANDLER_STATUS, |
|
155 | - SessionStartHandler::SESSION_SAVE_HANDLER_STATUS_SUCCESS |
|
156 | - ); |
|
157 | - } |
|
145 | + /** |
|
146 | + * Sets the 'ee_session_save_handler_status' WP option value to 'session_save_handler_success' |
|
147 | + * |
|
148 | + * @since 4.9.68.p |
|
149 | + * @return bool |
|
150 | + */ |
|
151 | + private function setSessionSaveHandlerStatusToValid() |
|
152 | + { |
|
153 | + return update_option( |
|
154 | + SessionStartHandler::OPTION_NAME_SESSION_SAVE_HANDLER_STATUS, |
|
155 | + SessionStartHandler::SESSION_SAVE_HANDLER_STATUS_SUCCESS |
|
156 | + ); |
|
157 | + } |
|
158 | 158 | |
159 | - /** |
|
160 | - * Sets the 'ee_session_save_handler_status' WP option value to 'session_save_handler_untested' |
|
161 | - * |
|
162 | - * @since 4.9.68.p |
|
163 | - * @return bool |
|
164 | - */ |
|
165 | - private function resetSessionSaveHandlerStatus() |
|
166 | - { |
|
167 | - return update_option( |
|
168 | - SessionStartHandler::OPTION_NAME_SESSION_SAVE_HANDLER_STATUS, |
|
169 | - SessionStartHandler::SESSION_SAVE_HANDLER_STATUS_UNKNOWN |
|
170 | - ); |
|
171 | - } |
|
159 | + /** |
|
160 | + * Sets the 'ee_session_save_handler_status' WP option value to 'session_save_handler_untested' |
|
161 | + * |
|
162 | + * @since 4.9.68.p |
|
163 | + * @return bool |
|
164 | + */ |
|
165 | + private function resetSessionSaveHandlerStatus() |
|
166 | + { |
|
167 | + return update_option( |
|
168 | + SessionStartHandler::OPTION_NAME_SESSION_SAVE_HANDLER_STATUS, |
|
169 | + SessionStartHandler::SESSION_SAVE_HANDLER_STATUS_UNKNOWN |
|
170 | + ); |
|
171 | + } |
|
172 | 172 | |
173 | - /** |
|
174 | - * Returns `true` if the 'ee_session_save_handler_status' WP option value |
|
175 | - * is equal to 'session_save_handler_success' |
|
176 | - * |
|
177 | - * @since 4.9.68.p |
|
178 | - * @return bool |
|
179 | - */ |
|
180 | - private function sessionSaveHandlerIsValid() |
|
181 | - { |
|
182 | - return $this->getSessionSaveHandlerStatus() === SessionStartHandler::SESSION_SAVE_HANDLER_STATUS_SUCCESS; |
|
183 | - } |
|
173 | + /** |
|
174 | + * Returns `true` if the 'ee_session_save_handler_status' WP option value |
|
175 | + * is equal to 'session_save_handler_success' |
|
176 | + * |
|
177 | + * @since 4.9.68.p |
|
178 | + * @return bool |
|
179 | + */ |
|
180 | + private function sessionSaveHandlerIsValid() |
|
181 | + { |
|
182 | + return $this->getSessionSaveHandlerStatus() === SessionStartHandler::SESSION_SAVE_HANDLER_STATUS_SUCCESS; |
|
183 | + } |
|
184 | 184 | |
185 | - /** |
|
186 | - * Returns `true` if the 'ee_session_save_handler_status' WP option value |
|
187 | - * is equal to 'session_save_handler_failed' |
|
188 | - * |
|
189 | - * @since 4.9.68.p |
|
190 | - * @return bool |
|
191 | - */ |
|
192 | - private function sessionSaveHandlerFailed() |
|
193 | - { |
|
194 | - return $this->getSessionSaveHandlerStatus() === SessionStartHandler::SESSION_SAVE_HANDLER_STATUS_FAILED; |
|
195 | - } |
|
185 | + /** |
|
186 | + * Returns `true` if the 'ee_session_save_handler_status' WP option value |
|
187 | + * is equal to 'session_save_handler_failed' |
|
188 | + * |
|
189 | + * @since 4.9.68.p |
|
190 | + * @return bool |
|
191 | + */ |
|
192 | + private function sessionSaveHandlerFailed() |
|
193 | + { |
|
194 | + return $this->getSessionSaveHandlerStatus() === SessionStartHandler::SESSION_SAVE_HANDLER_STATUS_FAILED; |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * Returns `true` if no errors were detected with the session save handler, |
|
199 | - * otherwise attempts to work notify the appropriate authorities |
|
200 | - * with a suggestion for how to fix the issue, and returns `false`. |
|
201 | - * |
|
202 | - * |
|
203 | - * @since 4.9.68.p |
|
204 | - * @return bool |
|
205 | - */ |
|
206 | - private function handleSessionSaveHandlerErrors() |
|
207 | - { |
|
208 | - // Check if we had a fatal error last time while trying to start the session |
|
209 | - if ($this->sessionSaveHandlerFailed()) { |
|
210 | - // apparently, last time we tried using the custom session save handler there was a fatal |
|
211 | - if ($this->request->requestParamIsSet(SessionStartHandler::REQUEST_PARAM_RETRY_SESSION)) { |
|
212 | - $this->resetSessionSaveHandlerStatus(); |
|
213 | - // remove "ee_retry_session", otherwise if the problem still isn't fixed, |
|
214 | - // we'll just keep getting the fatal error over and over. |
|
215 | - // Better to remove it and redirect, and try on the next request |
|
216 | - EEH_URL::safeRedirectAndExit( |
|
217 | - remove_query_arg( |
|
218 | - array(SessionStartHandler::REQUEST_PARAM_RETRY_SESSION), |
|
219 | - EEH_URL::current_url() |
|
220 | - ) |
|
221 | - ); |
|
222 | - } |
|
223 | - // so the session is broken, don't try it again, |
|
224 | - // just show a message to users that can fix it |
|
225 | - $this->displaySessionSaveHandlerErrorNotice(); |
|
226 | - return false; |
|
227 | - } |
|
228 | - return true; |
|
229 | - } |
|
197 | + /** |
|
198 | + * Returns `true` if no errors were detected with the session save handler, |
|
199 | + * otherwise attempts to work notify the appropriate authorities |
|
200 | + * with a suggestion for how to fix the issue, and returns `false`. |
|
201 | + * |
|
202 | + * |
|
203 | + * @since 4.9.68.p |
|
204 | + * @return bool |
|
205 | + */ |
|
206 | + private function handleSessionSaveHandlerErrors() |
|
207 | + { |
|
208 | + // Check if we had a fatal error last time while trying to start the session |
|
209 | + if ($this->sessionSaveHandlerFailed()) { |
|
210 | + // apparently, last time we tried using the custom session save handler there was a fatal |
|
211 | + if ($this->request->requestParamIsSet(SessionStartHandler::REQUEST_PARAM_RETRY_SESSION)) { |
|
212 | + $this->resetSessionSaveHandlerStatus(); |
|
213 | + // remove "ee_retry_session", otherwise if the problem still isn't fixed, |
|
214 | + // we'll just keep getting the fatal error over and over. |
|
215 | + // Better to remove it and redirect, and try on the next request |
|
216 | + EEH_URL::safeRedirectAndExit( |
|
217 | + remove_query_arg( |
|
218 | + array(SessionStartHandler::REQUEST_PARAM_RETRY_SESSION), |
|
219 | + EEH_URL::current_url() |
|
220 | + ) |
|
221 | + ); |
|
222 | + } |
|
223 | + // so the session is broken, don't try it again, |
|
224 | + // just show a message to users that can fix it |
|
225 | + $this->displaySessionSaveHandlerErrorNotice(); |
|
226 | + return false; |
|
227 | + } |
|
228 | + return true; |
|
229 | + } |
|
230 | 230 | |
231 | - /** |
|
232 | - * Generates an EE_Error notice regarding the current session woes |
|
233 | - * but only if the current user is an admin with permission to 'install_plugins'. |
|
234 | - * |
|
235 | - * @since 4.9.68.p |
|
236 | - */ |
|
237 | - private function displaySessionSaveHandlerErrorNotice() |
|
238 | - { |
|
239 | - if (current_user_can('install_plugins')) { |
|
240 | - $retry_session_url = add_query_arg( |
|
241 | - array(SessionStartHandler::REQUEST_PARAM_RETRY_SESSION => true), |
|
242 | - EEH_URL::current_url() |
|
243 | - ); |
|
244 | - EE_Error::add_error( |
|
245 | - sprintf( |
|
246 | - esc_html__( |
|
247 | - 'It appears there was a fatal error while starting the session, so Event Espresso is not able to process registrations normally. Some hosting companies, like Pantheon, require an extra plugin for Event Espresso to work. Please install the %1$sWordPress Native PHP Sessions plugin%2$s, then %3$sclick here to check if the problem is resolved.%2$s', |
|
248 | - 'event_espresso' |
|
249 | - ), |
|
250 | - '<a href="https://wordpress.org/plugins/wp-native-php-sessions/">', |
|
251 | - '</a>', |
|
252 | - '<a href="' . $retry_session_url . '">' |
|
253 | - ), |
|
254 | - __FILE__, |
|
255 | - __FUNCTION__, |
|
256 | - __LINE__ |
|
257 | - ); |
|
258 | - } |
|
259 | - } |
|
231 | + /** |
|
232 | + * Generates an EE_Error notice regarding the current session woes |
|
233 | + * but only if the current user is an admin with permission to 'install_plugins'. |
|
234 | + * |
|
235 | + * @since 4.9.68.p |
|
236 | + */ |
|
237 | + private function displaySessionSaveHandlerErrorNotice() |
|
238 | + { |
|
239 | + if (current_user_can('install_plugins')) { |
|
240 | + $retry_session_url = add_query_arg( |
|
241 | + array(SessionStartHandler::REQUEST_PARAM_RETRY_SESSION => true), |
|
242 | + EEH_URL::current_url() |
|
243 | + ); |
|
244 | + EE_Error::add_error( |
|
245 | + sprintf( |
|
246 | + esc_html__( |
|
247 | + 'It appears there was a fatal error while starting the session, so Event Espresso is not able to process registrations normally. Some hosting companies, like Pantheon, require an extra plugin for Event Espresso to work. Please install the %1$sWordPress Native PHP Sessions plugin%2$s, then %3$sclick here to check if the problem is resolved.%2$s', |
|
248 | + 'event_espresso' |
|
249 | + ), |
|
250 | + '<a href="https://wordpress.org/plugins/wp-native-php-sessions/">', |
|
251 | + '</a>', |
|
252 | + '<a href="' . $retry_session_url . '">' |
|
253 | + ), |
|
254 | + __FILE__, |
|
255 | + __FUNCTION__, |
|
256 | + __LINE__ |
|
257 | + ); |
|
258 | + } |
|
259 | + } |
|
260 | 260 | } |