@@ -15,699 +15,699 @@ |
||
15 | 15 | { |
16 | 16 | |
17 | 17 | |
18 | - /** |
|
19 | - * @type string reference for sending action |
|
20 | - */ |
|
21 | - const action_sending = 'sending'; |
|
22 | - |
|
23 | - /** |
|
24 | - * @type string reference for generation action |
|
25 | - */ |
|
26 | - const action_generating = 'generation'; |
|
27 | - |
|
28 | - |
|
29 | - /** |
|
30 | - * @type EE_Message_Repository $_message_repository |
|
31 | - */ |
|
32 | - protected $_message_repository; |
|
33 | - |
|
34 | - /** |
|
35 | - * Sets the limit of how many messages are generated per process. |
|
36 | - * |
|
37 | - * @type int |
|
38 | - */ |
|
39 | - protected $_batch_count; |
|
40 | - |
|
41 | - |
|
42 | - /** |
|
43 | - * This is an array of cached queue items being stored in this object. |
|
44 | - * The array keys will be the ID of the EE_Message in the db if saved. If the EE_Message |
|
45 | - * is not saved to the db then its key will be an increment of "UNS" (i.e. UNS1, UNS2 etc.) |
|
46 | - * |
|
47 | - * @type EE_Message[] |
|
48 | - */ |
|
49 | - protected $_cached_queue_items; |
|
50 | - |
|
51 | - /** |
|
52 | - * Tracks the number of unsaved queue items. |
|
53 | - * |
|
54 | - * @type int |
|
55 | - */ |
|
56 | - protected $_unsaved_count = 0; |
|
57 | - |
|
58 | - /** |
|
59 | - * used to record if a do_messenger_hooks has already been called for a message type. This prevents multiple |
|
60 | - * hooks getting fired if users have setup their action/filter hooks to prevent duplicate calls. |
|
61 | - * |
|
62 | - * @type array |
|
63 | - */ |
|
64 | - protected $_did_hook = array(); |
|
65 | - |
|
66 | - |
|
67 | - /** |
|
68 | - * Constructor. |
|
69 | - * Setup all the initial properties and load a EE_Message_Repository. |
|
70 | - * |
|
71 | - * @param \EE_Message_Repository $message_repository |
|
72 | - */ |
|
73 | - public function __construct(EE_Message_Repository $message_repository) |
|
74 | - { |
|
75 | - $this->_batch_count = apply_filters('FHEE__EE_Messages_Queue___batch_count', 50); |
|
76 | - $this->_message_repository = $message_repository; |
|
77 | - } |
|
78 | - |
|
79 | - |
|
80 | - /** |
|
81 | - * Add a EE_Message object to the queue |
|
82 | - * |
|
83 | - * @param EE_Message $message |
|
84 | - * @param array $data This will be an array of data to attach to the object in the repository. If the |
|
85 | - * object is persisted, this data will be saved on an extra_meta object related to |
|
86 | - * EE_Message. |
|
87 | - * @param bool $preview Whether this EE_Message represents a preview or not. |
|
88 | - * @param bool $test_send This indicates whether to do a test send instead of actual send. A test send will |
|
89 | - * use the messenger send method but typically is based on preview data. |
|
90 | - * @return bool Whether the message was successfully added to the repository or not. |
|
91 | - */ |
|
92 | - public function add(EE_Message $message, $data = array(), $preview = false, $test_send = false) |
|
93 | - { |
|
94 | - $data['preview'] = $preview; |
|
95 | - $data['test_send'] = $test_send; |
|
96 | - return $this->_message_repository->add($message, $data); |
|
97 | - } |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * Removes EE_Message from _queue that matches the given EE_Message if the pointer is on a matching EE_Message |
|
102 | - * |
|
103 | - * @param EE_Message $message The message to detach from the queue |
|
104 | - * @param bool $persist This flag indicates whether to attempt to delete the object from the db as well. |
|
105 | - * @return bool |
|
106 | - */ |
|
107 | - public function remove(EE_Message $message, $persist = false) |
|
108 | - { |
|
109 | - if ($persist && $this->_message_repository->current() !== $message) { |
|
110 | - // get pointer on right message |
|
111 | - if ($this->_message_repository->has($message)) { |
|
112 | - $this->_message_repository->rewind(); |
|
113 | - while ($this->_message_repository->valid()) { |
|
114 | - if ($this->_message_repository->current() === $message) { |
|
115 | - break; |
|
116 | - } |
|
117 | - $this->_message_repository->next(); |
|
118 | - } |
|
119 | - } else { |
|
120 | - return false; |
|
121 | - } |
|
122 | - } |
|
123 | - return $persist ? $this->_message_repository->delete() : $this->_message_repository->remove($message); |
|
124 | - } |
|
125 | - |
|
126 | - |
|
127 | - /** |
|
128 | - * Persists all queued EE_Message objects to the db. |
|
129 | - * |
|
130 | - * @param bool $do_hooks_only @see EE_Message_Repository::saveAll |
|
131 | - * @return array @see EE_Messages_Repository::saveAll() for return values. |
|
132 | - */ |
|
133 | - public function save($do_hooks_only = false) |
|
134 | - { |
|
135 | - return $this->_message_repository->saveAll($do_hooks_only); |
|
136 | - } |
|
137 | - |
|
138 | - |
|
139 | - /** |
|
140 | - * @return EE_Message_Repository |
|
141 | - */ |
|
142 | - public function get_message_repository() |
|
143 | - { |
|
144 | - return $this->_message_repository; |
|
145 | - } |
|
146 | - |
|
147 | - |
|
148 | - /** |
|
149 | - * This does the following things: |
|
150 | - * 1. Checks if there is a lock on generation (prevents race conditions). If there is a lock then exits (return |
|
151 | - * false). |
|
152 | - * 2. If no lock, sets lock, then retrieves a batch of non-generated EE_Message objects and adds to queue |
|
153 | - * 3. Returns bool. True = batch ready. False = no batch ready (or nothing available for generation). |
|
154 | - * Note: Callers should make sure they release the lock otherwise batch generation will be prevented from |
|
155 | - * continuing. The lock is on a transient that is set to expire after one hour as a fallback in case locks are not |
|
156 | - * removed. |
|
157 | - * |
|
158 | - * @return bool true if successfully retrieved batch, false no batch ready. |
|
159 | - */ |
|
160 | - public function get_batch_to_generate() |
|
161 | - { |
|
162 | - if ($this->is_locked(EE_Messages_Queue::action_generating)) { |
|
163 | - return false; |
|
164 | - } |
|
165 | - |
|
166 | - // lock batch generation to prevent race conditions. |
|
167 | - $this->lock_queue(EE_Messages_Queue::action_generating); |
|
168 | - |
|
169 | - $query_args = array( |
|
170 | - // key 0 = where conditions |
|
171 | - 0 => array('STS_ID' => EEM_Message::status_incomplete), |
|
172 | - 'order_by' => $this->_get_priority_orderby(), |
|
173 | - 'limit' => $this->_batch_count, |
|
174 | - ); |
|
175 | - $messages = EEM_Message::instance()->get_all($query_args); |
|
176 | - |
|
177 | - if (! $messages) { |
|
178 | - return false; // nothing to generate |
|
179 | - } |
|
180 | - |
|
181 | - foreach ($messages as $message) { |
|
182 | - if ($message instanceof EE_Message) { |
|
183 | - $data = $message->all_extra_meta_array(); |
|
184 | - $this->add($message, $data); |
|
185 | - } |
|
186 | - } |
|
187 | - return true; |
|
188 | - } |
|
189 | - |
|
190 | - |
|
191 | - /** |
|
192 | - * This does the following things: |
|
193 | - * 1. Checks if there is a lock on sending (prevents race conditions). If there is a lock then exits (return |
|
194 | - * false). |
|
195 | - * 2. Grabs the allowed number of messages to send for the rate_limit. If cannot send any more messages, then |
|
196 | - * return false. |
|
197 | - * 2. If no lock, sets lock, then retrieves a batch of EE_Message objects, adds to queue and triggers execution. |
|
198 | - * 3. On success or unsuccessful send, sets status appropriately. |
|
199 | - * 4. Saves messages via the queue |
|
200 | - * 5. Releases lock. |
|
201 | - * |
|
202 | - * @return bool true on success, false if something preventing sending (i.e. lock set). Note: true does not |
|
203 | - * necessarily mean that all messages were successfully sent. It just means that this method |
|
204 | - * successfully completed. On true, client may want to call $this->count_STS_in_queue( |
|
205 | - * EEM_Message::status_failed ) to see if any failed EE_Message objects. Each failed message object |
|
206 | - * will also have a saved error message on it to assist with notifying user. |
|
207 | - */ |
|
208 | - public function get_to_send_batch_and_send() |
|
209 | - { |
|
210 | - $rate_limit = $this->get_rate_limit(); |
|
211 | - if ( |
|
212 | - $rate_limit < 1 |
|
213 | - || $this->is_locked(EE_Messages_Queue::action_sending) |
|
214 | - ) { |
|
215 | - return false; |
|
216 | - } |
|
217 | - |
|
218 | - $this->lock_queue(EE_Messages_Queue::action_sending); |
|
219 | - |
|
220 | - $batch = $this->_batch_count < $rate_limit ? $this->_batch_count : $rate_limit; |
|
221 | - |
|
222 | - $query_args = array( |
|
223 | - // key 0 = where conditions |
|
224 | - 0 => array('STS_ID' => array('IN', EEM_Message::instance()->stati_indicating_to_send())), |
|
225 | - 'order_by' => $this->_get_priority_orderby(), |
|
226 | - 'limit' => $batch, |
|
227 | - ); |
|
228 | - |
|
229 | - $messages_to_send = EEM_Message::instance()->get_all($query_args); |
|
230 | - |
|
231 | - |
|
232 | - // any to send? |
|
233 | - if (! $messages_to_send) { |
|
234 | - $this->unlock_queue(EE_Messages_Queue::action_sending); |
|
235 | - return false; |
|
236 | - } |
|
237 | - |
|
238 | - $queue_count = 0; |
|
239 | - |
|
240 | - // add to queue. |
|
241 | - foreach ($messages_to_send as $message) { |
|
242 | - if ($message instanceof EE_Message) { |
|
243 | - $queue_count++; |
|
244 | - $this->add($message); |
|
245 | - } |
|
246 | - } |
|
247 | - |
|
248 | - // send messages (this also updates the rate limit) |
|
249 | - $this->execute(); |
|
250 | - |
|
251 | - // release lock |
|
252 | - $this->unlock_queue(EE_Messages_Queue::action_sending); |
|
253 | - // update rate limit |
|
254 | - $this->set_rate_limit($queue_count); |
|
255 | - return true; |
|
256 | - } |
|
257 | - |
|
258 | - |
|
259 | - /** |
|
260 | - * Locks the queue so that no other queues can call the "batch" methods. |
|
261 | - * |
|
262 | - * @param string $type The type of queue being locked. |
|
263 | - */ |
|
264 | - public function lock_queue($type = EE_Messages_Queue::action_generating) |
|
265 | - { |
|
266 | - update_option($this->_get_lock_key($type), $this->_get_lock_expiry($type)); |
|
267 | - } |
|
268 | - |
|
269 | - |
|
270 | - /** |
|
271 | - * Unlocks the queue so that batch methods can be used. |
|
272 | - * |
|
273 | - * @param string $type The type of queue being unlocked. |
|
274 | - */ |
|
275 | - public function unlock_queue($type = EE_Messages_Queue::action_generating) |
|
276 | - { |
|
277 | - delete_option($this->_get_lock_key($type)); |
|
278 | - } |
|
279 | - |
|
280 | - |
|
281 | - /** |
|
282 | - * Retrieve the key used for the lock transient. |
|
283 | - * |
|
284 | - * @param string $type The type of lock. |
|
285 | - * @return string |
|
286 | - */ |
|
287 | - protected function _get_lock_key($type = EE_Messages_Queue::action_generating) |
|
288 | - { |
|
289 | - return '_ee_lock_' . $type; |
|
290 | - } |
|
291 | - |
|
292 | - |
|
293 | - /** |
|
294 | - * Retrieve the expiry time for the lock transient. |
|
295 | - * |
|
296 | - * @param string $type The type of lock |
|
297 | - * @return int time to expiry in seconds. |
|
298 | - */ |
|
299 | - protected function _get_lock_expiry($type = EE_Messages_Queue::action_generating) |
|
300 | - { |
|
301 | - return time() + (int) apply_filters('FHEE__EE_Messages_Queue__lock_expiry', HOUR_IN_SECONDS, $type); |
|
302 | - } |
|
303 | - |
|
304 | - |
|
305 | - /** |
|
306 | - * Returns the key used for rate limit transient. |
|
307 | - * |
|
308 | - * @return string |
|
309 | - */ |
|
310 | - protected function _get_rate_limit_key() |
|
311 | - { |
|
312 | - return '_ee_rate_limit'; |
|
313 | - } |
|
314 | - |
|
315 | - |
|
316 | - /** |
|
317 | - * Returns the rate limit expiry time. |
|
318 | - * |
|
319 | - * @return int |
|
320 | - */ |
|
321 | - protected function _get_rate_limit_expiry() |
|
322 | - { |
|
323 | - return time() + (int) apply_filters('FHEE__EE_Messages_Queue__rate_limit_expiry', HOUR_IN_SECONDS); |
|
324 | - } |
|
325 | - |
|
326 | - |
|
327 | - /** |
|
328 | - * Returns the default rate limit for sending messages. |
|
329 | - * |
|
330 | - * @return int |
|
331 | - */ |
|
332 | - protected function _default_rate_limit() |
|
333 | - { |
|
334 | - return (int) apply_filters('FHEE__EE_Messages_Queue___rate_limit', 200); |
|
335 | - } |
|
336 | - |
|
337 | - |
|
338 | - /** |
|
339 | - * Return the orderby array for priority. |
|
340 | - * |
|
341 | - * @return array |
|
342 | - */ |
|
343 | - protected function _get_priority_orderby() |
|
344 | - { |
|
345 | - return array( |
|
346 | - 'MSG_priority' => 'ASC', |
|
347 | - 'MSG_modified' => 'DESC', |
|
348 | - ); |
|
349 | - } |
|
350 | - |
|
351 | - |
|
352 | - /** |
|
353 | - * Returns whether batch methods are "locked" or not, and if models an currently be used to query the database. |
|
354 | - * Return true when batch methods should not be used; returns false when they can be. |
|
355 | - * |
|
356 | - * @param string $type The type of lock being checked for. |
|
357 | - * @return bool |
|
358 | - */ |
|
359 | - public function is_locked($type = EE_Messages_Queue::action_generating) |
|
360 | - { |
|
361 | - if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
362 | - return true; |
|
363 | - } |
|
364 | - $lock = (int) get_option($this->_get_lock_key($type), 0); |
|
365 | - /** |
|
366 | - * This filters the default is_locked behaviour. |
|
367 | - */ |
|
368 | - $is_locked = filter_var( |
|
369 | - apply_filters( |
|
370 | - 'FHEE__EE_Messages_Queue__is_locked', |
|
371 | - $lock > time(), |
|
372 | - $this |
|
373 | - ), |
|
374 | - FILTER_VALIDATE_BOOLEAN |
|
375 | - ); |
|
376 | - |
|
377 | - /** |
|
378 | - * @see usage of this filter in EE_Messages_Queue::initiate_request_by_priority() method. |
|
379 | - * Also implemented here because messages processed on the same request should not have any locks applied. |
|
380 | - */ |
|
381 | - if ( |
|
382 | - apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false) |
|
383 | - || EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request |
|
384 | - ) { |
|
385 | - $is_locked = false; |
|
386 | - } |
|
387 | - |
|
388 | - |
|
389 | - return $is_locked; |
|
390 | - } |
|
391 | - |
|
392 | - |
|
393 | - /** |
|
394 | - * Retrieves the rate limit that may be cached as a transient. |
|
395 | - * If the rate limit is not set, then this sets the default rate limit and expiry and returns it. |
|
396 | - * |
|
397 | - * @param bool $return_expiry If true then return the expiry time not the rate_limit. |
|
398 | - * @return int |
|
399 | - */ |
|
400 | - protected function get_rate_limit($return_expiry = false) |
|
401 | - { |
|
402 | - $stored_rate_info = get_option($this->_get_rate_limit_key(), array()); |
|
403 | - $rate_limit = isset($stored_rate_info[0]) |
|
404 | - ? (int) $stored_rate_info[0] |
|
405 | - : 0; |
|
406 | - $expiry = isset($stored_rate_info[1]) |
|
407 | - ? (int) $stored_rate_info[1] |
|
408 | - : 0; |
|
409 | - // set the default for tracking? |
|
410 | - if (empty($stored_rate_info) || time() > $expiry) { |
|
411 | - $expiry = $this->_get_rate_limit_expiry(); |
|
412 | - $rate_limit = $this->_default_rate_limit(); |
|
413 | - update_option($this->_get_rate_limit_key(), array($rate_limit, $expiry)); |
|
414 | - } |
|
415 | - return $return_expiry ? $expiry : $rate_limit; |
|
416 | - } |
|
417 | - |
|
418 | - |
|
419 | - /** |
|
420 | - * This updates existing rate limit with the new limit which is the old minus the batch. |
|
421 | - * |
|
422 | - * @param int $batch_completed This sets the new rate limit based on the given batch that was completed. |
|
423 | - */ |
|
424 | - protected function set_rate_limit($batch_completed) |
|
425 | - { |
|
426 | - // first get the most up to date rate limit (in case its expired and reset) |
|
427 | - $rate_limit = $this->get_rate_limit(); |
|
428 | - $expiry = $this->get_rate_limit(true); |
|
429 | - $new_limit = $rate_limit - $batch_completed; |
|
430 | - // updating the transient option directly to avoid resetting the expiry. |
|
431 | - |
|
432 | - update_option($this->_get_rate_limit_key(), array($new_limit, $expiry)); |
|
433 | - } |
|
434 | - |
|
435 | - |
|
436 | - /** |
|
437 | - * This method checks the queue for ANY EE_Message objects with a priority matching the given priority passed in. |
|
438 | - * If that exists, then we immediately initiate a non-blocking request to do the requested action type. |
|
439 | - * Note: Keep in mind that there is the possibility that the request will not execute if there is already another |
|
440 | - * request running on a queue for the given task. |
|
441 | - * |
|
442 | - * @param string $task This indicates what type of request is going to be initiated. |
|
443 | - * @param int $priority This indicates the priority that triggers initiating the request. |
|
444 | - */ |
|
445 | - public function initiate_request_by_priority($task = 'generate', $priority = EEM_Message::priority_high) |
|
446 | - { |
|
447 | - // determine what status is matched with the priority as part of the trigger conditions. |
|
448 | - $status = $task == 'generate' |
|
449 | - ? EEM_Message::status_incomplete |
|
450 | - : EEM_Message::instance()->stati_indicating_to_send(); |
|
451 | - // always make sure we save because either this will get executed immediately on a separate request |
|
452 | - // or remains in the queue for the regularly scheduled queue batch. |
|
453 | - $this->save(); |
|
454 | - /** |
|
455 | - * This filter/option allows users to override processing of messages on separate requests and instead have everything |
|
456 | - * happen on the same request. If this is utilized remember: |
|
457 | - * - message priorities don't matter |
|
458 | - * - existing unprocessed messages in the queue will not get processed unless manually triggered. |
|
459 | - * - things will be perceived to take longer to happen for end users (i.e. registrations) because of the additional |
|
460 | - * processing happening on the same request. |
|
461 | - * - any race condition protection (locks) are removed because they don't apply when things are processed on |
|
462 | - * the same request. |
|
463 | - */ |
|
464 | - if ( |
|
465 | - apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false) |
|
466 | - || EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request |
|
467 | - ) { |
|
468 | - $messages_processor = EE_Registry::instance()->load_lib('Messages_Processor'); |
|
469 | - if ($messages_processor instanceof EE_Messages_Processor) { |
|
470 | - return $messages_processor->process_immediately_from_queue($this); |
|
471 | - } |
|
472 | - // if we get here then that means the messages processor couldn't be loaded so messages will just remain |
|
473 | - // queued for manual triggering by end user. |
|
474 | - } |
|
475 | - |
|
476 | - if ($this->_message_repository->count_by_priority_and_status($priority, $status)) { |
|
477 | - EE_Messages_Scheduler::initiate_scheduled_non_blocking_request($task); |
|
478 | - } |
|
479 | - } |
|
480 | - |
|
481 | - |
|
482 | - /** |
|
483 | - * Loops through the EE_Message objects in the _queue and calls the messenger send methods for each message. |
|
484 | - * |
|
485 | - * @param bool $save Used to indicate whether to save the message queue after sending |
|
486 | - * (default will save). |
|
487 | - * @param mixed $sending_messenger (optional) When the sending messenger is different than |
|
488 | - * what is on the EE_Message object in the queue. |
|
489 | - * For instance, showing the browser view of an email message, |
|
490 | - * or giving a pdf generated view of an html document. |
|
491 | - * This should be an instance of EE_messenger but if you call this |
|
492 | - * method |
|
493 | - * intending it to be a sending messenger but a valid one could not be |
|
494 | - * retrieved then send in an instance of EE_Error that contains the |
|
495 | - * related error message. |
|
496 | - * @param bool|int $by_priority When set, this indicates that only messages |
|
497 | - * matching the given priority should be executed. |
|
498 | - * @return int Number of messages sent. Note, 0 does not mean that no messages were processed. |
|
499 | - * Also, if the messenger is an request type messenger (or a preview), |
|
500 | - * its entirely possible that the messenger will exit before |
|
501 | - */ |
|
502 | - public function execute($save = true, $sending_messenger = null, $by_priority = false) |
|
503 | - { |
|
504 | - $messages_sent = 0; |
|
505 | - $this->_did_hook = array(); |
|
506 | - $this->_message_repository->rewind(); |
|
507 | - |
|
508 | - while ($this->_message_repository->valid()) { |
|
509 | - $error_messages = array(); |
|
510 | - /** @type EE_Message $message */ |
|
511 | - $message = $this->_message_repository->current(); |
|
512 | - // only process things that are queued for sending |
|
513 | - if (! in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_to_send())) { |
|
514 | - $this->_message_repository->next(); |
|
515 | - continue; |
|
516 | - } |
|
517 | - // if $by_priority is set and does not match then continue; |
|
518 | - if ($by_priority && $by_priority != $message->priority()) { |
|
519 | - $this->_message_repository->next(); |
|
520 | - continue; |
|
521 | - } |
|
522 | - // error checking |
|
523 | - if (! $message->valid_messenger()) { |
|
524 | - $error_messages[] = sprintf( |
|
525 | - esc_html__('The %s messenger is not active at time of sending.', 'event_espresso'), |
|
526 | - $message->messenger() |
|
527 | - ); |
|
528 | - } |
|
529 | - if (! $message->valid_message_type()) { |
|
530 | - $error_messages[] = sprintf( |
|
531 | - esc_html__('The %s message type is not active at the time of sending.', 'event_espresso'), |
|
532 | - $message->message_type() |
|
533 | - ); |
|
534 | - } |
|
535 | - // if there was supposed to be a sending messenger for this message, but it was invalid/inactive, |
|
536 | - // then it will instead be an EE_Error object, so let's check for that |
|
537 | - if ($sending_messenger instanceof EE_Error) { |
|
538 | - $error_messages[] = $sending_messenger->getMessage(); |
|
539 | - } |
|
540 | - // if there are no errors, then let's process the message |
|
541 | - if (empty($error_messages)) { |
|
542 | - if ($save) { |
|
543 | - $message->set_messenger_is_executing(); |
|
544 | - } |
|
545 | - if ($this->_process_message($message, $sending_messenger)) { |
|
546 | - $messages_sent++; |
|
547 | - } |
|
548 | - } |
|
549 | - $this->_set_error_message($message, $error_messages); |
|
550 | - // add modified time |
|
551 | - $message->set_modified(time()); |
|
552 | - // we save each message after its processed to make sure its status persists in case PHP times-out or runs |
|
553 | - // out of memory. @see https://events.codebasehq.com/projects/event-espresso/tickets/10281 |
|
554 | - if ($save) { |
|
555 | - $message->save(); |
|
556 | - } |
|
557 | - |
|
558 | - $this->_message_repository->next(); |
|
559 | - } |
|
560 | - if ($save) { |
|
561 | - $this->save(true); |
|
562 | - } |
|
563 | - return $messages_sent; |
|
564 | - } |
|
565 | - |
|
566 | - |
|
567 | - /** |
|
568 | - * _process_message |
|
569 | - * |
|
570 | - * @param EE_Message $message |
|
571 | - * @param mixed $sending_messenger (optional) |
|
572 | - * @return bool |
|
573 | - */ |
|
574 | - protected function _process_message(EE_Message $message, $sending_messenger = null) |
|
575 | - { |
|
576 | - // these *should* have been validated in the execute() method above |
|
577 | - $messenger = $message->messenger_object(); |
|
578 | - $message_type = $message->message_type_object(); |
|
579 | - // do actions for sending messenger if it differs from generating messenger and swap values. |
|
580 | - if ( |
|
581 | - $sending_messenger instanceof EE_messenger |
|
582 | - && $messenger instanceof EE_messenger |
|
583 | - && $sending_messenger->name != $messenger->name |
|
584 | - ) { |
|
585 | - $messenger->do_secondary_messenger_hooks($sending_messenger->name); |
|
586 | - $messenger = $sending_messenger; |
|
587 | - } |
|
588 | - // send using messenger, but double check objects |
|
589 | - if ($messenger instanceof EE_messenger && $message_type instanceof EE_message_type) { |
|
590 | - // set hook for message type (but only if not using another messenger to send). |
|
591 | - if (! isset($this->_did_hook[ $message_type->name ])) { |
|
592 | - $message_type->do_messenger_hooks($messenger); |
|
593 | - $this->_did_hook[ $message_type->name ] = 1; |
|
594 | - } |
|
595 | - // if preview then use preview method |
|
596 | - return $this->_message_repository->is_preview() |
|
597 | - ? $this->_do_preview($message, $messenger, $message_type, $this->_message_repository->is_test_send()) |
|
598 | - : $this->_do_send($message, $messenger, $message_type); |
|
599 | - } |
|
600 | - return false; |
|
601 | - } |
|
602 | - |
|
603 | - |
|
604 | - /** |
|
605 | - * The intention of this method is to count how many EE_Message objects |
|
606 | - * are in the queue with a given status. |
|
607 | - * Example usage: |
|
608 | - * After a caller calls the "EE_Message_Queue::execute()" method, the caller can check if there were any failed |
|
609 | - * sends by calling $queue->count_STS_in_queue( EEM_Message_Queue::status_failed ). |
|
610 | - * |
|
611 | - * @param array|string $status Stati to check for in queue |
|
612 | - * @return int Count of EE_Message's matching the given status. |
|
613 | - */ |
|
614 | - public function count_STS_in_queue($status) |
|
615 | - { |
|
616 | - $count = 0; |
|
617 | - $status = is_array($status) ? $status : array($status); |
|
618 | - $this->_message_repository->rewind(); |
|
619 | - foreach ($this->_message_repository as $message) { |
|
620 | - if (in_array($message->STS_ID(), $status)) { |
|
621 | - $count++; |
|
622 | - } |
|
623 | - } |
|
624 | - return $count; |
|
625 | - } |
|
626 | - |
|
627 | - |
|
628 | - /** |
|
629 | - * Executes the get_preview method on the provided messenger. |
|
630 | - * |
|
631 | - * @param EE_Message $message |
|
632 | - * @param EE_messenger $messenger |
|
633 | - * @param EE_message_type $message_type |
|
634 | - * @param $test_send |
|
635 | - * @return bool true means all went well, false means, not so much. |
|
636 | - */ |
|
637 | - protected function _do_preview( |
|
638 | - EE_Message $message, |
|
639 | - EE_messenger $messenger, |
|
640 | - EE_message_type $message_type, |
|
641 | - $test_send |
|
642 | - ) { |
|
643 | - if ($preview = $messenger->get_preview($message, $message_type, $test_send)) { |
|
644 | - if (! $test_send) { |
|
645 | - $message->set_content($preview); |
|
646 | - } |
|
647 | - $message->set_STS_ID(EEM_Message::status_sent); |
|
648 | - return true; |
|
649 | - } else { |
|
650 | - $message->set_STS_ID(EEM_Message::status_failed); |
|
651 | - return false; |
|
652 | - } |
|
653 | - } |
|
654 | - |
|
655 | - |
|
656 | - /** |
|
657 | - * Executes the send method on the provided messenger |
|
658 | - * EE_Messengers are expected to: |
|
659 | - * - return true if the send was successful. |
|
660 | - * - return false if the send was unsuccessful but can be tried again. |
|
661 | - * - throw an Exception if the send was unsuccessful and cannot be tried again. |
|
662 | - * |
|
663 | - * @param EE_Message $message |
|
664 | - * @param EE_messenger $messenger |
|
665 | - * @param EE_message_type $message_type |
|
666 | - * @return bool true means all went well, false means, not so much. |
|
667 | - */ |
|
668 | - protected function _do_send(EE_Message $message, EE_messenger $messenger, EE_message_type $message_type) |
|
669 | - { |
|
670 | - try { |
|
671 | - if ($messenger->send_message($message, $message_type)) { |
|
672 | - $message->set_STS_ID(EEM_Message::status_sent); |
|
673 | - return true; |
|
674 | - } else { |
|
675 | - $message->set_STS_ID(EEM_Message::status_retry); |
|
676 | - return false; |
|
677 | - } |
|
678 | - } catch (SendMessageException $e) { |
|
679 | - $message->set_STS_ID(EEM_Message::status_failed); |
|
680 | - $message->set_error_message($e->getMessage()); |
|
681 | - return false; |
|
682 | - } |
|
683 | - } |
|
684 | - |
|
685 | - |
|
686 | - /** |
|
687 | - * This sets any necessary error messages on the message object and its status to failed. |
|
688 | - * |
|
689 | - * @param EE_Message $message |
|
690 | - * @param array $error_messages the response from the messenger. |
|
691 | - */ |
|
692 | - protected function _set_error_message(EE_Message $message, $error_messages) |
|
693 | - { |
|
694 | - $error_messages = (array) $error_messages; |
|
695 | - if (in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_failed_sending())) { |
|
696 | - $notices = EE_Error::has_notices(); |
|
697 | - $error_messages[] = esc_html__( |
|
698 | - 'Messenger and Message Type were valid and active, but the messenger send method failed.', |
|
699 | - 'event_espresso' |
|
700 | - ); |
|
701 | - if ($notices === 1) { |
|
702 | - $notices = EE_Error::get_vanilla_notices(); |
|
703 | - $notices['errors'] = isset($notices['errors']) ? $notices['errors'] : array(); |
|
704 | - $error_messages[] = implode("\n", $notices['errors']); |
|
705 | - } |
|
706 | - } |
|
707 | - if (count($error_messages) > 0) { |
|
708 | - $msg = esc_html__('Message was not executed successfully.', 'event_espresso'); |
|
709 | - $msg = $msg . "\n" . implode("\n", $error_messages); |
|
710 | - $message->set_error_message($msg); |
|
711 | - } |
|
712 | - } |
|
18 | + /** |
|
19 | + * @type string reference for sending action |
|
20 | + */ |
|
21 | + const action_sending = 'sending'; |
|
22 | + |
|
23 | + /** |
|
24 | + * @type string reference for generation action |
|
25 | + */ |
|
26 | + const action_generating = 'generation'; |
|
27 | + |
|
28 | + |
|
29 | + /** |
|
30 | + * @type EE_Message_Repository $_message_repository |
|
31 | + */ |
|
32 | + protected $_message_repository; |
|
33 | + |
|
34 | + /** |
|
35 | + * Sets the limit of how many messages are generated per process. |
|
36 | + * |
|
37 | + * @type int |
|
38 | + */ |
|
39 | + protected $_batch_count; |
|
40 | + |
|
41 | + |
|
42 | + /** |
|
43 | + * This is an array of cached queue items being stored in this object. |
|
44 | + * The array keys will be the ID of the EE_Message in the db if saved. If the EE_Message |
|
45 | + * is not saved to the db then its key will be an increment of "UNS" (i.e. UNS1, UNS2 etc.) |
|
46 | + * |
|
47 | + * @type EE_Message[] |
|
48 | + */ |
|
49 | + protected $_cached_queue_items; |
|
50 | + |
|
51 | + /** |
|
52 | + * Tracks the number of unsaved queue items. |
|
53 | + * |
|
54 | + * @type int |
|
55 | + */ |
|
56 | + protected $_unsaved_count = 0; |
|
57 | + |
|
58 | + /** |
|
59 | + * used to record if a do_messenger_hooks has already been called for a message type. This prevents multiple |
|
60 | + * hooks getting fired if users have setup their action/filter hooks to prevent duplicate calls. |
|
61 | + * |
|
62 | + * @type array |
|
63 | + */ |
|
64 | + protected $_did_hook = array(); |
|
65 | + |
|
66 | + |
|
67 | + /** |
|
68 | + * Constructor. |
|
69 | + * Setup all the initial properties and load a EE_Message_Repository. |
|
70 | + * |
|
71 | + * @param \EE_Message_Repository $message_repository |
|
72 | + */ |
|
73 | + public function __construct(EE_Message_Repository $message_repository) |
|
74 | + { |
|
75 | + $this->_batch_count = apply_filters('FHEE__EE_Messages_Queue___batch_count', 50); |
|
76 | + $this->_message_repository = $message_repository; |
|
77 | + } |
|
78 | + |
|
79 | + |
|
80 | + /** |
|
81 | + * Add a EE_Message object to the queue |
|
82 | + * |
|
83 | + * @param EE_Message $message |
|
84 | + * @param array $data This will be an array of data to attach to the object in the repository. If the |
|
85 | + * object is persisted, this data will be saved on an extra_meta object related to |
|
86 | + * EE_Message. |
|
87 | + * @param bool $preview Whether this EE_Message represents a preview or not. |
|
88 | + * @param bool $test_send This indicates whether to do a test send instead of actual send. A test send will |
|
89 | + * use the messenger send method but typically is based on preview data. |
|
90 | + * @return bool Whether the message was successfully added to the repository or not. |
|
91 | + */ |
|
92 | + public function add(EE_Message $message, $data = array(), $preview = false, $test_send = false) |
|
93 | + { |
|
94 | + $data['preview'] = $preview; |
|
95 | + $data['test_send'] = $test_send; |
|
96 | + return $this->_message_repository->add($message, $data); |
|
97 | + } |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * Removes EE_Message from _queue that matches the given EE_Message if the pointer is on a matching EE_Message |
|
102 | + * |
|
103 | + * @param EE_Message $message The message to detach from the queue |
|
104 | + * @param bool $persist This flag indicates whether to attempt to delete the object from the db as well. |
|
105 | + * @return bool |
|
106 | + */ |
|
107 | + public function remove(EE_Message $message, $persist = false) |
|
108 | + { |
|
109 | + if ($persist && $this->_message_repository->current() !== $message) { |
|
110 | + // get pointer on right message |
|
111 | + if ($this->_message_repository->has($message)) { |
|
112 | + $this->_message_repository->rewind(); |
|
113 | + while ($this->_message_repository->valid()) { |
|
114 | + if ($this->_message_repository->current() === $message) { |
|
115 | + break; |
|
116 | + } |
|
117 | + $this->_message_repository->next(); |
|
118 | + } |
|
119 | + } else { |
|
120 | + return false; |
|
121 | + } |
|
122 | + } |
|
123 | + return $persist ? $this->_message_repository->delete() : $this->_message_repository->remove($message); |
|
124 | + } |
|
125 | + |
|
126 | + |
|
127 | + /** |
|
128 | + * Persists all queued EE_Message objects to the db. |
|
129 | + * |
|
130 | + * @param bool $do_hooks_only @see EE_Message_Repository::saveAll |
|
131 | + * @return array @see EE_Messages_Repository::saveAll() for return values. |
|
132 | + */ |
|
133 | + public function save($do_hooks_only = false) |
|
134 | + { |
|
135 | + return $this->_message_repository->saveAll($do_hooks_only); |
|
136 | + } |
|
137 | + |
|
138 | + |
|
139 | + /** |
|
140 | + * @return EE_Message_Repository |
|
141 | + */ |
|
142 | + public function get_message_repository() |
|
143 | + { |
|
144 | + return $this->_message_repository; |
|
145 | + } |
|
146 | + |
|
147 | + |
|
148 | + /** |
|
149 | + * This does the following things: |
|
150 | + * 1. Checks if there is a lock on generation (prevents race conditions). If there is a lock then exits (return |
|
151 | + * false). |
|
152 | + * 2. If no lock, sets lock, then retrieves a batch of non-generated EE_Message objects and adds to queue |
|
153 | + * 3. Returns bool. True = batch ready. False = no batch ready (or nothing available for generation). |
|
154 | + * Note: Callers should make sure they release the lock otherwise batch generation will be prevented from |
|
155 | + * continuing. The lock is on a transient that is set to expire after one hour as a fallback in case locks are not |
|
156 | + * removed. |
|
157 | + * |
|
158 | + * @return bool true if successfully retrieved batch, false no batch ready. |
|
159 | + */ |
|
160 | + public function get_batch_to_generate() |
|
161 | + { |
|
162 | + if ($this->is_locked(EE_Messages_Queue::action_generating)) { |
|
163 | + return false; |
|
164 | + } |
|
165 | + |
|
166 | + // lock batch generation to prevent race conditions. |
|
167 | + $this->lock_queue(EE_Messages_Queue::action_generating); |
|
168 | + |
|
169 | + $query_args = array( |
|
170 | + // key 0 = where conditions |
|
171 | + 0 => array('STS_ID' => EEM_Message::status_incomplete), |
|
172 | + 'order_by' => $this->_get_priority_orderby(), |
|
173 | + 'limit' => $this->_batch_count, |
|
174 | + ); |
|
175 | + $messages = EEM_Message::instance()->get_all($query_args); |
|
176 | + |
|
177 | + if (! $messages) { |
|
178 | + return false; // nothing to generate |
|
179 | + } |
|
180 | + |
|
181 | + foreach ($messages as $message) { |
|
182 | + if ($message instanceof EE_Message) { |
|
183 | + $data = $message->all_extra_meta_array(); |
|
184 | + $this->add($message, $data); |
|
185 | + } |
|
186 | + } |
|
187 | + return true; |
|
188 | + } |
|
189 | + |
|
190 | + |
|
191 | + /** |
|
192 | + * This does the following things: |
|
193 | + * 1. Checks if there is a lock on sending (prevents race conditions). If there is a lock then exits (return |
|
194 | + * false). |
|
195 | + * 2. Grabs the allowed number of messages to send for the rate_limit. If cannot send any more messages, then |
|
196 | + * return false. |
|
197 | + * 2. If no lock, sets lock, then retrieves a batch of EE_Message objects, adds to queue and triggers execution. |
|
198 | + * 3. On success or unsuccessful send, sets status appropriately. |
|
199 | + * 4. Saves messages via the queue |
|
200 | + * 5. Releases lock. |
|
201 | + * |
|
202 | + * @return bool true on success, false if something preventing sending (i.e. lock set). Note: true does not |
|
203 | + * necessarily mean that all messages were successfully sent. It just means that this method |
|
204 | + * successfully completed. On true, client may want to call $this->count_STS_in_queue( |
|
205 | + * EEM_Message::status_failed ) to see if any failed EE_Message objects. Each failed message object |
|
206 | + * will also have a saved error message on it to assist with notifying user. |
|
207 | + */ |
|
208 | + public function get_to_send_batch_and_send() |
|
209 | + { |
|
210 | + $rate_limit = $this->get_rate_limit(); |
|
211 | + if ( |
|
212 | + $rate_limit < 1 |
|
213 | + || $this->is_locked(EE_Messages_Queue::action_sending) |
|
214 | + ) { |
|
215 | + return false; |
|
216 | + } |
|
217 | + |
|
218 | + $this->lock_queue(EE_Messages_Queue::action_sending); |
|
219 | + |
|
220 | + $batch = $this->_batch_count < $rate_limit ? $this->_batch_count : $rate_limit; |
|
221 | + |
|
222 | + $query_args = array( |
|
223 | + // key 0 = where conditions |
|
224 | + 0 => array('STS_ID' => array('IN', EEM_Message::instance()->stati_indicating_to_send())), |
|
225 | + 'order_by' => $this->_get_priority_orderby(), |
|
226 | + 'limit' => $batch, |
|
227 | + ); |
|
228 | + |
|
229 | + $messages_to_send = EEM_Message::instance()->get_all($query_args); |
|
230 | + |
|
231 | + |
|
232 | + // any to send? |
|
233 | + if (! $messages_to_send) { |
|
234 | + $this->unlock_queue(EE_Messages_Queue::action_sending); |
|
235 | + return false; |
|
236 | + } |
|
237 | + |
|
238 | + $queue_count = 0; |
|
239 | + |
|
240 | + // add to queue. |
|
241 | + foreach ($messages_to_send as $message) { |
|
242 | + if ($message instanceof EE_Message) { |
|
243 | + $queue_count++; |
|
244 | + $this->add($message); |
|
245 | + } |
|
246 | + } |
|
247 | + |
|
248 | + // send messages (this also updates the rate limit) |
|
249 | + $this->execute(); |
|
250 | + |
|
251 | + // release lock |
|
252 | + $this->unlock_queue(EE_Messages_Queue::action_sending); |
|
253 | + // update rate limit |
|
254 | + $this->set_rate_limit($queue_count); |
|
255 | + return true; |
|
256 | + } |
|
257 | + |
|
258 | + |
|
259 | + /** |
|
260 | + * Locks the queue so that no other queues can call the "batch" methods. |
|
261 | + * |
|
262 | + * @param string $type The type of queue being locked. |
|
263 | + */ |
|
264 | + public function lock_queue($type = EE_Messages_Queue::action_generating) |
|
265 | + { |
|
266 | + update_option($this->_get_lock_key($type), $this->_get_lock_expiry($type)); |
|
267 | + } |
|
268 | + |
|
269 | + |
|
270 | + /** |
|
271 | + * Unlocks the queue so that batch methods can be used. |
|
272 | + * |
|
273 | + * @param string $type The type of queue being unlocked. |
|
274 | + */ |
|
275 | + public function unlock_queue($type = EE_Messages_Queue::action_generating) |
|
276 | + { |
|
277 | + delete_option($this->_get_lock_key($type)); |
|
278 | + } |
|
279 | + |
|
280 | + |
|
281 | + /** |
|
282 | + * Retrieve the key used for the lock transient. |
|
283 | + * |
|
284 | + * @param string $type The type of lock. |
|
285 | + * @return string |
|
286 | + */ |
|
287 | + protected function _get_lock_key($type = EE_Messages_Queue::action_generating) |
|
288 | + { |
|
289 | + return '_ee_lock_' . $type; |
|
290 | + } |
|
291 | + |
|
292 | + |
|
293 | + /** |
|
294 | + * Retrieve the expiry time for the lock transient. |
|
295 | + * |
|
296 | + * @param string $type The type of lock |
|
297 | + * @return int time to expiry in seconds. |
|
298 | + */ |
|
299 | + protected function _get_lock_expiry($type = EE_Messages_Queue::action_generating) |
|
300 | + { |
|
301 | + return time() + (int) apply_filters('FHEE__EE_Messages_Queue__lock_expiry', HOUR_IN_SECONDS, $type); |
|
302 | + } |
|
303 | + |
|
304 | + |
|
305 | + /** |
|
306 | + * Returns the key used for rate limit transient. |
|
307 | + * |
|
308 | + * @return string |
|
309 | + */ |
|
310 | + protected function _get_rate_limit_key() |
|
311 | + { |
|
312 | + return '_ee_rate_limit'; |
|
313 | + } |
|
314 | + |
|
315 | + |
|
316 | + /** |
|
317 | + * Returns the rate limit expiry time. |
|
318 | + * |
|
319 | + * @return int |
|
320 | + */ |
|
321 | + protected function _get_rate_limit_expiry() |
|
322 | + { |
|
323 | + return time() + (int) apply_filters('FHEE__EE_Messages_Queue__rate_limit_expiry', HOUR_IN_SECONDS); |
|
324 | + } |
|
325 | + |
|
326 | + |
|
327 | + /** |
|
328 | + * Returns the default rate limit for sending messages. |
|
329 | + * |
|
330 | + * @return int |
|
331 | + */ |
|
332 | + protected function _default_rate_limit() |
|
333 | + { |
|
334 | + return (int) apply_filters('FHEE__EE_Messages_Queue___rate_limit', 200); |
|
335 | + } |
|
336 | + |
|
337 | + |
|
338 | + /** |
|
339 | + * Return the orderby array for priority. |
|
340 | + * |
|
341 | + * @return array |
|
342 | + */ |
|
343 | + protected function _get_priority_orderby() |
|
344 | + { |
|
345 | + return array( |
|
346 | + 'MSG_priority' => 'ASC', |
|
347 | + 'MSG_modified' => 'DESC', |
|
348 | + ); |
|
349 | + } |
|
350 | + |
|
351 | + |
|
352 | + /** |
|
353 | + * Returns whether batch methods are "locked" or not, and if models an currently be used to query the database. |
|
354 | + * Return true when batch methods should not be used; returns false when they can be. |
|
355 | + * |
|
356 | + * @param string $type The type of lock being checked for. |
|
357 | + * @return bool |
|
358 | + */ |
|
359 | + public function is_locked($type = EE_Messages_Queue::action_generating) |
|
360 | + { |
|
361 | + if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
362 | + return true; |
|
363 | + } |
|
364 | + $lock = (int) get_option($this->_get_lock_key($type), 0); |
|
365 | + /** |
|
366 | + * This filters the default is_locked behaviour. |
|
367 | + */ |
|
368 | + $is_locked = filter_var( |
|
369 | + apply_filters( |
|
370 | + 'FHEE__EE_Messages_Queue__is_locked', |
|
371 | + $lock > time(), |
|
372 | + $this |
|
373 | + ), |
|
374 | + FILTER_VALIDATE_BOOLEAN |
|
375 | + ); |
|
376 | + |
|
377 | + /** |
|
378 | + * @see usage of this filter in EE_Messages_Queue::initiate_request_by_priority() method. |
|
379 | + * Also implemented here because messages processed on the same request should not have any locks applied. |
|
380 | + */ |
|
381 | + if ( |
|
382 | + apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false) |
|
383 | + || EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request |
|
384 | + ) { |
|
385 | + $is_locked = false; |
|
386 | + } |
|
387 | + |
|
388 | + |
|
389 | + return $is_locked; |
|
390 | + } |
|
391 | + |
|
392 | + |
|
393 | + /** |
|
394 | + * Retrieves the rate limit that may be cached as a transient. |
|
395 | + * If the rate limit is not set, then this sets the default rate limit and expiry and returns it. |
|
396 | + * |
|
397 | + * @param bool $return_expiry If true then return the expiry time not the rate_limit. |
|
398 | + * @return int |
|
399 | + */ |
|
400 | + protected function get_rate_limit($return_expiry = false) |
|
401 | + { |
|
402 | + $stored_rate_info = get_option($this->_get_rate_limit_key(), array()); |
|
403 | + $rate_limit = isset($stored_rate_info[0]) |
|
404 | + ? (int) $stored_rate_info[0] |
|
405 | + : 0; |
|
406 | + $expiry = isset($stored_rate_info[1]) |
|
407 | + ? (int) $stored_rate_info[1] |
|
408 | + : 0; |
|
409 | + // set the default for tracking? |
|
410 | + if (empty($stored_rate_info) || time() > $expiry) { |
|
411 | + $expiry = $this->_get_rate_limit_expiry(); |
|
412 | + $rate_limit = $this->_default_rate_limit(); |
|
413 | + update_option($this->_get_rate_limit_key(), array($rate_limit, $expiry)); |
|
414 | + } |
|
415 | + return $return_expiry ? $expiry : $rate_limit; |
|
416 | + } |
|
417 | + |
|
418 | + |
|
419 | + /** |
|
420 | + * This updates existing rate limit with the new limit which is the old minus the batch. |
|
421 | + * |
|
422 | + * @param int $batch_completed This sets the new rate limit based on the given batch that was completed. |
|
423 | + */ |
|
424 | + protected function set_rate_limit($batch_completed) |
|
425 | + { |
|
426 | + // first get the most up to date rate limit (in case its expired and reset) |
|
427 | + $rate_limit = $this->get_rate_limit(); |
|
428 | + $expiry = $this->get_rate_limit(true); |
|
429 | + $new_limit = $rate_limit - $batch_completed; |
|
430 | + // updating the transient option directly to avoid resetting the expiry. |
|
431 | + |
|
432 | + update_option($this->_get_rate_limit_key(), array($new_limit, $expiry)); |
|
433 | + } |
|
434 | + |
|
435 | + |
|
436 | + /** |
|
437 | + * This method checks the queue for ANY EE_Message objects with a priority matching the given priority passed in. |
|
438 | + * If that exists, then we immediately initiate a non-blocking request to do the requested action type. |
|
439 | + * Note: Keep in mind that there is the possibility that the request will not execute if there is already another |
|
440 | + * request running on a queue for the given task. |
|
441 | + * |
|
442 | + * @param string $task This indicates what type of request is going to be initiated. |
|
443 | + * @param int $priority This indicates the priority that triggers initiating the request. |
|
444 | + */ |
|
445 | + public function initiate_request_by_priority($task = 'generate', $priority = EEM_Message::priority_high) |
|
446 | + { |
|
447 | + // determine what status is matched with the priority as part of the trigger conditions. |
|
448 | + $status = $task == 'generate' |
|
449 | + ? EEM_Message::status_incomplete |
|
450 | + : EEM_Message::instance()->stati_indicating_to_send(); |
|
451 | + // always make sure we save because either this will get executed immediately on a separate request |
|
452 | + // or remains in the queue for the regularly scheduled queue batch. |
|
453 | + $this->save(); |
|
454 | + /** |
|
455 | + * This filter/option allows users to override processing of messages on separate requests and instead have everything |
|
456 | + * happen on the same request. If this is utilized remember: |
|
457 | + * - message priorities don't matter |
|
458 | + * - existing unprocessed messages in the queue will not get processed unless manually triggered. |
|
459 | + * - things will be perceived to take longer to happen for end users (i.e. registrations) because of the additional |
|
460 | + * processing happening on the same request. |
|
461 | + * - any race condition protection (locks) are removed because they don't apply when things are processed on |
|
462 | + * the same request. |
|
463 | + */ |
|
464 | + if ( |
|
465 | + apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false) |
|
466 | + || EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request |
|
467 | + ) { |
|
468 | + $messages_processor = EE_Registry::instance()->load_lib('Messages_Processor'); |
|
469 | + if ($messages_processor instanceof EE_Messages_Processor) { |
|
470 | + return $messages_processor->process_immediately_from_queue($this); |
|
471 | + } |
|
472 | + // if we get here then that means the messages processor couldn't be loaded so messages will just remain |
|
473 | + // queued for manual triggering by end user. |
|
474 | + } |
|
475 | + |
|
476 | + if ($this->_message_repository->count_by_priority_and_status($priority, $status)) { |
|
477 | + EE_Messages_Scheduler::initiate_scheduled_non_blocking_request($task); |
|
478 | + } |
|
479 | + } |
|
480 | + |
|
481 | + |
|
482 | + /** |
|
483 | + * Loops through the EE_Message objects in the _queue and calls the messenger send methods for each message. |
|
484 | + * |
|
485 | + * @param bool $save Used to indicate whether to save the message queue after sending |
|
486 | + * (default will save). |
|
487 | + * @param mixed $sending_messenger (optional) When the sending messenger is different than |
|
488 | + * what is on the EE_Message object in the queue. |
|
489 | + * For instance, showing the browser view of an email message, |
|
490 | + * or giving a pdf generated view of an html document. |
|
491 | + * This should be an instance of EE_messenger but if you call this |
|
492 | + * method |
|
493 | + * intending it to be a sending messenger but a valid one could not be |
|
494 | + * retrieved then send in an instance of EE_Error that contains the |
|
495 | + * related error message. |
|
496 | + * @param bool|int $by_priority When set, this indicates that only messages |
|
497 | + * matching the given priority should be executed. |
|
498 | + * @return int Number of messages sent. Note, 0 does not mean that no messages were processed. |
|
499 | + * Also, if the messenger is an request type messenger (or a preview), |
|
500 | + * its entirely possible that the messenger will exit before |
|
501 | + */ |
|
502 | + public function execute($save = true, $sending_messenger = null, $by_priority = false) |
|
503 | + { |
|
504 | + $messages_sent = 0; |
|
505 | + $this->_did_hook = array(); |
|
506 | + $this->_message_repository->rewind(); |
|
507 | + |
|
508 | + while ($this->_message_repository->valid()) { |
|
509 | + $error_messages = array(); |
|
510 | + /** @type EE_Message $message */ |
|
511 | + $message = $this->_message_repository->current(); |
|
512 | + // only process things that are queued for sending |
|
513 | + if (! in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_to_send())) { |
|
514 | + $this->_message_repository->next(); |
|
515 | + continue; |
|
516 | + } |
|
517 | + // if $by_priority is set and does not match then continue; |
|
518 | + if ($by_priority && $by_priority != $message->priority()) { |
|
519 | + $this->_message_repository->next(); |
|
520 | + continue; |
|
521 | + } |
|
522 | + // error checking |
|
523 | + if (! $message->valid_messenger()) { |
|
524 | + $error_messages[] = sprintf( |
|
525 | + esc_html__('The %s messenger is not active at time of sending.', 'event_espresso'), |
|
526 | + $message->messenger() |
|
527 | + ); |
|
528 | + } |
|
529 | + if (! $message->valid_message_type()) { |
|
530 | + $error_messages[] = sprintf( |
|
531 | + esc_html__('The %s message type is not active at the time of sending.', 'event_espresso'), |
|
532 | + $message->message_type() |
|
533 | + ); |
|
534 | + } |
|
535 | + // if there was supposed to be a sending messenger for this message, but it was invalid/inactive, |
|
536 | + // then it will instead be an EE_Error object, so let's check for that |
|
537 | + if ($sending_messenger instanceof EE_Error) { |
|
538 | + $error_messages[] = $sending_messenger->getMessage(); |
|
539 | + } |
|
540 | + // if there are no errors, then let's process the message |
|
541 | + if (empty($error_messages)) { |
|
542 | + if ($save) { |
|
543 | + $message->set_messenger_is_executing(); |
|
544 | + } |
|
545 | + if ($this->_process_message($message, $sending_messenger)) { |
|
546 | + $messages_sent++; |
|
547 | + } |
|
548 | + } |
|
549 | + $this->_set_error_message($message, $error_messages); |
|
550 | + // add modified time |
|
551 | + $message->set_modified(time()); |
|
552 | + // we save each message after its processed to make sure its status persists in case PHP times-out or runs |
|
553 | + // out of memory. @see https://events.codebasehq.com/projects/event-espresso/tickets/10281 |
|
554 | + if ($save) { |
|
555 | + $message->save(); |
|
556 | + } |
|
557 | + |
|
558 | + $this->_message_repository->next(); |
|
559 | + } |
|
560 | + if ($save) { |
|
561 | + $this->save(true); |
|
562 | + } |
|
563 | + return $messages_sent; |
|
564 | + } |
|
565 | + |
|
566 | + |
|
567 | + /** |
|
568 | + * _process_message |
|
569 | + * |
|
570 | + * @param EE_Message $message |
|
571 | + * @param mixed $sending_messenger (optional) |
|
572 | + * @return bool |
|
573 | + */ |
|
574 | + protected function _process_message(EE_Message $message, $sending_messenger = null) |
|
575 | + { |
|
576 | + // these *should* have been validated in the execute() method above |
|
577 | + $messenger = $message->messenger_object(); |
|
578 | + $message_type = $message->message_type_object(); |
|
579 | + // do actions for sending messenger if it differs from generating messenger and swap values. |
|
580 | + if ( |
|
581 | + $sending_messenger instanceof EE_messenger |
|
582 | + && $messenger instanceof EE_messenger |
|
583 | + && $sending_messenger->name != $messenger->name |
|
584 | + ) { |
|
585 | + $messenger->do_secondary_messenger_hooks($sending_messenger->name); |
|
586 | + $messenger = $sending_messenger; |
|
587 | + } |
|
588 | + // send using messenger, but double check objects |
|
589 | + if ($messenger instanceof EE_messenger && $message_type instanceof EE_message_type) { |
|
590 | + // set hook for message type (but only if not using another messenger to send). |
|
591 | + if (! isset($this->_did_hook[ $message_type->name ])) { |
|
592 | + $message_type->do_messenger_hooks($messenger); |
|
593 | + $this->_did_hook[ $message_type->name ] = 1; |
|
594 | + } |
|
595 | + // if preview then use preview method |
|
596 | + return $this->_message_repository->is_preview() |
|
597 | + ? $this->_do_preview($message, $messenger, $message_type, $this->_message_repository->is_test_send()) |
|
598 | + : $this->_do_send($message, $messenger, $message_type); |
|
599 | + } |
|
600 | + return false; |
|
601 | + } |
|
602 | + |
|
603 | + |
|
604 | + /** |
|
605 | + * The intention of this method is to count how many EE_Message objects |
|
606 | + * are in the queue with a given status. |
|
607 | + * Example usage: |
|
608 | + * After a caller calls the "EE_Message_Queue::execute()" method, the caller can check if there were any failed |
|
609 | + * sends by calling $queue->count_STS_in_queue( EEM_Message_Queue::status_failed ). |
|
610 | + * |
|
611 | + * @param array|string $status Stati to check for in queue |
|
612 | + * @return int Count of EE_Message's matching the given status. |
|
613 | + */ |
|
614 | + public function count_STS_in_queue($status) |
|
615 | + { |
|
616 | + $count = 0; |
|
617 | + $status = is_array($status) ? $status : array($status); |
|
618 | + $this->_message_repository->rewind(); |
|
619 | + foreach ($this->_message_repository as $message) { |
|
620 | + if (in_array($message->STS_ID(), $status)) { |
|
621 | + $count++; |
|
622 | + } |
|
623 | + } |
|
624 | + return $count; |
|
625 | + } |
|
626 | + |
|
627 | + |
|
628 | + /** |
|
629 | + * Executes the get_preview method on the provided messenger. |
|
630 | + * |
|
631 | + * @param EE_Message $message |
|
632 | + * @param EE_messenger $messenger |
|
633 | + * @param EE_message_type $message_type |
|
634 | + * @param $test_send |
|
635 | + * @return bool true means all went well, false means, not so much. |
|
636 | + */ |
|
637 | + protected function _do_preview( |
|
638 | + EE_Message $message, |
|
639 | + EE_messenger $messenger, |
|
640 | + EE_message_type $message_type, |
|
641 | + $test_send |
|
642 | + ) { |
|
643 | + if ($preview = $messenger->get_preview($message, $message_type, $test_send)) { |
|
644 | + if (! $test_send) { |
|
645 | + $message->set_content($preview); |
|
646 | + } |
|
647 | + $message->set_STS_ID(EEM_Message::status_sent); |
|
648 | + return true; |
|
649 | + } else { |
|
650 | + $message->set_STS_ID(EEM_Message::status_failed); |
|
651 | + return false; |
|
652 | + } |
|
653 | + } |
|
654 | + |
|
655 | + |
|
656 | + /** |
|
657 | + * Executes the send method on the provided messenger |
|
658 | + * EE_Messengers are expected to: |
|
659 | + * - return true if the send was successful. |
|
660 | + * - return false if the send was unsuccessful but can be tried again. |
|
661 | + * - throw an Exception if the send was unsuccessful and cannot be tried again. |
|
662 | + * |
|
663 | + * @param EE_Message $message |
|
664 | + * @param EE_messenger $messenger |
|
665 | + * @param EE_message_type $message_type |
|
666 | + * @return bool true means all went well, false means, not so much. |
|
667 | + */ |
|
668 | + protected function _do_send(EE_Message $message, EE_messenger $messenger, EE_message_type $message_type) |
|
669 | + { |
|
670 | + try { |
|
671 | + if ($messenger->send_message($message, $message_type)) { |
|
672 | + $message->set_STS_ID(EEM_Message::status_sent); |
|
673 | + return true; |
|
674 | + } else { |
|
675 | + $message->set_STS_ID(EEM_Message::status_retry); |
|
676 | + return false; |
|
677 | + } |
|
678 | + } catch (SendMessageException $e) { |
|
679 | + $message->set_STS_ID(EEM_Message::status_failed); |
|
680 | + $message->set_error_message($e->getMessage()); |
|
681 | + return false; |
|
682 | + } |
|
683 | + } |
|
684 | + |
|
685 | + |
|
686 | + /** |
|
687 | + * This sets any necessary error messages on the message object and its status to failed. |
|
688 | + * |
|
689 | + * @param EE_Message $message |
|
690 | + * @param array $error_messages the response from the messenger. |
|
691 | + */ |
|
692 | + protected function _set_error_message(EE_Message $message, $error_messages) |
|
693 | + { |
|
694 | + $error_messages = (array) $error_messages; |
|
695 | + if (in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_failed_sending())) { |
|
696 | + $notices = EE_Error::has_notices(); |
|
697 | + $error_messages[] = esc_html__( |
|
698 | + 'Messenger and Message Type were valid and active, but the messenger send method failed.', |
|
699 | + 'event_espresso' |
|
700 | + ); |
|
701 | + if ($notices === 1) { |
|
702 | + $notices = EE_Error::get_vanilla_notices(); |
|
703 | + $notices['errors'] = isset($notices['errors']) ? $notices['errors'] : array(); |
|
704 | + $error_messages[] = implode("\n", $notices['errors']); |
|
705 | + } |
|
706 | + } |
|
707 | + if (count($error_messages) > 0) { |
|
708 | + $msg = esc_html__('Message was not executed successfully.', 'event_espresso'); |
|
709 | + $msg = $msg . "\n" . implode("\n", $error_messages); |
|
710 | + $message->set_error_message($msg); |
|
711 | + } |
|
712 | + } |
|
713 | 713 | } |
@@ -172,9 +172,9 @@ discard block |
||
172 | 172 | 'order_by' => $this->_get_priority_orderby(), |
173 | 173 | 'limit' => $this->_batch_count, |
174 | 174 | ); |
175 | - $messages = EEM_Message::instance()->get_all($query_args); |
|
175 | + $messages = EEM_Message::instance()->get_all($query_args); |
|
176 | 176 | |
177 | - if (! $messages) { |
|
177 | + if ( ! $messages) { |
|
178 | 178 | return false; // nothing to generate |
179 | 179 | } |
180 | 180 | |
@@ -230,7 +230,7 @@ discard block |
||
230 | 230 | |
231 | 231 | |
232 | 232 | // any to send? |
233 | - if (! $messages_to_send) { |
|
233 | + if ( ! $messages_to_send) { |
|
234 | 234 | $this->unlock_queue(EE_Messages_Queue::action_sending); |
235 | 235 | return false; |
236 | 236 | } |
@@ -286,7 +286,7 @@ discard block |
||
286 | 286 | */ |
287 | 287 | protected function _get_lock_key($type = EE_Messages_Queue::action_generating) |
288 | 288 | { |
289 | - return '_ee_lock_' . $type; |
|
289 | + return '_ee_lock_'.$type; |
|
290 | 290 | } |
291 | 291 | |
292 | 292 | |
@@ -358,7 +358,7 @@ discard block |
||
358 | 358 | */ |
359 | 359 | public function is_locked($type = EE_Messages_Queue::action_generating) |
360 | 360 | { |
361 | - if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
361 | + if ( ! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
362 | 362 | return true; |
363 | 363 | } |
364 | 364 | $lock = (int) get_option($this->_get_lock_key($type), 0); |
@@ -510,7 +510,7 @@ discard block |
||
510 | 510 | /** @type EE_Message $message */ |
511 | 511 | $message = $this->_message_repository->current(); |
512 | 512 | // only process things that are queued for sending |
513 | - if (! in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_to_send())) { |
|
513 | + if ( ! in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_to_send())) { |
|
514 | 514 | $this->_message_repository->next(); |
515 | 515 | continue; |
516 | 516 | } |
@@ -520,13 +520,13 @@ discard block |
||
520 | 520 | continue; |
521 | 521 | } |
522 | 522 | // error checking |
523 | - if (! $message->valid_messenger()) { |
|
523 | + if ( ! $message->valid_messenger()) { |
|
524 | 524 | $error_messages[] = sprintf( |
525 | 525 | esc_html__('The %s messenger is not active at time of sending.', 'event_espresso'), |
526 | 526 | $message->messenger() |
527 | 527 | ); |
528 | 528 | } |
529 | - if (! $message->valid_message_type()) { |
|
529 | + if ( ! $message->valid_message_type()) { |
|
530 | 530 | $error_messages[] = sprintf( |
531 | 531 | esc_html__('The %s message type is not active at the time of sending.', 'event_espresso'), |
532 | 532 | $message->message_type() |
@@ -588,9 +588,9 @@ discard block |
||
588 | 588 | // send using messenger, but double check objects |
589 | 589 | if ($messenger instanceof EE_messenger && $message_type instanceof EE_message_type) { |
590 | 590 | // set hook for message type (but only if not using another messenger to send). |
591 | - if (! isset($this->_did_hook[ $message_type->name ])) { |
|
591 | + if ( ! isset($this->_did_hook[$message_type->name])) { |
|
592 | 592 | $message_type->do_messenger_hooks($messenger); |
593 | - $this->_did_hook[ $message_type->name ] = 1; |
|
593 | + $this->_did_hook[$message_type->name] = 1; |
|
594 | 594 | } |
595 | 595 | // if preview then use preview method |
596 | 596 | return $this->_message_repository->is_preview() |
@@ -641,7 +641,7 @@ discard block |
||
641 | 641 | $test_send |
642 | 642 | ) { |
643 | 643 | if ($preview = $messenger->get_preview($message, $message_type, $test_send)) { |
644 | - if (! $test_send) { |
|
644 | + if ( ! $test_send) { |
|
645 | 645 | $message->set_content($preview); |
646 | 646 | } |
647 | 647 | $message->set_STS_ID(EEM_Message::status_sent); |
@@ -706,7 +706,7 @@ discard block |
||
706 | 706 | } |
707 | 707 | if (count($error_messages) > 0) { |
708 | 708 | $msg = esc_html__('Message was not executed successfully.', 'event_espresso'); |
709 | - $msg = $msg . "\n" . implode("\n", $error_messages); |
|
709 | + $msg = $msg."\n".implode("\n", $error_messages); |
|
710 | 710 | $message->set_error_message($msg); |
711 | 711 | } |
712 | 712 | } |
@@ -170,7 +170,7 @@ discard block |
||
170 | 170 | } |
171 | 171 | |
172 | 172 | $this->_queue->lock_queue(); |
173 | - $messages = is_array($messages) ? $messages : array( $messages ); |
|
173 | + $messages = is_array($messages) ? $messages : array($messages); |
|
174 | 174 | foreach ($messages as $message) { |
175 | 175 | if ($message instanceof EE_Message) { |
176 | 176 | $data = $message->all_extra_meta_array(); |
@@ -205,7 +205,7 @@ discard block |
||
205 | 205 | $this->_init_queue_and_generator(); |
206 | 206 | } |
207 | 207 | |
208 | - $messages = is_array($messages) ? $messages : array( $messages ); |
|
208 | + $messages = is_array($messages) ? $messages : array($messages); |
|
209 | 209 | |
210 | 210 | foreach ($messages as $message) { |
211 | 211 | $this->_queue->add($message); |
@@ -338,8 +338,8 @@ discard block |
||
338 | 338 | protected function _queue_for_generation_loop($messages_to_generate) |
339 | 339 | { |
340 | 340 | // make sure is in an array. |
341 | - if (! is_array($messages_to_generate)) { |
|
342 | - $messages_to_generate = array( $messages_to_generate ); |
|
341 | + if ( ! is_array($messages_to_generate)) { |
|
342 | + $messages_to_generate = array($messages_to_generate); |
|
343 | 343 | } |
344 | 344 | |
345 | 345 | foreach ($messages_to_generate as $message_to_generate) { |
@@ -379,7 +379,7 @@ discard block |
||
379 | 379 | */ |
380 | 380 | public function generate_for_preview(EE_Message_To_Generate $message_to_generate, $test_send = false) |
381 | 381 | { |
382 | - if (! $message_to_generate->valid()) { |
|
382 | + if ( ! $message_to_generate->valid()) { |
|
383 | 383 | EE_Error::add_error( |
384 | 384 | esc_html__('Unable to generate preview because of invalid data', 'event_espresso'), |
385 | 385 | __FILE__, |
@@ -396,7 +396,7 @@ discard block |
||
396 | 396 | if ($generated_queue->execute(false)) { |
397 | 397 | // the first queue item should be the preview |
398 | 398 | $generated_queue->get_message_repository()->rewind(); |
399 | - if (! $generated_queue->get_message_repository()->valid()) { |
|
399 | + if ( ! $generated_queue->get_message_repository()->valid()) { |
|
400 | 400 | return $generated_queue; |
401 | 401 | } |
402 | 402 | return $generated_queue; |
@@ -418,7 +418,7 @@ discard block |
||
418 | 418 | */ |
419 | 419 | public function queue_for_sending(EE_Message_To_Generate $message_to_generate) |
420 | 420 | { |
421 | - if (! $message_to_generate->valid()) { |
|
421 | + if ( ! $message_to_generate->valid()) { |
|
422 | 422 | return false; |
423 | 423 | } |
424 | 424 | $this->_init_queue_and_generator(); |
@@ -440,7 +440,7 @@ discard block |
||
440 | 440 | */ |
441 | 441 | public function generate_and_send_now(EE_Message_To_Generate $message_to_generate) |
442 | 442 | { |
443 | - if (! $message_to_generate->valid()) { |
|
443 | + if ( ! $message_to_generate->valid()) { |
|
444 | 444 | return null; |
445 | 445 | } |
446 | 446 | // is there supposed to be a sending messenger for this message? |
@@ -466,7 +466,7 @@ discard block |
||
466 | 466 | $this->_queue->execute(false, $sending_messenger); |
467 | 467 | return $this->_queue; |
468 | 468 | } elseif ($message_to_generate->get_EE_Message()->STS_ID() === EEM_Message::status_incomplete) { |
469 | - $generated_queue = $this->generate_and_return(array( $message_to_generate )); |
|
469 | + $generated_queue = $this->generate_and_return(array($message_to_generate)); |
|
470 | 470 | $generated_queue->execute(false, $sending_messenger); |
471 | 471 | return $generated_queue; |
472 | 472 | } |
@@ -532,12 +532,12 @@ discard block |
||
532 | 532 | $this->_init_queue_and_generator(); |
533 | 533 | $messages = EEM_Message::instance()->get_all(array( |
534 | 534 | array( |
535 | - 'MSG_ID' => array( 'IN', $message_ids ), |
|
535 | + 'MSG_ID' => array('IN', $message_ids), |
|
536 | 536 | 'STS_ID' => array( |
537 | 537 | 'IN', |
538 | 538 | array_merge( |
539 | 539 | EEM_Message::instance()->stati_indicating_sent(), |
540 | - array( EEM_Message::status_retry ) |
|
540 | + array(EEM_Message::status_retry) |
|
541 | 541 | ), |
542 | 542 | ), |
543 | 543 | ), |
@@ -576,15 +576,15 @@ discard block |
||
576 | 576 | } |
577 | 577 | |
578 | 578 | // make sure is an array |
579 | - $regIDs = is_array($regIDs) ? $regIDs : array( $regIDs ); |
|
579 | + $regIDs = is_array($regIDs) ? $regIDs : array($regIDs); |
|
580 | 580 | |
581 | 581 | foreach ($regIDs as $regID) { |
582 | 582 | $reg = EEM_Registration::instance()->get_one_by_ID($regID); |
583 | - if (! $reg instanceof EE_Registration) { |
|
583 | + if ( ! $reg instanceof EE_Registration) { |
|
584 | 584 | EE_Error::add_error(sprintf(esc_html__('Unable to retrieve a registration object for the given reg id (%s)', 'event_espresso'), $regID)); |
585 | 585 | return false; |
586 | 586 | } |
587 | - $regs_to_send[ $reg->transaction_ID() ][ $reg->status_ID() ][] = $reg; |
|
587 | + $regs_to_send[$reg->transaction_ID()][$reg->status_ID()][] = $reg; |
|
588 | 588 | } |
589 | 589 | |
590 | 590 | $messages_to_generate = array(); |
@@ -592,14 +592,14 @@ discard block |
||
592 | 592 | foreach ($regs_to_send as $status_group) { |
593 | 593 | foreach ($status_group as $status_id => $registrations) { |
594 | 594 | $message_type = EEH_MSG_Template::convert_reg_status_to_message_type($status_id); |
595 | - if (! $message_type) { |
|
595 | + if ( ! $message_type) { |
|
596 | 596 | continue; |
597 | 597 | } |
598 | 598 | $messages_to_generate = array_merge( |
599 | 599 | $messages_to_generate, |
600 | 600 | $this->setup_mtgs_for_all_active_messengers( |
601 | 601 | $message_type, |
602 | - array( $registrations, $status_id ) |
|
602 | + array($registrations, $status_id) |
|
603 | 603 | ) |
604 | 604 | ); |
605 | 605 | } |
@@ -16,595 +16,595 @@ |
||
16 | 16 | { |
17 | 17 | |
18 | 18 | |
19 | - /** |
|
20 | - * @type EE_Message_Resource_Manager $_message_resource_manager |
|
21 | - */ |
|
22 | - protected $_message_resource_manager; |
|
23 | - |
|
24 | - /** |
|
25 | - * @type EE_Messages_Queue |
|
26 | - */ |
|
27 | - protected $_queue; |
|
28 | - |
|
29 | - /** |
|
30 | - * @type EE_Messages_Generator |
|
31 | - */ |
|
32 | - protected $_generator; |
|
33 | - |
|
34 | - |
|
35 | - |
|
36 | - |
|
37 | - /** |
|
38 | - * constructor |
|
39 | - * |
|
40 | - * @param EE_Message_Resource_Manager $message_resource_manager |
|
41 | - */ |
|
42 | - public function __construct(EE_Message_Resource_Manager $message_resource_manager) |
|
43 | - { |
|
44 | - $this->_message_resource_manager = $message_resource_manager; |
|
45 | - $this->_init_queue_and_generator(); |
|
46 | - } |
|
47 | - |
|
48 | - |
|
49 | - |
|
50 | - |
|
51 | - /** |
|
52 | - * This method sets (or resets) the various properties for use. |
|
53 | - * |
|
54 | - * - $_queue = holds the messages queue |
|
55 | - * - $_generator = holds the messages generator |
|
56 | - */ |
|
57 | - protected function _init_queue_and_generator() |
|
58 | - { |
|
59 | - $this->_generator = EE_Registry::factory('EE_Messages_Generator'); |
|
60 | - $this->_queue = $this->_generator->generation_queue(); |
|
61 | - } |
|
62 | - |
|
63 | - |
|
64 | - |
|
65 | - |
|
66 | - /** |
|
67 | - * This returns the current set queue. |
|
68 | - * @return EE_Messages_Queue |
|
69 | - */ |
|
70 | - public function get_queue() |
|
71 | - { |
|
72 | - return $this->_queue; |
|
73 | - } |
|
74 | - |
|
75 | - |
|
76 | - /** |
|
77 | - * This method can be utilized to process messages from a queue and they will be processed immediately on the same |
|
78 | - * request. Please note that this method alone does not bypass the usual "locks" for generation/sending (it assumes |
|
79 | - * client code has already filtered those if necessary). |
|
80 | - * |
|
81 | - * @param EE_Messages_Queue $queue_to_process |
|
82 | - * @return bool true for success false for error. |
|
83 | - * @throws EE_Error |
|
84 | - * @throws EE_Error |
|
85 | - */ |
|
86 | - public function process_immediately_from_queue(EE_Messages_Queue $queue_to_process) |
|
87 | - { |
|
88 | - $success = false; |
|
89 | - $messages_to_send = array(); |
|
90 | - $messages_to_generate = array(); |
|
91 | - // loop through and setup the various messages from the queue so we know what is being processed |
|
92 | - $queue_to_process->get_message_repository()->rewind(); |
|
93 | - foreach ($queue_to_process->get_message_repository() as $message) { |
|
94 | - if ($message->STS_ID() === EEM_Message::status_incomplete) { |
|
95 | - $messages_to_generate[] = $message; |
|
96 | - continue; |
|
97 | - } |
|
98 | - |
|
99 | - if (in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_to_send())) { |
|
100 | - $messages_to_send[] = $message; |
|
101 | - continue; |
|
102 | - } |
|
103 | - } |
|
104 | - |
|
105 | - // do generation/sends |
|
106 | - if ($messages_to_generate) { |
|
107 | - $success = $this->batch_generate_from_queue($messages_to_generate, true); |
|
108 | - } |
|
109 | - |
|
110 | - if ($messages_to_send) { |
|
111 | - $sent = $this->batch_send_from_queue($messages_to_send, true); |
|
112 | - // if there was messages to generate and it failed, then we override any success value for the sending process |
|
113 | - // otherwise we just use the return from batch send. The intent is that there is a simple response for success/fail. |
|
114 | - // Either everything was successful or we consider it a fail. To be clear, this is a limitation of doing |
|
115 | - // all messages processing on the same request. |
|
116 | - $success = $messages_to_generate && ! $success ? false : $sent; |
|
117 | - } |
|
118 | - return $success; |
|
119 | - } |
|
120 | - |
|
121 | - |
|
122 | - /** |
|
123 | - * Calls the EE_Messages_Queue::get_batch_to_generate() method and sends to EE_Messages_Generator. |
|
124 | - * |
|
125 | - * @param EE_Message[] $messages Array of EE_Message objects (optional) to build the queue with. |
|
126 | - * @param bool $clear_queue Whether to ensure a fresh queue or not. |
|
127 | - * |
|
128 | - * @return bool|EE_Messages_Queue return false if nothing generated. This returns a new EE_Message_Queue with |
|
129 | - * generated messages. |
|
130 | - */ |
|
131 | - public function batch_generate_from_queue($messages = array(), $clear_queue = false) |
|
132 | - { |
|
133 | - if ($this->_build_queue_for_generation($messages, $clear_queue)) { |
|
134 | - $new_queue = $this->_generator->generate(); |
|
135 | - if ($new_queue instanceof EE_Messages_Queue) { |
|
136 | - // unlock queue |
|
137 | - $this->_queue->unlock_queue(); |
|
138 | - $new_queue->initiate_request_by_priority('send'); |
|
139 | - return $new_queue; |
|
140 | - } |
|
141 | - } |
|
142 | - $this->_queue->unlock_queue(); |
|
143 | - return false; |
|
144 | - } |
|
145 | - |
|
146 | - |
|
147 | - |
|
148 | - /** |
|
149 | - * This method preps a queue for generation. |
|
150 | - * |
|
151 | - * @since 4.9.0 |
|
152 | - * |
|
153 | - * @param EE_Message[] $messages Array of EE_Message objects to build the queue with |
|
154 | - * |
|
155 | - * @param bool $clear_queue This indicates whether the existing queue should be dumped or not. |
|
156 | - * |
|
157 | - * @return bool true means queue prepped, false means there was a lock so no generation please. |
|
158 | - */ |
|
159 | - protected function _build_queue_for_generation($messages = array(), $clear_queue = false) |
|
160 | - { |
|
161 | - |
|
162 | - if ($clear_queue) { |
|
163 | - $this->_init_queue_and_generator(); |
|
164 | - } |
|
165 | - |
|
166 | - if ($messages) { |
|
167 | - // if generation is locked then get out now because that means processing is already happening. |
|
168 | - if ($this->_queue->is_locked()) { |
|
169 | - return false; |
|
170 | - } |
|
171 | - |
|
172 | - $this->_queue->lock_queue(); |
|
173 | - $messages = is_array($messages) ? $messages : array( $messages ); |
|
174 | - foreach ($messages as $message) { |
|
175 | - if ($message instanceof EE_Message) { |
|
176 | - $data = $message->all_extra_meta_array(); |
|
177 | - $this->_queue->add($message, $data); |
|
178 | - } |
|
179 | - } |
|
180 | - return true; |
|
181 | - } else { |
|
182 | - return $this->_queue->get_batch_to_generate(); |
|
183 | - } |
|
184 | - } |
|
185 | - |
|
186 | - |
|
187 | - /** |
|
188 | - * This method preps a queue for sending. |
|
189 | - * |
|
190 | - * @param EE_Message[] $messages |
|
191 | - * @param bool $clear_queue Used to indicate whether to start with a fresh queue or not. |
|
192 | - * |
|
193 | - * @return bool true means queue prepped, false means there was a lock so no queue prepped. |
|
194 | - */ |
|
195 | - protected function _build_queue_for_sending($messages, $clear_queue = false) |
|
196 | - { |
|
197 | - // if sending is locked then get out now because that means processing is already happening. |
|
198 | - if ($this->_queue->is_locked(EE_Messages_Queue::action_sending)) { |
|
199 | - return false; |
|
200 | - } |
|
201 | - |
|
202 | - $this->_queue->lock_queue(EE_Messages_Queue::action_sending); |
|
203 | - |
|
204 | - if ($clear_queue) { |
|
205 | - $this->_init_queue_and_generator(); |
|
206 | - } |
|
207 | - |
|
208 | - $messages = is_array($messages) ? $messages : array( $messages ); |
|
209 | - |
|
210 | - foreach ($messages as $message) { |
|
211 | - $this->_queue->add($message); |
|
212 | - } |
|
213 | - return true; |
|
214 | - } |
|
215 | - |
|
216 | - |
|
217 | - /** |
|
218 | - * Calls the EE_Message_Queue::get_to_send_batch_and_send() method and then immediately just calls EE_Message_Queue::execute() |
|
219 | - * to iterate and send unsent messages. |
|
220 | - * |
|
221 | - * @param EE_Message[] $messages If an array of messages is sent in then use it. |
|
222 | - * |
|
223 | - * @param bool $clear_queue Whether to initialize a new queue or keep the existing one. |
|
224 | - * |
|
225 | - * @return EE_Messages_Queue |
|
226 | - */ |
|
227 | - public function batch_send_from_queue($messages = array(), $clear_queue = false) |
|
228 | - { |
|
229 | - |
|
230 | - if ($messages && $this->_build_queue_for_sending($messages, $clear_queue)) { |
|
231 | - $this->_queue->execute(); |
|
232 | - $this->_queue->unlock_queue(EE_Messages_Queue::action_sending); |
|
233 | - } else { |
|
234 | - // get messages to send and execute. |
|
235 | - $this->_queue->get_to_send_batch_and_send(); |
|
236 | - } |
|
237 | - // note: callers can use the EE_Messages_Queue::count_STS_in_queue() method to find out if there were any failed |
|
238 | - // messages in the queue and decide how to handle at that point. |
|
239 | - return $this->_queue; |
|
240 | - } |
|
241 | - |
|
242 | - |
|
243 | - |
|
244 | - |
|
245 | - |
|
246 | - |
|
247 | - /** |
|
248 | - * This immediately generates messages using the given array of EE_Message_To_Generate objects and returns the |
|
249 | - * EE_Message_Queue with the generated messages for the caller to work with. Note, this does NOT save the generated |
|
250 | - * messages in the queue, leaving it up to the caller to do so. |
|
251 | - * |
|
252 | - * @param EE_Message_To_Generate[] $messages_to_generate |
|
253 | - * @return EE_Messages_Queue |
|
254 | - */ |
|
255 | - public function generate_and_return($messages_to_generate) |
|
256 | - { |
|
257 | - $this->_init_queue_and_generator(); |
|
258 | - $this->_queue_for_generation_loop($messages_to_generate); |
|
259 | - return $this->_generator->generate(false); |
|
260 | - } |
|
261 | - |
|
262 | - |
|
263 | - |
|
264 | - |
|
265 | - /** |
|
266 | - * Executes the generator generate method on the current internal queue, and returns the generated queue. |
|
267 | - * @param bool $persist Indicate whether to instruct the generator to persist the generated queue (true) or not (false). |
|
268 | - * @return EE_Messages_Queue |
|
269 | - */ |
|
270 | - public function generate_queue($persist = true) |
|
271 | - { |
|
272 | - return $this->_generator->generate($persist); |
|
273 | - } |
|
19 | + /** |
|
20 | + * @type EE_Message_Resource_Manager $_message_resource_manager |
|
21 | + */ |
|
22 | + protected $_message_resource_manager; |
|
23 | + |
|
24 | + /** |
|
25 | + * @type EE_Messages_Queue |
|
26 | + */ |
|
27 | + protected $_queue; |
|
28 | + |
|
29 | + /** |
|
30 | + * @type EE_Messages_Generator |
|
31 | + */ |
|
32 | + protected $_generator; |
|
33 | + |
|
34 | + |
|
35 | + |
|
36 | + |
|
37 | + /** |
|
38 | + * constructor |
|
39 | + * |
|
40 | + * @param EE_Message_Resource_Manager $message_resource_manager |
|
41 | + */ |
|
42 | + public function __construct(EE_Message_Resource_Manager $message_resource_manager) |
|
43 | + { |
|
44 | + $this->_message_resource_manager = $message_resource_manager; |
|
45 | + $this->_init_queue_and_generator(); |
|
46 | + } |
|
47 | + |
|
48 | + |
|
49 | + |
|
50 | + |
|
51 | + /** |
|
52 | + * This method sets (or resets) the various properties for use. |
|
53 | + * |
|
54 | + * - $_queue = holds the messages queue |
|
55 | + * - $_generator = holds the messages generator |
|
56 | + */ |
|
57 | + protected function _init_queue_and_generator() |
|
58 | + { |
|
59 | + $this->_generator = EE_Registry::factory('EE_Messages_Generator'); |
|
60 | + $this->_queue = $this->_generator->generation_queue(); |
|
61 | + } |
|
62 | + |
|
63 | + |
|
64 | + |
|
65 | + |
|
66 | + /** |
|
67 | + * This returns the current set queue. |
|
68 | + * @return EE_Messages_Queue |
|
69 | + */ |
|
70 | + public function get_queue() |
|
71 | + { |
|
72 | + return $this->_queue; |
|
73 | + } |
|
74 | + |
|
75 | + |
|
76 | + /** |
|
77 | + * This method can be utilized to process messages from a queue and they will be processed immediately on the same |
|
78 | + * request. Please note that this method alone does not bypass the usual "locks" for generation/sending (it assumes |
|
79 | + * client code has already filtered those if necessary). |
|
80 | + * |
|
81 | + * @param EE_Messages_Queue $queue_to_process |
|
82 | + * @return bool true for success false for error. |
|
83 | + * @throws EE_Error |
|
84 | + * @throws EE_Error |
|
85 | + */ |
|
86 | + public function process_immediately_from_queue(EE_Messages_Queue $queue_to_process) |
|
87 | + { |
|
88 | + $success = false; |
|
89 | + $messages_to_send = array(); |
|
90 | + $messages_to_generate = array(); |
|
91 | + // loop through and setup the various messages from the queue so we know what is being processed |
|
92 | + $queue_to_process->get_message_repository()->rewind(); |
|
93 | + foreach ($queue_to_process->get_message_repository() as $message) { |
|
94 | + if ($message->STS_ID() === EEM_Message::status_incomplete) { |
|
95 | + $messages_to_generate[] = $message; |
|
96 | + continue; |
|
97 | + } |
|
98 | + |
|
99 | + if (in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_to_send())) { |
|
100 | + $messages_to_send[] = $message; |
|
101 | + continue; |
|
102 | + } |
|
103 | + } |
|
104 | + |
|
105 | + // do generation/sends |
|
106 | + if ($messages_to_generate) { |
|
107 | + $success = $this->batch_generate_from_queue($messages_to_generate, true); |
|
108 | + } |
|
109 | + |
|
110 | + if ($messages_to_send) { |
|
111 | + $sent = $this->batch_send_from_queue($messages_to_send, true); |
|
112 | + // if there was messages to generate and it failed, then we override any success value for the sending process |
|
113 | + // otherwise we just use the return from batch send. The intent is that there is a simple response for success/fail. |
|
114 | + // Either everything was successful or we consider it a fail. To be clear, this is a limitation of doing |
|
115 | + // all messages processing on the same request. |
|
116 | + $success = $messages_to_generate && ! $success ? false : $sent; |
|
117 | + } |
|
118 | + return $success; |
|
119 | + } |
|
120 | + |
|
121 | + |
|
122 | + /** |
|
123 | + * Calls the EE_Messages_Queue::get_batch_to_generate() method and sends to EE_Messages_Generator. |
|
124 | + * |
|
125 | + * @param EE_Message[] $messages Array of EE_Message objects (optional) to build the queue with. |
|
126 | + * @param bool $clear_queue Whether to ensure a fresh queue or not. |
|
127 | + * |
|
128 | + * @return bool|EE_Messages_Queue return false if nothing generated. This returns a new EE_Message_Queue with |
|
129 | + * generated messages. |
|
130 | + */ |
|
131 | + public function batch_generate_from_queue($messages = array(), $clear_queue = false) |
|
132 | + { |
|
133 | + if ($this->_build_queue_for_generation($messages, $clear_queue)) { |
|
134 | + $new_queue = $this->_generator->generate(); |
|
135 | + if ($new_queue instanceof EE_Messages_Queue) { |
|
136 | + // unlock queue |
|
137 | + $this->_queue->unlock_queue(); |
|
138 | + $new_queue->initiate_request_by_priority('send'); |
|
139 | + return $new_queue; |
|
140 | + } |
|
141 | + } |
|
142 | + $this->_queue->unlock_queue(); |
|
143 | + return false; |
|
144 | + } |
|
145 | + |
|
146 | + |
|
147 | + |
|
148 | + /** |
|
149 | + * This method preps a queue for generation. |
|
150 | + * |
|
151 | + * @since 4.9.0 |
|
152 | + * |
|
153 | + * @param EE_Message[] $messages Array of EE_Message objects to build the queue with |
|
154 | + * |
|
155 | + * @param bool $clear_queue This indicates whether the existing queue should be dumped or not. |
|
156 | + * |
|
157 | + * @return bool true means queue prepped, false means there was a lock so no generation please. |
|
158 | + */ |
|
159 | + protected function _build_queue_for_generation($messages = array(), $clear_queue = false) |
|
160 | + { |
|
161 | + |
|
162 | + if ($clear_queue) { |
|
163 | + $this->_init_queue_and_generator(); |
|
164 | + } |
|
165 | + |
|
166 | + if ($messages) { |
|
167 | + // if generation is locked then get out now because that means processing is already happening. |
|
168 | + if ($this->_queue->is_locked()) { |
|
169 | + return false; |
|
170 | + } |
|
171 | + |
|
172 | + $this->_queue->lock_queue(); |
|
173 | + $messages = is_array($messages) ? $messages : array( $messages ); |
|
174 | + foreach ($messages as $message) { |
|
175 | + if ($message instanceof EE_Message) { |
|
176 | + $data = $message->all_extra_meta_array(); |
|
177 | + $this->_queue->add($message, $data); |
|
178 | + } |
|
179 | + } |
|
180 | + return true; |
|
181 | + } else { |
|
182 | + return $this->_queue->get_batch_to_generate(); |
|
183 | + } |
|
184 | + } |
|
185 | + |
|
186 | + |
|
187 | + /** |
|
188 | + * This method preps a queue for sending. |
|
189 | + * |
|
190 | + * @param EE_Message[] $messages |
|
191 | + * @param bool $clear_queue Used to indicate whether to start with a fresh queue or not. |
|
192 | + * |
|
193 | + * @return bool true means queue prepped, false means there was a lock so no queue prepped. |
|
194 | + */ |
|
195 | + protected function _build_queue_for_sending($messages, $clear_queue = false) |
|
196 | + { |
|
197 | + // if sending is locked then get out now because that means processing is already happening. |
|
198 | + if ($this->_queue->is_locked(EE_Messages_Queue::action_sending)) { |
|
199 | + return false; |
|
200 | + } |
|
201 | + |
|
202 | + $this->_queue->lock_queue(EE_Messages_Queue::action_sending); |
|
203 | + |
|
204 | + if ($clear_queue) { |
|
205 | + $this->_init_queue_and_generator(); |
|
206 | + } |
|
207 | + |
|
208 | + $messages = is_array($messages) ? $messages : array( $messages ); |
|
209 | + |
|
210 | + foreach ($messages as $message) { |
|
211 | + $this->_queue->add($message); |
|
212 | + } |
|
213 | + return true; |
|
214 | + } |
|
215 | + |
|
216 | + |
|
217 | + /** |
|
218 | + * Calls the EE_Message_Queue::get_to_send_batch_and_send() method and then immediately just calls EE_Message_Queue::execute() |
|
219 | + * to iterate and send unsent messages. |
|
220 | + * |
|
221 | + * @param EE_Message[] $messages If an array of messages is sent in then use it. |
|
222 | + * |
|
223 | + * @param bool $clear_queue Whether to initialize a new queue or keep the existing one. |
|
224 | + * |
|
225 | + * @return EE_Messages_Queue |
|
226 | + */ |
|
227 | + public function batch_send_from_queue($messages = array(), $clear_queue = false) |
|
228 | + { |
|
229 | + |
|
230 | + if ($messages && $this->_build_queue_for_sending($messages, $clear_queue)) { |
|
231 | + $this->_queue->execute(); |
|
232 | + $this->_queue->unlock_queue(EE_Messages_Queue::action_sending); |
|
233 | + } else { |
|
234 | + // get messages to send and execute. |
|
235 | + $this->_queue->get_to_send_batch_and_send(); |
|
236 | + } |
|
237 | + // note: callers can use the EE_Messages_Queue::count_STS_in_queue() method to find out if there were any failed |
|
238 | + // messages in the queue and decide how to handle at that point. |
|
239 | + return $this->_queue; |
|
240 | + } |
|
241 | + |
|
242 | + |
|
243 | + |
|
244 | + |
|
245 | + |
|
246 | + |
|
247 | + /** |
|
248 | + * This immediately generates messages using the given array of EE_Message_To_Generate objects and returns the |
|
249 | + * EE_Message_Queue with the generated messages for the caller to work with. Note, this does NOT save the generated |
|
250 | + * messages in the queue, leaving it up to the caller to do so. |
|
251 | + * |
|
252 | + * @param EE_Message_To_Generate[] $messages_to_generate |
|
253 | + * @return EE_Messages_Queue |
|
254 | + */ |
|
255 | + public function generate_and_return($messages_to_generate) |
|
256 | + { |
|
257 | + $this->_init_queue_and_generator(); |
|
258 | + $this->_queue_for_generation_loop($messages_to_generate); |
|
259 | + return $this->_generator->generate(false); |
|
260 | + } |
|
261 | + |
|
262 | + |
|
263 | + |
|
264 | + |
|
265 | + /** |
|
266 | + * Executes the generator generate method on the current internal queue, and returns the generated queue. |
|
267 | + * @param bool $persist Indicate whether to instruct the generator to persist the generated queue (true) or not (false). |
|
268 | + * @return EE_Messages_Queue |
|
269 | + */ |
|
270 | + public function generate_queue($persist = true) |
|
271 | + { |
|
272 | + return $this->_generator->generate($persist); |
|
273 | + } |
|
274 | 274 | |
275 | 275 | |
276 | 276 | |
277 | 277 | |
278 | - /** |
|
279 | - * Queue for generation. Note this does NOT persist to the db. Client code should call get_message_repository()->save() if desire |
|
280 | - * to persist. This method is provided to client code to decide what it wants to do with queued messages for generation. |
|
281 | - * @param EE_Message_To_Generate $message_to_generate |
|
282 | - * @param bool $test_send Whether this item is for a test send or not. |
|
283 | - * @return EE_Messages_Queue |
|
284 | - */ |
|
285 | - public function queue_for_generation(EE_Message_To_Generate $message_to_generate, $test_send = false) |
|
286 | - { |
|
287 | - if ($message_to_generate->valid()) { |
|
288 | - $this->_generator->create_and_add_message_to_queue($message_to_generate, $test_send); |
|
289 | - } |
|
290 | - } |
|
291 | - |
|
292 | - |
|
293 | - |
|
294 | - |
|
295 | - |
|
296 | - |
|
297 | - |
|
298 | - /** |
|
299 | - * This receives an array of EE_Message_To_Generate objects, converts them to EE_Message adds them to the generation queue |
|
300 | - * and then persists to storage. |
|
301 | - * |
|
302 | - * @param EE_Message_To_Generate[] $messages_to_generate |
|
303 | - */ |
|
304 | - public function batch_queue_for_generation_and_persist($messages_to_generate) |
|
305 | - { |
|
306 | - $this->_init_queue_and_generator(); |
|
307 | - $this->_queue_for_generation_loop($messages_to_generate); |
|
308 | - $this->_queue->save(); |
|
309 | - } |
|
310 | - |
|
311 | - |
|
312 | - |
|
313 | - |
|
314 | - |
|
315 | - |
|
316 | - /** |
|
317 | - * This receives an array of EE_Message_To_Generate objects, converts them to EE_Message and adds them to the generation |
|
318 | - * queue. Does NOT persist to storage (unless there is an error. |
|
319 | - * Client code can retrieve the generated queue by calling EEM_Messages_Processor::get_queue() |
|
320 | - * |
|
321 | - * @param EE_Message_To_Generate[] $messages_to_generate |
|
322 | - */ |
|
323 | - public function batch_queue_for_generation_no_persist($messages_to_generate) |
|
324 | - { |
|
325 | - $this->_init_queue_and_generator(); |
|
326 | - $this->_queue_for_generation_loop($messages_to_generate); |
|
327 | - } |
|
328 | - |
|
329 | - |
|
330 | - |
|
331 | - |
|
332 | - /** |
|
333 | - * Simply loops through the given array of EE_Message_To_Generate objects and adds them to the _queue as EE_Message |
|
334 | - * objects. |
|
335 | - * |
|
336 | - * @param EE_Message_To_Generate[] $messages_to_generate |
|
337 | - */ |
|
338 | - protected function _queue_for_generation_loop($messages_to_generate) |
|
339 | - { |
|
340 | - // make sure is in an array. |
|
341 | - if (! is_array($messages_to_generate)) { |
|
342 | - $messages_to_generate = array( $messages_to_generate ); |
|
343 | - } |
|
344 | - |
|
345 | - foreach ($messages_to_generate as $message_to_generate) { |
|
346 | - if ($message_to_generate instanceof EE_Message_To_Generate && $message_to_generate->valid()) { |
|
347 | - $this->queue_for_generation($message_to_generate); |
|
348 | - } |
|
349 | - } |
|
350 | - } |
|
351 | - |
|
352 | - |
|
353 | - |
|
354 | - |
|
355 | - |
|
356 | - /** |
|
357 | - * Receives an array of EE_Message_To_Generate objects and generates the EE_Message objects, then persists (so its |
|
358 | - * queued for sending). |
|
359 | - * @param EE_Message_To_Generate[] |
|
360 | - * @return EE_Messages_Queue |
|
361 | - */ |
|
362 | - public function generate_and_queue_for_sending($messages_to_generate) |
|
363 | - { |
|
364 | - $this->_init_queue_and_generator(); |
|
365 | - $this->_queue_for_generation_loop($messages_to_generate); |
|
366 | - return $this->_generator->generate(true); |
|
367 | - } |
|
368 | - |
|
369 | - |
|
370 | - |
|
371 | - |
|
372 | - |
|
373 | - /** |
|
374 | - * Generate for preview and execute right away. |
|
375 | - * |
|
376 | - * @param EE_Message_To_Generate $message_to_generate |
|
377 | - * @param bool $test_send Whether this is a test send or not. |
|
378 | - * @return EE_Messages_Queue | bool false if unable to generate otherwise the generated queue. |
|
379 | - */ |
|
380 | - public function generate_for_preview(EE_Message_To_Generate $message_to_generate, $test_send = false) |
|
381 | - { |
|
382 | - if (! $message_to_generate->valid()) { |
|
383 | - EE_Error::add_error( |
|
384 | - esc_html__('Unable to generate preview because of invalid data', 'event_espresso'), |
|
385 | - __FILE__, |
|
386 | - __FUNCTION__, |
|
387 | - __LINE__ |
|
388 | - ); |
|
389 | - return false; |
|
390 | - } |
|
391 | - // just make sure preview is set on the $message_to_generate (in case client forgot) |
|
392 | - $message_to_generate->set_preview(true); |
|
393 | - $this->_init_queue_and_generator(); |
|
394 | - $this->queue_for_generation($message_to_generate, $test_send); |
|
395 | - $generated_queue = $this->_generator->generate(false); |
|
396 | - if ($generated_queue->execute(false)) { |
|
397 | - // the first queue item should be the preview |
|
398 | - $generated_queue->get_message_repository()->rewind(); |
|
399 | - if (! $generated_queue->get_message_repository()->valid()) { |
|
400 | - return $generated_queue; |
|
401 | - } |
|
402 | - return $generated_queue; |
|
403 | - } else { |
|
404 | - return false; |
|
405 | - } |
|
406 | - } |
|
407 | - |
|
408 | - |
|
409 | - /** |
|
410 | - * This queues for sending. |
|
411 | - * The messenger send now method is also verified to see if sending immediately is requested. |
|
412 | - * otherwise its just saved to the queue. |
|
413 | - * |
|
414 | - * @param EE_Message_To_Generate $message_to_generate |
|
415 | - * @return bool true or false for success. |
|
416 | - * @throws EE_Error |
|
417 | - * @throws EE_Error |
|
418 | - */ |
|
419 | - public function queue_for_sending(EE_Message_To_Generate $message_to_generate) |
|
420 | - { |
|
421 | - if (! $message_to_generate->valid()) { |
|
422 | - return false; |
|
423 | - } |
|
424 | - $this->_init_queue_and_generator(); |
|
425 | - $message = $message_to_generate->get_EE_Message(); |
|
426 | - $this->_queue->add($message); |
|
427 | - if ($message->send_now()) { |
|
428 | - $this->_queue->execute(false); |
|
429 | - } else { |
|
430 | - $this->_queue->save(); |
|
431 | - } |
|
432 | - return true; |
|
433 | - } |
|
434 | - |
|
435 | - |
|
436 | - /** |
|
437 | - * This generates and sends from the given EE_Message_To_Generate class immediately. |
|
438 | - * @param EE_Message_To_Generate $message_to_generate |
|
439 | - * @return EE_Messages_Queue | null |
|
440 | - */ |
|
441 | - public function generate_and_send_now(EE_Message_To_Generate $message_to_generate) |
|
442 | - { |
|
443 | - if (! $message_to_generate->valid()) { |
|
444 | - return null; |
|
445 | - } |
|
446 | - // is there supposed to be a sending messenger for this message? |
|
447 | - if ($message_to_generate instanceof EEI_Has_Sending_Messenger) { |
|
448 | - // make sure it's valid, but if it's not, |
|
449 | - // then set the value of $sending_messenger to an EE_Error object |
|
450 | - // so that downstream code can easily see that things went wrong. |
|
451 | - $sending_messenger = $message_to_generate->sending_messenger() instanceof EE_messenger |
|
452 | - ? $message_to_generate->sending_messenger() |
|
453 | - : new EE_Error( |
|
454 | - esc_html__( |
|
455 | - 'There was a specific sending messenger requested for the send action, but it was either invalid or not active at time of sending.', |
|
456 | - 'event_espresso' |
|
457 | - ) |
|
458 | - ); |
|
459 | - } else { |
|
460 | - $sending_messenger = null; |
|
461 | - } |
|
462 | - |
|
463 | - if ($message_to_generate->get_EE_Message()->STS_ID() === EEM_Message::status_idle) { |
|
464 | - $this->_init_queue_and_generator(); |
|
465 | - $this->_queue->add($message_to_generate->get_EE_Message()); |
|
466 | - $this->_queue->execute(false, $sending_messenger); |
|
467 | - return $this->_queue; |
|
468 | - } elseif ($message_to_generate->get_EE_Message()->STS_ID() === EEM_Message::status_incomplete) { |
|
469 | - $generated_queue = $this->generate_and_return(array( $message_to_generate )); |
|
470 | - $generated_queue->execute(false, $sending_messenger); |
|
471 | - return $generated_queue; |
|
472 | - } |
|
473 | - return null; |
|
474 | - } |
|
475 | - |
|
476 | - |
|
477 | - |
|
478 | - |
|
479 | - /** |
|
480 | - * Creates mtg objects for all active messengers and queues for generation. |
|
481 | - * This method also calls the execute by priority method on the queue which will optionally kick off a new non-blocking |
|
482 | - * request to complete the action if the priority for the message requires immediate action. |
|
483 | - * @param string $message_type |
|
484 | - * @param mixed $data The data being used for generation. |
|
485 | - * @param bool $persist Whether to persist the queued messages to the db or not. |
|
486 | - */ |
|
487 | - public function generate_for_all_active_messengers($message_type, $data, $persist = true) |
|
488 | - { |
|
489 | - $messages_to_generate = $this->setup_mtgs_for_all_active_messengers($message_type, $data); |
|
490 | - if ($persist) { |
|
491 | - $this->batch_queue_for_generation_and_persist($messages_to_generate); |
|
492 | - $this->_queue->initiate_request_by_priority(); |
|
493 | - } else { |
|
494 | - $this->batch_queue_for_generation_no_persist($messages_to_generate); |
|
495 | - } |
|
496 | - } |
|
497 | - |
|
498 | - |
|
499 | - |
|
500 | - |
|
501 | - /** |
|
502 | - * This simply loops through all active messengers and takes care of setting up the |
|
503 | - * EE_Message_To_Generate objects. |
|
504 | - * @param $message_type |
|
505 | - * @param $data |
|
506 | - * |
|
507 | - * @return EE_Message_To_Generate[] |
|
508 | - */ |
|
509 | - public function setup_mtgs_for_all_active_messengers($message_type, $data) |
|
510 | - { |
|
511 | - $messages_to_generate = array(); |
|
512 | - foreach ($this->_message_resource_manager->active_messengers() as $messenger_slug => $messenger_object) { |
|
513 | - $message_to_generate = new EE_Message_To_Generate($messenger_slug, $message_type, $data); |
|
514 | - if ($message_to_generate->valid()) { |
|
515 | - $messages_to_generate[] = $message_to_generate; |
|
516 | - } |
|
517 | - } |
|
518 | - return $messages_to_generate; |
|
519 | - } |
|
520 | - |
|
521 | - |
|
522 | - /** |
|
523 | - * This accepts an array of EE_Message::MSG_ID values and will use that to retrieve the objects from the database |
|
524 | - * and send. |
|
525 | - * |
|
526 | - * @param array $message_ids |
|
527 | - * @throws EE_Error |
|
528 | - * @throws EE_Error |
|
529 | - */ |
|
530 | - public function setup_messages_from_ids_and_send($message_ids) |
|
531 | - { |
|
532 | - $this->_init_queue_and_generator(); |
|
533 | - $messages = EEM_Message::instance()->get_all(array( |
|
534 | - array( |
|
535 | - 'MSG_ID' => array( 'IN', $message_ids ), |
|
536 | - 'STS_ID' => array( |
|
537 | - 'IN', |
|
538 | - array_merge( |
|
539 | - EEM_Message::instance()->stati_indicating_sent(), |
|
540 | - array( EEM_Message::status_retry ) |
|
541 | - ), |
|
542 | - ), |
|
543 | - ), |
|
544 | - )); |
|
545 | - // set the Messages to resend. |
|
546 | - foreach ($messages as $message) { |
|
547 | - if ($message instanceof EE_Message) { |
|
548 | - $message->set_STS_ID(EEM_Message::status_resend); |
|
549 | - $this->_queue->add($message); |
|
550 | - } |
|
551 | - } |
|
552 | - |
|
553 | - $this->_queue->initiate_request_by_priority('send'); |
|
554 | - } |
|
555 | - |
|
556 | - |
|
557 | - /** |
|
558 | - * This method checks for registration IDs in the request via the given key and creates the messages to generate |
|
559 | - * objects from them, then returns the array of messages to generate objects. |
|
560 | - * Note, this sets up registrations for the registration family of message types. |
|
561 | - * |
|
562 | - * @param string $registration_ids_key This is used to indicate what represents the registration ids in the request. |
|
563 | - * |
|
564 | - * @return EE_Message_To_Generate[]|bool |
|
565 | - * @throws EE_Error |
|
566 | - */ |
|
567 | - public function setup_messages_to_generate_from_registration_ids_in_request($registration_ids_key = '_REG_ID') |
|
568 | - { |
|
569 | - /** @var RequestInterface $request */ |
|
570 | - $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
571 | - $regs_to_send = array(); |
|
572 | - $regIDs = $request->getRequestParam($registration_ids_key, [], 'int', true); |
|
573 | - if (empty($regIDs)) { |
|
574 | - EE_Error::add_error(esc_html__('Something went wrong because we\'re missing the registration ID', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
575 | - return false; |
|
576 | - } |
|
577 | - |
|
578 | - // make sure is an array |
|
579 | - $regIDs = is_array($regIDs) ? $regIDs : array( $regIDs ); |
|
580 | - |
|
581 | - foreach ($regIDs as $regID) { |
|
582 | - $reg = EEM_Registration::instance()->get_one_by_ID($regID); |
|
583 | - if (! $reg instanceof EE_Registration) { |
|
584 | - EE_Error::add_error(sprintf(esc_html__('Unable to retrieve a registration object for the given reg id (%s)', 'event_espresso'), $regID)); |
|
585 | - return false; |
|
586 | - } |
|
587 | - $regs_to_send[ $reg->transaction_ID() ][ $reg->status_ID() ][] = $reg; |
|
588 | - } |
|
589 | - |
|
590 | - $messages_to_generate = array(); |
|
591 | - |
|
592 | - foreach ($regs_to_send as $status_group) { |
|
593 | - foreach ($status_group as $status_id => $registrations) { |
|
594 | - $message_type = EEH_MSG_Template::convert_reg_status_to_message_type($status_id); |
|
595 | - if (! $message_type) { |
|
596 | - continue; |
|
597 | - } |
|
598 | - $messages_to_generate = array_merge( |
|
599 | - $messages_to_generate, |
|
600 | - $this->setup_mtgs_for_all_active_messengers( |
|
601 | - $message_type, |
|
602 | - array( $registrations, $status_id ) |
|
603 | - ) |
|
604 | - ); |
|
605 | - } |
|
606 | - } |
|
607 | - |
|
608 | - return $messages_to_generate; |
|
609 | - } |
|
278 | + /** |
|
279 | + * Queue for generation. Note this does NOT persist to the db. Client code should call get_message_repository()->save() if desire |
|
280 | + * to persist. This method is provided to client code to decide what it wants to do with queued messages for generation. |
|
281 | + * @param EE_Message_To_Generate $message_to_generate |
|
282 | + * @param bool $test_send Whether this item is for a test send or not. |
|
283 | + * @return EE_Messages_Queue |
|
284 | + */ |
|
285 | + public function queue_for_generation(EE_Message_To_Generate $message_to_generate, $test_send = false) |
|
286 | + { |
|
287 | + if ($message_to_generate->valid()) { |
|
288 | + $this->_generator->create_and_add_message_to_queue($message_to_generate, $test_send); |
|
289 | + } |
|
290 | + } |
|
291 | + |
|
292 | + |
|
293 | + |
|
294 | + |
|
295 | + |
|
296 | + |
|
297 | + |
|
298 | + /** |
|
299 | + * This receives an array of EE_Message_To_Generate objects, converts them to EE_Message adds them to the generation queue |
|
300 | + * and then persists to storage. |
|
301 | + * |
|
302 | + * @param EE_Message_To_Generate[] $messages_to_generate |
|
303 | + */ |
|
304 | + public function batch_queue_for_generation_and_persist($messages_to_generate) |
|
305 | + { |
|
306 | + $this->_init_queue_and_generator(); |
|
307 | + $this->_queue_for_generation_loop($messages_to_generate); |
|
308 | + $this->_queue->save(); |
|
309 | + } |
|
310 | + |
|
311 | + |
|
312 | + |
|
313 | + |
|
314 | + |
|
315 | + |
|
316 | + /** |
|
317 | + * This receives an array of EE_Message_To_Generate objects, converts them to EE_Message and adds them to the generation |
|
318 | + * queue. Does NOT persist to storage (unless there is an error. |
|
319 | + * Client code can retrieve the generated queue by calling EEM_Messages_Processor::get_queue() |
|
320 | + * |
|
321 | + * @param EE_Message_To_Generate[] $messages_to_generate |
|
322 | + */ |
|
323 | + public function batch_queue_for_generation_no_persist($messages_to_generate) |
|
324 | + { |
|
325 | + $this->_init_queue_and_generator(); |
|
326 | + $this->_queue_for_generation_loop($messages_to_generate); |
|
327 | + } |
|
328 | + |
|
329 | + |
|
330 | + |
|
331 | + |
|
332 | + /** |
|
333 | + * Simply loops through the given array of EE_Message_To_Generate objects and adds them to the _queue as EE_Message |
|
334 | + * objects. |
|
335 | + * |
|
336 | + * @param EE_Message_To_Generate[] $messages_to_generate |
|
337 | + */ |
|
338 | + protected function _queue_for_generation_loop($messages_to_generate) |
|
339 | + { |
|
340 | + // make sure is in an array. |
|
341 | + if (! is_array($messages_to_generate)) { |
|
342 | + $messages_to_generate = array( $messages_to_generate ); |
|
343 | + } |
|
344 | + |
|
345 | + foreach ($messages_to_generate as $message_to_generate) { |
|
346 | + if ($message_to_generate instanceof EE_Message_To_Generate && $message_to_generate->valid()) { |
|
347 | + $this->queue_for_generation($message_to_generate); |
|
348 | + } |
|
349 | + } |
|
350 | + } |
|
351 | + |
|
352 | + |
|
353 | + |
|
354 | + |
|
355 | + |
|
356 | + /** |
|
357 | + * Receives an array of EE_Message_To_Generate objects and generates the EE_Message objects, then persists (so its |
|
358 | + * queued for sending). |
|
359 | + * @param EE_Message_To_Generate[] |
|
360 | + * @return EE_Messages_Queue |
|
361 | + */ |
|
362 | + public function generate_and_queue_for_sending($messages_to_generate) |
|
363 | + { |
|
364 | + $this->_init_queue_and_generator(); |
|
365 | + $this->_queue_for_generation_loop($messages_to_generate); |
|
366 | + return $this->_generator->generate(true); |
|
367 | + } |
|
368 | + |
|
369 | + |
|
370 | + |
|
371 | + |
|
372 | + |
|
373 | + /** |
|
374 | + * Generate for preview and execute right away. |
|
375 | + * |
|
376 | + * @param EE_Message_To_Generate $message_to_generate |
|
377 | + * @param bool $test_send Whether this is a test send or not. |
|
378 | + * @return EE_Messages_Queue | bool false if unable to generate otherwise the generated queue. |
|
379 | + */ |
|
380 | + public function generate_for_preview(EE_Message_To_Generate $message_to_generate, $test_send = false) |
|
381 | + { |
|
382 | + if (! $message_to_generate->valid()) { |
|
383 | + EE_Error::add_error( |
|
384 | + esc_html__('Unable to generate preview because of invalid data', 'event_espresso'), |
|
385 | + __FILE__, |
|
386 | + __FUNCTION__, |
|
387 | + __LINE__ |
|
388 | + ); |
|
389 | + return false; |
|
390 | + } |
|
391 | + // just make sure preview is set on the $message_to_generate (in case client forgot) |
|
392 | + $message_to_generate->set_preview(true); |
|
393 | + $this->_init_queue_and_generator(); |
|
394 | + $this->queue_for_generation($message_to_generate, $test_send); |
|
395 | + $generated_queue = $this->_generator->generate(false); |
|
396 | + if ($generated_queue->execute(false)) { |
|
397 | + // the first queue item should be the preview |
|
398 | + $generated_queue->get_message_repository()->rewind(); |
|
399 | + if (! $generated_queue->get_message_repository()->valid()) { |
|
400 | + return $generated_queue; |
|
401 | + } |
|
402 | + return $generated_queue; |
|
403 | + } else { |
|
404 | + return false; |
|
405 | + } |
|
406 | + } |
|
407 | + |
|
408 | + |
|
409 | + /** |
|
410 | + * This queues for sending. |
|
411 | + * The messenger send now method is also verified to see if sending immediately is requested. |
|
412 | + * otherwise its just saved to the queue. |
|
413 | + * |
|
414 | + * @param EE_Message_To_Generate $message_to_generate |
|
415 | + * @return bool true or false for success. |
|
416 | + * @throws EE_Error |
|
417 | + * @throws EE_Error |
|
418 | + */ |
|
419 | + public function queue_for_sending(EE_Message_To_Generate $message_to_generate) |
|
420 | + { |
|
421 | + if (! $message_to_generate->valid()) { |
|
422 | + return false; |
|
423 | + } |
|
424 | + $this->_init_queue_and_generator(); |
|
425 | + $message = $message_to_generate->get_EE_Message(); |
|
426 | + $this->_queue->add($message); |
|
427 | + if ($message->send_now()) { |
|
428 | + $this->_queue->execute(false); |
|
429 | + } else { |
|
430 | + $this->_queue->save(); |
|
431 | + } |
|
432 | + return true; |
|
433 | + } |
|
434 | + |
|
435 | + |
|
436 | + /** |
|
437 | + * This generates and sends from the given EE_Message_To_Generate class immediately. |
|
438 | + * @param EE_Message_To_Generate $message_to_generate |
|
439 | + * @return EE_Messages_Queue | null |
|
440 | + */ |
|
441 | + public function generate_and_send_now(EE_Message_To_Generate $message_to_generate) |
|
442 | + { |
|
443 | + if (! $message_to_generate->valid()) { |
|
444 | + return null; |
|
445 | + } |
|
446 | + // is there supposed to be a sending messenger for this message? |
|
447 | + if ($message_to_generate instanceof EEI_Has_Sending_Messenger) { |
|
448 | + // make sure it's valid, but if it's not, |
|
449 | + // then set the value of $sending_messenger to an EE_Error object |
|
450 | + // so that downstream code can easily see that things went wrong. |
|
451 | + $sending_messenger = $message_to_generate->sending_messenger() instanceof EE_messenger |
|
452 | + ? $message_to_generate->sending_messenger() |
|
453 | + : new EE_Error( |
|
454 | + esc_html__( |
|
455 | + 'There was a specific sending messenger requested for the send action, but it was either invalid or not active at time of sending.', |
|
456 | + 'event_espresso' |
|
457 | + ) |
|
458 | + ); |
|
459 | + } else { |
|
460 | + $sending_messenger = null; |
|
461 | + } |
|
462 | + |
|
463 | + if ($message_to_generate->get_EE_Message()->STS_ID() === EEM_Message::status_idle) { |
|
464 | + $this->_init_queue_and_generator(); |
|
465 | + $this->_queue->add($message_to_generate->get_EE_Message()); |
|
466 | + $this->_queue->execute(false, $sending_messenger); |
|
467 | + return $this->_queue; |
|
468 | + } elseif ($message_to_generate->get_EE_Message()->STS_ID() === EEM_Message::status_incomplete) { |
|
469 | + $generated_queue = $this->generate_and_return(array( $message_to_generate )); |
|
470 | + $generated_queue->execute(false, $sending_messenger); |
|
471 | + return $generated_queue; |
|
472 | + } |
|
473 | + return null; |
|
474 | + } |
|
475 | + |
|
476 | + |
|
477 | + |
|
478 | + |
|
479 | + /** |
|
480 | + * Creates mtg objects for all active messengers and queues for generation. |
|
481 | + * This method also calls the execute by priority method on the queue which will optionally kick off a new non-blocking |
|
482 | + * request to complete the action if the priority for the message requires immediate action. |
|
483 | + * @param string $message_type |
|
484 | + * @param mixed $data The data being used for generation. |
|
485 | + * @param bool $persist Whether to persist the queued messages to the db or not. |
|
486 | + */ |
|
487 | + public function generate_for_all_active_messengers($message_type, $data, $persist = true) |
|
488 | + { |
|
489 | + $messages_to_generate = $this->setup_mtgs_for_all_active_messengers($message_type, $data); |
|
490 | + if ($persist) { |
|
491 | + $this->batch_queue_for_generation_and_persist($messages_to_generate); |
|
492 | + $this->_queue->initiate_request_by_priority(); |
|
493 | + } else { |
|
494 | + $this->batch_queue_for_generation_no_persist($messages_to_generate); |
|
495 | + } |
|
496 | + } |
|
497 | + |
|
498 | + |
|
499 | + |
|
500 | + |
|
501 | + /** |
|
502 | + * This simply loops through all active messengers and takes care of setting up the |
|
503 | + * EE_Message_To_Generate objects. |
|
504 | + * @param $message_type |
|
505 | + * @param $data |
|
506 | + * |
|
507 | + * @return EE_Message_To_Generate[] |
|
508 | + */ |
|
509 | + public function setup_mtgs_for_all_active_messengers($message_type, $data) |
|
510 | + { |
|
511 | + $messages_to_generate = array(); |
|
512 | + foreach ($this->_message_resource_manager->active_messengers() as $messenger_slug => $messenger_object) { |
|
513 | + $message_to_generate = new EE_Message_To_Generate($messenger_slug, $message_type, $data); |
|
514 | + if ($message_to_generate->valid()) { |
|
515 | + $messages_to_generate[] = $message_to_generate; |
|
516 | + } |
|
517 | + } |
|
518 | + return $messages_to_generate; |
|
519 | + } |
|
520 | + |
|
521 | + |
|
522 | + /** |
|
523 | + * This accepts an array of EE_Message::MSG_ID values and will use that to retrieve the objects from the database |
|
524 | + * and send. |
|
525 | + * |
|
526 | + * @param array $message_ids |
|
527 | + * @throws EE_Error |
|
528 | + * @throws EE_Error |
|
529 | + */ |
|
530 | + public function setup_messages_from_ids_and_send($message_ids) |
|
531 | + { |
|
532 | + $this->_init_queue_and_generator(); |
|
533 | + $messages = EEM_Message::instance()->get_all(array( |
|
534 | + array( |
|
535 | + 'MSG_ID' => array( 'IN', $message_ids ), |
|
536 | + 'STS_ID' => array( |
|
537 | + 'IN', |
|
538 | + array_merge( |
|
539 | + EEM_Message::instance()->stati_indicating_sent(), |
|
540 | + array( EEM_Message::status_retry ) |
|
541 | + ), |
|
542 | + ), |
|
543 | + ), |
|
544 | + )); |
|
545 | + // set the Messages to resend. |
|
546 | + foreach ($messages as $message) { |
|
547 | + if ($message instanceof EE_Message) { |
|
548 | + $message->set_STS_ID(EEM_Message::status_resend); |
|
549 | + $this->_queue->add($message); |
|
550 | + } |
|
551 | + } |
|
552 | + |
|
553 | + $this->_queue->initiate_request_by_priority('send'); |
|
554 | + } |
|
555 | + |
|
556 | + |
|
557 | + /** |
|
558 | + * This method checks for registration IDs in the request via the given key and creates the messages to generate |
|
559 | + * objects from them, then returns the array of messages to generate objects. |
|
560 | + * Note, this sets up registrations for the registration family of message types. |
|
561 | + * |
|
562 | + * @param string $registration_ids_key This is used to indicate what represents the registration ids in the request. |
|
563 | + * |
|
564 | + * @return EE_Message_To_Generate[]|bool |
|
565 | + * @throws EE_Error |
|
566 | + */ |
|
567 | + public function setup_messages_to_generate_from_registration_ids_in_request($registration_ids_key = '_REG_ID') |
|
568 | + { |
|
569 | + /** @var RequestInterface $request */ |
|
570 | + $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
571 | + $regs_to_send = array(); |
|
572 | + $regIDs = $request->getRequestParam($registration_ids_key, [], 'int', true); |
|
573 | + if (empty($regIDs)) { |
|
574 | + EE_Error::add_error(esc_html__('Something went wrong because we\'re missing the registration ID', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
575 | + return false; |
|
576 | + } |
|
577 | + |
|
578 | + // make sure is an array |
|
579 | + $regIDs = is_array($regIDs) ? $regIDs : array( $regIDs ); |
|
580 | + |
|
581 | + foreach ($regIDs as $regID) { |
|
582 | + $reg = EEM_Registration::instance()->get_one_by_ID($regID); |
|
583 | + if (! $reg instanceof EE_Registration) { |
|
584 | + EE_Error::add_error(sprintf(esc_html__('Unable to retrieve a registration object for the given reg id (%s)', 'event_espresso'), $regID)); |
|
585 | + return false; |
|
586 | + } |
|
587 | + $regs_to_send[ $reg->transaction_ID() ][ $reg->status_ID() ][] = $reg; |
|
588 | + } |
|
589 | + |
|
590 | + $messages_to_generate = array(); |
|
591 | + |
|
592 | + foreach ($regs_to_send as $status_group) { |
|
593 | + foreach ($status_group as $status_id => $registrations) { |
|
594 | + $message_type = EEH_MSG_Template::convert_reg_status_to_message_type($status_id); |
|
595 | + if (! $message_type) { |
|
596 | + continue; |
|
597 | + } |
|
598 | + $messages_to_generate = array_merge( |
|
599 | + $messages_to_generate, |
|
600 | + $this->setup_mtgs_for_all_active_messengers( |
|
601 | + $message_type, |
|
602 | + array( $registrations, $status_id ) |
|
603 | + ) |
|
604 | + ); |
|
605 | + } |
|
606 | + } |
|
607 | + |
|
608 | + return $messages_to_generate; |
|
609 | + } |
|
610 | 610 | } |
@@ -12,304 +12,304 @@ |
||
12 | 12 | class EE_Message_To_Generate |
13 | 13 | { |
14 | 14 | |
15 | - /** |
|
16 | - * @type string name of EE_messenger |
|
17 | - */ |
|
18 | - protected $_messenger_name = null; |
|
19 | - |
|
20 | - /** |
|
21 | - * @type string name of EE_message_type |
|
22 | - */ |
|
23 | - protected $_message_type_name = null; |
|
24 | - |
|
25 | - /** |
|
26 | - * @type EE_messenger |
|
27 | - */ |
|
28 | - protected $_messenger = null; |
|
29 | - |
|
30 | - /** |
|
31 | - * @type EE_message_type |
|
32 | - */ |
|
33 | - protected $_message_type = null; |
|
34 | - |
|
35 | - /** |
|
36 | - * Identifier for the context the message is to be generated for. |
|
37 | - * @type string |
|
38 | - */ |
|
39 | - protected $_context = ''; |
|
40 | - |
|
41 | - /** |
|
42 | - * Data that will be used to generate message. |
|
43 | - * @type array |
|
44 | - */ |
|
45 | - protected $_data = array(); |
|
46 | - |
|
47 | - /** |
|
48 | - * Whether this message is for a preview or not. |
|
49 | - * @type bool |
|
50 | - */ |
|
51 | - protected $_preview = false; |
|
52 | - |
|
53 | - /** |
|
54 | - * @type EE_Message $_message |
|
55 | - */ |
|
56 | - protected $_message = null; |
|
57 | - |
|
58 | - /** |
|
59 | - * This is set by the constructor to indicate whether the incoming messenger |
|
60 | - * and message type are valid. This can then be checked by callers to determine whether |
|
61 | - * to generate this message or not. |
|
62 | - * @type bool |
|
63 | - */ |
|
64 | - protected $_valid = false; |
|
65 | - |
|
66 | - /** |
|
67 | - * If there are any errors (non exception errors) they get added to this array for callers to decide |
|
68 | - * how to handle. |
|
69 | - * @type array |
|
70 | - */ |
|
71 | - protected $_error_msg = array(); |
|
72 | - |
|
73 | - /** |
|
74 | - * Can be accessed via the send_now() method, this is set in the validation |
|
75 | - * routine via the EE_messenger::send_now() method. |
|
76 | - * @type bool |
|
77 | - */ |
|
78 | - protected $_send_now = false; |
|
79 | - |
|
80 | - /** |
|
81 | - * Holds the classname for the data handler used by the current message type. |
|
82 | - * This is set on the first call to the public `get_data_handler_class_name()` method. |
|
83 | - * @type string |
|
84 | - */ |
|
85 | - protected $_data_handler_class_name = ''; |
|
86 | - |
|
87 | - /** |
|
88 | - * one of the message status constants on EEM_Message |
|
89 | - * |
|
90 | - * @type string |
|
91 | - */ |
|
92 | - protected $_message_status = ''; |
|
93 | - |
|
94 | - |
|
95 | - |
|
96 | - /** |
|
97 | - * Constructor |
|
98 | - * |
|
99 | - * @param string $messenger_name Slug representing messenger |
|
100 | - * @param string $message_type_name Slug representing message type. |
|
101 | - * @param mixed $data Data used for generating message. |
|
102 | - * @param string $context Optional context to restrict message generated for. |
|
103 | - * @param bool $preview Whether this is being used to generate a preview or not. |
|
104 | - * @param string $status |
|
105 | - */ |
|
106 | - public function __construct( |
|
107 | - $messenger_name, |
|
108 | - $message_type_name, |
|
109 | - $data = array(), |
|
110 | - $context = '', |
|
111 | - $preview = false, |
|
112 | - $status = EEM_Message::status_incomplete |
|
113 | - ) { |
|
114 | - $this->_messenger_name = $messenger_name; |
|
115 | - $this->_message_type_name = $message_type_name; |
|
116 | - $this->_data = is_array($data) ? $data : array( $data ); |
|
117 | - $this->_context = $context; |
|
118 | - $this->_preview = $preview; |
|
119 | - $this->_status = $status; |
|
120 | - // attempt to generate message immediately |
|
121 | - $this->_message = $this->_generate_message(); |
|
122 | - } |
|
123 | - |
|
124 | - |
|
125 | - |
|
126 | - /** |
|
127 | - * @return string |
|
128 | - */ |
|
129 | - public function context() |
|
130 | - { |
|
131 | - return $this->_context; |
|
132 | - } |
|
133 | - |
|
134 | - |
|
135 | - |
|
136 | - /** |
|
137 | - * @return array |
|
138 | - */ |
|
139 | - public function data() |
|
140 | - { |
|
141 | - return $this->_data; |
|
142 | - } |
|
143 | - |
|
144 | - |
|
145 | - |
|
146 | - /** |
|
147 | - * @return EE_messenger |
|
148 | - */ |
|
149 | - public function messenger() |
|
150 | - { |
|
151 | - return $this->_messenger; |
|
152 | - } |
|
153 | - |
|
154 | - |
|
155 | - |
|
156 | - /** |
|
157 | - * @return EE_message_type |
|
158 | - */ |
|
159 | - public function message_type() |
|
160 | - { |
|
161 | - return $this->_message_type; |
|
162 | - } |
|
163 | - |
|
164 | - |
|
165 | - |
|
166 | - /** |
|
167 | - * @return boolean |
|
168 | - */ |
|
169 | - public function preview() |
|
170 | - { |
|
171 | - return $this->_preview; |
|
172 | - } |
|
173 | - |
|
174 | - |
|
175 | - |
|
176 | - /** |
|
177 | - * @param boolean $preview |
|
178 | - */ |
|
179 | - public function set_preview($preview) |
|
180 | - { |
|
181 | - $this->_preview = filter_var($preview, FILTER_VALIDATE_BOOLEAN); |
|
182 | - } |
|
183 | - |
|
184 | - |
|
185 | - |
|
186 | - /** |
|
187 | - * @return bool |
|
188 | - */ |
|
189 | - public function send_now() |
|
190 | - { |
|
191 | - return $this->_send_now; |
|
192 | - } |
|
193 | - |
|
194 | - |
|
195 | - |
|
196 | - /** |
|
197 | - * Simply returns the state of the $_valid property. |
|
198 | - * |
|
199 | - * @return bool |
|
200 | - */ |
|
201 | - public function valid() |
|
202 | - { |
|
203 | - return $this->_valid; |
|
204 | - } |
|
205 | - |
|
206 | - |
|
207 | - |
|
208 | - /** |
|
209 | - * generates an EE_Message using the supplied arguments and some defaults |
|
210 | - * |
|
211 | - * @param array $properties |
|
212 | - * @return string |
|
213 | - */ |
|
214 | - protected function _generate_message($properties = array()) |
|
215 | - { |
|
216 | - $message = EE_Message_Factory::create( |
|
217 | - array_merge( |
|
218 | - array( |
|
219 | - 'MSG_messenger' => $this->_messenger_name, |
|
220 | - 'MSG_message_type' => $this->_message_type_name, |
|
221 | - 'MSG_context' => $this->_context, |
|
222 | - 'STS_ID' => $this->_status, |
|
223 | - ), |
|
224 | - $properties |
|
225 | - ) |
|
226 | - ); |
|
227 | - // validate the message, and if it's good, set some properties |
|
228 | - try { |
|
229 | - $message->is_valid_for_sending_or_generation(true); |
|
230 | - $this->_valid = true; |
|
231 | - $this->_messenger = $message->messenger_object(); |
|
232 | - $this->_message_type = $message->message_type_object(); |
|
233 | - $this->_send_now = $message->send_now(); |
|
234 | - } catch (Exception $e) { |
|
235 | - $this->_valid = false; |
|
236 | - $this->_error_msg[] = $e->getMessage(); |
|
237 | - } |
|
238 | - return $message; |
|
239 | - } |
|
240 | - |
|
241 | - |
|
242 | - |
|
243 | - /** |
|
244 | - * Returns an instantiated EE_Message object from the internal data. |
|
245 | - * |
|
246 | - * @return EE_Message |
|
247 | - */ |
|
248 | - public function get_EE_Message() |
|
249 | - { |
|
250 | - // already set ? |
|
251 | - if ($this->_message instanceof EE_Message) { |
|
252 | - return $this->_message; |
|
253 | - } |
|
254 | - // no? then let's create one |
|
255 | - $this->_message = $this->_generate_message(); |
|
256 | - return $this->_message; |
|
257 | - } |
|
258 | - |
|
259 | - |
|
260 | - |
|
261 | - /** |
|
262 | - * This returns the data_handler class name for the internal message type set. |
|
263 | - * Note: this also verifies that the data handler class exists. If it doesn't then $_valid is set to false |
|
264 | - * and the data_handler_class name is set to an empty string. |
|
265 | - * |
|
266 | - * @param bool $preview Used to indicate that the preview data handler is to be returned. |
|
267 | - * @return string |
|
268 | - */ |
|
269 | - public function get_data_handler_class_name($preview = false) |
|
270 | - { |
|
271 | - if ($this->_data_handler_class_name === '' && $this->valid()) { |
|
272 | - $ref = $preview ? 'Preview' : $this->_message_type->get_data_handler($this->_data); |
|
273 | - // make sure internal data is updated. |
|
274 | - $this->_data = $this->_message_type->get_data(); |
|
275 | - |
|
276 | - // verify |
|
277 | - $this->_data_handler_class_name = EE_Message_To_Generate::verify_and_retrieve_class_name_for_data_handler_reference($ref); |
|
278 | - if ($this->_data_handler_class_name === '') { |
|
279 | - $this->_valid = false; |
|
280 | - } |
|
281 | - } |
|
282 | - return $this->_data_handler_class_name; |
|
283 | - } |
|
284 | - |
|
285 | - |
|
286 | - |
|
287 | - /** |
|
288 | - * Validates the given string as a reference for an existing, accessible data handler and returns the class name |
|
289 | - * For the handler the reference matches. |
|
290 | - * |
|
291 | - * @param string $data_handler_reference |
|
292 | - * @return string |
|
293 | - */ |
|
294 | - public static function verify_and_retrieve_class_name_for_data_handler_reference($data_handler_reference) |
|
295 | - { |
|
296 | - $class_name = 'EE_Messages_' . $data_handler_reference . '_incoming_data'; |
|
297 | - if (! class_exists($class_name)) { |
|
298 | - EE_Error::add_error( |
|
299 | - sprintf( |
|
300 | - esc_html__( |
|
301 | - 'The included data handler reference (%s) does not match any valid, accessible, "EE_Messages_incoming_data" classes. Looking for %s.', |
|
302 | - 'event_espresso' |
|
303 | - ), |
|
304 | - $data_handler_reference, |
|
305 | - $class_name |
|
306 | - ), |
|
307 | - __FILE__, |
|
308 | - __FUNCTION__, |
|
309 | - __LINE__ |
|
310 | - ); |
|
311 | - $class_name = ''; // clear out class_name so caller knows this isn't valid. |
|
312 | - } |
|
313 | - return $class_name; |
|
314 | - } |
|
15 | + /** |
|
16 | + * @type string name of EE_messenger |
|
17 | + */ |
|
18 | + protected $_messenger_name = null; |
|
19 | + |
|
20 | + /** |
|
21 | + * @type string name of EE_message_type |
|
22 | + */ |
|
23 | + protected $_message_type_name = null; |
|
24 | + |
|
25 | + /** |
|
26 | + * @type EE_messenger |
|
27 | + */ |
|
28 | + protected $_messenger = null; |
|
29 | + |
|
30 | + /** |
|
31 | + * @type EE_message_type |
|
32 | + */ |
|
33 | + protected $_message_type = null; |
|
34 | + |
|
35 | + /** |
|
36 | + * Identifier for the context the message is to be generated for. |
|
37 | + * @type string |
|
38 | + */ |
|
39 | + protected $_context = ''; |
|
40 | + |
|
41 | + /** |
|
42 | + * Data that will be used to generate message. |
|
43 | + * @type array |
|
44 | + */ |
|
45 | + protected $_data = array(); |
|
46 | + |
|
47 | + /** |
|
48 | + * Whether this message is for a preview or not. |
|
49 | + * @type bool |
|
50 | + */ |
|
51 | + protected $_preview = false; |
|
52 | + |
|
53 | + /** |
|
54 | + * @type EE_Message $_message |
|
55 | + */ |
|
56 | + protected $_message = null; |
|
57 | + |
|
58 | + /** |
|
59 | + * This is set by the constructor to indicate whether the incoming messenger |
|
60 | + * and message type are valid. This can then be checked by callers to determine whether |
|
61 | + * to generate this message or not. |
|
62 | + * @type bool |
|
63 | + */ |
|
64 | + protected $_valid = false; |
|
65 | + |
|
66 | + /** |
|
67 | + * If there are any errors (non exception errors) they get added to this array for callers to decide |
|
68 | + * how to handle. |
|
69 | + * @type array |
|
70 | + */ |
|
71 | + protected $_error_msg = array(); |
|
72 | + |
|
73 | + /** |
|
74 | + * Can be accessed via the send_now() method, this is set in the validation |
|
75 | + * routine via the EE_messenger::send_now() method. |
|
76 | + * @type bool |
|
77 | + */ |
|
78 | + protected $_send_now = false; |
|
79 | + |
|
80 | + /** |
|
81 | + * Holds the classname for the data handler used by the current message type. |
|
82 | + * This is set on the first call to the public `get_data_handler_class_name()` method. |
|
83 | + * @type string |
|
84 | + */ |
|
85 | + protected $_data_handler_class_name = ''; |
|
86 | + |
|
87 | + /** |
|
88 | + * one of the message status constants on EEM_Message |
|
89 | + * |
|
90 | + * @type string |
|
91 | + */ |
|
92 | + protected $_message_status = ''; |
|
93 | + |
|
94 | + |
|
95 | + |
|
96 | + /** |
|
97 | + * Constructor |
|
98 | + * |
|
99 | + * @param string $messenger_name Slug representing messenger |
|
100 | + * @param string $message_type_name Slug representing message type. |
|
101 | + * @param mixed $data Data used for generating message. |
|
102 | + * @param string $context Optional context to restrict message generated for. |
|
103 | + * @param bool $preview Whether this is being used to generate a preview or not. |
|
104 | + * @param string $status |
|
105 | + */ |
|
106 | + public function __construct( |
|
107 | + $messenger_name, |
|
108 | + $message_type_name, |
|
109 | + $data = array(), |
|
110 | + $context = '', |
|
111 | + $preview = false, |
|
112 | + $status = EEM_Message::status_incomplete |
|
113 | + ) { |
|
114 | + $this->_messenger_name = $messenger_name; |
|
115 | + $this->_message_type_name = $message_type_name; |
|
116 | + $this->_data = is_array($data) ? $data : array( $data ); |
|
117 | + $this->_context = $context; |
|
118 | + $this->_preview = $preview; |
|
119 | + $this->_status = $status; |
|
120 | + // attempt to generate message immediately |
|
121 | + $this->_message = $this->_generate_message(); |
|
122 | + } |
|
123 | + |
|
124 | + |
|
125 | + |
|
126 | + /** |
|
127 | + * @return string |
|
128 | + */ |
|
129 | + public function context() |
|
130 | + { |
|
131 | + return $this->_context; |
|
132 | + } |
|
133 | + |
|
134 | + |
|
135 | + |
|
136 | + /** |
|
137 | + * @return array |
|
138 | + */ |
|
139 | + public function data() |
|
140 | + { |
|
141 | + return $this->_data; |
|
142 | + } |
|
143 | + |
|
144 | + |
|
145 | + |
|
146 | + /** |
|
147 | + * @return EE_messenger |
|
148 | + */ |
|
149 | + public function messenger() |
|
150 | + { |
|
151 | + return $this->_messenger; |
|
152 | + } |
|
153 | + |
|
154 | + |
|
155 | + |
|
156 | + /** |
|
157 | + * @return EE_message_type |
|
158 | + */ |
|
159 | + public function message_type() |
|
160 | + { |
|
161 | + return $this->_message_type; |
|
162 | + } |
|
163 | + |
|
164 | + |
|
165 | + |
|
166 | + /** |
|
167 | + * @return boolean |
|
168 | + */ |
|
169 | + public function preview() |
|
170 | + { |
|
171 | + return $this->_preview; |
|
172 | + } |
|
173 | + |
|
174 | + |
|
175 | + |
|
176 | + /** |
|
177 | + * @param boolean $preview |
|
178 | + */ |
|
179 | + public function set_preview($preview) |
|
180 | + { |
|
181 | + $this->_preview = filter_var($preview, FILTER_VALIDATE_BOOLEAN); |
|
182 | + } |
|
183 | + |
|
184 | + |
|
185 | + |
|
186 | + /** |
|
187 | + * @return bool |
|
188 | + */ |
|
189 | + public function send_now() |
|
190 | + { |
|
191 | + return $this->_send_now; |
|
192 | + } |
|
193 | + |
|
194 | + |
|
195 | + |
|
196 | + /** |
|
197 | + * Simply returns the state of the $_valid property. |
|
198 | + * |
|
199 | + * @return bool |
|
200 | + */ |
|
201 | + public function valid() |
|
202 | + { |
|
203 | + return $this->_valid; |
|
204 | + } |
|
205 | + |
|
206 | + |
|
207 | + |
|
208 | + /** |
|
209 | + * generates an EE_Message using the supplied arguments and some defaults |
|
210 | + * |
|
211 | + * @param array $properties |
|
212 | + * @return string |
|
213 | + */ |
|
214 | + protected function _generate_message($properties = array()) |
|
215 | + { |
|
216 | + $message = EE_Message_Factory::create( |
|
217 | + array_merge( |
|
218 | + array( |
|
219 | + 'MSG_messenger' => $this->_messenger_name, |
|
220 | + 'MSG_message_type' => $this->_message_type_name, |
|
221 | + 'MSG_context' => $this->_context, |
|
222 | + 'STS_ID' => $this->_status, |
|
223 | + ), |
|
224 | + $properties |
|
225 | + ) |
|
226 | + ); |
|
227 | + // validate the message, and if it's good, set some properties |
|
228 | + try { |
|
229 | + $message->is_valid_for_sending_or_generation(true); |
|
230 | + $this->_valid = true; |
|
231 | + $this->_messenger = $message->messenger_object(); |
|
232 | + $this->_message_type = $message->message_type_object(); |
|
233 | + $this->_send_now = $message->send_now(); |
|
234 | + } catch (Exception $e) { |
|
235 | + $this->_valid = false; |
|
236 | + $this->_error_msg[] = $e->getMessage(); |
|
237 | + } |
|
238 | + return $message; |
|
239 | + } |
|
240 | + |
|
241 | + |
|
242 | + |
|
243 | + /** |
|
244 | + * Returns an instantiated EE_Message object from the internal data. |
|
245 | + * |
|
246 | + * @return EE_Message |
|
247 | + */ |
|
248 | + public function get_EE_Message() |
|
249 | + { |
|
250 | + // already set ? |
|
251 | + if ($this->_message instanceof EE_Message) { |
|
252 | + return $this->_message; |
|
253 | + } |
|
254 | + // no? then let's create one |
|
255 | + $this->_message = $this->_generate_message(); |
|
256 | + return $this->_message; |
|
257 | + } |
|
258 | + |
|
259 | + |
|
260 | + |
|
261 | + /** |
|
262 | + * This returns the data_handler class name for the internal message type set. |
|
263 | + * Note: this also verifies that the data handler class exists. If it doesn't then $_valid is set to false |
|
264 | + * and the data_handler_class name is set to an empty string. |
|
265 | + * |
|
266 | + * @param bool $preview Used to indicate that the preview data handler is to be returned. |
|
267 | + * @return string |
|
268 | + */ |
|
269 | + public function get_data_handler_class_name($preview = false) |
|
270 | + { |
|
271 | + if ($this->_data_handler_class_name === '' && $this->valid()) { |
|
272 | + $ref = $preview ? 'Preview' : $this->_message_type->get_data_handler($this->_data); |
|
273 | + // make sure internal data is updated. |
|
274 | + $this->_data = $this->_message_type->get_data(); |
|
275 | + |
|
276 | + // verify |
|
277 | + $this->_data_handler_class_name = EE_Message_To_Generate::verify_and_retrieve_class_name_for_data_handler_reference($ref); |
|
278 | + if ($this->_data_handler_class_name === '') { |
|
279 | + $this->_valid = false; |
|
280 | + } |
|
281 | + } |
|
282 | + return $this->_data_handler_class_name; |
|
283 | + } |
|
284 | + |
|
285 | + |
|
286 | + |
|
287 | + /** |
|
288 | + * Validates the given string as a reference for an existing, accessible data handler and returns the class name |
|
289 | + * For the handler the reference matches. |
|
290 | + * |
|
291 | + * @param string $data_handler_reference |
|
292 | + * @return string |
|
293 | + */ |
|
294 | + public static function verify_and_retrieve_class_name_for_data_handler_reference($data_handler_reference) |
|
295 | + { |
|
296 | + $class_name = 'EE_Messages_' . $data_handler_reference . '_incoming_data'; |
|
297 | + if (! class_exists($class_name)) { |
|
298 | + EE_Error::add_error( |
|
299 | + sprintf( |
|
300 | + esc_html__( |
|
301 | + 'The included data handler reference (%s) does not match any valid, accessible, "EE_Messages_incoming_data" classes. Looking for %s.', |
|
302 | + 'event_espresso' |
|
303 | + ), |
|
304 | + $data_handler_reference, |
|
305 | + $class_name |
|
306 | + ), |
|
307 | + __FILE__, |
|
308 | + __FUNCTION__, |
|
309 | + __LINE__ |
|
310 | + ); |
|
311 | + $class_name = ''; // clear out class_name so caller knows this isn't valid. |
|
312 | + } |
|
313 | + return $class_name; |
|
314 | + } |
|
315 | 315 | } |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | ) { |
114 | 114 | $this->_messenger_name = $messenger_name; |
115 | 115 | $this->_message_type_name = $message_type_name; |
116 | - $this->_data = is_array($data) ? $data : array( $data ); |
|
116 | + $this->_data = is_array($data) ? $data : array($data); |
|
117 | 117 | $this->_context = $context; |
118 | 118 | $this->_preview = $preview; |
119 | 119 | $this->_status = $status; |
@@ -293,8 +293,8 @@ discard block |
||
293 | 293 | */ |
294 | 294 | public static function verify_and_retrieve_class_name_for_data_handler_reference($data_handler_reference) |
295 | 295 | { |
296 | - $class_name = 'EE_Messages_' . $data_handler_reference . '_incoming_data'; |
|
297 | - if (! class_exists($class_name)) { |
|
296 | + $class_name = 'EE_Messages_'.$data_handler_reference.'_incoming_data'; |
|
297 | + if ( ! class_exists($class_name)) { |
|
298 | 298 | EE_Error::add_error( |
299 | 299 | sprintf( |
300 | 300 | esc_html__( |
@@ -20,113 +20,113 @@ |
||
20 | 20 | { |
21 | 21 | |
22 | 22 | |
23 | - /** |
|
24 | - * This messenger is used to send the generated message. |
|
25 | - * |
|
26 | - * @type EE_messenger |
|
27 | - */ |
|
28 | - protected $_sending_messenger = ''; |
|
29 | - |
|
30 | - |
|
31 | - /** |
|
32 | - * Holds the token from the request. |
|
33 | - * |
|
34 | - * @type string |
|
35 | - */ |
|
36 | - public $token = ''; |
|
37 | - |
|
38 | - |
|
39 | - /** |
|
40 | - * Constructor |
|
41 | - * This instantiates the object using arguments from the given request and calling the parent constructor. |
|
42 | - * |
|
43 | - * @param EE_Message_Resource_Manager $message_resource_manager |
|
44 | - * @param RequestInterface $request |
|
45 | - * @throws EE_Error |
|
46 | - */ |
|
47 | - public function __construct(EE_Message_Resource_Manager $message_resource_manager, RequestInterface $request) |
|
48 | - { |
|
49 | - parent::__construct( |
|
50 | - $request->getRequestParam('gen_msgr'), |
|
51 | - $request->getRequestParam('message_type'), |
|
52 | - [], |
|
53 | - $request->getRequestParam('context') |
|
54 | - ); |
|
55 | - if (! $this->valid()) { |
|
56 | - return; |
|
57 | - } |
|
58 | - $this->token = $request->getRequestParam('token'); |
|
59 | - $this->_sending_messenger = $message_resource_manager->get_active_messenger( |
|
60 | - $request->getRequestParam('snd_msgr') |
|
61 | - ); |
|
62 | - $this->_validate_request(); |
|
63 | - $this->_data = $this->_get_data_from_request($request->getRequestParam('id')); |
|
64 | - } |
|
65 | - |
|
66 | - |
|
67 | - /** |
|
68 | - * @return EE_messenger |
|
69 | - */ |
|
70 | - public function sending_messenger() |
|
71 | - { |
|
72 | - return $this->_sending_messenger; |
|
73 | - } |
|
74 | - |
|
75 | - |
|
76 | - /** |
|
77 | - * This validates set properties from the incoming request. |
|
78 | - * |
|
79 | - * @throws EE_Error |
|
80 | - */ |
|
81 | - protected function _validate_request() |
|
82 | - { |
|
83 | - if ( |
|
84 | - ! $this->_sending_messenger instanceof EE_messenger |
|
85 | - || ! $this->_messenger instanceof EE_messenger |
|
86 | - || ! $this->_message_type instanceof EE_message_type |
|
87 | - || empty($this->_context) |
|
88 | - || empty($this->token) |
|
89 | - ) { |
|
90 | - throw new EE_Error(esc_html__( |
|
91 | - 'The request for the "msg_url_trigger" route has a malformed url.', |
|
92 | - 'event_espresso' |
|
93 | - )); |
|
94 | - } |
|
95 | - } |
|
96 | - |
|
97 | - |
|
98 | - /** |
|
99 | - * This returns the data property according to what is expected from the request. |
|
100 | - * |
|
101 | - * @param $id |
|
102 | - * @return mixed (whatever the data is returned from the message type). |
|
103 | - * @throws EE_Error |
|
104 | - */ |
|
105 | - protected function _get_data_from_request($id) |
|
106 | - { |
|
107 | - // get the EE_Registration from the token |
|
108 | - /** @type EE_Registration $registration */ |
|
109 | - $registration = EEM_Registration::instance()->get_one([['REG_url_link' => $this->token]]); |
|
110 | - // if no registration then bail early. |
|
111 | - if (! $registration instanceof EE_Registration) { |
|
112 | - throw new EE_Error(esc_html__('Unable to complete the request because the token is invalid.', 'event_espresso')); |
|
113 | - } |
|
114 | - |
|
115 | - return $this->_get_data_to_use($registration, $id); |
|
116 | - } |
|
117 | - |
|
118 | - |
|
119 | - /** |
|
120 | - * This uses the set message type to retrieve the data in the correct format as it came from the url. |
|
121 | - * |
|
122 | - * @param EE_Registration $registration |
|
123 | - * @param int $data_id This is sometimes used for secondary data a message type requires. |
|
124 | - * @return mixed Data prepared as needed for generating this message. |
|
125 | - * @throws EE_Error |
|
126 | - */ |
|
127 | - protected function _get_data_to_use($registration, $data_id) |
|
128 | - { |
|
129 | - // use incoming data from url to setup data for the message type requirements |
|
130 | - return $this->_message_type->get_data_for_context($this->_context, $registration, $data_id); |
|
131 | - } |
|
23 | + /** |
|
24 | + * This messenger is used to send the generated message. |
|
25 | + * |
|
26 | + * @type EE_messenger |
|
27 | + */ |
|
28 | + protected $_sending_messenger = ''; |
|
29 | + |
|
30 | + |
|
31 | + /** |
|
32 | + * Holds the token from the request. |
|
33 | + * |
|
34 | + * @type string |
|
35 | + */ |
|
36 | + public $token = ''; |
|
37 | + |
|
38 | + |
|
39 | + /** |
|
40 | + * Constructor |
|
41 | + * This instantiates the object using arguments from the given request and calling the parent constructor. |
|
42 | + * |
|
43 | + * @param EE_Message_Resource_Manager $message_resource_manager |
|
44 | + * @param RequestInterface $request |
|
45 | + * @throws EE_Error |
|
46 | + */ |
|
47 | + public function __construct(EE_Message_Resource_Manager $message_resource_manager, RequestInterface $request) |
|
48 | + { |
|
49 | + parent::__construct( |
|
50 | + $request->getRequestParam('gen_msgr'), |
|
51 | + $request->getRequestParam('message_type'), |
|
52 | + [], |
|
53 | + $request->getRequestParam('context') |
|
54 | + ); |
|
55 | + if (! $this->valid()) { |
|
56 | + return; |
|
57 | + } |
|
58 | + $this->token = $request->getRequestParam('token'); |
|
59 | + $this->_sending_messenger = $message_resource_manager->get_active_messenger( |
|
60 | + $request->getRequestParam('snd_msgr') |
|
61 | + ); |
|
62 | + $this->_validate_request(); |
|
63 | + $this->_data = $this->_get_data_from_request($request->getRequestParam('id')); |
|
64 | + } |
|
65 | + |
|
66 | + |
|
67 | + /** |
|
68 | + * @return EE_messenger |
|
69 | + */ |
|
70 | + public function sending_messenger() |
|
71 | + { |
|
72 | + return $this->_sending_messenger; |
|
73 | + } |
|
74 | + |
|
75 | + |
|
76 | + /** |
|
77 | + * This validates set properties from the incoming request. |
|
78 | + * |
|
79 | + * @throws EE_Error |
|
80 | + */ |
|
81 | + protected function _validate_request() |
|
82 | + { |
|
83 | + if ( |
|
84 | + ! $this->_sending_messenger instanceof EE_messenger |
|
85 | + || ! $this->_messenger instanceof EE_messenger |
|
86 | + || ! $this->_message_type instanceof EE_message_type |
|
87 | + || empty($this->_context) |
|
88 | + || empty($this->token) |
|
89 | + ) { |
|
90 | + throw new EE_Error(esc_html__( |
|
91 | + 'The request for the "msg_url_trigger" route has a malformed url.', |
|
92 | + 'event_espresso' |
|
93 | + )); |
|
94 | + } |
|
95 | + } |
|
96 | + |
|
97 | + |
|
98 | + /** |
|
99 | + * This returns the data property according to what is expected from the request. |
|
100 | + * |
|
101 | + * @param $id |
|
102 | + * @return mixed (whatever the data is returned from the message type). |
|
103 | + * @throws EE_Error |
|
104 | + */ |
|
105 | + protected function _get_data_from_request($id) |
|
106 | + { |
|
107 | + // get the EE_Registration from the token |
|
108 | + /** @type EE_Registration $registration */ |
|
109 | + $registration = EEM_Registration::instance()->get_one([['REG_url_link' => $this->token]]); |
|
110 | + // if no registration then bail early. |
|
111 | + if (! $registration instanceof EE_Registration) { |
|
112 | + throw new EE_Error(esc_html__('Unable to complete the request because the token is invalid.', 'event_espresso')); |
|
113 | + } |
|
114 | + |
|
115 | + return $this->_get_data_to_use($registration, $id); |
|
116 | + } |
|
117 | + |
|
118 | + |
|
119 | + /** |
|
120 | + * This uses the set message type to retrieve the data in the correct format as it came from the url. |
|
121 | + * |
|
122 | + * @param EE_Registration $registration |
|
123 | + * @param int $data_id This is sometimes used for secondary data a message type requires. |
|
124 | + * @return mixed Data prepared as needed for generating this message. |
|
125 | + * @throws EE_Error |
|
126 | + */ |
|
127 | + protected function _get_data_to_use($registration, $data_id) |
|
128 | + { |
|
129 | + // use incoming data from url to setup data for the message type requirements |
|
130 | + return $this->_message_type->get_data_for_context($this->_context, $registration, $data_id); |
|
131 | + } |
|
132 | 132 | } |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | [], |
53 | 53 | $request->getRequestParam('context') |
54 | 54 | ); |
55 | - if (! $this->valid()) { |
|
55 | + if ( ! $this->valid()) { |
|
56 | 56 | return; |
57 | 57 | } |
58 | 58 | $this->token = $request->getRequestParam('token'); |
@@ -108,7 +108,7 @@ discard block |
||
108 | 108 | /** @type EE_Registration $registration */ |
109 | 109 | $registration = EEM_Registration::instance()->get_one([['REG_url_link' => $this->token]]); |
110 | 110 | // if no registration then bail early. |
111 | - if (! $registration instanceof EE_Registration) { |
|
111 | + if ( ! $registration instanceof EE_Registration) { |
|
112 | 112 | throw new EE_Error(esc_html__('Unable to complete the request because the token is invalid.', 'event_espresso')); |
113 | 113 | } |
114 | 114 |
@@ -17,915 +17,915 @@ |
||
17 | 17 | { |
18 | 18 | |
19 | 19 | |
20 | - /** |
|
21 | - * message type child classes will set what contexts are associated with the message type via this array. |
|
22 | - * format: |
|
23 | - * array( |
|
24 | - * 'context' => array( |
|
25 | - * 'label' => esc_html__('Context Label', 'event_espresso'), |
|
26 | - * 'description' => esc_html__('Context description (for help popups)', 'event_espresso') |
|
27 | - * )); |
|
28 | - * |
|
29 | - * @var array |
|
30 | - */ |
|
31 | - protected $_contexts = array(); |
|
32 | - |
|
33 | - |
|
34 | - /** |
|
35 | - * This property is used to define what the display label will be for contexts (eg. "Recipients", "Themes" etc.) |
|
36 | - * Format: |
|
37 | - * array( 'label' => 'something', 'plural' => 'somethings', 'description' => 'something' ); |
|
38 | - * |
|
39 | - * @var array |
|
40 | - */ |
|
41 | - protected $_context_label; |
|
42 | - |
|
43 | - |
|
44 | - /** MESSAGE ASSEMBLING PROPERTIES **/ |
|
45 | - /** |
|
46 | - * This parameter simply holds all the message objects for retrieval by the controller and sending to the messenger. |
|
47 | - * |
|
48 | - * @var array of message objects. |
|
49 | - */ |
|
50 | - public $messages = array(); |
|
51 | - |
|
52 | - /** |
|
53 | - * The following holds the templates that will be used to assemble the message object for the messenger. |
|
54 | - * |
|
55 | - * @var array |
|
56 | - */ |
|
57 | - protected $_templates; |
|
58 | - |
|
59 | - |
|
60 | - /** |
|
61 | - * If a specific template is being parsed, this will hold the message template group GRP_ID for that template. |
|
62 | - * |
|
63 | - * @var int. |
|
64 | - */ |
|
65 | - protected $_GRP_ID; |
|
66 | - |
|
67 | - |
|
68 | - /** OTHER INFO PROPERTIES **/ |
|
69 | - /** |
|
70 | - * This will hold the count of the message objects in the messages array. This could be used for determining if |
|
71 | - * batching/queueing is needed. |
|
72 | - * |
|
73 | - * @var int |
|
74 | - */ |
|
75 | - public $count = 0; |
|
76 | - |
|
77 | - |
|
78 | - /** |
|
79 | - * This is set via the `do_messenger_hooks` method and contains the messenger being used to send the message of |
|
80 | - * this message type at time of sending. |
|
81 | - * |
|
82 | - * @var EE_messenger |
|
83 | - */ |
|
84 | - protected $_active_messenger; |
|
85 | - |
|
86 | - |
|
87 | - /** |
|
88 | - * This will hold the shortcode_replace instance for handling replacement of shortcodes in the various templates |
|
89 | - * |
|
90 | - * @var object |
|
91 | - */ |
|
92 | - protected $_shortcode_replace; |
|
93 | - |
|
94 | - |
|
95 | - /** |
|
96 | - * The purpose for this property is to simply allow message types to indicate if the message generated is intended |
|
97 | - * for only single context. Child message types should redefine this variable (if necessary) in the |
|
98 | - * _set_data_Handler() method. |
|
99 | - * |
|
100 | - * @var boolean |
|
101 | - */ |
|
102 | - protected $_single_message = false; |
|
103 | - |
|
104 | - |
|
105 | - /** |
|
106 | - * This will hold an array of specific reg_ids that are receiving messages. |
|
107 | - * |
|
108 | - * @since 4.7.x |
|
109 | - * @var array |
|
110 | - */ |
|
111 | - protected $_regs_for_sending = array(); |
|
112 | - |
|
113 | - |
|
114 | - /** |
|
115 | - * This holds the data passed to this class from the controller and also the final processed data. |
|
116 | - * |
|
117 | - * @var object |
|
118 | - */ |
|
119 | - protected $_data; |
|
120 | - |
|
121 | - |
|
122 | - /** |
|
123 | - * this is just a flag indicating whether we're in preview mode or not. |
|
124 | - * |
|
125 | - * @var bool |
|
126 | - */ |
|
127 | - protected $_preview = false; |
|
128 | - |
|
129 | - |
|
130 | - /** |
|
131 | - * This just holds defaults for addressee data that children merge with their data array setup |
|
132 | - * |
|
133 | - * @var array |
|
134 | - */ |
|
135 | - protected $_default_addressee_data; |
|
136 | - |
|
137 | - |
|
138 | - /** |
|
139 | - * Child classes declare through this property what handler they want to use for the incoming data and this string |
|
140 | - * is used to instantiate the EE_Messages_incoming_data child class for that handler. |
|
141 | - * |
|
142 | - * @var string |
|
143 | - */ |
|
144 | - protected $_data_handler; |
|
145 | - |
|
146 | - |
|
147 | - /** |
|
148 | - * This holds any specific fields for holding any settings related to a message type (if any needed) |
|
149 | - * |
|
150 | - * @var array |
|
151 | - */ |
|
152 | - protected $_admin_settings_fields = array(); |
|
153 | - |
|
154 | - /** |
|
155 | - * this property will hold any existing setting that may have been set in the admin. |
|
156 | - * |
|
157 | - * @var array |
|
158 | - */ |
|
159 | - protected $_existing_admin_settings = array(); |
|
160 | - |
|
161 | - |
|
162 | - /** |
|
163 | - * This is used to designate the generating and alternative sending messengers for a message type. It is set via |
|
164 | - * set_with_messengers() on construct. Note, generating messenger also acts as a sending messenger for this message |
|
165 | - * type. However ONLY the generating messengers are used for creating templates for this message type. Should be |
|
166 | - * in this format: |
|
167 | - * { |
|
168 | - * |
|
169 | - * @type string $generating_messenger the name of the generating messenger. Generating |
|
170 | - * messengers are used for generating templates, |
|
171 | - * doing validation and defining valid shortcodes. |
|
172 | - * { |
|
173 | - * @type string $sending_messenger values are the name(s) for the sending |
|
174 | - * messengers. sending messengers are |
|
175 | - * just valid delivery vehicles that will utilize |
|
176 | - * the templates (and generated EE_message |
|
177 | - * objects from the generating messengers). |
|
178 | - * } |
|
179 | - * } |
|
180 | - * @since 4.5.0 |
|
181 | - * @var array |
|
182 | - */ |
|
183 | - protected $_with_messengers = array(); |
|
184 | - |
|
185 | - |
|
186 | - /** |
|
187 | - * This holds the addressees in an array indexed by context for later retrieval when assembling the message objects. |
|
188 | - * |
|
189 | - * @access protected |
|
190 | - * @var array |
|
191 | - */ |
|
192 | - protected $_addressees = array(); |
|
193 | - |
|
194 | - |
|
195 | - /** |
|
196 | - * This allows each message type to set what alternate messenger&message type combination can be used for fallback |
|
197 | - * default templates if there are no specific ones defined for this messenger and message type. Should be in the |
|
198 | - * format: |
|
199 | - * array( |
|
200 | - * 'messenger' => 'message_type', |
|
201 | - * 'another_messenger' => another_message_type |
|
202 | - * ); |
|
203 | - * This is set in the message type constructor. |
|
204 | - * |
|
205 | - * @var array |
|
206 | - */ |
|
207 | - protected $_master_templates = array(); |
|
208 | - |
|
209 | - |
|
210 | - /** |
|
211 | - * This holds whatever the set template pack is for a message template group when generating messages. |
|
212 | - * |
|
213 | - * @since 4.5.0 |
|
214 | - * @var EE_Messages_Template_Pack |
|
215 | - */ |
|
216 | - protected $_template_pack; |
|
217 | - |
|
218 | - |
|
219 | - /** |
|
220 | - * This holds whatever the set variation is for a message template group when generating messages. |
|
221 | - * |
|
222 | - * @since 4.5.0 |
|
223 | - * @var string |
|
224 | - */ |
|
225 | - protected $_variation; |
|
226 | - |
|
227 | - |
|
228 | - /** |
|
229 | - * EE_message_type constructor. |
|
230 | - */ |
|
231 | - public function __construct() |
|
232 | - { |
|
233 | - $this->_messages_item_type = 'message_type'; |
|
234 | - $this->_set_contexts(); |
|
235 | - $this->_set_with_messengers(); |
|
236 | - parent::__construct(); |
|
237 | - } |
|
238 | - |
|
239 | - |
|
240 | - /** |
|
241 | - * This sets the data handler for the message type. It must be used to define the _data_handler property. It is |
|
242 | - * called when messages are setup. |
|
243 | - * |
|
244 | - * @abstract |
|
245 | - * @access protected |
|
246 | - * @return void |
|
247 | - */ |
|
248 | - abstract protected function _set_data_handler(); |
|
249 | - |
|
250 | - |
|
251 | - /** |
|
252 | - * This method should return a EE_Base_Class object (or array of EE_Base_Class objects) for the given context and |
|
253 | - * ID (which should be the primary key id for the base class). Client code doesn't have to know what a message |
|
254 | - * type's data handler is. |
|
255 | - * |
|
256 | - * @since 4.5.0 |
|
257 | - * @param string $context This should be a string matching a valid context for the message type. |
|
258 | - * @param EE_Registration $registration Need a registration to ensure that the data is valid (prevents people |
|
259 | - * guessing a url). |
|
260 | - * @param int $id Optional. Integer corresponding to the value for the primary key of a |
|
261 | - * EE_Base_Class_Object |
|
262 | - * @return mixed ( EE_Base_Class||EE_Base_Class[] ) |
|
263 | - */ |
|
264 | - abstract protected function _get_data_for_context($context, EE_Registration $registration, $id); |
|
265 | - |
|
266 | - |
|
267 | - /** |
|
268 | - * _set_contexts |
|
269 | - * This sets up the contexts associated with the message_type |
|
270 | - * |
|
271 | - * @abstract |
|
272 | - * @access protected |
|
273 | - * @return void |
|
274 | - */ |
|
275 | - abstract protected function _set_contexts(); |
|
276 | - |
|
277 | - |
|
278 | - /** |
|
279 | - * This is used to get the "id" value fo the msg_trigger_url generated by get_url_trigger(). |
|
280 | - * In most cases, child classes don't need anything, (hence the default of 0), however if there is a specific |
|
281 | - * EE_Base_Class that is required in generating a message for a message type recipient then the message |
|
282 | - * type should override this method and use the given params to generate the correct ID. |
|
283 | - * |
|
284 | - * @param string $context The message type context. |
|
285 | - * @param EE_Registration $registration Registration object |
|
286 | - * @deprecated 4.9.0 |
|
287 | - * @return int |
|
288 | - */ |
|
289 | - protected function _get_id_for_msg_url($context, EE_Registration $registration) |
|
290 | - { |
|
291 | - return 0; |
|
292 | - } |
|
293 | - |
|
294 | - |
|
295 | - /** |
|
296 | - * This sets up any action/filter hooks this message type puts in place for a specific messenger. Note that by |
|
297 | - * default this does nothing. Child classes will need to override if they want to add specific hooks for a |
|
298 | - * messenger. |
|
299 | - * |
|
300 | - * @since 1.0.0 |
|
301 | - * @return void |
|
302 | - */ |
|
303 | - protected function _do_messenger_hooks() |
|
304 | - { |
|
305 | - return; |
|
306 | - } |
|
307 | - |
|
308 | - |
|
309 | - /** |
|
310 | - * This is a public wrapper for the protected _do_messenger_hooks() method. |
|
311 | - * For backward compat reasons, this was done rather than making the protected method public. |
|
312 | - * |
|
313 | - * @param EE_messenger $messenger This is used to set the $_active_messenger property, so message types are able |
|
314 | - * to know what messenger is being used to send the message at the time of |
|
315 | - * sending. |
|
316 | - * @since 4.9.0 |
|
317 | - */ |
|
318 | - public function do_messenger_hooks($messenger = null) |
|
319 | - { |
|
320 | - $this->_active_messenger = $messenger; |
|
321 | - $this->_do_messenger_hooks(); |
|
322 | - } |
|
323 | - |
|
324 | - |
|
325 | - /** |
|
326 | - * This method returns whether this message type should always generate a new copy |
|
327 | - * when requested, or if links can be to the already generated copy. |
|
328 | - * Note: this does NOT affect viewing/resending already generated messages in the EE_Message list table. |
|
329 | - * Child classes should override this if different from the default of false. |
|
330 | - * |
|
331 | - * @return bool false means can link to generated EE_Message. true must regenerate. |
|
332 | - */ |
|
333 | - public function always_generate() |
|
334 | - { |
|
335 | - return false; |
|
336 | - } |
|
337 | - |
|
338 | - |
|
339 | - /** |
|
340 | - * Returns the priority for the message type. |
|
341 | - * Priorities are defined as constants on EEM_Message. Currently there are three priorities: |
|
342 | - * - EEM_Message::priority_high |
|
343 | - * - EEM_Message::priority_medium |
|
344 | - * - EEM_Message::priority_low |
|
345 | - * Priority is used to determine the weight the message type is given when queuing for generation and/or sending. |
|
346 | - * |
|
347 | - * @see EEM_Message for more phpdocs on priority. |
|
348 | - * The default priority for all message_types is EEM_Message::priority_low. Message Types wanting to give |
|
349 | - * a higher priority must override this method. Also note, messengers are able to override priorities |
|
350 | - * queuing instructions if their "send_now" flag is set to true. An example of this is the HTML messenger |
|
351 | - * which displays things in the browser. |
|
352 | - * @since 4.9.0 |
|
353 | - * @return int |
|
354 | - */ |
|
355 | - public function get_priority() |
|
356 | - { |
|
357 | - return EEM_Message::priority_low; |
|
358 | - } |
|
359 | - |
|
360 | - |
|
361 | - /** |
|
362 | - * This runs the _set_data_handler() method for message types and then returns what got set. |
|
363 | - * |
|
364 | - * @param mixed $data This sets the data property for the message type with the incoming data used for generating. |
|
365 | - * @return string (the reference for the data handler) (will be an empty string if could not be determined). |
|
366 | - */ |
|
367 | - public function get_data_handler($data) |
|
368 | - { |
|
369 | - $this->_data = $data; |
|
370 | - $this->_set_data_handler(); |
|
371 | - return apply_filters('FHEE__EE_message_type__get_data_handler', $this->_data_handler, $this); |
|
372 | - } |
|
373 | - |
|
374 | - |
|
375 | - /** |
|
376 | - * This is called externally to reset the value of the $_data property for the message type. |
|
377 | - * Please note the value of the _data is highly volatile. It was added as an interim measure ensuring |
|
378 | - * EE_Message_To_Generate objects have any changes to the _data property when `_set_data_handler` method is called |
|
379 | - * (and for back compat reasons). This particular method is used in |
|
380 | - * EE_Messages_Generator::_reset_current_properties to ensure that the internal _data on the message type is |
|
381 | - * cleaned before subsequent EE_Message generation in the same request. |
|
382 | - * |
|
383 | - * @todo This needs refactored along with the whole _set_data_handler() method in EE_message_types. Need to |
|
384 | - * ensure that there is no manipulation of the _data property during run time so there's a clear |
|
385 | - * expectation of what it is. Likely we need to ensure that _data is not persisted IN the message type |
|
386 | - * at all. |
|
387 | - * @internal Plugin authors, do not implement this method, it is subject to change. |
|
388 | - * @since 4.9 |
|
389 | - */ |
|
390 | - public function reset_data() |
|
391 | - { |
|
392 | - $this->_data = null; |
|
393 | - } |
|
394 | - |
|
395 | - |
|
396 | - /** |
|
397 | - * This does some validation of incoming params gets the url trigger from the defined method in the specific child |
|
398 | - * class and then filters the results. |
|
399 | - * |
|
400 | - * @param string $context The message type context |
|
401 | - * @param string $sending_messenger The sending messenger |
|
402 | - * @param EE_Registration $registration Registration object |
|
403 | - * @throws EE_Error |
|
404 | - * @deprecated 4.9.0 Likely 4.9.10 or 4.10.0 will remove this method completely |
|
405 | - * @return string generated url |
|
406 | - */ |
|
407 | - public function get_url_trigger($context, $sending_messenger, EE_Registration $registration) |
|
408 | - { |
|
409 | - // validate context |
|
410 | - // valid context? |
|
411 | - if (! isset($this->_contexts[ $context ])) { |
|
412 | - throw new EE_Error( |
|
413 | - sprintf( |
|
414 | - esc_html__('The context %s is not a valid context for %s.', 'event_espresso'), |
|
415 | - $context, |
|
416 | - get_class($this) |
|
417 | - ) |
|
418 | - ); |
|
419 | - } |
|
420 | - // valid sending_messenger? |
|
421 | - $not_valid_msgr = false; |
|
422 | - foreach ($this->_with_messengers as $generating => $sendings) { |
|
423 | - if (empty($sendings) || array_search($sending_messenger, $sendings) === false) { |
|
424 | - $not_valid_msgr = true; |
|
425 | - } |
|
426 | - } |
|
427 | - if ($not_valid_msgr) { |
|
428 | - throw new EE_Error( |
|
429 | - sprintf( |
|
430 | - esc_html__( |
|
431 | - 'The given sending messenger string (%s) does not match a valid sending messenger with the %s. If this is incorrect, make sure that the message type has defined this messenger as a sending messenger in its $_with_messengers array.', |
|
432 | - 'event_espresso' |
|
433 | - ), |
|
434 | - $sending_messenger, |
|
435 | - get_class($this) |
|
436 | - ) |
|
437 | - ); |
|
438 | - } |
|
439 | - return EEH_MSG_Template::generate_url_trigger( |
|
440 | - $sending_messenger, |
|
441 | - $this->_active_messenger->name, |
|
442 | - $context, |
|
443 | - $this->name, |
|
444 | - $registration, |
|
445 | - $this->_GRP_ID, |
|
446 | - $this->_get_id_for_msg_url($context, $registration) |
|
447 | - ); |
|
448 | - } |
|
449 | - |
|
450 | - |
|
451 | - /** |
|
452 | - * Wrapper for _get_data_for_context() that handles some validation before calling the main class and also allows |
|
453 | - * for filtering. This is (currently) called by the EED_Messages module. |
|
454 | - * |
|
455 | - * @since 4.5.0 |
|
456 | - * @throws EE_Error |
|
457 | - * @param string $context This should be a string matching a valid context for the message type. |
|
458 | - * @param EE_Registration $registration Need a registration to ensure that the data is valid (prevents people |
|
459 | - * guessing a url). |
|
460 | - * @param int $id Optional. Integer corresponding to the value for the primary key of a |
|
461 | - * EE_Base_Class_Object |
|
462 | - * @return mixed (EE_Base_Class||EE_Base_Class[]) |
|
463 | - */ |
|
464 | - public function get_data_for_context($context, EE_Registration $registration, $id = 0) |
|
465 | - { |
|
466 | - // valid context? |
|
467 | - if (! isset($this->_contexts[ $context ])) { |
|
468 | - throw new EE_Error( |
|
469 | - sprintf( |
|
470 | - esc_html__('The context %s is not a valid context for %s.', 'event_espresso'), |
|
471 | - $context, |
|
472 | - get_class($this) |
|
473 | - ) |
|
474 | - ); |
|
475 | - } |
|
476 | - // get data and apply global and class specific filters on it. |
|
477 | - $data = apply_filters( |
|
478 | - 'FHEE__EE_message_type__get_data_for_context__data', |
|
479 | - $this->_get_data_for_context($context, $registration, $id), |
|
480 | - $this |
|
481 | - ); |
|
482 | - $data = apply_filters('FHEE__' . get_class($this) . '__get_data_for_context__data', $data, $this); |
|
483 | - // if empty then something went wrong! |
|
484 | - if (empty($data)) { |
|
485 | - throw new EE_Error( |
|
486 | - sprintf( |
|
487 | - esc_html__( |
|
488 | - 'There is no data retrieved, it is possible that the id given (%d) does not match any value in the database for the corresponding EE_Base_Class used by the data handler for the %s message type.', |
|
489 | - 'event_espresso' |
|
490 | - ), |
|
491 | - $id, |
|
492 | - $this->name |
|
493 | - ) |
|
494 | - ); |
|
495 | - } |
|
496 | - return $data; |
|
497 | - } |
|
498 | - |
|
499 | - |
|
500 | - /** |
|
501 | - * This returns the contents of the _data property. |
|
502 | - * Please note the value of the _data is highly volatile. It was added as an interim measure ensuring |
|
503 | - * EE_Message_To_Generate objects have any changes to the _data property when `_set_data_handler` method is called. |
|
504 | - * |
|
505 | - * @todo This needs refactored along with the whole _set_data_handler() method in EE_message_types. Need to |
|
506 | - * ensure that there is no manipulation of the _data property during run time so there's a clear |
|
507 | - * expectation of what it is. |
|
508 | - * @internal Plugin authors, do not implement this method, it is subject to change. |
|
509 | - * @return mixed |
|
510 | - */ |
|
511 | - public function get_data() |
|
512 | - { |
|
513 | - return $this->_data; |
|
514 | - } |
|
515 | - |
|
516 | - |
|
517 | - /** |
|
518 | - * used to set the $_with_messengers property. (this is a default, child classes SHOULD override) |
|
519 | - * |
|
520 | - * @see property definition for description of setup. |
|
521 | - * @since 4.5.0 |
|
522 | - * @abstract |
|
523 | - * @return void |
|
524 | - */ |
|
525 | - protected function _set_with_messengers() |
|
526 | - { |
|
527 | - $this->_with_messengers = array( |
|
528 | - 'email' => array('html'), |
|
529 | - ); |
|
530 | - } |
|
531 | - |
|
532 | - |
|
533 | - /** |
|
534 | - * Return the value of the _with_messengers property |
|
535 | - * |
|
536 | - * @since 4.5.0 |
|
537 | - * @return array |
|
538 | - */ |
|
539 | - public function with_messengers() |
|
540 | - { |
|
541 | - return apply_filters( |
|
542 | - 'FHEE__EE_message_type__get_with_messengers__with_messengers__' . get_class($this), |
|
543 | - $this->_with_messengers |
|
544 | - ); |
|
545 | - } |
|
546 | - |
|
547 | - |
|
548 | - /** |
|
549 | - * this public method accepts a page slug (for an EE_admin page) and will return the response from the child class |
|
550 | - * callback function if that page is registered via the `_admin_registered_page` property set by the child class. |
|
551 | - * * |
|
552 | - * |
|
553 | - * @param string $page the slug of the EE admin page |
|
554 | - * @param array $messengers an array of active messenger objects |
|
555 | - * @param string $action the page action (to allow for more specific handling - i.e. edit vs. add pages) |
|
556 | - * @param array $extra This is just an extra argument that can be used to pass additional data for setting up |
|
557 | - * page content. |
|
558 | - * @access public |
|
559 | - * @return string |
|
560 | - */ |
|
561 | - public function get_message_type_admin_page_content( |
|
562 | - $page, |
|
563 | - $action = null, |
|
564 | - $extra = array(), |
|
565 | - $messengers = array() |
|
566 | - ) { |
|
567 | - // we can also further refine the context by action (if present). |
|
568 | - return $this->_get_admin_page_content($page, $action, $extra, $messengers); |
|
569 | - } |
|
570 | - |
|
571 | - |
|
572 | - /** |
|
573 | - * @return array |
|
574 | - */ |
|
575 | - public function get_contexts() |
|
576 | - { |
|
577 | - return $this->_contexts; |
|
578 | - } |
|
579 | - |
|
580 | - |
|
581 | - /** |
|
582 | - * This just returns the context label for a given context (as set in $_context_label property) |
|
583 | - * |
|
584 | - * @access public |
|
585 | - * @return array |
|
586 | - */ |
|
587 | - public function get_context_label() |
|
588 | - { |
|
589 | - return $this->_context_label; |
|
590 | - } |
|
591 | - |
|
592 | - |
|
593 | - /** |
|
594 | - * This just returns the (filtered) _master_templates property. |
|
595 | - * |
|
596 | - * @see property definition for documentation. |
|
597 | - * @return array |
|
598 | - */ |
|
599 | - public function get_master_templates() |
|
600 | - { |
|
601 | - // first class specific filter then filter that by the global filter. |
|
602 | - $master_templates = apply_filters( |
|
603 | - 'FHEE__' . get_class($this) . '__get_master_templates', |
|
604 | - $this->_master_templates |
|
605 | - ); |
|
606 | - return apply_filters('FHEE__EE_message_type__get_master_templates', $master_templates, $this); |
|
607 | - } |
|
608 | - |
|
609 | - |
|
610 | - /** |
|
611 | - * Accepts an incoming data handler which contains data for processing, and returns an array of |
|
612 | - * EE_Messages_Addressee objects. |
|
613 | - * |
|
614 | - * @param EE_Messages_incoming_data $data |
|
615 | - * @param string $context Limit addressees to specific context. |
|
616 | - * @return array An array indexed by context where each context is an array of EE_Messages_Addressee objects for |
|
617 | - * that context |
|
618 | - * @throws EE_Error |
|
619 | - */ |
|
620 | - public function get_addressees(EE_Messages_incoming_data $data, $context = '') |
|
621 | - { |
|
622 | - // override _data |
|
623 | - $this->_data = $data; |
|
624 | - $addressees = array(); |
|
625 | - $original_contexts = $this->_contexts; |
|
626 | - // if incoming context then limit to that context |
|
627 | - if (! empty($context)) { |
|
628 | - $cntxt = ! empty($this->_contexts[ $context ]) ? $this->_contexts[ $context ] : ''; |
|
629 | - if (! empty($cntxt)) { |
|
630 | - $this->_contexts = array(); |
|
631 | - $this->_contexts[ $context ] = $cntxt; |
|
632 | - } |
|
633 | - } |
|
634 | - $this->_set_default_addressee_data(); |
|
635 | - if ($this->_process_data()) { |
|
636 | - $addressees = $this->_addressees; |
|
637 | - } |
|
638 | - |
|
639 | - // reset contexts and addressees |
|
640 | - $this->_contexts = $original_contexts; |
|
641 | - $this->_addressees = array(); |
|
642 | - return $addressees; |
|
643 | - } |
|
644 | - |
|
645 | - |
|
646 | - /** |
|
647 | - * processes the data object so we get |
|
648 | - * |
|
649 | - * @throws EE_Error |
|
650 | - * @return bool true means data was processed successfully, false means not. |
|
651 | - */ |
|
652 | - protected function _process_data() |
|
653 | - { |
|
654 | - // at a minimum, we NEED EE_Attendee objects. |
|
655 | - if (empty($this->_data->attendees)) { |
|
656 | - return false; // there's no data to process! |
|
657 | - } |
|
658 | - // process addressees for each context. Child classes will have to have methods for |
|
659 | - // each context defined to handle the processing of the data object within them |
|
660 | - foreach ($this->_contexts as $context => $details) { |
|
661 | - $xpctd_method = '_' . $context . '_addressees'; |
|
662 | - if (! method_exists($this, $xpctd_method)) { |
|
663 | - throw new EE_Error( |
|
664 | - sprintf( |
|
665 | - esc_html__( |
|
666 | - 'The data for %1$s message type cannot be prepared because there is no set method for doing so. The expected method name is "%2$s" please doublecheck the %1$s message type class and make sure that method is present', |
|
667 | - 'event_espresso' |
|
668 | - ), |
|
669 | - $this->label['singular'], |
|
670 | - $xpctd_method |
|
671 | - ) |
|
672 | - ); |
|
673 | - } |
|
674 | - $this->_addressees[ $context ] = call_user_func(array($this, $xpctd_method)); |
|
675 | - } |
|
676 | - return true; // data was processed successfully. |
|
677 | - } |
|
678 | - |
|
679 | - |
|
680 | - /** |
|
681 | - * sets the default_addressee_data property, |
|
682 | - * |
|
683 | - * @access private |
|
684 | - * @return void |
|
685 | - */ |
|
686 | - private function _set_default_addressee_data() |
|
687 | - { |
|
688 | - $this->_default_addressee_data = array( |
|
689 | - 'billing' => $this->_data->billing, |
|
690 | - 'taxes' => $this->_data->taxes, |
|
691 | - 'tax_line_items' => $this->_data->tax_line_items, |
|
692 | - 'additional_line_items' => $this->_data->additional_line_items, |
|
693 | - 'grand_total_line_item' => $this->_data->grand_total_line_item, |
|
694 | - 'txn' => $this->_data->txn, |
|
695 | - 'payments' => $this->_data->payments, |
|
696 | - 'payment' => isset($this->_data->payment) && $this->_data->payment instanceof EE_Payment |
|
697 | - ? $this->_data->payment |
|
698 | - : null, |
|
699 | - 'reg_objs' => $this->_data->reg_objs, |
|
700 | - 'registrations' => $this->_data->registrations, |
|
701 | - 'datetimes' => $this->_data->datetimes, |
|
702 | - 'tickets' => $this->_data->tickets, |
|
703 | - 'line_items_with_children' => $this->_data->line_items_with_children, |
|
704 | - 'questions' => $this->_data->questions, |
|
705 | - 'answers' => $this->_data->answers, |
|
706 | - 'txn_status' => $this->_data->txn_status, |
|
707 | - 'total_ticket_count' => $this->_data->total_ticket_count, |
|
708 | - ); |
|
709 | - if (is_array($this->_data->primary_attendee_data)) { |
|
710 | - $this->_default_addressee_data = array_merge( |
|
711 | - $this->_default_addressee_data, |
|
712 | - $this->_data->primary_attendee_data |
|
713 | - ); |
|
714 | - $this->_default_addressee_data['primary_att_obj'] = $this->_data->primary_attendee_data['att_obj']; |
|
715 | - $this->_default_addressee_data['primary_reg_obj'] = $this->_data->primary_attendee_data['reg_obj']; |
|
716 | - } |
|
717 | - } |
|
718 | - |
|
719 | - |
|
720 | - |
|
721 | - /******************** |
|
20 | + /** |
|
21 | + * message type child classes will set what contexts are associated with the message type via this array. |
|
22 | + * format: |
|
23 | + * array( |
|
24 | + * 'context' => array( |
|
25 | + * 'label' => esc_html__('Context Label', 'event_espresso'), |
|
26 | + * 'description' => esc_html__('Context description (for help popups)', 'event_espresso') |
|
27 | + * )); |
|
28 | + * |
|
29 | + * @var array |
|
30 | + */ |
|
31 | + protected $_contexts = array(); |
|
32 | + |
|
33 | + |
|
34 | + /** |
|
35 | + * This property is used to define what the display label will be for contexts (eg. "Recipients", "Themes" etc.) |
|
36 | + * Format: |
|
37 | + * array( 'label' => 'something', 'plural' => 'somethings', 'description' => 'something' ); |
|
38 | + * |
|
39 | + * @var array |
|
40 | + */ |
|
41 | + protected $_context_label; |
|
42 | + |
|
43 | + |
|
44 | + /** MESSAGE ASSEMBLING PROPERTIES **/ |
|
45 | + /** |
|
46 | + * This parameter simply holds all the message objects for retrieval by the controller and sending to the messenger. |
|
47 | + * |
|
48 | + * @var array of message objects. |
|
49 | + */ |
|
50 | + public $messages = array(); |
|
51 | + |
|
52 | + /** |
|
53 | + * The following holds the templates that will be used to assemble the message object for the messenger. |
|
54 | + * |
|
55 | + * @var array |
|
56 | + */ |
|
57 | + protected $_templates; |
|
58 | + |
|
59 | + |
|
60 | + /** |
|
61 | + * If a specific template is being parsed, this will hold the message template group GRP_ID for that template. |
|
62 | + * |
|
63 | + * @var int. |
|
64 | + */ |
|
65 | + protected $_GRP_ID; |
|
66 | + |
|
67 | + |
|
68 | + /** OTHER INFO PROPERTIES **/ |
|
69 | + /** |
|
70 | + * This will hold the count of the message objects in the messages array. This could be used for determining if |
|
71 | + * batching/queueing is needed. |
|
72 | + * |
|
73 | + * @var int |
|
74 | + */ |
|
75 | + public $count = 0; |
|
76 | + |
|
77 | + |
|
78 | + /** |
|
79 | + * This is set via the `do_messenger_hooks` method and contains the messenger being used to send the message of |
|
80 | + * this message type at time of sending. |
|
81 | + * |
|
82 | + * @var EE_messenger |
|
83 | + */ |
|
84 | + protected $_active_messenger; |
|
85 | + |
|
86 | + |
|
87 | + /** |
|
88 | + * This will hold the shortcode_replace instance for handling replacement of shortcodes in the various templates |
|
89 | + * |
|
90 | + * @var object |
|
91 | + */ |
|
92 | + protected $_shortcode_replace; |
|
93 | + |
|
94 | + |
|
95 | + /** |
|
96 | + * The purpose for this property is to simply allow message types to indicate if the message generated is intended |
|
97 | + * for only single context. Child message types should redefine this variable (if necessary) in the |
|
98 | + * _set_data_Handler() method. |
|
99 | + * |
|
100 | + * @var boolean |
|
101 | + */ |
|
102 | + protected $_single_message = false; |
|
103 | + |
|
104 | + |
|
105 | + /** |
|
106 | + * This will hold an array of specific reg_ids that are receiving messages. |
|
107 | + * |
|
108 | + * @since 4.7.x |
|
109 | + * @var array |
|
110 | + */ |
|
111 | + protected $_regs_for_sending = array(); |
|
112 | + |
|
113 | + |
|
114 | + /** |
|
115 | + * This holds the data passed to this class from the controller and also the final processed data. |
|
116 | + * |
|
117 | + * @var object |
|
118 | + */ |
|
119 | + protected $_data; |
|
120 | + |
|
121 | + |
|
122 | + /** |
|
123 | + * this is just a flag indicating whether we're in preview mode or not. |
|
124 | + * |
|
125 | + * @var bool |
|
126 | + */ |
|
127 | + protected $_preview = false; |
|
128 | + |
|
129 | + |
|
130 | + /** |
|
131 | + * This just holds defaults for addressee data that children merge with their data array setup |
|
132 | + * |
|
133 | + * @var array |
|
134 | + */ |
|
135 | + protected $_default_addressee_data; |
|
136 | + |
|
137 | + |
|
138 | + /** |
|
139 | + * Child classes declare through this property what handler they want to use for the incoming data and this string |
|
140 | + * is used to instantiate the EE_Messages_incoming_data child class for that handler. |
|
141 | + * |
|
142 | + * @var string |
|
143 | + */ |
|
144 | + protected $_data_handler; |
|
145 | + |
|
146 | + |
|
147 | + /** |
|
148 | + * This holds any specific fields for holding any settings related to a message type (if any needed) |
|
149 | + * |
|
150 | + * @var array |
|
151 | + */ |
|
152 | + protected $_admin_settings_fields = array(); |
|
153 | + |
|
154 | + /** |
|
155 | + * this property will hold any existing setting that may have been set in the admin. |
|
156 | + * |
|
157 | + * @var array |
|
158 | + */ |
|
159 | + protected $_existing_admin_settings = array(); |
|
160 | + |
|
161 | + |
|
162 | + /** |
|
163 | + * This is used to designate the generating and alternative sending messengers for a message type. It is set via |
|
164 | + * set_with_messengers() on construct. Note, generating messenger also acts as a sending messenger for this message |
|
165 | + * type. However ONLY the generating messengers are used for creating templates for this message type. Should be |
|
166 | + * in this format: |
|
167 | + * { |
|
168 | + * |
|
169 | + * @type string $generating_messenger the name of the generating messenger. Generating |
|
170 | + * messengers are used for generating templates, |
|
171 | + * doing validation and defining valid shortcodes. |
|
172 | + * { |
|
173 | + * @type string $sending_messenger values are the name(s) for the sending |
|
174 | + * messengers. sending messengers are |
|
175 | + * just valid delivery vehicles that will utilize |
|
176 | + * the templates (and generated EE_message |
|
177 | + * objects from the generating messengers). |
|
178 | + * } |
|
179 | + * } |
|
180 | + * @since 4.5.0 |
|
181 | + * @var array |
|
182 | + */ |
|
183 | + protected $_with_messengers = array(); |
|
184 | + |
|
185 | + |
|
186 | + /** |
|
187 | + * This holds the addressees in an array indexed by context for later retrieval when assembling the message objects. |
|
188 | + * |
|
189 | + * @access protected |
|
190 | + * @var array |
|
191 | + */ |
|
192 | + protected $_addressees = array(); |
|
193 | + |
|
194 | + |
|
195 | + /** |
|
196 | + * This allows each message type to set what alternate messenger&message type combination can be used for fallback |
|
197 | + * default templates if there are no specific ones defined for this messenger and message type. Should be in the |
|
198 | + * format: |
|
199 | + * array( |
|
200 | + * 'messenger' => 'message_type', |
|
201 | + * 'another_messenger' => another_message_type |
|
202 | + * ); |
|
203 | + * This is set in the message type constructor. |
|
204 | + * |
|
205 | + * @var array |
|
206 | + */ |
|
207 | + protected $_master_templates = array(); |
|
208 | + |
|
209 | + |
|
210 | + /** |
|
211 | + * This holds whatever the set template pack is for a message template group when generating messages. |
|
212 | + * |
|
213 | + * @since 4.5.0 |
|
214 | + * @var EE_Messages_Template_Pack |
|
215 | + */ |
|
216 | + protected $_template_pack; |
|
217 | + |
|
218 | + |
|
219 | + /** |
|
220 | + * This holds whatever the set variation is for a message template group when generating messages. |
|
221 | + * |
|
222 | + * @since 4.5.0 |
|
223 | + * @var string |
|
224 | + */ |
|
225 | + protected $_variation; |
|
226 | + |
|
227 | + |
|
228 | + /** |
|
229 | + * EE_message_type constructor. |
|
230 | + */ |
|
231 | + public function __construct() |
|
232 | + { |
|
233 | + $this->_messages_item_type = 'message_type'; |
|
234 | + $this->_set_contexts(); |
|
235 | + $this->_set_with_messengers(); |
|
236 | + parent::__construct(); |
|
237 | + } |
|
238 | + |
|
239 | + |
|
240 | + /** |
|
241 | + * This sets the data handler for the message type. It must be used to define the _data_handler property. It is |
|
242 | + * called when messages are setup. |
|
243 | + * |
|
244 | + * @abstract |
|
245 | + * @access protected |
|
246 | + * @return void |
|
247 | + */ |
|
248 | + abstract protected function _set_data_handler(); |
|
249 | + |
|
250 | + |
|
251 | + /** |
|
252 | + * This method should return a EE_Base_Class object (or array of EE_Base_Class objects) for the given context and |
|
253 | + * ID (which should be the primary key id for the base class). Client code doesn't have to know what a message |
|
254 | + * type's data handler is. |
|
255 | + * |
|
256 | + * @since 4.5.0 |
|
257 | + * @param string $context This should be a string matching a valid context for the message type. |
|
258 | + * @param EE_Registration $registration Need a registration to ensure that the data is valid (prevents people |
|
259 | + * guessing a url). |
|
260 | + * @param int $id Optional. Integer corresponding to the value for the primary key of a |
|
261 | + * EE_Base_Class_Object |
|
262 | + * @return mixed ( EE_Base_Class||EE_Base_Class[] ) |
|
263 | + */ |
|
264 | + abstract protected function _get_data_for_context($context, EE_Registration $registration, $id); |
|
265 | + |
|
266 | + |
|
267 | + /** |
|
268 | + * _set_contexts |
|
269 | + * This sets up the contexts associated with the message_type |
|
270 | + * |
|
271 | + * @abstract |
|
272 | + * @access protected |
|
273 | + * @return void |
|
274 | + */ |
|
275 | + abstract protected function _set_contexts(); |
|
276 | + |
|
277 | + |
|
278 | + /** |
|
279 | + * This is used to get the "id" value fo the msg_trigger_url generated by get_url_trigger(). |
|
280 | + * In most cases, child classes don't need anything, (hence the default of 0), however if there is a specific |
|
281 | + * EE_Base_Class that is required in generating a message for a message type recipient then the message |
|
282 | + * type should override this method and use the given params to generate the correct ID. |
|
283 | + * |
|
284 | + * @param string $context The message type context. |
|
285 | + * @param EE_Registration $registration Registration object |
|
286 | + * @deprecated 4.9.0 |
|
287 | + * @return int |
|
288 | + */ |
|
289 | + protected function _get_id_for_msg_url($context, EE_Registration $registration) |
|
290 | + { |
|
291 | + return 0; |
|
292 | + } |
|
293 | + |
|
294 | + |
|
295 | + /** |
|
296 | + * This sets up any action/filter hooks this message type puts in place for a specific messenger. Note that by |
|
297 | + * default this does nothing. Child classes will need to override if they want to add specific hooks for a |
|
298 | + * messenger. |
|
299 | + * |
|
300 | + * @since 1.0.0 |
|
301 | + * @return void |
|
302 | + */ |
|
303 | + protected function _do_messenger_hooks() |
|
304 | + { |
|
305 | + return; |
|
306 | + } |
|
307 | + |
|
308 | + |
|
309 | + /** |
|
310 | + * This is a public wrapper for the protected _do_messenger_hooks() method. |
|
311 | + * For backward compat reasons, this was done rather than making the protected method public. |
|
312 | + * |
|
313 | + * @param EE_messenger $messenger This is used to set the $_active_messenger property, so message types are able |
|
314 | + * to know what messenger is being used to send the message at the time of |
|
315 | + * sending. |
|
316 | + * @since 4.9.0 |
|
317 | + */ |
|
318 | + public function do_messenger_hooks($messenger = null) |
|
319 | + { |
|
320 | + $this->_active_messenger = $messenger; |
|
321 | + $this->_do_messenger_hooks(); |
|
322 | + } |
|
323 | + |
|
324 | + |
|
325 | + /** |
|
326 | + * This method returns whether this message type should always generate a new copy |
|
327 | + * when requested, or if links can be to the already generated copy. |
|
328 | + * Note: this does NOT affect viewing/resending already generated messages in the EE_Message list table. |
|
329 | + * Child classes should override this if different from the default of false. |
|
330 | + * |
|
331 | + * @return bool false means can link to generated EE_Message. true must regenerate. |
|
332 | + */ |
|
333 | + public function always_generate() |
|
334 | + { |
|
335 | + return false; |
|
336 | + } |
|
337 | + |
|
338 | + |
|
339 | + /** |
|
340 | + * Returns the priority for the message type. |
|
341 | + * Priorities are defined as constants on EEM_Message. Currently there are three priorities: |
|
342 | + * - EEM_Message::priority_high |
|
343 | + * - EEM_Message::priority_medium |
|
344 | + * - EEM_Message::priority_low |
|
345 | + * Priority is used to determine the weight the message type is given when queuing for generation and/or sending. |
|
346 | + * |
|
347 | + * @see EEM_Message for more phpdocs on priority. |
|
348 | + * The default priority for all message_types is EEM_Message::priority_low. Message Types wanting to give |
|
349 | + * a higher priority must override this method. Also note, messengers are able to override priorities |
|
350 | + * queuing instructions if their "send_now" flag is set to true. An example of this is the HTML messenger |
|
351 | + * which displays things in the browser. |
|
352 | + * @since 4.9.0 |
|
353 | + * @return int |
|
354 | + */ |
|
355 | + public function get_priority() |
|
356 | + { |
|
357 | + return EEM_Message::priority_low; |
|
358 | + } |
|
359 | + |
|
360 | + |
|
361 | + /** |
|
362 | + * This runs the _set_data_handler() method for message types and then returns what got set. |
|
363 | + * |
|
364 | + * @param mixed $data This sets the data property for the message type with the incoming data used for generating. |
|
365 | + * @return string (the reference for the data handler) (will be an empty string if could not be determined). |
|
366 | + */ |
|
367 | + public function get_data_handler($data) |
|
368 | + { |
|
369 | + $this->_data = $data; |
|
370 | + $this->_set_data_handler(); |
|
371 | + return apply_filters('FHEE__EE_message_type__get_data_handler', $this->_data_handler, $this); |
|
372 | + } |
|
373 | + |
|
374 | + |
|
375 | + /** |
|
376 | + * This is called externally to reset the value of the $_data property for the message type. |
|
377 | + * Please note the value of the _data is highly volatile. It was added as an interim measure ensuring |
|
378 | + * EE_Message_To_Generate objects have any changes to the _data property when `_set_data_handler` method is called |
|
379 | + * (and for back compat reasons). This particular method is used in |
|
380 | + * EE_Messages_Generator::_reset_current_properties to ensure that the internal _data on the message type is |
|
381 | + * cleaned before subsequent EE_Message generation in the same request. |
|
382 | + * |
|
383 | + * @todo This needs refactored along with the whole _set_data_handler() method in EE_message_types. Need to |
|
384 | + * ensure that there is no manipulation of the _data property during run time so there's a clear |
|
385 | + * expectation of what it is. Likely we need to ensure that _data is not persisted IN the message type |
|
386 | + * at all. |
|
387 | + * @internal Plugin authors, do not implement this method, it is subject to change. |
|
388 | + * @since 4.9 |
|
389 | + */ |
|
390 | + public function reset_data() |
|
391 | + { |
|
392 | + $this->_data = null; |
|
393 | + } |
|
394 | + |
|
395 | + |
|
396 | + /** |
|
397 | + * This does some validation of incoming params gets the url trigger from the defined method in the specific child |
|
398 | + * class and then filters the results. |
|
399 | + * |
|
400 | + * @param string $context The message type context |
|
401 | + * @param string $sending_messenger The sending messenger |
|
402 | + * @param EE_Registration $registration Registration object |
|
403 | + * @throws EE_Error |
|
404 | + * @deprecated 4.9.0 Likely 4.9.10 or 4.10.0 will remove this method completely |
|
405 | + * @return string generated url |
|
406 | + */ |
|
407 | + public function get_url_trigger($context, $sending_messenger, EE_Registration $registration) |
|
408 | + { |
|
409 | + // validate context |
|
410 | + // valid context? |
|
411 | + if (! isset($this->_contexts[ $context ])) { |
|
412 | + throw new EE_Error( |
|
413 | + sprintf( |
|
414 | + esc_html__('The context %s is not a valid context for %s.', 'event_espresso'), |
|
415 | + $context, |
|
416 | + get_class($this) |
|
417 | + ) |
|
418 | + ); |
|
419 | + } |
|
420 | + // valid sending_messenger? |
|
421 | + $not_valid_msgr = false; |
|
422 | + foreach ($this->_with_messengers as $generating => $sendings) { |
|
423 | + if (empty($sendings) || array_search($sending_messenger, $sendings) === false) { |
|
424 | + $not_valid_msgr = true; |
|
425 | + } |
|
426 | + } |
|
427 | + if ($not_valid_msgr) { |
|
428 | + throw new EE_Error( |
|
429 | + sprintf( |
|
430 | + esc_html__( |
|
431 | + 'The given sending messenger string (%s) does not match a valid sending messenger with the %s. If this is incorrect, make sure that the message type has defined this messenger as a sending messenger in its $_with_messengers array.', |
|
432 | + 'event_espresso' |
|
433 | + ), |
|
434 | + $sending_messenger, |
|
435 | + get_class($this) |
|
436 | + ) |
|
437 | + ); |
|
438 | + } |
|
439 | + return EEH_MSG_Template::generate_url_trigger( |
|
440 | + $sending_messenger, |
|
441 | + $this->_active_messenger->name, |
|
442 | + $context, |
|
443 | + $this->name, |
|
444 | + $registration, |
|
445 | + $this->_GRP_ID, |
|
446 | + $this->_get_id_for_msg_url($context, $registration) |
|
447 | + ); |
|
448 | + } |
|
449 | + |
|
450 | + |
|
451 | + /** |
|
452 | + * Wrapper for _get_data_for_context() that handles some validation before calling the main class and also allows |
|
453 | + * for filtering. This is (currently) called by the EED_Messages module. |
|
454 | + * |
|
455 | + * @since 4.5.0 |
|
456 | + * @throws EE_Error |
|
457 | + * @param string $context This should be a string matching a valid context for the message type. |
|
458 | + * @param EE_Registration $registration Need a registration to ensure that the data is valid (prevents people |
|
459 | + * guessing a url). |
|
460 | + * @param int $id Optional. Integer corresponding to the value for the primary key of a |
|
461 | + * EE_Base_Class_Object |
|
462 | + * @return mixed (EE_Base_Class||EE_Base_Class[]) |
|
463 | + */ |
|
464 | + public function get_data_for_context($context, EE_Registration $registration, $id = 0) |
|
465 | + { |
|
466 | + // valid context? |
|
467 | + if (! isset($this->_contexts[ $context ])) { |
|
468 | + throw new EE_Error( |
|
469 | + sprintf( |
|
470 | + esc_html__('The context %s is not a valid context for %s.', 'event_espresso'), |
|
471 | + $context, |
|
472 | + get_class($this) |
|
473 | + ) |
|
474 | + ); |
|
475 | + } |
|
476 | + // get data and apply global and class specific filters on it. |
|
477 | + $data = apply_filters( |
|
478 | + 'FHEE__EE_message_type__get_data_for_context__data', |
|
479 | + $this->_get_data_for_context($context, $registration, $id), |
|
480 | + $this |
|
481 | + ); |
|
482 | + $data = apply_filters('FHEE__' . get_class($this) . '__get_data_for_context__data', $data, $this); |
|
483 | + // if empty then something went wrong! |
|
484 | + if (empty($data)) { |
|
485 | + throw new EE_Error( |
|
486 | + sprintf( |
|
487 | + esc_html__( |
|
488 | + 'There is no data retrieved, it is possible that the id given (%d) does not match any value in the database for the corresponding EE_Base_Class used by the data handler for the %s message type.', |
|
489 | + 'event_espresso' |
|
490 | + ), |
|
491 | + $id, |
|
492 | + $this->name |
|
493 | + ) |
|
494 | + ); |
|
495 | + } |
|
496 | + return $data; |
|
497 | + } |
|
498 | + |
|
499 | + |
|
500 | + /** |
|
501 | + * This returns the contents of the _data property. |
|
502 | + * Please note the value of the _data is highly volatile. It was added as an interim measure ensuring |
|
503 | + * EE_Message_To_Generate objects have any changes to the _data property when `_set_data_handler` method is called. |
|
504 | + * |
|
505 | + * @todo This needs refactored along with the whole _set_data_handler() method in EE_message_types. Need to |
|
506 | + * ensure that there is no manipulation of the _data property during run time so there's a clear |
|
507 | + * expectation of what it is. |
|
508 | + * @internal Plugin authors, do not implement this method, it is subject to change. |
|
509 | + * @return mixed |
|
510 | + */ |
|
511 | + public function get_data() |
|
512 | + { |
|
513 | + return $this->_data; |
|
514 | + } |
|
515 | + |
|
516 | + |
|
517 | + /** |
|
518 | + * used to set the $_with_messengers property. (this is a default, child classes SHOULD override) |
|
519 | + * |
|
520 | + * @see property definition for description of setup. |
|
521 | + * @since 4.5.0 |
|
522 | + * @abstract |
|
523 | + * @return void |
|
524 | + */ |
|
525 | + protected function _set_with_messengers() |
|
526 | + { |
|
527 | + $this->_with_messengers = array( |
|
528 | + 'email' => array('html'), |
|
529 | + ); |
|
530 | + } |
|
531 | + |
|
532 | + |
|
533 | + /** |
|
534 | + * Return the value of the _with_messengers property |
|
535 | + * |
|
536 | + * @since 4.5.0 |
|
537 | + * @return array |
|
538 | + */ |
|
539 | + public function with_messengers() |
|
540 | + { |
|
541 | + return apply_filters( |
|
542 | + 'FHEE__EE_message_type__get_with_messengers__with_messengers__' . get_class($this), |
|
543 | + $this->_with_messengers |
|
544 | + ); |
|
545 | + } |
|
546 | + |
|
547 | + |
|
548 | + /** |
|
549 | + * this public method accepts a page slug (for an EE_admin page) and will return the response from the child class |
|
550 | + * callback function if that page is registered via the `_admin_registered_page` property set by the child class. |
|
551 | + * * |
|
552 | + * |
|
553 | + * @param string $page the slug of the EE admin page |
|
554 | + * @param array $messengers an array of active messenger objects |
|
555 | + * @param string $action the page action (to allow for more specific handling - i.e. edit vs. add pages) |
|
556 | + * @param array $extra This is just an extra argument that can be used to pass additional data for setting up |
|
557 | + * page content. |
|
558 | + * @access public |
|
559 | + * @return string |
|
560 | + */ |
|
561 | + public function get_message_type_admin_page_content( |
|
562 | + $page, |
|
563 | + $action = null, |
|
564 | + $extra = array(), |
|
565 | + $messengers = array() |
|
566 | + ) { |
|
567 | + // we can also further refine the context by action (if present). |
|
568 | + return $this->_get_admin_page_content($page, $action, $extra, $messengers); |
|
569 | + } |
|
570 | + |
|
571 | + |
|
572 | + /** |
|
573 | + * @return array |
|
574 | + */ |
|
575 | + public function get_contexts() |
|
576 | + { |
|
577 | + return $this->_contexts; |
|
578 | + } |
|
579 | + |
|
580 | + |
|
581 | + /** |
|
582 | + * This just returns the context label for a given context (as set in $_context_label property) |
|
583 | + * |
|
584 | + * @access public |
|
585 | + * @return array |
|
586 | + */ |
|
587 | + public function get_context_label() |
|
588 | + { |
|
589 | + return $this->_context_label; |
|
590 | + } |
|
591 | + |
|
592 | + |
|
593 | + /** |
|
594 | + * This just returns the (filtered) _master_templates property. |
|
595 | + * |
|
596 | + * @see property definition for documentation. |
|
597 | + * @return array |
|
598 | + */ |
|
599 | + public function get_master_templates() |
|
600 | + { |
|
601 | + // first class specific filter then filter that by the global filter. |
|
602 | + $master_templates = apply_filters( |
|
603 | + 'FHEE__' . get_class($this) . '__get_master_templates', |
|
604 | + $this->_master_templates |
|
605 | + ); |
|
606 | + return apply_filters('FHEE__EE_message_type__get_master_templates', $master_templates, $this); |
|
607 | + } |
|
608 | + |
|
609 | + |
|
610 | + /** |
|
611 | + * Accepts an incoming data handler which contains data for processing, and returns an array of |
|
612 | + * EE_Messages_Addressee objects. |
|
613 | + * |
|
614 | + * @param EE_Messages_incoming_data $data |
|
615 | + * @param string $context Limit addressees to specific context. |
|
616 | + * @return array An array indexed by context where each context is an array of EE_Messages_Addressee objects for |
|
617 | + * that context |
|
618 | + * @throws EE_Error |
|
619 | + */ |
|
620 | + public function get_addressees(EE_Messages_incoming_data $data, $context = '') |
|
621 | + { |
|
622 | + // override _data |
|
623 | + $this->_data = $data; |
|
624 | + $addressees = array(); |
|
625 | + $original_contexts = $this->_contexts; |
|
626 | + // if incoming context then limit to that context |
|
627 | + if (! empty($context)) { |
|
628 | + $cntxt = ! empty($this->_contexts[ $context ]) ? $this->_contexts[ $context ] : ''; |
|
629 | + if (! empty($cntxt)) { |
|
630 | + $this->_contexts = array(); |
|
631 | + $this->_contexts[ $context ] = $cntxt; |
|
632 | + } |
|
633 | + } |
|
634 | + $this->_set_default_addressee_data(); |
|
635 | + if ($this->_process_data()) { |
|
636 | + $addressees = $this->_addressees; |
|
637 | + } |
|
638 | + |
|
639 | + // reset contexts and addressees |
|
640 | + $this->_contexts = $original_contexts; |
|
641 | + $this->_addressees = array(); |
|
642 | + return $addressees; |
|
643 | + } |
|
644 | + |
|
645 | + |
|
646 | + /** |
|
647 | + * processes the data object so we get |
|
648 | + * |
|
649 | + * @throws EE_Error |
|
650 | + * @return bool true means data was processed successfully, false means not. |
|
651 | + */ |
|
652 | + protected function _process_data() |
|
653 | + { |
|
654 | + // at a minimum, we NEED EE_Attendee objects. |
|
655 | + if (empty($this->_data->attendees)) { |
|
656 | + return false; // there's no data to process! |
|
657 | + } |
|
658 | + // process addressees for each context. Child classes will have to have methods for |
|
659 | + // each context defined to handle the processing of the data object within them |
|
660 | + foreach ($this->_contexts as $context => $details) { |
|
661 | + $xpctd_method = '_' . $context . '_addressees'; |
|
662 | + if (! method_exists($this, $xpctd_method)) { |
|
663 | + throw new EE_Error( |
|
664 | + sprintf( |
|
665 | + esc_html__( |
|
666 | + 'The data for %1$s message type cannot be prepared because there is no set method for doing so. The expected method name is "%2$s" please doublecheck the %1$s message type class and make sure that method is present', |
|
667 | + 'event_espresso' |
|
668 | + ), |
|
669 | + $this->label['singular'], |
|
670 | + $xpctd_method |
|
671 | + ) |
|
672 | + ); |
|
673 | + } |
|
674 | + $this->_addressees[ $context ] = call_user_func(array($this, $xpctd_method)); |
|
675 | + } |
|
676 | + return true; // data was processed successfully. |
|
677 | + } |
|
678 | + |
|
679 | + |
|
680 | + /** |
|
681 | + * sets the default_addressee_data property, |
|
682 | + * |
|
683 | + * @access private |
|
684 | + * @return void |
|
685 | + */ |
|
686 | + private function _set_default_addressee_data() |
|
687 | + { |
|
688 | + $this->_default_addressee_data = array( |
|
689 | + 'billing' => $this->_data->billing, |
|
690 | + 'taxes' => $this->_data->taxes, |
|
691 | + 'tax_line_items' => $this->_data->tax_line_items, |
|
692 | + 'additional_line_items' => $this->_data->additional_line_items, |
|
693 | + 'grand_total_line_item' => $this->_data->grand_total_line_item, |
|
694 | + 'txn' => $this->_data->txn, |
|
695 | + 'payments' => $this->_data->payments, |
|
696 | + 'payment' => isset($this->_data->payment) && $this->_data->payment instanceof EE_Payment |
|
697 | + ? $this->_data->payment |
|
698 | + : null, |
|
699 | + 'reg_objs' => $this->_data->reg_objs, |
|
700 | + 'registrations' => $this->_data->registrations, |
|
701 | + 'datetimes' => $this->_data->datetimes, |
|
702 | + 'tickets' => $this->_data->tickets, |
|
703 | + 'line_items_with_children' => $this->_data->line_items_with_children, |
|
704 | + 'questions' => $this->_data->questions, |
|
705 | + 'answers' => $this->_data->answers, |
|
706 | + 'txn_status' => $this->_data->txn_status, |
|
707 | + 'total_ticket_count' => $this->_data->total_ticket_count, |
|
708 | + ); |
|
709 | + if (is_array($this->_data->primary_attendee_data)) { |
|
710 | + $this->_default_addressee_data = array_merge( |
|
711 | + $this->_default_addressee_data, |
|
712 | + $this->_data->primary_attendee_data |
|
713 | + ); |
|
714 | + $this->_default_addressee_data['primary_att_obj'] = $this->_data->primary_attendee_data['att_obj']; |
|
715 | + $this->_default_addressee_data['primary_reg_obj'] = $this->_data->primary_attendee_data['reg_obj']; |
|
716 | + } |
|
717 | + } |
|
718 | + |
|
719 | + |
|
720 | + |
|
721 | + /******************** |
|
722 | 722 | * setup default shared addressee object/contexts |
723 | 723 | * These can be utilized simply by defining the context in the child message type. |
724 | 724 | * They can also be overridden if a specific message type needs to do something different for that context. |
725 | 725 | ****************/ |
726 | - /** |
|
727 | - * see abstract declaration in parent class for details, children message types can |
|
728 | - * override these valid shortcodes if desired (we include all for all contexts by default). |
|
729 | - */ |
|
730 | - protected function _set_valid_shortcodes() |
|
731 | - { |
|
732 | - $all_shortcodes = array( |
|
733 | - 'attendee_list', |
|
734 | - 'attendee', |
|
735 | - 'datetime_list', |
|
736 | - 'datetime', |
|
737 | - 'event_list', |
|
738 | - 'event_meta', |
|
739 | - 'event', |
|
740 | - 'organization', |
|
741 | - 'recipient_details', |
|
742 | - 'recipient_list', |
|
743 | - 'ticket_list', |
|
744 | - 'ticket', |
|
745 | - 'transaction', |
|
746 | - 'venue', |
|
747 | - 'primary_registration_details', |
|
748 | - 'primary_registration_list', |
|
749 | - 'event_author', |
|
750 | - 'email', |
|
751 | - 'messenger', |
|
752 | - ); |
|
753 | - $contexts = $this->get_contexts(); |
|
754 | - foreach ($contexts as $context => $details) { |
|
755 | - $this->_valid_shortcodes[ $context ] = $all_shortcodes; |
|
756 | - // make sure non admin context does not include the event_author shortcodes |
|
757 | - if ($context != 'admin') { |
|
758 | - if (($key = array_search('event_author', $this->_valid_shortcodes[ $context ])) !== false) { |
|
759 | - unset($this->_valid_shortcodes[ $context ][ $key ]); |
|
760 | - } |
|
761 | - } |
|
762 | - } |
|
763 | - // make sure admin context does not include the recipient_details shortcodes |
|
764 | - // IF we have admin context hooked in message types might not have that context. |
|
765 | - if (! empty($this->_valid_shortcodes['admin'])) { |
|
766 | - if (($key = array_search('recipient_details', $this->_valid_shortcodes['admin'])) !== false) { |
|
767 | - unset($this->_valid_shortcodes['admin'][ $key ]); |
|
768 | - } |
|
769 | - // make sure admin context does not include the recipient_details shortcodes |
|
770 | - if (($key = array_search('recipient_list', $this->_valid_shortcodes['admin'])) !== false) { |
|
771 | - unset($this->_valid_shortcodes['admin'][ $key ]); |
|
772 | - } |
|
773 | - } |
|
774 | - } |
|
775 | - |
|
776 | - |
|
777 | - /** |
|
778 | - * Used by Validators to modify the valid shortcodes. |
|
779 | - * |
|
780 | - * @param array $new_config array of valid shortcodes (by context) |
|
781 | - * @return void sets valid_shortcodes property |
|
782 | - */ |
|
783 | - public function reset_valid_shortcodes_config($new_config) |
|
784 | - { |
|
785 | - foreach ($new_config as $context => $shortcodes) { |
|
786 | - $this->_valid_shortcodes[ $context ] = $shortcodes; |
|
787 | - } |
|
788 | - } |
|
789 | - |
|
790 | - |
|
791 | - /** |
|
792 | - * returns an array of addressee objects for event_admins |
|
793 | - * |
|
794 | - * @access protected |
|
795 | - * @return array array of EE_Messages_Addressee objects |
|
796 | - * @throws EE_Error |
|
797 | - * @throws InvalidArgumentException |
|
798 | - * @throws InvalidDataTypeException |
|
799 | - * @throws InvalidInterfaceException |
|
800 | - */ |
|
801 | - protected function _admin_addressees() |
|
802 | - { |
|
803 | - $admin_events = array(); |
|
804 | - $addressees = array(); |
|
805 | - // first we need to get the event admin user id for all the events |
|
806 | - // and setup an addressee object for each unique admin user. |
|
807 | - foreach ($this->_data->events as $line_ref => $event) { |
|
808 | - $admin_id = $this->_get_event_admin_id($event['ID']); |
|
809 | - // make sure we are just including the events that belong to this admin! |
|
810 | - $admin_events[ $admin_id ][ $line_ref ] = $event; |
|
811 | - } |
|
812 | - // k now we can loop through the event_admins and setup the addressee data. |
|
813 | - foreach ($admin_events as $admin_id => $event_details) { |
|
814 | - $aee = array( |
|
815 | - 'user_id' => $admin_id, |
|
816 | - 'events' => $event_details, |
|
817 | - 'attendees' => $this->_data->attendees, |
|
818 | - 'recipient_id' => $admin_id, |
|
819 | - 'recipient_type' => 'WP_User', |
|
820 | - ); |
|
821 | - $aee = array_merge($this->_default_addressee_data, $aee); |
|
822 | - $addressees[] = new EE_Messages_Addressee($aee); |
|
823 | - } |
|
824 | - return $addressees; |
|
825 | - } |
|
826 | - |
|
827 | - |
|
828 | - /** |
|
829 | - * Takes care of setting up the addressee object(s) for the primary attendee. |
|
830 | - * |
|
831 | - * @access protected |
|
832 | - * @return array of EE_Addressee objects |
|
833 | - * @throws EE_Error |
|
834 | - */ |
|
835 | - protected function _primary_attendee_addressees() |
|
836 | - { |
|
837 | - $aee = $this->_default_addressee_data; |
|
838 | - $aee['events'] = $this->_data->events; |
|
839 | - $aee['attendees'] = $this->_data->attendees; |
|
840 | - $aee['recipient_id'] = $aee['primary_att_obj'] instanceof EE_Attendee ? $aee['primary_att_obj']->ID() : 0; |
|
841 | - $aee['recipient_type'] = 'Attendee'; |
|
842 | - // great now we can instantiate the $addressee object and return (as an array); |
|
843 | - $add[] = new EE_Messages_Addressee($aee); |
|
844 | - return $add; |
|
845 | - } |
|
846 | - |
|
847 | - |
|
848 | - /** |
|
849 | - * Takes care of setting up the addressee object(s) for the registered attendees |
|
850 | - * |
|
851 | - * @access protected |
|
852 | - * @return array of EE_Addressee objects |
|
853 | - */ |
|
854 | - protected function _attendee_addressees() |
|
855 | - { |
|
856 | - $add = array(); |
|
857 | - // we just have to loop through the attendees. We'll also set the attached events for each attendee. |
|
858 | - // use to verify unique attendee emails... we don't want to sent multiple copies to the same attendee do we? |
|
859 | - $already_processed = array(); |
|
860 | - foreach ($this->_data->attendees as $att_id => $details) { |
|
861 | - // set the attendee array to blank on each loop; |
|
862 | - $aee = array(); |
|
863 | - if ( |
|
864 | - isset($this->_data->reg_obj) |
|
865 | - && ($this->_data->reg_obj->attendee_ID() != $att_id) |
|
866 | - && $this->_single_message |
|
867 | - ) { |
|
868 | - continue; |
|
869 | - } |
|
870 | - // is $this->_regs_for_sending present? |
|
871 | - // If so, let's make sure we ONLY generate addressee for registrations in that array. |
|
872 | - if (! empty($this->_regs_for_sending) && is_array($this->_regs_for_sending)) { |
|
873 | - $regs_allowed = array_intersect_key(array_flip($this->_regs_for_sending), $details['reg_objs']); |
|
874 | - if (empty($regs_allowed)) { |
|
875 | - continue; |
|
876 | - } |
|
877 | - } |
|
878 | - if ( |
|
879 | - apply_filters( |
|
880 | - 'FHEE__EE_message_type___attendee_addressees__prevent_duplicate_email_sends', |
|
881 | - true, |
|
882 | - $this->_data, |
|
883 | - $this |
|
884 | - ) |
|
885 | - && in_array($att_id, $already_processed, true) |
|
886 | - ) { |
|
887 | - continue; |
|
888 | - } |
|
889 | - $already_processed[] = $att_id; |
|
890 | - foreach ($details as $item => $value) { |
|
891 | - $aee[ $item ] = $value; |
|
892 | - if ($item === 'line_ref') { |
|
893 | - foreach ($value as $event_id) { |
|
894 | - $aee['events'][ $event_id ] = $this->_data->events[ $event_id ]; |
|
895 | - } |
|
896 | - } |
|
897 | - if ($item === 'attendee_email') { |
|
898 | - $aee['attendee_email'] = $value; |
|
899 | - } |
|
900 | - /*if ( $item == 'registration_id' ) { |
|
726 | + /** |
|
727 | + * see abstract declaration in parent class for details, children message types can |
|
728 | + * override these valid shortcodes if desired (we include all for all contexts by default). |
|
729 | + */ |
|
730 | + protected function _set_valid_shortcodes() |
|
731 | + { |
|
732 | + $all_shortcodes = array( |
|
733 | + 'attendee_list', |
|
734 | + 'attendee', |
|
735 | + 'datetime_list', |
|
736 | + 'datetime', |
|
737 | + 'event_list', |
|
738 | + 'event_meta', |
|
739 | + 'event', |
|
740 | + 'organization', |
|
741 | + 'recipient_details', |
|
742 | + 'recipient_list', |
|
743 | + 'ticket_list', |
|
744 | + 'ticket', |
|
745 | + 'transaction', |
|
746 | + 'venue', |
|
747 | + 'primary_registration_details', |
|
748 | + 'primary_registration_list', |
|
749 | + 'event_author', |
|
750 | + 'email', |
|
751 | + 'messenger', |
|
752 | + ); |
|
753 | + $contexts = $this->get_contexts(); |
|
754 | + foreach ($contexts as $context => $details) { |
|
755 | + $this->_valid_shortcodes[ $context ] = $all_shortcodes; |
|
756 | + // make sure non admin context does not include the event_author shortcodes |
|
757 | + if ($context != 'admin') { |
|
758 | + if (($key = array_search('event_author', $this->_valid_shortcodes[ $context ])) !== false) { |
|
759 | + unset($this->_valid_shortcodes[ $context ][ $key ]); |
|
760 | + } |
|
761 | + } |
|
762 | + } |
|
763 | + // make sure admin context does not include the recipient_details shortcodes |
|
764 | + // IF we have admin context hooked in message types might not have that context. |
|
765 | + if (! empty($this->_valid_shortcodes['admin'])) { |
|
766 | + if (($key = array_search('recipient_details', $this->_valid_shortcodes['admin'])) !== false) { |
|
767 | + unset($this->_valid_shortcodes['admin'][ $key ]); |
|
768 | + } |
|
769 | + // make sure admin context does not include the recipient_details shortcodes |
|
770 | + if (($key = array_search('recipient_list', $this->_valid_shortcodes['admin'])) !== false) { |
|
771 | + unset($this->_valid_shortcodes['admin'][ $key ]); |
|
772 | + } |
|
773 | + } |
|
774 | + } |
|
775 | + |
|
776 | + |
|
777 | + /** |
|
778 | + * Used by Validators to modify the valid shortcodes. |
|
779 | + * |
|
780 | + * @param array $new_config array of valid shortcodes (by context) |
|
781 | + * @return void sets valid_shortcodes property |
|
782 | + */ |
|
783 | + public function reset_valid_shortcodes_config($new_config) |
|
784 | + { |
|
785 | + foreach ($new_config as $context => $shortcodes) { |
|
786 | + $this->_valid_shortcodes[ $context ] = $shortcodes; |
|
787 | + } |
|
788 | + } |
|
789 | + |
|
790 | + |
|
791 | + /** |
|
792 | + * returns an array of addressee objects for event_admins |
|
793 | + * |
|
794 | + * @access protected |
|
795 | + * @return array array of EE_Messages_Addressee objects |
|
796 | + * @throws EE_Error |
|
797 | + * @throws InvalidArgumentException |
|
798 | + * @throws InvalidDataTypeException |
|
799 | + * @throws InvalidInterfaceException |
|
800 | + */ |
|
801 | + protected function _admin_addressees() |
|
802 | + { |
|
803 | + $admin_events = array(); |
|
804 | + $addressees = array(); |
|
805 | + // first we need to get the event admin user id for all the events |
|
806 | + // and setup an addressee object for each unique admin user. |
|
807 | + foreach ($this->_data->events as $line_ref => $event) { |
|
808 | + $admin_id = $this->_get_event_admin_id($event['ID']); |
|
809 | + // make sure we are just including the events that belong to this admin! |
|
810 | + $admin_events[ $admin_id ][ $line_ref ] = $event; |
|
811 | + } |
|
812 | + // k now we can loop through the event_admins and setup the addressee data. |
|
813 | + foreach ($admin_events as $admin_id => $event_details) { |
|
814 | + $aee = array( |
|
815 | + 'user_id' => $admin_id, |
|
816 | + 'events' => $event_details, |
|
817 | + 'attendees' => $this->_data->attendees, |
|
818 | + 'recipient_id' => $admin_id, |
|
819 | + 'recipient_type' => 'WP_User', |
|
820 | + ); |
|
821 | + $aee = array_merge($this->_default_addressee_data, $aee); |
|
822 | + $addressees[] = new EE_Messages_Addressee($aee); |
|
823 | + } |
|
824 | + return $addressees; |
|
825 | + } |
|
826 | + |
|
827 | + |
|
828 | + /** |
|
829 | + * Takes care of setting up the addressee object(s) for the primary attendee. |
|
830 | + * |
|
831 | + * @access protected |
|
832 | + * @return array of EE_Addressee objects |
|
833 | + * @throws EE_Error |
|
834 | + */ |
|
835 | + protected function _primary_attendee_addressees() |
|
836 | + { |
|
837 | + $aee = $this->_default_addressee_data; |
|
838 | + $aee['events'] = $this->_data->events; |
|
839 | + $aee['attendees'] = $this->_data->attendees; |
|
840 | + $aee['recipient_id'] = $aee['primary_att_obj'] instanceof EE_Attendee ? $aee['primary_att_obj']->ID() : 0; |
|
841 | + $aee['recipient_type'] = 'Attendee'; |
|
842 | + // great now we can instantiate the $addressee object and return (as an array); |
|
843 | + $add[] = new EE_Messages_Addressee($aee); |
|
844 | + return $add; |
|
845 | + } |
|
846 | + |
|
847 | + |
|
848 | + /** |
|
849 | + * Takes care of setting up the addressee object(s) for the registered attendees |
|
850 | + * |
|
851 | + * @access protected |
|
852 | + * @return array of EE_Addressee objects |
|
853 | + */ |
|
854 | + protected function _attendee_addressees() |
|
855 | + { |
|
856 | + $add = array(); |
|
857 | + // we just have to loop through the attendees. We'll also set the attached events for each attendee. |
|
858 | + // use to verify unique attendee emails... we don't want to sent multiple copies to the same attendee do we? |
|
859 | + $already_processed = array(); |
|
860 | + foreach ($this->_data->attendees as $att_id => $details) { |
|
861 | + // set the attendee array to blank on each loop; |
|
862 | + $aee = array(); |
|
863 | + if ( |
|
864 | + isset($this->_data->reg_obj) |
|
865 | + && ($this->_data->reg_obj->attendee_ID() != $att_id) |
|
866 | + && $this->_single_message |
|
867 | + ) { |
|
868 | + continue; |
|
869 | + } |
|
870 | + // is $this->_regs_for_sending present? |
|
871 | + // If so, let's make sure we ONLY generate addressee for registrations in that array. |
|
872 | + if (! empty($this->_regs_for_sending) && is_array($this->_regs_for_sending)) { |
|
873 | + $regs_allowed = array_intersect_key(array_flip($this->_regs_for_sending), $details['reg_objs']); |
|
874 | + if (empty($regs_allowed)) { |
|
875 | + continue; |
|
876 | + } |
|
877 | + } |
|
878 | + if ( |
|
879 | + apply_filters( |
|
880 | + 'FHEE__EE_message_type___attendee_addressees__prevent_duplicate_email_sends', |
|
881 | + true, |
|
882 | + $this->_data, |
|
883 | + $this |
|
884 | + ) |
|
885 | + && in_array($att_id, $already_processed, true) |
|
886 | + ) { |
|
887 | + continue; |
|
888 | + } |
|
889 | + $already_processed[] = $att_id; |
|
890 | + foreach ($details as $item => $value) { |
|
891 | + $aee[ $item ] = $value; |
|
892 | + if ($item === 'line_ref') { |
|
893 | + foreach ($value as $event_id) { |
|
894 | + $aee['events'][ $event_id ] = $this->_data->events[ $event_id ]; |
|
895 | + } |
|
896 | + } |
|
897 | + if ($item === 'attendee_email') { |
|
898 | + $aee['attendee_email'] = $value; |
|
899 | + } |
|
900 | + /*if ( $item == 'registration_id' ) { |
|
901 | 901 | $aee['attendee_registration_id'] = $value; |
902 | 902 | }/**/ |
903 | - } |
|
904 | - // note the FIRST reg object in this array is the one |
|
905 | - // we'll use for this attendee as the primary registration for this attendee. |
|
906 | - $aee['reg_obj'] = reset($this->_data->attendees[ $att_id ]['reg_objs']); |
|
907 | - $aee['attendees'] = $this->_data->attendees; |
|
908 | - $aee['recipient_id'] = $att_id; |
|
909 | - $aee['recipient_type'] = 'Attendee'; |
|
910 | - // merge in the primary attendee data |
|
911 | - $aee = array_merge($this->_default_addressee_data, $aee); |
|
912 | - $add[] = new EE_Messages_Addressee($aee); |
|
913 | - } |
|
914 | - return $add; |
|
915 | - } |
|
916 | - |
|
917 | - |
|
918 | - /** |
|
919 | - * @param $event_id |
|
920 | - * @return int |
|
921 | - * @throws EE_Error |
|
922 | - * @throws InvalidArgumentException |
|
923 | - * @throws InvalidDataTypeException |
|
924 | - * @throws InvalidInterfaceException |
|
925 | - */ |
|
926 | - protected function _get_event_admin_id($event_id) |
|
927 | - { |
|
928 | - $event = EEM_Event::instance()->get_one_by_ID($event_id); |
|
929 | - return $event instanceof EE_Event ? $event->wp_user() : 0; |
|
930 | - } |
|
903 | + } |
|
904 | + // note the FIRST reg object in this array is the one |
|
905 | + // we'll use for this attendee as the primary registration for this attendee. |
|
906 | + $aee['reg_obj'] = reset($this->_data->attendees[ $att_id ]['reg_objs']); |
|
907 | + $aee['attendees'] = $this->_data->attendees; |
|
908 | + $aee['recipient_id'] = $att_id; |
|
909 | + $aee['recipient_type'] = 'Attendee'; |
|
910 | + // merge in the primary attendee data |
|
911 | + $aee = array_merge($this->_default_addressee_data, $aee); |
|
912 | + $add[] = new EE_Messages_Addressee($aee); |
|
913 | + } |
|
914 | + return $add; |
|
915 | + } |
|
916 | + |
|
917 | + |
|
918 | + /** |
|
919 | + * @param $event_id |
|
920 | + * @return int |
|
921 | + * @throws EE_Error |
|
922 | + * @throws InvalidArgumentException |
|
923 | + * @throws InvalidDataTypeException |
|
924 | + * @throws InvalidInterfaceException |
|
925 | + */ |
|
926 | + protected function _get_event_admin_id($event_id) |
|
927 | + { |
|
928 | + $event = EEM_Event::instance()->get_one_by_ID($event_id); |
|
929 | + return $event instanceof EE_Event ? $event->wp_user() : 0; |
|
930 | + } |
|
931 | 931 | } |
@@ -408,7 +408,7 @@ discard block |
||
408 | 408 | { |
409 | 409 | // validate context |
410 | 410 | // valid context? |
411 | - if (! isset($this->_contexts[ $context ])) { |
|
411 | + if ( ! isset($this->_contexts[$context])) { |
|
412 | 412 | throw new EE_Error( |
413 | 413 | sprintf( |
414 | 414 | esc_html__('The context %s is not a valid context for %s.', 'event_espresso'), |
@@ -464,7 +464,7 @@ discard block |
||
464 | 464 | public function get_data_for_context($context, EE_Registration $registration, $id = 0) |
465 | 465 | { |
466 | 466 | // valid context? |
467 | - if (! isset($this->_contexts[ $context ])) { |
|
467 | + if ( ! isset($this->_contexts[$context])) { |
|
468 | 468 | throw new EE_Error( |
469 | 469 | sprintf( |
470 | 470 | esc_html__('The context %s is not a valid context for %s.', 'event_espresso'), |
@@ -479,7 +479,7 @@ discard block |
||
479 | 479 | $this->_get_data_for_context($context, $registration, $id), |
480 | 480 | $this |
481 | 481 | ); |
482 | - $data = apply_filters('FHEE__' . get_class($this) . '__get_data_for_context__data', $data, $this); |
|
482 | + $data = apply_filters('FHEE__'.get_class($this).'__get_data_for_context__data', $data, $this); |
|
483 | 483 | // if empty then something went wrong! |
484 | 484 | if (empty($data)) { |
485 | 485 | throw new EE_Error( |
@@ -539,7 +539,7 @@ discard block |
||
539 | 539 | public function with_messengers() |
540 | 540 | { |
541 | 541 | return apply_filters( |
542 | - 'FHEE__EE_message_type__get_with_messengers__with_messengers__' . get_class($this), |
|
542 | + 'FHEE__EE_message_type__get_with_messengers__with_messengers__'.get_class($this), |
|
543 | 543 | $this->_with_messengers |
544 | 544 | ); |
545 | 545 | } |
@@ -600,7 +600,7 @@ discard block |
||
600 | 600 | { |
601 | 601 | // first class specific filter then filter that by the global filter. |
602 | 602 | $master_templates = apply_filters( |
603 | - 'FHEE__' . get_class($this) . '__get_master_templates', |
|
603 | + 'FHEE__'.get_class($this).'__get_master_templates', |
|
604 | 604 | $this->_master_templates |
605 | 605 | ); |
606 | 606 | return apply_filters('FHEE__EE_message_type__get_master_templates', $master_templates, $this); |
@@ -624,11 +624,11 @@ discard block |
||
624 | 624 | $addressees = array(); |
625 | 625 | $original_contexts = $this->_contexts; |
626 | 626 | // if incoming context then limit to that context |
627 | - if (! empty($context)) { |
|
628 | - $cntxt = ! empty($this->_contexts[ $context ]) ? $this->_contexts[ $context ] : ''; |
|
629 | - if (! empty($cntxt)) { |
|
630 | - $this->_contexts = array(); |
|
631 | - $this->_contexts[ $context ] = $cntxt; |
|
627 | + if ( ! empty($context)) { |
|
628 | + $cntxt = ! empty($this->_contexts[$context]) ? $this->_contexts[$context] : ''; |
|
629 | + if ( ! empty($cntxt)) { |
|
630 | + $this->_contexts = array(); |
|
631 | + $this->_contexts[$context] = $cntxt; |
|
632 | 632 | } |
633 | 633 | } |
634 | 634 | $this->_set_default_addressee_data(); |
@@ -653,13 +653,13 @@ discard block |
||
653 | 653 | { |
654 | 654 | // at a minimum, we NEED EE_Attendee objects. |
655 | 655 | if (empty($this->_data->attendees)) { |
656 | - return false; // there's no data to process! |
|
656 | + return false; // there's no data to process! |
|
657 | 657 | } |
658 | 658 | // process addressees for each context. Child classes will have to have methods for |
659 | 659 | // each context defined to handle the processing of the data object within them |
660 | 660 | foreach ($this->_contexts as $context => $details) { |
661 | - $xpctd_method = '_' . $context . '_addressees'; |
|
662 | - if (! method_exists($this, $xpctd_method)) { |
|
661 | + $xpctd_method = '_'.$context.'_addressees'; |
|
662 | + if ( ! method_exists($this, $xpctd_method)) { |
|
663 | 663 | throw new EE_Error( |
664 | 664 | sprintf( |
665 | 665 | esc_html__( |
@@ -671,7 +671,7 @@ discard block |
||
671 | 671 | ) |
672 | 672 | ); |
673 | 673 | } |
674 | - $this->_addressees[ $context ] = call_user_func(array($this, $xpctd_method)); |
|
674 | + $this->_addressees[$context] = call_user_func(array($this, $xpctd_method)); |
|
675 | 675 | } |
676 | 676 | return true; // data was processed successfully. |
677 | 677 | } |
@@ -707,7 +707,7 @@ discard block |
||
707 | 707 | 'total_ticket_count' => $this->_data->total_ticket_count, |
708 | 708 | ); |
709 | 709 | if (is_array($this->_data->primary_attendee_data)) { |
710 | - $this->_default_addressee_data = array_merge( |
|
710 | + $this->_default_addressee_data = array_merge( |
|
711 | 711 | $this->_default_addressee_data, |
712 | 712 | $this->_data->primary_attendee_data |
713 | 713 | ); |
@@ -750,25 +750,25 @@ discard block |
||
750 | 750 | 'email', |
751 | 751 | 'messenger', |
752 | 752 | ); |
753 | - $contexts = $this->get_contexts(); |
|
753 | + $contexts = $this->get_contexts(); |
|
754 | 754 | foreach ($contexts as $context => $details) { |
755 | - $this->_valid_shortcodes[ $context ] = $all_shortcodes; |
|
755 | + $this->_valid_shortcodes[$context] = $all_shortcodes; |
|
756 | 756 | // make sure non admin context does not include the event_author shortcodes |
757 | 757 | if ($context != 'admin') { |
758 | - if (($key = array_search('event_author', $this->_valid_shortcodes[ $context ])) !== false) { |
|
759 | - unset($this->_valid_shortcodes[ $context ][ $key ]); |
|
758 | + if (($key = array_search('event_author', $this->_valid_shortcodes[$context])) !== false) { |
|
759 | + unset($this->_valid_shortcodes[$context][$key]); |
|
760 | 760 | } |
761 | 761 | } |
762 | 762 | } |
763 | 763 | // make sure admin context does not include the recipient_details shortcodes |
764 | 764 | // IF we have admin context hooked in message types might not have that context. |
765 | - if (! empty($this->_valid_shortcodes['admin'])) { |
|
765 | + if ( ! empty($this->_valid_shortcodes['admin'])) { |
|
766 | 766 | if (($key = array_search('recipient_details', $this->_valid_shortcodes['admin'])) !== false) { |
767 | - unset($this->_valid_shortcodes['admin'][ $key ]); |
|
767 | + unset($this->_valid_shortcodes['admin'][$key]); |
|
768 | 768 | } |
769 | 769 | // make sure admin context does not include the recipient_details shortcodes |
770 | 770 | if (($key = array_search('recipient_list', $this->_valid_shortcodes['admin'])) !== false) { |
771 | - unset($this->_valid_shortcodes['admin'][ $key ]); |
|
771 | + unset($this->_valid_shortcodes['admin'][$key]); |
|
772 | 772 | } |
773 | 773 | } |
774 | 774 | } |
@@ -783,7 +783,7 @@ discard block |
||
783 | 783 | public function reset_valid_shortcodes_config($new_config) |
784 | 784 | { |
785 | 785 | foreach ($new_config as $context => $shortcodes) { |
786 | - $this->_valid_shortcodes[ $context ] = $shortcodes; |
|
786 | + $this->_valid_shortcodes[$context] = $shortcodes; |
|
787 | 787 | } |
788 | 788 | } |
789 | 789 | |
@@ -807,11 +807,11 @@ discard block |
||
807 | 807 | foreach ($this->_data->events as $line_ref => $event) { |
808 | 808 | $admin_id = $this->_get_event_admin_id($event['ID']); |
809 | 809 | // make sure we are just including the events that belong to this admin! |
810 | - $admin_events[ $admin_id ][ $line_ref ] = $event; |
|
810 | + $admin_events[$admin_id][$line_ref] = $event; |
|
811 | 811 | } |
812 | 812 | // k now we can loop through the event_admins and setup the addressee data. |
813 | 813 | foreach ($admin_events as $admin_id => $event_details) { |
814 | - $aee = array( |
|
814 | + $aee = array( |
|
815 | 815 | 'user_id' => $admin_id, |
816 | 816 | 'events' => $event_details, |
817 | 817 | 'attendees' => $this->_data->attendees, |
@@ -869,7 +869,7 @@ discard block |
||
869 | 869 | } |
870 | 870 | // is $this->_regs_for_sending present? |
871 | 871 | // If so, let's make sure we ONLY generate addressee for registrations in that array. |
872 | - if (! empty($this->_regs_for_sending) && is_array($this->_regs_for_sending)) { |
|
872 | + if ( ! empty($this->_regs_for_sending) && is_array($this->_regs_for_sending)) { |
|
873 | 873 | $regs_allowed = array_intersect_key(array_flip($this->_regs_for_sending), $details['reg_objs']); |
874 | 874 | if (empty($regs_allowed)) { |
875 | 875 | continue; |
@@ -888,10 +888,10 @@ discard block |
||
888 | 888 | } |
889 | 889 | $already_processed[] = $att_id; |
890 | 890 | foreach ($details as $item => $value) { |
891 | - $aee[ $item ] = $value; |
|
891 | + $aee[$item] = $value; |
|
892 | 892 | if ($item === 'line_ref') { |
893 | 893 | foreach ($value as $event_id) { |
894 | - $aee['events'][ $event_id ] = $this->_data->events[ $event_id ]; |
|
894 | + $aee['events'][$event_id] = $this->_data->events[$event_id]; |
|
895 | 895 | } |
896 | 896 | } |
897 | 897 | if ($item === 'attendee_email') { |
@@ -903,7 +903,7 @@ discard block |
||
903 | 903 | } |
904 | 904 | // note the FIRST reg object in this array is the one |
905 | 905 | // we'll use for this attendee as the primary registration for this attendee. |
906 | - $aee['reg_obj'] = reset($this->_data->attendees[ $att_id ]['reg_objs']); |
|
906 | + $aee['reg_obj'] = reset($this->_data->attendees[$att_id]['reg_objs']); |
|
907 | 907 | $aee['attendees'] = $this->_data->attendees; |
908 | 908 | $aee['recipient_id'] = $att_id; |
909 | 909 | $aee['recipient_type'] = 'Attendee'; |
@@ -15,105 +15,105 @@ |
||
15 | 15 | { |
16 | 16 | |
17 | 17 | |
18 | - /** |
|
19 | - * @type EE_Message_Type_Collection $_message_type_collection |
|
20 | - */ |
|
21 | - protected $_message_type_collection = null; |
|
22 | - |
|
23 | - |
|
24 | - |
|
25 | - /** |
|
26 | - * EE_Message_Type_Collection_Loader constructor. |
|
27 | - * |
|
28 | - * @param EE_Message_Type_Collection $message_types |
|
29 | - */ |
|
30 | - public function __construct(EE_Message_Type_Collection $message_types) |
|
31 | - { |
|
32 | - $this->set_message_type_collection($message_types); |
|
33 | - } |
|
34 | - |
|
35 | - |
|
36 | - |
|
37 | - /** |
|
38 | - * @return EE_Message_Type_Collection |
|
39 | - */ |
|
40 | - public function message_type_collection() |
|
41 | - { |
|
42 | - return $this->_message_type_collection; |
|
43 | - } |
|
44 | - |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * @param mixed $message_types |
|
49 | - */ |
|
50 | - public function set_message_type_collection(EE_Message_Type_Collection $message_types) |
|
51 | - { |
|
52 | - $this->_message_type_collection = $message_types; |
|
53 | - } |
|
54 | - |
|
55 | - |
|
56 | - |
|
57 | - /** |
|
58 | - * load_message_types |
|
59 | - * globs the supplied filepath and adds any found |
|
60 | - * |
|
61 | - * @param string $folder |
|
62 | - * @throws \EE_Error |
|
63 | - */ |
|
64 | - public function load_message_types_from_folder($folder = '') |
|
65 | - { |
|
66 | - // make sure autoloaders are set (fail-safe) |
|
67 | - EED_Messages::set_autoloaders(); |
|
68 | - $folder = ! empty($folder) ? $folder : EE_LIBRARIES . 'messages/message_type'; |
|
69 | - $folder .= $folder[ strlen($folder) - 1 ] != '/' ? '/' : ''; |
|
70 | - // get all the files in that folder that end in ".class.php |
|
71 | - $filepaths = apply_filters( |
|
72 | - 'FHEE__EE_messages__get_installed__messagetype_files', |
|
73 | - glob($folder . '*.class.php') |
|
74 | - ); |
|
75 | - if (empty($filepaths)) { |
|
76 | - return; |
|
77 | - } |
|
78 | - foreach ((array) $filepaths as $file_path) { |
|
79 | - // extract filename from path |
|
80 | - $file_path = basename($file_path); |
|
81 | - // now remove any file extensions |
|
82 | - $message_type_class_name = substr($file_path, 0, strpos($file_path, '.')); |
|
83 | - |
|
84 | - // if this class name doesn't represent a message type class, then we just skip. |
|
85 | - if (strpos(strtolower($message_type_class_name), 'message_type') === false) { |
|
86 | - continue; |
|
87 | - } |
|
88 | - |
|
89 | - if (! class_exists($message_type_class_name)) { |
|
90 | - throw new EE_Error( |
|
91 | - sprintf( |
|
92 | - esc_html__('The "%1$s" message type class can\'t be loaded from %2$s. Likely there is a typo in the class name or the file name.', 'event_espresso'), |
|
93 | - $message_type_class_name, |
|
94 | - $file_path |
|
95 | - ) |
|
96 | - ); |
|
97 | - } |
|
98 | - |
|
99 | - $this->_load_message_type(new $message_type_class_name()); |
|
100 | - } |
|
101 | - } |
|
102 | - |
|
103 | - |
|
104 | - /** |
|
105 | - * Loads the given message type into the message type collection if it doesn't already exist. |
|
106 | - * @param EE_message_type $message_type |
|
107 | - * @return bool |
|
108 | - */ |
|
109 | - protected function _load_message_type(EE_message_type $message_type) |
|
110 | - { |
|
111 | - if ($this->message_type_collection()->has_by_name($message_type->name)) { |
|
112 | - return true; |
|
113 | - } |
|
114 | - return $this->message_type_collection()->add( |
|
115 | - $message_type, |
|
116 | - $message_type->name |
|
117 | - ); |
|
118 | - } |
|
18 | + /** |
|
19 | + * @type EE_Message_Type_Collection $_message_type_collection |
|
20 | + */ |
|
21 | + protected $_message_type_collection = null; |
|
22 | + |
|
23 | + |
|
24 | + |
|
25 | + /** |
|
26 | + * EE_Message_Type_Collection_Loader constructor. |
|
27 | + * |
|
28 | + * @param EE_Message_Type_Collection $message_types |
|
29 | + */ |
|
30 | + public function __construct(EE_Message_Type_Collection $message_types) |
|
31 | + { |
|
32 | + $this->set_message_type_collection($message_types); |
|
33 | + } |
|
34 | + |
|
35 | + |
|
36 | + |
|
37 | + /** |
|
38 | + * @return EE_Message_Type_Collection |
|
39 | + */ |
|
40 | + public function message_type_collection() |
|
41 | + { |
|
42 | + return $this->_message_type_collection; |
|
43 | + } |
|
44 | + |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * @param mixed $message_types |
|
49 | + */ |
|
50 | + public function set_message_type_collection(EE_Message_Type_Collection $message_types) |
|
51 | + { |
|
52 | + $this->_message_type_collection = $message_types; |
|
53 | + } |
|
54 | + |
|
55 | + |
|
56 | + |
|
57 | + /** |
|
58 | + * load_message_types |
|
59 | + * globs the supplied filepath and adds any found |
|
60 | + * |
|
61 | + * @param string $folder |
|
62 | + * @throws \EE_Error |
|
63 | + */ |
|
64 | + public function load_message_types_from_folder($folder = '') |
|
65 | + { |
|
66 | + // make sure autoloaders are set (fail-safe) |
|
67 | + EED_Messages::set_autoloaders(); |
|
68 | + $folder = ! empty($folder) ? $folder : EE_LIBRARIES . 'messages/message_type'; |
|
69 | + $folder .= $folder[ strlen($folder) - 1 ] != '/' ? '/' : ''; |
|
70 | + // get all the files in that folder that end in ".class.php |
|
71 | + $filepaths = apply_filters( |
|
72 | + 'FHEE__EE_messages__get_installed__messagetype_files', |
|
73 | + glob($folder . '*.class.php') |
|
74 | + ); |
|
75 | + if (empty($filepaths)) { |
|
76 | + return; |
|
77 | + } |
|
78 | + foreach ((array) $filepaths as $file_path) { |
|
79 | + // extract filename from path |
|
80 | + $file_path = basename($file_path); |
|
81 | + // now remove any file extensions |
|
82 | + $message_type_class_name = substr($file_path, 0, strpos($file_path, '.')); |
|
83 | + |
|
84 | + // if this class name doesn't represent a message type class, then we just skip. |
|
85 | + if (strpos(strtolower($message_type_class_name), 'message_type') === false) { |
|
86 | + continue; |
|
87 | + } |
|
88 | + |
|
89 | + if (! class_exists($message_type_class_name)) { |
|
90 | + throw new EE_Error( |
|
91 | + sprintf( |
|
92 | + esc_html__('The "%1$s" message type class can\'t be loaded from %2$s. Likely there is a typo in the class name or the file name.', 'event_espresso'), |
|
93 | + $message_type_class_name, |
|
94 | + $file_path |
|
95 | + ) |
|
96 | + ); |
|
97 | + } |
|
98 | + |
|
99 | + $this->_load_message_type(new $message_type_class_name()); |
|
100 | + } |
|
101 | + } |
|
102 | + |
|
103 | + |
|
104 | + /** |
|
105 | + * Loads the given message type into the message type collection if it doesn't already exist. |
|
106 | + * @param EE_message_type $message_type |
|
107 | + * @return bool |
|
108 | + */ |
|
109 | + protected function _load_message_type(EE_message_type $message_type) |
|
110 | + { |
|
111 | + if ($this->message_type_collection()->has_by_name($message_type->name)) { |
|
112 | + return true; |
|
113 | + } |
|
114 | + return $this->message_type_collection()->add( |
|
115 | + $message_type, |
|
116 | + $message_type->name |
|
117 | + ); |
|
118 | + } |
|
119 | 119 | } |
@@ -65,12 +65,12 @@ discard block |
||
65 | 65 | { |
66 | 66 | // make sure autoloaders are set (fail-safe) |
67 | 67 | EED_Messages::set_autoloaders(); |
68 | - $folder = ! empty($folder) ? $folder : EE_LIBRARIES . 'messages/message_type'; |
|
69 | - $folder .= $folder[ strlen($folder) - 1 ] != '/' ? '/' : ''; |
|
68 | + $folder = ! empty($folder) ? $folder : EE_LIBRARIES.'messages/message_type'; |
|
69 | + $folder .= $folder[strlen($folder) - 1] != '/' ? '/' : ''; |
|
70 | 70 | // get all the files in that folder that end in ".class.php |
71 | 71 | $filepaths = apply_filters( |
72 | 72 | 'FHEE__EE_messages__get_installed__messagetype_files', |
73 | - glob($folder . '*.class.php') |
|
73 | + glob($folder.'*.class.php') |
|
74 | 74 | ); |
75 | 75 | if (empty($filepaths)) { |
76 | 76 | return; |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | continue; |
87 | 87 | } |
88 | 88 | |
89 | - if (! class_exists($message_type_class_name)) { |
|
89 | + if ( ! class_exists($message_type_class_name)) { |
|
90 | 90 | throw new EE_Error( |
91 | 91 | sprintf( |
92 | 92 | esc_html__('The "%1$s" message type class can\'t be loaded from %2$s. Likely there is a typo in the class name or the file name.', 'event_espresso'), |
@@ -16,127 +16,127 @@ |
||
16 | 16 | |
17 | 17 | |
18 | 18 | |
19 | - /** |
|
20 | - * @type EE_Messenger_Collection $_messenger_collection |
|
21 | - */ |
|
22 | - protected $_messenger_collection = null; |
|
23 | - |
|
24 | - |
|
25 | - |
|
26 | - /** |
|
27 | - * EE_Messenger_Collection_Loader constructor. |
|
28 | - * |
|
29 | - * @param EE_Messenger_Collection $messengers |
|
30 | - */ |
|
31 | - public function __construct(EE_Messenger_Collection $messengers) |
|
32 | - { |
|
33 | - $this->set_messenger_collection($messengers); |
|
34 | - } |
|
35 | - |
|
36 | - |
|
37 | - |
|
38 | - /** |
|
39 | - * @return EE_Messenger_Collection |
|
40 | - */ |
|
41 | - public function messenger_collection() |
|
42 | - { |
|
43 | - return $this->_messenger_collection; |
|
44 | - } |
|
45 | - |
|
46 | - |
|
47 | - |
|
48 | - /** |
|
49 | - * @param mixed $messengers |
|
50 | - */ |
|
51 | - public function set_messenger_collection(EE_Messenger_Collection $messengers) |
|
52 | - { |
|
53 | - $this->_messenger_collection = $messengers; |
|
54 | - } |
|
55 | - |
|
56 | - |
|
57 | - |
|
58 | - /** |
|
59 | - * load_messengers |
|
60 | - * globs the supplied filepath and adds any found |
|
61 | - * |
|
62 | - * @param string $folder |
|
63 | - * @throws \EE_Error |
|
64 | - */ |
|
65 | - public function load_messengers_from_folder($folder = '') |
|
66 | - { |
|
67 | - // make sure autoloaders are set (fail-safe) |
|
68 | - EED_Messages::set_autoloaders(); |
|
69 | - $folder = ! empty($folder) ? $folder : EE_LIBRARIES . 'messages/messenger'; |
|
70 | - $folder .= $folder[ strlen($folder) - 1 ] != '/' ? '/' : ''; |
|
71 | - // get all the files in that folder that end in ".class.php |
|
72 | - $filepaths = apply_filters( |
|
73 | - 'FHEE__EE_messages__get_installed__messenger_files', |
|
74 | - glob($folder . '*.class.php') |
|
75 | - ); |
|
76 | - if (empty($filepaths)) { |
|
77 | - return; |
|
78 | - } |
|
79 | - foreach ((array) $filepaths as $file_path) { |
|
80 | - // extract filename from path |
|
81 | - $file_path = basename($file_path); |
|
82 | - // now remove any file extensions |
|
83 | - $messenger_class_name = substr($file_path, 0, strpos($file_path, '.')); |
|
84 | - |
|
85 | - // first check to see if the class name represents an actual messenger class. |
|
86 | - if (strpos(strtolower($messenger_class_name), 'messenger') === false) { |
|
87 | - continue; |
|
88 | - } |
|
89 | - |
|
90 | - if (! class_exists($messenger_class_name)) { |
|
91 | - throw new EE_Error( |
|
92 | - sprintf( |
|
93 | - esc_html__('The "%1$s" messenger class can\'t be loaded from %2$s. Likely there is a typo in the class name or the file name.', 'event_espresso'), |
|
94 | - $messenger_class_name, |
|
95 | - $file_path |
|
96 | - ) |
|
97 | - ); |
|
98 | - } |
|
99 | - |
|
100 | - $this->_load_messenger(new $messenger_class_name()); |
|
101 | - } |
|
102 | - } |
|
103 | - |
|
104 | - |
|
105 | - /** |
|
106 | - * load_messengers |
|
107 | - * globs the supplied filepath and adds any found |
|
108 | - * |
|
109 | - * @return array |
|
110 | - */ |
|
111 | - public function load_active_messengers_from_db() |
|
112 | - { |
|
113 | - // make sure autoloaders are set (fail-safe) |
|
114 | - EED_Messages::set_autoloaders(); |
|
115 | - $active_messengers = apply_filters( |
|
116 | - 'FHEE__EEH_MSG_Template__get_active_messengers_in_db', |
|
117 | - get_option('ee_active_messengers', array()) |
|
118 | - ); |
|
119 | - foreach ((array) $active_messengers as $active_messenger_classname => $active_messenger) { |
|
120 | - $this->_load_messenger($active_messenger); |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - |
|
125 | - |
|
126 | - /** |
|
127 | - * load_messenger |
|
128 | - * |
|
129 | - * @param \EE_messenger $messenger |
|
130 | - * @return bool |
|
131 | - */ |
|
132 | - protected function _load_messenger(EE_messenger $messenger) |
|
133 | - { |
|
134 | - if ($this->messenger_collection()->has_by_name($messenger->name)) { |
|
135 | - return true; |
|
136 | - } |
|
137 | - return $this->messenger_collection()->add( |
|
138 | - $messenger, |
|
139 | - $messenger->name |
|
140 | - ); |
|
141 | - } |
|
19 | + /** |
|
20 | + * @type EE_Messenger_Collection $_messenger_collection |
|
21 | + */ |
|
22 | + protected $_messenger_collection = null; |
|
23 | + |
|
24 | + |
|
25 | + |
|
26 | + /** |
|
27 | + * EE_Messenger_Collection_Loader constructor. |
|
28 | + * |
|
29 | + * @param EE_Messenger_Collection $messengers |
|
30 | + */ |
|
31 | + public function __construct(EE_Messenger_Collection $messengers) |
|
32 | + { |
|
33 | + $this->set_messenger_collection($messengers); |
|
34 | + } |
|
35 | + |
|
36 | + |
|
37 | + |
|
38 | + /** |
|
39 | + * @return EE_Messenger_Collection |
|
40 | + */ |
|
41 | + public function messenger_collection() |
|
42 | + { |
|
43 | + return $this->_messenger_collection; |
|
44 | + } |
|
45 | + |
|
46 | + |
|
47 | + |
|
48 | + /** |
|
49 | + * @param mixed $messengers |
|
50 | + */ |
|
51 | + public function set_messenger_collection(EE_Messenger_Collection $messengers) |
|
52 | + { |
|
53 | + $this->_messenger_collection = $messengers; |
|
54 | + } |
|
55 | + |
|
56 | + |
|
57 | + |
|
58 | + /** |
|
59 | + * load_messengers |
|
60 | + * globs the supplied filepath and adds any found |
|
61 | + * |
|
62 | + * @param string $folder |
|
63 | + * @throws \EE_Error |
|
64 | + */ |
|
65 | + public function load_messengers_from_folder($folder = '') |
|
66 | + { |
|
67 | + // make sure autoloaders are set (fail-safe) |
|
68 | + EED_Messages::set_autoloaders(); |
|
69 | + $folder = ! empty($folder) ? $folder : EE_LIBRARIES . 'messages/messenger'; |
|
70 | + $folder .= $folder[ strlen($folder) - 1 ] != '/' ? '/' : ''; |
|
71 | + // get all the files in that folder that end in ".class.php |
|
72 | + $filepaths = apply_filters( |
|
73 | + 'FHEE__EE_messages__get_installed__messenger_files', |
|
74 | + glob($folder . '*.class.php') |
|
75 | + ); |
|
76 | + if (empty($filepaths)) { |
|
77 | + return; |
|
78 | + } |
|
79 | + foreach ((array) $filepaths as $file_path) { |
|
80 | + // extract filename from path |
|
81 | + $file_path = basename($file_path); |
|
82 | + // now remove any file extensions |
|
83 | + $messenger_class_name = substr($file_path, 0, strpos($file_path, '.')); |
|
84 | + |
|
85 | + // first check to see if the class name represents an actual messenger class. |
|
86 | + if (strpos(strtolower($messenger_class_name), 'messenger') === false) { |
|
87 | + continue; |
|
88 | + } |
|
89 | + |
|
90 | + if (! class_exists($messenger_class_name)) { |
|
91 | + throw new EE_Error( |
|
92 | + sprintf( |
|
93 | + esc_html__('The "%1$s" messenger class can\'t be loaded from %2$s. Likely there is a typo in the class name or the file name.', 'event_espresso'), |
|
94 | + $messenger_class_name, |
|
95 | + $file_path |
|
96 | + ) |
|
97 | + ); |
|
98 | + } |
|
99 | + |
|
100 | + $this->_load_messenger(new $messenger_class_name()); |
|
101 | + } |
|
102 | + } |
|
103 | + |
|
104 | + |
|
105 | + /** |
|
106 | + * load_messengers |
|
107 | + * globs the supplied filepath and adds any found |
|
108 | + * |
|
109 | + * @return array |
|
110 | + */ |
|
111 | + public function load_active_messengers_from_db() |
|
112 | + { |
|
113 | + // make sure autoloaders are set (fail-safe) |
|
114 | + EED_Messages::set_autoloaders(); |
|
115 | + $active_messengers = apply_filters( |
|
116 | + 'FHEE__EEH_MSG_Template__get_active_messengers_in_db', |
|
117 | + get_option('ee_active_messengers', array()) |
|
118 | + ); |
|
119 | + foreach ((array) $active_messengers as $active_messenger_classname => $active_messenger) { |
|
120 | + $this->_load_messenger($active_messenger); |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + |
|
125 | + |
|
126 | + /** |
|
127 | + * load_messenger |
|
128 | + * |
|
129 | + * @param \EE_messenger $messenger |
|
130 | + * @return bool |
|
131 | + */ |
|
132 | + protected function _load_messenger(EE_messenger $messenger) |
|
133 | + { |
|
134 | + if ($this->messenger_collection()->has_by_name($messenger->name)) { |
|
135 | + return true; |
|
136 | + } |
|
137 | + return $this->messenger_collection()->add( |
|
138 | + $messenger, |
|
139 | + $messenger->name |
|
140 | + ); |
|
141 | + } |
|
142 | 142 | } |
@@ -66,12 +66,12 @@ discard block |
||
66 | 66 | { |
67 | 67 | // make sure autoloaders are set (fail-safe) |
68 | 68 | EED_Messages::set_autoloaders(); |
69 | - $folder = ! empty($folder) ? $folder : EE_LIBRARIES . 'messages/messenger'; |
|
70 | - $folder .= $folder[ strlen($folder) - 1 ] != '/' ? '/' : ''; |
|
69 | + $folder = ! empty($folder) ? $folder : EE_LIBRARIES.'messages/messenger'; |
|
70 | + $folder .= $folder[strlen($folder) - 1] != '/' ? '/' : ''; |
|
71 | 71 | // get all the files in that folder that end in ".class.php |
72 | 72 | $filepaths = apply_filters( |
73 | 73 | 'FHEE__EE_messages__get_installed__messenger_files', |
74 | - glob($folder . '*.class.php') |
|
74 | + glob($folder.'*.class.php') |
|
75 | 75 | ); |
76 | 76 | if (empty($filepaths)) { |
77 | 77 | return; |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | continue; |
88 | 88 | } |
89 | 89 | |
90 | - if (! class_exists($messenger_class_name)) { |
|
90 | + if ( ! class_exists($messenger_class_name)) { |
|
91 | 91 | throw new EE_Error( |
92 | 92 | sprintf( |
93 | 93 | esc_html__('The "%1$s" messenger class can\'t be loaded from %2$s. Likely there is a typo in the class name or the file name.', 'event_espresso'), |
@@ -15,232 +15,232 @@ |
||
15 | 15 | { |
16 | 16 | |
17 | 17 | |
18 | - /** |
|
19 | - * Used for holding the EE_Message_Template GRP_ID field value. |
|
20 | - * @var [type] |
|
21 | - */ |
|
22 | - protected $_GRP_ID; |
|
23 | - |
|
24 | - /** |
|
25 | - * holds the messenger object |
|
26 | - * |
|
27 | - * @var EE_messenger |
|
28 | - */ |
|
29 | - protected $_messenger; |
|
30 | - |
|
31 | - /** |
|
32 | - * holds the message type object |
|
33 | - * |
|
34 | - * @var EE_message_type |
|
35 | - */ |
|
36 | - protected $_message_type; |
|
37 | - |
|
38 | - /** |
|
39 | - * holds the fields used (this is retrieved from the messenger) |
|
40 | - * |
|
41 | - * @var array |
|
42 | - */ |
|
43 | - protected $_fields; |
|
44 | - |
|
45 | - /** |
|
46 | - * holds the assembled template (with defaults) for creation in the database |
|
47 | - * |
|
48 | - * @var array |
|
49 | - */ |
|
50 | - protected $_templates; |
|
51 | - |
|
52 | - /** |
|
53 | - * holds the contexts used (this is retrieved from the message type) |
|
54 | - * |
|
55 | - * @var array |
|
56 | - */ |
|
57 | - protected $_contexts; |
|
58 | - |
|
59 | - |
|
60 | - /** |
|
61 | - * @var EEM_Message_Template_Group |
|
62 | - */ |
|
63 | - protected $_message_template_group_model; |
|
64 | - |
|
65 | - |
|
66 | - /** |
|
67 | - * @var EEM_Message_Template |
|
68 | - */ |
|
69 | - protected $_message_template_model; |
|
70 | - |
|
71 | - |
|
72 | - /** |
|
73 | - * EE_Messages_Template_Defaults constructor. |
|
74 | - * |
|
75 | - * @param EE_messenger $messenger |
|
76 | - * @param EE_message_type $message_type |
|
77 | - * @param int $GRP_ID Optional. If included then we're just regenerating |
|
78 | - * the template fields for the given group not the |
|
79 | - * message template group itself |
|
80 | - * @param EEM_Message_Template_Group $message_template_group_model |
|
81 | - * @param EEM_Message_Template $message_template_model |
|
82 | - * @throws EE_Error |
|
83 | - */ |
|
84 | - public function __construct( |
|
85 | - EE_messenger $messenger, |
|
86 | - EE_message_type $message_type, |
|
87 | - $GRP_ID = 0, |
|
88 | - EEM_Message_Template_Group $message_template_group_model, |
|
89 | - EEM_Message_Template $message_template_model |
|
90 | - ) { |
|
91 | - $this->_messenger = $messenger; |
|
92 | - $this->_message_type = $message_type; |
|
93 | - $this->_GRP_ID = $GRP_ID; |
|
94 | - // set the model object |
|
95 | - $this->_message_template_group_model = $message_template_group_model; |
|
96 | - $this->_message_template_model = $message_template_model; |
|
97 | - $this->_fields = $this->_messenger->get_template_fields(); |
|
98 | - $this->_contexts = $this->_message_type->get_contexts(); |
|
99 | - } |
|
100 | - |
|
101 | - |
|
102 | - /** |
|
103 | - * Setup the _template_data property. |
|
104 | - * This method sets the _templates property array before templates are created. |
|
105 | - * |
|
106 | - * @param string $template_pack This corresponds to a template pack class reference which will contain information |
|
107 | - * about where to obtain the templates. |
|
108 | - * |
|
109 | - */ |
|
110 | - final private function _set_templates($template_pack) |
|
111 | - { |
|
112 | - |
|
113 | - // get the corresponding template pack object (if present. If not then we just load the default and add a |
|
114 | - // notice). The class name should be something like 'EE_Messages_Template_Pack_Default' where "default' would be |
|
115 | - // the incoming template pack reference. |
|
116 | - $class_name = 'EE_Messages_Template_Pack_' . str_replace(' ', '_', ucwords(str_replace('_', ' ', $template_pack))); |
|
117 | - |
|
118 | - if (! class_exists($class_name)) { |
|
119 | - EE_Error::add_error( |
|
120 | - sprintf( |
|
121 | - esc_html__( |
|
122 | - 'The template pack represented by a class corresponding to "%1$s" does not exist. Likely the autoloader for this class has the wrong path or the incoming reference is misspelled. The default template pack has been used to generate the templates instead.', |
|
123 | - 'event_espresso' |
|
124 | - ), |
|
125 | - $class_name |
|
126 | - ), |
|
127 | - __FILE__, |
|
128 | - __FUNCTION__, |
|
129 | - __LINE__ |
|
130 | - ); |
|
131 | - $class_name = 'EE_Messages_Template_Pack_Default'; |
|
132 | - } |
|
133 | - /** @type EE_Messages_Template_Pack $template_pack */ |
|
134 | - $template_pack = new $class_name(); |
|
135 | - |
|
136 | - // get all the templates from the template pack. |
|
137 | - $this->_templates = $template_pack->get_templates($this->_messenger, $this->_message_type); |
|
138 | - } |
|
139 | - |
|
140 | - |
|
141 | - /** |
|
142 | - * Return the contexts for the message type as cached on this instance. |
|
143 | - * @return array |
|
144 | - */ |
|
145 | - public function get_contexts() |
|
146 | - { |
|
147 | - return $this->_contexts; |
|
148 | - } |
|
149 | - |
|
150 | - |
|
151 | - |
|
152 | - |
|
153 | - |
|
154 | - /** |
|
155 | - * public facing create new templates method |
|
156 | - * |
|
157 | - * @return mixed (array|bool) success array or false. |
|
158 | - */ |
|
159 | - public function create_new_templates() |
|
160 | - { |
|
161 | - $template_pack = 'default'; |
|
162 | - // if we have the GRP_ID then let's use that to see if there is a set template pack and use that for the new templates. |
|
163 | - if (! empty($this->_GRP_ID)) { |
|
164 | - $message_template_group = $this->_message_template_group_model->get_one_by_ID($this->_GRP_ID); |
|
165 | - $template_pack = $message_template_group instanceof EE_Message_Template_Group |
|
166 | - ? $message_template_group->get_template_pack_name() |
|
167 | - : 'default'; |
|
168 | - // we also need to reset the template variation to default |
|
169 | - $message_template_group->set_template_pack_variation('default'); |
|
170 | - } |
|
171 | - return $this->_create_new_templates($template_pack); |
|
172 | - } |
|
173 | - |
|
174 | - |
|
175 | - |
|
176 | - |
|
177 | - |
|
178 | - /** |
|
179 | - * Handles creating new default templates. |
|
180 | - * |
|
181 | - * @param string $template_pack This corresponds to a template pack class reference |
|
182 | - * which will contain information about where to obtain the templates. |
|
183 | - * @return mixed (array|bool) success array or false. |
|
184 | - */ |
|
185 | - protected function _create_new_templates($template_pack) |
|
186 | - { |
|
187 | - |
|
188 | - $this->_set_templates($template_pack); |
|
189 | - |
|
190 | - // necessary properties are set, let's save the default templates |
|
191 | - if (empty($this->_GRP_ID)) { |
|
192 | - $main_template_data = array( |
|
193 | - 'MTP_messenger' => $this->_messenger->name, |
|
194 | - 'MTP_message_type' => $this->_message_type->name, |
|
195 | - 'MTP_is_override' => 0, |
|
196 | - 'MTP_deleted' => 0, |
|
197 | - 'MTP_is_global' => 1, |
|
198 | - 'MTP_user_id' => EEH_Activation::get_default_creator_id(), |
|
199 | - 'MTP_is_active' => 1, |
|
200 | - ); |
|
201 | - // let's insert the above and get our GRP_ID, then reset the template data array to just include the GRP_ID |
|
202 | - $grp_id = $this->_message_template_group_model->insert($main_template_data); |
|
203 | - if (empty($grp_id)) { |
|
204 | - return $grp_id; |
|
205 | - } |
|
206 | - $this->_GRP_ID = $grp_id; |
|
207 | - } |
|
208 | - |
|
209 | - $template_data = array( 'GRP_ID' => $this->_GRP_ID ); |
|
210 | - |
|
211 | - foreach ($this->_contexts as $context => $details) { |
|
212 | - foreach ($this->_fields as $field => $field_type) { |
|
213 | - if ($field != 'extra') { |
|
214 | - $template_data['MTP_context'] = $context; |
|
215 | - $template_data['MTP_template_field'] = $field; |
|
216 | - $template_data['MTP_content'] = $this->_templates[ $context ][ $field ]; |
|
217 | - |
|
218 | - $MTP = $this->_message_template_model->insert($template_data); |
|
219 | - if (! $MTP) { |
|
220 | - EE_Error::add_error( |
|
221 | - sprintf( |
|
222 | - esc_html__( |
|
223 | - 'There was an error in saving new template data for %1$s messenger, %2$s message type, %3$s context and %4$s template field.', |
|
224 | - 'event_espresso' |
|
225 | - ), |
|
226 | - $this->_messenger->name, |
|
227 | - $this->_message_type->name, |
|
228 | - $context, |
|
229 | - $field |
|
230 | - ), |
|
231 | - __FILE__, |
|
232 | - __FUNCTION__, |
|
233 | - __LINE__ |
|
234 | - ); |
|
235 | - return false; |
|
236 | - } |
|
237 | - } |
|
238 | - } |
|
239 | - } |
|
240 | - |
|
241 | - return array( |
|
242 | - 'GRP_ID' => $this->_GRP_ID, |
|
243 | - 'MTP_context' => key($this->_contexts) |
|
244 | - ); |
|
245 | - } |
|
18 | + /** |
|
19 | + * Used for holding the EE_Message_Template GRP_ID field value. |
|
20 | + * @var [type] |
|
21 | + */ |
|
22 | + protected $_GRP_ID; |
|
23 | + |
|
24 | + /** |
|
25 | + * holds the messenger object |
|
26 | + * |
|
27 | + * @var EE_messenger |
|
28 | + */ |
|
29 | + protected $_messenger; |
|
30 | + |
|
31 | + /** |
|
32 | + * holds the message type object |
|
33 | + * |
|
34 | + * @var EE_message_type |
|
35 | + */ |
|
36 | + protected $_message_type; |
|
37 | + |
|
38 | + /** |
|
39 | + * holds the fields used (this is retrieved from the messenger) |
|
40 | + * |
|
41 | + * @var array |
|
42 | + */ |
|
43 | + protected $_fields; |
|
44 | + |
|
45 | + /** |
|
46 | + * holds the assembled template (with defaults) for creation in the database |
|
47 | + * |
|
48 | + * @var array |
|
49 | + */ |
|
50 | + protected $_templates; |
|
51 | + |
|
52 | + /** |
|
53 | + * holds the contexts used (this is retrieved from the message type) |
|
54 | + * |
|
55 | + * @var array |
|
56 | + */ |
|
57 | + protected $_contexts; |
|
58 | + |
|
59 | + |
|
60 | + /** |
|
61 | + * @var EEM_Message_Template_Group |
|
62 | + */ |
|
63 | + protected $_message_template_group_model; |
|
64 | + |
|
65 | + |
|
66 | + /** |
|
67 | + * @var EEM_Message_Template |
|
68 | + */ |
|
69 | + protected $_message_template_model; |
|
70 | + |
|
71 | + |
|
72 | + /** |
|
73 | + * EE_Messages_Template_Defaults constructor. |
|
74 | + * |
|
75 | + * @param EE_messenger $messenger |
|
76 | + * @param EE_message_type $message_type |
|
77 | + * @param int $GRP_ID Optional. If included then we're just regenerating |
|
78 | + * the template fields for the given group not the |
|
79 | + * message template group itself |
|
80 | + * @param EEM_Message_Template_Group $message_template_group_model |
|
81 | + * @param EEM_Message_Template $message_template_model |
|
82 | + * @throws EE_Error |
|
83 | + */ |
|
84 | + public function __construct( |
|
85 | + EE_messenger $messenger, |
|
86 | + EE_message_type $message_type, |
|
87 | + $GRP_ID = 0, |
|
88 | + EEM_Message_Template_Group $message_template_group_model, |
|
89 | + EEM_Message_Template $message_template_model |
|
90 | + ) { |
|
91 | + $this->_messenger = $messenger; |
|
92 | + $this->_message_type = $message_type; |
|
93 | + $this->_GRP_ID = $GRP_ID; |
|
94 | + // set the model object |
|
95 | + $this->_message_template_group_model = $message_template_group_model; |
|
96 | + $this->_message_template_model = $message_template_model; |
|
97 | + $this->_fields = $this->_messenger->get_template_fields(); |
|
98 | + $this->_contexts = $this->_message_type->get_contexts(); |
|
99 | + } |
|
100 | + |
|
101 | + |
|
102 | + /** |
|
103 | + * Setup the _template_data property. |
|
104 | + * This method sets the _templates property array before templates are created. |
|
105 | + * |
|
106 | + * @param string $template_pack This corresponds to a template pack class reference which will contain information |
|
107 | + * about where to obtain the templates. |
|
108 | + * |
|
109 | + */ |
|
110 | + final private function _set_templates($template_pack) |
|
111 | + { |
|
112 | + |
|
113 | + // get the corresponding template pack object (if present. If not then we just load the default and add a |
|
114 | + // notice). The class name should be something like 'EE_Messages_Template_Pack_Default' where "default' would be |
|
115 | + // the incoming template pack reference. |
|
116 | + $class_name = 'EE_Messages_Template_Pack_' . str_replace(' ', '_', ucwords(str_replace('_', ' ', $template_pack))); |
|
117 | + |
|
118 | + if (! class_exists($class_name)) { |
|
119 | + EE_Error::add_error( |
|
120 | + sprintf( |
|
121 | + esc_html__( |
|
122 | + 'The template pack represented by a class corresponding to "%1$s" does not exist. Likely the autoloader for this class has the wrong path or the incoming reference is misspelled. The default template pack has been used to generate the templates instead.', |
|
123 | + 'event_espresso' |
|
124 | + ), |
|
125 | + $class_name |
|
126 | + ), |
|
127 | + __FILE__, |
|
128 | + __FUNCTION__, |
|
129 | + __LINE__ |
|
130 | + ); |
|
131 | + $class_name = 'EE_Messages_Template_Pack_Default'; |
|
132 | + } |
|
133 | + /** @type EE_Messages_Template_Pack $template_pack */ |
|
134 | + $template_pack = new $class_name(); |
|
135 | + |
|
136 | + // get all the templates from the template pack. |
|
137 | + $this->_templates = $template_pack->get_templates($this->_messenger, $this->_message_type); |
|
138 | + } |
|
139 | + |
|
140 | + |
|
141 | + /** |
|
142 | + * Return the contexts for the message type as cached on this instance. |
|
143 | + * @return array |
|
144 | + */ |
|
145 | + public function get_contexts() |
|
146 | + { |
|
147 | + return $this->_contexts; |
|
148 | + } |
|
149 | + |
|
150 | + |
|
151 | + |
|
152 | + |
|
153 | + |
|
154 | + /** |
|
155 | + * public facing create new templates method |
|
156 | + * |
|
157 | + * @return mixed (array|bool) success array or false. |
|
158 | + */ |
|
159 | + public function create_new_templates() |
|
160 | + { |
|
161 | + $template_pack = 'default'; |
|
162 | + // if we have the GRP_ID then let's use that to see if there is a set template pack and use that for the new templates. |
|
163 | + if (! empty($this->_GRP_ID)) { |
|
164 | + $message_template_group = $this->_message_template_group_model->get_one_by_ID($this->_GRP_ID); |
|
165 | + $template_pack = $message_template_group instanceof EE_Message_Template_Group |
|
166 | + ? $message_template_group->get_template_pack_name() |
|
167 | + : 'default'; |
|
168 | + // we also need to reset the template variation to default |
|
169 | + $message_template_group->set_template_pack_variation('default'); |
|
170 | + } |
|
171 | + return $this->_create_new_templates($template_pack); |
|
172 | + } |
|
173 | + |
|
174 | + |
|
175 | + |
|
176 | + |
|
177 | + |
|
178 | + /** |
|
179 | + * Handles creating new default templates. |
|
180 | + * |
|
181 | + * @param string $template_pack This corresponds to a template pack class reference |
|
182 | + * which will contain information about where to obtain the templates. |
|
183 | + * @return mixed (array|bool) success array or false. |
|
184 | + */ |
|
185 | + protected function _create_new_templates($template_pack) |
|
186 | + { |
|
187 | + |
|
188 | + $this->_set_templates($template_pack); |
|
189 | + |
|
190 | + // necessary properties are set, let's save the default templates |
|
191 | + if (empty($this->_GRP_ID)) { |
|
192 | + $main_template_data = array( |
|
193 | + 'MTP_messenger' => $this->_messenger->name, |
|
194 | + 'MTP_message_type' => $this->_message_type->name, |
|
195 | + 'MTP_is_override' => 0, |
|
196 | + 'MTP_deleted' => 0, |
|
197 | + 'MTP_is_global' => 1, |
|
198 | + 'MTP_user_id' => EEH_Activation::get_default_creator_id(), |
|
199 | + 'MTP_is_active' => 1, |
|
200 | + ); |
|
201 | + // let's insert the above and get our GRP_ID, then reset the template data array to just include the GRP_ID |
|
202 | + $grp_id = $this->_message_template_group_model->insert($main_template_data); |
|
203 | + if (empty($grp_id)) { |
|
204 | + return $grp_id; |
|
205 | + } |
|
206 | + $this->_GRP_ID = $grp_id; |
|
207 | + } |
|
208 | + |
|
209 | + $template_data = array( 'GRP_ID' => $this->_GRP_ID ); |
|
210 | + |
|
211 | + foreach ($this->_contexts as $context => $details) { |
|
212 | + foreach ($this->_fields as $field => $field_type) { |
|
213 | + if ($field != 'extra') { |
|
214 | + $template_data['MTP_context'] = $context; |
|
215 | + $template_data['MTP_template_field'] = $field; |
|
216 | + $template_data['MTP_content'] = $this->_templates[ $context ][ $field ]; |
|
217 | + |
|
218 | + $MTP = $this->_message_template_model->insert($template_data); |
|
219 | + if (! $MTP) { |
|
220 | + EE_Error::add_error( |
|
221 | + sprintf( |
|
222 | + esc_html__( |
|
223 | + 'There was an error in saving new template data for %1$s messenger, %2$s message type, %3$s context and %4$s template field.', |
|
224 | + 'event_espresso' |
|
225 | + ), |
|
226 | + $this->_messenger->name, |
|
227 | + $this->_message_type->name, |
|
228 | + $context, |
|
229 | + $field |
|
230 | + ), |
|
231 | + __FILE__, |
|
232 | + __FUNCTION__, |
|
233 | + __LINE__ |
|
234 | + ); |
|
235 | + return false; |
|
236 | + } |
|
237 | + } |
|
238 | + } |
|
239 | + } |
|
240 | + |
|
241 | + return array( |
|
242 | + 'GRP_ID' => $this->_GRP_ID, |
|
243 | + 'MTP_context' => key($this->_contexts) |
|
244 | + ); |
|
245 | + } |
|
246 | 246 | } |
@@ -113,9 +113,9 @@ discard block |
||
113 | 113 | // get the corresponding template pack object (if present. If not then we just load the default and add a |
114 | 114 | // notice). The class name should be something like 'EE_Messages_Template_Pack_Default' where "default' would be |
115 | 115 | // the incoming template pack reference. |
116 | - $class_name = 'EE_Messages_Template_Pack_' . str_replace(' ', '_', ucwords(str_replace('_', ' ', $template_pack))); |
|
116 | + $class_name = 'EE_Messages_Template_Pack_'.str_replace(' ', '_', ucwords(str_replace('_', ' ', $template_pack))); |
|
117 | 117 | |
118 | - if (! class_exists($class_name)) { |
|
118 | + if ( ! class_exists($class_name)) { |
|
119 | 119 | EE_Error::add_error( |
120 | 120 | sprintf( |
121 | 121 | esc_html__( |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | { |
161 | 161 | $template_pack = 'default'; |
162 | 162 | // if we have the GRP_ID then let's use that to see if there is a set template pack and use that for the new templates. |
163 | - if (! empty($this->_GRP_ID)) { |
|
163 | + if ( ! empty($this->_GRP_ID)) { |
|
164 | 164 | $message_template_group = $this->_message_template_group_model->get_one_by_ID($this->_GRP_ID); |
165 | 165 | $template_pack = $message_template_group instanceof EE_Message_Template_Group |
166 | 166 | ? $message_template_group->get_template_pack_name() |
@@ -206,17 +206,17 @@ discard block |
||
206 | 206 | $this->_GRP_ID = $grp_id; |
207 | 207 | } |
208 | 208 | |
209 | - $template_data = array( 'GRP_ID' => $this->_GRP_ID ); |
|
209 | + $template_data = array('GRP_ID' => $this->_GRP_ID); |
|
210 | 210 | |
211 | 211 | foreach ($this->_contexts as $context => $details) { |
212 | 212 | foreach ($this->_fields as $field => $field_type) { |
213 | 213 | if ($field != 'extra') { |
214 | 214 | $template_data['MTP_context'] = $context; |
215 | 215 | $template_data['MTP_template_field'] = $field; |
216 | - $template_data['MTP_content'] = $this->_templates[ $context ][ $field ]; |
|
216 | + $template_data['MTP_content'] = $this->_templates[$context][$field]; |
|
217 | 217 | |
218 | 218 | $MTP = $this->_message_template_model->insert($template_data); |
219 | - if (! $MTP) { |
|
219 | + if ( ! $MTP) { |
|
220 | 220 | EE_Error::add_error( |
221 | 221 | sprintf( |
222 | 222 | esc_html__( |
@@ -8,7 +8,7 @@ |
||
8 | 8 | <tr class="item"> |
9 | 9 | <td>[LINE_ITEM_NAME][LINE_ITEM_TAXABLE_*]</td> |
10 | 10 | <td colspan="2">[LINE_ITEM_DESCRIPTION] |
11 | - <p class="ticket-note"><?php echo sprintf(esc_html__('This ticket can be used once at %s of the dates/times below.', 'event_espresso'), '[TKT_USES_* schema=' . esc_html__('any', 'event_espresso') . ']'); ?></p> |
|
11 | + <p class="ticket-note"><?php echo sprintf(esc_html__('This ticket can be used once at %s of the dates/times below.', 'event_espresso'), '[TKT_USES_* schema='.esc_html__('any', 'event_espresso').']'); ?></p> |
|
12 | 12 | </td> |
13 | 13 | <td class="item_c">[LINE_ITEM_QUANTITY]</td> |
14 | 14 | <td class="item_c">[LINE_ITEM_AMOUNT]</td> |