@@ -14,979 +14,979 @@ |
||
14 | 14 | { |
15 | 15 | |
16 | 16 | |
17 | - /** |
|
18 | - * @type EE_Messages_Data_Handler_Collection |
|
19 | - */ |
|
20 | - protected $_data_handler_collection; |
|
21 | - |
|
22 | - /** |
|
23 | - * @type EE_Message_Template_Group_Collection |
|
24 | - */ |
|
25 | - protected $_template_collection; |
|
26 | - |
|
27 | - /** |
|
28 | - * This will hold the data handler for the current EE_Message being generated. |
|
29 | - * |
|
30 | - * @type EE_Messages_incoming_data |
|
31 | - */ |
|
32 | - protected $_current_data_handler; |
|
33 | - |
|
34 | - /** |
|
35 | - * This holds the EE_Messages_Queue that contains the messages to generate. |
|
36 | - * |
|
37 | - * @type EE_Messages_Queue |
|
38 | - */ |
|
39 | - protected $_generation_queue; |
|
40 | - |
|
41 | - /** |
|
42 | - * This holds the EE_Messages_Queue that will store the generated EE_Message objects. |
|
43 | - * |
|
44 | - * @type EE_Messages_Queue |
|
45 | - */ |
|
46 | - protected $_ready_queue; |
|
47 | - |
|
48 | - /** |
|
49 | - * This is a container for any error messages that get created through the generation |
|
50 | - * process. |
|
51 | - * |
|
52 | - * @type array |
|
53 | - */ |
|
54 | - protected $_error_msg = array(); |
|
55 | - |
|
56 | - /** |
|
57 | - * Flag used to set when the current EE_Message in the generation queue has been verified. |
|
58 | - * |
|
59 | - * @type bool |
|
60 | - */ |
|
61 | - protected $_verified = false; |
|
62 | - |
|
63 | - /** |
|
64 | - * This will hold the current messenger object corresponding with the current EE_Message in the generation queue. |
|
65 | - * |
|
66 | - * @type EE_messenger |
|
67 | - */ |
|
68 | - protected $_current_messenger; |
|
69 | - |
|
70 | - /** |
|
71 | - * This will hold the current message type object corresponding with the current EE_Message in the generation queue. |
|
72 | - * |
|
73 | - * @type EE_message_type |
|
74 | - */ |
|
75 | - protected $_current_message_type; |
|
76 | - |
|
77 | - /** |
|
78 | - * @type EEH_Parse_Shortcodes |
|
79 | - */ |
|
80 | - protected $_shortcode_parser; |
|
81 | - |
|
82 | - |
|
83 | - /** |
|
84 | - * @param EE_Messages_Queue $generation_queue |
|
85 | - * @param \EE_Messages_Queue $ready_queue |
|
86 | - * @param \EE_Messages_Data_Handler_Collection $data_handler_collection |
|
87 | - * @param \EE_Message_Template_Group_Collection $template_collection |
|
88 | - * @param \EEH_Parse_Shortcodes $shortcode_parser |
|
89 | - */ |
|
90 | - public function __construct( |
|
91 | - EE_Messages_Queue $generation_queue, |
|
92 | - EE_Messages_Queue $ready_queue, |
|
93 | - EE_Messages_Data_Handler_Collection $data_handler_collection, |
|
94 | - EE_Message_Template_Group_Collection $template_collection, |
|
95 | - EEH_Parse_Shortcodes $shortcode_parser |
|
96 | - ) { |
|
97 | - $this->_generation_queue = $generation_queue; |
|
98 | - $this->_ready_queue = $ready_queue; |
|
99 | - $this->_data_handler_collection = $data_handler_collection; |
|
100 | - $this->_template_collection = $template_collection; |
|
101 | - $this->_shortcode_parser = $shortcode_parser; |
|
102 | - } |
|
103 | - |
|
104 | - |
|
105 | - /** |
|
106 | - * @return EE_Messages_Queue |
|
107 | - */ |
|
108 | - public function generation_queue() |
|
109 | - { |
|
110 | - return $this->_generation_queue; |
|
111 | - } |
|
112 | - |
|
113 | - |
|
114 | - /** |
|
115 | - * This iterates through the provided queue and generates the EE_Message objects. |
|
116 | - * When iterating through the queue, the queued item that served as the base for generating other EE_Message |
|
117 | - * objects gets removed and the new EE_Message objects get added to a NEW queue. The NEW queue is then returned |
|
118 | - * for the caller to decide what to do with it. |
|
119 | - * |
|
120 | - * @param bool $save Whether to save the EE_Message objects in the new queue or just return. |
|
121 | - * @return EE_Messages_Queue The new queue for holding generated EE_Message objects. |
|
122 | - * @throws EE_Error |
|
123 | - * @throws ReflectionException |
|
124 | - */ |
|
125 | - public function generate($save = true) |
|
126 | - { |
|
127 | - // iterate through the messages in the queue, generate, and add to new queue. |
|
128 | - $this->_generation_queue->get_message_repository()->rewind(); |
|
129 | - while ($this->_generation_queue->get_message_repository()->valid()) { |
|
130 | - // reset "current" properties |
|
131 | - $this->_reset_current_properties(); |
|
132 | - |
|
133 | - /** @type EE_Message $msg */ |
|
134 | - $msg = $this->_generation_queue->get_message_repository()->current(); |
|
135 | - |
|
136 | - /** |
|
137 | - * need to get the next object and capture it for setting manually after deletes. The reason is that when |
|
138 | - * an object is removed from the repo then valid for the next object will fail. |
|
139 | - */ |
|
140 | - $this->_generation_queue->get_message_repository()->next(); |
|
141 | - $next_msg = $this->_generation_queue->get_message_repository()->current(); |
|
142 | - // restore pointer to current item |
|
143 | - $this->_generation_queue->get_message_repository()->set_current($msg); |
|
144 | - |
|
145 | - // skip and delete if the current $msg is NOT incomplete (queued for generation) |
|
146 | - if ($msg->STS_ID() !== EEM_Message::status_incomplete) { |
|
147 | - // we keep this item in the db just remove from the repo. |
|
148 | - $this->_generation_queue->get_message_repository()->remove($msg); |
|
149 | - // next item |
|
150 | - $this->_generation_queue->get_message_repository()->set_current($next_msg); |
|
151 | - continue; |
|
152 | - } |
|
153 | - |
|
154 | - if ($this->_verify()) { |
|
155 | - // let's get generating! |
|
156 | - $this->_generate(); |
|
157 | - } |
|
158 | - |
|
159 | - // don't persist debug_only messages if the messages system is not in debug mode. |
|
160 | - if ($msg->STS_ID() === EEM_Message::status_debug_only |
|
161 | - && ! EEM_Message::debug() |
|
162 | - ) { |
|
163 | - do_action( |
|
164 | - 'AHEE__EE_Messages_Generator__generate__before_debug_delete', |
|
165 | - $msg, |
|
166 | - $this->_error_msg, |
|
167 | - $this->_current_messenger, |
|
168 | - $this->_current_message_type, |
|
169 | - $this->_current_data_handler |
|
170 | - ); |
|
171 | - $this->_generation_queue->get_message_repository()->delete(); |
|
172 | - $this->_generation_queue->get_message_repository()->set_current($next_msg); |
|
173 | - continue; |
|
174 | - } |
|
175 | - |
|
176 | - // if there are error messages then let's set the status and the error message. |
|
177 | - if ($this->_error_msg) { |
|
178 | - // if the status is already debug only, then let's leave it at that. |
|
179 | - if ($msg->STS_ID() !== EEM_Message::status_debug_only) { |
|
180 | - $msg->set_STS_ID(EEM_Message::status_failed); |
|
181 | - } |
|
182 | - do_action( |
|
183 | - 'AHEE__EE_Messages_Generator__generate__processing_failed_message', |
|
184 | - $msg, |
|
185 | - $this->_error_msg, |
|
186 | - $this->_current_messenger, |
|
187 | - $this->_current_message_type, |
|
188 | - $this->_current_data_handler |
|
189 | - ); |
|
190 | - $msg->set_error_message( |
|
191 | - esc_html__('Message failed to generate for the following reasons: ', 'event_espresso') |
|
192 | - . "\n" |
|
193 | - . implode("\n", $this->_error_msg) |
|
194 | - ); |
|
195 | - $msg->set_modified(time()); |
|
196 | - } else { |
|
197 | - do_action( |
|
198 | - 'AHEE__EE_Messages_Generator__generate__before_successful_generated_message_delete', |
|
199 | - $msg, |
|
200 | - $this->_error_msg, |
|
201 | - $this->_current_messenger, |
|
202 | - $this->_current_message_type, |
|
203 | - $this->_current_data_handler |
|
204 | - ); |
|
205 | - // remove from db |
|
206 | - $this->_generation_queue->get_message_repository()->delete(); |
|
207 | - } |
|
208 | - // next item |
|
209 | - $this->_generation_queue->get_message_repository()->set_current($next_msg); |
|
210 | - } |
|
211 | - |
|
212 | - // generation queue is ALWAYS saved to record any errors in the generation process. |
|
213 | - $this->_generation_queue->save(); |
|
214 | - |
|
215 | - /** |
|
216 | - * save _ready_queue if flag set. |
|
217 | - * Note: The EE_Message objects have values set via the EE_Base_Class::set_field_or_extra_meta() method. This |
|
218 | - * means if a field was added that is not a valid database column. The EE_Message was already saved to the db |
|
219 | - * so a EE_Extra_Meta entry could be created and attached to the EE_Message. In those cases the save flag is |
|
220 | - * irrelevant. |
|
221 | - */ |
|
222 | - if ($save) { |
|
223 | - $this->_ready_queue->save(); |
|
224 | - } |
|
225 | - |
|
226 | - // final reset of properties |
|
227 | - $this->_reset_current_properties(); |
|
228 | - |
|
229 | - return $this->_ready_queue; |
|
230 | - } |
|
231 | - |
|
232 | - |
|
233 | - /** |
|
234 | - * This resets all the properties used for holding "current" values corresponding to the current EE_Message object |
|
235 | - * in the generation queue. |
|
236 | - */ |
|
237 | - protected function _reset_current_properties() |
|
238 | - { |
|
239 | - $this->_verified = false; |
|
240 | - // make sure any _data value in the current message type is reset |
|
241 | - if ($this->_current_message_type instanceof EE_message_type) { |
|
242 | - $this->_current_message_type->reset_data(); |
|
243 | - } |
|
244 | - $this->_current_messenger = $this->_current_message_type = $this->_current_data_handler = null; |
|
245 | - } |
|
246 | - |
|
247 | - |
|
248 | - /** |
|
249 | - * This proceeds with the actual generation of a message. By the time this is called, there should already be a |
|
250 | - * $_current_data_handler set and all incoming information should be validated for the current EE_Message in the |
|
251 | - * _generating_queue. |
|
252 | - * |
|
253 | - * @return bool Whether the message was successfully generated or not. |
|
254 | - * @throws EE_Error |
|
255 | - * @throws InvalidArgumentException |
|
256 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
257 | - * @throws InvalidInterfaceException |
|
258 | - */ |
|
259 | - protected function _generate() |
|
260 | - { |
|
261 | - // double check verification has run and that everything is ready to work with (saves us having to validate |
|
262 | - // everything again). |
|
263 | - if (! $this->_verified) { |
|
264 | - return false; // get out because we don't have a valid setup to work with. |
|
265 | - } |
|
266 | - |
|
267 | - |
|
268 | - try { |
|
269 | - $addressees = $this->_current_message_type->get_addressees( |
|
270 | - $this->_current_data_handler, |
|
271 | - $this->_generation_queue->get_message_repository()->current()->context() |
|
272 | - ); |
|
273 | - } catch (EE_Error $e) { |
|
274 | - $this->_error_msg[] = $e->getMessage(); |
|
275 | - return false; |
|
276 | - } |
|
277 | - |
|
278 | - |
|
279 | - // if no addressees then get out because there is nothing to generation (possible bad data). |
|
280 | - if (! $this->_valid_addressees($addressees)) { |
|
281 | - do_action( |
|
282 | - 'AHEE__EE_Messages_Generator___generate__invalid_addressees', |
|
283 | - $this->_generation_queue->get_message_repository()->current(), |
|
284 | - $addressees, |
|
285 | - $this->_current_messenger, |
|
286 | - $this->_current_message_type, |
|
287 | - $this->_current_data_handler |
|
288 | - ); |
|
289 | - $this->_generation_queue->get_message_repository()->current()->set_STS_ID( |
|
290 | - EEM_Message::status_debug_only |
|
291 | - ); |
|
292 | - $this->_error_msg[] = esc_html__( |
|
293 | - 'This is not a critical error but an informational notice. Unable to generate messages EE_Messages_Addressee objects. There were no attendees prepared by the data handler. Sometimes this is because messages only get generated for certain registration statuses. For example, the ticket notice message type only goes to approved registrations.', |
|
294 | - 'event_espresso' |
|
295 | - ); |
|
296 | - return false; |
|
297 | - } |
|
298 | - |
|
299 | - $message_template_group = $this->_get_message_template_group(); |
|
300 | - |
|
301 | - // in the unlikely event there is no EE_Message_Template_Group available, get out! |
|
302 | - if (! $message_template_group instanceof EE_Message_Template_Group) { |
|
303 | - $this->_error_msg[] = esc_html__( |
|
304 | - 'Unable to get the Message Templates for the Message being generated. No message template group accessible.', |
|
305 | - 'event_espresso' |
|
306 | - ); |
|
307 | - return false; |
|
308 | - } |
|
309 | - |
|
310 | - // get formatted templates for using to parse and setup EE_Message objects. |
|
311 | - $templates = $this->_get_templates($message_template_group); |
|
312 | - |
|
313 | - |
|
314 | - // setup new EE_Message objects (and add to _ready_queue) |
|
315 | - return $this->_assemble_messages($addressees, $templates, $message_template_group); |
|
316 | - } |
|
317 | - |
|
318 | - |
|
319 | - /** |
|
320 | - * Retrieves the message template group being used for generating messages. |
|
321 | - * Note: this also utilizes the EE_Message_Template_Group_Collection to avoid having to hit the db multiple times. |
|
322 | - * |
|
323 | - * @return EE_Message_Template_Group|null |
|
324 | - * @throws EE_Error |
|
325 | - * @throws InvalidArgumentException |
|
326 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
327 | - * @throws InvalidInterfaceException |
|
328 | - */ |
|
329 | - protected function _get_message_template_group() |
|
330 | - { |
|
331 | - // first see if there is a specific message template group requested (current message in the queue has a specific |
|
332 | - // GRP_ID |
|
333 | - $message_template_group = $this->_specific_message_template_group_from_queue(); |
|
334 | - if ($message_template_group instanceof EE_Message_Template_Group) { |
|
335 | - return $message_template_group; |
|
336 | - } |
|
337 | - |
|
338 | - // get event_ids from the datahandler so we can check to see if there's already a message template group for them |
|
339 | - // in the collection. |
|
340 | - $event_ids = $this->_get_event_ids_from_current_data_handler(); |
|
341 | - $message_template_group = $this->_template_collection->get_by_key( |
|
342 | - $this->_template_collection->getKey( |
|
343 | - $this->_current_messenger->name, |
|
344 | - $this->_current_message_type->name, |
|
345 | - $event_ids |
|
346 | - ) |
|
347 | - ); |
|
348 | - |
|
349 | - // if we have a message template group then no need to hit the database, just return it. |
|
350 | - if ($message_template_group instanceof EE_Message_Template_Group) { |
|
351 | - return $message_template_group; |
|
352 | - } |
|
353 | - |
|
354 | - // okay made it here, so let's get the global group first for this messenger and message type to ensure |
|
355 | - // there is no override set. |
|
356 | - $global_message_template_group = |
|
357 | - $this->_get_global_message_template_group_for_current_messenger_and_message_type(); |
|
358 | - |
|
359 | - if ($global_message_template_group instanceof EE_Message_Template_Group |
|
360 | - && $global_message_template_group->get('MTP_is_override') |
|
361 | - ) { |
|
362 | - return $global_message_template_group; |
|
363 | - } |
|
364 | - |
|
365 | - // if we're still here, that means there was no message template group for the events in the collection and |
|
366 | - // the global message template group for the messenger and message type is not set for override. So next step is |
|
367 | - // to see if there is a common shared custom message template group for this set of events. |
|
368 | - $message_template_group = $this->_get_shared_message_template_for_events($event_ids); |
|
369 | - if ($message_template_group instanceof EE_Message_Template_Group) { |
|
370 | - return $message_template_group; |
|
371 | - } |
|
372 | - |
|
373 | - // STILL here? Okay that means the fallback is to just use the global message template group for this event set. |
|
374 | - // So we'll cache the global group for this event set (so this logic doesn't have to be repeated in this request) |
|
375 | - // and return it. |
|
376 | - if ($global_message_template_group instanceof EE_Message_Template_Group) { |
|
377 | - $this->_template_collection->add( |
|
378 | - $global_message_template_group, |
|
379 | - $event_ids |
|
380 | - ); |
|
381 | - return $global_message_template_group; |
|
382 | - } |
|
383 | - |
|
384 | - // if we land here that means there's NO active message template group for this set. |
|
385 | - // TODO this will be a good target for some optimization down the road. Whenever there is no active message |
|
386 | - // template group for a given event set then cache that result so we don't repeat the logic. However, for now, |
|
387 | - // this should likely bit hit rarely enough that it's not a significant issue. |
|
388 | - return null; |
|
389 | - } |
|
390 | - |
|
391 | - |
|
392 | - /** |
|
393 | - * This checks the current message in the queue and determines if there is a specific Message Template Group |
|
394 | - * requested for that message. |
|
395 | - * |
|
396 | - * @return EE_Message_Template_Group|null |
|
397 | - * @throws EE_Error |
|
398 | - * @throws InvalidArgumentException |
|
399 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
400 | - * @throws InvalidInterfaceException |
|
401 | - */ |
|
402 | - protected function _specific_message_template_group_from_queue() |
|
403 | - { |
|
404 | - // is there a GRP_ID already on the EE_Message object? If there is, then a specific template has been requested |
|
405 | - // so let's use that. |
|
406 | - $GRP_ID = $this->_generation_queue->get_message_repository()->current()->GRP_ID(); |
|
407 | - |
|
408 | - if ($GRP_ID) { |
|
409 | - // attempt to retrieve from repo first |
|
410 | - $message_template_group = $this->_template_collection->get_by_ID($GRP_ID); |
|
411 | - if ($message_template_group instanceof EE_Message_Template_Group) { |
|
412 | - return $message_template_group; // got it! |
|
413 | - } |
|
414 | - |
|
415 | - // nope don't have it yet. Get from DB then add to repo if its not here, then that means the current GRP_ID |
|
416 | - // is not valid, so we'll continue on in the code assuming there's NO GRP_ID. |
|
417 | - $message_template_group = EEM_Message_Template_Group::instance()->get_one_by_ID($GRP_ID); |
|
418 | - if ($message_template_group instanceof EE_Message_Template_Group) { |
|
419 | - $this->_template_collection->add($message_template_group); |
|
420 | - return $message_template_group; |
|
421 | - } |
|
422 | - } |
|
423 | - return null; |
|
424 | - } |
|
425 | - |
|
426 | - |
|
427 | - /** |
|
428 | - * Returns whether the event ids passed in all share the same message template group for the current message type |
|
429 | - * and messenger. |
|
430 | - * |
|
431 | - * @param array $event_ids |
|
432 | - * @return bool true means they DO share the same message template group, false means they don't. |
|
433 | - * @throws EE_Error |
|
434 | - * @throws InvalidArgumentException |
|
435 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
436 | - * @throws InvalidInterfaceException |
|
437 | - */ |
|
438 | - protected function _queue_shares_same_message_template_group_for_events(array $event_ids) |
|
439 | - { |
|
440 | - foreach ($this->_current_data_handler->events as $event) { |
|
441 | - $event_ids[ $event['ID'] ] = $event['ID']; |
|
442 | - } |
|
443 | - $count_of_message_template_groups = EEM_Message_Template_Group::instance()->count( |
|
444 | - array( |
|
445 | - array( |
|
446 | - 'Event.EVT_ID' => array('IN', $event_ids), |
|
447 | - 'MTP_messenger' => $this->_current_messenger->name, |
|
448 | - 'MTP_message_type' => $this->_current_message_type->name, |
|
449 | - ), |
|
450 | - ), |
|
451 | - 'GRP_ID', |
|
452 | - true |
|
453 | - ); |
|
454 | - return $count_of_message_template_groups === 1; |
|
455 | - } |
|
456 | - |
|
457 | - |
|
458 | - /** |
|
459 | - * This will get the shared message template group for events that are in the current data handler but ONLY if |
|
460 | - * there's a single shared message template group among all the events. Otherwise it returns null. |
|
461 | - * |
|
462 | - * @param array $event_ids |
|
463 | - * @return EE_Message_Template_Group|null |
|
464 | - * @throws EE_Error |
|
465 | - * @throws InvalidArgumentException |
|
466 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
467 | - * @throws InvalidInterfaceException |
|
468 | - */ |
|
469 | - protected function _get_shared_message_template_for_events(array $event_ids) |
|
470 | - { |
|
471 | - $message_template_group = null; |
|
472 | - if ($this->_queue_shares_same_message_template_group_for_events($event_ids)) { |
|
473 | - $message_template_group = EEM_Message_Template_Group::instance()->get_one( |
|
474 | - array( |
|
475 | - array( |
|
476 | - 'Event.EVT_ID' => array('IN', $event_ids), |
|
477 | - 'MTP_messenger' => $this->_current_messenger->name, |
|
478 | - 'MTP_message_type' => $this->_current_message_type->name, |
|
479 | - 'MTP_is_active' => true, |
|
480 | - ), |
|
481 | - 'group_by' => 'GRP_ID', |
|
482 | - ) |
|
483 | - ); |
|
484 | - // store this in the collection if its valid |
|
485 | - if ($message_template_group instanceof EE_Message_Template_Group) { |
|
486 | - $this->_template_collection->add( |
|
487 | - $message_template_group, |
|
488 | - $event_ids |
|
489 | - ); |
|
490 | - } |
|
491 | - } |
|
492 | - return $message_template_group; |
|
493 | - } |
|
494 | - |
|
495 | - |
|
496 | - /** |
|
497 | - * Retrieves the global message template group for the current messenger and message type. |
|
498 | - * |
|
499 | - * @return EE_Message_Template_Group|null |
|
500 | - * @throws EE_Error |
|
501 | - * @throws InvalidArgumentException |
|
502 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
503 | - * @throws InvalidInterfaceException |
|
504 | - */ |
|
505 | - protected function _get_global_message_template_group_for_current_messenger_and_message_type() |
|
506 | - { |
|
507 | - // first check the collection (we use an array with 0 in it to represent global groups). |
|
508 | - $global_message_template_group = $this->_template_collection->get_by_key( |
|
509 | - $this->_template_collection->getKey( |
|
510 | - $this->_current_messenger->name, |
|
511 | - $this->_current_message_type->name, |
|
512 | - array(0) |
|
513 | - ) |
|
514 | - ); |
|
515 | - |
|
516 | - // if we don't have a group lets hit the db. |
|
517 | - if (! $global_message_template_group instanceof EE_Message_Template_Group) { |
|
518 | - $global_message_template_group = EEM_Message_Template_Group::instance()->get_one( |
|
519 | - array( |
|
520 | - array( |
|
521 | - 'MTP_messenger' => $this->_current_messenger->name, |
|
522 | - 'MTP_message_type' => $this->_current_message_type->name, |
|
523 | - 'MTP_is_active' => true, |
|
524 | - 'MTP_is_global' => true, |
|
525 | - ), |
|
526 | - ) |
|
527 | - ); |
|
528 | - // if we have a group, add it to the collection. |
|
529 | - if ($global_message_template_group instanceof EE_Message_Template_Group) { |
|
530 | - $this->_template_collection->add( |
|
531 | - $global_message_template_group, |
|
532 | - array(0) |
|
533 | - ); |
|
534 | - } |
|
535 | - } |
|
536 | - return $global_message_template_group; |
|
537 | - } |
|
538 | - |
|
539 | - |
|
540 | - /** |
|
541 | - * Returns an array of event ids for all the events within the current data handler. |
|
542 | - * |
|
543 | - * @return array |
|
544 | - */ |
|
545 | - protected function _get_event_ids_from_current_data_handler() |
|
546 | - { |
|
547 | - $event_ids = array(); |
|
548 | - foreach ($this->_current_data_handler->events as $event) { |
|
549 | - $event_ids[ $event['ID'] ] = $event['ID']; |
|
550 | - } |
|
551 | - return $event_ids; |
|
552 | - } |
|
553 | - |
|
554 | - |
|
555 | - /** |
|
556 | - * Retrieves formatted array of template information for each context specific to the given |
|
557 | - * EE_Message_Template_Group |
|
558 | - * |
|
559 | - * @param EE_Message_Template_Group $message_template_group |
|
560 | - * @return array The returned array is in this structure: |
|
561 | - * array( |
|
562 | - * 'field_name' => array( |
|
563 | - * 'context' => 'content' |
|
564 | - * ) |
|
565 | - * ) |
|
566 | - * @throws EE_Error |
|
567 | - */ |
|
568 | - protected function _get_templates(EE_Message_Template_Group $message_template_group) |
|
569 | - { |
|
570 | - $templates = array(); |
|
571 | - $context_templates = $message_template_group->context_templates(); |
|
572 | - foreach ($context_templates as $context => $template_fields) { |
|
573 | - foreach ($template_fields as $template_field => $template_obj) { |
|
574 | - if (! $template_obj instanceof EE_Message_Template) { |
|
575 | - continue; |
|
576 | - } |
|
577 | - $templates[ $template_field ][ $context ] = $template_obj->get('MTP_content'); |
|
578 | - } |
|
579 | - } |
|
580 | - return $templates; |
|
581 | - } |
|
582 | - |
|
583 | - |
|
584 | - /** |
|
585 | - * Assembles new fully generated EE_Message objects and adds to _ready_queue |
|
586 | - * |
|
587 | - * @param array $addressees Array of EE_Messages_Addressee objects indexed by message type |
|
588 | - * context. |
|
589 | - * @param array $templates formatted array of templates used for parsing data. |
|
590 | - * @param EE_Message_Template_Group $message_template_group |
|
591 | - * @return bool true if message generation went a-ok. false if some sort of exception occurred. Note: The |
|
592 | - * method will attempt to generate ALL EE_Message objects and add to |
|
593 | - * the _ready_queue. Successfully generated messages get added to the |
|
594 | - * queue with EEM_Message::status_idle, unsuccessfully generated |
|
595 | - * messages will get added to the queue as EEM_Message::status_failed. |
|
596 | - * Very rarely should "false" be returned from this method. |
|
597 | - * @throws EE_Error |
|
598 | - */ |
|
599 | - protected function _assemble_messages($addressees, $templates, EE_Message_Template_Group $message_template_group) |
|
600 | - { |
|
601 | - |
|
602 | - // if templates are empty then get out because we can't generate anything. |
|
603 | - if (! $templates) { |
|
604 | - $this->_error_msg[] = esc_html__( |
|
605 | - 'Unable to assemble messages because there are no templates retrieved for generating the messages with', |
|
606 | - 'event_espresso' |
|
607 | - ); |
|
608 | - return false; |
|
609 | - } |
|
610 | - |
|
611 | - // We use this as the counter for generated messages because don't forget we may be executing this inside of a |
|
612 | - // generation_queue. So _ready_queue may have generated EE_Message objects already. |
|
613 | - $generated_count = 0; |
|
614 | - foreach ($addressees as $context => $recipients) { |
|
615 | - foreach ($recipients as $recipient) { |
|
616 | - $message = $this->_setup_message_object($context, $recipient, $templates, $message_template_group); |
|
617 | - if ($message instanceof EE_Message) { |
|
618 | - $this->_ready_queue->add( |
|
619 | - $message, |
|
620 | - array(), |
|
621 | - $this->_generation_queue->get_message_repository()->is_preview(), |
|
622 | - $this->_generation_queue->get_message_repository()->is_test_send() |
|
623 | - ); |
|
624 | - $generated_count++; |
|
625 | - } |
|
626 | - |
|
627 | - // if the current MSG being generated is for a test send then we'll only use ONE message in the |
|
628 | - // generation. |
|
629 | - if ($this->_generation_queue->get_message_repository()->is_test_send()) { |
|
630 | - break 2; |
|
631 | - } |
|
632 | - } |
|
633 | - } |
|
634 | - |
|
635 | - // if there are no generated messages then something else fatal went wrong. |
|
636 | - return $generated_count > 0; |
|
637 | - } |
|
638 | - |
|
639 | - |
|
640 | - /** |
|
641 | - * @param string $context The context for the generated message. |
|
642 | - * @param EE_Messages_Addressee $recipient |
|
643 | - * @param array $templates formatted array of templates used for parsing data. |
|
644 | - * @param EE_Message_Template_Group $message_template_group |
|
645 | - * @return bool|EE_Message |
|
646 | - * @throws EE_Error |
|
647 | - */ |
|
648 | - protected function _setup_message_object( |
|
649 | - $context, |
|
650 | - EE_Messages_Addressee $recipient, |
|
651 | - $templates, |
|
652 | - EE_Message_Template_Group $message_template_group |
|
653 | - ) { |
|
654 | - // stuff we already know |
|
655 | - $transaction_id = $recipient->txn instanceof EE_Transaction ? $recipient->txn->ID() : 0; |
|
656 | - $transaction_id = empty($transaction_id) && $this->_current_data_handler->txn instanceof EE_Transaction |
|
657 | - ? $this->_current_data_handler->txn->ID() |
|
658 | - : $transaction_id; |
|
659 | - $message_fields = array( |
|
660 | - 'GRP_ID' => $message_template_group->ID(), |
|
661 | - 'TXN_ID' => $transaction_id, |
|
662 | - 'MSG_messenger' => $this->_current_messenger->name, |
|
663 | - 'MSG_message_type' => $this->_current_message_type->name, |
|
664 | - 'MSG_context' => $context, |
|
665 | - ); |
|
666 | - |
|
667 | - // recipient id and type should be on the EE_Messages_Addressee object but if this is empty, let's try to grab |
|
668 | - // the info from the att_obj found in the EE_Messages_Addressee object. |
|
669 | - if (empty($recipient->recipient_id) || empty($recipient->recipient_type)) { |
|
670 | - $message_fields['MSG_recipient_ID'] = $recipient->att_obj instanceof EE_Attendee |
|
671 | - ? $recipient->att_obj->ID() |
|
672 | - : 0; |
|
673 | - $message_fields['MSG_recipient_type'] = 'Attendee'; |
|
674 | - } else { |
|
675 | - $message_fields['MSG_recipient_ID'] = $recipient->recipient_id; |
|
676 | - $message_fields['MSG_recipient_type'] = $recipient->recipient_type; |
|
677 | - } |
|
678 | - $message = EE_Message_Factory::create($message_fields); |
|
679 | - |
|
680 | - // grab valid shortcodes for shortcode parser |
|
681 | - $mt_shortcodes = $this->_current_message_type->get_valid_shortcodes(); |
|
682 | - $m_shortcodes = $this->_current_messenger->get_valid_shortcodes(); |
|
683 | - |
|
684 | - // if the 'to' field is empty or the context is inactive we skip EXCEPT if we're previewing |
|
685 | - if (( |
|
686 | - ( |
|
687 | - empty($templates['to'][ $context ]) |
|
688 | - && ! $this->_current_messenger->allow_empty_to_field() |
|
689 | - ) |
|
690 | - || ! $message_template_group->is_context_active($context) |
|
691 | - ) |
|
692 | - && ! $this->_generation_queue->get_message_repository()->is_preview() |
|
693 | - ) { |
|
694 | - // we silently exit here and do NOT record a fail because the message is "turned off" by having no "to" |
|
695 | - // field. |
|
696 | - return false; |
|
697 | - } |
|
698 | - $error_msg = array(); |
|
699 | - foreach ($templates as $field => $field_context) { |
|
700 | - $error_msg = array(); |
|
701 | - // let's setup the valid shortcodes for the incoming context. |
|
702 | - $valid_shortcodes = $mt_shortcodes[ $context ]; |
|
703 | - // merge in valid shortcodes for the field. |
|
704 | - $shortcodes = isset($m_shortcodes[ $field ]) ? $m_shortcodes[ $field ] : $valid_shortcodes; |
|
705 | - if (isset($templates[ $field ][ $context ])) { |
|
706 | - // prefix field. |
|
707 | - $column_name = 'MSG_' . $field; |
|
708 | - try { |
|
709 | - $content = $this->_shortcode_parser->parse_message_template( |
|
710 | - $templates[ $field ][ $context ], |
|
711 | - $recipient, |
|
712 | - $shortcodes, |
|
713 | - $this->_current_message_type, |
|
714 | - $this->_current_messenger, |
|
715 | - $message |
|
716 | - ); |
|
717 | - // the model field removes slashes when setting (usually necessary when the input is from the |
|
718 | - // request) but this value is from another model and has no slashes. So add them so it matchces |
|
719 | - // what the field expected (otherwise slashes will have been stripped from this an extra time) |
|
720 | - $message->set_field_or_extra_meta($column_name, addslashes($content)); |
|
721 | - } catch (EE_Error $e) { |
|
722 | - $error_msg[] = sprintf( |
|
723 | - esc_html__( |
|
724 | - 'There was a problem generating the content for the field %s: %s', |
|
725 | - 'event_espresso' |
|
726 | - ), |
|
727 | - $field, |
|
728 | - $e->getMessage() |
|
729 | - ); |
|
730 | - $message->set_STS_ID(EEM_Message::status_failed); |
|
731 | - } |
|
732 | - } |
|
733 | - } |
|
734 | - |
|
735 | - if ($message->STS_ID() === EEM_Message::status_failed) { |
|
736 | - $error_msg = esc_html__('There were problems generating this message:', 'event_espresso') |
|
737 | - . "\n" |
|
738 | - . implode("\n", $error_msg); |
|
739 | - $message->set_error_message($error_msg); |
|
740 | - } else { |
|
741 | - $message->set_STS_ID(EEM_Message::status_idle); |
|
742 | - } |
|
743 | - return $message; |
|
744 | - } |
|
745 | - |
|
746 | - |
|
747 | - /** |
|
748 | - * This verifies that the incoming array has a EE_messenger object and a EE_message_type object and sets appropriate |
|
749 | - * error message if either is missing. |
|
750 | - * |
|
751 | - * @return bool true means there were no errors, false means there were errors. |
|
752 | - * @throws EE_Error |
|
753 | - * @throws ReflectionException |
|
754 | - */ |
|
755 | - protected function _verify() |
|
756 | - { |
|
757 | - // reset error message to an empty array. |
|
758 | - $this->_error_msg = array(); |
|
759 | - $valid = true; |
|
760 | - $valid = $valid ? $this->_validate_messenger_and_message_type() : $valid; |
|
761 | - $valid = $valid ? $this->_validate_and_setup_data() : $valid; |
|
762 | - |
|
763 | - // set the verified flag so we know everything has been validated. |
|
764 | - $this->_verified = $valid; |
|
765 | - |
|
766 | - return $valid; |
|
767 | - } |
|
768 | - |
|
769 | - |
|
770 | - /** |
|
771 | - * This accepts an array and validates that it is an array indexed by context with each value being an array of |
|
772 | - * EE_Messages_Addressee objects. |
|
773 | - * |
|
774 | - * @param array $addressees Keys correspond to contexts for the message type and values are EE_Messages_Addressee[] |
|
775 | - * @return bool |
|
776 | - */ |
|
777 | - protected function _valid_addressees($addressees) |
|
778 | - { |
|
779 | - if (! $addressees || ! is_array($addressees)) { |
|
780 | - return false; |
|
781 | - } |
|
782 | - |
|
783 | - foreach ($addressees as $addressee_array) { |
|
784 | - foreach ($addressee_array as $addressee) { |
|
785 | - if (! $addressee instanceof EE_Messages_Addressee) { |
|
786 | - return false; |
|
787 | - } |
|
788 | - } |
|
789 | - } |
|
790 | - return true; |
|
791 | - } |
|
792 | - |
|
793 | - |
|
794 | - /** |
|
795 | - * This validates the messenger, message type, and presences of generation data for the current EE_Message in the |
|
796 | - * queue. This process sets error messages if something is wrong. |
|
797 | - * |
|
798 | - * @return bool true is if there are no errors. false is if there is. |
|
799 | - */ |
|
800 | - protected function _validate_messenger_and_message_type() |
|
801 | - { |
|
802 | - |
|
803 | - // first are there any existing error messages? If so then return. |
|
804 | - if ($this->_error_msg) { |
|
805 | - return false; |
|
806 | - } |
|
807 | - /** @type EE_Message $message */ |
|
808 | - $message = $this->_generation_queue->get_message_repository()->current(); |
|
809 | - try { |
|
810 | - $this->_current_messenger = $message->valid_messenger(true) |
|
811 | - ? $message->messenger_object() |
|
812 | - : null; |
|
813 | - } catch (Exception $e) { |
|
814 | - $this->_error_msg[] = $e->getMessage(); |
|
815 | - } |
|
816 | - try { |
|
817 | - $this->_current_message_type = $message->valid_message_type(true) |
|
818 | - ? $message->message_type_object() |
|
819 | - : null; |
|
820 | - } catch (Exception $e) { |
|
821 | - $this->_error_msg[] = $e->getMessage(); |
|
822 | - } |
|
823 | - |
|
824 | - /** |
|
825 | - * Check if there is any generation data, but only if this is not for a preview. |
|
826 | - */ |
|
827 | - if (! $this->_generation_queue->get_message_repository()->get_generation_data() |
|
828 | - && ( |
|
829 | - ! $this->_generation_queue->get_message_repository()->is_preview() |
|
830 | - && $this->_generation_queue->get_message_repository()->get_data_handler() |
|
831 | - !== 'EE_Messages_Preview_incoming_data' |
|
832 | - ) |
|
833 | - ) { |
|
834 | - $this->_error_msg[] = esc_html__( |
|
835 | - 'There is no generation data for this message. Unable to generate.', |
|
836 | - 'event_espresso' |
|
837 | - ); |
|
838 | - } |
|
839 | - |
|
840 | - return empty($this->_error_msg); |
|
841 | - } |
|
842 | - |
|
843 | - |
|
844 | - /** |
|
845 | - * This method retrieves the expected data handler for the message type and validates the generation data for that |
|
846 | - * data handler. |
|
847 | - * |
|
848 | - * @return bool true means there are no errors. false means there were errors (and handler did not get setup). |
|
849 | - * @throws EE_Error |
|
850 | - * @throws ReflectionException |
|
851 | - */ |
|
852 | - protected function _validate_and_setup_data() |
|
853 | - { |
|
854 | - |
|
855 | - // First, are there any existing error messages? If so, return because if there were errors elsewhere this can't |
|
856 | - // be used anyways. |
|
857 | - if ($this->_error_msg) { |
|
858 | - return false; |
|
859 | - } |
|
860 | - |
|
861 | - $generation_data = $this->_generation_queue->get_message_repository()->get_generation_data(); |
|
862 | - |
|
863 | - /** @type EE_Messages_incoming_data $data_handler_class_name - well not really... just the class name actually*/ |
|
864 | - $data_handler_class_name = $this->_generation_queue->get_message_repository()->get_data_handler() |
|
865 | - ? $this->_generation_queue->get_message_repository()->get_data_handler() |
|
866 | - : 'EE_Messages_' . $this->_current_message_type->get_data_handler($generation_data) . '_incoming_data'; |
|
867 | - |
|
868 | - // If this EE_Message is for a preview, then let's switch out to the preview data handler. |
|
869 | - if ($this->_generation_queue->get_message_repository()->is_preview()) { |
|
870 | - $data_handler_class_name = 'EE_Messages_Preview_incoming_data'; |
|
871 | - } |
|
872 | - |
|
873 | - // First get the class name for the data handler (and also verifies it exists. |
|
874 | - if (! class_exists($data_handler_class_name)) { |
|
875 | - $this->_error_msg[] = sprintf( |
|
876 | - esc_html__( |
|
877 | - 'The included data handler class name does not match any valid, accessible, "%1$s" classes. Looking for %2$s.', |
|
878 | - 'event_espresso' |
|
879 | - ), |
|
880 | - 'EE_Messages_incoming_data', |
|
881 | - $data_handler_class_name |
|
882 | - ); |
|
883 | - return false; |
|
884 | - } |
|
885 | - |
|
886 | - // convert generation_data for data_handler_instantiation. |
|
887 | - $generation_data = $data_handler_class_name::convert_data_from_persistent_storage($generation_data); |
|
888 | - |
|
889 | - // note, this may set error messages as well. |
|
890 | - $this->_set_data_handler($generation_data, $data_handler_class_name); |
|
891 | - |
|
892 | - return empty($this->_error_msg); |
|
893 | - } |
|
894 | - |
|
895 | - |
|
896 | - /** |
|
897 | - * Sets the $_current_data_handler property that is used for generating the current EE_Message in the queue, and |
|
898 | - * adds it to the _data repository. |
|
899 | - * |
|
900 | - * @param mixed $generating_data This is data expected by the instantiated data handler. |
|
901 | - * @param string $data_handler_class_name This is the reference string indicating what data handler is being |
|
902 | - * instantiated. |
|
903 | - * @return void . |
|
904 | - * @throws EE_Error |
|
905 | - * @throws ReflectionException |
|
906 | - */ |
|
907 | - protected function _set_data_handler($generating_data, $data_handler_class_name) |
|
908 | - { |
|
909 | - // valid classname for the data handler. Now let's setup the key for the data handler repository to see if there |
|
910 | - // is already a ready data handler in the repository. |
|
911 | - $this->_current_data_handler = $this->_data_handler_collection->get_by_key( |
|
912 | - $this->_data_handler_collection->get_key( |
|
913 | - $data_handler_class_name, |
|
914 | - $generating_data |
|
915 | - ) |
|
916 | - ); |
|
917 | - if (! $this->_current_data_handler instanceof EE_Messages_incoming_data) { |
|
918 | - // no saved data_handler in the repo so let's set one up and add it to the repo. |
|
919 | - try { |
|
920 | - $this->_current_data_handler = new $data_handler_class_name($generating_data); |
|
921 | - $this->_data_handler_collection->add($this->_current_data_handler, $generating_data); |
|
922 | - } catch (EE_Error $e) { |
|
923 | - $this->_error_msg[] = $e->get_error(); |
|
924 | - } |
|
925 | - } |
|
926 | - } |
|
927 | - |
|
928 | - |
|
929 | - /** |
|
930 | - * The queued EE_Message for generation does not save the data used for generation as objects |
|
931 | - * because serialization of those objects could be problematic if the data is saved to the db. |
|
932 | - * So this method calls the static method on the associated data_handler for the given message_type |
|
933 | - * and that preps the data for later instantiation when generating. |
|
934 | - * |
|
935 | - * @param EE_Message_To_Generate $message_to_generate |
|
936 | - * @param bool $preview Indicate whether this is being used for a preview or not. |
|
937 | - * @return mixed Prepped data for persisting to the queue. false is returned if unable to prep data. |
|
938 | - */ |
|
939 | - protected function _prepare_data_for_queue(EE_Message_To_Generate $message_to_generate, $preview) |
|
940 | - { |
|
941 | - /** @type EE_Messages_incoming_data $data_handler - well not really... just the class name actually */ |
|
942 | - $data_handler = $message_to_generate->get_data_handler_class_name($preview); |
|
943 | - if (! $message_to_generate->valid()) { |
|
944 | - return false; // unable to get the data because the info in the EE_Message_To_Generate class is invalid. |
|
945 | - } |
|
946 | - return $data_handler::convert_data_for_persistent_storage($message_to_generate->data()); |
|
947 | - } |
|
948 | - |
|
949 | - |
|
950 | - /** |
|
951 | - * This sets up a EEM_Message::status_incomplete EE_Message object and adds it to the generation queue. |
|
952 | - * |
|
953 | - * @param EE_Message_To_Generate $message_to_generate |
|
954 | - * @param bool $test_send Whether this is just a test send or not. Typically used for previews. |
|
955 | - */ |
|
956 | - public function create_and_add_message_to_queue(EE_Message_To_Generate $message_to_generate, $test_send = false) |
|
957 | - { |
|
958 | - // prep data |
|
959 | - $data = $this->_prepare_data_for_queue($message_to_generate, $message_to_generate->preview()); |
|
960 | - |
|
961 | - $message = $message_to_generate->get_EE_Message(); |
|
962 | - |
|
963 | - $GRP_ID = EE_Registry::instance()->REQ->get('GRP_ID', 0) > 0 |
|
964 | - ? EE_Registry::instance()->REQ->get('GRP_ID') |
|
965 | - : apply_filters( |
|
966 | - 'FHEE__EE_Messages_Generator__create_and_add_message_to_queue_GRP_ID', |
|
967 | - 0, |
|
968 | - $message, |
|
969 | - $message_to_generate, |
|
970 | - $test_send |
|
971 | - ); |
|
972 | - |
|
973 | - if ($GRP_ID > 0) { |
|
974 | - $message->set_GRP_ID($GRP_ID); |
|
975 | - } |
|
976 | - |
|
977 | - if ($data === false) { |
|
978 | - $message->set_STS_ID(EEM_Message::status_failed); |
|
979 | - $message->set_error_message( |
|
980 | - esc_html__( |
|
981 | - 'Unable to prepare data for persistence to the database.', |
|
982 | - 'event_espresso' |
|
983 | - ) |
|
984 | - ); |
|
985 | - } else { |
|
986 | - // make sure that the data handler is cached on the message as well |
|
987 | - $data['data_handler_class_name'] = $message_to_generate->get_data_handler_class_name(); |
|
988 | - } |
|
989 | - |
|
990 | - $this->_generation_queue->add($message, $data, $message_to_generate->preview(), $test_send); |
|
991 | - } |
|
17 | + /** |
|
18 | + * @type EE_Messages_Data_Handler_Collection |
|
19 | + */ |
|
20 | + protected $_data_handler_collection; |
|
21 | + |
|
22 | + /** |
|
23 | + * @type EE_Message_Template_Group_Collection |
|
24 | + */ |
|
25 | + protected $_template_collection; |
|
26 | + |
|
27 | + /** |
|
28 | + * This will hold the data handler for the current EE_Message being generated. |
|
29 | + * |
|
30 | + * @type EE_Messages_incoming_data |
|
31 | + */ |
|
32 | + protected $_current_data_handler; |
|
33 | + |
|
34 | + /** |
|
35 | + * This holds the EE_Messages_Queue that contains the messages to generate. |
|
36 | + * |
|
37 | + * @type EE_Messages_Queue |
|
38 | + */ |
|
39 | + protected $_generation_queue; |
|
40 | + |
|
41 | + /** |
|
42 | + * This holds the EE_Messages_Queue that will store the generated EE_Message objects. |
|
43 | + * |
|
44 | + * @type EE_Messages_Queue |
|
45 | + */ |
|
46 | + protected $_ready_queue; |
|
47 | + |
|
48 | + /** |
|
49 | + * This is a container for any error messages that get created through the generation |
|
50 | + * process. |
|
51 | + * |
|
52 | + * @type array |
|
53 | + */ |
|
54 | + protected $_error_msg = array(); |
|
55 | + |
|
56 | + /** |
|
57 | + * Flag used to set when the current EE_Message in the generation queue has been verified. |
|
58 | + * |
|
59 | + * @type bool |
|
60 | + */ |
|
61 | + protected $_verified = false; |
|
62 | + |
|
63 | + /** |
|
64 | + * This will hold the current messenger object corresponding with the current EE_Message in the generation queue. |
|
65 | + * |
|
66 | + * @type EE_messenger |
|
67 | + */ |
|
68 | + protected $_current_messenger; |
|
69 | + |
|
70 | + /** |
|
71 | + * This will hold the current message type object corresponding with the current EE_Message in the generation queue. |
|
72 | + * |
|
73 | + * @type EE_message_type |
|
74 | + */ |
|
75 | + protected $_current_message_type; |
|
76 | + |
|
77 | + /** |
|
78 | + * @type EEH_Parse_Shortcodes |
|
79 | + */ |
|
80 | + protected $_shortcode_parser; |
|
81 | + |
|
82 | + |
|
83 | + /** |
|
84 | + * @param EE_Messages_Queue $generation_queue |
|
85 | + * @param \EE_Messages_Queue $ready_queue |
|
86 | + * @param \EE_Messages_Data_Handler_Collection $data_handler_collection |
|
87 | + * @param \EE_Message_Template_Group_Collection $template_collection |
|
88 | + * @param \EEH_Parse_Shortcodes $shortcode_parser |
|
89 | + */ |
|
90 | + public function __construct( |
|
91 | + EE_Messages_Queue $generation_queue, |
|
92 | + EE_Messages_Queue $ready_queue, |
|
93 | + EE_Messages_Data_Handler_Collection $data_handler_collection, |
|
94 | + EE_Message_Template_Group_Collection $template_collection, |
|
95 | + EEH_Parse_Shortcodes $shortcode_parser |
|
96 | + ) { |
|
97 | + $this->_generation_queue = $generation_queue; |
|
98 | + $this->_ready_queue = $ready_queue; |
|
99 | + $this->_data_handler_collection = $data_handler_collection; |
|
100 | + $this->_template_collection = $template_collection; |
|
101 | + $this->_shortcode_parser = $shortcode_parser; |
|
102 | + } |
|
103 | + |
|
104 | + |
|
105 | + /** |
|
106 | + * @return EE_Messages_Queue |
|
107 | + */ |
|
108 | + public function generation_queue() |
|
109 | + { |
|
110 | + return $this->_generation_queue; |
|
111 | + } |
|
112 | + |
|
113 | + |
|
114 | + /** |
|
115 | + * This iterates through the provided queue and generates the EE_Message objects. |
|
116 | + * When iterating through the queue, the queued item that served as the base for generating other EE_Message |
|
117 | + * objects gets removed and the new EE_Message objects get added to a NEW queue. The NEW queue is then returned |
|
118 | + * for the caller to decide what to do with it. |
|
119 | + * |
|
120 | + * @param bool $save Whether to save the EE_Message objects in the new queue or just return. |
|
121 | + * @return EE_Messages_Queue The new queue for holding generated EE_Message objects. |
|
122 | + * @throws EE_Error |
|
123 | + * @throws ReflectionException |
|
124 | + */ |
|
125 | + public function generate($save = true) |
|
126 | + { |
|
127 | + // iterate through the messages in the queue, generate, and add to new queue. |
|
128 | + $this->_generation_queue->get_message_repository()->rewind(); |
|
129 | + while ($this->_generation_queue->get_message_repository()->valid()) { |
|
130 | + // reset "current" properties |
|
131 | + $this->_reset_current_properties(); |
|
132 | + |
|
133 | + /** @type EE_Message $msg */ |
|
134 | + $msg = $this->_generation_queue->get_message_repository()->current(); |
|
135 | + |
|
136 | + /** |
|
137 | + * need to get the next object and capture it for setting manually after deletes. The reason is that when |
|
138 | + * an object is removed from the repo then valid for the next object will fail. |
|
139 | + */ |
|
140 | + $this->_generation_queue->get_message_repository()->next(); |
|
141 | + $next_msg = $this->_generation_queue->get_message_repository()->current(); |
|
142 | + // restore pointer to current item |
|
143 | + $this->_generation_queue->get_message_repository()->set_current($msg); |
|
144 | + |
|
145 | + // skip and delete if the current $msg is NOT incomplete (queued for generation) |
|
146 | + if ($msg->STS_ID() !== EEM_Message::status_incomplete) { |
|
147 | + // we keep this item in the db just remove from the repo. |
|
148 | + $this->_generation_queue->get_message_repository()->remove($msg); |
|
149 | + // next item |
|
150 | + $this->_generation_queue->get_message_repository()->set_current($next_msg); |
|
151 | + continue; |
|
152 | + } |
|
153 | + |
|
154 | + if ($this->_verify()) { |
|
155 | + // let's get generating! |
|
156 | + $this->_generate(); |
|
157 | + } |
|
158 | + |
|
159 | + // don't persist debug_only messages if the messages system is not in debug mode. |
|
160 | + if ($msg->STS_ID() === EEM_Message::status_debug_only |
|
161 | + && ! EEM_Message::debug() |
|
162 | + ) { |
|
163 | + do_action( |
|
164 | + 'AHEE__EE_Messages_Generator__generate__before_debug_delete', |
|
165 | + $msg, |
|
166 | + $this->_error_msg, |
|
167 | + $this->_current_messenger, |
|
168 | + $this->_current_message_type, |
|
169 | + $this->_current_data_handler |
|
170 | + ); |
|
171 | + $this->_generation_queue->get_message_repository()->delete(); |
|
172 | + $this->_generation_queue->get_message_repository()->set_current($next_msg); |
|
173 | + continue; |
|
174 | + } |
|
175 | + |
|
176 | + // if there are error messages then let's set the status and the error message. |
|
177 | + if ($this->_error_msg) { |
|
178 | + // if the status is already debug only, then let's leave it at that. |
|
179 | + if ($msg->STS_ID() !== EEM_Message::status_debug_only) { |
|
180 | + $msg->set_STS_ID(EEM_Message::status_failed); |
|
181 | + } |
|
182 | + do_action( |
|
183 | + 'AHEE__EE_Messages_Generator__generate__processing_failed_message', |
|
184 | + $msg, |
|
185 | + $this->_error_msg, |
|
186 | + $this->_current_messenger, |
|
187 | + $this->_current_message_type, |
|
188 | + $this->_current_data_handler |
|
189 | + ); |
|
190 | + $msg->set_error_message( |
|
191 | + esc_html__('Message failed to generate for the following reasons: ', 'event_espresso') |
|
192 | + . "\n" |
|
193 | + . implode("\n", $this->_error_msg) |
|
194 | + ); |
|
195 | + $msg->set_modified(time()); |
|
196 | + } else { |
|
197 | + do_action( |
|
198 | + 'AHEE__EE_Messages_Generator__generate__before_successful_generated_message_delete', |
|
199 | + $msg, |
|
200 | + $this->_error_msg, |
|
201 | + $this->_current_messenger, |
|
202 | + $this->_current_message_type, |
|
203 | + $this->_current_data_handler |
|
204 | + ); |
|
205 | + // remove from db |
|
206 | + $this->_generation_queue->get_message_repository()->delete(); |
|
207 | + } |
|
208 | + // next item |
|
209 | + $this->_generation_queue->get_message_repository()->set_current($next_msg); |
|
210 | + } |
|
211 | + |
|
212 | + // generation queue is ALWAYS saved to record any errors in the generation process. |
|
213 | + $this->_generation_queue->save(); |
|
214 | + |
|
215 | + /** |
|
216 | + * save _ready_queue if flag set. |
|
217 | + * Note: The EE_Message objects have values set via the EE_Base_Class::set_field_or_extra_meta() method. This |
|
218 | + * means if a field was added that is not a valid database column. The EE_Message was already saved to the db |
|
219 | + * so a EE_Extra_Meta entry could be created and attached to the EE_Message. In those cases the save flag is |
|
220 | + * irrelevant. |
|
221 | + */ |
|
222 | + if ($save) { |
|
223 | + $this->_ready_queue->save(); |
|
224 | + } |
|
225 | + |
|
226 | + // final reset of properties |
|
227 | + $this->_reset_current_properties(); |
|
228 | + |
|
229 | + return $this->_ready_queue; |
|
230 | + } |
|
231 | + |
|
232 | + |
|
233 | + /** |
|
234 | + * This resets all the properties used for holding "current" values corresponding to the current EE_Message object |
|
235 | + * in the generation queue. |
|
236 | + */ |
|
237 | + protected function _reset_current_properties() |
|
238 | + { |
|
239 | + $this->_verified = false; |
|
240 | + // make sure any _data value in the current message type is reset |
|
241 | + if ($this->_current_message_type instanceof EE_message_type) { |
|
242 | + $this->_current_message_type->reset_data(); |
|
243 | + } |
|
244 | + $this->_current_messenger = $this->_current_message_type = $this->_current_data_handler = null; |
|
245 | + } |
|
246 | + |
|
247 | + |
|
248 | + /** |
|
249 | + * This proceeds with the actual generation of a message. By the time this is called, there should already be a |
|
250 | + * $_current_data_handler set and all incoming information should be validated for the current EE_Message in the |
|
251 | + * _generating_queue. |
|
252 | + * |
|
253 | + * @return bool Whether the message was successfully generated or not. |
|
254 | + * @throws EE_Error |
|
255 | + * @throws InvalidArgumentException |
|
256 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
257 | + * @throws InvalidInterfaceException |
|
258 | + */ |
|
259 | + protected function _generate() |
|
260 | + { |
|
261 | + // double check verification has run and that everything is ready to work with (saves us having to validate |
|
262 | + // everything again). |
|
263 | + if (! $this->_verified) { |
|
264 | + return false; // get out because we don't have a valid setup to work with. |
|
265 | + } |
|
266 | + |
|
267 | + |
|
268 | + try { |
|
269 | + $addressees = $this->_current_message_type->get_addressees( |
|
270 | + $this->_current_data_handler, |
|
271 | + $this->_generation_queue->get_message_repository()->current()->context() |
|
272 | + ); |
|
273 | + } catch (EE_Error $e) { |
|
274 | + $this->_error_msg[] = $e->getMessage(); |
|
275 | + return false; |
|
276 | + } |
|
277 | + |
|
278 | + |
|
279 | + // if no addressees then get out because there is nothing to generation (possible bad data). |
|
280 | + if (! $this->_valid_addressees($addressees)) { |
|
281 | + do_action( |
|
282 | + 'AHEE__EE_Messages_Generator___generate__invalid_addressees', |
|
283 | + $this->_generation_queue->get_message_repository()->current(), |
|
284 | + $addressees, |
|
285 | + $this->_current_messenger, |
|
286 | + $this->_current_message_type, |
|
287 | + $this->_current_data_handler |
|
288 | + ); |
|
289 | + $this->_generation_queue->get_message_repository()->current()->set_STS_ID( |
|
290 | + EEM_Message::status_debug_only |
|
291 | + ); |
|
292 | + $this->_error_msg[] = esc_html__( |
|
293 | + 'This is not a critical error but an informational notice. Unable to generate messages EE_Messages_Addressee objects. There were no attendees prepared by the data handler. Sometimes this is because messages only get generated for certain registration statuses. For example, the ticket notice message type only goes to approved registrations.', |
|
294 | + 'event_espresso' |
|
295 | + ); |
|
296 | + return false; |
|
297 | + } |
|
298 | + |
|
299 | + $message_template_group = $this->_get_message_template_group(); |
|
300 | + |
|
301 | + // in the unlikely event there is no EE_Message_Template_Group available, get out! |
|
302 | + if (! $message_template_group instanceof EE_Message_Template_Group) { |
|
303 | + $this->_error_msg[] = esc_html__( |
|
304 | + 'Unable to get the Message Templates for the Message being generated. No message template group accessible.', |
|
305 | + 'event_espresso' |
|
306 | + ); |
|
307 | + return false; |
|
308 | + } |
|
309 | + |
|
310 | + // get formatted templates for using to parse and setup EE_Message objects. |
|
311 | + $templates = $this->_get_templates($message_template_group); |
|
312 | + |
|
313 | + |
|
314 | + // setup new EE_Message objects (and add to _ready_queue) |
|
315 | + return $this->_assemble_messages($addressees, $templates, $message_template_group); |
|
316 | + } |
|
317 | + |
|
318 | + |
|
319 | + /** |
|
320 | + * Retrieves the message template group being used for generating messages. |
|
321 | + * Note: this also utilizes the EE_Message_Template_Group_Collection to avoid having to hit the db multiple times. |
|
322 | + * |
|
323 | + * @return EE_Message_Template_Group|null |
|
324 | + * @throws EE_Error |
|
325 | + * @throws InvalidArgumentException |
|
326 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
327 | + * @throws InvalidInterfaceException |
|
328 | + */ |
|
329 | + protected function _get_message_template_group() |
|
330 | + { |
|
331 | + // first see if there is a specific message template group requested (current message in the queue has a specific |
|
332 | + // GRP_ID |
|
333 | + $message_template_group = $this->_specific_message_template_group_from_queue(); |
|
334 | + if ($message_template_group instanceof EE_Message_Template_Group) { |
|
335 | + return $message_template_group; |
|
336 | + } |
|
337 | + |
|
338 | + // get event_ids from the datahandler so we can check to see if there's already a message template group for them |
|
339 | + // in the collection. |
|
340 | + $event_ids = $this->_get_event_ids_from_current_data_handler(); |
|
341 | + $message_template_group = $this->_template_collection->get_by_key( |
|
342 | + $this->_template_collection->getKey( |
|
343 | + $this->_current_messenger->name, |
|
344 | + $this->_current_message_type->name, |
|
345 | + $event_ids |
|
346 | + ) |
|
347 | + ); |
|
348 | + |
|
349 | + // if we have a message template group then no need to hit the database, just return it. |
|
350 | + if ($message_template_group instanceof EE_Message_Template_Group) { |
|
351 | + return $message_template_group; |
|
352 | + } |
|
353 | + |
|
354 | + // okay made it here, so let's get the global group first for this messenger and message type to ensure |
|
355 | + // there is no override set. |
|
356 | + $global_message_template_group = |
|
357 | + $this->_get_global_message_template_group_for_current_messenger_and_message_type(); |
|
358 | + |
|
359 | + if ($global_message_template_group instanceof EE_Message_Template_Group |
|
360 | + && $global_message_template_group->get('MTP_is_override') |
|
361 | + ) { |
|
362 | + return $global_message_template_group; |
|
363 | + } |
|
364 | + |
|
365 | + // if we're still here, that means there was no message template group for the events in the collection and |
|
366 | + // the global message template group for the messenger and message type is not set for override. So next step is |
|
367 | + // to see if there is a common shared custom message template group for this set of events. |
|
368 | + $message_template_group = $this->_get_shared_message_template_for_events($event_ids); |
|
369 | + if ($message_template_group instanceof EE_Message_Template_Group) { |
|
370 | + return $message_template_group; |
|
371 | + } |
|
372 | + |
|
373 | + // STILL here? Okay that means the fallback is to just use the global message template group for this event set. |
|
374 | + // So we'll cache the global group for this event set (so this logic doesn't have to be repeated in this request) |
|
375 | + // and return it. |
|
376 | + if ($global_message_template_group instanceof EE_Message_Template_Group) { |
|
377 | + $this->_template_collection->add( |
|
378 | + $global_message_template_group, |
|
379 | + $event_ids |
|
380 | + ); |
|
381 | + return $global_message_template_group; |
|
382 | + } |
|
383 | + |
|
384 | + // if we land here that means there's NO active message template group for this set. |
|
385 | + // TODO this will be a good target for some optimization down the road. Whenever there is no active message |
|
386 | + // template group for a given event set then cache that result so we don't repeat the logic. However, for now, |
|
387 | + // this should likely bit hit rarely enough that it's not a significant issue. |
|
388 | + return null; |
|
389 | + } |
|
390 | + |
|
391 | + |
|
392 | + /** |
|
393 | + * This checks the current message in the queue and determines if there is a specific Message Template Group |
|
394 | + * requested for that message. |
|
395 | + * |
|
396 | + * @return EE_Message_Template_Group|null |
|
397 | + * @throws EE_Error |
|
398 | + * @throws InvalidArgumentException |
|
399 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
400 | + * @throws InvalidInterfaceException |
|
401 | + */ |
|
402 | + protected function _specific_message_template_group_from_queue() |
|
403 | + { |
|
404 | + // is there a GRP_ID already on the EE_Message object? If there is, then a specific template has been requested |
|
405 | + // so let's use that. |
|
406 | + $GRP_ID = $this->_generation_queue->get_message_repository()->current()->GRP_ID(); |
|
407 | + |
|
408 | + if ($GRP_ID) { |
|
409 | + // attempt to retrieve from repo first |
|
410 | + $message_template_group = $this->_template_collection->get_by_ID($GRP_ID); |
|
411 | + if ($message_template_group instanceof EE_Message_Template_Group) { |
|
412 | + return $message_template_group; // got it! |
|
413 | + } |
|
414 | + |
|
415 | + // nope don't have it yet. Get from DB then add to repo if its not here, then that means the current GRP_ID |
|
416 | + // is not valid, so we'll continue on in the code assuming there's NO GRP_ID. |
|
417 | + $message_template_group = EEM_Message_Template_Group::instance()->get_one_by_ID($GRP_ID); |
|
418 | + if ($message_template_group instanceof EE_Message_Template_Group) { |
|
419 | + $this->_template_collection->add($message_template_group); |
|
420 | + return $message_template_group; |
|
421 | + } |
|
422 | + } |
|
423 | + return null; |
|
424 | + } |
|
425 | + |
|
426 | + |
|
427 | + /** |
|
428 | + * Returns whether the event ids passed in all share the same message template group for the current message type |
|
429 | + * and messenger. |
|
430 | + * |
|
431 | + * @param array $event_ids |
|
432 | + * @return bool true means they DO share the same message template group, false means they don't. |
|
433 | + * @throws EE_Error |
|
434 | + * @throws InvalidArgumentException |
|
435 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
436 | + * @throws InvalidInterfaceException |
|
437 | + */ |
|
438 | + protected function _queue_shares_same_message_template_group_for_events(array $event_ids) |
|
439 | + { |
|
440 | + foreach ($this->_current_data_handler->events as $event) { |
|
441 | + $event_ids[ $event['ID'] ] = $event['ID']; |
|
442 | + } |
|
443 | + $count_of_message_template_groups = EEM_Message_Template_Group::instance()->count( |
|
444 | + array( |
|
445 | + array( |
|
446 | + 'Event.EVT_ID' => array('IN', $event_ids), |
|
447 | + 'MTP_messenger' => $this->_current_messenger->name, |
|
448 | + 'MTP_message_type' => $this->_current_message_type->name, |
|
449 | + ), |
|
450 | + ), |
|
451 | + 'GRP_ID', |
|
452 | + true |
|
453 | + ); |
|
454 | + return $count_of_message_template_groups === 1; |
|
455 | + } |
|
456 | + |
|
457 | + |
|
458 | + /** |
|
459 | + * This will get the shared message template group for events that are in the current data handler but ONLY if |
|
460 | + * there's a single shared message template group among all the events. Otherwise it returns null. |
|
461 | + * |
|
462 | + * @param array $event_ids |
|
463 | + * @return EE_Message_Template_Group|null |
|
464 | + * @throws EE_Error |
|
465 | + * @throws InvalidArgumentException |
|
466 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
467 | + * @throws InvalidInterfaceException |
|
468 | + */ |
|
469 | + protected function _get_shared_message_template_for_events(array $event_ids) |
|
470 | + { |
|
471 | + $message_template_group = null; |
|
472 | + if ($this->_queue_shares_same_message_template_group_for_events($event_ids)) { |
|
473 | + $message_template_group = EEM_Message_Template_Group::instance()->get_one( |
|
474 | + array( |
|
475 | + array( |
|
476 | + 'Event.EVT_ID' => array('IN', $event_ids), |
|
477 | + 'MTP_messenger' => $this->_current_messenger->name, |
|
478 | + 'MTP_message_type' => $this->_current_message_type->name, |
|
479 | + 'MTP_is_active' => true, |
|
480 | + ), |
|
481 | + 'group_by' => 'GRP_ID', |
|
482 | + ) |
|
483 | + ); |
|
484 | + // store this in the collection if its valid |
|
485 | + if ($message_template_group instanceof EE_Message_Template_Group) { |
|
486 | + $this->_template_collection->add( |
|
487 | + $message_template_group, |
|
488 | + $event_ids |
|
489 | + ); |
|
490 | + } |
|
491 | + } |
|
492 | + return $message_template_group; |
|
493 | + } |
|
494 | + |
|
495 | + |
|
496 | + /** |
|
497 | + * Retrieves the global message template group for the current messenger and message type. |
|
498 | + * |
|
499 | + * @return EE_Message_Template_Group|null |
|
500 | + * @throws EE_Error |
|
501 | + * @throws InvalidArgumentException |
|
502 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
503 | + * @throws InvalidInterfaceException |
|
504 | + */ |
|
505 | + protected function _get_global_message_template_group_for_current_messenger_and_message_type() |
|
506 | + { |
|
507 | + // first check the collection (we use an array with 0 in it to represent global groups). |
|
508 | + $global_message_template_group = $this->_template_collection->get_by_key( |
|
509 | + $this->_template_collection->getKey( |
|
510 | + $this->_current_messenger->name, |
|
511 | + $this->_current_message_type->name, |
|
512 | + array(0) |
|
513 | + ) |
|
514 | + ); |
|
515 | + |
|
516 | + // if we don't have a group lets hit the db. |
|
517 | + if (! $global_message_template_group instanceof EE_Message_Template_Group) { |
|
518 | + $global_message_template_group = EEM_Message_Template_Group::instance()->get_one( |
|
519 | + array( |
|
520 | + array( |
|
521 | + 'MTP_messenger' => $this->_current_messenger->name, |
|
522 | + 'MTP_message_type' => $this->_current_message_type->name, |
|
523 | + 'MTP_is_active' => true, |
|
524 | + 'MTP_is_global' => true, |
|
525 | + ), |
|
526 | + ) |
|
527 | + ); |
|
528 | + // if we have a group, add it to the collection. |
|
529 | + if ($global_message_template_group instanceof EE_Message_Template_Group) { |
|
530 | + $this->_template_collection->add( |
|
531 | + $global_message_template_group, |
|
532 | + array(0) |
|
533 | + ); |
|
534 | + } |
|
535 | + } |
|
536 | + return $global_message_template_group; |
|
537 | + } |
|
538 | + |
|
539 | + |
|
540 | + /** |
|
541 | + * Returns an array of event ids for all the events within the current data handler. |
|
542 | + * |
|
543 | + * @return array |
|
544 | + */ |
|
545 | + protected function _get_event_ids_from_current_data_handler() |
|
546 | + { |
|
547 | + $event_ids = array(); |
|
548 | + foreach ($this->_current_data_handler->events as $event) { |
|
549 | + $event_ids[ $event['ID'] ] = $event['ID']; |
|
550 | + } |
|
551 | + return $event_ids; |
|
552 | + } |
|
553 | + |
|
554 | + |
|
555 | + /** |
|
556 | + * Retrieves formatted array of template information for each context specific to the given |
|
557 | + * EE_Message_Template_Group |
|
558 | + * |
|
559 | + * @param EE_Message_Template_Group $message_template_group |
|
560 | + * @return array The returned array is in this structure: |
|
561 | + * array( |
|
562 | + * 'field_name' => array( |
|
563 | + * 'context' => 'content' |
|
564 | + * ) |
|
565 | + * ) |
|
566 | + * @throws EE_Error |
|
567 | + */ |
|
568 | + protected function _get_templates(EE_Message_Template_Group $message_template_group) |
|
569 | + { |
|
570 | + $templates = array(); |
|
571 | + $context_templates = $message_template_group->context_templates(); |
|
572 | + foreach ($context_templates as $context => $template_fields) { |
|
573 | + foreach ($template_fields as $template_field => $template_obj) { |
|
574 | + if (! $template_obj instanceof EE_Message_Template) { |
|
575 | + continue; |
|
576 | + } |
|
577 | + $templates[ $template_field ][ $context ] = $template_obj->get('MTP_content'); |
|
578 | + } |
|
579 | + } |
|
580 | + return $templates; |
|
581 | + } |
|
582 | + |
|
583 | + |
|
584 | + /** |
|
585 | + * Assembles new fully generated EE_Message objects and adds to _ready_queue |
|
586 | + * |
|
587 | + * @param array $addressees Array of EE_Messages_Addressee objects indexed by message type |
|
588 | + * context. |
|
589 | + * @param array $templates formatted array of templates used for parsing data. |
|
590 | + * @param EE_Message_Template_Group $message_template_group |
|
591 | + * @return bool true if message generation went a-ok. false if some sort of exception occurred. Note: The |
|
592 | + * method will attempt to generate ALL EE_Message objects and add to |
|
593 | + * the _ready_queue. Successfully generated messages get added to the |
|
594 | + * queue with EEM_Message::status_idle, unsuccessfully generated |
|
595 | + * messages will get added to the queue as EEM_Message::status_failed. |
|
596 | + * Very rarely should "false" be returned from this method. |
|
597 | + * @throws EE_Error |
|
598 | + */ |
|
599 | + protected function _assemble_messages($addressees, $templates, EE_Message_Template_Group $message_template_group) |
|
600 | + { |
|
601 | + |
|
602 | + // if templates are empty then get out because we can't generate anything. |
|
603 | + if (! $templates) { |
|
604 | + $this->_error_msg[] = esc_html__( |
|
605 | + 'Unable to assemble messages because there are no templates retrieved for generating the messages with', |
|
606 | + 'event_espresso' |
|
607 | + ); |
|
608 | + return false; |
|
609 | + } |
|
610 | + |
|
611 | + // We use this as the counter for generated messages because don't forget we may be executing this inside of a |
|
612 | + // generation_queue. So _ready_queue may have generated EE_Message objects already. |
|
613 | + $generated_count = 0; |
|
614 | + foreach ($addressees as $context => $recipients) { |
|
615 | + foreach ($recipients as $recipient) { |
|
616 | + $message = $this->_setup_message_object($context, $recipient, $templates, $message_template_group); |
|
617 | + if ($message instanceof EE_Message) { |
|
618 | + $this->_ready_queue->add( |
|
619 | + $message, |
|
620 | + array(), |
|
621 | + $this->_generation_queue->get_message_repository()->is_preview(), |
|
622 | + $this->_generation_queue->get_message_repository()->is_test_send() |
|
623 | + ); |
|
624 | + $generated_count++; |
|
625 | + } |
|
626 | + |
|
627 | + // if the current MSG being generated is for a test send then we'll only use ONE message in the |
|
628 | + // generation. |
|
629 | + if ($this->_generation_queue->get_message_repository()->is_test_send()) { |
|
630 | + break 2; |
|
631 | + } |
|
632 | + } |
|
633 | + } |
|
634 | + |
|
635 | + // if there are no generated messages then something else fatal went wrong. |
|
636 | + return $generated_count > 0; |
|
637 | + } |
|
638 | + |
|
639 | + |
|
640 | + /** |
|
641 | + * @param string $context The context for the generated message. |
|
642 | + * @param EE_Messages_Addressee $recipient |
|
643 | + * @param array $templates formatted array of templates used for parsing data. |
|
644 | + * @param EE_Message_Template_Group $message_template_group |
|
645 | + * @return bool|EE_Message |
|
646 | + * @throws EE_Error |
|
647 | + */ |
|
648 | + protected function _setup_message_object( |
|
649 | + $context, |
|
650 | + EE_Messages_Addressee $recipient, |
|
651 | + $templates, |
|
652 | + EE_Message_Template_Group $message_template_group |
|
653 | + ) { |
|
654 | + // stuff we already know |
|
655 | + $transaction_id = $recipient->txn instanceof EE_Transaction ? $recipient->txn->ID() : 0; |
|
656 | + $transaction_id = empty($transaction_id) && $this->_current_data_handler->txn instanceof EE_Transaction |
|
657 | + ? $this->_current_data_handler->txn->ID() |
|
658 | + : $transaction_id; |
|
659 | + $message_fields = array( |
|
660 | + 'GRP_ID' => $message_template_group->ID(), |
|
661 | + 'TXN_ID' => $transaction_id, |
|
662 | + 'MSG_messenger' => $this->_current_messenger->name, |
|
663 | + 'MSG_message_type' => $this->_current_message_type->name, |
|
664 | + 'MSG_context' => $context, |
|
665 | + ); |
|
666 | + |
|
667 | + // recipient id and type should be on the EE_Messages_Addressee object but if this is empty, let's try to grab |
|
668 | + // the info from the att_obj found in the EE_Messages_Addressee object. |
|
669 | + if (empty($recipient->recipient_id) || empty($recipient->recipient_type)) { |
|
670 | + $message_fields['MSG_recipient_ID'] = $recipient->att_obj instanceof EE_Attendee |
|
671 | + ? $recipient->att_obj->ID() |
|
672 | + : 0; |
|
673 | + $message_fields['MSG_recipient_type'] = 'Attendee'; |
|
674 | + } else { |
|
675 | + $message_fields['MSG_recipient_ID'] = $recipient->recipient_id; |
|
676 | + $message_fields['MSG_recipient_type'] = $recipient->recipient_type; |
|
677 | + } |
|
678 | + $message = EE_Message_Factory::create($message_fields); |
|
679 | + |
|
680 | + // grab valid shortcodes for shortcode parser |
|
681 | + $mt_shortcodes = $this->_current_message_type->get_valid_shortcodes(); |
|
682 | + $m_shortcodes = $this->_current_messenger->get_valid_shortcodes(); |
|
683 | + |
|
684 | + // if the 'to' field is empty or the context is inactive we skip EXCEPT if we're previewing |
|
685 | + if (( |
|
686 | + ( |
|
687 | + empty($templates['to'][ $context ]) |
|
688 | + && ! $this->_current_messenger->allow_empty_to_field() |
|
689 | + ) |
|
690 | + || ! $message_template_group->is_context_active($context) |
|
691 | + ) |
|
692 | + && ! $this->_generation_queue->get_message_repository()->is_preview() |
|
693 | + ) { |
|
694 | + // we silently exit here and do NOT record a fail because the message is "turned off" by having no "to" |
|
695 | + // field. |
|
696 | + return false; |
|
697 | + } |
|
698 | + $error_msg = array(); |
|
699 | + foreach ($templates as $field => $field_context) { |
|
700 | + $error_msg = array(); |
|
701 | + // let's setup the valid shortcodes for the incoming context. |
|
702 | + $valid_shortcodes = $mt_shortcodes[ $context ]; |
|
703 | + // merge in valid shortcodes for the field. |
|
704 | + $shortcodes = isset($m_shortcodes[ $field ]) ? $m_shortcodes[ $field ] : $valid_shortcodes; |
|
705 | + if (isset($templates[ $field ][ $context ])) { |
|
706 | + // prefix field. |
|
707 | + $column_name = 'MSG_' . $field; |
|
708 | + try { |
|
709 | + $content = $this->_shortcode_parser->parse_message_template( |
|
710 | + $templates[ $field ][ $context ], |
|
711 | + $recipient, |
|
712 | + $shortcodes, |
|
713 | + $this->_current_message_type, |
|
714 | + $this->_current_messenger, |
|
715 | + $message |
|
716 | + ); |
|
717 | + // the model field removes slashes when setting (usually necessary when the input is from the |
|
718 | + // request) but this value is from another model and has no slashes. So add them so it matchces |
|
719 | + // what the field expected (otherwise slashes will have been stripped from this an extra time) |
|
720 | + $message->set_field_or_extra_meta($column_name, addslashes($content)); |
|
721 | + } catch (EE_Error $e) { |
|
722 | + $error_msg[] = sprintf( |
|
723 | + esc_html__( |
|
724 | + 'There was a problem generating the content for the field %s: %s', |
|
725 | + 'event_espresso' |
|
726 | + ), |
|
727 | + $field, |
|
728 | + $e->getMessage() |
|
729 | + ); |
|
730 | + $message->set_STS_ID(EEM_Message::status_failed); |
|
731 | + } |
|
732 | + } |
|
733 | + } |
|
734 | + |
|
735 | + if ($message->STS_ID() === EEM_Message::status_failed) { |
|
736 | + $error_msg = esc_html__('There were problems generating this message:', 'event_espresso') |
|
737 | + . "\n" |
|
738 | + . implode("\n", $error_msg); |
|
739 | + $message->set_error_message($error_msg); |
|
740 | + } else { |
|
741 | + $message->set_STS_ID(EEM_Message::status_idle); |
|
742 | + } |
|
743 | + return $message; |
|
744 | + } |
|
745 | + |
|
746 | + |
|
747 | + /** |
|
748 | + * This verifies that the incoming array has a EE_messenger object and a EE_message_type object and sets appropriate |
|
749 | + * error message if either is missing. |
|
750 | + * |
|
751 | + * @return bool true means there were no errors, false means there were errors. |
|
752 | + * @throws EE_Error |
|
753 | + * @throws ReflectionException |
|
754 | + */ |
|
755 | + protected function _verify() |
|
756 | + { |
|
757 | + // reset error message to an empty array. |
|
758 | + $this->_error_msg = array(); |
|
759 | + $valid = true; |
|
760 | + $valid = $valid ? $this->_validate_messenger_and_message_type() : $valid; |
|
761 | + $valid = $valid ? $this->_validate_and_setup_data() : $valid; |
|
762 | + |
|
763 | + // set the verified flag so we know everything has been validated. |
|
764 | + $this->_verified = $valid; |
|
765 | + |
|
766 | + return $valid; |
|
767 | + } |
|
768 | + |
|
769 | + |
|
770 | + /** |
|
771 | + * This accepts an array and validates that it is an array indexed by context with each value being an array of |
|
772 | + * EE_Messages_Addressee objects. |
|
773 | + * |
|
774 | + * @param array $addressees Keys correspond to contexts for the message type and values are EE_Messages_Addressee[] |
|
775 | + * @return bool |
|
776 | + */ |
|
777 | + protected function _valid_addressees($addressees) |
|
778 | + { |
|
779 | + if (! $addressees || ! is_array($addressees)) { |
|
780 | + return false; |
|
781 | + } |
|
782 | + |
|
783 | + foreach ($addressees as $addressee_array) { |
|
784 | + foreach ($addressee_array as $addressee) { |
|
785 | + if (! $addressee instanceof EE_Messages_Addressee) { |
|
786 | + return false; |
|
787 | + } |
|
788 | + } |
|
789 | + } |
|
790 | + return true; |
|
791 | + } |
|
792 | + |
|
793 | + |
|
794 | + /** |
|
795 | + * This validates the messenger, message type, and presences of generation data for the current EE_Message in the |
|
796 | + * queue. This process sets error messages if something is wrong. |
|
797 | + * |
|
798 | + * @return bool true is if there are no errors. false is if there is. |
|
799 | + */ |
|
800 | + protected function _validate_messenger_and_message_type() |
|
801 | + { |
|
802 | + |
|
803 | + // first are there any existing error messages? If so then return. |
|
804 | + if ($this->_error_msg) { |
|
805 | + return false; |
|
806 | + } |
|
807 | + /** @type EE_Message $message */ |
|
808 | + $message = $this->_generation_queue->get_message_repository()->current(); |
|
809 | + try { |
|
810 | + $this->_current_messenger = $message->valid_messenger(true) |
|
811 | + ? $message->messenger_object() |
|
812 | + : null; |
|
813 | + } catch (Exception $e) { |
|
814 | + $this->_error_msg[] = $e->getMessage(); |
|
815 | + } |
|
816 | + try { |
|
817 | + $this->_current_message_type = $message->valid_message_type(true) |
|
818 | + ? $message->message_type_object() |
|
819 | + : null; |
|
820 | + } catch (Exception $e) { |
|
821 | + $this->_error_msg[] = $e->getMessage(); |
|
822 | + } |
|
823 | + |
|
824 | + /** |
|
825 | + * Check if there is any generation data, but only if this is not for a preview. |
|
826 | + */ |
|
827 | + if (! $this->_generation_queue->get_message_repository()->get_generation_data() |
|
828 | + && ( |
|
829 | + ! $this->_generation_queue->get_message_repository()->is_preview() |
|
830 | + && $this->_generation_queue->get_message_repository()->get_data_handler() |
|
831 | + !== 'EE_Messages_Preview_incoming_data' |
|
832 | + ) |
|
833 | + ) { |
|
834 | + $this->_error_msg[] = esc_html__( |
|
835 | + 'There is no generation data for this message. Unable to generate.', |
|
836 | + 'event_espresso' |
|
837 | + ); |
|
838 | + } |
|
839 | + |
|
840 | + return empty($this->_error_msg); |
|
841 | + } |
|
842 | + |
|
843 | + |
|
844 | + /** |
|
845 | + * This method retrieves the expected data handler for the message type and validates the generation data for that |
|
846 | + * data handler. |
|
847 | + * |
|
848 | + * @return bool true means there are no errors. false means there were errors (and handler did not get setup). |
|
849 | + * @throws EE_Error |
|
850 | + * @throws ReflectionException |
|
851 | + */ |
|
852 | + protected function _validate_and_setup_data() |
|
853 | + { |
|
854 | + |
|
855 | + // First, are there any existing error messages? If so, return because if there were errors elsewhere this can't |
|
856 | + // be used anyways. |
|
857 | + if ($this->_error_msg) { |
|
858 | + return false; |
|
859 | + } |
|
860 | + |
|
861 | + $generation_data = $this->_generation_queue->get_message_repository()->get_generation_data(); |
|
862 | + |
|
863 | + /** @type EE_Messages_incoming_data $data_handler_class_name - well not really... just the class name actually*/ |
|
864 | + $data_handler_class_name = $this->_generation_queue->get_message_repository()->get_data_handler() |
|
865 | + ? $this->_generation_queue->get_message_repository()->get_data_handler() |
|
866 | + : 'EE_Messages_' . $this->_current_message_type->get_data_handler($generation_data) . '_incoming_data'; |
|
867 | + |
|
868 | + // If this EE_Message is for a preview, then let's switch out to the preview data handler. |
|
869 | + if ($this->_generation_queue->get_message_repository()->is_preview()) { |
|
870 | + $data_handler_class_name = 'EE_Messages_Preview_incoming_data'; |
|
871 | + } |
|
872 | + |
|
873 | + // First get the class name for the data handler (and also verifies it exists. |
|
874 | + if (! class_exists($data_handler_class_name)) { |
|
875 | + $this->_error_msg[] = sprintf( |
|
876 | + esc_html__( |
|
877 | + 'The included data handler class name does not match any valid, accessible, "%1$s" classes. Looking for %2$s.', |
|
878 | + 'event_espresso' |
|
879 | + ), |
|
880 | + 'EE_Messages_incoming_data', |
|
881 | + $data_handler_class_name |
|
882 | + ); |
|
883 | + return false; |
|
884 | + } |
|
885 | + |
|
886 | + // convert generation_data for data_handler_instantiation. |
|
887 | + $generation_data = $data_handler_class_name::convert_data_from_persistent_storage($generation_data); |
|
888 | + |
|
889 | + // note, this may set error messages as well. |
|
890 | + $this->_set_data_handler($generation_data, $data_handler_class_name); |
|
891 | + |
|
892 | + return empty($this->_error_msg); |
|
893 | + } |
|
894 | + |
|
895 | + |
|
896 | + /** |
|
897 | + * Sets the $_current_data_handler property that is used for generating the current EE_Message in the queue, and |
|
898 | + * adds it to the _data repository. |
|
899 | + * |
|
900 | + * @param mixed $generating_data This is data expected by the instantiated data handler. |
|
901 | + * @param string $data_handler_class_name This is the reference string indicating what data handler is being |
|
902 | + * instantiated. |
|
903 | + * @return void . |
|
904 | + * @throws EE_Error |
|
905 | + * @throws ReflectionException |
|
906 | + */ |
|
907 | + protected function _set_data_handler($generating_data, $data_handler_class_name) |
|
908 | + { |
|
909 | + // valid classname for the data handler. Now let's setup the key for the data handler repository to see if there |
|
910 | + // is already a ready data handler in the repository. |
|
911 | + $this->_current_data_handler = $this->_data_handler_collection->get_by_key( |
|
912 | + $this->_data_handler_collection->get_key( |
|
913 | + $data_handler_class_name, |
|
914 | + $generating_data |
|
915 | + ) |
|
916 | + ); |
|
917 | + if (! $this->_current_data_handler instanceof EE_Messages_incoming_data) { |
|
918 | + // no saved data_handler in the repo so let's set one up and add it to the repo. |
|
919 | + try { |
|
920 | + $this->_current_data_handler = new $data_handler_class_name($generating_data); |
|
921 | + $this->_data_handler_collection->add($this->_current_data_handler, $generating_data); |
|
922 | + } catch (EE_Error $e) { |
|
923 | + $this->_error_msg[] = $e->get_error(); |
|
924 | + } |
|
925 | + } |
|
926 | + } |
|
927 | + |
|
928 | + |
|
929 | + /** |
|
930 | + * The queued EE_Message for generation does not save the data used for generation as objects |
|
931 | + * because serialization of those objects could be problematic if the data is saved to the db. |
|
932 | + * So this method calls the static method on the associated data_handler for the given message_type |
|
933 | + * and that preps the data for later instantiation when generating. |
|
934 | + * |
|
935 | + * @param EE_Message_To_Generate $message_to_generate |
|
936 | + * @param bool $preview Indicate whether this is being used for a preview or not. |
|
937 | + * @return mixed Prepped data for persisting to the queue. false is returned if unable to prep data. |
|
938 | + */ |
|
939 | + protected function _prepare_data_for_queue(EE_Message_To_Generate $message_to_generate, $preview) |
|
940 | + { |
|
941 | + /** @type EE_Messages_incoming_data $data_handler - well not really... just the class name actually */ |
|
942 | + $data_handler = $message_to_generate->get_data_handler_class_name($preview); |
|
943 | + if (! $message_to_generate->valid()) { |
|
944 | + return false; // unable to get the data because the info in the EE_Message_To_Generate class is invalid. |
|
945 | + } |
|
946 | + return $data_handler::convert_data_for_persistent_storage($message_to_generate->data()); |
|
947 | + } |
|
948 | + |
|
949 | + |
|
950 | + /** |
|
951 | + * This sets up a EEM_Message::status_incomplete EE_Message object and adds it to the generation queue. |
|
952 | + * |
|
953 | + * @param EE_Message_To_Generate $message_to_generate |
|
954 | + * @param bool $test_send Whether this is just a test send or not. Typically used for previews. |
|
955 | + */ |
|
956 | + public function create_and_add_message_to_queue(EE_Message_To_Generate $message_to_generate, $test_send = false) |
|
957 | + { |
|
958 | + // prep data |
|
959 | + $data = $this->_prepare_data_for_queue($message_to_generate, $message_to_generate->preview()); |
|
960 | + |
|
961 | + $message = $message_to_generate->get_EE_Message(); |
|
962 | + |
|
963 | + $GRP_ID = EE_Registry::instance()->REQ->get('GRP_ID', 0) > 0 |
|
964 | + ? EE_Registry::instance()->REQ->get('GRP_ID') |
|
965 | + : apply_filters( |
|
966 | + 'FHEE__EE_Messages_Generator__create_and_add_message_to_queue_GRP_ID', |
|
967 | + 0, |
|
968 | + $message, |
|
969 | + $message_to_generate, |
|
970 | + $test_send |
|
971 | + ); |
|
972 | + |
|
973 | + if ($GRP_ID > 0) { |
|
974 | + $message->set_GRP_ID($GRP_ID); |
|
975 | + } |
|
976 | + |
|
977 | + if ($data === false) { |
|
978 | + $message->set_STS_ID(EEM_Message::status_failed); |
|
979 | + $message->set_error_message( |
|
980 | + esc_html__( |
|
981 | + 'Unable to prepare data for persistence to the database.', |
|
982 | + 'event_espresso' |
|
983 | + ) |
|
984 | + ); |
|
985 | + } else { |
|
986 | + // make sure that the data handler is cached on the message as well |
|
987 | + $data['data_handler_class_name'] = $message_to_generate->get_data_handler_class_name(); |
|
988 | + } |
|
989 | + |
|
990 | + $this->_generation_queue->add($message, $data, $message_to_generate->preview(), $test_send); |
|
991 | + } |
|
992 | 992 | } |